Skip to content

Debugger func-eval can use stale non-leaf register values after GC #135249

Closed

Description

@jkoritzinsky

Description

CoreCLR debugger func-eval can use a stale serialized register value when an argument is a register-resident value type from a non-leaf managed frame and the value type contains an object reference or managed byref.

Source analysis was performed against current main commit f5f66b5f3ceadcd7daca3098b48ba5c8930f01f7 (2026-10-05). The relevant lifetime is:

  1. DBI snapshots a non-leaf register rather than retaining an updatable location. CordbNativeFrame::GetLeftSideAddressOfRegister returns kNonLeafFrameRegAddr for every non-leaf frame (rsthread.cpp#L6414-L6424). RegValueHome simultaneously captures the register bits in m_regValue (rspriv.h#L7788-L7794), and CopyToIPCEType serializes the marker plus reg1Value (valuehome.cpp#L29-L35). CordbEval::GatherArgInfo does this for enregistered ELEMENT_TYPE_VALUETYPE arguments (rsthread.cpp#L9280-L9314), then SendFuncEval copies those bytes into the Left Side's DebuggerEval::m_argData buffer (rsthread.cpp#L9397-L9433). There is no later write that refreshes the serialized reg1Value/reg2Value; the EE only reads them.

  2. The native bootstrap establishes normal interrupted-frame reporting, but not a root for the serialized copy. FuncEvalHijackWorker pushes a FuncEvalFrame before starting the eval (funceval.cpp#L3816-L3897). FuncEvalFrame::UpdateRegDisplay exposes the saved interrupted leaf context in DebuggerEval::m_context as mutable register locations for stack walking and GC (debugger.cpp#L15933-L15987). A GC can therefore walk the interrupted managed stack and update the actual root location reconstructed for a non-leaf frame, whether that location is a saved context slot or a stack spill.

  3. The initial argument-protection pass omits an enregistered value type. GCProtectArgsAndDoNormalFuncEval allocates protected object/interior arrays and pushes a ProtectValueClassFrame, then calls GCProtectAllPassedArgs in a no-GC region before any type resolution (funceval.cpp#L3485-L3574). For ELEMENT_TYPE_VALUETYPE, however, that pass only records a location when argAddr != 0; an enregistered value type has argAddr == 0, so its register bits are neither copied nor protected (funceval.cpp#L1509-L1519). The ProtectValueClassFrame list is still empty for this argument.

  4. GC-capable work occurs before the register value is materialized. DoNormalFuncEval immediately enters a preemptive region and calls ResolveFuncEvalGenericArgInfo (funceval.cpp#L3199-L3233). That function is GC_TRIGGERS and performs type-handle resolution, LoadMethodDef, method/type activation, class initialization, and potentially instantiation loading (funceval.cpp#L1659-L1723); TypeDataWalk::ReadTypeHandle is itself GC_TRIGGERS and may load array, pointer/byref, and instantiated value types (debugger.cpp#L11789-L11838). Method/class activation and cctors can also allocate or run managed code. Further GC-capable boxing and return-object allocation occur before packing (funceval.cpp#L3285-L3372).

  5. The non-leaf snapshot is consumed only after that GC window. CopyArgsToBuffer explicitly does nothing for value types (funceval.cpp#L2271-L2408). Only while PackArgumentArray is already in GCX_FORBID does GetFuncEvalArgValue materialize an enregistered value type (funceval.cpp#L2634-L2653, funceval.cpp#L816-L932). GetRegisterValue distinguishes the two cases: a non-leaf marker returns the serialized regValue, while a leaf marker reads DebuggerEval::m_context (funceval.cpp#L261-L275).

This creates the stale-reference mechanism: a compacting GC after the early protection pass updates the original interrupted-frame root through normal stack walking, but it cannot update the separate scalar copy in m_argData[].argHome.reg1Value. Late packing then copies the pre-GC object reference or interior byref into the invoked method's argument.

Leaf CPU-context slots are different and appear safe for this specific problem: kLeafFrameRegAddr causes the late read to use pDE->m_context, which is the storage exposed by FuncEvalFrame::UpdateRegDisplay and is GC-updated. The unsafe path is the effectively immutable serialized value selected by kNonLeafFrameRegAddr.

No invariant prevents the input shape. JIT VLT_REG locations are handed to GetLocalRegisterValue without a value-type GC-layout exclusion (rsthread.cpp#L8325-L8352); CordbValue::CreateValueByType supports ELEMENT_TYPE_VALUETYPE with a register home (divalue.cpp#L230-L263). The DBI comment only assumes an enregistered value class has a single field (valuehome.cpp#L824-L830); a pointer-sized struct with one object-reference field or one managed-byref field satisfies that assumption. GatherArgInfo has no ContainsGCPointers or byref-like rejection for value-type arguments.

Reproduction Steps

No deterministic public repro was run for this report. The conclusion above is from the current-main source lifetime, not from the in-progress managed func-eval branch or from a clean runtime A/B experiment.

A deterministic debugger-harness regression test should:

  1. Stop at a GC-safe point in a deeper managed method while a non-leaf frame has a live pointer-sized value-type local in a register. Test both a struct with one object-reference field and a byref-like struct with one managed-byref field.
  2. Obtain that non-leaf local through ICorDebugILFrame and pass its ICorDebugValue as an argument to ICorDebugEval::CallFunction.
  3. Inject a compacting GC after the func-eval native bootstrap and GCProtectAllPassedArgs, but before PackArgumentArray performs the late register read. Arrange for the referenced object to move.
  4. Have the evaluated method verify the received object identity or byref target/address.
  5. Include leaf-register and memory-home controls. The leaf case should use the GC-updated DebuggerEval::m_context; the memory-home case should reread updated storage.

The test needs a JIT/debugger hook or controlled code shape to guarantee the non-leaf VLT_REG home and the GC timing.

Expected behavior

All object references and managed byrefs contained in a func-eval argument remain live and are updated to their post-GC locations before the argument is passed to the evaluated method, regardless of whether the debugger obtained the value from a leaf or non-leaf frame.

Actual behavior

For a non-leaf register-resident value type, a compacting GC in the resolution/activation/boxing window can update the original interrupted-frame home but leave the serialized regValue unchanged. The evaluated method can receive a stale object reference or stale interior pointer. This can produce a wrong object/value, an invalid byref, or a debuggee crash/corruption. No crash or corruption is claimed as observed for this report.

Regression?

Not established. This appears to be an existing lifetime gap in current main; no regressing commit was identified or bisected.

Known Workarounds

No general runtime workaround is known. A debugger can avoid passing non-leaf enregistered value-type values containing object references or managed byrefs, or first materialize an equivalent value in GC-tracked/memory-backed storage when possible.

Configuration

  • Source: current dotnet/runtime main, commit f5f66b5f3ceadcd7daca3098b48ba5c8930f01f7 (2026-10-05).
  • Component: CoreCLR native debugger / ICorDebugEval.
  • x64 is a concrete architecture for a pointer-sized single-register case. The marker, serialization, early-protection, and late-read logic is shared, but other architectures were not independently executed.
  • No end-to-end runtime repro was run.

Other information

Suggested fix direction:

  • Capture and GC-protect register-resident value types before the first GC-capable work, using their exact layout, or register the serialized storage itself in place once an exact no-load layout is available.
  • Ensure the initial protection step cannot itself require type loading or allocation before the values are rooted.
  • Preserve the distinction between leaf context-backed register slots and non-leaf serialized values.
  • Avoid reporting the same storage slot twice if protection is integrated with existing interrupted-frame reporting.
  • Keep late argument packing in a no-GC region, but do not rely on that region to repair an earlier stale snapshot.

Focused open-and-closed issue searches for debugger func eval non-leaf register GC stale value type, serialized regValue GC root, and nonleaf frame func-eval struct found no duplicate. Related but distinct issues are:

Note

This issue was drafted with GitHub Copilot.

Activity

  1. dotnet-policy-service commented on Oct 5, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
    See info in area-owners.md if you want to be subscribed.

  2. added
    untriaged
    on Oct 5, 2026
  3. removed
    untriaged
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions