Skip to content

Concurrent generic type loading can leave inherited dictionaries null on Arm64 #134245

Closed

Description

@davidwrighton

CoreCLR intentionally publishes a MethodTable before loading its exact parent and interfaces. Exact-parent promotion writes the parent, inherited generic dictionary pointers, interface map, and required vtable data before an interlocked completion update. However, GetLoadLevel reads the completion flags without acquire semantics. A successful fast path can therefore report CLASS_LOAD_EXACTPARENTS while a subsequent payload read still sees pre-promotion data on Arm64 and other weakly ordered supported targets.

A source-derived witness uses concurrent loading of a three-level hierarchy carrying inherited generic dictionaries, such as A<T>, B<T> : A<T>, and C<T> : B<T>. A stale NULL read from B<T> can be retained or propagated by C<T>'s copy-down loop, after which C<T> is marked exact and may later be marked fully loaded. This was not dynamically reproduced, and no specific crash or incorrect managed result has been verified. A bounded open/closed issue and pull-request search found no confirmed duplicate.

Evidence

  • src\coreclr\vm\clsload.cpp:2712-2717 - the loader documents and implements publication before exact parent/base/interface loading.
  • src\coreclr\vm\class.cpp:1107-1132 - promotion writes the exact parent, ensures the parent is exact, copies inherited dictionary pointers at lines 1122-1127, and loads the exact interface map.
  • src\coreclr\vm\class.cpp:1254-1280 - exact-parent/interface and conditional vtable work precede SetHasExactParent.
  • src\coreclr\vm\methodtable.h:1380-1398,2152-2155 - GetLoadLevel takes a plain flags snapshot at line 1384, while SetHasExactParent publishes completion with InterlockedAnd at line 2155.
  • src\coreclr\vm\generics.cpp:349-355 and src\coreclr\utilcode\loaderheap.cpp:930-931 - a new instantiation initializes only its type-owned dictionary slot; inherited slots begin zeroed and are filled by exact-parent loading.

Feasible interleaving

  1. Thread A publishes B<string> at CLASS_LOAD_APPROXPARENTS; its inherited A<string> dictionary slot is still NULL.
  2. Thread A fills B<string>'s exact-parent payload and performs the release-capable InterlockedAnd that clears HasApproxParent.
  3. Thread B builds C<string>, observes the cleared flag through the plain GetLoadLevel read in EnsureLoaded, but then observes a stale NULL in B<string>'s inherited dictionary slot.
  4. The copy loop at class.cpp:1125-1127 either skips a NULL-to-NULL assignment or propagates the stale value, leaving C<string> exact/loaded with a missing inherited dictionary. A later generic lookup or dispatch can fail, although that downstream symptom is not dynamically confirmed.

Expected behavior

Observing CLASS_LOAD_EXACTPARENTS or a later level must acquire the completion publication, so all exact parent, inherited dictionary, interface map, and required vtable writes happen before subsequent payload consumption.

Actual behavior

The writer already performs an ordered interlocked RMW after the payload writes, but readiness helpers read the same flags word plainly. Successful loader fast paths take no promotion lock and supply no acquire operation before payload use. On Arm64, the payload load can therefore be satisfied before the control-dependent flag observation.

Suggested fix

Keep the existing interlocked writers. Add a centralized acquire snapshot of MethodTableAuxiliaryData::m_dwFlags using VolatileLoad, and use it in MethodTable::GetLoadLevel, IsFullyLoaded, and HasApproxParent. A reader that observes an exact/dependency/full transition will then acquire the preceding payload stores before it uses them.

The analogous TypeDesc::GetLoadLevel and TypeDesc::IsFullyLoaded plain reads should be audited and may be updated in the same change if the intended contract covers every TypeHandle readiness observation. They are not needed to fix the demonstrated MethodTable witness.

Safety considerations

This repair remains lock-free and introduces no loader lock ordering, blocking, reentrancy, callback, allocation, or GC-mode change. MethodTables and dictionaries retain their existing loader-allocator lifetime; there is no payload reclamation, ABA, or new unload ownership rule. Failure and unwind paths are unchanged.

VolatileLoad supplies an acquire load on Arm64 and compiler ordering on other targets. The cost is an ldar-class operation on weak-memory architectures and potentially reduced CSE/compiler freedom, including at IsFullyLoaded callers in interface-map paths. Performance should be measured, but a pending-load lock would be broader, slower, and risk recursive-loader deadlocks.

Suggested validation

Add a focused test that races cold B<T> and C<T> loading across many fresh three-or-more-level generic hierarchies, then exercises inherited generic methods, exact base types, and virtual/interface dispatch. Instrument the path to confirm the parent was published approximately, the successful EnsureLoaded fast path ran, and no wait supplied incidental synchronization.

On optimized Windows and Linux Arm64 builds, inspect the gate-to-payload code to confirm the flags read changes from a plain load to ldar or equivalent and payload reads remain after it. Model the minimal publication sequence with an AArch64 litmus test; run many-core Arm64 Release stress plus Arm32, LoongArch64, RISC-V, and x64 regression coverage where available. Add debug assertions for non-NULL inherited dictionary slots after exact-parent copy, and measure startup, generic-loader, and interface-cast throughput. None of this validation was run while preparing this draft.

Reviewer synthesis

Both reviewers confirmed the Release path, the sufficient interlocked writer, the missing acquire reader, and the typesystem classification. The selected repair adopts Reviewer B's centralized acquire helper while retaining Reviewer A's narrower distinction that TypeDesc parity is adjacent rather than necessary to prove this MethodTable bug. Reviewer A also correctly excludes the pending-entry shortcut as the cleanest witness because holder cleanup performs an interlocked operation; class.cpp:1114-1127 has no such intervening edge.

Reviewer B's durable-corruption conclusion is supported, but line 1127 does not necessarily write NULL: when both fresh inherited slots compare equal to stale zero, line 1125 skips the assignment. The verified consequence is that the child can nevertheless be published exact with the inherited slot still NULL.

Note

This issue draft was generated with GitHub Copilot and requires maintainer review before filing.

Activity

  1. added
    untriaged
    on Sep 19, 2026
  2. removed
    untriaged
    on Oct 5, 2026
  3. added a commit that references this issue on Oct 5, 2026
    dd39def
  4. locked and limited conversation to collaborators on Nov 5, 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