Repository navigation
Description
Description
Since upgrading to .NET 9, I get frequent debugger crashes when pausing in both Debug and Release configurations for my dictionary container. I haven't been able to notice any particular pattern to it.
Reproduction Steps
Run the 'Benchmarks' project from https://github.com/kg/SimdDictionary/ and hit Pause in VS during the self-test period while it is testing TailCollider, then make sure the Autos or Locals tab(s) are visible in the debugger. Local evaluation will fail and the application process will collapse if VectorizedDictionary code is currently running. Pausing in Main works though. This is not 100% reproducible - setting a breakpoint in a specific spot doesn't reproduce it every time - but if I run the self test and pause repeatedly it will eventually crash while evaluating locals. It seems to happen even if the autos/locals tabs aren't visible.
Expected behavior
No application crash even if local evaluation fails.
Actual behavior
Local evaluation fails and the application crashes.
---------------------------
Microsoft Visual Studio
---------------------------
The target process exited with code -1073740791 (0xC0000409) while evaluating the function ''.
If the problem happens regularly, consider disabling the Tools->Options setting "Debugging->General->Enable property evaluation and other implicit function calls" or debugging the cause by evaluating the expression from the Immediate window. See help for information on doing this.
---------------------------
OK Help
---------------------------
Regression?
I was able to get debugger crashes on .NET8 but it seems much much worse on 9. Could be a regression from upgrading VS, I'm not sure.
Known Workarounds
It looks like this could be related to some locals being NullRefs, so it's possible the workaround is to not use ref and Unsafe.NullRef at all, but that doesn't seem particularly reasonable.
Configuration
Visual Studio 2022 Version 17.12.3
PS C:\Users\kg> dotnet --list-runtimes
Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
x64 on a Ryzen 7950X
Other information
Sometimes it says evaluation failed for 'pair.Value' (but I don't have a watch on that, so what gives?)
Pair is defined like so:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct Pair {
public K Key;
public V Value;
}

Activity
dotnet-policy-service commentedon Dec 9, 2024
Tagging subscribers to this area: @tommcdon
See info in area-owners.md if you want to be subscribed.
kg commentedon Dec 9, 2024
I'm happy to investigate this further locally if someone can give me tips on how to troubleshoot it. I'm kind of stumped because the process just goes away, I don't know how to get it into a mode where I can see what code is crashing during evaluation or where.
EDIT: I tried native debugging and it's worse, there's not even a popup, just this in the log:
EDIT 2: The suggested "native-only debugging" does not seem to be an option in VS2022's UI anymore :(
EDIT 3: This suggests it's related to Vector128:
In retrospect, I've had problems with the debugger displaying incorrect values for Vector128s in the past while working on this, so it might be the problem.
tommcdon commentedon Dec 10, 2024
Thanks for the bug report and investigation @kg! I was not able to reproduce on my machine (Intel I7), and so it is possible that it doesn't have all of the vectorization support needed to reproduce the bug.
As for investigating, could you try setting a conditional breakpoint in
ShimProcess::HandleWin32DebugEvent, located in coreclr\debug\di\shimprocess.cpp, on the "IsExceptionEvent" line, with the following condition:pEvent->dwDebugEventCode == 1 && pEvent->u.Exception.ExceptionRecord.ExceptionCode == 0xc0000409? Then non-invasively attach windbg to the debuggee and dump the callstack (e.g. with thekcommand).kg commentedon Dec 10, 2024
Do I need to set that breakpoint in the debuggee or inside of VS?
ShimProcess::HandleWin32DebugEvent's not resolving even with symbols loaded. I tried attaching windbg to msvsmon to see if I could set a BP on ShimProcess there and that didn't work either.EDIT: From digging around by using a custom #include in a source server file,
di\shimprocess.cppisn't part of the symbolstommcdon commentedon Dec 10, 2024
@kg - to stop on the 0xC0000409 error, we need to attach a second instance of VS to the first instance of VS debugging the app. When you F5 the app on the first instance, it will load mscordbi.dll which contains the
ShimProcess::HandleWin32DebugEventfunction. I usually set a C++ function breakpoint then, when it hits it, I clear it and then set the conditional line breakpoint at the appropriate place.kg commentedon Dec 10, 2024
Main thread stack:
I think maybe I need to disable tiered compilation to get a workable stack here... none of the other threads seem to be doing anything.
EDIT: Hmm, started it with TieredCompilation set to 0 and the managed frames are still bare symbols.
kg commentedon Dec 10, 2024
Got it:
Edit: Reproed again, another different !DumpStack trace:
tommcdon commentedon Dec 10, 2024
@kg - thanks for the call stacks! The first one (from main), indicates that we were dispatching an APC. @thaystg recently produced a bug fix related to APC's here - #110533. It might be worth testing with the fix (I suggest a retail build as VS can trigger various asserts on debug builds). Note that if you try a private runtime build, please see https://aka.ms/vs/unsigned-dotnet-debugger-lib.
janvorli commentedon Dec 11, 2024
@kg the 0xC0000409 is most likely a CET related issue. When the crash occurs, it would be helpful to dump the shadow stack too, it may reveal the culprit. You can use e.g.
dps @sspto do that.kg commentedon Dec 11, 2024
From the minidump I took:
janvorli commentedon Dec 11, 2024
@kg can you please dump the regular stack trace too? This doesn't seem to match the return addresses on the regular stack you've dumped in the comment #110552 (comment) above, so I assume it is a different run.
janvorli commentedon Dec 11, 2024
It would be also better to use
!clrstack -fto dump the stack trace so that we can see the managed methods too.kg commentedon Dec 11, 2024
They match the 'another dumpstack trace' from the previous comment and are from a minidump I captured. Maybe minidumps don't work right?
!clrstack -fdoesn't seem to work.kg commentedon Dec 11, 2024
Attempting to repro again:
Exception thrown at 0x00007FFEB0FB7F28 (Microsoft.CodeAnalysis.ExpressionEvaluator.ResultProvider.ni.dll) in devenv.exe: 0xC0000005: Access violation reading location 0x0000000000000000.On the second try I got a repro.
dps @ssp:!clrstack -f!DumpStackI have a minidump of this instance if you want it.
janvorli commentedon Dec 11, 2024
@kg hmm, the shadow stack seems to match the real one, so that's strange. Please share the dump, I'll take a look to see if I can spot something.
kg commentedon Dec 11, 2024
Wouldn't upload as a github attachment so here's a dropbox link.
https://www.dropbox.com/scl/fi/6n1hvmla5j9ep1vevc7e7/debugee.zip?rlkey=hzeawe6xtbyvs0lyte9iq0een&dl=0
janvorli commentedon Dec 11, 2024
I can see what's wrong. The special user mode APC that we use for runtime suspension was just about to return (it restores a context to do that), but the Rip in the context that it wants to restore to doesn't match the frame below the
KiUserApcDispatch, but it is the address ofFuncEvalHijack. So, it seems there is a race / bug between the funceval hijacking and the special user mode APC.The special user mode APC works as follows. When we want to suspend a runtime, we send the user mode APC to all threads using the
QueueUserAPC2Windows API. Windows kernel then redirects the target thread toKiUserApcDispatchthat calls ourApcActivationCallbackStubwhich further callsThread::ApcActivationCallback. The callback in the case that happened in your run ends up waiting until the runtime is resumed again and then it returns back to the managed code.The funceval hijack is performed in
Debugger::FuncEvalSetup. It occurs when the runtime is suspended, and it patches the Rip in the context of the suspended thread to theFuncEvalHijack. I am not 100% sure on how it works further, but it seems that in our case, we resume the runtime with theFuncEvalHijackin the context. When CET is enabled, Windows don't allow setting a context to a location that's not on the shadow stack (or in a list of registered allowed continuation locations in the executable - used for EH or it was not registered by a call toSetProcessDynamicEHContinuationTargets, which can only be called out of process).Maybe our debugger would need to register the
FuncEvalHijackvia theSetProcessDynamicEHContinuationTargetsto fix the problem, but I'll let diagnostics folks to reason about this.cc: @tommcdon
ato321 commentedon Jan 6, 2025
I am not sure if this is same problem, but I have noticed under .NET9 step-by-step debugging fails if any other thread throws a exception (user handled). I made a small test program and if you put the break line after the thread starting, then you would notice that sub-sequent steps would not be breaking anymore after exception thrown in the running thread.
However, changing this project to .NET8 makes debugging work just fine.