@amritk/nish-aarch64-linux 0.38.0 → 0.40.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.
Files changed (86) hide show
  1. package/INSTALL.md +74 -129
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +455 -65
  4. package/package.json +1 -1
  5. package/runtime/core/host.d.ts +11 -0
  6. package/runtime/core/host.mjs +35 -0
  7. package/runtime/nish.d.ts +320 -33
  8. package/runtime/nish.h +84 -10
  9. package/runtime/nish.mjs +69 -6
  10. package/runtime/node.mjs +117 -0
  11. package/runtime/page-frame.d.ts +123 -0
  12. package/runtime/page-frame.mjs +736 -0
  13. package/runtime/page-game.d.ts +132 -0
  14. package/runtime/page-game.mjs +738 -0
  15. package/runtime/runtime-aes.c +97 -12
  16. package/runtime/runtime-async.c +43 -10
  17. package/runtime/runtime-bytes.c +88 -4
  18. package/runtime/runtime-checksum.c +207 -15
  19. package/runtime/runtime-net.c +64 -3
  20. package/runtime/runtime-os.c +174 -27
  21. package/runtime/runtime-region.c +94 -10
  22. package/runtime/runtime-sandbox.c +404 -17
  23. package/runtime/runtime-sha256.c +11 -4
  24. package/runtime/runtime-wasm.c +437 -0
  25. package/runtime/runtime.c +5 -0
  26. package/runtime/vite.mjs +1 -1
  27. package/scripts/build.sh +42 -8
  28. package/std/README.md +78 -91
  29. package/std/array.ts +36 -3
  30. package/std/collections.ts +51 -0
  31. package/std/compress/checksum.ts +13 -0
  32. package/std/compress/deflate.ts +0 -2
  33. package/std/crypto/hmac.ts +37 -10
  34. package/std/crypto/p256.ts +1629 -2732
  35. package/std/crypto/p384.ts +3 -1
  36. package/std/crypto/pki.ts +151 -13
  37. package/std/crypto/sha256.ts +34 -6
  38. package/std/crypto/x25519.ts +153 -64
  39. package/std/crypto/x509.ts +120 -10
  40. package/std/host/game.ts +273 -0
  41. package/std/input.ts +56 -0
  42. package/std/json-tokenizer.ts +344 -111
  43. package/std/json-typed.ts +256 -12
  44. package/std/json-value.ts +144 -3
  45. package/std/json.ts +63 -8
  46. package/std/net/fetch.ts +22 -4
  47. package/std/net/handler.ts +130 -16
  48. package/std/net/http-server.ts +3382 -0
  49. package/std/net/http1-client-codec.ts +6 -2
  50. package/std/net/http1-codec.ts +636 -162
  51. package/std/net/http1-server.ts +569 -42
  52. package/std/net/http2-cleartext.ts +526 -0
  53. package/std/net/http2-tls.ts +48 -0
  54. package/std/net/http2.ts +131 -14
  55. package/std/net/http3-server.ts +29 -3
  56. package/std/net/http3.ts +180 -7
  57. package/std/net/postgres-async.ts +1304 -86
  58. package/std/net/postgres-carrier.ts +343 -10
  59. package/std/net/postgres.ts +122 -7
  60. package/std/net/prefork.ts +195 -11
  61. package/std/net/quic-datagram.ts +30 -0
  62. package/std/net/quic-listener.ts +38 -12
  63. package/std/net/quic-packet.ts +19 -6
  64. package/std/net/quic-recovery.ts +373 -27
  65. package/std/net/quic-stream.ts +195 -37
  66. package/std/net/quic.ts +516 -22
  67. package/std/net/router.ts +407 -38
  68. package/std/net/serve.ts +1022 -110
  69. package/std/net/tls/codec.ts +110 -32
  70. package/std/net/tls/record-server.ts +37 -7
  71. package/std/net/tls/verify.ts +19 -1
  72. package/std/net/tls-tcp.ts +56 -1
  73. package/std/net/tls.ts +196 -50
  74. package/std/net/url.ts +83 -35
  75. package/std/net/websocket.ts +129 -19
  76. package/std/net/webtransport.ts +185 -1
  77. package/std/raster.ts +837 -0
  78. package/std/regex.ts +69 -47
  79. package/std/region.ts +62 -11
  80. package/std/release.ts +593 -0
  81. package/std/secret.ts +26 -59
  82. package/std/synth.ts +143 -0
  83. package/std/text.ts +233 -0
  84. package/std/threads.ts +298 -0
  85. package/std/yaml-json.ts +368 -28
  86. package/std/yaml.ts +122 -27
@@ -17,8 +17,9 @@
17
17
  * `target("+sha2")`. A machine without them, or a process with
18
18
  * `NISH_SHA256=soft`, runs the Nish code in std/crypto/sha256.ts instead: the
19
19
  * compiler keeps that code and branches on `nish_sha256_hw` at each call. Every
20
- * other target, wasm32 included, compiles `nish_sha256_hw` to `return 0` and
21
- * the kernel to a trap nothing reaches, so a link that names it still resolves.
20
+ * other target, wasm32 included, and a toolchain without the intrinsic headers
21
+ * compile `nish_sha256_hw` to `return 0` and the kernel to a trap nothing
22
+ * reaches, so a link that names it still resolves.
22
23
  *
23
24
  * The kernel checks the guard `sha256Compress` runs its body under and returns
24
25
  * 0 without touching anything when it fails, so the caller runs the Nish
