Skip to content

sandbox Phase 3 — precompile the WASM module once, instantiate per invocation #3297

Description

@os-zhuang

Part 3 of #3275 — see its Phase 3 section for the full spec. Blocked by #3296 (Phase 2).

Verified API (quickjs-emscripten@0.32.0): newVariant(baseVariant, { wasmModule }) where wasmModule?: OrLoader<WebAssembly.Module>"Emscripten will instantiate the WebAssembly.Instance from this existing WebAssembly.Module." The compiled WebAssembly.Module is stateless bytecode; each newQuickJSWASMModule(variant) still allocates a fresh linear memory (isolation preserved) as long as wasmMemory is NOT also passed (that would share memory = the rejected model).

Change (packages/runtime/src/sandbox/quickjs-runner.ts), module-level once per process:

const wasmModule = await WebAssembly.compile(baseWasmBytes);      // compile ONCE
const FAST_VARIANT = newVariant(RELEASE_SYNC, { wasmModule });    // reuse across instantiations

then Phase 2's newQuickJSWASMModule()newQuickJSWASMModule(FAST_VARIANT).

  • Confirm how to obtain baseWasmBytes once for the RELEASE_SYNC variant (variant wasmLocation; CustomizeVariantOptions.wasmBinary). Fallback: the Emscripten instantiateWasm(imports, onSuccess) hook via emscriptenModulenew WebAssembly.Instance(cachedModule, imports) per call.
  • If clean bytes access proves awkward in 0.32, use the instantiateWasm fallback; if neither is clean, this phase is deferrable — Phase 2 already removes the asyncify cost.

Acceptance criteria

  • Compiled module cached once; per-invocation instantiate only.
  • Isolation invariant asserted (RSS soak): fresh instance ⇒ fresh linear memory; RSS returns on dispose across N invocations.
  • Bench shows the compile-cost removal vs Phase 2.
  • Sandbox suite green unchanged.

Do NOT take the shared-singleton-module route (see #3275 §REJECTED — cross-tenant blast radius + RSS ratchet).

Refs: #3275 (parent) · #3296 (Phase 2).

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions