@ai-ecoverse/slicc-kernel 1.64.0 → 1.64.2

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.
@@ -13,6 +13,94 @@ var __export = (target, all) => {
13
13
  __defProp(target, name2, { get: all[name2], enumerable: true });
14
14
  };
15
15
 
16
+ // src/process/js/lockdown.ts
17
+ function chain(target) {
18
+ const out = [];
19
+ for (let at = target; at && at !== Object.prototype; ) {
20
+ out.push(at);
21
+ at = Object.getPrototypeOf(at);
22
+ }
23
+ return out;
24
+ }
25
+ function hide(target, names) {
26
+ for (const name2 of names) {
27
+ const holders = chain(target);
28
+ for (const holder of holders) {
29
+ if (Object.getOwnPropertyDescriptor(holder, name2)?.configurable) {
30
+ Reflect.deleteProperty(holder, name2);
31
+ }
32
+ }
33
+ const kept = holders.slice(1).some((holder) => Object.hasOwn(holder, name2));
34
+ const defined = Reflect.defineProperty(target, name2, {
35
+ value: void 0,
36
+ writable: false,
37
+ enumerable: false,
38
+ configurable: false
39
+ });
40
+ if (kept || !defined && Reflect.get(target, name2) !== void 0) {
41
+ throw new Error(`slicc-kernel: cannot withhold ${name2} from a JS program`);
42
+ }
43
+ }
44
+ }
45
+ function vacate(target, names) {
46
+ for (const name2 of names) {
47
+ for (let at = target; at && at !== Object.prototype; ) {
48
+ if (Object.getOwnPropertyDescriptor(at, name2)?.configurable) Reflect.deleteProperty(at, name2);
49
+ at = Object.getPrototypeOf(at);
50
+ }
51
+ Reflect.defineProperty(target, name2, {
52
+ value: void 0,
53
+ writable: true,
54
+ enumerable: false,
55
+ configurable: true
56
+ });
57
+ if (Reflect.get(target, name2) !== void 0) {
58
+ throw new Error(`slicc-kernel: cannot withhold ${name2} from a JS program`);
59
+ }
60
+ }
61
+ }
62
+ function lockdown(scope2 = globalThis) {
63
+ hide(scope2, BLOCKED_GLOBALS);
64
+ vacate(scope2, OPEN_SLOTS);
65
+ const navigator = scope2.navigator;
66
+ if (navigator && typeof navigator === "object") hide(navigator, BLOCKED_NAVIGATOR);
67
+ }
68
+ var BLOCKED_GLOBALS, BLOCKED_NAVIGATOR, OPEN_SLOTS;
69
+ var init_lockdown = __esm({
70
+ "src/process/js/lockdown.ts"() {
71
+ "use strict";
72
+ BLOCKED_GLOBALS = [
73
+ "XMLHttpRequest",
74
+ "WebSocketStream",
75
+ "WebTransport",
76
+ "EventSource",
77
+ "importScripts",
78
+ "indexedDB",
79
+ "caches",
80
+ "cookieStore",
81
+ "Worker",
82
+ "SharedWorker",
83
+ "BroadcastChannel",
84
+ "postMessage",
85
+ "close",
86
+ "webkitRequestFileSystem",
87
+ "webkitRequestFileSystemSync",
88
+ "webkitResolveLocalFileSystemURL",
89
+ "webkitResolveLocalFileSystemSyncURL"
90
+ ];
91
+ BLOCKED_NAVIGATOR = [
92
+ "storage",
93
+ "storageBuckets",
94
+ "locks",
95
+ "serviceWorker",
96
+ "usb",
97
+ "hid",
98
+ "serial"
99
+ ];
100
+ OPEN_SLOTS = ["fetch", "WebSocket"];
101
+ }
102
+ });
103
+
16
104
  // src/kernel/protocol.ts
17
105
  var WASM_PROCESS_INIT, WASM_PROCESS_EXIT, WASM_PROCESS_ERROR, WASM_THREAD_SPAWN, WASM_THREAD_INIT, WASM_THREAD_EXIT, WASM_MAX_THREADS, WASM_DEFAULT_THREADS;
