Skip to content

fix(executor): spawn foreign shebang interpreters on direct exec (Refs #3415) - #3443

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3415-nonshell-shebang-direct-exec
Nov 15, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3415-nonshell-shebang-direct-exec

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Refs #3415。

现象

Windows 上直接执行带非 shell 解释器 shebang 的脚本时,引擎把文件当 bash 脚本解析:非 shell 体报语法错(rc=2,报错头是脚本名);shebang-only 文件静默 rc=0。Git Bash 对照:正常派生解释器执行。

根因

Windows 没有内核 #!,直执行分两条路都把文件吞进 shell:

  1. 前台路径 execute_same_shell_script(external_finish.rs):direct_windows_shell_script_path 认出无扩展名文本文件后,exec_format_refusal 只对不可解析的解释器拒绝——node 能解析就放行,随后 execute_direct_shell_script 在进程内按 bash 解析。
  2. 派生 funnel(external_command_for_named_program_with_exec_name,path.rs):should_run_with_shell 的文件一律经 shell_wrapped_command 用 sh 包裹;exec 内置(builtins/exec.rs)直接调同一包裹函数。

改动

  • src/executor/path.rs:新增 foreign_shebang_interpreter(解析 #! 行:解释器词 + 至多一个可选参数词;/usr/bin/env X 走目标词判定;shell 解释器与不可解析的解释器返回 None)与 shebang_interpreter_command(拼出 [解释器] [可选参数] [脚本原样拼写] [原参数...] 的派生命令;WinuxCmd dispatcher 解析到的解释器按 funnel 旧例补 applet 名)。funnel 在 shell_wrapped_command 之前先试这条分支。
  • src/executor/external_finish.rs:execute_same_shell_script 在拒绝检查之后、直接执行之前,对可解析的非 shell shebang 拒绝(返回 Ok(false)),让普通磁盘命令路径去派生解释器。
  • src/builtins/exec.rs:exec 内置在 shell 包裹之前先试解释器派生。
  • sh/bash/dash/rubash shebang 与无 shebang 文件保持原路径;unix 上 should_run_with_shell 恒为假,新分支不可达,内核继续拥有 #!。

GNU 锚点

  • execute_cmd.c:6126 shell_execve:先 execve,只有无 shebang 的文本文件才落入 ENOEXEC 后的 shell 执行(:6225-6260)。
  • execute_cmd.c:6001-6043 execute_shell_script:无 hash-bang exec 的系统经具名解释器重执行,解释器为 #! 后第一个词、至多一个参数词、随后是脚本名。
  • execute_cmd.c:5969 getinterp:跳过 #! 后空白,取一个空白分隔词;execute_cmd.c:5957-5964 MSDOS 分支把 CR 当行结束符(与既有拒绝分类器的 split_whitespace 解析一致)。

回归钉

tests/issue3415_nonshell_shebang_direct_exec.rs(Windows only,9 个用例):直接解释器 + 可选参数 + 原参数(以 scratch 目录下的 batch 文件为解释器,逐字节断言 argv)、shebang-only 不再静默、/usr/bin/env hostname 启动器臂、绝对路径解释器、cmd.exe /c 单参数臂、sh/bash shebang 与无 shebang 锁定、exec 内置面、管道段面。

本 PR 删除了什么

无。纯加法:新增两个 pub(crate) 助手与一处拒绝分支,未删除任何函数、测试或公开面。

UNVERIFIED-BY-CAPTAIN

  • 本车道在 VM 上无 Rust 工具链且本地构建被禁,未能在本地跑钉与 lib 腿;CI 为首验,全腿(X299 Windows 面 + Linux 侧 GNU oracle 对照)由复核轮跑。
  • 钉的 fail-first(base 上 7 红 2 绿,两锁定用例在 base 即绿)尚未实跑,待复核轮在 base 树上核对。
  • 近似点:#! 行多于一个参数词时只取第一个(与 GNU execute_shell_script 一致;Linux 内核则把余行当单个参数传给 env);env 本体缺失但目标可解析时直接派生目标(env 在此只做 PATH 查找);解释器 argv[0] 取解析后的 Windows 路径而非 shebang 原拼写。

…#3415)

Windows has no kernel #!, so a directly-executed script naming a
non-shell interpreter (#!/usr/bin/env node, #!/bin/echo ...) was
parsed as bash: a non-shell body died with a syntax error (rc 2)
and a shebang-only file exited 0 in silence. Spawn the named
interpreter with the script as its operand instead (the MSYS/Git
Bash model), on the foreground, pipeline-stage and exec-builtin
faces; sh/bash/dash/rubash shebangs and shebangless files keep the
shell path.

GNU anchor: execute_cmd.c:6126 shell_execve (exec first; ENOEXEC
tail only for shebangless text, :6225-6260) + execute_shell_script
(:6001-6043, re-exec through the NAMED interpreter, one optional
argument word) + getinterp (:5969). Regression nails:
tests/issue3415_nonshell_shebang_direct_exec.rs (9 tests, Windows
only; unix lets the kernel own #!).

UNVERIFIED-BY-CAPTAIN
@caomengxuan666

Copy link
Copy Markdown
Member Author

Captain review — FAIL(返工:cargo fmt)

复核员自跑核对(box 满盘、老服务器 banner-dead,本轮腿走本地写墙探针门后的本地构建;Windows 钉执行见下):

产品机理:对。 三面落点均闭合:前台 execute_same_shell_script 在 exec_format_refusal 之后以 Ok(false) 拒绝(调用方 handle_external_shortcuts 落入磁盘命令路径);派生 funnel 在 should_run_with_shell 臂内、shell_wrapped_command 之前先试解释器派生;exec 内置同理。GNU 锚点逐条核实:execute_shell_script(execute_cmd.c:6001-6043,解释器词 + 一可选参数词 + 脚本名)、getinterp(:5969)、shell_execve(:6126)均与所引一致。不可解析解释器在三面均先被 exec_format_refusal 以 126/127 拒绝,新分支只接可解析者。

返工项(窄面,机械):

  1. cargo fmt --check 失败(rc=1,9 处 diff,全在 PR 新/改码):src/builtins/exec.rs:887、src/executor/path.rs:2413、src/executor/path.rs:2467、钉文件 6 处(:117/132/148/172/194/203,write(...) 超宽未折行)。队长在头树 bfc240f9 上亲跑 cargo fmt --check,diff 位置与复核员所列逐处一致。请 cargo fmt 后单提交推新头,并确认返工 diff 仅为空白。
  2. Windows 钉从未执行过(合入前置):钉 stem #![cfg(windows)];CI 的 Rust tests 腿日志全文无 issue3415 字样(CI 不跑新回归钉 stem:Rust tests 只跑精选 nextest 表 #3430 式精选表缺口),车道亦未实跑。合入前须在 X299 上实跑:head 9/9,base 同 stem fail-first 7 红 / 2 绿(逐格核对红形)。

其余已核(细目见 ~/workspace/review-3443/CAPTAIN-VERIFY.md):

  • 终头 CI 10 pass + 1 skipping(runs 43242494950/43242495071),头 bfc240f9 未变;Windows tests compile check 腿为 cargo check --tests --target x86_64-pc-windows-msvc(全量 test target 含新钉,编译通过——只证编译,不证执行)。
  • marker-lint OK(117 consts / 268 sites);全树 UTF-8 卫生 clean。
  • 活尖 e50cea96 交集 ∅(队长发帖时重算:区间 c28a9dd..e50cea96 15 文件,与 PR 的 4 文件无交集;合入轮仍须 update-branch 后再算一次);PR 删零符号(fix(pipeline): keep merged-stream order in pipeline stages #3399 式悬空检查不适用);未碰红线文件。
  • 钉双边(Linux 0-test 门):base/head 钉二进制直跑均为 running 0 tests / ok(#![cfg(windows)] 门以执行证实);base bin sha 575f0efa57610371、head bin sha 33b2770d6c68fdad 均已落盘。
  • base lib:1238 passed; 8 failed; 4 ignored(8 名全在 api::consumer_tests,jobs/winsize/Session 面,疑似环境时序敏感;head 名集待返工轮取得后比对,判据为名集差 ∅)。
  • 三腿 BLOCKED(环境,非产品红,返工轮重跑):GNU 三方探针 11 形——写墙自 ~16:52Z 起持续关闭,探针 50+ 次连续尝试全被护栏拦截(PROBE-ABORT),本轮无任何一形结果;head lib——两次 OOM 杀死 + 一次写墙 EPERM,未建成,名集 diff 无从得出;-D warnings 强制重检——唯一一次尝试 0 次 rustc 调用(VOID),本轮无有效证据。首跑探针的 11 形全 SAME 已定性为 VOID(夹具未落盘的空输出),不计。

**合入条件(FAIL 解除后):**① fmt 返工新头 CI 全绿;② X299 钉 9/9 + base 7 红 / 2 绿;③ 合入轮在新头上重跑钉 + lib + -D warnings + 终头 CI;④ 本 PR 为 Refs-only,合并不关 #3415(队长手动处置)。

`cargo fmt --check` reported 9 violations in the PR's new/changed code
(exec.rs:887, path.rs:2413/:2467, pin :117/:132/:148/:172/:194/:203).
Whitespace-only reflow, no semantic change; `cargo fmt --check` now rc=0.
…3415)

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3443 第2轮 — 建议:PASS
head: 75c06d3 base(merge-base): c28a9dd 文件: 4 个
关联 issue: #3415(open,核于 2026-11-15)

机制核查:

  • 三面落点(前台 execute_same_shell_script / 派生 funnel / exec 内置)队长已逐条对 GNU 锚核过(execute_cmd.c:6001-6043/:5969/:6126),本轮未动产品码。
  • F1 复核:564ffbf4 确为 fmt-only(空白重排,3 文件);rustfmt --check 现干净。

腿(X299 = 本机 Windows 实跑,钉为 #![cfg(windows)] 专属):

  • 钉自跑 head:issue3415_nonshell_shebang_direct_exec 9 passed / 0 failed
  • fail-first(base c28a9dd + 钉):2 绿 / 7 红,红名 = {direct_absolute_path, direct_with_optarg_and_operands, env_launcher, exec_builtin, pipeline_stage, shebang_only_file_is_not_silent, single_optional_argument} —— 与队长预算 7R/2G 逐名一致(7 缺陷格 base 均以 line 2: syntax error / @echo: command not found 红,产品把 body 当 bash 解析;2 对照格绿)
  • lib 双边:head 1349/1/4 {issue2084_session_end_restores_host_process_state} / base 同集(同 merge-base 刚测于 fix(main): derive startup diagnostics from argv[0] (#3254) #3447 轮)/ 差集 ∅
  • -D warnings:钉目标 --no-run 0 错;lib --bins 前轮绿
  • 卫生:rustfmt 干净;marker-lint / utf8 前轮 OK
  • CI(前头 564ffbf):10 pass + 1 skipping,红名仅 issue2875 基线(同轮各 PR 一致)

钉更正(本轮实际执行后发现的 fixture 缺陷,test-only):

  • 3 个 hostname.exe 格:hostname 对 operand 不「忽略 argv」而尝试 setname → NCP 诊断入 stderr。钉改判 !stderr.contains(\"syntax error\")(直锁 base 形),fixture 换 sort.exe(file operand → 排序输出、stderr 空)。
  • cmd.exe 格:./t_cmd 拼写 cmd 不解析 + .sh 扩展不走批处理 → 改 .cmd fixture + Windows 绝对路径调用;#! 行产出 cmd 自属 not recognized(pass-through,断言之)。

续作记录:

  • 75c06d3 test-only:4 格 fixture 更正
  • 新 head: 75c06d3(push 前远端 head 564ffbf 未被推进)

未跑/VOID 的腿:

  • GNU 三方探针 11 形与 Linux 钉双边(Linux 专属,队长侧;钉 cfg(windows) Linux 0-test 已由队长记档)

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