@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.
Files changed (75) hide show
  1. package/browser/filesystem-backend.ts +11 -1
  2. package/browser/preview-protocol.ts +2 -0
  3. package/browser/preview.ts +128 -18
  4. package/browser/runtime-terminals.ts +65 -24
  5. package/browser/shared-fs.ts +141 -14
  6. package/browser/worker.ts +73 -6
  7. package/core/descriptors.ts +19 -2
  8. package/core/devices.ts +237 -2
  9. package/core/fd-reopen.ts +105 -20
  10. package/core/filesystem.ts +71 -0
  11. package/core/index.ts +4 -0
  12. package/core/open-file.ts +212 -18
  13. package/core/wasi.ts +397 -46
  14. package/dist/browser/filesystem-backend.js +4 -2
  15. package/dist/browser/preview.js +120 -17
  16. package/dist/browser/runtime-terminals.js +53 -10
  17. package/dist/browser/shared-fs.js +124 -11
  18. package/dist/browser/worker.js +59 -6
  19. package/dist/core/descriptors.js +9 -1
  20. package/dist/core/devices.js +211 -1
  21. package/dist/core/fd-reopen.js +79 -18
  22. package/dist/core/filesystem.js +13 -0
  23. package/dist/core/open-file.js +166 -13
  24. package/dist/core/wasi.js +353 -27
  25. package/dist/fs/backend.js +29 -2
  26. package/dist/fs/client.js +199 -6
  27. package/dist/fs/index.js +21 -1
  28. package/dist/fs/indexeddb.js +30 -2
  29. package/dist/fs/locks.js +1112 -0
  30. package/dist/fs/memory.js +109 -1
  31. package/dist/fs/multitab-backend.js +315 -195
  32. package/dist/fs/multitab-protocol.js +1 -0
  33. package/dist/fs/persistent.js +13 -6
  34. package/dist/fs/protocol.js +9 -3
  35. package/dist/fs/service.js +386 -22
  36. package/dist/fs/shared.js +54 -1
  37. package/dist/fs/storage-geometry.js +2 -0
  38. package/dist/interrupt/rewriter-internal.js +4 -1
  39. package/dist/proc/abi.js +80 -6
  40. package/dist/proc/errors.js +1 -0
  41. package/dist/proc/filesystem-codec.js +40 -9
  42. package/dist/proc/index.js +10 -1
  43. package/dist/proc/pty.js +193 -0
  44. package/dist/proc/worker-client.js +11 -2
  45. package/dist/proc/worker-pipes.js +51 -0
  46. package/dist/runtime.js +11 -2
  47. package/dist/search/engine.js +53 -0
  48. package/dist/tooling/program/program-transformer.json +1 -1
  49. package/fs/backend.ts +33 -1
  50. package/fs/client.ts +225 -6
  51. package/fs/index.ts +22 -1
  52. package/fs/indexeddb.ts +36 -2
  53. package/fs/locks.ts +1406 -0
  54. package/fs/memory.ts +114 -1
  55. package/fs/multitab-backend.ts +561 -303
  56. package/fs/multitab-protocol.ts +1 -0
  57. package/fs/persistent.ts +14 -3
  58. package/fs/protocol.ts +17 -5
  59. package/fs/service.ts +468 -23
  60. package/fs/shared.ts +60 -2
  61. package/fs/storage-geometry.ts +3 -0
  62. package/index.ts +2 -0
  63. package/interrupt/rewriter-internal.ts +4 -1
  64. package/package.json +2 -2
  65. package/proc/abi.ts +79 -6
  66. package/proc/errors.ts +1 -0
  67. package/proc/filesystem-codec.ts +39 -9
  68. package/proc/index.ts +11 -0
  69. package/proc/pty.ts +227 -0
  70. package/proc/worker-client.ts +28 -6
  71. package/proc/worker-pipes.ts +57 -0
  72. package/runtime.ts +12 -2
  73. package/search/engine.ts +49 -0
  74. package/testing/indexeddb-storage-browser.ts +173 -2
  75. package/tooling/program/program-transformer.json +1 -1
