Skip to content

test: add fail-first regression pins for literal control-byte constants (Refs #3816) - #3817

Merged
caomengxuan666 merged 11 commits into
masterfrom
fix/3816-literal-control-char-constants
Nov 17, 2026
Merged

caomengxuan666 merged 11 commits into
masterfrom
fix/3816-literal-control-char-constants

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

根因(已验证)

字面 C0 字节在引号词内以裸字节进入 transport text,与 C0 载体标记同字节(markers.rs:CTLESC 0x11、PARAM_NAME_END_MARKER 0x13、DATA_BACKSLASH 0x14、PROTECTED_ESCAPED_SQUOTE 0x16、DATA_SQUOTE 0x17、DATA_DQUOTE 0x18、DATA_BACKTICK 0x1a、IFS_GLUE 0x1c、STORAGE_WORD_PREFIX 0x1d、DATA_DOLLAR 0x1f),被下游解码器当协议认领:赋值边界丢 CTLESC 对与 IFS_GLUE、walker 尾链把 DATA_* 还原为所指字符、decode_to_visible_text 解码其余。$'\xXX' 不受影响,因 decode_ansi_c_quoted + push_ansi_c_byte(lexer/ansi.rs)已在入口把解码出的载体字节编为 raw-byte 标记对(E000+E001+byte);单/双引号与非引号去引号路径没有这一步。modernish 32 个 CC 常量中 10 个因此损坏(CC11/CC13/CC14/CC16/CC17/CC18/CC1A/CC1C/CC1D/CC1F),其余 22 个与 GNU 一致;$'\xXX' 形式 32/32 正确。

修法引入的两处真回归(均已修掉,见下):

  • ESC 0x1b 误覆盖:dequote 入口 tagging 用 is_assignment_carrier_byte(含 0x1b),与 niubash#200 的词首-ESC 归属(tag_word_initial_raw_esc,词首打标记、词中保持裸字节)冲突,致 2 格 lib 红。
  • heredoc 反引号体内 DATA_BACKTICK 被 tagging:prepare_unquoted_heredoc_expansion 把 \`` 转为 DATA_BACKTICK 供 walker 扫描;mut walker 反引号臂直接重解析,未走 whole-word 路径的 decode_backtick_substitution_source` restore;重解析时单引号 span 含该载体字节,tagging 将其打成 [E000, E001+0x1a],argv 物化 restore 只认裸载体而漏过,echo 输出 raw 0x1a。

GNU 锚点:parse.y:5694-5706 got_character/got_escaped_character(数据侧 CTLESC/CTLNUL 前插 CTLESC);subst.c:4807 dequote_string(只去 CTLESC);subst.c:11882 case '\''(单引号 span 按字面数据处理);parse.y:4451 parse_comsub;subst.c:7143 command_substitute;parse.y:3877 parse_matched_pair(P_BACKQUOTE 下反斜杠只在 $/反引号/\ 前特殊)。

修法

  • dequote 入口 tagging:字面载体字节打成 raw-byte marker 对(E000 + E001+byte),与 ANSI-C 解码器同契约。落点:src/lexer/ansi.rs(push_tagged_source_char / push_tagged_source_span)、src/lexer/classification.rs(protect_fully_single_quoted_assignment)、src/lexer/quotes.rs(remove_shell_quotes_inner_cursor 单引号 span/通用 span/反斜杠转义、remove_double_quoted_into_cursor span、remove_double_quoted_into 逐字符、remove_shell_quotes_outside_backticks 三臂、fast-path disqualifier);fast path 不得原样返回含载体字节的词。
  • ESC 排除:新增 is_literal_source_carrier_byte(is_assignment_carrier_byte 集排除 0x1b),5 处调用点改用它;is_assignment_carrier_byte 本身不动(ANSI-C 解码器等仍用它)。
  • mut walker 反引号臂补 carrier restore:重解析前对 body 做与 decode_backtick_substitution_source 相同的 restore(DATA_BACKTICK→反引号、CTLESC→删、DATA_DOLLAR→$、PROTECTED_BACKSLASH→\);walker 已消费 \ 转义,故不重跑 decode_old_style_backtick_source。

回归钉

tests/issue3816_literal_control_bytes.rs(9 格,fail-first):单/双引号赋值 32 字节全留、非引号赋值、非引号词(含 probe_unq_word fast-path 钉)、反斜杠转义赋值、单引号词 32 字节全留、ANSI-C 赋值对照、单引号元字符守卫、32 常量拼接。基底 2/8,修后 9/9。

验证(X299,Windows MSVC)

  • 钉 9/9;lib 全量 1368 passed / 1 failed(仅既存 issue2084),与基底一致;heredoc CLI 单测 PASS;fmt 与 RUSTFLAGS="-D warnings" check 全过;三项 hygiene 全过。
  • GNU oracle 对照(Linux 侧):10 个损坏常量在 GNU 单引号赋值中全部 1 字节原样保留;heredoc 用例 GNU 输出 24 61 20 60 20 5c 2a 0a,与测试期望一致。
  • CI 5 腿失败格裁决(头 efe7f47,run 43959649058;7770c770 头同失败,非 fmt 引入):Rust tests / goldens / macOS 三腿皆为同 2 格 ESC lib 测试(真回归,已修);CLI 腿为 heredoc 1 格(真回归,已修);census 腿在 master 上亦失败(基线红),头独有格即上述 3 格(已修)。守卫 stem 三处红(marker_leak_golden 7、carrier_byte 2、issue824 2)基底同红,Windows PUA 既存面。goldens 腿另有 2 格基线红(master 同现:issue_753::case_9、upstream_suite_slice_snapshots braces snapshot),修后仍会红。详见 REPORT.md §6。

本 PR 删除了什么

无功能删除。本 PR 只改 4 个源码文件(src/lexer/ansi.rs、src/lexer/classification.rs、src/lexer/quotes.rs、src/executor/embedded_mutations.rs,均为修法与注释)并新增 1 个测试文件(tests/issue3816_literal_control_bytes.rs,9 格钉);efe7f47d 仅做 rustfmt 折叠,无行为变化。未删任何公开 API、测试或行为;未碰 src/lexer/continuation.rs;未改 docs/api-ledger.md。

UNVERIFIED-BY-CAPTAIN

A bare CR byte before LF is consumed as a CRLF line ending by the
script reader before the lexer sees it (Windows leg: unquoted CR
value comes back empty, escaped CR joins the next line) -- a
line-ending face separate from #3816. 0x0d stays pinned by the
quoted shapes.
…uote entry (Refs #3816)

A literal C0 byte inside a quoted or unquoted word is user data, but
it is byte-identical to one of the C0 transport carriers
(markers.rs), so a bare copy let downstream decoders claim it as
protocol: the assignment boundary dropped CTLESC pairs and IFS_GLUE,
the walker tail restored DATA_* carriers to the characters they
denote, and decode_to_visible_text decoded the rest. 10 of the 32
modernish CC constants were damaged this way (CC11 empty, CC14 as
backslash, CC16 as \\' , CC17 as quote, CC18 as dquote, CC1A as
backtick, CC1F as dollar, CC13/CC1C/CC1D empty).

Tag each literal carrier byte at the dequote entry as a raw-byte
marker pair (E000 + E001+byte) -- the same contract the ANSI-C
decoder already applies to its decoded bytes (push_ansi_c_byte),
whose output boundary restores the pair to the literal byte. The
$'\\xXX' form was already 32/32 correct precisely because it
tagged; literal source bytes now get the same treatment.

Sites (all dequote entry; verbatim-copy helpers for re-parse are
untouched -- they pass through this same entry and get tagged
exactly once there):
- lexer/ansi.rs: push_tagged_source_char / push_tagged_source_span
  (new shared helpers next to is_assignment_carrier_byte; span form
  bulk-copies when no carrier byte is present)
- lexer/classification.rs: protect_fully_single_quoted_assignment
- lexer/quotes.rs: remove_shell_quotes_inner_cursor (single-quote
  span, generic span, backslash-escaped char), remove_double_quoted_into_cursor
  (span), remove_double_quoted_into (iterator, per-char),
  remove_shell_quotes_outside_backticks (single-quote, backslash,
  unquoted arms)

GNU anchors: parse.y:5694-5706 got_character/got_escaped_character
(GNU inserts CTLESC before a CTLESC/CTLNUL byte arriving as data);
subst.c:4807 dequote_string (removes only CTLESC bytes);
subst.c:11882 case '\\'' (single-quoted span -> add_quoted_string
as literal data).
out is &mut String in remove_double_quoted_into_cursor /
remove_double_quoted_into, so the tagged-push call sites need
&mut *out, not &mut out (E0596).
)

remove_shell_quotes_inner_cursor's trigger-free fast path returned
the raw word unchanged -- its admission ('no trigger byte') is not
'provably trivial' (#117): a word with no quote/escape trigger can
still hold a literal control byte colliding with a transport carrier
(V=<0x11>), which then rode bare into transport and was claimed as
protocol downstream. A carrier byte now disqualifies the fast path
exactly like a trigger byte; the walk tags it in its bulk span.

This is the second leak behind the unquoted-assignment failure: the
sq/dq/esc/sq-word shapes were fixed by the dequote-entry tagging
(282489a) but unquoted assignment still failed the same 10 bytes.
Adds an unquoted plain-word pin (same function, non-assignment
position) alongside the assignment one.
Collapse the three over-wrapped forms rustfmt --edition 2021
--check flags in the dequote-entry tagging code (ansi.rs
push_tagged_source_char / push_tagged_source_span, quotes.rs
remove_shell_quotes_outside_backticks guard). No behavior change.
…ing (Refs #3816)

niubash#200 owns ESC (0x1b): word-initial ESC is tagged once by
tag_word_initial_raw_esc, mid-word ESC stays a bare byte. The #3816
dequote-entry tagging used is_assignment_carrier_byte (which covers
0x14..=0x1f, i.e. 0x1b), so it double-tagged the mid-word ESC and
stripped the bare byte, breaking the two niubash#200 lib guards.
0x1b is not one of #3816's ten damaged constants, so excluding it
does not affect the nine pins. is_assignment_carrier_byte itself is
unchanged (ANSI-C decoder and printf/mapfile/read paths keep it).
…tick re-parse (Refs #3816)

prepare_unquoted_heredoc_expansion turns \`/$ into DATA_BACKTICK/
DATA_DOLLAR so the walker scan does not read them as syntax. The
whole-word backtick entry restores those carriers before the re-parse
(decode_backtick_substitution_source); the embedded mut-walker
backtick arm dispatched straight to the parser, so a bare DATA_BACKTICK
rode into the re-parse, where the #3816 dequote tagging tagged it as a
literal source byte and the argv-materialization restore (bare carriers
only) missed it -- a literal backtick leaked as raw 0x1a. Apply the
same carrier restores here; the walker scan already consumed backslash
escapes, so no decode_old_style_backtick_source re-pass.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3817 第1轮 — 建议:FAIL
head: 6eacaf0 base(merge-base): 969230b 文件: 5 个 +478/-12
关联 issue: #3816(状态: open,我核过的时间: 2026-11-17 11:5x CST)

机制核查:

  • 方向正确:dequote 入口把字面源 C0 carrier 字节打成 raw-byte marker 对(E000+E001+byte),与 ANSI-C 解码器 push_ansi_c_byte 同一入口契约;ESC 0x1b 按 niubash#200 归属排除;embedded_mutations heredoc 嵌反引号臂在重解析前恢复 DATA_* carrier。无桥接层、无症状守卫。
  • superseded 核查:merge-base 后 master 未触碰 ansi.rs/classification.rs/quotes.rs/embedded_mutations.rs,无已合 PR 覆盖 字面控制字节常量损坏:32 个 CC 常量中 10 个被替换为元字符 #3816 —— 不 superseded。

腿:

  • 钉自跑:issue3816_literal_control_bytes 9/9 通过(head,Windows)
  • fail-first:base 969230b + 钉文件 → 2 passed / 7 failed,红名 = {controlchars_concatenation_is_32_bytes, literal_backslash_escaped_assignment_keeps_control_bytes, literal_double_quoted_assignment_keeps_all_32_control_bytes, literal_single_quoted_assignment_keeps_all_32_control_bytes, literal_single_quoted_word_keeps_all_32_control_bytes, literal_unquoted_assignment_keeps_control_bytes, literal_unquoted_word_keeps_control_bytes},与 PR 声称一致(ansi_c 与 metachar 两控制钉双侧绿)
  • lib:head cargo test --lib -j2 1368 passed / 1 failed / 4 ignored,失败名 = {api::consumer_tests::issue2084_session_end_restores_host_process_state}(已知基线 [api] 进程内 Session 改写宿主进程环境与信号处置,后代子进程继承(嵌入契约面待盘点裁定) #2084);base 侧未单独重跑(判词已由 CI 红决定,见 F2)
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --all-targets 通过,0 警告
  • 卫生:rustfmt 逐文件 5/5 rc=0;utf8-boundary-hygiene clean;marker-lint 见 F1
  • CI(run 43971070773):Rust tests job 红 = marker-lint 2 violations(本 PR 引入);census 红 47 stem vs master 37 stem,新增 10 stem 全部为 unix-only respawn/transport/heredoc/fifo 族;goldens 红 = {issue_753::case_9, upstream_suite_slice_snapshots} 基线;其余 job 绿
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1),脚本 probes-y2/rubash-3816-perbyte.sh(0x10-0x1f 逐字节单引号赋值)→ GNU 与 head 逐字节 IDENTICAL;probes-y2/gnu-3816-sq2.sh(0x10-0x1f 连续 16 字节)→ 双侧 LEN=16 一致

F 点:

  • F1: marker-lint 违规(CI Rust tests job 红)。src/executor/embedded_mutations.rs:1132 .replace(CTLESC, "") 与 :1133 .replace(DATA_DOLLAR, "$") 为未登记 site。修法明确:本文件已有 20+ 条同不变量登记(replace:DATA_BACKTICK、replace:PROTECTED_BACKSLASH 等,scripts/marker-lint-allowlist.txt:121-136),embedded_parameters.rs 的同名 decode_backtick_substitution_source 亦按「owner restore chain: bare carrier is always protocol(user data arrives E400/raw-byte paired)」登记——本 PR 的 字面控制字节常量损坏:32 个 CC 常量中 10 个被替换为元字符 #3816 tagging 恰好强化了该不变量(字面字节已成对,不可能 bare)。补两行 allowlist 即可消红,续作时可顺手做。
  • F2: census 新增 10 红 stem(本 PR run 独有;master run 44002101833/44002068162/43998401645 与 fix(parser): alias pre-pass treats quotes in comsub comments/heredocs as opaque (Refs #3813) #3821/fix(executor): expand non-ASCII bracket members to UTF-8 byte escapes in C locale (Refs #3791) #3822 run 均 0 命中):{array_compound_quote_regressions, issue1585_echo_trailing_paren_regressions, issue1649_bash_monoseconds_regressions, issue1820_fifo_redirect_regressions, issue1862_background_fifo_redirect, issue2058_respawn_transport_regressions, issue2202_lineno_span_transport_regressions, issue2332_respawn_text_body_lineno_regressions, issue2388_asyncsub_heredoc_body_regressions, issue3054_respawn_first_word_end_line}。全部 #![cfg(unix)],Windows 本机不可复跑(0 tests collected)。失败形态统一为 respawn 子进程 stdout 为空(如 issue2058 left: "" vs 期望 func=[g f main] …)——疑似 E000/raw-byte marker 对跨 respawn transport 序列化边界未解码所致,需队长 Linux 侧归因。本地 Windows 面(钉自跑/fail-first/lib/GNU 对照)全绿,缺陷只在 unix respawn 传输面暴露。

续作记录:无(判词 FAIL,未动分支;F1 修法已在 F1 下注明,待队长裁定是否续作)

未跑/VOID 的腿:

  • base lib 双边:未单独重跑(见上)
  • 10 个 unix-only stem 本地复跑:Windows 不适用,待队长 Linux oracle / CI 归因

@caomengxuan666

Copy link
Copy Markdown
Member Author

【o1 收网判词:FAIL(unix 面真回归,未合并)】head 6eacaf0d

#3816 的字面控制字节修复在 Windows 面成立,钉与 GNU 对照均通过;但本 PR 的 lexer 载体 tagging 在 unix 面打断了 async/coproc/respawn 的 body 重渲染 —— 这批钉全是 #![cfg(unix)],Windows 本机看不到,CI census(ubuntu)看到的就是它们。

Windows(本机)腿:

  • 钉 issue3816_literal_control_bytes 9/9;fail-first(base 969230b9)7 failed / 2 passed(红名 7 格全部一致)
  • GNU 对照:32 个控制字节(0x01-0x1f、0x7f)× 三种引号形态(单引号赋值 / 双引号赋值 / 单引号词)逐字节与 WSL GNU bash 5.3.20 一致;聚合 CONTROLCHARS = 32 字节(GNU 同)
  • lib 双边:head 1368P/1F、base 1368P/1F,同名 api::consumer_tests::issue2084_session_end_restores_host_process_state(本机环境绑定)→ 差集空
  • -D warnings 净、5 个改动文件 rustfmt 净

WSL Linux 原生构建(wsl bash -lc + cargo build/test,同一 worktree/同一测试文件)——这是判词依据:

stem base 969230b9 head 6eacaf0d
issue2332_respawn_text_body_lineno_regressions 12 passed 4 passed / 8 failed
issue2388_asyncsub_heredoc_body_regressions 9 passed 2 passed / 7 failed
issue2058_respawn_transport_regressions 12 passed 6 passed / 6 failed
issue1862_background_fifo_redirect 6 passed 0 passed / 6 failed
issue3054_respawn_first_word_end_line 12 passed 6 passed / 6 failed
issue1868_fifo_double_open_regressions 3P/1F(环境 fifo,两侧同) 1P/3F

失败形态是语义性的、非超时:issue2332_async_brace_group 期望 b1=4\nb2=5\n 实得 "";issue2058_respawn_children_keep_parent_shlvl 期望 asub=2\ncop=2\n 实得空 —— async/coproc 子进程 body 整体丢失。

CI 归因(run 43971070773,census 与 master 同日同基底 run 43935071198 对比;后者 head 即本 PR 的 merge-base 969230b9):

  • census:PR 131 红 vs master 79 红;PR-only 47 格,全部落在上述 5 个 async/respawn 族 + issue1820/1868 fifo 族 + 3 格 at-star/assoc(同族边缘);master-only 4 格为登记 flake
  • goldens 腿红 = issue_753::case_9 + upstream_suite_slice_snapshots(基线,master 同现)
  • 其余腿(Rust tests macOS、CLI、darwin/linux 交叉、shell 功能)绿

结论:pin 与 Windows 面可以合,但产品修法在 unix 打穿了 respawn/async body 文本路径(可能方向:把 tagging 限定到真正需要保护的 dequote 出口,或把 respawn body 文本在传子进程前统一 restore)。建议作者按 unix 原生腿返工后再 ready;崩在这批 unix-gated 钉上不能 merge。伞 #3605 与 issue #3816 均保持 OPEN。

…' bytes literal (Refs #3816)

Two regressions the #3816 dequote-entry tagging exposed on the unix
census:

1. Respawn transport carried engine carriers into a re-lexed child.
   `bash_command_source_text` prints `redirect.target` — the COOKED word
   — so `>> "$V"` shipped `$V` + PARAM_NAME_END_MARKER (0x13).  The
   child's dequote entry then tagged that protocol byte as literal data
   (#3816) and the redirect opened `/tmp/x\023`; every `( ... ) &` with
   a quoted variable redirect lost its output (issue2058/2202/2332/2388/
   3054/1820/1862/1649).  GNU never re-lexes (fork children carry the
   AST), so the renderer must emit SOURCE-level text: new
   `markers::decode_transport_word_text` (the `heredoc_delimiter_text`
   chain, rubash#1151) is now shared by `redirect_target_source_text`
   (both redirect printers) and the here-document delimiter.

2. A compound array element `([0]=$'\\[a\\]')` leaked a literal 0x11.
   The preserve-quotes ANSI-C arm wraps the decoded bytes in the
   synthetic single-quoted span GNU's `sh_single_quote` produces
   (parse.y:5564-5571), so the bytes are already quoted data — the
   CTLESC glob protection that
   `escape_decoded_ansi_c_quotes` adds is only meaningful in an
   unquoted region.  Inside the span the carrier is data, so the escape
   pair survived to argv (`array_compound_quote_regressions`).  New
   `escape_decoded_ansi_c_quotes_unprotected` is used only by that arm.

Marker-lint: allowlist the two bare-carrier restore sites in the
embedded backtick re-parse (same "engine text, bare carrier is always
protocol" invariant as the 20+ neighbours) and retire the
redirections.rs replace entry folded into the shared helper.

Verified (Windows + WSL Linux native): census stems of the audit list
9/10 green (issue1585 heredoc-in-comsub EOF warning still open),
`array_compound_quote_regressions` 17/17, `issue3816_literal_control_bytes`
9/9, lib 1374 passed / 1 failed (known issue2084 env), marker-lint OK,
`-D warnings` clean, rustfmt clean.
@caomengxuan666

Copy link
Copy Markdown
Member Author

【o1 收网】第2轮 — 判词 PENDING(F1 已修,F2 10 格 → 剩 1 格):新 commit 6c9bd9634 已推 origin(未改写历史;git ls-remote 核 head=6c9bd9634)。

head 6c9bd96(父 6dab10bf8 = 原 6eacaf0 合入 origin/master 8a85f33);base(merge-base) 969230b;关联 #3816 保持 OPEN(缺陷未全绿,不关单)。

F1(marker-lint 2 违规)— 已修

embedded_mutations.rs 的两处 bare-carrier restore 按邻接 20+ 条同不变量登记
("input is engine-encoded word/storage text where a bare carrier is always protocol(user data arrives E400/raw-byte-paired),故 bare strip 不会吃 data");同时把 parser/redirections.rs 的 replace:PARAM_NAME_END_MARKER 条目并入新的共享 helper 后退休。python scripts/check-marker-literals.py → OK (118 registry consts, 269 allowlisted sites)。

F2(census 10 格)— 9/10 已绿,剩 1 格

WSL Linux 原生构建逐 stem 复跑(target/wsl-probes/c4.log):

  • 已绿:issue2058(12/12)、issue2202(10/10)、issue2332(12/12)、issue2388(9/9)、issue3054(12/12)、issue1649(17/17,array_compound_quote 同档)、issue1820(4/4)、issue1862(6/6)、issue1585 的另外 3 格。
  • 仍红:issue1585_echo_trailing_paren_regressions::issue1585_comsub_eof5_quoted_form_still_yields_hi。

根因(已定位,两个机制缺陷)

  1. respawn 文本带 engine carrier 进子 lexer(修掉 8 个 stem 的主因)。
    bash_command_source_text 打印 redirect.target(cooked word),( echo x >> "$V" ) & 于是把 $V + PARAM_NAME_END_MARKER(0x13) 送进子进程;子进程 dequote 入口把该 protocol 字节当字面数据 tagging(字面控制字节常量损坏:32 个 CC 常量中 10 个被替换为元字符 #3816),open(2) 实际打到 /tmp/x\023(strace 实证),文件永远没建。GNU 从不 re-lex(fork 子进程拿 AST),所以重渲染必须是 source-level 文本:新增 markers::decode_transport_word_text(即 heredoc_delimiter_text 既有链,rubash#1151:CTLESC 对→数据字符、DATA_*→其所指、丢 PARAM_NAME_END、除 ANSI_C_IFS_GUARD),由 redirect_target_source_text(append_function_redirect + builtin_names::format_redirect 两处打印器)与 heredoc delimiter 共用。
  2. 合成单引号 span 内仍打 CTLESC glob 保护(array_compound_quote_regressions::dollar_single_quoted_subscript_element_decodes_once)。
    preserve_quotes 的 ANSI-C 臂按 GNU parse.y:5564-5571 用 sh_single_quote 语义包一层单引号,字节已是 quoted data;escape_decoded_ansi_c_quotes 仍给 *?[ 前插 CTLESC——保护只在 unquoted 区域有意义,放进单引号里就变成数据,([0]=$'\[a\]') 于是存成 \+0x11+[a\]。新增 escape_decoded_ansi_c_quotes_unprotected(仅该臂使用),glob 保护在 unquoted 臂保持原样。array_compound_quote_regressions 17/17、issue3816_literal_control_bytes 9/9。

剩余 1 格的形态

echo "$(\ncat <<\)\nhi\n))":GNU 输出 hi 且报 warning: here-document at line 2 delimited by end-of-file (wanted )');head 把 ))"行当成 delimiter 终止了 heredoc,输出hi+)且无 warning。即 comsub 内 heredoc 在"delimiter)/ 闭合))`"交叠处的 gather 终止判定与 GNU make_cmd.c:626 的 EOF 判定不一致(z2=master 基线实测与 GNU 一致 → 确属本 PR 面)。这是 parser/heredoc gather 的独立根因,未在本轮修完;不掩盖、不写守卫。

腿(本轮实跑)

  • 钉:issue3816_literal_control_bytes 9/9(Windows);array_compound_quote_regressions 17/17(Windows+WSL)
  • fail-first 沿用第1轮(base 969230b 2p/7f,红名一致);本轮新增回归钉在 master 基线由 z2 二进制实测通过(p6.sh 全 8 形 + eof5 形状逐字节对 GNU)
  • lib(head,Windows):1374 passed / 1 failed / 4 ignored,唯一失败 = api::consumer_tests::issue2084_session_end_restores_host_process_state(既存环境);base lib 沿用同窗口 master 结果(同名单点)
  • RUSTFLAGS="-D warnings" cargo check --locked 0 警告;涉改 7 文件 rustfmt --check 全 OK;LF 无 CRLF
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1);脚本 target/wsl-probes/{p5,p6,p2,p3,p4}.sh 与 strace 证据在 worktree(未入库)
  • CI:未跑(head 刚推,census 未出);下次跑需按新 head 归因

