Skip to content

fix(alias): same-read guard stamps monotonic read-unit generation — cross-unit line collisions expand like GNU (rubash#817) - #833

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/817
Oct 18, 2026

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #817.

Root cause

The executor-level alias same-read visibility guard used the definition's physical line as identity: __RUBASH_ALIAS_LINE_<name> (= the defining command's AST line) compared against __RUBASH_CURRENT_LINE, equal values suppress expansion (src/executor/arithmetic_aliases.rs alias_defined_on_current_line; record at src/executor/alias_set_builtins.rs). But nested read units renumber lines — a sourced file restarts at 1 per file (run_source → read_next_source_group) and an eval string renumbers caller_line + i - 1 (src/executor/trap_exec.rs execute_eval_source) — so a definition at the unit's line N collided with the sourcer's own later physical line N: a perfectly legal use was suppressed with command not found / rc=127 where GNU expands. GNU's rule is same-read, not same-line (parse.y read-time expansion; the nested unit is a distinct input stream — evalfile.c push_stream / evalstring.c parse_and_execute pushed string).

Fix — monotonic read-unit generation (audited direction)

Identity becomes (read-generation, line):

  • ShellState gains read_generation (the stream the executing commands were read by) + read_generation_ceiling (monotonic high-water; sibling units never share a value — a depth counter would collide on back-to-back sources, which is load-bearing for the sibling shape below). Cloned across the subshell fork boundary like every C global: a subshell body is the same read unit as its parent.
  • enter_read_unit()/exit_read_unit() (src/executor/public_accessors.rs) push/pop at exactly the existing __RUBASH_CURRENT_LINE save/restore seam: src/builtins/source/execution.rs pushes at the evalfile.c:253-257 push_stream point and pops where run_unwind_frame restores line_number (evalfile.c:395, before the RETURN trap); execute_eval_source (src/executor/trap_exec.rs) wraps its body with the pair.
  • Definitions record __RUBASH_ALIAS_GEN_<name> alongside the line (record_alias_definition_lines); the guard suppresses only when generation AND line both match; unalias/unalias -a clear both marker halves. The line marker's value format is untouched (${__RUBASH_ALIAS_LINE_x} still reads the physical line — tests/issue367_alias_line_hygiene_regressions.rs passes unchanged).

GNU differential evidence (WSL GNU bash 5.3.0(1), stdin oracle bash --noprofile --norc < probe)

Both ticket repros, executor level:

Probe GNU rubash before (db4aaf7) rubash after
Repro 1 — stdin script + source def5.sh, def file-line 5 == caller use-line 5 AX rc=0 line 5: x: command not found rc=127 AX rc=0
Repro 2 — eval body line 4 renumbers to caller line 6, use at 6 AY rc=0 line 6: y: command not found rc=127 AY rc=0
Control — same script, use at non-colliding line 7 AY rc=0 AY rc=0 AY rc=0
Sibling — source A defines x @ its line 5, source B uses x @ its line 5 AX rc=0 AX rc=0 AX rc=0 (needs the monotonic ceiling)
Nested — source nest defines n, source usey uses n @ same line NN rc=0 NN rc=0 NN rc=0

i368 audit must-survive semantics table, row by row (all matched GNU before on 8ab35e89; re-verified after the fix on this branch):

# Shape GNU rubash after Verdict
1 alias ll='echo LL'; ll same line, stdin script 127 127, line 2: ll: command not found preserved
2 same-line inside a sourced file (def2.sh line 2) 127, def2.sh: line 2: x2: command not found 127, byte-identical diagnostic preserved
3 same-line inside an eval string 127 127, line 2: z: command not found preserved
4 definition line N, use at line N+1 (plain script) expands (WW rc=0) expands preserved
5 source/eval definition, use at non-colliding later line expands (AX rc=0) expands preserved
6 -i (piped) multi-submission alias use expands (MM) expands (MM) preserved
7 -i -c shopt expand_aliases default on on preserved
8 $LINENO across -i submissions 1, 2 cumulative 1, 2 preserved
9 bash -c collision layout (. def5.sh … use at line 5) expands (AX rc=0) expands (grouped driver, no marker guard) preserved

Tests

tests/issue817_alias_read_generation_regressions.rs (9 tests): both repros, the audit control, the sibling-generation shape (pins the monotonic ceiling), and guard-rail rows 1-4 + 9 including the byte-identical row-2 diagnostic. Existing tests/issue367_alias_line_hygiene_regressions.rs passes unchanged (marker namespace untouched).

Full-suite differential vs baseline (db4aaf7)

cargo test --no-fail-fast on this branch vs a baseline worktree at db4aaf7, bidirectional: 0 vanished failures, 0 real new failures. The only name-level diff between runs is two pre-existing environment-flaky families that fail/pass identically on both sides in matched runs: the command -p standard-path pair and executor_command_chaining::part_071::test_shellmath_decimal_divide_script_path (needs the third_party/bash submodule, which fresh worktrees don't populate; passes on both sides once the submodule is checked out).

Intersection check

GPL: no vendored/GPL source copied; only factual file:line anchors in comments. cargo fmt --check clean. PR to master, not merged.

…ross-unit line collisions expand like GNU (rubash#817)

The executor-level alias same-read visibility guard compared the
definition's PHYSICAL line against __RUBASH_CURRENT_LINE, so a
definition recorded inside a nested read unit collided with the
caller's own later physical lines: a sourced file restarts group
numbering at 1 and an eval string renumbers caller_line+i-1, so a
definition at the unit's line N suppressed a perfectly legal use at
the sourcer's line N ('command not found', rc=127) where GNU expands
— the rule is same-READ, not same-line (parse.y read-time expansion).

Identity is now (read-generation, line): each alias definition also
records __RUBASH_ALIAS_GEN_<name> from ShellState::read_generation,
and alias_defined_on_current_line suppresses only when BOTH match.
The generation boundary is exactly the existing CURRENT_LINE
save/restore seam: builtins/source/execution.rs pushes at the
evalfile.c:253-257 push_stream point and pops where run_unwind_frame
restores line_number (evalfile.c:395); execute_eval_source wraps its
body with enter/exit_read_unit (evalstring.c parse_and_execute's
pushed string). A monotonic ceiling keeps sibling units from ever
sharing a value (a depth counter would collide on back-to-back
sources); ShellState cloning carries the current generation across
the subshell fork boundary, where the body stays the same read unit.
unalias / unalias -a clear both marker halves.

Verified against WSL GNU bash 5.3.0(1) (stdin oracle): both ticket
repros (stdin+source, eval) now print AX/AY rc=0; the i368 audit's
9-row guard-rail table is byte-matched row by row (same-line
suppression on stdin scripts / inside sourced files / inside eval
strings stays 127 with GNU's diagnostics, N/N+1 and non-colliding
source/eval uses expand, sibling sources expand, -i piped
multi-submission expands, -i -c expand_aliases defaults on, $LINENO
stays cumulative, the -c grouped-driver collision layout expands).
tests/issue817_alias_read_generation_regressions.rs pins the two
repros, the control, the sibling-generation shape, and rows 1-4+9.
@caomengxuan666
caomengxuan666 merged commit 2c28756 into master Oct 18, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/817 branch October 18, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant