@amritk/nish-aarch64-linux 0.37.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.
Files changed (73) hide show
  1. package/INSTALL.md +28 -16
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +380 -47
  4. package/package.json +1 -1
  5. package/runtime/core/arrays.d.ts +4 -0
  6. package/runtime/core/arrays.mjs +33 -0
  7. package/runtime/core/host.d.ts +11 -0
  8. package/runtime/core/host.mjs +35 -0
  9. package/runtime/nish.d.ts +645 -66
  10. package/runtime/nish.h +101 -15
  11. package/runtime/nish.mjs +69 -6
  12. package/runtime/node.mjs +117 -0
  13. package/runtime/runtime-aes.c +12 -4
  14. package/runtime/runtime-async.c +267 -51
  15. package/runtime/runtime-checksum.c +207 -15
  16. package/runtime/runtime-cross.c +2 -1
  17. package/runtime/runtime-net.c +32 -3
  18. package/runtime/runtime-os.c +174 -27
  19. package/runtime/runtime-region.c +170 -8
  20. package/runtime/runtime-sandbox.c +377 -17
  21. package/runtime/runtime-sha256.c +11 -4
  22. package/runtime/runtime-text.c +1068 -0
  23. package/runtime/runtime-wasm.c +18 -3
  24. package/runtime/runtime.c +55 -961
  25. package/runtime/vite.mjs +48 -11
  26. package/scripts/build.sh +113 -36
  27. package/std/LICENSE-fdlibm +8 -0
  28. package/std/README.md +7 -4
  29. package/std/array.ts +266 -0
  30. package/std/compress/checksum.ts +13 -0
  31. package/std/compress/deflate.ts +0 -2
  32. package/std/crypto/p256.ts +247 -74
  33. package/std/crypto/p384.ts +3 -1
  34. package/std/crypto/pki.ts +35 -9
  35. package/std/crypto/x25519.ts +153 -64
  36. package/std/crypto/x509.ts +113 -10
  37. package/std/host/text.ts +86 -0
  38. package/std/json-tokenizer.ts +20 -0
  39. package/std/json-typed.ts +217 -8
  40. package/std/math.ts +743 -41
  41. package/std/net/dns-resolver.ts +46 -6
  42. package/std/net/dns.ts +50 -13
  43. package/std/net/fetch.ts +691 -129
  44. package/std/net/handler.ts +142 -16
  45. package/std/net/http-compress.ts +17 -9
  46. package/std/net/http-fields.ts +19 -4
  47. package/std/net/http1-codec.ts +517 -154
  48. package/std/net/http1-server.ts +508 -25
  49. package/std/net/http2-cleartext.ts +526 -0
  50. package/std/net/http2-tls.ts +48 -0
  51. package/std/net/http2.ts +84 -0
  52. package/std/net/postgres-async.ts +2223 -0
  53. package/std/net/postgres-carrier.ts +343 -10
  54. package/std/net/postgres.ts +129 -11
  55. package/std/net/prefork.ts +195 -11
  56. package/std/net/quic-datagram.ts +30 -0
  57. package/std/net/quic-listener.ts +38 -12
  58. package/std/net/quic-packet.ts +19 -6
  59. package/std/net/quic-recovery.ts +134 -19
  60. package/std/net/quic-stream.ts +174 -36
  61. package/std/net/quic.ts +418 -18
  62. package/std/net/router.ts +39 -17
  63. package/std/net/serve.ts +867 -0
  64. package/std/net/tls/codec.ts +110 -32
  65. package/std/net/tls/record-server.ts +37 -7
  66. package/std/net/tls-tcp.ts +56 -1
  67. package/std/net/tls.ts +196 -50
  68. package/std/net/websocket.ts +55 -18
  69. package/std/net/webtransport.ts +179 -1
  70. package/std/regex.ts +69 -47
  71. package/std/region.ts +62 -11
  72. package/std/secret.ts +26 -59
  73. package/std/text.ts +464 -5
@@ -55,8 +55,30 @@
55
55
  *
56
56
  * Freeing a region frees its chunks through runtime.c's `nish_free_arena`,
57
57
  * with the region's arena swapped in for the call, so an `-DNISH_ASAN` build