未跑/VOID:CI 逐格、WSL 全量 lib(磁盘曾到 43G 暂停构建,已恢复)、issue1585 的紫格修复。判 PENDING 而非 PASS:census 仍有 1 甲确定性红,合入会带一棵红进 master。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【P1 车道复核】PR #3817 第2轮 — 判词:FAIL(1 个 PR 引入的语义回归,跨 Windows/Linux 复现;前一轮 10 个 census 红 stem 中 9 个已由新 head 修好)

  • 复核 head:6c9bd96343b1d0b3936d9c9438d80a50a4ebaf7d(含 6dab10bf8 merge master da256e71a;本轮再 merge origin/master b2e488e54,无冲突)
  • 关联 issue 字面控制字节常量损坏:32 个 CC 常量中 10 个被替换为元字符 #3816:保持 OPEN(未关闭任何 issue/PR)
  • 构建:Windows MSVC cargo build 绿;marker-lint python3 scripts/check-marker-literals.py 绿(上一轮 F1 已随 master allowlist 消解)
  • 钉:issue3816_literal_control_bytes 9/9 通过;-D warnings 无警告

1) 上一轮 census 10 个红 stem 归因(同机同环境,Linux WSL,base=origin/master 独立 worktree)

方法:run-census-3817.sh,两个 worktree 各自 cargo test --test <stem>,逐 stem 240s 超时;产物 target/issue-suites/results/census-3817-attribution.txt。

