Libgit2sharp: AccessViolationException on `RetrieveStatus`

Created on 9 Jun 2021  路  15Comments  路  Source: libgit2/libgit2sharp

I'm experiencing an issue which presumably originates from an update of git on my device (updated Visual Studio), but causes AccessViolationException in libgit2sharp.

Reproduction steps

This happens with all repos on my device, regardless of what state the repo is in, it seems:

var repo = new Repository("path/to/some/repo");
var status = repo.RetrieveStatus(new StatusOptions { Show = StatusShowOption.IndexAndWorkDir });

The second line throws the System.AccessViolationException (trace, see below)

Expected behavior

No exception.

Actual behavior

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at LibGit2Sharp.Core.NativeMethods.git_status_list_new(LibGit2Sharp.Core.git_status_list* ByRef, LibGit2Sharp.Core.git_repository*, LibGit2Sharp.Core.GitStatusOptions)
--------------------------------
   at LibGit2Sharp.Core.Proxy.git_status_list_new(LibGit2Sharp.Core.Handles.RepositoryHandle, LibGit2Sharp.Core.GitStatusOptions)
   at LibGit2Sharp.RepositoryStatus..ctor(LibGit2Sharp.Repository, LibGit2Sharp.StatusOptions)
   at MyCode()

Version of LibGit2Sharp (release number or SHA1)

I'm on version 0.27.0-preview-0096.

Operating system(s) tested; .NET runtime tested

> git --version
git version 2.31.1.windows.1
> dotnet --version
5.0.301



md5-773b3aa60bedbba29da4dce5920f5de0



> dotnet --list-sdks
2.1.816 [C:\Program Files\dotnet\sdk]
3.1.409 [C:\Program Files\dotnet\sdk]
5.0.100 [C:\Program Files\dotnet\sdk]
5.0.104 [C:\Program Files\dotnet\sdk]
5.0.201 [C:\Program Files\dotnet\sdk]
5.0.203 [C:\Program Files\dotnet\sdk]
5.0.301 [C:\Program Files\dotnet\sdk]



md5-773b3aa60bedbba29da4dce5920f5de0



> dotnet --list-runtimes
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Please let me know if you need further information to debug this.

Thanks
Stefan

Most helpful comment

I debugged into the native code and saw that the GitStatusOptions passed to git_status_list_new look different when comparing .NET Runtime v5.0.7 and v5.0.6:

With v5.0.6 the baseline field is NULL where with v5.0.7 it is no longer null. Therefor the code walk sets a different head value, see:
https://github.com/libgit2/libgit2/blob/868f4bcb4d3290f4b5320f030fccdf1e7fc8ac8a/src/status.c#L285

When manually moving the execution to the else branch, the code works as expected and without further errors

All 15 comments

I've done a few more things to identify the root cause of this.

  • Ran the test suite of this project: Two tests fail, but I suspect they fail for completely unrelated issues (which is that the directory I'm running them in contains both a space and a diacritic). So nothing helpful there.
  • Tried different versions of this library:

    • 0.25.0 and later fail

    • 0.24.1 and earlier don't fail

This is the change where netstandard2.0 support was introduced, so that's most likely an issue with the different TFM OR with a change in LibGit2Sharp.NativeBinaries between v1.0.196 and v1.0.210. That looks to be the commit https://github.com/libgit2/libgit2sharp.nativebinaries/commit/3ee0aa73e3e22dc8f069d112220b9cd7bdea6390, where in turn it seems like the changes are essentially just the changes from libgit2 https://github.com/libgit2/libgit2/compare/2d2a60258f83b937a9a840ae1bea4968492b099e...6311e886d8b5377c6037cd9937ccf66a71f3361d.

Why any of those changes might cause this crash is beyond me, as I'm unfamiliar with that code base, if it truly is from that change anyway.

As said before, I'm happy to provide more information / try more things. This library is of great value to me, so I naturally would love to see this issue resolved without downgrading to an older version.

Thanks
Stefan

We ran into the same issue after an update of Visual Studio to v16.10.1 - which updated the .NET Runtime to version 5.0.7.

I published our app as a self-contained executable and pinned the .NET Runtime version to 5.0.6 via a global.json file.

{
  "sdk": {
    "version": "5.0.300"
  }
}

With this change the Exception no longer appears. When going back to 5.0.7 the Exception occurs again, so it seems to be an issue with the .NET Runtime

Do you know whether the dotnet runtime team is already aware of this regression?

I have no idea, but i have not found any issue regarding this regression.
We have mostly been looking for workarounds ;)

BTW: setting the runtime version with a runtimeconfig.json file works too

I've raised an issue in the dotnet/runtime repo: https://github.com/dotnet/runtime/issues/53990

We have the same issue with 5.0.104

I noticed that using x64 does work for me

Using x64 does not work for me.

Using x64 does not work for me.

true, for me the combination of the global.json and x64 worked. Just one of them didn't do the trick.
I also added <RuntimeFrameworkVersion>5.0.4</RuntimeFrameworkVersion> in my csproj

I debugged into the native code and saw that the GitStatusOptions passed to git_status_list_new look different when comparing .NET Runtime v5.0.7 and v5.0.6:

With v5.0.6 the baseline field is NULL where with v5.0.7 it is no longer null. Therefor the code walk sets a different head value, see:
https://github.com/libgit2/libgit2/blob/868f4bcb4d3290f4b5320f030fccdf1e7fc8ac8a/src/status.c#L285

When manually moving the execution to the else branch, the code works as expected and without further errors

Awesome, @Breakpoint21!

So it seems like the bug is the unmatched property baseline, between https://github.com/libgit2/libgit2sharp/blob/df3b22a754ef56da8d7e3c330ce2d783c2b7982e/LibGit2Sharp/Core/GitStatusOptions.cs#L6-L20
and
https://github.com/libgit2/libgit2/blob/508361401fbb5d87118045eaeae3356a729131aa/include/git2/status.h#L170-L197

The question now is whether it needs to be added in C# oder removed in C.

It seems like it, but i wonder why this is working with older version of the runtime, since the baseline field was added a while ago (libgit2 version 0.27 - january 2019) changelog

Pure "luck"... It was never guaranteed to be always null, so it was only chance that interop put null there pre 5.0.7, which has kept the bug hidden.

I just saw, that there is already a matching PR #1884

1884 fixed this issue, so I recommend people affected by this to upgrade to 0.27.0-preview-0100. If anyone here is still experiencing AccessViolationExceptions with the newest version, when working with the newest dotnet runtime, I'd suggest to post it referencing this issue, as the root cause might be very similar.

Was this page helpful?
0 / 5 - 0 ratings