Skip to content

fix(env): launch #! scripts through the shared childproc resolver (Refs #1623) - #1628

Merged
caomengxuan666 merged 2 commits into
mainfrom
fix/1623-env-shebang-childproc
Nov 14, 2026
Merged

caomengxuan666 merged 2 commits into
mainfrom
fix/1623-env-shebang-childproc

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

niubash #1002 的第二断点:即使 env.exe 已在分发里(WinuxCmd #1622),
env <hook> 仍无法启动 #!/usr/bin/env bash 的 hook——

winuxcmd env ./s1.sh a
env: failed to run command './s1.sh': Permission denied    (rc 126)

git for Windows 的 parse_interpreter()(compat/mingw.c:1873-1907)会剥掉
shebang 的可选参数,所以 git 实际 spawn 的是 env <hook>;MSYS 的 env
经 execve 重读 shebang。niugit 没有 MSYS 层,所以 env 自身必须做 shebang
重读。

缺口在共享 helper 内,不在 env 的调用点:env.cpp 早已调
winux::childproc::plan_child_launch,但 src/utils/childproc.cppm 的
plan_child_launch 只处理原生镜像与 .cmd/.bat(经 COMSPEC),没有任何
shebang 处理——scan_candidates 对任意常规文件都报 Found,随后
CreateProcessW 以 126 死掉。

修法

