Repository navigation
Description
CoreCLR keeps the scratch storage for ObjectsAllocatedByClass in a static EEToProfInterfaceImpl field, but every profiler instance resets and frees that storage. With an active main profiler tracking GC and a second notification profiler being torn down, the second profiler can free hash-table and array storage retained by the main profiler's in-progress heap walk. The GC thread can then read or write freed native memory, double-free an array during growth, fault, corrupt the native heap, or report invalid allocation statistics.
The clearest supported path is a notification profiler whose InitializeForAttach returns failure: initialization failure calls TerminateProfiling while the profiler status is still initializing, and that function only performs profiler evacuation for active status. Successful notification-profiler detach has a second source-derived interleaving because its ThreadStore lock ends before destruction. These paths were validated from source in the current checkout, but no dynamic reproduction, build, or test was performed.
Evidence
src\coreclr\vm\eetoprofinterfaceimpl.h:542-544- The reusable allocation-by-class block is static even though its lifetime is managed by individualEEToProfInterfaceImplobjects.src\coreclr\vm\eetoprofinterfaceimpl.cpp:423,471,986-1015- Every profiler constructor clears the global pointer, and every profiler destructor frees the pointed-to hash table and arrays regardless of which instance populated them.src\coreclr\vm\eetoprofinterfaceimpl.cpp:1458-1586,5350-5435- A GC walk saves the block inpHeapId, mutates its hash storage, builds callback arrays, and continues using it independently of the static pointer.src\coreclr\vm\profilinghelper.cpp:1182-1306,1591-1617- A failed initialization callsTerminateProfilingwith initializing status; evacuation runs only for active status, after which the interface is deleted without GC serialization.src\coreclr\inc\profilepriv.h:48-56,177-181- Initializing and detaching statuses are below active, so active GC dispatch excludes the failing or detaching profiler but does not exclude the separate active main profiler.
Feasible interleaving
- Main profiler M is active with
COR_PRF_MONITOR_GC. Notification profiler N is attached; N's constructor clears the static cache, N is registered with initializing status, andInitializeForAttachremains in progress. - A GC reports allocations to M, allocates cache block B, publishes B in the static field, retains B in its local
pHeapId, and continues the heap walk. - N returns a failure HRESULT.
LoadProfilercallsTerminateProfiling; because N is initializing rather than active, teardown skipsIsProfilerEvacuatedand deletes N without taking ThreadStore. - N's destructor frees B's hash table and arrays and clears the static pointer even though M's GC context still retains B.
- M's next hash lookup, insertion, resize, or callback-array operation accesses freed storage in a Release build.
Expected behavior
Allocation-by-class scratch must be owned by the profiler instance that uses it and must remain alive until that owner's serialized heap walk and reporting callback have finished. Construction or teardown of another profiler must not alter its ownership or lifetime.
Actual behavior
The cache is process-global but reclaimed per instance. Notification profilers cannot monitor GC and never legitimately own this cache, yet their failed initialization or detach can reclaim the active main profiler's storage without synchronizing with its GC walk.
Suggested fix
Make m_pSavedAllocDataBlock a non-static EEToProfInterfaceImpl member, initialize it in the constructor initializer list, remove the static definition and constructor's global reset, and have the destructor free only its instance's hash table, arrays, and outer AllocByClassData block. Keep the existing pHeapId walk context and empty-walk behavior.
This needs no new lock-free publication protocol: only the main profiler may set COR_PRF_MONITOR_GC, so only one instance consumes the walk context. Existing active-to-detaching status publication and ThreadStore evacuation serialize that owner's destruction with its GC walk. Initializing notification instances cannot enter active GC callbacks and their instance cache remains null.
Safety considerations
The fix keeps allocation-by-class reporting lock-free and preserves cache reuse, GC mode, callback ordering, and performance. It adds one pointer per profiler instance and no steady-state allocation or lock. The destructor may reclaim the outer block, which currently leaks, only after the existing owner-lifetime protocol makes it unreachable.
Do not hold ThreadStore across the full destructor: it releases profiler interfaces and may unload third-party code. Do not hold the profiling status lock in GC reporting, and do not use only an atomic exchange of the existing static pointer because a walk may already retain a borrowed pointer. The bug is architecture-independent and possible under sequential consistency; the selected ownership fix adds no Arm64-specific memory-ordering requirement.
Suggested validation
Add a mixed-profiler test with an active GC-tracking main profiler and a notification profiler whose InitializeForAttach waits on a native gate, then returns failure after a nonempty generation-zero allocation walk has started. Verify continued, internally consistent ObjectsAllocatedByClass callbacks. Add a successful notification-detach stress variant and exercise repeated failures, empty walks, main-profiler detach/reattach, more than 1,024 classes, and array growth.
Use deterministic test-only synchronization plus PageHeap or an ASan-capable configuration to expose stale reads, writes, and double frees. Cover Release and Checked builds, workstation and server GC, and x64 and Arm64. Arm64 coverage validates parity; weak ordering is not required for the original failure.
Reviewer synthesis
Both reviewers confirmed the ownership defect, Release reachability, profiler bucket, and lack of a confirmed duplicate. Reviewer B identified the stronger failed-initialization path and correctly clarified that a lone profiler's detach is excluded from active callbacks; two instances are required. The selected repair follows Reviewer A's smaller instance-ownership design. Reviewer B's added per-walk flag and epoch-retirement stack are unnecessary under the current one-GC-tracking-profiler rule and would expand state, reclamation, and empty-walk risk without strengthening the demonstrated lifetime proof.
Note
This issue draft was generated with GitHub Copilot and requires maintainer review before filing.
Activity
dotnet-policy-service commentedon Sep 19, 2026
Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag
See info in area-owners.md if you want to be subscribed.
Fix allocation-by-class profiler cache ownership (#134390)