Repository navigation
[JSC] Bytecode cache: a payload records its size, and one shorter than that is a miss - #707
Conversation
…n that is a miss GenericCacheEntry gains the payload's total byte length, written by Encoder::release() once the last page is aligned. isUpToDate() rejects a payload whose Decoder is shorter than the recorded length (or that was never stamped), so a cache file cut short by a partial write or a truncated copy costs a parse instead of a read past the end of the buffer. Nothing else about the payload is inspected. decodeSourceCodeKey() gets the header-size guard the other two decode entry points already had. cachedTypesFormatRevision 9 -> 10.
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline finding, I also checked the mapped-file path in Encoder::release(): the size is stamped into the entry's page memory before releaseMapped() truncates and writes the pages, so the recorded length does reach the on-disk file; and the eval path passes a null slot only because no GenericCacheEntry is allocated for it, so nothing is left unstamped there. The new record-size static_asserts (16/48/48/28) follow from adding one uint32_t to all-4-byte fields and hold on every ABI.
Extended reasoning...
Findings are present, so this is only the ruled-out note. The ordering question for the mapped path was verified against Encoder::release() and releaseMapped() in Source/JavaScriptCore/runtime/CachedTypes.cpp: the slot write at line ~913 precedes the m_fileHandle.write(page.span()) loop at ~939, and the entry lives in the first page, so the stamp is persisted. The eval branch of the top-level encodeCodeBlock allocates no entry, so a null slot there is consistent. The size static_asserts are ABI-independent since every field of these records is a 4-byte integer or enum. The remaining concerns (the updatable jsc-shell cache path, the 4 GiB RELEASE_ASSERT, the layout-offset shift for external consumers being covered by the revision bump) are either the posted inline finding or were ruled out as non-issues, so the inline comment is what a human should weigh.
| if (m_cacheVersion != currentCacheVersion()) | ||
| return false; | ||
| // A payload cut short (a partial write, a truncated file) is not this payload. | ||
| if (!m_payloadSize || decoder.size() < m_payloadSize) |
There was a problem hiding this comment.
🟣 pre-existing, not blocking: Pre-existing: a jsc-shell disk cache that has grown by function updates and is then cut short still decodes past the end and crashes, exactly as on the base. The new check at CachedTypes.cpp:4871 compares against m_payloadSize, which is stamped at Encoder::release() for the initial payload only; the code blocks CachedBytecode::addFunctionUpdate appends later lie beyond it, so a file truncated anywhere in that appended region passes isUpToDate. Fix: bound every offset the decoder follows, or record the file's full size on commit, so a truncated appended region is also a miss rather than an out-of-bounds read; Bun never appends, so only the jsc shell's JSC_diskCachePath files are affected.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Extended reasoning...
jsc.cpp:1439-1441 encodes a lazily compiled function with encodeFunctionCodeBlock and calls addFunctionUpdate; that payload has no GenericCacheEntry and is not counted in the header. CachedBytecode.cpp:226-249 commitUpdates patches the base record's CachedWriteBarrier offset to point at the appended region and appends the payload after m_payload.size(). The header's m_payloadSize stays what Encoder::release() wrote at CachedTypes.cpp:915: the original payload size. If the file is later cut to a length between m_payloadSize and its full size, decodeCodeBlockImpl at CachedTypes.cpp:5107 passes the header guard, isUpToDate at CachedTypes.cpp:4871 sees decoder.size() >= m_payloadSize and returns true. The patched offset is then followed by decodeFunctionCodeBlock at CachedTypes.cpp:5203 via ptrForOffsetFromBase (CachedTypes.h:262), which has no bounds check, reading past the mapped buffer: the same SIGSEGV the PR describes for the header case. CachedTypes.cpp:1122 notes Bun never calls addFunctionUpdate, so this reaches only the jsc shell disk cache used by JSTests/stress/bytecode-cache-*…
Verification: pre-existing (the base at b54a473 already decodes any truncated file past its end by the same route; this change fixes the base-payload truncation but does not reach the appended region). Trigger: a jsc-shell disk cache (JSC_diskCachePath) written by a run that lazily compiled functions, so the file is base payload + appended function payloads, later cut short inside the appended region.…
|
Preview build of d20611b: |
…eBlock linking (#43616) ### What does this PR do? Bumps `WEBKIT_VERSION` from `ebd5a6145bf7` to `63a807e88ce0` (oven-sh/WebKit `main`). Three commits come in: - oven-sh/WebKit#677 — bytecode cache: the per-child payload damage check (`regionIsIntact`) and the eight always-on decoder options are gone; `CodeBlock::finishCreation` resolves each variable a block *reads* once (an 8-slot on-stack cache keyed on name/depth/type) instead of once per `resolve_scope` and again per `get_from_scope`; one-byte varints are read inline; `Decoder::offsetOf/size/ptrForOffsetFromBase` are plain pointer arithmetic. - oven-sh/WebKit#707 — a payload records its total size in its header and one shorter than that is a cache miss, which is what `regionIsIntact` used to provide for truncated `.jsc` files (`bundler_bytecode_portable` › "a .jsc that is truncated is a cache miss, not a crash"). - oven-sh/WebKit#693 — WTF: the thread suspend signal handler acts only on requests from `suspend()`/`resume()`. No Bun-side source changes: nothing in the tree referenced the removed options, and no JSC header Bun includes changed. This replaces #42923, whose `trusted` flag plumbing targets an API that #677 ended up deleting outright. ### Numbers A large command-line application (~2,000 ES modules) built with `bun build --compile --bytecode`, ThinLTO release builds, main-thread user-mode instructions from spawn until its first prompt is drawn, 10 interleaved runs: | | median | range | |---|---|---| | `ebd5a6145bf7` | 1212 M | 1203–1265 | | `b54a4733d8f7` | 1136 M | 1092–1148 | −6.3%. `--help` of the same application: 441 M → 387 M. Resident memory unchanged. The linking change applies to every CodeBlock (source-parsed code too), the decoder changes only to `--bytecode` / compile-cache payloads. ### How did you verify your code works? CI. Locally (debug+ASan): `bundler_compile` 85/85 and `bundler_compile_prelinked` 79/79 pass; `bundler_bytecode_portable`'s truncated-`.jsc` case is what found the need for #707 (heap-buffer-overflow on `b54a4733`, cache miss on `63a807e8`). --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
#677 removed
CachedCodeBlock::regionIsIntact(), which was also what turned a truncated disk-cache payload into a cache miss. Onmaina.bytecode-cache/.jscfile cut short after its header is decoded past the end of the buffer:Bun ships a test for exactly this (
bundler_bytecode_portable: "a .jsc that is truncated is a cache miss, not a crash"), which is how it showed up; under ASan it is a heap-buffer-overflow in the decoder.The fix keeps the payload uninspected and adds one O(1) comparison:
GenericCacheEntryrecords the payload's total byte length (m_payloadSize, 12 → 16 bytes).Encoder::release()writes it once the last page is aligned;encodeCodeBlock/encodeBuiltinFunctionhand it their entry's slot.isUpToDate()returns false when the Decoder is shorter than the recorded length, or the length was never stamped.decodeSourceCodeKey()gets the same header-size guarddecodeCodeBlockImpl()andisCachedBytecodeStillValid()already have.cachedTypesFormatRevision9 → 10; record-size static_asserts updated.Tested: the truncation matrix above against a
mainbuild with identical flags;JSTests/stress/bytecode-cache-*andlink-resolves-repeated-reads-of-one-variable.jspass.