[mono] --jitdump: mono_jit_dump_cleanup() closes the stream without resetting perf_dump_file
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 55/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- c
調査の方向性
Start with src/mono/mono/mini/mini-runtime.c, especially mono_enable_jit_dump(), mono_emit_jit_dump(), and mono_jit_dump_cleanup(); trace how cleanup is reached from mini_cleanup() and the two runtime shutdown paths named in the issue. Reproduce with the attached harness and ASan setup. Done means repeated cleanup and compilation after cleanup no longer use or close the dump stream after it has been closed.
索引モデルが issue の本文から書いたものです。
説明
Description
mono_jit_dump_cleanup() closes the jitdump stream but leaves the global perf_dump_file non-NULL. mono_emit_jit_dump() only checks that pointer, so any method compiled after that point passes the closed — and already freed — FILE * to fwrite(), and a second cleanup calls fclose() on it again. mini_cleanup() calls mono_jit_dump_cleanup(), and in an embedded host it can run more than once: mono_runtime_quit() cleans up, then mono_jit_cleanup() runs mono_thread_manage_internal() — which fires the ProcessExit event, JIT-compiling managed methods — and calls mini_cleanup() again. Background threads that are still compiling while the main thread tears the runtime down hit the same stale pointer.
Reproduction Steps
Attached: jitdump_host.c (harness), JitDumpPoc.cs, JitDumpRace.cs, and the build hook files.
The harness enables jitdump exactly like mono --jitdump does (mono_enable_jit_dump()), initialises the runtime, runs a managed entry point, and then cleans up twice:
mono_enable_jit_dump (); /* fopen("/tmp/jit-<pid>.dump") */
mono_debug_init (JD_MONO_DEBUG_FORMAT_MONO);
mono_jit_init_version (...);
mono_jit_exec (...); /* the managed program */
mono_runtime_quit (); /* first mini_cleanup(): fclose, pointer kept */
mono_jit_cleanup (domain); /* ProcessExit event JITs methods -> fwrite on the closed FILE; second fclose */
Build (Mono runtime with AddressSanitizer, Linux x64, tag v10.0.15)
-
Build the runtime with the attached CMake hook (
asan-flags.cmake,asan-default-options.c) — the Mono CMake build has no sanitizer switch:./build.sh -restore -build -c release /p:RuntimeFlavor=Mono /p:KeepNativeSymbols=true \ "/p:MonoObjDir=<abs>/asan-obj/" "/p:RuntimeBinDir=<abs>/asan-bin/" \ "/p:CMakeArgs=-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=<abs>/asan-flags.cmake" \ -projects <repo>/src/mono/mono.projThe instrumented runtime is
<abs>/asan-obj/out/bin/mono-sgen. Run every ASan binary of this build with
ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp
(detect_stack_use_after_return=0is baked intomono-sgenbyasan-default-options.c, but a separately linked harness needs it from the environment: Mono's GC-suspend code is not compatible with ASan's fake stack). -
Add the harness as an extra executable linked into that runtime (
extra-targets.cmake,jitdump_host.c):cmake -S <repo>/src/mono -B <abs>/asan-obj \ "-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=<abs>/asan-flags.cmake;<abs>/extra-targets.cmake" \ -DMONO_VERIFY_HARNESS_SRC=<abs>/jitdump_host.c -DMONO_VERIFY_HARNESS_NAME=jitdump_host cmake --build <abs>/asan-obj --target jitdump_host -
Compile
JitDumpPoc.cstoJitDumpPoc.exeagainst the managed assemblies, then run (the harness also acceptscleanup-quit,quit-quit,cleanup-cleanupand the single-cleanup controls):MONO_PATH=<mono assemblies> LD_LIBRARY_PATH=<dir with libSystem.*.so> \ ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp \ <abs>/asan-obj/jitdump_host quit-cleanup <abs>/JitDumpPoc.exe
Expected behavior
The dump stream is closed once; a host that cleans up twice, or a method compiled after cleanup, does not touch a closed stream.
Actual behavior
ASan reports a double free of the FILE object (asan.txt):
ERROR: AddressSanitizer: attempting double-free on 0x515000000080 in thread T0
#4 mono_jit_dump_cleanup src/mono/mono/mini/mini-runtime.c:2257:3
#5 mini_cleanup src/mono/mono/mini/mini-runtime.c:5267:2
#6 mono_jit_cleanup src/mono/mono/mini/driver.c:2768:2
#7 cleanup poc/jitdump_host.c:131:3
previously allocated by thread T0 here:
#4 mono_enable_jit_dump src/mono/mono/mini/mini-runtime.c:2115:20
The report's "freed by" stack is wrong: ASan keeps the id of the freeing stack in the first bytes of the freed block, and the stale fwrite() between the two fclose() calls overwrites them.
Without ASan the process crashes (exit code 139 in both runs on release/10.0.15; 20 of 20 runs on main@bdd9b750b70): mono_jit_cleanup() fires ProcessExit, the runtime JIT-compiles methods, mono_emit_jit_dump() reaches fwrite(..., perf_dump_file) (mini-runtime.c:2195) with a FILE whose fields now belong to another allocation and writes to NULL. Single-cleanup runs (mono_jit_cleanup() alone, mono_runtime_quit() alone, or the driver's own mono_main()) are clean on both builds.
The pure-managed variant (JitDumpRace.exe: mono-sgen --jitdump with background threads that keep compiling new methods while the main thread exits) does reach the stale write — strace shows write(-1, ...) on the closed dump fd — but the window is below a millisecond and the process usually exits 0 before the damage is observable, so it is not a reliable reproducer.
Regression?
No response
Known Workarounds
No response
Configuration
- .NET 10.0.15 (tag
v10.0.15, commita3788b71495457354b64ca8040b6ded5ff6ca4f9), Mono runtime built from source with the attached hook, linux-x64, clang 18, Release flags plus-fsanitize=address. main(commit5f8157f1522a) has the same code at the same lines; the same report was reproduced onmain@bdd9b750b70.- v11.0.0 (tag
v11.0.0): same code (sources compared; not run).
Other information
Root cause
src/mono/mono/mini/mini-runtime.c:2251-2258 — identical on main and release/10.0:
void
mono_jit_dump_cleanup (void)
{
if (perf_dump_mmap_addr != MAP_FAILED)
munmap (perf_dump_mmap_addr, sizeof(FileHeader));
if (perf_dump_file)
fclose (perf_dump_file);
}
The only assignment to perf_dump_file is the fopen() in mono_enable_jit_dump() (mini-runtime.c:2115). The emit path only tests it (mini-runtime.c:2146-2147):
if (!perf_dump_file)
return;
and later writes with it (mini-runtime.c:2195). mono_jit_dump_cleanup() is called from mini_cleanup() (mini-runtime.c:5277 on main, 5267 on release/10.0), which is reachable twice in an embedded host: from mono_runtime_quit_internal() (src/mono/mono/metadata/appdomain.c:379-388, quit_function at 387) and from mono_jit_cleanup() (src/mono/mono/mini/driver.c:2736-2748 on main, 2758-2770 on release/10.0), where mono_thread_manage_internal() runs the ProcessExit event before the second mini_cleanup().
- 主要言語
- C#
- スター
- 18.5k
- フォーク
- 5.8k
- 平均マージ
- 2日 8時間
- マージ済み PR(30日)
- 597
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dotnet/runtime のほかの issue
-
area-VM-meta-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
area-Debugger-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
dotnet/runtime#138494 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-AssemblyLoader-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
dotnet/runtime#138493 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log call対応中かも @Mohamedadel-prog が 2 日前に担当しました。 オープンarea-Extensions-Logging untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
dotnet/runtime#138418 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
area-System.IO untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
dotnet/runtime#138417 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
juandresrodca/AdminForge#69 ·
メンテナーはふだん 1 日以内に返信
-
Keep PublicAPI.Unshipped.txt sorted, with a guard, so concurrent API PRs stop conflicting on itオープンchore small tooling
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
BattletechModders/ModTek#256 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
Facepunch/sbox-public#12484 ·
メンテナーはふだん 2 日以内に返信