Skip to content

executor: virtualize ulimit inside in-process fork regions (run-vredir) - #3688

Merged
caomengxuan666 merged 2 commits into
masterfrom
devin/vredir-ulimit-virtualization
Nov 16, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
devin/vredir-ulimit-virtualization

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

ulimit -n 6 inside a ( )/$( ) layer, a pipeline element, or an in-process ${THIS_SH}/script child lowered the SHARED process's real RLIMIT_NOFILE — after the region returned, every {fd} allocation failed F_DUPFD EINVAL (base 10 >= limit 6), which is the whole run-vredir cascade on the macOS gate. GNU runs those bodies in real children whose setrlimit results die with the process (execute_cmd.c:1576 execute_in_subshell -> builtins/ulimit.def:632 set_limit). The existing snapshot/apply_process_rlimits save-restore cannot fix this: an unprivileged hard-limit drop is irreversible.

This changes the model to virtualization instead of restore:

  • New Executor::ulimit_virtualized() — !owns_process_image || subshell_depth > 0 || emulated_child_depth > 0 (new Cell bumped at the pipeline-stage bracket in pipeline_exec.rs and the in-process script-child run in external_finish.rs; fork-copy comsubs are covered by !owns_process_image).
  • Virtualized plat::set records the effective pair in __RUBASH_ULIMIT_<OPT> env cells and applies only the SOFT side to the real setrlimit — always restorable by the region's rlimit restore, and the kernel still enforces the soft side so an EMFILE consumer sees the same errno as under GNU. Virtualized plat::get reads cell-or-real. This is the same contract the Windows emulation already runs under.
  • Cells are bracketed per region via emulated_cells_snapshot/restore (pipeline stage, shared-executor comsub interior), so a nested region keeps an enclosing region's cells instead of leaking them; the script child's wholesale env restore drops them for free.
  • The REDIR_VARASSIGN alloc failure is now emitted as GNU's two reporters emit it: the sys_error line goes to fd 2 immediately with the script-name-only prolog (error.c error_prolog(0) — ./vredir6.sub: redirection error: ..., no line N:), and only the file_error line returns for the caller's line-stamped prefix.

run-vredir on the niubash gate drops to a single residual line — ./vredir8.sub: line 30: /dev/tty: — which is environment-bound: the shipped vredir.right golden was generated with a controlling terminal; the gate environment has none, and GNU bash on the same box fails that open too (with the Darwin ENXIO wording).

Pins: tests/issue3605_ulimit_region_virtualization.rs — ( ) and pipeline-element hard drops die with the region, the region still sees its own effective pair, and a plain function body's ulimit still reaches the shell (GNU parity: functions are not forked).

Refs #3605

rubash#3605 / run-vredir: `ulimit -n 6` inside a `( )`/`$( )` layer, a
pipeline element, or an in-process `${THIS_SH}`/script child lowered
the SHARED process's real RLIMIT_NOFILE, so every later `{fd}`
allocation saw F_DUPFD EINVAL (base 10 >= limit 6) and the suite
cascaded through vredir6/7/8. GNU runs all of those bodies in real
children whose setrlimit results die with the process
(execute_cmd.c:1576 execute_in_subshell -> builtins/ulimit.def:632
set_limit -> setrlimit); rubash's save/restore cannot help because a
hard drop is irreversible without CAP_SYS_RESOURCE.

Model the GNU boundary instead: new Executor::ulimit_virtualized()
(`!owns_process_image || subshell_depth>0 || emulated_child_depth>0`)
gates a virtualized plat::set that applies only the SOFT side to the
kernel (always restorable, keeps kernel EMFILE enforcement honest)
and records the effective pair in `__RUBASH_ULIMIT_*` env cells — the
same contract the Windows platform layer already runs under.
`emulated_child_depth` is bumped at the pipeline-stage bracket
(pipeline_exec.rs), the in-process script-child run
(external_finish.rs); fork-copy comsubs are covered by
!owns_process_image. Cells are restored per-region
(emulated_cells_snapshot/restore) so nested regions keep their own
values; the script child's env restore drops them wholesale.

