@ai-ecoverse/slicc-kernel 1.46.1 → 1.47.1

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/dist/node.js CHANGED
@@ -9174,6 +9174,7 @@ var SYSCALL_OPS = /* @__PURE__ */ new Set([
9174
9174
  "proc-captured",
9175
9175
  "proc-fork",
9176
9176
  "proc-kill",
9177
+ "thread-kill",
9177
9178
  "proc-list",
9178
9179
  "mount-list",
9179
9180
  "mount",
@@ -9338,14 +9339,14 @@ var WasmProcess = class {
9338
9339
  if (isFdSyscall(req)) return await this.fdSyscall(req, cancelled);
9339
9340
  if (isTtySyscall(req)) return this.ttySyscall(req);
9340
9341
  if (isJobSyscall(req)) return this.jobSyscall(req);
9341
- if (isSocketSyscall(req)) return await this.socketSyscall(req);
9342
+ if (isSocketSyscall(req)) return await this.socketSyscall(req, cancelled);
9342
9343
  if (isHttpSyscall(req)) return await this.httpSyscall(req);
9343
9344
  if (isHostSyscall(req)) {
9344
9345
  return await hostSyscall(req, {
9345
9346
  pid: this.pid,
9346
9347
  locks: this.options.locks,
9347
9348
  ops: this.asyncOps,
9348
- blocking: () => this.blockingSignal(),
9349
+ blocking: () => this.blockingSignal(cancelled),
9349
9350
  valid: (inner) => isWasmSyscall(inner) && !isHostSyscall(inner)
9350
9351
  });
9351
9352
  }
@@ -9353,7 +9354,7 @@ var WasmProcess = class {
9353
9354
  const { ptys, jobs } = this.options;
9354
9355
  return ptySyscall(req, { pid: this.pid, fds: this.fds, ptys, jobs });
9355
9356
  }
9356
- return await this.procSyscall(req);
9357
+ return await this.procSyscall(req, cancelled);
9357
9358
  } catch (e) {
9358
9359
  if (e instanceof KernelError || e instanceof SpawnError) {
9359
9360
  return { ok: false, errno: e.code, message: e.code };
@@ -9368,11 +9369,11 @@ var WasmProcess = class {
9368
9369
  refuseIfPending(restart) {
9369
9370
  if (restart && this.options.hasPending?.()) throw new KernelError("EINTR");
9370
9371
  }
9371
- blockingSignal() {
9372
- if (this.options.hasPending?.()) throw new KernelError("EINTR");
9373
- return this.interrupt.signal;
9372
+ blockingSignal(cancelled) {
9373
+ if (this.options.hasPending?.() || cancelled?.aborted) throw new KernelError("EINTR");
9374
+ return cancelled ? AbortSignal.any([this.interrupt.signal, cancelled]) : this.interrupt.signal;
9374
9375
  }
9375
- async read(req) {
9376
+ async read(req, cancelled) {
9376
9377
  const file = this.fds.get(req.fd).file;
9377
9378
  const read = req.peek ? file.peek : file.read;
9378
9379
  if (!read) throw new KernelError(req.peek && file.read ? "EOPNOTSUPP" : "EBADF");
@@ -9381,15 +9382,15 @@ var WasmProcess = class {
9381
9382
  if (file instanceof KernelSocket && file.unconnected) throw new KernelError("ENOTCONN");
9382
9383
  const ready2 = pollFile(file).readable;
9383
9384
  if (!ready2 && req.nonblock) throw new KernelError("EAGAIN");
9384
- const signal = ready2 ? this.interrupt.signal : this.blockingSignal();
9385
+ const signal = ready2 ? this.interrupt.signal : this.blockingSignal(cancelled);
9385
9386
  return read.call(file, Math.max(0, Math.min(req.max, MAX_READ)), signal);
9386
9387
  }
9387
- async write(fd, body, nonblock = false) {
9388
+ async write(fd, body, nonblock = false, cancelled) {
9388
9389
  const file = this.fds.get(fd).file;
9389
9390
  if (!file.write) throw new KernelError("EBADF");
9390
9391
  if (!pollFile(file).writable) {
9391
9392
  if (nonblock) throw new KernelError("EAGAIN");
9392
- return file.write(body, this.blockingSignal());
9393
+ return file.write(body, this.blockingSignal(cancelled));
9393
9394
  }
9394
9395
  if (nonblock) return file.write(body, AbortSignal.abort());
9395
9396
  return file.write(
@@ -9444,9 +9445,13 @@ var WasmProcess = class {
9444
9445
  if (isVfsSyscall(req)) return this.vfsSyscall(req);
9445
9446
  switch (req.op) {
9446
9447
  case "fd-read":
9447
- return { ok: true, kind: "bytes", bytes: await this.read(req) };
9448
+ return { ok: true, kind: "bytes", bytes: await this.read(req, cancelled) };
9448
9449
  case "fd-write":
9449
- return { ok: true, kind: "json", json: await this.write(req.fd, req.body, req.nonblock) };
9450
+ return {
9451
+ ok: true,
9452
+ kind: "json",
9453
+ json: await this.write(req.fd, req.body, req.nonblock, cancelled)
9454
+ };
9450
9455
  case "fd-close":
9451
9456
  this.options.locks?.closed(this.pid, req.fd);
9452
9457
  await Promise.resolve(this.fds.close(req.fd));
@@ -9511,8 +9516,7 @@ var WasmProcess = class {
9511
9516
  return { ok: true, kind: "json", json: this.options.jobs?.terminalNames() ?? [] };
9512
9517
  case "fd-select": {
9513
9518
  const { read, write, timeoutMs } = req;
9514
- const blocking = this.blockingSignal();
9515
- const signal = cancelled ? AbortSignal.any([blocking, cancelled]) : blocking;
9519
+ const signal = this.blockingSignal(cancelled);
9516
9520
  const selected = await selectFds(this.fds, read, write, timeoutMs, signal);
9517
9521
  return { ok: true, kind: "json", json: selected };
9518
9522
  }
@@ -9682,13 +9686,13 @@ var WasmProcess = class {
9682
9686
  if (!http) return Promise.resolve({ ok: false, errno: "ENETUNREACH", message: NO_TRANSPORT });
9683
9687
  return http.syscall(req);
9684
9688
  }
9685
- socketSyscall(req) {
9689
+ socketSyscall(req, cancelled) {
9686
9690
  this.net ??= this.options.net ?? new LoopbackNet();
9687
9691
  const resolver = this.options.resolver ?? HOSTS_ONLY;
9688
9692
  return socketSyscall(req, {
9689
9693
  fds: this.fds,
9690
9694
  net: this.net,
9691
- blocking: () => this.blockingSignal(),
9695
+ blocking: () => this.blockingSignal(cancelled),
9692
9696
  resolve: (name2, family, blocking) => resolver.resolve(name2, family, blocking)
9693
9697
  });
9694
9698
  }
@@ -9698,7 +9702,7 @@ var WasmProcess = class {
9698
9702
  if (!tty) throw new KernelError("ENOTTY");
9699
9703
  return tty;
9700
9704
  }
9701
- async procSyscall(req) {
9705
+ async procSyscall(req, cancelled) {
9702
9706
  switch (req.op) {
9703
9707
  case "proc-fork":
9704
9708
  this.refuseIfPending(req.restart);
@@ -9707,7 +9711,7 @@ var WasmProcess = class {
9707
9711
  this.refuseIfPending(req.restart);
9708
9712
  return { ok: true, kind: "json", json: await this.spawn(req) };
9709
9713
  case "proc-wait": {
9710
- const signal = req.nohang ? this.interrupt.signal : this.blockingSignal();
9714
+ const signal = req.nohang ? this.interrupt.signal : this.blockingSignal(cancelled);
9711
9715
  const flags = {
9712
9716
  untraced: req.untraced,
9713
9717
  continued: req.continued,
@@ -9767,7 +9771,10 @@ var WasmProcess = class {
9767
9771
  this.inherited &= ~(req.defaults ?? 0);
9768
9772
  return { ok: true, kind: "void" };
9769
9773
  case "sig-pause":
9770
- return this.pause();
9774
+ return this.pause(cancelled);
9775
+ case "thread-kill":
9776
+ if (!this.options.threadKill?.(req.tid, req.sig)) this.options.raise?.(req.sig);
9777
+ return { ok: true, kind: "void" };
9771
9778
  case "proc-captured":
9772
9779
  return { ok: true, kind: "bytes", bytes: this.children.captured(req.pid, req.slot) };
9773
9780
  }
@@ -9789,8 +9796,8 @@ var WasmProcess = class {
9789
9796
  }
9790
9797
  return { ok: true, kind: "void" };
9791
9798
  }
9792
- pause() {
9793
- return aborted(this.blockingSignal());
9799
+ pause(cancelled) {
9800
+ return aborted(this.blockingSignal(cancelled));
9794
9801
  }
9795
9802
  alarm;
9796
9803
  dlLog = [];
@@ -9907,6 +9914,18 @@ var Dying = class {
9907
9914
  return [typeof reported === "number" ? reported : CRASHED, void 0];
9908
9915
  }
9909
9916
  };
9917
+ function killThread(thread, caught, sig) {
9918
+ if (!thread || (caught & sigbit(sig)) === 0) return false;
9919
+ Atomics.or(thread.header, SAB_I_SIGNALS, sigbit(sig));
9920
+ Atomics.notify(thread.header, SAB_I_SIGNALS);
9921
+ const blocked = thread.interrupt;
9922
+ thread.interrupt = new AbortController();
9923
+ blocked.abort();
9924
+ return true;
9925
+ }
9926
+ function threadCancel(thread) {
9927
+ return Atomics.load(thread.header, SAB_I_SIGNALS) !== 0 ? AbortSignal.abort() : thread.interrupt.signal;
9928
+ }
9910
9929
  function notifiers(header3) {
9911
9930
  return {
9912
9931
  onPending(sig) {
@@ -9942,7 +9961,7 @@ function openLane(token, dispatch2) {
9942
9961
  }
9943
9962
  };
9944
9963
  }
9945
- function processInit(opts, sab, umask, lane) {
9964
+ function processInit(opts, sab, umask, cred, lane) {
9946
9965
  const base = {
9947
9966
  type: WASM_PROCESS_INIT,
9948
9967
  pid: opts.shownPid ?? opts.pid,
@@ -9952,6 +9971,7 @@ function processInit(opts, sab, umask, lane) {
9952
9971
  cwd: opts.cwd,
9953
9972
  sab,
9954
9973
  umask,
9974
+ cred,
9955
9975
  ...opts.ppid !== void 0 ? { ppid: opts.ppid } : {}
9956
9976
  };
9957
9977
  if (opts.program.abi === "js") {
@@ -9989,10 +10009,8 @@ function holdingToken(opts, holds) {
9989
10009
  renamed: (from, to) => holds.renamed(from, to)
9990
10010
  });
9991
10011
  }
9992
- function spawnWasmProcess(opts) {
9993
- const sab = new SharedArrayBuffer(SAB_HEADER_BYTES + SAB_DEFAULT_WINDOW_BYTES);
9994
- const header3 = new Int32Array(sab, 0, SAB_HEADER_I32);
9995
- const process2 = new WasmProcess(opts.pid, opts.fds, {
10012
+ function processOptions(opts, header3, hooks) {
10013
+ return {
9996
10014
  ...inherited(opts),
9997
10015
  ...opts.identity ? { identity: opts.identity } : {},
9998
10016
  ...opts.onSyscall ? { onSyscall: opts.onSyscall } : {},
@@ -10017,9 +10035,23 @@ function spawnWasmProcess(opts) {
10017
10035
  ...notifiers(header3),
10018
10036
  onTimer: (which) => void Atomics.or(header3, SAB_I_TIMERS, 1 << which),
10019
10037
  hasPending: () => Atomics.load(header3, SAB_I_SIGNALS) !== 0 || Atomics.load(header3, SAB_I_TIMERS) !== 0,
10020
- raise: (sig) => signal(sig),
10038
+ raise: hooks.raise,
10039
+ threadKill: hooks.threadKill,
10021
10040
  ...opts.program.abi === "wasi" || opts.program.abi === "js" ? { pendingBits: () => Atomics.load(header3, SAB_I_SIGNALS) } : {}
10022
- });
10041
+ };
10042
+ }
10043
+ function spawnWasmProcess(opts) {
10044
+ const sab = new SharedArrayBuffer(SAB_HEADER_BYTES + SAB_DEFAULT_WINDOW_BYTES);
10045
+ const header3 = new Int32Array(sab, 0, SAB_HEADER_I32);
10046
+ const threadSignals = /* @__PURE__ */ new Map();
10047
+ const process2 = new WasmProcess(
10048
+ opts.pid,
10049
+ opts.fds,
10050
+ processOptions(opts, header3, {
10051
+ raise: (sig) => signal(sig),
10052
+ threadKill: (tid, sig) => killThread(threadSignals.get(tid), process2.signalMasks().caught, sig)
10053
+ })
10054
+ );
10023
10055
  publishStops(process2, header3);
10024
10056
  const holds = new HeldPaths();
10025
10057
  opts.held?.add(holds);
@@ -10086,7 +10118,15 @@ function spawnWasmProcess(opts) {
10086
10118
  }
10087
10119
  const tsab = new SharedArrayBuffer(SAB_HEADER_BYTES + SAB_DEFAULT_WINDOW_BYTES);
10088
10120
  const tw = opts.createWorker();
10089
- const tresponder = attachSyncSabResponder(tw, tsab, token, { dispatch: dispatch2 });
10121
+ const tsignals = {
10122
+ header: new Int32Array(tsab, 0, SAB_HEADER_I32),
10123
+ interrupt: new AbortController()
10124
+ };
10125
+ threadSignals.set(thread.tid, tsignals);
10126
+ const tdispatch = (req) => dying.serving(
10127
+ () => isWasmSyscall(req) ? process2.syscall(req, threadCancel(tsignals)) : dispatchSyncFs(req)
10128
+ );
10129
+ const tresponder = attachSyncSabResponder(tw, tsab, token, { dispatch: tdispatch });
10090
10130
  const onThreadMessage = (event) => {
10091
10131
  const data = event.data;
10092
10132
  if (data?.type === WASM_THREAD_EXIT) endThread(thread.tid);
@@ -10095,6 +10135,7 @@ function spawnWasmProcess(opts) {
10095
10135
  tw.addEventListener("message", onThreadMessage);
10096
10136
  tw.addEventListener("error", onError);
10097
10137
  threads.set(thread.tid, () => {
10138
+ threadSignals.delete(thread.tid);
10098
10139
  tw.removeEventListener("message", onThreadMessage);
10099
10140
  tw.removeEventListener("error", onError);
10100
10141
  tresponder.dispose();
@@ -10109,13 +10150,14 @@ function spawnWasmProcess(opts) {
10109
10150
  env: opts.env,
10110
10151
  cwd: opts.cwd,
10111
10152
  sab: tsab,
10153
+ cred: copyCred(process2.cred),
10112
10154
  ...opts.ppid !== void 0 ? { ppid: opts.ppid } : {},
10113
10155
  thread
10114
10156
  };
10115
10157
  tw.postMessage(tinit);
10116
10158
  };
10117
10159
  worker.postMessage(
10118
- processInit(opts, sab, process2.umask, lane?.lane),
10160
+ processInit(opts, sab, process2.umask, copyCred(process2.cred), lane?.lane),
10119
10161
  transferOf(opts, lane?.lane)
10120
10162
  );
10121
10163
  const signal = (sig) => {