@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.
@@ -8046,36 +8046,57 @@ function pathOpensLinks(Fs) {
8046
8046
  if (!mayOpen || !isLink) return;
8047
8047
  fs.mayOpen = (node, flags) => flags & O_PATH && isLink.call(fs, node.mode) ? 0 : mayOpen.call(fs, node, flags);
8048
8048
  }
8049
+ function importKey(namespace, name2, glue) {
8050
+ const own = glue?.[name2];
8051
+ if (typeof own === "function") {
8052
+ const found = Object.keys(namespace).find((k) => namespace[k] === own);
8053
+ if (found !== void 0) return found;
8054
+ }
8055
+ const literal = LITERALS[name2];
8056
+ return typeof namespace[literal] === "function" ? literal : void 0;
8057
+ }
8058
+ function wrapImport(imports, name2, glue, wrap2) {
8059
+ let wrapped = false;
8060
+ for (const ns of namespaces(imports)) {
8061
+ const key = importKey(ns, name2, glue);
8062
+ if (key === void 0) continue;
8063
+ ns[key] = wrap2(ns[key]);
8064
+ wrapped = true;
8065
+ }
8066
+ if (!wrapped && typeof glue?.[name2] === "function") {
8067
+ throw new Error(`slicc-kernel cannot find the glue's ${LITERALS[name2]} among its imports`);
8068
+ }
8069
+ }
8049
8070
  function kernelUmask(imports, deps) {
8050
8071
  let umask = deps.initial & 511;
8051
- for (const ns of namespaces(imports)) {
8052
- const { __syscall_openat: openat, __syscall_mkdirat: mkdirat } = ns;
8053
- const { __syscall_mknodat: mknodat, __syscall_umask: own } = ns;
8054
- if (typeof openat === "function") {
8055
- ns.__syscall_openat = (dirfd, path, flags, varargs) => {
8056
- const memory = deps.memory();
8057
- if (flags & O_CREAT2 && varargs && memory)
8058
- new Int32Array(memory.buffer)[varargs >> 2] &= ~umask;
8059
- return openat(dirfd, path, flags, varargs);
8060
- };
8061
- }
8062
- if (typeof mkdirat === "function") {
8063
- ns.__syscall_mkdirat = (dirfd, path, mode) => mkdirat(dirfd, path, mode & ~umask);
8064
- }
8065
- if (typeof mknodat === "function") {
8066
- ns.__syscall_mknodat = (dirfd, path, mode, dev) => mknodat(dirfd, path, mode & ~umask, dev);
8067
- }
8068
- if (typeof own === "function") {
8072
+ const { glue } = deps;
8073
+ wrapImport(imports, "openat", glue, (openat) => (dirfd, path, flags, varargs) => {
8074
+ const memory = deps.memory();
8075
+ if (flags & O_CREAT2 && varargs && memory) new Int32Array(memory.buffer)[varargs >> 2] &= ~umask;
8076
+ return openat(dirfd, path, flags, varargs);
8077
+ });
8078
+ wrapImport(
8079
+ imports,
8080
+ "mkdirat",
8081
+ glue,
8082
+ (mkdirat) => (dirfd, path, mode) => mkdirat(dirfd, path, mode & ~umask)
8083
+ );
8084
+ wrapImport(
8085
+ imports,
8086
+ "mknodat",
8087
+ glue,
8088
+ (mknodat) => (dirfd, path, mode, dev) => mknodat(dirfd, path, mode & ~umask, dev)
8089
+ );
8090
+ wrapImport(imports, "umask", glue, (own) => {
8091
+ own(umask);
8092
+ return (mask) => {
8093
+ const old = umask;
8094
+ umask = mask & 511;
8069
8095
  own(umask);
8070
- ns.__syscall_umask = (mask) => {
8071
- const old = umask;
8072
- umask = mask & 511;
8073
- own(umask);
8074
- deps.set(umask);
8075
- return old;
8076
- };
8077
- }
8078
- }
8096
+ deps.set(umask);
8097
+ return old;
8098
+ };
8099
+ });
8079
8100
  }
8080
8101
  function touching(times, memory) {
8081
8102
  if (times === 0) return true;
@@ -8094,13 +8115,11 @@ function touch(Fs, on, run) {
8094
8115
  }
8095
8116
  }
