[mono] AOT profiler: prof_shutdown() closes the output FILE twice
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 64/100
Hướng nghiên cứu
Bắt đầu với src/mono/mono/profiler/aot.c, đặc biệt là prof_save(), prof_shutdown() và trình xử lý lệnh port=; issue chỉ ra từng lần đóng lặp lại. Theo dõi xem hàm nào sở hữu và đóng từng FILE*, sau đó kiểm tra hoặc bổ sung kiểm thử tập trung cho các đường đi tắt máy và lưu qua socket. Hoàn tất khi tất cả các đường đi kết thúc profile được mô tả đều ghi đầu ra và thoát mà không đóng hai lần.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
When the AOT profiler (src/mono/mono/profiler/aot.c) writes to a file, prof_shutdown() calls prof_save(), which writes the data and closes the file, and then closes the same FILE* again. This happens in every normal way of ending a profile: when duration= expires, when the method named by write-at-method= is compiled, and when a save command arrives on the port= socket (that path has the same double close at aot.c:286-288).
Reproduction Steps
On Linux the AOT profiler is not linked into mono-sgen (it is built as the static library mono-profiler-aot-static), so the attached harness links the runtime and the profiler into one executable and calls mono_profiler_init_aot() before initialising the runtime. That is how a host that takes AOT profiles uses this profiler. The profiler code itself is unmodified.
mono_profiler_init_aot ("aot:output=<file>,duration=1");
domain = mono_jit_init_version ("aotprof", "v4.0.30319");
/* ... */
mono_jit_exec (domain, assembly, 1, args);
Attached: aotprof_harness.c, aotprof-hook.cmake (adds the harness and links mono-profiler-aot-static), PoC.cs, and the build hook files.
Build (Mono runtime with AddressSanitizer, Linux x64, tag v10.0.15)
The Mono CMake build has no sanitizer switch, so the flags are injected with a CMake hook. All files are attached; <abs> stands for absolute paths on your machine.
-
Build the runtime (
asan-flags.cmake,asan-default-options.c):./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). -
Build the harness with its own hook:
cmake -S <repo>/src/mono -B <abs>/asan-obj \ "-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=<abs>/asan-flags.cmake;<abs>/aotprof-hook.cmake" \ -DAOTPROF_HARNESS_SRC=<abs>/aotprof_harness.c -DAOTPROF_HARNESS_NAME=aotprof_harness cmake --build <abs>/asan-obj --target aotprof_harness -
Compile
PoC.cstoPoC.dll(libraries from a Mono build of the same tree onMONO_PATH) and run: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/aotprof_harness <abs>/PoC.dll "aot:output=<abs>/out.aotprofile,duration=1" wait:4The helper thread ends the profile one second after the runtime is initialised.
Expected behavior
The profile file is written and the process exits normally.
Actual behavior
ASan reports a double free of the FILE object (asan.txt):
ERROR: AddressSanitizer: attempting double-free on 0x515000000080 in thread T4
#4 prof_shutdown src/mono/mono/profiler/aot.c:731:4 <- second fclose
#5 helper_thread src/mono/mono/profiler/aot.c:321:2
freed by thread T4 here:
#4 prof_shutdown src/mono/mono/profiler/aot.c:729:3 <- prof_save(), inlined
previously allocated by thread T0 here:
#4 mono_profiler_init_aot src/mono/mono/profiler/aot.c:387:27 <- fopen
Without ASan glibc aborts with free(): double free detected in tcache 2 (exit code 134).
The other ways of ending the profile behave the same on main@bdd9b750b70: output=#3,duration=0 (the fd form), port=<P> followed by a save command, and write-at-method=. A profile with none of duration, port or write-at-method never reaches prof_shutdown() and is clean.
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; the line numbers below are formain, withrelease/10.0in parentheses. The same report was reproduced onmain@bdd9b750b70.- v11.0.0 (tag
v11.0.0): same code (sources compared; not run).
Other information
Root cause
prof_save() closes the file it was given (src/mono/mono/profiler/aot.c:715-716 on main, 710-711 on release/10.0):
fwrite (prof->buf, 1, prof->buf_pos, file);
fclose (file);
prof_shutdown() passes prof->outfile to it and then closes it again (aot.c:733-737 on main, 728-732 on release/10.0):
if (prof->outfile || prof->send_to) {
prof_save (prof, prof->outfile);
if (prof->outfile)
fclose (prof->outfile);
}
The command handler for the port= mode repeats the pattern (aot.c:283-288 on main): fdopen(), prof_save(), fclose().
- Ngôn ngữ chính
- C#
- Star
- 18.5k
- Fork
- 5.8k
- Merge trung bình
- 2 ngày 8 giờ
- Pull request đã merge (30 ngày)
- 597
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/runtime
-
area-VM-meta-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area-Debugger-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
dotnet/runtime#138494 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-AssemblyLoader-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#138493 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log callCó thể đã có người làm @Mohamedadel-prog đã nhận 1 ngày trước. Đang mởarea-Extensions-Logging untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
dotnet/runtime#138418 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-System.IO untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#138417 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của dotnet/runtime
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
microsoft/sqltoolsservice#2851 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
elsa-workflows/elsa-foundation#6322 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
mohebifar/made-in-iran#273 ·
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with TakeCó thể đã có người làm @Mohamedadel-prog đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
dotnet/efcore#39848 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày