Skip to content

Crash when calling a certain DllImport method while debugger is attached on Linux #116392

Closed as duplicate of#43147
@Exanite

Description

@Exanite

Description

Context:
I'm not sure if this is a duplicate of another issue, but it's not the first issue I've reported where the issue is specific to crashing while the debugger is attached on Linux.

I first reported this issue to the ClangSharp repo (dotnet/ClangSharp#603); however, Tanner Gooding replied to me in the Silk.NET Discord that this is likely a runtime/debugger issue. This is why I'm opening the issue here.

I first discovered this issue while working on Silk.NET 3's new bindings generator, which uses ClangSharp. This issue can also be reproduced there. Reproducing this issue using Silk.NET 3's codebase is a bit involved, so I've provided a much simpler reproduction repo below.

Issue description:
The program crashes when running CXIndex.Create(); from ClangSharp.PInvokeGenerator under the following conditions:

  1. Must be on Linux (Windows is fine)
  2. Debugger must be attached. Tested with both VS Code and Rider debuggers.

Note: Unlike the other issue I've reported, it does not matter whether the program is compiled in Debug or Release configuration. It also does not matter whether the env var DOTNET_LegacyExceptionHandling=1 is set or not.

Also note: CXIndex.Create(); calls clang.createIndex(0, 0). This second method is the DllImport method.

Reproduction Steps

Run the program in the following repo:
https://github.com/Exanite/ClangSharpLinuxCrashRepro

Make sure the conditions for the crash are met (see above) and observe that the program crashes. The line the debugger should break on will be the CXIndex.Create(); line.

Alternatively, put a breakpoint on the CXIndex.Create(); line and the line immediately after. Run the program with the debugger attached. The debugger should pause execution on CXIndex.Create();, resuming execution should immediately crash the program.

On .NET 8 and .NET 9 with DOTNET_LegacyExceptionHandling=1, I get the following exception:

Fatal error. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
   at System.GC.RunFinalizers()

On .NET 8 and .NET 9 without DOTNET_LegacyExceptionHandling=1, I get the following exception:

Process terminated. InternalError
   at System.Environment.FailFast(System.Runtime.CompilerServices.StackCrawlMarkHandle, System.String, System.Runtime.CompilerServices.ObjectHandleOnStack, System.String)
   at System.Environment.FailFast(System.Threading.StackCrawlMark ByRef, System.String, System.Exception, System.String)
   at System.Environment.FailFast(System.String)
   at System.Runtime.EH.FallbackFailFast(System.Runtime.RhFailFastReason, System.Object)
   at System.Runtime.EH.RhThrowHwEx(UInt32, ExInfo ByRef)
   at System.GC.RunFinalizers()

On .NET 10 preview (10.0.100-preview.4.25258.110), I get the following exception:

System.ExecutionEngineException: Illegal instruction: Attempted to execute an instruction code not defined by the processor.
   at System.GC.RunFinalizers()

Expected behavior

The program does not crash and completes execution successfully.

Actual behavior

See reproduction steps section.

Regression?

I've tested on .NET 8, 9, and 10 preview, so it doesn't seem like a regression.

Known Workarounds

None that I know of.

Configuration

OS:
Arch Linux x64

.NET version:
Tested on .NET 8, .NET 9, and .NET 10 preview (10.0.100-preview.4.25258.110). Crashes on all three.
Note the exception is different on .NET 10. See the repro section for more details.

Debugger used:
Tested with Rider and VS Code. Crashes on both.

ClangSharp.PInvokeGenerator version:
Occurs on all ClangSharp.PInvokeGenerator versions I've tried.
This includes the newest version, oldest version, and some versions in between. I list the exact versions in the .csproj file of the repro repo.

Clang version:

# clang --version
clang version 19.1.7
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Setting LD_DEBUG=libs shows that the resolved clang shared library is /usr/lib/libclang.so, which symlinks to libclang.so.19.1, so the version I got from the command line should be accurate.

CPU
Ryzen 9 5900X

Other information

This is a similar issue that I reported:
#116153

I first reported this issue to the ClangSharp repo (dotnet/ClangSharp#603), but Tanner Gooding replied to me in Discord that this is likely a runtime/debugger issue.

What I find weird is that this crash is reliably triggered when calling CXIndex.Create();.
I'm not familiar with the ClangSharp API so I haven't tested the API much further.

CXIndex.Create(); calls clang.createIndex(0, 0). This second method is the DllImport method. Both of these lines crash.

The only other method I've tested is clang.createCXCursorSet(), which is also another DllImport method. This method works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions