[mono][debugger] compute_frame_info_from() leaks the frame list after CMD_THREAD_SET_IP
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
- 75/100
Hướng nghiên cứu
Đọc src/mono/mono/component/debugger-agent.c, bắt đầu với compute_frame_info_from() và hàm cùng cấp compute_frame_info(), hàm giải phóng danh sách frame của nó. Theo dõi cách đường dẫn khôi phục trạng thái được gọi sau CMD_THREAD_SET_IP, sau đó chạy bản tái hiện được cung cấp drive_case3.py và so sánh với chế độ breakpoint. Hoàn thành khi các yêu cầu thông tin frame lặp lại sau SET_IP không còn làm bộ nhớ tăng thêm một nút danh sách cho mỗi frame.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
When the soft debugger agent computes the frames of a stopped thread from its "restore state" (the path taken after the debugger changed the instruction pointer with CMD_THREAD_SET_IP), it builds a GSList of frames and copies the pointers into the result array, but never frees the list. Every stack read on such a stop leaks one GSList node per frame. The sibling path, compute_frame_info(), frees its list.
Reproduction Steps
Managed program plus a small debugger-protocol client, both attached (PoC.cs, sdb.py, drive_case3.py).
-
PoC.csbuilds a 30-deep managed call stack and then loops, so the thread can be stopped on a deep stack. -
Start it with the debugger agent listening:
<abs>/asan-obj/out/bin/mono-sgen --debugger-agent=transport=dt_socket,address=127.0.0.1:<port>,server=y,suspend=y <abs>/PoC.dll <abs>/run-dir 30 -
Run the client (
drive_case3.py --port <port> --run <abs>/run-dir --requests 4000 --sample-every 1000 --mode breakpoint-setip --il-offsets 1,2,3,4). It connects, sets breakpoints in the PoC methods, resumes, waits for a breakpoint hit with at least 15 frames, sendsCMD_THREAD_SET_IPto another IL offset of the same method, and then sendsCMD_THREAD_GET_FRAME_INFO4000 times while sampling the target's RSS. -
Control session: the same with
--mode breakpoint(noSET_IP).
Build (Mono runtime with AddressSanitizer, Linux x64, tag v10.0.15)
The leak does not need ASan; any Mono build shows it. To use the same build as the attached numbers:
-
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 it withASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp. -
Build the Mono-flavored managed framework from the same tree and put the assemblies on
MONO_PATH(steps in the attachedMANAGED-BUILD.md); add them toMONO_PATHandLD_LIBRARY_PATHin the command above. -
Compile
PoC.cstoPoC.dllagainst those assemblies.
Expected behavior
Reading the stack frames of a stopped thread does not allocate memory that is never released; RSS stays flat while the thread is stopped.
Actual behavior
RSS grows by about one 32-byte GSList node per frame per request (measurements.txt, build without ASan, 33-frame stack, 4000 requests each):
| session | RSS growth | bytes per request |
|---|---|---|
| breakpoints, stack reads only (control) | 428 KiB | 109.6 |
breakpoints, SET_IP, then stack reads |
4660 KiB | 1193.0 |
The control's ~100 bytes per request are a separate, smaller leak that I noticed on the way: mono_debugger_log_command() formats a message with g_strdup_printf() and never frees it (src/mono/mono/component/debugger-state-machine.c:100). The trigger adds 33 × 32 bytes on top. The growth is linear in the number of requests. There is no ASan report: this is a plain leak.
Regression?
No response
Known Workarounds
No response
Configuration
- .NET 10.0.15 (tag
v10.0.15, commita3788b71495457354b64ca8040b6ded5ff6ca4f9), Mono runtime built from source, linux-x64, protocol version 2.68 announced by the client. main(commit5f8157f1522a) has the same code; the line numbers below are formain, withrelease/10.0in parentheses. The same leak was measured onmain@bdd9b750b70.- v11.0.0 (tag
v11.0.0): same code (sources compared; not run).
Other information
Root cause
The stack walk callback appends one node per frame (src/mono/mono/component/debugger-agent.c:2974, 2977 on release/10.0):
ud->frames = g_slist_append (ud->frames, frame);
compute_frame_info_from() (debugger-agent.c:3037-3061, 3040-3064 on release/10.0) walks the stack, copies the frame pointers into the result array and returns — user_data.frames is never freed:
mono_walk_stack_with_state (process_frame, state, opts, &user_data);
nframes = g_slist_length (user_data.frames);
res = g_new0 (StackFrame*, nframes);
l = user_data.frames;
for (i = 0; i < nframes; ++i) {
res [i] = (StackFrame *)l->data;
l = l->next;
}
*out_nframes = nframes;
return res;
The sibling function compute_frame_info() frees its list at the end (g_slist_free (user_data.frames);, debugger-agent.c:3153, 3156 on release/10.0), which shows that the ownership rule is "caller frees". It calls compute_frame_info_from() when the thread's restore state is valid and its IP differs from the thread's current IP (debugger-agent.c:3078-3080, 3081-3083 on release/10.0) — exactly the state left behind by CMD_THREAD_SET_IP. Stack reads on an ordinary suspend take the other path and do not leak.
- 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-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
-
area-Codegen-AOT-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
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