@ai-ecoverse/slicc-kernel 1.54.4 → 1.54.6

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/README.md CHANGED
@@ -187,8 +187,8 @@ On top of preview1 a WASI program gets:
187
187
 
188
188
  - **Threads**: `wasi.thread-spawn` (wasm32-wasip1-threads) and WASIX `thread_spawn_v2` start a worker per thread on the process's shared memory, sized from the module's own import (a 2 GiB maximum when the engine cannot reserve more). The threads share one descriptor table; `exit` or a trap in any thread ends the process. At most 64 threads per process; `SLICC_WASM_THREADS` sets another cap, up to 256.
189
189
  - **WASIX** (a module importing `wasix_32v1`): fork and setjmp/longjmp through Asyncify (the module is built with `wasm-opt --asyncify`), exec, `posix_spawn`, `waitpid` (with `WNOHANG`, and `WUNTRACED` for a child that stops; WASIX has no `WCONTINUED`), pipes, `dup2`, signal handlers and interval timers, terminal modes, and dynamic linking: a position-independent main module loads side modules with `dlopen` from `LD_LIBRARY_PATH`, its runtime path, `/lib`, `/usr/lib` and `/usr/local/lib`. Each thread has its own copy of the function table, so the kernel hands out its slots (a side module's range, a `dlsym`'d function, a closure): threads loading or looking up at the same time never get the same slot. As in glibc, one `dlopen` at a time runs in a process, constructors included: a `dlopen` from a library's constructor goes ahead in its own thread, and another thread's `dlopen` waits, then finds the library loaded and gets the same handle. A thread waiting there still ends or stops with its process, but a signal handler it has runs once that `dlopen` returns. The lock is let go when a `dlopen` fails (an exception, a trap or `pthread_exit` out of a constructor included) and when its thread ends; a forked child starts with it free. In such a program, `dlopen(NULL)`, and `dlopen` of the program's own file (also by a path that resolves to it through symlinks), give a handle for the program that is already running (no second copy is loaded); a program that isn't position-independent gets an error from `dlopen(NULL)`, as from any `dlopen`; `dlsym` on it searches the program, then the loaded libraries, and `dlclose` of it does nothing. A function already in the table (the program's own, or a library's) keeps its slot, so `dlsym(handle, "strlen") == strlen`.
190
- - **Foreign calls** (libffi's WASIX backend, so Python's `ctypes`): `call_dynamic` calls a function through the program's indirect function table (a static module's exported `__indirect_function_table`, or the dynamic linker's), its arguments and results packed in memory one after another (`i32`/`f32` 4 bytes, `i64`/`f64` 8). With `strict` set, the buffers must fit the function exactly (else `EINVAL`); without it, missing arguments are zero and results that do not fit are left out. A trap or exception of the callee goes through to the caller. JS cannot ask a function for its type, so the kernel reads the types from the module files (the program's and each side module's, once, when a program first calls through `call_dynamic`): a table slot that is empty, past the end, holds a function of no module the kernel knows of, or takes or returns a `v128`, gives `EINVAL`. For closures (callbacks such as `CFUNCTYPE`), a dynamically linked program gets table slots from `closure_allocate`, and `closure_free` gives them back for reuse and empties the slot in every thread; a static program gets `ENOTSUP`. `closure_prepare` puts a trampoline with the requested signature (any of `i32`, `i64`, `f32`, `f64`, `v128`) into such a slot, in every thread: each call stores its arguments below the stack pointer and calls the backing function as `backing(values, results, user_data)`, then returns what it wrote to `results`. The trampolines are small wasm modules made and compiled at run time, once per signature in each thread (an instance per closure), so a page whose Content Security Policy forbids compiling WebAssembly (no `'wasm-unsafe-eval'`) cannot run them; it cannot run the program either. A `fork` or a `longjmp` (both through Asyncify) inside a function reached through `call_dynamic` cannot unwind through it.
191
- - **Job control:** `^Z`, `SIGTSTP`, `SIGTTIN` and `SIGTTOU` stop a WASIX program at once, unless it has a handler for them. A stopped program that is busy computing is stopped for job control (`jobs` shows it, `fg`, `bg` and `SIGCONT` work), but its worker keeps a core busy until its next system call, since a worker's running code cannot be frozen from outside. Agents should `kill` such a job rather than leave it stopped. For the same reason, a signal with a handler or a terminating default (`^C`, `SIGTERM`) reaches a program that is busy computing only at its next system call, so a CPU-bound loop may not end on `^C`; `kill -9` always ends it. A thread blocked in the kernel or waiting on a lock (`pthread_cond_wait`, a futex) is woken by a signal for it (a futex wait checks for signals every 50 ms, so each waiting thread wakes briefly that often), and `pthread_kill` runs the handler on the thread it names. A signal that ends a sleep or a `poll`/`select` with a timeout makes the call fail with `EINTR` in a program whose libc says it is wasix-sysroot 2025.9.30-22 or later (its `slicc.libc` custom section). Older libcs turn that answer into `ENOTSUP` from `nanosleep` (up to -18), so for a program without the section the wait ends as if it had timed out, unless it imports `fd_fdflags_set` (then `EINTR`, as before).
190
+ - **Foreign calls** (libffi's WASIX backend, so Python's `ctypes`): `call_dynamic` calls a function through the program's indirect function table (a static module's exported `__indirect_function_table`, or the dynamic linker's), its arguments and results packed in memory one after another (`i32`/`f32` 4 bytes, `i64`/`f64` 8). With `strict` set, the buffers must fit the function exactly (else `EINVAL`, before the function is called); without it, missing arguments are zero (one cut short keeps the bytes given, with zeros above them) and results that do not fit are left out. A trap or exception of the callee goes through to the caller. JS cannot ask a function for its type, so the kernel reads the types from the module files (the program's and each side module's, once, when a program first calls through `call_dynamic`): a table slot that is empty, past the end, holds a function of no module the kernel knows of, or takes or returns a `v128`, gives `EINVAL`. For closures (callbacks such as `CFUNCTYPE`), a dynamically linked program gets table slots from `closure_allocate`, and `closure_free` gives them back for reuse and empties the slot in every thread; a static program gets `ENOTSUP`. `closure_prepare` puts a trampoline with the requested signature (any of `i32`, `i64`, `f32`, `f64`, `v128`) into such a slot, in every thread: each call stores its arguments below the stack pointer and calls the backing function as `backing(values, results, user_data)`, then returns what it wrote to `results`. The trampolines are small wasm modules made and compiled at run time, once per signature in each thread (an instance per closure), so a page whose Content Security Policy forbids compiling WebAssembly (no `'wasm-unsafe-eval'`) cannot run them; it cannot run the program either. A `fork` or a `longjmp` (both through Asyncify) inside a function reached through `call_dynamic` cannot unwind through it.
191
+ - **Job control:** `^Z`, `SIGTSTP`, `SIGTTIN` and `SIGTTOU` stop a WASIX program at once, unless it has a handler for them. A stopped program that is busy computing is stopped for job control (`jobs` shows it, `fg`, `bg` and `SIGCONT` work), but its worker keeps a core busy until its next system call, since a worker's running code cannot be frozen from outside. Agents should `kill` such a job rather than leave it stopped. For the same reason, a signal with a handler or a terminating default (`^C`, `SIGTERM`) reaches a program that is busy computing only at its next system call, so a CPU-bound loop may not end on `^C`; `kill -9` always ends it. A thread blocked in the kernel or waiting on a lock (`pthread_cond_wait`, a futex) is woken by a signal for it (a futex wait checks for signals every 50 ms, so each waiting thread wakes briefly that often), and `pthread_kill` runs the handler on the thread it names. A sleep, or a clock in `poll_oneoff`, never ends before the time asked (it can end later, never earlier). A signal that ends a sleep or a `poll`/`select` with a timeout makes the call fail with `EINTR` in a program whose libc says it is wasix-sysroot 2025.9.30-22 or later (its `slicc.libc` custom section). Older libcs turn that answer into `ENOTSUP` from `nanosleep` (up to -18), so for a program without the section the wait ends as if it had timed out, unless it imports `fd_fdflags_set` (then `EINTR`, as before).
192
192
  - **Pipes** have no file type for WASI and WASIX programs' `fstat` (`st_mode` type 0), because WASI has no FIFO file type. A socket type, as kernels 1.47.3 to 1.48.1 gave them, made Ruby treat a `popen` pipe as a socket (`close_write` then never ends the child's input). The kernel's `slicc_fs.fd_mode` answers `S_IFIFO | 0600` (Linux numbering) for a pipe, and wasix-sysroot 2025.9.30-21 and later take the type from it, so programs built on it see a FIFO (`S_ISFIFO`); older libcs keep only the permission bits. Until then Python's asyncio refuses a pipe for a subprocess (`Pipe transport is only for pipes, sockets and character devices`). They are read and written as pipes; socket calls on them fail with `ENOTSOCK`, and `isatty()` is false. Emscripten programs, bash included, see a FIFO. The same holds for any kernel descriptor that is neither seekable nor a terminal or device, such as captured output.
193
193
  - **Sockets**: the WASIX socket calls (`sock_open`, `sock_pair` for an `AF_UNIX` stream pair, `bind`, `listen`, `connect`, `accept`, options, local and peer names) are the kernel's loopback sockets, and preview1's `sock_recv` / `sock_send` / `sock_shutdown` work on them and on inherited ones. Writing to a socket whose peer is gone fails with `EPIPE`, through `fd_write` as through `sock_send`, as if `MSG_NOSIGNAL` were set, since a WASI program cannot ignore `SIGPIPE`; a pipe or terminal whose reader is gone still ends the program with status 141. HTTP and HTTPS leave through the realm proxy that `https_proxy` names, as for any other program; `resolve` answers `/etc/hosts` names and literal addresses, and the [uplink](#uplink)'s names.
194
194
  - **Diagnostics**: a trap ends the program with 134 and its message on stderr; with `SLICC_WASM_BACKTRACE=1` the wasm frames follow, named from the module's name section, or from a sidecar for a module shipped without one (`<module>.names` beside it, or the same path in a `<package>-names` package). `SLICC_WASI_STATS=1` makes a program print its calls, counted and timed, as it ends.
@@ -1698,7 +1698,7 @@ function pollOneoff(deps, inPtr, outPtr, nsubs, neventsPtr) {
1698
1698
  const { events, read: read2, write: write2 } = classify(deps.fds, subs);
1699
1699
  let interrupted = false;
1700
1700
  const earliest = subs.reduce((min, s) => Math.min(min, s.deadline), Infinity);
1701
- const expired = () => performance.now() + CLOCK_SLACK_MS >= earliest;
1701
+ const expired = () => performance.now() >= earliest;
1702
1702
  if (read2.length > 0 || write2.length > 0 || events.length === 0) {
1703
1703
  do {
1704
1704
  let timeoutMs = -1;
@@ -1720,7 +1720,7 @@ function pollOneoff(deps, inPtr, outPtr, nsubs, neventsPtr) {
1720
1720
  }
1721
1721
  const after = performance.now();
1722
1722
  for (const s of subs) {
1723
- if (s.type === EVENTTYPE.CLOCK && (interrupted || s.deadline <= after + CLOCK_SLACK_MS)) {
1723
+ if (s.type === EVENTTYPE.CLOCK && (interrupted || s.deadline <= after)) {
1724
1724
  events.push({ userdata: s.userdata, error: E.SUCCESS, type: s.type });
1725
1725
  }
1726
1726
  }
@@ -1728,12 +1728,10 @@ function pollOneoff(deps, inPtr, outPtr, nsubs, neventsPtr) {
1728
1728
  mem.view().setUint32(neventsPtr, events.length, true);
1729
1729
  return E.SUCCESS;
1730
1730
  }
1731
- var CLOCK_SLACK_MS;
1732
1731
  var init_wasi_poll = __esm({
1733
1732
  "src/process/wasi/wasi-poll.ts"() {
1734
1733
  "use strict";
1735
1734
  init_wasi_abi();
1736
- CLOCK_SLACK_MS = 0.5;
1737
1735
  }
1738
1736
  });
1739
1737
 
@@ -9339,6 +9337,12 @@ function read(view, at, type) {
9339
9337
  if (type === "f32") return view.getFloat32(at, true);
9340
9338
  return view.getFloat64(at, true);
9341
9339
  }
9340
+ function readSome(view, at, end, type) {
9341
+ if (at + SIZE2[type] <= end) return read(view, at, type);
9342
+ const some = new DataView(new ArrayBuffer(8));
9343
+ for (let i = 0; at + i < end; i++) some.setUint8(i, view.getUint8(at + i));
9344
+ return read(some, 0, type);
9345
+ }
9342
9346
  function write(view, at, type, value2) {
9343
9347
  if (type === "i32") view.setInt32(at, Number(value2), true);
9344
9348
  else if (type === "i64") view.setBigInt64(at, BigInt(value2), true);
@@ -9349,7 +9353,8 @@ function scalars(types) {
9349
9353
  if (types.includes("other")) throw new WasiError("EINVAL");
9350
9354
  return types;
9351
9355
  }
9352
- function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
9356
+ function callDynamic(ctx, ...operands) {
9357
+ const [id, values, valuesLen, results, resultsLen, strict] = operands.map((n) => n >>> 0);
9353
9358
  const table = ctx.table();
9354
9359
  if (!table) throw new WasiError("ENOTSUP");
9355
9360
  const fn = id < table.length ? table.get(id) : null;
@@ -9357,33 +9362,31 @@ function callDynamic(ctx, id, values, valuesLen, results, resultsLen, strict) {
9357
9362
  if (!fn || !sig) throw new WasiError("EINVAL");
9358
9363
  const params = scalars(sig.params);
9359
9364
  const outs = scalars(sig.results);
9365
+ const size = (types) => types.reduce((n, type) => n + SIZE2[type], 0);
9366
+ if (strict && (size(params) !== valuesLen || size(outs) !== resultsLen)) {
9367
+ throw new WasiError("EINVAL");
9368
+ }
9360
9369
  const view = ctx.mem.view();
9370
+ if (values + valuesLen > view.byteLength || results + resultsLen > view.byteLength) {
9371
+ throw new WasiError("EFAULT");
9372
+ }
9361
9373
  const end = values + valuesLen;
9362
9374
  let at = values;
9363
9375
  const args = params.map((type) => {
9364
- if (at + SIZE2[type] > end) {
9365
- if (strict) throw new WasiError("EINVAL");
9366
- return type === "i64" ? 0n : 0;
9367
- }
9368
- const v = read(view, at, type);
9376
+ const v = readSome(view, at, end, type);
9369
9377
  at += SIZE2[type];
9370
9378
  return v;
9371
9379
  });
9372
- if (strict && at !== end) throw new WasiError("EINVAL");
9373
9380
  const returned = fn(...args);
9374
9381
  const got = outs.length === 1 ? [returned] : returned ?? [];
9375
9382
  const after = ctx.mem.view();
9376
9383
  const stop = results + resultsLen;
9377
9384
  let out = results;
9378
9385
  outs.forEach((type, i) => {
9379
- if (out + SIZE2[type] > stop) {
9380
- if (strict) throw new WasiError("EINVAL");
9381
- return;
9382
- }
9386
+ if (out + SIZE2[type] > stop) return;
9383
9387
  write(after, out, type, got[i]);
9384
9388
  out += SIZE2[type];
9385
9389
  });
9386
- if (strict && out !== stop) throw new WasiError("EINVAL");
9387
9390
  }
9388
9391
  var SIZE2;
9389
9392
  var init_wasix_ffi = __esm({