Skip to content

fix(executor): admit Mach-O images in the exec gates on macOS (Refs #3585) - #3586

Merged
caomengxuan666 merged 1 commit into
masterfrom
devin/macos-macho-exec
Nov 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
devin/macos-macho-exec

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #3585

What

On macOS, rubash's two exec admission gates whitelist only #!, \x7fELF, and MZ, so every Mach-O binary is classified as an ENOEXEC candidate and refused with cannot execute binary file (126). No external command can run at all on macOS — same on the released niubash v1.5.0 tarball.

  • Add executor::path::is_macho_image() matching thin Mach-O (MH_MAGIC/MH_CIGAM, 32- and 64-bit, both byte orders) and fat/universal (FAT_MAGIC/FAT_CIGAM, both byte orders); cfg-gated, returns false off-macOS.
  • Admit it in program_execs_directly() and is_enoexec_candidate().

GNU reference

execute_cmd.c:6126 shell_execve() calls execve(2) directly and only falls back to script interpretation on ENOEXEC — the kernel, not the shell, decides what's loadable. On macOS that's Mach-O, not ELF.

Verification (macOS 26.6.2 arm64, niubash built against this branch)

  • GNU Bash upstream gate (scripts/run-bash-upstream-with-niubash.sh): 0/86 → 30/86 (remainder: pre-existing semantic diffs + macOS harness gaps like missing recho//dev/tty)
  • cargo test --workspace failing targets: ~30 → 15 (remainder: test portability, wave-2 unix signal items — same classes as CI)
  • PTY gate: 7/8 → 8/8
  • niu -c external commands, pipelines, PATH lookup, absolute paths all exec; ./binary as a script operand still correctly refused with 126
  • cargo fmt --check -p rubash clean

Draft per workflow (first commit). Written by Devin

@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】
PR #3586 第1轮 — 建议:PASS
head: fba6f70 base(merge-base): 96802ca 文件: 2 个 +39/-4
关联 issue: #3585(状态:open,我核过的时间:2026-11-15 本机)

机制核查:

  • is_enoexec_candidate(external_inner.rs:6135 域)新增早退:#!/ELF 之后,4 字节头命中 Mach-O 魔数 → 非 ENOEXEC 候选。GNU 依据:execute_cmd.c:6126 shell_execve() 用本地内核 execve;macOS 内核加载 Mach-O(thin+fat),不会产生 ENOEXEC 脚本回退——把 Mach-O 当候选会在 macOS 上误报 "cannot execute binary file"。方向与 GNU 一致。
  • 魔数表正确:MH_MAGIC/CIGAM 32+64 位(feedface/feedfacf 双字节序)+ FAT_MAGIC/CIGAM(cafebabe/cafebabf 双序),共 8 项,与 <mach-o/loader.h>+<mach-o/fat.h> 一致。
  • 跨平台零风险:is_macho_image 非 macOS 目标恒 false(cfg 桩),Windows/Linux 行为逐字节不变。
  • 无关改动:无;无桥接层/半成品修法。

腿(逐项):

  • 钉自跑:PR 无新测试文件(macOS 专属路径,作者未带钉——观察项,非 F 点:该行为只能在 macOS runner 上钉)。
  • fail-first / lib 双边(本地):无钉可 fail-first;本地 Windows 构建不覆盖 cfg(macos) 代码路径,VOID。
  • lib 双边(经 CI 代跑):head Rust tests (macOS) lib run = 1243 passed / 1 failed = {issue2875 基线};Windows Rust tests 同理 {issue2875}。差集 {∅}。
  • -D warnings:cfg 桩签名完整(_prefix 命名正确),编译期无新警告面;CI 各腿编译已过。
  • 卫生:rustfmt 1.10.0 直跑两文件——external_inner.rs 0 diff;path.rs 有一处 import 排序 diff(line 2803 windows cfg 块),在 merge-base 96802ca 上同位置同 diff → 基线级 rustfmt 版本漂移,非本 PR。CI utf8/marker 检查在其 job 内已过。
  • CI:三红任务(GNU goldens / Rust tests / Rust tests macOS)红名全部 = {issue2875_input_procsub_child_runs_as_rubash_shell} —— 基线,非本 PR。
  • GNU 对照:macOS 专属,本机(Windows+WSL Linux)无法复现 Mach-O exec 路径;语义经 GNU 源核对(上)。

F 点:无。
续作记录:无。
未跑/VOID 的腿:本地 lib 双边与 -D warnings 全量(cfg(macos) 在本机不编译;CI 已代证);macOS 实机 Mach-O exec 对照(无 macOS 环境)。

@caomengxuan666

Copy link
Copy Markdown
Member Author

【队长终裁】PASS

核过审计报告(6885829768)+ CI 全表。机制与 GNU 一致(shell_execve 用本地内核 execve;macOS 内核加载 Mach-O 不会走 ENOEXEC 脚本回退);魔数表 8 项与 <mach-o/loader.h>+<mach-o/fat.h> 一致;cfg(macos) 桩使非 macOS 目标行为逐字节不变。CI macOS lib 腿红集 {issue2875},与 Windows 腿差集 ∅;三红任务红名全为 issue2875 基线。无新钉——macOS 专属路径在 Windows/Linux 上无法钉,属观察项非 F 点;后续可在 macOS 腿上补钉,不拦合入。CONFLICTING——合入轮先 update-branch + 合并形重验(钉面为空,以 lib 双边 + -D warnings 为验收)。

@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 15, 2026 02:05
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-MERGE】批次汇总

M4 分工批次(Wave 1 草稿 7 个 rubash + 2 个 niubash)处理完毕:

全部 head oid 与最新【WIN-AGENT-AUDIT】记录比对一致;7 个 rubash draft 均已 gh pr ready 转正;全部 squash 合并未删分支。

@caomengxuan666
caomengxuan666 merged commit af4e689 into master Nov 15, 2026
8 of 11 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 16, 2026
…(Refs #3605) (#3671)

* fix(builtins): wrap inline printf precision digits into the libc int field (Refs #3605)

GNU hands `%.<digits>' verbatim to the host libc's printf(3) via the
PF() macro (builtins/printf.def:152-175). On BSD/Darwin libc the
precision digit run accumulates into an int field (xprintf.c:558-559),
wrapping mod 2^32: `printf "%.9223372036854775825s" XY' prints XY, and
the vendored printf.right golden expects exactly that. The previous
EOVERFLOW+PRETURN model fits an inline field *width* (xprintf.c:604-625
feeds an INT_MAX-checked pad) but not precision.

Precision overflow now wraps the digit run two's-complement into i32:
positive results apply as precision, negative results are "as if it
were missing". Inline width overflow keeps the EOVERFLOW error, and
the printstr specs (b/q/Q/T/S/C/wide) keep GNU decodeint() erange
semantics unchanged.

Verified on macOS arm64 vs GNU bash 5.3.20: %.9223372036854775825s/d
render as %.17s/%.17d, %.4294967295s prints the full string, and
niubash run-printf is PASS (the only remaining diff is the Mach-O
exec gap covered by PR #3586). New unit tests pin the wrap, the
wrapped-negative case, and the width EOVERFLOW boundary.

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

* test(builtins): pin inline printf width/precision overflow EOVERFLOW (Refs #3605)

Reverts the i32-wrap model for inline precision digits: on the glibc
oracle (WSL GNU bash 5.3.20) both width and precision digit runs outside
i32 fail the directive with EOVERFLOW, matching printf.def's PF() ->
host libc printf(3) path. The BSD/Darwin wrap model is unverifiable here
and is not the project oracle, so the product change is dropped; what
remains is regression coverage asserting the verified verdict.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
caomengxuan666 added a commit that referenced this pull request Nov 17, 2026
* fix(builtins): wrap inline printf precision digits into the libc int field (Refs #3605)

GNU hands `%.<digits>' verbatim to the host libc's printf(3) via the
PF() macro (builtins/printf.def:152-175). On BSD/Darwin libc the
precision digit run accumulates into an int field (xprintf.c:558-559),
wrapping mod 2^32: `printf "%.9223372036854775825s" XY' prints XY, and
the vendored printf.right golden expects exactly that. The previous
EOVERFLOW+PRETURN model fits an inline field *width* (xprintf.c:604-625
feeds an INT_MAX-checked pad) but not precision.

Precision overflow now wraps the digit run two's-complement into i32:
positive results apply as precision, negative results are "as if it
were missing". Inline width overflow keeps the EOVERFLOW error, and
the printstr specs (b/q/Q/T/S/C/wide) keep GNU decodeint() erange
semantics unchanged.

Verified on macOS arm64 vs GNU bash 5.3.20: %.9223372036854775825s/d
render as %.17s/%.17d, %.4294967295s prints the full string, and
niubash run-printf is PASS (the only remaining diff is the Mach-O
exec gap covered by PR #3586). New unit tests pin the wrap, the
wrapped-negative case, and the width EOVERFLOW boundary.

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

* api: Session::run admits live expand_aliases with an empty table (Refs #3605)

rubash#3605 class B (run-alias tail): Session::run's grouped-driver
admission required a NON-EMPTY alias table on top of the live
expand_aliases flag, so a submission like '-o posix -c' text — where
shell.c:1853 init_noninteractive seeds expand_aliases before the
reader starts but no aliases are defined yet — took the whole-source
route. That route's word-level alias fallback expands reserved words
without parse.y:5751-5769's posix ordering (posix checks reserved
words BEFORE alias expansion), so 'alias for=echo\nal for foo in v\ndo
...' expanded 'for' to 'echo' and died on a syntax error where GNU
runs the loop and prints 'foo=v bar='.

GNU's reader is incremental (evalstring.c parse_and_execute) and the
alias gate is consulted on every token (parse.y:5759), so aliases
defined by an earlier line must be live for the lines read after it —
the same admission main.rs computes as alias_live_at_start for the -c
route (rubash#414). Drop the non-empty-table term: the flag is the
whole admission, matching the bin-side answer.

Verified: niu -o posix -c 'alias for=echo; al for foo in v; do echo
foo=$foo bar=$bar; done' now prints 'foo=v bar=' (GNU bash 5.3.20
identical); niubash gate run-alias diff cleared (PASS). New
session_api test pins the posix run over this contract; the
pre-existing macOS-only build_run_and_state_views_round_trip /var ->
/private/var canonicalization flake is unchanged.

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

* test(api): spell the posix-alias pin redirect with a relative path (Refs #3605)

---------

Co-authored-by: Devin AI <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.

1 participant