@@ -46,11 +47,17 @@
46
47
 
47
48
  #include "nish.h"
48
49
 
49
- #if defined(__x86_64__)
50
+ #if defined(__has_include)
51
+ #define NISH_SHA256_HAS(h) __has_include(h)
52
+ #else
53
+ #define NISH_SHA256_HAS(h) 0
54
+ #endif
55
+
56
+ #if defined(__x86_64__) && NISH_SHA256_HAS(<cpuid.h>) && NISH_SHA256_HAS(<immintrin.h>)
50
57
  #include <cpuid.h>
51
58
  #include <immintrin.h>
52
59
  #define NISH_SHA256_X86 1
53
- #elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
60
+ #elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && NISH_SHA256_HAS(<arm_neon.h>)
54
61
  #include <arm_neon.h>
55
62
  #define NISH_SHA256_ARM 1
56
63
  #endif
@@ -111,11 +111,24 @@ void *nish_alloc_struct(uint64_t size) {
111
111
  return nish_arena_grow(size);
112
112
  }
113
113
 
114
+ /* A module with root storage defines both itself (src/emit-root.ts), emptying
115
+ every root first through nish_runtime_root_reset; scripts/build.sh compiles
116
+ it with NISH_WASM_ROOTS, which leaves these two out and takes the root keep
117
+ below in. The wasm profile exports every function, so the root keep is
118
+ compiled into no other module. */
119
+ #ifndef NISH_WASM_ROOTS
114
120
  void nish_reset_arena(void) { nish_arena.off = 0; }
115
121
  void nish_free_arena(void) { nish_arena.off = 0; } /* memory cannot be returned */
122
+ #endif
116
123
  uint64_t nish_arena_used(void) { return nish_arena.off; }
117
124
  uint64_t nish_arena_mark(void) { return nish_arena.buf ? (uint64_t)(uintptr_t)(nish_arena.buf + nish_arena.off) : 0; }
118
125
  void nish_arena_release(uint64_t mark) { nish_arena.off = mark ? mark - (uintptr_t)nish_arena.buf : 0; }
126
+ /* The call-site reclaim's keep (wp6-memory.md §2a). The native runtime moves
127
+ `p` down onto the mark; here it always refuses, the answer runtime.c gives
128
+ whenever it is uncertain: `p` stays where it is and nothing is released, so
129
+ it reads and writes no memory and only reclaims less. String code reached
130
+ from wasm (nish/term) calls it; without it the link fails. */
131
+ void *nish_arena_keep(uint64_t mark, void *p) { (void)mark; return p; }
119
132
 
120
133
  /* ---- Arrays: %struct.nish_array = type { i64, i64, i8* } */
121
134
  typedef struct nish_array { uint64_t len; uint64_t cap; char *data; } nish_array;
@@ -147,6 +160,415 @@ void nish_array_grow(nish_array *a, uint64_t elem_size) {
147
160
  a->cap = cap;
148
161
  }
149
162
 
163
+ #ifdef NISH_WASM_ROOTS
164
+ /* ---- Root storage (docs/wp55-persistent-root.md §4): keep what the roots
165
+ * reach between export calls, release the rest.
166
+ *
167
+ * The loader of a program with root storage calls `nish_root_keep(mark)`
168
+ * (written by the compiler, src/emit-root.ts) where it would release the
169
+ * call's arena, and that calls `nish_runtime_root_keep` with every root table.
170
+ * The **kept region** is [floor, off): `floor` is the lowest mark a keep has
171
+ * seen, and nothing below it ever moves. While the region is at most
172
+ * `2 * live + 64 KiB` nothing is released. Past that, the graph is traced
173
+ * from the root slots, the blocks it reaches inside the region are slid down
174
+ * to `floor` in address order, every pointer to them is rewritten, and `off`
175
+ * is set past the last; `live` is what was kept. Everything the trace needs
176
+ * is scratch above `off`, which the next allocation reuses.
177
+ *
178
+ * A pointer outside the region (a string constant, memory below `floor`) is
179
+ * followed, so what it reaches is traced too, but it is never moved. The
180
+ * blocks are the compiler's: a string is `8 + len + 1` bytes, an object
181
+ * `sizeof` its struct, an array a 24-byte header and `cap` slots of data,
182
+ * each rounded to 8 as every allocation is. Overlapping and adjacent blocks
183
+ * merge into one extent, so a block reached twice, or through a pointer into
184
+ * its middle, moves once and keeps its shape. Each extent moves down by a
185
+ * multiple of 8, so nothing loses its alignment.
186
+ *
187
+ * The page can write every byte the trace reads (docs/security/wasm-host.md,
188
+ * WH-10), so the trace believes no pointer and no length it did not check.
189
+ * An object, an array's header and its data lie in the arena below `off`;
190
+ * a string lies there or in the static data, where the compiler puts its
191
+ * constants. An array's `len` is at most its `cap`, and `cap` slots fit
192
+ * before `off`; a string's bytes fit before the end of its area. Anything
193
+ * else panics (NISH_DIE, kind panic, so the page hears why) before the trace
194
+ * reads it, and nothing has moved: the rewrites come after the trace. The
195
+ * scratch lies above `off`, so no pointer the trace follows can reach it. */
196
+ #define NISH_TRACE_STRUCT 0
197
+ #define NISH_TRACE_STRING 1
198
+ #define NISH_TRACE_ARRAY 2
199
+ #define NISH_TRACE_UNION 3
200
+
201
+ /* `%nish.trace = type { i32, i32, i32, i32*, i8** }` (src/emit-root.ts). */
202
+ typedef struct nish_trace {
203
+ int32_t kind;
204
+ int32_t size;
205
+ int32_t count;
206
+ const int32_t *at;
207
+ const struct nish_trace *const *to;
208
+ } nish_trace;
209
+
210
+ /* `%nish.root = type { i8*, i32, i8* }`: `count` slots, each `desc` or null. */
211
+ typedef struct nish_root {
212
+ char **slots;
213
+ int32_t count;
214
+ const nish_trace *desc;
215
+ } nish_root;
216
+
217
+ /* The two layouts above are what src/emit-root.ts writes as wasm32 IR: three
218
+ * `i32`s and two 4-byte pointers, and a pointer, an `i32` and a pointer. A
219
+ * target with another pointer size would read every descriptor at the wrong
220
+ * offsets, so the build refuses it here rather than tracing garbage. */
221
+ _Static_assert(sizeof(void *) == 4, "root descriptors are emitted for wasm32's 4-byte pointers");
222
+ _Static_assert(sizeof(nish_trace) == 20, "%nish.trace layout is ABI: src/emit-root.ts");
223
+ _Static_assert(sizeof(nish_root) == 12, "%nish.root layout is ABI: src/emit-root.ts");
224
+
225
+ static const nish_root *const *nish_roots;
226
+ static int32_t nish_root_count;
227
+ static uint64_t nish_root_floor = (uint64_t)-1;
228
+ static uint64_t nish_root_live;
229
+
230
+ static void nish_roots_clear(void) {
231
+ for (int32_t r = 0; r < nish_root_count; r++) {
232
+ const nish_root *root = nish_roots[r];
233
+ for (int32_t i = 0; i < root->count; i++) root->slots[i] = 0;
234
+ }
235
+ nish_root_floor = (uint64_t)-1;
236
+ nish_root_live = 0;
237
+ }
238
+
239
+ /* Scratch: a bump allocator from the old `off` up, growing memory but never
240
+ `off`, so the call's own blocks below it are untouched. */
241
+ static uintptr_t nish_scratch_top;
242
+
243
+ static void *nish_scratch(uint64_t size) {
244
+ size = (size + 7) & ~(uint64_t)7;
245
+ uint64_t at = nish_scratch_top - (uintptr_t)nish_arena.buf;
246
+ uint64_t need = at + size;
247
+ if (need < size || need > (uintptr_t)-1 - (uintptr_t)nish_arena.buf) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
248
+ if (need > nish_arena.cap) {
249
+ uint64_t pages = (need - nish_arena.cap + NISH_PAGE - 1) / NISH_PAGE;
250
+ if (__builtin_wasm_memory_grow(0, (uintptr_t)pages) == (uintptr_t)-1) NISH_DIE(NISH_PANIC_MEMORY, -1, 0, 0, 0, 0);
251
+ nish_arena.cap += pages * NISH_PAGE;
252
+ }
253
+ void *p = (void *)nish_scratch_top;
254
+ nish_scratch_top += (uintptr_t)size;
255
+ return p;
256
+ }
257
+
258
+ /* Chunks of scratch the trace is done with: the copy a vector or the seen set
259
+ grew out of, and, once the trace ends, the work list and the seen set. A
260
+ request takes the smallest chunk it fits and leaves the rest of it free;
261
+ only what none fits bumps `nish_scratch_top`. Without this the old copies
262
+ alone added the live slot and block lists over again. */
263
+ #define NISH_SCRATCH_FREE 64
264
+ static struct { uintptr_t at; uint64_t size; } nish_scratch_free[NISH_SCRATCH_FREE];
265
+ static uint32_t nish_scratch_nfree;
266
+
267
+ static void nish_scratch_give(void *p, uint64_t size) {
268
+ size &= ~(uint64_t)7;
269
+ if (!p || !size || nish_scratch_nfree == NISH_SCRATCH_FREE) return;
270
+ nish_scratch_free[nish_scratch_nfree].at = (uintptr_t)p;
271
+ nish_scratch_free[nish_scratch_nfree].size = size;
272
+ nish_scratch_nfree++;
273
+ }
274
+
275
+ static void *nish_scratch_take(uint64_t size) {
276
+ size = (size + 7) & ~(uint64_t)7;
277
+ uint32_t best = NISH_SCRATCH_FREE;
278
+ for (uint32_t i = 0; i < nish_scratch_nfree; i++)
279
+ if (nish_scratch_free[i].size >= size && (best == NISH_SCRATCH_FREE || nish_scratch_free[i].size < nish_scratch_free[best].size))
280
+ best = i;
281
+ if (best == NISH_SCRATCH_FREE) return nish_scratch(size);
282
+ void *p = (void *)nish_scratch_free[best].at;
283
+ nish_scratch_free[best].at += (uintptr_t)size;
284
+ nish_scratch_free[best].size -= size;
285
+ if (!nish_scratch_free[best].size) nish_scratch_free[best] = nish_scratch_free[--nish_scratch_nfree];
286
+ return p;
287
+ }
288
+
289
+ /* A growable vector of 32-bit words in scratch; a grown vector gives its old
290
+ copy back to the free chunks. */
291
+ typedef struct { uint32_t *at; uint32_t len; uint32_t cap; } nish_words;
292
+
293
+ static void nish_words_push(nish_words *v, uint32_t a, uint32_t b) {
294
+ if (v->len + 2 > v->cap) {
295
+ uint32_t cap = v->cap ? v->cap * 2 : 256;
296
+ uint32_t *at = (uint32_t *)nish_scratch_take((uint64_t)cap * 4);
297
+ if (v->len) __builtin_memcpy(at, v->at, (uintptr_t)v->len * 4);
298
+ nish_scratch_give(v->at, (uint64_t)v->cap * 4);
299
+ v->at = at;
300
+ v->cap = cap;
301
+ }
302
+ v->at[v->len] = a;
303
+ v->at[v->len + 1] = b;
304
+ v->len += 2;
305
+ }
306
+
307
+ /* The trace's state: the region, what it reached and what is left to visit. */
308
+ static struct {
309
+ uintptr_t lo, hi;
310
+ nish_words blocks; /* start, end: a block in the region */
311
+ nish_words slots; /* address, 0: a slot holding a pointer into the region */
312
+ nish_words work; /* object, descriptor: left to visit */
313
+ uint32_t *seen; /* open addressing on (object, descriptor) pairs */
314
+ uint32_t seen_cap, seen_len;
315
+ } nish_tr;
316
+
317
+ static uint32_t nish_seen_hash(uint32_t p, uint32_t d) {
318
+ uint32_t h = (p ^ (d * 0x9e3779b1u)) * 0x85ebca6bu;
319
+ return h ^ (h >> 15);
320
+ }
321
+
322
+ /* Whether (p, d) was new, adding it; the set doubles at half full. */
323
+ static int nish_seen_add(uint32_t p, uint32_t d) {
324
+ if (nish_tr.seen_len * 2 >= nish_tr.seen_cap) {
325
+ uint32_t *old = nish_tr.seen;
326
+ uint32_t old_cap = nish_tr.seen_cap;
327
+ nish_tr.seen_cap = old_cap ? old_cap * 2 : 1024;
328
+ nish_tr.seen = (uint32_t *)nish_scratch_take((uint64_t)nish_tr.seen_cap * 8);
329
+ __builtin_memset(nish_tr.seen, 0, (uintptr_t)nish_tr.seen_cap * 8);
330
+ nish_tr.seen_len = 0;
331
+ for (uint32_t i = 0; i < old_cap; i++)
332
+ if (old[2 * i]) nish_seen_add(old[2 * i], old[2 * i + 1]);
333
+ nish_scratch_give(old, (uint64_t)old_cap * 8);
334
+ }
335
+ uint32_t mask = nish_tr.seen_cap - 1;
336
+ for (uint32_t i = nish_seen_hash(p, d) & mask;; i = (i + 1) & mask) {
337
+ if (!nish_tr.seen[2 * i]) {
338
+ nish_tr.seen[2 * i] = p;
339
+ nish_tr.seen[2 * i + 1] = d;
340
+ nish_tr.seen_len++;
341
+ return 1;
342
+ }
343
+ if (nish_tr.seen[2 * i] == p && nish_tr.seen[2 * i + 1] == d) return 0;
344
+ }
345
+ }
346
+
347
+ static int nish_in_region(uintptr_t p) { return p >= nish_tr.lo && p < nish_tr.hi; }
348
+
349
+ /* The static data wasm-ld lays out: string constants live here. */
350
+ extern unsigned char __global_base;
351
+ extern unsigned char __data_end;
352
+
353
+ #define NISH_FORGED_POINTER "root storage: a pointer in the kept graph names memory outside the arena"
354
+ #define NISH_FORGED_LENGTH "root storage: a length in the kept graph runs past its block"
355
+ #define NISH_FORGED(msg) NISH_DIE(NISH_PANIC_MESSAGE, -1, (msg), (uint32_t)(sizeof(msg) - 1), 0, 0)
356
+
357
+ /* The end of the area `p` may lie in and `size` bytes from it fit before:
358
+ the arena [buf, off), or for a string the static data too; 0 for neither.
359
+ The checks subtract, so no forged size wraps a sum. */
360
+ static uintptr_t nish_area_end(uintptr_t p, uint64_t size, int string) {
361
+ uintptr_t end = 0;
362
+ if (p >= (uintptr_t)nish_arena.buf && p <= nish_tr.hi) end = nish_tr.hi;
363
+ else if (string && p >= (uintptr_t)&__global_base && p <= (uintptr_t)&__data_end) end = (uintptr_t)&__data_end;
364
+ return end && size <= (uint64_t)(end - p) ? end : 0;
365
+ }
366
+
367
+ static void nish_block(uintptr_t p, uint64_t size) {
368
+ if (!size || !nish_in_region(p)) return;
369
+ uint64_t end = (uint64_t)p + ((size + 7) & ~(uint64_t)7);
370
+ if (end > nish_tr.hi) end = nish_tr.hi; /* a block is never past `off` */
371
+ nish_words_push(&nish_tr.blocks, (uint32_t)p, (uint32_t)end);
372
+ }
373
+
374
+ /* The pointer slot `s`, which holds an object of descriptor `d` or null. */
375
+ static void nish_slot(char **s, const nish_trace *d) {
376
+ uintptr_t p = (uintptr_t)*s;
377
+ if (!p) return;
378
+ /* The fixed part of the block: a string's length word, an array's header,
379
+ a union's tag (its arm is checked once the tag is read). */
380
+ uint64_t head = d->kind == NISH_TRACE_STRING ? 8 : d->kind == NISH_TRACE_ARRAY ? sizeof(nish_array)
381
+ : d->kind == NISH_TRACE_UNION ? 4 : (uint64_t)d->size;
382
+ if (!nish_area_end(p, head, d->kind == NISH_TRACE_STRING)) NISH_FORGED(NISH_FORGED_POINTER);
383
+ if (nish_in_region(p)) nish_words_push(&nish_tr.slots, (uint32_t)(uintptr_t)s, 0);
384
+ if (nish_seen_add((uint32_t)p, (uint32_t)(uintptr_t)d)) nish_words_push(&nish_tr.work, (uint32_t)p, (uint32_t)(uintptr_t)d);
385
+ }
386
+
387
+ static void nish_fields(char *p, const nish_trace *d) {
388
+ for (int32_t i = 0; i < d->count; i++) nish_slot((char **)(p + d->at[i]), d->to[i]);
389
+ }
390
+
391
+ static void nish_visit(char *p, const nish_trace *d) {
392
+ switch (d->kind) {
393
+ case NISH_TRACE_STRING: {
394
+ uint64_t len;
395
+ __builtin_memcpy(&len, p, 8);
396
+ uintptr_t end = nish_area_end((uintptr_t)p, 0, 1);
397
+ if (len > (uint64_t)(end - (uintptr_t)p) - 8) NISH_FORGED(NISH_FORGED_LENGTH);
398
+ nish_block((uintptr_t)p, 8 + len + 1);
399
+ break;
400
+ }
401
+ case NISH_TRACE_STRUCT:
402
+ nish_block((uintptr_t)p, (uint64_t)d->size);
403
+ nish_fields(p, d);
404
+ break;
405
+ case NISH_TRACE_UNION: {
406
+ int32_t tag;
407
+ __builtin_memcpy(&tag, p, 4);
408
+ for (int32_t i = 0; i < d->count; i++) {
409
+ if (d->at[i] == tag) {
410
+ if (!nish_area_end((uintptr_t)p, (uint64_t)d->to[i]->size, 0)) NISH_FORGED(NISH_FORGED_LENGTH);
411
+ nish_block((uintptr_t)p, (uint64_t)d->to[i]->size);
412
+ nish_fields(p, d->to[i]);
413
+ break;
414
+ }
415
+ }
416
+ break;
417
+ }
418
+ case NISH_TRACE_ARRAY: {
419
+ nish_array *a = (nish_array *)p;
420
+ nish_block((uintptr_t)p, sizeof *a);
421
+ /* `len <= cap` and `cap * size <= off - data`, by division, so a forged
422
+ `cap` cannot wrap the product and a forged `len` walks no slot the
423
+ data does not hold. */
424
+ if (a->len > a->cap) NISH_FORGED(NISH_FORGED_LENGTH);
425
+ if (!a->data) {
426
+ if (a->cap) NISH_FORGED(NISH_FORGED_POINTER);
427
+ break;
428
+ }
429
+ if (!nish_area_end((uintptr_t)a->data, 0, 0)) NISH_FORGED(NISH_FORGED_POINTER);
430
+ if (d->size > 0 && a->cap > (uint64_t)(nish_tr.hi - (uintptr_t)a->data) / (uint64_t)d->size) NISH_FORGED(NISH_FORGED_LENGTH);
431
+ if (nish_in_region((uintptr_t)a->data)) nish_words_push(&nish_tr.slots, (uint32_t)(uintptr_t)&a->data, 0);
432
+ nish_block((uintptr_t)a->data, a->cap * (uint64_t)d->size);
433
+ if (d->count == 1) {
434
+ for (uint64_t i = 0; i < a->len; i++) nish_slot((char **)(a->data + i * (uint64_t)d->size), d->to[0]);
435
+ } else if (d->count == 2) {
436
+ for (uint64_t i = 0; i < a->len; i++) nish_fields(a->data + i * (uint64_t)d->size, d->to[0]);
437
+ }
438
+ break;
439
+ }
440
+ }
441
+ }
442
+
443
+ /* Heapsort of `n` pairs by their first word: no recursion, no scratch. */
444
+ static void nish_sift(uint32_t *v, uint32_t at, uint32_t n) {
445
+ for (;;) {
446
+ uint32_t big = at, l = 2 * at + 1, r = l + 1;
447
+ if (l < n && v[2 * l] > v[2 * big]) big = l;
448
+ if (r < n && v[2 * r] > v[2 * big]) big = r;
449
+ if (big == at) return;
450
+ uint32_t a = v[2 * at], b = v[2 * at + 1];
451
+ v[2 * at] = v[2 * big];
452
+ v[2 * at + 1] = v[2 * big + 1];
453
+ v[2 * big] = a;
454
+ v[2 * big + 1] = b;
455
+ at = big;
456
+ }
457
+ }
458
+
459
+ static void nish_sort_pairs(uint32_t *v, uint32_t n) {
460
+ for (uint32_t i = n / 2; i-- > 0;) nish_sift(v, i, n);
461
+ while (n > 1) {
462
+ n--;
463
+ uint32_t a = v[0], b = v[1];
464
+ v[0] = v[2 * n];
465
+ v[1] = v[2 * n + 1];
466
+ v[2 * n] = a;
467
+ v[2 * n + 1] = b;
468
+ nish_sift(v, 0, n);
469
+ }
470
+ }
471
+
472
+ /* Where the region address `p` lands: inside an extent, at its new place;
473
+ in a gap, at the end of the extent before it, which is where the gap's
474
+ bytes went (a pointer past a block's end, as an empty array's data can be). */
475
+ static uint32_t nish_moved(const uint32_t *ext, uint32_t n, uint32_t p) {
476
+ uint32_t lo = 0, hi = n; /* the first extent starting after p */
477
+ while (lo < hi) {
478
+ uint32_t mid = (lo + hi) / 2;
479
+ if (ext[3 * mid] <= p) lo = mid + 1;
480
+ else hi = mid;
481
+ }
482
+ if (lo == 0) return (uint32_t)nish_tr.lo;
483
+ const uint32_t *e = ext + 3 * (lo - 1);
484
+ return p < e[1] ? e[2] + (p - e[0]) : e[2] + (e[1] - e[0]);
485
+ }
486
+
487
+ static void nish_root_compact(void) {
488
+ nish_tr.lo = (uintptr_t)nish_arena.buf + (uintptr_t)nish_root_floor;
489
+ nish_tr.hi = (uintptr_t)nish_arena.buf + (uintptr_t)nish_arena.off;
490
+ nish_scratch_top = (nish_tr.hi + 7) & ~(uintptr_t)7;
491
+ nish_tr.blocks.len = nish_tr.blocks.cap = 0;
492
+ nish_tr.slots.len = nish_tr.slots.cap = 0;
493
+ nish_tr.work.len = nish_tr.work.cap = 0;
494
+ nish_tr.seen = 0;
495
+ nish_tr.seen_cap = nish_tr.seen_len = 0;
496
+ nish_scratch_nfree = 0;
497
+ for (int32_t r = 0; r < nish_root_count; r++) {
498
+ const nish_root *root = nish_roots[r];
499
+ for (int32_t i = 0; i < root->count; i++) nish_slot(&root->slots[i], root->desc);
500
+ }
501
+ while (nish_tr.work.len) {
502
+ nish_tr.work.len -= 2;
503
+ nish_visit((char *)(uintptr_t)nish_tr.work.at[nish_tr.work.len],
504
+ (const nish_trace *)(uintptr_t)nish_tr.work.at[nish_tr.work.len + 1]);
505
+ }
506
+ /* Blocks into extents (start, end, new start). An extent takes three words
507
+ where a block took two, so the extents go in a chunk of their own, which
508
+ may be the work list or the seen set the trace no longer needs. */
509
+ uint32_t nb = nish_tr.blocks.len / 2;
510
+ nish_sort_pairs(nish_tr.blocks.at, nb);
511
+ nish_scratch_give(nish_tr.work.at, (uint64_t)nish_tr.work.cap * 4);
512
+ nish_scratch_give(nish_tr.seen, (uint64_t)nish_tr.seen_cap * 8);
513
+ uint32_t *ext = (uint32_t *)nish_scratch_take((uint64_t)nb * 12 + 12);
514
+ uint32_t ne = 0, to = (uint32_t)nish_tr.lo;
515
+ for (uint32_t i = 0; i < nb; i++) {
516
+ uint32_t start = nish_tr.blocks.at[2 * i], end = nish_tr.blocks.at[2 * i + 1];
517
+ if (ne && start <= ext[3 * (ne - 1) + 1]) {
518
+ uint32_t *e = ext + 3 * (ne - 1);
519
+ if (end > e[1]) {
520
+ to += end - e[1];
521
+ e[1] = end;
522
+ }
523
+ continue;
524
+ }
525
+ ext[3 * ne] = start;
526
+ ext[3 * ne + 1] = end;
527
+ ext[3 * ne + 2] = to;
528
+ to += end - start;
529
+ ne++;
530
+ }
531
+ /* Every slot once: rewriting one twice would move its pointer twice. */
532
+ uint32_t ns = nish_tr.slots.len / 2;
533
+ nish_sort_pairs(nish_tr.slots.at, ns);
534
+ for (uint32_t i = 0; i < ns; i++) {
535
+ uint32_t s = nish_tr.slots.at[2 * i];
536
+ if (i && s == nish_tr.slots.at[2 * (i - 1)]) continue;
537
+ char **slot = (char **)(uintptr_t)s;
538
+ *slot = (char *)(uintptr_t)nish_moved(ext, ne, (uint32_t)(uintptr_t)*slot);
539
+ }
540
+ /* Down, in address order: each extent's new place is at or below its old
541
+ one and above every earlier extent's, so a forward move is safe. */
542
+ for (uint32_t i = 0; i < ne; i++) {
543
+ uint32_t *e = ext + 3 * i;
544
+ if (e[2] != e[0]) __builtin_memmove((char *)(uintptr_t)e[2], (char *)(uintptr_t)e[0], (uintptr_t)(e[1] - e[0]));
545
+ }
546
+ nish_root_live = (uint64_t)(to - (uint32_t)nish_tr.lo);
547
+ nish_arena.off = (uint64_t)((uintptr_t)to - (uintptr_t)nish_arena.buf);
548
+ }
549
+
550
+ /* Root storage's nish_reset_arena and nish_free_arena (WP55 §4.2): a root
551
+ left pointing at a released block would be traced into whatever lands
552
+ there, so every root is emptied before the arena. */
553
+ void nish_runtime_root_reset(const nish_root *const *roots, int32_t count) {
554
+ nish_roots = roots;
555
+ nish_root_count = count;
556
+ nish_roots_clear();
557
+ nish_arena.off = 0;
558
+ }
559
+
560
+ /* After an export call that began at `mark`: keep what `roots` reach. */
561
+ void nish_runtime_root_keep(uint64_t mark, const nish_root *const *roots, int32_t count) {
562
+ nish_roots = roots;
563
+ nish_root_count = count;
564
+ if (!nish_arena.buf) return;
565
+ uint64_t at = mark ? mark - (uintptr_t)nish_arena.buf : 0;
566
+ if (at < nish_root_floor) nish_root_floor = at;
567
+ if (nish_arena.off - nish_root_floor <= 2 * nish_root_live + NISH_PAGE) return;
568
+ nish_root_compact();
569
+ }
570
+ #endif /* NISH_WASM_ROOTS */
571
+
150
572
  /* ---- Panics: `unreachable`, which the host sees as a RuntimeError; under
151
573
  * wasm-host the page hears of each first (NISH_DIE above). */
