feat(codemode): fail runaway recursion with a RangeError at 10000 nested calls (#48891)

This commit is contained in:
Aiden Cline
2026-09-13 21:04:36 -05:00
committed by GitHub
Unverified
parent 476432de1e
commit 48875190ef
5 changed files with 102 additions and 38 deletions
+4
View File
@@ -100,6 +100,10 @@ ultimate source of truth. Upstream test262 files run verbatim from `test/test262
- [x] Function declarations, function expressions, and arrow functions.
- [x] Synchronous and `async` functions.
- [x] Closures, recursion, default parameters, rest parameters, and destructured parameters.
- [x] A call depth limit of 10000: deeper nesting throws a catchable `RangeError: Maximum call stack size exceeded`
at the overflowing call instead of running until the timeout. Callbacks invoked by built-ins count below the
call that invoked the built-in, and a resumed `await` starts from depth 0 as in JS, so long async chains such
as recursive pagination are unaffected.
- [x] Expression and block function bodies.
- [x] User callbacks for the supported Array, Map, Set, URLSearchParams, sort, string-replacement, and `Array.from`
mapper APIs, with one shared acceptance rule everywhere including promise reactions.
+4 -1
View File
@@ -7,7 +7,10 @@ import type { ProgramError } from "./objects.js"
/** Any parsed node; the interpreter narrows on `type` and reads `loc` for diagnostics. */
export type AstNode = Node
export const CallSite = Context.Reference<AstNode | undefined>("codemode/CallSite", { defaultValue: () => undefined })
/** The program call a built-in is running under: where to locate failures born inside it, and how deep the stack is there. */
export const CallSite = Context.Reference<{ readonly node?: AstNode; readonly depth: number }>("codemode/CallSite", {
defaultValue: () => ({ depth: 0 }),
})
export type Binding = {
mutable: boolean
@@ -52,7 +52,7 @@ export class PromiseRuntime<R> {
return Effect.flatMap(CallSite, (site) => {
// Allocate before forking so reruns get distinct IDs and diagnostics retain creation order.
const id = this.nextID++
const body = Effect.catchDefect(effect, (defect) => Effect.die(locate(defect, site)))
const body = Effect.catchDefect(effect, (defect) => Effect.die(locate(defect, site.node)))
return Effect.map(Effect.forkIn(body, this.scope, { startImmediately: true }), (fiber) => {
const promise = new ProgramPromise(this.proto, fiber)
this.active.add(promise)
+48 -36
View File
@@ -280,6 +280,8 @@ export class Runtime<R> {
}
}
const MAX_CALL_DEPTH = 10_000
/** One activation: the top-level program or a single function call, evaluating against its own scope chain. */
class Frame<R> {
private generatorState?: GeneratorState
@@ -288,6 +290,8 @@ class Frame<R> {
constructor(
private readonly runtime: Runtime<R>,
private scopes: ScopeStack,
/** Nested call depth; resets when an await resumes, since the continuation runs from the job queue. */
private depth = 0,
) {}
run(program: Program): Effect.Effect<unknown, unknown, R> {
@@ -691,7 +695,10 @@ class Frame<R> {
private awaitValue(value: unknown): Effect.Effect<unknown, unknown, R> {
return Effect.flatMap(resolvePromise(this.runtime.runner, this.runtime.promises, value), (promise) =>
this.settlePromise(promise),
Effect.ensuring(
this.settlePromise(promise),
Effect.sync(() => (this.depth = 0)),
),
)
}
@@ -1569,7 +1576,7 @@ class Frame<R> {
}
return yield* self.createToolCallPromise(callable.path, args)
}
if (callable instanceof ProgramFunction) return yield* self.invokeFunction(callable, args)
if (callable instanceof ProgramFunction) return yield* self.invokeFunction(callable, args, node)
if (callable instanceof NativeFunction) {
return yield* self.native(() => (callable as NativeFunction<R>).call(thisValue, args), node)
}
@@ -1582,7 +1589,7 @@ class Frame<R> {
return Effect.provideService(
Effect.catchDefect(Effect.suspend(body), (defect) => Effect.die(locate(defect, node))),
CallSite,
node,
{ node, depth: this.depth },
)
}
@@ -1610,45 +1617,50 @@ class Frame<R> {
})
}
invokeFunction(fn: ProgramFunction, args: Array<unknown>): Effect.Effect<unknown, unknown, R> {
// A callback invoked by a built-in runs below the call that invoked the built-in, so the deeper of the two counts.
invokeFunction(fn: ProgramFunction, args: Array<unknown>, node?: AstNode): Effect.Effect<unknown, unknown, R> {
const self = this
const invocation = new Frame(this.runtime, new ScopeStack([...fn.capturedScopes, new Map()]))
const run = Effect.gen(function* () {
// Seed all parameters first so defaults cannot fall through to same-named outer bindings.
const paramScope = invocation.scopes.current()
for (const parameter of fn.parameters) {
for (const name of collectPatternNames(parameter)) {
paramScope.set(name, { mutable: true, value: undefined, initialized: false })
return Effect.flatMap(CallSite, (site) => {
const depth = Math.max(self.depth, site.depth) + 1
if (depth > MAX_CALL_DEPTH) throw rangeError("Maximum call stack size exceeded", node)
const invocation = new Frame(this.runtime, new ScopeStack([...fn.capturedScopes, new Map()]), depth)
const run = Effect.gen(function* () {
// Seed all parameters first so defaults cannot fall through to same-named outer bindings.
const paramScope = invocation.scopes.current()
for (const parameter of fn.parameters) {
for (const name of collectPatternNames(parameter)) {
paramScope.set(name, { mutable: true, value: undefined, initialized: false })
}
}
}
for (const [index, parameter] of fn.parameters.entries()) {
if (parameter.type === "RestElement") {
yield* invocation.declarePattern(
parameter.argument,
new ProgramArray(self.runtime.prototypes.Array, args.slice(index)),
true,
parameter,
true,
)
break
for (const [index, parameter] of fn.parameters.entries()) {
if (parameter.type === "RestElement") {
yield* invocation.declarePattern(
parameter.argument,
new ProgramArray(self.runtime.prototypes.Array, args.slice(index)),
true,
parameter,
true,
)
break
}
yield* invocation.declarePattern(parameter, args[index], true, parameter, true)
}
yield* invocation.declarePattern(parameter, args[index], true, parameter, true)
}
if (fn.body.type === "BlockStatement") {
invocation.scopes.push()
invocation.hoistVars(fn.body.body, paramScope)
const result = yield* invocation.evaluateStatement(fn.body)
return result.kind === "return" ? result.value : undefined
}
if (fn.body.type === "BlockStatement") {
invocation.scopes.push()
invocation.hoistVars(fn.body.body, paramScope)
const result = yield* invocation.evaluateStatement(fn.body)
return result.kind === "return" ? result.value : undefined
}
return yield* invocation.evaluateExpression(fn.body)
return yield* invocation.evaluateExpression(fn.body)
})
if (fn.generator) return Effect.succeed(this.createGenerator(invocation, run, fn.async))
if (!fn.async) return run
return this.runtime.promises.createWithSelf((self) =>
Effect.flatMap(run, (value) => resolvePromiseValue(invocation.runtime.runner, value, self)),
)
})
if (fn.generator) return Effect.succeed(this.createGenerator(invocation, run, fn.async))
if (!fn.async) return run
return this.runtime.promises.createWithSelf((self) =>
Effect.flatMap(run, (value) => resolvePromiseValue(invocation.runtime.runner, value, self)),
)
}
private createGenerator(
+45
View File
@@ -131,3 +131,48 @@ describe("host errors escaping built-ins", () => {
expect(failure.message).toBe("TypeError: Iterator next must be a function. (line 1, col 8)")
})
})
describe("call depth", () => {
test("runaway recursion fails fast with a catchable RangeError", async () => {
const started = Date.now()
expect(
await value(`
const f = (n) => f(n + 1)
try { f(0) } catch (e) { return [e.name, e instanceof RangeError, e.message] }
`),
).toEqual(["RangeError", true, "Maximum call stack size exceeded"])
expect(Date.now() - started).toBeLessThan(2000)
})
test("uncaught overflow reports the call that overflowed", async () => {
const failure = await error(`const f = (n) => f(n + 1); return f(0)`)
expect(failure.kind).toBe("ExecutionFailure")
expect(failure.message).toBe("RangeError: Maximum call stack size exceeded (line 1, col 18)")
})
test("the limit is 10000 nested calls", async () => {
expect(await value(`let depth = 0; const f = () => { depth++; f() }; try { f() } catch { return depth }`)).toBe(
10000,
)
expect(await value(`const f = (n) => (n === 0 ? 0 : 1 + f(n - 1)); return f(9000)`)).toBe(9000)
})
test("recursion through a built-in callback counts", async () => {
const failure = await error(`const f = (n) => [n].map((x) => f(x + 1)); return f(0)`)
expect(failure.message).toStartWith("RangeError: Maximum call stack size exceeded")
})
test("an await resets the depth, so long async chains are fine", async () => {
expect(
await value(`
const page = async (n) => { await null; return n === 0 ? "done" : page(n - 1) }
return await page(3000)
`),
).toBe("done")
})
test("an async function that recurses before its first await overflows like JS", async () => {
const failure = await error(`const f = async (n) => f(n + 1); return await f(0)`)
expect(failure.message).toStartWith("RangeError: Maximum call stack size exceeded")
})
})