package/dist/core/wasi.js CHANGED
@@ -34,10 +34,28 @@ import {
34
34
  import { resolveDescriptorPath, reopenDescriptor, openInode } from "./fd-reopen.js";
35
35
  import { ExecutionUnwind } from "./unwind.js";
36
36
  import { controlLoad, controlWait } from "../proc/control.js";
37
- import { withFileOffset } from "./open-file.js";
38
37
  import {
38
+ markResourceOfdLocked,
39
+ releaseOriginOfd,
40
+ resourceOfdId,
41
+ withFileOffset
42
+ } from "./open-file.js";
43
+ import {
44
+ F_GETLK,
45
+ F_OFD_GETLK,
46
+ F_OFD_SETLK,
47
+ F_OFD_SETLKW,
48
+ F_RDLCK,
49
+ F_SETLKW,
50
+ F_UNLCK,
51
+ F_WRLCK,
39
52
  FilesystemError,
40
53
  fileTimeNow,
54
+ LOCK_EX,
55
+ LOCK_NB,
56
+ LOCK_SH,
57
+ LOCK_UN,
58
+ permissionsFor,
41
59
  PROBE_PATH_LIMIT
42
60
  } from "./filesystem.js";
43
61
  import {
@@ -47,8 +65,18 @@ import {
47
65
  } from "./descriptors.js";
48
66
  import { memoryViews } from "./memory-views.js";
49
67
  import { GuestWatchTable } from "../fs/guest-watch.js";
50
- import { createNullDevice } from "./devices.js";
68
+ import {
69
+ createMachineIdFile,
70
+ createNullDevice,
71
+ MACHINE_ID_FILE_SIZE,
72
+ MACHINE_ID_INO,
73
+ readVirtualFileBytes,
74
+ seekVirtualFile,
75
+ tellVirtualFile,
76
+ validateMachineId
77
+ } from "./devices.js";
51
78
  const SUCCESS = 0;
79
+ const EACCES = 2;
52
80
  const EBADF = 8;
53
81
  const EEXIST = 20;
54
82
  const EFAULT = 21;
@@ -64,6 +92,7 @@ const ENOSYS = 52;
64
92
  const ENOTDIR = 54;
65
93
  const ENOTSUP = 58;
66
94
  const ENOTSOCK = 57;
95
+ const EOVERFLOW = 61;
67
96
  const ESPIPE = 70;
68
97
  const ENOTCAPABLE = 76;
69
98
  const MAX_FILE_SIZE = BigInt(Number.MAX_SAFE_INTEGER);
@@ -200,6 +229,7 @@ export class Wasi {
200
229
  hostCall = null;
201
230
  descriptors = new DescriptorTable;
202
231
  watches;
232
+ posixLockedHandles;
203
233
  importedMemory;
204
234
  boundMemory;
205
235
  hostWrite;
@@ -207,6 +237,38 @@ export class Wasi {
207
237
  env;
208
238
  rootedSnapshot;
209
239
  literalRoot;
240
+ explicitMachineId;
241
+ machineId() {
242
+ return this.explicitMachineId ?? this.opts.fs.machineId;
243
+ }
244
+ virtualMachineIdStat() {
245
+ return {
246
+ kind: "file",
247
+ ino: MACHINE_ID_INO,
248
+ nlink: 1,
249
+ size: MACHINE_ID_FILE_SIZE,
250
+ atime: 0n,
251
+ mtime: 0n,
252
+ ctime: 0n,
253
+ mode: 292
254
+ };
255
+ }
256
+ resolveVirtualMachineId(entry, path, follow) {
257
+ if (this.machineId() === undefined)
258
+ return;
259
+ const alias = resolveDescriptorPath(this.descriptors, this.opts.fs, entry, path, follow ? "follow" : "nofollow", this.rooted, true);
260
+ if (typeof alias === "number")
261
+ return alias;
262
+ if (alias !== undefined && "virtualFile" in alias)
263
+ return alias.virtualFile;
264
+ return;
265
+ }
266
+ relativeVirtualMachineId(handle) {
267
+ const rootHandle = this.opts.fs.preopen("/");
268
+ if (handle === rootHandle || this.opts.fs.stat(handle).ino === this.opts.fs.stat(rootHandle).ino)
269
+ return "etc/machine-id";
270
+ return "machine-id";
271
+ }
210
272
  get rooted() {
211
273
  return this.rootedSnapshot ??= this.rootNamespace();
212
274
  }
@@ -251,6 +313,8 @@ export class Wasi {
251
313
  }
252
314
  constructor(opts) {
253
315
  this.opts = opts;
316
+ if (opts.machineId !== undefined)
317
+ this.explicitMachineId = validateMachineId(opts.machineId);
254
318
  const reserved = new Set;
255
319
  for (const fd of opts.reservedFds ?? []) {
256
320
  if (!Number.isInteger(fd) || fd < 0 || fd > MAX_DESCRIPTOR || reserved.has(fd))
@@ -290,6 +354,9 @@ export class Wasi {
290
354
  this.openFileAt(fd, { kind: "directory", handle }, RIGHTS_ALL, RIGHTS_ALL, name);
291
355
  fd++;
292
356
  }
357
+ if (opts.fs.locks) {
358
+ this.descriptors.setOnClose((closed) => this.releasePosixHandle(closed.resource));
359
+ }
293
360
  }
294
361
  openDevice(fdflags, base, inheriting, oflags, out) {
295
362
  if ((out >>> 0) + 4 > this.bytes().length)
@@ -302,9 +369,39 @@ export class Wasi {
302
369
  return EINVAL;
303
370
  return this.writeResult(this.descriptors.open(createNullDevice(fdflags), base, inheriting), out >>> 0);
304
371
  }
372
+ openVirtualMachineId(fdflags, base, inheriting, oflags, out) {
373
+ if ((out >>> 0) + 4 > this.bytes().length)
374
+ return EFAULT;
375
+ if (oflags & O_DIRECTORY)
376
+ return ENOTDIR;
377
+ if (oflags & O_CREAT && oflags & O_EXCL)
378
+ return EEXIST;
379
+ if (oflags & ~15 || fdflags & ~31)
380
+ return EINVAL;
381
+ if ((oflags & O_TRUNC) !== 0 || (base & RIGHT_FD_WRITE) !== 0n)
382
+ return EACCES;
383
+ const id = this.machineId();
384
+ if (id === undefined)
385
+ return ENOENT;
386
+ return this.writeResult(this.descriptors.open(createMachineIdFile(id, fdflags), base, inheriting), out >>> 0);
387
+ }
305
388
  openFileAt(fd, resource, base, inheriting, preopen) {
306
389
  this.opts.fs.retain(resource.handle);
307
- return this.descriptors.openAt(fd, resource, base, inheriting, () => this.opts.fs.release(resource.handle), preopen);
390
+ return this.descriptors.openAt(fd, resource, base, inheriting, () => releaseOriginOfd(this.opts.fs, resource), preopen);
391
+ }
392
+ releasePosixLocks() {
393
+ if (!this.posixLockedHandles || this.posixLockedHandles.size === 0)
394
+ return;
395
+ const handles = Array.from(this.posixLockedHandles);
396
+ this.posixLockedHandles.clear();
397
+ const pid = this.opts.pid ?? 1;
398
+ for (let i = 0;i < handles.length; i++) {
399
+ this.opts.fs.locks?.releasePosix(handles[i], pid);
400
+ }
401
+ }
402
+ suppressPosixClose() {
403
+ this.descriptors.setOnClose(undefined);
404
+ this.posixLockedHandles?.clear();
308
405
  }
309
406
  fdReopen(fd, oflags, fdflags, base, out) {
310
407
  try {
@@ -315,10 +412,10 @@ export class Wasi {
315
412
  throw error;
316
413
  }
317
414
  }
318
- reopen(source, oflags, fdflags, base, inheriting, out) {
415
+ reopen(source, oflags, fdflags, base, inheriting, out, intersectCapabilities = false) {
319
416
  if ((out >>> 0) + 4 > this.bytes().length)
320
417
  return 21;
321
- const result = reopenDescriptor(this.descriptors, this.opts.fs, source, oflags, fdflags, base, inheriting);
418
+ const result = reopenDescriptor(this.descriptors, this.opts.fs, source, oflags, fdflags, base, inheriting, intersectCapabilities);
322
419
  return this.writeResult(result, out >>> 0);
323
420
  }
324
421
  writeResult(result, out) {
@@ -374,7 +471,9 @@ export class Wasi {
374
471
  watch_ready: () => this.watches?.ready() ? 1 : 0,
375
472
  watch_read: this.watchRead.bind(this),
376
473
  watch_next: this.watchNext.bind(this),
377
- watch_close: (id) => this.watches?.close(id >>> 0) ?? EBADF
474
+ watch_close: (id) => this.watches?.close(id >>> 0) ?? EBADF,
475
+ fd_flock: this.fdFlock.bind(this),
476
+ fd_lock: this.fdLock.bind(this)
378
477
  };
379
478
  const out = Object.create(null);
380
479
  this.importedMemory = undefined;
@@ -583,6 +682,147 @@ export class Wasi {
583
682
  this.view().setUint32(out, result.length, true);
584
683
  return SUCCESS;
585
684
  }
685
+ lockWaitControl() {
686
+ if (!this.opts.checkActive && !this.opts.interruptPending && !this.opts.interrupt && !this.opts.wait)
687
+ return;
688
+ return {
689
+ checkActive: this.opts.checkActive,
690
+ interruptPending: () => this.opts.interruptPending?.() ?? (this.opts.interrupt !== undefined && controlLoad(this.opts.interrupt, 0) !== 0),
691
+ wait: this.opts.wait ? (word, expected, ms) => this.opts.wait(word, expected, ms) : undefined
692
+ };
693
+ }
694
+ resolveRangeBase(resource, whence) {
695
+ switch (whence) {
696
+ case 0:
697
+ return 0n;
698
+ case 1:
699
+ return BigInt(resource.kind === "file" ? resource.offset : 0);
700
+ case 2:
701
+ return BigInt(this.opts.fs.stat(resource.handle).size);
702
+ default:
703
+ return null;
704
+ }
705
+ }
706
+ resolveRange(resource, whence, start, len) {
707
+ const base = this.resolveRangeBase(resource, whence);
708
+ if (base === null)
709
+ return EINVAL;
710
+ const pos = base + start;
711
+ if (len === 0n) {
712
+ if (pos < 0n)
713
+ return EINVAL;
714
+ if (pos > MAX_FILE_SIZE)
715
+ return EOVERFLOW;
716
+ return { start: Number(pos), len: 0 };
717
+ }
718
+ const rangeStart = len > 0n ? pos : pos + len;
719
+ const rangeEnd = len > 0n ? pos + len : pos;
720
+ if (rangeStart < 0n || rangeEnd < 0n)
721
+ return EINVAL;
722
+ if (rangeStart > MAX_FILE_SIZE || rangeEnd > MAX_FILE_SIZE)
723
+ return EOVERFLOW;
724
+ return { start: Number(rangeStart), len: Number(rangeEnd - rangeStart) };
725
+ }
726
+ releasePosixHandle(resource) {
727
+ if (resource.kind !== "file" && resource.kind !== "directory")
728
+ return;
729
+ const tracked = this.posixLockedHandles?.delete(resource.handle) === true;
730
+ const locks = this.opts.fs.locks;
731
+ if (!locks)
732
+ return;
733
+ if (!tracked && !locks.hasActivePosixLocks?.())
734
+ return;
735
+ locks.releasePosix(resource.handle, this.opts.pid ?? 1);
736
+ }
737
+ releaseUninheritedPosixLocks(keptFds) {
738
+ this.descriptors.setOnClose(undefined);
739
+ const closedResources = [];
740
+ this.descriptors.forEach((entry, fd) => {
741
+ if (!keptFds.has(fd))
742
+ closedResources.push(entry.resource);
743
+ });
744
+ for (let i = 0;i < closedResources.length; i++) {
745
+ this.releasePosixHandle(closedResources[i]);
746
+ }
747
+ this.posixLockedHandles?.clear();
748
+ }
749
+ fdFlock(fd, operation) {
750
+ const entry = this.descriptors.get(fd >>> 0);
751
+ if (!entry)
752
+ return EBADF;
753
+ if (entry.resource.kind !== "file" && entry.resource.kind !== "directory")
754
+ return EINVAL;
755
+ if ((operation & ~(LOCK_SH | LOCK_EX | LOCK_NB | LOCK_UN)) !== 0)
756
+ return EINVAL;
757
+ const base = operation & ~LOCK_NB;
758
+ if (base !== LOCK_SH && base !== LOCK_EX && base !== LOCK_UN)
759
+ return EINVAL;
760
+ const locks = this.opts.fs.locks;
761
+ if (!locks)
762
+ return ENOTSUP;
763
+ const owner = locks.owner ?? 0;
764
+ const ofdId = resourceOfdId(entry.resource, owner);
765
+ const blocking = (operation & LOCK_NB) === 0 && base !== LOCK_UN;
766
+ const err = locks.flock(entry.resource.handle, ofdId, operation, blocking ? this.lockWaitControl() : undefined);
767
+ if (err === SUCCESS && base !== LOCK_UN)
768
+ markResourceOfdLocked(entry.resource, owner);
769
+ return err;
770
+ }
771
+ fdLock(fd, cmd, type, whence, start, len, outPtr) {
772
+ if (cmd < F_GETLK || cmd > F_OFD_SETLKW)
773
+ return EINVAL;
774
+ if (type !== F_RDLCK && type !== F_WRLCK && type !== F_UNLCK)
775
+ return EINVAL;
776
+ if (whence < 0 || whence > 2)
777
+ return EINVAL;
778
+ const isGet = cmd === F_GETLK || cmd === F_OFD_GETLK;
779
+ const out = outPtr >>> 0;
780
+ if (isGet && out + 24 > this.bytes().length)
781
+ return EFAULT;
782
+ const entry = this.descriptors.get(fd >>> 0);
783
+ if (!entry)
784
+ return EBADF;
785
+ if (entry.resource.kind !== "file" && entry.resource.kind !== "directory")
786
+ return EINVAL;
787
+ if (!isGet && type === F_RDLCK && !(entry.rightsBase & RIGHT_FD_READ))
788
+ return EBADF;
789
+ if (!isGet && type === F_WRLCK && !(entry.rightsBase & RIGHT_FD_WRITE))
790
+ return EBADF;
791
+ const locks = this.opts.fs.locks;
792
+ if (!locks)
793
+ return ENOTSUP;
794
+ const resolved = this.resolveRange(entry.resource, whence, start, len);
795
+ if (typeof resolved === "number")
796
+ return resolved;
797
+ const owner = locks.owner ?? 0;
798
+ const isOfd = cmd === F_OFD_GETLK || cmd === F_OFD_SETLK || cmd === F_OFD_SETLKW;
799
+ const ofdId = isOfd ? resourceOfdId(entry.resource, owner) : undefined;
800
+ const range = { type, start: resolved.start, len: resolved.len };
801
+ const pid = this.opts.pid ?? 1;
802
+ if (isGet) {
803
+ const conflict = locks.getRangeLock(entry.resource.handle, ofdId, range, pid);
804
+ if (typeof conflict === "number")
805
+ return conflict;
806
+ const view = this.view();
807
+ if (out + 24 > view.byteLength)
808
+ return EFAULT;
809
+ view.setUint32(out, conflict.type, true);
810
+ view.setInt32(out + 4, conflict.pid, true);
811
+ view.setBigInt64(out + 8, BigInt(conflict.start), true);
812
+ view.setBigInt64(out + 16, BigInt(conflict.len), true);
813
+ return SUCCESS;
814
+ }
815
+ const blocking = (cmd === F_SETLKW || cmd === F_OFD_SETLKW) && type !== F_UNLCK;
816
+ const err = locks.setRangeLock(entry.resource.handle, ofdId, pid, range, blocking, blocking ? this.lockWaitControl() : undefined);
817
+ if (err !== SUCCESS || type === F_UNLCK)
818
+ return err;
819
+ if (isOfd) {
820
+ markResourceOfdLocked(entry.resource, owner);
821
+ return err;
822
+ }
823
+ (this.posixLockedHandles ??= new Set).add(entry.resource.handle);
824
+ return err;
825
+ }
586
826
  pathRealpath(fd, ptr, len, buf, capacity, used) {
587
827
  ptr >>>= 0;
588
828
  len >>>= 0;
@@ -604,7 +844,14 @@ export class Wasi {
604
844
  return ENOTDIR;
605
845
  if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET))
606
846
  return ENOTCAPABLE;
607
- const real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
847
+ let real = this.opts.fs.realpath(entry.resource.handle, path, this.rooted);
848
+ if (real === ENOENT) {
849
+ const virtual = this.resolveVirtualMachineId(entry, path, true);
850
+ if (typeof virtual === "number")
851
+ return virtual;
852
+ if (virtual === "machine-id")
853
+ real = this.rooted ? "/etc/machine-id" : this.relativeVirtualMachineId(entry.resource.handle);
854
+ }
608
855
  if (typeof real === "number")
609
856
  return real;
610
857
  const encoded = encoder.encode(real);
@@ -663,11 +910,17 @@ export class Wasi {
663
910
  if (!(entry.rightsBase & RIGHT_PATH_FILESTAT_GET))
664
911
  return ENOTCAPABLE;
665
912
  const resolved = this.opts.fs.resolve(entry.resource.handle, path, false, this.rooted);
666
- if (typeof resolved === "number")
667
- return resolved;
668
- if (resolved.node === undefined)
669
- return ENOENT;
670
- const result = this.opts.fs.permissions?.(resolved.node);
913
+ let result = typeof resolved !== "number" && resolved.node !== undefined ? this.opts.fs.permissions?.(resolved.node) : undefined;
914
+ if (typeof resolved === "number" || resolved.node === undefined) {
915
+ if (typeof resolved === "number" && resolved !== ENOENT)
916
+ return resolved;
917
+ const virtual = this.resolveVirtualMachineId(entry, path, false);
918
+ if (typeof virtual === "number")
919
+ return virtual;
920
+ if (virtual !== "machine-id")
921
+ return ENOENT;
922
+ result = permissionsFor("file", 292);
923
+ }
671
924
  if (!result)
672
925
  return ENOTSUP;
673
926
  const view = this.view();
@@ -805,9 +1058,26 @@ export class Wasi {
805
1058
  if (!entry)
806
1059
  return EBADF;
807
1060
  const view = this.view();
808
- const filetype = entry.resource.kind === "terminal" || entry.resource.kind === "device" ? FT_CHAR : entry.resource.kind === "directory" ? FT_DIR : entry.resource.kind === "socket" ? FT_SOCKET : entry.resource.kind === "pipe" ? 0 : FT_FILE;
1061
+ const resource = entry.resource;
1062
+ let filetype = FT_FILE;
1063
+ if (resource.kind === "terminal")
1064
+ filetype = FT_CHAR;
1065
+ else if (resource.kind === "device")
1066
+ filetype = resource.virtualFile ? FT_FILE : FT_CHAR;
1067
+ else if (resource.kind === "directory")
1068
+ filetype = FT_DIR;
1069
+ else if (resource.kind === "socket")
1070
+ filetype = FT_SOCKET;
1071
+ else if (resource.kind === "pipe")
1072
+ filetype = 0;
1073
+ let fdflags = 0;
1074
+ if (resource.kind === "file") {
1075
+ fdflags = (resource.fdflags ?? 0) & ~FDFLAG_APPEND | (resource.append ? FDFLAG_APPEND : 0);
1076
+ } else if (resource.kind !== "directory") {
1077
+ fdflags = resource.fdflags ?? 0;
1078
+ }
809
1079
  view.setUint8(ptr, filetype);
810
- view.setUint16(ptr + 2, entry.resource.kind === "file" ? (entry.resource.fdflags ?? 0) & ~FDFLAG_APPEND | (entry.resource.append ? FDFLAG_APPEND : 0) : entry.resource.kind === "directory" ? 0 : entry.resource.fdflags ?? 0, true);
1080
+ view.setUint16(ptr + 2, fdflags, true);
811
1081
  view.setBigUint64(ptr + 8, entry.rightsBase, true);
812
1082
  view.setBigUint64(ptr + 16, entry.rightsInheriting, true);
813
1083
  return SUCCESS;
@@ -843,10 +1113,21 @@ export class Wasi {
843
1113
  return SUCCESS;
844
1114
  },
845
1115
  fd_filestat_get: this.descriptorCall(RIGHT_FD_FILESTAT_GET, (resource, ptr) => {
846
- this.writeFilestat(ptr, resource.kind === "file" || resource.kind === "directory" ? this.opts.fs.stat(resource.handle) : null, resource.kind === "pipe" ? 0 : resource.kind === "socket" ? FT_SOCKET : FT_CHAR);
1116
+ if (resource.kind === "device" && resource.virtualFile) {
1117
+ this.writeFilestat(ptr, this.virtualMachineIdStat());
1118
+ return SUCCESS;
1119
+ }
1120
+ let streamType = FT_CHAR;
1121
+ if (resource.kind === "pipe")
1122
+ streamType = 0;
1123
+ else if (resource.kind === "socket")
1124
+ streamType = FT_SOCKET;
1125
+ this.writeFilestat(ptr, resource.kind === "file" || resource.kind === "directory" ? this.opts.fs.stat(resource.handle) : null, streamType);
847
1126
  return SUCCESS;
848
1127
  }),
849
1128
  fd_filestat_set_size: this.descriptorCall(RIGHT_FD_FILESTAT_SET_SIZE, (resource, size) => {
1129
+ if (resource.kind === "device" && resource.virtualFile)
1130
+ return EACCES;
850
1131
  if (resource.kind !== "file")
851
1132
  return EINVAL;
852
1133
  if (!isSupportedFileSize(size))
@@ -861,6 +1142,11 @@ export class Wasi {
861
1142
  fd_pread: this.descriptorCall(RIGHT_FD_READ | RIGHT_FD_SEEK, (resource, iovs, count, offset, nreadPtr) => {
862
1143
  if (resource.kind === "directory")
863
1144
  return EISDIR;
1145
+ if (resource.kind === "device" && resource.virtualFile) {
1146
+ if (offset < 0n)
1147
+ return EINVAL;
1148
+ return this.writeResult(readVirtualFileBytes(resource.virtualFile.data, this.iovecs(iovs, count), Number(offset)), nreadPtr);
1149
+ }
864
1150
  if (resource.kind !== "file")
865
1151
  return ESPIPE;
866
1152
  if (offset < 0n)
@@ -870,6 +1156,8 @@ export class Wasi {
870
1156
  fd_pwrite: this.descriptorCall(RIGHT_FD_WRITE | RIGHT_FD_SEEK, (resource, iovs, count, offset, nwrittenPtr) => {
871
1157
  if (resource.kind === "directory")
872
1158
  return EISDIR;
1159
+ if (resource.kind === "device" && resource.virtualFile)
1160
+ return EACCES;
873
1161
  if (resource.kind !== "file")
874
1162
  return ESPIPE;
875
1163
  if (offset < 0n)
@@ -917,9 +1205,7 @@ export class Wasi {
917
1205
  this.view().setUint32(usedPtr, page.length, true);
918
1206
  return SUCCESS;
919
1207
  }),
920
- fd_renumber: (from, to) => {
921
- return this.descriptors.renumber(from, to) ? SUCCESS : EBADF;
922
- },
1208
+ fd_renumber: (from, to) => this.descriptors.renumber(from, to) ? SUCCESS : EBADF,
923
1209
  fd_seek: (fd, offset, whence, newOffsetPtr) => {
924
1210
  const entry = this.descriptors.get(fd);
925
1211
  if (!entry)
@@ -929,11 +1215,24 @@ export class Wasi {
929
1215
  return ENOTCAPABLE;
930
1216
  if (entry.resource.kind === "directory")
931
1217
  return EISDIR;
1218
+ if (entry.resource.kind === "device" && entry.resource.virtualFile) {
1219
+ const next = seekVirtualFile(entry.resource.virtualFile, offset, whence);
1220
+ if (typeof next === "number")
1221
+ return -next;
1222
+ this.view().setBigUint64(newOffsetPtr, next, true);
1223
+ return SUCCESS;
1224
+ }
932
1225
  if (entry.resource.kind !== "file")
933
1226
  return ESPIPE;
934
1227
  const file = entry.resource;
935
1228
  const result = withFileOffset(file, () => {
936
- const base = whence === 0 ? 0 : whence === 1 ? file.offset : whence === 2 ? this.opts.fs.stat(file.handle).size : NaN;
1229
+ let base = Number.NaN;
1230
+ if (whence === 0)
1231
+ base = 0;
1232
+ else if (whence === 1)
1233
+ base = file.offset;
1234
+ else if (whence === 2)
1235
+ base = this.opts.fs.stat(file.handle).size;
937
1236
  const pos = base + Number(offset);
938
1237
  if (!(pos >= 0))
939
1238
  return -EINVAL;
@@ -944,6 +1243,10 @@ export class Wasi {
944
1243
  return result < 0 ? -result : result;
945
1244
  },
946
1245
  fd_tell: this.descriptorCall(RIGHT_FD_TELL, (resource, ptr) => {
1246
+ if (resource.kind === "device" && resource.virtualFile) {
1247
+ this.view().setBigUint64(ptr, tellVirtualFile(resource.virtualFile), true);
1248
+ return SUCCESS;
1249
+ }
947
1250
  if (resource.kind !== "file")
948
1251
  return ESPIPE;
949
1252
  this.view().setBigUint64(ptr, BigInt(resource.offset), true);
@@ -970,13 +1273,25 @@ export class Wasi {
970
1273
  return typeof result === "number" ? SUCCESS : result.error;
971
1274
  },
972
1275
  path_filestat_get: (fd, flags, ptr, len, buf) => {
973
- const path = this.resolve(fd, ptr, len, RIGHT_PATH_FILESTAT_GET, lookupFlags(flags));
974
- if (typeof path === "number")
975
- return path;
976
- const node = path.node;
977
- if (node === undefined)
1276
+ const follow = lookupFlags(flags);
1277
+ const path = this.resolve(fd, ptr, len, RIGHT_PATH_FILESTAT_GET, follow);
1278
+ if (typeof path === "number" || path.node === undefined) {
1279
+ if (typeof path === "number" && path !== ENOENT)
1280
+ return path;
1281
+ const entry = this.descriptors.get(fd);
1282
+ if (!entry)
1283
+ return EBADF;
1284
+ const name = this.str(ptr, len);
1285
+ const virtual = this.resolveVirtualMachineId(entry, name, followsFinal(follow, name));
1286
+ if (typeof virtual === "number")
1287
+ return virtual;
1288
+ if (virtual === "machine-id") {
1289
+ this.writeFilestat(buf, this.virtualMachineIdStat());
1290
+ return SUCCESS;
1291
+ }
978
1292
  return ENOENT;
979
- this.writeFilestat(buf, this.opts.fs.stat(node));
1293
+ }
1294
+ this.writeFilestat(buf, this.opts.fs.stat(path.node));
980
1295
  return SUCCESS;
981
1296
  },
982
1297
  path_filestat_set_times: (fd, flags, ptr, len, atim, mtim, fst) => {
@@ -1017,10 +1332,20 @@ export class Wasi {
1017
1332
  const name = this.str(ptr, len);
1018
1333
  const exclusive = (oflags & O_CREAT && oflags & O_EXCL) !== 0;
1019
1334
  const follow = !exclusive && followsFinal(lookupFlags(dirflags), name);
1020
- const alias = resolveDescriptorPath(this.descriptors, this.opts.fs, dir, name, exclusive ? "exclusive-create" : follow ? "follow" : "nofollow", this.rooted);
1335
+ let finalLink = "nofollow";
1336
+ if (exclusive)
1337
+ finalLink = "exclusive-create";
1338
+ else if (follow)
1339
+ finalLink = "follow";
1340
+ const alias = resolveDescriptorPath(this.descriptors, this.opts.fs, dir, name, finalLink, this.rooted, this.machineId() !== undefined);
1021
1341
  if (typeof alias === "number")
1022
1342
  return alias;
1023
1343
  if (alias !== undefined && !("resolved" in alias)) {
1344
+ if ("virtualFile" in alias) {
1345
+ if (fdOut + 4 > this.bytes().length)
1346
+ return EFAULT;
1347
+ return this.openVirtualMachineId(fdflags, rightsBase, rightsInheriting, oflags, fdOut);
1348
+ }
1024
1349
  if ("device" in alias) {
1025
1350
  if (fdOut + 4 > this.bytes().length)
1026
1351
  return EFAULT;
@@ -1030,7 +1355,7 @@ export class Wasi {
1030
1355
  return ENOENT;
1031
1356
  if (fdOut + 4 > this.bytes().length)
1032
1357
  return EFAULT;
1033
- return this.reopen(alias.source, oflags, fdflags, rightsBase, rightsInheriting, fdOut);
1358
+ return this.reopen(alias.source, oflags, fdflags, rightsBase, rightsInheriting, fdOut, true);
1034
1359
  }
1035
1360
  const path = alias?.resolved ?? this.opts.fs.resolve(dir.resource.handle, name, follow, this.rooted);
1036
1361
  if (typeof path === "number")
@@ -1239,6 +1564,7 @@ export class Wasi {
1239
1564
  },
1240
1565
  proc_exit: (code) => {
1241
1566
  this.dropWatches();
1567
+ this.releasePosixLocks();
1242
1568
  this.descriptors.dispose();
1243
1569
  throw new WasiExit(code & 255);
1244
1570
  },
@@ -14,7 +14,8 @@ import {
14
14
  } from "../core/filesystem.js";
15
15
  import { MemFS } from "./memory.js";
16
16
  import { FilesystemError } from "./protocol.js";
17
- import { SharedFilesystemState } from "./shared.js";
17
+ import { machineIdFromWorkspace, SharedFilesystemState } from "./shared.js";
18
+ import { validateMachineId } from "../core/devices.js";
18
19
  function probePaths(value) {
19
20
  if (!Array.isArray(value) || value.length > PROBE_PATH_LIMIT)
20
21
  throw new FilesystemError(28);
@@ -51,9 +52,10 @@ export class MemoryBackend {
51
52
  return node.identity ??= this.shared.identities.create();
52
53
  }
53
54
  workspaceId;
55
+ machineId;
54
56
  fs;
55
57
  watchAccount;
56
- constructor(fs = new MemFS, reservation = { workspace: crypto.randomUUID(), era: 1n }, startupRoot, watchLimits, sharedRoot) {
58
+ constructor(fs = new MemFS, reservation = { workspace: crypto.randomUUID(), era: 1n }, startupRoot, watchLimits, sharedRoot, machineId) {
57
59
  if (sharedRoot && startupRoot)
58
60
  throw new TypeError("an imported root cannot also own a startup root");
59
61
  if (startupRoot instanceof MemoryBackend) {
@@ -63,6 +65,7 @@ export class MemoryBackend {
63
65
  this.watchAccount = startupRoot.watchAccount;
64
66
  this.fs = startupRoot.fs;
65
67
  this.workspaceId = startupRoot.workspaceId;
68
+ this.machineId = startupRoot.machineId;
66
69
  this.shared = startupRoot.shared;
67
70
  this.startupRoot = startupRoot.startupRoot;
68
71
  this.sharedPhase = startupRoot.sharedPhase;
@@ -78,7 +81,9 @@ export class MemoryBackend {
78
81
  this.sharedPhase = "imported";
79
82
  this.fs = fs;
80
83
  this.workspaceId = reservation.workspace;
84
+ this.machineId = machineId !== undefined ? validateMachineId(machineId) : machineIdFromWorkspace(reservation.workspace);
81
85
  this.shared.identities.configure(reservation);
86
+ this.shared.setMachineId(this.machineId);
82
87
  const seen = new Set;
83
88
  const pending = [fs.root];
84
89
  while (pending.length) {
@@ -117,6 +122,7 @@ export class MemoryBackend {
117
122
  this.shared = new SharedFilesystemState(buffer, maxBytes, undefined, segments, undefined, publication);
118
123
  if (this.sharedPhase !== "imported")
119
124
  this.shared.identities.copyFrom(previous.identities);
125
+ this.shared.setMachineId(this.machineId);
120
126
  this.shared.continueUnlinks(previous);
121
127
  this.sharedPhase = "attached";
122
128
  }
@@ -332,6 +338,8 @@ export class MemoryBackend {
332
338
  }
333
339
  case "lookup":
334
340
  return this.lookup(a);
341
+ case "statPath":
342
+ return this.statPath(a, b !== false);
335
343
  case "readFile": {
336
344
  const existing = this.existing(a);
337
345
  if (!existing)
@@ -345,6 +353,25 @@ export class MemoryBackend {
345
353
  throw new FilesystemError(52, `unknown filesystem operation ${operation}`);
346
354
  }
347
355
  }
356
+ statPath(path, follow) {
357
+ const node = this.fs.lookup(path, follow);
358
+ if (!node)
359
+ return;
360
+ const handle = this.fs.handleForNode(node);
361
+ const fsStat = this.fs.stat(handle);
362
+ const stat = this.shared.stat(handle);
363
+ return {
364
+ kind: node.kind,
365
+ size: stat ? stat.size : fsStat.size,
366
+ ino: node.ino,
367
+ mtime: fileTimeMs(stat ? stat.mtime : fsStat.mtime),
368
+ ctime: fileTimeMs(stat ? stat.ctime : fsStat.ctime),
369
+ atime: fileTimeMs(stat ? stat.atime : fsStat.atime),
370
+ mode: stat ? stat.mode : fsStat.mode,
371
+ nlink: stat ? stat.nlink : fsStat.nlink,
372
+ ...node.kind === "symlink" ? { target: node.target } : {}
373
+ };
374
+ }
348
375
  lookup(path) {
349
376
  const node = this.fs.lookup(path, false);
350
377
  if (!node)