stem base(da256e7) head 6eacaf0(上一轮) head 6c9bd96(当前) 归因
array_compound_quote_regressions PASS 17/17 FAIL 1(dollar_single_quoted_subscript_element_decodes_once,多出 0x11 CTLESC) PASS PR 引入,已修
issue1585_echo_trailing_paren_regressions PASS 4/4 FAIL 1 FAIL 1 PR 引入,未修
issue1649_bash_monoseconds_regressions PASS 4/4 PASS PASS 基线 flake,非 PR
issue1820_fifo_redirect_regressions PASS 3/3 FAIL 2 PASS PR 引入,已修
issue1862_background_fifo_redirect PASS 6/6 FAIL 6 PASS PR 引入,已修
issue2058_respawn_transport_regressions PASS 12/12 FAIL 6 PASS PR 引入,已修
issue2202_lineno_span_transport_regressions PASS 10/10 FAIL 7 PASS PR 引入,已修
issue2332_respawn_text_body_lineno_regressions PASS 12/12 FAIL 8 PASS PR 引入,已修
issue2388_asyncsub_heredoc_body_regressions PASS 9/9 FAIL 7 PASS PR 引入,已修
issue3054_respawn_first_word_end_line PASS 12/12 FAIL 6 PASS PR 引入,已修

