Skip to content

cDAC fails to resolve generic instantiations with type arguments from a collectible ALC #130143

Closed

Description

@steveisok

Component

Microsoft.Diagnostics.DataContractReader (cDAC) — RuntimeTypeSystem contract / DBI legacy layer.

Summary

When cDAC reconstructs a loaded generic instantiation (GetConstructedType for ELEMENT_TYPE_GENERICINST), it searches for the instantiation only in the AvailableParamTypes table of the generic definition's loader module. The runtime, however, assigns a generic instantiation to a loader module computed from all of its type arguments (ClassLoader::ComputeLoaderModule). When a type argument's loader module differs from the definition's — most notably when an argument lives in a collectible AssemblyLoadContext — the instantiation is registered in that other module's table, so cDAC looks in the wrong place and fails to resolve the type. The legacy DAC resolves it correctly, so this is a cDAC-vs-DAC divergence.

Affected code

src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/RuntimeTypeSystem_1.cs

// GetConstructedType (~line 1262)
loaderModule = GetLoaderModule(typeHandle);          // typeHandle = the OPEN generic definition
ModuleHandle moduleHandle = loaderContract.GetModuleHandleFromModulePtr(loaderModule);
foreach (TargetPointer ptr in loaderContract.GetAvailableTypeParams(moduleHandle))  // searches ONE module
{
    if (corElementType == CorElementType.GenericInst &&
        GenericInstantiationMatch(typeHandle, potentialMatch, typeArguments) && IsLoaded(potentialMatch))
        return potentialMatch;
    ...
}
return new TypeHandle(TargetPointer.Null);           // not found → caller sees "class not loaded"

GetLoaderModule(TypeHandle) (~line 1420) for a MethodTable returns the type's own stored loader module (the definition's module); it does not consider type arguments.

Expected behavior

The loaded instantiation should be located using the same loader-module algorithm the runtime uses to register it — ClassLoader::ComputeLoaderModuleWorker in src/coreclr/vm/clsload.cpp:98:

  • Start from the definition module.
  • If any class/method type argument is in a collectible module, select the module of the latest-created collectible loader allocator among the arguments (clsload.cpp:156-193).
  • Otherwise fall back to the definition module (CoreLib when null).

cDAC should compute the loader module from the type arguments (mirroring ComputeLoaderModuleWorker) before searching GetAvailableTypeParams, or otherwise search the correct module's table.

Actual behavior

GetConstructedType searches only GetLoaderModule(definition)'s AvailableParamTypes. For an instantiation whose real loader module is an argument's (collectible/ALC) module, the match is not found and GetConstructedType returns a null TypeHandle. Downstream this surfaces as a failure to resolve the type / read fields of such an object under the debugger, where the legacy DAC succeeds.

Impact

Debugging (ICorDebug / DBI over cDAC, DOTNET_ENABLE_CDAC=1) of any object whose type is a generic instantiation with a type argument from a different — especially collectible/AssemblyLoadContext — assembly. Reproduces cleanly with async state machines, because the compiler-generated state-machine struct is often defined in a different assembly than AsyncTaskMethodBuilder/Task<T>, producing AsyncStateMachineBox<TResult, TStateMachine> where TStateMachine comes from another (collectible) ALC. Inspecting such a box's fields (e.g. StateMachine) via ICorDebugObjectValue::GetFieldValue fails under cDAC while succeeding under the legacy DAC.

Repro sketch

  1. App loads an assembly into a collectible AssemblyLoadContext that defines an async method (its state machine struct lives in that ALC's assembly).
  2. Break while such a task is live and have the debugger inspect the async continuation / the AsyncStateMachineBox<…, StateMachineFromOtherAssembly> object's fields.
  3. Run once with DOTNET_ENABLE_CDAC=0 (works) and once with DOTNET_ENABLE_CDAC=1 (fails to resolve the instantiation).

Suggested fix

Implement the ComputeLoaderModule algorithm in the cDAC RuntimeTypeSystem contract (type-argument-driven, with the collectible "latest loader allocator" rule) and use it in GetConstructedType to choose which module's GetAvailableTypeParams to search — instead of GetLoaderModule(definition). A DumpTests case using a generic instantiated with a type argument from a collectible ALC would guard the regression.

Notes

  • Discovered via post-mortem analysis of a debugger-host crash while inspecting an async state machine whose StateMachine field type came from another assembly; ICorDebugObjectValue::GetFieldValue returned E_INVALIDARG under cDAC. The exact HRESULT depends on which consumer receives the unresolved type (the type-reconstruction path itself yields CORDBG_E_CLASS_NOT_LOADED); the common root is the loader-module mismatch above.

Activity

  1. added
    untriaged
    on Jul 2, 2026
  2. added and removed
    untriaged
    on Jul 2, 2026
  3. dotnet-policy-service commented on Jul 2, 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.

  4. self-assigned this
    on Jul 2, 2026
  5. added a commit that references this issue on Jul 9, 2026
  6. added a commit that references this issue on Jul 15, 2026
  7. locked and limited conversation to collaborators on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions