@waffle-os/runtime 0.0.156 → 0.0.157
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/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +71 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +397 -46
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +13 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +353 -27
- package/dist/fs/backend.js +29 -2
- package/dist/fs/client.js +199 -6
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +109 -1
- package/dist/fs/multitab-backend.js +315 -195
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +13 -6
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +386 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +33 -1
- package/fs/client.ts +225 -6
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +114 -1
- package/fs/multitab-backend.ts +561 -303
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +14 -3
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +468 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
package/core/wasi.ts
CHANGED
|
@@ -34,18 +34,37 @@ import {
|
|
|
34
34
|
import { resolveDescriptorPath, reopenDescriptor, openInode } from "./fd-reopen.js";
|
|
35
35
|
import { ExecutionUnwind } from "./unwind.js";
|
|
36
36
|
import { type ControlCells, controlLoad, controlWait } from "../proc/control.js";
|
|
37
|
-
import {
|
|
37
|
+
import {
|
|
38
|
+
markResourceOfdLocked,
|
|
39
|
+
releaseOriginOfd,
|
|
40
|
+
resourceOfdId,
|
|
41
|
+
withFileOffset,
|
|
42
|
+
} from "./open-file.js";
|
|
38
43
|
// WASI preview1 adapter over filesystem handles and shared open descriptions.
|
|
39
44
|
// Runs synchronously in a guest worker; services supply blocking I/O and readiness.
|
|
40
45
|
|
|
41
46
|
import {
|
|
47
|
+
F_GETLK,
|
|
48
|
+
F_OFD_GETLK,
|
|
49
|
+
F_OFD_SETLK,
|
|
50
|
+
F_OFD_SETLKW,
|
|
51
|
+
F_RDLCK,
|
|
52
|
+
F_SETLKW,
|
|
53
|
+
F_UNLCK,
|
|
54
|
+
F_WRLCK,
|
|
42
55
|
FilesystemError,
|
|
43
56
|
fileTimeNow,
|
|
57
|
+
LOCK_EX,
|
|
58
|
+
LOCK_NB,
|
|
59
|
+
LOCK_SH,
|
|
60
|
+
LOCK_UN,
|
|
61
|
+
permissionsFor,
|
|
44
62
|
PROBE_PATH_LIMIT,
|
|
45
63
|
type FileHandle,
|
|
46
64
|
type FileSystem,
|
|
47
65
|
type FileStat,
|
|
48
66
|
type FileTime,
|
|
67
|
+
type LockWaitControl,
|
|
49
68
|
type ProbeKind,
|
|
50
69
|
type ResolvedPath,
|
|
51
70
|
} from "./filesystem.js";
|
|
@@ -53,16 +72,27 @@ import {
|
|
|
53
72
|
DescriptorTable,
|
|
54
73
|
OpenDescription,
|
|
55
74
|
MAX_DESCRIPTOR,
|
|
75
|
+
type Descriptor,
|
|
56
76
|
type Readiness,
|
|
57
77
|
type Resource,
|
|
58
78
|
type Stream,
|
|
59
79
|
} from "./descriptors.js";
|
|
60
80
|
import { memoryViews, type MemoryViews } from "./memory-views.js";
|
|
61
81
|
import { GuestWatchTable, type GuestWatchReadiness } from "../fs/guest-watch.js";
|
|
62
|
-
import {
|
|
82
|
+
import {
|
|
83
|
+
createMachineIdFile,
|
|
84
|
+
createNullDevice,
|
|
85
|
+
MACHINE_ID_FILE_SIZE,
|
|
86
|
+
MACHINE_ID_INO,
|
|
87
|
+
readVirtualFileBytes,
|
|
88
|
+
seekVirtualFile,
|
|
89
|
+
tellVirtualFile,
|
|
90
|
+
validateMachineId,
|
|
91
|
+
} from "./devices.js";
|
|
63
92
|
|
|
64
93
|
// errno
|
|
65
94
|
const SUCCESS = 0;
|
|
95
|
+
const EACCES = 2;
|
|
66
96
|
const EBADF = 8;
|
|
67
97
|
const EEXIST = 20;
|
|
68
98
|
const EFAULT = 21;
|
|
@@ -79,6 +109,7 @@ const ENOSYS = 52;
|
|
|
79
109
|
const ENOTDIR = 54;
|
|
80
110
|
const ENOTSUP = 58;
|
|
81
111
|
const ENOTSOCK = 57;
|
|
112
|
+
const EOVERFLOW = 61;
|
|
82
113
|
const ESPIPE = 70;
|
|
83
114
|
const ENOTCAPABLE = 76;
|
|
84
115
|
|
|
@@ -128,9 +159,13 @@ export class WasiExit extends ExecutionUnwind {
|
|
|
128
159
|
}
|
|
129
160
|
|
|
130
161
|
export interface WasiOptions {
|
|
162
|
+
/** Logical process PID reported in advisory lock queries (F_GETLK). Defaults to 1. */
|
|
163
|
+
pid?: number;
|
|
131
164
|
args: readonly string[];
|
|
132
165
|
env: Record<string, string>;
|
|
133
166
|
fs: FileSystem;
|
|
167
|
+
/** Optional explicit 32-char lowercase hex machine-id(5) override for virtual `/etc/machine-id`. */
|
|
168
|
+
machineId?: string;
|
|
134
169
|
/** Explicit guest adapter namespaces; unknown imports fail before instantiation. */
|
|
135
170
|
namespaces?: WebAssembly.Imports;
|
|
136
171
|
/** Reserved socket number used by a guest adapter, not an ordinary descriptor. */
|
|
@@ -149,6 +184,8 @@ export interface WasiOptions {
|
|
|
149
184
|
diagnostic?(data: Uint8Array): void;
|
|
150
185
|
/** Blocks the calling thread for `ms` milliseconds when no shared notification exists. */
|
|
151
186
|
sleep(ms: number): void;
|
|
187
|
+
/** Checked before blocking waits so fatal signals unwind instead of returning EINTR. */
|
|
188
|
+
checkActive?(): void;
|
|
152
189
|
/** Wake word; without interruptPending, a nonzero value is ready. Poll never consumes it. */
|
|
153
190
|
interrupt?: ControlCells;
|
|
154
191
|
/** Nonconsuming readiness when interrupt is advisory (for example handled process signals). */
|
|
@@ -292,6 +329,7 @@ export class Wasi {
|
|
|
292
329
|
hostCall: ((slot: number, args: number[]) => number) | null = null;
|
|
293
330
|
readonly descriptors = new DescriptorTable();
|
|
294
331
|
private watches?: GuestWatchTable;
|
|
332
|
+
private posixLockedHandles?: Set<FileHandle>;
|
|
295
333
|
private importedMemory?: WebAssembly.Memory;
|
|
296
334
|
private boundMemory?: WebAssembly.Memory;
|
|
297
335
|
private hostWrite?: FdWrite;
|
|
@@ -304,6 +342,50 @@ export class Wasi {
|
|
|
304
342
|
private rootedSnapshot?: boolean;
|
|
305
343
|
/** The handle a `/` preopen spelled exactly `/` opened: the filesystem root, known for free. */
|
|
306
344
|
private literalRoot?: number;
|
|
345
|
+
private readonly explicitMachineId?: string;
|
|
346
|
+
private machineId(): string | undefined {
|
|
347
|
+
return this.explicitMachineId ?? this.opts.fs.machineId;
|
|
348
|
+
}
|
|
349
|
+
private virtualMachineIdStat(): FileStat {
|
|
350
|
+
return {
|
|
351
|
+
kind: "file",
|
|
352
|
+
ino: MACHINE_ID_INO,
|
|
353
|
+
nlink: 1,
|
|
354
|
+
size: MACHINE_ID_FILE_SIZE,
|
|
355
|
+
atime: 0n,
|
|
356
|
+
mtime: 0n,
|
|
357
|
+
ctime: 0n,
|
|
358
|
+
mode: 0o444,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
private resolveVirtualMachineId(
|
|
362
|
+
entry: Descriptor,
|
|
363
|
+
path: string,
|
|
364
|
+
follow: boolean,
|
|
365
|
+
): "machine-id" | number | undefined {
|
|
366
|
+
if (this.machineId() === undefined) return undefined;
|
|
367
|
+
const alias = resolveDescriptorPath(
|
|
368
|
+
this.descriptors,
|
|
369
|
+
this.opts.fs,
|
|
370
|
+
entry,
|
|
371
|
+
path,
|
|
372
|
+
follow ? "follow" : "nofollow",
|
|
373
|
+
this.rooted,
|
|
374
|
+
true,
|
|
375
|
+
);
|
|
376
|
+
if (typeof alias === "number") return alias;
|
|
377
|
+
if (alias !== undefined && "virtualFile" in alias) return alias.virtualFile;
|
|
378
|
+
return undefined;
|
|
379
|
+
}
|
|
380
|
+
private relativeVirtualMachineId(handle: FileHandle): string {
|
|
381
|
+
const rootHandle = this.opts.fs.preopen("/");
|
|
382
|
+
if (
|
|
383
|
+
handle === rootHandle ||
|
|
384
|
+
this.opts.fs.stat(handle).ino === this.opts.fs.stat(rootHandle).ino
|
|
385
|
+
)
|
|
386
|
+
return "etc/machine-id";
|
|
387
|
+
return "machine-id";
|
|
388
|
+
}
|
|
307
389
|
private get rooted(): boolean {
|
|
308
390
|
return (this.rootedSnapshot ??= this.rootNamespace());
|
|
309
391
|
}
|
|
@@ -345,6 +427,7 @@ export class Wasi {
|
|
|
345
427
|
}
|
|
346
428
|
|
|
347
429
|
constructor(private opts: WasiOptions) {
|
|
430
|
+
if (opts.machineId !== undefined) this.explicitMachineId = validateMachineId(opts.machineId);
|
|
348
431
|
const reserved = new Set<number>();
|
|
349
432
|
for (const fd of opts.reservedFds ?? []) {
|
|
350
433
|
if (!Number.isInteger(fd) || fd < 0 || fd > MAX_DESCRIPTOR || reserved.has(fd))
|
|
@@ -390,6 +473,9 @@ export class Wasi {
|
|
|
390
473
|
this.openFileAt(fd, { kind: "directory", handle }, RIGHTS_ALL, RIGHTS_ALL, name);
|
|
391
474
|
fd++;
|
|
392
475
|
}
|
|
476
|
+
if (opts.fs.locks) {
|
|
477
|
+
this.descriptors.setOnClose((closed) => this.releasePosixHandle(closed.resource));
|
|
478
|
+
}
|
|
393
479
|
}
|
|
394
480
|
|
|
395
481
|
private openDevice(
|
|
@@ -409,6 +495,26 @@ export class Wasi {
|
|
|
409
495
|
);
|
|
410
496
|
}
|
|
411
497
|
|
|
498
|
+
private openVirtualMachineId(
|
|
499
|
+
fdflags: number,
|
|
500
|
+
base: bigint,
|
|
501
|
+
inheriting: bigint,
|
|
502
|
+
oflags: number,
|
|
503
|
+
out: number,
|
|
504
|
+
): number {
|
|
505
|
+
if ((out >>> 0) + 4 > this.bytes().length) return EFAULT;
|
|
506
|
+
if (oflags & O_DIRECTORY) return ENOTDIR;
|
|
507
|
+
if (oflags & O_CREAT && oflags & O_EXCL) return EEXIST;
|
|
508
|
+
if (oflags & ~15 || fdflags & ~31) return EINVAL;
|
|
509
|
+
if ((oflags & O_TRUNC) !== 0 || (base & RIGHT_FD_WRITE) !== 0n) return EACCES;
|
|
510
|
+
const id = this.machineId();
|
|
511
|
+
if (id === undefined) return ENOENT;
|
|
512
|
+
return this.writeResult(
|
|
513
|
+
this.descriptors.open(createMachineIdFile(id, fdflags), base, inheriting),
|
|
514
|
+
out >>> 0,
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
|
|
412
518
|
private openFileAt(
|
|
413
519
|
fd: number,
|
|
414
520
|
resource: Extract<Resource, { handle: number }>,
|
|
@@ -422,11 +528,28 @@ export class Wasi {
|
|
|
422
528
|
resource,
|
|
423
529
|
base,
|
|
424
530
|
inheriting,
|
|
425
|
-
() => this.opts.fs
|
|
531
|
+
() => releaseOriginOfd(this.opts.fs, resource),
|
|
426
532
|
preopen,
|
|
427
533
|
);
|
|
428
534
|
}
|
|
429
535
|
|
|
536
|
+
/** Release all POSIX process-scoped byte-range locks held by this guest. */
|
|
537
|
+
releasePosixLocks(): void {
|
|
538
|
+
if (!this.posixLockedHandles || this.posixLockedHandles.size === 0) return;
|
|
539
|
+
const handles = Array.from(this.posixLockedHandles);
|
|
540
|
+
this.posixLockedHandles.clear();
|
|
541
|
+
const pid = this.opts.pid ?? 1;
|
|
542
|
+
for (let i = 0; i < handles.length; i++) {
|
|
543
|
+
this.opts.fs.locks?.releasePosix(handles[i]!, pid);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
/** Suppress per-descriptor POSIX lock release during thread-member teardown. */
|
|
548
|
+
suppressPosixClose(): void {
|
|
549
|
+
this.descriptors.setOnClose(undefined);
|
|
550
|
+
this.posixLockedHandles?.clear();
|
|
551
|
+
}
|
|
552
|
+
|
|
430
553
|
/** runtime_proc uses positive errno and checks output memory before changing the inode. */
|
|
431
554
|
fdReopen(fd: number, oflags: number, fdflags: number, base: bigint, out: number): number {
|
|
432
555
|
try {
|
|
@@ -444,6 +567,7 @@ export class Wasi {
|
|
|
444
567
|
base: bigint,
|
|
445
568
|
inheriting: bigint,
|
|
446
569
|
out: number,
|
|
570
|
+
intersectCapabilities = false,
|
|
447
571
|
): number {
|
|
448
572
|
if ((out >>> 0) + 4 > this.bytes().length) return 21;
|
|
449
573
|
const result = reopenDescriptor(
|
|
@@ -454,6 +578,7 @@ export class Wasi {
|
|
|
454
578
|
fdflags,
|
|
455
579
|
base,
|
|
456
580
|
inheriting,
|
|
581
|
+
intersectCapabilities,
|
|
457
582
|
);
|
|
458
583
|
return this.writeResult(result, out >>> 0);
|
|
459
584
|
}
|
|
@@ -527,6 +652,8 @@ export class Wasi {
|
|
|
527
652
|
watch_read: this.watchRead.bind(this),
|
|
528
653
|
watch_next: this.watchNext.bind(this),
|
|
529
654
|
watch_close: (id: number) => this.watches?.close(id >>> 0) ?? EBADF,
|
|
655
|
+
fd_flock: this.fdFlock.bind(this),
|
|
656
|
+
fd_lock: this.fdLock.bind(this),
|
|
530
657
|
};
|
|
531
658
|
const out: WebAssembly.Imports = Object.create(null);
|
|
532
659
|
this.importedMemory = undefined;
|
|
@@ -763,6 +890,166 @@ export class Wasi {
|
|
|
763
890
|
return SUCCESS;
|
|
764
891
|
}
|
|
765
892
|
|
|
893
|
+
private lockWaitControl(): LockWaitControl | undefined {
|
|
894
|
+
if (
|
|
895
|
+
!this.opts.checkActive &&
|
|
896
|
+
!this.opts.interruptPending &&
|
|
897
|
+
!this.opts.interrupt &&
|
|
898
|
+
!this.opts.wait
|
|
899
|
+
)
|
|
900
|
+
return undefined;
|
|
901
|
+
return {
|
|
902
|
+
checkActive: this.opts.checkActive,
|
|
903
|
+
interruptPending: () =>
|
|
904
|
+
this.opts.interruptPending?.() ??
|
|
905
|
+
(this.opts.interrupt !== undefined && controlLoad(this.opts.interrupt, 0) !== 0),
|
|
906
|
+
wait: this.opts.wait
|
|
907
|
+
? (word, expected, ms) => this.opts.wait!(word, expected, ms)
|
|
908
|
+
: undefined,
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
private resolveRangeBase(
|
|
913
|
+
resource: Extract<Resource, { kind: "file" | "directory" }>,
|
|
914
|
+
whence: number,
|
|
915
|
+
): bigint | null {
|
|
916
|
+
switch (whence) {
|
|
917
|
+
case 0:
|
|
918
|
+
return 0n;
|
|
919
|
+
case 1:
|
|
920
|
+
return BigInt(resource.kind === "file" ? resource.offset : 0);
|
|
921
|
+
case 2:
|
|
922
|
+
return BigInt(this.opts.fs.stat(resource.handle).size);
|
|
923
|
+
default:
|
|
924
|
+
return null;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
private resolveRange(
|
|
929
|
+
resource: Extract<Resource, { kind: "file" | "directory" }>,
|
|
930
|
+
whence: number,
|
|
931
|
+
start: bigint,
|
|
932
|
+
len: bigint,
|
|
933
|
+
): { readonly start: number; readonly len: number } | number {
|
|
934
|
+
const base = this.resolveRangeBase(resource, whence);
|
|
935
|
+
if (base === null) return EINVAL;
|
|
936
|
+
const pos = base + start;
|
|
937
|
+
if (len === 0n) {
|
|
938
|
+
if (pos < 0n) return EINVAL;
|
|
939
|
+
if (pos > MAX_FILE_SIZE) return EOVERFLOW;
|
|
940
|
+
return { start: Number(pos), len: 0 };
|
|
941
|
+
}
|
|
942
|
+
const rangeStart = len > 0n ? pos : pos + len;
|
|
943
|
+
const rangeEnd = len > 0n ? pos + len : pos;
|
|
944
|
+
if (rangeStart < 0n || rangeEnd < 0n) return EINVAL;
|
|
945
|
+
if (rangeStart > MAX_FILE_SIZE || rangeEnd > MAX_FILE_SIZE) return EOVERFLOW;
|
|
946
|
+
return { start: Number(rangeStart), len: Number(rangeEnd - rangeStart) };
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
private releasePosixHandle(resource: Resource): void {
|
|
950
|
+
if (resource.kind !== "file" && resource.kind !== "directory") return;
|
|
951
|
+
const tracked = this.posixLockedHandles?.delete(resource.handle) === true;
|
|
952
|
+
const locks = this.opts.fs.locks;
|
|
953
|
+
if (!locks) return;
|
|
954
|
+
if (!tracked && !locks.hasActivePosixLocks?.()) return;
|
|
955
|
+
locks.releasePosix(resource.handle, this.opts.pid ?? 1);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/** Release POSIX locks only for descriptors not inherited across a successful execve. */
|
|
959
|
+
releaseUninheritedPosixLocks(keptFds: ReadonlySet<number>): void {
|
|
960
|
+
this.descriptors.setOnClose(undefined);
|
|
961
|
+
const closedResources: Resource[] = [];
|
|
962
|
+
this.descriptors.forEach((entry, fd) => {
|
|
963
|
+
if (!keptFds.has(fd)) closedResources.push(entry.resource);
|
|
964
|
+
});
|
|
965
|
+
for (let i = 0; i < closedResources.length; i++) {
|
|
966
|
+
this.releasePosixHandle(closedResources[i]!);
|
|
967
|
+
}
|
|
968
|
+
this.posixLockedHandles?.clear();
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/** runtime_fs.fd_flock: advisory whole-file lock (flock(2)), OFD-scoped. */
|
|
972
|
+
private fdFlock(fd: number, operation: number): number {
|
|
973
|
+
const entry = this.descriptors.get(fd >>> 0);
|
|
974
|
+
if (!entry) return EBADF;
|
|
975
|
+
if (entry.resource.kind !== "file" && entry.resource.kind !== "directory") return EINVAL;
|
|
976
|
+
if ((operation & ~(LOCK_SH | LOCK_EX | LOCK_NB | LOCK_UN)) !== 0) return EINVAL;
|
|
977
|
+
const base = operation & ~LOCK_NB;
|
|
978
|
+
if (base !== LOCK_SH && base !== LOCK_EX && base !== LOCK_UN) return EINVAL;
|
|
979
|
+
const locks = this.opts.fs.locks;
|
|
980
|
+
if (!locks) return ENOTSUP;
|
|
981
|
+
const owner = locks.owner ?? 0;
|
|
982
|
+
const ofdId = resourceOfdId(entry.resource, owner);
|
|
983
|
+
const blocking = (operation & LOCK_NB) === 0 && base !== LOCK_UN;
|
|
984
|
+
const err = locks.flock(
|
|
985
|
+
entry.resource.handle,
|
|
986
|
+
ofdId,
|
|
987
|
+
operation,
|
|
988
|
+
blocking ? this.lockWaitControl() : undefined,
|
|
989
|
+
);
|
|
990
|
+
if (err === SUCCESS && base !== LOCK_UN) markResourceOfdLocked(entry.resource, owner);
|
|
991
|
+
return err;
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
/** runtime_fs.fd_lock: POSIX (F_GETLK/F_SETLK/F_SETLKW) and OFD byte-range advisory locks. */
|
|
995
|
+
private fdLock(
|
|
996
|
+
fd: number,
|
|
997
|
+
cmd: number,
|
|
998
|
+
type: number,
|
|
999
|
+
whence: number,
|
|
1000
|
+
start: bigint,
|
|
1001
|
+
len: bigint,
|
|
1002
|
+
outPtr: number,
|
|
1003
|
+
): number {
|
|
1004
|
+
if (cmd < F_GETLK || cmd > F_OFD_SETLKW) return EINVAL;
|
|
1005
|
+
if (type !== F_RDLCK && type !== F_WRLCK && type !== F_UNLCK) return EINVAL;
|
|
1006
|
+
if (whence < 0 || whence > 2) return EINVAL;
|
|
1007
|
+
const isGet = cmd === F_GETLK || cmd === F_OFD_GETLK;
|
|
1008
|
+
const out = outPtr >>> 0;
|
|
1009
|
+
if (isGet && out + 24 > this.bytes().length) return EFAULT;
|
|
1010
|
+
const entry = this.descriptors.get(fd >>> 0);
|
|
1011
|
+
if (!entry) return EBADF;
|
|
1012
|
+
if (entry.resource.kind !== "file" && entry.resource.kind !== "directory") return EINVAL;
|
|
1013
|
+
if (!isGet && type === F_RDLCK && !(entry.rightsBase & RIGHT_FD_READ)) return EBADF;
|
|
1014
|
+
if (!isGet && type === F_WRLCK && !(entry.rightsBase & RIGHT_FD_WRITE)) return EBADF;
|
|
1015
|
+
const locks = this.opts.fs.locks;
|
|
1016
|
+
if (!locks) return ENOTSUP;
|
|
1017
|
+
const resolved = this.resolveRange(entry.resource, whence, start, len);
|
|
1018
|
+
if (typeof resolved === "number") return resolved;
|
|
1019
|
+
const owner = locks.owner ?? 0;
|
|
1020
|
+
const isOfd = cmd === F_OFD_GETLK || cmd === F_OFD_SETLK || cmd === F_OFD_SETLKW;
|
|
1021
|
+
const ofdId = isOfd ? resourceOfdId(entry.resource, owner) : undefined;
|
|
1022
|
+
const range = { type, start: resolved.start, len: resolved.len } as const;
|
|
1023
|
+
const pid = this.opts.pid ?? 1;
|
|
1024
|
+
if (isGet) {
|
|
1025
|
+
const conflict = locks.getRangeLock(entry.resource.handle, ofdId, range, pid);
|
|
1026
|
+
if (typeof conflict === "number") return conflict;
|
|
1027
|
+
const view = this.view();
|
|
1028
|
+
if (out + 24 > view.byteLength) return EFAULT;
|
|
1029
|
+
view.setUint32(out, conflict.type, true);
|
|
1030
|
+
view.setInt32(out + 4, conflict.pid, true);
|
|
1031
|
+
view.setBigInt64(out + 8, BigInt(conflict.start), true);
|
|
1032
|
+
view.setBigInt64(out + 16, BigInt(conflict.len), true);
|
|
1033
|
+
return SUCCESS;
|
|
1034
|
+
}
|
|
1035
|
+
const blocking = (cmd === F_SETLKW || cmd === F_OFD_SETLKW) && type !== F_UNLCK;
|
|
1036
|
+
const err = locks.setRangeLock(
|
|
1037
|
+
entry.resource.handle,
|
|
1038
|
+
ofdId,
|
|
1039
|
+
pid,
|
|
1040
|
+
range,
|
|
1041
|
+
blocking,
|
|
1042
|
+
blocking ? this.lockWaitControl() : undefined,
|
|
1043
|
+
);
|
|
1044
|
+
if (err !== SUCCESS || type === F_UNLCK) return err;
|
|
1045
|
+
if (isOfd) {
|
|
1046
|
+
markResourceOfdLocked(entry.resource, owner);
|
|
1047
|
+
return err;
|
|
1048
|
+
}
|
|
1049
|
+
(this.posixLockedHandles ??= new Set()).add(entry.resource.handle);
|
|
1050
|
+
return err;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
766
1053
|
/** runtime_fs.path_realpath: realpath(3) of `path` from `fd` in one filesystem request. Writes
|
|
767
1054
|
* the UTF-8 result's length to `used`; a result longer than `capacity` is ENOBUFS, with
|
|
768
1055
|
* nothing written to `buf`. See docs/host-abi.md. */
|
|
@@ -790,7 +1077,15 @@ export class Wasi {
|
|
|
790
1077
|
if (!entry) return EBADF;
|
|
791
1078
|
if (entry.resource.kind !== "directory") return ENOTDIR;
|
|
792
1079
|
if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET)) return ENOTCAPABLE;
|
|
793
|
-
|
|
1080
|
+
let real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
|
|
1081
|
+
if (real === ENOENT) {
|
|
1082
|
+
const virtual = this.resolveVirtualMachineId(entry, path, true);
|
|
1083
|
+
if (typeof virtual === "number") return virtual;
|
|
1084
|
+
if (virtual === "machine-id")
|
|
1085
|
+
real = this.rooted
|
|
1086
|
+
? "/etc/machine-id"
|
|
1087
|
+
: this.relativeVirtualMachineId(entry.resource.handle);
|
|
1088
|
+
}
|
|
794
1089
|
if (typeof real === "number") return real;
|
|
795
1090
|
const encoded = encoder.encode(real);
|
|
796
1091
|
// Reacquire after the service round trip; another thread may have grown shared memory.
|
|
@@ -849,9 +1144,17 @@ export class Wasi {
|
|
|
849
1144
|
if (entry.resource.kind !== "directory") return ENOTDIR;
|
|
850
1145
|
if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET)) return ENOTCAPABLE;
|
|
851
1146
|
const resolved = this.opts.fs.resolve(entry.resource.handle, path, false, this.rooted);
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
1147
|
+
let result =
|
|
1148
|
+
typeof resolved !== "number" && resolved.node !== undefined
|
|
1149
|
+
? this.opts.fs.permissions?.(resolved.node)
|
|
1150
|
+
: undefined;
|
|
1151
|
+
if (typeof resolved === "number" || resolved.node === undefined) {
|
|
1152
|
+
if (typeof resolved === "number" && resolved !== ENOENT) return resolved;
|
|
1153
|
+
const virtual = this.resolveVirtualMachineId(entry, path, false);
|
|
1154
|
+
if (typeof virtual === "number") return virtual;
|
|
1155
|
+
if (virtual !== "machine-id") return ENOENT;
|
|
1156
|
+
result = permissionsFor("file", 0o444);
|
|
1157
|
+
}
|
|
855
1158
|
if (!result) return ENOTSUP;
|
|
856
1159
|
const view = this.view();
|
|
857
1160
|
view.setUint32(out, result.mode, true);
|
|
@@ -1007,27 +1310,22 @@ export class Wasi {
|
|
|
1007
1310
|
const entry = this.descriptors.get(fd);
|
|
1008
1311
|
if (!entry) return EBADF;
|
|
1009
1312
|
const view = this.view();
|
|
1010
|
-
const
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1313
|
+
const resource = entry.resource;
|
|
1314
|
+
let filetype = FT_FILE;
|
|
1315
|
+
if (resource.kind === "terminal") filetype = FT_CHAR;
|
|
1316
|
+
else if (resource.kind === "device") filetype = resource.virtualFile ? FT_FILE : FT_CHAR;
|
|
1317
|
+
else if (resource.kind === "directory") filetype = FT_DIR;
|
|
1318
|
+
else if (resource.kind === "socket") filetype = FT_SOCKET;
|
|
1319
|
+
else if (resource.kind === "pipe") filetype = 0;
|
|
1320
|
+
let fdflags = 0;
|
|
1321
|
+
if (resource.kind === "file") {
|
|
1322
|
+
fdflags =
|
|
1323
|
+
((resource.fdflags ?? 0) & ~FDFLAG_APPEND) | (resource.append ? FDFLAG_APPEND : 0);
|
|
1324
|
+
} else if (resource.kind !== "directory") {
|
|
1325
|
+
fdflags = resource.fdflags ?? 0;
|
|
1326
|
+
}
|
|
1020
1327
|
view.setUint8(ptr, filetype);
|
|
1021
|
-
view.setUint16(
|
|
1022
|
-
ptr + 2,
|
|
1023
|
-
entry.resource.kind === "file"
|
|
1024
|
-
? ((entry.resource.fdflags ?? 0) & ~FDFLAG_APPEND) |
|
|
1025
|
-
(entry.resource.append ? FDFLAG_APPEND : 0)
|
|
1026
|
-
: entry.resource.kind === "directory"
|
|
1027
|
-
? 0
|
|
1028
|
-
: (entry.resource.fdflags ?? 0),
|
|
1029
|
-
true,
|
|
1030
|
-
);
|
|
1328
|
+
view.setUint16(ptr + 2, fdflags, true);
|
|
1031
1329
|
view.setBigUint64(ptr + 8, entry.rightsBase, true);
|
|
1032
1330
|
view.setBigUint64(ptr + 16, entry.rightsInheriting, true);
|
|
1033
1331
|
return SUCCESS;
|
|
@@ -1064,18 +1362,26 @@ export class Wasi {
|
|
|
1064
1362
|
return SUCCESS;
|
|
1065
1363
|
},
|
|
1066
1364
|
fd_filestat_get: this.descriptorCall(RIGHT_FD_FILESTAT_GET, (resource, ptr: number) => {
|
|
1365
|
+
if (resource.kind === "device" && resource.virtualFile) {
|
|
1366
|
+
this.writeFilestat(ptr, this.virtualMachineIdStat());
|
|
1367
|
+
return SUCCESS;
|
|
1368
|
+
}
|
|
1369
|
+
let streamType = FT_CHAR;
|
|
1370
|
+
if (resource.kind === "pipe") streamType = 0;
|
|
1371
|
+
else if (resource.kind === "socket") streamType = FT_SOCKET;
|
|
1067
1372
|
this.writeFilestat(
|
|
1068
1373
|
ptr,
|
|
1069
1374
|
resource.kind === "file" || resource.kind === "directory"
|
|
1070
1375
|
? this.opts.fs.stat(resource.handle)
|
|
1071
1376
|
: null,
|
|
1072
|
-
|
|
1377
|
+
streamType,
|
|
1073
1378
|
);
|
|
1074
1379
|
return SUCCESS;
|
|
1075
1380
|
}),
|
|
1076
1381
|
fd_filestat_set_size: this.descriptorCall(
|
|
1077
1382
|
RIGHT_FD_FILESTAT_SET_SIZE,
|
|
1078
1383
|
(resource, size: bigint) => {
|
|
1384
|
+
if (resource.kind === "device" && resource.virtualFile) return EACCES;
|
|
1079
1385
|
if (resource.kind !== "file") return EINVAL;
|
|
1080
1386
|
if (!isSupportedFileSize(size)) return EFBIG;
|
|
1081
1387
|
return this.opts.fs.resize(resource.handle, Number(size), true);
|
|
@@ -1093,6 +1399,17 @@ export class Wasi {
|
|
|
1093
1399
|
RIGHT_FD_READ | RIGHT_FD_SEEK,
|
|
1094
1400
|
(resource, iovs: number, count: number, offset: bigint, nreadPtr: number) => {
|
|
1095
1401
|
if (resource.kind === "directory") return EISDIR;
|
|
1402
|
+
if (resource.kind === "device" && resource.virtualFile) {
|
|
1403
|
+
if (offset < 0n) return EINVAL;
|
|
1404
|
+
return this.writeResult(
|
|
1405
|
+
readVirtualFileBytes(
|
|
1406
|
+
resource.virtualFile.data,
|
|
1407
|
+
this.iovecs(iovs, count),
|
|
1408
|
+
Number(offset),
|
|
1409
|
+
),
|
|
1410
|
+
nreadPtr,
|
|
1411
|
+
);
|
|
1412
|
+
}
|
|
1096
1413
|
if (resource.kind !== "file") return ESPIPE;
|
|
1097
1414
|
if (offset < 0n) return EINVAL;
|
|
1098
1415
|
return this.writeResult(
|
|
@@ -1105,6 +1422,7 @@ export class Wasi {
|
|
|
1105
1422
|
RIGHT_FD_WRITE | RIGHT_FD_SEEK,
|
|
1106
1423
|
(resource, iovs: number, count: number, offset: bigint, nwrittenPtr: number) => {
|
|
1107
1424
|
if (resource.kind === "directory") return EISDIR;
|
|
1425
|
+
if (resource.kind === "device" && resource.virtualFile) return EACCES;
|
|
1108
1426
|
if (resource.kind !== "file") return ESPIPE;
|
|
1109
1427
|
if (offset < 0n) return EINVAL;
|
|
1110
1428
|
return this.writeResult(
|
|
@@ -1166,26 +1484,27 @@ export class Wasi {
|
|
|
1166
1484
|
return SUCCESS;
|
|
1167
1485
|
},
|
|
1168
1486
|
),
|
|
1169
|
-
fd_renumber: (from: number, to: number) =>
|
|
1170
|
-
|
|
1171
|
-
},
|
|
1487
|
+
fd_renumber: (from: number, to: number) =>
|
|
1488
|
+
this.descriptors.renumber(from, to) ? SUCCESS : EBADF,
|
|
1172
1489
|
fd_seek: (fd: number, offset: bigint, whence: number, newOffsetPtr: number) => {
|
|
1173
1490
|
const entry = this.descriptors.get(fd);
|
|
1174
1491
|
if (!entry) return EBADF;
|
|
1175
1492
|
const right = offset === 0n && whence === 1 ? RIGHT_FD_TELL | RIGHT_FD_SEEK : RIGHT_FD_SEEK;
|
|
1176
1493
|
if (!(entry.rightsBase & right)) return ENOTCAPABLE;
|
|
1177
1494
|
if (entry.resource.kind === "directory") return EISDIR;
|
|
1495
|
+
if (entry.resource.kind === "device" && entry.resource.virtualFile) {
|
|
1496
|
+
const next = seekVirtualFile(entry.resource.virtualFile, offset, whence);
|
|
1497
|
+
if (typeof next === "number") return -next;
|
|
1498
|
+
this.view().setBigUint64(newOffsetPtr, next, true);
|
|
1499
|
+
return SUCCESS;
|
|
1500
|
+
}
|
|
1178
1501
|
if (entry.resource.kind !== "file") return ESPIPE;
|
|
1179
1502
|
const file = entry.resource;
|
|
1180
1503
|
const result = withFileOffset(file, () => {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
? file.offset
|
|
1186
|
-
: whence === 2
|
|
1187
|
-
? this.opts.fs.stat(file.handle).size
|
|
1188
|
-
: NaN;
|
|
1504
|
+
let base = Number.NaN;
|
|
1505
|
+
if (whence === 0) base = 0;
|
|
1506
|
+
else if (whence === 1) base = file.offset;
|
|
1507
|
+
else if (whence === 2) base = this.opts.fs.stat(file.handle).size;
|
|
1189
1508
|
const pos = base + Number(offset);
|
|
1190
1509
|
if (!(pos >= 0)) return -EINVAL;
|
|
1191
1510
|
file.offset = pos;
|
|
@@ -1195,6 +1514,10 @@ export class Wasi {
|
|
|
1195
1514
|
return result < 0 ? -result : result;
|
|
1196
1515
|
},
|
|
1197
1516
|
fd_tell: this.descriptorCall(RIGHT_FD_TELL, (resource, ptr: number) => {
|
|
1517
|
+
if (resource.kind === "device" && resource.virtualFile) {
|
|
1518
|
+
this.view().setBigUint64(ptr, tellVirtualFile(resource.virtualFile), true);
|
|
1519
|
+
return SUCCESS;
|
|
1520
|
+
}
|
|
1198
1521
|
if (resource.kind !== "file") return ESPIPE;
|
|
1199
1522
|
this.view().setBigUint64(ptr, BigInt(resource.offset), true);
|
|
1200
1523
|
return SUCCESS;
|
|
@@ -1226,11 +1549,22 @@ export class Wasi {
|
|
|
1226
1549
|
return typeof result === "number" ? SUCCESS : result.error;
|
|
1227
1550
|
},
|
|
1228
1551
|
path_filestat_get: (fd: number, flags: number, ptr: number, len: number, buf: number) => {
|
|
1229
|
-
const
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1552
|
+
const follow = lookupFlags(flags);
|
|
1553
|
+
const path = this.resolve(fd, ptr, len, RIGHT_PATH_FILESTAT_GET, follow);
|
|
1554
|
+
if (typeof path === "number" || path.node === undefined) {
|
|
1555
|
+
if (typeof path === "number" && path !== ENOENT) return path;
|
|
1556
|
+
const entry = this.descriptors.get(fd);
|
|
1557
|
+
if (!entry) return EBADF;
|
|
1558
|
+
const name = this.str(ptr, len);
|
|
1559
|
+
const virtual = this.resolveVirtualMachineId(entry, name, followsFinal(follow, name));
|
|
1560
|
+
if (typeof virtual === "number") return virtual;
|
|
1561
|
+
if (virtual === "machine-id") {
|
|
1562
|
+
this.writeFilestat(buf, this.virtualMachineIdStat());
|
|
1563
|
+
return SUCCESS;
|
|
1564
|
+
}
|
|
1565
|
+
return ENOENT;
|
|
1566
|
+
}
|
|
1567
|
+
this.writeFilestat(buf, this.opts.fs.stat(path.node));
|
|
1234
1568
|
return SUCCESS;
|
|
1235
1569
|
},
|
|
1236
1570
|
path_filestat_set_times: (
|
|
@@ -1298,23 +1632,39 @@ export class Wasi {
|
|
|
1298
1632
|
const name = this.str(ptr, len);
|
|
1299
1633
|
const exclusive = (oflags & O_CREAT && oflags & O_EXCL) !== 0;
|
|
1300
1634
|
const follow = !exclusive && followsFinal(lookupFlags(dirflags), name);
|
|
1635
|
+
let finalLink: "exclusive-create" | "follow" | "nofollow" = "nofollow";
|
|
1636
|
+
if (exclusive) finalLink = "exclusive-create";
|
|
1637
|
+
else if (follow) finalLink = "follow";
|
|
1301
1638
|
const alias = resolveDescriptorPath(
|
|
1302
1639
|
this.descriptors,
|
|
1303
1640
|
this.opts.fs,
|
|
1304
1641
|
dir,
|
|
1305
1642
|
name,
|
|
1306
|
-
|
|
1643
|
+
finalLink,
|
|
1307
1644
|
this.rooted,
|
|
1645
|
+
this.machineId() !== undefined,
|
|
1308
1646
|
);
|
|
1309
1647
|
if (typeof alias === "number") return alias;
|
|
1310
1648
|
if (alias !== undefined && !("resolved" in alias)) {
|
|
1649
|
+
if ("virtualFile" in alias) {
|
|
1650
|
+
if (fdOut + 4 > this.bytes().length) return EFAULT;
|
|
1651
|
+
return this.openVirtualMachineId(fdflags, rightsBase, rightsInheriting, oflags, fdOut);
|
|
1652
|
+
}
|
|
1311
1653
|
if ("device" in alias) {
|
|
1312
1654
|
if (fdOut + 4 > this.bytes().length) return EFAULT;
|
|
1313
1655
|
return this.openDevice(fdflags, rightsBase, rightsInheriting, oflags, fdOut);
|
|
1314
1656
|
}
|
|
1315
1657
|
if (!this.descriptors.get(alias.source)) return ENOENT;
|
|
1316
1658
|
if (fdOut + 4 > this.bytes().length) return EFAULT;
|
|
1317
|
-
return this.reopen(
|
|
1659
|
+
return this.reopen(
|
|
1660
|
+
alias.source,
|
|
1661
|
+
oflags,
|
|
1662
|
+
fdflags,
|
|
1663
|
+
rightsBase,
|
|
1664
|
+
rightsInheriting,
|
|
1665
|
+
fdOut,
|
|
1666
|
+
true,
|
|
1667
|
+
);
|
|
1318
1668
|
}
|
|
1319
1669
|
const path =
|
|
1320
1670
|
alias?.resolved ?? this.opts.fs.resolve(dir.resource.handle, name, follow, this.rooted);
|
|
@@ -1575,6 +1925,7 @@ export class Wasi {
|
|
|
1575
1925
|
|
|
1576
1926
|
proc_exit: (code: number) => {
|
|
1577
1927
|
this.dropWatches();
|
|
1928
|
+
this.releasePosixLocks();
|
|
1578
1929
|
this.descriptors.dispose();
|
|
1579
1930
|
// Match native process exits; negative i32 values must not alias child-control errors.
|
|
1580
1931
|
throw new WasiExit(code & 0xff);
|