Also split the REDIR_VARASSIGN alloc failure into GNU's two
reporters: the `sys_error` line now goes to fd 2 immediately with the
script-name-only prolog (error.c error_prolog(0), no line segment —
`./vredir6.sub: redirection error: ...`), while only the file_error
line returns for the caller's `line N:` prefix.

Pins: tests/issue3605_ulimit_region_virtualization.rs — subshell /
pipeline-element hard drops die with the region, the region still
sees its own effective pair, and a plain function body's ulimit still
reaches the shell (GNU parity — functions are not forked).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 16, 2026 08:46
@caomengxuan666
caomengxuan666 merged commit 7a01b9d into master Nov 16, 2026
8 of 11 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

【WIN-AGENT-AUDIT】PR #3688 第1轮 — 建议:PASS(已合并 squash 7a01b9d)

head(合并时): 264f048 base(merge-base): 96802ca 文件: 9 个 +333/-63
关联 issue: #3605(状态:open,我核过的时间:2026-11-16 本地)— 注意:#3605 是 macOS gate 伞目录("48/86 remaining divergences"),PR 只 Refs 不 Fixes,按口径未关单,仅记录关联。

机制核查:

  • GNU 语义核实:execute_cmd.c:1576 execute_in_subshell(子 shell 体内 setrlimit 私有于 fork 映像)与 builtins/ulimit.def:632 set_limit 的引用属实。虚拟化设计(env-cell 记账 + 只把 SOFT 侧落真实 setrlimit + 硬降只进 cell)与 GNU fork 语义等价:子进程 setrlimit 死亡不外泄,父进程不受影响;软限仍由内核强制,EMFILE errno 与 GNU 一致。非翻译层/半成品。
  • 无关改动:无。trap_exec.rs 的 fd_dup_error_payload→report_fd_dup_alloc_error 拆分属同 issue 面(run-vredir 诊断形状)。

腿(逐项):

  • 钉自跑:issue3605_ulimit_region_virtualization 4 passed / 0 failed(head)。
  • fail-first:Windows base 4/4 绿(平台层恒仿真,泄漏面在 Windows 不存在)——但已在 WSL Linux 以非特权用户 testu(uid 1000) 做真实 unix fail-first:base 二进制 h1 ( ulimit -n 6 ) 泄漏 → ulimit -n 报 6、exec {v} EINVAL rc=1;h2 管道元素同泄漏;head 二进制 h1/h2 报 1024 + fd=10,与 GNU bash 5.3.20 同用户输出逐字节一致。WSL root 下 base 也绿(CAP_SYS_RESOURCE 可逆,须用非特权用户才暴露)。
  • GNU 对照(WSL /usr/local/bin/bash 5.3.20,testu):h1 1024/fd=10;h3 6 + stderr 两行 h3.sh: redirection error: cannot duplicate fd: Invalid argument(无 line 段)+ h3.sh: line 1: /dev/null: Invalid argument;h4 7。head 输出逐字节一致;base 在 h3 第 1 行误带 line 1:(PR 修复的诊断形状,已实测)。
  • lib 双边:head {issue2084_session_end_restores_host_process_state} / base {同} / 差集 {}(env 并行 flake,单跑两侧均过,非本 PR 账)。
  • -D warnings:cargo check --lib --bins 0 警告;钉 stem --no-run 0 警告。--all-targets 基线红 issue1021_exec_procsub_regressions.rs unused-import(merge-base 既有,非本 PR)。
  • 卫生:rustfmt 初检发现 PR 新增代码 3 处 fmt 违规(ulimit.rs / trap_exec.rs / 钉文件),已在分支上补 264f0484 style(ulimit): rustfmt ...(WSL git 提交,因 Windows cargo fmt 受 32K 命令行上限 os error 206 全仓不可跑;WSL cargo fmt --check 全仓 exit 0);marker-lint OK;utf8-boundary clean。
  • CI(head 6bc7584 + fmt-only delta):3 红任务 Rust tests / macOS / goldens 唯一红格 = issue2875_input_procsub_child_runs_as_rubash_shell 基线;其余全绿。

F 点:无。

续作记录:

未跑/VOID 的腿:无(unix fail-first 已以 WSL testu 补齐,非 VOID)。

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