Skip to content

fix(executor): let a live fd-0 File endpoint outrank the FUNCTION_STDIN mirror in exec spawns (Refs #3046) - #3461

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3046-exec-pipeline-chardev-stdin
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3046-exec-pipeline-chardev-stdin

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

根因

exec_inherited_stdin(src/executor/trap_exec.rs)先查 FUNCTION_STDIN 镜像、后查 fd 表。文本通道对字符设备故意 decline(shell_options.rs stdin_string_for_command:设备无 EOF 可 slurp,"the caller binds the live fd instead"),且 execute_exec 的 bind_command_fd0_input 也确实绑了 live FileFd;但镜像臂先到:stage-0 镜像为空 → 孩子得 Stdio::null()(0 字节);后级 stage 镜像为上游 pipe 字节 → 孩子读 pipe(echo hi | exec dd </dev/urandom 读到 "hi\n")。普通文件"正常"只是假象——文本通道把文件内容 slurp 进镜像,孩子读的是镜像而非 fd。

GNU 锚点(vendored bash 5.3.20)

  • execute_cmd.c:5520-5522(execute_subshell_builtin_or_function):do_piping 在先、do_redirections (redirects, RX_ACTIVE) 在后——元素自己的重定向覆盖 pipe 的 fd 0
  • execute_cmd.c:6375 do_piping:dup2 (pipe_in, 0)
  • builtins/exec.def:231:shell_execve 以已绑定的描述符替换镜像
  • redir.c:725 redir_open:目标统一 open,文件类型从不参与

修法

exec_inherited_stdin 内:live fd-table File/CoprocStdout 端点先于 FUNCTION_STDIN 镜像解析——与库内既定优先序一致(function_stdin_is_command_source:镜像仅当 fd 0 自身未绑定时才是 fd 0 的绑定;stdin_string_for_command 尾部:"an exec-installed fd 0 in the fd table shadows the ambient FUNCTION_STDIN buffer")。bind 的文本分支在 stage 文本时移除 fd 表项,故同一 bind 装的文本镜像旁不可能有 stale File 端点。

钉

tests/issue3046_exec_pipeline_chardev_stdin.rs(15 格,master 已有,#3173):stdin urandom/head/zero、对照、stderr devnull/文件、subshell、chain、comsub mktemp 链、top-level 对照、stdout-to-file 诚实例外。本 PR 不新增钉文件。

证据

  • 判别式探针(盒上旧二进制 + GNU 5.3.20):printf 'ABCDE' | exec dd bs=10 </dev/urandom rubash 5 / GNU 10;echo hi | exec dd </dev/urandom rubash 3 / GNU 10;subshell 面 rubash 正常;stderr 面在 Nov-10 二进制上已不复现(应为区间提交 d4f7d94 + a0c4644 已修,须头上以钉复核)
  • 详见 ~/workspace/fix-3046/REPORT.md

本 PR 删除了什么

无。仅调整 exec_inherited_stdin 内两个分支的求值顺序(File 臂移至 FUNCTION_STDIN 镜像臂之前),无函数、无文件、无公开面的增删。

UNVERIFIED-BY-CAPTAIN

返工(2026-11-13,头 ae6b2f1)

  • 旧头 f2bae8f 的 CI 9 腿全红,单一根因 error[E0004]: non-exhaustive patterns(src/executor/trap_exec.rs:4538:15):exec_inherited_stdin 尾部的 match self.fd_table.read_endpoint(0) 缺 File / CoprocStdout 两臂。
  • 返工补两臂,绑定与上方 early return 逐字同形(live fd 句柄 dup 后交孩子),与 open_fd_read_endpoint_stream(shell_options.rs:1182/1194)一致;无通配臂、无语义改动。rustfmt --check 已清;验证腿(cargo check、钉 15 格双边、GNU 三方探针、lib 双边、-D warnings、hygiene、终头 CI)由盒驱动回填中,详见 ~/workspace/fix-3046/REPORT.md §7–§8。

UNVERIFIED-BY-CAPTAIN

…IN mirror in exec spawns (Refs #3046)

Root cause: `exec_inherited_stdin` consulted the FUNCTION_STDIN mirror
before the fd table. The text channel deliberately declines character
devices (shell_options.rs stdin_string_for_command: a device has no
EOF to slurp, "the caller binds the live fd instead"), and
execute_exec's bind_command_fd0_input does bind that live FileFd —
but the mirror arm fired first, so a stage-0 mirror (empty: no
producer) handed the child Stdio::null() (0 bytes), and a later
stage's mirror handed it the upstream pipe bytes (`echo hi | exec dd
</dev/urandom` read "hi\n"), shadowing the redirect the shell had
just bound. A regular file only "worked" because the text channel
slurped its content into the mirror; the child read the mirror, not
the fd.

GNU anchors (vendored bash 5.3.20):
- execute_cmd.c:5520-5522 (execute_subshell_builtin_or_function):
  do_piping first, then do_redirections (redirects, RX_ACTIVE) — the
  element's own redirect replaces the pipe on fd 0.
- execute_cmd.c:6375 do_piping: dup2 (pipe_in, 0).
- builtins/exec.def:231: shell_execve replaces the image with the
  descriptors as bound.
- redir.c:725 redir_open: the target is opened uniformly; the file
  TYPE of the stdin target is never consulted.

Fix: resolve a live fd-table File/CoprocStdout endpoint on slot 0
before the FUNCTION_STDIN mirror — the precedence the codebase
already states in function_stdin_is_command_source ("the mirror is
fd 0's binding only while fd 0 itself is unbound") and in
stdin_string_for_command's tail ("an exec-installed fd 0 in the fd
table shadows the ambient FUNCTION_STDIN buffer"). The bind's text
branch removes the fd-table entry when it stages text, so no stale
File endpoint can coexist with a text mirror from the same bind.

Nails: tests/issue3046_exec_pipeline_chardev_stdin.rs (15 cells,
already on master via #3173) — stdin urandom/head/zero, controls,
stderr devnull/file, subshell, chain, comsub mktemp-style chain,
top-level contrasts, stdout-to-file exception. No new test file.

本 PR 删除了什么:无。仅调整 `exec_inherited_stdin` 内两个分支的
求值顺序(File 臂移至 FUNCTION_STDIN 镜像臂之前),无函数、无文
件、无公开面的增删。

UNVERIFIED-BY-CAPTAIN
…in's endpoint match (Refs #3046)

Rework of f2bae8f: CI was red on a single E0004 (non-exhaustive
patterns) at trap_exec.rs:4538 — the trailing match on
fd_table.read_endpoint(0) had no arms for File / CoprocStdout.
Both arms bind the live fd exactly as the early return above does
(duplicate_handle_inheritable + handle_to_file), the same shape as
open_fd_read_endpoint_stream (shell_options.rs:1182/1194); no
wildcard, no semantic change. GNU anchor unchanged (REPORT §2:
execute_cmd.c:5520-5522, builtins/exec.def:231, redir.c:725).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】

PR #3461 第1轮 — 建议:PENDING(缺腿)
head: ae6b2f1 base(merge-base): e50cea9 文件: 1 个 +46/-0
关联 issue: #3046(状态:open,我核过的时间:本地当前;本 PR 无先验评论)

机制核查:

  • 方向正确:exec_inherited_stdin 原先先查 FUNCTION_STDIN 文本镜像再查 fd_table 端点,导致命令自带 < 绑出的活 FileFd 被镜像遮蔽(stage-0 空镜像→Stdio::null() 0 字节;后段镜像→把上游管道字节喂给 exec 子进程)。GNU execute_cmd.c:5520-5522 execute_subshell_builtin_or_function 先 do_piping 后 do_redirections:元素自带重定向覆盖 fd0 上的管道,redir_open 不区分目标文件类型。修复把 fd_table.read_endpoint(0) 的 File/CoprocStdout 臂提前为 early return(dup 句柄下发),镜像只在 fd0 未绑时兜底——与 shell_options.rs 既有 precedence 注释一致;末尾 match 补 File/CoprocStdout 臂仅为穷尽性且注释声明不可合并为通配,写法干净。
  • 无关改动:无(单文件 +46 行注释+两个 match 臂)。

腿(逐项):

  • 钉自跑:issue3046_exec_pipeline_chardev_stdin(既有 15 格钉,base 上已存在)head → 8 passed / 7 failed。7 红全部 /dev/urandom / /dev/zero 相关,报 'No such file or directory'——Windows 无 chardev,环境绑定失败;同一 stem 在 head 的 Linux CI「Rust tests」全绿(钉 15 格在 Linux 实跑通过)。
  • fail-first:base e50cea9 同 stem → 同样 8/7,红集与 head 完全相同 → 本腿在 Windows VOID(无 chardev,无法区分)。补验:echo hi | exec cat t.txt(常规文件)在 base 已正确输出 FILEDATA——base 上常规文件重定向本就不触发镜像遮蔽,bug 只在 chardev 绑定面可观测,与 PR 注释自洽;Linux 侧 base-red 未实跑(推断:镜像遮蔽→dd 读 0 字节→0\n,与 Windows base 同形不同因)。
  • lib 双边:head 失败名集 {issue2084_session_end_restores_host_process_state} / base 同集 / 差集 ∅。
  • -D warnings:lib check 干净 0 警告。
  • 卫生:rustfmt --check trap_exec.rs FMT-OK;marker-lint OK;utf8-boundary clean。
  • CI:红任务仅 Rust tests (macOS) = 已知基线 issue2875(日志:1242 passed/1 failed,唯一红名 issue2875_input_procsub_child_runs_as_rubash_shell);Linux Rust tests / GNU goldens / compat CLI / cross-target×2 / shell tests / Windows compile / niubash build / API ledger 全绿。
  • GNU 对照:WSL GNU bash 5.3.20(1)。echo hi | exec dd bs=16 count=1 </dev/urandom → 输出 16 随机字节(非 'hi\n');echo hi | exec cat /etc/hostname → 打印文件内容;( exec dd ... </dev/urandom ) 子 shell 同形随机输出——元素自带 < 替换 fd0 管道确认。脚本:target/probes-3461/oracle.sh。

F 点:无。

续作记录:未动代码。

未跑/VOID 的腿:

  • fail-first 信号腿:Windows 上 base/head 红集相同(均 /dev 缺失),无法归因;Linux 上 base 跑该 stem 未执行——建议队长 Linux 车道对 base e50cea9 跑一次 issue3046 stem 确认 7 格因镜像遮蔽而红(head Linux 已绿,CI 实证)。除此之外所有腿已绿,机制与 GNU 锚点吻合。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】

PR #3461 第2轮 — 建议:PASS(缺腿补齐)
head: ae6b2f1 base(merge-base): e50cea9
关联 issue: #3046

本轮补腿(WSL Linux /root/rubash-wsl,CARGO_INCREMENTAL=0,cargo test --locked -j 2):

  • head ae6b2f1:cargo test --test issue3046_exec_pipeline_chardev_stdin → 15 passed / 0 failed。
  • base e50cea9:同 stem → 10 passed / 5 failed。红集 = {comsub_mktemp_style_chain, pipeline_exec_chain_all_stages, pipeline_exec_stdin_dev_zero, pipeline_exec_stdin_head_urandom, pipeline_exec_stdin_urandom},签名全为镜像遮蔽(exec dd </dev/urandom | wc -c → 0\n,dd 读 0 字节)。
  • 与上轮预期(7 格)的出入说明:Windows base 上另 2 红为 /dev 缺失的环境红(stderr_to_devnull 面),Linux 上 /dev 存在故绿——Linux fail-first 干净演示缺陷面:5 格 chardev-stdin 镜像遮蔽红,head 全绿。与审计推断(base→dd 读 0 字节)同因同形。

未动代码;其余腿按第1轮审计记录全绿。判词翻 PASS,按协议 squash 合并。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 04:40
@caomengxuan666
caomengxuan666 merged commit c2a5800 into master Nov 15, 2026
10 of 11 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】批次汇总(L 波 12 项,终报存 final-report-rework-l.md)

已合并 10:#3461 c2a5800|#3596 cac7a13(解锁链)|#3486 5dcd1e6|#3480 f72b5c2|#3439 b7c3af5|#3438 27d3483|#3463 fc574f5|#3375 83e2fe7(fork 三轮叠加:PwdOutcome comsub 调用点 + FileFd.closed 两字面量 + 再 merge master,终头 CI 红全 = issue2875 基线)|#3600 7353ef7|#3589 e527da1(niubash 镜像 0c8d81e 配对补齐)|#3166 016db15(bats-core 树到位,cell A 实跑为缺陷复现)

未合并 1:#3554 判词 PASS、腿全绿(unix 钉 4/4 + base overlay 0/4 + 两残差疑点与 GNU 逐字节一致),mergeState=DIRTY 文本冲突(hash.rs/lookup_paths.rs/path.rs)——按协议未解冲突,待 owner 处理后即合。

遗留观察(非阻塞):#3375 复核发现 renamed-cwd 下 pwd -P 物理解析存量缺口(master 与 head 同红,GNU 按 inode 解析成功),被 #3349 poison 放大为后续 pwd rc=1——建议单列 issue。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】批次补充:#3554 已收口——owner 授权 merge origin/master 解冲突(hash.rs 取 PR 语义面,包超集 master #3358 自承 cache-sync 残差),合并头 637e2f5 Linux 钉 4/4 + GNU 探针逐字节一致 + Windows 残差同审计归因 + CI 红全 {issue2875} 基线 → squash 合并 46bb796b。至此 L 波 12 条全部收口:11 合并 + #3596 解锁链直合,无未清项。终报 final-report-rework-l.md 已更新。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-ISSUE-CLOSE】批次汇总(lane C:本轮新合并 PR 关联 issue 补关,终报存 final-report-close-c.md)

已关 12:rubash#3420←#3438|#2777←#3463|#3397←#3471|#3213←#3473|#2763←#3474|#3267←#3475|#2769←#3480|#3299←#3482|#3448←#3486|#3572+#3571←#3589(§5 文案同 PR 顺带修,niubash 镜像已配对)|niubash#879←#1009(五主题裁定=harness 侧,按 issue 裁定路径完成)

已关跳过 10:#3093 #3349 #3353 #3046 #3425 #3359 #2875 #3542 #3430 #3573

不关 8:rubash#3049 #3091(#3166 纯钉、缺陷 live)|#3026 #3032 #3048(#3169 纯钉、缺陷 live)|niubash#955(跨仓对应项,rubash PR 不可交付)|niubash#993(#998 只落 §8/§11/§12 三面)|niubash#731(K3 多项工单,#1011 仅交付钉,范围 1/4 未落地)

口径备注:squash SHA 取 mergeCommit.oid 前 7(同 close-A/B);清单 7 条所给前缀为 head SHA 已更正(#3461/#3473/#3480/#3486/#3554/#3588/#3593)。无留置项。

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