Repository navigation
WIP: P3 fd sweep — applier position/allocator fixes + socket spelling-open ENXIO - #2300
Conversation
…cation (rubash#2282)
GNU do_redirections is one ordered walk over one descriptor space
(redir.c:237 do_redirections): the REDIR_VARASSIGN allocation
(redir.c:940, fcntl F_DUPFD at SHELL_FD_BASE) searches the same
space, so a numbered entry before the {var} entry already holds its
slot — cmd 10<f2 {v}<f1 allocates v=11 — while a numbered entry
after it does not count yet — cmd {v}<f1 10<f2 allocates v=10 and
the later bind wins the slot (both shapes oracle-verified against
GNU bash 5.3.20, probes/p2282*.sh in the cluster workdir).
The applier allocated from the fd table alone, whose entries do not
include the command's own numbered slots on the simple-command
path, so v squatted the pending slot and the child's fd collided;
the spawn-delivery helper (fd_var_delivery_fd) already avoided
these slots when computing the child's descriptor.
Fix: FdTable::allocate_dynamic_with_limit_avoiding counts extra
slots as occupied in the lowest-free walk; every applier face
(simple-command funnel, exec stdio walk, compound scope) carries
the running set of slots bound so far in the walk into
execute_dynamic_fd_var_redirect / _heredoc. Heredoc arms apply
after the plain list in the AST split and count the full set.
Two-{var} increasing-order allocation is unchanged (allocations
still land in the table immediately).
Pins: tests/issue2282_applier_own_slots.rs (8 tests, nested-rubash
observers; not run locally under compile rationing — collector
batch-verifies).
Refs #2282
…on (rubash#2281)
GNU do_redirections is one ordered walk (redir.c:237): a close
takes effect at its own position in the list. rubash recorded
{var} closes (rubash#2272) without positions, and both consumers
applied them at the wrong end of the walk:
- apply_external_fd_delivery performed every recorded close
up-front, so cmd 3<file {v}<&- (v=3) delivered the just-bound
file at fd 3 (the up-front close hit the ambient entry, then
the walk bound the file) where GNU's child sees 3 closed.
- reject_invalid_redirects never saw the closes (the {v}<&-
entry skips its scan), so cmd {v}<&- {w}<&9 (v=9) validated
the dup against the still-open ambient entry and delivered
it where GNU fails the dup with EBADF and runs nothing.
Fix: fd_var_pending_closes records are (position, fd) pairs,
stamped with the applier run's generation so a gate can tell
its own command's records from an enclosing command's. The
gate folds each record into its opened overlay exactly once,
at the first candidate past its position (a same-list rebind
after the close still wins: {v}<&- 9<f9 {w}<&9 delivers F9).
The delivery applies each close when its walk passes the
position (apply_pending_close_at): the shared table step
(Executor::apply_fd_var_pending_close, snapshots into
fd_var_close_restore as before) plus revocation of anything
already delivered for the slot - ambient env stamps, command
pins, walk_opened / numbered_dup_bindings records. Move-form
closes (sources of {var}<&N-) carry their entry's position
too and poison later dups through the same fold.
Oracle-verified shapes (GNU bash 5.3.20, probes/p2281.sh +
p2281-extra.sh in the cluster workdir): bind-then-close,
close-then-dup rejection (rc=1, command not run, parent
binding survives), dup-before-close control, rebind-after-
close, move-form poisoning, function-face close visibility.
Pins: tests/issue2281_pending_close_positions.rs (6 tests,
nested-rubash observers; not run locally under compile
rationing - collector batch-verifies).
Refs #2281
…ion execution (rubash#2280)
The dynamic-fd applier records {var} closes (rubash#2272) for
the spawn delivery to perform, but an in-process command has no
delivery: the records were dropped unapplied at undo, so a
function body reading the just-closed descriptor still saw it
open, and a builtin's own read of it succeeded. GNU runs a
builtin/function's do_redirections in the shell's own context
(execute_cmd.c:5648 execute_builtin_or_function): the close is
visible for the whole execution and undo_redirections restores
the binding at command end (oracle-verified, probes/p2280.sh +
p2280-double.sh in the cluster workdir).
Fix: at dispatch, in-process commands perform their recorded
closes in the table and replay the snapshots when the dispatch
returns - in execute_materialized_command using the funnel's
existing builtin/function classification (the same one the
rejected arm uses), and in execute_function_command_invocation,
the function fast path that bypasses the funnel. The snapshots
are kept locally (Executor::apply_fd_var_closes_for_in_process
/ perform_fd_var_pending_close): a body command's applier run
clears the shared fd_var_close_restore list, so queueing them
there would lose them mid-call. A builtin name that turns out
external after all (disabled builtin) stays correct - the
delivery finds the table already closed and the local replay
restores exactly once.
Snapshot replay (restore_fd_var_close_snapshots) now runs in
reverse - stack discipline: a descriptor one command closed
twice restores to its pre-command binding. The shared undo in
undo_child_fd_var_redirects replays through the same helper;
its previous forward replay ended on the youngest (closed)
snapshot and lost the parent binding for double closes on the
spawn face too.
Pins: tests/issue2280_inprocess_pending_closes.rs (5 tests;
not run locally under compile rationing - collector
batch-verifies).
Refs #2280
…ubash#2276)
GNU builds with HAVE_DEV_FD do no descriptor resolution for the
/dev/fd/N spelling: do_redirections (redir.c) hands the target word
to redir_open for the r_input/r_output arms, the kernel resolves the
/dev/fd/N magic symlink, and open(2) on a socket's magic link fails
ENXIO ("No such device or address"). A regular file fresh-opens
(the rubash#2256 rule) and a pipe attaches; only the socket refuses.
rubash's tolerant dup machinery instead served the socket's stream
on every spelling face (oracle probes/p2276.sh vs GNU bash 5.3.20:
operand `cat /dev/fd/3`, `cat < /dev/fd/3`, and `> /dev/fd/3` all
fail ENXIO rc=1 in GNU; rubash streamed "hello" rc=0).
The verdict now lives in the alias layer:
Executor::fd_alias_spelling_open_error returns ENXIO when the fd's
endpoint is a File FileFd whose handle is_socket_handle (sockets are
adopted as File endpoints carrying the /dev/tcp target name), and
None otherwise. ENXIO (errno 6) joins the posix_errors table.
Guarded spelling faces: the shared open_fd_read_endpoint serve; the
compound input/output bind arms; the spawn walk's output-target
open (through its open-failure protocol); the builtin output
binder's fd>=3 spelling arm; the <> readwrite pair binder; the
read-builtin spelling resolver and the $(<...)/source resolver
(both decline — the descriptor arms beside them are untouched);
and the in-process operand serves (dev_fd_operand_live_file,
dev_fd_operand_bytes), with cat's operand diagnostic now reporting
the alias layer's reason ("cat: /dev/fd/3: No such device or
address") instead of a hardcoded ENOENT.
Descriptor faces are dups, not opens, and are unaffected: <&N, >&N,
read -u N keep streaming sockets. The one dup-shaped caller of the
shared serve (external_setup's <&N arm) only reaches it with
non-File endpoints — a socket endpoint is File and takes the
true-dup arm first — so guarding the serve cannot leak into dups.
On Unix a foreign child's literal spelling also fails correctly:
the spawn materializer pins the real descriptor and the child's
own open hits the kernel's ENXIO.
Pins: tests/issue2276_socket_spelling_enxio.rs (7 tests: operand,
redirect-input, and output-spelling ENXIO faces; read -u and <&3
descriptor controls; pipe-attach and regular-file spelling
controls), expectations from the GNU oracle. Pins not run locally
per cluster compile rationing; cargo check --tests is clean and
the collector batch-verifies under scrubbed fds.
Refs #2276
…loses defer to the call's binds (rubash#2281)
Two ordering defects in the pending-close machinery, both
oracle-verified against GNU bash 5.3.20 (script-file probes):
1. Parent leak on rejection. undo_child_fd_var_redirects closed
whatever descriptor the variable NAMED when the undo ran. For a
pure close record ({v}<&-) the applier bound nothing, so the
variable still named the parent's own descriptor — a
gate-rejected command (`cmd {v}<&- {w}<&9` after `exec 9<seed`)
destroyed the parent's fd 9, where GNU's parent keeps it (the
close runs only in the forked child, execute_cmd.c
do_redirections; the delivery path masked the leak through its
close snapshots, the rejection path has none). The undo record
now carries the descriptor the applier itself bound for the
variable (read back after a successful apply; None for close
forms and failed applies) and the undo closes exactly that one.
2. Function face bind-then-close. GNU applies a function call's
whole redirect list in one ordered walk before the body runs
(execute_cmd.c:5606), so `f 3<f {v}<&-` (v=3) leaves fd 3 closed
for the body. The fast path performed the recorded closes before
execute_function installed the call's binds in its redirect
wraps, so the later bind resurrected the descriptor. The fast
path now defers the records (fd_var_deferred_closes);
execute_function drains them at entry and performs them inside
the wraps once the binds are installed, skipping a close whose
descriptor a later entry of the same list re-decides, and
replays the snapshots before the wraps unwind (undo-stack
order). The dispatch hook path is unchanged.
Local (collector re-verifies): issue2281 pins 5/6 — the
remaining failure is rebind_after_close_wins_for_later_dup,
whose expectation the oracle falsifies (a dup shares the open
file description, redir.c dup2: an observer that drains fd 9
first sees fd 10 at EOF in GNU too; rubash's output byte-matches
GNU). Full proof + an adjacent script-face discovery are in
REPORT-ADDENDUM.md. issue2280 5/5, issue2282 8/8, controls
2272/2279/2255/2256/2235/2270 green, lib 985, regression at the
known local stock set.
Refs #2281
… with ENXIO (rubash#2276) The 110fd8c alias-layer verdict (fd_alias_spelling_open_error) was consulted by most serve faces, but two faces never reached it: the stdio plan resolves a spelling output target as a dup, and the cat emulation's text stdin channel fell through its reopen attempt to open_fd_read_endpoint_stream — the dup-semantics stream serve — and drained the socket. GNU fails both in do_redirections itself (redir.c: the r_input/r_output arms hand the spelling to redir_open; the kernel refuses a socket's magic link with ENXIO), before any serve exists. The reject gate now delivers the verdict beside the #1963 dangling-alias judgment, for the same file-open kind set: a spelling target whose aliased descriptor is open but backed by a socket endpoint is rejected with `{target}: No such device or address` (ENXIO via the alias layer's own error). The judged endpoint is the one the ordered walk would open at that entry — the ambient table entry, or a socket an earlier entry of the same list bound from a net target. Defense in depth: stdin_string_for_command's spelling arm declines on the verdict instead of falling through to the stream serve (ungated synthesized commands); the <&N dup arm and the descriptor faces are untouched. Local (collector re-verifies): issue2276 pins 7/7 — both bounced faces now refuse ENXIO rc=1; the operand face, the read -u / <&3 socket controls, and the pipe / regular-file spelling controls stay green. Full diagnosis in REPORT-ADDENDUM.md. Refs #2276
… offset The pin expected fd9=F9 and fd10=F9 from a single observer reading fd 9 then fd 10. fd 10 is a dup of fd 9 (redir.c do_redirection_internal, dup2): one shared open file description, so the fd-9 read drains the offset and fd 10 reads at EOF. GNU bash 5.3.20 yields exactly that (oracle probes/followup/r_oracle.sh); rubash byte-matches GNU. The pin now asserts the rebind property per descriptor with single-fd observers in fresh runs. Tests only. Refs #2281
cd89593 to
49aad9b
Compare
|
Branch recomposed (collector): the earlier composition merge (cd89593) was corrupt — an intermediate merge in the branch's ancestry had baked in ours-side resolutions, so merging current master silently reverted master-side changes across 39 unrelated files (the PR ballooned to 61 files). I discarded that merge and rebuilt the branch as a clean cherry-pick of the PR's seven substantive commits (ba8096a, 87f9bad, d5f7e3f, 110fd8c, d5c1282, 62e0d10, d026d9e) onto current master 8a3d6a9. The only conflict was the known declaration-anchor collision in external_inner.rs (master's #2293 StageStdioContext vs this PR's #2281 apply_pending_close_at), resolved as a union of both declarations. The resulting diff is exactly the original change set: 22 files, +1396/−92, cargo check clean. No semantic content changed. |
WIP: P3 fd sweep — four small issues in/around the dynamic-fd applier and the /dev/fd spelling-open layer (the rubash#2277 code family). One commit per issue; pins named per issue; expectations oracle-verified against GNU bash 5.3.20 from script files (probes in the cluster workdir
~/workspace/fix-p3-applier-socket/probes/).Per cluster compile rationing, pins were NOT run locally beyond
cargo check/cargo check --tests— the collector batch-verifies the pin targets under scrubbed fds (these targets are not in rubash CI's named set).{var}allocator counts the ordered walk's already-bound numbered slots (position-aware, matching GNU's single descriptor space; heredoc arms count the full set).{var}closes take effect during builtin/function execution (applied at dispatch, restored at completion from local snapshots); the spawn undo replay now restores in reverse order./dev/fd/Nspelling-open of a socket endpoint fails ENXIO in the alias layer (spelling faces only: operand serves, input/output bind arms, the<&-excluding read/source resolvers, the<>pair binder; descriptor faces<&N/>&N/read -u Nunaffected, pinned as controls).