@ai-ecoverse/slicc-kernel 1.73.0 → 1.73.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.
@@ -2650,36 +2650,57 @@ function pathOpensLinks(Fs) {
2650
2650
  if (!mayOpen || !isLink) return;
2651
2651
  fs.mayOpen = (node, flags) => flags & O_PATH && isLink.call(fs, node.mode) ? 0 : mayOpen.call(fs, node, flags);
2652
2652
  }
2653
+ function importKey(namespace, name2, glue) {
2654
+ const own = glue?.[name2];
2655
+ if (typeof own === "function") {
2656
+ const found = Object.keys(namespace).find((k) => namespace[k] === own);
2657
+ if (found !== void 0) return found;
2658
+ }
2659
+ const literal = LITERALS[name2];
2660
+ return typeof namespace[literal] === "function" ? literal : void 0;
2661
+ }
2662
+ function wrapImport(imports, name2, glue, wrap2) {
2663
+ let wrapped = false;
2664
+ for (const ns of namespaces(imports)) {
2665
+ const key = importKey(ns, name2, glue);
2666
+ if (key === void 0) continue;
2667
+ ns[key] = wrap2(ns[key]);
2668
+ wrapped = true;
2669
+ }
2670
+ if (!wrapped && typeof glue?.[name2] === "function") {
2671
+ throw new Error(`slicc-kernel cannot find the glue's ${LITERALS[name2]} among its imports`);
2672
+ }
2673
+ }
2653
2674
  function kernelUmask(imports, deps) {
2654
2675
  let umask = deps.initial & 511;
2655
- for (const ns of namespaces(imports)) {
2656
- const { __syscall_openat: openat, __syscall_mkdirat: mkdirat } = ns;
2657
- const { __syscall_mknodat: mknodat, __syscall_umask: own } = ns;
2658
- if (typeof openat === "function") {
2659
- ns.__syscall_openat = (dirfd, path, flags, varargs) => {
2660
- const memory = deps.memory();
2661
- if (flags & O_CREAT2 && varargs && memory)
2662
- new Int32Array(memory.buffer)[varargs >> 2] &= ~umask;
2663
- return openat(dirfd, path, flags, varargs);
2664
- };
2665
- }
2666
- if (typeof mkdirat === "function") {
2667
- ns.__syscall_mkdirat = (dirfd, path, mode) => mkdirat(dirfd, path, mode & ~umask);
2668
- }
2669
- if (typeof mknodat === "function") {
2670
- ns.__syscall_mknodat = (dirfd, path, mode, dev) => mknodat(dirfd, path, mode & ~umask, dev);
2671
- }
2672
- if (typeof own === "function") {
2676
+ const { glue } = deps;
2677
+ wrapImport(imports, "openat", glue, (openat) => (dirfd, path, flags, varargs) => {
2678
+ const memory = deps.memory();
2679
+ if (flags & O_CREAT2 && varargs && memory) new Int32Array(memory.buffer)[varargs >> 2] &= ~umask;
2680
+ return openat(dirfd, path, flags, varargs);
2681
+ });
2682
+ wrapImport(
2683
+ imports,
2684
+ "mkdirat",
2685
+ glue,
2686
+ (mkdirat) => (dirfd, path, mode) => mkdirat(dirfd, path, mode & ~umask)
2687
+ );
2688
+ wrapImport(
2689
+ imports,
2690
+ "mknodat",
2691
+ glue,
2692
+ (mknodat) => (dirfd, path, mode, dev) => mknodat(dirfd, path, mode & ~umask, dev)
2693
+ );
2694
+ wrapImport(imports, "umask", glue, (own) => {
2695
+ own(umask);
2696
+ return (mask) => {
2697
+ const old = umask;
2698
+ umask = mask & 511;
2673
2699
  own(umask);
2674
- ns.__syscall_umask = (mask) => {
2675
- const old = umask;
2676
- umask = mask & 511;
2677
- own(umask);
2678
- deps.set(umask);
2679
- return old;
2680
- };
2681
- }
2682
- }
2700
+ deps.set(umask);
2701
+ return old;
2702
+ };
2703
+ });
2683
2704
  }
2684
2705
  function touching(times, memory) {
2685
2706
  if (times === 0) return true;
@@ -2698,13 +2719,11 @@ function touch(Fs, on, run) {
2698
2719
  }
2699
2720
  }
2700
2721
  function noFollowUtimes(imports, fs, glue, memory = () => void 0) {
2701
- for (const namespace of namespaces(imports)) {
2702
- const key = Object.keys(namespace).find(
2703
- (name2) => glue !== void 0 && namespace[name2] === glue || name2 === "__syscall_utimensat"
2704
- );
2705
- const utimensat = key === void 0 ? void 0 : namespace[key];
2706
- if (key === void 0 || typeof utimensat !== "function") continue;
2707
- namespace[key] = (dirfd, path, times, flags) => {
2722
+ wrapImport(
2723
+ imports,
2724
+ "utimensat",
2725
+ { utimensat: glue },
2726
+ (utimensat) => (dirfd, path, times, flags) => {
2708
2727
  const Fs = fs();
2709
2728
  const on = touching(times, memory());
2710
2729
  const { utime, lookupPath, ErrnoError } = Fs ?? {};
@@ -2721,8 +2740,8 @@ function noFollowUtimes(imports, fs, glue, memory = () => void 0) {
2721
2740
  } finally {
2722
2741
  Fs.utime = utime;
2723
2742
  }
2724
- };
2725
- }
2743
+ }
2744
+ );
2726
2745
  }
