Repository navigation
Description
CoreCLR can independently publish a generic parameter's constraint-array pointer and its completeness flags from two concurrent builders. If a partial builder's array wins the pointer CAS while a full builder wins the flags CAS, the runtime permanently advertises All over an array whose generic-interface slots are null. An All consumer can then cache an incorrect cast result or dereference a null native type handle.
This is supported Release code and the failing schedule is valid under sequential consistency on x64 and Arm64. Source analysis establishes ordinary CoreLib and qualifying ReadyToRun paths that can leave the cache cold until partial castability and full reflection requests race. No dynamic reproduction, build, or test has been run; occurrence frequency remains unmeasured and the decisive CAS window is short.
Evidence
src\coreclr\vm\typedesc.cpp:830-837,953-981- concurrent first loaders can build separate arrays; the pointer CAS loser can still publish completeness derived from its discarded array.src\coreclr\vm\typedesc.cpp:884-927- partial loading can omit a generic-instantiation interface constraint, leaving its zero-initialized slot null.src\coreclr\vm\typedesc.cpp:704-739,984-990- readers trust the completeness word, return the separately stored pointer, and do not repair null entries onceAllis advertised.src\coreclr\vm\ceeload.cpp:457-465andsrc\coreclr\vm\methodtable.cpp:4573-4584,4758-4828- the system module, and qualifying ReadyToRun modules, skip the eager validation that would otherwise issue partial constraint requests.src\coreclr\vm\typedesc.cpp:401-406andsrc\coreclr\vm\runtimehandles.cpp:601-623- ordinary array castability can request partial constraints while reflection independently requestsAll.src\coreclr\vm\clsload.cpp:2929-2974,3010-3022- final-level loading runs outside the pending-type-load portion and does not serialize arbitrary constraint-cache callers.
Feasible interleaving
- A partial castability request and reflection's
Allrequest both observeNonefor one generic parameter with a generic-interface constraint. Each builds a private array; the partial array retains a null interface slot. - The partial thread wins the array-pointer CAS and pauses before publishing its flags.
- The
Allthread loses the pointer CAS but wins the independent flags CAS, publishingAllbased on its discarded complete array. - The partial thread loses the flags CAS, retries, observes
All, and exits. Future callers trustAll; reflection or cast traversal can omit a required constraint, cache a false result, or dereference a null native handle in Release.
Expected behavior
The advertised constraint count and coverage must describe the one published array. Observing All must acquire a fully initialized array in which every metadata constraint has a non-null canonical TypeHandle.
Actual behavior
The pointer and coverage have separate linearization points and can have different winning builders. Acquire/release ordering makes each atomic operation visible but cannot associate a full builder's flags with the partial builder's winning array.
Suggested fix
Use one loader-lifetime constraint array. For a nonzero count, allocate a zero-initialized candidate and release-CAS it into m_constraints before filling; a CAS loser must acquire and adopt the winning pointer and back out only its unpublished allocation. All builders then fill the same array.
Keep the completeness CAS as the coverage publication point. Before release-publishing a coverage value, that thread must have written every slot promised by that coverage using aligned atomic/volatile stores. Readers must acquire-load the completeness word before reading the pointer and slots. Preserve the zero-constraint contract and monotonic partial-to-All upgrade. Never reclaim the published singleton before its loader allocator; if filling throws, leave coverage unadvanced so a later caller can repair the retained array.
Safety considerations
The schedule is architecture-independent, but the repair's acquire/release contract must be verified on Arm64. The array contains native TypeHandle values rather than moving GC references and remains owned by the defining loader allocator, including for collectible contexts. An exception after early pointer publication may retain one empty or partially filled array, but must not advance coverage; subsequent attempts can safely refill canonical, idempotent slots.
Do not hold a new broad lock across constraint resolution. The method is MODE_ANY, triggers GC and class loading, and can be reached recursively through constraint analysis; such a lock would require a complete reentrancy, GC-mode, and loader-lock-order proof. The singleton protocol preserves lock-free reads, adds no reclamation scheme or layout object, and costs only rare duplicate fill work during first-load contention.
Suggested validation
Add deterministic test-only instrumentation around the pointer/coverage publication window. First demonstrate that the old code can reach All with a null slot, then verify that the singleton repair cannot. Race a fresh generic-interface-only parameter's partial array-cast path against GetGenericParameterConstraints without warming the cache, and assert the final All state has the expected non-null constraint identity.
Cover partial/partial, All/All, both mixed winner orders, zero constraints, failed fills followed by retry, recursive constraints, GC stress, collectible loader unload, allocation backout, and DAC enumeration. Run Release stress on x64 and Arm64; Arm64 is validation of publication ordering, not a required reproduction condition. Measure losing allocations and duplicate slot filling.
Reviewer synthesis
Both reviews validate the two-CAS defect, the typesystem classification, architecture independence, and the absence of a confirmed duplicate. The CoreLib skip-validation path and the ReadyToRun/unlocked-final-level analysis are complementary: CoreLib proves ReadyToRun is not required, while ReadyToRun supplies a controllable app-module variant.
The selected singleton-array proposal is narrower than replacing the representation with immutable snapshots and avoids the latter's getter, DAC, layout, and retained-snapshot changes. Its publication order is sufficient because every coverage publisher writes all slots it promises before the release CAS, readers acquire that same coverage word, published storage has loader lifetime, and no discarded array can be the basis for advertised completeness. Confidence reflects the source proof; the short timing window affects expected frequency and test strategy rather than validity.
Note
This issue draft was generated with GitHub Copilot and requires maintainer review before filing.
Activity
Fix concurrent generic constraint cache publication (#135181)