@waffle-os/runtime 0.0.145 → 0.0.146
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -0
- package/browser/command-input.ts +86 -0
- package/browser/command-owner.ts +5 -0
- package/browser/display/guest.ts +2 -18
- package/browser/display/surface-info.ts +19 -0
- package/browser/operations/cells.ts +90 -0
- package/browser/operations/guest.ts +94 -0
- package/browser/operations/owner.ts +201 -0
- package/browser/operations/protocol.ts +16 -0
- package/browser/operations/types.ts +41 -0
- package/browser/operations/writer.ts +106 -0
- package/browser/preview1-worker.ts +8 -1
- package/browser/process-broker.ts +372 -22
- package/browser/process-start.ts +5 -2
- package/browser/run-command.ts +388 -0
- package/browser/shared-fs.ts +20 -3
- package/browser/worker-memory.ts +6 -0
- package/browser/worker.ts +121 -27
- package/dist/browser/command-input.js +82 -0
- package/dist/browser/command-owner.js +2 -0
- package/dist/browser/display/guest.js +2 -18
- package/dist/browser/display/surface-info.js +18 -0
- package/dist/browser/operations/cells.js +80 -0
- package/dist/browser/operations/guest.js +67 -0
- package/dist/browser/operations/owner.js +175 -0
- package/dist/browser/operations/protocol.js +0 -0
- package/dist/browser/operations/types.js +0 -0
- package/dist/browser/operations/writer.js +98 -0
- package/dist/browser/preview1-worker.js +4 -0
- package/dist/browser/process-broker.js +317 -18
- package/dist/browser/process-start.js +3 -0
- package/dist/browser/run-command.js +377 -0
- package/dist/browser/shared-fs.js +8 -3
- package/dist/browser/worker-memory.js +10 -1
- package/dist/browser/worker.js +83 -28
- package/dist/fs/client.js +81 -18
- package/dist/fs/resolve-observation.js +11 -0
- package/dist/fs/service.js +51 -4
- package/dist/proc/command-transfer.js +72 -0
- package/dist/proc/directory.js +9 -1
- package/dist/proc/intrinsics/host.js +4 -1
- package/dist/proc/intrinsics.js +1 -0
- package/dist/proc/pipes.js +37 -4
- package/dist/proc/registry.js +25 -14
- package/dist/proc/start-command.js +101 -0
- package/dist/proc/supervisor.js +34 -10
- package/dist/proc/worker-broker.js +86 -31
- package/dist/proc/worker-client.js +67 -31
- package/dist/programs/collection.js +2 -0
- package/dist/runtime.js +14 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +113 -17
- package/fs/resolve-observation.ts +25 -0
- package/fs/service.ts +63 -4
- package/index.ts +5 -0
- package/package.json +9 -1
- package/proc/command-transfer.ts +96 -0
- package/proc/directory.ts +10 -1
- package/proc/intrinsics/host.ts +5 -1
- package/proc/intrinsics.ts +1 -0
- package/proc/pipes.ts +39 -6
- package/proc/registry.ts +52 -17
- package/proc/start-command.ts +157 -0
- package/proc/supervisor.ts +34 -9
- package/proc/worker-broker.ts +100 -38
- package/proc/worker-client.ts +84 -31
- package/programs/collection.ts +4 -0
- package/runtime.ts +44 -3
- package/scripts/test-browser.ts +49 -29
- package/tests/browser-controls-discovery.test.ts +2 -10
- package/tests/browser-matrix.test.ts +105 -59
- package/tests/browser-navigation.test.ts +1 -1
- package/tests/browser-runner-lifecycle.ts +23 -13
- package/tests/command-input.test.ts +40 -0
- package/tests/command-scope.test.ts +422 -0
- package/tests/filesystem-client-intrinsics.test.ts +2 -0
- package/tests/filesystem-refcount.test.ts +2 -0
- package/tests/filesystem-resolve-observation.test.ts +185 -0
- package/tests/fixtures/command-bounded-admission.ts +31 -0
- package/tests/fixtures/command-disposal.ts +47 -0
- package/tests/fixtures/command-guests.ts +123 -0
- package/tests/fixtures/operation-fs-worker.ts +114 -0
- package/tests/fixtures/operation-observer-worker.ts +41 -0
- package/tests/fixtures/operation-page.ts +522 -0
- package/tests/fixtures/process-start-worker.ts +32 -0
- package/tests/link-browser-runner.ts +95 -22
- package/tests/node-exports.test.ts +29 -0
- package/tests/operation-diagnostics-browser.ts +192 -0
- package/tests/operation-diagnostics.test.ts +291 -0
- package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
- package/tests/page-runner/browser-controls-runner.test.ts +20 -0
- package/tests/page-runner/browser-controls-runner.ts +1 -0
- package/tests/page-runner/browser-controls-suite.ts +20 -3
- package/tests/page-runner/control-run.ts +9 -1
- package/tests/page-runner/driver.ts +2 -0
- package/tests/page-runner/playwright-driver.ts +1 -1
- package/tests/page-runner/suite-host.ts +26 -1
- package/tests/page-runner/suite-startup.test.ts +39 -0
- package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
- package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
- package/tests/page-runner/worker-capability-controls.ts +174 -15
- package/tests/pipe-reader-reuse-races.test.ts +44 -0
- package/tests/process-start-browser-runner.ts +12 -4
- package/tests/process-start-browser.ts +66 -5
- package/tests/program-build-tools.test.ts +1 -1
- package/tests/runtime-facade.test.ts +5 -0
- package/tests/start-command.test.ts +109 -0
- package/tests/threads-guest-fixture.ts +1 -1
- package/tests/threads-guest-module.ts +6 -3
- package/tests/threads-guest.test.ts +25 -0
- package/tests/threads-runtime-browser-runner.ts +36 -13
- package/tests/threads-runtime-browser.ts +231 -92
- package/tests/threads-runtime-cases.ts +18 -1
- package/tests/threads-runtime-cleanup.ts +5 -1
- package/tests/threads-runtime-fd-browser.ts +247 -0
- package/tests/threads-runtime-fd-control.ts +3 -385
- package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
- package/tests/threads-runtime-fd-module.ts +13 -2
- package/tests/threads-runtime-fd-service.ts +23 -2
- package/tests/threads-runtime-growth-worker.ts +61 -0
- package/tests/threads-runtime-host.ts +17 -4
- package/tests/threads-runtime-modules.ts +23 -5
- package/tests/threads-runtime-regression-controls.ts +386 -0
- package/tests/threads-runtime-regression-modules.ts +242 -0
- package/tests/threads-runtime-service-barrier.ts +8 -1
- package/tests/webkit-diagnostics.ts +125 -0
- package/tests/webkit-error-output.test.ts +118 -0
- package/tests/worker-memory-bootstrap.test.ts +19 -1
- package/tests/worker-memory-broker.test.ts +16 -8
- package/tooling/ci-paths.test.ts +329 -61
- package/tooling/private-authority.ts +14 -0
- package/tooling/program/program-transformer.json +1 -1
- package/tooling/tarball-smoke.ts +3 -1
|
@@ -13,6 +13,7 @@ export const fdLayout = {
|
|
|
13
13
|
held: 448,
|
|
14
14
|
replacement: 512,
|
|
15
15
|
after: 576,
|
|
16
|
+
status: [1024, 1056, 1088, 1120, 1152],
|
|
16
17
|
} as const;
|
|
17
18
|
|
|
18
19
|
function checked(instructions: number[]): number[] {
|
|
@@ -41,6 +42,10 @@ function pipe(byte: number, address: number): number[] {
|
|
|
41
42
|
return store(address + 32, i32(byte)).concat(checked(io("write", 1, address, 1)));
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
function stat(address: number): number[] {
|
|
46
|
+
return checked(i32(4).concat(i32(address), call(6)));
|
|
47
|
+
}
|
|
48
|
+
|
|
44
49
|
export function descriptorModule(selected: DescriptorCase): Uint8Array<ArrayBuffer> {
|
|
45
50
|
const p = fdLayout;
|
|
46
51
|
return guestModule({
|
|
@@ -51,24 +56,29 @@ export function descriptorModule(selected: DescriptorCase): Uint8Array<ArrayBuff
|
|
|
51
56
|
{ name: "fd_seek", params: [0x7f, 0x7e, 0x7f, 0x7f] },
|
|
52
57
|
{ name: "fd_tell", params: [0x7f, 0x7f] },
|
|
53
58
|
{ name: "fd_close", params: [0x7f] },
|
|
59
|
+
{ name: "fd_fdstat_get", params: [0x7f, 0x7f] },
|
|
54
60
|
],
|
|
55
|
-
initial:
|
|
61
|
+
initial: stat(p.status[0]).concat(
|
|
62
|
+
checked(io("read", 4, p.first, 2)),
|
|
56
63
|
tell(p.first + 16),
|
|
57
64
|
store(p.spawn, i32(1).concat(call(0))),
|
|
58
65
|
wait32(p.childReady),
|
|
66
|
+
stat(p.status[2]),
|
|
59
67
|
checked(io("read", 4, p.resumed, 2)),
|
|
60
68
|
tell(p.resumed + 16),
|
|
61
69
|
seek(-9n, p.resumed + 24),
|
|
62
70
|
signal(p.cursorReady),
|
|
63
71
|
store(p.held + 32, i32(0x5655)),
|
|
64
72
|
checked(io(selected, 4, p.held, 2)),
|
|
73
|
+
stat(p.status[4]),
|
|
65
74
|
tell(p.held + 16),
|
|
66
75
|
checked(io("read", 4, p.after, 1)),
|
|
67
76
|
tell(p.after + 16),
|
|
68
77
|
pipe(77, 704),
|
|
69
78
|
wait32(p.mainRelease),
|
|
70
79
|
),
|
|
71
|
-
secondary:
|
|
80
|
+
secondary: stat(p.status[1]).concat(
|
|
81
|
+
checked(io("read", 4, p.child, 2)),
|
|
72
82
|
tell(p.child + 16),
|
|
73
83
|
seek(1n, p.child + 24),
|
|
74
84
|
store(672, i32(0x7978)),
|
|
@@ -79,6 +89,7 @@ export function descriptorModule(selected: DescriptorCase): Uint8Array<ArrayBuff
|
|
|
79
89
|
checked(io("read", 0, 768, 1)),
|
|
80
90
|
pipe(80, 832),
|
|
81
91
|
checked(i32(4).concat(call(5))),
|
|
92
|
+
stat(p.status[3]),
|
|
82
93
|
tell(p.replacement + 16),
|
|
83
94
|
checked(io("read", 4, p.replacement, 2)),
|
|
84
95
|
store(928, i32(81)),
|
|
@@ -17,11 +17,14 @@ export type FdServiceEvent =
|
|
|
17
17
|
| { kind: "fd-rebound"; references: number };
|
|
18
18
|
|
|
19
19
|
let original: OpenDescription | undefined;
|
|
20
|
+
let file: OpenDescription | undefined;
|
|
21
|
+
let guestClose = false;
|
|
20
22
|
const close = DescriptorTable.prototype.close;
|
|
21
23
|
DescriptorTable.prototype.close = function (fd) {
|
|
24
|
+
if (!guestClose || fd !== 4 || original) return close.call(this, fd);
|
|
22
25
|
const before = this.get(fd);
|
|
23
26
|
const replacement = this.get(5);
|
|
24
|
-
if (
|
|
27
|
+
if (!before || !replacement) return close.call(this, fd);
|
|
25
28
|
original = before.description;
|
|
26
29
|
const result = close.call(this, fd);
|
|
27
30
|
if (original.references !== 1) throw new Error("delayed operation does not own the original pin");
|
|
@@ -29,10 +32,28 @@ DescriptorTable.prototype.close = function (fd) {
|
|
|
29
32
|
self.postMessage({ kind: "fd-rebound", references: original.references });
|
|
30
33
|
return result;
|
|
31
34
|
};
|
|
35
|
+
const get = DescriptorTable.prototype.get;
|
|
36
|
+
DescriptorTable.prototype.get = function (fd) {
|
|
37
|
+
const entry = get.call(this, fd);
|
|
38
|
+
if (fd === 4 && entry?.resource.kind === "file") file ??= entry.description;
|
|
39
|
+
return entry;
|
|
40
|
+
};
|
|
32
41
|
const submit = ThreadService.prototype.submit;
|
|
33
42
|
ThreadService.prototype.submit = function (call, request) {
|
|
34
43
|
self.postMessage({ kind: "fd-admitted", call, request });
|
|
35
|
-
|
|
44
|
+
guestClose = request.kind === "close";
|
|
45
|
+
let pending: Promise<ThreadResult>;
|
|
46
|
+
try {
|
|
47
|
+
pending = submit.call(this, call, request);
|
|
48
|
+
} finally {
|
|
49
|
+
guestClose = false;
|
|
50
|
+
}
|
|
51
|
+
return pending.then((result) => {
|
|
52
|
+
if (request.kind === "fdstat" && request.fd === 4 && call.thread.tid === 1 && !original) {
|
|
53
|
+
// The experimental guest import reads status. This host sets NONBLOCK between two reads.
|
|
54
|
+
const resource = file?.resource;
|
|
55
|
+
if (resource?.kind === "file") resource.fdflags = 4;
|
|
56
|
+
}
|
|
36
57
|
const pin = original?.resource;
|
|
37
58
|
self.postMessage({
|
|
38
59
|
kind: "fd-completed",
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bootstrapGuestWorker,
|
|
3
|
+
type GuestWorkerEvent,
|
|
4
|
+
type GuestWorkerRequest,
|
|
5
|
+
} from "../thread/guest-worker.js";
|
|
6
|
+
|
|
7
|
+
type WasmBufferConversion = { toResizableBuffer?(): unknown };
|
|
8
|
+
|
|
9
|
+
function resizableView(
|
|
10
|
+
memory: WebAssembly.Memory & WasmBufferConversion,
|
|
11
|
+
): { kind: "supported"; view: Uint8Array } | { kind: "missing" | "rejected" } {
|
|
12
|
+
if (typeof memory.toResizableBuffer !== "function") return { kind: "missing" };
|
|
13
|
+
let buffer: unknown;
|
|
14
|
+
try {
|
|
15
|
+
const clone: WasmBufferConversion = structuredClone(memory);
|
|
16
|
+
if (!clone.toResizableBuffer) return { kind: "missing" };
|
|
17
|
+
buffer = clone.toResizableBuffer();
|
|
18
|
+
} catch {
|
|
19
|
+
return { kind: "rejected" };
|
|
20
|
+
}
|
|
21
|
+
if (!(buffer instanceof SharedArrayBuffer) || !buffer.growable)
|
|
22
|
+
throw new Error("shared Wasm conversion did not return a growable SAB");
|
|
23
|
+
return { kind: "supported", view: new Uint8Array(buffer) };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface GrowthObservation {
|
|
27
|
+
kind: "growth-observed";
|
|
28
|
+
phase: "received" | "instantiated" | "returned";
|
|
29
|
+
fixed: number;
|
|
30
|
+
refreshed: number;
|
|
31
|
+
resizable: number | "missing" | "rejected";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
self.onmessage = ({ data }: MessageEvent<GuestWorkerRequest>) => {
|
|
35
|
+
self.onmessage = null;
|
|
36
|
+
const { memory, entry } = data;
|
|
37
|
+
if (entry.kind !== "secondary" || entry.startArg !== 2) {
|
|
38
|
+
void bootstrapGuestWorker(data, self);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const kept = new Uint8Array(memory.buffer);
|
|
42
|
+
const resizable = resizableView(memory);
|
|
43
|
+
const observe = (phase: GrowthObservation["phase"]) =>
|
|
44
|
+
self.postMessage({
|
|
45
|
+
kind: "growth-observed",
|
|
46
|
+
phase,
|
|
47
|
+
fixed: kept.byteLength,
|
|
48
|
+
refreshed: memory.buffer.byteLength,
|
|
49
|
+
resizable: resizable.kind === "supported" ? resizable.view.byteLength : resizable.kind,
|
|
50
|
+
} satisfies GrowthObservation);
|
|
51
|
+
observe("received");
|
|
52
|
+
void bootstrapGuestWorker(data, {
|
|
53
|
+
addEventListener: self.addEventListener.bind(self),
|
|
54
|
+
removeEventListener: self.removeEventListener.bind(self),
|
|
55
|
+
postMessage(event: GuestWorkerEvent) {
|
|
56
|
+
if (event.kind === "ready") observe("instantiated");
|
|
57
|
+
if (event.kind === "thread-returned") observe("returned");
|
|
58
|
+
self.postMessage(event);
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
};
|
|
@@ -2,7 +2,13 @@ import { BrowserProcessBroker } from "../browser/process-broker.js";
|
|
|
2
2
|
import { bytes, declareSharedMemoryPlatform } from "../memory/index.js";
|
|
3
3
|
import { createRuntime } from "../runtime.js";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
type ProcessFilesystem = ConstructorParameters<typeof BrowserProcessBroker>[0]["filesystem"];
|
|
6
|
+
|
|
7
|
+
export function createThreadRuntime(
|
|
8
|
+
local: boolean,
|
|
9
|
+
errors: string[],
|
|
10
|
+
fixture?: { workerURL: string | URL; connect(filesystem: ProcessFilesystem): void },
|
|
11
|
+
) {
|
|
6
12
|
return createRuntime({
|
|
7
13
|
sharedMemory: declareSharedMemoryPlatform({
|
|
8
14
|
kind: "bounded",
|
|
@@ -11,9 +17,16 @@ export function createThreadRuntime(local: boolean, errors: string[]) {
|
|
|
11
17
|
}),
|
|
12
18
|
filesystem: {
|
|
13
19
|
mode: "memory",
|
|
14
|
-
workerURL:
|
|
20
|
+
workerURL:
|
|
21
|
+
fixture?.workerURL ??
|
|
22
|
+
(local ? new URL("../browser/fs-worker.ts", import.meta.url) : "/fs-worker.js"),
|
|
23
|
+
},
|
|
24
|
+
processHost: (options) => {
|
|
25
|
+
fixture?.connect(options.filesystem);
|
|
26
|
+
return new BrowserProcessBroker({
|
|
27
|
+
...options,
|
|
28
|
+
onError: (error) => errors.push(String(error)),
|
|
29
|
+
});
|
|
15
30
|
},
|
|
16
|
-
processHost: (options) =>
|
|
17
|
-
new BrowserProcessBroker({ ...options, onError: (error) => errors.push(String(error)) }),
|
|
18
31
|
});
|
|
19
32
|
}
|
|
@@ -8,6 +8,16 @@ import {
|
|
|
8
8
|
call,
|
|
9
9
|
store,
|
|
10
10
|
} from "./threads-guest-module.js";
|
|
11
|
+
import type { BrowserRuntimeThreadCase } from "./threads-runtime-cases.js";
|
|
12
|
+
import { descriptorModule } from "./threads-runtime-fd-module.js";
|
|
13
|
+
import {
|
|
14
|
+
admissionThreadModule,
|
|
15
|
+
fatalThreadModule,
|
|
16
|
+
overlappingWaitModule,
|
|
17
|
+
receivingGrowthModule,
|
|
18
|
+
spawnImport,
|
|
19
|
+
threadIdModule,
|
|
20
|
+
} from "./threads-runtime-regression-modules.js";
|
|
11
21
|
const u32 = (value: number): number[] => {
|
|
12
22
|
const result: number[] = [];
|
|
13
23
|
do {
|
|
@@ -77,11 +87,19 @@ export function runtimeSurvivorModule(): Uint8Array<ArrayBuffer> {
|
|
|
77
87
|
);
|
|
78
88
|
}
|
|
79
89
|
|
|
80
|
-
export function generatedRuntimeThreadModule(
|
|
81
|
-
|
|
90
|
+
export function generatedRuntimeThreadModule(
|
|
91
|
+
selected: BrowserRuntimeThreadCase,
|
|
92
|
+
): Uint8Array<ArrayBuffer> {
|
|
93
|
+
if (selected === "fd-read") return descriptorModule("read");
|
|
94
|
+
if (selected === "fd-write") return descriptorModule("write");
|
|
95
|
+
if (selected === "fatal-exit") return fatalThreadModule();
|
|
96
|
+
if (selected === "admission") return admissionThreadModule();
|
|
97
|
+
if (selected === "tid-boundaries") return threadIdModule();
|
|
98
|
+
if (selected === "growth") return receivingGrowthModule();
|
|
99
|
+
if (selected === "overlapping-waits") return overlappingWaitModule();
|
|
82
100
|
if (selected === "join")
|
|
83
101
|
return guestModule({
|
|
84
|
-
imports: [
|
|
102
|
+
imports: [spawnImport, { name: "fd_write", params: [0x7f, 0x7f, 0x7f, 0x7f] }],
|
|
85
103
|
initial: i32(0x12345678).concat(call(0), [0x1a], wait32(0)),
|
|
86
104
|
secondary: store(256, i32(99)).concat(
|
|
87
105
|
store(128, i32(256)),
|
|
@@ -98,12 +116,12 @@ export function generatedRuntimeThreadModule(selected: string): Uint8Array<Array
|
|
|
98
116
|
});
|
|
99
117
|
if (selected === "child-exit")
|
|
100
118
|
return guestModule({
|
|
101
|
-
imports: [
|
|
119
|
+
imports: [spawnImport, { name: "proc_exit", params: [0x7f], result: false }],
|
|
102
120
|
initial: i32(0).concat(call(0), [0x1a], wait64(0)),
|
|
103
121
|
secondary: i32(0).concat(call(1)),
|
|
104
122
|
});
|
|
105
123
|
return guestModule({
|
|
106
|
-
imports: [
|
|
124
|
+
imports: [spawnImport],
|
|
107
125
|
initial:
|
|
108
126
|
selected === "cpu"
|
|
109
127
|
? [0x03, 0x40, 0x0c, 0, 0x0b]
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { readWaitRegistration } from "../interrupt/wait.js";
|
|
2
|
+
import type { GuestWorkerEvent, GuestWorkerRequest } from "../thread/guest-worker.js";
|
|
3
|
+
import type { RuntimeThreadOwner } from "../thread/runtime-owner.js";
|
|
4
|
+
import type { GrowthObservation } from "./threads-runtime-growth-worker.js";
|
|
5
|
+
import {
|
|
6
|
+
admissionLayout,
|
|
7
|
+
fatalLayout,
|
|
8
|
+
fatalReadSentinels,
|
|
9
|
+
growthLayout,
|
|
10
|
+
maxThreadId,
|
|
11
|
+
overlapLayout,
|
|
12
|
+
tidLayout,
|
|
13
|
+
} from "./threads-runtime-regression-modules.js";
|
|
14
|
+
|
|
15
|
+
export function check(value: unknown, label: string): asserts value {
|
|
16
|
+
if (!value) throw new Error(label);
|
|
17
|
+
}
|
|
18
|
+
export async function until(predicate: () => boolean, label = "thread observation"): Promise<void> {
|
|
19
|
+
let timer: ReturnType<typeof setInterval>;
|
|
20
|
+
await deadline(
|
|
21
|
+
new Promise<void>((resolve, reject) => {
|
|
22
|
+
const poll = () => {
|
|
23
|
+
try {
|
|
24
|
+
if (predicate()) resolve();
|
|
25
|
+
} catch (error) {
|
|
26
|
+
reject(error);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
timer = setInterval(poll, 5);
|
|
30
|
+
poll();
|
|
31
|
+
}),
|
|
32
|
+
label,
|
|
33
|
+
).finally(() => clearInterval(timer));
|
|
34
|
+
}
|
|
35
|
+
export function deadline<T>(promise: Promise<T>, label: string): Promise<T> {
|
|
36
|
+
let timer: ReturnType<typeof setTimeout>;
|
|
37
|
+
return Promise.race([
|
|
38
|
+
promise,
|
|
39
|
+
new Promise<never>((_, reject) => {
|
|
40
|
+
timer = setTimeout(() => reject(new Error(label)), 15000);
|
|
41
|
+
}),
|
|
42
|
+
]).finally(() => clearTimeout(timer));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type GuestObservation = {
|
|
46
|
+
readonly worker: Worker;
|
|
47
|
+
readonly request: GuestWorkerRequest;
|
|
48
|
+
readonly handler: Worker["onmessage"];
|
|
49
|
+
readonly events: { event: GuestWorkerEvent; at: number }[];
|
|
50
|
+
enters: number;
|
|
51
|
+
};
|
|
52
|
+
type ServiceRead = { tid: number; pid: number };
|
|
53
|
+
type ObservedMessage =
|
|
54
|
+
| GuestWorkerEvent
|
|
55
|
+
| GrowthObservation
|
|
56
|
+
| ({ kind: "fixture-stdin-pending" | "fixture-stdin-settled" } & ServiceRead);
|
|
57
|
+
export class ThreadRegressionObservations {
|
|
58
|
+
readonly guests = new Map<number, GuestObservation>();
|
|
59
|
+
readonly growth: GrowthObservation[] = [];
|
|
60
|
+
stdin: ServiceRead | undefined;
|
|
61
|
+
stdinSettled: ServiceRead | undefined;
|
|
62
|
+
private memory: WebAssembly.Memory | undefined;
|
|
63
|
+
private guestConstructionCount = 0;
|
|
64
|
+
constructor(private readonly stopped: ReadonlySet<Worker>) {}
|
|
65
|
+
get guestConstructions(): number {
|
|
66
|
+
return this.guestConstructionCount;
|
|
67
|
+
}
|
|
68
|
+
attach(worker: Worker, target: string): void {
|
|
69
|
+
const guest = /guest-bootstrap|thread-guest|thread-growth/.test(target);
|
|
70
|
+
if (guest) this.guestConstructionCount++;
|
|
71
|
+
if (!guest && !target.includes("thread-barrier")) return;
|
|
72
|
+
worker.addEventListener("message", ({ data }: MessageEvent<ObservedMessage>) => {
|
|
73
|
+
if (!data || typeof data !== "object" || !("kind" in data)) return;
|
|
74
|
+
if (data.kind === "fixture-stdin-pending") {
|
|
75
|
+
this.stdin = data;
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (data.kind === "fixture-stdin-settled") {
|
|
79
|
+
this.stdinSettled = data;
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (data.kind === "growth-observed") {
|
|
83
|
+
this.growth.push(data);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if ("thread" in data)
|
|
87
|
+
this.guests.get(data.thread.tid)?.events.push({ event: data, at: performance.now() });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
post(worker: Worker, message: unknown): void {
|
|
91
|
+
if (!message || typeof message !== "object") return;
|
|
92
|
+
if ("module" in message && "entry" in message && "waits" in message) {
|
|
93
|
+
const request = message as GuestWorkerRequest;
|
|
94
|
+
this.memory = request.memory;
|
|
95
|
+
this.guests.set(request.thread.tid, {
|
|
96
|
+
request,
|
|
97
|
+
worker,
|
|
98
|
+
handler: worker.onmessage,
|
|
99
|
+
events: [],
|
|
100
|
+
enters: 0,
|
|
101
|
+
});
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (!("kind" in message) || message.kind !== "enter") return;
|
|
105
|
+
const guest = Array.from(this.guests.values()).find((item) => item.worker === worker);
|
|
106
|
+
if (guest) guest.enters++;
|
|
107
|
+
}
|
|
108
|
+
word(address: number): number {
|
|
109
|
+
return Atomics.load(this.words(), address / 4);
|
|
110
|
+
}
|
|
111
|
+
signal(address: number, value = 1): void {
|
|
112
|
+
const words = this.words();
|
|
113
|
+
Atomics.store(words, address / 4, value);
|
|
114
|
+
Atomics.notify(words, address / 4);
|
|
115
|
+
}
|
|
116
|
+
wake(address: number, width: 4 | 8): number {
|
|
117
|
+
const words = this.words();
|
|
118
|
+
return width === 4
|
|
119
|
+
? Atomics.notify(words, address / width, 1)
|
|
120
|
+
: Atomics.notify(new BigInt64Array(words.buffer), address / width, 1);
|
|
121
|
+
}
|
|
122
|
+
waiting(tid: number, address: number, width: 4 | 8): boolean {
|
|
123
|
+
const guest = this.guests.get(tid);
|
|
124
|
+
if (!guest) return false;
|
|
125
|
+
const current = readWaitRegistration(new Int32Array(guest.request.waits));
|
|
126
|
+
return current?.address === address && current.width === width;
|
|
127
|
+
}
|
|
128
|
+
event(tid: number, kind: GuestWorkerEvent["kind"]) {
|
|
129
|
+
return this.guests.get(tid)?.events.find((item) => item.event.kind === kind);
|
|
130
|
+
}
|
|
131
|
+
retired(tid: number): boolean {
|
|
132
|
+
const guest = this.guests.get(tid);
|
|
133
|
+
return !!guest && this.stopped.has(guest.worker);
|
|
134
|
+
}
|
|
135
|
+
entries(tid: number): number | undefined {
|
|
136
|
+
return this.guests.get(tid)?.enters;
|
|
137
|
+
}
|
|
138
|
+
private words() {
|
|
139
|
+
check(this.memory, "missing guest memory");
|
|
140
|
+
return new Int32Array(this.memory.buffer);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type Observations = ThreadRegressionObservations;
|
|
145
|
+
export async function releaseFatalThread(control: Observations, pid: number): Promise<void> {
|
|
146
|
+
const p = fatalLayout;
|
|
147
|
+
await until(
|
|
148
|
+
() =>
|
|
149
|
+
control.stdin?.tid === 2 &&
|
|
150
|
+
control.waiting(3, p.timer, 8) &&
|
|
151
|
+
control.waiting(4, p.exit, 4) &&
|
|
152
|
+
control.waiting(1, p.main, 4),
|
|
153
|
+
"same-PID stdin, finite wait and exit barriers",
|
|
154
|
+
);
|
|
155
|
+
check(control.stdin?.pid === pid && !control.stdinSettled, "same-PID stdin was still pending");
|
|
156
|
+
checkFatalWrites(control);
|
|
157
|
+
control.signal(p.exit);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function checkFatalWrites(control: Observations): void {
|
|
161
|
+
const p = fatalLayout;
|
|
162
|
+
check(
|
|
163
|
+
control.word(p.bytes) === fatalReadSentinels.payload &&
|
|
164
|
+
control.word(p.count) === fatalReadSentinels.count,
|
|
165
|
+
"terminal stdin call wrote to the guest",
|
|
166
|
+
);
|
|
167
|
+
for (const address of [p.sentinels, p.sentinels + 4, p.sentinels + 8, p.sentinels + 12])
|
|
168
|
+
check(control.word(address) === 0, `terminal guest published a write at ${address}`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function checkFatalRetirement(control: Observations, pid: number) {
|
|
172
|
+
const tids = [1, 2, 3, 4];
|
|
173
|
+
await until(
|
|
174
|
+
() => tids.every((tid) => control.retired(tid) && control.event(tid, "host-idle")),
|
|
175
|
+
"same-PID native Worker stops",
|
|
176
|
+
);
|
|
177
|
+
checkFatalWrites(control);
|
|
178
|
+
check(
|
|
179
|
+
control.stdinSettled?.pid === pid && control.stdinSettled.tid === 2,
|
|
180
|
+
"drain settled the same-PID stdin service call",
|
|
181
|
+
);
|
|
182
|
+
check(
|
|
183
|
+
control.guests.size === tids.length &&
|
|
184
|
+
tids.every((tid) => control.guests.get(tid)?.request.thread.pid === pid),
|
|
185
|
+
"each same-PID guest must become idle and stop",
|
|
186
|
+
);
|
|
187
|
+
return { stdinTid: control.stdin?.tid, timedTid: 3, retiredTids: tids };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export async function runOverlappingWaits(control: Observations) {
|
|
191
|
+
const p = overlapLayout;
|
|
192
|
+
const tids = [2, 3] as const;
|
|
193
|
+
await until(() => control.waiting(2, p.waits[0], 4), "wait32 armed before launching wait64");
|
|
194
|
+
control.signal(p.launchSecond);
|
|
195
|
+
await until(
|
|
196
|
+
() => control.waiting(2, p.waits[0], 4) && control.waiting(3, p.waits[1], 8),
|
|
197
|
+
"overlapping native wait32 and wait64 registrations",
|
|
198
|
+
);
|
|
199
|
+
control.signal(p.canaryGate);
|
|
200
|
+
await until(() => control.word(p.canaryCount) === 1, "canonical fd_write during both waits");
|
|
201
|
+
check(
|
|
202
|
+
control.waiting(2, p.waits[0], 4) && control.waiting(3, p.waits[1], 8),
|
|
203
|
+
"both waits must still be live after the canonical service reply",
|
|
204
|
+
);
|
|
205
|
+
await until(() => control.wake(p.waits[1], 8) === 1, "wake the parked wait64");
|
|
206
|
+
await until(() => control.retired(3), "later wait64 completes while wait32 stays parked");
|
|
207
|
+
check(control.waiting(2, p.waits[0], 4), "wait32 remained parked until wait64 completed");
|
|
208
|
+
await until(() => control.wake(p.waits[0], 4) === 1, "wake the parked wait32");
|
|
209
|
+
await until(() => control.retired(2), "ordinary return of the earlier waiter");
|
|
210
|
+
const results = p.results.map((address) => [control.word(address), control.word(address + 4)]);
|
|
211
|
+
check(
|
|
212
|
+
results.every(([notified, timedOut]) => notified === 0 && timedOut === 2),
|
|
213
|
+
"notify and timeout results",
|
|
214
|
+
);
|
|
215
|
+
const durationsMs = tids.map((tid) => {
|
|
216
|
+
const armed = control.event(tid, "wait-registered");
|
|
217
|
+
const cleared = control.event(tid, "wait-cleared");
|
|
218
|
+
check(armed && cleared, "finite wait registration and clear observers");
|
|
219
|
+
return cleared.at - armed.at;
|
|
220
|
+
});
|
|
221
|
+
control.signal(p.finish);
|
|
222
|
+
return { results, durationsMs };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export async function runThreadAdmission(control: Observations) {
|
|
226
|
+
const p = admissionLayout;
|
|
227
|
+
await until(() => control.waiting(4, p.startGate, 4), "fourth thread pending in Wasm start");
|
|
228
|
+
check(
|
|
229
|
+
control.guestConstructions === 4 && control.guests.size === 4 && !control.event(4, "ready"),
|
|
230
|
+
"main and pending thread count at cap",
|
|
231
|
+
);
|
|
232
|
+
control.signal(p.firstRefusal);
|
|
233
|
+
await until(
|
|
234
|
+
() => control.word(p.pendingResult) !== 0,
|
|
235
|
+
"spawn response with a pending fourth thread",
|
|
236
|
+
);
|
|
237
|
+
check(
|
|
238
|
+
control.word(p.pendingResult) === -6 && control.guestConstructions === 4,
|
|
239
|
+
"signed pending refusal does not construct a Worker",
|
|
240
|
+
);
|
|
241
|
+
control.signal(p.startGate);
|
|
242
|
+
await until(() => control.word(p.admitted) === 4, "fourth thread accepted after ready");
|
|
243
|
+
control.signal(p.secondRefusal);
|
|
244
|
+
await until(
|
|
245
|
+
() => control.word(p.runningResult) !== 0,
|
|
246
|
+
"spawn response with a running fourth thread",
|
|
247
|
+
);
|
|
248
|
+
check(
|
|
249
|
+
control.word(p.runningResult) === -6 &&
|
|
250
|
+
control.guests.size === 4 &&
|
|
251
|
+
control.guestConstructions === 4,
|
|
252
|
+
"signed refusal at cap without a worker",
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
control.signal(p.returnGate);
|
|
256
|
+
await until(() => control.retired(3), "ordinary return releases exactly one slot");
|
|
257
|
+
check(
|
|
258
|
+
control.word(p.returned) === 1 &&
|
|
259
|
+
control.event(3, "thread-returned") &&
|
|
260
|
+
!control.retired(1) &&
|
|
261
|
+
!control.retired(2) &&
|
|
262
|
+
!control.retired(4),
|
|
263
|
+
"ordinary return left all siblings running",
|
|
264
|
+
);
|
|
265
|
+
control.signal(p.startGate, 0);
|
|
266
|
+
control.signal(p.retry);
|
|
267
|
+
await until(() => control.waiting(5, p.startGate, 4), "fresh Worker reused the returned slot");
|
|
268
|
+
|
|
269
|
+
replayRetiredEvents(control);
|
|
270
|
+
control.signal(p.canary);
|
|
271
|
+
await until(() => control.word(p.canaryCount) === 1, "sibling service canary after stale replay");
|
|
272
|
+
check(
|
|
273
|
+
control.entries(5) === 0 &&
|
|
274
|
+
control.waiting(5, p.startGate, 4) &&
|
|
275
|
+
!control.retired(5) &&
|
|
276
|
+
control.word(p.replacementResult) === 0,
|
|
277
|
+
"late ready/idle changed the pending replacement",
|
|
278
|
+
);
|
|
279
|
+
control.signal(p.startGate);
|
|
280
|
+
await until(
|
|
281
|
+
() => control.word(p.replacementResult) === 5 && control.word(p.replacementEntry) === 5,
|
|
282
|
+
"replacement authentic ready and compiled entry",
|
|
283
|
+
);
|
|
284
|
+
check(control.entries(5) === 1, "replacement entered exactly once");
|
|
285
|
+
control.signal(p.finish);
|
|
286
|
+
return { pendingRefusal: -6, runningRefusal: -6, returned: 3, replacement: 5 };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function replayRetiredEvents(control: Observations): void {
|
|
290
|
+
const old = control.guests.get(3);
|
|
291
|
+
const replacement = control.guests.get(5);
|
|
292
|
+
check(
|
|
293
|
+
old && old.handler && replacement?.handler && old.worker !== replacement.worker,
|
|
294
|
+
"distinct former and replacement owner callbacks",
|
|
295
|
+
);
|
|
296
|
+
for (const kind of ["ready", "host-idle"] as const) {
|
|
297
|
+
const captured = control.event(3, kind);
|
|
298
|
+
check(captured, `retired worker has no ${kind} for replay`);
|
|
299
|
+
const event = new MessageEvent("message", { data: captured.event });
|
|
300
|
+
old.handler.call(old.worker, event);
|
|
301
|
+
replacement.handler.call(replacement.worker, event);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export async function observeThreadOwner() {
|
|
306
|
+
const { RuntimeThreadOwner } = await import("../thread/runtime-owner.js");
|
|
307
|
+
const create = RuntimeThreadOwner.create;
|
|
308
|
+
let owner: RuntimeThreadOwner | undefined;
|
|
309
|
+
RuntimeThreadOwner.create = async (options) => (owner = await create(options));
|
|
310
|
+
return {
|
|
311
|
+
seed(expected: number, next: number) {
|
|
312
|
+
check(
|
|
313
|
+
owner && Reflect.get(owner, "tid") === expected,
|
|
314
|
+
`expected allocator cursor ${expected}`,
|
|
315
|
+
);
|
|
316
|
+
check(Reflect.set(owner, "tid", next), "could not seed the allocator cursor");
|
|
317
|
+
},
|
|
318
|
+
restore() {
|
|
319
|
+
RuntimeThreadOwner.create = create;
|
|
320
|
+
},
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export async function runThreadIds(
|
|
325
|
+
control: Observations,
|
|
326
|
+
allocator: Awaited<ReturnType<typeof observeThreadOwner>>,
|
|
327
|
+
) {
|
|
328
|
+
const p = tidLayout;
|
|
329
|
+
await until(() => control.waiting(1, p.gates[0], 4), "initial guest reached the first TID gate");
|
|
330
|
+
allocator.seed(1, 65534);
|
|
331
|
+
await checkThreadId(control, 0, 65535);
|
|
332
|
+
await checkThreadId(control, 1, 65536);
|
|
333
|
+
allocator.seed(65536, maxThreadId - 1);
|
|
334
|
+
await checkThreadId(control, 2, maxThreadId);
|
|
335
|
+
control.signal(p.gates[3]);
|
|
336
|
+
await until(() => control.word(p.results[3]) !== 0, "signed exhaustion result");
|
|
337
|
+
check(
|
|
338
|
+
control.word(p.results[3]) === -6 &&
|
|
339
|
+
control.word(p.entries[3]) === 0 &&
|
|
340
|
+
control.guests.size === 4 &&
|
|
341
|
+
control.guestConstructions === 4,
|
|
342
|
+
"exhaustion must fail without launching or entering a Worker",
|
|
343
|
+
);
|
|
344
|
+
control.signal(p.finish);
|
|
345
|
+
return { results: p.results.map((address) => control.word(address)) };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async function checkThreadId(control: Observations, index: number, tid: number): Promise<void> {
|
|
349
|
+
const p = tidLayout;
|
|
350
|
+
control.signal(p.gates[index]!);
|
|
351
|
+
await until(
|
|
352
|
+
() => control.word(p.results[index]!) !== 0 && control.retired(tid),
|
|
353
|
+
`TID ${tid} returns and retires`,
|
|
354
|
+
);
|
|
355
|
+
check(
|
|
356
|
+
control.word(p.results[index]!) === tid &&
|
|
357
|
+
control.word(p.entries[index]!) === tid &&
|
|
358
|
+
control.event(tid, "thread-returned"),
|
|
359
|
+
`TID ${tid} round trip through the owner and compiled entry`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export async function runReceivingGrowth(control: Observations) {
|
|
364
|
+
await until(() => control.growth.length === 3, "receiving Worker growth observations");
|
|
365
|
+
const phases = ["received", "instantiated", "returned"];
|
|
366
|
+
const extents = [2, 3, 4].map((pages) => pages * 65536);
|
|
367
|
+
const optional = control.growth[0]?.resizable;
|
|
368
|
+
for (const [index, sample] of control.growth.entries()) {
|
|
369
|
+
check(
|
|
370
|
+
sample.phase === phases[index] &&
|
|
371
|
+
sample.fixed === extents[0] &&
|
|
372
|
+
sample.refreshed === extents[index],
|
|
373
|
+
`receiving Worker fixed views: ${JSON.stringify(sample)}`,
|
|
374
|
+
);
|
|
375
|
+
check(
|
|
376
|
+
sample.resizable === (typeof optional === "number" ? extents[index] : optional),
|
|
377
|
+
`receiving Worker resizable view: ${JSON.stringify(sample)}`,
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
check(
|
|
381
|
+
control.word(growthLayout.first + 16) === 1 && control.word(growthLayout.second + 16) === 1,
|
|
382
|
+
"both guest host calls wrote their result beyond the previous extent",
|
|
383
|
+
);
|
|
384
|
+
control.signal(growthLayout.finished);
|
|
385
|
+
return { views: control.growth };
|
|
386
|
+
}
|