@amritk/nish-aarch64-linux 0.38.0 → 0.39.0
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/bin/nish +0 -0
- package/docs/AI.md +348 -37
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +301 -18
- package/runtime/nish.h +74 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +15 -0
- package/runtime/runtime.c +5 -0
- package/scripts/build.sh +27 -8
- package/std/README.md +5 -4
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/net/handler.ts +130 -16
- package/std/net/http1-codec.ts +499 -150
- package/std/net/http1-server.ts +495 -22
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +162 -58
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +233 -0
package/runtime/nish.h
CHANGED
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
* time), runtime-aes.c (AES-GCM on the CPU's AES instructions),
|
|
14
14
|
* runtime-sha256.c (SHA-256 on the CPU's SHA instructions), runtime-chacha20.c
|
|
15
15
|
* (ChaCha20's keystream on AVX2 or NEON vectors), runtime-checksum.c
|
|
16
|
-
* (CRC-32 on its carry-less multiply or CRC instructions
|
|
16
|
+
* (CRC-32 on its carry-less multiply or CRC instructions, Adler-32 on SSSE3 or
|
|
17
|
+
* NEON) and runtime-term.c
|
|
17
18
|
* (stdin and the terminal, `nish:term`) split off
|
|
18
19
|
* the same way since. They are separate so that each carries its own measured
|
|
19
20
|
* code-size ceiling (docs/wp7-runtime.md, "Runtime additions and budget"); a
|
|
@@ -137,6 +138,15 @@ void *nish_arena_keep(uint64_t mark, void *p);
|
|
|
137
138
|
/* Bytes bumped in the current chunk (`Arena.used()`); a steady-state loop keeps it flat. */
|
|
138
139
|
uint64_t nish_arena_used(void);
|
|
139
140
|
|
|
141
|
+
/* ---- cErrno (WP27 S5, docs/wp27-ffi.md §10) -----------------------------
|
|
142
|
+
* A program that names `cErrno` stores 0 through `nish_errno_ptr()` right
|
|
143
|
+
* before every `declare function` call and copies errno into
|
|
144
|
+
* `nish_errno_slot` right after it, before any result marshalling; `cErrno()`
|
|
145
|
+
* loads the slot. The slot is per thread under NISH_THREADS, so the runtime's
|
|
146
|
+
* own allocation and string copying cannot disturb it. */
|
|
147
|
+
extern NISH_TLS int32_t nish_errno_slot;
|
|
148
|
+
int *nish_errno_ptr(void);
|
|
149
|
+
|
|
140
150
|
/* ---- Regions (runtime-region.c) -----------------------------------------
|
|
141
151
|
* Arenas made, entered, left and freed one at a time
|
|
142
152
|
* (docs/wp46-long-running-memory.md). A handle is a nonzero `uint64_t`: a
|
|
@@ -152,10 +162,15 @@ uint64_t nish_arena_used(void);
|
|
|
152
162
|
* entered and makes its handle stale. `nish_region_adopt` instead moves the
|
|
153
163
|
* chunks of a region that is not entered onto the thread's current arena, so
|
|
154
164
|
* they live on until that arena releases below them, and makes its handle
|
|
155
|
-
* stale: an `awaitAll` lane's region, once the lane has answered
|
|
165
|
+
* stale: an `awaitAll` lane's region, once the lane has answered; it panics
|
|
166
|
+
* on a child region or a region with children. `nish_region_child_new` makes
|
|
167
|
+
* a region whose parent is the one entered last on this thread, and panics
|
|
168
|
+
* when none is; freeing a region frees its children, deepest first, and
|
|
169
|
+
* panics before freeing anything when any of them is entered. The wasm
|
|
156
170
|
* profile has none of these (scripts/build.sh refuses IR that names them);
|
|
157
171
|
* wasi has. */
|
|
158
172
|
uint64_t nish_region_new(void);
|
|
173
|
+
uint64_t nish_region_child_new(void);
|
|
159
174
|
void *nish_region_enter(uint64_t r);
|
|
160
175
|
void nish_region_leave(uint64_t r, void *root);
|
|
161
176
|
void nish_region_free(uint64_t r);
|
|
@@ -167,14 +182,14 @@ void nish_region_adopt(uint64_t r);
|
|
|
167
182
|
* a node for a task of `kind` whose frame is `bytes` long, the frame 32 bytes
|
|
168
183
|
* in, with a region of its own; `nish_async_enter` makes a node the running
|
|
169
184
|
* task and enters its region, answering the task it displaced, which
|
|
170
|
-
* `nish_async_leave` restores; `nish_async_done`
|
|
171
|
-
* region and reuses its node. `nish_async_wait_fd` (`dir` 0
|
|
172
|
-
* writable) and `nish_async_sleep` answer a token
|
|
173
|
-
* pending, 1 ready, 0 failed. `nish_async_next`
|
|
174
|
-
* resume, blocking in the kernel until one is ready,
|
|
175
|
-
* ready and nothing waits. `nish_async_stat` answers
|
|
176
|
-
* completions dropped because their task had finished,
|
|
177
|
-
* finished.
|
|
185
|
+
* `nish_async_leave` restores; `nish_async_done` empties a finished task's
|
|
186
|
+
* region and reuses its node, region and all. `nish_async_wait_fd` (`dir` 0
|
|
187
|
+
* readable, 1 writable) and `nish_async_sleep` answer a token
|
|
188
|
+
* `nish_async_poll` reads: -1 pending, 1 ready, 0 failed. `nish_async_next`
|
|
189
|
+
* answers the next task to resume, blocking in the kernel until one is ready,
|
|
190
|
+
* or null when nothing is ready and nothing waits. `nish_async_stat` answers
|
|
191
|
+
* 0: nodes allocated, 1: completions dropped because their task had finished,
|
|
192
|
+
* 2: spawned tasks not yet finished.
|
|
178
193
|
*
|
|
179
194
|
* Cancellation is cooperative. A task's handle (`nish_async_task_id`) is its
|
|
180
195
|
* slot and generation, and `main`'s is 0. `nish_async_cancel` flags a task and
|
|
@@ -485,6 +500,21 @@ int32_t nish_crc32_hw(void);
|
|
|
485
500
|
(u8[]), conditioned as zlib's crc32(): 0 starts a message, and the answer
|
|
486
501
|
continues one. The caller has checked 0 <= off <= off + len <= buf.len. */
|
|
487
502
|
uint32_t nish_crc32_update(uint32_t crc, const nish_array *buf, int32_t off, int32_t len);
|
|
503
|
+
/* ---- Adler-32 on SSSE3 or NEON vectors ------------------------------------
|
|
504
|
+
* runtime/runtime-checksum.c as well: the compiler calls the kernel in place
|
|
505
|
+
* of std/compress/checksum.ts's private `adler32Walk`, behind
|
|
506
|
+
* `nish_adler32_hw`, and calls the Nish loop itself when that answers 0. */
|
|
507
|
+
/* 1 when this process runs the kernel, 0 when it runs the Nish loop. Resolved
|
|
508
|
+
on the first call and fixed from then on: `NISH_ADLER32=soft` pins 0; unset
|
|
509
|
+
or any other value is the best this machine has, CPUID leaf 1's SSSE3 bit on
|
|
510
|
+
x86-64 and always the kernel on a little-endian AArch64, where NEON is
|
|
511
|
+
baseline. Always 0 on other targets, wasm32 among them. */
|
|
512
|
+
int32_t nish_adler32_hw(void);
|
|
513
|
+
/* The Adler-32 (RFC 1950) `adler` extended over buf[off..off+len) (u8[]):
|
|
514
|
+
1 starts a message. An empty window answers `adler` as it came; any other
|
|
515
|
+
answer is reduced mod 65521 in both halves. The caller has checked
|
|
516
|
+
0 <= off <= off + len <= buf.len. */
|
|
517
|
+
uint32_t nish_adler32_update(uint32_t adler, const nish_array *buf, int32_t off, int32_t len);
|
|
488
518
|
|
|
489
519
|
/* `secureZero(bytes)` (#385): sets every byte of `bytes` to zero through
|
|
490
520
|
* `volatile` stores, out of line, so that neither `-O2` nor `-flto` can drop
|
|
@@ -547,6 +577,23 @@ nish_array *nish_readdir(const nish_str *path);
|
|
|
547
577
|
* and the bytes live in the arena, so copy what you keep before the next reset
|
|
548
578
|
* or release. */
|
|
549
579
|
nish_array *nish_read_file_bytes(const nish_str *path);
|
|
580
|
+
/* `fileOpen(path)`, `fileReadAt(fd, buf, off, len, pos)`, `fileSize(fd)` and
|
|
581
|
+
* `fileClose(fd)` (WP44): a regular file read a window at a time. `fileOpen`
|
|
582
|
+
* answers a descriptor (`O_RDONLY | O_CLOEXEC | O_NONBLOCK`, so a FIFO with
|
|
583
|
+
* no writer does not hang the open) or -21 for a directory and -22
|
|
584
|
+
* for anything else that is not a regular file. `nish_file_read_at` reads at
|
|
585
|
+
* most `len` bytes from byte `pos` into `buf[off, off + len)`, which the
|
|
586
|
+
* compiled call has range-checked; it answers the count, 0 at or past the end
|
|
587
|
+
* and for `len` 0, and -22 for a `pos` below 0 or a `pos + len` past
|
|
588
|
+
* 2^63 - 1. A negative `fd` is -9 to the three that take one, before any
|
|
589
|
+
* call. `nish_file_size` answers `st_size` now; `nish_file_close` closes
|
|
590
|
+
* and is never retried. None allocates. Every failure is a negated errno in
|
|
591
|
+
* Linux's numbering on every platform (-2, -6, -9, -13, -20, -21, -22, -23,
|
|
592
|
+
* -24, -29, -36, -40), and any other is -5; a path holding a NUL is -2. */
|
|
593
|
+
int32_t nish_file_open(const nish_str *path);
|
|
594
|
+
int32_t nish_file_read_at(int32_t fd, nish_array *buf, int64_t off, int64_t len, int64_t pos);
|
|
595
|
+
int64_t nish_file_size(int32_t fd);
|
|
596
|
+
int32_t nish_file_close(int32_t fd);
|
|
550
597
|
/* `renameSync(from, to)`, `unlinkSync(path)` and `writeFileBytesSync(path,
|
|
551
598
|
* bytes)`: rename(2), unlink(2), and every element of a `u8[]` written to a
|
|
552
599
|
* file opened as `nish_write_file` opens it (`O_TRUNC`, 0644, a link refused).
|
|
@@ -614,6 +661,14 @@ int32_t nish_spawn_child(const nish_array *argv);
|
|
|
614
661
|
* copied; SIGPIPE is blocked on the calling thread for the call, a SIGPIPE the
|
|
615
662
|
* call raised is consumed, and the thread's mask is restored after it. */
|
|
616
663
|
int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err);
|
|
664
|
+
/* `spawnPipedWithin(argv, stdin, stdout, stderr, timeoutMs)`: `nish_spawn_piped`
|
|
665
|
+
* with a deadline of `timeout_ms` milliseconds on CLOCK_MONOTONIC, a negative
|
|
666
|
+
* one none (`nish_spawn_piped` is this with -1). The same answers, plus -3 for
|
|
667
|
+
* a run the deadline ended: the child, its pid alone, is killed with SIGKILL
|
|
668
|
+
* and reaped, and both arrays keep the bytes read before the kill. The wait for
|
|
669
|
+
* the child's exit is bounded by the same deadline. -1 always under WASI. */
|
|
670
|
+
int32_t nish_spawn_piped_within(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err,
|
|
671
|
+
int32_t timeout_ms);
|
|
617
672
|
/* `spawnWithFds(argv, fds)`: `nish_spawn_child` with the child holding
|
|
618
673
|
* `fds[i]` (element type `int32_t`) as descriptor `3 + i`, close-on-exec
|
|
619
674
|
* cleared there. More than 64 descriptors or a negative one is -EINVAL before
|
|
@@ -949,6 +1004,15 @@ int64_t nish_cpu_online(void);
|
|
|
949
1004
|
`nish_cpu_prime` caches the pool's CPU count while /sys is still readable. */
|
|
950
1005
|
void nish_sandbox(int32_t caps, int32_t mode, const char *read_dirs, const char *write_dirs, const char *exec_paths);
|
|
951
1006
|
void nish_sandbox_check(void);
|
|
1007
|
+
/* SBX-1: set by `nish_sandbox` when the program has an exec allowlist;
|
|
1008
|
+
`nish_spawn_start` (runtime-os.c) then leaves the spawn to it. It checks
|
|
1009
|
+
`file` as `posix_spawnp` would resolve it, starts the child with the file
|
|
1010
|
+
actions `actions` (a `posix_spawn_file_actions_t`) and `argv`, holds it to
|
|
1011
|
+
that one exec, and answers its pid or a negative errno. The actions' `dup2`s
|
|
1012
|
+
read the `n` descriptors of `dup` (null for none) and write `base` onward;
|
|
1013
|
+
with the redirects of 1 and 2, nothing they write is above `base + n + 2`. */
|
|
1014
|
+
extern int32_t (*nish_spawn_sandboxed)(const char *file, const int *dup, int base, int n, const void *actions,
|
|
1015
|
+
char **argv);
|
|
952
1016
|
void nish_cpu_prime(void);
|
|
953
1017
|
void nish_parallel_range(nish_par_body body, void *ctx, int64_t len, int64_t grain);
|
|
954
1018
|
/* Keeps every later region and join on threads created and joined per call,
|
package/runtime/nish.mjs
CHANGED
|
@@ -89,6 +89,12 @@ provide("panic", shim.panic);
|
|
|
89
89
|
provide("readFileSync", shim.readFileSync);
|
|
90
90
|
provide("readFileSyncOrNull", shim.readFileSyncOrNull);
|
|
91
91
|
provide("readFileBytesSync", shim.readFileBytesSync);
|
|
92
|
+
// WP44: a regular file read a window at a time; the errors are Linux's
|
|
93
|
+
// negated errno on every platform, as `runtime-os.c` answers them.
|
|
94
|
+
provide("fileOpen", shim.fileOpen);
|
|
95
|
+
provide("fileReadAt", shim.fileReadAt);
|
|
96
|
+
provide("fileSize", shim.fileSize);
|
|
97
|
+
provide("fileClose", shim.fileClose);
|
|
92
98
|
provide("writeFileSync", shim.writeFileSync);
|
|
93
99
|
provide("appendFileSync", shim.appendFileSync);
|
|
94
100
|
provide("writeFileBytesSync", shim.writeFileBytesSync);
|
|
@@ -104,6 +110,7 @@ provide("realpathSync", shim.realpathSync);
|
|
|
104
110
|
provide("spawnSync", shim.spawnSync);
|
|
105
111
|
provide("spawnSyncTo", shim.spawnSyncTo);
|
|
106
112
|
provide("spawnPiped", shim.spawnPiped);
|
|
113
|
+
provide("spawnPipedWithin", shim.spawnPipedWithin);
|
|
107
114
|
// WP34 N12. `spawn`, `spawnWithFds` and `waitPid` throw, naming themselves: a
|
|
108
115
|
// child is reaped by Node's event loop. `kill`, `availableParallelism` and
|
|
109
116
|
// `totalMemory` answer.
|
|
@@ -342,29 +349,85 @@ const unsafeSource = UNSAFE_EXPORTS.map((name) => `export const ${name} = global
|
|
|
342
349
|
// table, keyed by its handle (a BigInt, as every `i64` is here), `enter` runs
|
|
343
350
|
// its function on the root, and `free` drops the entry. The handle checks the
|
|
344
351
|
// native runtime makes are made here too, so a freed handle or a second
|
|
345
|
-
// `enter` throws where the native program stops.
|
|
352
|
+
// `enter` throws where the native program stops. A child (`child`,
|
|
353
|
+
// `childWith`) records the region entered last as its parent, as
|
|
354
|
+
// `runtime/runtime-region.c` does; `free` of a parent drops its children,
|
|
355
|
+
// deepest first, and throws before dropping anything if any is entered.
|
|
346
356
|
const regionSource = `
|
|
347
357
|
const roots = new Map();
|
|
348
358
|
const entered = new Set();
|
|
359
|
+
const parents = new Map();
|
|
360
|
+
const children = new Map();
|
|
361
|
+
const stack = [];
|
|
349
362
|
let next = 0n;
|
|
350
363
|
const rootOf = (r) => {
|
|
351
364
|
if (!roots.has(r)) throw new Error("nish:region: the region is freed or was never made");
|
|
352
365
|
if (entered.has(r)) throw new Error("nish:region: the region is entered already");
|
|
353
366
|
return roots.get(r);
|
|
354
367
|
};
|
|
368
|
+
const inside = (r, g) => {
|
|
369
|
+
entered.add(r);
|
|
370
|
+
stack.push(r);
|
|
371
|
+
try { return g(); } finally { stack.pop(); entered.delete(r); }
|
|
372
|
+
};
|
|
355
373
|
const run = (r, f, arg, withArg) => {
|
|
356
374
|
const root = rootOf(r);
|
|
357
|
-
|
|
358
|
-
|
|
375
|
+
return inside(r, () => (withArg ? f(root, arg) : f(root)));
|
|
376
|
+
};
|
|
377
|
+
const make = (parent, build) => {
|
|
378
|
+
next += 1n;
|
|
379
|
+
const r = next;
|
|
380
|
+
roots.set(r, null);
|
|
381
|
+
children.set(r, new Set());
|
|
382
|
+
if (parent !== 0n) {
|
|
383
|
+
parents.set(r, parent);
|
|
384
|
+
children.get(parent).add(r);
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
roots.set(r, inside(r, build));
|
|
388
|
+
} catch (e) {
|
|
389
|
+
roots.delete(r);
|
|
390
|
+
children.delete(r);
|
|
391
|
+
parents.delete(r);
|
|
392
|
+
if (parent !== 0n) children.get(parent).delete(r);
|
|
393
|
+
throw e;
|
|
394
|
+
}
|
|
395
|
+
return r;
|
|
396
|
+
};
|
|
397
|
+
const parentNow = () => {
|
|
398
|
+
if (stack.length === 0) throw new Error("nish:region: a child region made with no region entered");
|
|
399
|
+
return stack[stack.length - 1];
|
|
400
|
+
};
|
|
401
|
+
// The regions under r and r, each after every region under it: deepest first.
|
|
402
|
+
const subtree = (r) => {
|
|
403
|
+
const out = [];
|
|
404
|
+
const todo = [r];
|
|
405
|
+
while (todo.length > 0) {
|
|
406
|
+
const x = todo.pop();
|
|
407
|
+
out.push(x);
|
|
408
|
+
for (const c of children.get(x)) todo.push(c);
|
|
409
|
+
}
|
|
410
|
+
return out.reverse();
|
|
359
411
|
};
|
|
360
412
|
export class Region {}
|
|
361
|
-
export const region = (f) =>
|
|
362
|
-
export const regionWith = (arg, f) =>
|
|
413
|
+
export const region = (f) => make(0n, () => f());
|
|
414
|
+
export const regionWith = (arg, f) => make(0n, () => f(arg));
|
|
415
|
+
export const child = (f) => make(parentNow(), () => f());
|
|
416
|
+
export const childWith = (arg, f) => make(parentNow(), () => f(arg));
|
|
363
417
|
export const enter = (r, f) => run(r, f, undefined, false);
|
|
364
418
|
export const enterWith = (r, arg, f) => run(r, f, arg, true);
|
|
365
419
|
export const free = (r) => {
|
|
366
420
|
if (entered.has(r)) throw new Error("nish:region: the region is freed while it is entered");
|
|
367
|
-
if (!roots.
|
|
421
|
+
if (!roots.has(r)) throw new Error("nish:region: the region is freed or was never made");
|
|
422
|
+
const doomed = subtree(r);
|
|
423
|
+
if (doomed.some((d) => entered.has(d))) throw new Error("nish:region: the region is freed while it is entered");
|
|
424
|
+
const parent = parents.get(r);
|
|
425
|
+
if (parent !== undefined) children.get(parent).delete(r);
|
|
426
|
+
for (const d of doomed) {
|
|
427
|
+
roots.delete(d);
|
|
428
|
+
children.delete(d);
|
|
429
|
+
parents.delete(d);
|
|
430
|
+
}
|
|
368
431
|
};
|
|
369
432
|
`;
|
|
370
433
|
const TERM_EXPORTS = ["readStdin", "waitStdin", "isTerminal", "terminalSize", "setRawMode", "setTerminalRestore"];
|
package/runtime/node.mjs
CHANGED
|
@@ -132,6 +132,104 @@ function putFileMode(path, data, flag, mode) {
|
|
|
132
132
|
return answer;
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
/**
|
|
136
|
+
* WP44's file errors as `runtime-os.c`'s `nish_fs_err` answers them: the
|
|
137
|
+
* negated Linux errno of the set docs/wp44-streaming-files.md §3 names, by
|
|
138
|
+
* Node's code rather than the platform's number, so a macOS run answers what
|
|
139
|
+
* Linux does. Any other failure is -5 (`EIO`).
|
|
140
|
+
*/
|
|
141
|
+
const FILE_ERRNO = {
|
|
142
|
+
ENOENT: -2,
|
|
143
|
+
ENXIO: -6,
|
|
144
|
+
EBADF: -9,
|
|
145
|
+
EACCES: -13,
|
|
146
|
+
ENOTDIR: -20,
|
|
147
|
+
EISDIR: -21,
|
|
148
|
+
EINVAL: -22,
|
|
149
|
+
ENFILE: -23,
|
|
150
|
+
EMFILE: -24,
|
|
151
|
+
ESPIPE: -29,
|
|
152
|
+
ENAMETOOLONG: -36,
|
|
153
|
+
ELOOP: -40,
|
|
154
|
+
};
|
|
155
|
+
const fileErr = (e) => FILE_ERRNO[e.code] ?? -5;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* A negative descriptor is -9 (`EBADF`), as the kernel answers it natively:
|
|
159
|
+
* Node throws `ERR_OUT_OF_RANGE` for one before it asks, which `fileErr` would
|
|
160
|
+
* make -5.
|
|
161
|
+
*/
|
|
162
|
+
const EBADF = -9;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* `fileOpen(path)` (WP44): a descriptor on a regular file opened read-only and
|
|
166
|
+
* non-blocking, or a negative errno; a directory is -21 and any other kind -22,
|
|
167
|
+
* closed again first. A path holding a NUL is -2, as `nish_cpath` answers it.
|
|
168
|
+
*/
|
|
169
|
+
function fileOpen(path) {
|
|
170
|
+
if (path.includes("\0")) return -2;
|
|
171
|
+
let fd;
|
|
172
|
+
try {
|
|
173
|
+
fd = fs.openSync(path, fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0));
|
|
174
|
+
} catch (e) {
|
|
175
|
+
return fileErr(e);
|
|
176
|
+
}
|
|
177
|
+
let r;
|
|
178
|
+
try {
|
|
179
|
+
const st = fs.fstatSync(fd);
|
|
180
|
+
r = st.isFile() ? 0 : st.isDirectory() ? -21 : -22;
|
|
181
|
+
} catch (e) {
|
|
182
|
+
r = fileErr(e);
|
|
183
|
+
}
|
|
184
|
+
if (r === 0) return fd;
|
|
185
|
+
errnoOr(0, () => fs.closeSync(fd));
|
|
186
|
+
return r;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** The scratch a positional read lands in before its bytes are copied to the array. */
|
|
190
|
+
let fileScratch = Buffer.alloc(0);
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* `fileReadAt(fd, buf, off, len, pos)` (WP44): up to `len` bytes from offset
|
|
194
|
+
* `pos` into `buf[off..]`, the count (0 at end of file) or a negative errno.
|
|
195
|
+
* The window is checked by the caller (`core/host.mjs`); `pos` is a bigint.
|
|
196
|
+
*/
|
|
197
|
+
function fileReadAt(fd, buf, off, len, pos) {
|
|
198
|
+
if (pos < 0n || BigInt(len) > 0x7fffffffffffffffn - pos) return -22;
|
|
199
|
+
if (len === 0) return 0;
|
|
200
|
+
if (fd < 0) return EBADF;
|
|
201
|
+
let n;
|
|
202
|
+
try {
|
|
203
|
+
if (fileScratch.length < len) fileScratch = Buffer.alloc(len);
|
|
204
|
+
n = fs.readSync(fd, fileScratch, 0, len, pos);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
return fileErr(e);
|
|
207
|
+
}
|
|
208
|
+
for (let i = 0; i < n; i++) buf[off + i] = fileScratch[i];
|
|
209
|
+
return n;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** `fileSize(fd)` (WP44): the size in bytes as a bigint, or a negative errno. */
|
|
213
|
+
function fileSize(fd) {
|
|
214
|
+
if (fd < 0) return BigInt(EBADF);
|
|
215
|
+
try {
|
|
216
|
+
return fs.fstatSync(fd, { bigint: true }).size;
|
|
217
|
+
} catch (e) {
|
|
218
|
+
return BigInt(fileErr(e));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** `fileClose(fd)` (WP44): 0, or a negative errno. */
|
|
223
|
+
function fileClose(fd) {
|
|
224
|
+
if (fd < 0) return EBADF;
|
|
225
|
+
try {
|
|
226
|
+
fs.closeSync(fd);
|
|
227
|
+
return 0;
|
|
228
|
+
} catch (e) {
|
|
229
|
+
return fileErr(e);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
135
233
|
/** `writeFileSyncMode(path, data, mode)`: `WRITE_FLAGS`, so a link at the path is refused. */
|
|
136
234
|
const writeFileSyncMode = (path, data, mode) => putFileMode(path, data, WRITE_FLAGS, mode);
|
|
137
235
|
|
|
@@ -320,6 +418,18 @@ const PIPED_CAP = 16 * 1024 * 1024;
|
|
|
320
418
|
* A child that exits without reading its stdin is not an error on either side.
|
|
321
419
|
*/
|
|
322
420
|
function spawnPiped(argv, input) {
|
|
421
|
+
return spawnPipedWithin(argv, input, -1);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* `spawnPipedWithin(argv, stdin, timeoutMs)`'s run: `spawnPiped`'s, with
|
|
426
|
+
* Node's `timeout` set when `timeoutMs` is not negative. Node kills the child
|
|
427
|
+
* with `SIGKILL` when it passes and reports `ETIMEDOUT`, which is -3 here as
|
|
428
|
+
* in the runtime, with the bytes Node read before the kill kept. Node reads a
|
|
429
|
+
* `timeout` of 0 as none, so a deadline of zero is passed as 1 ms: already
|
|
430
|
+
* past for any child that has to start a program.
|
|
431
|
+
*/
|
|
432
|
+
function spawnPipedWithin(argv, input, timeoutMs) {
|
|
323
433
|
if (argv.length === 0) return [-1, [], []];
|
|
324
434
|
let r;
|
|
325
435
|
// Node throws, rather than reporting, for an empty command or a NUL in any
|
|
@@ -329,6 +439,7 @@ function spawnPiped(argv, input) {
|
|
|
329
439
|
input: Uint8Array.from(input),
|
|
330
440
|
maxBuffer: PIPED_CAP,
|
|
331
441
|
killSignal: "SIGKILL",
|
|
442
|
+
...(timeoutMs >= 0 ? { timeout: Math.max(timeoutMs, 1) } : {}),
|
|
332
443
|
});
|
|
333
444
|
} catch {
|
|
334
445
|
return [-1, [], []];
|
|
@@ -338,6 +449,7 @@ function spawnPiped(argv, input) {
|
|
|
338
449
|
const err = bytes(r.stderr);
|
|
339
450
|
// `EPIPE` is the child closing its stdin unread, which Node reports with
|
|
340
451
|
// the status it still waited for; the runtime ignores it the same way.
|
|
452
|
+
if (r.error !== undefined && r.error.code === "ETIMEDOUT") return [-3, out, err];
|
|
341
453
|
if (r.error !== undefined && r.error.code !== "EPIPE") return [r.error.code === "ENOBUFS" ? -2 : -1, out, err];
|
|
342
454
|
if (r.signal !== null && r.signal !== undefined) return [128 + (os.constants.signals[r.signal] ?? 0), out, err];
|
|
343
455
|
return [r.status === null ? -1 : r.status, out, err];
|
|
@@ -463,6 +575,10 @@ const CALLS = {
|
|
|
463
575
|
readFileSync,
|
|
464
576
|
readFileSyncOrNull,
|
|
465
577
|
readFileBytesSync,
|
|
578
|
+
fileOpen,
|
|
579
|
+
fileReadAt,
|
|
580
|
+
fileSize,
|
|
581
|
+
fileClose,
|
|
466
582
|
writeFileSync,
|
|
467
583
|
appendFileSync,
|
|
468
584
|
writeFileBytesSync,
|
|
@@ -482,6 +598,7 @@ const CALLS = {
|
|
|
482
598
|
spawnSync,
|
|
483
599
|
spawnSyncTo,
|
|
484
600
|
spawnPiped,
|
|
601
|
+
spawnPipedWithin,
|
|
485
602
|
kill,
|
|
486
603
|
availableParallelism,
|
|
487
604
|
totalMemory,
|
package/runtime/runtime-aes.c
CHANGED
|
@@ -19,8 +19,9 @@
|
|
|
19
19
|
* Nish code in std/crypto/aes.ts instead: the compiler keeps that code and
|
|
20
20
|
* branches on `nish_aes_hw` at each call, so a table-free C AES here would only
|
|
21
21
|
* be a second copy of the code under review. Every other target, wasm32
|
|
22
|
-
* included,
|
|
23
|
-
*
|
|
22
|
+
* included, and a toolchain without the intrinsic headers compile
|
|
23
|
+
* `nish_aes_hw` to `return 0` and the kernels to a trap nothing reaches, so a
|
|
24
|
+
* link that names them still resolves.
|
|
24
25
|
*
|
|
25
26
|
* The round-key array, `rk`, is the `8 * (rounds + 1)` words (88 or 120 u64s)
|
|
26
27
|
* the bitsliced schedule uses, laid out differently on this path. One process
|
|
@@ -70,12 +71,19 @@
|
|
|
70
71
|
|
|
71
72
|
#include "nish.h"
|
|
72
73
|
|
|
73
|
-
#if defined(
|
|
74
|
+
#if defined(__has_include)
|
|
75
|
+
#define NISH_AES_HAS(h) __has_include(h)
|
|
76
|
+
#else
|
|
77
|
+
#define NISH_AES_HAS(h) 0
|
|
78
|
+
#endif
|
|
79
|
+
|
|
80
|
+
#if defined(__x86_64__) && NISH_AES_HAS(<cpuid.h>) && NISH_AES_HAS(<tmmintrin.h>) && \
|
|
81
|
+
NISH_AES_HAS(<wmmintrin.h>)
|
|
74
82
|
#include <cpuid.h>
|
|
75
83
|
#include <tmmintrin.h>
|
|
76
84
|
#include <wmmintrin.h>
|
|
77
85
|
#define NISH_AES_X86 1
|
|
78
|
-
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
86
|
+
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && NISH_AES_HAS(<arm_neon.h>)
|
|
79
87
|
#include <arm_neon.h>
|
|
80
88
|
#define NISH_AES_ARM 1
|
|
81
89
|
#endif
|
package/runtime/runtime-async.c
CHANGED
|
@@ -49,16 +49,30 @@
|
|
|
49
49
|
* number is reused for. The duplicate is closed when the wait fires.
|
|
50
50
|
*
|
|
51
51
|
* **Regions.** Each spawned task owns a region (runtime-region.c), entered
|
|
52
|
-
* whenever the task runs and
|
|
52
|
+
* whenever the task runs and emptied when it finishes, so whatever a task
|
|
53
53
|
* allocates is given back when it ends. The driver enters and leaves around
|
|
54
|
-
* each resume (`nish_async_enter`, `nish_async_leave`).
|
|
54
|
+
* each resume (`nish_async_enter`, `nish_async_leave`). A node keeps its
|
|
55
|
+
* region from its first task on: `nish_async_done` enters it once more and
|
|
56
|
+
* rewinds it (`nish_reset_arena`) rather than freeing it, so the node's next
|
|
57
|
+
* task (a later spawn of the same async function: a node never changes
|
|
58
|
+
* kind) makes no region, and bumps from the newest chunk the last one left
|
|
59
|
+
* when that chunk is at most 64 KiB (`NISH_ASYNC_KEEP`), so a task that fits
|
|
60
|
+
* it asks `malloc` for nothing. The region keeps its handle and generation:
|
|
61
|
+
* the handle never leaves this file, and a task's region has no children
|
|
62
|
+
* (NL2351), so no copy of it is made stale by living on. A larger chunk is
|
|
63
|
+
* freed, and once the free nodes keep 16 MiB between them
|
|
64
|
+
* (`NISH_ASYNC_KEEP_TOTAL`) a finishing task keeps none and its region starts
|
|
65
|
+
* again from its first chunk size, so a burst of large tasks does not pin its
|
|
66
|
+
* high-water mark. The kept chunk is not zeroed: arena memory never is, and
|
|
67
|
+
* the compiler initialises what the language promises
|
|
68
|
+
* (docs/security/async.md, ASYNC-20).
|
|
55
69
|
*
|
|
56
70
|
* **Cancellation.** `nish_async_cancel` marks a task cancelled
|
|
57
71
|
* (`NISH_TASK_CANCELLED`, a bit beside the queued bit) and fails each wait it
|
|
58
72
|
* has pending, so every parked await answers false and the task is queued; a
|
|
59
73
|
* wait the task makes afterwards is made already failed. Nothing is unwound:
|
|
60
74
|
* the task runs on from each failed await to its end, its `using` bindings
|
|
61
|
-
* dispose as they do on any return, and `nish_async_done`
|
|
75
|
+
* dispose as they do on any return, and `nish_async_done` empties its region.
|
|
62
76
|
* `nish_async_cancel_after` arms a deadline that cancels a task when it
|
|
63
77
|
* passes, a timer in the heap that `done` takes out, and
|
|
64
78
|
* `nish_async_wait_fd_within` a descriptor wait that fails when its own
|
|
@@ -106,9 +120,9 @@ typedef struct nish_task {
|
|
|
106
120
|
struct nish_task *next; /* the ready queue's link, or the free list's */
|
|
107
121
|
int32_t kind; /* the spawned function's kind; -1 for `main` */
|
|
108
122
|
uint32_t gen; /* moves on each time the node is reused */
|
|
109
|
-
uint64_t region; /* the task's region; 0 for `main` and a
|
|
123
|
+
uint64_t region; /* the task's region; 0 for `main` and a node never yet used */
|
|
110
124
|
uint32_t index; /* the node's place in the task table, for ever */
|
|
111
|
-
uint32_t flags; /* NISH_TASK_QUEUED, NISH_TASK_CANCELLED */
|
|
125
|
+
uint32_t flags; /* NISH_TASK_QUEUED, NISH_TASK_CANCELLED; on the free list, the kept bytes */
|
|
112
126
|
} nish_task;
|
|
113
127
|
|
|
114
128
|
/* On the ready queue, so it is queued once. */
|
|
@@ -174,6 +188,16 @@ static uint32_t nish_async_fd_waits;
|
|
|
174
188
|
static int32_t nish_async_allocated;
|
|
175
189
|
static int32_t nish_async_late;
|
|
176
190
|
static int32_t nish_async_live; /* spawned tasks not yet done, each holding a region */
|
|
191
|
+
/* What a finished task's region may keep for the node's next task: its newest
|
|
192
|
+
chunk when that is no larger than NISH_ASYNC_KEEP, and nothing once the
|
|
193
|
+
free nodes together keep NISH_ASYNC_KEEP_TOTAL, so the most the free lists
|
|
194
|
+
hold is that total and one chunk. A free node's `flags` hold what its
|
|
195
|
+
region kept, from bit NISH_TASK_KEPT_SHIFT up. */
|
|
196
|
+
#define NISH_ASYNC_KEEP ((uint64_t)1 << 16)
|
|
197
|
+
#define NISH_ASYNC_KEEP_TOTAL ((uint64_t)1 << 24)
|
|
198
|
+
#define NISH_TASK_KEPT_SHIFT 8
|
|
199
|
+
static uint64_t nish_async_kept; /* bytes the free nodes' regions keep */
|
|
200
|
+
extern NISH_TLS uint64_t nish_region_chunk; /* runtime.c: the entered region's next chunk size */
|
|
177
201
|
|
|
178
202
|
enum { NISH_ASYNC_OOM, NISH_ASYNC_POLLER, NISH_ASYNC_TOO_MANY, NISH_ASYNC_WAIT };
|
|
179
203
|
#define NISH_ASYNC_MSG(text) { sizeof text - 1, text }
|
|
@@ -267,8 +291,9 @@ void *nish_async_spawn(int32_t kind, uint64_t bytes) {
|
|
|
267
291
|
slab->free = t->next;
|
|
268
292
|
t->next = NULL;
|
|
269
293
|
t->kind = kind;
|
|
294
|
+
nish_async_kept -= t->flags >> NISH_TASK_KEPT_SHIFT;
|
|
270
295
|
t->flags = 0;
|
|
271
|
-
t->region = nish_region_new();
|
|
296
|
+
if (!t->region) t->region = nish_region_new();
|
|
272
297
|
nish_async_live++;
|
|
273
298
|
return t;
|
|
274
299
|
}
|
|
@@ -291,8 +316,8 @@ void nish_async_leave(void *node, void *prev) {
|
|
|
291
316
|
|
|
292
317
|
static void nish_async_disarm(nish_task *t);
|
|
293
318
|
|
|
294
|
-
/* A task finished: take out its deadline,
|
|
295
|
-
back, a generation on. */
|
|
319
|
+
/* A task finished: take out its deadline, empty its region for the node's
|
|
320
|
+
next task and put its node back, a generation on. */
|
|
296
321
|
void nish_async_done(void *node) {
|
|
297
322
|
nish_task *t = node;
|
|
298
323
|
if (t->kind < 0) return;
|
|
@@ -313,8 +338,16 @@ void nish_async_done(void *node) {
|
|
|
313
338
|
}
|
|
314
339
|
t->flags &= ~NISH_TASK_QUEUED;
|
|
315
340
|
}
|
|
316
|
-
|
|
317
|
-
|
|
341
|
+
nish_region_enter(t->region);
|
|
342
|
+
nish_reset_arena();
|
|
343
|
+
if (nish_arena.cap > (nish_async_kept < NISH_ASYNC_KEEP_TOTAL ? NISH_ASYNC_KEEP : 0)) {
|
|
344
|
+
nish_free_arena();
|
|
345
|
+
nish_region_chunk = 8;
|
|
346
|
+
}
|
|
347
|
+
uint64_t kept = nish_arena.cap;
|
|
348
|
+
nish_region_leave(t->region, NULL);
|
|
349
|
+
nish_async_kept += kept;
|
|
350
|
+
t->flags = (uint32_t)kept << NISH_TASK_KEPT_SHIFT;
|
|
318
351
|
nish_async_live--;
|
|
319
352
|
t->gen = t->gen == UINT32_MAX ? 1 : t->gen + 1;
|
|
320
353
|
nish_slab *slab = &nish_async_slabs[t->kind];
|