152
574
  #ifdef NISH_WASM_HOST
@@ -182,6 +604,21 @@ void nish_exit(int32_t code) { (void)code; __builtin_trap(); }
182
604
  * link; the loader throws a RangeError before the call reaches it. */
183
605
  void nish_panic(const void *s) { (void)s; __builtin_trap(); }
184
606
  #endif
607
+ /* ---- Streaming reads (WP44 §9 Q7): the checker refuses `fileOpen`,
608
+ * `fileReadAt`, `fileSize` and `fileClose` on a freestanding wasm32 target
609
+ * (NL2718), but `--profile wasm` links IR compiled for the host, which no
610
+ * check sees. There is no file system to read, so each answers -38 (`ENOSYS`)
611
+ * and the link never fails on their names. The path is `const void *`, as
612
+ * `nish_panic`'s string is: this file has no `nish_str`, and the ABI is the
613
+ * same pointer. tests/run.js's wasm profile section links all four. */
614
+ int32_t nish_file_open(const void *path) { (void)path; return -38; }
615
+ int32_t nish_file_read_at(int32_t fd, nish_array *buf, int64_t off, int64_t len, int64_t pos) {
616
+ (void)fd; (void)buf; (void)off; (void)len; (void)pos;
617
+ return -38;
618
+ }
619
+ int64_t nish_file_size(int32_t fd) { (void)fd; return -38; }
620
+ int32_t nish_file_close(int32_t fd) { (void)fd; return -38; }
621
+
185
622
  /* Output has nowhere to go either. */