上一轮 respawn 族根因已由 6c9bd9634 的 command_text::redirect_target_source_text(重渲染 redirect target 先做 decode_transport_word_text)修好:最小复现 ( echo hi > "$O" ) & 在 6eacaf0 上子进程静默不执行(>> "$V" 的 PARAM_NAME_END_MARKER 被重词法按字面 \023 吃进路径),现与 GNU 一致。

2) 未修回归(阻断项):双引号 comsub 内 escaped-delimiter heredoc

用例就是既有 stem tests/issue1585_echo_trailing_paren_regressions.rs::issue1585_comsub_eof5_quoted_form_still_yields_hi:

echo "$(
cat <<\)
hi
))"
shell stdout stderr
WSL GNU bash 5.3.20(脚本文件) hi\n warning: here-document at line 2 delimited by end-of-file (wanted )')`
master(b2e488e54) hi\n 同上
PR head 6c9bd96(Windows 与 Linux 同形) hi\n)\u{1c}\n 无

cargo test --test issue1585_echo_trailing_paren_regressions 在 Windows head 上同样红:

left: "hi\n)\u{1c}\n"
right: "hi\n"

即结果里多出一个 ) 和一个 IFS_GLUE 0x1c 载体外泄,且 GNU 的 heredoc EOF warning 消失。base 侧该 stem 全绿,故非基线/flake。

