Skip to content

2.0 机械替换打样: true/false 接线 rust2 staticlib 并删除 C++ 实现 (#1488) - #1550

Merged
caomengxuan666 merged 3 commits into
mainfrom
rust2/wire-true-false
Nov 9, 2026
Merged

caomengxuan666 merged 3 commits into
mainfrom
rust2/wire-true-false

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

#1488 的第一次真实机械替换打样:true + false 从 C++ 实现换成 rust2 库实现,链进 winuxcmd.exe,C++ 旧实现同 PR 删除(不双轨)。配方沉淀在 rust2/REPLACEMENT-RECIPE.md,下一个命令照做。

决策

  • 链接形态 = staticlib(不是 cdylib)。[ffi] winuxcore.dll 产品路径实证:调用不应用 shell 重定向(数据丢失)+ dispatch 期不轮询取消 + 出货认领面为空(niubash#727 门①③ 皆红) #1487 / niubash#727 的根因是 DLL 边界的加载期 CRT stdio 绑定;staticlib 由同一次 link 折进 winuxcmd.exe,一个映像一个 CRT。winuxcmd-ffi 同一 ABI 源出两种 crate-type(cdylib 留给宿主实验)。
  • CRT 对齐:rustc 的 MSVC 默认是 /defaultlib:msvcrt(/MD),winuxcmd 是 /MT。Rust 侧加 -C target-feature=+crt-static 后归档要的是 libcmt;链接零 LNK4098。USE_STATIC_CRT=OFF 时自动去掉该 flag。
  • 边界:C++ 只保留 REGISTER_COMMAND 注册(help/man/补全元数据)和调度器共用的前置机制(唯一参数 --help/--version 拦截、改写钩子、选项策略);handler 体换成一行 Rust 调用。产品的 help/version 文本是全部 176 个 applet 共享的 WinuxCmd 品牌格式,逐命令搬走会破坏门 c 并分叉 help 体系。

改动(3 个 commit,一事一 commit)

  1. rust2(true/false):接线前的 Oracle 复核发现 pilot 本身偏离 GNU。true.c 只在 argc == 2 时认 --help/--version,并保持 EXIT_STATUS(false --help 打印后仍 rc 1);pilot 却是任意位置都认,而且返回 0。这些行当时被分类为 note,从不进门。修正为唯一参数规则,新增 2 个单测(55/55 绿);t06/t14/f06/f14 升为 strict,f07 改为 win。run-gate.sh 的 TREE_MNT 原先硬编码成 wc-20rust,换个工作树 GNU 侧就静默 MISSING,现改为从 checkout 推导。
  2. 接线:
    • CMakeLists.txt:WINUXCMD_ENABLE_RUST2 开关(检测到 cargo 才默认 ON;显式 ON 但没有 cargo 时配置期报错);WINUXCMD_RUST2_COMMANDS 清单;custom command 执行 cargo rustc --release --locked --crate-type staticlib,CARGO_TARGET_DIR=<build>/rust2-target(不污染 rust2/target);imported lib 加上 rustc 报告的 native libs。
    • 新增 src/shared/rust2_abi.h(ABI v1 的 C 镜像)和 src/bridge/rust2_bridge.cppm(module rust2_bridge:用 Win32 取 env/cwd 快照,不抛异常,输出经 console 层出口)。
    • true.cpp/false.cpp 的 handler 改为 return winux::rust2::run_command(name, ctx.raw_args);。
  3. 配方:rust2/REPLACEMENT-RECIPE.md;wiring 矩阵探针 rust2/gate/wire/;README 指针。

未装 Rust 的机器

隐藏 cargo(PATH、CARGO_HOME、USERPROFILE\.cargo\bin)后实测:配置自动设为 OFF,winuxcmd.exe 构建成功、可运行;true/false 不随包发布(command not found)。这是"换完即弃、不双轨"的代价:没有 C++ 兜底体。CI/打包机需要装 Rust 工具链。

三重门账本(Debug build-vs;Oracle = WSL coreutils 9.4;rebase 到 c9ed2a1 之后复跑)

门 对象 结果
a GNU oracle 22 例矩阵 × true/false(无参、-h、唯一 --help/--version、带多余操作数的 help/version、非法长/短选项、--、-、---、--help=x、stdin、POSIXLY_CORRECT…) rc 44/44,stderr 44/44,stdout 34/44。不一致的 10 行全部是唯一参数 --help/--version 的品牌文本,与 1.x 逐字节相同,rc 仍对齐 GNU
a GNU oracle run-gate.sh true false,RUST_BIN=新 exe 38 strict + 2 win 全绿
b 既有测试 全量 ctest 改动前后失败集完全相同:3830 例中 20 个失败(rebase 后 3837 例中 20 个,同一批名字,与 true/false 无关);true_cmd/false_cmd 10/10;差分语料 true/false 在新旧 exe 上均 4/4 PASS
c 与旧 exe 逐字节 改动前拷出的旧 winuxcmd.exe vs 新 exe rc/stdout/stderr 44/44/44;run-gate 的 r=c 列 40/40;Release(unity + LTCG)构建同样 44/44/44

体积:Debug winuxcmd.exe 增大 1.0 MB(Rust std,一次性成本,后续命令共享)。

遗留(已写入配方 §5)

  • ABI v1 的 stdin 是整块 buffer,bridge 不转发 stdin。读 stdin 的命令(cat/tr/sort…)要等流式 ABI v2。
  • bridge 的 stdout 走 safePrint,后者会对整串做 translate_legacy 目录查找。true/false 没有输出,不受影响;要打印数据的命令(basename/dirname/printenv…)接线前,先在 console 层加一个不翻译的原样写出口。
  • native libs 清单是固定写死的;rust2 若新增系统依赖导致 LNK2019,按配方里的一行命令刷新。

备注

本机 git 到 github.com 的传输不通(本地代理 127.0.0.1:65532 拒绝连接),分支是经 GitHub Git Data API 重放推上来的。每个 blob/tree/commit 的 SHA 都与本地逐一核对一致(HEAD 5aba132f)。

…1488)

GNU coreutils true.c recognizes --help/--version only when argc == 2 and
keeps EXIT_STATUS afterwards, so `false --help` prints and exits 1 while
`true --help x` is a silent success. The wave1 pilot scanned every
argument and returned 0 after printing, which the gate never caught
because those rows were classified `note`. Probe (WSL coreutils 9.4):
false --help -> rc 1 + text; true x --help -> rc 0, no output;
POSIXLY_CORRECT=1 true --help still prints.

- truefalse.rs: sole-argument rule, status kept; 2 new unit tests
- gen-cases.py: t06/t14/f06/f14 -> strict, f07 -> win (branded text)
- run-gate.sh: derive the WSL tree path from the checkout instead of
  the hard-coded /mnt/d/repo/wc-20rust (gate ran GNU-less elsewhere)
- GATE-REPORT.md: true/false rows re-measured, 38 GNU-gated + 2 win green

This is the wiring wave's precondition: winuxcmd.exe now runs this
function for true/false.
First mechanical replacement of the 2.0 command-library track: winuxcmd.exe
now runs rust2's truefalse.rs for `true`/`false`; the C++ implementation is
deleted (no dual track).

Link shape: staticlib, not cdylib. #1487/niubash#727 traced the FFI damage
to the DLL boundary (load-time CRT stdio binding); a staticlib is folded
into winuxcmd.exe by the same link - one image, one CRT. winuxcmd-ffi now
builds both crate types from one ABI source; Rust is compiled with
+crt-static so the archive asks for libcmt, matching USE_STATIC_CRT (/MT).

- CMakeLists.txt: WINUXCMD_ENABLE_RUST2 (default ON iff cargo is found;
  explicit ON without cargo is a configure error), WINUXCMD_RUST2_COMMANDS
  list, `cargo rustc --release --locked --crate-type staticlib` custom
  command with CARGO_TARGET_DIR=<build>/rust2-target, imported static lib
  with the rustc-reported native libs. OFF filters out the replaced
  commands and the bridge: a machine without Rust still builds, without
  true/false.
- src/shared/rust2_abi.h: C mirror of the winuxcmd_run ABI (v1).
- src/bridge/rust2_bridge.cppm: module rust2_bridge, run_command(): env
  and cwd snapshot via Win32 (no throwing calls; no /EHsc here), stdout/
  stderr drained through the console layer. stdin is not forwarded (ABI
  v1 is buffer-only), so only non-stdin commands may use it.
- src/commands/true.cpp, false.cpp: registration metadata unchanged (the
  dispatcher's branded sole --help/--version stays shared); handler body
  is `return winux::rust2::run_command(name, ctx.raw_args);`.

Gates (Debug build-vs, WSL coreutils 9.4 oracle):
- GNU oracle, 22-case matrix x2: rc 44/44, stderr 44/44, stdout 34/44
  (the 10 rows are sole --help/--version: branded text, identical to 1.x);
  rust2/gate/run-gate.sh with RUST_BIN=<new exe>: 38 strict + 2 win green.
- ctest: 3830 tests, failing set identical before/after (20, none
  true/false related); true_cmd/false_cmd 10/10; differential corpus
  true/false 4/4 on old and new exe.
- old exe vs new exe: rc/stdout/stderr 44/44/44 (also on a Release/unity
  /LTCG build); run-gate r=c 40/40.
- no-cargo machine (cargo hidden): configures OFF, builds, runs.
REPLACEMENT-RECIPE.md distills the first real replacement into reusable
steps: the fixed decisions (staticlib + crt-static, ABI v1, what stays C++),
the per-command procedure (baseline capture, Rust-side gate refresh, the
one-line handler rewrite, CMake list append), the three gates and how to
run them with the existing harness (RUST_BIN/CPP_BIN pointing at the
product exe), and the pitfalls met on the way (note rows hiding pilot
divergence, argv[0] multicall naming, probe-loop stdin loss, WSL env
forwarding, the #1412 pre-include vs clang-format, /EHsc-less bridge code,
no-Rust configure simulation).

gate/wire/ holds the wiring-wave matrix probe (22 cases x true/false,
rc/stdout/stderr) and the comparator; README.md and GATE-REPORT.md updated
(true/false rows re-measured: 19 strict + 1 win each, C++ parity 19/20).
@caomengxuan666
caomengxuan666 merged commit 4be9012 into main Nov 9, 2026
17 of 21 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 10, 2026
note rows are record-only and never gate; that made them a blind spot.
The `false --help` pilot bug (#1550) lived inside a note row until manual
wiring checks caught it. This tool ends indefinite immunity:

  1. inventory — every note-mode row with its expected-deviation claim
     from gate/note-registry.tsv (undocumented notes are a finding).
  2. GNU re-check — each note case re-run against GNU (WSL), Rust, C++;
     verdicts:
       UPGRADE-STRICT    Rust byte-matches GNU; the row can gate again
                         (C++ divergence stays recorded via r=c:no)
       UPGRADE-EXIT      rc matches GNU, stdout platform-form-bound
       MASKED-DIVERGENCE the note hides a live Rust-vs-GNU divergence
                         — the exact #1550 class
  3. upgrade-candidate list — promotion decisions owned by the command
     lane; this sweep only reports.

First sweep (df wave1, real data):
  - df/df12-output-subset — UPGRADE-STRICT candidate (Rust rc+stdout
    byte-match GNU; C++ deviation inherited-not-fixed)
  - df/df13-drive-path — MASKED-DIVERGENCE: GNU rc=1 (no such file under
    WSL), Rust rc=0 prints a filesystem table (registered deviation:
    Rust follows the C++ drive model). Command lane must fix or
    re-justify; the registry claim documents it, the probe keeps it
    visible.

Exit 1 when masked divergences exist (CI-wireable). Refs #1488, #1550.
aphrody-labs Bot pushed a commit to aphrody-labs/WinuxCmd that referenced this pull request Nov 15, 2026
…st (winx#1488, unixwin#1531)

Port of the C++ engine's compile-time prefilters (regex.cppm
Pattern::fast_ / build_fast_path — the kwset/dfa "must" analysis
precedent of unixwin#1531) to rust2/crates/winux-regex:

- anchored: first consumable element is `^` -> single attempt at 0,
  none when start > 0 (AnchorStart compares absolute positions).
- prefix: required literal prefix (Literal runs, single-member classes,
  {m} literal expansions, zero-width assertions transparent) ->
  memchr-style candidate jumps; the interpreter still validates every
  candidate through the unchanged local_attempt path.
- first: 256-byte first-byte set expanded per byte through the engine's
  own class_matches/fold rule (single source of truth, matcher's rule
  hoisted to a pub(crate) fn); legacy advance rule kept, only the
  futile attempts are skipped.
- pure_literal: whole tree Literal-only with no captures -> a prefix
  candidate IS the match, byte-for-byte the interpreter's result.
- Unmodeled shapes fall back to the legacy scan; a continuation-range
  prefix first byte is rejected in build (candidates would sit on
  positions the legacy traversal never attempts). Necessary conditions
  only — no semantic fork (unixwin#1550 lesson).

Correctness gates: the unixwin#1603 138-row GNU grep -o matrix is byte-identical
to the pre-change baseline in BOTH feature modes; new
tests/fastpath_equiv.rs (hand corpus per tier + 120k-seed deterministic
fuzz) requires fast vs disable_fast_path equality across checked_find /
checked_match / find_all / matches_entire, WorkLimit surfaced identically.
The filters live behind the default `fast-path` feature plus a
doc(hidden) disable_fast_path() knob for A/B diffing.

Performance (47 MiB / 819k lines, release, unixwin#1531 shape): grep -c fox
9.713s -> 57.979ms (167x); first-tier (f|F)(o|O)(x|X) 16.726s ->
405ms (41x); dot-head fallback control unchanged (~1% noise);
pathological (.*)*b and \(.*\)*b long lines stay WorkLimit-bounded in
both modes (75-80s, unchanged) — the unixwin#1603 caps are untouched.
@caomengxuan666
caomengxuan666 deleted the rust2/wire-true-false branch November 18, 2026 05:50
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