186
623
  void nish_write(const void *s, int32_t fd, _Bool newline) { (void)s; (void)fd; (void)newline; }
187
624
 
package/runtime/runtime.c CHANGED
@@ -368,6 +368,11 @@ uint64_t nish_arena_mark(void) {
368
368
  }
369
369
  uint64_t nish_arena_used(void) { return nish_arena.off; }
370
370
 
371
+ /* ---- cErrno (WP27 S5, docs/wp27-ffi.md §10): compiled code zeroes errno through this
372
+ * pointer before a foreign call and copies it into the per-thread slot after. */
373
+ NISH_TLS int32_t nish_errno_slot;
374
+ int *nish_errno_ptr(void) { return &errno; }
375
+
371
376
  void nish_arena_release(uint64_t mark) {
372
377
  nish_chunk *c = nish_arena.chunks;
373
378
  if (!mark) { nish_reset_arena(); return; }
package/runtime/vite.mjs CHANGED
@@ -118,7 +118,7 @@ export default function nish(options = {}) {
118
118
  );
119
119
  throw new Error(
120
120
  `nish/vite: the compiler ${compiler} (${version}) wrote a loader with no \`loadSync\` export: ` +
121
- "it predates `loadSync` (WP50 stage 1), which the generated module calls. " +
121
+ "it predates `loadSync` (nish 0.36.0), which the generated module calls. " +
122
122
  "Use a newer one: update the nish package, or pass its path as `nish({ compiler })`.",
123
123
  );
124
124
  }
