Files
2026-07-08 16:15:37 +02:00

191 lines
6.4 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { createPluginSandbox } from "./runtime.js";
import type { PluginManifest } from "./manifest.js";
/**
* Error-path coverage for the WASM sandbox. The happy path + traps are
* covered in runtime.test.ts; this file exercises the failure branches:
* instantiate failure, fuel exhaustion, out-of-bounds memory traps, and
* host-import exceptions (both Error and non-Error throws).
*/
const FIXTURE_MANIFEST: PluginManifest = {
schemaVersion: 1,
id: "de.hivegrid.test",
version: "1.0.0",
name: "Test",
entry: "plugin.wasm",
wasmSha256: "0".repeat(64),
signature: "0".repeat(128),
signedBy: "0".repeat(64),
allowedHosts: [],
capabilities: [],
hooks: [{ name: "noop", export: "noop" }],
};
/**
* Compiles cleanly but imports `env.missing_fn`, which the host never
* provides — instantiation fails with a LinkError.
*
* WAT:
* (module (import "env" "missing_fn" (func)) (func (export "noop")))
*/
const INSTANTIATE_FAIL_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x02, 0x12, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x0a, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6e, 0x67, 0x5f, 0x66, 0x6e, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00,
0x07, 0x08, 0x01, 0x04, 0x6e, 0x6f, 0x6f, 0x70, 0x00, 0x01,
0x0a, 0x04, 0x01, 0x02, 0x00, 0x0b,
]);
/**
* `oob` reads from address 1_000_000 with a single 64 KiB page mapped —
* the load traps with "memory access out of bounds".
*
* WAT:
* (module (memory 1)
* (func (export "oob") (result i32) i32.const 1000000 i32.load))
*/
const OOB_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x05, 0x01, 0x60, 0x00, 0x01, 0x7f,
0x03, 0x02, 0x01, 0x00,
0x05, 0x03, 0x01, 0x00, 0x01,
0x07, 0x07, 0x01, 0x03, 0x6f, 0x6f, 0x62, 0x00, 0x00,
0x0a, 0x0b, 0x01, 0x09, 0x00, 0x41, 0xc0, 0x84, 0x3d, 0x28, 0x02, 0x00, 0x0b,
]);
/**
* `callboom` invokes the imported `env.boom` host function — whatever
* `boom` throws propagates back through the WASM frame to the caller.
*
* WAT:
* (module (import "env" "boom" (func $boom))
* (func (export "callboom") call $boom))
*/
const CALLBOOM_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x02, 0x0c, 0x01, 0x03, 0x65, 0x6e, 0x76, 0x04, 0x62, 0x6f, 0x6f, 0x6d, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00,
0x07, 0x0c, 0x01, 0x08, 0x63, 0x61, 0x6c, 0x6c, 0x62, 0x6f, 0x6f, 0x6d, 0x00, 0x01,
0x0a, 0x06, 0x01, 0x04, 0x00, 0x10, 0x00, 0x0b,
]);
const VALID_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x06, 0x01, 0x60, 0x01, 0x7f, 0x01, 0x7f,
0x03, 0x02, 0x01, 0x00,
0x07, 0x0a, 0x01, 0x06, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x00, 0x00,
0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x41, 0x02, 0x6c, 0x0b,
]);
describe("createPluginSandbox error paths", () => {
test("compile_failed sandbox still honours close() and fuelRemaining()", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
manifest: FIXTURE_MANIFEST,
fuelLimit: 17,
});
const result = await sandbox.invoke("double", 1);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.reason).toBe("compile_failed");
// The compile-failed closure exposes a working close()/fuelRemaining().
expect(sandbox.fuelRemaining()).toBe(17);
expect(() => sandbox.close()).not.toThrow();
});
test("reports instantiate_failed when a required import is missing", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: INSTANTIATE_FAIL_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 9,
// Intentionally do NOT provide env.missing_fn → LinkError.
});
const result = await sandbox.invoke("noop", 0);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.reason).toBe("instantiate_failed");
expect(result.fuelUsed).toBe(0);
expect(result.message).toMatch(/missing_fn|callable|import/iu);
}
expect(sandbox.fuelRemaining()).toBe(9);
expect(() => sandbox.close()).not.toThrow();
});
test("returns fuel_exhausted once the per-invocation budget hits zero", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: VALID_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 1,
});
const first = await sandbox.invoke("double", 3);
expect(first.ok).toBe(true);
if (first.ok) expect(first.output).toBe(6);
expect(sandbox.fuelRemaining()).toBe(0);
const second = await sandbox.invoke("double", 4);
expect(second.ok).toBe(false);
if (!second.ok) expect(second.reason).toBe("fuel_exhausted");
});
test("classifies out-of-bounds memory traps as memory_limit_exceeded", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: OOB_WASM,
manifest: FIXTURE_MANIFEST,
memoryLimitMb: 1,
fuelLimit: 10,
});
const result = await sandbox.invoke("oob", 0);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.reason).toBe("memory_limit_exceeded");
expect(result.message).toMatch(/out of bounds|memory access/iu);
}
});
test("classifies a host-import Error throw as a trap with the error message", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: CALLBOOM_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 10,
hostImports: {
env: {
boom: () => {
throw new Error("host binding exploded");
},
},
},
});
const result = await sandbox.invoke("callboom", 0);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.reason).toBe("trap");
expect(result.message).toBe("host binding exploded");
}
});
test("stringifies a non-Error host-import throw before surfacing the trap", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: CALLBOOM_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 10,
hostImports: {
env: {
boom: () => {
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw "string-shaped failure";
},
},
},
});
const result = await sandbox.invoke("callboom", 0);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.reason).toBe("trap");
expect(result.message).toBe("string-shaped failure");
}
});
});