Skip to content

Type-event cache lifetime race can crash CoreCLR while tracing #134242

Closed

Description

@davidwrighton

CoreCLR's global type-event cache establishes the existence of its outer and per-module
objects under GetHashCrst(), but retains or assumes those objects across later lock
acquisitions. A concurrent tracing-state callback can delete the complete graph in
those gaps. The next access can be a null dereference, a use-after-free read, or an
SHash::Add through freed storage that can corrupt the native heap or free an invalid
backing-array pointer. This is supported Release CoreCLR code used by Windows ETW and
cross-platform EventPipe.

This finding is source-derived at checkout
9b84671e937f6c183b321263c4da4fed474da218; no build, test, or dynamic reproduction was
performed in this investigation. A second deletion path on collectible loader-allocator
unload was verified, but its overlap with a legal outstanding TypeHandle from the
same loader allocator remains a stress-test hypothesis. The provider-disable
interleaving independently establishes the lifetime violation. Exact-symbol duplicate
searches found no existing same-root-cause issue.

Evidence

  • src\coreclr\vm\eventtrace.cpp:1509-1561 - the initial holder validates or creates
    s_pAllLoggedTypes, then later holders dereference it at 1541, 1556, and 1561
    without revalidating it.
  • src\coreclr\vm\eventtrace.cpp:1583-1610 - a borrowed
    LoggedTypesFromModule* survives unlocks and is later used for two lookups and an
    SHash::Add; the final operation can write through freed storage and grow/free a
    backing table reached through that storage.
  • src\coreclr\vm\eventtrace.cpp:1689-1718 and
    src\coreclr\vm\eventtrace.cpp:1807-1835 - disabling the type keyword deletes every
    module cache, deletes the global cache, and clears the root while holding the same
    lock.
  • src\coreclr\vm\eventtrace.cpp:1635-1677,
    src\coreclr\vm\eventtrace.cpp:3049-3072, and
    src\coreclr\vm\eventtrace.cpp:5406-5526 - collectible-loader unload reaches
    OnModuleUnload, which unlinks a module cache under the lock and deletes it after
    unlocking.
  • src\coreclr\vm\eventtrace.cpp:1496-1622 history - commit
    8deadd2de5ee1c220c3cf851c474c3717a944167 (dotnet/runtime PR Fix Crst level violation error on SendMethodDetails event #36932) changed the
    prior single critical interval into the current initial acquisition plus four later
    acquisitions while preserving the global null check only in the initial acquisition.

Feasible interleaving

  1. A logging thread validates the type keyword and s_pAllLoggedTypes under the holder
    at 1509, then releases the holder at 1533.
  2. A provider-control thread disables the last consumer of the type keyword, acquires
    the cache lock, deletes the outer and inner caches, clears the root, and unlocks.
  3. The logging thread reacquires the lock and dereferences the cleared root at 1541,
    or it uses a module pointer obtained before deletion at 1584, 1602, or 1610.
  4. A Release process can terminate with a native access violation or suffer native heap
    corruption; no weak-memory reordering is required.

Expected behavior

Every access to the global cache and a module cache should remain protected by the same
lock acquisition that established the object's current membership and lifetime.
Deletion must not reclaim either object until all protected accesses to it have
finished.

Actual behavior

The helper protects individual hash operations but carries the common existence and
lifetime invariant across unlocked gaps. The epoch only invalidates thread-local
caches; it has no reader participation, grace period, or ownership semantics for the
global graph.

Suggested fix

Use the existing GetHashCrst() as an operation-level lifetime lock. Compute the
loader-module key and construct type values outside the lock. Under one acquisition,
recheck the type keyword and current root, look up the current module object, and
complete the inner type lookup/insertion before unlocking.

If the module object is absent, unlock without retaining a borrowed pointer, allocate a
privately owned LoggedTypesFromModule, and reacquire. Repeat the enabled/root/module
checks, use a concurrently inserted module or transfer the private candidate only after
successful outer insertion, and complete the inner lookup/insertion before unlocking.
Dispose an untransferred candidate exactly once after unlock. The structural invariant
should be that no pointer borrowed from the global cache crosses an unlock.

Safety considerations

Do not replace the root with acquire/release atomics alone: publication ordering does
not prevent reclamation, the inner SHash objects are mutable, and the current epoch is
not a safe-reclamation protocol. Making only the outer object immortal would still
leave the per-module use-after-free.

Preserve GC_NOTRIGGER, MODE_ANY, and CRST_UNSAFE_ANYMODE. Keep potentially
lock-taking construction, callbacks, type-parameter recursion, event emission, and
managed execution outside this leaf lock. Inner SHash::Add is already serialized by
the lock; the repaired scope should remain bounded so a cooperative reader does not
materially delay GC suspension. OnTypesKeywordTurnedOff can retain deletion under the
lock. OnModuleUnload can retain unlink-under-lock and delete-after-unlock once no
reader is allowed to carry a borrowed pointer across unlock.

The failure is architecture-independent and sequentially consistent. The same lifetime
invariant must nevertheless be validated on Arm64. The thread-local fast path remains
lock-free; only the already locked global helper changes. Measure contention because
the safer form uses fewer but slightly longer critical sections.

Suggested validation

Add deterministic native test hooks at the old unlock boundaries so a controller can
delete the cache before the reader resumes. Exercise both null-root and stale-module
schedules. Add Release stress with many threads first-touching types while EventPipe
sessions repeatedly enable and disable type and sampled-allocation keywords, and repeat
with Windows ETW.

Also run a continuously enabled tracing variant that repeatedly loads and unloads
collectible AssemblyLoadContext instances; this should confirm or disprove overlap
with the verified OnModuleUnload reclamation path. Cover competing module insertion,
disable/re-enable generations, duplicate types, allocation-failure ownership, emitted
type-record correctness, checked lock-order/GC contracts, and native heap diagnostics.
Run x64 and Arm64, plus EventPipe on Windows and a supported Unix system. No such tests
were run for this draft.

Reviewer synthesis

Both reviewers validated the primary lifetime defect, rejected publication-only
lock-free repairs, selected the profiler bucket, and recommended restoring lifetime
protection with the existing lock. Reviewer B identified the additional global
dereferences, the write at 1610, the collectible-unload deleter, and the regression
commit. Reviewer A's operation-level critical-section proposal is selected because it
makes lookup and last use indivisible and is less fragile than revalidating borrowed
pointers independently in every existing scope. The collectible-unload call path is
verified, but Reviewer A's caution is retained: its no-toggle concurrent interleaving
still needs dynamic or stronger lifetime validation.

Note

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

Activity

  1. added
    untriaged
    on Sep 19, 2026
  2. dotnet-policy-service commented on Sep 19, 2026

    @dotnet-policy-service
    Contributor

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

  3. removed
    untriaged
    on Sep 20, 2026
  4. modified the milestones: 11.0.0, 12.0.0 on Sep 20, 2026
  5. added a commit that references this issue on Sep 26, 2026
    33baf8e
  6. locked and limited conversation to collaborators on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions