Files
unkernel/test/jit.test.ts
Yubinghan 9d9327abf5 js: tell the JIT the memory is shared where the page hides SharedArrayBuffer (#173)
* js: tell the JIT the memory is shared where the page hides SharedArrayBuffer

A page that is not cross-origin isolated has no SharedArrayBuffer global,
yet an SMP kernel there (cpus > 1, run on one CPU) has a shared memory.
caps() left out CAP_SHARED, so every compiled module failed to
instantiate ("mismatch in shared state of memory") and the guest ran on
the interpreter only. Ask !(buffer instanceof ArrayBuffer), as compile()
does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* js: test for a shared memory in virtqueues without the SharedArrayBuffer global, as the JIT does

* ci: run test:jit with the SDK unit tests

---------

Co-authored-by: Pooya Parsa <pooya@pi0.io>
2026-12-04 23:19:05 +01:00

33 lines
1.3 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// The JIT host's capabilities (js/core/jit.ts), without a kernel.
//
// node --test test/jit.test.ts
import test from "node:test";
import assert from "node:assert/strict";
import { JitHost } from "../js/core/jit.ts";
const CAP_SHARED = 8;
function capsOf(memory: WebAssembly.Memory): number {
const jit = new JitHost({ memory: () => memory, memory64: () => false });
jit.attach({ __indirect_function_table: new WebAssembly.Table({ element: "anyfunc", initial: 1 }) });
return jit.caps();
}
test("caps: a shared memory is shared also where the page hides SharedArrayBuffer (not cross-origin isolated)", () => {
const g = globalThis as { SharedArrayBuffer?: SharedArrayBufferConstructor };
const sab = g.SharedArrayBuffer;
const shared = new WebAssembly.Memory({ initial: 1, maximum: 1, shared: true });
const plain = new WebAssembly.Memory({ initial: 1, maximum: 1 });
assert.equal(capsOf(shared) & CAP_SHARED, CAP_SHARED);
assert.equal(capsOf(plain) & CAP_SHARED, 0);
delete g.SharedArrayBuffer;
try {
assert.equal(capsOf(shared) & CAP_SHARED, CAP_SHARED, "JIT modules must import the memory as shared");
assert.equal(capsOf(plain) & CAP_SHARED, 0);
} finally {
g.SharedArrayBuffer = sab;
}
});