← All reports

Shared Wasm memory caches refreshed without touching the JS wrapper

Component: JSC WebAssembly | 2bde8cf

Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h

m_memories[i].set(vm, this, value);
- // during initialization, this cannot be run in updateCachedMemories()
- updateCachedMemoryBaseSizePair(i);
- if (!i) {
- WTF::storeStoreFence();
- m_cachedMemory0Size = m_memories[i]->memory().size();
- m_wasmMemory = value->memory();
+ m_wasmMemories[i] = value->memory();
+ if (!i)
+ m_wasmMemory = m_wasmMemories[i];
+
+ value->memory().registerInstance(*this);
+ {
+ Locker locker { value->memory().handle().lock() };
+ cachedMemoryBaseSizePairs()[i] = { m_wasmMemories[i]->basePointer(), m_wasmMemories[i]->mappedCapacity() };
+ if (!i) {
+ WTF::storeStoreFence();
+ m_cachedMemory0Size = m_wasmMemories[i]->size();
+ }
}
- m_memories[i]->memory().registerInstance(*this);
...
void updateMatchingCachedMemoriesConcurrently(const SharedArrayBufferContents& grownMemory)
{
- if (!m_wasmMemory)
- return;
- for (unsigned i = 0; i < m_moduleInformation->memoryCount(); i++) {
- if (!m_memories[i] || m_memories[i]->memory().shared() != &grownMemory)
+ for (unsigned i = 0; i < m_wasmMemories.size(); ++i) {
+ if (!m_wasmMemories[i] || m_wasmMemories[i]->shared() != &grownMemory)
continue;

Any worker holding a shared WebAssembly.Memory can grow it. Every instance that imports the memory caches its base and size (m_cachedMemory0Size and the base/size pairs) so JIT'd Wasm code can bounds-check without reloading. When one thread grows the memory, it walks all registered instances and updates their caches. The JS-visible JSWebAssemblyMemory wrapper is a GC cell that another worker's heap may sweep, and Wasm::Memory uses non-thread-safe refcounting.

Previously the grow thread reloaded each instance's JSWebAssemblyMemory wrapper while another worker could be sweeping it. Each JSWebAssemblyInstance now keeps a per-slot Wasm::Memory in m_wasmMemories and refreshes from that. Setup registers the instance with the buffer first and only then writes the base/size pair and m_cachedMemory0Size under the handle lock. A grow that lands after registration therefore finds the instance, and the lock orders it against the cache write. The grow walk reads the RefPtr already in the slot instead of copying it.

The cached memory size is a bounds-check input for Wasm code, so closing this grow-vs-sweep lifetime race directly protects JIT bounds checks. The race is reachable by workers sharing a WebAssembly.Memory, which in browsers requires cross-origin isolation.

Hunt for other paths that still reach JSWebAssemblyMemory or copy a RefPtr<Wasm::Memory> from a non-owning thread, such as other grow/notify walks, instance teardown, and updateCachedMemories() callers. Under the new ordering, look for windows where an instance is registered but its cache is uninitialized, for readers that skip the handle lock, and for fence gaps left by the restructured storeStoreFence.

Then check the lifetime of m_wasmMemories entries against instance destruction or GC while the grow thread iterates the registered-instance list. Also check multi-memory modules (cachedMemoryBaseSizePairCount > 1), the slot-0 special case, and how BBQ/OMG and the different bounds-checking modes (versus signaling memory) inline or cache base/size.

The regression test covers only a single imported shared memory in slot 0. Multi-memory modules, exported (module-created) shared memories, and instance teardown racing a grow remain productive fuzz targets.