Skip to content

Fix generic type parameter resolution for fields (#1368, #1369)#1373

Merged
leculver merged 1 commit intomicrosoft:mainfrom
leculver:fix/issues-1368-1369
Feb 26, 2026
Merged

Fix generic type parameter resolution for fields (#1368, #1369)#1373
leculver merged 1 commit intomicrosoft:mainfrom
leculver:fix/issues-1368-1369

Conversation

@leculver
Copy link
Copy Markdown
Contributor

  • Filter out System.__Canon types from field type resolution in CacheFields. When a field's MethodTable resolves to __Canon (the canonical type used for shared generic instantiations), set the type to null to trigger the metadata signature fallback path.

  • Add concrete generic type argument resolution in ClrField.InitData(). Parse the parent type's name to extract concrete generic type arguments (e.g., 'LinkedListNode<System.String>' -> [System.String]) and pass them to GetOrCreateTypeFromSignature for Var/MVar substitution.

  • Extend GetOrCreateTypeFromSignature to accept optional concrete type arguments. When resolving Var(index) in field signatures, use the concrete type instead of creating a ClrGenericType placeholder.

  • Improve GenericInstantiation resolution to attempt looking up concrete generic types by name when concrete type arguments are available.

  • Fix ClrValueType.TryReadStringField missing _interior parameter in GetAddress call.

  • Add regression tests for LinkedListNode.item field type resolution and Dictionary<string,int>._entries array reading.

…rosoft#1369)

- Filter out System.__Canon types from field type resolution in CacheFields.
  When a field's MethodTable resolves to __Canon (the canonical type used
  for shared generic instantiations), set the type to null to trigger the
  metadata signature fallback path.

- Add concrete generic type argument resolution in ClrField.InitData().
  Parse the parent type's name to extract concrete generic type arguments
  (e.g., 'LinkedListNode<System.String>' -> [System.String]) and pass
  them to GetOrCreateTypeFromSignature for Var/MVar substitution.

- Extend GetOrCreateTypeFromSignature to accept optional concrete type
  arguments. When resolving Var(index) in field signatures, use the
  concrete type instead of creating a ClrGenericType placeholder.

- Improve GenericInstantiation resolution to attempt looking up concrete
  generic types by name when concrete type arguments are available.

- Fix ClrValueType.TryReadStringField missing _interior parameter in
  GetAddress call.

- Add regression tests for LinkedListNode<string>.item field type
  resolution and Dictionary<string,int>._entries array reading.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@leculver
Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

This was linked to issues Feb 25, 2026
@leculver leculver merged commit 847dcd6 into microsoft:main Feb 26, 2026
8 checks passed
@leculver leculver deleted the fix/issues-1368-1369 branch February 26, 2026 01:59
max-charlamb pushed a commit that referenced this pull request Apr 10, 2026
…eholder

When __Canon filtering (introduced in #1373) nulls a field's type and
the metadata signature fallback resolves to a ClrGenericType placeholder
(e.g. 'TStateMachine' with no fields), fall back to resolving via the
field's MethodTable directly.

This handles compiler-generated types nested inside open generic classes
(e.g. async state machines in local functions) where GetTypeByName fails
because it compares closed-generic names against open-generic TypeDefs.

The MethodTable resolves to __Canon, which restores the pre-#1373
behavior: the field reports IsObjectReference=true, so consumers use
ReadObjectField to get the concrete type from the heap.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
leculver added a commit to leculver/clrmd that referenced this pull request Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.

Split InitData into two methods:
  - InitData(forName) populates _name and _attributes from metadata only.
  - InitType() resolves _type from the field signature, including the
    ClrGenericType + MethodTable fallback from PR microsoft#1373.

Type initialization state is tracked separately via _typeInitialized so the
old _attributes sentinel no longer doubles as a type-resolution flag, and so
deterministically failed type resolutions are not retried on every Type access.

Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver added a commit to leculver/clrmd that referenced this pull request Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.

Split InitData into two methods:
  - InitData(forName) populates _name and _attributes from metadata only.
  - InitType() resolves _type from the field signature, including the
    ClrGenericType + MethodTable fallback from PR microsoft#1373.

Type initialization state is tracked separately via _typeInitialized so the
old _attributes sentinel no longer doubles as a type-resolution flag, and so
deterministically failed type resolutions are not retried on every Type access.

Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver added a commit to leculver/clrmd that referenced this pull request Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.

Split InitData into two methods:
  - InitData(forName) populates _name and _attributes from metadata only.
  - InitType() resolves _type from the field signature, including the
    ClrGenericType + MethodTable fallback from PR microsoft#1373.

Type initialization state is tracked separately via _typeInitialized so the
old _attributes sentinel no longer doubles as a type-resolution flag, and so
deterministically failed type resolutions are not retried on every Type access.

Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver added a commit to leculver/clrmd that referenced this pull request Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.

Split the lazy load:

  - InitData(forName) populates _name and _attributes from metadata only.
  - GetClrType() / ResolveType() resolves _type from the field signature,
    including the ClrGenericType + MethodTable fallback from PR microsoft#1373. Runs
    only when the Type property is actually accessed.

Type-resolution state is tracked in a separate _typeInitialized field
(0 = not computed, 1 = resolved, 2 = no resolvable type) so the _attributes
sentinel no longer doubles as a type-resolution flag, and so deterministically
failed resolutions don't retry on every Type access.

No locks or Interlocked are used on the type-resolution path. Reads and writes
of int and reference are atomic in .NET, and the result is deterministic for
a given field, so racing threads always agree. GetClrType() re-reads _type
after _typeInitialized and recomputes if it sees _typeInitialized == 1 with
a null _type, which tolerates write-write reordering on weakly-ordered
architectures (ARM).

Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
leculver added a commit that referenced this pull request Apr 28, 2026
ClrField.InitData was eagerly resolving the field's type signature even when
the caller only requested Name or Attributes. The type-resolution path calls
ContainingType.GetConcreteGenericTypeArguments, which calls ClrHeap.GetTypeByName,
which performs a module-wide scan and triggers expensive mscordacwks
GetMethodTableName DAC calls. This made field-heavy workloads (e.g. enumerating
async state machines) ~10x slower than ClrMD 3.1.

Split the lazy load:

  - InitData(forName) populates _name and _attributes from metadata only.
  - GetClrType() / ResolveType() resolves _type from the field signature,
    including the ClrGenericType + MethodTable fallback from PR #1373. Runs
    only when the Type property is actually accessed.

Type-resolution state is tracked in a separate _typeInitialized field
(0 = not computed, 1 = resolved, 2 = no resolvable type) so the _attributes
sentinel no longer doubles as a type-resolution flag, and so deterministically
failed resolutions don't retry on every Type access.

No locks or Interlocked are used on the type-resolution path. Reads and writes
of int and reference are atomic in .NET, and the result is deterministic for
a given field, so racing threads always agree. GetClrType() re-reads _type
after _typeInitialized and recomputes if it sees _typeInitialized == 1 with
a null _type, which tolerates write-write reordering on weakly-ordered
architectures (ARM).

Add regression tests verifying that reading Name (or Attributes) before Type
still produces the correct concrete generic instantiation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LinkedList<T> reading Dictionary Fields Reading

2 participants