8096
8117
  function noFollowUtimes(imports, fs, glue, memory = () => void 0) {
8097
- for (const namespace of namespaces(imports)) {
8098
- const key = Object.keys(namespace).find(
8099
- (name2) => glue !== void 0 && namespace[name2] === glue || name2 === "__syscall_utimensat"
8100
- );
8101
- const utimensat = key === void 0 ? void 0 : namespace[key];
8102
- if (key === void 0 || typeof utimensat !== "function") continue;
8103
- namespace[key] = (dirfd, path, times, flags) => {
8118
+ wrapImport(
8119
+ imports,
8120
+ "utimensat",
8121
+ { utimensat: glue },
8122
+ (utimensat) => (dirfd, path, times, flags) => {
8104
8123
  const Fs = fs();
8105
8124
  const on = touching(times, memory());
8106
8125
  const { utime, lookupPath, ErrnoError } = Fs ?? {};
@@ -8117,8 +8136,8 @@ function noFollowUtimes(imports, fs, glue, memory = () => void 0) {
8117
8136
  } finally {
8118
8137
  Fs.utime = utime;
8119
8138
  }
8120
- };
8121
- }
8139
+ }
8140
+ );
8122
8141
  }
8123
8142
  function errnoOf(err) {
8124
8143
  const { errno: errno2, code: code2 } = err ?? {};
@@ -8470,22 +8489,18 @@ function kernelChecks(Fs, memory, call2) {
8470
8489
  bridge.access(liveNodePath(node), call2.amode, kind, (call2.flags & AT_SYMLINK_NOFOLLOW2) !== 0);
8471
8490
  return 0;
8472
8491
  }
8473
- function kernelAccess(imports, fs, memory) {
8474
- for (const ns of namespaces(imports)) {
8475
- const own = ns.__syscall_faccessat;
8476
- if (typeof own !== "function") continue;
8477
- ns.__syscall_faccessat = (dirfd, path, amode, flags) => {
8478
- const rc = own(dirfd, path, amode, flags);
8479
- const Fs = fs();
8480
- const heap = memory();
8481
- if (rc !== 0 || !Fs?.lookupPath || !heap) return rc;
8482
- try {
8483
- return kernelChecks(Fs, heap, { dirfd, path, amode: amode & 7, flags });
8484
- } catch (err) {
8485
- return -errnoOf(err);
8486
- }
8487
- };
8488
- }
8492
+ function kernelAccess(imports, fs, memory, glue) {
8493
+ wrapImport(imports, "faccessat", glue, (own) => (dirfd, path, amode, flags) => {
8494
+ const rc = own(dirfd, path, amode, flags);
8495
+ const Fs = fs();
8496
+ const heap = memory();
8497
+ if (rc !== 0 || !Fs?.lookupPath || !heap) return rc;
8498
+ try {
8499
+ return kernelChecks(Fs, heap, { dirfd, path, amode: amode & 7, flags });
8500
+ } catch (err) {
8501
+ return -errnoOf(err);
8502
+ }
8503
+ });
8489
8504
  }
8490
8505
  function linuxDevices(Fs) {
8491
8506
  const { chmod } = Fs;
@@ -8497,7 +8512,7 @@ function linuxDevices(Fs) {
8497
8512
  }
8498
8513
  }
8499
8514
  }
8500
- var O_CLOEXEC, FD_CLOEXEC, F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_DUPFD_CLOEXEC, tracked, IMPORT_NAMES, EBADF, AT_SYMLINK_NOFOLLOW2, EMSCRIPTEN_EPERM, O_PATH, UTIME_NOW, UTIME_OMIT, PROT_WRITE, MAP_PRIVATE, MAP_TYPE, O_WRONLY4, MAP_ALIGN, DIR_MODE, LINK_MODE, CHAR_DEVICE, O_ACCMODE2, O_CREAT2, field, pinnedRoot, rootKind, NAME, AT_FDCWD, AT_EACCESS2, DEVICE_MODES;
8515
+ var O_CLOEXEC, FD_CLOEXEC, F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_DUPFD_CLOEXEC, tracked, IMPORT_NAMES, EBADF, AT_SYMLINK_NOFOLLOW2, EMSCRIPTEN_EPERM, O_PATH, LITERALS, UTIME_NOW, UTIME_OMIT, PROT_WRITE, MAP_PRIVATE, MAP_TYPE, O_WRONLY4, MAP_ALIGN, DIR_MODE, LINK_MODE, CHAR_DEVICE, O_ACCMODE2, O_CREAT2, field, pinnedRoot, rootKind, NAME, AT_FDCWD, AT_EACCESS2, DEVICE_MODES;
8501
8516
  var init_process_fds = __esm({
8502
8517
  "src/process/process-fds.ts"() {
8503
8518
  "use strict";
@@ -8526,6 +8541,16 @@ var init_process_fds = __esm({
8526
8541
  AT_SYMLINK_NOFOLLOW2 = 256;
8527
8542
  EMSCRIPTEN_EPERM = 63;
8528
8543
  O_PATH = 2097152;
8544
+ LITERALS = {
8545
+ openat: "__syscall_openat",
8546
+ mkdirat: "__syscall_mkdirat",
8547
+ mknodat: "__syscall_mknodat",
8548
+ umask: "__syscall_umask",
8549
+ faccessat: "__syscall_faccessat",
8550
+ utimensat: "__syscall_utimensat",
8551
+ getppid: "slicc_getppid_js",
8552
+ resizeHeap: "emscripten_resize_heap"
8553
+ };
8529
8554
  UTIME_NOW = 1073741823;
8530
8555
  UTIME_OMIT = 1073741822;
8531
8556
  PROT_WRITE = 2;
@@ -8746,6 +8771,11 @@ function number(r) {
8746
8771
  if (!r.ok) return -wasiErrno(r.errno);
8747
8772
  return r.kind === "json" && typeof r.json === "number" ? r.json : -wasiErrno("EIO");
8748
8773
  }
8774
+ function throughKernel(sig) {
8775
+ if (sig === 0) return false;
8776
+ const action = defaultAction(sig);
8777
+ return action === "terminate" || action === "stop";
8778
+ }
8749
8779
  function restarted2(issue) {
8750
8780
  for (; ; ) {
8751
8781
  const r = issue();
@@ -8958,7 +8988,7 @@ function createProcessKernel(deps) {
8958
8988
  return status(call2({ op: "umount", target: at(target), flags }, `umount ${target}`));
8959
8989
  },
8960
8990
  kill(pid, sig) {
8961
- if (pid === deps.pid) {
8991
+ if (pid === deps.pid && !throughKernel(sig)) {
8962
8992
  if (sig !== 0) deps.raise?.(sig);
8963
8993
  return 0;
8964
8994
  }
@@ -9002,6 +9032,7 @@ var S_IFMT2, S_IFDIR2, POLLIN2, POLLOUT2, POLLERR2, POLLHUP2, POLLNVAL, SELECT_S
9002
9032
  var init_process_children = __esm({
9003
9033
  "src/process/process-children.ts"() {
9004
9034
  "use strict";
9035
+ init_signals();
9005
9036
  init_process_fork();
9006
9037
  init_wasi_errno();
9007
9038
  S_IFMT2 = 61440;
@@ -9902,9 +9933,8 @@ function identify(transport, init) {
9902
9933
  const id = r.ok && r.kind === "json" ? r.json : null;
9903
9934
  return id ?? { pid: init.pid, ppid: init.ppid ?? 1 };
9904
9935
  }
9905
- function liveParent(imports, parent) {
9906
- const env = imports.env;
9907
- if (env && typeof env.slicc_getppid_js === "function") env.slicc_getppid_js = parent;
9936
+ function liveParent(imports, parent, glue) {
9937
+ wrapImport(imports, "getppid", glue, () => parent);
9908
9938
  }
9909
9939
  function signalMasks(m) {
9910
9940
  const mask = m.sliccSigMask;
@@ -9912,15 +9942,12 @@ function signalMasks(m) {
9912
9942
  if (!mask || caught === -1) return null;
9913
9943
  return { caught, ignored: mask(1), restart: mask(2) };
9914
9944
  }
9915
- function reportGrowth(imports, publish) {
9916
- const env = imports.env;
9917
- const resize = env?.emscripten_resize_heap;
9918
- if (!env || typeof resize !== "function") return;
9919
- env.emscripten_resize_heap = (...args) => {
9945
+ function reportGrowth(imports, publish, glue) {
9946
+ wrapImport(imports, "resizeHeap", glue, (resize) => (...args) => {
9920
9947
  const grown = resize(...args);
9921
9948
  publish();
9922
9949
  return grown;
9923
- };
9950
+ });
9924
9951
  }
9925
9952
  function callerOwner(Fs, init) {
9926
9953
  return init.owners ? {} : { owner: ownByCaller(Fs, init.cred, credAbi(init.program.glue)) };
@@ -9928,6 +9955,25 @@ function callerOwner(Fs, init) {
9928
9955
  function utimensatOf(module) {
9929
9956
  return ownValue(module, "sliccSyscalls")?.utimensat;
9930
9957
  }
9958
+ function kernelWrappers(imports, module, deps) {
9959
+ const { init, transport, sys, memory } = deps;
9960
+ const kernelGlue = ownValue(module, "sliccKernelImports");
9961
+ liveParent(imports, deps.ppid, kernelGlue);
9962
+ kernelUmask(imports, {
9963
+ glue: kernelGlue,
9964
+ initial: init.umask ?? 18,
9965
+ memory,
9966
+ set: (mask) => void transport.call({ op: "proc-umask", mask }, Number.POSITIVE_INFINITY, "umask")
9967
+ });
9968
+ const fsOf = () => ownValue(module, "FS");
9969
+ noFollowUtimes(imports, fsOf, kernelGlue?.utimensat ?? utimensatOf(module), memory);
9970
+ kernelAccess(imports, fsOf, memory, kernelGlue);
9971
+ const glue = ownValue(module, "sliccFdImports");
9972
+ syncFsync(imports, fsOf, glue);
9973
+ positionalIo(imports, { glue, fs: fsOf, sys, memory });
9974
+ const grown = () => publishMemory(init.sab, memory()?.buffer.byteLength ?? 0);
9975
+ reportGrowth(imports, grown, kernelGlue);
9976
+ }
9931
9977
  async function runWasmProcess(init, port3, deps = {}) {
9932
9978
  const signals = new SignalGate(
9933
9979
  createSyncSabTransport(init.sab, port3, {
@@ -9989,24 +10035,13 @@ async function runWasmProcess(init, port3, deps = {}) {
9989
10035
  }
9990
10036
  }
9991
10037
  });
9992
- liveParent(imports, () => identify(transport, init).ppid);
9993
- kernelUmask(imports, {
9994
- initial: init.umask ?? 18,
9995
- memory: () => memory,
9996
- set: (mask) => void transport.call({ op: "proc-umask", mask }, Number.POSITIVE_INFINITY, "umask")
9997
- });
9998
- const fsOf = () => ownValue(module, "FS");
9999
- noFollowUtimes(imports, fsOf, utimensatOf(module), () => memory);
10000
- kernelAccess(imports, fsOf, () => memory);
10001
- const glue = ownValue(module, "sliccFdImports");
10002
- syncFsync(imports, () => ownValue(module, "FS"), glue);
10003
- positionalIo(imports, {
10004
- glue,
10005
- fs: () => ownValue(module, "FS"),
10038
+ kernelWrappers(imports, module, {
10039
+ init,
10040
+ transport,
10006
10041
  sys,
10007
- memory: () => memory
10042
+ memory: () => memory,
10043
+ ppid: () => identify(transport, init).ppid
10008
10044
  });
10009
- reportGrowth(imports, () => publishMemory(init.sab, memory?.buffer.byteLength ?? 0));
10010
10045
  return WebAssembly.instantiate(init.program.module, imports);
10011
10046
  }).then((instance) => {
10012
10047
  memory = wasmMemory(instance, imports);
@@ -10188,6 +10223,16 @@ var init_process_runtime = __esm({
10188
10223
  " ioctl: typeof ___syscall_ioctl === 'function' ? ___syscall_ioctl : undefined,",
10189
10224
  " setitimer: typeof __setitimer_js === 'function' ? __setitimer_js : undefined,",
10190
10225
  "};",
10226
+ "Module.sliccKernelImports ??= {",
10227
+ " openat: typeof ___syscall_openat === 'function' ? ___syscall_openat : undefined,",
10228
+ " mkdirat: typeof ___syscall_mkdirat === 'function' ? ___syscall_mkdirat : undefined,",
10229
+ " mknodat: typeof ___syscall_mknodat === 'function' ? ___syscall_mknodat : undefined,",
10230
+ " umask: typeof ___syscall_umask === 'function' ? ___syscall_umask : undefined,",
10231
+ " faccessat: typeof ___syscall_faccessat === 'function' ? ___syscall_faccessat : undefined,",
10232
+ " utimensat: typeof ___syscall_utimensat === 'function' ? ___syscall_utimensat : undefined,",
10233
+ " getppid: typeof _slicc_getppid_js === 'function' ? _slicc_getppid_js : undefined,",
10234
+ " resizeHeap: typeof _emscripten_resize_heap === 'function' ? _emscripten_resize_heap : undefined,",
10235
+ "};",
10191
10236
  "Module.sliccFdImports ??= {",
10192
10237
  " fd_sync: typeof _fd_sync === 'function' ? _fd_sync : undefined,",
10193
10238
  " fd_pread: typeof _fd_pread === 'function' ? _fd_pread : undefined,",