Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions src/jsc/NodeCompileCache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct Entry {
/// 128-byte-aligned blob. JSC's bytecode decoder reads the blob in place and
/// requires the same alignment the standalone graph provides (see
/// StandaloneModuleGraph.rs "Bytecode alignment" note). Either a heap buffer
/// or a span inside a whole-file mapping.
/// or a span inside a mapping of the cache file.
struct AlignedBlob {
ptr: core::ptr::NonNull<u8>,
len: usize,
Expand All @@ -71,11 +71,15 @@ struct AlignedBlob {

enum Backing {
Heap,
/// `PROT_READ`/`MAP_PRIVATE` mapping of the whole cache file; `ptr` points
/// at [`blob_file_offset`] inside it, 128-aligned because the mapping base
/// is page-aligned. Safe against entry rewrites: writers go through
/// tmpfile + rename, so a replaced file's old inode stays live under the
/// mapping.
/// What is still mapped (`PROT_READ`/`MAP_PRIVATE`) of the cache file. The
/// file is mapped whole to validate it, then the pages in front of the one
/// holding [`blob_file_offset`] (header + stored code, never read again
/// once compared) are unmapped, leaving the tail from the last page
/// boundary at or before the blob; when the blob begins in the first page
/// or that partial unmap fails, this is still the whole file. `ptr` stays
/// 128-aligned because page boundaries are. Safe against entry rewrites:
/// writers go through tmpfile + rename, so a replaced file's old inode
/// stays live under the mapping.
Comment thread
robobun marked this conversation as resolved.
Resolved
Map {
base: core::ptr::NonNull<u8>,
map_len: usize,
Expand Down Expand Up @@ -110,6 +114,39 @@ impl AlignedBlob {
// SAFETY: `ptr` is valid for `len` bytes for the lifetime of `self`.
unsafe { core::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.len) }
}

/// Takes ownership of a validated whole-file mapping, keeping only the
/// pages from the blob onwards mapped (see [`Backing::Map`]). The stored
/// code in the unmapped prefix was paged in by the byte compare and would
/// otherwise stay resident next to the live source string for the rest of
/// the process.
///
/// # Safety
/// `(base, map_len)` must be a live mapping nothing else reads any more,
/// with `blob_off + blob_len <= map_len` and `base + blob_off` 128-aligned.
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
unsafe fn from_mapping(
base: core::ptr::NonNull<u8>,
map_len: usize,
blob_off: usize,
blob_len: usize,
) -> Self {
let prefix_len = blob_off - blob_off % bun_alloc::page_size();
// SAFETY: `prefix_len <= blob_off <= map_len` per the contract.
let (ptr, tail) = unsafe { (base.add(blob_off), base.add(prefix_len)) };
let (base, map_len) = if prefix_len != 0 && sys::munmap(base.as_ptr(), prefix_len).is_ok() {
(tail, map_len - prefix_len)
} else {
// The blob starts in the first page, or the kernel refused the
// partial unmap: keep owning the whole mapping and release it at
// once on drop.
Comment thread
robobun marked this conversation as resolved.
Resolved
(base, map_len)
};
Self {
ptr,
len: blob_len,
backing: Backing::Map { base, map_len },
}
}
}

impl Drop for AlignedBlob {
Expand Down Expand Up @@ -831,14 +868,11 @@ fn read_cache_file(state: &CacheState, key: u64, entry: &mut Entry, code: Option
});
let blob = if map_is_aligned {
let (base, map_len) = map_guard.take().expect("checked above");
// SAFETY: `blob_off + cache_size == map_len` was just validated.
let ptr =
unsafe { core::ptr::NonNull::new_unchecked(base.as_ptr().add(blob_off as usize)) };
AlignedBlob {
ptr,
len: cache_size as usize,
backing: Backing::Map { base, map_len },
}
// SAFETY: `blob_off + cache_size == map_len` and the blob's alignment
// were just validated, and `bytes`/`blob_bytes` (views of the whole
// mapping, part of which is unmapped here) are not used past this
// point.
unsafe { AlignedBlob::from_mapping(base, map_len, blob_off as usize, cache_size as usize) }
} else {
// No mapping, or the blob would be misaligned in it: copy to an
// aligned heap buffer instead (map_guard unmaps on return).
Expand Down
75 changes: 74 additions & 1 deletion test/js/node/module/node-module-module.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from "bun:test";
import fs from "fs";
import { bunEnv, bunExe, isWindows, ospath, tempDir } from "harness";
import { bunEnv, bunExe, isLinux, isWindows, ospath, tempDir } from "harness";
import Module, { _nodeModulePaths, builtinModules, createRequire, isBuiltin, wrap } from "module";
import path from "path";

Expand Down Expand Up @@ -216,6 +216,79 @@
expect(exitCode).toBe(0);
});

test.skipIf(!isLinux)("compile cache keeps only the bytecode pages of an accepted entry mapped", async () => {
// An accepted entry file is mmapped so JSC reads the bytecode in place. The
// stored source in front of the bytecode is only read by the byte compare
// (the module's live source holds the same text), so its pages are unmapped
// afterwards instead of staying resident for the rest of the process.
const sourceSize = 256 * 1024;
using dir = tempDir("compile-cache-unmap", {
// Spans several pages even with 64 KiB pages.
"big.js": `module.exports = "${Buffer.alloc(sourceSize, "x").toString()}";`,
// Prints the mappings (file offset + mapped length) of every file whose
// path contains argv[2].
"main.js": `
const length = require("./big.js").length;
const mapped = require("fs")
.readFileSync("/proc/self/maps", "utf8")
.split("\\n")
.filter(line => line.includes(process.argv[2]))
.map(line => {
const [range, , offset] = line.split(/\\s+/);
const [start, end] = range.split("-");
return { offset: parseInt(offset, 16), length: parseInt(end, 16) - parseInt(start, 16) };
});
console.log(JSON.stringify({ length, mapped }));
`,
});
const cacheDir = path.join(String(dir), "cc");
const env = { ...bunEnv, NODE_COMPILE_CACHE: cacheDir };
async function run(filter) {
await using proc = Bun.spawn({ cmd: [bunExe(), "main.js", filter], env, cwd: String(dir), stderr: "pipe" });
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
return JSON.parse(stdout);
}

// Misses populate the cache and leave nothing mapped.
expect(await run(cacheDir)).toEqual({ length: sourceSize, mapped: [] });

// big.js has the large entry. Its layout (NodeCompileCache.rs) is a 108-byte
// header whose second u32 is the stored source's size, the stored source,
// then the bytecode at the next multiple of 128.
const entries = [...new Bun.Glob("**/*").scanSync({ cwd: cacheDir, onlyFiles: true })]
.map(f => path.join(cacheDir, f))
.sort((a, b) => fs.statSync(b).size - fs.statSync(a).size);
expect(entries).toHaveLength(2);
const entry = fs.readFileSync(entries[0]);
const codeSize = entry.readUInt32LE(4);
expect(codeSize).toBeGreaterThanOrEqual(sourceSize);
const bytecodeOffset = Math.ceil((108 + codeSize) / 128) * 128;

Check warning on line 267 in test/js/node/module/node-module-module.test.js

View check run for this annotation

Claude / Claude Code Review

Test hardcodes wrong header size (108 instead of 76)

The test's `bytecodeOffset` computation and its comment use a 108-byte header, but `HEADER_SIZE` in `src/jsc/NodeCompileCache.rs` is `3 * 4 + 2 * HASH_SIZE = 76` bytes — the actual `blob_file_offset` is `(76 + code_size).next_multiple_of(128)`. The 32-byte error is masked today by the subsequent `roundDown(pageSize)`, but it's a latent flake: change `108` → `76` (or derive it as `3*4 + 2*32` with a reference to `HEADER_SIZE`) in both the comment and the computation. The PR description repeats th
Comment thread
robobun marked this conversation as resolved.
Resolved

// /proc/self/auxv is a list of (type, value) u64 pairs; AT_PAGESZ is type 6.
const auxv = fs.readFileSync("/proc/self/auxv");
let pageSize = 0;
for (let i = 0; i + 16 <= auxv.length; i += 16) {
if (auxv.readBigUInt64LE(i) === 6n) pageSize = Number(auxv.readBigUInt64LE(i + 8));
}
expect(pageSize).toBeGreaterThan(0);
const roundDown = n => n - (n % pageSize);
const roundUp = n => roundDown(n + pageSize - 1);

// Only the pages from the one the bytecode starts on through the end of
// the file stay mapped.
expect(await run(path.basename(entries[0]))).toEqual({
length: sourceSize,
mapped: [
{
offset: roundDown(bytecodeOffset),
length: roundUp(entry.length) - roundDown(bytecodeOffset),
},
],
});
});

const compileCacheEnv = { ...bunEnv };
delete compileCacheEnv.NODE_COMPILE_CACHE;
delete compileCacheEnv.NODE_COMPILE_CACHE_PORTABLE;
Expand Down
Loading