From 8b2ee5b0e6a183f010ec12ba24f97ea5343fd757 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 09:32:07 +0000 Subject: [PATCH 1/4] bake: type the server HMR patch loaders on JSC::JSGlobalObject BakeLoadServerHmrPatch and BakeLoadServerHmrPatchWithSourceMap are defined inside namespace Bake, so their GlobalObject* parameter is Bake::GlobalObject*, the production build's global. Their only callers are in DevServer.rs, which runs in the ordinary runtime VM and passes a plain Zig::GlobalObject. The bodies only use the JSGlobalObject part, so take JSC::JSGlobalObject* like BakeLoadInitialServerCode does. Add a source lint that flags any bake extern "C" entry point taking Bake::GlobalObject* that is bound from Rust outside production.rs, and run the source lints when bake C++ changes. --- .github/workflows/source-lints.yml | 4 + src/runtime/bake/BakeSourceProvider.cpp | 4 +- .../bake-global-object-ffi.test.ts | 111 ++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 test/internal/source-lints/bake-global-object-ffi.test.ts diff --git a/.github/workflows/source-lints.yml b/.github/workflows/source-lints.yml index afeebaf9aa0c..e864340acfa2 100644 --- a/.github/workflows/source-lints.yml +++ b/.github/workflows/source-lints.yml @@ -14,6 +14,8 @@ on: paths: - "src/**/*.rs" - "src/jsc/bindings/**" + - "src/runtime/bake/*.cpp" + - "src/runtime/bake/*.h" - "scripts/build/**" - "scripts/glob-sources.ts" - "scripts/utils.mjs" @@ -29,6 +31,8 @@ on: paths: - "src/**/*.rs" - "src/jsc/bindings/**" + - "src/runtime/bake/*.cpp" + - "src/runtime/bake/*.h" - "scripts/build/**" - "scripts/glob-sources.ts" - "scripts/utils.mjs" diff --git a/src/runtime/bake/BakeSourceProvider.cpp b/src/runtime/bake/BakeSourceProvider.cpp index b337c9f3f218..eda1cd2dd87f 100644 --- a/src/runtime/bake/BakeSourceProvider.cpp +++ b/src/runtime/bake/BakeSourceProvider.cpp @@ -58,7 +58,7 @@ extern "C" JSC::JSPromise* BakeLoadModuleByKey(GlobalObject* global, JSC::JSStri return JSC::loadAndEvaluateModule(global, key->getString(global), nullptr, nullptr); } -extern "C" JSC::EncodedJSValue BakeLoadServerHmrPatch(GlobalObject* global, BunString source) { +extern "C" JSC::EncodedJSValue BakeLoadServerHmrPatch(JSC::JSGlobalObject* global, BunString source) { JSC::VM&vm = global->vm(); auto scope = DECLARE_THROW_SCOPE(vm); @@ -80,7 +80,7 @@ extern "C" JSC::EncodedJSValue BakeLoadServerHmrPatch(GlobalObject* global, BunS return JSC::JSValue::encode(result); } -extern "C" JSC::EncodedJSValue BakeLoadServerHmrPatchWithSourceMap(GlobalObject* global, BunString source, const char* sourceMapJSONPtr, size_t sourceMapJSONLength) { +extern "C" JSC::EncodedJSValue BakeLoadServerHmrPatchWithSourceMap(JSC::JSGlobalObject* global, BunString source, const char* sourceMapJSONPtr, size_t sourceMapJSONLength) { JSC::VM&vm = global->vm(); auto scope = DECLARE_THROW_SCOPE(vm); diff --git a/test/internal/source-lints/bake-global-object-ffi.test.ts b/test/internal/source-lints/bake-global-object-ffi.test.ts new file mode 100644 index 000000000000..e9ff73dff64e --- /dev/null +++ b/test/internal/source-lints/bake-global-object-ffi.test.ts @@ -0,0 +1,111 @@ +import { Glob } from "bun"; +import { expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import path from "node:path"; + +// `Bake::GlobalObject` (src/runtime/bake/BakeGlobalObject.h) is the global of a +// `bun build --app` production build: `BakeCreateProdGlobal` is the only thing +// that creates one, and it adds `m_perThreadData` on top of `Zig::GlobalObject`. +// The dev server runs inside the ordinary runtime VM, whose global is a plain +// `Zig::GlobalObject`. +// +// An `extern "C"` entry point in src/runtime/bake/ whose parameter is typed +// `GlobalObject*` (`Bake::GlobalObject*`, since these files sit in +// `namespace Bake`) therefore claims that every caller passes a production +// global. Nothing checks that claim at the boundary: the Rust bindings all spell +// the parameter `&JSGlobalObject`, so a dev-server caller compiles, and the C++ +// body reads a `Zig::GlobalObject` as if it were the larger `Bake::GlobalObject`. +// +// Motivating instance: `BakeLoadServerHmrPatch` and +// `BakeLoadServerHmrPatchWithSourceMap` (src/runtime/bake/BakeSourceProvider.cpp) +// took `GlobalObject*` while their only callers were in +// src/runtime/bake/DevServer.rs. Entry points reachable from the dev server take +// `JSC::JSGlobalObject*`, like `BakeLoadInitialServerCode` next to them. +// +// The lint: every bake `extern "C"` function with a `Bake::GlobalObject*` +// parameter may only be bound (`fn Name(..)` in an extern block, `#[link_name]`, +// or a `HOST_EXPORT` marker) from the Rust files listed below, which are the +// ones whose global really is a production global. Recognized C++ shape: one +// `extern "C" Name(...)` declaration or definition per function, which is +// how every entry point in src/runtime/bake/ is written. + +const root = path.resolve(import.meta.dir, "..", "..", ".."); +const bakeDir = "src/runtime/bake"; + +const PRODUCTION_GLOBAL_RUST_FILES = new Set(["src/runtime/bake/production.rs"]); + +const EXTERN_C_FUNCTION = /extern\s+"C"\s+[^;{}()]*?\b(\w+)\s*\(/g; +// `GlobalObject*`, `Bake::GlobalObject*` or `::Bake::GlobalObject*` (also by +// reference). The lookbehind rejects `JSC::JSGlobalObject*` and +// `Zig::GlobalObject*`, which every global satisfies. +const BAKE_GLOBAL_PARAM = /(? { + for (const rel of new Glob(pattern).scanSync({ cwd: path.join(root, dir) })) { + const file = path.posix.join(dir, rel.replaceAll(path.sep, "/")); + yield { file, source: stripLineComments(readFileSync(path.join(root, file), "utf8")) }; + } +} + +// C++ function name -> where it is declared with a `Bake::GlobalObject*` parameter. +const declaredAt = new Map(); +let scannedCxx = 0; +for (const { file, source } of readSources(bakeDir, "*.{cpp,h}")) { + scannedCxx++; + for (const m of source.matchAll(EXTERN_C_FUNCTION)) { + const paramsStart = m.index + m[0].length; + let depth = 1; + let i = paramsStart; + for (; i < source.length && depth > 0; i++) { + if (source[i] === "(") depth++; + else if (source[i] === ")") depth--; + } + if (!BAKE_GLOBAL_PARAM.test(source.slice(paramsStart, i - 1))) continue; + const name = m[1]; + declaredAt.set(name, [...(declaredAt.get(name) ?? []), `${file}:${lineOf(source, m.index)}`]); + } +} + +const names = [...declaredAt.keys()].sort(); +const offenders: string[] = []; +const boundFromProduction: string[] = []; +if (names.length > 0) { + const alternatives = names.join("|"); + const RUST_BINDING = new RegExp( + String.raw`\bfn\s+(${alternatives})\s*\(|\blink_name\s*=\s*"(${alternatives})"|\bHOST_EXPORT\(\s*(${alternatives})\b`, + "g", + ); + for (const { file, source } of readSources("src", "**/*.rs")) { + for (const m of source.matchAll(RUST_BINDING)) { + const name = m[1] ?? m[2] ?? m[3]; + const entry = `${file}:${lineOf(source, m.index)}: ${name} (takes Bake::GlobalObject* at ${declaredAt.get(name)!.join(", ")})`; + (PRODUCTION_GLOBAL_RUST_FILES.has(file) ? boundFromProduction : offenders).push(entry); + } + } +} +offenders.sort(); + +test("scans the bake C++ sources", () => { + expect(scannedCxx).toBeGreaterThan(0); +}); + +test("the pattern still recognizes entry points that take Bake::GlobalObject*", () => { + // If this goes empty, either the last such entry point was retyped (then + // delete this lint) or the declarations no longer match EXTERN_C_FUNCTION. + expect(names).not.toBeEmpty(); + // Same for the Rust side of the match: the production build binds these. + expect(boundFromProduction).not.toBeEmpty(); +}); + +test("entry points that take Bake::GlobalObject* are only bound by the production build", () => { + expect(offenders).toEqual([]); +}); From 1563110c024a41c07d8e090e545259def9eb7a42 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 09:54:22 +0000 Subject: [PATCH 2/4] lint: strip comments only on the C++ side so HOST_EXPORT markers are matched --- test/internal/source-lints/bake-global-object-ffi.test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/internal/source-lints/bake-global-object-ffi.test.ts b/test/internal/source-lints/bake-global-object-ffi.test.ts index e9ff73dff64e..12f684aaaeaf 100644 --- a/test/internal/source-lints/bake-global-object-ffi.test.ts +++ b/test/internal/source-lints/bake-global-object-ffi.test.ts @@ -40,6 +40,9 @@ const EXTERN_C_FUNCTION = /extern\s+"C"\s+[^;{}()]*?\b(\w+)\s*\(/g; // `Zig::GlobalObject*`, which every global satisfies. const BAKE_GLOBAL_PARAM = /(? { for (const rel of new Glob(pattern).scanSync({ cwd: path.join(root, dir) })) { const file = path.posix.join(dir, rel.replaceAll(path.sep, "/")); - yield { file, source: stripLineComments(readFileSync(path.join(root, file), "utf8")) }; + yield { file, source: readFileSync(path.join(root, file), "utf8") }; } } // C++ function name -> where it is declared with a `Bake::GlobalObject*` parameter. const declaredAt = new Map(); let scannedCxx = 0; -for (const { file, source } of readSources(bakeDir, "*.{cpp,h}")) { +for (const { file, source: rawSource } of readSources(bakeDir, "*.{cpp,h}")) { scannedCxx++; + const source = stripLineComments(rawSource); for (const m of source.matchAll(EXTERN_C_FUNCTION)) { const paramsStart = m.index + m[0].length; let depth = 1; From 6d2778e84357a47f4a98ab002b7b36683d2e7a65 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 14:11:55 +0000 Subject: [PATCH 3/4] ci: retrigger From e88dc2c0c52fb74fbed2ea31f10b33f0b6475a04 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 15 Aug 2026 15:43:06 +0000 Subject: [PATCH 4/4] bake: take JSC::JSGlobalObject* in every bake extern "C" entry point BakeLoadModuleByKey and BakeGlobalObject__attachPerThreadData were the last two bake entry points whose parameter was typed Bake::GlobalObject*. Rust binds every one of them as &JSGlobalObject, so the parameter type is a claim nothing checks; follow BakeGlobalObject__getPerThreadData instead and uncheckedDowncast inside the one function that needs the production field, which asserts the claim in debug builds. The lint now simply bans Bake::GlobalObject* parameters on bake extern "C" functions. The source-lints workflow trigger is left alone: merge_group runs it unconditionally and bake changes almost always touch a .rs file. --- .github/workflows/source-lints.yml | 4 - src/runtime/bake/BakeGlobalObject.cpp | 4 +- src/runtime/bake/BakeGlobalObject.h | 2 +- src/runtime/bake/BakeSourceProvider.cpp | 2 +- .../bake-global-object-ffi.test.ts | 102 +++++------------- 5 files changed, 30 insertions(+), 84 deletions(-) diff --git a/.github/workflows/source-lints.yml b/.github/workflows/source-lints.yml index e864340acfa2..afeebaf9aa0c 100644 --- a/.github/workflows/source-lints.yml +++ b/.github/workflows/source-lints.yml @@ -14,8 +14,6 @@ on: paths: - "src/**/*.rs" - "src/jsc/bindings/**" - - "src/runtime/bake/*.cpp" - - "src/runtime/bake/*.h" - "scripts/build/**" - "scripts/glob-sources.ts" - "scripts/utils.mjs" @@ -31,8 +29,6 @@ on: paths: - "src/**/*.rs" - "src/jsc/bindings/**" - - "src/runtime/bake/*.cpp" - - "src/runtime/bake/*.h" - "scripts/build/**" - "scripts/glob-sources.ts" - "scripts/utils.mjs" diff --git a/src/runtime/bake/BakeGlobalObject.cpp b/src/runtime/bake/BakeGlobalObject.cpp index 766b100614fe..2d63dab1949e 100644 --- a/src/runtime/bake/BakeGlobalObject.cpp +++ b/src/runtime/bake/BakeGlobalObject.cpp @@ -268,9 +268,9 @@ extern "C" GlobalObject* BakeCreateProdGlobal(void* console) return global; } -extern "C" void BakeGlobalObject__attachPerThreadData(GlobalObject* global, void* perThreadData) +extern "C" void BakeGlobalObject__attachPerThreadData(JSC::JSGlobalObject* global, void* perThreadData) { - global->m_perThreadData = perThreadData; + uncheckedDowncast(global)->m_perThreadData = perThreadData; } const JSC::ClassInfo Bake::GlobalObject::s_info = { "GlobalObject"_s, &Base::s_info, nullptr, nullptr, diff --git a/src/runtime/bake/BakeGlobalObject.h b/src/runtime/bake/BakeGlobalObject.h index 58e4d3f2753d..80ace1ca93ef 100644 --- a/src/runtime/bake/BakeGlobalObject.h +++ b/src/runtime/bake/BakeGlobalObject.h @@ -38,6 +38,6 @@ class GlobalObject : public Zig::GlobalObject { }; extern "C" void* BakeGlobalObject__getPerThreadData(JSC::JSGlobalObject* global); -extern "C" void BakeGlobalObject__attachPerThreadData(GlobalObject* global, void* perThreadData); +extern "C" void BakeGlobalObject__attachPerThreadData(JSC::JSGlobalObject* global, void* perThreadData); }; // namespace Kit diff --git a/src/runtime/bake/BakeSourceProvider.cpp b/src/runtime/bake/BakeSourceProvider.cpp index eda1cd2dd87f..bd28416d8a72 100644 --- a/src/runtime/bake/BakeSourceProvider.cpp +++ b/src/runtime/bake/BakeSourceProvider.cpp @@ -54,7 +54,7 @@ extern "C" JSC::EncodedJSValue BakeLoadInitialServerCode(JSC::JSGlobalObject* gl RELEASE_AND_RETURN(scope, JSC::JSValue::encode(JSC::profiledCall(global, JSC::ProfilingReason::API, fn, callData, JSC::jsUndefined(), args))); } -extern "C" JSC::JSPromise* BakeLoadModuleByKey(GlobalObject* global, JSC::JSString* key) { +extern "C" JSC::JSPromise* BakeLoadModuleByKey(JSC::JSGlobalObject* global, JSC::JSString* key) { return JSC::loadAndEvaluateModule(global, key->getString(global), nullptr, nullptr); } diff --git a/test/internal/source-lints/bake-global-object-ffi.test.ts b/test/internal/source-lints/bake-global-object-ffi.test.ts index 12f684aaaeaf..85c830d42667 100644 --- a/test/internal/source-lints/bake-global-object-ffi.test.ts +++ b/test/internal/source-lints/bake-global-object-ffi.test.ts @@ -3,69 +3,43 @@ import { expect, test } from "bun:test"; import { readFileSync } from "node:fs"; import path from "node:path"; -// `Bake::GlobalObject` (src/runtime/bake/BakeGlobalObject.h) is the global of a -// `bun build --app` production build: `BakeCreateProdGlobal` is the only thing -// that creates one, and it adds `m_perThreadData` on top of `Zig::GlobalObject`. -// The dev server runs inside the ordinary runtime VM, whose global is a plain -// `Zig::GlobalObject`. +// `Bake::GlobalObject` (src/runtime/bake/BakeGlobalObject.h) only exists in a +// `bun build --app` production build; the dev server runs in the ordinary +// runtime VM, whose global is a plain `Zig::GlobalObject`. Rust binds every bake +// entry point as `&JSGlobalObject`, so an `extern "C"` parameter typed +// `GlobalObject*` (`Bake::GlobalObject*` inside `namespace Bake`) is a claim +// about the caller that nothing checks: `BakeLoadServerHmrPatch` was declared +// that way while only ever being called by the dev server. // -// An `extern "C"` entry point in src/runtime/bake/ whose parameter is typed -// `GlobalObject*` (`Bake::GlobalObject*`, since these files sit in -// `namespace Bake`) therefore claims that every caller passes a production -// global. Nothing checks that claim at the boundary: the Rust bindings all spell -// the parameter `&JSGlobalObject`, so a dev-server caller compiles, and the C++ -// body reads a `Zig::GlobalObject` as if it were the larger `Bake::GlobalObject`. +// The idiom in these files is to take `JSC::JSGlobalObject*` and +// `uncheckedDowncast` inside the functions that need the +// production-only fields (`BakeGlobalObject__getPerThreadData`), which asserts +// the claim in debug builds. This lint keeps the signatures on that idiom. // -// Motivating instance: `BakeLoadServerHmrPatch` and -// `BakeLoadServerHmrPatchWithSourceMap` (src/runtime/bake/BakeSourceProvider.cpp) -// took `GlobalObject*` while their only callers were in -// src/runtime/bake/DevServer.rs. Entry points reachable from the dev server take -// `JSC::JSGlobalObject*`, like `BakeLoadInitialServerCode` next to them. -// -// The lint: every bake `extern "C"` function with a `Bake::GlobalObject*` -// parameter may only be bound (`fn Name(..)` in an extern block, `#[link_name]`, -// or a `HOST_EXPORT` marker) from the Rust files listed below, which are the -// ones whose global really is a production global. Recognized C++ shape: one -// `extern "C" Name(...)` declaration or definition per function, which is -// how every entry point in src/runtime/bake/ is written. +// Recognized shape: one `extern "C" Name(...)` declaration or definition +// per function, which is how every entry point in src/runtime/bake/ is written. const root = path.resolve(import.meta.dir, "..", "..", ".."); const bakeDir = "src/runtime/bake"; -const PRODUCTION_GLOBAL_RUST_FILES = new Set(["src/runtime/bake/production.rs"]); - const EXTERN_C_FUNCTION = /extern\s+"C"\s+[^;{}()]*?\b(\w+)\s*\(/g; // `GlobalObject*`, `Bake::GlobalObject*` or `::Bake::GlobalObject*` (also by // reference). The lookbehind rejects `JSC::JSGlobalObject*` and // `Zig::GlobalObject*`, which every global satisfies. const BAKE_GLOBAL_PARAM = /(? { - for (const rel of new Glob(pattern).scanSync({ cwd: path.join(root, dir) })) { - const file = path.posix.join(dir, rel.replaceAll(path.sep, "/")); - yield { file, source: readFileSync(path.join(root, file), "utf8") }; - } -} - -// C++ function name -> where it is declared with a `Bake::GlobalObject*` parameter. -const declaredAt = new Map(); -let scannedCxx = 0; -for (const { file, source: rawSource } of readSources(bakeDir, "*.{cpp,h}")) { - scannedCxx++; - const source = stripLineComments(rawSource); +let externCFunctions = 0; +const offenders: string[] = []; +for (const rel of new Glob("*.{cpp,h}").scanSync({ cwd: path.join(root, bakeDir) })) { + const file = path.posix.join(bakeDir, rel.replaceAll(path.sep, "/")); + const source = stripLineComments(readFileSync(path.join(root, file), "utf8")); for (const m of source.matchAll(EXTERN_C_FUNCTION)) { + externCFunctions++; const paramsStart = m.index + m[0].length; let depth = 1; let i = paramsStart; @@ -74,42 +48,18 @@ for (const { file, source: rawSource } of readSources(bakeDir, "*.{cpp,h}")) { else if (source[i] === ")") depth--; } if (!BAKE_GLOBAL_PARAM.test(source.slice(paramsStart, i - 1))) continue; - const name = m[1]; - declaredAt.set(name, [...(declaredAt.get(name) ?? []), `${file}:${lineOf(source, m.index)}`]); - } -} - -const names = [...declaredAt.keys()].sort(); -const offenders: string[] = []; -const boundFromProduction: string[] = []; -if (names.length > 0) { - const alternatives = names.join("|"); - const RUST_BINDING = new RegExp( - String.raw`\bfn\s+(${alternatives})\s*\(|\blink_name\s*=\s*"(${alternatives})"|\bHOST_EXPORT\(\s*(${alternatives})\b`, - "g", - ); - for (const { file, source } of readSources("src", "**/*.rs")) { - for (const m of source.matchAll(RUST_BINDING)) { - const name = m[1] ?? m[2] ?? m[3]; - const entry = `${file}:${lineOf(source, m.index)}: ${name} (takes Bake::GlobalObject* at ${declaredAt.get(name)!.join(", ")})`; - (PRODUCTION_GLOBAL_RUST_FILES.has(file) ? boundFromProduction : offenders).push(entry); - } + const line = source.slice(0, m.index).split("\n").length; + offenders.push(`${file}:${line}: ${m[1]}`); } } offenders.sort(); -test("scans the bake C++ sources", () => { - expect(scannedCxx).toBeGreaterThan(0); -}); - -test("the pattern still recognizes entry points that take Bake::GlobalObject*", () => { - // If this goes empty, either the last such entry point was retyped (then - // delete this lint) or the declarations no longer match EXTERN_C_FUNCTION. - expect(names).not.toBeEmpty(); - // Same for the Rust side of the match: the production build binds these. - expect(boundFromProduction).not.toBeEmpty(); +test('the pattern still recognizes the bake extern "C" entry points', () => { + // Guards against the ban below passing because the declarations changed + // shape (or the directory moved) and nothing was scanned. + expect(externCFunctions).toBeGreaterThan(0); }); -test("entry points that take Bake::GlobalObject* are only bound by the production build", () => { +test('bake extern "C" entry points take JSC::JSGlobalObject*, not Bake::GlobalObject*', () => { expect(offenders).toEqual([]); });