58
- * forgets the freed blocks as it does for the thread's arena. */
58
+ * forgets the freed blocks as it does for the thread's arena.
59
+ *
60
+ * **Adopting** a region gives its chunks to the current arena instead of
61
+ * freeing them, then retires the handle as `free` does: `awaitAll` runs each
62
+ * call that waits in a region of its own, and adopts it when the call
63
+ * finishes, so the answer stays readable and is freed with the caller's
64
+ * memory (docs/security/async.md, ASYNC-14). A child region, or a region with
65
+ * children, is never a lane's, and adopting one panics: its chunks would
66
+ * outlive the tree `free` walks (ASYNC-19).
67
+ *
68
+ * **Children.** `nish_region_child_new` makes a region whose parent is the
69
+ * region entered last on this thread (`child` and `childWith`), and panics
70
+ * when none is. A slot links its parent and its children through slot indices
71
+ * (index + 1, 0 for none): a parent, its first child, and the previous and
72
+ * next sibling, four 32-bit links, so a slot is 96 bytes (80 before
73
+ * children). Making a child, and unlinking one as `free` does, are O(1);
74
+ * `free` of a parent first walks its whole subtree, once, to panic before
75
+ * freeing anything if any region in it is entered, then frees it deepest
76
+ * first -- each region's chunks and then its handle, so every copy of a
77
+ * child's handle is stale afterwards, as the parent's is -- O(1) a region. A
78
+ * child is always on its parent's thread, and the links are only read and
79
+ * written by that thread, so they take no lock. */
59
80
 
81
+ #include <stddef.h>
60
82
  #include <stdint.h>
61
83
  #include <stdlib.h>
62
84
 
@@ -73,6 +95,20 @@ extern NISH_TLS uint64_t nish_region_chunk;
73
95
  #define NISH_REGION_GEN_END ((uint64_t)1 << 40)
74
96
  #define NISH_REGION_PAGE 1024
75
97
 
98
+ /* runtime.c's `nish_chunk`, which `nish_region_adopt` links through and
99
+ carves: the same three facts runtime.c asserts of it. Bit 0 of `cap` marks a
100
+ carved chunk, as runtime.c's NISH_CHUNK_CARVED. */
101
+ typedef struct nish_chunk_link { struct nish_chunk_link *next; uint64_t cap; } nish_chunk_link;
102
+ _Static_assert(sizeof(nish_chunk_link) == 16, "runtime.c's nish_chunk: { next, cap }, the bytes after it");
103
+ _Static_assert(offsetof(nish_chunk_link, next) == 0, "runtime.c's nish_chunk: next first");
104
+ _Static_assert(offsetof(nish_chunk_link, cap) == 8, "runtime.c's nish_chunk: cap second");
105
+ #define NISH_CHUNK_CARVED 1
106
+
107
+ /* The least a caller's chunk must have left for `nish_region_adopt` to carve
108
+ it into a chunk of its own: below it the bytes are given up, so an adopt
109
+ leaves at most this much behind. */
110
+ #define NISH_ADOPT_CARVE 256
111
+
76
112
  typedef struct nish_region_slot {
77
113
  struct nish_arena arena; /* the region's arena, or, while entered, the one it displaced */
78
114
  uint64_t gen; /* the live handle's generation; NISH_REGION_GEN_END once retired */
@@ -81,7 +117,12 @@ typedef struct nish_region_slot {
81
117
  uint64_t outer; /* while entered: the handle entered before it; while free: the next free slot + 1 */
82
118
  void *root; /* what `leave` recorded, answered by `enter` */
83
119
  uint64_t entered;
120
+ uint32_t parent; /* the parent's index + 1, or 0: a region `nish_region_new` made */
121
+ uint32_t first_child; /* the newest child's index + 1, or 0 */
122
+ uint32_t prev_sibling; /* the parent's next newer child's index + 1, or 0 */
123
+ uint32_t next_sibling; /* the parent's next older child's index + 1, or 0 */
84
124
  } nish_region_slot;
125
+ _Static_assert(sizeof(nish_region_slot) == 96, "the slot size the header states");
85
126
 
86
127
  static nish_region_slot *nish_region_pages[NISH_REGION_SLOTS / NISH_REGION_PAGE];
87
128
  static uint64_t nish_region_used; /* slots ever handed out: the next never-used index */
@@ -122,6 +163,8 @@ enum {
122
163
  NISH_REGION_LEAVE,
123
164
  NISH_REGION_FULL,
124
165
  NISH_REGION_OOM,
166
+ NISH_REGION_NO_PARENT,
167
+ NISH_REGION_ADOPT_TREE,
125
168
  };
126
169
  #define NISH_REGION_MSG(text) { sizeof text - 1, text }
127
170
  static const struct { uint64_t len; char data[72]; } nish_region_messages[] = {
@@ -133,6 +176,8 @@ static const struct { uint64_t len; char data[72]; } nish_region_messages[] = {
133
176
  NISH_REGION_MSG("nish: leave of a region that is not the one entered last"),
134
177
  NISH_REGION_MSG("nish: more than 16777216 live regions"),
135
178
  NISH_REGION_MSG("nish: out of memory"),
179
+ NISH_REGION_MSG("nish: a child region made with no region entered"),
180
+ NISH_REGION_MSG("nish: adopt of a region with a parent or children"),
136
181
  };
137
182
  static NISH_COLD void nish_region_die(int why) {
138
183
  nish_panic((const nish_str *)&nish_region_messages[why]);
@@ -152,6 +197,12 @@ static nish_region_slot *nish_region_slot_of(uint64_t r) {
152
197
  return s;
153
198
  }
154
199
 
200
+ /* The slot whose index + 1 is `link`, which is not 0. */
201
+ static nish_region_slot *nish_region_at(uint32_t link) {
202
+ uint64_t index = (uint64_t)link - 1;
203
+ return &nish_region_pages[index / NISH_REGION_PAGE][index % NISH_REGION_PAGE];
204
+ }
205
+
155
206
  /* Swap the slot's arena and chunk size with the thread's: `enter` and `leave`
156
207
  both, and `free` around its `nish_free_arena`. */
157
208
  static void nish_region_swap(nish_region_slot *s) {
@@ -198,12 +249,30 @@ uint64_t nish_region_new(void) {
198
249
  }
199
250
  s->chunk = 8;
200
251
  s->outer = 0;
252
+ s->parent = 0;
253
+ s->first_child = 0;
254
+ s->prev_sibling = 0;
255
+ s->next_sibling = 0;
201
256
  __atomic_store_n(&s->thread, me, __ATOMIC_RELAXED);
202
257
  uint64_t r = s->gen << NISH_REGION_SLOT_BITS | index;
203
258
  nish_region_unlock();
204
259
  return r;
205
260
  }
206
261
 
262
+ uint64_t nish_region_child_new(void) {
263
+ uint64_t p = nish_region_current;
264
+ if (!p) nish_region_die(NISH_REGION_NO_PARENT);
265
+ nish_region_slot *parent = nish_region_slot_of(p);
266
+ uint64_t r = nish_region_new();
267
+ nish_region_slot *s = nish_region_slot_of(r);
268
+ uint32_t link = (uint32_t)(r & (NISH_REGION_SLOTS - 1)) + 1;
269
+ s->parent = (uint32_t)(p & (NISH_REGION_SLOTS - 1)) + 1;
270
+ s->next_sibling = parent->first_child;
271
+ if (parent->first_child) nish_region_at(parent->first_child)->prev_sibling = link;
272
+ parent->first_child = link;
273
+ return r;
274
+ }
275
+
207
276
  void *nish_region_enter(uint64_t r) {
208
277
  nish_region_slot *s = nish_region_slot_of(r);
209
278
  if (s->entered) nish_region_die(NISH_REGION_ENTERED);
@@ -223,14 +292,11 @@ void nish_region_leave(uint64_t r, void *root) {
223
292
  s->root = root;
224
293
  }
225
294
 
226
- void nish_region_free(uint64_t r) {
227
- nish_region_slot *s = nish_region_slot_of(r);
228
- if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
229
- nish_region_swap(s);
230
- nish_free_arena();
231
- nish_region_swap(s);
295
+ /* Give the slot back: its generation moves on, so every copy of `r` is
296
+ stale, and the slot joins the free list unless that generation is the last.
297
+ The slot's arena is already empty. */
298
+ static void nish_region_retire(nish_region_slot *s, uint64_t index) {
232
299
  s->root = 0;
233
- uint64_t index = r & (NISH_REGION_SLOTS - 1);
234
300
  nish_region_lock();
235
301
  __atomic_store_n(&s->thread, 0, __ATOMIC_RELAXED);
236
302
  uint64_t gen = s->gen + 1;
@@ -241,3 +307,99 @@ void nish_region_free(uint64_t r) {
241
307
  }
242
308
  nish_region_unlock();
243
309
  }
310
+
311
+ /* Take the slot out of its parent's child list. */
312
+ static void nish_region_unlink(nish_region_slot *s) {
313
+ if (!s->parent) return;
314
+ if (s->prev_sibling) nish_region_at(s->prev_sibling)->next_sibling = s->next_sibling;
315
+ else nish_region_at(s->parent)->first_child = s->next_sibling;
316
+ if (s->next_sibling) nish_region_at(s->next_sibling)->prev_sibling = s->prev_sibling;
317
+ s->parent = 0;
318
+ s->prev_sibling = 0;
319
+ s->next_sibling = 0;
320
+ }
321
+
322
+ /* The region after `s` in a walk of the subtree under `top` that visits a
323
+ region before its children (first child, else next sibling, else the next
324
+ sibling of the nearest ancestor below `top` that has one), or 0 at the end. */
325
+ static nish_region_slot *nish_region_next_in(nish_region_slot *s, nish_region_slot *top) {
326
+ if (s->first_child) return nish_region_at(s->first_child);
327
+ while (s != top) {
328
+ if (s->next_sibling) return nish_region_at(s->next_sibling);
329
+ s = nish_region_at(s->parent);
330
+ }
331
+ return 0;
332
+ }
333
+
334
+ void nish_region_free(uint64_t r) {
335
+ nish_region_slot *top = nish_region_slot_of(r);
336
+ /* Nothing is freed unless nothing in the tree is entered. */
337
+ for (nish_region_slot *s = top; s; s = nish_region_next_in(s, top))
338
+ if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
339
+ nish_region_unlink(top);
340
+ nish_region_slot *s = top;
341
+ for (;;) {
342
+ while (s->first_child) s = nish_region_at(s->first_child);
343
+ /* A leaf: free it, then go on from its parent, which may now be one. */
344
+ nish_region_slot *up = s == top ? 0 : nish_region_at(s->parent);
345
+ uint64_t index = s == top ? (r & (NISH_REGION_SLOTS - 1)) : (uint64_t)up->first_child - 1;
346
+ nish_region_unlink(s);
347
+ nish_region_swap(s);
348
+ nish_free_arena();
349
+ nish_region_swap(s);
350
+ nish_region_retire(s, index);
351
+ if (!up) return;
352
+ s = up;
353
+ }
354
+ }
355
+
356
+ /* An `awaitAll` lane's region, once the lane has finished: its chunks go on
357
+ top of the arena in `nish_arena`, newest first as there, so what the lane
358
+ answered lives on in the arena that awaited it and is given back when that
359
+ arena next releases below them. What the caller's current chunk had left is
360
+ then carved into a chunk of its own, on top of the region's, and bumping
361
+ goes on there: the caller's chunk is not buried under the region's, which
362
+ would leave its rest unused and make the caller's next allocation grow a
363
+ fresh 64 KiB chunk -- a chunk per adopt in a loop that never releases
364
+ (docs/security/async.md, ASYNC-15). The carved chunk has no malloc of its
365
+ own, and bit 0 of its `cap` says so, so `nish_free_until` passes it by; it
366
+ always sits above the chunk it was carved from, so whatever frees that one
367
+ has dropped it first. Less than NISH_ADOPT_CARVE left is given up. The
368
+ next-chunk size (`nish_region_chunk`) is not touched: it is the caller's,
369
+ which a region only swaps while entered. A newest chunk the lane released
370
+ back to empty is freed, not moved, so a lane's temporaries do not outlive
371
+ it. A region that never allocated moves nothing. Like `free`, it panics on
372
+ a region that is entered. */
373
+ void nish_region_adopt(uint64_t r) {
374
+ nish_region_slot *s = nish_region_slot_of(r);
375
+ if (s->entered) nish_region_die(NISH_REGION_FREE_ENTERED);
376
+ if (s->parent || s->first_child) nish_region_die(NISH_REGION_ADOPT_TREE);
377
+ nish_chunk_link *head = s->arena.chunks;
378
+ if (head && s->arena.off == 0) {
379
+ /* The lane rewound its newest chunk to empty: a release to the mark it
380
+ took on entry, 0 in an empty region, keeps that chunk for reuse
381
+ (`nish_reset_arena`), but nothing will bump it again once adopted. */
382
+ nish_chunk_link *next = head->next;
383
+ if (!(head->cap & NISH_CHUNK_CARVED)) free(head);
384
+ head = next;
385
+ uint64_t cap = head ? head->cap & ~(uint64_t)NISH_CHUNK_CARVED : 0;
386
+ s->arena = head ? (struct nish_arena){ (char *)(head + 1), cap, cap, (void *)head } : (struct nish_arena){ 0 };
387
+ }
388
+ if (head) {
389
+ nish_chunk_link *last = head;
390
+ while (last->next) last = last->next;
391
+ struct nish_arena caller = nish_arena;
392
+ uint64_t left = caller.buf ? caller.cap - caller.off : 0;
393
+ last->next = caller.chunks;
394
+ nish_arena = s->arena;
395
+ if (left >= NISH_ADOPT_CARVE) {
396
+ nish_chunk_link *c = (nish_chunk_link *)(caller.buf + caller.off);
397
+ uint64_t cap = left - sizeof *c;
398
+ c->next = nish_arena.chunks;
399
+ c->cap = cap | NISH_CHUNK_CARVED;
400
+ nish_arena = (struct nish_arena){ (char *)(c + 1), 0, cap, (void *)c };
401
+ }
402
+ }
403
+ s->arena = (struct nish_arena){ 0 };
404
+ nish_region_retire(s, r & (NISH_REGION_SLOTS - 1));
405
+ }