Skip to content

[cDAC] Implement DacDbiImpl.ResolveTypeReference - #129958

Merged
rcj1 merged 10 commits into
mainfrom
copilot/discuss-resolvetypereference-implementation
Jul 10, 2026
Merged

rcj1 merged 10 commits into
mainfrom
copilot/discuss-resolvetypereference-implementation

Conversation

@rcj1

@rcj1 rcj1 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Port native ResolveTypeReference to cDAC, using metadata instead of the various DAC hash structures to resolve types in different assemblies by name.

Copilot AI review requested due to automatic review settings June 29, 2026 01:41
@rcj1 rcj1 changed the title Implement cDAC DacDbiImpl.ResolveTypeReference [cDAC] Implement DacDbiImpl.ResolveTypeReference Jun 29, 2026

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

Implements cDAC-side support for DacDbiImpl.ResolveTypeReference by resolving mdTypeRef tokens to mdTypeDef tokens using ECMA-335 metadata (with support for AssemblyRef scopes, exported-type forwarders, and nested types), and adds unit tests covering several resolution scenarios.

Changes:

  • Implement DacDbiImpl.ResolveTypeReference in the managed cDAC implementation (replacing E_NOTIMPL / legacy fallback behavior).
  • Add unit tests that construct in-memory metadata graphs to validate AssemblyRef, type-forwarder, and nested-type resolution.
  • Extend EcmaMetadataUtils.TokenType with mdtAssemblyRef to support lookup-map token construction.
Show a summary per file
File Description
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Legacy/Dbi/DacDbiImpl.cs Adds managed implementation for ResolveTypeReference, including metadata-based scope/name walking and forwarder/nested-type resolution helpers.
src/native/managed/cdac/tests/UnitTests/DacDbiImplTests.cs Adds new unit tests and metadata-builder helpers to validate the new resolution logic across several scenarios.
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/EcmaMetadataUtils.cs Adds mdtAssemblyRef token type constant used by the resolver logic and tests.

Copilot's findings

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

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 29, 2026 01:51
@rcj1

rcj1 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please fix the unit tests

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: 3/3 changed files
  • Comments generated: 2

Copilot AI commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@copilot please fix the unit tests

Fixed in 7e9e2f1. The NotCached test previously relied on metadata access throwing, which the catch-block autofix surfaced as E_FAIL. It now uses mock contracts where the TypeRef resolves to its own module but no matching TypeDef exists, so resolution genuinely fails and returns CORDBG_E_CLASS_NOT_LOADED. All 20 ResolveTypeReference tests pass.

Note

This response was AI-generated by Copilot.

Copilot finished work on behalf of rcj1 June 29, 2026 02:03
@github-actions

This comment has been minimized.

@rcj1

rcj1 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please fix the unit tests and build to confirm.

Copilot AI review requested due to automatic review settings June 29, 2026 02:21
@rcj1
rcj1 removed the request for review from Copilot June 29, 2026 02:21

Copilot AI commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@copilot please fix the unit tests and build to confirm.

Done in c6b1f1a. Built and confirmed all 248 DacDbiImpl tests pass (20 ResolveTypeReference). Also gated exported-type follow on IsForwarder and made AddTypeDef namespace nullable per the new review findings.

Note

This response was AI-generated by Copilot.

Copilot AI review requested due to automatic review settings June 29, 2026 02:22
@rcj1
rcj1 removed the request for review from Copilot June 29, 2026 02:22
Copilot finished work on behalf of rcj1 June 29, 2026 02:22
@rcj1 rcj1 self-assigned this Jun 29, 2026
Copilot AI review requested due to automatic review settings June 29, 2026 16:32
@rcj1
rcj1 force-pushed the copilot/discuss-resolvetypereference-implementation branch from 2bf53e7 to 557ad42 Compare June 29, 2026 16:32
@github-actions

This comment has been minimized.

Copilot AI review requested due to automatic review settings June 30, 2026 06:33
Co-authored-by: Copilot Autofix powered by AI <[email protected]>

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: 3/3 changed files
  • Comments generated: 2

Copilot AI review requested due to automatic review settings June 30, 2026 06:39

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: 3/3 changed files
  • Comments generated: 5

@github-actions

Copy link
Copy Markdown
Contributor

Copilot Code Review

Holistic Assessment

Motivation: The PR ports DacDbiImpl.ResolveTypeReference from native DAC to cDAC using ECMA metadata, replacing hash-table-based resolution with a metadata-walking approach. This is a justified incremental step in the cDAC porting effort.

Approach: The implementation correctly prioritizes the cached TypeRef→MethodTable lookup (fast path), then falls back to metadata-based resolution with type-forwarder chain following. The approach is appropriate.

