Skip to content

Fix profiler instrumented IL map use-after-free - #136808

Merged
lateralusX merged 1 commit into
dotnet:mainfrom
pcshrosbree:fix/instrumented-il-map-uaf
Nov 5, 2026
Merged

lateralusX merged 1 commit into
dotnet:mainfrom
pcshrosbree:fix/instrumented-il-map-uaf

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: When a profiler replaced a method's instrumented IL map, CoreCLR freed the old map while another thread could still be reading it. The getter now returns its own copy, made under the lock, and the reader frees that copy when it is done. In 30 runs per build, a new profiler test failed every time without the fix and passed every time with it.

Fixes #136804

Problem

Module::SetInstrumentedILOffsetMapping frees the old COR_IL_MAP array when a profiler sends a new map for the same method token. Module::GetInstrumentedILOffsetMapping returns a shallow copy of the stored pointer and releases m_pDynamicILCrst before the caller reads it. A reader on one thread can therefore read the array after a writer on another thread has freed it. Value-type instantiations of a generic method share one token, so a profiler that sends a map from each JITCompilationStarted replaces it whenever two instantiations compile concurrently. #136804 has the details and the original repro.

Change

  • The getter returns an owning copy. On the non-DAC build, GetInstrumentedILOffsetMapping copies the array while it still holds the Crst. 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.
  • InstrumentedILOffsetMappingHolder is a small move-only holder in ilinstrumentation.h (non-DAC only) that frees the copy when the reader's scope ends.
  • The three readers use the holder for the load-time map: DebuggerJitInfo::SetBoundaries, CEECodeGenInfo::CompressDebugInfo and Debugger::getBoundariesHelper. The ReJIT map from ILCodeVersion::GetInstrumentedILMap stays a borrowed pointer, because it lives on the loader heap and must not be freed.
  • The setter stores the new map before it frees the old one. Before, if AddOrReplace threw on out-of-memory while growing the table, the table kept a pointer to the freed array, and ~Module later freed it a second time.
  • If the copy cannot be allocated, the getter returns an empty map, so that read gets no instrumented translation instead of a dangling pointer.

Holding the Crst across the reads would also close the race. But the readers do substantial work under debugger locks, and getBoundariesHelper switches GC mode, so that would need a lock-order and GC-mode audit. Returning a copy avoids both.

Testing

  • New test profiler/instrumentedilmap. The profiler sends a 50,000-entry map whose entries are all (0, 0) on every JITCompilationStarted of Target<T>. The app compiles 3,036 value-type instantiations of Target<T> on 8 threads. On every JITCompilationFinished, the profiler reads the mapping back and fails if it is empty or if any IL offset is neither 0 nor a special value. With every entry equal, each translation of an ordinary offset scans the whole map, which raises the chance that another thread replaces the map during the read. I built Checked runtimes on linux-x64 from this PR's base (main at c772c4c6907), with and without the product change, and ran the test 30 times on each. All 30 runs failed without the fix, and none failed with it. Of the 30 failures, 22 reported unexpected IL offsets and 8 crashed with a segmentation fault. Detection was measured with glibc only. The test is marked CLRTestPriority 1.
  • The original repro, on release/10.0 at 00a171f44e0 built in Release with and without this change: freed-memory reads in 23 of 100 runs without it, 0 of 100 with it.
  • Built Checked (runtime and DAC) on linux-x64 for main, release/10.0, release/9.0 and release/8.0; the change ports to the release branches with small adjustments if a backport is wanted.

Note

This change was prepared with AI assistance (Anthropic Claude and OpenAI Codex) under my direction. AI agents ran the builds and tests on my machine. I reviewed the change and the test results before opening it.

Module::SetInstrumentedILOffsetMapping freed the old map array when a
profiler sent a new map for the same method token, while
GetInstrumentedILOffsetMapping handed out shallow copies that readers
used after the lock was released. A reader on one thread could then read
the array after another thread freed it.

