mirror of
https://github.com/oven-sh/bun
synced 2026-02-13 12:29:07 +00:00
fix(compile): ensure bytecode alignment accounts for section header (#26299)
## Summary Fixes bytecode alignment in standalone executables to prevent crashes when loading bytecode cache on Windows. The bytecode offset needs to be aligned such that when loaded at runtime, the bytecode pointer is 128-byte aligned. Previously, alignment was based on arbitrary memory addresses during compilation, which didn't account for the 8-byte section header prepended at runtime. This caused the bytecode to be misaligned, leading to segfaults in `JSC::CachedJSValue::decode` on Windows. ## Root Cause At runtime, embedded data starts 8 bytes after the PE/Mach-O section virtual address (which is page-aligned, hence 128-byte aligned). For bytecode at offset `O` to be aligned: ``` (section_va + 8 + O) % 128 == 0 => (8 + O) % 128 == 0 => O % 128 == 120 ``` The previous code used `std.mem.alignInSlice()` which found aligned addresses based on the compilation buffer's arbitrary address, not accounting for the 8-byte header offset at load time. ## Changes - **`src/StandaloneModuleGraph.zig`**: Calculate bytecode offset to satisfy `offset % 128 == 120` instead of using `alignInSlice` - **`test/regression/issue/26298.test.ts`**: Added regression tests for bytecode cache in standalone executables ## Test plan - [x] Added regression test `test/regression/issue/26298.test.ts` with 3 test cases - [x] Existing `HelloWorldBytecode` test passes - [x] Build succeeds Fixes #26298 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -429,16 +429,49 @@ pub const StandaloneModuleGraph = struct {
|
||||
|
||||
const bytecode: StringPointer = brk: {
|
||||
if (output_file.bytecode_index != std.math.maxInt(u32)) {
|
||||
// Use up to 256 byte alignment for bytecode
|
||||
// Not aligning it correctly will cause a runtime assertion error, or a segfault.
|
||||
// Bytecode alignment for JSC bytecode cache deserialization.
|
||||
// Not aligning correctly causes a runtime assertion error or segfault.
|
||||
//
|
||||
// PLATFORM-SPECIFIC ALIGNMENT:
|
||||
// - PE (Windows) and Mach-O (macOS): The module graph data is embedded in
|
||||
// a dedicated section with an 8-byte size header. At runtime, the section
|
||||
// is memory-mapped at a page-aligned address (hence 128-byte aligned).
|
||||
// The data buffer starts 8 bytes after the section start.
|
||||
// For bytecode at offset O to be 128-byte aligned:
|
||||
// (section_va + 8 + O) % 128 == 0
|
||||
// => O % 128 == 120
|
||||
//
|
||||
// - ELF (Linux): The module graph data is appended to the executable and
|
||||
// read into a heap-allocated buffer at runtime. The allocator provides
|
||||
// natural alignment, and there's no 8-byte section header offset.
|
||||
// However, using target_mod=120 is still safe because:
|
||||
// - If the buffer is 128-aligned: bytecode at offset 120 is at (128n + 120),
|
||||
// which when loaded at a 128-aligned address gives proper alignment.
|
||||
// - The extra 120 bytes of padding is acceptable overhead.
|
||||
//
|
||||
// This alignment strategy (target_mod=120) works for all platforms because
|
||||
// it's the worst-case offset needed for the 8-byte header scenario.
|
||||
const bytecode = output_files[output_file.bytecode_index].value.buffer.bytes;
|
||||
const aligned = std.mem.alignInSlice(string_builder.writable(), 128).?;
|
||||
@memcpy(aligned[0..bytecode.len], bytecode[0..bytecode.len]);
|
||||
const unaligned_space = aligned[bytecode.len..];
|
||||
const offset = @intFromPtr(aligned.ptr) - @intFromPtr(string_builder.ptr.?);
|
||||
const current_offset = string_builder.len;
|
||||
// Calculate padding so that (current_offset + padding) % 128 == 120
|
||||
// This accounts for the 8-byte section header on PE/Mach-O platforms.
|
||||
const target_mod: usize = 128 - @sizeOf(u64); // 120 = accounts for 8-byte header
|
||||
const current_mod = current_offset % 128;
|
||||
const padding = if (current_mod <= target_mod)
|
||||
target_mod - current_mod
|
||||
else
|
||||
128 - current_mod + target_mod;
|
||||
// Zero the padding bytes to ensure deterministic output
|
||||
const writable = string_builder.writable();
|
||||
@memset(writable[0..padding], 0);
|
||||
string_builder.len += padding;
|
||||
const aligned_offset = string_builder.len;
|
||||
const writable_after_padding = string_builder.writable();
|
||||
@memcpy(writable_after_padding[0..bytecode.len], bytecode[0..bytecode.len]);
|
||||
const unaligned_space = writable_after_padding[bytecode.len..];
|
||||
const len = bytecode.len + @min(unaligned_space.len, 128);
|
||||
string_builder.len += len;
|
||||
break :brk StringPointer{ .offset = @truncate(offset), .length = @truncate(len) };
|
||||
break :brk StringPointer{ .offset = @truncate(aligned_offset), .length = @truncate(len) };
|
||||
} else {
|
||||
break :brk .{};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user