package/scripts/build.sh CHANGED
@@ -16,7 +16,8 @@
16
16
  # <dir>/runtime-aes.c, AES-GCM on the CPU's AES instructions,
17
17
  # <dir>/runtime-sha256.c, SHA-256 on the CPU's SHA instructions,
18
18
  # <dir>/runtime-chacha20.c, ChaCha20's keystream on AVX2 or NEON vectors,
19
- # <dir>/runtime-checksum.c, CRC-32 on its carry-less multiply or CRC instructions,
19
+ # <dir>/runtime-checksum.c, CRC-32 on its carry-less multiply or CRC instructions
20
+ # and Adler-32 on SSSE3 or NEON,
20
21
  # <dir>/runtime-term.c, stdin and the terminal of `nish:term`,
21
22
  # <dir>/runtime-bytes.c, the byte-class runs of `nish/bytes`,
22
23
  # <dir>/runtime-sandbox.c, the confinement `--sandbox` applies,
@@ -445,10 +446,11 @@ if [ "$debug" = 1 ]; then common+=(-g); strip_flag=(); fi
445
446
  # own command line instead of using `common`; it is empty on every ordinary
446
447
  # build, hence the bash 3.2 expansion spelling explained below.
447
448
  #
448
- # -pthread is for runtime-parallel.c, the translation unit that divides a range
449
+ # -pthread is here for runtime-parallel.c, the translation unit that divides a range
449
450
  # of work across threads: it is compiled in either configuration and only spawns
