Skip to content

fix(glob): export strmatch for loadables and route GLOBIGNORE through it (Refs #3605) - #3630

Merged
caomengxuan666 merged 4 commits into
masterfrom
devin/strmatch-loadable-export
Nov 17, 2026
Merged

caomengxuan666 merged 4 commits into
masterfrom
devin/strmatch-loadable-export

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Refs #3605 (macOS gate class A).

glob-bracket.tests compiles strmatch.so with -undefined dynamic_lookup; after enable -f, the plugin calls the host symbol strmatch(). niu exported no C symbols, so dyld bound the call to an unbound address → SIGSEGV (exit 139) on first invocation.

  • src/executor/strmatch.rs (new): single-byte port of lib/glob/strmatch.c (strmatch() at 32-39), smatch.c helpers (FOLD 301-304, SDOT/PDOT 313-314, collsym 168-182, is_cclass 283-297, dequote_pathname glob.c:431), and the full sm_loop.c GMATCH/PATSCAN/BRACKMATCH/EXTMATCH under the real FNM_* flags (strmatch.h:35-55). Exported #[no_mangle] extern "C" strmatch so loadables resolve it against the host image exactly as GNU bash exports it.
  • src/executor/glob.rs ignore_pattern_matches(): pathexp.c:543-546 glob_name_is_acceptable() runs every ignore spec through strmatch(pattern, name, FNM_PATHNAME|FNMATCH_EXTFLAG|FNMATCH_NOCASEGLOB) — one matcher. Extglob specs previously went to the conditional case-pattern engine, which lacks the PATHNAME n < endp bound (sm_loop.c:284-290): GNU never tries an extglob sitting AT the slash position, so *@(/)cd/efg must not match ab/cd/efg (glob-bracket.right [Bug 批量] 命令替换状态污染 / heredoc / case 模式 / 参数替换 / 路径转换 / 重定向 / 调试钩子 - 20+ 个与 GNU Bash 的兼容性差异(含最小复现) #20). Now routed through the unified port with GNU's exact flag set.

Single-byte (C locale) port; internal_wstrmatch multibyte path not implemented.

Verified (macOS, niubash gate)

  • run-glob-bracket: PASS (was SIGSEGV / exit 139)
  • run-extglob, run-extglob2, run-extglob3, run-globstar, run-quote: all PASS, no regressions
  • New tests/issue3605_globignore_strmatch_regressions.rs pins the *@(/) and plain-pattern PATHNAME bounds end-to-end through the shell.

… it (Refs #3605)

macOS gate class A: glob-bracket.tests builds strmatch.so with
-undefined dynamic_lookup and the plugin crashed calling back into the
shell — niu exported no C symbols, so dyld bound the strmatch() call to
an unbound address and the first invocation SIGSEGVed.

* src/executor/strmatch.rs: single-byte port of lib/glob/strmatch.c
  (strmatch() at 32-39), smatch.c helpers (FOLD 301-304, SDOT/PDOT
  313-314, collsym 168-182, is_cclass 283-297, dequote_pathname
  glob.c:431), and the full sm_loop.c GMATCH/PATSCAN/BRACKMATCH/EXTMATCH
  under the real FNM_* flag set (strmatch.h:35-55). Exported
  #[no_mangle] extern "C" strmatch so loadables resolve it against the
  host image exactly as GNU bash exports it.

* src/executor/glob.rs ignore_pattern_matches(): pathexp.c:543-546
  glob_name_is_acceptable() runs every ignore spec through
  strmatch(pattern, name, FNM_PATHNAME|FNMATCH_EXTFLAG|FNMATCH_NOCASEGLOB)
  — one matcher. Extglob specs previously went to the conditional
  case-pattern engine, which lacks the PATHNAME n < endp bound
  (sm_loop.c:284-290): an extglob sitting AT the slash position is never
  tried by GNU, so *@(/)cd/efg must not match ab/cd/efg
  (glob-bracket.right #20). The engine also let * cross /.
  Now routed through the unified port with GNU's flag set.

Verified on macOS via niubash gate: run-glob-bracket PASS (was SIGSEGV
exit 139); run-extglob, run-extglob2, run-extglob3, run-globstar,
run-quote all still PASS. tests/issue3605 covers the *@(/) and
plain-pattern PATHNAME bounds end-to-end through the shell.

Refs #3605

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

caomengxuan666 commented Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3630 第1轮 — 建议:FAIL
head: 847825a base(merge-base): 96802ca 文件: 4 个 +1031/-25
关联 issue: #3605(伞目录 issue,状态 open,不得关闭;本 PR 只 Refs)

机制核查:

  • 主线成立:pathexp.c:528-549 glob_name_is_acceptable 对每条 GLOBIGNORE spec 只调一个 strmatch(p->val, name, FNM_PATHNAME|FNMATCH_EXTFLAG|FNMATCH_NOCASEGLOB);FNMATCH_EXTFLAG/NOCASEGLOB 定义见 pathexp.h:39-45(extglob → FNM_EXTMATCH、glob_ignore_case → FNM_CASEFOLD)。旧代码把 extglob spec 送 conditional case-pattern 引擎(缺 PATHNAME n<endp 界,sm_loop.c:284-290),确属错路;新 src/executor/strmatch.rs 的 GMATCH/PATSCAN/BRACKMATCH/EXTMATCH 与 sm_loop.c:80-390 对照读过,* 折叠、/ 分支、--p 回溯、FNM_DOTDOT/LEADING_DIR 收尾一致;#[no_mangle] extern "C" strmatch 对应 lib/glob/strmatch.c:32-39(GNU 从 shell 镜像导出同一符号)。
  • 但 GLOBIGNORE 路径在 GNU 上是 strmatch → xstrmatch(smatch.c:585-603):MB_CUR_MAX>1 且 name/pattern 含多字节字符时走 internal_wstrmatch(宽字符)。本 PR 只有单字节 CHAR 版,且把原来的 char 级 strmatch_pathname 路径整个替换掉,多字节面因此回退。
  • 无关改动:无(glob.rs 只改 ignore_pattern_matches;mod.rs 一行 module;新 matcher + 新钉)。

腿(逐项给数字与名字):

  • 钉自跑:cargo test --test issue3605_globignore_strmatch_regressions → 0 passed / 0 failed(文件 #![cfg(unix)],Windows 整文件 cfg 掉 = VOID)。
  • fail-first:同 stem 在 base 96802ca 上同样 0 tests(VOID)。用二进制手工判别:GLOBIGNORE='*@(/)cd/efg'; echo */*/efg*(fixture target/probes/g3630)base 输出 */*/efg*(错,被吞),head 输出 ab/cd/efg = GNU 5.3.20。缺陷面在 base 可复现,钉在 Linux 上应能判别。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 失败名集 {同左} / 差集 ∅。
  • -D warnings:本 PR 文件 0 新增(无关 stem issue2036_s2_introspection_windows 既有 field stderr is never read)。
  • 卫生:fmt rc=0(4 文件逐个);marker-lint OK(117/268);utf8 脚本不存在(VOID)。
  • CI:红任务 = Rust tests / Rust tests (macOS) / GNU regression goldens and canaries;红名只有 api::consumer_tests::issue2875_input_procsub_child_runs_as_rubash_shell(基线 issue2875)。macOS loadable 面 CI 未覆盖(run-glob-bracket 属 niubash gate,本 lane 无法执行,作者自报 PASS)。
  • GNU 对照(WSL GNU bash 5.3.20,脚本 target/probes/m3630.sh / m3630e.sh / g3630d.sh / g3630e.sh):纯 pattern 13 形 + extglob 9 形 全部 byte-identical;多字节形见 F1。

F 点(建议 FAIL):

  • F1: 多字节 GLOBIGNORE 行为回退。GNU smatch.c:585-603 xstrmatch 在 multibyte locale 对含多字节字符的 name/pattern 用 internal_wstrmatch;本 PR 单字节端口使 ? 只吃一个字节、[Ä] 只比一个字节。实测(fixture u/Ä.txt,LANG=C.UTF-8 LC_ALL=C.UTF-8,脚本 target/probes/g3630e.sh):
    • GLOBIGNORE='u/?.txt'; echo u/* → GNU u/*(吞掉)/ base u/* / head u/Ä.txt(错)
    • GLOBIGNORE='u/[Ä].txt'; echo u/* → GNU u/* / base u/* / head u/Ä.txt(错)
    • GLOBIGNORE='u/??.txt'; echo u/* → GNU u/Ä.txt / base u/Ä.txt / head u/*(错,反向多吞)
      对照口径补充(公平起见):LC_ALL=C LANG=C 下 GNU 同样是字节语义(w1 u/Ä.txt、w2 u/Ä.txt、w3 u/*),此时 head 与 GNU 一致,而 base 的 char 级实现反而错(会吞掉 w1/w2)。GNU 是按 MB_CUR_MAX(smatch.c:592-596)在 internal_strmatch 与 internal_wstrmatch 之间切换的,所以两个实现各错一个 locale;本 PR 把「UTF-8 默认环境」这一侧打红 = 在主流 Linux locale 下回退。
      修法方向:按 GNU 的 MB_CUR_MAX/multibyte 判定条件化——多字节 locale 下走宽字符(或保留旧 char 引擎做 fallback),MB_CUR_MAX==1 时才走新的单字节端口;并把上面 3 形按两种 locale 各加进钉。
  • F2(提示): src/executor/glob.rs 现在同时存在两份 strmatch 移植(新 strmatch::strmatch_bytes 与旧 strmatch_pathname/parse_bracket_match char 版),同名 GNU 语义由两个 owner 解释,后续修一处漏一处风险高;建议在 F1 的 fallback 里就统一掉,而不是并存。

未跑/VOID 的腿:

  • 钉自跑/fail-first:#![cfg(unix)] → Windows 0 tests,VOID(用 head/base 二进制 + GNU 5.3.20 手工替代,见上)。
  • macOS dyld loadable 面(-undefined dynamic_lookup 解析 strmatch):本平台不可执行,作者自报 niubash gate PASS,本 lane 未复核。

…th (Refs #3605)

smatch.c:585-603 xstrmatch(): when locale_mb_cur_max > 1 and either the
name or the pattern carries multibyte characters, GNU converts both to
wide chars and runs internal_wstrmatch; the single-byte internal_strmatch
is used otherwise. The previous commit ported only the single-byte
compile, so under UTF-8 `?`/`[Ä]` in a GLOBIGNORE spec consumed one byte
apiece: `u/?.txt` leaked `u/Ä.txt` and `u/??.txt` over-swallowed it
(audit F1 on #3630).

- strmatch.rs: lift the sm_loop engine onto `char` elements. Bytes are
  raised through the Latin-1 bijection (order- and equality-preserving),
  so strmatch_bytes stays the single-byte compile while the new
  strmatch_chars entry is the wide compile. An internal FNM_WIDE flag
  selects towlower-style folding (char::to_lowercase) and the glibc-
  aligned isw* predicates in locale::utf8_posix_class_matches instead of
  the ASCII domains (smatch.c:301-304 FOLD, smatch.c:283-297 is_cclass).
- glob.rs ignore_pattern_matches: mirror the xstrmatch dispatch —
  is_multi_byte_active() plus a non-ASCII byte in pattern or name (the
  mbsrtowcs "has multibyte" probe for &str inputs) routes to
  strmatch_chars; everything else stays byte-wise.
- The superseded strmatch_pathname/BracketMatch/parse_bracket_match/
  bracket_class_matches/posix_class_matches tree is removed; the unified
  port now serves both locales.
- New pin tests/issue3605_globignore_strmatch_multibyte.rs spawns the
  binary under LC_ALL=en_US.UTF-8 and LC_ALL=C (locale is env-driven, so
  the pin discriminates on Windows too, addressing the audit's
  cfg(unix)-void note). GNU 5.3.20 reference output: UTF-8 `u/?.txt`
  leaves `u/ab.txt u/Äb.txt`; C locale leaves
  `u/ab.txt u/Ä.txt u/Äb.txt u/é.txt`.

Verification: probe matrix (GLOBIGNORE `?`/`??`/`[Ä]`/`[[:alpha:]]`/`*`,
extglob `@(Ä|é)`/`@(?)`/`?(Ä)`, nocaseglob `Ä`/`[ä]`) byte-identical to
GNU bash 5.3.20 under both locales; pins 6/6 green.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

Rework pushed as 2479884b.

F1 (multibyte): ignore_pattern_matches now performs the xstrmatch dispatch from smatch.c:585-603 — under a multibyte locale with non-ASCII data in pattern or name it routes to strmatch_chars, the wide compile. The engine itself was lifted onto char elements in src/executor/strmatch.rs: bytes enter through the Latin-1 bijection (order/equality-preserving), so strmatch_bytes remains the single-byte compile while an internal FNM_WIDE selector switches fold to towlower-style lowercasing and is_cclass to the isw* domains via the existing locale::utf8_posix_class_matches (same glibc-aligned predicate the pattern matcher uses). The exported strmatch C ABI still resolves to the byte compile, matching strmatch.c:32-39.

Verified against GNU bash 5.3.20 byte-for-byte under both LC_ALL=en_US.UTF-8 and LC_ALL=C: u/?.txt, u/??.txt, u/[Ä].txt, u/[[:alpha:]].txt, u/*.txt, extglob @(Ä|é)/@(?)/?(Ä), nocaseglob Ä/[ä] — all identical. UTF-8: u/?.txt now swallows Ä.txt/é.txt/c.txt; C: byte-wise as before (the over-swallow the old char engine had under C is gone too since the byte path is domain-correct).

F2 (pin void on Windows): new tests/issue3605_globignore_strmatch_multibyte.rs drops the cfg(unix) gate — it spawns CARGO_BIN_EXE_rubash with LC_ALL set explicitly, which selects the wide path on unix and Windows alike (utf8_locale_active accepts a UTF-8 name on non-unix). Four discriminating cases assert the GNU outputs for both locales; the UTF-8 ? case fails on the byte-only head and the C case fails on the old char-only engine.

The dead strmatch_pathname/parse_bracket_match/bracket_class_matches/posix_class_matches tree is deleted — the unified port now serves both locales.

…able-export

# Conflicts:
#	src/executor/glob.rs
Loadable builtins resolve `strmatch()` against the shell image. GNU's
exported symbol wraps `xstrmatch` (lib/glob/strmatch.c:33-39), so the C
ABI must follow the MB_CUR_MAX dispatch of smatch.c:585-603 instead of
answering from the single-byte compile: under a multibyte locale with
multibyte data GNU runs `internal_wstrmatch`, and `?`/`[Ä]` consume one
character.

`ignore_pattern_matches` now calls the same `xstrmatch` entry, so the
GLOBIGNORE path and the C ABI share one dispatch; the single-byte and
wide compiles stay separate below it.

Linux links the binary with `-rdynamic` (build.rs), mirroring GNU's
configure:23147 `linux*) LOCAL_LDFLAGS=-rdynamic # allow dynamic loading`.
Without it `#[no_mangle] strmatch` stays out of `.dynsym` and a loadable
dies on its first call.

Pins: a real loadable fixture (tests/fixtures/dynamic_builtin/
strmatch_probe.c) asserts the exported ABI under LC_ALL=C.UTF-8 and
LC_ALL=C; GNU bash 5.3.20 is byte-identical on both.
@caomengxuan666

Copy link
Copy Markdown
Member Author

判词 P — 收网 rework(head e1e1101c,base/master f02d7299d)

F1(multibyte 回退)已修:xstrmatch 现在是唯一分派点(smatch.c:585-603):

  • MB_CUR_MAX==1(rubash 口径:locale::is_multi_byte_active() 为假)→ internal_strmatch 单字节;
  • 否则若 name+pattern 均单字节(mbsmbchar 探续字节,lib/sh/utf8.c:49-58)且 pattern 的 [:class:] 全为 POSIX 名(posix_cclass_only,smatch.c:505-545)→ 仍走单字节;
  • 其余 mbsrtowcs 失败回退单字节,成功 → internal_wstrmatch。

GLOBIGNORE 也改走同一 xstrmatch,F2 的两份移植已收敛为一条(旧 strmatch_pathname 树删除,glob.rs 不再自判 locale)。

额外发现并修复(本轮新增,原 PR 未覆盖):

  1. #[no_mangle] strmatch 原先直连单字节编译,但 GNU 导出符号是 xstrmatch 的薄封装(lib/glob/strmatch.c:33-39)——loadable 调 strmatch('u/?.txt','u/Ä.txt') GNU 返回 0,原 head 返回 1。现 C ABI 也走 xstrmatch。
  2. Linux 上符号在静态 symtab、不在 .dynsym:build.rs 为 bin 加 -rdynamic,依据 GNU configure:23147 linux*) LOCAL_LDFLAGS=-rdynamic # allow dynamic loading。没有它,loadable 首次调用即死(本机上 enable 成功、调用时静默终止)。

腿(WSL Linux 原生构建:cargo build --bin rubash,/usr/local/bin/bash 5.3.20 脚本文件基线)

  • 钉(WSL,cargo test):issue3605_globignore_strmatch_regressions 3 passed(含新 loadable 格)、issue3605_globignore_strmatch_multibyte 4 passed。
  • fail-first(base f02d7299d,WSL 原生构建,同钉):loadable 格 FAILED(base 无导出符号,无输出);2 个 GLOBIGNORE 格 base 已绿(master 4c7c2d19c fix(glob): GLOBIGNORE pathname matching for slash brackets and extglob (Refs #2664) #3705 独立修过,非本 PR 判别点)。多字节 4 格 base 亦绿——它们是「本 PR 不得回退」的守卫,不是新缺陷判别;这一点如实记录。
  • C ABI 8 形(LC_ALL=C.UTF-8 / LC_ALL=C):u/?.txt、u/??.txt、u/[Ä].txt、ab@(/)cd/efg、*@(/)cd/efg、literal、u/?.txt vs u/c.txt、[[:alpha:]] — GNU 与 rubash 逐字节相同(含 UTF-8 u1=0/u2=1/b1=0,C u1=1/u2=0/b1=1)。
  • GLOBIGNORE 17 形 × 两 locale:逐字节相同;另跑 audit 原 w1/w2/w3 四形两 locale:相同。
  • 回归钉:[upstream] Glob: extglob alternative may match / in pathname expansion; slash-in-bracket cases halt output entirely #2664 两文件各 6 passed(统一 matcher 未丢 [upstream] Glob: extglob alternative may match / in pathname expansion; slash-in-bracket cases halt output entirely #2664 的 bracket-slash/extglob PATHNAME 界)。
  • lib 双边:head 失败名集 {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同名集 / 差集 ∅。
  • RUSTFLAGS="-D warnings" cargo check:0 警告(顺带删除旧树遗留的两个 unused re-export)。
  • rustfmt:改动 4 个 .rs 全 --check 通过;合并带入的 2 个 master 文件按 hook 要求格式化后入 merge commit。
  • 未跑:macOS dyld loadable 实机(本平台不可执行,Linux .dynsym 与 -rdynamic 已实证)。

#3605 伞 issue 保持 OPEN。探针与输出:target/devin-dispatch/o3-3630-*.sh|out、smprobe.*。

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