Skip to content

Bytecode cache: build 1–3 character inline atoms from the slot's bytes, not from the loaded word - #519

Merged
Jarred-Sumner merged 1 commit into
mainfrom
claude/inline-atom-read-slot-bytes
Aug 26, 2026
Merged

Jarred-Sumner merged 1 commit into
mainfrom
claude/inline-atom-read-slot-bytes

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

What

Decoder::atomForInlineString took the 4-byte slot as a uint32_t and shifted each character out of the register into a std::array on the stack, then passed that to AtomStringImpl::add. The slot is already sitting in the payload as [tag|len<<2][c0][c1][c2], so take it as std::span<const uint8_t, 4> and hand add() bytes 1..length directly. Length 1 (SmallStrings) and the length-2 VM table index read the same bytes. No format change; encoder untouched. A static_assert records that the byte view assumes the little-endian word the encoder writes.

x64 before (3-char path, as shipped in bun 1.4.1 / win-x64):

mov  ebx, r8d
mov  byte ptr [rbp-0x20], bh      ; char0
mov  ecx, ebx ; shr ecx, 16 ; mov [rbp-0x1f], cl
shr  ebx, 24  ; mov [rbp-0x1e], bl
lea  rcx, [rbp-0x20] ; call AtomStringImpl::add

after:

lea  rsi, [slot+1]
call AtomStringImpl::add

Why

Field reports on the 1.4.1 --compile --bytecode --splitting payload: ReferenceError: \rzt is not defined at a position where the payload holds Rzt — and the same for dor, TYr, qms, Bqe, $Pt, and the global Set (\ret). Every case is a 3-character name whose first character became 0x0D, which is exactly the slot's tag byte for length 3, with characters 2–3 intact. In the code above that is the one mov m8, bh storing bl: same register, correct value in it (chars 2–3 prove that), no intervening instruction. Nothing at the source level produces that, and the .bun section is mapped read-only, so this looks like the execution environment (CPU or an x64 translation layer) mis-handling a legacy high-byte-register store. Reading the characters from memory takes that sequence off the path; if the signature survives the release that ships this, that theory is dead and we've lost nothing — the new code is shorter either way.

Verified

JSCOnly RelWithDebInfo, USE_BUN_JSC_ADDITIONS=ON:

  • JSTests/stress/bytecode-cache-*.js, class-fields-bytecode-cache.js, code-cache-incorrect-caching.js + 300 random stress files through bytecode-cache-test-helper.sh (encode run, then JSC_forceDiskCache decode run): 296 pass / 10 fail, the 10 fail identically on a build of main (flag-dependent tests run without their //@ flags).
  • Targeted script of 1/2/3-char vars, functions, params, string constants and 3-char globals (Set, Map, NaN): encode-run and forced-decode-run output identical.

…s, not from the loaded word

atomForInlineString took the slot as a uint32_t and shifted each character out of it into a
stack array before handing that to AtomStringImpl::add. The slot is already four bytes in the
payload — tag byte, then the characters — so pass it as a span and give add() bytes 1..length
directly: no per-character extraction, no temporary. The two-character table index and the
SmallStrings lookup read the same bytes.

Motivation is a field signature on the 1.4.1 --compile --bytecode payload (Windows only, a
handful of machines, repeatable per machine): `ReferenceError: \rzt is not defined` where the
payload holds `Rzt`, i.e. the atom's first character is the slot's tag byte (0x0D for length 3)
with characters 2-3 intact. In the shipped x64 code that is the single `mov m8, bh` that stores
character 0 behaving as `mov m8, bl`; nothing at the source level produces it. Reading the
characters from memory removes that instruction sequence from the path regardless of what
mis-executes it.
@coderabbitai

coderabbitai Bot commented Aug 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

  • Run on-demand review

On-demand reviews are free for the next 25 days. After that, they cost $0.25 per reviewed file.

Or wait 46 minutes for your next included review.

View limit details

Limit details: You’ve used the included review currently available. Your 80 included PR review attempts over the past 7 days set your current allowance at 1 review per hour.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8a437e16-2a08-44fe-9a78-820805d60002

📥 Commits

Reviewing files that changed from the base of the PR and between 7688227 and 016026c.

📒 Files selected for processing (2)
  • Source/JavaScriptCore/runtime/CachedTypes.cpp
  • Source/JavaScriptCore/runtime/CachedTypes.h

Comment @coderabbitai help to get the list of available commands.

@Jarred-Sumner
Jarred-Sumner merged commit 2da33d5 into main Aug 26, 2026
45 checks passed

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — semantics-preserving refactor from shift/mask to a byte-span view.

What was reviewed:

  • Verified against the encoder (tryEncodeInlineString): on little-endian, byte 0 is tag|len<<2 and bytes 1..3 are the characters, so slot[0]>>2 & 3 and slot.subspan(1).first(length) read exactly what the old shifts extracted; the static_assert pins that assumption.
  • Length-2 cache index characters[0] | characters[1]<<8 equals the old (packed>>8) & 0xffff; length is masked to ≤3 so .first(length) on the 3-byte subspan is always in bounds.
  • asByteSpan<Offset, 4>(m_offset) (Offset = int32_t) yields span<const uint8_t, 4> over the in-payload field; AtomStringImpl::add copies, so no lifetime concern, and the slot→entry rename removes the parameter shadow.
Extended reasoning...

Overview

The PR changes Decoder::atomForInlineString in Source/JavaScriptCore/runtime/CachedTypes.cpp (and its declaration in CachedTypes.h) to accept a std::span<const uint8_t, 4> view over the 4-byte inline-string slot instead of a uint32_t loaded via bit_cast. The single call site, VariableLengthObject::inlineString, now passes asByteSpan<Offset, sizeof(Offset)>(m_offset) (where Offset is int32_t). The encoder side (tryEncodeInlineString) is unchanged: it writes tag | len<<2 | c0<<8 | c1<<16 | c2<<24 bit-cast into m_offset, so on a little-endian host the byte layout is [tag|len<<2][c0][c1][c2]. The new decoder reads slot[0] for the length and slot[1..1+len) for the characters, which is byte-for-byte identical to the old shift/mask logic. A static_assert(std::endian::native == std::endian::little) documents and enforces the assumption. The local slot (cache-table entry reference) was renamed to entry to avoid shadowing the new parameter.

Security risks

None introduced. The bytecode cache decoder already trusts the payload it maps; this change does not widen that trust. length is masked with & 3, subspan(1) on a fixed-extent span of 4 yields a fixed-extent span of 3, and .first(length) with length ≤ 3 is always in bounds. Latin1Character is unsigned char, so the uint8_t span converts without reinterpretation. AtomStringImpl::add copies its input, so the span pointing into the cached object's storage does not escape. No new heap writes, no format change, no auth/crypto/permissions surface.

Level of scrutiny

Low-to-moderate. This is a ~20-line refactor confined to one function and one call site, with mechanically verifiable equivalence to the prior code on little-endian targets (the only targets JSC ships on, now enforced at compile time). The motivation — sidestepping a suspected legacy high-byte-register store miscompile/misexecution on some x64 Windows environments — does not affect correctness review: the new code is equivalent regardless of whether that theory holds.

Other factors

The repository's CODEOWNERS lists /Source/JavaScriptCore @ WebKit/jsc-reviewers, but that file is inherited from upstream WebKit, explicitly states contributors do not "own" components, and references an upstream team not applicable to this fork — so it is advisory reviewer routing rather than an ownership gate here. The PR description reports passing the bytecode-cache-* stress tests plus a targeted 1/2/3-char round-trip script under JSC_forceDiskCache. The bug-hunting run exited on dry_streak with no findings and no candidates ruled out.

@github-actions

Copy link
Copy Markdown

Preview Builds

Commit Release Date
016026c0 autobuild-preview-pr-519-016026c0 2026-08-26 19:58:29 UTC

Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Aug 26, 2026
Bytecode cache: build 1-3 character inline atoms from the slot's bytes,
not from the loaded word (oven-sh/WebKit#519).
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Aug 26, 2026
### What does this PR do?

Bumps WebKit from `76882271d74a` to `2da33d53e33e` — one commit:

- oven-sh/WebKit#519 — Bytecode cache: build 1-3 character inline atoms
from the slot's bytes, not from the loaded word

### How did you verify your code works?

Release `autobuild-2da33d53e33e71055d66f4b84537aa8cb0b0e99d` is
published with all 42 prebuilt assets; CI builds against it.
robobun added a commit to oven-sh/bun that referenced this pull request Aug 26, 2026
Main moved WEBKIT_VERSION to 2da33d53 (oven-sh/WebKit#519).
oven-sh/WebKit#268 is rebased onto that commit, so its preview carries
everything main's pin has plus the two async context fixes.
robobun added a commit to oven-sh/bun that referenced this pull request Aug 28, 2026
Main moved WEBKIT_VERSION to 2da33d53 (oven-sh/WebKit#519).
oven-sh/WebKit#268 is rebased onto that commit, so its preview carries
everything main's pin has plus the two async context fixes.
robobun added a commit to oven-sh/bun that referenced this pull request Aug 28, 2026
Main moved WEBKIT_VERSION to 2da33d53 (oven-sh/WebKit#519).
oven-sh/WebKit#268 is rebased onto that commit, so its preview carries
everything main's pin has plus the two async context fixes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant