Skip to content

[cDAC] Fix #130143 - search correct loader module for generic instantiations - #130158

Merged
rcj1 merged 4 commits into
mainfrom
copilot/fix-runtime-issue-130143
Jul 9, 2026
Merged

rcj1 merged 4 commits into
mainfrom
copilot/fix-runtime-issue-130143

Conversation

@rcj1

@rcj1 rcj1 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Implement correct loader module lookup algorithm in GetConstructedType. Fixes #130143.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@rcj1
rcj1 requested review from max-charlamb and noahfalk July 3, 2026 04:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the cDAC RuntimeTypeSystem contract implementation so GetConstructedType selects the loader module for ELEMENT_TYPE_GENERICINST using a type-argument-driven algorithm (mirroring CoreCLR’s ClassLoader::ComputeLoaderModuleWorker), addressing failures when type arguments come from collectible AssemblyLoadContexts. It also adjusts the design doc to reflect the updated loader-module selection approach.

Changes:

  • Compute loader module for GenericInst based on definition module + type arguments (collectible-aware), and use it when scanning AvailableTypeParams.
  • Refactor loader allocator probing into a helper (TryGetCollectibleLoaderAllocator) and consolidate loader-module computation logic.
  • Update docs/design/datacontracts/RuntimeTypeSystem.md to remove the old FnPtr-specific helper and reference the new algorithm (needs a syntax fix per review comment).
Show a summary per file
File Description
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/RuntimeTypeSystem_1.cs Implements CoreCLR-mirroring loader-module computation for constructed types, fixing generic instantiation lookup across collectible contexts.
docs/design/datacontracts/RuntimeTypeSystem.md Updates the RuntimeTypeSystem design doc snippet to match the new loader-module selection strategy (currently contains an incomplete line that should be corrected).

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Copilot Code Review

Holistic Assessment

Motivation: Justified and well-scoped. Issue #130143 documents a real cDAC-vs-DAC divergence: GetConstructedType searches the wrong module's AvailableParamTypes table for generic instantiations whose type arguments come from a different (especially collectible) loader module.

