From b1aafd717a50b934169146eb498c349dbc72e868 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 8 Oct 2026 06:57:11 +0000 Subject: [PATCH] [JSC] getBytecodeIndex() gives a builtin frame its own bytecode index An error that JSC raises in a builtin with a source URL (one of the embedder's modules) quotes the wrong source text. Bun prints "undefined is not an object (evaluating 'stream')" where the access that failed is `stream._writableState`. The line and the column of the error are right. FindFirstCallerFrameWithCodeblockFunctor, behind getBytecodeIndex(), takes the code block of the frame it stops at, but leaves the bytecode index at 0 when that code block is a builtin. Upstream never reads that pair: a builtin has no expression info there. Here a builtin with a source URL has expression info in every build (BytecodeGenerator::emitExpressionInfo), and appendSourceToErrorMessage() quotes from it. ExpressionInfo maps an index before the first entry to the first entry, so the text was that of the first expression of the function. With debugger hooks in the bytecode it was the head of the function, as "(near '...')". The functor now takes the index from the frame that gives the code block, so both callers get the index of the frame it stops at. appendSourceToErrorMessage() reads the expression info and the text from that one code block: its text is now that of the expression that failed, as for user code. It still returns early for a private builtin. slow_path_check_tdz gets the same index. A TDZ error raised in the frame the functor stops at now names the binding of the check, where it named the first expression of the function. Its other defects stay: it uses a null code block when the functor finds no frame, it takes the text from its own code block when the functor stops at another frame, and some checks have no expression info of their own. --- ...rce-url-error-quotes-failing-expression.js | 93 +++++++++++++++++++ Source/JavaScriptCore/runtime/Error.cpp | 5 + 2 files changed, 98 insertions(+) create mode 100644 JSTests/stress/builtin-with-source-url-error-quotes-failing-expression.js diff --git a/JSTests/stress/builtin-with-source-url-error-quotes-failing-expression.js b/JSTests/stress/builtin-with-source-url-error-quotes-failing-expression.js new file mode 100644 index 0000000000000..574a55160af03 --- /dev/null +++ b/JSTests/stress/builtin-with-source-url-error-quotes-failing-expression.js @@ -0,0 +1,93 @@ +//@ defaultRun; run("debugger-bytecode", "--forceDebuggerBytecodeGeneration=true") + +// A builtin whose source has a URL is one of the embedder's modules. It has expression info in every build, and an +// error raised in it quotes its source text like an error in user code does. The text must be that of the expression +// that failed. It was looked up at bytecode index 0: the first expression of the function (`object` and `callback` +// below), or the head of the function when the bytecode has debugger hooks. + +function shouldBe(actual, expected, message) { + if (actual !== expected) + throw new Error(`${message}: expected ${JSON.stringify(expected)} but got ${JSON.stringify(actual)}`); +} + +function messageOf(callback) { + try { + callback(); + } catch (error) { + return error.message; + } + return "did not throw"; +} + +const readValueSource = `(function (object) { + var copy = object; + return copy.state.value; +})`; + +const readBeforeInitializationSource = `(function (callback, ready) { + callback(); + if (ready) + return ready; + var before = later; + let later = 1; + return before; +})`; + +const nothing = () => { }; + +function test(readValue, readBeforeInitialization, iterations, message) { + for (let i = 0; i < iterations; i++) { + // Most calls succeed, so that the builtins tier up. The first call and every 16th call after it fail. + if (i % 16) { + shouldBe(readValue({ state: { value: i } }), i, message); + shouldBe(readBeforeInitialization(nothing, true), true, message); + continue; + } + shouldBe(messageOf(() => readValue({ })), "undefined is not an object (evaluating 'copy.state.value')", message); + shouldBe(messageOf(() => readBeforeInitialization(nothing, false)), "Cannot access 'later' before initialization.", message); + } +} + +// builtinFromBytecodeCache() makes a builtin with a source URL, the way an embedder does: from source, or from the +// bytes that encodeBuiltinFunction() made of it. +test(builtinFromBytecodeCache(readValueSource, false), builtinFromBytecodeCache(readBeforeInitializationSource, false), testLoopCount, "from source"); +test(builtinFromBytecodeCache(readValueSource, true), builtinFromBytecodeCache(readBeforeInitializationSource, true), 32, "from the bytecode cache"); + +// $vm.createBuiltin() gives its source no URL: a private builtin, like the engine's own. appendSourceToErrorMessage() +// keeps its text out of the message in every build. +const privateReadValue = $vm.createBuiltin(readValueSource); +for (let i = 0; i < 32; i++) + shouldBe(messageOf(() => privateReadValue({ })), "undefined is not an object", "private builtin"); + +// Each way to fail below gives the message that the same function gives as user code. The expression that fails is +// never the first one of the function. In the `Object.keys` body a host function makes the error, and the text is +// that of the call. +const bodies = [ + "return copy.state.value;", + "return copy.state[key];", + "copy.state.value = 1;", + "delete copy.state.value;", + "return key in copy.state;", + "return copy instanceof copy.state;", + "return new copy.state();", + "return copy.state();", + "return Object.keys(copy.state);", + "var before = later; let later = copy; return before;", +]; +for (const body of bodies) { + const source = `(function (object, key) { + var copy = object; + ${body} +})`; + const expected = messageOf(() => (0, eval)(source)({ }, "key")); + shouldBe(expected === "did not throw", false, body); + for (const roundTrip of [false, true]) { + const builtin = builtinFromBytecodeCache(source, roundTrip); + for (let i = 0; i < 3; i++) + shouldBe(messageOf(() => builtin({ }, "key")), expected, body); + } +} +shouldBe(messageOf(() => builtinFromBytecodeCache(`(function (object) { + var copy = object; + return Object.keys(copy.state); +})`, false)({ })), "undefined is not an object (evaluating 'Object.keys(copy.state)')", "host function"); diff --git a/Source/JavaScriptCore/runtime/Error.cpp b/Source/JavaScriptCore/runtime/Error.cpp index 8093069fc6cfc..e5ebbcf80d8b5 100644 --- a/Source/JavaScriptCore/runtime/Error.cpp +++ b/Source/JavaScriptCore/runtime/Error.cpp @@ -150,8 +150,13 @@ class FindFirstCallerFrameWithCodeblockFunctor { m_codeBlock = codeBlock; +#if USE(BUN_JSC_ADDITIONS) + // A builtin can have expression info (BytecodeGenerator::emitExpressionInfo), and the callers look it up by this index. + m_bytecodeIndex = visitor->bytecodeIndex(); +#else if (!codeBlock->unlinkedCodeBlock()->isBuiltinFunction()) m_bytecodeIndex = visitor->bytecodeIndex(); +#endif return IterationStatus::Done; }