Files
faunosteo/packages/core-wasm/tests/panic.test.ts
T

53 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 { loadCore } from '../src/index'
import { coreBuilt } from './helpers/core'
describe.skipIf(!coreBuilt)('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+$/)
})
})