3) 根因判断(GNU C 锚点)

GNU:parse.y:4451 parse_comsub() 提取的 comsub body 在 ) 处结束,heredoc 因此读到 body EOF → redir.c here-doc 的 EOF warning;parse.y:5694-5706 got_character/got_escaped_character 只在词法扫描源字节时前插 CTLESC,subst.c:4807 dequote_string() 只去 CTLESC,subst.c:11882 单引号 span 按字面数据。

本 head 的具体缺陷:src/lexer/quotes.rs 把它当成「dequote 入口的裸字节必是字面源数据」并无条件打 raw-byte marker 对,但该入口拿到的文本已经含引擎载体——本例里的 IFS_GLUE 0x1c 是 lexer 为字段分割插入的协议字节,被 push_tagged_source_char 当成字面数据打成 E000/E001 对后,下游解码未能还原(观测:最终 argv 里出现裸 0x1c),comsub body 的 heredoc 分隔符匹配也随之走偏()) 行被当成终止行 → warning 消失,多出的 )/0x1c 进入 echo 输出)。

也就是说 #3816 的不变量「dequote 入口的 carrier 字节是用户数据」对通用 dequote 路径不成立:只有能证明是源字节的位置(例如 protect_fully_single_quoted_assignment 的源 span)才成立;remove_shell_quotes_outside_backticks 的通用臂 / remove_double_quoted_into 逐字符臂收到的是已经过 lexer 协议化的文本。