src/utils/childproc.cppm(共享 helper,env/timeout/nohup/nice/stdbuf/
watch/sdiff/diff3/diff/find/xargs/install 共用)按
rust2/crates/winux-cmds/src/childproc.rs 的既定语义补齐:

  • classify_file:batch 扩展 → COMSPEC;MZ → 原生镜像;#! →
    resolve_shebang_script 递归解析;其余文件保持原样(仍交
    CreateProcessW,126——Rust 的 BadFormat "Exec format error" 不在本单
    范围)。
  • parse_shebang_line:首个 space/tab 切 interpreter,余部为 ONE 可选参数,
    尾 space/tab 修剪、CR 保留(CRLF shebang 命中不到 interpreter → 127);
    #! 为空或首行超 256 字节无换行 → 126;递归上限 3。
  • resolve_interpreter:裸名改写 ./name(cwd 相对,永不 PATH 搜索,
    GNU 实测 "#!env sh" → 127);A 原生链 → B shell-namespace mapper
    (shell_tree_to_native_w,#!/usr/bin/env 必经此路)→ C 叶名走 PATH。
  • plan_child_launch 增加 script 分支:lpApplicationName = 解析后的
    interpreter 路径(interpreter 是 .cmd/.bat 时走 COMSPEC),
    lpCommandLine = interpreter [opt-arg] script args...;链式脚本只追加
    外层 [arg?, script](内层脚本恰出现一次,childproc: shebang interpreter that is itself a script gets a duplicated argv token #1595);env -a 对脚本无
    效(内核模型丢弃 argv[0]),command_line_override 对脚本忽略(与
    batch 分支一致)。
  • src/utils/native_path.cppm:shell_tree_to_native_w 加 export
    (第二位消费者;Rust 侧对应函数本就公开)。
  • COMSPEC 取值抽为 comspec_path(),batch 分支行为不变。

非 #! 子进程的代码路径逐字节不变(classify 对其返回 Image,走原有
native 分支);新增开销仅为每次 spawn 一次 256 字节头读取。

钉

tests/unit/childproc/childproc_unit_test.cpp 新增 9 个(Pipeline 实 spawn):

  • env_shebang_script_launches_through_interpreter(原生 interpreter +
    可选参数,argv 顺序由 marker 证明)
  • env_shebang_script_with_posix_interpreter_resolves_via_shell_root
    (#!/usr/bin/env echo + NIU_ROOT 桩根——即 [gap] factor: 0 and 1 are printed with themselves as factors #1002 hook 形态)
  • env_shebang_script_missing_interpreter_reports_127
  • env_shebang_script_crlf_line_misses_interpreter_reports_127
  • env_shebang_script_chain_appends_outer_tokens_once(childproc: shebang interpreter that is itself a script gets a duplicated argv token #1595)
  • env_shebang_script_with_batch_interpreter_routes_through_comspec
  • env_shebang_script_bare_hashbang_reports_126
  • env_shebang_script_overlong_first_line_reports_126
  • timeout_shebang_script_launches_through_interpreter(第二调用方)

验收

本 PR 删除了什么

  • 无产品代码删除。plan_child_launch 的 batch 分支内联 COMSPEC 取值改为
    调 comspec_path()(行为不变);其余为新增代码与测试。
  • 未改动:非 #! 子进程的解析/启动路径;env.cpp 调用点(无需改);
    Rust childproc.rs(已是参考实现,未动)。

UNVERIFIED-BY-CAPTAIN

  • 队长复核未完成;本 PR 为草稿,勿合入。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 14, 2026 07:41
caomengxuan666 added a commit that referenced this pull request Nov 14, 2026
…historical snapshot (#1629)

The checkpoint section records the acceptance state as audited by the
lead review: current main had 50 commands wired (not the historical
76/42 numbers), A1/tail/ABI-v3 are not accepted yet, I18N is an
acceptance prerequisite, and the safety record for preserve-root
(chmod/chgrp/chown) blocks wiring until the listed gates pass.

Captain addendum records same-day movement: #1627 merged, PRs
#1624/#1625/#1626/#1628 open, and the fix/batch-1595-1616-1617 trio.
caomengxuan666 and others added 2 commits November 14, 2026 15:45
…busybox verb (Refs #1623)

The three failing x64-default ctest cases copied the multicall
winuxcmd.exe to interp.exe and assumed argv[1] verb dispatch. The
multicall dispatches by exe stem (main.cpp:478 wmain ->
path.cppm:16 get_executable_name; Mode 2 stem dispatch, no argv[1]
fallback), so 'interp' fell to 'command not found' 127. Stage the
interpreter under its own registered name winuxcmd.exe (Mode 1 verb
form) — the [interpreter, opt-arg, script, argv] shape the cases
verify is unchanged.

Test-only; product code untouched. Local default-edition build clean;
childproc.* 26/26, env.*/timeout.* 80/80; the 20 remaining local
gtest failures are host-environment-only (nice/nohup PATH resolution,
NIU_ROOT layout pins) and all pass on CI x64 default.
@caomengxuan666
caomengxuan666 force-pushed the fix/1623-env-shebang-childproc branch from 9f2e589 to 4336897 Compare November 14, 2026 07:46
@caomengxuan666

Copy link
Copy Markdown
Member Author

队长复核 — WinuxCmd#1628(Refs #1623):机制 PASS,两处 shebang 语义需改后合入

#! 走共享 childproc 解析器这个方向是对的,9 个新钉把 [interpreter, arg?, script, argv…]、链式脚本、CRLF、缺解释器、batch 解释器、第二调用方都钉住了。我在本机(head 4336897 的新鲜构建)复跑 --gtest_filter=childproc.* → 26/26 全绿(含 9 个新增)。相邻套件 env/timeout/nice/nohup/stdbuf 123 项 118 绿,5 红均与本 diff 无关:2 个 nice 是本地 link 集缺 true.exe(用存在的命令跑同一路径 rc 0),3 个 nohup 是本工具 shell stdin 非 tty 触发 nohup: ignoring input(功能输出本身正确)。CI 的 oracle / rust-tests / baseline / i18n 与 x64-full / arm64-default / arm64-full 均绿,复查时 x64-default 仍在跑。

需要改的语义(GNU oracle 实测)

用 WSL GNU(kernel 6.18.33.2-microsoft-standard-WSL2,coreutils 9.4 env)逐字节复测:

形态 GNU env 本实现 判定
#!/bin/echo + 300×x + \n(解释器存在) rc 0,启动解释器,arg 在 256 字节缓冲处截断 rc 126 分歧
#!/nonexistent/interp + 300×x rc 127(ENOENT) rc 126 分歧
#!\n / #! \n raw exec = ENOEXEC;GNU env 经 execvp 的 /bin/sh 回退 → rc 0 rc 126 边界内(见下),但 pin 的 GNU 说法要改
CRLF #!/bin/echo\r\n rc 127 rc 127 一致
短文件无尾换行 #!/bin/echo rc 0,启动 rc 0,启动 一致

结论:“256 字节内没有换行就不是脚本(126)”不是当前内核 binfmt_script 的模型。内核在 256 字节缓冲内解析,解释器取得到的就启动(参数截断),所以 env_shebang_script_overlong_first_line_reports_126 这条钉把错误模型固化成了“GNU 锚点”,会挡住以后正确的修复。

#!\n 的 126 可以保留——GNU 的 0 来自 glibc execvp 的 /bin/sh 回退,而 rust2 参考实现已把“execvp 回退不在契约内”写成文档边界。但 pin 名/注释必须写成产品边界,不能写成 GNU/内核结论(内核给的是 ENOEXEC,不是 126 的直接来源)。

要求的修改

  1. parse_shebang_line:去掉 line_end == body.len() && head.len() == HEAD_BYTES → None 这条拒绝;缓冲内没有换行时按缓冲末尾收行(保留现有的尾随 space/tab 修剪),解释器取不到才落 126/127 的既有分类。这样长行会启动解释器、缺解释器自然落 127,与实测一致。
  2. 对应改钉:env_shebang_script_overlong_first_line_reports_126 换成“长行仍启动解释器(参数截断)”的正向钉;bare_hashbang 的注释改成“产品 BadFormat 边界(libc execvp 回退不在契约内)”。
  3. 同步 Rust 参考实现 rust2/crates/winux-cmds/src/childproc.rs(同一 256 规则、shebang_overlong_first_line_is_not_a_script 及其注释),否则 C++/Rust 会当场分叉。
  4. 改完重跑 childproc 全量,附长行正向形的实跑输出。

另外记录

  • Pass C(POSIX-rooted 解释器不存在时取叶名走 PATH)是对内核的有意让步:GNU 对 #!/nonexistent/interp 是 127,而本实现若 PATH 上有 interp.exe 会启动它。代码注释写了,但建议在测试/文档里也记一条“已知让步”,别被当成 parity。
  • 本 PR 是本分支上非脚本 childproc 路径的零行为变更声明成立:classify_file 对非 #!/非 MZ 文件仍返回 Image,原有 126 面未动。

@caomengxuan666
caomengxuan666 merged commit 35f4864 into main Nov 14, 2026
15 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 14, 2026
…r (Refs #1628 #1623) (#1664)

The merged #1628 modeled an overlong first line as ENOEXEC (126), reading
"the line fills BINPRM_BUF_SIZE with no newline" as "not a script". GNU does
not reject it: exec.c zero-fills a BINPRM_BUF_SIZE (256) buffer but reads
only BINPRM_BUF_SIZE - 1 (255) bytes into it, and binfmt_script.c parses the
line inside that window. Past the window the line is truncated at the edge
and the script still launches.

Parse within the 255-byte window in both implementations. The only
remaining nullopt/None cases are a `#!` with no interpreter token and the
recursion guard. The bare `#!` stays 126 as a product BadFormat boundary:
GNU env reaches 0 only through the libc execvp /bin/sh fallback, while raw
exec gets ENOEXEC.

Pins:
- tests/unit/childproc: the overlong-line 126 pin becomes a positive launch
  pin that asserts the optional argument is cut to exactly the 255-byte
  window (240 x's after "#!./interp.cmd "); a long line whose interpreter is
  missing stays 127. The bare-`#!` pin is reworded as the product boundary.
- rust2 childproc: the same replacement, exact argv pinned to
  SHEBANG_LINE_BYTES - 10, plus the missing-interpreter 127 case.

Probed WSL GNU 6.18 / GNU env 9.4: `#!/bin/echo ` + 300 x's -> rc 0 with a
243-byte argument; `#!/nonexistent/interp` + 300 x's -> 127; bare `#!` ->
env 0 (execvp fallback) vs raw exec ENOEXEC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant