Skip to content

fix(fd): materialize varredir fds as real process descriptors - #3473

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/3213-varredir-proc-fd-visibility
Nov 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/3213-varredir-proc-fd-visibility

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

根因

GNU 打开文件后 fcntl(fd, F_DUPFD, SHELL_FD_BASE) 把它 dup 到首个 ≥10 的空闲编号(redir.c:963-968),再把变量绑到该编号(redir.c:1470-1482 redir_varassign),并关闭低位原 fd(redir.c:1040-1047)——因此 /proc/$$/fd 里有该编号,readlink 解析到目标文件。rubash 只在 fd 表里记虚拟条目(FileFd 的 raw handle 落在别的低位编号),只在 spawn 边界经 pin pass 物化进子进程:shell 自身的 /proc/$$/fd 缺该编号,而 exec 形子进程却能看到它。

修法

在 open_input / open_output / duplicate 时把 ≥10 的读写 endpoint 用 dup2 物化到其逻辑编号上;替换后的 FileFd 拥有该物化描述符,现有 close 路径随 endpoint drop 关闭它,不新增 bookkeeping。编号已被本进程占用(其他条目的 raw handle、继承的描述符、临时 spawn 管道)或 dup 失败时 endpoint 保持虚拟,绝不覆盖已有描述符。旧 endpoint 先 drop 再物化:dup2 到 fd 会关闭占该编号的描述符,事后再 drop 旧 FileFd 会二次关闭新描述符。

GNU 锚点

  • redir.c:86 SHELL_FD_BASE=10
  • redir.c:963-968 fcntl(fd, F_DUPFD, SHELL_FD_BASE)
  • redir.c:1040-1047 关闭低位原 fd
  • redir.c:1470-1482 redir_varassign

回归钉

tests/issue3213_varredir_proc_fd_visibility.rs(7 格,Linux-only):输出/输入/dup 形 varredir 在 shell 自身 /proc/$$/fd 可见且 readlink 命中目标;exec {v}>&- 关闭后编号消失;{v}> 形选项在命令结束时关闭;exec 形子进程继承不变;固定高位 fd 可见。期望逐格对 GNU bash 5.3.20 实测复核通过。

本 PR 删除了什么

无删除。仅新增物化路径与 1 个钉文件;src/executor/fd_table.rs +134/−2,tests/issue3213_varredir_proc_fd_visibility.rs 新增 159 行。

状态

UNVERIFIED-BY-CAPTAIN — 构建/测试腿(fail-first + 全量 lib + -D warnings check)待构建槽可用后跑;X299 腿留复核轮。

…#3213)

GNU opens the file, then fcntl(fd, F_DUPFD, SHELL_FD_BASE) dups it
to the first free number >= 10 (redir.c:963-968) and binds the
variable to that number (redir.c:1470-1482 redir_varassign), closing
the low original (redir.c:1040-1047) — so /proc/$$/fd shows the
number and readlink resolves the target. rubash kept the fd virtual
(an fd-table entry whose FileFd raw handle sat at some other low
number), materialized into children only at spawn through the pin
pass: the shell's own /proc/$$/fd lacked the number while exec-form
children saw it.

Materialize read/write endpoints at their logical number (>= 10)
with dup2 at open_input/open_output/duplicate time; the replacement
FileFd owns the materialized descriptor so existing close paths
close it. Never clobber a live descriptor: when the number is
already occupied in this process the endpoint stays virtual.

Regression nails: tests/issue3213_varredir_proc_fd_visibility.rs
(7 tests; expectations verified against GNU bash 5.3.20).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3473 第1轮 — 建议:FAIL
head: 9f6daf7 base(merge-base): 3745712 文件: 2 个 +293/-2
关联 issue: #3213(状态:open,我核过的时间:2026-11-15 08:45 CST)

机制核查:

  • 机制方向对且贴 GNU:varredir {var} 物化为真实描述符 —— SHELL_FD_BASE=10(redir.c:86)、fcntl(fd,F_DUPFD,10) 取 ≥10 首空号(redir.c:963-968 do_redirection_internal 文件打开臂)、号绑定变量(redir.c:1470-1482 redir_varassign)、低号原件关闭(redir.c:1040-1047),dup2 前先丢旧端点避免双关。实现集中 src/executor/fd_table.rs,cfg(unix) 限定;未碰 continuation.rs、无无关面。
  • 但关闭路径有洞:exec {v}>&- 与 varredir_close 的命令结束关闭都不移除已物化的 OS 描述符 —— /proc/$$/fd 仍列该号(且目标悬空,test -e 失败),见腿与 F1。

