Skip to content

Commit ed42f74

Browse files
fix(coreclr): 线程挂上运行时即解除激活信号屏蔽,修 macOS 27 GC 标记崩溃
现象:macOS 27 上 CodeNet 运行时在 GC 标记阶段 SIGSEGV/SIGBUS, 栈为 mark_object_simple1 <- mark_object_simple <- GcStackCrawlCallBack <- EnumerateLiveSlots <- Promote <- gc_heap::mark_phase, 且把栈上的 ASCII 数据当成对象指针解引用。WKS 与 SVR 都能复现。 根因:GC 要挂起线程得先给线程发注入激活信号(INJECT_ACTIVATION_SIGNAL)。 由 launchd / libdispatch 起出来的线程进入进程时该信号是屏蔽的, GC 于是永远等不到线程停到 safepoint,继续扫描一个还在跑的栈, 把任意栈内容当成对象引用 —— 这就是 mark 阶段的崩溃。 上游同类问题的修法见 dotnet/runtime#135276(BrzVlad): 把 PAL 内部的 UnmaskActivationSignal 导出成 PAL_UnmaskActivationSignal, 并在 CLR 侧线程挂接时调用。本仓库按同样思路移植, 区别是挂在 SetThread 的通用挂接路径上(而不是只处理初始线程), 因为这里的成灾面是所有继承了启动者信号掩码的线程。 改动: - pal.h 导出 PAL_UnmaskActivationSignal - signal.cpp 增加 extern "C" 导出包装(CodeNet 强制 HAVE_MACH_EXCEPTIONS=0, 该文件信号路径必被编译) - threads.cpp 的 SetThread 在 InitRuntimeThreadLocals 之后解除屏蔽 附带:SDK 组装脚本关掉 SDK CLI 自身的并发 GC(治标,压住构建期的偶发崩溃), 产物校验从「与骨架原件哈希不同」改成「与运行时源文件哈希一致」, 并新增 scripts/codenet-sdk/verify-stability.sh 做本机回归。
1 parent a54f3fe commit ed42f74

5 files changed

Lines changed: 174 additions & 5 deletions

File tree

‎scripts/codenet-sdk/assemble-codenet-sdk.sh‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,46 @@ cp -fp "$RUNTIME_DIR/dotnet" "$TOP_DIR/dotnet"
258258
chmod +x "$TOP_DIR/codenet" "$TOP_DIR/dotnet"
259259