4) 变绿需要什么

  1. 不要对通用 dequote 入口的全部 carrier 字节打 raw-byte 对;要么把 marking 前移到真正的源扫描层(区分 lexer 生成的 CTLESC/IFS_GLUE/PARAM_NAME_END_MARKER/DATA_* 与用户字面字节),要么把该臂的 admission 收成可证明为字面源数据的白名单([架构] 文本层快速路径必须收敛:$() 词级捷径全仓审计 + 要求逐条对照 GNU C 源码 #117 口径:黑名单/逐症状守卫不接受)。
  2. 用 GNU C 说明 IFS_GLUE 0x1c 在该入口的归属(subst.c:11229 expand_word_internal 的 IFS 处理 vs parse.y 的源字节),并给出跨类矩阵:单/双引号 × 赋值/实参 × 字面/转义/替换 × comsub/heredoc(不只 eof5 单点)。
  3. 修好后重跑 cargo test --test issue1585_echo_trailing_paren_regressions 与上面 10-stem 台账;两张表都绿才可 ready。

未合并:head 仍带 1 条跨平台语义回归,合入即给 master 带红。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【P1 车道复核 · 补充】#3817 唯一残留的精确根因(已实验定位,供后续修法参考)

承接 2026-11-17 上一条评论(10 stem 归因表 + 唯一残留 issue1585_comsub_eof5_quoted_form_still_yields_hi)。本轮把根因收敛到具体行列,结论不变:判词 FAIL,不合并,保持 draft。

精确机制

GNU make_cmd.c:602-611 的「) 落在 heredoc 定界符行上 → heredoc 由 EOF 终止并告警」在 rubash 里由一条**词法期拼接的 IFS_GLUE(0x1c)**标记:

  1. src/lexer/mod.rs:1499:当 comsub 的 ) 出现在 heredoc 定界符行(本例定界符就是 ),行是 )))时,在定界符之后、第一个 ) 之前 insert(IFS_GLUE),让逻辑行变成 hi\n)\x1c)"。
  2. src/executor/command_substitution_pipelines.rs:718 command_substitution_heredoc_output_mut_typed():消费这个裸 0x1c → 走 GNU 的 EOF 告警分支。
  3. src/executor/embedded_mutations.rs:2416-2417:真实 parser 之前 replace(IFS_GLUE, "") 兜底删除。

#3816 的 dequote 入口 tagging 在 0x1c 上失配:

  • src/lexer/quotes.rs:2532(remove_double_quoted_into 的逐字符臂)等 push_tagged_source_char(&mut out, ch) 把这一步看到的 0x1c 当成字面源数据,改写成 E000 + E001 + 0x1c 三字符 raw-byte 对。
  • 于是 (2) 的裸 0x1c 检测落空 → GNU 的 heredoc EOF warning 消失;
  • (3) 的兜底删除也落空 → 该三字符对一路存活到 argv,出边界时被还原成字面 0x1c(观测输出 hi\n)\u{1c}\n);同时定界符行文本从 )\x1c 变成 ) + 三字符对,行匹配偏移,) 多出一个。

即:IFS_GLUE 在 dequote 入口既有字面源数据身份(本例赋值里用户写的真实 0x1c,必须保留),也有词法期簿记身份(mod.rs:1499 的拼接,必须被消费/删除),两者字节相同,dequote 这一层无法凭字节判定。

已验证的排除项

正确修法的方向(需要协议级改动)

二选一,两者都属于 carrier/协议面,需按仓库规则跑完整 ledger(83 suite 切片 + 10 stem 台账):

  1. 前移到源层 tagging:在 lexer 把源字节写进 word buffer 的位置(source→word 的唯一入口)就打 raw-byte 对,dequote 阶段看到的所有裸 carrier 从此只可能是协议;protect_fully_single_quoted_assignment 那类源 span tagging 保持不变。
  2. 给这条簿记一个专属标记:markers.rs 注册一个 PUA 常量(E000..E4FF 之外的用户数据区),mod.rs:1499 拼该常量、两个消费者改查该常量,tagging 完全不碰它;需同步 registry + marker-lint allowlist,并证明它不会从任何输出边界外泄。

本轮未选任一(会动 carrier 协议且无法在本地跑完整 ledger),故不合。上一轮的关键实测与 10 stem 归因表见本 PR 上一条评论(issuecomment-6942370695)与 D:/repo/rubash/target/devin-dispatch/p1.md。