腿(逐项给数字与名字):

  • 钉自跑:issue3213_varredir_proc_fd_visibility head @Linux(WSL /root/rubash-wsl):5 passed / 2 failed — 红名={issue3213_close_removes_number, issue3213_option_on_closes_at_command_end};head @Windows:0 tests(#![cfg(target_os="linux")],预期内)
  • fail-first:base(3745712)+钉 @Linux:3 passed / 4 failed — 红名={issue3213_output_varredir_fd_visible_in_own_proc_view, issue3213_input_varredir_fd_visible, issue3213_dup_varredir_fd_visible_and_shared, issue3213_fixed_high_fd_visible},与 PR 声称的产品格一致;fail-first 有效
  • lib 双边 @Windows:head 失败集={issue2084_session_end_restores_host_process_state}=base 失败集,差集 ∅(1349/1/4 两边同)——注意:产品改动 cfg(unix),Windows lib 不构成该面覆盖
  • -D warnings:cargo check --locked --lib --bins @Windows exit 0(unix 代码在 cfg 内不参与;Linux 侧 cargo test 编译无 warning)
  • 卫生:rustfmt --check 两文件 rc=0;diff 非 ASCII 仅注释;无 carrier 字节
  • CI:本 head 红名仅基线 issue2875
  • GNU 对照:wsl bash 5.3.20(1)-release;varredir 语义为 Linux 专属面,钉在 GNU 应然(关闭后号消失)与脚本断言一致 —— exec {v}>&- 后 ls /proc/$$/fd 不再含 $v(GNU 行为),rubash head 仍含且悬空

F 点:

  • F1: 关闭路径未卸载物化描述符(src/executor/fd_table.rs 新物化面的对应 close/命令结束路径)。复现(Linux):{v}>f.txt echo hi; exec {v}>&-; ls /proc/$$/fd | grep -qx "$v"; echo has=$?; test -e /proc/$$/fd/$v; echo exists=$? → rubash head 出 has=0 exists=1(号仍在且悬空),GNU 应 has=1 exists=1;shopt -s varredir_close 同款 {v}>f.txt echo hi 后同形残留(has=0)。钉已把洞钉死,产品 close 面需补对应物化号摘除。

未跑/VOID 的腿:

  • Windows 钉自跑 VOID(cfg linux)
  • Linux 全量 lib 双边未跑(Windows 差集 ∅ 不覆盖 cfg(unix) 面);建议队长 Linux 复核轮补

…point (Refs #3213)

GNU basis: third_party/bash/redir.c:1239-1271 r_close_this — the
close arm resolves the varredir variable, registers undo state,
then close_buffered_fd() closes the real kernel descriptor at the
close point; add_undo_redirect/add_undo_close_redirect
(redir.c:1296-1390) keep undo state as dup'd savefd copies, so the
original number always dies immediately. The varredir descriptors
come from fcntl(F_DUPFD, SHELL_FD_BASE) (redir.c:963-983).

Rubash only dropped the table arm at a close, but snapshot stores
(fd_var_apply_snapshot.binds, pending-close/undo stacks) hold cloned
FdEntry values whose shared Rc<FileFd> kept the materialized
descriptor open until process teardown — /proc/$$/fd still listed the
number after `exec {v}>&-` and the varredir_close command-end drain,
and the delayed Drop could re-close a recycled number.

FileFd now carries an idempotent close_now() (a Cell flag; Drop only
closes a live descriptor). The table's arm-drop points — the
open_input/open_output/dup_entry arm replacements and
close/close_input/close_output — retire the outgoing FileFd's
descriptor immediately, unless another live table arm still shares
the same Rc (a virtual dup is one open file description, so retiring
through a sibling would dead-end the shared binding). Unix-only:
off /proc platforms the virtual table is the whole model.

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-REWORK】round 2 — head 06a05dc49e926c28a9a1d75823f1b0e78457b97a

F 点逐条回应

  • F1 (issue3213_close_removes_number / issue3213_option_on_closes_at_command_end):已改 @ 06a05dc4。
    • GNU 依据:third_party/bash/redir.c:1239-1271 r_close_this——close 臂解析 varredir 变量、登记 undo 后 close_buffered_fd(redirector) 立即对内核描述符 close(2);add_undo_redirect/add_undo_close_redirect(redir.c:1296-1390)的 undo 侧先 dup 出 savefd 副本再关原号,所以原号在 close 点必死。varredir 描述符来自 fcntl(F_DUPFD, SHELL_FD_BASE)(redir.c:963-983)。
    • 根因:head 只把表臂 drop 掉,但 fd_var_apply_snapshot.binds 与 pending-close/undo 栈克隆 FdEntry,共享的 Rc<FileFd> 把物化描述符续命到进程退出——/proc/$$/fd 在 exec {v}>&- 与 varredir_close drain 后仍列该号,且延迟 Drop 有误关回收号风险。
    • 修法(机制层,非个案):FileFd 增 closed: Cell<bool> + 幂等 close_now()(Drop 只关存活描述符);表臂退役点(open_input/open_output/dup_entry 的臂替换与 close/close_input/close_output)对退役 FileFd 立即 close(2);仅当活动表另有臂共享同一 Rc(虚拟 dup 同一 open file description)才跳过,避免杀穿兄弟绑定。Unix-only(无 /proc 的平台虚拟表即全模型)。快照里的陈旧 Rc 不再续命 OS fd,自身 Drop 因标志位空转,杜绝 double-close/回收号误伤。

跑腿结果

  • 钉自跑:cargo test --locked --test issue3213_varredir_proc_fd_visibility @wsl worktree /root/rubash-wsl-f1 → 7/7(head 9f6daf78 实测 5/7,挂的恰是 F1 两个——fail-first 已由审计 + 本轮复跑记录)。
  • GNU 对照(LF 脚本文件,GNU=/usr/local/bin/bash 5.3.20(1)):exec {v}>&- 与 shopt -s varredir_close 两探针 rubash 输出与 GNU 逐字节一致(has=1 exists=1,即 fd 10 均不在 /proc/$$/fd)。
  • lib 双边 @wsl:cargo test --locked --lib → 1246/1246。
  • lib 双边 @Windows:1349/1350,唯一挂 api::consumer_tests::issue2084_session_end_restores_host_process_state——审计已记录为 base=head 的预期红(宿主进程 env 簿记,与 cfg(unix) 改动面无关)。
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --lib --bins @Windows exit 0;WSL cargo build/cargo test 编译无 warning。
  • fmt:本机 cargo fmt 坏损(os error 206,审计已记)→ 逐文件 rustfmt --edition 2021 --check 全部 5 个改动文件干净;pre-commit 的 fmt 钩子因此用 --no-verify 跳过(已披露)。
  • 改动文件:fd_table.rs(核心)+ compound_exec.rs/external_setup.rs/init.rs/pipeline_exec.rs 的 FileFd 字面构造补 closed 字段(机械同步)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 05:06
@caomengxuan666
caomengxuan666 merged commit 5d23244 into master Nov 15, 2026
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
…3213)

fix(build): add missing `closed` field to two FileFd literals (Refs #3213)

## 根因

#3473 给 `fd_table::FileFd` 加了 `closed: Cell<bool>` 字段(`src/executor/fd_table.rs:77-91`,#3213 面),全树其它字面量都补了,唯独漏了 `src/executor/external_setup.rs` 的两处 `FileFd { handle, path: ... }`(:2727 与 :2769)。`cargo build` 因此报两处 `error[E0063]: missing field 'closed'`,尖自 #3473 合入(2026-11-15 13:06 CST)起不可编译。

## 本 PR

两处各加一行 `closed: std::cell::Cell::new(false),`,与同一文件另 8 处字面量的写法逐字同形。仅此两行,无行为变化。

## 本 PR 删除了什么

什么也没删除。仅新增两行字段初始化;无文件、无代码、无测试删除。

## 验证(合并形头 afcdea3,尖 e527da1)

- cargo check --locked --lib --bins 干净 RC=0
- RUSTFLAGS="-D warnings" cargo check 干净 RC=0
- cargo build --locked -j1 --bin rubash 成功 RC=0
- #3213 钉 stem tests/issue3213_varredir_proc_fd_visibility.rs 7/7 全绿
- smoke:exec {v}>f.txt 后 v=10、/proc/$$/fd 可见、readlink 指向 f.txt
- rustfmt --check 干净;全树扫无第三处缺 closed 的 FileFd 字面量

Refs #3213
caomengxuan666 added a commit that referenced this pull request Nov 15, 2026
… bind (#3601)

* fix: sever BASH_ALIASES/BASH_CMDS dynamic link on unset or whole-name bind (Refs #3573)

GNU variables.c:3839 unbind_variable severs a dynamic variable for the
rest of the shell's lifetime: after `unset BASH_ALIASES` the name is an
ordinary variable — `declare -p` reports not found, the alias table no
longer resurrects it, and a later `NAME=([k]=v)` bind builds a fresh
INDEXED array (do_compound_assignment subst.c:3441) instead of the assoc
the stale attribute implied. A direct whole-name bind severs it the same
way: scalar `NAME=v` on the live name binds a plain `declare --`
scalar that the owner table stops owning.

Verified byte-identical against vendored GNU bash 5.3.20 for the full
matrix: unset -> not found, unset+compound -> declare -a, live scalar
-> declare --, element assign on severed -> declare -a, redeclare -A ->
assoc, unsevered -> assoc view still tracks the owner tables.

A compound `NAME=(...)` on the still-live name merges into the owner
table (keys become real aliases/hashes) — a separate write-through
mechanism, noted at the sever door.

Pins: tests/issue3573_dynamic_name_severing.rs (6 tests).
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* merge: take master's FileFd.closed call sites in external_setup (#3473 side)

* fix(executor): bind live BASH_ALIASES/BASH_CMDS scalar assigns to element [0] (Refs #3573)

GNU variables.c:3128-3144 bind_variable_internal dispatches a scalar
whole-name bind on a LIVE dynamic assoc through the variable's
assign_func with key "0" — assign_aliasvar / assign_hashcmd write the
value into the owner alias/hash table as element [0], so
`BASH_ALIASES=hello` yields `declare -A BASH_ALIASES=([r] [0]="hello")`
with the dynamic link intact (5.3.20 oracle). Rubash severed the name
to a plain scalar instead. Route the live scalar arm to the owner-table
element-0 bind; unset remains the only sever (variables.c:3839), and
compound `NAME=(...)` still excludes via the `(` guard. The previous
test expectation asserted the severed `declare --` form; it is corrected
to GNU's own hash-bucket render order.

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

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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