Summary: ⚠️ Needs Human Review. The implementation is functionally correct and well-tested, but there is one robustness concern (unbounded loop on potentially cyclic metadata) that warrants maintainer judgment. The remaining open review threads about helper visibility and test resource disposal are low-severity.


Detailed Findings

Detailed Findings

⚠️ Robustness — Unbounded while(true) loop in TryGetTypeRefScopeAndName (line 328)

The while(true) loop walking nested TypeRef scopes has no iteration bound. If metadata is corrupt and TypeRef resolution scopes form a logical cycle (e.g., TypeRef A scoped to TypeRef B, which is scoped back to A), this will hang. The code already demonstrates awareness of this pattern — TrySearchModulesForTypeDef bounds its forwarder chain to MaxTypeForwardingChainSize = 1024.

In practice, MetadataReader would throw BadImageFormatException on invalid row references, but valid-but-cyclic handles would not trigger this. Consider applying a similar iteration cap:

for (int depth = 0; depth < MaxTypeForwardingChainSize; depth++)
{
    TypeReference typeRef = reader.GetTypeReference(handle);
    nameChain.Add((reader.GetString(typeRef.Namespace), reader.GetString(typeRef.Name)));
    scope = typeRef.ResolutionScope;
    if (scope.Kind == HandleKind.TypeReference)
    {
        handle = (TypeReferenceHandle)scope;
        continue;
    }
    break;
}

Severity: low (corrupt metadata is uncommon), but the fix is trivial and consistent with existing patterns in this PR.

💡 Design — TryFindTopLevelTypeDef / TryFindNestedTypeDef visibility

These methods are public in EcmaMetadataUtils but currently only consumed by DacDbiImpl in the Legacy project. Since InternalsVisibleTo is already configured for both the Legacy and Tests projects, these could be internal to avoid growing the utility class's public surface area. However, if future cDAC contracts will also need type-name-based lookup, keeping them public is reasonable — this is a judgment call for maintainers.

💡 Test hygiene — MetadataReaderProvider not disposed

The test methods keep MetadataReaderProvider alive via GC.KeepAlive to prevent the backing memory from being collected while MetadataReader is in use, but never call Dispose(). Since MetadataReaderProvider holds unmanaged memory, explicit disposal at test end would be cleaner. A using declaration or try/finally would suffice.

✅ Error handling pattern is correct

The catch (Exception ex) { hr = ex.HResult; } followed by if (!resolved && hr == HResults.S_OK) { hr = CORDBG_E_CLASS_NOT_LOADED; } correctly distinguishes three outcomes: success, exception-with-HRESULT, and "not found". The conditional only overrides hr when no exception occurred (hr is still S_OK), so it does not mask unexpected failures.

✅ Output parameter initialization

The code zeros pTargetRefInfo->vmAssembly and pTargetRefInfo->typeToken at method entry (line 228-229), ensuring callers never observe stale data regardless of resolution outcome.

✅ Type-forwarder detection logic

TryFindTopLevelExportedForwarder filters on exportedType.Implementation.Kind != HandleKind.AssemblyReference, which is functionally equivalent to checking IsForwarder — only forwarders have an AssemblyReference as their implementation handle. Non-forwarder exported types use File or nested ExportedType handles.

✅ Test coverage

Four well-structured test scenarios covering: TypeDef passthrough, cache miss with no metadata, cross-assembly TypeRef resolution, type-forwarder chain following, and nested type resolution. Good use of MetadataBuilder to construct precise test metadata.

Note

This review was generated by Copilot.

Generated by Code Review for issue #129958 · ● 52.6M · ◷

@davidwrighton davidwrighton 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.

The type resolution algorithm appears correct to me.

@noahfalk noahfalk 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.

This looks reasonable to me but happy to defer to David who has more expertise in this part of the runtime behavior.

Copilot AI review requested due to automatic review settings July 10, 2026 16:41
@rcj1
rcj1 had a problem deploying to copilot-pat-pool July 10, 2026 16:41 — 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: 4/4 changed files
  • Comments generated: 4

Copilot AI review requested due to automatic review settings July 10, 2026 18:09
@rcj1
rcj1 had a problem deploying to copilot-pat-pool July 10, 2026 18:10 — 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: 4/4 changed files
  • Comments generated: 2

@rcj1
rcj1 merged commit 2f3204f into main Jul 10, 2026
76 of 77 checks passed
@rcj1
rcj1 deleted the copilot/discuss-resolvetypereference-implementation branch July 10, 2026 22:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 11, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
Port native ResolveTypeReference to cDAC, using metadata instead of the
various DAC hash structures to resolve types in different assemblies by
name.

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 11, 2026

This branch had an error being deployed

1 failed deployment
copilot-pat-pool — 12f9eeff Deployed Jul 10, 2026 by rcj1 via pre_activation #14441
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.

7 participants