Skip to content

test(executor): pin fd 0 consumption cursor to transport units - #711

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/fd0-raw-byte-cursor
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/fd0-raw-byte-cursor

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

What this is

Follow-up to #695 (fixing unixwin/niubash#383, the inline wc -c transport-byte
miscount). That PR's body flagged the fd 0 consumption cursor as an
"unresolved, deliberately not fixed" candidate of the same class:

  • pipeline_stages.rs self.pipeline_stdin_consumed.set(Some(input.len()))
    (payload written via shell_text_to_raw_bytes(input))
  • pipeline_exec.rs (base + stage0_input.len()) for FUNCTION_STDIN_OFFSET
    (payload written via shell_text_to_raw_bytes(&stage0_input))

I audited those sites. They are correct as written — the cursor is not a raw
byte count, it is a byte index into the shell's transport text, and the two
are deliberately different units. This PR records the invariant and pins it
with regression tests so a future sweep does not "fix" the three sites into a
bug.

Why the sites are right (construction proof)

Between in-process stages a payload travels as transport text: a raw control
byte is the carrier pair U+E000 + U+E0xx (locale.rs:352), and a lone invalid
high byte takes the same shape. So transport_len != raw_len — x6 per control
byte, x2 per high byte — and shell_text_to_raw_bytes(..).len() recovers the
raw length.

FUNCTION_STDIN_OFFSET is a byte index into that transport buffer. Every
consumer slices with it and therefore requires transport units:

  • read_io.rs::read_function_stdin — &input[offset..] then
    offset + consumed (both transport);
  • shell_options.rs::function_stdin_remaining — input[offset..];
  • select_exec.rs::read_select_input — &stdin_content[*stdin_offset..];
  • function_calls.rs — input.get(offset..) and parent_stdin_base + child_offset.

The pipeline delta is folded as base + consumed, where base is itself a
transport offset, so consumed must be transport too. input/stage0_input
is exactly FUNCTION_STDIN[base..], so base + transport_len lands on
end-of-buffer (a valid transport index) whereas a raw delta stops short by the
carrier overhead and re-exposes already-consumed bytes.

#695 was the reverse case: a count reported to the user (wc -c) had to be
raw. Here an index into transport text must be transport. Same boundary,
opposite direction.

Evidence

Debug probe at the real call site, printf '\033A\n' | { od -An -tx1 | od -An -tx1; }
(concurrent external path, pipeline_exec.rs):

transport=8 raw=3     # 0x1B carrier pair (6) + "A" + "\n"

I then applied the naive raw-unit change at the three sites and the follow-up
reader visibly replayed consumed bytes instead of seeing EOF:

command transport units (correct) raw units (naive "fix")
{ od | od; read x; ... } <<< $'\033A' after=[] rc=1 after=[\u{e01c}A] rc=0
{ od | od; read x; ... } <<< $'ab\315X' after=[] rc=1 after=[\u{e0ce}X] rc=0
{ read x; od | od; read y; } <<< $'line\n\033B\nTAIL' y=[] rc=1 y=[TAIL] rc=0

The raw form leaks half of a carrier pair (U+E01C / U+E0CE) plus the tail.
So the change that looked like the fix would have been the bug.

Changes

  • Comment-only at the three sites documenting the transport-unit invariant
    (no behavior change).
  • New tests/fd0_consumption_cursor_transport_units.rs: four tests that fully
    consume a carrier-bearing shared fd 0 with an external pipeline and assert
    the follow-up reader sees EOF, plus a remainder-path round trip. Three of
    them fail under a raw-unit cursor.

Tests run

cargo test --test fd0_consumption_cursor_transport_units          # 4 passed
cargo test --test fd0_consumption_cursor_transport_units \
  --test issue575_exec_child_stdin_regressions \
  --test issue586_stdin_script_on_demand_regressions \
  --test issue647_function_redirect_stdin_regressions \
  --test issue429_pipeline_stdin_redirect_regressions \
  --test issue436_inherited_stdin_stage_gate_regressions \
  --test issue615_inherited_stdin \
  --test issue277_read_carrier_domain_regressions \
  --test issue158_pipeline_drain_deadlock \
  --test issue166_tr_pipeline_options \
  --test issue165_cat_pipe_reader_options \
  --test stdin_multibyte_regressions \
  --test issue368_comsub_fd_visibility \
  --test decode_to_visible_text
cargo fmt --check                                                 # clean

issue647_function_redirect_stdin_regressions::function_call_file_redirect_survives_external_spawn
fails, but it is pre-existing and environmental: its external_true() helper
injects an unquoted C:\...\true.exe into the script, which the shell mangles
to C:Userscmx...: command not found. Unrelated to this (comment-only) change.

Same class, not this PR

While auditing I found a genuine, user-visible instance of the class in the
read builtin's -n/-N char limit (read_io.rs): the limit counts transport
chars, so a carrier pair is split and counted as two characters —

printf 'AB\033C' | { read -n3 c; printf '%s' "$c" | od -An -tx1; }
# rubash: 41 42 ee 80 80     GNU: 41 42 1b

That is a separate fix (paired-char consumption in read_function_stdin) and is
left out here to keep this change scoped to the audited cursor sites.

The shared fd 0 cursor (FUNCTION_STDIN_OFFSET) is a byte index into the
shell's transport text: read_function_stdin, function_stdin_remaining and
select all slice the same buffer with it. The pipeline drivers therefore
advance it by the transport length they handed the external child
(pipeline_stages.rs `input.len()`, pipeline_exec.rs `stage0_input.len()`),
never by shell_text_to_raw_bytes(..).len(): a raw delta under-advances by
the carrier overhead and replays bytes the child already consumed.

This is the unit-twin of #695 / unixwin/niubash#383. There a byte count
handed to the user had to be raw; here an index into transport text must be
transport, so the three audited sites are correct as written. Document the
invariant and add regression tests that fail if a future sweep converts
them to raw units.
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