@waffle-os/runtime 0.0.149 → 0.0.151
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/browser/diagnostic.ts +234 -2
- package/browser/process-broker.ts +42 -4
- package/browser/worker.ts +34 -5
- package/core/open-file.ts +126 -52
- package/dist/browser/diagnostic.js +150 -1
- package/dist/browser/process-broker.js +47 -2
- package/dist/browser/worker.js +24 -6
- package/dist/core/open-file.js +112 -50
- package/dist/fs/client.js +290 -46
- package/dist/fs/protocol.js +7 -4
- package/dist/fs/service.js +77 -16
- package/dist/fs/shared.js +6 -6
- package/dist/interrupt/rewriter-internal.js +2 -1
- package/dist/proc/abi.js +152 -16
- package/dist/proc/command-stdio.js +81 -33
- package/dist/proc/directory.js +111 -0
- package/dist/proc/errors.js +1 -0
- package/dist/proc/supervisor.js +130 -24
- package/dist/proc/table.js +9 -1
- package/dist/proc/types.js +5 -0
- package/dist/proc/worker-client.js +34 -5
- package/dist/runtime.js +3 -0
- package/dist/thread/service.js +85 -10
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +298 -44
- package/fs/protocol.ts +9 -4
- package/fs/service.ts +99 -19
- package/fs/shared.ts +7 -7
- package/interrupt/rewriter-internal.ts +2 -1
- package/package.json +1 -1
- package/proc/abi.ts +185 -17
- package/proc/command-stdio.ts +98 -41
- package/proc/directory.ts +131 -0
- package/proc/errors.ts +1 -0
- package/proc/supervisor.ts +173 -36
- package/proc/table.ts +11 -0
- package/proc/types.ts +6 -0
- package/proc/worker-client.ts +36 -5
- package/runtime.ts +3 -0
- package/thread/service.ts +125 -7
- package/tooling/program/program-transformer.json +1 -1
package/fs/service.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
retainResponseBytes,
|
|
9
9
|
captureRequestMemory,
|
|
10
10
|
closeRequestMemory,
|
|
11
|
+
requestMemoryRetired,
|
|
11
12
|
retireRequestMemory,
|
|
12
13
|
requestTransportError,
|
|
13
14
|
requestLengths,
|
|
@@ -148,6 +149,8 @@ interface Session {
|
|
|
148
149
|
stages: Map<number, SessionStage>;
|
|
149
150
|
retainedRequests: Map<number, FilesystemRequestBuffer>;
|
|
150
151
|
pending: Map<number, FilesystemRequestBuffer>;
|
|
152
|
+
/** Scalar write/append requests already dispatched to backend execution. */
|
|
153
|
+
inFlightWrites: Set<number>;
|
|
151
154
|
/** Transferred inputs by request id. Removed only when that request's work settles. */
|
|
152
155
|
privateInputs: Map<number, Uint8Array>;
|
|
153
156
|
preparingHandles: Map<number, number>;
|
|
@@ -161,6 +164,10 @@ interface Session {
|
|
|
161
164
|
reader?: WorkspaceReadAuthority;
|
|
162
165
|
readerClosing?: Promise<void>;
|
|
163
166
|
}
|
|
167
|
+
function isCommittedScalarWrite(operation: string, state: State): boolean {
|
|
168
|
+
return (operation === "write" || operation === "append") && state === State.Complete;
|
|
169
|
+
}
|
|
170
|
+
|
|
164
171
|
function sessionRevoked(session: Session): boolean {
|
|
165
172
|
return isRevoked(session.connection) || !!requestTransportError(session.requests);
|
|
166
173
|
}
|
|
@@ -296,6 +303,7 @@ export class FilesystemService {
|
|
|
296
303
|
stages: new Map(),
|
|
297
304
|
retainedRequests: new Map(),
|
|
298
305
|
pending: new Map(),
|
|
306
|
+
inFlightWrites: new Set(),
|
|
299
307
|
privateInputs: new Map(),
|
|
300
308
|
preparingHandles: new Map(),
|
|
301
309
|
preparations: new Set(),
|
|
@@ -1189,11 +1197,11 @@ export class FilesystemService {
|
|
|
1189
1197
|
try {
|
|
1190
1198
|
this.finishSocketOperation(session, operation, result, args);
|
|
1191
1199
|
const pending = session.pending.get(id);
|
|
1200
|
+
const scalarWrite = operation === "write" || operation === "append";
|
|
1192
1201
|
if (
|
|
1193
|
-
sessionRevoked(session) ||
|
|
1194
|
-
!this.sessions.has(session) ||
|
|
1195
1202
|
!pending ||
|
|
1196
|
-
|
|
1203
|
+
(!scalarWrite &&
|
|
1204
|
+
(sessionRevoked(session) || !this.sessions.has(session) || requestCancelled(pending)))
|
|
1197
1205
|
)
|
|
1198
1206
|
throw new FilesystemError(11);
|
|
1199
1207
|
if (operation === "preopen" || (operation === "create" && typeof result === "number"))
|
|
@@ -1295,6 +1303,7 @@ export class FilesystemService {
|
|
|
1295
1303
|
let opened = false;
|
|
1296
1304
|
let preparing = false;
|
|
1297
1305
|
let staging = false;
|
|
1306
|
+
let committedWrite = false;
|
|
1298
1307
|
let admission: PublicationAdmission | undefined;
|
|
1299
1308
|
let dirtyRetry: number | undefined;
|
|
1300
1309
|
const epoch = session.epoch;
|
|
@@ -1340,6 +1349,7 @@ export class FilesystemService {
|
|
|
1340
1349
|
prepared?.read && (!this.backend.isPreparedReadValid || this.backend.isPreparedReadValid())
|
|
1341
1350
|
? prepared.read
|
|
1342
1351
|
: undefined;
|
|
1352
|
+
if (operation === "write" || operation === "append") session.inFlightWrites.add(id);
|
|
1343
1353
|
const value = preparedRead
|
|
1344
1354
|
? this.finishResult(session, id, operation, preparedRead.value)
|
|
1345
1355
|
: local && operation !== "flush" && operation !== "sync"
|
|
@@ -1348,6 +1358,7 @@ export class FilesystemService {
|
|
|
1348
1358
|
this.announceSegments(this.backend.takeSharedSegments?.() ?? [], this.sessions);
|
|
1349
1359
|
state = this.writeResponse(session, id, cells, response, value, bytes);
|
|
1350
1360
|
staging = operation === "stageWrite";
|
|
1361
|
+
committedWrite = isCommittedScalarWrite(operation, state);
|
|
1351
1362
|
} catch (error) {
|
|
1352
1363
|
this.discardResponse(session, id);
|
|
1353
1364
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
@@ -1373,23 +1384,20 @@ export class FilesystemService {
|
|
|
1373
1384
|
this.cancelPublication(session, admission);
|
|
1374
1385
|
this.releasePrepared(session, prepared);
|
|
1375
1386
|
cells[5] = Atomics.load(epoch, 0);
|
|
1376
|
-
|
|
1377
|
-
this.discardResponse(session, id);
|
|
1378
|
-
state = State.Cancelled;
|
|
1379
|
-
}
|
|
1380
|
-
// Complete is what lets the client commit a stage. Never publish it for a stage the
|
|
1381
|
-
// owner no longer holds, or that commit would have nothing to apply.
|
|
1382
|
-
if (staging && !session.stages.has(id)) state = State.Cancelled;
|
|
1387
|
+
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1383
1388
|
this.settleWalk(session, id, state === State.Complete);
|
|
1384
1389
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1385
1390
|
Atomics.store(cells, 0, state);
|
|
1386
1391
|
Atomics.notify(cells, 0);
|
|
1387
1392
|
wake(session.connection);
|
|
1393
|
+
session.inFlightWrites.delete(id);
|
|
1388
1394
|
session.pending.delete(id);
|
|
1389
1395
|
session.privateInputs.delete(id);
|
|
1390
1396
|
this.retainRequestReply(session, id, buffer);
|
|
1391
1397
|
session.connection.port.postMessage({ completed: id });
|
|
1392
1398
|
this.sweep();
|
|
1399
|
+
} else {
|
|
1400
|
+
session.inFlightWrites.delete(id);
|
|
1393
1401
|
}
|
|
1394
1402
|
}
|
|
1395
1403
|
if (dirtyRetry !== undefined && prepared) {
|
|
@@ -1400,6 +1408,25 @@ export class FilesystemService {
|
|
|
1400
1408
|
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1401
1409
|
}
|
|
1402
1410
|
}
|
|
1411
|
+
private settleCancelledState(
|
|
1412
|
+
session: Session,
|
|
1413
|
+
id: number,
|
|
1414
|
+
buffer: FilesystemRequestBuffer,
|
|
1415
|
+
state: State,
|
|
1416
|
+
staging: boolean,
|
|
1417
|
+
committedWrite: boolean,
|
|
1418
|
+
): State {
|
|
1419
|
+
if (sessionRevoked(session) || requestCancelled(buffer)) {
|
|
1420
|
+
this.discardResponse(session, id);
|
|
1421
|
+
// Preserve inline committed write/append results so shared-offset leases observe
|
|
1422
|
+
// an already-landed mutation before releasing the offset lock.
|
|
1423
|
+
if (!committedWrite) state = State.Cancelled;
|
|
1424
|
+
}
|
|
1425
|
+
// Complete is what lets the client commit a stage. Never publish it for a stage the
|
|
1426
|
+
// owner no longer holds, or that commit would have nothing to apply.
|
|
1427
|
+
if (staging && !session.stages.has(id)) return State.Cancelled;
|
|
1428
|
+
return state;
|
|
1429
|
+
}
|
|
1403
1430
|
private holdResponseMemory(session: Session, id: number, release: () => void): void {
|
|
1404
1431
|
const prior = session.responseMemory.get(id);
|
|
1405
1432
|
session.responseMemory.set(id, () => {
|
|
@@ -1904,6 +1931,16 @@ export class FilesystemService {
|
|
|
1904
1931
|
this.backend.shared.publication?.releasePin(id);
|
|
1905
1932
|
session.publicationPins?.clear();
|
|
1906
1933
|
}
|
|
1934
|
+
private cancelPendingRequests(session: Session): void {
|
|
1935
|
+
for (const [id, buffer] of session.pending) {
|
|
1936
|
+
const cells = requestCells(buffer);
|
|
1937
|
+
Atomics.store(cells, 6, 1);
|
|
1938
|
+
Atomics.store(cells, 0, State.Cancelled);
|
|
1939
|
+
Atomics.notify(cells, 0);
|
|
1940
|
+
session.connection.port.postMessage({ completed: id });
|
|
1941
|
+
}
|
|
1942
|
+
session.pending.clear();
|
|
1943
|
+
}
|
|
1907
1944
|
private releaseSessionPins(session: Session): void {
|
|
1908
1945
|
this.cancelSessionWatchResponses(session);
|
|
1909
1946
|
this.watchService?.closeOwner(session.connection.owner);
|
|
@@ -1940,18 +1977,14 @@ export class FilesystemService {
|
|
|
1940
1977
|
);
|
|
1941
1978
|
return;
|
|
1942
1979
|
}
|
|
1980
|
+
// Keep the session open until every already-dispatched write/append publishes its
|
|
1981
|
+
// authoritative commit or abort state; request()'s finally calls sweep() on completion.
|
|
1982
|
+
if (session.inFlightWrites.size) return;
|
|
1943
1983
|
if (ownRevocation === 2) this.backend.revokeOwner?.(session.connection.owner, session.epoch);
|
|
1944
1984
|
// A discard can be lost when a port closes. Retirement settles what remains, before this
|
|
1945
1985
|
// session's file pins and request blocks go: a committed write is applied, never dropped.
|
|
1946
1986
|
for (const id of Array.from(session.stages.keys())) this.settleStage(session, id);
|
|
1947
|
-
|
|
1948
|
-
const cells = requestCells(buffer);
|
|
1949
|
-
Atomics.store(cells, 6, 1);
|
|
1950
|
-
Atomics.store(cells, 0, State.Cancelled);
|
|
1951
|
-
Atomics.notify(cells, 0);
|
|
1952
|
-
session.connection.port.postMessage({ completed: id });
|
|
1953
|
-
}
|
|
1954
|
-
session.pending.clear();
|
|
1987
|
+
this.cancelPendingRequests(session);
|
|
1955
1988
|
wake(session.connection);
|
|
1956
1989
|
for (const id of session.walkTransactions.keys()) this.settleWalk(session, id, false);
|
|
1957
1990
|
session.walks?.dispose();
|
|
@@ -2118,7 +2151,54 @@ export class FilesystemService {
|
|
|
2118
2151
|
}
|
|
2119
2152
|
private async shutdownFence(session: Session): Promise<void> {
|
|
2120
2153
|
revokeFilesystemService(session.connection);
|
|
2121
|
-
|
|
2154
|
+
const quiesced = waitForFilesystemQuiescence(session.connection);
|
|
2155
|
+
let done = false;
|
|
2156
|
+
void quiesced.finally(() => {
|
|
2157
|
+
done = true;
|
|
2158
|
+
wake(session.connection);
|
|
2159
|
+
});
|
|
2160
|
+
const signal = new Int32Array(session.connection.signal);
|
|
2161
|
+
while (!done) {
|
|
2162
|
+
const observed = Atomics.load(signal, 0);
|
|
2163
|
+
if (requestTransportError(session.requests)) {
|
|
2164
|
+
session.transportFailed = true;
|
|
2165
|
+
await this.drainFailedSessionRequests(session);
|
|
2166
|
+
break;
|
|
2167
|
+
}
|
|
2168
|
+
const waiting = atomicsWaitAsync(signal, 0, observed);
|
|
2169
|
+
if (waiting.async) await waiting.value;
|
|
2170
|
+
}
|
|
2171
|
+
await quiesced;
|
|
2172
|
+
}
|
|
2173
|
+
private async drainFailedSessionRequests(session: Session): Promise<void> {
|
|
2174
|
+
// A transport error stops receive() from accepting new descriptors. Drain every already-
|
|
2175
|
+
// accepted request (including flush/sync barriers kept outside this.queue) and preparation
|
|
2176
|
+
// first, then retire transport memory so undelivered client writes can release their leases.
|
|
2177
|
+
await waitForRequestQuiescence(session.connection, session.requests);
|
|
2178
|
+
for (;;) {
|
|
2179
|
+
const queue = this.queue;
|
|
2180
|
+
await queue;
|
|
2181
|
+
const pending = Array.from(this.runningRequests).filter(
|
|
2182
|
+
(done) => this.work.get(done) === session,
|
|
2183
|
+
);
|
|
2184
|
+
if (pending.length) await Promise.allSettled(pending);
|
|
2185
|
+
if (session.preparations.size) await Promise.allSettled(Array.from(session.preparations));
|
|
2186
|
+
const stillRunning = Array.from(this.runningRequests).some(
|
|
2187
|
+
(done) => this.work.get(done) === session,
|
|
2188
|
+
);
|
|
2189
|
+
if (
|
|
2190
|
+
queue === this.queue &&
|
|
2191
|
+
!stillRunning &&
|
|
2192
|
+
!session.preparations.size &&
|
|
2193
|
+
!session.inFlightWrites.size
|
|
2194
|
+
)
|
|
2195
|
+
break;
|
|
2196
|
+
}
|
|
2197
|
+
this.cancelPendingRequests(session);
|
|
2198
|
+
const failure = requestTransportError(session.requests);
|
|
2199
|
+
if (failure?.reason !== "stale" && !requestMemoryRetired(session.requests))
|
|
2200
|
+
retireRequestMemory(session.requests);
|
|
2201
|
+
wake(session.connection);
|
|
2122
2202
|
}
|
|
2123
2203
|
private async drainShutdown(owner: ShutdownOwner, fences: Promise<void>[]): Promise<void> {
|
|
2124
2204
|
const failures: unknown[] = [];
|
package/fs/shared.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
type FileId,
|
|
16
16
|
type FileVersion,
|
|
17
17
|
} from "./file-identity.js";
|
|
18
|
-
import { typedArraySet } from "../proc/intrinsics.js";
|
|
18
|
+
import { Int32ArrayConstructor, atomicsLoad, typedArraySet } from "../proc/intrinsics.js";
|
|
19
19
|
import {
|
|
20
20
|
PublicationDomain,
|
|
21
21
|
type PublicationAdmission,
|
|
@@ -289,10 +289,10 @@ export class SharedFilesystemState {
|
|
|
289
289
|
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
290
290
|
}
|
|
291
291
|
isDirectDisabled(): boolean {
|
|
292
|
-
return (
|
|
292
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
293
293
|
}
|
|
294
294
|
isClientCacheDisabled(): boolean {
|
|
295
|
-
return (
|
|
295
|
+
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_CLIENT_CACHE) !== 0;
|
|
296
296
|
}
|
|
297
297
|
/** Reset all per-mount filesystem state in place across Host remount/demotion while preserving
|
|
298
298
|
* tab-lifetime header invariants (`MAGIC`, `maxSegments`, owner/probe words, `WATCH_OFFSET`,
|
|
@@ -2491,13 +2491,13 @@ export function sharedRevoked(connection: {
|
|
|
2491
2491
|
ownerStates: SharedArrayBuffer;
|
|
2492
2492
|
publication?: PublicationConnection;
|
|
2493
2493
|
}): () => boolean {
|
|
2494
|
-
const words = [new
|
|
2495
|
-
connection.revocations.map((buffer) => new
|
|
2494
|
+
const words = [new Int32ArrayConstructor(connection.ownerStates, 0, 1)].concat(
|
|
2495
|
+
connection.revocations.map((buffer) => new Int32ArrayConstructor(buffer)),
|
|
2496
2496
|
);
|
|
2497
2497
|
if (connection.publication)
|
|
2498
|
-
words.push(new
|
|
2498
|
+
words.push(new Int32ArrayConstructor(connection.publication.buffers.control, 0, 1));
|
|
2499
2499
|
return () => {
|
|
2500
|
-
for (let i = 0; i < words.length; i++) if (
|
|
2500
|
+
for (let i = 0; i < words.length; i++) if (atomicsLoad(words[i]!, 0) !== 0) return true;
|
|
2501
2501
|
return false;
|
|
2502
2502
|
};
|
|
2503
2503
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Bump on every edit. interrupt-transform-pin.test.ts binds this revision to the implementation.
|
|
2
|
-
export const INTERRUPT_REWRITER_REVISION =
|
|
2
|
+
export const INTERRUPT_REWRITER_REVISION = 8;
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Fail-closed core-Wasm interrupt instrumentation. Supported features are MVP,
|
|
@@ -61,6 +61,7 @@ const runtimeProcSignatures = new Map<string, FunctionSignature>([
|
|
|
61
61
|
["wait_job", { params: [0x7f, 0x7f, 0x7f], results: [0x7f] }],
|
|
62
62
|
["kill", { params: [0x7f, 0x7f], results: [0x7f] }],
|
|
63
63
|
["kill_job", { params: [0x7f, 0x7f], results: [0x7f] }],
|
|
64
|
+
["probe_process", { params: [0x7f], results: [0x7f] }],
|
|
64
65
|
["start_status", { params: [0x7f], results: [0x7f] }],
|
|
65
66
|
["poll", { params: [0x7f, 0x7f, 0x7f, 0x7f], results: [0x7f] }],
|
|
66
67
|
["interrupted", { params: [], results: [0x7f] }],
|
package/package.json
CHANGED
package/proc/abi.ts
CHANGED
|
@@ -34,7 +34,12 @@ import {
|
|
|
34
34
|
type Stream,
|
|
35
35
|
} from "../core/descriptors.js";
|
|
36
36
|
import type { FileSystem } from "../core/filesystem.js";
|
|
37
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
tryClaimFileOffset,
|
|
39
|
+
withFileOffset,
|
|
40
|
+
type AsyncFileOffsetLease,
|
|
41
|
+
} from "../core/open-file.js";
|
|
42
|
+
import type { DirectDisabledIOControl, FilesystemCompletion } from "../fs/client.js";
|
|
38
43
|
import { PipeController } from "./pipes.js";
|
|
39
44
|
import { readOutcome } from "./outcome.js";
|
|
40
45
|
import { WasiExit } from "../core/wasi.js";
|
|
@@ -56,6 +61,7 @@ import {
|
|
|
56
61
|
ProcessError,
|
|
57
62
|
Signal,
|
|
58
63
|
checkedJobLifetime,
|
|
64
|
+
checkedKillSignal,
|
|
59
65
|
checkedProcessCwd,
|
|
60
66
|
checkedProcessKind,
|
|
61
67
|
snapshotLogicalProcessIdentity,
|
|
@@ -91,7 +97,10 @@ export interface ProcessService {
|
|
|
91
97
|
defaultSignal(pid: number, signal: number): void;
|
|
92
98
|
/** Privileged local services check guest authority; broker clients are already caller-bound. */
|
|
93
99
|
canSignal?(callerPid: number, targetPid: number): boolean;
|
|
100
|
+
canProbe?(callerPid: number, targetPid: number): boolean;
|
|
94
101
|
canSignalJob?(callerPid: number, job: number): boolean;
|
|
102
|
+
canProbeJob?(callerPid: number, job: number): boolean;
|
|
103
|
+
canSignalProcess?(callerPid: number, processId: number): boolean;
|
|
95
104
|
accessStart?(pid: number, path: string, cwd: string, wake?: Int32Array): number;
|
|
96
105
|
accessTake?(pid: number, request: number): number;
|
|
97
106
|
accessCancel?(pid: number, request: number): number;
|
|
@@ -101,6 +110,16 @@ export interface ProcessService {
|
|
|
101
110
|
}
|
|
102
111
|
|
|
103
112
|
interface NonblockingFileSystem extends FileSystem {
|
|
113
|
+
readonly signal?: Int32Array;
|
|
114
|
+
readonly directDisabled?: boolean;
|
|
115
|
+
directDisabledIO?(
|
|
116
|
+
operation: "read" | "write" | "append",
|
|
117
|
+
handle: number,
|
|
118
|
+
bytes: Uint8Array,
|
|
119
|
+
offset: number,
|
|
120
|
+
control: DirectDisabledIOControl,
|
|
121
|
+
): { written: number; offset: number } | number;
|
|
122
|
+
trackDeferredWrite?(completion: FilesystemCompletion, onSettled: () => void): void;
|
|
104
123
|
tryRead?(handle: number, chunks: readonly Uint8Array[], offset: number): number;
|
|
105
124
|
tryWrite?(handle: number, chunks: readonly Uint8Array[], offset: number): number;
|
|
106
125
|
tryAppend?(
|
|
@@ -108,6 +127,7 @@ interface NonblockingFileSystem extends FileSystem {
|
|
|
108
127
|
chunks: readonly Uint8Array[],
|
|
109
128
|
): { written: number; offset: number } | number;
|
|
110
129
|
ioReadiness?(handle: number, writing: boolean): Readiness;
|
|
130
|
+
ioPending?(handle: number, writing: boolean): boolean;
|
|
111
131
|
readiness?(request: number): { ready: boolean } | undefined;
|
|
112
132
|
}
|
|
113
133
|
interface NonblockingStream extends Stream {
|
|
@@ -204,9 +224,16 @@ function sameWord(first: Int32Array | undefined, second: Int32Array): boolean {
|
|
|
204
224
|
);
|
|
205
225
|
}
|
|
206
226
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
227
|
+
function validWakeWord(word: Int32Array | undefined): Int32Array | undefined {
|
|
228
|
+
return word && isSharedArrayBuffer(typedArrayBuffer(word)) && typedArrayLength(word) === 1
|
|
229
|
+
? word
|
|
230
|
+
: undefined;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Guest adapter for the runtime_proc WASM namespace. Only poll, blocking wait_job, and
|
|
234
|
+
* bounded regular-file RPCs on direct-disabled backends wait. FileSystem supplies immediate
|
|
235
|
+
* nonblocking I/O whenever direct shared-memory access is enabled; streams with potentially
|
|
236
|
+
* blocking read/write must expose tryRead/tryWrite. */
|
|
210
237
|
export class ProcessABI {
|
|
211
238
|
readonly imports: Record<string, (...args: number[]) => number>;
|
|
212
239
|
private readonly now: () => number;
|
|
@@ -265,16 +292,33 @@ export class ProcessABI {
|
|
|
265
292
|
wait_job: (job, flags, ptr) => this.result(() => this.waitJob(job!, flags!, ptr!)),
|
|
266
293
|
kill: (pid, signal) =>
|
|
267
294
|
this.result(() => {
|
|
268
|
-
|
|
269
|
-
|
|
295
|
+
const sig = checkedKillSignal(signal!);
|
|
296
|
+
const allowed =
|
|
297
|
+
sig === 0 && options.service.canProbe
|
|
298
|
+
? options.service.canProbe(options.pid, pid!)
|
|
299
|
+
: options.service.canSignal?.(options.pid, pid!);
|
|
300
|
+
if (allowed === false) return -ProcessErrno.PERM;
|
|
301
|
+
if (sig === 0 && allowed === true) return 0;
|
|
302
|
+
options.service.kill(pid!, sig);
|
|
270
303
|
return 0;
|
|
271
304
|
}),
|
|
272
305
|
kill_job: (job, signal) =>
|
|
273
306
|
this.result(() => {
|
|
274
|
-
|
|
275
|
-
|
|
307
|
+
const sig = checkedKillSignal(signal!);
|
|
308
|
+
const allowed =
|
|
309
|
+
sig === 0 && options.service.canProbeJob
|
|
310
|
+
? options.service.canProbeJob(options.pid, job!)
|
|
311
|
+
: options.service.canSignalJob?.(options.pid, job!);
|
|
312
|
+
if (allowed === false) return -ProcessErrno.PERM;
|
|
313
|
+
if (sig === 0 && allowed === true) return 0;
|
|
314
|
+
options.service.killJob(job!, sig);
|
|
276
315
|
return 0;
|
|
277
316
|
}),
|
|
317
|
+
probe_process: (pid) =>
|
|
318
|
+
this.result(() => {
|
|
319
|
+
if (!options.service.canSignalProcess) return -NOSYS;
|
|
320
|
+
return options.service.canSignalProcess(options.pid, pid!) ? 0 : -ProcessErrno.PERM;
|
|
321
|
+
}),
|
|
278
322
|
start_status: (pid) => this.result(() => options.service.startStatus(options.pid, pid!)),
|
|
279
323
|
poll: (ptr, count, timeout, out) =>
|
|
280
324
|
this.result(() => this.poll(ptr!, count!, timeout!, out!)),
|
|
@@ -568,6 +612,86 @@ export class ProcessABI {
|
|
|
568
612
|
throw new ProcessError(NOTCAPABLE, "descriptor rights denied");
|
|
569
613
|
return entry;
|
|
570
614
|
}
|
|
615
|
+
private settleDeferredWriteLease(
|
|
616
|
+
fs: NonblockingFileSystem,
|
|
617
|
+
completion: FilesystemCompletion,
|
|
618
|
+
lease: AsyncFileOffsetLease,
|
|
619
|
+
append: boolean,
|
|
620
|
+
): void {
|
|
621
|
+
const finish = () => {
|
|
622
|
+
try {
|
|
623
|
+
const committed = completion.tryConsumeCommitted();
|
|
624
|
+
if (committed !== undefined) {
|
|
625
|
+
if (append) lease.commit((committed as { written: number; offset: number }).offset);
|
|
626
|
+
else if (typeof committed === "number" && committed > 0)
|
|
627
|
+
lease.commit(lease.position + committed);
|
|
628
|
+
}
|
|
629
|
+
} catch {
|
|
630
|
+
// Aborted or failed write: leave shared offset unchanged before releasing the lease.
|
|
631
|
+
} finally {
|
|
632
|
+
completion.release();
|
|
633
|
+
lease.release();
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
if (fs.trackDeferredWrite) fs.trackDeferredWrite(completion, finish);
|
|
637
|
+
else completion.onBackendSettled(finish);
|
|
638
|
+
}
|
|
639
|
+
private directDisabledFileIO(
|
|
640
|
+
write: boolean,
|
|
641
|
+
resource: Extract<Descriptor["resource"], { kind: "file" }>,
|
|
642
|
+
bytes: Uint8Array,
|
|
643
|
+
fs: NonblockingFileSystem,
|
|
644
|
+
): number {
|
|
645
|
+
this.checkFatal();
|
|
646
|
+
if (this.pending()) return -ProcessErrno.AGAIN;
|
|
647
|
+
const claim = tryClaimFileOffset(resource, true);
|
|
648
|
+
if (claim?.kind === "failed") {
|
|
649
|
+
this.checkFatal();
|
|
650
|
+
return -claim.errno;
|
|
651
|
+
}
|
|
652
|
+
if (claim?.kind === "busy") return -ProcessErrno.AGAIN;
|
|
653
|
+
const lease = claim?.kind === "acquired" ? claim.lease : undefined;
|
|
654
|
+
let releaseLease = true;
|
|
655
|
+
let outcome: number;
|
|
656
|
+
try {
|
|
657
|
+
const append = write && (lease ? lease.append : resource.append);
|
|
658
|
+
const position = lease ? lease.position : resource.offset;
|
|
659
|
+
const op = append ? "append" : write ? "write" : "read";
|
|
660
|
+
const commitOffset = (result: { written: number; offset: number } | number): number => {
|
|
661
|
+
if (typeof result !== "number") {
|
|
662
|
+
if (lease) lease.commit(result.offset);
|
|
663
|
+
else resource.offset = result.offset;
|
|
664
|
+
return result.written;
|
|
665
|
+
}
|
|
666
|
+
if (result > 0) {
|
|
667
|
+
if (lease) lease.commit(position + result);
|
|
668
|
+
else resource.offset = position + result;
|
|
669
|
+
}
|
|
670
|
+
return result;
|
|
671
|
+
};
|
|
672
|
+
const result = fs.directDisabledIO!(op, resource.handle, bytes, position, {
|
|
673
|
+
checkFatal: () => this.checkFatal(),
|
|
674
|
+
pending: () => this.pending(),
|
|
675
|
+
wait: (ms, word, expected) => {
|
|
676
|
+
assertTerminalWaitAllowed();
|
|
677
|
+
if (this.options.wait) this.options.wait(ms, word, expected);
|
|
678
|
+
else controlWait(word, 0, expected, ms);
|
|
679
|
+
},
|
|
680
|
+
commitWrite: commitOffset,
|
|
681
|
+
deferWrite: lease
|
|
682
|
+
? (completion) => {
|
|
683
|
+
releaseLease = false;
|
|
684
|
+
this.settleDeferredWriteLease(fs, completion, lease, append);
|
|
685
|
+
}
|
|
686
|
+
: undefined,
|
|
687
|
+
});
|
|
688
|
+
outcome = commitOffset(result);
|
|
689
|
+
} finally {
|
|
690
|
+
if (lease && releaseLease) lease.release();
|
|
691
|
+
}
|
|
692
|
+
this.checkFatal();
|
|
693
|
+
return outcome;
|
|
694
|
+
}
|
|
571
695
|
private io(write: boolean, fd: number, ptr: number, length: number): number {
|
|
572
696
|
const entry = this.descriptor(fd, write ? WRITE : READ),
|
|
573
697
|
resource = entry.resource;
|
|
@@ -576,6 +700,8 @@ export class ProcessABI {
|
|
|
576
700
|
if (typedArrayLength(bytes) === 0) return 0;
|
|
577
701
|
if (resource.kind === "file") {
|
|
578
702
|
const fs: NonblockingFileSystem = this.options.fs;
|
|
703
|
+
if (fs.directDisabled && fs.directDisabledIO)
|
|
704
|
+
return this.directDisabledFileIO(write, resource, bytes, fs);
|
|
579
705
|
return withFileOffset(
|
|
580
706
|
resource,
|
|
581
707
|
() => {
|
|
@@ -950,6 +1076,44 @@ export class ProcessABI {
|
|
|
950
1076
|
}
|
|
951
1077
|
return fullyNotified;
|
|
952
1078
|
}
|
|
1079
|
+
private pollFilesystemWait(sub: Subscription): "pending" | "file" | "none" {
|
|
1080
|
+
if (sub.kind === 3) return this.options.service.filesystemReady ? "none" : "pending";
|
|
1081
|
+
if (sub.kind !== 0 && sub.kind !== 1) return "none";
|
|
1082
|
+
const resource = this.descriptor(sub.id, POLL).resource;
|
|
1083
|
+
if (resource.kind !== "file") return "none";
|
|
1084
|
+
const fs: NonblockingFileSystem = this.options.fs;
|
|
1085
|
+
return fs.ioPending?.(resource.handle, sub.kind === 1) ? "pending" : "file";
|
|
1086
|
+
}
|
|
1087
|
+
private scanPollSubscriptions(
|
|
1088
|
+
subscriptions: readonly Subscription[],
|
|
1089
|
+
timeout: number,
|
|
1090
|
+
hasFsSignal: boolean,
|
|
1091
|
+
): {
|
|
1092
|
+
ready: { sub: Subscription; flags: number }[];
|
|
1093
|
+
waitOnFs: boolean;
|
|
1094
|
+
sawFile: boolean;
|
|
1095
|
+
allFs: boolean;
|
|
1096
|
+
} {
|
|
1097
|
+
const ready: { sub: Subscription; flags: number }[] = [];
|
|
1098
|
+
let waitOnFs = false;
|
|
1099
|
+
let sawFile = false;
|
|
1100
|
+
let allFs = subscriptions.length > 0;
|
|
1101
|
+
for (const sub of subscriptions) {
|
|
1102
|
+
const state = this.readiness(sub, timeout);
|
|
1103
|
+
if (state && ((sub.kind === 1 ? state.writable : state.readable) || state.hangup)) {
|
|
1104
|
+
ready.push({ sub, flags: state.hangup ? 1 : 0 });
|
|
1105
|
+
continue;
|
|
1106
|
+
}
|
|
1107
|
+
if (!hasFsSignal) continue;
|
|
1108
|
+
const fsWait = this.pollFilesystemWait(sub);
|
|
1109
|
+
if (fsWait === "none") allFs = false;
|
|
1110
|
+
else if (fsWait === "pending") {
|
|
1111
|
+
waitOnFs = true;
|
|
1112
|
+
sawFile = true;
|
|
1113
|
+
} else sawFile = true;
|
|
1114
|
+
}
|
|
1115
|
+
return { ready, waitOnFs, sawFile, allFs };
|
|
1116
|
+
}
|
|
953
1117
|
private poll(ptr: number, count: number, timeout: number, out: number): number {
|
|
954
1118
|
count >>>= 0;
|
|
955
1119
|
if (!Number.isInteger(timeout) || timeout < -1) return -ProcessErrno.INVAL;
|
|
@@ -970,6 +1134,7 @@ export class ProcessABI {
|
|
|
970
1134
|
if (this.pending()) return 0;
|
|
971
1135
|
const unregister: (() => void)[] = [];
|
|
972
1136
|
const wake = this.options.wake;
|
|
1137
|
+
const fsWord = validWakeWord((this.options.fs as NonblockingFileSystem).signal);
|
|
973
1138
|
try {
|
|
974
1139
|
const fullyNotified = this.registerPollWakes(subscriptions, wake, unregister);
|
|
975
1140
|
const deadline = timeout === -1 ? Infinity : this.now() + timeout;
|
|
@@ -977,13 +1142,13 @@ export class ProcessABI {
|
|
|
977
1142
|
// Observe sequence before state; publication racing scan makes Atomics.wait return immediately.
|
|
978
1143
|
const word = wake ?? this.options.signals.interrupt;
|
|
979
1144
|
const sequence = controlLoad(word, 0);
|
|
1145
|
+
const fsSequence = fsWord ? controlLoad(fsWord, 0) : 0;
|
|
980
1146
|
if (this.pending()) return 0;
|
|
981
|
-
const
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
}
|
|
1147
|
+
const { ready, waitOnFs, sawFile, allFs } = this.scanPollSubscriptions(
|
|
1148
|
+
subscriptions,
|
|
1149
|
+
timeout,
|
|
1150
|
+
fsWord !== undefined,
|
|
1151
|
+
);
|
|
987
1152
|
if (ready.length) {
|
|
988
1153
|
// Waiting/host services can grow memory; never retain a DataView across them.
|
|
989
1154
|
const output = this.view(out, ready.length * 16);
|
|
@@ -998,13 +1163,16 @@ export class ProcessABI {
|
|
|
998
1163
|
if (this.pending()) return 0;
|
|
999
1164
|
const remaining = deadline - this.now();
|
|
1000
1165
|
if (remaining <= 0) return 0;
|
|
1166
|
+
if (allFs && sawFile && fsWord && controlLoad(fsWord, 0) !== fsSequence) continue;
|
|
1001
1167
|
// Independent signals, early child startup, filesystem and legacy streams need a bounded
|
|
1002
1168
|
// rescan. A fully notified wait set can park until its actual deadline.
|
|
1169
|
+
const useFsWake = allFs && waitOnFs && fsWord !== undefined;
|
|
1170
|
+
const waitWord = useFsWake ? fsWord : word;
|
|
1171
|
+
const expected = useFsWake ? fsSequence : sequence;
|
|
1003
1172
|
const milliseconds = fullyNotified ? remaining : Math.min(remaining, 10);
|
|
1004
|
-
const expected = sequence;
|
|
1005
1173
|
assertTerminalWaitAllowed();
|
|
1006
|
-
if (this.options.wait) this.options.wait(milliseconds,
|
|
1007
|
-
else controlWait(
|
|
1174
|
+
if (this.options.wait) this.options.wait(milliseconds, waitWord, expected);
|
|
1175
|
+
else controlWait(waitWord, 0, expected, milliseconds);
|
|
1008
1176
|
}
|
|
1009
1177
|
} finally {
|
|
1010
1178
|
for (let index = unregister.length - 1; index >= 0; index--) unregister[index]!();
|