Approach: Correct. The PR introduces a unified ComputeLoaderModule that mirrors the native ComputeLoaderModuleWorker (clsload.cpp:98). The refactoring is clean — the old FindFnPtrLoaderModule is replaced by the same unified algorithm, which also fixes an incidental bug in the FnPtr non-collectible path (old code always fell back to CoreLib; native code uses the first arg's module).

Summary: ⚠️ Needs Human Review. The core algorithm faithfully mirrors the native code and the fix is correct for the reported issue. However, the design-doc update leaves broken pseudocode, and no regression test was added. A human reviewer familiar with the loader-module invariants should confirm the algorithm match and decide whether the design-doc and test gaps are acceptable for merge.


Detailed Findings

Detailed Findings

✅ Algorithm correctness — ComputeLoaderModule mirrors native ComputeLoaderModuleWorker

I compared the PR's ComputeLoaderModule against ClassLoader::ComputeLoaderModuleWorker in src/coreclr/vm/clsload.cpp across several scenarios:

Scenario Native result PR result Match?
Non-collectible definition, non-collectible args Definition module (or first arg's if def is null) Same ✅
Null definition (FnPtr), non-collectible args First arg's module, CoreLib if none Same ✅
Non-collectible definition, one collectible arg Collectible arg's module Same ✅
Collectible definition (num=3), collectible arg (num=5) Arg's module (higher creation number) Same ✅
Collectible definition (num=5), collectible arg (num=3) Definition module Same ✅
Null definition, all null args CoreLib Same ✅

The callers in GetConstructedType correctly dispatch:

  • FnPtr → ComputeLoaderModule(TargetPointer.Null, typeArguments) — matches ComputeLoaderModuleForFunctionPointer calling ComputeLoaderModuleWorker(NULL, 0, classInst, Instantiation())
  • GenericInst → ComputeLoaderModule(GetModule(typeHandle), typeArguments) — matches ComputeLoaderModuleWorker(pMT->GetModule(), token, classInst, Instantiation())
  • Array/Ptr/ByRef → GetLoaderModule(typeHandle) unchanged — matches ComputeLoaderModuleForParamType which returns paramType.GetLoaderModule() directly

The TryGetCollectibleLoaderAllocator helper is clean and correctly null-checks the module pointer, the loader allocator pointer, and the IsCollectible flag.

✅ FnPtr non-collectible case also fixed

The old FindFnPtrLoaderModule always returned CoreLib when no args were collectible. The native ComputeLoaderModuleWorker (with null definition) returns the first non-null arg's module instead (falling back to CoreLib only when pLoaderModule is still null). The unified ComputeLoaderModule now matches this native behavior, fixing a secondary divergence.

⚠️ Design document has broken pseudocode

In docs/design/datacontracts/RuntimeTypeSystem.md, line 1171:

TargetPointer loaderModule = // see [link](...)

This is an incomplete assignment — the right-hand side is a comment with a URL, not executable pseudocode. The previous version documented the actual algorithm inline and included a FindFnPtrLoaderModule method description. The design doc should either describe the new unified ComputeLoaderModule algorithm in pseudocode or at minimum have a valid placeholder (e.g., ComputeLoaderModule(...)) rather than a syntactically broken statement.

⚠️ No regression test added

The linked issue explicitly recommends: "A DumpTests case using a generic instantiated with a type argument from a collectible ALC would guard the regression." No test was added in this PR. Given that this is the kind of loader-module-selection bug that can easily regress, a DumpTests debuggee exercising the collectible ALC path (e.g., an async state machine with TStateMachine from a collectible assembly) would provide meaningful coverage. If testing is deferred, a tracking issue or TODO comment would help.

💡 Minor: misleading comment at line 1352

The comment "If nothing was found, then by default we use the definition module" (copied from the native code) is slightly misleading in context. The check if (latestLoaderModule != TargetPointer.Null) actually tests whether something was found. A clearer comment might be: "Use the latest collectible loader module if one was found; otherwise fall through with the definition module."

Note

This review was generated by GitHub Copilot.

Generated by Code Review for issue #130158 · ● 38M · ◷

@max-charlamb max-charlamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we could add a dumptest that verifies this handling. It shouldn't be too difficult to make something with multiple modules.

@rcj1

rcj1 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please add a dump test according to #130158 (review)

Copilot AI review requested due to automatic review settings July 8, 2026 18:27
@rcj1
rcj1 removed the request for review from Copilot July 8, 2026 18:27
Copilot AI review requested due to automatic review settings July 8, 2026 19:05
@rcj1
rcj1 removed the request for review from Copilot July 8, 2026 19:05

Copilot AI commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

@copilot please add a dump test according to #130158 (review)

Added a dump test for this scenario. New debuggee CollectibleGenericInst loads a copy of its own assembly into a collectible AssemblyLoadContext, takes a type from it, and roots a List<CollectibleArg> over that collectible type — the runtime then registers the instantiation in the collectible argument's loader module rather than the (CoreLib) definition module.

CollectibleGenericInstDumpTests.GetConstructedType_ResolvesGenericInstWithCollectibleTypeArgument finds that instance, confirms its loader module differs from its definition module, and asserts GetConstructedType(List<>, GenericInst, [collectibleArg]) resolves back to it. Without the fix, GetConstructedType searches the definition module's AvailableParamTypes and returns null; with the fix it computes the loader module from the type argument and finds the instantiation.

The debuggee and the DumpTests project both compile cleanly; the dump is produced by the existing dump-generation leg.

Note

This comment was generated by GitHub Copilot.

Copilot finished work on behalf of rcj1 July 8, 2026 19:06
Copilot AI had a problem deploying to copilot-pat-pool July 8, 2026 19:06 Failure
Copilot AI review requested due to automatic review settings July 8, 2026 20:17
@rcj1
rcj1 had a problem deploying to copilot-pat-pool July 8, 2026 20:17 — with GitHub Actions Failure

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 6/6 changed files
  • Comments generated: 1

@rcj1
rcj1 merged commit 7136322 into main Jul 9, 2026
78 of 79 checks passed
@rcj1
rcj1 deleted the copilot/fix-runtime-issue-130143 branch July 9, 2026 02:10
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 10, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
…ntiations (#130158)

Implement correct loader module lookup algorithm in
`GetConstructedType`. Fixes #130143.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Max Charlamb <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 10, 2026

This branch had an error being deployed

1 failed deployment
copilot-pat-pool — 2b7ab595 Deployed Jul 8, 2026 by rcj1 via pre_activation #14132
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants