Repository navigation
Make Buffer read*/write* native functions with a DFG/FTL intrinsic - #35343
Conversation
|
Updated 12:12 AM PT - Aug 23rd, 2026
❌ @Jarred-Sumner, your commit 8815348 has 1 failures in
🧪 To try this PR locally: bunx bun-pr 35343That installs a local version of the PR into your bun-35343 --bun |
|
Found 1 issue this PR may fix:
🤖 Generated with Claude Code |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughChangesBuffer fixed-width and variable-width numeric accessors now use native JSC host functions and registered buffer accessor intrinsics. JavaScript implementations and obsolete builtin helpers are removed, with tests and benchmarks covering correctness, errors, receivers, JIT behavior, multiple buffers, and access patterns. Buffer accessor migration
WebKit build version
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
There was a problem hiding this comment.
Both earlier nits are addressed (9bc68f3 passes jsNumber(number) to OUT_OF_RANGE with a test for the writeInt16LE("40000") message; 922d63b removes writeU_Int8 and the dead checkBounds export). No new issues found. Deferring to a human given the scope — 32 hot-path accessors reimplemented in C++ with a new JSC intrinsic and a companion WebKit change.
Also checked: the registerBufferAccessor<T, LE, isWrite> template args line up 1:1 with each DEFINE_BUFFER_READ/WRITE instantiation; bufferAccessLoad/Store byte-flip only on BE and use WTF::unalignedLoad/Store; the write .length change from 1→2 matches Node.
Extended reasoning...
Overview
Rewrites all 32 fixed-width Buffer.prototype.read*/write* accessors (plus the 4 BigInt64 reads) from JS builtins that lazily attached a per-Buffer DataView into C++ host functions in JSBuffer.cpp, each tagged BufferAccessorIntrinsic and registered with JSC's new BufferAccessorRegistry so DFG/FTL inline them as bounds-checked loads/stores. Removes the now-dead $checkBufferRead global, writeU_Int8, and the checkBounds export. Adds tier-up correctness/error tests and a benchmark.
Follow-up on prior review
My two earlier inline nits were both addressed:
- 9bc68f3:
throwValueOutOfRangenow reports the coercedjsNumber(number), not the raw argument; awriteInt16LE("40000")→"Received 40000"assertion covers it. - 922d63b:
writeU_Int8deleted andcheckBoundsdropped from the default export (kept as an internal callee ofcheckInt).
The one candidate raised and refuted this run — write* .length moving from 1 to 2 — is correct: Node's own write* have .length === 2 (the JS builtin table's 1 was the pre-existing divergence).
Security risks
None user-facing. The memory-safety surface is the unaligned load/store on view->vector() + offset: the fast path guards offset32 >= 0 && offset32 + byteSize <= byteLength (no overflow — offset32 is a non-negative int32 widened to size_t, byteSize ≤ 8), and the slow path re-derives offset from bufferAccessCheckOffsetBounds which enforces 0 ≤ offset ≤ byteLength - byteSize in doubles before the cast. Detached buffers have byteLength() == 0, so byteLength < byteSize throws ERR_BUFFER_OUT_OF_BOUNDS before any storage is touched.
Level of scrutiny
High. This is a hot-path Node-compat rewrite in C++ that reimplements bounds/range validation from scratch, introduces a new JSC intrinsic contract (host-function behavior must exactly match what the JIT'd form OSR-exits to), depends on a companion WebKit PR, and touches raw typed-array storage. That combination warrants a maintainer's eyes even though the implementation reads correctly and the 622 existing + 62 Node parallel buffer tests pass.
Other factors
WTF::flipBytesis only applied whensizeof(Storage) > 1and the accessor is BE — Bun targets are all little-endian, so LE accessors are a straightunalignedLoad/Store.readFloat*/readDouble*route throughJSC::purifyNaN, matching DataView semantics for NaN-encoded JSValues.registerBufferAccessorsWithJSC()runs understd::call_oncefromJSBufferPrototype::finishCreation, so per-worker prototype creation doesn't double-register.- The
bufferWriteslow path preserves Node's odd per-width validation order (offset-then-value for 1 byte, value-then-offset for 2/4 bytes, offset-only for floats), which the comment documents.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@scripts/build/deps/webkit.ts`:
- Line 13: Update the WEBKIT_VERSION constant to use the merged WebKit commit
SHA instead of the temporary autobuild-preview-pr-330-be77ad70 tag, ensuring
prebuilt downloads remain available after the pull request closes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 4668b660-0b0a-47f9-8512-a03041a84680
📒 Files selected for processing (1)
scripts/build/deps/webkit.ts
There was a problem hiding this comment.
No new issues found — the last two ordering nits (BigInt writer receiver-vs-value/offset, and the Received undefined on the DataView branch) are addressed in 2a8c837 and pinned by tests. Deferring to a human because of scope: ~600 lines of new hot-path C++ in JSBuffer.cpp backed by a new JSC intrinsic, and WEBKIT_VERSION is pinned to the oven-sh/WebKit#330 preview build, so merge order with that PR needs a human call.
What was reviewed:
- All four accessor families (
bufferRead<T>,bufferWrite<T>,bufferReadVarWidth,bufferWriteVarWidth, and the four BigInt writers) now share the same offset-type → receiver → bounds validation order; the BigInt writers' deferred-receiver shape matchesbufferWrite<T>. validateOffsetBigInt64's new!viewHasLengthbranch reportsjsNumber(offsetD)so an omitted offset formats as0.- The
byteLength()→length()switch in the BigInt writers is intentional (element-count semantics, matchinglib/internal/buffer.jsand the fixed-width family) and is covered by theUint16Arrayreceiver test. registerBufferAccessorsWithJSC()template args match each host function's<T, isLittleEndian>instantiation one-for-one.
Extended reasoning...
Overview
This PR replaces ~40 Buffer.prototype.read*/write* JS builtins with C++ host functions in JSBuffer.cpp, each carrying BufferAccessorIntrinsic and registered via a new BufferAccessorRegistry so DFG/FTL can compile call sites into bounds-checked loads/stores. It deletes the $dataView/$checkBufferRead private machinery and internal/buffer.ts, updates the prototype hash table, and adds ~800 lines of tests (a JIT-behavior suite with a differential fuzzer against useJIT=0, plus tier-up coverage in buffer.test.js). WEBKIT_VERSION is bumped to a preview build of oven-sh/WebKit#330, which supplies the intrinsic and registry.
Security risks
None identified. The new host functions bounds-check via view->length() before every view->vector() access, use WTF::unalignedLoad/Store with explicit byte-swap, and all size/offset arithmetic widens to size_t/double before comparison. No user-controlled data reaches allocation or the filesystem. The toNumber coercion (which can run user JS) happens before any pointer is dereferenced, and RETURN_IF_EXCEPTION follows it.
Level of scrutiny
High. This is hot-path native code on one of the most-called Node APIs, with a new JIT intrinsic whose correctness depends on the host function and the registered DataViewData descriptor agreeing exactly (a mismatch would produce wrong results only after tier-up). It also pins the build to an unmerged WebKit PR's preview artifact — merging this before WebKit#330 lands (or without swapping the pin to a real hash) would leave main on a preview tag. That's a merge-ordering decision a maintainer should make.
Other factors
The PR has been through six rounds of review on argument-validation ordering in error paths (all nit-level, all resolved and pinned by tests verified against Node v26.5.0). This run's bug-hunting pass found nothing new. Test coverage is unusually thorough — differential fuzzing of every accessor against a JIT-off reference, cross-view aliasing / CSE ordering, resizable/growable receivers, >2GB/>4GB-byteOffset views, and prototype/instance shadowing after tier-up — and the JSC side ran the stress suite under 20 fuzzing configurations. Nonetheless the combination of new C++ template machinery, a new JSC intrinsic, and a cross-repo dependency is well past the bar for auto-approval.
The fixed-width accessors (readInt8 ... readDoubleBE, the BigInt64 reads, writeInt8 ... writeDoubleBE) were JS builtins that lazily created a hidden DataView on each Buffer (`this.$dataView ||= new DataView(...)`) and validated through $checkBufferRead / internal/buffer.js. They are now C++ host functions in JSBuffer.cpp with the same semantics (error codes, messages and the per-function argument-validation order of lib/internal/buffer.js), registered with JSC's BufferAccessorRegistry and carrying BufferAccessorIntrinsic so the DFG/FTL compile call sites into bounds-checked loads/stores on the receiver's storage and OSR-exit back to the host function for anything they don't speculate. The existing writeBigInt64* host functions get the intrinsic too. The variable-width accessors (readIntLE etc.) are unchanged; the now-unused $checkBufferRead global is removed. Adds tier-up coverage to test/js/node/buffer.test.js and a bench/snippets/buffer-read-write.mjs benchmark.
…on cleanups
- ERR_OUT_OF_RANGE("value") for write* now formats the coerced number (like Node
and the previous JS implementation), not the raw argument.
- Derive each accessor's registration descriptor from the same template arguments
the host function is instantiated with, instead of a hand-encoded flag list.
- Tighten the new tier-up tests: assert error codes, and count mismatches instead
of asserting per iteration (much faster under debug+ASAN).
- Trim over-long comments.
readIntLE/BE, readUIntLE/BE, writeIntLE/BE and writeUIntLE/BE (byteLength 1..6) become C++ host functions with the same lib/internal/buffer.js semantics, which removes the last users of the hidden `this.$dataView` and of internal/buffer.js (both deleted). They register with JSC as variable-width accessors, so a call site with a constant byteLength of 1, 2 or 4 compiles to the same bounds-checked load/store as the fixed-width methods; other widths stay on the host function.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Main moved WEBKIT_VERSION to aea1f010 (oven-sh/WebKit#330, the buffer accessor intrinsic that #35343 registers). oven-sh/WebKit#268 is rebased onto that commit, so its preview carries everything main's pin has plus the two async context fixes.
Buffer.prototype.read*/write*become JIT-inlined native functionsAll of the accessors (
readInt8…readDoubleBE, the BigInt64 reads/writes, and the variable-widthreadIntLE/BE,readUIntLE/BE,writeIntLE/BE,writeUIntLE/BE) move from JS builtins that lazily created a hiddenDataViewon each Buffer to C++ host functions inJSBuffer.cpp. Each is registered with JSC's newBufferAccessorRegistryand carriesBufferAccessorIntrinsic, so DFG/FTL compile call sites into a bounds-checked load/store on the receiver's storage; for the variable-width family, a call site whosebyteLengthargument is a constant 1, 2 or 4 gets the same node. Depends on oven-sh/WebKit#330 (WEBKIT_VERSIONcurrently points at that PR's preview build).The host functions are the single source of truth for behavior: they mirror
lib/internal/buffer.js'scheckBounds()/checkInt()/boundsError()(error codes, messages, and the per-function argument-validation order), and the JIT exits back to them for anything it doesn't speculate. The now-unused$checkBufferReadglobal, the$dataViewprivate name, andinternal/buffer.jsare removed.Benchmark: CI build of this PR (
bunx bun-pr 35343) vs Bun 1.4.0 vs Node 26.5.0, macOS arm64, ns per operation (bench/snippets/buffer-read-write.mjs):Writes moved the most because the old builtins bounds-checked each store by loading
this[offset]/this[offset+1]and comparing toundefined, then loaded the hidden$dataViewfield, then calledDataView.setX(which re-checks bounds); now it is one range check + one bounds check + the store, all hoistable in loops (the 1- and 2-byte value range checks are graph nodes visible to integer range analysis, and theoffset + byteSizereturn value is dead unless the caller uses it).Tests:
test/js/node/buffer.test.js(tier-up coverage),test/js/node/buffer-jit.test.ts(compile counts converge, per-exit host fallback, load/store ordering and cross-view aliasing, prototype/instance replacement after tier-up, resizable/growable receivers), plus Node'stest-buffer-*parallel suite, all on a debug/ASAN build. On the JSC side, the fivebuffer-accessor-jit*.jsstress tests pass under 20 fuzzing / stress configurations (randomizing / double / narrowing / widening prediction agents at several seeds, OSR-exit fuzzing at every check, AI-state validation, eager compilation, andcollectContinuously/forceGCSlowPathsGC stress).Linux x64 (2026-08-22)
CI build of this PR (
bunx bun-pr 4618381) vs the CI build of its merge-base on main (85deae0, the JS-builtin implementation) vs Node 26.3.0. 64-core x86_64 EC2 box, idle, pinned to 8 cores, 5 interleaved rounds per arm, median of the mitata averages. ns per accessor call (bench/snippets/buffer-read-write.mjs):Most cells land on a multiple of ~0.27 ns: at 4096 calls per sample the loop runs at one CPU cycle per iteration, so a 0.27 ns step is one cycle.
readUInt32BE(0.82 → 1.10) andreadInt32LE(0)(0.33 → 0.39) are one cycle slower on this box in every round.readFloatLEis within noise. Everything else is flat or faster; the writes and the many-buffers cases are where the old path paid for the hiddenDataView+undefinedbounds probes.Note the two arms differ in WebKit (
b7f217b4on main vs thepreview-pr-330build for this PR); that is unavoidable because the intrinsic lives in oven-sh/WebKit#330.perf statinstructions and cycles per call for the same accessors (earlier localrelease-localbuild, Xeon 8488C, process startup subtracted):readBigInt64LEis dominated by the BigInt arithmetic in the benchmark loop, so the accessor itself is within noise of the old path there.