Skip to content

fix(hash): re-validate relative hash entries on every read (Refs #3359) - #3554

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/3359-hash-relative-path-revalidate
Nov 15, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/3359-hash-relative-path-revalidate

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #3359

修了什么

如果命令是经相对 PATH 条目(PATH=.:…)记进哈希表的,文件删掉后 rubash 仍原样给出旧拼写:执行报 ./mycmd / <TMP>/./mycmd: No such file or directory,type、command -v、hash -t 仍报已哈希。GNU 的 phash_search 在每次读表时复核相对条目:./ 形可执行就给出;否则若存的路径以 . 开头且所在目录就是 cwd,则返回 NULL,查找回落到 PATH 漫游(command not found,127)。表项本身保留,直到漫游在别处命中时被覆盖。

  • src/builtins/hash.rs:新增共享实现 phash_search(env, name) / phash_search_spelling(path, env)。它取代原先 hash -t 私有的 phash_search_target,探针改为锚定会话 cwd(G-02)。
  • src/executor/path.rs find_user_command:查哈希镜像就是走一遍 phash_search。表项被拒时,丢掉该名的 PATH 扫描旧备忘,改走 PATH 漫游。checkhash 复核作用在 phash_search 给出的值上,与 GNU 顺序一致。
  • src/executor/lookup_paths.rs(command_path / command_paths)、src/executor/type_describe.rs:type、command -v 的读面改走 phash_search。
  • record_command_resolution:若表项被 phash_search 拒绝(即本次结果来自漫游),按 phash_insert 覆盖并把 times_found 置 1,不再给旧项计数。
  • hash NAME:记录 recorded_resolution_spelling(与执行记录走同一漏斗,[A2-13] Hash table stores absolutized path; type prints it instead of the as-found relative spelling #2696),并同步镜像。原先存的是锚定会话的绝对路径,后续读面因此看不到它的相对性。

删了什么

  • 删去 hash -t 私有的 phash_search_target(已并入共享实现)。
  • 删去 hash NAME 的绝对路径存储,以及"find_user_command 已写缓存,所以不需要镜像"这条旁路。

附带:tests/d11_pipeline_squote_carrier_regressions.rs 在 master(#3511)合入时未格式化,导致 pre-commit 的 rustfmt 检查失败。本 PR 对它跑了 rustfmt,只改格式;#3531 带有同一改动,两边字节相同。

GNU 依据

  • hashcmd.c:126 phash_search():HASH_RELPATH 分支 hashcmd.c:146-161(./ 形可执行即给出)、hashcmd.c:176-187(same_file(".", dir) 成立则返回 NULL);hashcmd.c:115 记 RELPATH 标志。
  • findcmd.c:365 search_for_command 先调 phash_search;返回 NULL 时 findcmd.c:400-426 漫游,并以 phash_insert(..., found=1) 覆盖。
  • builtins/type.def:353 describe_command 与 builtins/hash.def:284 list_hashed_filename_targets 同样读 phash_search。
  • builtins/hash.def:213-225 add_hashed_command:phash_insert(w, find_user_command(w), dot_found_in_search, 0),存的是搜索时的原样拼接。

验证

回归测试 tests/issue3359_hash_relative_path_revalidate.rs:4 个测试,#![cfg(unix)],期望值是 GNU 5.3.20 跑同一脚本的 stdout/stderr,逐字节比对。

环境 修前 修后
Linux(WSL 构建,同一树) 0/4 4/4

议题种子 seed_r93b2.sh(Linux):修后 10/10 PASS(GNU 10/10)。

同族 stem 在 Linux 全部通过:issue1360 2/2、issue1361 6/6、issue1362 2/2、issue1368 2/2、issue1382 3/3、issue1489 1/1、issue1551 14/14、issue1648 4/4、issue2066 9/9、issue2514 6/6、issue2518 2/2、issue2696 2/2、issue2700 2/2、issue2736 9/9、issue2972 16/16、issue3020 9/9、issue3083 4/4、issue3147 10/10、type_p_flag_regressions 13/13;--lib hash 5/5、--lib lookup 5/5。

Windows:同一组 stem 修前修后的失败测试名单逐个相同,无新增失败;--lib hash 6/6、--lib lookup 8/8。既有的失败共 26 行 FAILED,都是本机 Windows 环境下修前就存在的(issue1361 5、issue1489 1、issue1551 9、issue2700 1、issue2972 3、type_p_flag 1 等)。

Owed

caomengxuan666 and others added 2 commits November 14, 2026 13:07
GNU hashcmd.c:126 phash_search is the single read of a remembered
location for execution (findcmd.c:365 search_for_command), type /
command -v (type.def:353) and hash -t (hash.def:284). For a stored
relative path (HASH_RELPATH) it serves the ./-prefixed form when
executable and answers NULL when the stored ./path's directory is the
cwd and the file is gone (hashcmd.c:146-187), so the lookup re-walks
PATH. rubash applied that rule only to hash -t and served the stored
spelling unvalidated everywhere else; `hash NAME` also stored the
cwd-anchored path instead of the search's as-written join
(hash.def:213-225 add_hashed_command).

- builtins::hash::phash_search / phash_search_spelling: one shared
  implementation, probes anchored at the session cwd.
- path::find_user_command's hash-mirror consult goes through it; a
  refused entry drops the stale PATH-scan memo and walks PATH.
- type/command -v describe faces read through phash_search.
- record_command_resolution overwrites a refused entry (phash_insert,
  times_found = 1) instead of bumping it.
- `hash NAME` records recorded_resolution_spelling and syncs the mirror.

Also rustfmt tests/d11_pipeline_squote_carrier_regressions.rs, which
landed unformatted on master (#3511) and fails the pre-commit check.

Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…3359)

Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】

PR #3554 第1轮 — 建议:PENDING(缺腿)
head: 309cf74 base(merge-base): 8b48c16 文件: 6 个
关联 issue: #3359(状态:open,我核过的时间:2026-11-15 本地)

机制核查:

  • 新增单一读口 phash_search(builtins/hash.rs):stored RELPATH(不以 / 开头)每次读时复验——dotted ./ 形可执行则返回;不可执行且目录即 cwd 则答 NULL、条目保留(GNU hashcmd.c:146-191 phash_search、:176-187 same_file)。执行(findcmd.c:365)、type/command -v(type.def:353)、hash -t(hash.def:284)三个消费面都改路由到它——lookup_paths.rs 两处、type_describe.rs verbose、path.rs 的 mirror consult 全部接入。
  • hash NAME 记录改为 recorded_resolution_spelling(as-written join,非 session-anchored),并对内部 lookup cache 同步写(对应 findcmd.c:416-426 phash_insert 语义)。
  • record_command_resolution 增加 served 判据:phash_search 拒绝的陈旧条目不计 hits 且被 PATH-walk 结果覆盖(times_found=1 语义)。
  • 可疑/无关改动:d11 仅 rustfmt;无桥接修法;HASH_CHKDOT 未建模但注释明确指向 rubash#3551(自列 Owed)。

腿:

  • 钉自跑:issue3359_hash_relative_path_revalidate 全文件 #![cfg(unix)]——Windows 上 0/0 VOID;CI 的 macOS job 只跑 cargo test --lib,该钉在任何 CI 腿均不执行。
  • fail-first:base 上同样 0/0,本平台不可判钉红绿。
  • lib 双边:head {api::consumer_tests::issue2084_session_end_restores_host_process_state} / base 同 / 差集 ∅。
  • -D warnings:1 error:tests/issue1331_1334_signal_and_startup.rs run_script never used——不在本 PR diff,基线存量。
  • 卫生:rustfmt --check 六个改动文件全过。
  • CI:3 个红 job 红名单各只有 issue2875_input_procsub_child_runs_as_rubash_shell,全为基线。
  • GNU 对照:WSL /usr/local/bin/bash 5.3.20(1)-release,探针 target/audit-w2/w3359/case.sh、case2.sh。hash -p ./mycmd 显式存相对拼写后 rm mycmd:hash -t 与 GNU 一致(hash: mycmd: not found rc=1,条目仍列在 hash -l 输出 builtin hash -p ./mycmd mycmd);执行 miss rc=127 一致。

残差疑点(需 Linux oracle 复核,未计入判词):

  • Windows 上 type mycmd/command -v mycmd 在条目失效后仍打印 mycmd is ./mycmd rc=0(GNU rc=1)。归因方向:type 的非 verbose 路径先经 describe_emulated_claim_with_io(!cfg!(windows) 早退,type_describe.rs:296-315),疑为 Windows claim 面旁路了 phash_search——unix 上该面不存在,钉断言 type rc=1 应可在 Linux 复现;需 Linux oracle 确认。
  • Windows 上 hash NAME 记录的是 /d/.../w3359/./mycmd(anchored 绝对形)而非 GNU 的 ./mycmd——显示面分歧,疑为 G-02 session-cwd 锚定的 Windows 显示契约,需 Linux 上确认记录的是 as-written join。

未跑/VOID 的腿:

  • 决定性腿:unix-only 钉的全部 4 个测试(explicit_hash / run_once / refound / relative_entries)——本机无 Linux 工具链、WSL 无 cargo、CI 无集成测试 unix 腿。机制与 GNU 源码逐点吻合、Windows 可及面局部一致,但按规程不把没跑写成通过,故 PENDING。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】

PR #3554 第2轮 — 建议:PASS(缺腿补齐 + 两残差疑点复核完毕)
head: 309cf74(与审计一致,无人推进) base(merge-base): 8b48c16
关联 issue: #3359

本轮补腿(WSL Linux /root/rubash-wsl,CARGO_INCREMENTAL=0,cargo test --locked -j 2):

  • head 309cf74:cargo test --test issue3359_hash_relative_path_revalidate → 4 passed / 0 failed。
  • base 8b48c16 + 钉 overlay:→ 0 passed / 4 failed。红集 = 全部四格 {explicit_hash, run_once, refound, relative_entries},签名:base 记录 anchored /tmp/..././mycmd、stale 后 type/command-v/hash-t 仍 rc=0 且 mycmd is hashed (...)、refound 格 PATH 重查未覆盖 stale 条目(run rc=127)。fail-first 精确命中修复面。

两残差疑点的 Linux oracle 复核(LF 脚本文件,GNU /usr/local/bin/bash 5.3.20 vs rubash head,target/probes-3554/case.sh):

  • 残差 a(type/command -v stale 仍 rc=0):Linux head 上 type mycmd → type: mycmd: not found rc=1、command -v rc=1,与 GNU 逐字节一致 → 审计所见 Windows rc=0 为 Windows claim 面(describe_emulated_claim_with_io 的 !cfg!(windows) 门控)独有,unix 路径 phash_search 复验正确。
  • 残差 b(hash NAME 记录 anchored 绝对形):Linux head 记录 ./mycmd(as-written join,mycmd is hashed (./mycmd)、builtin hash -p ./mycmd mycmd),与 GNU 一致 → Windows 所见绝对形为 session-cwd 锚定的 Windows 显示契约,非本 PR 缺陷。
  • 全量探针输出 rubash vs GNU 双侧逐字节一致。

未动代码;其余腿按第1轮审计记录全绿。判词翻 PASS,按协议 squash 合并。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 04:47
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】补充记录(合并阻塞)

判词 PASS 不变,但 gh pr merge --squash 报 "Pull Request has merge conflicts"(mergeStateStatus=DIRTY)。按协议第 9 条不解冲突、跳过合并,待 owner 解冲突后可直接合(腿已全绿)。已 gh pr ready 去 draft。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-REWORK】

PR #3554 第3轮 — 建议:PASS(冲突已解 + 腿复核)
head: 637e2f5(309cf749 + merge origin/master 9f2cf15,普通 push 叠加 merge commit,未改写历史)
关联 issue: #3359

冲突解(owner 授权 merge origin/master):

  • 实际冲突仅 src/builtins/hash.rs 一处(lookup_paths.rs/path.rs 自动合一)。master [bug] Explicit 'hash name' records the absolutized path for relative PATH entries (follow-up of #2696) #3358 与本 PR 改同一行:master 记 as-found 拼写但刻意不同步 lookup cache(注释自承 REPORT.md §4 mirror-sync 残差)。取本 PR 面(recorded + set_command_lookup_cache 同步写)——它正是 master 留下的残差的完成态,两修目标一致、本 PR 语义包超集。
  • 产品语义判定:非机械相邻行,属语义取面;GNU 依据同前(builtins/hash.def:213-225 phash_insert、findcmd.c:416-426)。

合并头复核腿:

  • Linux 编译 + 钉自跑(/root/rubash-wsl,CARGO_INCREMENTAL=0):cargo test --test issue3359_hash_relative_path_revalidate → 4 passed / 0 failed。
  • GNU 对照(LF 脚本,GNU 5.3.20 vs Linux rubash 合并头,/tmp/probe3359):执行、type、hash -t、hash -l、rm 后 stale 三面全 逐字节一致(hash -t/type/command -v rc=1 + not found,条目仍列 builtin hash -p ./mycmd mycmd)。
  • Windows 残差复核(Windows rubash.exe 合并头,target/probes-3554/win-run):两残差原样复现,与第1轮审计归因一致——anchored /d/.../win-run/./mycmd 显示形 + rm 后 type/command -v/hash -t 仍 rc=0;归 !cfg!(windows) claim 面(type_describe.rs:296-315)与 G-02 session-cwd 锚定,非本 PR 缺陷、按审计不计入判词。
  • CI:7 job 绿;3 红(Rust tests / macOS / GNU goldens)红名单各仅 issue2875_input_procsub_child_runs_as_rubash_shell,全为基线。
  • mergeable=MERGEABLE / mergeStateStatus=CLEAN。

判词 PASS 维持,按协议 squash 合并。

@caomengxuan666
caomengxuan666 merged commit 46bb796 into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
…g (Refs #3845) (#3847)

find_user_command's hash-mirror consult handed phash_search's stored
string to callers verbatim as a PathBuf. The hash table keeps the
shell-domain display spelling (/d/..., ./elem/name) for `hash -t`
fidelity; on unix that string already is the filesystem path
execute_cmd.c:6133 shell_execve execve()s, but on Windows it is not
spawnable — Command::new ENOENTs and the diagnostic escapes to the
shell's stderr, e.g. `cat cap4 | wc -c` inside a command substitution
after any earlier `cat` run recorded the entry (golden issue1230).

Introduce: #3554 (46bb796) replaced `return Some(path.clone())` —
the mirror's fs path — with the verbatim served spelling for the
CHKDOT/RELPATH re-validation arm. #3631 is innocent: the parent tree
(9148457) reproduces the leak identically.

Return shell_path_to_windows(served) instead — the same funnel
record_command_resolution used to build the mirror. It is the
identity on unix, maps /d/... to D:\..., and keeps ./elem/name
relative so the post-cd ENOENT re-resolution of rubash#2696 is
preserved. The checkhash probe now stats the real file too.

Pin: executor::path::tests::
hash_consult_returns_spawnable_fs_path_for_display_spelling (red
before, green after).

Generated with [Devin](https://devin.ai)

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
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.

[bug] Deleted file hashed via a relative PATH entry is not re-validated on lookup

1 participant