Repository navigation
fix(fd): snapshot fd 0 with a kernel dup before the procsub-body bind (Refs #3731) - #3801
Merged
Merged
Conversation
#3731) A >(...) on a command's fd 1 is persistent: the deferred body does not run at the command's end (it runs at the fd's close), so the v1 cells read ahead of the body and passed vacuously — the only v1 failures were body-output ordering, not the hazard. Every regression cell now binds the substitution to fd 3 and closes it explicitly (exec 3>&-), which runs the deferred body synchronously before the post-close reads.
…eparate parser gap
- >({ echo a; echo b; }) is valid GNU but rubash rejects it as
'ambiguous redirect' (measured on base f56716b + pin v2: the
compound cell was the stem's only red, a driver syntax error, not
the fd-0 hazard); use >(echo a; echo b) so the cell tests the
#3731 restore, and record the { } gap in REPORT §4.
- Doc comment: fail-first expectation is 8 regression cells on the
unfixed base (devnull passes either way: EOF rc=1 vs EBADF rc=1);
retire is #[cfg(unix)], so the fail-first leg MUST run on Linux —
on Windows the pin passes on base by construction.
…F stderr (Refs #3731) Measured on Linux base f56716b + pin v3: 2 passed / 9 failed. Eight regression cells fail on the post-close read (rc=1, empty line); devnull fails on the EBADF diagnostic itself (`read: 0: read error: Bad file descriptor` on stderr), not on rc/stdout. The v3 doc comment said devnull 'passes on base either way' — wrong by that one line. Comment-only change; no assertion or driver touched, so the v3 legs stand as run.
caomengxuan666
added a commit
that referenced
this pull request
Nov 16, 2026
…efs #3786) ## 根因 `printf %f` 把精确半值小数向下舍入,GNU 向上舍入:`%.3f` 1.2345 → GNU `1.235`、rubash `1.234`;`%.3f` -1.234567 → GNU `-1.235`、rubash `-1.234`;`%.2f` 1.245 → GNU `1.25`、rubash `1.24`。 `src/builtins/printf/float.rs` 的 `format_fixed` 从 80-bit long double 展开精确十进制数字串后,先 `digits.resize(keep + 1, 0)` 截掉 guard 位之后的尾巴,再调 `round_half_even(..., false)`。被截掉的尾巴正是该值高出十进制半值的部分(1.2345 的 long double 值比十进制半值高 2.6e-21),于是值被误读为精确平局(guard=5、尾全零),按 half-even 向下舍入。旧注释 "Exact expansion, so the tail past the guard digit is all zeros" 本身就是错误的假定——这就是缺陷。 `%e`/`%g`(`significant_digits`)与 `%a` 早已在截断前算 sticky 位,只有 `format_fixed` 漏了。 ## GNU 锚点 `~/workspace/rubash-audit/build-gnu/bash-5.3/builtins/printf.def`:`:209` `typedef long double floatmax_t`;`:213` `strtofltmax` = `strtold`;`:1495-1515` `getfloatmax()` 用 `strtofltmax` 解析;`:774-798` 浮点转换带 `L` 修饰格式化。GNU 把操作数按 80-bit long double 解析并对该精确二进制值做一次正确舍入。 ## 改动 - `src/builtins/printf/float.rs`(`format_fixed`,+9/−3):在 resize 截断前从精确尾巴算 sticky 位,再传给 `round_half_even`——与 `significant_digits`(`%e`/`%g`)同形。只有 guard==5 且精确尾巴非零时受影响(值严格高于十进制半值 → 向上);精确平局(如 `%.0f` 2.5 → 2)与 guard<5 不变。 - `tests/issue3786_printf_half_rounding.rs`(新增,20 测):4 个缺陷格 + 16 个对照格(含 `%e`/`%g` 对照)。 ## 验证(X299,Windows;修法为纯 Rust long-double 仿真,平台无关) - Fail-first(基底 c3bf91e + 钉):钉 16/20(4 个缺陷格红:`f3_1_2345_rounds_up`、`f3_neg_1_234567_rounds_up`、`f2_1_245_rounds_up`、`f3_width_neg_rounds_up`);lib 1356 通过 / 1 失败 / 4 忽略(`api::consumer_tests::issue2084_session_end_restores_host_process_state`,已知 Windows 环境失败)。 - 修后(基底 + 钉 + 修法):钉 **20/20**;lib **1356 通过 / 1 失败 / 4 忽略**(同名 issue2084,基线一致);`RUSTFLAGS="-D warnings" cargo check --locked --lib --bins` rc=0;fmt:本地 `cargo fmt --check` 两改动文件 rc=0,X299 上 `rustfmt --check --edition 2021` 两文件 OK 无 diff。腿脚本的 `FMT_RC=1` 是 `cargo fmt --check -- <path>` 调用形在 Windows 上报 os error 206(文件名太长)致 cargo fmt 未及运行——脚手架假象,非格式问题。 ## 本 PR 删除了什么 未删除任何代码、测试、文档或公开接口。仅改 `format_fixed` 的舍入输入(sticky 位),旧注释随缺陷逻辑一并替换。 队长复核(2026-11-17 02:0x CST):diff 已逐行读过——float.rs +9/−3 恰为截断前算 sticky 位,别无其他;CI run 43923584348:goldens 恰 2 基线格(issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots),census 77 格、issue3786 钉全绿,其余 7 项作业全绿、1 项跳过;3 格 head-only(elf_operand_is_refused_not_run_as_script、issue3275_cd_back_into_recreated_dir_adopts_it、issue3575_posix_cd_out_after_failure_restores_pwd)为 census 抖动——printf %f 修法不可达 cd/elf/assoc 面,且 issue3575 同现于 #3801/#3804 头;漂移 c3bf91e...fcc1de5 与本单两文件零交集 → 按零交集例外合入。
caomengxuan666
marked this pull request as ready for review
November 16, 2026 18:14
caomengxuan666
added a commit
that referenced
this pull request
Nov 16, 2026
#3729) ## Summary Fixes the stdio-marker reinstate hazard in `close_persistent_output_fd` (rubash#3729): the face took a bare `Rc` clone of the fd's entry, then `open_output(fd, Stdout/Stderr marker)` retired the displaced descriptor (`retire_read_arm`, rubash#3213), and the restore re-inserted the retired entry — the same hazard as #3706. The snapshot is now a kernel duplicate via `FdTable::saved_entry` (the #3723/#3739 pattern), exactly like GNU's `fcntl (fd, F_DUPFD, SHELL_FD_BASE)` save (redir.c:965/1082/1144). A dup failure propagates (GNU fails the redirect the same way). ## 本 PR 删除了什么 无删除。仅将一行裸 clone 改为内核 dup 快照,并新增回归钉文件 `tests/issue3729_stdio_marker_fd_restore.rs`(7 格)。 ## Reachability note (read before reviewing the pin) The face needs a File-backed read arm AND the output-procsub registration to coexist on fd 1/2 at close/flush time. They cannot: every input bind calls `clear_output_fd_mirrors` (rubash#3655) and drops the registration, and every output bind retires any read arm. A full trigger battery against GNU bash 5.3.20 and the base binary (f56716b) confirms it — `exec 2> >(...)` + `exec 2<input.txt` + close/rebind/dup-to-closed/failed-rebind, group variants, and the `{var}`-close form all agree with GNU on the read result. So there is NO shell-level fail-first cell for the face itself: the 7 pin cells are GUARDS on the face's code path (stdio-marker reinstate + fd state after close/rebind), written so a future change that makes the face reachable cannot silently reintroduce the hazard. All 7 pass on the base and on the fixed head. Full battery + evidence in REPORT.md (workdir `~/workspace/fix-3729/REPORT.md`). A separate real divergence in the same area — the deferred procsub body is dropped after an input rebind (GNU runs it at close, rubash does not) — is documented as a residual in the REPORT for the captain to file as its own issue; it is deliberately NOT pinned here (it fails on the fixed head too). ## Legs (队长复核) - X299 base pin stem (`issue3729_stdio_marker_fd_restore`): **7 passed / 0 failed** (base build RC=0, 45.10s). - X299 head pin stem: **7 passed / 0 failed** (head build RC=0, 14.02s). - X299 lib bilateral: head **1362 passed / 1 failed** vs base **1362 passed / 1 failed** — sole fail on both sides is the known `api::consumer_tests::issue2084_session_end_restores_host_process_state` (no new red). - `RUSTFLAGS="-D warnings" cargo check --locked --all-targets`: **RC=0**, zero diagnostics (39.83s). - `rustfmt --edition 2021 --check` on the 2 changed files: **RC=0**. - marker-lint: **OK** (118 registry consts, 268 allowlisted sites). - CI(更新头 6fd24c2,run 43926778677,已 update-branch 到尖 52ffeba):goldens 恰 2 基线格(issue_753::case_9_declare_indexed_operand_binds_nothing、upstream_suite_slice_snapshots);census 78 格、issue3729 钉全绿;其余 8 项作业全绿、1 项跳过。3 格 head-only(elf_operand_is_refused_not_run_as_script、issue1902_stdout_stderr_names_follow_their_bindings、issue3575_posix_cd_out_after_failure_restores_pwd)皆为已备案 census 抖动格(issue1902 为 #3799 复核裁定的抖动格;elf/issue3575 同现于 #3801/#3807 头;修法面已证不可达,钉为 guard 钉)。旧头 census 重跑已证同头两次跑 4 格翻动。 队长复核(2026-11-17 02:2x CST):diff 已读——trap_exec.rs 1 行裸 clone 改 kernel-dup 快照 + 7 格 guard 钉;reachability §3.2 要点已亲核(clear_output_fd_mirrors 删双键、三处 set_fd_input_* 尾调用、open_output 替换双臂);X299 腿钉 7/7 双边、lib 双边同名同数、-D warnings rc=0、fmt 干净、marker-lint OK。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #3731.
根因
execute_persistent_output_process_substitution(src/executor/trap_exec.rs:7357,base f56716b;修后位于 7360)以裸 Rc clone 保存 fd 0:let old_fd0 = self.fd_table.entries.get(&0).cloned();。随后的set_fd_input_text(0, ...)经open_inputretire 被顶掉的 File 描述符(retire_read_arm / retire_write_arm,#3213;#[cfg(unix)]),restore 又把已 retire 的 entry 插回去——下一次从 fd 0 读便失败 EBADF。与 #3706(组重定向)、#3727(函数调用 stdin)同 hazard,落在 procsub 体解析面。唯一调用方是close_persistent_output_fd(:7174);体在 fd 关闭时运行,不在命令结束时运行——钉须显式exec 3>&-触发,否则读在体前、空过。GNU 锚点
fcntl (fd, F_DUPFD, SHELL_FD_BASE))。do_redirections (command->redirects, RX_ACTIVE|RX_UNDOABLE)。r1=0 l1=[o1] r2=0 l2=[o2] r3=0 l3=[o3]rc=0;body-reads / cat / readwrite / loop 均与 GNU 一致。修法
一行(+3 行注释),沿 #3723 同款:
let old_fd0 = self.fd_table.saved_entry(0)?;(FdEntry::dup_file_endpoints,#3739 已带入 master;unix 下fcntl(handle, F_DUPFD_CLOEXEC, 0))。dup 失败时?返回ExecuteError::IoError,体不运行、fd 0 不动;无裸 clone 回退。restore 保持直接 insert 不变(同restore_command_fd0_input的形态)。本 PR 删除了什么
无删除:不删除任何行为或公开面,仅把 1 行裸 clone 保存换成 kernel-dup 快照,并新增回归钉文件 1 个(tests/issue3731_procsub_body_fd0_restore.rs,11 组)。不碰
docs/api-ledger.md、src/lexer/continuation.rs。验证(实测)
钉:11 组(primitive、body-reads、cat、compound、twice、readwrite、loop、nested、devnull、control、implicit-doc)。compound 用
>(echo a; echo b);>({ ... })群组体是另一 parser 缺口(见下)。driver.sh: line 7: read: 0: read error: Bad file descriptor。lib 1260 passed / 0 failed / 4 ignored(LIB_RC=0)。日志~/leg-3731-linux-base.log(服务器)。RUSTFLAGS="-D warnings" cargo checkrc=0。日志~/leg-3731-linux-fixed.log(服务器)。lib 双边与 base 完全一致。api::consumer_tests::issue2084_session_end_restores_host_process_state,既存基线),RUSTFLAGS="-D warnings" cargo checkrc=0。日志C:\Users\WWW\leg-3731-fixed-launch.log。注:retire 是#[cfg(unix)],Windows 上钉在 base 即按构造通过,本腿仅作无回归验证。rustfmt --edition 2021 --check,trap_exec.rs(61a80f15)rc=0,钉 v4 rc=0。UNVERIFIED-BY-CAPTAIN
残差
>({ echo a; echo b; })群组体:GNU 合法(实测,rc=0),rubash 报ambiguous redirect——独立 parser 缺口,不属本单,已记 REPORT §4,待队长立案。