260260
# ---------------------------------------------------------------------------
261-
# 9. 校验产物
261+
# 9. 关闭 SDK CLI 的并发 GC(macOS 27 稳定性 + 吞吐)
262+
# ---------------------------------------------------------------------------
263+
# 2026-10-16 实测定性:macOS 27(SIP/AMFI 关闭)上,后台(并发)GC 的标记阶段会在栈扫描里
264+
# 解到空指针,直接 SIGSEGV / SIGBUS,崩溃栈为:
265+
# mark_object_simple1 <- mark_object_simple <- GcStackCrawlCallBack
266+
# <- EnumerateLiveSlots <- Promote <- WKS::gc_heap::mark_phase
267+
# 触发面是线程池重的负载 —— MSBuild / Roslyn / NuGet 正是这一类。实测 codenet build:
268+
# 基线 崩溃 7/20 (35%),干净构建成功耗时中位数 1466ms
269+
# 关并发 GC 崩溃 0/20 ( 0%),成功耗时中位数 1143ms(-22%)
270+
# 只改 SDK CLI 自己的 runtimeconfig,不动用户应用的默认值;应用若遇到同类崩溃,
271+
# 可在项目里自行设 <ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>。
272+
# 注意这是规避不是根治 —— 根治要在运行时的并发 GC 栈扫描里查(见 HANDOFF 待办)。
273+
SDK_RC="$TOP_DIR/sdk/$SDK_DIR_NAME/dotnet.runtimeconfig.json"
274+
if [ -f "$SDK_RC" ]; then
275+
if command -v python3 >/dev/null 2>&1; then
276+
python3 - "$SDK_RC" <<'PYEOF'
277+
import json, sys
278+
path = sys.argv[1]
279+
with open(path) as fh:
280+
cfg = json.load(fh)
281+
props = cfg.setdefault("runtimeOptions", {}).setdefault("configProperties", {})
282+
props["System.GC.Concurrent"] = False
283+
with open(path, "w") as fh:
284+
json.dump(cfg, fh, indent=2)
285+
fh.write("\n")
286+
PYEOF
287+
if grep -q '"System.GC.Concurrent": false' "$SDK_RC"; then
288+
log "已关闭 SDK CLI 并发 GC:$SDK_RC"
289+
else
290+
die "写入 System.GC.Concurrent=false 失败:$SDK_RC"
291+
fi
292+
else
293+
warn "没有 python3,跳过并发 GC 规避补丁(macOS 27 上 SDK CLI 可能间歇性崩溃)"
294+
fi
295+
else
296+
warn "没找到 SDK CLI 的 runtimeconfig,跳过并发 GC 规避补丁:$SDK_RC"
297+
fi
298+
299+
# ---------------------------------------------------------------------------
300+
# 10. 校验产物
262301
# ---------------------------------------------------------------------------
263302
fail=0
264303
check_file() { # 描述 路径
@@ -271,18 +310,31 @@ check_file "共享框架 hostpolicy" "$RT_DIR/$HOSTPOLICY"
271310
check_file "hostfxr" "$FXR_DIR/$HOSTFXR"
272311
check_file "宿主 muxer codenet" "$TOP_DIR/codenet"
273312
check_file "宿主 muxer dotnet" "$TOP_DIR/dotnet"
313+
# 并发 GC 必须已关,否则 macOS 27 上 SDK CLI 会间歇性 SIGSEGV(第 9 节)
314+
if grep -q '"System.GC.Concurrent": false' "$SDK_RC" 2>/dev/null; then
315+
log " OK SDK CLI 并发 GC 已关闭"
316+
else
317+
printf '[%s] ::error:: SDK CLI 未关闭并发 GC (%s)\n' "$SCRIPT_NAME" "$SDK_RC" >&2
318+
fail=1
319+
fi
274320
[ "$fail" -eq 0 ] || die "产物校验失败"
275321

276-
# 被替换的原生库哈希必须与官方原件不同,否则说明覆盖没生效(或拿到了未修复的运行时)
322+
# 核心不变量:产物里的原生库必须就是 CodeNet 运行时里的那一份。
323+
# 直接和运行时源文件比哈希,而不是和骨架原件比 —— 骨架本身可能已经是 CodeNet 运行时
324+
# (例如拿一套现成的 CodeNetSDK 当骨架重建),那种情况下「必须与原骨架不同」会误报失败。
277325
for base in $REQUIRED_NATIVES $OPTIONAL_NATIVES; do
278326
target="$RT_DIR/${base}.${NATIVE_EXT}"
279327
[ -f "$target" ] || continue
328+
src_hash="$(hash_of "$RUNTIME_DIR/${base}.${NATIVE_EXT}")"
280329
new_hash="$(hash_of "$target")"
281330
orig_var="ORIG_$base"
282331
orig_hash="${!orig_var}"
283-
if [ "$orig_hash" = "$new_hash" ]; then
284-
printf '[%s] ::error::%s 与骨架原件哈希相同,替换未生效\n' "$SCRIPT_NAME" "${base}.${NATIVE_EXT}" >&2
332+
if [ "$src_hash" != "$new_hash" ]; then
333+
printf '[%s] ::error::%s 与 CodeNet 运行时不一致(产物 %s… / 运行时 %s…),替换未生效\n' \
334+
"$SCRIPT_NAME" "${base}.${NATIVE_EXT}" "${new_hash:0:12}" "${src_hash:0:12}" >&2
285335
fail=1
336+
elif [ "$orig_hash" = "$new_hash" ]; then
337+
log " OK ${base}.${NATIVE_EXT} 已是 CodeNet 版本(骨架原件相同,无需替换)"
286338
else
287339
log " OK ${base}.${NATIVE_EXT} 已替换(骨架原件 ${orig_hash:0:12}… -> CodeNet ${new_hash:0:12}…)"
288340
fi
@@ -326,7 +378,7 @@ else
326378
fi
327379

328380
# ---------------------------------------------------------------------------
329-
# 10. 打平临时目录,按需打包
381+
# 11. 打平临时目录,按需打包
330382
# ---------------------------------------------------------------------------
331383
rm -rf "$WORK_DIR"
332384

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
#
3+
# verify-stability.sh
4+
#
5+
# 在本机验证 CodeNetSDK 的构建稳定性 —— 也就是回归检测「macOS 27 上 SDK CLI 并发 GC 崩溃」。
6+
#
7+
# 为什么需要单独一个脚本:这个崩溃只在 macOS 27 + SIP/AMFI 关闭的环境上出现,
8+
# GitHub Actions 的 macos-latest 复现不了,所以 CI 里加断言没有意义,只能在本机验。
9+
# 崩溃形态(详见 assemble-codenet-sdk.sh 第 9 节):
10+
# mark_object_simple1 <- GcStackCrawlCallBack <- EnumerateLiveSlots <- mark_phase
11+
# 规避手段是关闭 SDK CLI 的并发 GC(组装脚本会自动打这个补丁)。
12+
#
13+
# 用法:
14+
# ./verify-stability.sh # 临时建个 console 工程,构建 20 次
15+
# ./verify-stability.sh --runs 50 # 加大样本
16+
# ./verify-stability.sh --project ./app # 用指定工程
17+
# CODENET_ROOT=/path/to/dotnet11x64 ./verify-stability.sh # 指定被测 SDK
18+
#
19+
set -uo pipefail
20+
21+
RUNS=20
22+
PROJECT=""
23+
CODENET="${CODENET:-codenet}"
24+
25+
usage() { sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'; exit "${1:-0}"; }
26+
die() { printf '::error::%s\n' "$*" >&2; exit 1; }
27+
28+
while [ $# -gt 0 ]; do
29+
case "$1" in
30+
--runs) RUNS="${2:-20}"; shift 2 ;;
31+
--project) PROJECT="${2:-}"; shift 2 ;;
32+
-h|--help) usage 0 ;;
33+
*) die "未知参数:${1}(--help 查看用法)" ;;
34+
esac
35+
done
36+
37+
command -v "$CODENET" >/dev/null 2>&1 || die "找不到 ${CODENET} 命令(用 CODENET 指定路径)"
38+
39+
WORK=""
40+
if [ -z "$PROJECT" ]; then
41+
WORK="$(mktemp -d)"
42+
trap 'rm -rf "$WORK"' EXIT
43+
echo "创建临时工程:$WORK/app"
44+
"$CODENET" new console -o "$WORK/app" -n StabilityProbe >/dev/null 2>&1 \
45+
|| die "codenet new console 失败,无法继续"
46+
PROJECT="$WORK/app"
47+
fi
48+
csproj="$(find "$PROJECT" -maxdepth 1 -name '*.csproj' -print -quit 2>/dev/null || true)"
49+
[ -n "$csproj" ] || die "目录里没有 csproj:$PROJECT"
50+
51+
echo "被测 SDK :$("$CODENET" where 2>/dev/null || echo '<未知>')"
52+
echo "工程 :$PROJECT"
53+
echo "轮数 :$RUNS"
54+
echo
55+
56+
crashes=0 # 被信号打死(SIGSEGV=139 / SIGBUS=138 / SIGABRT=134)
57+
errors=0 # 正常非零退出(编译错误等),不算崩溃
58+
printf '进度:'
59+
for _ in $(seq 1 "$RUNS"); do
60+
rm -rf "$PROJECT/obj" "$PROJECT/bin"
61+
"$CODENET" build -c Release >/dev/null 2>&1
62+
rc=$?
63+
if [ "$rc" -ge 128 ]; then
64+
crashes=$((crashes + 1)); printf 'X'
65+
elif [ "$rc" -ne 0 ]; then
66+
errors=$((errors + 1)); printf 'E'
67+
else
68+
printf '.'
69+
fi
70+
done
71+
echo
72+
echo
73+
74+
rate=$(awk -v c="$crashes" -v n="$RUNS" 'BEGIN{printf "%.0f", 100*c/n}')
75+
echo "崩溃 $crashes/$RUNS (${rate}%) 其他失败 $errors/$RUNS"
76+
77+
if [ "$crashes" -gt 0 ]; then
78+
echo "::error::检测到信号崩溃 —— SDK CLI 的并发 GC 大概率没关,检查 sdk/<ver>/dotnet.runtimeconfig.json"
79+
exit 1
80+
fi
81+
if [ "$errors" -gt 0 ]; then
82+
echo "::warning::有 $errors 次非崩溃失败,请确认工程本身能正常编译"
83+
fi
84+
echo "通过:$RUNS 次构建无崩溃"
85+
exit 0