PR 保持 OPEN + draft,未 ready、未合入;issue #3816 保持 OPEN。

…(Refs #3816)

The quote-removal command-substitution copiers closed a substitution at
the first unescaped close-paren, ignoring here-documents gathered inside
the body. GNU parse_comsub (parse.y:4451) gathers pending
here-documents, so a close-paren on a delimiter-prefixed line is
consumed by the gather, not read as the substitution closer
(make_cmd.c:602-627).

For a double-quoted substitution whose heredoc delimiter is a paren, the
early close ejected the IFS_GLUE bookkeeping byte into the surrounding
quote span, where the literal-carrier tagging encoded it as a raw-byte
pair. The heredoc plan then lost its closed-by-paren evidence: the
warning vanished and a paren-plus-0x1c leaked into the output.

Mirror the span extractor: both body copiers (cursor and char forms)
now consume here-document bodies via the same
skip_heredoc_in_command_substitution the extractor uses, and copy
arithmetic spans verbatim so a shift operator inside $((...)) is never
misread as a here-document start (rubash#2930's guard).
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】PR #3817 第3轮 — 建议:PASS(本地全腿绿;Unix-only census 腿待 Linux CI 复核)
head: 307633e(原 6c9bd96 + 返工 commit) base(merge-base): da256e7(fail-first 实测树 b2e488e = merge-base+1,无关 net-bind 提交) issue: #3816 OPEN

根因与修法(回应上一轮精确根因评论)

上一轮把残留归因为「IFS_GLUE 在 dequote 入口字节同形、无法判定身份」,给出两个协议级方向(源层前移 tagging / 专属 PUA 标记)。本轮实测发现更深一层的机制错误,修法不需要动 carrier 协议:

  • copy_dollar_paren_body_raw_cursor / copy_dollar_paren_body_raw(quotes.rs 引号剥离期的 $( verbatim 拷贝器)此前不认识 heredoc,在 body 内第一个未转义 ) 处闭合。本例 "$(\ncat <<\\)\nhi\n)\x1c)" 在定界符前缀 ) 处提前闭合,把引擎簿记字节 \x1c 挤出 verbatim 区,落入外层双引号 span 的 carrier-tagging 臂,被当成字面源字节编码成 E000/E001 对 → heredoc 计划丢 closed-by-paren 证据 → 警告消失 + )+0x1c 外泄。
  • GNU 侧 parse_comsub(parse.y:4451,PST_EOFTOKEN parse.y:4513)在解析后 gather 挂起 heredoc(parse.y:4563-4567),定界符行上的 ) 由 gather 消费而非充当闭合符;make_here_document(make_cmd.c:512+,告警 make_cmd.c:602-627)据此在 EOF 处告警。dequote 期 span 检测本应与提取期 skip_cmd_subst(skip.rs:16+,heredoc 臂 skip.rs:169-174 → skip_heredoc_in_command_substitution skip.rs:230)一致——此前两者对同一输入给出不同闭合点,这正是 bug。
  • 修法:两个 body 拷贝器新增 <</<<- heredoc 跳过,直接复用 Lexer::skip_heredoc_in_command_substitution(comsub_heredoc_skip,quotes.rs:1174),保证 cooked span 与 extracted span 永远一致;heredoc 区内 ) 不再触发闭合。header 行含 ) 的闭合形状($(cat <<EOF))由旋转机械预处理,skip 返回 closes 时拷贝器同样以 gather 后文本收尾,与 extractor 的 break 语义对齐。
  • 连带补齐 $(( 算术守卫(镜像 skip.rs:130-146 rubash#2930):body 拷贝器与两个 substitution 包装函数在 $( 后识见 (( 且 dollar_double_paren_is_arithmetic 为真时,整段 verbatim 拷贝至 ))(新增 copy_arith_paren_body_cursor/raw),防止算术左移 << 被新 heredoc 臂误判。

F 点回应

  • F1(IFS_GLUE 0x1c 外泄 + 警告消失,issue1585_comsub_eof5_quoted_form_still_yields_hi):已修。根因是 dequote 拷贝器缺 heredoc 语义导致簿记字节逃逸到 tagging 臂;现簿记字节留在 verbatim $( 区内。复现 echo "$(\ncat <<\\)\nhi\n))":修复前 hi\n)\x1c\n 无警告;修复后 stdout hi\n + warning: here-document at line 2 delimited by end-of-file (wanted \)')`,与 GNU 逐字节一致。
  • F2(marker-lint embedded_mutations.rs CTLESC/DATA_DOLLAR):PR 已随附 allowlist 条目(scripts/marker-lint-allowlist.txt +2 行,owner restore chain 论证),lint 实测 OK,无需再动。
  • F3(10 个 Unix-only census 红 stem):本机 Windows 下 7 个 stem 0 用例(cfg(unix) 全空)、issue1649 4/4、array_compound_quote_regressions 17/17、issue1585 4/4 全绿;respawn/fifo 族为 Unix-only,Windows VOID,待 Linux CI 的 Regression pin stem census 腿复核。

腿(逐项数字)

  • 钉自跑:issue1585_echo_trailing_paren_regressions 4/4(修复前 3/4,eof5 红);issue3816_literal_control_bytes 9/9。
  • fail-first:base(b2e488e) 上 issue3816 钉 2/9,红名 = {controlchars_concatenation_is_32_bytes, literal_backslash_escaped_assignment_keeps_control_bytes, literal_double_quoted_assignment_keeps_all_32_control_bytes, literal_single_quoted_assignment_keeps_all_32_control_bytes, literal_single_quoted_word_keeps_all_32_control_bytes, literal_unquoted_assignment_keeps_control_bytes, literal_unquoted_word_keeps_control_bytes},与 PR 声称一致(fail-first 有效);issue1585 在 base 4/4 绿 → 证实该红为 PR 引入、本轮已消。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同集 / 差集 {}(issue2084 为已知基线 [api] 进程内 Session 改写宿主进程环境与信号处置,后代子进程继承(嵌入契约面待盘点裁定) #2084)。head 1374 passed / base 1374 passed。
  • -D warnings:RUSTFLAGS="-D warnings" cargo check --locked --all-targets 0 警告。
  • 卫生:rustfmt 已逐文件跑(quotes.rs);marker-lint OK(118 consts / 269 sites);utf8-boundary 5 处违规均在 src/executor/strmatch.rs(本 PR 未触碰文件,基线)。
  • CI(新 head 307633e):niubash builds against this tree 红 = cmp rubash/docs/api-ledger.md niubash/docs/api-ledger.md 跨仓 ledger 不同步(byte 62406),与本 PR 无关(未触碰 docs),归基础设施/niubash 侧同步基线;其余任务 pending 中。
  • GNU 对照(WSL /usr/local/bin/bash 5.3.20,LF 脚本文件,target/probes-3817/matrix/):12/12 形状 stdout+rc 一致——m1 quoted-eof5、m2 unquoted-eof5(stderr 警告逐字一致)、m3 )独占行、m4 EOF) 后缀、m5 header-close $(cat <<EOF)(stderr command substitution: 1 unterminated here-document 一致)、m6 body 含 )、m7 "$((1 << 2))"、m8 comsub 内算术移位、m9 嵌套 comsub+heredoc、m10 引号内 comsub+heredoc、m11 双 heredoc、m12 <<- tab 剥离。
  • 相邻面复跑:issue1981/issue1982 heredoc-comsub stem 在改动前后同败(0/4、0/3,cat: command not found——Windows 无 cat,环境 VOID,非本 PR 账);issue2025 6/6、issue2026 5/5、issue1151 1/1、issue1184_f5 2/2、heredoc_regressions 3/3、comsub_nonascii_span_regressions 10/10 全绿。

续作记录

未跑/VOID 的腿

  • Unix-only census 7 stem(fifo/respawn/lineno 族):Windows 下 0 用例,全 VOID,需队长 Linux oracle 或 CI census 腿复核。
  • issue1981/issue1982:Windows 环境无 cat,两侧同败,环境 VOID。
  • CI 全量任务仍在 pending,合入前需等绿。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 17, 2026 09:25
@caomengxuan666
caomengxuan666 merged commit ccb8614 into master Nov 17, 2026
8 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
…que spans (#3836)

* fix(#3823): funsub ${...} bodies treat # comments and heredocs as opaque spans

Root cause: three funsub/${...} extent scanners quote-tracked through
# comments and heredoc bodies, so an apostrophe in a comment (# don't)
opened a phantom single-quote that swallowed the rest of the input, and
a } line inside a heredoc body either closed the funsub early or was
swallowed with it. ${x:-$(...)} leaked its closing } into the default
word for the same reason one layer down (the ${...} body collector had
no $(...) unit arm).

GNU anchors (bash 5.3): parse.y:3631-3645 shell_getc (# at a token start
discards to EOL); parse.y:4451 parse_comsub (body parsed by the real
parser); parse.y:5506 funsub (parser.h:85 FUNSUB_CHAR); make_cmd.c:512
make_here_document (body read as raw lines); subst.c:1825
extract_dollar_brace_string.

- src/lexer/skip.rs: Lexer::skip_braced funsub branch gains a
  token-boundary-gated # comment arm and a << heredoc arm.
- src/executor/command_substitution.rs: extract_funsub_body_tail gains
  the same two arms (new push_funsub_heredoc helper).
- src/executor/command_subst_helpers.rs: collect_braced_parameter_body_into
  skips $(...) as one unit via skip_parenthesized_unit_corrected.
- src/lexer/dolbrace.rs: skip_parenthesized gains a # comment arm
  (comment-only; heredocs stay with the corrected scanner) + unit test.
- src/lexer/alias_stream.rs: skip_dollar_brace gains a funsub branch
  delegating to skip_funsub_body.
- tests/issue3823_funsub_lexer_residual.rs: regression pins f1/h2/h3/d1.

Known limitation: f1/h2/h3 also need the mut walker's funsub collector
(expand_current_shell_braced_substitution, embedded_mutations.rs), which
is BLOCKED on in-flight #3817 (same file). Until that hunk lands, the
f1/h2/h3 pins are expected RED (literal-text values); d1 is green with
this change. See REPORT.md in the workdir.

Refs: #3823

* fix(executor): funsub ${...} bodies treat # comments and heredocs as opaque spans (Refs #3823)

Complete the two missing funsub arms so all four pins pass:

- expand_current_shell_braced_substitution (embedded_mutations.rs):
  add the at_word_start token-boundary state, the parse.y:3631
  shell_getc `#` comment arm, and the make_cmd.c:512 heredoc arm
  (reusing push_funsub_heredoc, `<<<` stays a herestring), mirroring
  extract_funsub_body_tail.
- scan_braced_chars_from (dolbrace.rs): dispatch `${`+FUNSUB_CHAR to
  alias_stream::skip_funsub_body so the residual gates
  (param_residuals, unquoted_brace_group_depth,
  command_substitutions_balanced, continuation.rs callers) stop
  closing the span at a `}` inside a heredoc body or a `'` inside a
  comment.
- db_scan_dolbrace_body (skip.rs): same FUNSUB_CHAR dispatch into a
  new db_scan_funsub_body carrying comment/heredoc/quote/comsub arms
  and the command-position `}` close, so unclosed_dollar_bracket_eof
  stops reporting a phantom `'` for f1/h2.
- current_shell_command_substitution_span (parameter_core.rs): take
  the extent from extract_funsub_body itself instead of a parallel
  depth count, so the detector and the collectors share one scan;
  `prefix${| echo reply }suffix` now correctly has no span (GNU:
  unexpected EOF while looking for matching `}', rc 2).

GNU anchors: parse.y:3631-3645 shell_getc, parse.y:4451 parse_comsub,
parse.y:5506 FUNSUB_CHAR (parser.h:85), make_cmd.c:512
make_here_document, parse.y:5400-5416 funsub `}` close. Probed on
GNU bash 5.3.20: all four pin shapes and the heredoc `}`-line
variants match byte-for-byte.

---------

Co-authored-by: 曹梦轩 <[email protected]>
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