runtime: Crashes during debugging when pausing in VS2022/.NET9

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.

Image Image

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;
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (17 by maintainers)

Most upvoted comments

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 of FuncEvalHijack. 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 QueueUserAPC2 Windows API. Windows kernel then redirects the target thread to KiUserApcDispatch that calls our ApcActivationCallbackStub which further calls Thread::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 the FuncEvalHijack. I am not 100% sure on how it works further, but it seems that in our case, we resume the runtime with the FuncEvalHijack in 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 to SetProcessDynamicEHContinuationTargets, which can only be called out of process).

Maybe our debugger would need to register the FuncEvalHijack via the SetProcessDynamicEHContinuationTargets to fix the problem, but I’ll let diagnostics folks to reason about this.

cc: @tommcdon

@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::HandleWin32DebugEvent function. 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.