@waffle-os/runtime 0.0.150 → 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/process-broker.ts +42 -4
- package/dist/browser/process-broker.js +47 -2
- package/dist/interrupt/rewriter-internal.js +2 -1
- package/dist/proc/abi.js +20 -6
- 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/tooling/program/program-transformer.json +1 -1
- package/interrupt/rewriter-internal.ts +2 -1
- package/package.json +1 -1
- package/proc/abi.ts +25 -4
- 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/tooling/program/program-transformer.json +1 -1
|
@@ -39,6 +39,8 @@ import {
|
|
|
39
39
|
directorySlots,
|
|
40
40
|
directoryJob,
|
|
41
41
|
findDirectoryJob,
|
|
42
|
+
probeDirectoryJob,
|
|
43
|
+
probeDirectoryLogicalPid,
|
|
42
44
|
directoryControls,
|
|
43
45
|
isCollectedProcess,
|
|
44
46
|
reserveDirectoryProcess,
|
|
@@ -68,6 +70,7 @@ import {
|
|
|
68
70
|
ProcessErrno,
|
|
69
71
|
ProcessError,
|
|
70
72
|
checkedSignal,
|
|
73
|
+
checkedKillSignal,
|
|
71
74
|
checkedJobLifetime,
|
|
72
75
|
checkedProcessCwd,
|
|
73
76
|
snapshotSpawnWorker,
|
|
@@ -290,6 +293,10 @@ export class WorkerProcessClient {
|
|
|
290
293
|
if (commandTransferFailed(this.options.transport.commandTransfer))
|
|
291
294
|
throw new ProcessError(ProcessErrno.SRCH, "command transfer cleanup failed");
|
|
292
295
|
}
|
|
296
|
+
checkAlive() {
|
|
297
|
+
if (readOutcome(this.signals.outcome))
|
|
298
|
+
throw new ProcessError(ProcessErrno.SRCH, "process has exited");
|
|
299
|
+
}
|
|
293
300
|
admit() {
|
|
294
301
|
if (readOutcome(this.signals.outcome)) {
|
|
295
302
|
this.retireAccesses();
|
|
@@ -659,6 +666,7 @@ export class WorkerProcessClient {
|
|
|
659
666
|
canSignal(callerPid, pid) {
|
|
660
667
|
if (callerPid !== this.pid)
|
|
661
668
|
return false;
|
|
669
|
+
this.checkAlive();
|
|
662
670
|
const target = this.find(pid);
|
|
663
671
|
return directoryControls(this.options.transport.directory, () => {
|
|
664
672
|
controlLoad(target.controlParent);
|
|
@@ -672,19 +680,40 @@ export class WorkerProcessClient {
|
|
|
672
680
|
const group = this.requireJob(callerPid, job);
|
|
673
681
|
return directoryControls(this.options.transport.directory, () => controlLoad(group.controlParent), this.pid, true);
|
|
674
682
|
} catch (error) {
|
|
675
|
-
if (error instanceof ProcessError && error.errno ===
|
|
683
|
+
if (error instanceof ProcessError && error.errno === ProcessErrno.PERM)
|
|
676
684
|
return false;
|
|
677
685
|
throw error;
|
|
678
686
|
}
|
|
679
687
|
}
|
|
688
|
+
canProbeJob(callerPid, job) {
|
|
689
|
+
if (callerPid !== this.pid)
|
|
690
|
+
return false;
|
|
691
|
+
this.checkAlive();
|
|
692
|
+
return probeDirectoryJob(this.options.transport.directory, this.pid, job);
|
|
693
|
+
}
|
|
694
|
+
canSignalProcess(callerPid, processId) {
|
|
695
|
+
if (callerPid !== this.pid)
|
|
696
|
+
return false;
|
|
697
|
+
this.checkAlive();
|
|
698
|
+
return probeDirectoryLogicalPid(this.options.transport.directory, callerPid, processId, this.options.transport.slot);
|
|
699
|
+
}
|
|
680
700
|
kill(pid, signal) {
|
|
701
|
+
const sig = checkedKillSignal(signal);
|
|
681
702
|
if (!this.canSignal(this.pid, pid))
|
|
682
|
-
throw new ProcessError(
|
|
683
|
-
|
|
703
|
+
throw new ProcessError(ProcessErrno.PERM, "process is outside the caller's ancestry");
|
|
704
|
+
if (sig === 0)
|
|
705
|
+
return;
|
|
706
|
+
this.deliver([this.find(pid)], sig);
|
|
684
707
|
}
|
|
685
708
|
killJob(job, signal) {
|
|
709
|
+
const sig = checkedKillSignal(signal);
|
|
710
|
+
if (sig === 0) {
|
|
711
|
+
if (!this.canProbeJob(this.pid, job))
|
|
712
|
+
throw new ProcessError(ProcessErrno.PERM, "job is not owned by the caller");
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
686
715
|
if (!this.canSignalJob(this.pid, job))
|
|
687
|
-
throw new ProcessError(
|
|
716
|
+
throw new ProcessError(ProcessErrno.PERM, "job is not owned by the caller");
|
|
688
717
|
const directory = this.options.transport.directory;
|
|
689
718
|
const slots = directorySlots(directory);
|
|
690
719
|
const targets = [];
|
|
@@ -699,7 +728,7 @@ export class WorkerProcessClient {
|
|
|
699
728
|
throw error;
|
|
700
729
|
}
|
|
701
730
|
}
|
|
702
|
-
this.deliver(targets,
|
|
731
|
+
this.deliver(targets, sig, true);
|
|
703
732
|
}
|
|
704
733
|
deliver(targets, signal, collective = false) {
|
|
705
734
|
this.admit();
|
package/dist/runtime.js
CHANGED
|
@@ -84,7 +84,10 @@ const supervisorCapabilities = {
|
|
|
84
84
|
jobOf: "method",
|
|
85
85
|
cwd: "method",
|
|
86
86
|
canSignal: "method",
|
|
87
|
+
canProbe: "method",
|
|
87
88
|
canSignalJob: "method",
|
|
89
|
+
canProbeJob: "method",
|
|
90
|
+
canSignalProcess: "method",
|
|
88
91
|
canWaitJob: "method",
|
|
89
92
|
accessStart: "method",
|
|
90
93
|
accessTake: "method",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"commit":"
|
|
1
|
+
{"commit":"da522cacff05cf6f18e820967a2e0f52d2f19680","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
|
|
@@ -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
|
@@ -61,6 +61,7 @@ import {
|
|
|
61
61
|
ProcessError,
|
|
62
62
|
Signal,
|
|
63
63
|
checkedJobLifetime,
|
|
64
|
+
checkedKillSignal,
|
|
64
65
|
checkedProcessCwd,
|
|
65
66
|
checkedProcessKind,
|
|
66
67
|
snapshotLogicalProcessIdentity,
|
|
@@ -96,7 +97,10 @@ export interface ProcessService {
|
|
|
96
97
|
defaultSignal(pid: number, signal: number): void;
|
|
97
98
|
/** Privileged local services check guest authority; broker clients are already caller-bound. */
|
|
98
99
|
canSignal?(callerPid: number, targetPid: number): boolean;
|
|
100
|
+
canProbe?(callerPid: number, targetPid: number): boolean;
|
|
99
101
|
canSignalJob?(callerPid: number, job: number): boolean;
|
|
102
|
+
canProbeJob?(callerPid: number, job: number): boolean;
|
|
103
|
+
canSignalProcess?(callerPid: number, processId: number): boolean;
|
|
100
104
|
accessStart?(pid: number, path: string, cwd: string, wake?: Int32Array): number;
|
|
101
105
|
accessTake?(pid: number, request: number): number;
|
|
102
106
|
accessCancel?(pid: number, request: number): number;
|
|
@@ -288,16 +292,33 @@ export class ProcessABI {
|
|
|
288
292
|
wait_job: (job, flags, ptr) => this.result(() => this.waitJob(job!, flags!, ptr!)),
|
|
289
293
|
kill: (pid, signal) =>
|
|
290
294
|
this.result(() => {
|
|
291
|
-
|
|
292
|
-
|
|
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);
|
|
293
303
|
return 0;
|
|
294
304
|
}),
|
|
295
305
|
kill_job: (job, signal) =>
|
|
296
306
|
this.result(() => {
|
|
297
|
-
|
|
298
|
-
|
|
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);
|
|
299
315
|
return 0;
|
|
300
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
|
+
}),
|
|
301
322
|
start_status: (pid) => this.result(() => options.service.startStatus(options.pid, pid!)),
|
|
302
323
|
poll: (ptr, count, timeout, out) =>
|
|
303
324
|
this.result(() => this.poll(ptr!, count!, timeout!, out!)),
|
package/proc/directory.ts
CHANGED
|
@@ -346,6 +346,137 @@ export function* directoryGroups(
|
|
|
346
346
|
}
|
|
347
347
|
}
|
|
348
348
|
}
|
|
349
|
+
/** Read-only execution-handle lookup: ignores processes whose job was sealed/collected. */
|
|
350
|
+
export function peekDirectoryProcess(
|
|
351
|
+
directory: SharedArrayBuffer,
|
|
352
|
+
pid: number,
|
|
353
|
+
): ReturnType<typeof directoryState> | undefined {
|
|
354
|
+
const capacity = directoryCapacity(directory);
|
|
355
|
+
const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
|
|
356
|
+
for (let slot = 0; slot < capacity; slot++) {
|
|
357
|
+
const token = atomicsLoad(processes, slot * PROCESS_WORDS);
|
|
358
|
+
if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== pid) continue;
|
|
359
|
+
try {
|
|
360
|
+
const state = directoryState({ directory, slot, pid });
|
|
361
|
+
if (controlLoad(state.jobPending, 0) >= 0) return state;
|
|
362
|
+
} catch (error) {
|
|
363
|
+
if (!isCollectedProcess(error)) throw error;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
/** Read-only logical-PID scan: returns live/zombie members whose job is not sealed. */
|
|
369
|
+
export function peekDirectoryProcessesByLogicalPid(
|
|
370
|
+
directory: SharedArrayBuffer,
|
|
371
|
+
processId: number,
|
|
372
|
+
callerSlot?: number,
|
|
373
|
+
callerPid?: number,
|
|
374
|
+
): ReturnType<typeof directoryState>[] {
|
|
375
|
+
const matches: ReturnType<typeof directoryState>[] = [];
|
|
376
|
+
if (!Number.isInteger(processId) || processId <= 0 || processId > 0x7fffffff) return matches;
|
|
377
|
+
const capacity = directoryCapacity(directory);
|
|
378
|
+
const processes = new BigInt64ArrayConstructor(directory, HEADER_BYTES, capacity * PROCESS_WORDS);
|
|
379
|
+
for (let slot = 0; slot < capacity; slot++) {
|
|
380
|
+
const base = slot * PROCESS_WORDS;
|
|
381
|
+
const token = atomicsLoad(processes, base);
|
|
382
|
+
if ((token & 0xffffffffn) !== 1n) continue;
|
|
383
|
+
const pid = Number(token >> 32n);
|
|
384
|
+
if (callerSlot !== undefined && slot === callerSlot && pid !== callerPid) continue;
|
|
385
|
+
if (atomicsLoad(processes, base + PROCESS_ID) !== packedControl(pid, processId)) continue;
|
|
386
|
+
try {
|
|
387
|
+
const state = directoryState({ directory, slot, pid });
|
|
388
|
+
if (controlLoad(state.jobPending, 0) >= 0) arrayAppend(matches, state);
|
|
389
|
+
} catch (error) {
|
|
390
|
+
if (!isCollectedProcess(error)) throw error;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return matches;
|
|
394
|
+
}
|
|
395
|
+
/** Read-only execution-handle probe against an authoritative shared directory. */
|
|
396
|
+
export function probeDirectoryProcess(
|
|
397
|
+
directory: SharedArrayBuffer,
|
|
398
|
+
callerPid: number,
|
|
399
|
+
pid: number,
|
|
400
|
+
): boolean {
|
|
401
|
+
const target = peekDirectoryProcess(directory, pid);
|
|
402
|
+
if (!target) throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
403
|
+
try {
|
|
404
|
+
return directoryControls(
|
|
405
|
+
directory,
|
|
406
|
+
() => {
|
|
407
|
+
controlLoad(target.controlParent);
|
|
408
|
+
return target.pid;
|
|
409
|
+
},
|
|
410
|
+
callerPid,
|
|
411
|
+
);
|
|
412
|
+
} catch (error) {
|
|
413
|
+
if (isCollectedProcess(error))
|
|
414
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
415
|
+
throw error;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
/** Read-only logical-PID probe against an authoritative shared directory. */
|
|
419
|
+
export function probeDirectoryLogicalPid(
|
|
420
|
+
directory: SharedArrayBuffer,
|
|
421
|
+
callerPid: number,
|
|
422
|
+
processId: number,
|
|
423
|
+
callerSlot?: number,
|
|
424
|
+
): boolean {
|
|
425
|
+
const matches = peekDirectoryProcessesByLogicalPid(directory, processId, callerSlot, callerPid);
|
|
426
|
+
let found = false;
|
|
427
|
+
for (let index = 0; index < matches.length; index++) {
|
|
428
|
+
const target = matches[index]!;
|
|
429
|
+
try {
|
|
430
|
+
const controlled = directoryControls(
|
|
431
|
+
directory,
|
|
432
|
+
() => {
|
|
433
|
+
controlLoad(target.controlParent);
|
|
434
|
+
return target.pid;
|
|
435
|
+
},
|
|
436
|
+
callerPid,
|
|
437
|
+
);
|
|
438
|
+
found = true;
|
|
439
|
+
if (controlled) return true;
|
|
440
|
+
} catch (error) {
|
|
441
|
+
if (!isCollectedProcess(error)) throw error;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (!found) throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
/** Read-only job probe against an authoritative shared directory. */
|
|
448
|
+
export function probeDirectoryJob(
|
|
449
|
+
directory: SharedArrayBuffer,
|
|
450
|
+
callerPid: number,
|
|
451
|
+
job: number,
|
|
452
|
+
): boolean {
|
|
453
|
+
const group = peekDirectoryJob(directory, job);
|
|
454
|
+
if (!group) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
455
|
+
try {
|
|
456
|
+
return directoryControls(directory, () => controlLoad(group.controlParent), callerPid, true);
|
|
457
|
+
} catch (error) {
|
|
458
|
+
if (isCollectedProcess(error)) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
459
|
+
throw error;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
/** Read-only job lookup: never assists sealed collection or wakes the directory. */
|
|
463
|
+
export function peekDirectoryJob(
|
|
464
|
+
directory: SharedArrayBuffer,
|
|
465
|
+
id: number,
|
|
466
|
+
): ReturnType<typeof directoryJob> | undefined {
|
|
467
|
+
for (let slot = 0; slot < directoryCapacity(directory); slot++) {
|
|
468
|
+
const raw = row(directory, slot, true);
|
|
469
|
+
const token = atomicsLoad(raw, 0);
|
|
470
|
+
if ((token & 0xffffffffn) !== 1n || Number(token >> 32n) !== id) continue;
|
|
471
|
+
try {
|
|
472
|
+
if (controlLoad(cells(raw, id, 4)) < 0) return undefined;
|
|
473
|
+
return directoryJob(directory, slot, id);
|
|
474
|
+
} catch (error) {
|
|
475
|
+
if (!isCollectedProcess(error)) throw error;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return undefined;
|
|
479
|
+
}
|
|
349
480
|
export function findDirectoryJob(
|
|
350
481
|
directory: SharedArrayBuffer,
|
|
351
482
|
id: number,
|
package/proc/errors.ts
CHANGED
package/proc/supervisor.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { admitProcessObservation, captureNativeReturn } from "./observation.js";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
directoryState,
|
|
4
|
+
directoryHasProcessSibling,
|
|
5
|
+
peekDirectoryJob,
|
|
6
|
+
peekDirectoryProcess,
|
|
7
|
+
probeDirectoryJob,
|
|
8
|
+
probeDirectoryLogicalPid,
|
|
9
|
+
probeDirectoryProcess,
|
|
10
|
+
} from "./directory.js";
|
|
3
11
|
import { allocateSharedControl } from "../memory/control.ts";
|
|
4
12
|
import {
|
|
5
13
|
isInstance,
|
|
@@ -56,13 +64,14 @@ import {
|
|
|
56
64
|
ProcessErrno,
|
|
57
65
|
ProcessError,
|
|
58
66
|
Signal,
|
|
59
|
-
|
|
67
|
+
checkedKillSignal,
|
|
60
68
|
checkedJobLifetime,
|
|
61
69
|
checkedProcessCwd,
|
|
62
70
|
snapshotSpawnWorker,
|
|
63
71
|
checkedProcessKind,
|
|
64
72
|
snapshotLogicalProcessIdentity,
|
|
65
73
|
type AdmittedSpawnReservation,
|
|
74
|
+
type JobLifetime,
|
|
66
75
|
type ProcessKind,
|
|
67
76
|
type LogicalProcessIdentity,
|
|
68
77
|
type ChildExitEvent,
|
|
@@ -378,10 +387,26 @@ export class ProcessSupervisor {
|
|
|
378
387
|
this.access(pid, id).owner.requests.delete(id);
|
|
379
388
|
return 0;
|
|
380
389
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
this.
|
|
384
|
-
|
|
390
|
+
private requireLiveCaller(callerPid: number): ProcessRecord {
|
|
391
|
+
const caller = this.table.require(callerPid);
|
|
392
|
+
if (this.disposed || caller.result !== null || readOutcome(caller.outcome))
|
|
393
|
+
throw new ProcessError(ProcessErrno.SRCH, "process has exited");
|
|
394
|
+
return caller;
|
|
395
|
+
}
|
|
396
|
+
private callerDirectory(caller: ProcessRecord): SharedArrayBuffer | undefined {
|
|
397
|
+
return isInstance(caller.outcome, OutcomeCell) ? caller.outcome.validity.buffer : undefined;
|
|
398
|
+
}
|
|
399
|
+
private isLiveDirectoryRecord(record: ProcessRecord): boolean {
|
|
400
|
+
return (
|
|
401
|
+
!isInstance(record.outcome, OutcomeCell) ||
|
|
402
|
+
peekDirectoryProcess(record.outcome.validity.buffer, record.pid) !== undefined
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
private isLiveDirectoryJob(jobId: number): boolean {
|
|
406
|
+
const directory = this.table.jobDirectory(jobId);
|
|
407
|
+
return !directory || peekDirectoryJob(directory, jobId) !== undefined;
|
|
408
|
+
}
|
|
409
|
+
private controls(callerPid: number, pid: number): boolean {
|
|
385
410
|
let target: number | undefined = pid;
|
|
386
411
|
while (target !== undefined && target !== 0) {
|
|
387
412
|
if (target === callerPid) return true;
|
|
@@ -389,12 +414,94 @@ export class ProcessSupervisor {
|
|
|
389
414
|
}
|
|
390
415
|
return false;
|
|
391
416
|
}
|
|
417
|
+
/** Guest adapters may signal themselves and descendants, never a sibling or ancestor. */
|
|
418
|
+
canSignal(callerPid: number, pid: number): boolean {
|
|
419
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
420
|
+
const target = this.table.require(pid);
|
|
421
|
+
if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
|
|
422
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
423
|
+
return this.controls(callerPid, pid);
|
|
424
|
+
}
|
|
425
|
+
/** Signal 0 execution-handle probe: authoritative shared directory for directory-backed targets. */
|
|
426
|
+
canProbe(callerPid: number, pid: number): boolean {
|
|
427
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
428
|
+
const target = this.table.get(pid);
|
|
429
|
+
if (target) {
|
|
430
|
+
if (caller.rootPid === undefined || target.rootPid !== caller.rootPid)
|
|
431
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
432
|
+
if (isInstance(target.outcome, OutcomeCell)) {
|
|
433
|
+
if (!peekDirectoryProcess(target.outcome.validity.buffer, pid))
|
|
434
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
435
|
+
return (
|
|
436
|
+
this.controls(callerPid, pid) ||
|
|
437
|
+
probeDirectoryProcess(target.outcome.validity.buffer, callerPid, pid)
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
return this.controls(callerPid, pid);
|
|
441
|
+
}
|
|
442
|
+
const directory = this.callerDirectory(caller);
|
|
443
|
+
if (directory) return probeDirectoryProcess(directory, callerPid, pid);
|
|
444
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
445
|
+
}
|
|
446
|
+
/** Probe a logical process PID P across live and uncollected zombie members. */
|
|
447
|
+
canSignalProcess(callerPid: number, processId: number): boolean {
|
|
448
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
449
|
+
if (
|
|
450
|
+
caller.rootPid === undefined ||
|
|
451
|
+
!Number.isInteger(processId) ||
|
|
452
|
+
processId <= 0 ||
|
|
453
|
+
processId > 0x7fffffff
|
|
454
|
+
)
|
|
455
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
|
|
456
|
+
let found = false;
|
|
457
|
+
for (const record of this.table.values()) {
|
|
458
|
+
if (
|
|
459
|
+
record.rootPid !== caller.rootPid ||
|
|
460
|
+
record.processIdentity.pid !== processId ||
|
|
461
|
+
!this.isLiveDirectoryRecord(record)
|
|
462
|
+
)
|
|
463
|
+
continue;
|
|
464
|
+
found = true;
|
|
465
|
+
if (this.controls(callerPid, record.pid)) return true;
|
|
466
|
+
}
|
|
467
|
+
const directory = this.callerDirectory(caller);
|
|
468
|
+
if (directory) {
|
|
469
|
+
try {
|
|
470
|
+
if (probeDirectoryLogicalPid(directory, callerPid, processId)) return true;
|
|
471
|
+
found = true;
|
|
472
|
+
} catch (error) {
|
|
473
|
+
if (!(error instanceof ProcessError) || error.errno !== ProcessErrno.SRCH) throw error;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (!found) throw new ProcessError(ProcessErrno.SRCH, `unknown process ${processId}`);
|
|
477
|
+
return false;
|
|
478
|
+
}
|
|
392
479
|
canSignalJob(callerPid: number, jobId: number): boolean {
|
|
393
|
-
this.
|
|
480
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
394
481
|
const job = this.table.job(jobId);
|
|
395
|
-
if (!job
|
|
482
|
+
if (!job || caller.rootPid === undefined || job.rootPid !== caller.rootPid)
|
|
483
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
396
484
|
return job.controlParent === callerPid;
|
|
397
485
|
}
|
|
486
|
+
/** Signal 0 job probe: authoritative shared directory for directory-backed jobs. */
|
|
487
|
+
canProbeJob(callerPid: number, jobId: number): boolean {
|
|
488
|
+
const caller = this.requireLiveCaller(callerPid);
|
|
489
|
+
const job = this.table.job(jobId);
|
|
490
|
+
if (job) {
|
|
491
|
+
if (caller.rootPid === undefined || job.rootPid !== caller.rootPid)
|
|
492
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
493
|
+
const directory = this.table.jobDirectory(jobId);
|
|
494
|
+
if (directory) {
|
|
495
|
+
if (!peekDirectoryJob(directory, jobId))
|
|
496
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
497
|
+
return job.controlParent === callerPid || probeDirectoryJob(directory, callerPid, jobId);
|
|
498
|
+
}
|
|
499
|
+
return job.controlParent === callerPid;
|
|
500
|
+
}
|
|
501
|
+
const directory = this.callerDirectory(caller);
|
|
502
|
+
if (directory) return probeDirectoryJob(directory, callerPid, jobId);
|
|
503
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
504
|
+
}
|
|
398
505
|
/** A job is controlled by its owner and every live or retained ancestor of that owner. */
|
|
399
506
|
canWaitJob(callerPid: number, jobId: number): boolean {
|
|
400
507
|
// A synchronously accepted worker reap can reach the page after its caller was reaped
|
|
@@ -514,11 +621,14 @@ export class ProcessSupervisor {
|
|
|
514
621
|
isInstance(options.outcome, OutcomeCell)
|
|
515
622
|
? {
|
|
516
623
|
ownerPid: pid,
|
|
624
|
+
rootPid: pid,
|
|
517
625
|
leaderPid: pid,
|
|
518
626
|
outcome: options.outcome,
|
|
519
627
|
foreground: options.foreground ?? true,
|
|
520
628
|
}
|
|
521
629
|
: undefined,
|
|
630
|
+
"independent",
|
|
631
|
+
pid,
|
|
522
632
|
);
|
|
523
633
|
this.table.insert(
|
|
524
634
|
{
|
|
@@ -617,32 +727,14 @@ export class ProcessSupervisor {
|
|
|
617
727
|
const processIdentity = this.spawnIdentity(pid, processKind, parent, capturedIdentity);
|
|
618
728
|
this.table.checkPid(pid, admitted ? reservation.outcome : undefined);
|
|
619
729
|
const observation = admitProcessObservation(pid, outcome, reservation.observation);
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
job,
|
|
629
|
-
jobIdentity,
|
|
630
|
-
jobLifetime,
|
|
631
|
-
);
|
|
632
|
-
createdJob = true;
|
|
633
|
-
}
|
|
634
|
-
if (job === undefined) {
|
|
635
|
-
const group = this.table.createJob(
|
|
636
|
-
parentPid,
|
|
637
|
-
selector === 0,
|
|
638
|
-
undefined,
|
|
639
|
-
undefined,
|
|
640
|
-
jobLifetime,
|
|
641
|
-
);
|
|
642
|
-
group.controlParent = reservation.controlParent ?? parentPid;
|
|
643
|
-
job = group.id;
|
|
644
|
-
createdJob = true;
|
|
645
|
-
}
|
|
730
|
+
const { job, createdJob } = this.resolveSpawnJob(
|
|
731
|
+
parentPid,
|
|
732
|
+
parent,
|
|
733
|
+
selector,
|
|
734
|
+
jobLifetime,
|
|
735
|
+
reservation,
|
|
736
|
+
jobIdentity,
|
|
737
|
+
);
|
|
646
738
|
if (jobIdentity) this.table.adoptJobIdentity(job, jobIdentity);
|
|
647
739
|
this.checkJobAdmission(job, admitted);
|
|
648
740
|
const descriptors = new DescriptorTable();
|
|
@@ -709,6 +801,41 @@ export class ProcessSupervisor {
|
|
|
709
801
|
}
|
|
710
802
|
return pid;
|
|
711
803
|
}
|
|
804
|
+
private resolveSpawnJob(
|
|
805
|
+
parentPid: number,
|
|
806
|
+
parent: ProcessRecord | undefined,
|
|
807
|
+
selector: number,
|
|
808
|
+
jobLifetime: JobLifetime,
|
|
809
|
+
reservation: SpawnReservation,
|
|
810
|
+
jobIdentity?: SharedJobIdentity,
|
|
811
|
+
): { job: number; createdJob: boolean } {
|
|
812
|
+
const rootPid = reservation.rootPid ?? parent?.rootPid;
|
|
813
|
+
const job = reservation.job ?? (selector > 0 ? selector : undefined);
|
|
814
|
+
if (job === undefined) {
|
|
815
|
+
const group = this.table.createJob(
|
|
816
|
+
parentPid,
|
|
817
|
+
selector === 0,
|
|
818
|
+
undefined,
|
|
819
|
+
undefined,
|
|
820
|
+
jobLifetime,
|
|
821
|
+
rootPid,
|
|
822
|
+
);
|
|
823
|
+
group.controlParent = reservation.controlParent ?? parentPid;
|
|
824
|
+
return { job: group.id, createdJob: true };
|
|
825
|
+
}
|
|
826
|
+
if (this.table.job(job)) return { job, createdJob: false };
|
|
827
|
+
if (reservation.job === undefined)
|
|
828
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${job}`);
|
|
829
|
+
this.table.createJob(
|
|
830
|
+
jobIdentity?.ownerPid ?? parentPid,
|
|
831
|
+
jobIdentity?.foreground ?? selector === 0,
|
|
832
|
+
job,
|
|
833
|
+
jobIdentity,
|
|
834
|
+
jobLifetime,
|
|
835
|
+
rootPid,
|
|
836
|
+
);
|
|
837
|
+
return { job, createdJob: true };
|
|
838
|
+
}
|
|
712
839
|
private checkSpawnParent(
|
|
713
840
|
parentPid: number,
|
|
714
841
|
parent: ProcessRecord | undefined,
|
|
@@ -1213,8 +1340,13 @@ export class ProcessSupervisor {
|
|
|
1213
1340
|
}
|
|
1214
1341
|
/** Default signals terminate immediately; registered handlers receive shared pending delivery. */
|
|
1215
1342
|
kill(pid: number, signal: number): void {
|
|
1216
|
-
const sig =
|
|
1343
|
+
const sig = checkedKillSignal(signal),
|
|
1217
1344
|
record = this.table.require(pid);
|
|
1345
|
+
if (sig === 0) {
|
|
1346
|
+
if (!this.isLiveDirectoryRecord(record))
|
|
1347
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown process ${pid}`);
|
|
1348
|
+
return;
|
|
1349
|
+
}
|
|
1218
1350
|
if (record.result) return;
|
|
1219
1351
|
this.recordSignal(record, sig);
|
|
1220
1352
|
if (record.signal === Signal.SIGKILL || readOutcome(record.outcome))
|
|
@@ -1267,9 +1399,14 @@ export class ProcessSupervisor {
|
|
|
1267
1399
|
this.finish(pid, winner);
|
|
1268
1400
|
}
|
|
1269
1401
|
killJob(jobId: number, signal: number): void {
|
|
1270
|
-
const sig =
|
|
1402
|
+
const sig = checkedKillSignal(signal);
|
|
1271
1403
|
const job = this.table.job(jobId);
|
|
1272
1404
|
if (!job) throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
1405
|
+
if (sig === 0) {
|
|
1406
|
+
if (!this.isLiveDirectoryJob(jobId))
|
|
1407
|
+
throw new ProcessError(ProcessErrno.SRCH, `unknown job ${jobId}`);
|
|
1408
|
+
return;
|
|
1409
|
+
}
|
|
1273
1410
|
// New background jobs are independent, even when their parent belongs to this job.
|
|
1274
1411
|
const targets = Array.from(job.members, (pid) => this.table.require(pid));
|
|
1275
1412
|
// Record every target before callbacks: a dying process cannot spawn through an as-yet
|
package/proc/table.ts
CHANGED
|
@@ -109,6 +109,7 @@ export interface ProcessReplacement {
|
|
|
109
109
|
export interface ProcessJob {
|
|
110
110
|
readonly id: number;
|
|
111
111
|
readonly ownerPid: number;
|
|
112
|
+
readonly rootPid: number;
|
|
112
113
|
controlParent: number;
|
|
113
114
|
readonly foreground: boolean;
|
|
114
115
|
readonly jobLifetime: JobLifetime;
|
|
@@ -181,6 +182,14 @@ export class ProcessTable {
|
|
|
181
182
|
const outcome = this.jobs.get(id)?.leaderOutcome;
|
|
182
183
|
return isInstance(outcome, OutcomeCell) && !outcome.valid();
|
|
183
184
|
}
|
|
185
|
+
jobDirectory(id: number): SharedArrayBuffer | undefined {
|
|
186
|
+
const job = this.jobs.get(id);
|
|
187
|
+
if (!job) return undefined;
|
|
188
|
+
const outcome =
|
|
189
|
+
job.leaderOutcome ??
|
|
190
|
+
(job.leaderPid === undefined ? undefined : this.records.get(job.leaderPid)?.outcome);
|
|
191
|
+
return isInstance(outcome, OutcomeCell) ? outcome.validity.buffer : undefined;
|
|
192
|
+
}
|
|
184
193
|
values(): IterableIterator<ProcessRecord> {
|
|
185
194
|
return this.records.values();
|
|
186
195
|
}
|
|
@@ -198,6 +207,7 @@ export class ProcessTable {
|
|
|
198
207
|
id = this.identities.job(),
|
|
199
208
|
identity?: SharedJobIdentity,
|
|
200
209
|
jobLifetime: JobLifetime = "independent",
|
|
210
|
+
rootPid?: number,
|
|
201
211
|
): ProcessJob {
|
|
202
212
|
if (isInstance(identity?.outcome, OutcomeCell)) identity.outcome.check();
|
|
203
213
|
const captured = isInstance(identity?.outcome, OutcomeCell);
|
|
@@ -207,6 +217,7 @@ export class ProcessTable {
|
|
|
207
217
|
const job: MutableProcessJob = {
|
|
208
218
|
id,
|
|
209
219
|
ownerPid,
|
|
220
|
+
rootPid: rootPid ?? identity?.rootPid ?? this.records.get(ownerPid)?.rootPid ?? ownerPid,
|
|
210
221
|
controlParent: identity?.controlParent ?? ownerPid,
|
|
211
222
|
foreground,
|
|
212
223
|
jobLifetime: identity?.jobLifetime ?? jobLifetime,
|
package/proc/types.ts
CHANGED
|
@@ -122,6 +122,7 @@ export function snapshotSpawnWorker(worker: unknown): Pick<SpawnRequest, "worker
|
|
|
122
122
|
export interface SharedJobIdentity {
|
|
123
123
|
readonly ownerPid: number;
|
|
124
124
|
readonly controlParent?: number;
|
|
125
|
+
readonly rootPid?: number;
|
|
125
126
|
readonly leaderPid: number;
|
|
126
127
|
readonly outcome: OutcomeWord;
|
|
127
128
|
readonly foreground: boolean;
|
|
@@ -198,3 +199,8 @@ export function checkedSignal(signal: number): ProcessSignal {
|
|
|
198
199
|
}
|
|
199
200
|
return signal;
|
|
200
201
|
}
|
|
202
|
+
/** Signal 0 is valid only for existence/permission probes on kill and killJob, never delivery. */
|
|
203
|
+
export function checkedKillSignal(signal: number): number {
|
|
204
|
+
if (signal === 0) return 0;
|
|
205
|
+
return checkedSignal(signal);
|
|
206
|
+
}
|