mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 05:12:29 +00:00
fix(module): prevent crash when resolving bun:main before entry_po… (#27027)
…int.generate() `ServerEntryPoint.source` defaults to `undefined`, and accessing its `.contents` or `.path.text` fields before `generate()` has been called causes a segfault. This happens when `bun:main` is resolved in contexts where `entry_point.generate()` is skipped (HTML entry points) or never called (test runner). Add a `generated` flag to `ServerEntryPoint` and guard both access sites: - `getHardcodedModule()` in ModuleLoader.zig (returns null instead of crashing) - `_resolve()` in VirtualMachine.zig (falls through to normal resolution) ### What does this PR do? ### How did you verify your code works? Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1140,14 +1140,14 @@ export fn Bun__runVirtualModule(globalObject: *JSGlobalObject, specifier_ptr: *c
|
||||
fn getHardcodedModule(jsc_vm: *VirtualMachine, specifier: bun.String, hardcoded: HardcodedModule) ?ResolvedSource {
|
||||
analytics.Features.builtin_modules.insert(hardcoded);
|
||||
return switch (hardcoded) {
|
||||
.@"bun:main" => .{
|
||||
.@"bun:main" => if (jsc_vm.entry_point.generated) .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneUTF8(jsc_vm.entry_point.source.contents),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier,
|
||||
.tag = .esm,
|
||||
.source_code_needs_deref = true,
|
||||
},
|
||||
} else null,
|
||||
.@"bun:internal-for-testing" => {
|
||||
if (!Environment.isDebug) {
|
||||
if (!is_allowed_to_use_internal_testing_apis)
|
||||
|
||||
@@ -1616,7 +1616,7 @@ fn _resolve(
|
||||
if (strings.eqlComptime(std.fs.path.basename(specifier), Runtime.Runtime.Imports.alt_name)) {
|
||||
ret.path = Runtime.Runtime.Imports.Name;
|
||||
return;
|
||||
} else if (strings.eqlComptime(specifier, main_file_name)) {
|
||||
} else if (strings.eqlComptime(specifier, main_file_name) and jsc_vm.entry_point.generated) {
|
||||
ret.result = null;
|
||||
ret.path = jsc_vm.entry_point.source.path.text;
|
||||
return;
|
||||
|
||||
@@ -150,6 +150,7 @@ pub const ClientEntryPoint = struct {
|
||||
|
||||
pub const ServerEntryPoint = struct {
|
||||
source: logger.Source = undefined,
|
||||
generated: bool = false,
|
||||
|
||||
pub fn generate(
|
||||
entry: *ServerEntryPoint,
|
||||
@@ -230,6 +231,7 @@ pub const ServerEntryPoint = struct {
|
||||
entry.source = logger.Source.initPathString(name, code);
|
||||
entry.source.path.text = name;
|
||||
entry.source.path.namespace = "server-entry";
|
||||
entry.generated = true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -634,3 +634,42 @@ test.concurrent("bun serve files with correct Content-Type headers", async () =>
|
||||
// The process will be automatically cleaned up by 'await using'
|
||||
}
|
||||
});
|
||||
|
||||
test("importing bun:main from HTML entry preload does not crash", async () => {
|
||||
const dir = tempDirWithFiles("html-entry-bun-main", {
|
||||
"index.html": /*html*/ `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Test</title></head>
|
||||
<body><h1>Hello</h1></body>
|
||||
</html>
|
||||
`,
|
||||
"preload.mjs": /*js*/ `
|
||||
try {
|
||||
await import("bun:main");
|
||||
} catch {}
|
||||
// Signal that preload ran successfully without crashing
|
||||
console.log("PRELOAD_OK");
|
||||
`,
|
||||
});
|
||||
|
||||
await using proc = Bun.spawn({
|
||||
cmd: [bunExe(), "--preload", "./preload.mjs", "index.html", "--port=0"],
|
||||
env: bunEnv,
|
||||
cwd: dir,
|
||||
stdout: "pipe",
|
||||
stderr: "pipe",
|
||||
});
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let text = "";
|
||||
for await (const chunk of proc.stdout) {
|
||||
text += decoder.decode(chunk, { stream: true });
|
||||
if (text.includes("http://")) break;
|
||||
}
|
||||
|
||||
expect(text).toContain("PRELOAD_OK");
|
||||
|
||||
proc.kill();
|
||||
await proc.exited;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user