Skip to content

WIP: fd-stdin fact cluster (#2131/#2132/#2101) - #2167

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/2131-2132-2101-fd-stdin
Oct 30, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/2131-2132-2101-fd-stdin

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Fd-fact cluster for the #1902 family's residual faces. One issue per commit. Verified locally against the GNU bash 5.3.20 oracle (script-file probes, byte-compared) and with per-issue regression pins (baseline-red / fix-green recorded per commit); full suite adjudication is this PR's CI.

Commits

  • [bug] 简单命令自身 fd-0 重定向不到达 fd 事实 sync([ -f /dev/stdin ] < file 假,#1902 家族) #2131 (39573f4) — a simple command's own redirections never reached the fd-fact sync ([ -f /dev/stdin ] < file answered from the ambient binding), and a compound's FUNCTION_STDIN mirror rebind stated no fact ({ [ -p /dev/stdin ]; } < /dev/null answered pipe). The sync now states each redirected descriptor's fact from its winning redirect (one shared classifier), a staged mirror records its source's fact in FD0_MIRROR_FACT (bind + region feed set it, the bind guard restores it), [[ ]] hands its redirection list to the sync, and the late [/test dispatch applies the command's redirects before evaluating — GNU's open-first order (execute_cmd.c + redir.c:767-955 do_redirections precede the builtin; test.c:544 → eaccess.c:116 sh_stataccess stats the bound descriptor). Pins: tests/issue2131_fd_alias_own_redirect_regressions.rs, 8/8 red on master → 8/8 green.
  • [bug] 继承 stdin 分类过粗:/dev/null 继承被归管道类([ -p /dev/stdin ] 误真,#1902 家族) #2132 (348006c) — inherited stdio was classified pipe-by-default: a /dev/null stdin answered -p true / -c false; ambient stdout/stderr files likewise. The three ambient arms now classify the process's real handles (console/pipe/socket/disk-file referent/char class/directory referent; new s socket class in the evaluator). Pins: tests/issue2132_inherited_stdio_classification_regressions.rs, ambient pins red pre-fix → 7/7 green.
  • [bug] 目录作外部命令 stdin 时在 open 阶段误杀(GNU 为子进程 read 失败;不读 stdin 的命令应照常运行) #2101 (5562483) — a directory as stdin was killed at open time on Windows and its read error was swallowed elsewhere. Windows opens retry directories with FILE_FLAG_BACKUP_SEMANTICS (fd layer + executor input opens); a compound's fd-0 stream class admits directories (real descriptor bind, not the error-swallowing mirror); the text stdin ladder declines a hard read error instead of serving an empty stream; read_all_bytes records the first hard error and the emulated cat reports cat: -: Is a directory with status 1. Pins: tests/issue2101_directory_stdin_regressions.rs, 3 Linux-observable faces red pre-fix → 6/6 green; the Windows open faces are adjudicated by the Windows CI job here.

Adjacent suites green locally (run outside the build slot, fd 9 closed): issue1902 8/8, issue1898 6/6, issue1968 14/14, issue2029 10/10, issue2030 8/8, issue2042 3/3, issue2053 4/4, issue2057 2/2, issue2103 11/11, issue2111 12/12.

Refs #2131
Refs #2132
Refs #2101

GNU applies a command's redirections before its builtin runs
(execute_cmd.c, redir.c:767-955 do_redirection_internal), and
test.c:544 unary_test -> lib/sh/eaccess.c:116 sh_stataccess stats
the descriptor as bound for that command. rubash's
sync_fd_alias_facts classified only fd-table entries, but a simple
command's own redirects are staged as payloads, never bound — so
`[ -f /dev/stdin ] < file` answered from the ambient stdin, and a
compound's text-channel (FUNCTION_STDIN mirror) rebind stated no
fact at all (`{ [ -p /dev/stdin ]; } < /dev/null` answered pipe).

- sync_fd_alias_facts gains a scoped form taking the evaluating
  command's redirection list: after the entries loop, each
  redirected descriptor's fact is stated from its winning redirect
  (one FdFactMark classifier shared by every consumer), applied in
  list order so a dup copies its source's own winning fact.
- A staged fd-0 mirror records its source's fact in
  FD0_MIRROR_FACT (set by bind_command_fd0_input's mirror branch
  and the flat-subshell region feed, restored by the bind guard);
  the sync lets it supersede only the ambient inherited entry the
  mirror itself superseded.
- `[[ ]]` stashes its redirection list on the executor for the
  scoped sync (only its fds 1/2 are ever bound).
- The late `[`/`test` dispatch now applies the command's redirects
  (apply_no_output_builtin_redirects) before evaluating, like the
  other two test entries — GNU's open-first order; without it the
  fd-1 target existed only at buffered-emit time.

Regression pins: tests/issue2131_fd_alias_own_redirect_regressions.rs
(8 tests; verified 8/8 red on master sources, 8/8 green here;
issue1902's 8 pins stay green).

Refs #2131
…efault

The fd-fact sync's ambient arms answered `p` (pipe) for every
non-terminal inherited stdin: with the process's stdin = /dev/null,
`[ -p /dev/stdin ]` was true and `[ -c /dev/stdin ]` false. The
ambient stdout/stderr sinks carried the same console-or-pipe
default (a regular-file stderr answered -p true, -f false, and a
`>&2` dup inherited the misclassification). GNU test.c:544 ->
lib/sh/eaccess.c:116 sh_stataccess stats the descriptor the
process actually holds, so the predicates follow the parent's
inode, not a default.

- New process_stdio_fact classifies a process stdio handle by the
  fd layer's own probes: console -> crw, pipe -> prw, socket ->
  srw (new 's' evaluator class; new unix is_socket_handle twin),
  disk file -> its referent target (windows: GetFinalPathNameBy-
  HandleW also covers directory handles), char device -> the
  null class for /dev/null|zero|full else console class, unix
  directory -> its /proc/self/fd referent target; anything
  unclassifiable keeps the historical pipe default.
- The InheritedProcessStdin arm and the ambient Stdout/Stderr
  arms consume it (a live stdout capture stays the pipe it is).

Regression pins:
tests/issue2132_inherited_stdio_classification_regressions.rs
(7 tests across null/file/pipe/directory ambients, ambient
stdout/stderr files, the compound-rebind reproducer, and the
#2131-mirror guard pins; the 4 ambient-mechanism pins verified
red on the pre-fix tree, all 7 green here; issue1902/2131 pins
stay green).

Refs #2132
…ng fails

GNU redir.c redir_open opens a directory like any file: the child
receives the descriptor, a non-reading command runs fine, and a
reading child fails with its own diagnostic (`cat: -: Is a
directory`, read.def:911 for the builtin). rubash killed the
command at open time on Windows and swallowed the read error
everywhere else.

- Windows fd layer: open_file_read retries a directory with
  FILE_FLAG_BACKUP_SEMANTICS (create_file_with_flags); the
  executor's std-File input opens (classified SyntheticLink arm +
  the open_input_redirect fallthrough) route through the same
  retry (open_redirect_input_file). Unix opens directories
  natively and is unchanged.
- A compound's fd-0 stream class admits directories, so
  `{ ...; } < dir` binds the real descriptor instead of the text
  mirror that swallowed the failure at bind time (the read
  builtin's EISDIR diagnostic and the external reader's error now
  surface; `{ cat; } < dir` was rc 0 silent).
- The text stdin ladder declines a hard read error on an empty
  buffer instead of serving it as an empty stream (both the
  exec-bound fd-0 arm and the `<&N` dup arm), and
  FdTable::read_all_bytes records the first hard error on an
  empty drain (the read_file_bytes discipline, rubash#1495); the
  emulated cat consults it at both stdin drains and reports
  `cat: -: Is a directory` with status 1.

Regression pins: tests/issue2101_directory_stdin_regressions.rs
(6 tests; the 3 Linux-observable defect faces verified red on the
pre-fix tree, all 6 green here; the simple-command faces carry
the Windows open defect and are adjudicated by Windows CI).
Adjacent suites green: issue1902/1968/2029/2030/2042/2053/2057/
2103/2111/1898.

Refs #2101
… widening

CI's UTF-8 boundary hygiene gate flagged the three
`mark.as_bytes().first() ... as char` widenings the ambient arms
used to read a fact's class letter. The marks are engine-minted
ASCII, but the gate's review rule is the point: decompose through
access_mark_parts, which matches the class byte against the
evaluator's alphabet (p/c/d/n/f/s) and never widens. No behavior
change; the hygiene script is clean and the #2131/#2132/#2101/
1902 pins stay green.

Refs #2132
Windows CI caught E0428: the constant already exists with the
other file-flag constants in this module (and is used by the
directory-attribute probe); the #2101 commit redefined it next
to create_file. Keep the single original definition.

Refs #2101
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 02:54
@caomengxuan666
caomengxuan666 merged commit 7e7cc2b into master Oct 30, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2131-2132-2101-fd-stdin branch October 30, 2026 07:16
caomengxuan666 added a commit that referenced this pull request Oct 30, 2026
…ndle (rubash#2171)

After `exec 0<dir`, a child that merely inspected the descriptor
(`ls -l /proc/self/fd/0`) invalidated the parent's fd-0 FileFd:
the next builtin read reported `Bad file descriptor` where GNU
reports the descriptor's own `Is a directory` (read.def:911).
Same death for any File endpoint on any fd (an exec-bound regular
file on fd 3 died identically).

Root cause, in the unix branch of materialize_dev_fd_operands:
the attach record (DevOperandMaterialization) owns what it
records — its Drop closes every recorded handle once the child
exits, like GNU closing the parent's pipe end once the forked
child owns its copy. Two of its three arms record fresh, owned
handles (a capture-pipe read end, a just-opened procsub file).
The File arm recorded the endpoint's OWN handle — the parent's
live descriptor — so the record's drop closed the parent's
FileFd under it; the fd-table entry survived but pointed at a
dead handle.

Fix: the File arm attaches a duplicate
(crate::fd::duplicate_handle). A dup shares the open file
description, offset included, so the child's /proc view and the
#1952 shared-offset read face are unchanged (verified: a child
reading through the operand behaves byte-identically to GNU
before and after); the record now closes only its dup, and the
parent's descriptor survives any number of inspecting children.

Platform shape: the defect and the fix are unix-branch only. The
Windows materialization never hands a child the parent handle —
it rewrites the operand to the endpoint's path or a temp file,
and its deferred consume step seeks (consume_all_text) rather
than closes. The read-side EISDIR shape is #2101/#2167's and is
not touched.

Collision note: this is the cluster's only hunk in
src/executor/dev_fd_operands.rs, which open PR #2156 also edits
(its hunks are the drain/finish regions; this one is the unix
materialize loop's File arm). The borrowed-handle invariant
lives only in this file, so the hunk is minimal and flagged in
the PR body for merge sequencing.

Pins: tests/issue2171_proc_fd_operand_survival_regressions.rs
(4 tests; 3 red on master, 1 guard green both ways).

Refs #2171
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