ReviewOS

zig-utils/zig-js

WebAssembly threads: shared Memory backing and JavaScript API ownership

#285
Closed chris opened this 9 days ago ·
9 days ago

Parent: #265 Depends on: #284 Related: #1 Roadmap: #142, #134

Implement shared WebAssembly.Memory storage and its JavaScript embedding boundary.

Acceptance:

  • Represent shared memory with stable cross-thread backing and explicit maximum/ownership metadata.
  • Expose WebAssembly.Memory({shared:true}) through the correct SharedArrayBuffer identity and descriptor semantics.
  • Preserve backing identity and visibility across grow, imports, instances, Context Thread, and Worker boundaries.
  • Define destruction, registry, GC rooting, and concurrent grow/view lifetime without use-after-free or accidental store capture.
  • Add deterministic API, identity, growth, import/link, leak, and ownership tests.
9 days ago

Implemented on main in a62e0c5 and documented in e321f04.

The native store now distinguishes ordinary allocations from a stable refcounted shared slab reserved to the declared maximum. Shared grows are serialized, failure-atomic, fixed-address, and return unique previous page counts under contention. Imports reject sharedness mismatches.

The JS boundary follows the pinned Threads proposal: descriptor access is initial, maximum, shared; shared requires maximum; every successful grow publishes a new fixed-length SharedArrayBuffer; old buffers keep their original length without detaching and alias the same data. Structured-clone wire v2 preserves fixed/growable SAB view metadata without transporting a Context, store, or Memory owner.

Evidence on a62e0c5:

  • WebAssembly filter: 176/176 pass
  • Worker filter: 33/33 pass
  • structured-clone filter: 11/11 pass
  • focused SharedBufferStorage filter: 3/3 pass
  • all runs: 0 failures, 0 skips, 0 leaks

Witnesses cover eight racing native grows, backing lifetime after native owner destruction, JS descriptor and grow identity, defined/exported/imported memories, sharedness link mismatch, shared-realm Thread visibility, structured clone, and an isolated Worker mutating an old 64 KiB view after live memory reached 128 KiB.

Sign in to comment on this issue.