[mono] AOT profiler: prof_shutdown() closes the output FILE twice

未关闭 适合新手
#138,403 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
64/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c
领域
backend

调研方向

从 src/mono/mono/profiler/aot.c 开始,重点查看 prof_save()、prof_shutdown() 和 port= 命令处理程序;issue 标出了每次重复关闭的位置。追踪哪个函数负责每个 FILE* 并将其关闭,然后检查或添加针对关闭和套接字保存路径的测试覆盖。完成的标准是,所有描述的 profile 结束路径都能写入输出并退出,且不会重复关闭。

由索引模型根据 Issue 内容生成。

描述

area-Codegen-AOT-mono untriaged
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.

  1. 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.proj
    

    The 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=0 is baked into mono-sgen by asan-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).

  2. 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
    
  3. Compile PoC.cs to PoC.dll (libraries from a Mono build of the same tree on MONO_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:4
    

    The helper thread ends the profile one second after the runtime is initialised.

attachment.zip

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, commit a3788b71495457354b64ca8040b6ded5ff6ca4f9), Mono runtime built from source with the attached hook, linux-x64, clang 18, Release flags plus -fsanitize=address.
  • main (commit 5f8157f1522a) has the same code; the line numbers below are for main, with release/10.0 in parentheses. The same report was reproduced on main@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().

主要语言
C#
星标
18.5k
派生
5.8k
平均合并
2 天 8 小时
30 天内合并 PR
597

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。