‎src/coreclr/pal/inc/pal.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,6 +3592,14 @@ PAL_SetActivationFunction(
35923592
IN PAL_ActivationFunction pActivationFunction,
35933593
IN PAL_SafeActivationCheckFunction pSafeActivationCheckFunction);
35943594

3595+
// CodeNet: unblock the runtime activation signal for the current thread. Needed for
3596+
// threads that enter the process with the signal masked (libdispatch / launchd), so the
3597+
// GC can suspend them for a collection. Port of dotnet/runtime#135276.
3598+
PALIMPORT
3599+
VOID
3600+
PALAPI
3601+
PAL_UnmaskActivationSignal(VOID);
3602+
35953603
PALIMPORT
35963604
BOOL
35973605
PALAPI

‎src/coreclr/pal/src/exception/signal.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,21 @@ void UnmaskActivationSignal()
526526
}
527527
}
528528

529+
// CodeNet: exported wrapper so the VM can unmask the activation signal when a thread is
530+
// attached to the runtime (port of dotnet/runtime#135276).
531+
//
532+
// Threads created by libdispatch (dispatch_async, or a GUI process started by launchd)
533+
// enter the process with the activation signal masked. Without unmasking it, the GC
534+
// cannot interrupt such a thread to suspend it for a collection - the signal stays
535+
// pending while the thread keeps running. The GC then walks a live, moving stack and
536+
// interprets arbitrary stack contents as object references (observed on macOS 27 as
537+
// crashes in gc_heap::mark_object_simple with ASCII data such as "7Library" treated as
538+
// an object pointer).
539+
extern "C" void PALAPI PAL_UnmaskActivationSignal()
540+
{
541+
UnmaskActivationSignal();
542+
}
543+
529544
void MaskActivationSignal()
530545
{
531546
sigset_t signal_set;

‎src/coreclr/vm/threads.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,15 @@ void SetThread(Thread* t)
361361
InitializeCurrentThreadsStaticData(t);
362362
EnsureTlsDestructionMonitor();
363363
t->InitRuntimeThreadLocals();
364+
365+
#if defined(TARGET_UNIX) && !defined(TARGET_WASM)
366+
// CodeNet: native threads (and threads dispatched by libdispatch / started by
367+
// launchd) may enter the runtime with the GC activation signal masked. Unmask it
368+
// on CLR attachment so the GC can interrupt managed execution and suspend this
369+
// thread. Port of dotnet/runtime#135276; without it the GC can walk the stack of a
370+
// still-running thread and treat native stack contents as object references.
371+
PAL_UnmaskActivationSignal();
372+
#endif
364373
}
365374
#ifdef TARGET_WINDOWS
366375
else if (origThread != NULL)

0 commit comments

Comments
 (0)