Skip to content
Open
27 changes: 13 additions & 14 deletions scripts/build/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,22 @@
if (usePch) {
n.comment("─── PCH ───");
n.blank();
// Dep outputs are IMPLICIT inputs (not order-only). The crucial case is
// local WebKit: headers live in buildDir and get REGENERATED by dep_build
// mid-run. At startup, ninja sees old headers via PCH's depfile → thinks
// PCH is fresh. dep_build then regenerates them. cxx fails with "file
// modified since PCH was built". As implicit inputs, restat sees the .a
// changed → PCH rebuilds → one-build convergence. See the pch() docstring.
// Dep outputs and codegen headers are IMPLICIT inputs (not order-only):
// both are regenerated mid-run (local WebKit headers by dep_build, codegen
// headers by their step), and the PCH's depfile records them by absolute
// path, which is not the node the producing edge declares. At startup ninja
// sees the old header → thinks the PCH is fresh → cxx compiles against a
// stale PCH (or fails with "file modified since PCH was built"). As
// implicit inputs, restat sees the regenerated file → PCH rebuilds in the
// same run. See the pch() docstring.
//
// Codegen stays order-only: those outputs only change if inputs change,
// and inputs don't change mid-build. cppAll (not all) — bake/.rs outputs
// are rust-only; pulling them here would run bake-codegen in cpp-only CI
// mode where it fails on the pinned bun version (see cppAll docstring).
// Scripts that emit undeclared .h also emit a .cpp/.h in cppAll, so they
// still run. cxx transitively waits: cxx → PCH → deps+cppAll.
// cppAll (not all) — bake/.rs outputs are rust-only; pulling them here
// would run bake-codegen in cpp-only CI mode where it fails on the pinned
// bun version (see cppAll docstring). cxx transitively waits: cxx → PCH →
// deps+cppAll.
pchOut = pch(n, cfg, "src/jsc/bindings/root-pch.h", {
flags: cxxFlagsFull,
implicitInputs: depHeaderSignal,
orderOnlyInputs: codegen.cppAll,
implicitInputs: [...depHeaderSignal, ...codegen.cppAll],

Check warning on line 297 in scripts/build/bun.ts

View check run for this annotation

Claude / Claude Code Review

codegen.cppAll as implicit PCH input causes spurious full C++ rebuilds

Making all of `codegen.cppAll` an implicit PCH input over-applies the fix: `cppAll` includes generated **.cpp** files (WebCoreJSBuiltins.cpp, ZigGeneratedClasses.cpp, JSSink.cpp, every bindgenv2 .cpp) and headers the PCH never reaches (InternalModuleRegistryConstants.h, InternalModuleRegistry+*.h, SyntheticModuleType.h, …). The `pch` rule has no `restat: true`, so editing any `src/js/builtins/*.ts` (which shifts `fn.sourceOffset` in WebCoreJSBuiltins.cpp) — or in release, any `src/js/{node,b
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
});
}

Expand Down
20 changes: 16 additions & 4 deletions scripts/build/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
* the per-file Generated*.h headers and not just the .cpp that gets compiled.
*
* Files nothing compiles may stay undeclared (.d.ts twins, bundle-modules'
* eval/ dir, JSSink.lut.txt consumed within its own step). The remaining
* #included exception is BunBuiltinNames+extras.h from bundle-functions.ts,
* reached through the PCH.
* eval/ dir, JSSink.lut.txt consumed within its own step).
*/

import { spawnSync } from "node:child_process";
Expand Down Expand Up @@ -759,10 +757,16 @@ function emitJsModules({ n, cfg, sources, o, dirStamp }: Ctx): void {
// ($makeErrorWithCode(N, ...)); without this dep an ErrorCode.ts edit leaves
// stale error numbers in the JS bundles while the C++ enum regenerates.
const errorCodeInput = resolve(cfg.cwd, "src", "jsc", "bindings", "ErrorCode.ts");
// replacements.ts derives the @Name intrinsic globals from this table, and
// bundle-functions.ts emits BunBuiltinNames+extras.h as the ones missing from
// BunBuiltinNames.h.
const globalObjectTableInput = resolve(cfg.cwd, "src", "jsc", "bindings", "ZigGlobalObject.lut.txt");
const builtinNamesInput = resolve(cfg.cwd, "src", "js", "builtins", "BunBuiltinNames.h");

const outputs = [
resolve(cfg.codegenDir, "WebCoreJSBuiltins.cpp"),
resolve(cfg.codegenDir, "WebCoreJSBuiltins.h"),
resolve(cfg.codegenDir, "BunBuiltinNames+extras.h"),
resolve(cfg.codegenDir, "InternalModuleRegistryConstants.h"),
resolve(cfg.codegenDir, "InternalModuleRegistry+createInternalModuleById.h"),
resolve(cfg.codegenDir, "InternalModuleRegistry+enum.h"),
Expand All @@ -785,7 +789,15 @@ function emitJsModules({ n, cfg, sources, o, dirStamp }: Ctx): void {
n.build({
outputs,
rule: "codegen",
inputs: [script, ...sources.js, ...sources.jsCodegen, extraInput, errorCodeInput],
inputs: [
script,
...sources.js,
...sources.jsCodegen,
extraInput,
errorCodeInput,
globalObjectTableInput,
builtinNamesInput,
],
orderOnlyInputs: [dirStamp],
vars: {
cwd: cfg.cwd,
Expand Down
24 changes: 11 additions & 13 deletions scripts/build/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,25 +358,23 @@ export function pch(
opts: {
flags: string[];
/**
* Files whose change must invalidate the PCH. Typically: dep output
* libs (libJavaScriptCore.a etc.).
* Files whose change must invalidate the PCH: dep output libs
* (libJavaScriptCore.a etc.) and generated headers.
*
* Can't be order-only: the depfile tracks headers, but ninja stats at
* startup. Local WebKit headers live in buildDir and get regenerated
* by dep_build MID-RUN. At startup ninja sees old headers → thinks
* PCH is fresh → cxx fails with "file modified since PCH was built"
* → needs a second build. With these implicit, restat propagates the
* lib change to PCH and it rebuilds in the same run.
* Can't be order-only: the depfile tracks headers, but ninja stats them
* at startup and records them by absolute path (a different node from
* the producing edge's declared output). Anything regenerated MID-RUN —
* local WebKit headers by dep_build, codegen headers by their step —
* leaves the PCH looking fresh → cxx builds against a stale PCH or fails
* with "file modified since PCH was built" → needs a second build. With
* these implicit, restat propagates the change to the PCH and it
* rebuilds in the same run.
*
* Cost: PCH also rebuilds on unrelated dep bumps (brotli etc.). Rare
* enough to accept for correctness.
*/
implicitInputs?: string[];
/**
* Must exist before PCH compiles; changes don't invalidate it.
* Codegen outputs go here — they only change when inputs change,
* and inputs don't change mid-build.
*/
/** Must exist before PCH compiles; changes don't invalidate it. */
orderOnlyInputs?: string[];
},
): { pch: string; wrapperHeader: string } {
Expand Down
8 changes: 4 additions & 4 deletions src/codegen/bundle-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { sliceSourceCode } from "./builtin-parser";
import { createAssertClientJS, createLogClientJS } from "./client-js";
import { getJS2NativeDTS } from "./generate-js2native";
import { cap, checkAscii, low, writeIfNotChanged } from "./helpers";
import { applyGlobalReplacements, define } from "./replacements";
import { applyGlobalReplacements, define, intrinsicGlobals } from "./replacements";

const PARALLEL = false;
const KEEP_TMP = true;
Expand Down Expand Up @@ -329,8 +329,7 @@ $$capture_start$$(${fn.async ? "async " : ""}${
(usesAssert ? createAssertClientJS(fn.name) : ""),
)
.replace(/^\((async )?function\(/, "($1function (")
.replace(/__intrinsic__/g, "@")
.replace(/__no_intrinsic__/g, "") + "\n";
.replace(/__intrinsic__/g, "@") + "\n";

const errors = [...finalReplacement.matchAll(/@bundleError\((.*)\)/g)];
if (errors.length) {
Expand Down Expand Up @@ -415,7 +414,8 @@ export async function bundleBuiltinFunctions({ requireTransformer }: BundleBuilt
}
const combinedSourceCodeLength = combinedSourceCode.length;

let additionalPrivateNames = new Set();
// Every @Name a builtin can reference must exist as a private symbol.
let additionalPrivateNames = new Set(intrinsicGlobals);

function privateName(name) {
additionalPrivateNames.add(name);
Expand Down
3 changes: 1 addition & 2 deletions src/codegen/bundle-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ for (const entrypoint of bundledEntryPoints) {
throw new Error(`Builtin Bundler: do not use import.meta.require() (in ${file_path}))`);
})
.replace(/return \$\nexport /, "return")
.replace(/__intrinsic__/g, "@")
.replace(/__no_intrinsic__/g, "") + "\n";
.replace(/__intrinsic__/g, "@") + "\n";
captured = captured.replace(
/function\s*\(.*?\)\s*{/,
'$&"use strict";' +
Expand Down
43 changes: 22 additions & 21 deletions src/codegen/replacements.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFileSync } from "node:fs";
import path from "node:path";
import NodeErrors from "../jsc/bindings/ErrorCode.ts";
import jsclasses from "./../jsc/bindings/js_classes";
import { sliceSourceCode } from "./builtin-parser";
Expand Down Expand Up @@ -51,40 +53,39 @@ export const globalReplacements: ReplacementRule[] = [
},
];

// This is a list of globals we should access using @ notation
// This prevents a global override attacks.
// Note that the public `Bun` global is immutable.
// Globals from ZigGlobalObject.lut.txt whose value is an object the runtime
// creates (constructors, `process`, `crypto`, ...). Builtins reference these as
// @Name, which Zig::GlobalObject::getOwnPropertySlot resolves to the original
// value even if user code replaced or deleted globalThis.Name — the same
// guarantee Node's lib/ gets from taking them off internalBinding().
export const intrinsicGlobals = [
...readFileSync(path.join(import.meta.dir, "../jsc/bindings/ZigGlobalObject.lut.txt"), "utf8").matchAll(
/^\s+([A-Za-z]\w*)\s+(\S+)\s+(\S+)\s*$/gm,
),
]
.filter(
([, , value, attributes]) =>
/\b(CellProperty|ClassStructure)\b/.test(attributes) ||
(/\bPropertyCallback\b/.test(attributes) && value.endsWith("ConstructorCallback")),
)
.map(([, name]) => name);
Comment thread
dylan-conway marked this conversation as resolved.
Resolved

// Globals builtins access as @Name so user code replacing globalThis.Name cannot
// reach them. The rest are provided by JSC.
// undefined -> __intrinsic__undefined -> @undefined
export const globalsToPrefix = [
"AbortSignal",
...intrinsicGlobals,
"Array",
"ArrayBuffer",
"Buffer",
"Infinity",
"Promise",
"ReadableByteStreamController",
"ReadableStream",
"ReadableStreamBYOBReader",
"ReadableStreamBYOBRequest",
"ReadableStreamDefaultController",
"ReadableStreamDefaultReader",
"TransformStream",
"TransformStreamDefaultController",
"Uint8Array",
"String",
"RegExp",
"WritableStream",
"WritableStreamDefaultController",
"WritableStreamDefaultWriter",
"isFinite",
"undefined",
];

replacements.push({
from: new RegExp(`\\bextends\\s+(${globalsToPrefix.join("|")})`, "g"),
to: "extends __no_intrinsic__%1",
});

// These enums map to $<enum>IdToLabel and $<enum>LabelToId (ids start at 1)
// Make sure to define in ./builtins.d.ts
export const enums = {
Expand Down
4 changes: 4 additions & 0 deletions src/js/builtins.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ declare class ReadableStreamBYOBReader {
constructor(stream: ReadableStream);
}

// Globals the runtime creates (see intrinsicGlobals in codegen/replacements.ts)
// are rewritten to $Name automatically; spell $Name only where a local shadows it.
declare const $Worker: typeof Worker;

// Inlining our enum types
declare const $ImportKindIdToLabel: Array<import("bun").ImportKind>;
declare const $ImportKindLabelToId: Record<import("bun").ImportKind, number>;
Expand Down
12 changes: 0 additions & 12 deletions src/js/builtins/BunBuiltinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,8 @@ using namespace JSC;
// Keep this list sorted.
#define BUN_COMMON_PRIVATE_IDENTIFIERS_EACH_PROPERTY_NAME(macro) \
macro($$typeof) \
macro(AbortSignal) \
macro(Buffer) \
macro(ReadableByteStreamController) \
macro(ReadableStream) \
macro(ReadableStreamBYOBReader) \
macro(ReadableStreamBYOBRequest) \
macro(ReadableStreamDefaultController) \
macro(ReadableStreamDefaultReader) \
macro(SQL) \
macro(TransformStream) \
macro(TransformStreamDefaultController) \
macro(WritableStream) \
macro(WritableStreamDefaultController) \
macro(WritableStreamDefaultWriter) \
macro(_debugInfo) \
macro(_debugStack) \
macro(_debugTask) \
Expand Down
6 changes: 1 addition & 5 deletions src/js/builtins/ProcessObjectInternals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ export function initializeNextTickQueue(
) {
var queue;
var tickInitHooks;
var process;
var nextTickQueue = nextTickQueue;
var drainMicrotasks = drainMicrotasksFn;
var reportUncaughtException = reportUncaughtExceptionFn;
Expand Down Expand Up @@ -411,10 +410,7 @@ export function initializeNextTickQueue(

function nextTick(cb, ...args) {
validateFunction(cb, "callback");
if (setup) {
setup();
process = globalThis.process;
}
if (setup) setup();
if (process._exiting) return;

const tock = {
Expand Down
2 changes: 1 addition & 1 deletion src/js/bun/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const FFIType = {

const suffix = process.platform === "win32" ? "dll" : process.platform === "darwin" ? "dylib" : "so";

var ffi = globalThis.Bun.FFI;
var ffi = Bun.FFI;
const ptr = (arg1, arg2) => (typeof arg2 === "undefined" ? ffi.ptr(arg1) : ffi.ptr(arg1, arg2));
const toBuffer = ffi.toBuffer;
const toArrayBuffer = ffi.toArrayBuffer;
Expand Down
2 changes: 1 addition & 1 deletion src/js/internal/worker/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function receiveMessageFromWorker(source, value, memory) {
// Bun half of Node's createMainThreadPort: create the channel linking a (future)
// thread to the main thread. Called before `new Worker`.
function createMessagingChannel() {
const { port1, port2 } = new globalThis.MessageChannel();
const { port1, port2 } = new MessageChannel();
// port1 (portToMain) stays with the hub; port2 (portToWorker) is transferred to
// the new thread where it becomes that thread's mainThreadPort.
return { portToMain: port1, portToWorker: port2 };
Expand Down
3 changes: 0 additions & 3 deletions src/js/node/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ Certificate.verifySpkac = verifySpkac;
Certificate.exportPublicKey = exportPublicKey;
Certificate.exportChallenge = exportChallenge;

var Buffer = globalThis.Buffer;
const { isAnyArrayBuffer, isArrayBufferView } = require("node:util/types");

function getArrayBufferOrView(buffer, name, encoding?) {
Expand All @@ -123,8 +122,6 @@ function getArrayBufferOrView(buffer, name, encoding?) {
return buffer;
}

const crypto = globalThis.crypto;

var crypto_exports: any = {};

crypto_exports.getRandomValues = value => crypto.getRandomValues(value);
Expand Down
2 changes: 0 additions & 2 deletions src/js/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const { METHODS, STATUS_CODES, setMaxHTTPHeaderSize, getMaxHTTPHeaderSize } = re

if (process.env.NODE_DEBUG) require("node:util").debuglog("http");

const { WebSocket, CloseEvent, MessageEvent } = globalThis;

function createServer(options, callback) {
return new Server(options, callback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/node/inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function open(port?: number, host?: string, wait?: boolean) {
const hostname = typeof host === "string" && host.length > 0 ? host : "127.0.0.1";
// Bracket bare IPv6 hosts so they survive URL parsing.
const hostPart = hostname.includes(":") && !hostname.startsWith("[") ? `[${hostname}]` : hostname;
const requestedUrl = `ws://${hostPart}:${portNumber}/${globalThis.crypto.randomUUID()}`;
const requestedUrl = `ws://${hostPart}:${portNumber}/${crypto.randomUUID()}`;

const disposable = {
__proto__: null,
Expand Down
Loading
Loading