Files
bchanot 343d6f4c1f feat(workspace): R2.4 core runtime — lazy load, poison on trap, offline re-instantiation, per-generation sheet handles
packages/core-wasm: browser-safe CoreRuntime (synchronous calls once ready;
a WebAssembly.RuntimeError poisons the instance, the call answers internal,
a fresh instance comes from the kept compiled module and a glue record
imported in advance; at most 3 recoveries per page; retry after a load
failure), SheetSession (handle rebuilt per instance generation, never
called on a dropped one; invalid_sheet without poisoning), Node source.
crates/wasm: SheetHandle (sheet held in the module memory). Two builds:
pkg/ shipped without trap exports (checked), pkg-proofs/ for the trap
tests; BUILD_STAMP (hash of the Rust sources) refused when missing or
stale by the vitest global setup instead of skipping. Failure envelope
carries a numeric found version; compact JS-number output.
2026-10-14 00:12:04 +02:00

52 lines
2.3 KiB
TypeScript

/**
* R0 proof: on wasm32 a Rust panic is a trap (`WebAssembly.RuntimeError`), the shadow
* stack overflow is a trap too (no silent corruption), and a trapped instance must never be
* reused: review III.3 rule 5 (poison and re-instantiate) rests on these three facts.
*
* Observed at R0 (Node 24, wasm-bindgen 0.2.129):
* - after a plain panic the same instance still answers calls, which is the dangerous case:
* the shadow stack pointer was never unwound, so later frames run in a shifted stack;
* - after a stack-exhaustion trap the next call on the same instance fails in the allocator
* (`memory access out of bounds`), deterministically;
* - a genuinely new instance (new module record, new memory) recovers in full.
*/
import { loadProofModule as loadCore } from '../src/node'
describe('panics and stack exhaustion (wasm)', () => {
it('turns a panic into a WebAssembly.RuntimeError', async () => {
const core = await loadCore()
expect(() => core.panic_for_test()).toThrow(WebAssembly.RuntimeError)
})
it('traps on shadow stack exhaustion instead of returning garbage', async () => {
const core = await loadCore()
expect(core.recurse_for_test(100)).toBe(100)
// 2 000 000 frames of at least 128 bytes exceed the 1 MiB shadow stack by far.
expect(() => core.recurse_for_test(2_000_000)).toThrow(
WebAssembly.RuntimeError,
)
})
it('leaves a stack-trapped instance unusable and recovers with a fresh instance', async () => {
const poisoned = await loadCore()
expect(() => poisoned.recurse_for_test(2_000_000)).toThrow(
WebAssembly.RuntimeError,
)
expect(() => poisoned.core_version()).toThrow(WebAssembly.RuntimeError)
const fresh = await loadCore()
expect(fresh.core_version()).toMatch(/^\d+\.\d+\.\d+$/)
expect(JSON.parse(fresh.call('pretty_json', '[1.0]'))).toEqual({
ok: true,
value: '[\n 1\n]',
})
})
it('may keep answering after a plain panic, which is why no instance is reused after any trap', async () => {
const poisoned = await loadCore()
expect(() => poisoned.panic_for_test()).toThrow(WebAssembly.RuntimeError)
// Not asserted either way: the instance's stack pointer is undefined from here on.
const fresh = await loadCore()
expect(fresh.core_version()).toMatch(/^\d+\.\d+\.\d+$/)
})
})