450
- # under this macro, so this is the build where the flag has to be on the command
451
- # line. On a current glibc the library half is already inside libc and the link
451
+ # under this macro, so this is the build where it needs the flag on the command
452
+ # line. (Every Linux build has the flag already, for runtime-sandbox.c; see
453
+ # where `common` gets its hardening flags below.) On a current glibc the library half is already inside libc and the link
452
454
  # would succeed without it; passing it is what makes that an implementation
453
455
  # detail rather than something the build depends on.
454
456
  #
@@ -491,6 +493,22 @@ case "$(uname -s)" in
491
493
  Darwin) ;;
492
494
  *)
493
495
  common+=(-fstack-clash-protection)
496
+ # runtime-sandbox.c runs each spawn under an exec allowlist on threads of
497
+ # its own, and a glibc before 2.34 keeps pthread_create outside libc. The
498
+ # flag is on every build rather than the sandboxed ones because this
499
+ # script cannot tell them apart: an input may be an object file, where
500
+ # the call of `nish_sandbox` is not there to be read. A build without
501
+ # `--sandbox` is the binary it was: on glibc 2.34 and later the flag adds
502
+ # nothing to the link (measured on 2.39: `arena_placement` and
503
+ # `caps_process_spawn` at debug, speed and size are byte for byte the
504
+ # same with and without it). Before 2.34 it names libpthread to the
505
+ # linker, and a build without `--sandbox` can then carry a DT_NEEDED on
506
+ # libpthread.so.0 that it did not have: the debug profile always (its link
507
+ # line has no --as-needed), speed and size wherever the linker still sees
508
+ # the runtime's reference to pthread_create. That is one more library of
509
+ # the same glibc loaded at start, and not measured: no machine the tests
510
+ # run on has such a glibc.
511
+ common+=(-pthread)
494
512
  # shellcheck disable=SC2054 # -Wl,<flag> is one argument: the comma is the linker's
495
513
  common+=(-Wl,-z,relro,-z,now -Wl,-z,noexecstack) ;;
496
514
  esac
@@ -780,15 +798,16 @@ case "$profile" in
780
798
  # IR compiled for the host rather than with `--target wasm32` calls std/crypto/aes.ts's
781
799
  # AES kernels behind `nish_aes_hw`, std/crypto/sha256.ts's block function behind
782
800
  # `nish_sha256_hw`, std/crypto/chacha20poly1305.ts's keystream loop behind
783
- # `nish_chacha20_hw` and std/compress/checksum.ts's CRC-32 kernel behind
784
- # `nish_crc32_hw` (src/kernels.ts). runtime-aes.c's, runtime-sha256.c's,
801
+ # `nish_chacha20_hw` and std/compress/checksum.ts's CRC-32 and Adler-32
802
+ # kernels behind `nish_crc32_hw` and `nish_adler32_hw` (src/kernels.ts). runtime-aes.c's, runtime-sha256.c's,
785
803
  # runtime-chacha20.c's and runtime-checksum.c's wasm32 builds answer 0 there,
786
804
  # so the Nish code runs, and
787
805
  # each is added only for such IR: every other module links the bytes it always did.
788
- # Each pair is a unit and the kernel its probe names.
806
+ # Each pair is a unit and the kernel its probe names; checksum has two, and
807
+ # its second pair sees the unit the first added, so it is added once.
789
808
  # A caller that names the unit itself is left alone, as `halves` leaves it:
790
809
  # one object named twice is a duplicate-symbol error.
791
- for pair in aes:aes sha256:sha256 chacha20:chacha20 checksum:crc32; do
810
+ for pair in aes:aes sha256:sha256 chacha20:chacha20 checksum:crc32 checksum:adler32; do
792
811
  unit=${pair%%:*}
793
812
  kernel=${pair#*:}
794
813
  named=0
@@ -870,12 +889,27 @@ case "$profile" in
870
889
  esac
871
890
  done
872
891
  fi
892
+ # Root storage (WP55): a module with root storage defines its own
893
+ # `nish_reset_arena`, which empties every root through the runtime's
894
+ # nish_runtime_root_reset. Its IR links runtime-wasm.c, compiled with
895
+ # NISH_WASM_ROOTS, which takes in the root keep and leaves the arena resets
896
+ # to the IR. The definition is the trigger: a bare `declare` of the keep
897
+ # (every `--runtime-decls` module has one) or a string spelling a name is
898
+ # not. Without it the module links the bytes it always did: the wasm
899
+ # profile exports every function it compiles.
900
+ roots_wanted=0
901
+ for i in ${inputs[@]+"${inputs[@]}"}; do
902
+ case "$i" in
903
+ *.ll) if grep -qE '^define void @nish_reset_arena\(' "$i"; then roots_wanted=1; round_wanted=1; fi ;;
904
+ esac
905
+ done
873
906
  if [ "$round_wanted" = 1 ] && [ "$wasm_named" = 0 ]; then
874
907
  inputs+=("$(cd "$(dirname "$0")/../runtime" && pwd)/runtime-wasm.c")
875
908
  fi
876
909
  # -mbulk-memory lowers llvm.memset/memcpy (`new Array<T>(n)`, `push` growth) to the
877
910
  # memory.fill/memory.copy instructions instead of libc calls the freestanding link lacks.
878
911
  wasm_compile=(-Wno-override-module --target=wasm32-unknown-unknown -Oz -nostdlib -mbulk-memory -ffp-contract=off)
912
+ if [ "$roots_wanted" = 1 ]; then wasm_compile+=(-DNISH_WASM_ROOTS); fi
879
913
  if [ "$profile" = wasm ]; then
880
914
  "$CC" "${wasm_compile[@]}" \
881
915
  ${tls[@]+"${tls[@]}"} \