Repository navigation
Fix profiler instrumented IL map use-after-free - #136808
Conversation
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
|
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. |
|
Tagging subscribers to this area: @steveisok, @tommcdon, @dotnet/dotnet-diag |
lateralusX
left a comment
There was a problem hiding this comment.
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?
Thanks for the review. I measured it on linux-x64 with Release builds of one In the configurations with a map, a test profiler supplied an identity map of N entries in every For each run, I calculated the average time per JIT. In the table,
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. |
|
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. |
|
Thank you for the review and merge! |
Fixes #136804
Problem
Module::SetInstrumentedILOffsetMappingfrees the oldCOR_IL_MAParray when a profiler sends a new map for the same method token.Module::GetInstrumentedILOffsetMappingreturns a shallow copy of the stored pointer and releasesm_pDynamicILCrstbefore 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 eachJITCompilationStartedreplaces it whenever two instantiations compile concurrently. #136804 has the details and the original repro.Change
GetInstrumentedILOffsetMappingcopies the array while it still holds the Crst. The DAC build keeps today's shallow view, and the two fields ofInstrumentedILOffsetMappingkeep their layout, so the DAC and cDAC readers are unchanged.InstrumentedILOffsetMappingHolderis a small move-only holder inilinstrumentation.h(non-DAC only) that frees the copy when the reader's scope ends.DebuggerJitInfo::SetBoundaries,CEECodeGenInfo::CompressDebugInfoandDebugger::getBoundariesHelper. The ReJIT map fromILCodeVersion::GetInstrumentedILMapstays a borrowed pointer, because it lives on the loader heap and must not be freed.AddOrReplacethrew on out-of-memory while growing the table, the table kept a pointer to the freed array, and~Modulelater freed it a second time.Holding the Crst across the reads would also close the race. But the readers do substantial work under debugger locks, and
getBoundariesHelperswitches GC mode, so that would need a lock-order and GC-mode audit. Returning a copy avoids both.Testing
profiler/instrumentedilmap. The profiler sends a 50,000-entry map whose entries are all(0, 0)on everyJITCompilationStartedofTarget<T>. The app compiles 3,036 value-type instantiations ofTarget<T>on 8 threads. On everyJITCompilationFinished, 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 (mainatc772c4c6907), 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 markedCLRTestPriority1.release/10.0at00a171f44e0built in Release with and without this change: freed-memory reads in 23 of 100 runs without it, 0 of 100 with it.main,release/10.0,release/9.0andrelease/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.