Make the getter copy the array while it holds m_pDynamicILCrst and
return a copy the caller owns. Add InstrumentedILOffsetMappingHolder, a
move-only holder that frees the copy at the end of the reader's scope,
and use it in the three readers for the load-time map. The ReJIT map
stays a borrowed pointer. The DAC build keeps the shallow view, and the
layout of InstrumentedILOffsetMapping is unchanged.

Store the new map before freeing the old one, so that an out-of-memory
throw from AddOrReplace no longer leaves the table pointing at freed
memory.

Add the profiler test instrumentedilmap, which replaces one generic
method's map from many threads and checks every mapping read back.

Fixes dotnet#136804
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 1, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
11 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@lateralusX lateralusX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good to me, but I'm a little curious about the potential performance impact of allocating and copying the map on each retrieval. Have you measured that overhead with a representative profiler workload?

@pcshrosbree

Copy link
Copy Markdown
Contributor Author

TL;DR: I measured the overhead with a synthetic workload that supplied a map for every JIT-compiled method. The results showed no clear added cost at 64 or 512 entries. At 4,096 entries, the added cost was 1.8–2.8 µs per JIT after adjusting for the no-map runs. I kept the copy as a small fix that protects all three readers.

Thanks for the review. I measured it on linux-x64 with Release builds of one release/10.0 commit (00a171f44e0), with and without this change. This is a synthetic comparison, not a measurement with a production profiler.

In the configurations with a map, a test profiler supplied an identity map of N entries in every JITCompilationStarted callback, so every JIT-compiled method had a map. In half the runs it also read each mapping back with GetILToNativeMapping from JITCompilationFinished. It timed each compilation from JITCompilationStarted to JITCompilationFinished. The app JIT-compiles about 3,800 methods with DOTNET_ReadyToRun=0. Each configuration ran 40 times per build, in alternating base/fixed and fixed/base pairs.

For each run, I calculated the average time per JIT. In the table, Base is the median of those averages for the unchanged build. To calculate Change, I first subtracted the unchanged build's median from the changed build's median. I then subtracted the corresponding difference measured with no map supplied (+0.5 µs without readback, +0.2 µs with it). Parentheses give the 95% bootstrap interval for Change.

Map entries Read back Base µs/JIT Change, µs/JIT
64 no 43.4 −0.3 (−0.8 to +0.2)
512 no 44.9 −0.1 (−0.7 to +0.4)
4096 no 52.2 +1.8 (+1.1 to +2.4), +3.5%
64 yes 44.8 +0.3 (−0.2 to +0.6)
512 yes 46.6 +0.5 (−0.1 to +0.9)
4096 yes 54.2 +2.8 (+2.3 to +3.2), +5.2%

After subtracting the no-map control, the intervals at 64 and 512 entries include zero. At 4,096 entries, the net increase was 1.8 µs per JIT without readback and 2.8 µs with readback (2.3 and 3.1 µs before the adjustment). Without readback, supplying a map that size increased the unchanged build's median by about 10 µs per JIT. For methods without a supplied map, the getter returns an empty mapping without allocating a map buffer.

A reference-counted immutable array could avoid the copy on each retrieval, but it would need coordinated ownership changes and care for the DAC readers. I kept the copy as a small fix that covers all three readers. I can share the profiler and the app.

@lateralusX

Copy link
Copy Markdown
Member

Thanks for measuring this. The results look reasonable to me. Since map allocation/copying only occurs when a profiler explicit supplies an IL map, this doesn't add overhead to default execution. Agree that the copy is the smaller and more targeted/isolated fix than moving over to a ref counted implementation to eliminate allocation/copy needs and we always have ability to change later if it turns out to affect performance too much.

@lateralusX
lateralusX merged commit 86c21e6 into dotnet:main Nov 5, 2026
118 of 120 checks passed
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Diagnostics-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants