Repository navigation
Disable arr_loc redirect caching in spins that may free RFC cells - #2289
EpsilonPhoenix wants to merge 2 commits into
Conversation
|
The fix is right: both new tests reproduce on main, and histogram's time doesn't change. We can't merge it yet, though, because
So this needs about 55 tokens of real simplification somewhere else in |
7edd5cc to
a4c2aa4
Compare
|
Thanks for the clear review! Rebased cleanly onto
Final |
facts_hot never gets a null type, row_val never sees a Bool cell, js_def never gets an intrinsic, and the JS lane never reads fuel. anf calls the existing ty_all. The emitted C and JS are byte-identical on every test.
User-visible failure
When compiled to native C, programs that drop or split an RFC-wrapped
Arrayinside a flat (spin_*) function and then allocate/wrap a newArrayof the same size class can silently read the wrong array's elements (Array.get) and silently corrupt another live array's backing storage (Array.set).This reproduces both in 100% pure, safe Bend (passing
bend --verdict, viarfc_sealwhenBLK_SHR == 1) and inArray.fork+Array.joincode.Root cause
Commit
ef66a7cc(#1155) caches the resolved data locationq{i}for eachboxparameterr{i}of aspin_*function at entry (Term h{i} = r{i};) and rewritesblk_loc(e.mem, a)inarr_locto:This assumes that any later
Arrayhandleawith the same 64-bitTermbits ash{i}still points toq{i}. Whenh{i}is an RFC-wrappedArray(term_rfc(h{i}) == true), that invariant breaks if thespin_*function (or aspin_*callee) freesr{i}:r{i}(term_sink,blk_free,blk_half,blk_node, or a freeingspin_*callee) returns its class-0 RFC redirect cellrto the lane's LIFO free list and its backing blockold_locto its size-class free list.padatold_loc, a new arraybatnew_loc, and wrapbin an RFC cell (rfc_sealorArray.fork), which pops the same class-0 cellr.b1now has identical 64-bitTermbits toh0, yet points tonew_loc != q0 (old_loc).arr_locmatchesb1 == h0and returns staleq0(pad's backing storage), causingArray.getto readpadandArray.setto overwritepad.This also occurs across self-tail-loops (
WL_AGAIN) when aspin_*drops itsArrayparameter and loops with a recycled RFC-wrapped handle (so merely restrictingarr_loctop === iis insufficient).Minimal change
bend2/comp.ts(term_same, commit 1): remove the redundant hand-written recursion overLam,App,Ctr, andMatthat duplicatedBend.term_lower, keeping the fast short-circuit onx === y || x.$ !== y.$ || (x.$ === "Ctr" && x.k !== y.k). Saves 156 tokens (65,989 -> 65,833) so the fix lands in the file's normal style with 87 tokens of headroom (65,913 / 66,000).bend2/comp.ts(arr_q,arr_loc,emit_spin,emit_native, commit 2):"free"fact perspin_*inc_units()'s existing fixed-point loop whenever aspin_*takes at least one"box"parameter (seg.ks.includes("box")) and either emits an array-RFC-freeing helper (/term_sink|blk_(free|half|node)/) or calls anotherspin_*inseg.refswithfact("free", r).fidtoarr_q(k, f)so spins withfact("free", f)omitTerm h{i} = r{i};/, u64 q${i}and always resolveblk_loc(e.mem, a)freshly (including when passingqinto a non-freeing leafspin_*).seg.ks.includes("box")is sound (a spin with no boxed parameters cannot decrement a caller's RFC refcount) and preserves#1155caching on hot non-freeing loops such asspin_array_holdandbench/runtime/histogram.maineverywhere except the spins that lose theirh/qcache.tests/reg/spin_array_reuse.bend(100% safe Bend, passes--verdict): covers direct drop (term_sink), self-tail-loop (WL_AGAIN), transitive freeingspin_*callee + non-freeing leaf callee,ANodematch (blk_half), andALeafmatch (blk_free), checking bothArray.getreads andArray.setnon-corruption ofpad.tests/reg/spin_array_fork_reuse.bend: covers theArray.fork+Array.joinreuse path.Before / after evidence
On
tests/reg/spin_array_reuse.bend(mainat3d5bf43d):padand overwritespadon all 5 cases):--verdict:ALL PROOFS CHECK; Bun JS, Node JS, native C-O3&-DBEND_POISON,--threads 1&--threads 4all match):On
tests/reg/spin_array_fork_reuse.bend:111333 333-O3&-DBEND_POISON,--threads 1&--threads 4):222333 111Hot-loop caching & output preservation:
tests/reg/spin_array_hold.bendandbench/runtime/histogram/main.bend: emitted C only changes on the freeing spins; all hot non-freeing loops (spin_bump,spin_tot_go,spin_swap,spin_col_go,spin_grp_go,spin_cols_go,spin_count_go,spin_zero_go,spin_fill_go,spin_sum_go) are byte-for-byte identical tomain.bench/runtime/lexer/main.bend: emitted C and JS are byte-for-byte identical tomain(and JS is byte-for-byte identical across all 36bench/runtime/*+match/u32/f32/nat/chartests).Validation & limits
bun 1.3.12,node v24.14.1,clang 23.1.5, Leanv4.34.0):bun gates/repo.ts:PASS: 56 / 56(bend2/comp.ts:65,913 / 66,000ttok,-76net frommain).tests/reg/spin_array_reuse.bend(--verdict, Bun, Node, C-O3&-DBEND_POISON,--threads 1&--threads 4),tests/reg/spin_array_fork_reuse.bend,tests/reg/spin_array_hold.bend, and all 20 runnabletests/run/*array*.bendtests on native C.gates/test.ts,gates/perf.ts), Metal, and CUDA device lanes.