pcbjam/tests/jspi/jspi-stack.spec.ts
Viktor Vaczi 3f09a46ff5 jspi: migration phases 0-7 — build knob, scheduler shim, test successor suite
Toolchain: emsdk 6.0.6 (versions.sh; cache-hash keys on it). Build knob
PCBJAM_ASYNC_BACKEND=jspi|asyncify: build-kicad-target.sh links editors with
-sJSPI + -sJSPI_EXPORTS=@scripts/common/jspi-exports.txt + --pre-js
jspi-scheduler.js (no DYNCALLS, no post-link asyncify pipeline); wx build
stamps the backend and forces clean on flip or unknown provenance;
docker/build.sh passes the knob, seeds the emscripten ports cache from the
volume every launch, jspi postprocess = patch-env-shim only.

scripts/common/shims/jspi-scheduler.js: the JSPI successor scheduler —
token-wait registry, resume turnstile (one armed resume between engine
re-entries, SP swaps only at microtask boundaries), green-region spill
stacks (16-aligned tops), S1 embind mutator FIFO lane + parker wraps, S6
shutdown, libctx integration hooks (suspend/end/quarantine + g_current
arm/clear), SuspendError attributor, lost-wake + stuck-window watchdogs,
__wxWaitDump observability.

Embind: PARKER registrations get emscripten::async() under PCBJAM_JSPI
(wasm/bindings/pcbjam_async_policy.h). nanosleep yields route via the shim.

Tests: tests/asyncify -> tests/jspi successor suite (jspi-stack red/green
shadow-stack battery, jspi-coroutine MiniCoro harness, suspend-races
semantic scenarios + __wxWaitDump books coherence); projects jspi-firefox/
jspi-chrome (asyncify-webkit retired — no JSPI in WebKit); unconditional
Firefox JSPI pref; guard-beacons -> wait-beacons (+wxScheduler/libctxJspi
families); Makefile.wasm links test apps against JSPI with the shim as a
tracked link prerequisite.

Web: WasmTool setRo await + __wxWaitDump forensics, open-flow contained
promise, scheduler-shim.test.ts retargeted (8 green).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NDeBaKKhQztd8KiVtHuyXr
2026-08-13 07:06:24 +02:00

67 lines
2.7 KiB
TypeScript

import { test, expect } from '../e2e/utils/fixtures';
// The permanent regression tripwire for the JSPI shadow-stack hazard
// (emscripten #27364): JSPI switches the native stack per activation but NOT
// the C spill stack (__stack_pointer), so concurrently-suspended activations
// corrupt each other unless a discipline is applied. The harness
// (tests/apps/standalone/jspi-stack, browser battery in its index.html) runs:
//
// red/single mitigation OFF → MUST detect corruption (the
// harness can still see the bug; a silent red means
// the tripwire itself broke)
// green-copy/single per-suspension [sp, entrySp] snapshot+restore — the
// discipline jspi-scheduler.js applies to wx entries
// green-region/single per-activation stack region + SP swap — the
// discipline libcontext's JSPI backend applies to
// KiCad coroutines
// green-copy/pthread both disciplines again with cross-thread allocator
// green-region/pthread churn running during the parks
//
// Output contract per combo:
// [JSPI_STACK] SCENARIO <mode>/<variant> corruptA=.. corruptB=.. corruptNested=.. verdict=<RED|GREEN|UNEXPECTED>
const COMBOS = [
{ name: 'red/single', verdict: 'RED' },
{ name: 'green-copy/single', verdict: 'GREEN' },
{ name: 'green-region/single', verdict: 'GREEN' },
{ name: 'green-copy/pthread', verdict: 'GREEN' },
{ name: 'green-region/pthread', verdict: 'GREEN' },
];
test.describe('JSPI shadow-stack red/green battery', () => {
test('red detects corruption; both mitigations hold, incl. pthread churn', async ({
page,
testLogger,
}) => {
test.setTimeout(120000);
await page.goto('/standalone/jspi-stack/');
await expect
.poll(
() =>
testLogger.consoleLogs.find((l) => l.includes('[JSPI_STACK] DONE')) ??
testLogger.consoleLogs.find((l) => l.includes('[JSPI_STACK] FATAL')) ??
null,
{
timeout: 90000,
message: 'battery should emit DONE (FATAL/silence = harness wedge)',
},
)
.not.toBeNull();
const fatal = testLogger.consoleLogs.filter((l) => l.includes('[JSPI_STACK] FATAL'));
expect(fatal, `harness fatal: ${fatal.join(' || ')}`).toHaveLength(0);
for (const combo of COMBOS) {
const line = testLogger.consoleLogs.find((l) =>
l.includes(`[JSPI_STACK] SCENARIO ${combo.name} `),
);
expect.soft(line, `combo ${combo.name} should have run`).toBeTruthy();
if (line) {
expect
.soft(line, `combo ${combo.name} verdict`)
.toContain(`verdict=${combo.verdict}`);
}
}
});
});