nodejs / nodejs/node

Large argv leaves V8 no stack on macOS: `RangeError` at bootstrap, before the first JS frame

未关闭
#65,936 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

v8 engine
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v24.11.1, V8 13.6.233.10-node.28

Platform

Darwin 25.6.0 Darwin Kernel Version 25.6.0 arm64, Apple M2, ARG_MAX 1048576, ulimit -s 8176 KB

Subsystem

v8

What steps will reproduce the bug?

One line, with an empty environment so the block under test is argv alone:

env -i "$(command -v node)" /dev/null "$(node -e "process.stdout.write('a'.repeat(960000))")"

The script is /dev/null, so nothing user-written runs. node -e '' with the same argument dies
identically, which places the fault in the runtime's startup rather than in any code being loaded.

The threshold is sharp. Bisected on the machine above:

Single argument Result
958,979 bytes exit 0
958,980 bytes RangeError: Maximum call stack size exceeded, exit 7
1,000,000 bytes RangeError: Maximum call stack size exceeded, exit 7

The crash band runs from there to ARG_MAX, 89,596 bytes wide. Above ARG_MAX the shell refuses
the exec with argument list too long and node never starts, which is a different and correctly
reported failure.

Passing the same block as an environment variable behind a short command line dies identically:
env -i "BLOB=$big" node /dev/null exits 7 with an empty command line.

--stack-size removes it, which is the confirmation that the stack is what ran out:

--stack-size=3072   argv   950000 bytes -> exit 0
--stack-size=3072   argv   960000 bytes -> exit 0
--stack-size=3072   argv  1000000 bytes -> exit 0

The environment cannot carry the flag as a workaround, because node refuses it there:

$ env -i NODE_OPTIONS=--stack-size=3072 node -e 'console.log("ran")'
node: --stack-size= is not allowed in NODE_OPTIONS
exit 9

How often does it reproduce? Is there a required condition?

Every time, on macOS, for any argv plus environment block inside the band. Raising the stack rlimit
does not move the threshold: a 1,000,000-byte argument exits 7 at both ulimit -s 8176 and
ulimit -s 65536, because the geometry is measured from the top of the stack rather than from its
size.

What is the expected behavior?

Either of these would be an improvement on the current outcome, and the first is the one worth
having:

  1. The stack limit accounts for the argv and environment block the kernel has already placed at the
    top of the main thread's stack, so a large command line costs the isolate nothing.
  2. Failing that, node reports the cause. A block that leaves the isolate with no stack should
    produce a diagnostic naming the command-line size, not a RangeError at
    <anonymous_script>:0.

What do you see instead?

RangeError: Maximum call stack size exceeded
    at <anonymous_script>:0

Exit code 7, before the first line of user JavaScript. A caller sees a stack-overflow error for a
program that never ran.

Additional information

The mechanism, from V8's own source, read on v8/v8 main on 2026-09-09. On macOS the kernel
places argv and the environment at the high end of the main thread's stack, and
pthread_get_stackaddr_np returns that same high address:

src/base/platform/platform-darwin.cc:139

Stack::StackSlot Stack::ObtainCurrentThreadStackStart() {
  return pthread_get_stackaddr_np(pthread_self());
}

The initial JS stack limit is then set that far below the stack start, with no allowance for what
the kernel already wrote there:

src/execution/stack-guard.cc:247

void StackGuard::ThreadLocal::Initialize(Isolate* isolate,
                                         const ExecutionAccess& lock) {
  const uintptr_t kLimitSize = v8_flags.stack_size * KB;
  DCHECK_GT(base::Stack::GetStackStart(), kLimitSize);
  uintptr_t limit = base::Stack::GetStackStart() - kLimitSize;

v8_flags.stack_size defaults to V8_DEFAULT_STACK_SIZE_KB, which is 984 on this target
(src/common/globals.h, the #else branch: "Slightly less than 1MB, since Windows' default stack
size for the main execution thread is 1MB"). 984 KiB is 1,007,616 bytes, and the measured ceiling
is 958,980, so about 47.5 KiB of that budget is gone to node's own bootstrap frames before the
first stack check runs. The rest of the budget is consumed by the argv block sitting above the
stack pointer but below GetStackStart().

The DCHECK_GT on the line above compares the stack start against the limit size in absolute
terms, so it does not catch this: the stack start is a large address and passes the check while the
usable region beneath it is already gone.

Other platforms do not reach the band, for unrelated reasons rather than because they handle it:

  • Linux (Debian 12 and Alpine, measured 2026-09-07) refuses any single argument over 131,072 bytes
    with E2BIG at exec, so the per-argument path fails before node starts. The total is larger:
    23 arguments of 90,000 characters, 2,070,023 bytes, answered normally on both images.
  • Windows caps a whole command line at 32,767 characters, far below the band.
Prior art

Searched on 2026-09-09 across nodejs/node issues, nodejs/help issues, and the V8 tracker, on
the symptom terms (argv, ARG_MAX, argument list too long, exit status 7, stack base,
anonymous_script) and the mechanism terms. No open or closed report describes this.

The nearest is nodejs/node#28319, "node js script
exits when input is large". It reports the same visible symptom, exit status 7 with the script
never starting, but on Linux at 170,000 bytes, and it was closed in 2019 as a kernel limitation:

It's a kernel limitation and as such not under our control. I'll close this out.

That answer is right for the Linux E2BIG path and does not cover this one. Here the exec
succeeds, node starts, and the process dies inside V8's own bootstrap on a block the kernel
accepted.

The V8 tracker at issues.chromium.org requires sign-in to search, so that half of the search was
done through public web search rather than against the tracker directly. Worth re-running from a
signed-in session before filing.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用提供的 macOS argv 命令重现该失败,然后阅读 src/base/platform/platform-darwin.cc:139 和 src/execution/stack-guard.cc:247,以及 src/common/globals.h 中的 V8_DEFAULT_STACK_SIZE_KB 定义。跟踪初始栈限制与 kernel 提供的 argv 和环境块之间的关系。当大 argv 情况在 bootstrap 期间不再失败,或报告命令行大小而不是误导性的 RangeError 时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
cpp, macos, node.js
领域
backend, operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
活跃
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。