-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Reject hot-reloaded builtin JS from a different codegen generation in debug builds #36942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
6
commits into
main
Choose a base branch
from
farm/0d9fe1ab/fix-dev-js-reload-skew
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b29e6a
debug builds: reject hot-reloaded builtin JS from a different codegen…
robobun 005d9a1
[autofix.ci] apply automated fixes
autofix-ci[bot] 380232c
review: require the generation stamp to be the file's final line
robobun fa0704b
review: keep the generation define off the PCH include chain
robobun c639ae2
review: derive the replacements signature from the built rules
robobun 516d928
review: keep the dev-reload test out of the parallel bucket
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, isDebug } from "harness"; | ||
| import fs from "node:fs"; | ||
| import { dirname, join } from "node:path"; | ||
|
|
||
| // Non-CI debug builds hot-reload builtin JS from `<buildDir>/js` | ||
| // (BUN_DYNAMIC_JS_LOAD_PATH) so `src/js` edits apply without relinking. Those | ||
| // files bake in codegen-assigned numeric IDs ($lazy native-call IDs, internal | ||
| // module registry indices, error-code IDs), so a file written by a different | ||
| // codegen run than the one the binary was built from can dispatch to the wrong | ||
| // native binding. Each file carries a trailing `@bun-internal-module-generation` | ||
| // stamp that the loader checks; a mismatch must fail with an actionable error | ||
| // instead of loading misnumbered code. | ||
| // | ||
| // Only dev debug builds read the hot-reload dir. Codegen writes `<buildDir>/js` | ||
| // for release builds too, so gate on the build flavor as well as the dir; CI | ||
| // debug builds and USE_SYSTEM_BUN have no dir next to the binary and skip. | ||
| const jsDir = join(dirname(bunExe()), "js"); | ||
| const osJsPath = join(jsDir, "node", "os.js"); | ||
| const hasDynamicJS = isDebug && fs.existsSync(osJsPath); | ||
|
|
||
| const SKEW_MESSAGE = "different codegen generation"; | ||
|
|
||
| async function requireOsInChild() { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", "const os = require('node:os'); console.log(typeof os.freemem)"], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode }; | ||
| } | ||
|
|
||
| describe.skipIf(!hasDynamicJS)("builtin JS hot-reload generation guard", () => { | ||
| test("a file from a different codegen generation is rejected with an actionable error", async () => { | ||
| const original = fs.readFileSync(osJsPath); | ||
| try { | ||
| let tampered = original.toString("latin1"); | ||
| // Simulate a codegen run that renumbered native-call IDs: shift the os | ||
| // binding's $lazy ID by one (dispatches to a different native function) | ||
| // and stamp the file as belonging to another generation. | ||
| tampered = tampered.replace(/bound\(@lazy\((\d+)\)\)/, (_, id) => `bound(@lazy(${Number(id) + 1}))`); | ||
| tampered = tampered.replace( | ||
| /(@bun-internal-module-generation=)[0-9a-f]+/, | ||
| "$1" + Buffer.alloc(16, "0").toString(), | ||
| ); | ||
| expect(tampered).not.toBe(original.toString("latin1")); | ||
| fs.writeFileSync(osJsPath, tampered, "latin1"); | ||
|
|
||
| const { stdout, stderr, exitCode } = await requireOsInChild(); | ||
| expect(stderr).toContain(SKEW_MESSAGE); | ||
| expect(stdout).not.toContain("function"); | ||
| expect(exitCode).not.toBe(0); | ||
| } finally { | ||
| fs.writeFileSync(osJsPath, original); | ||
| } | ||
| }); | ||
|
|
||
| test("an edited file with an intact generation stamp still hot-reloads", async () => { | ||
| const original = fs.readFileSync(osJsPath); | ||
| try { | ||
| const marker = "BUN_DEV_RELOAD_MARKER_1b2d"; | ||
| const edited = original.toString("latin1").replace('"use strict";', `"use strict";console.error("${marker}");`); | ||
| expect(edited).not.toBe(original.toString("latin1")); | ||
| fs.writeFileSync(osJsPath, edited, "latin1"); | ||
|
|
||
| const { stdout, stderr, exitCode } = await requireOsInChild(); | ||
| expect(stderr).toContain(marker); | ||
| expect(stdout).toContain("function"); | ||
| expect(exitCode).toBe(0); | ||
| } finally { | ||
| fs.writeFileSync(osJsPath, original); | ||
| } | ||
| }); | ||
|
|
||
| test("a valid stamp that is not the final line does not count", async () => { | ||
| const original = fs.readFileSync(osJsPath); | ||
| try { | ||
| // The valid stamp becomes a decoy: a foreign stamp follows it as the | ||
| // real last line, as if a different codegen run appended to the file. | ||
| const foreignStamp = "// @bun-internal-module-generation=" + Buffer.alloc(16, "0").toString() + "\n"; | ||
| fs.writeFileSync(osJsPath, Buffer.concat([original, Buffer.from(foreignStamp, "latin1")])); | ||
|
|
||
| const { stdout, stderr, exitCode } = await requireOsInChild(); | ||
| expect(stderr).toContain(SKEW_MESSAGE); | ||
| expect(stdout).not.toContain("function"); | ||
| expect(exitCode).not.toBe(0); | ||
| } finally { | ||
| fs.writeFileSync(osJsPath, original); | ||
| } | ||
| }); | ||
|
|
||
| test("a truncated file (codegen mid-write) is rejected, not misparsed", async () => { | ||
| const original = fs.readFileSync(osJsPath); | ||
| try { | ||
| // A partially-written file has no trailing generation stamp yet. | ||
| fs.writeFileSync(osJsPath, original.subarray(0, Math.floor(original.length / 2))); | ||
|
|
||
| const { stdout, stderr, exitCode } = await requireOsInChild(); | ||
| expect(stderr).toContain(SKEW_MESSAGE); | ||
| expect(stdout).not.toContain("function"); | ||
| expect(exitCode).not.toBe(0); | ||
| } finally { | ||
| fs.writeFileSync(osJsPath, original); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // Keep the file non-empty for runners without hot-reload support. | ||
| test.skipIf(hasDynamicJS)("builtin JS hot-reload not supported by this build", () => { | ||
| expect(hasDynamicJS).toBe(false); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.