From 5cbf31e2437d4efb4c1b995e8b04035fd8d3ef7b Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 4 Oct 2026 13:23:10 -0700 Subject: [PATCH 1/6] fix(jsc): retain syntax-selected stack source positions Keep call, construct, property-read and async-resume stack positions separate from exception expression ranges. Preserve exact instruction and resume-label mappings through bytecode rewriting, optimization and owned cache decoding. Extend the prior native-qualified call-position implementation with property lexer tokens and Node 24 regressions for builtin constructors and returning arrows. Keep proper-tail-call behavior unchanged; the paired Bun adapter preserves constructor syntax and invalidates its runtime transpiler cache. The paired integration builds on robobun's upstream source-position work in oven-sh/bun#35179, oven-sh/bun#37396, and oven-sh/bun#41580. Publication stays with the OpenClaw artifact coordinator. --- .../patches/009-stack-positions.patch | 504 ++++++++++++++++++ .github/openclaw/qualify.sh | 2 + .github/workflows/openclaw-checks.yml | 12 + JSTests/stress/callsite-async-position.mjs | 12 + JSTests/stress/callsite-syntax-positions.js | 116 ++++ JSTests/stress/stack-position-regressions.js | 104 ++++ OPENCLAW.md | 8 + .../bytecode/BytecodeOptimizer.cpp | 12 + .../bytecode/BytecodeRewriter.h | 11 + Source/JavaScriptCore/bytecode/CodeBlock.cpp | 8 + Source/JavaScriptCore/bytecode/CodeBlock.h | 1 + .../bytecode/UnlinkedCodeBlock.cpp | 15 + .../bytecode/UnlinkedCodeBlock.h | 5 + .../bytecode/UnlinkedCodeBlockGenerator.cpp | 16 + .../bytecode/UnlinkedCodeBlockGenerator.h | 12 + .../bytecompiler/BytecodeGenerator.cpp | 63 ++- .../bytecompiler/BytecodeGenerator.h | 33 +- .../bytecompiler/NodesCodegen.cpp | 74 ++- .../interpreter/Interpreter.cpp | 12 +- Source/JavaScriptCore/parser/ASTBuilder.h | 26 + Source/JavaScriptCore/parser/Nodes.h | 8 + Source/JavaScriptCore/parser/Parser.cpp | 7 + Source/JavaScriptCore/parser/SyntaxChecker.h | 3 + Source/JavaScriptCore/runtime/CachedTypes.cpp | 13 +- Source/JavaScriptCore/runtime/StackFrame.cpp | 2 + 25 files changed, 1006 insertions(+), 73 deletions(-) create mode 100644 .github/openclaw/qualification/patches/009-stack-positions.patch create mode 100644 JSTests/stress/callsite-async-position.mjs create mode 100644 JSTests/stress/callsite-syntax-positions.js create mode 100644 JSTests/stress/stack-position-regressions.js diff --git a/.github/openclaw/qualification/patches/009-stack-positions.patch b/.github/openclaw/qualification/patches/009-stack-positions.patch new file mode 100644 index 0000000000000..e29e8583e5e4a --- /dev/null +++ b/.github/openclaw/qualification/patches/009-stack-positions.patch @@ -0,0 +1,504 @@ +diff --git a/CHANGELOG.md b/CHANGELOG.md +index e6e2a21a..1288eaaf 100644 +--- a/CHANGELOG.md ++++ b/CHANGELOG.md +@@ -2,6 +2,8 @@ + + ## Unreleased + ++- Preserve built-in error constructor syntax and returning arrow frames, and use JSC syntax-selected call and property-read stack positions. Continues the source-position fixes from [oven-sh/bun#35179](https://github.com/oven-sh/bun/pull/35179), [oven-sh/bun#37396](https://github.com/oven-sh/bun/pull/37396), and [oven-sh/bun#41580](https://github.com/oven-sh/bun/pull/41580). Thanks @robobun! ++ + - Align WebKit and VM source-position assertions with Node's constructor locations and select these regressions for native stack-formatting changes in both fork CI lanes. + + - Preserve the native-context count in the fast `v8.getHeapStatistics()` adapter and avoid appending a second strict code-generation flag to inherited worker arguments. +diff --git a/docs/runtime/nodejs-compat.mdx b/docs/runtime/nodejs-compat.mdx +index 0274c5b4..106fbfc5 100644 +--- a/docs/runtime/nodejs-compat.mdx ++++ b/docs/runtime/nodejs-compat.mdx +@@ -19,7 +19,7 @@ The OpenClaw fork reports missing runtime imports without downloading npm packag + + `Error.prepareStackTrace` supports deletion, descriptor restoration, and accessor replacement. Nested stack formatting and throwing formatters leave later captures usable. + +-`CallSite.getLineNumber()` and `getColumnNumber()` return one-based positions, or `null` when a frame has no source position. Stack strings include column 1 and use the same constructor positions as CallSites, pointing to the `new` keyword, including across line breaks and in `node:vm` scripts. Some expression positions still differ from Node.js because JavaScriptCore and Bun's transpiler select different syntax locations. ++`CallSite.getLineNumber()` and `getColumnNumber()` return one-based positions, or `null` when a frame has no source position. Stack strings include column 1 and use the same constructor positions as CallSites, pointing to the `new` keyword, including across line breaks and in `node:vm` scripts. The matching OpenClaw engine supplies syntax-selected positions for calls, constructors, property reads, and async continuations. Built-in error constructors retain `new`, including in returning arrows. File-name and eval-origin formatting still follow Bun's existing conventions. + + ### [`node:assert`](https://nodejs.org/api/assert.html) + +diff --git a/src/ast/known_global.rs b/src/ast/known_global.rs +index 15c439d7..a69b7697 100644 +--- a/src/ast/known_global.rs ++++ b/src/ast/known_global.rs +@@ -103,7 +103,7 @@ impl KnownGlobal { + let constructor = lookup(original_name)?; + + match constructor { +- // Error constructors can be called without 'new' with identical behavior ++ // Removing `new` changes stack positions and makes returned errors eligible for tail calls. + KnownGlobal::Error + | KnownGlobal::TypeError + | KnownGlobal::SyntaxError +@@ -111,10 +111,7 @@ impl KnownGlobal { + | KnownGlobal::ReferenceError + | KnownGlobal::EvalError + | KnownGlobal::URIError +- | KnownGlobal::AggregateError => { +- // Convert `new Error(...)` to `Error(...)` to save bytes +- Some(Self::call_from_new(e, loc)) +- } ++ | KnownGlobal::AggregateError => None, + + KnownGlobal::Object => { + let n = e.args.len_u32(); +diff --git a/src/jsc/RuntimeTranspilerCache.rs b/src/jsc/RuntimeTranspilerCache.rs +index 1142ca3b..c7e0fa20 100644 +--- a/src/jsc/RuntimeTranspilerCache.rs ++++ b/src/jsc/RuntimeTranspilerCache.rs +@@ -66,7 +66,8 @@ bun_core::declare_scope!(cache, visible); + /// Version 34: Discard output containing a previous runtime plugin's resolved import paths. + /// Version 35: Preserve inferred names and inline type-only runtime evaluation edges. + /// Version 36: Runtime plugin imports retain their original specifiers and resolve when executed. +-const EXPECTED_VERSION: u32 = 36; ++/// Version 37: Preserve builtin error constructors and their stack positions and frames. ++const EXPECTED_VERSION: u32 = 37; + + /// Source files smaller than this are not written to / read from the on-disk + /// transpiler cache. Originally 50 KiB, which excluded almost every file in a +diff --git a/src/jsc/bindings/ErrorStackFrame.cpp b/src/jsc/bindings/ErrorStackFrame.cpp +index 14ec9fd8..9f8c3616 100644 +--- a/src/jsc/bindings/ErrorStackFrame.cpp ++++ b/src/jsc/bindings/ErrorStackFrame.cpp +@@ -22,12 +22,13 @@ static bool isConstruct(JSC::CodeBlock* code, JSC::BytecodeIndex bc) + } + } + +-ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc) ++ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync) + { + auto expr = code->expressionInfoForBytecodeIndex(bc); +- auto offset = expr.divot; +- // Constructors point to `new`; other calls retain JSC's syntax location. +- if (isConstruct(code, bc)) ++ auto callSitePosition = code->callSitePositionForBytecodeIndex(bc, isAsync); ++ auto offset = callSitePosition.value_or(expr.divot); ++ // Older or implicit bytecode can lack a syntax-selected stack position. ++ if (!callSitePosition && isConstruct(code, bc)) + offset -= std::min(offset, expr.startOffset); + + auto lineColumn = code->source().provider()->documentLineColumnForOffset(offset); +@@ -38,16 +39,18 @@ ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC:: + }; + } + +-ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc) ++ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync) + { + if (isConstruct(code, bc)) { +- auto position = getAdjustedPositionForBytecode(code, bc); ++ auto position = getAdjustedPositionForBytecode(code, bc, isAsync); + position.byte_position = -1; + return position; + } + +- // Keep JSC's cached lookup for frames that do not need an expression-range adjustment. +- auto lineColumn = code->lineColumnForBytecodeIndex(bc); ++ auto callSitePosition = code->callSitePositionForBytecodeIndex(bc, isAsync); ++ auto lineColumn = callSitePosition ++ ? code->source().provider()->documentLineColumnForOffset(*callSitePosition) ++ : code->lineColumnForBytecodeIndex(bc); + return { + .line_zero_based = OrdinalNumber::fromOneBasedInt(lineColumn.line).zeroBasedInt(), + .column_zero_based = OrdinalNumber::fromOneBasedInt(lineColumn.column).zeroBasedInt(), +diff --git a/src/jsc/bindings/ErrorStackFrame.h b/src/jsc/bindings/ErrorStackFrame.h +index a1c31678..367670b5 100644 +--- a/src/jsc/bindings/ErrorStackFrame.h ++++ b/src/jsc/bindings/ErrorStackFrame.h +@@ -4,8 +4,8 @@ + + namespace Bun { + +-ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc); ++ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync = false); + +-ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc); ++ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync = false); + + } // namespace Bun +diff --git a/src/jsc/bindings/ErrorStackTrace.cpp b/src/jsc/bindings/ErrorStackTrace.cpp +index 3550bb52..866d011c 100644 +--- a/src/jsc/bindings/ErrorStackTrace.cpp ++++ b/src/jsc/bindings/ErrorStackTrace.cpp +@@ -341,7 +341,7 @@ bool JSCStackFrame::calculateSourcePositions() + return false; + } + +- auto location = Bun::getAdjustedLineColumnForBytecode(m_codeBlock, m_bytecodeIndex); ++ auto location = Bun::getAdjustedLineColumnForBytecode(m_codeBlock, m_bytecodeIndex, isAsync()); + m_sourcePositions.line = location.line(); + m_sourcePositions.column = location.column(); + +diff --git a/src/jsc/bindings/FormatStackTraceForJS.cpp b/src/jsc/bindings/FormatStackTraceForJS.cpp +index 60aca2e7..403bf131 100644 +--- a/src/jsc/bindings/FormatStackTraceForJS.cpp ++++ b/src/jsc/bindings/FormatStackTraceForJS.cpp +@@ -321,7 +321,7 @@ WTF::String formatStackTrace( + if (!frame.hasLineAndColumnInfo()) continue; + + if (frame.codeBlock() && frame.hasBytecodeIndex()) { +- auto position = Bun::getAdjustedLineColumnForBytecode(frame.codeBlock(), frame.bytecodeIndex()); ++ auto position = Bun::getAdjustedLineColumnForBytecode(frame.codeBlock(), frame.bytecodeIndex(), frame.isAsyncFrame()); + originalLineColumns[i] = { static_cast(position.line().oneBasedInt()), static_cast(position.column().oneBasedInt()) }; + if (auto overrideLine = frame.codeBlock()->ownerExecutable()->overrideLineNumber(vm)) + originalLineColumns[i].line = *overrideLine; +diff --git a/src/jsc/bindings/ZigException.cpp b/src/jsc/bindings/ZigException.cpp +index cfd1059c..b7442cb3 100644 +--- a/src/jsc/bindings/ZigException.cpp ++++ b/src/jsc/bindings/ZigException.cpp +@@ -143,7 +143,7 @@ static void populateStackFramePosition(const JSC::StackFrame& stackFrame, BunStr + return; + } + +- auto location = Bun::getAdjustedPositionForBytecode(code, stackFrame.bytecodeIndex()); ++ auto location = Bun::getAdjustedPositionForBytecode(code, stackFrame.bytecodeIndex(), stackFrame.isAsyncFrame()); + memcpy(&position, &location, sizeof(ZigStackFramePosition)); + if (flags == PopulateStackTraceFlags::OnlyPosition) + return; +diff --git a/test/bundler/bundler_minify.test.ts b/test/bundler/bundler_minify.test.ts +index 052aa492..0d629bee 100644 +--- a/test/bundler/bundler_minify.test.ts ++++ b/test/bundler/bundler_minify.test.ts +@@ -1229,7 +1229,7 @@ describe("bundler", () => { + }, + }); + +- itBundled("minify/ErrorConstructorOptimization", { ++ itBundled("minify/ErrorConstructorPositions", { + files: { + "/entry.js": /* js */ ` + // Test all Error constructors +@@ -1272,25 +1272,25 @@ describe("bundler", () => { + `, + }, + capture: [ +- "Error()", +- 'Error("message")', +- 'Error("message", { cause: "cause" })', +- "TypeError()", +- 'TypeError("type error")', +- "SyntaxError()", +- 'SyntaxError("syntax error")', +- "RangeError()", +- 'RangeError("range error")', +- "ReferenceError()", +- 'ReferenceError("ref error")', +- "EvalError()", +- 'EvalError("eval error")', +- "URIError()", +- 'URIError("uri error")', +- 'AggregateError([], "aggregate error")', +- 'AggregateError([Error("e1")], "multiple")', +- "Error(msg)", +- "TypeError(getErrorMessage())", ++ "new Error", ++ 'new Error("message")', ++ 'new Error("message", { cause: "cause" })', ++ "new TypeError", ++ 'new TypeError("type error")', ++ "new SyntaxError", ++ 'new SyntaxError("syntax error")', ++ "new RangeError", ++ 'new RangeError("range error")', ++ "new ReferenceError", ++ 'new ReferenceError("ref error")', ++ "new EvalError", ++ 'new EvalError("eval error")', ++ "new URIError", ++ 'new URIError("uri error")', ++ 'new AggregateError([], "aggregate error")', ++ 'new AggregateError([new Error("e1")], "multiple")', ++ "new Error(msg)", ++ "new TypeError(getErrorMessage())", + "/* @__PURE__ */ new Date", + "/* @__PURE__ */ new Map", + "/* @__PURE__ */ new Set", +diff --git a/test/js/node/v8/callsite-async-position-fixture.mjs b/test/js/node/v8/callsite-async-position-fixture.mjs +new file mode 100644 +index 00000000..84cfc6db +--- /dev/null ++++ b/test/js/node/v8/callsite-async-position-fixture.mjs +@@ -0,0 +1,12 @@ ++async function leaf() { ++await Promise.resolve(); ++return new Error().stack; ++} ++async function parent() { ++return await leaf(); ++} ++const trace = await parent(); ++const parentFrame = trace.split("\n").find(line => /\bparent(?:\s|@|\()/.test(line)); ++const position = /:(\d+):(\d+)\)?$/.exec(parentFrame); ++if (!position || +position[1] !== 6 || +position[2] !== 8) ++ throw new Error(JSON.stringify({ expected: [6, 8], trace })); +diff --git a/test/js/node/v8/callsite-syntax-positions-fixture.js b/test/js/node/v8/callsite-syntax-positions-fixture.js +new file mode 100644 +index 00000000..32eed3ee +--- /dev/null ++++ b/test/js/node/v8/callsite-syntax-positions-fixture.js +@@ -0,0 +1,116 @@ ++// Keep call positions independent of exception-expression divots. ++let trace; ++function capture() { const stack = new Error().stack; trace = stack; return stack; } ++function Capture() { this.stack = new Error().stack; trace = this.stack; } ++function factory() { return capture; } ++const object = { capture }; ++const name = "capture"; ++const cases = [ ++ [function () { ++capture() ++ }, 10, 1, "capture()"], ++ [function () { ++capture /* comment */ () ++ }, 13, 1, "capture /* comment */ ()"], ++ [function () { ++capture ++() ++ }, 16, 1, "capture\n()"], ++ [function () { ++factory()() ++ }, 20, 10, "factory()()"], ++ [function () { ++factory() ++() ++ }, 24, 1, "factory()\n()"], ++ [function () { ++factory() /* comment */ () ++ }, 27, 25, "factory() /* comment */ ()"], ++ [function () { ++object.capture() ++ }, 30, 8, "object.capture()"], ++ [function () { ++object ++.capture() ++ }, 34, 2, "object\n.capture()"], ++ [function () { ++object . capture() ++ }, 37, 10, "object . capture()"], ++ [function () { ++object. /* comment */ capture() ++ }, 40, 23, "object. /* comment */ capture()"], ++ [function () { ++object.\u0063apture() ++ }, 43, 8, "object.\\u0063apture()"], ++ [function () { ++object[name]() ++ }, 46, 13, "object[name]()"], ++ [function () { ++object["capture"]() ++ }, 49, 18, "object[\"capture\"]()"], ++ [function () { ++object[ ++name ++] ++() ++ }, 55, 1, "object[\nname\n]\n()"], ++ [function () { ++(capture)() ++ }, 58, 10, "(capture)()"], ++ [function () { ++((capture))() ++ }, 61, 12, "((capture))()"], ++ [function () { ++(object.capture)() ++ }, 64, 17, "(object.capture)()"], ++ [function () { ++(object[name])() ++ }, 67, 15, "(object[name])()"], ++ [function () { ++(0, capture)() ++ }, 70, 13, "(0, capture)()"], ++ [function () { ++capture?.() ++ }, 73, 10, "capture?.()"], ++ [function () { ++object?.capture() ++ }, 76, 9, "object?.capture()"], ++ [function () { ++object.capture?.() ++ }, 79, 17, "object.capture?.()"], ++ [function () { ++object?.capture?.() ++ }, 82, 18, "object?.capture?.()"], ++ [function () { ++this() ++ }, 85, 5, "this()"], ++ [function () { ++capture`text` ++ }, 88, 8, "capture`text`"], ++ [function () { ++Reflect.apply(capture, null, []) ++ }, 91, 9, "Reflect.apply(capture, null, [])"], ++ [function () { ++new Capture().stack ++ }, 94, 1, "new Capture().stack"], ++ [function () { ++new ++Capture().stack ++ }, 97, 1, "new\nCapture().stack"], ++ [function () { ++new (Capture)().stack ++ }, 101, 1, "new (Capture)().stack"], ++ [function () { ++Reflect.construct(Capture, []).stack ++ }, 104, 9, "Reflect.construct(Capture, []).stack"], ++]; ++for (let iteration = 0; iteration < Math.min(typeof testLoopCount === "number" ? testLoopCount : 20, 100); ++iteration) { ++ for (const [fn, line, column, body] of cases) { ++ trace = ""; ++ fn.call(capture); ++ const frames = trace.split("\n").filter(line => /:(\d+):(\d+)\)?$/.test(line)); ++ const position = /:(\d+):(\d+)\)?$/.exec(frames[1]); ++ if (!position || +position[1] !== line || +position[2] !== column) ++ throw new Error(JSON.stringify({ body, expected: [line, column], trace })); ++ } ++} +diff --git a/test/js/node/v8/capture-stack-trace.test.js b/test/js/node/v8/capture-stack-trace.test.js +index ac527424..247d355e 100644 +--- a/test/js/node/v8/capture-stack-trace.test.js ++++ b/test/js/node/v8/capture-stack-trace.test.js +@@ -2456,3 +2456,27 @@ test.skipIf(totalmem() < 10 * 1024 ** 3)( + }, + 30_000, + ); ++ ++test("syntax positions preserve built-in constructors, arrow frames, and property reads", async () => { ++ await using proc = Bun.spawn({ ++ cmd: [bunExe(), import.meta.dir + "/stack-position-regressions-fixture.js"], ++ env: bunEnv, ++ stdout: "pipe", ++ stderr: "pipe", ++ }); ++ const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); ++ expect({ stdout, stderr, exitCode }).toEqual({ stdout: "", stderr: "", exitCode: 0 }); ++}); ++ ++for (const fixture of ["callsite-syntax-positions-fixture.js", "callsite-async-position-fixture.mjs"]) { ++ test(`syntax-selected stack positions: ${fixture}`, async () => { ++ await using proc = Bun.spawn({ ++ cmd: [bunExe(), import.meta.dir + "/" + fixture], ++ env: bunEnv, ++ stdout: "pipe", ++ stderr: "pipe", ++ }); ++ const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); ++ expect({ stdout, stderr, exitCode }).toEqual({ stdout: "", stderr: "", exitCode: 0 }); ++ }); ++} +diff --git a/test/js/node/v8/stack-position-regressions-fixture.js b/test/js/node/v8/stack-position-regressions-fixture.js +new file mode 100644 +index 00000000..fe71747d +--- /dev/null ++++ b/test/js/node/v8/stack-position-regressions-fixture.js +@@ -0,0 +1,104 @@ ++"use strict"; ++const name = "x"; ++const cases = [ ++ ['Error', function () { ++const error = new Error("test"); return error; ++ }, 5, 15], ++ ['TypeError', function () { ++const error = new TypeError("test"); return error; ++ }, 8, 15], ++ ['SyntaxError', function () { ++const error = new SyntaxError("test"); return error; ++ }, 11, 15], ++ ['RangeError', function () { ++const error = new RangeError("test"); return error; ++ }, 14, 15], ++ ['ReferenceError', function () { ++const error = new ReferenceError("test"); return error; ++ }, 17, 15], ++ ['EvalError', function () { ++const error = new EvalError("test"); return error; ++ }, 20, 15], ++ ['URIError', function () { ++const error = new URIError("test"); return error; ++ }, 23, 15], ++ ['AggregateError', function () { ++const error = new AggregateError([], "test"); return error; ++ }, 26, 15], ++ ['arrow-new', () => new Error("arrow"), 28, 25], ++ ['arrow-type', () => new TypeError("arrow"), 29, 26], ++ ['arrow-alias', () => new globalThis.Error("arrow"), 30, 27], ++ ['multiline-new', function () { ++const error = new ++ Error("multiline"); return error; ++ }, 32, 15], ++ ['constructor-comment', function () { ++const error = new /* comment */ Error("comment"); return error; ++ }, 36, 15], ++ ['null-dot', function () { ++try { null.x; } catch (error) { return error; } ++ }, 39, 12], ++ ['undefined-dot', function () { ++try { undefined.x; } catch (error) { return error; } ++ }, 42, 17], ++ ['null-length', function () { ++try { null.length; } catch (error) { return error; } ++ }, 45, 12], ++ ['null-comment', function () { ++try { null . /* comment */ x; } catch (error) { return error; } ++ }, 48, 28], ++ ['null-escape', function () { ++try { null.\u0078; } catch (error) { return error; } ++ }, 51, 12], ++ ['null-multiline', function () { ++try { null ++ .x; } catch (error) { return error; } ++ }, 55, 6], ++ ['null-bracket', function () { ++try { null["x"]; } catch (error) { return error; } ++ }, 58, 11], ++ ['null-number', function () { ++try { null[1]; } catch (error) { return error; } ++ }, 61, 11], ++ ['null-computed', function () { ++try { null[name]; } catch (error) { return error; } ++ }, 64, 11], ++ ['null-call', function () { ++try { null.x(); } catch (error) { return error; } ++ }, 67, 12], ++ ['null-parenthesized-call', function () { ++try { (null.x)(); } catch (error) { return error; } ++ }, 70, 13], ++ ['null-bracket-call', function () { ++try { null["x"](); } catch (error) { return error; } ++ }, 73, 11], ++ ['null-call-method', function () { ++try { null.call(); } catch (error) { return error; } ++ }, 76, 12], ++ ['null-apply-method', function () { ++try { null.apply(null, []); } catch (error) { return error; } ++ }, 79, 12], ++ ['null-has-own', function () { ++try { null.hasOwnProperty(name); } catch (error) { return error; } ++ }, 82, 12], ++ ]; ++function checkPosition(error, line, column, shape, structured) { ++ const frames = structured ? error.stack : error.stack.split("\n").filter(frame => /:(\d+):(\d+)\)?$/.test(frame)); ++ const actual = structured ? [frames[0].getLineNumber(), frames[0].getColumnNumber()] : /:(\d+):(\d+)\)?$/.exec(frames[0])?.slice(1).map(Number); ++ if (!actual || actual[0] !== line || actual[1] !== column) ++ throw new Error(JSON.stringify({ shape, structured, expected: [line, column], actual, trace: structured ? frames.map(String) : error.stack })); ++} ++for (let iteration = 0; iteration < Math.min(typeof testLoopCount === "number" ? testLoopCount : 20, 100); ++iteration) { ++ for (const [shape, create, line, column] of cases) { ++ checkPosition(create(), line, column, shape, false); ++ if (typeof process !== "undefined") { ++ const previous = Error.prepareStackTrace; ++ try { ++ Error.prepareStackTrace = (_, frames) => frames; ++ checkPosition(create(), line, column, shape, true); ++ } finally { ++ Error.prepareStackTrace = previous; ++ } ++ } ++ } ++} diff --git a/.github/openclaw/qualify.sh b/.github/openclaw/qualify.sh index fb7fb33a22832..f0178aa3d32e4 100644 --- a/.github/openclaw/qualify.sh +++ b/.github/openclaw/qualify.sh @@ -27,6 +27,7 @@ for arm in baseline candidate; do git apply --exclude=CHANGELOG.md --exclude='docs/*' --exclude='test/js/node/worker_threads/*' "$patch" done git apply --exclude=CHANGELOG.md --exclude='docs/*' "$inputs/patches/008-namespace.patch" + git apply --exclude=CHANGELOG.md --exclude='docs/*' "$inputs/patches/009-stack-positions.patch" cp "$inputs/worker-resource-limits.test.ts" test/js/node/worker_threads/openclaw-resource-limits.test.ts git diff --check git diff --binary HEAD > "$QUALIFICATION_DIR/bun-adapters.patch" @@ -60,6 +61,7 @@ python3 "$inputs/compare-fork-ci.py" "$QUALIFICATION_DIR/baseline" "$QUALIFICATI candidate="$QUALIFICATION_DIR/bun/build/qualify-candidate/bun" unset BUN_WEBKIT_ARTIFACT_MANIFEST BUN_BUILD_CACHE_DIR BUN_BUILD_PREFETCH_DIR "$candidate" test test/js/bun/resolve/esModule.test.ts test/js/bun/namespace-prototype-pollution.test.ts test/js/bun/resolve/require-esm-evaluating-cycle.test.ts test/js/bun/import-attributes/import-attributes.test.ts test/js/node/module/require-extensions.test.ts > "$QUALIFICATION_DIR/namespace.log" 2>&1 +"$candidate" test test/js/node/v8/capture-stack-trace.test.js > "$QUALIFICATION_DIR/stack-positions.log" 2>&1 "$candidate" "$inputs/segmenter.js" > "$QUALIFICATION_DIR/segmenter.log" 2>&1 "$candidate" test test/js/node/vm/vm.test.ts -t 'a Proxy in the prototype chain' > "$QUALIFICATION_DIR/proxy.log" 2>&1 "$candidate" test test/js/node/worker_threads/openclaw-resource-limits.test.ts -t 'resourceLimits' > "$QUALIFICATION_DIR/resource-limits.log" 2>&1 diff --git a/.github/workflows/openclaw-checks.yml b/.github/workflows/openclaw-checks.yml index d41891ab71558..f12bc06720d17 100644 --- a/.github/workflows/openclaw-checks.yml +++ b/.github/workflows/openclaw-checks.yml @@ -60,6 +60,16 @@ jobs: "$RUNNER_TEMP/jsc-artifact/bun-webkit/bin/jsc" --useDollarVM=1 -e 'if (typeof $vm.moduleNamespaceForRequire !== "function") throw new Error("namespace test hook is missing");' Tools/Scripts/run-jsc-stress-tests JSTests/stress --jsc "$RUNNER_TEMP/jsc-artifact/bun-webkit/bin/jsc" --child-processes 16 --filter 'module-namespace|module-loader-security-thenable-namespace' --output-dir "$RUNNER_TEMP/jsc-results" --no-retry python3 .github/openclaw/verify-jsc-results.py "$RUNNER_TEMP/jsc-results" + - name: Exercise syntax-selected stack positions + shell: bash + run: | + set -euo pipefail + jsc="$RUNNER_TEMP/jsc-artifact/bun-webkit/bin/jsc" + Tools/Scripts/run-jsc-stress-tests JSTests/stress --jsc "$jsc" --child-processes 16 --filter 'callsite-syntax-positions|stack-position-regressions' --output-dir "$RUNNER_TEMP/stack-results" --no-retry + python3 .github/openclaw/verify-jsc-results.py "$RUNNER_TEMP/stack-results" + "$jsc" -m JSTests/stress/callsite-async-position.mjs + "$jsc" --useJIT=false -m JSTests/stress/callsite-async-position.mjs + "$jsc" --useBytecodeOptimizer=true -m JSTests/stress/callsite-async-position.mjs - name: Exercise allocation sampling across execution tiers shell: bash run: | @@ -91,5 +101,7 @@ jobs: ${{ runner.temp }}/qualification/candidate/*.log ${{ runner.temp }}/jsc-results/results ${{ runner.temp }}/jsc-results/*.log + ${{ runner.temp }}/stack-results/results + ${{ runner.temp }}/stack-results/*.log if-no-files-found: error retention-days: 7 diff --git a/JSTests/stress/callsite-async-position.mjs b/JSTests/stress/callsite-async-position.mjs new file mode 100644 index 0000000000000..84cfc6db79100 --- /dev/null +++ b/JSTests/stress/callsite-async-position.mjs @@ -0,0 +1,12 @@ +async function leaf() { +await Promise.resolve(); +return new Error().stack; +} +async function parent() { +return await leaf(); +} +const trace = await parent(); +const parentFrame = trace.split("\n").find(line => /\bparent(?:\s|@|\()/.test(line)); +const position = /:(\d+):(\d+)\)?$/.exec(parentFrame); +if (!position || +position[1] !== 6 || +position[2] !== 8) + throw new Error(JSON.stringify({ expected: [6, 8], trace })); diff --git a/JSTests/stress/callsite-syntax-positions.js b/JSTests/stress/callsite-syntax-positions.js new file mode 100644 index 0000000000000..32eed3eed0dbc --- /dev/null +++ b/JSTests/stress/callsite-syntax-positions.js @@ -0,0 +1,116 @@ +// Keep call positions independent of exception-expression divots. +let trace; +function capture() { const stack = new Error().stack; trace = stack; return stack; } +function Capture() { this.stack = new Error().stack; trace = this.stack; } +function factory() { return capture; } +const object = { capture }; +const name = "capture"; +const cases = [ + [function () { +capture() + }, 10, 1, "capture()"], + [function () { +capture /* comment */ () + }, 13, 1, "capture /* comment */ ()"], + [function () { +capture +() + }, 16, 1, "capture\n()"], + [function () { +factory()() + }, 20, 10, "factory()()"], + [function () { +factory() +() + }, 24, 1, "factory()\n()"], + [function () { +factory() /* comment */ () + }, 27, 25, "factory() /* comment */ ()"], + [function () { +object.capture() + }, 30, 8, "object.capture()"], + [function () { +object +.capture() + }, 34, 2, "object\n.capture()"], + [function () { +object . capture() + }, 37, 10, "object . capture()"], + [function () { +object. /* comment */ capture() + }, 40, 23, "object. /* comment */ capture()"], + [function () { +object.\u0063apture() + }, 43, 8, "object.\\u0063apture()"], + [function () { +object[name]() + }, 46, 13, "object[name]()"], + [function () { +object["capture"]() + }, 49, 18, "object[\"capture\"]()"], + [function () { +object[ +name +] +() + }, 55, 1, "object[\nname\n]\n()"], + [function () { +(capture)() + }, 58, 10, "(capture)()"], + [function () { +((capture))() + }, 61, 12, "((capture))()"], + [function () { +(object.capture)() + }, 64, 17, "(object.capture)()"], + [function () { +(object[name])() + }, 67, 15, "(object[name])()"], + [function () { +(0, capture)() + }, 70, 13, "(0, capture)()"], + [function () { +capture?.() + }, 73, 10, "capture?.()"], + [function () { +object?.capture() + }, 76, 9, "object?.capture()"], + [function () { +object.capture?.() + }, 79, 17, "object.capture?.()"], + [function () { +object?.capture?.() + }, 82, 18, "object?.capture?.()"], + [function () { +this() + }, 85, 5, "this()"], + [function () { +capture`text` + }, 88, 8, "capture`text`"], + [function () { +Reflect.apply(capture, null, []) + }, 91, 9, "Reflect.apply(capture, null, [])"], + [function () { +new Capture().stack + }, 94, 1, "new Capture().stack"], + [function () { +new +Capture().stack + }, 97, 1, "new\nCapture().stack"], + [function () { +new (Capture)().stack + }, 101, 1, "new (Capture)().stack"], + [function () { +Reflect.construct(Capture, []).stack + }, 104, 9, "Reflect.construct(Capture, []).stack"], +]; +for (let iteration = 0; iteration < Math.min(typeof testLoopCount === "number" ? testLoopCount : 20, 100); ++iteration) { + for (const [fn, line, column, body] of cases) { + trace = ""; + fn.call(capture); + const frames = trace.split("\n").filter(line => /:(\d+):(\d+)\)?$/.test(line)); + const position = /:(\d+):(\d+)\)?$/.exec(frames[1]); + if (!position || +position[1] !== line || +position[2] !== column) + throw new Error(JSON.stringify({ body, expected: [line, column], trace })); + } +} diff --git a/JSTests/stress/stack-position-regressions.js b/JSTests/stress/stack-position-regressions.js new file mode 100644 index 0000000000000..fe71747d9f95d --- /dev/null +++ b/JSTests/stress/stack-position-regressions.js @@ -0,0 +1,104 @@ +"use strict"; +const name = "x"; +const cases = [ + ['Error', function () { +const error = new Error("test"); return error; + }, 5, 15], + ['TypeError', function () { +const error = new TypeError("test"); return error; + }, 8, 15], + ['SyntaxError', function () { +const error = new SyntaxError("test"); return error; + }, 11, 15], + ['RangeError', function () { +const error = new RangeError("test"); return error; + }, 14, 15], + ['ReferenceError', function () { +const error = new ReferenceError("test"); return error; + }, 17, 15], + ['EvalError', function () { +const error = new EvalError("test"); return error; + }, 20, 15], + ['URIError', function () { +const error = new URIError("test"); return error; + }, 23, 15], + ['AggregateError', function () { +const error = new AggregateError([], "test"); return error; + }, 26, 15], + ['arrow-new', () => new Error("arrow"), 28, 25], + ['arrow-type', () => new TypeError("arrow"), 29, 26], + ['arrow-alias', () => new globalThis.Error("arrow"), 30, 27], + ['multiline-new', function () { +const error = new + Error("multiline"); return error; + }, 32, 15], + ['constructor-comment', function () { +const error = new /* comment */ Error("comment"); return error; + }, 36, 15], + ['null-dot', function () { +try { null.x; } catch (error) { return error; } + }, 39, 12], + ['undefined-dot', function () { +try { undefined.x; } catch (error) { return error; } + }, 42, 17], + ['null-length', function () { +try { null.length; } catch (error) { return error; } + }, 45, 12], + ['null-comment', function () { +try { null . /* comment */ x; } catch (error) { return error; } + }, 48, 28], + ['null-escape', function () { +try { null.\u0078; } catch (error) { return error; } + }, 51, 12], + ['null-multiline', function () { +try { null + .x; } catch (error) { return error; } + }, 55, 6], + ['null-bracket', function () { +try { null["x"]; } catch (error) { return error; } + }, 58, 11], + ['null-number', function () { +try { null[1]; } catch (error) { return error; } + }, 61, 11], + ['null-computed', function () { +try { null[name]; } catch (error) { return error; } + }, 64, 11], + ['null-call', function () { +try { null.x(); } catch (error) { return error; } + }, 67, 12], + ['null-parenthesized-call', function () { +try { (null.x)(); } catch (error) { return error; } + }, 70, 13], + ['null-bracket-call', function () { +try { null["x"](); } catch (error) { return error; } + }, 73, 11], + ['null-call-method', function () { +try { null.call(); } catch (error) { return error; } + }, 76, 12], + ['null-apply-method', function () { +try { null.apply(null, []); } catch (error) { return error; } + }, 79, 12], + ['null-has-own', function () { +try { null.hasOwnProperty(name); } catch (error) { return error; } + }, 82, 12], + ]; +function checkPosition(error, line, column, shape, structured) { + const frames = structured ? error.stack : error.stack.split("\n").filter(frame => /:(\d+):(\d+)\)?$/.test(frame)); + const actual = structured ? [frames[0].getLineNumber(), frames[0].getColumnNumber()] : /:(\d+):(\d+)\)?$/.exec(frames[0])?.slice(1).map(Number); + if (!actual || actual[0] !== line || actual[1] !== column) + throw new Error(JSON.stringify({ shape, structured, expected: [line, column], actual, trace: structured ? frames.map(String) : error.stack })); +} +for (let iteration = 0; iteration < Math.min(typeof testLoopCount === "number" ? testLoopCount : 20, 100); ++iteration) { + for (const [shape, create, line, column] of cases) { + checkPosition(create(), line, column, shape, false); + if (typeof process !== "undefined") { + const previous = Error.prepareStackTrace; + try { + Error.prepareStackTrace = (_, frames) => frames; + checkPosition(create(), line, column, shape, true); + } finally { + Error.prepareStackTrace = previous; + } + } + } +} diff --git a/OPENCLAW.md b/OPENCLAW.md index 126c4c51e848e..1b35ae8a3749f 100644 --- a/OPENCLAW.md +++ b/OPENCLAW.md @@ -128,5 +128,13 @@ entrypoint. ## Unreleased +- Keep syntax-selected stack positions separate from exception ranges for calls, constructors, property reads, and async continuations. Preserve the positions across bytecode rewriting, optimization, and cache replay. + - Add real byte-based JSC heap allocation sampling with weighted allocating-stack profiles and independent major/minor collection inclusion. - Check allocation sampling across interpreter and JIT tiers before merging engine pull requests. + +## Stack positions + +`CodeBlock::callSitePositionForBytecodeIndex` returns an optional absolute source offset for an exact instruction or async resume label. Stack readers use this position while exception expression ranges and debugger line tables retain their existing divots. RareData owns the two sparse vectors, including decoded cache entries; the bytecode cache revision prevents older layouts from being reused. + +Bun must be rebuilt with its matching stack-reader adapter. It must also preserve built-in error constructor syntax: rewriting `new Error()` as `Error()` changes the position and allows a returning arrow to become a proper tail call. The engine's proper-tail-call behavior is unchanged. The integration continues @robobun’s upstream Bun source-position work in [#35179](https://github.com/oven-sh/bun/pull/35179), [#37396](https://github.com/oven-sh/bun/pull/37396), and [#41580](https://github.com/oven-sh/bun/pull/41580). diff --git a/Source/JavaScriptCore/bytecode/BytecodeOptimizer.cpp b/Source/JavaScriptCore/bytecode/BytecodeOptimizer.cpp index 69ba7bd481d58..b00e8aca93cca 100644 --- a/Source/JavaScriptCore/bytecode/BytecodeOptimizer.cpp +++ b/Source/JavaScriptCore/bytecode/BytecodeOptimizer.cpp @@ -2144,6 +2144,18 @@ void BytecodeOptimizerAccess::emit() return newOffsetForOld(probe); }); + for (auto* positions : { &m_codeBlock->m_callSitePositions, &m_codeBlock->m_asyncResumePositions }) { + positions->removeAllMatching([&](auto& position) { + if (position.first >= m_offsetToIndex.size()) + return true; + unsigned index = m_offsetToIndex[position.first]; + if (index == UINT_MAX || !m_insns[index].live) + return true; + position.first = m_insns[index].newOffset; + return false; + }); + } + // All jumps were sized to fit; nothing is out of line anymore. m_codeBlock->replaceOutOfLineJumpTargets(); diff --git a/Source/JavaScriptCore/bytecode/BytecodeRewriter.h b/Source/JavaScriptCore/bytecode/BytecodeRewriter.h index ab3537881dd50..57feb9a95213b 100644 --- a/Source/JavaScriptCore/bytecode/BytecodeRewriter.h +++ b/Source/JavaScriptCore/bytecode/BytecodeRewriter.h @@ -206,6 +206,17 @@ WTF_MAKE_NONCOPYABLE(BytecodeRewriter); return adjustJumpTarget(InsertionPoint(0, Position::Entrypoint), InsertionPoint(absoluteOffset, Position::LabelPoint)); } + std::optional adjustInstructionOffset(JSInstructionStream::Offset offset) + { + for (const auto& insertion : m_insertions) { + if (insertion.type == Insertion::Type::Remove + && offset >= static_cast(insertion.index.bytecodeOffset) + && offset - insertion.index.bytecodeOffset < insertion.removeLength) + return std::nullopt; + } + return adjustJumpTarget(InsertionPoint(0, Position::Entrypoint), InsertionPoint(offset, Position::OriginalBytecodePoint)); + } + int32_t adjustJumpTarget(JSInstructionStream::Offset originalBytecodeOffset, int32_t originalJumpTarget) { return adjustJumpTarget(InsertionPoint(originalBytecodeOffset, Position::LabelPoint), InsertionPoint(originalJumpTarget, Position::LabelPoint)); diff --git a/Source/JavaScriptCore/bytecode/CodeBlock.cpp b/Source/JavaScriptCore/bytecode/CodeBlock.cpp index ca3f29cb81722..c960c1d248072 100644 --- a/Source/JavaScriptCore/bytecode/CodeBlock.cpp +++ b/Source/JavaScriptCore/bytecode/CodeBlock.cpp @@ -2592,6 +2592,14 @@ ExpressionInfo::Entry CodeBlock::expressionInfoForBytecodeIndex(BytecodeIndex by return entry; } +std::optional CodeBlock::callSitePositionForBytecodeIndex(BytecodeIndex index, bool isAsync) const +{ + auto offset = m_unlinkedCode->callSitePositionForBytecodeIndex(index, isAsync); + if (!offset) + return std::nullopt; + return *offset + sourceOffset(); +} + bool CodeBlock::hasOpDebugForLineAndColumn(unsigned line, std::optional column) { const auto& instructionStream = instructions(); diff --git a/Source/JavaScriptCore/bytecode/CodeBlock.h b/Source/JavaScriptCore/bytecode/CodeBlock.h index 483afcf94e818..25ad03afe38bc 100644 --- a/Source/JavaScriptCore/bytecode/CodeBlock.h +++ b/Source/JavaScriptCore/bytecode/CodeBlock.h @@ -289,6 +289,7 @@ class CodeBlock : public JSCell { // For a thread that runs beside the mutator. It keeps nothing, so it decodes the expression info on every call. LineColumn lineColumnForBytecodeIndexConcurrently(BytecodeIndex) const; ExpressionInfo::Entry expressionInfoForBytecodeIndex(BytecodeIndex) const; + JS_EXPORT_PRIVATE std::optional callSitePositionForBytecodeIndex(BytecodeIndex, bool isAsync = false) const; std::optional bytecodeIndexFromCallSiteIndex(CallSiteIndex); diff --git a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.cpp b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.cpp index d7d0085565b7b..021ffa3a70673 100644 --- a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.cpp +++ b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.cpp @@ -153,6 +153,19 @@ size_t UnlinkedCodeBlock::estimatedSize(JSCell* cell, VM& vm) return Base::estimatedSize(cell, vm) + extraSize; } +std::optional UnlinkedCodeBlock::callSitePositionForBytecodeIndex(BytecodeIndex index, bool isAsync) const +{ + if (!m_rareData) + return std::nullopt; + const auto& positions = isAsync ? m_rareData->m_asyncResumePositions : m_rareData->m_callSitePositions; + auto it = std::lower_bound(positions.begin(), positions.end(), index.offset(), [](const auto& position, unsigned offset) { + return position.first < offset; + }); + if (it == positions.end() || it->first != index.offset()) + return std::nullopt; + return it->second; +} + size_t UnlinkedCodeBlock::RareData::sizeInBytes(const AbstractLocker&) const { size_t size = sizeof(RareData); @@ -162,6 +175,8 @@ size_t UnlinkedCodeBlock::RareData::sizeInBytes(const AbstractLocker&) const size += m_unlinkedStringSwitchJumpTables.byteSize(); size += m_typeProfilerInfoMap.capacity() * sizeof(decltype(m_typeProfilerInfoMap)::KeyValuePairType); size += m_opProfileControlFlowBytecodeOffsets.byteSize(); + size += m_callSitePositions.byteSize(); + size += m_asyncResumePositions.byteSize(); size += m_bitVectors.byteSize(); // FIXME: account for each bit vector. size += m_constantIdentifierSets.byteSize(); diff --git a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.h b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.h index 4cca7ec3062aa..b841129dbb51d 100644 --- a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.h +++ b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlock.h @@ -486,6 +486,9 @@ class UnlinkedCodeBlock : public JSCell { public: using OutOfLineJumpTargets = UncheckedKeyHashMap; + using CallSitePosition = std::pair; + std::optional callSitePositionForBytecodeIndex(BytecodeIndex, bool isAsync) const; + struct RareData { WTF_DEPRECATED_MAKE_STRUCT_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(RareData, UnlinkedCodeBlock_RareData); @@ -504,6 +507,8 @@ class UnlinkedCodeBlock : public JSCell { }; UncheckedKeyHashMap m_typeProfilerInfoMap; FixedVector m_opProfileControlFlowBytecodeOffsets; + FixedVector m_callSitePositions; + FixedVector m_asyncResumePositions; FixedVector m_bitVectors; FixedVector m_constantIdentifierSets; diff --git a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.cpp b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.cpp index de910491fd379..9ec22d3fea41e 100644 --- a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.cpp +++ b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.cpp @@ -81,6 +81,8 @@ bool UnlinkedCodeBlockGenerator::finalize(std::unique_ptr i || !m_unlinkedStringSwitchJumpTables.isEmpty() || !m_typeProfilerInfoMap.isEmpty() || !m_opProfileControlFlowBytecodeOffsets.isEmpty() + || !m_callSitePositions.isEmpty() + || !m_asyncResumePositions.isEmpty() || !m_bitVectors.isEmpty() || !m_constantIdentifierSets.isEmpty()) m_codeBlock->createRareDataIfNecessary(locker); @@ -92,6 +94,8 @@ bool UnlinkedCodeBlockGenerator::finalize(std::unique_ptr i m_codeBlock->m_rareData->m_unlinkedStringSwitchJumpTables = WTF::move(m_unlinkedStringSwitchJumpTables); m_codeBlock->m_rareData->m_typeProfilerInfoMap = WTF::move(m_typeProfilerInfoMap); m_codeBlock->m_rareData->m_opProfileControlFlowBytecodeOffsets = WTF::move(m_opProfileControlFlowBytecodeOffsets); + m_codeBlock->m_rareData->m_callSitePositions = WTF::move(m_callSitePositions); + m_codeBlock->m_rareData->m_asyncResumePositions = WTF::move(m_asyncResumePositions); m_codeBlock->m_rareData->m_bitVectors = WTF::move(m_bitVectors); m_codeBlock->m_rareData->m_constantIdentifierSets = WTF::move(m_constantIdentifierSets); } @@ -138,6 +142,18 @@ void UnlinkedCodeBlockGenerator::applyModification(BytecodeRewriter& rewriter) m_typeProfilerInfoMap.swap(adjustedTypeProfilerInfoMap); } + m_callSitePositions.removeAllMatching([&](auto& position) { + auto offset = rewriter.adjustInstructionOffset(position.first); + if (!offset) + return true; + position.first = *offset; + return false; + }); + + // Async stack frames use the state switch's resume label, including injected restore instructions. + for (auto& position : m_asyncResumePositions) + position.first = rewriter.adjustAbsoluteOffset(position.first); + Vector bytecodeOffsetAdjustments; rewriter.forEachLabelPoint([&] (int32_t bytecodeOffset) { bytecodeOffsetAdjustments.append(bytecodeOffset); diff --git a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.h b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.h index 13515ff946fe4..a05b48d9c1c15 100644 --- a/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.h +++ b/Source/JavaScriptCore/bytecode/UnlinkedCodeBlockGenerator.h @@ -76,6 +76,16 @@ class UnlinkedCodeBlockGenerator { UnlinkedMetadataTable& metadata() { return m_codeBlock->metadata(); } void addExpressionInfo(unsigned instructionOffset, unsigned divot, unsigned startOffset, unsigned endOffset); + void addCallSitePosition(unsigned instructionOffset, unsigned sourceOffset) + { + ASSERT(m_callSitePositions.isEmpty() || m_callSitePositions.last().first < instructionOffset); + m_callSitePositions.append({ instructionOffset, sourceOffset }); + } + void addAsyncResumePosition(unsigned instructionOffset, unsigned sourceOffset) + { + ASSERT(m_asyncResumePositions.isEmpty() || m_asyncResumePositions.last().first < instructionOffset); + m_asyncResumePositions.append({ instructionOffset, sourceOffset }); + } void addTypeProfilerExpressionInfo(unsigned instructionOffset, unsigned startDivot, unsigned endDivot); void addOpProfileControlFlowBytecodeOffset(JSInstructionStream::Offset offset) { @@ -210,6 +220,8 @@ class UnlinkedCodeBlockGenerator { Vector m_unlinkedStringSwitchJumpTables; UncheckedKeyHashMap m_typeProfilerInfoMap; Vector m_opProfileControlFlowBytecodeOffsets; + Vector m_callSitePositions; + Vector m_asyncResumePositions; Vector m_bitVectors; Vector m_constantIdentifierSets; unsigned m_numBinaryArithProfiles { 0 }; diff --git a/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp b/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp index 6ffe83b8db654..39c9f173c8536 100644 --- a/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp +++ b/Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp @@ -3838,30 +3838,30 @@ RegisterID* BytecodeGenerator::emitAsyncIteratorNext(RegisterID* dst, RegisterID return dst; } -RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { - return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall); + return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } -void BytecodeGenerator::emitCallIgnoreResult(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +void BytecodeGenerator::emitCallIgnoreResult(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { - emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall); + emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } -RegisterID* BytecodeGenerator::emitCallInTailPosition(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCallInTailPosition(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { if (m_allowTailCallOptimization) { m_codeBlock->setHasTailCalls(); - return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall); + return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } if (m_allowCallIgnoreResultOptimization) - return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall); - return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall); + return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall, callSitePosition); + return emitCall(dst, func, expectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } -RegisterID* BytecodeGenerator::emitCallDirectEval(RegisterID* dst, RegisterID* func, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCallDirectEval(RegisterID* dst, RegisterID* func, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { - return emitCall(dst, func, NoExpectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall); + return emitCall(dst, func, NoExpectedFunction, callArguments, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } ExpectedFunction BytecodeGenerator::expectedFunctionForIdentifier(const Identifier& identifier) @@ -3939,7 +3939,7 @@ LexicallyScopedFeatures BytecodeGenerator::computeFeaturesForCallDirectEval() } template -RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { constexpr auto opcodeID = CallOp::opcodeID; ASSERT(opcodeID == op_call || opcodeID == op_call_direct_eval || opcodeID == op_tail_call || opcodeID == op_call_ignore_result); @@ -3962,12 +3962,12 @@ RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, Expec emitExpressionInfo(spread->divot(), spread->divotStart(), spread->divotEnd()); OpSpread::emit(this, argumentRegister.get(), argumentRegister.get()); - return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, debuggableCall); + return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } } RefPtr argumentRegister; argumentRegister = expression->emitBytecode(*this, callArguments.argumentRegister(0)); - return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, debuggableCall); + return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } for (; n; n = n->m_next) emitNode(callArguments.argumentRegister(argument++), n); @@ -3989,6 +3989,8 @@ RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, Expec if (opcodeID == op_tail_call) emitLogShadowChickenTailIfNecessary(); + emitCallSitePosition(callSitePosition); + // Emit call. ASSERT(dst); ASSERT(dst != ignoredResult()); @@ -4009,21 +4011,21 @@ RegisterID* BytecodeGenerator::emitCall(RegisterID* dst, RegisterID* func, Expec return dst; } -RegisterID* BytecodeGenerator::emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { - return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall); + return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } -RegisterID* BytecodeGenerator::emitCallVarargsInTailPosition(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCallVarargsInTailPosition(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { if (m_allowTailCallOptimization) - return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall); - return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall); + return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall, callSitePosition); + return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } -RegisterID* BytecodeGenerator::emitConstructVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitConstructVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { - return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall); + return emitCallVarargs(dst, func, thisRegister, arguments, firstFreeRegister, firstVarArgOffset, divot, divotStart, divotEnd, debuggableCall, callSitePosition); } RegisterID* BytecodeGenerator::emitSuperConstructVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) @@ -4032,7 +4034,7 @@ RegisterID* BytecodeGenerator::emitSuperConstructVarargs(RegisterID* dst, Regist } template -RegisterID* BytecodeGenerator::emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall) +RegisterID* BytecodeGenerator::emitCallVarargs(RegisterID* dst, RegisterID* func, RegisterID* thisRegister, RegisterID* arguments, RegisterID* firstFreeRegister, int32_t firstVarArgOffset, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, DebuggableCall debuggableCall, std::optional callSitePosition) { if (shouldEmitDebugHooks() && debuggableCall == DebuggableCall::Yes) emitDebugHook(WillExecuteExpression, divotStart); @@ -4042,6 +4044,8 @@ RegisterID* BytecodeGenerator::emitCallVarargs(RegisterID* dst, RegisterID* func if (VarargsOp::opcodeID == op_tail_call_varargs) emitLogShadowChickenTailIfNecessary(); + emitCallSitePosition(callSitePosition); + // Emit call. ASSERT(dst != ignoredResult()); if constexpr (VarargsOp::opcodeID == op_tail_call_varargs) @@ -4155,7 +4159,7 @@ RegisterID* BytecodeGenerator::emitReturn(RegisterID* src) } template -RegisterID* BytecodeGenerator::emitConstructImpl(RegisterID* dst, RegisterID* func, RegisterID* lazyThis, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, bool isDefaultDerivedConstructorCall) +RegisterID* BytecodeGenerator::emitConstructImpl(RegisterID* dst, RegisterID* func, RegisterID* lazyThis, ExpectedFunction expectedFunction, CallArguments& callArguments, const JSTextPosition& divot, const JSTextPosition& divotStart, const JSTextPosition& divotEnd, bool isDefaultDerivedConstructorCall, std::optional callSitePosition) { ASSERT(func->refCount()); @@ -4180,13 +4184,13 @@ RegisterID* BytecodeGenerator::emitConstructImpl(RegisterID* dst, RegisterID* fu } move(callArguments.thisRegister(), lazyThis); - return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, DebuggableCall::No); + return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, DebuggableCall::No, callSitePosition); } } RefPtr argumentRegister; argumentRegister = expression->emitBytecode(*this, callArguments.argumentRegister(0)); move(callArguments.thisRegister(), lazyThis); - return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, DebuggableCall::No); + return emitCallVarargs::type>(dst, func, callArguments.thisRegister(), argumentRegister.get(), newTemporary(), 0, divot, divotStart, divotEnd, DebuggableCall::No, callSitePosition); } for (ArgumentListNode* n = argumentsNode->m_listNode; n; n = n->m_next) @@ -4205,6 +4209,7 @@ RegisterID* BytecodeGenerator::emitConstructImpl(RegisterID* dst, RegisterID* fu Ref