18
106
  var init_protocol = __esm({
@@ -2126,12 +2214,27 @@ var init_kernel_streams = __esm({
2126
2214
  const original = fs[name2];
2127
2215
  if (typeof original !== "function") continue;
2128
2216
  fs[name2] = (path, ...rest2) => {
2129
- if (!isPtyPath(path)) return original.call(fs, path, ...rest2);
2217
+ const owned = this.ownedTty(path);
2218
+ if (owned && isPtyPath(path)) return this.owned(path, ptyStat(path));
2219
+ if (!isPtyPath(path)) {
2220
+ const st = original.call(fs, path, ...rest2);
2221
+ return owned && TTY_PATH.test(path) ? this.owned(path, st) : st;
2222
+ }
2130
2223
  this.existingPty(path);
2131
2224
  return ptyStat(path);
2132
2225
  };
2133
2226
  }
2134
2227
  }
2228
+ ownedTty(path) {
2229
+ return this.options.owners === true && this.sys.ttyOwner !== void 0 && path !== "/dev/ptmx";
2230
+ }
2231
+ owned(path, st) {
2232
+ const ask = this.sys.ttyOwner;
2233
+ const owner = this.call(() => ask.call(this.sys, path));
2234
+ if (!owner) return st;
2235
+ const mode = st.mode & ~4095 | (owner.mode ?? 400);
2236
+ return { ...st, uid: owner.uid, gid: owner.gid, mode };
2237
+ }
2135
2238
  existingPty(path) {
2136
2239
  const n = PTY_PATH.exec(path)?.[1];
2137
2240
  const exists = n === void 0 ? this.sys.openPty !== void 0 : this.sys.ptyNumbers?.().includes(Number(n)) ?? false;
@@ -2812,8 +2915,11 @@ function useDevFd(Fs) {
2812
2915
  return fstat.call(Fs, fd);
2813
2916
  };
2814
2917
  }
2918
+ function pinRootKind() {
2919
+ pinnedRoot = rootKind();
2920
+ }
2815
2921
  function mountTable(mounts = []) {
2816
- const root = globalThis.navigator?.storage ? "opfs" : "memory";
2922
+ const root = pinnedRoot ?? rootKind();
2817
2923
  const lines = [`${root} / ${root} rw 0 0`, "devfs /dev devfs rw 0 0", "proc /proc proc rw 0 0"];
2818
2924
  for (const m of mounts) {
2819
2925
  const options = Object.entries(m.options).map(([k, v]) => v === "" ? k : `${k}=${v}`);
@@ -2919,7 +3025,7 @@ function linuxDevices(Fs) {
2919
3025
  }
2920
3026
  }
2921
3027
  }
2922
- var O_CLOEXEC, FD_CLOEXEC, F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_DUPFD_CLOEXEC, tracked, IMPORT_NAMES, EBADF, AT_SYMLINK_NOFOLLOW, EMSCRIPTEN_EPERM, O_PATH, UTIME_NOW, UTIME_OMIT, PROT_WRITE, MAP_PRIVATE, MAP_TYPE, O_WRONLY, MAP_ALIGN, DIR_MODE, LINK_MODE, CHAR_DEVICE, O_ACCMODE2, O_CREAT2, field, NAME, AT_FDCWD, AT_EACCESS, DEVICE_MODES;
3028
+ var O_CLOEXEC, FD_CLOEXEC, F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_DUPFD_CLOEXEC, tracked, IMPORT_NAMES, EBADF, AT_SYMLINK_NOFOLLOW, EMSCRIPTEN_EPERM, O_PATH, UTIME_NOW, UTIME_OMIT, PROT_WRITE, MAP_PRIVATE, MAP_TYPE, O_WRONLY, MAP_ALIGN, DIR_MODE, LINK_MODE, CHAR_DEVICE, O_ACCMODE2, O_CREAT2, field, pinnedRoot, rootKind, NAME, AT_FDCWD, AT_EACCESS, DEVICE_MODES;
2923
3029
  var init_process_fds = __esm({
2924
3030
  "src/process/process-fds.ts"() {
2925
3031
  "use strict";
@@ -2961,6 +3067,7 @@ var init_process_fds = __esm({
2961
3067
  O_ACCMODE2 = 3;
2962
3068
  O_CREAT2 = 64;
2963
3069
  field = (text) => (text || "none").replace(/[\s\\]/g, (c) => `\\${c.charCodeAt(0).toString(8).padStart(3, "0")}`);
3070
+ rootKind = () => globalThis.navigator?.storage ? "opfs" : "memory";
2964
3071
  NAME = /^[a-z_][a-z0-9_-]*$/i;
2965
3072
  AT_FDCWD = -100;
2966
3073
  AT_EACCESS = 512;
@@ -4230,6 +4337,9 @@ function kernelSys(transport) {
4230
4337
  ttyNames() {
4231
4338
  return json(call2({ op: "fd-tty-names" }, "fd-tty-names"));
4232
4339
  },
4340
+ ttyOwner(name2) {
4341
+ return json(call2({ op: "fd-tty-owner", name: name2 }, `fd-tty-owner ${name2}`));
4342
+ },
4233
4343
  openTty(name2) {
4234
4344
  const req = name2 === void 0 ? { op: "fd-open-tty" } : { op: "fd-open-tty", name: name2 };
4235
4345
  return json(call2(req, `fd-open-tty ${name2 ?? ""}`));
@@ -4474,7 +4584,8 @@ async function runWasmProcess(init, port2, deps = {}) {
4474
4584
  early = () => signals.quiet(() => vfs.flushDirty());
4475
4585
  const sigpipe = () => running.sliccSigpipe?.() === 1;
4476
4586
  const restartable = () => signals.restartable();
4477
- const streams = new KernelStreams(running.FS, sys, { sigpipe, restartable });
4587
+ const owners = init.owners ? { owners: true } : {};
4588
+ const streams = new KernelStreams(running.FS, sys, { sigpipe, restartable, ...owners });
4478
4589
  trackCloseOnExec(running.FS);
4479
4590
  quietQuit(running.FS);
4480
4591
  useMounts(running.FS, init.env, true);
@@ -12212,82 +12323,6 @@ var init_js_context = __esm({
12212
12323
  }
12213
12324
  });
12214
12325
 
12215
- // src/process/js/lockdown.ts
12216
- function chain(target) {
12217
- const out = [];
12218
- for (let at = target; at && at !== Object.prototype; ) {
12219
- out.push(at);
12220
- at = Object.getPrototypeOf(at);
12221
- }
12222
- return out;
12223
- }
12224
- function hide(target, names) {
12225
- for (const name2 of names) {
12226
- const holders = chain(target);
12227
- for (const holder of holders) {
12228
- if (Object.getOwnPropertyDescriptor(holder, name2)?.configurable) {
12229
- Reflect.deleteProperty(holder, name2);
12230
- }
12231
- }
12232
- const kept = holders.slice(1).some((holder) => Object.hasOwn(holder, name2));
12233
- const defined = Reflect.defineProperty(target, name2, {
12234
- value: void 0,
12235
- writable: false,
12236
- enumerable: false,
12237
- configurable: false
12238
- });
12239
- if (kept || !defined && Reflect.get(target, name2) !== void 0) {
12240
- throw new Error(`slicc-kernel: cannot withhold ${name2} from a JS program`);
12241
- }
12242
- }
12243
- }
12244
- function vacate(target, names) {
12245
- for (const name2 of names) {
12246
- for (let at = target; at && at !== Object.prototype; ) {
12247
- if (Object.getOwnPropertyDescriptor(at, name2)?.configurable) Reflect.deleteProperty(at, name2);
12248
- at = Object.getPrototypeOf(at);
12249
- }
12250
- Reflect.defineProperty(target, name2, {
12251
- value: void 0,
12252
- writable: true,
12253
- enumerable: false,
12254
- configurable: true
12255
- });
12256
- if (Reflect.get(target, name2) !== void 0) {
12257
- throw new Error(`slicc-kernel: cannot withhold ${name2} from a JS program`);
12258
- }
12259
- }
12260
- }
12261
- function lockdown(scope2 = globalThis) {
12262
- hide(scope2, BLOCKED_GLOBALS);
12263
- vacate(scope2, OPEN_SLOTS);
12264
- const navigator = scope2.navigator;
12265
- if (navigator && typeof navigator === "object") hide(navigator, BLOCKED_NAVIGATOR);
12266
- }
12267
- var BLOCKED_GLOBALS, BLOCKED_NAVIGATOR, OPEN_SLOTS;
12268
- var init_lockdown = __esm({
12269
- "src/process/js/lockdown.ts"() {
12270
- "use strict";
12271
- BLOCKED_GLOBALS = [
12272
- "XMLHttpRequest",
12273
- "WebSocketStream",
12274
- "WebTransport",
12275
- "EventSource",
12276
- "importScripts",
12277
- "indexedDB",
12278
- "caches",
12279
- "cookieStore",
12280
- "Worker",
12281
- "SharedWorker",
12282
- "BroadcastChannel",
12283
- "postMessage",
12284
- "close"
12285
- ];
12286
- BLOCKED_NAVIGATOR = ["storage", "locks", "serviceWorker"];
12287
- OPEN_SLOTS = ["fetch", "WebSocket"];
12288
- }
12289
- });
12290
-
12291
12326
  // src/process/js/js-runtime.ts
12292
12327
  var js_runtime_exports = {};
12293
12328
  __export(js_runtime_exports, {
@@ -12410,11 +12445,15 @@ var init_js_runtime = __esm({
12410
12445
  }
12411
12446
  });
12412
12447
 
12448
+ // src/process-worker.ts
12449
+ init_lockdown();
12450
+
12413
12451
  // src/process/process-entry.ts
12414
12452
  init_protocol();
12415
12453
  init_process_runtime();
12416
12454
  var loadWasi = () => Promise.resolve().then(() => (init_wasi_runtime(), wasi_runtime_exports));
12417
12455
  var loadJs = () => Promise.resolve().then(() => (init_js_runtime(), js_runtime_exports));
12456
+ var NOT_RUNNABLE2 = 126;
12418
12457
  function started(init, port2, run, wasi, js) {
12419
12458
  if (init.program?.abi === "js") {
12420
12459
  const jsInit = init;
@@ -12424,18 +12463,35 @@ function started(init, port2, run, wasi, js) {
12424
12463
  if (wasmInit.program?.abi === "wasi") return wasi().then((m) => m.runWasiProcess(wasmInit, port2));
12425
12464
  return run(wasmInit, port2);
12426
12465
  }
12427
- function processEntry(port2, run = runWasmProcess, wasi = loadWasi, js = loadJs) {
12428
- const failed5 = (err) => port2.postMessage({
12466
+ function processEntry(port2, run = runWasmProcess, wasi = loadWasi, js = loadJs, seal = () => {
12467
+ }) {
12468
+ const failed5 = (err, code2) => port2.postMessage({
12429
12469
  type: WASM_PROCESS_ERROR,
12430
- message: err instanceof Error ? err.stack ?? err.message : String(err)
12470
+ message: err instanceof Error ? err.stack ?? err.message : String(err),
12471
+ ...code2 === void 0 ? {} : { code: code2 }
12431
12472
  });
12473
+ let refused;
12474
+ const sealOnce = () => {
12475
+ if (refused === void 0) {
12476
+ try {
12477
+ seal();
12478
+ refused = null;
12479
+ } catch (error) {
12480
+ refused = { error };
12481
+ }
12482
+ }
12483
+ return refused;
12484
+ };
12432
12485
  return (data) => {
12433
12486
  const type = data?.type;
12487
+ if (type !== WASM_THREAD_INIT && type !== WASM_PROCESS_INIT) return;
12488
+ const refusal2 = sealOnce();
12434
12489
  if (type === WASM_THREAD_INIT) {
12490
+ if (refusal2) return failed5(refusal2.error);
12435
12491
  wasi().then((m) => m.runWasiThread(data, port2)).catch(failed5);
12436
12492
  return;
12437
12493
  }
12438
- if (type !== WASM_PROCESS_INIT) return;
12494
+ if (refusal2) return failed5(refusal2.error, NOT_RUNNABLE2);
12439
12495
  started(data, port2, run, wasi, js).then(
12440
12496
  (code2) => port2.postMessage({ type: WASM_PROCESS_EXIT, code: code2 }),
12441
12497
  failed5
@@ -12444,8 +12500,18 @@ function processEntry(port2, run = runWasmProcess, wasi = loadWasi, js = loadJs)
12444
12500
  }
12445
12501
 
12446
12502
  // src/process-worker.ts
12503
+ init_process_fds();
12447
12504
  var scope = globalThis;
12448
12505
  var post = scope.postMessage.bind(scope);
12449
- var onMessage = processEntry({ postMessage: (message2) => post(message2) });
12506
+ var onMessage = processEntry(
12507
+ { postMessage: (message2) => post(message2) },
12508
+ void 0,
12509
+ void 0,
12510
+ void 0,
12511
+ () => {
12512
+ pinRootKind();
12513
+ lockdown();
12514
+ }
12515
+ );
12450
12516
  scope.addEventListener("message", (event) => onMessage(event.data));
12451
12517
  //# sourceMappingURL=process-worker.js.map