2727
2746
  function errnoOf(err) {
2728
2747
  const { errno: errno2, code: code2 } = err ?? {};
@@ -3077,22 +3096,18 @@ function kernelChecks(Fs, memory, call2) {
3077
3096
  bridge.access(liveNodePath(node), call2.amode, kind, (call2.flags & AT_SYMLINK_NOFOLLOW) !== 0);
3078
3097
  return 0;
3079
3098
  }
3080
- function kernelAccess(imports, fs, memory) {
3081
- for (const ns of namespaces(imports)) {
3082
- const own = ns.__syscall_faccessat;
3083
- if (typeof own !== "function") continue;
3084
- ns.__syscall_faccessat = (dirfd, path, amode, flags) => {
3085
- const rc = own(dirfd, path, amode, flags);
3086
- const Fs = fs();
3087
- const heap = memory();
3088
- if (rc !== 0 || !Fs?.lookupPath || !heap) return rc;
3089
- try {
3090
- return kernelChecks(Fs, heap, { dirfd, path, amode: amode & 7, flags });
3091
- } catch (err) {
3092
- return -errnoOf(err);
3093
- }
3094
- };
3095
- }
3099
+ function kernelAccess(imports, fs, memory, glue) {
3100
+ wrapImport(imports, "faccessat", glue, (own) => (dirfd, path, amode, flags) => {
3101
+ const rc = own(dirfd, path, amode, flags);
3102
+ const Fs = fs();
3103
+ const heap = memory();
3104
+ if (rc !== 0 || !Fs?.lookupPath || !heap) return rc;
3105
+ try {
3106
+ return kernelChecks(Fs, heap, { dirfd, path, amode: amode & 7, flags });
3107
+ } catch (err) {
3108
+ return -errnoOf(err);
3109
+ }
3110
+ });
3096
3111
  }
3097
3112
  function linuxDevices(Fs) {
3098
3113
  const { chmod } = Fs;
@@ -3104,7 +3119,7 @@ function linuxDevices(Fs) {
3104
3119
  }
3105
3120
  }
3106
3121
  }
3107
- 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;
3122
+ 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, LITERALS, 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;
3108
3123
  var init_process_fds = __esm({
3109
3124
  "src/process/process-fds.ts"() {
3110
3125
  "use strict";
@@ -3133,6 +3148,16 @@ var init_process_fds = __esm({
3133
3148
  AT_SYMLINK_NOFOLLOW = 256;
3134
3149
  EMSCRIPTEN_EPERM = 63;
3135
3150
  O_PATH = 2097152;
3151
+ LITERALS = {
3152
+ openat: "__syscall_openat",
3153
+ mkdirat: "__syscall_mkdirat",
3154
+ mknodat: "__syscall_mknodat",
3155
+ umask: "__syscall_umask",
3156
+ faccessat: "__syscall_faccessat",
3157
+ utimensat: "__syscall_utimensat",
3158
+ getppid: "slicc_getppid_js",
3159
+ resizeHeap: "emscripten_resize_heap"
3160
+ };
3136
3161
  UTIME_NOW = 1073741823;
3137
3162
  UTIME_OMIT = 1073741822;
3138
3163
  PROT_WRITE = 2;
@@ -3353,6 +3378,11 @@ function number(r) {
3353
3378
  if (!r.ok) return -wasiErrno(r.errno);
3354
3379
  return r.kind === "json" && typeof r.json === "number" ? r.json : -wasiErrno("EIO");
3355
3380
  }
3381
+ function throughKernel(sig) {
3382
+ if (sig === 0) return false;
3383
+ const action = defaultAction(sig);
3384
+ return action === "terminate" || action === "stop";
3385
+ }
3356
3386
  function restarted(issue) {
3357
3387
  for (; ; ) {
3358
3388
  const r = issue();
@@ -3565,7 +3595,7 @@ function createProcessKernel(deps) {
3565
3595
  return status(call2({ op: "umount", target: at(target), flags }, `umount ${target}`));
3566
3596
  },
3567
3597
  kill(pid, sig) {
3568
- if (pid === deps.pid) {
3598
+ if (pid === deps.pid && !throughKernel(sig)) {
3569
3599
  if (sig !== 0) deps.raise?.(sig);
3570
3600
  return 0;
3571
3601
  }
@@ -3609,6 +3639,7 @@ var S_IFMT, S_IFDIR2, POLLIN2, POLLOUT2, POLLERR2, POLLHUP2, POLLNVAL, SELECT_SL
3609
3639
  var init_process_children = __esm({
3610
3640
  "src/process/process-children.ts"() {
3611
3641
  "use strict";
3642
+ init_signals();
3612
3643
  init_process_fork();
3613
3644
  init_wasi_errno();
3614
3645
  S_IFMT = 61440;
@@ -4528,9 +4559,8 @@ function identify(transport, init) {
4528
4559
  const id = r.ok && r.kind === "json" ? r.json : null;
4529
4560
  return id ?? { pid: init.pid, ppid: init.ppid ?? 1 };
4530
4561
  }
4531
- function liveParent(imports, parent) {
4532
- const env = imports.env;
4533
- if (env && typeof env.slicc_getppid_js === "function") env.slicc_getppid_js = parent;
4562
+ function liveParent(imports, parent, glue) {
4563
+ wrapImport(imports, "getppid", glue, () => parent);
4534
4564
  }
4535
4565
  function signalMasks(m) {
4536
4566
  const mask = m.sliccSigMask;
@@ -4538,15 +4568,12 @@ function signalMasks(m) {
4538
4568
  if (!mask || caught === -1) return null;
4539
4569
  return { caught, ignored: mask(1), restart: mask(2) };
4540
4570
  }
4541
- function reportGrowth(imports, publish) {
4542
- const env = imports.env;
4543
- const resize = env?.emscripten_resize_heap;
4544
- if (!env || typeof resize !== "function") return;
4545
- env.emscripten_resize_heap = (...args) => {
4571
+ function reportGrowth(imports, publish, glue) {
4572
+ wrapImport(imports, "resizeHeap", glue, (resize) => (...args) => {
4546
4573
  const grown = resize(...args);
4547
4574
  publish();
4548
4575
  return grown;
4549
- };
4576
+ });
4550
4577
  }
4551
4578
  function callerOwner(Fs, init) {
4552
4579
  return init.owners ? {} : { owner: ownByCaller(Fs, init.cred, credAbi(init.program.glue)) };
@@ -4554,6 +4581,25 @@ function callerOwner(Fs, init) {
4554
4581
  function utimensatOf(module) {
4555
4582
  return ownValue(module, "sliccSyscalls")?.utimensat;
4556
4583
  }
4584
+ function kernelWrappers(imports, module, deps) {
4585
+ const { init, transport, sys, memory } = deps;
4586
+ const kernelGlue = ownValue(module, "sliccKernelImports");
4587
+ liveParent(imports, deps.ppid, kernelGlue);
4588
+ kernelUmask(imports, {
4589
+ glue: kernelGlue,
4590
+ initial: init.umask ?? 18,
4591
+ memory,
4592
+ set: (mask) => void transport.call({ op: "proc-umask", mask }, Number.POSITIVE_INFINITY, "umask")
4593
+ });
4594
+ const fsOf = () => ownValue(module, "FS");
4595
+ noFollowUtimes(imports, fsOf, kernelGlue?.utimensat ?? utimensatOf(module), memory);
4596
+ kernelAccess(imports, fsOf, memory, kernelGlue);
4597
+ const glue = ownValue(module, "sliccFdImports");
4598
+ syncFsync(imports, fsOf, glue);
4599
+ positionalIo(imports, { glue, fs: fsOf, sys, memory });
4600
+ const grown = () => publishMemory(init.sab, memory()?.buffer.byteLength ?? 0);
4601
+ reportGrowth(imports, grown, kernelGlue);
4602
+ }
4557
4603
  async function runWasmProcess(init, port2, deps = {}) {
4558
4604
  const signals = new SignalGate(
4559
4605
  createSyncSabTransport(init.sab, port2, {
@@ -4615,24 +4661,13 @@ async function runWasmProcess(init, port2, deps = {}) {
4615
4661
  }
4616
4662
  }
4617
4663
  });
4618
- liveParent(imports, () => identify(transport, init).ppid);
4619
- kernelUmask(imports, {
4620
- initial: init.umask ?? 18,
4621
- memory: () => memory,
4622
- set: (mask) => void transport.call({ op: "proc-umask", mask }, Number.POSITIVE_INFINITY, "umask")
4623
- });
4624
- const fsOf = () => ownValue(module, "FS");
4625
- noFollowUtimes(imports, fsOf, utimensatOf(module), () => memory);
4626
- kernelAccess(imports, fsOf, () => memory);
4627
- const glue = ownValue(module, "sliccFdImports");
4628
- syncFsync(imports, () => ownValue(module, "FS"), glue);
4629
- positionalIo(imports, {
4630
- glue,
4631
- fs: () => ownValue(module, "FS"),
4664
+ kernelWrappers(imports, module, {
4665
+ init,
4666
+ transport,
4632
4667
  sys,
4633
- memory: () => memory
4668
+ memory: () => memory,
4669
+ ppid: () => identify(transport, init).ppid
4634
4670
  });
4635
- reportGrowth(imports, () => publishMemory(init.sab, memory?.buffer.byteLength ?? 0));
4636
4671
  return WebAssembly.instantiate(init.program.module, imports);
4637
4672
  }).then((instance) => {
4638
4673
  memory = wasmMemory(instance, imports);
@@ -4814,6 +4849,16 @@ var init_process_runtime = __esm({
4814
4849
  " ioctl: typeof ___syscall_ioctl === 'function' ? ___syscall_ioctl : undefined,",
4815
4850
  " setitimer: typeof __setitimer_js === 'function' ? __setitimer_js : undefined,",
4816
4851
  "};",
4852
+ "Module.sliccKernelImports ??= {",
4853
+ " openat: typeof ___syscall_openat === 'function' ? ___syscall_openat : undefined,",
4854
+ " mkdirat: typeof ___syscall_mkdirat === 'function' ? ___syscall_mkdirat : undefined,",
4855
+ " mknodat: typeof ___syscall_mknodat === 'function' ? ___syscall_mknodat : undefined,",
4856
+ " umask: typeof ___syscall_umask === 'function' ? ___syscall_umask : undefined,",
4857
+ " faccessat: typeof ___syscall_faccessat === 'function' ? ___syscall_faccessat : undefined,",
4858
+ " utimensat: typeof ___syscall_utimensat === 'function' ? ___syscall_utimensat : undefined,",
4859
+ " getppid: typeof _slicc_getppid_js === 'function' ? _slicc_getppid_js : undefined,",
4860
+ " resizeHeap: typeof _emscripten_resize_heap === 'function' ? _emscripten_resize_heap : undefined,",
4861
+ "};",
4817
4862
  "Module.sliccFdImports ??= {",
4818
4863
  " fd_sync: typeof _fd_sync === 'function' ? _fd_sync : undefined,",
4819
4864
  " fd_pread: typeof _fd_pread === 'function' ? _fd_pread : undefined,",