Skip to content

SetILInstrumentedCodeMap frees the previous map while another thread may still be reading it (use-after-free) #136804

Closed

Description

@pcshrosbree

TL;DR: When a profiler sends a second instrumented-IL map for a method, CoreCLR frees the first map while another thread may still be reading it. I reproduced corrupted IL offsets on .NET 10.0.13 and crashes in a test on main. MSRC reviewed it first and closed it as not a security issue. I have a small fix ready and can open a PR.

Description

Module::SetInstrumentedILOffsetMapping frees the old COR_IL_MAP array when a profiler replaces a method's map through ICorProfilerInfo::SetILInstrumentedCodeMap. Module::GetInstrumentedILOffsetMapping hands its callers a shallow copy of that array's pointer and releases the lock before they use it. So if one thread is reading the map while another thread replaces it, the reader uses freed memory.

The replacement is ordinary use. Generic value-type instantiations of one method share its method-def token, and so share one map entry. A profiler that sends a map from each JITCompilationStarted therefore replaces that entry each time another instantiation compiles, and instantiations can compile on several threads at once. Tiering (tier0, tier1 and on-stack replacement) can also compile one method more than once. No debugger needs to be attached.

Reproduction

Measured on Microsoft.NETCore.App 10.0.13, linux-x64, glibc 2.43.

  • A small test profiler sends an instrumented-IL map on each JITCompilationStarted, and reads the method's mapping on each JITCompilationFinished with GetILToNativeMapping. The map leaves IL offsets unchanged.
  • The app triggers compilation of 768 value-type instantiations of one generic method, across 8 threads that start together.
  • MALLOC_PERTURB_=165 makes glibc fill freed memory with 0xA5, so an IL offset of 0xA5A5A5A5 in a returned mapping means the runtime read the freed array. With the small 22-entry map, GLIBC_TUNABLES=glibc.malloc.tcache_count=0 is also needed, because glibc's thread cache returns small freed chunks without the fill.
Run Map sent Runs with 0xA5A5A5A5 in a mapping
22-entry map on every JIT 16 of 100
22-entry map (control) once 0 of 100
12,000-entry map on every JIT 8 of 20
12,000-entry map (control) once 0 of 20

The control sends the same map once, so nothing is freed by a replacement. For the 22-entry map, the difference between the replacement and control runs is significant (two-sided Fisher exact test, p = 1.6e-5). None of these runs crashed; every observed case corrupted IL offsets in the debugger's IL-to-native map (DebuggerJitInfo). The 50,000-entry test under "Validation" also recorded segmentation faults on the unfixed build, and none on the fixed build. I can share the profiler and the app.

Root cause

Line numbers are for release/10.0 at 425ba4b8e5a; main at c772c4c6907 has the same defect in the same setter, getter and three readers, at other line numbers.

  1. Copies share one buffer. InstrumentedILOffsetMapping (vm/ilinstrumentation.h) holds a count and a raw COR_IL_MAP*, with no destructor, copy constructor or copy assignment. Every copy points at the same heap array. Clear() (vm/ilinstrumentation.cpp:51) does delete[] on it.
  2. The setter frees the old array. Module::SetInstrumentedILOffsetMapping takes m_pDynamicILCrst, finds the existing entry for the token and calls Clear() on it (ceeload.cpp:1004), then stores the new map.
  3. The getter returns a shallow copy and drops the lock. Module::GetInstrumentedILOffsetMapping returns the stored value by value (ceeload.cpp:1056). The CrstHolder releases the lock as it returns, and the caller then reads the array unlocked.
  4. Three in-process readers use that copy:
    • DebuggerJitInfo::SetBoundaries (debug/ee/functioninfo.cpp:1085). A profiler's own GetILToNativeMapping call reaches it. This is the reader the repro exercises.
    • CEECodeGenInfo::CompressDebugInfo (vm/jitinterface.cpp:11425), at JIT completion. Not probed separately.
    • Debugger::getBoundariesHelper (debug/ee/debugger.cpp:3216). Its use of the map is inside #if defined(FEATURE_ISYM_READER), so Windows only.

The comments promise that the map "stays valid until the Module containing the method is destructed" (ceeload.cpp:979) and that "the profiler may potentially update the mapping more than once" (ceeload.cpp:980). The implementation does not keep that promise for copies already handed out.

The ReJIT per-version map is not affected. ProfilerFunctionControl::SetILInstrumentedCodeMap copies it into loader-heap memory owned by the ILCodeVersion (rejit.cpp:354, :1057), and nothing frees it on replacement.

Affected versions

  • release/10.0 and main: as above. Reproduced on 10.0.13 and on an unfixed main Checked build (see "Validation").
  • release/9.0 and release/8.0: the same type, Clear() and shallow-copy getter (ceeload.cpp:1004 on 9.0 at b0984b273c3, :1403 on 8.0 at 80c68f487bf). Found by reading the source, not reproduced. These branches have no CompressDebugInfo reader; they have a DAC reader in debug/daccess/dacdbiimpl.cpp:1054 instead, which reads target memory out of process and is not part of the race.

Proposed fix

The getter makes a deep copy of the array while it still holds the Crst, and returns a copy the caller owns. A small move-only holder (InstrumentedILOffsetMappingHolder) frees that copy when the reader's scope ends. The three readers use the holder for the load-time map only; the ReJIT map stays a borrowed pointer, because it lives on the loader heap and must not be freed. The DAC build keeps today's shallow view, and the two fields of InstrumentedILOffsetMapping keep their layout, so the DAC and cDAC readers are unchanged. If allocating the copy fails, the getter returns an empty map, so the readers leave the offsets untranslated instead of reading freed memory.

The setter also stores the new map before it frees the old one. Today it frees first. If AddOrReplace then throws on out-of-memory while growing the table, the table keeps a pointer to the freed array, and ~Module later frees it a second time.

Holding the Crst across the reads would also close the race, but the readers do real work under debugger locks, and getBoundariesHelper switches GC mode. That would need a lock-order and GC-mode audit, which returning an owned copy avoids.

The change is 5 files, +143/−9 on release/10.0. The main version has the same size. It differs in one re-indented block, in one comment that names main's compression helper, and in using UINT for the map-entry count. I can open a PR.

Validation

I built release/10.0 at 00a171f44e0 with and without the fix and ran the 22-entry replacement case 100 times on each build. Without the fix, 23 of 100 runs returned IL offsets containing 0xA5A5A5A5; with it, none of 100 did (two-sided Fisher exact test, p = 5.7e-8).

A new profiler test sends a 50,000-entry map. In one 30-run batch on a main Checked build at f0c7a08c8de, the unfixed runtime failed every run: 25 reported freed-memory IL offsets and 5 ended with a segmentation fault. The fixed runtime passed all 30.

Security

I reported this to MSRC privately before filing here. MSRC closed it as not a security vulnerability: the trigger needs a profiler, which is fully trusted native code in the process, so no security boundary is crossed (see the .NET baseline security assumptions). It is a reliability bug.

Note

This report was prepared with AI assistance (Anthropic Claude and OpenAI Codex) under my direction. AI agents ran the repro and read the sources on my machine. I reviewed the text before posting.

Activity

  1. added
    untriaged
    on Nov 1, 2026
  2. dotnet-policy-service commented on Nov 1, 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 Nov 5, 2026
  4. added a commit that references this issue on Nov 5, 2026
    86c21e6
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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions