@amritk/nish-x86_64-linux 0.42.0 → 0.45.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/runtime/runtime.c
CHANGED
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
* both use `-flto`, and the cold paths that lost the inlining are cold.
|
|
22
22
|
*/
|
|
23
23
|
#define _POSIX_C_SOURCE 200809L
|
|
24
|
-
#
|
|
24
|
+
#if defined NISH_ARENA_DEBUG || defined NISH_ARENA_RANGE
|
|
25
25
|
/* The debug arena's `madvise`, which POSIX leaves out (its `posix_madvise`
|
|
26
|
-
gives nothing back on glibc)
|
|
26
|
+
gives nothing back on glibc), and the compact-class range's `MAP_ANONYMOUS`,
|
|
27
|
+
`MAP_NORESERVE` and `MAP_FIXED_NOREPLACE`. */
|
|
27
28
|
#define _DEFAULT_SOURCE
|
|
28
29
|
#define _DARWIN_C_SOURCE
|
|
29
30
|
#endif
|
|
@@ -203,6 +204,156 @@ NISH_TLS uint64_t nish_region_chunk;
|
|
|
203
204
|
single allocation larger than this still gets a chunk of its own size. */
|
|
204
205
|
#define NISH_REGION_CHUNK_MAX ((uint64_t)1 << 20)
|
|
205
206
|
|
|
207
|
+
#ifdef NISH_ARENA_RANGE
|
|
208
|
+
/* A module compiled for this variant references it, so linking one against
|
|
209
|
+
the default runtime fails at the link rather than at the first reference.
|
|
210
|
+
The ASan build defines it too, so the same module links there. */
|
|
211
|
+
const int nish_arena_range_abi = 1;
|
|
212
|
+
/* The range's first byte: what a 32-bit reference counts from. Null under
|
|
213
|
+
NISH_ASAN, which reserves nothing. */
|
|
214
|
+
char *nish_ref_base;
|
|
215
|
+
#endif
|
|
216
|
+
|
|
217
|
+
/* NISH_ASAN wins over NISH_ARENA_RANGE: an instrumented build reserves
|
|
218
|
+
nothing and every block stays a malloc of its own, as without the range.
|
|
219
|
+
ASan's shadow memory on x86-64 lies across [1 GiB, 16 GiB), where the
|
|
220
|
+
range would go, and a block carved from a range has no redzone. */
|
|
221
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ASAN
|
|
222
|
+
/* ---- The compact-class range (`-DNISH_ARENA_RANGE`; docs/wp48-ref32-classes.md
|
|
223
|
+
§3.3, docs/security/runtime.md, "The compact-class range")
|
|
224
|
+
A program with a compact class links this variant, which build.sh selects
|
|
225
|
+
when the compiler asks for it; nothing selects it by hand. Every arena chunk
|
|
226
|
+
of 2 MiB or less is cut from one 15 GiB range, so a reference into one fits
|
|
227
|
+
32 bits once it is counted in 4-byte units from the range's base. On Linux
|
|
228
|
+
the range sits at the fixed address [1 GiB, 16 GiB) and the base is a
|
|
229
|
+
constant; elsewhere (macOS) the system places it and the base is
|
|
230
|
+
`nish_ref_base`. It is reserved once, before main, read-write and without
|
|
231
|
+
committing its pages; an address something else already holds is a panic
|
|
232
|
+
that names it and the limit, never a fallback to another address, because
|
|
233
|
+
compiled code may already treat the base as a constant.
|
|
234
|
+
A chunk's `cap` is a power of two from 4 KiB to 2 MiB, its class, with its
|
|
235
|
+
16-byte header in front; a chunk a release frees goes on its class's free
|
|
236
|
+
list instead of back to malloc, and the next request of that class takes
|
|
237
|
+
it before the range's high-water mark moves. One lock covers both, for
|
|
238
|
+
every thread. A chunk is told from a malloc'd one by its address. The
|
|
239
|
+
first 64 bytes of the range are never handed out, so offset 0 is never a
|
|
240
|
+
block. A request past 2 MiB keeps an exact malloc'd chunk, as today. The
|
|
241
|
+
range never shrinks: a chunk on a free list keeps its pages. */
|
|
242
|
+
#include <sys/mman.h>
|
|
243
|
+
#ifdef NISH_RANGE_TEST_SIZE
|
|
244
|
+
#define NISH_RANGE_SIZE ((uint64_t)(NISH_RANGE_TEST_SIZE))
|
|
245
|
+
#else
|
|
246
|
+
#define NISH_RANGE_SIZE ((uint64_t)15 << 30)
|
|
247
|
+
#endif
|
|
248
|
+
/* The fixed shape is Linux's. tests/runtime/range-test.c's sanitizer builds
|
|
249
|
+
take the based one (`NISH_RANGE_TEST_BASED`): their shadow is where the
|
|
250
|
+
fixed range would be. */
|
|
251
|
+
#if defined __linux__ && !defined NISH_RANGE_TEST_BASED
|
|
252
|
+
#define NISH_RANGE_FIXED ((uintptr_t)1 << 30)
|
|
253
|
+
#ifndef MAP_FIXED_NOREPLACE
|
|
254
|
+
#define MAP_FIXED_NOREPLACE 0x100000
|
|
255
|
+
#endif
|
|
256
|
+
#endif
|
|
257
|
+
#define NISH_RANGE_HOLE 64
|
|
258
|
+
#define NISH_RANGE_CLASS_MIN ((uint64_t)1 << 12)
|
|
259
|
+
#define NISH_RANGE_CLASS_MAX ((uint64_t)1 << 21)
|
|
260
|
+
#define NISH_RANGE_CLASSES 10
|
|
261
|
+
/* The range's mapping, `prot` its protection (the debug arena's is
|
|
262
|
+
PROT_NONE): the fixed address or none, with a panic on anything else. */
|
|
263
|
+
static char *nish_range_map(int prot) {
|
|
264
|
+
#ifdef NISH_RANGE_FIXED
|
|
265
|
+
void *p = mmap((void *)NISH_RANGE_FIXED, (size_t)NISH_RANGE_SIZE, prot,
|
|
266
|
+
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED_NOREPLACE, -1, 0);
|
|
267
|
+
/* A kernel older than MAP_FIXED_NOREPLACE takes the address as a hint and
|
|
268
|
+
maps elsewhere when it is busy: that is busy too. */
|
|
269
|
+
if (p != MAP_FAILED && p != (void *)NISH_RANGE_FIXED) munmap(p, (size_t)NISH_RANGE_SIZE);
|
|
270
|
+
/* EEXIST is the address taken; anything else (ENOMEM: a limit on address
|
|
271
|
+
space or mappings) is the system refusing the size. */
|
|
272
|
+
if (p == MAP_FAILED && errno != EEXIST)
|
|
273
|
+
nish_die("nish: the compact-class arena cannot reserve its 15 GiB range at 0x40000000 "
|
|
274
|
+
"(1 GiB to 16 GiB): the system refused the mapping (an address-space or mapping limit)\n");
|
|
275
|
+
if (p != (void *)NISH_RANGE_FIXED)
|
|
276
|
+
nish_die("nish: the compact-class arena cannot reserve its 15 GiB range at 0x40000000 "
|
|
277
|
+
"(1 GiB to 16 GiB): the address is in use\n");
|
|
278
|
+
return p;
|
|
279
|
+
#else
|
|
280
|
+
void *p = mmap(0, (size_t)NISH_RANGE_SIZE, prot, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
|
|
281
|
+
if (p == MAP_FAILED) nish_die("nish: the compact-class arena cannot reserve its 15 GiB range\n");
|
|
282
|
+
return p;
|
|
283
|
+
#endif
|
|
284
|
+
}
|
|
285
|
+
#endif
|
|
286
|
+
|
|
287
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ASAN && !defined NISH_ARENA_DEBUG
|
|
288
|
+
static int nish_range_state; /* 0 none, 1 reserving, 2 reserved */
|
|
289
|
+
static int nish_range_lock;
|
|
290
|
+
static uint64_t nish_range_top = NISH_RANGE_HOLE; /* the high-water mark, under the lock */
|
|
291
|
+
static nish_chunk *nish_range_free[NISH_RANGE_CLASSES];
|
|
292
|
+
|
|
293
|
+
static void nish_range_reserve(void) {
|
|
294
|
+
int want = 0;
|
|
295
|
+
if (!__atomic_compare_exchange_n(&nish_range_state, &want, 1, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) {
|
|
296
|
+
while (__atomic_load_n(&nish_range_state, __ATOMIC_ACQUIRE) != 2) {}
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
nish_ref_base = nish_range_map(PROT_READ | PROT_WRITE);
|
|
300
|
+
__atomic_store_n(&nish_range_state, 2, __ATOMIC_RELEASE);
|
|
301
|
+
}
|
|
302
|
+
/* Before main, so a busy address is a panic before the program runs. */
|
|
303
|
+
__attribute__((constructor)) static void nish_range_init(void) { nish_range_reserve(); }
|
|
304
|
+
|
|
305
|
+
static int nish_range_owns(const void *c) {
|
|
306
|
+
uintptr_t a = (uintptr_t)c, base = (uintptr_t)nish_ref_base;
|
|
307
|
+
return nish_ref_base && a >= base && a - base < NISH_RANGE_SIZE;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/* A class's index: `cap` is a power of two in [4 KiB, 2 MiB]. */
|
|
311
|
+
static unsigned nish_range_class(uint64_t cap) { return (unsigned)__builtin_ctzll(cap) - 12; }
|
|
312
|
+
|
|
313
|
+
static void nish_range_enter(void) {
|
|
314
|
+
while (__atomic_exchange_n(&nish_range_lock, 1, __ATOMIC_ACQUIRE)) {}
|
|
315
|
+
}
|
|
316
|
+
static void nish_range_leave(void) { __atomic_store_n(&nish_range_lock, 0, __ATOMIC_RELEASE); }
|
|
317
|
+
|
|
318
|
+
/* A chunk of class `cap`: its free list's newest, or the next unused bytes. */
|
|
319
|
+
static nish_chunk *nish_range_take(uint64_t cap) {
|
|
320
|
+
if (__atomic_load_n(&nish_range_state, __ATOMIC_ACQUIRE) != 2) nish_range_reserve();
|
|
321
|
+
unsigned k = nish_range_class(cap);
|
|
322
|
+
nish_range_enter();
|
|
323
|
+
nish_chunk *c = nish_range_free[k];
|
|
324
|
+
if (c) {
|
|
325
|
+
nish_range_free[k] = c->next;
|
|
326
|
+
} else {
|
|
327
|
+
uint64_t span = sizeof(nish_chunk) + cap;
|
|
328
|
+
/* `span > NISH_RANGE_SIZE` folds to false at 15 GiB; it keeps a smaller
|
|
329
|
+
test size (NISH_RANGE_TEST_SIZE) from wrapping the subtraction. */
|
|
330
|
+
if (span > NISH_RANGE_SIZE || nish_range_top > NISH_RANGE_SIZE - span) {
|
|
331
|
+
nish_range_leave();
|
|
332
|
+
nish_die("nish: the compact-class arena's 15 GiB range is used up\n");
|
|
333
|
+
}
|
|
334
|
+
c = (nish_chunk *)(nish_ref_base + nish_range_top);
|
|
335
|
+
nish_range_top += span;
|
|
336
|
+
}
|
|
337
|
+
nish_range_leave();
|
|
338
|
+
return c;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/* A chunk a release gives back: onto its class's list if the range holds it,
|
|
342
|
+
to free() if malloc made it. runtime-region.c's adopt drops through this. */
|
|
343
|
+
void nish_range_drop(void *p) {
|
|
344
|
+
nish_chunk *c = p;
|
|
345
|
+
if (!nish_range_owns(c)) {
|
|
346
|
+
free(c);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
unsigned k = nish_range_class(NISH_CHUNK_CAP(c));
|
|
350
|
+
nish_range_enter();
|
|
351
|
+
c->next = nish_range_free[k];
|
|
352
|
+
nish_range_free[k] = c;
|
|
353
|
+
nish_range_leave();
|
|
354
|
+
}
|
|
355
|
+
#endif
|
|
356
|
+
|
|
206
357
|
#ifdef NISH_ARENA_DEBUG
|
|
207
358
|
/* ---- The debug arena (`--debug-arena`, `-DNISH_ARENA_DEBUG`;
|
|
208
359
|
docs/security/runtime.md, "The debug arena")
|
|
@@ -239,7 +390,14 @@ static void nish_debug_on_fault(int sig, siginfo_t *info, void *ctx) {
|
|
|
239
390
|
(void)ctx;
|
|
240
391
|
uintptr_t a = (uintptr_t)info->si_addr, base = (uintptr_t)nish_debug_base;
|
|
241
392
|
struct sigaction *old = sig == SIGSEGV ? &nish_debug_old_segv : &nish_debug_old_bus;
|
|
242
|
-
|
|
393
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ASAN
|
|
394
|
+
/* The range's first page is never handed out: a fault there (a reference
|
|
395
|
+
of 0, counted from the base) is not a stale block. */
|
|
396
|
+
uint64_t lo = nish_debug_page;
|
|
397
|
+
#else
|
|
398
|
+
uint64_t lo = 0;
|
|
399
|
+
#endif
|
|
400
|
+
if (a >= base + lo && a - base < __atomic_load_n(&nish_debug_next, __ATOMIC_RELAXED)) {
|
|
243
401
|
char msg[] = "nish: --debug-arena: stale arena reference at 0x0000000000000000: "
|
|
244
402
|
"the memory was released by the scope that allocated it\n";
|
|
245
403
|
char *hex = msg + sizeof "nish: --debug-arena: stale arena reference at 0x" - 1;
|
|
@@ -263,6 +421,16 @@ static void nish_debug_reserve(void) {
|
|
|
263
421
|
}
|
|
264
422
|
long page = sysconf(_SC_PAGESIZE);
|
|
265
423
|
nish_debug_page = page > 0 ? (uint64_t)page : 4096;
|
|
424
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ASAN
|
|
425
|
+
/* With a compact class the reservation is the compact range itself, fixed
|
|
426
|
+
or based as without the debug arena, so a 32-bit reference still reaches
|
|
427
|
+
every block. No address is reused there either; its first page, which
|
|
428
|
+
holds the 64 bytes no block may have, is never handed out. */
|
|
429
|
+
nish_debug_base = nish_range_map(PROT_NONE);
|
|
430
|
+
nish_debug_size = NISH_RANGE_SIZE;
|
|
431
|
+
nish_debug_next = nish_debug_page;
|
|
432
|
+
nish_ref_base = nish_debug_base;
|
|
433
|
+
#else
|
|
266
434
|
for (uint64_t size = (uint64_t)1 << 40; size >= (uint64_t)1 << 30; size >>= 1) {
|
|
267
435
|
void *p = mmap(0, (size_t)size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
|
|
268
436
|
if (p != MAP_FAILED) {
|
|
@@ -272,6 +440,7 @@ static void nish_debug_reserve(void) {
|
|
|
272
440
|
}
|
|
273
441
|
}
|
|
274
442
|
if (!nish_debug_base) nish_die("nish: --debug-arena: cannot reserve address space for the arena\n");
|
|
443
|
+
#endif
|
|
275
444
|
struct sigaction sa;
|
|
276
445
|
memset(&sa, 0, sizeof sa);
|
|
277
446
|
sa.sa_sigaction = nish_debug_on_fault;
|
|
@@ -292,8 +461,13 @@ static nish_chunk *nish_debug_chunk(uint64_t cap) {
|
|
|
292
461
|
if (cap > nish_debug_size - sizeof(nish_chunk) - nish_debug_page) nish_oom();
|
|
293
462
|
uint64_t span = nish_debug_span(cap);
|
|
294
463
|
uint64_t at = __atomic_fetch_add(&nish_debug_next, span, __ATOMIC_RELAXED);
|
|
464
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ASAN
|
|
465
|
+
if (at > nish_debug_size - span)
|
|
466
|
+
nish_die("nish: --debug-arena: the compact-class arena's 15 GiB range is used up (no address is reused)\n");
|
|
467
|
+
#else
|
|
295
468
|
if (at > nish_debug_size - span)
|
|
296
469
|
nish_die("nish: --debug-arena: the arena's address space is used up (no address is reused)\n");
|
|
470
|
+
#endif
|
|
297
471
|
char *p = nish_debug_base + at;
|
|
298
472
|
if (mprotect(p, (size_t)span, PROT_READ | PROT_WRITE) != 0) nish_oom();
|
|
299
473
|
return (nish_chunk *)p;
|
|
@@ -322,6 +496,14 @@ void nish_debug_drop(void *c) {
|
|
|
322
496
|
}
|
|
323
497
|
#endif
|
|
324
498
|
|
|
499
|
+
#if defined NISH_ARENA_DEBUG && defined NISH_ARENA_RANGE && !defined NISH_ASAN
|
|
500
|
+
/* Before main, as without the debug arena: a busy address is a panic before
|
|
501
|
+
the program runs. */
|
|
502
|
+
__attribute__((constructor)) static void nish_debug_range_init(void) {
|
|
503
|
+
if (__atomic_load_n(&nish_debug_state, __ATOMIC_ACQUIRE) != 2) nish_debug_reserve();
|
|
504
|
+
}
|
|
505
|
+
#endif
|
|
506
|
+
|
|
325
507
|
/* Slow path (size 8-byte rounded): push a chunk (>= 64 KB, or a region's
|
|
326
508
|
next size), bump from it. A region's chunks double from its first, up to
|
|
327
509
|
NISH_REGION_CHUNK_MAX. */
|
|
@@ -345,6 +527,17 @@ void *nish_arena_grow(uint64_t size) {
|
|
|
345
527
|
uint64_t floor = next ? next : 65536;
|
|
346
528
|
uint64_t cap = size > floor ? size : floor;
|
|
347
529
|
if (next) nish_region_chunk = cap < NISH_REGION_CHUNK_MAX / 2 ? cap * 2 : NISH_REGION_CHUNK_MAX;
|
|
530
|
+
#endif
|
|
531
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ASAN && !defined NISH_ARENA_DEBUG
|
|
532
|
+
/* Up to 2 MiB, a chunk of the range: `cap` rounded up to its class. */
|
|
533
|
+
if (cap <= NISH_RANGE_CLASS_MAX) {
|
|
534
|
+
uint64_t k = cap < NISH_RANGE_CLASS_MIN ? NISH_RANGE_CLASS_MIN : (uint64_t)1 << (64 - __builtin_clzll(cap - 1));
|
|
535
|
+
nish_chunk *r = nish_range_take(k);
|
|
536
|
+
r->next = nish_arena.chunks;
|
|
537
|
+
r->cap = k;
|
|
538
|
+
nish_arena = (struct nish_arena){ (char *)(r + 1), size, k, r };
|
|
539
|
+
return nish_arena.buf;
|
|
540
|
+
}
|
|
348
541
|
#endif
|
|
349
542
|
/* A 64-bit request cannot always be asked for: wasm32's `size_t` is 32 bits,
|
|
350
543
|
and malloc would silently take the low half of a chunk nobody can address. */
|
|
@@ -377,11 +570,22 @@ void *nish_alloc_struct(uint64_t size) {
|
|
|
377
570
|
return nish_asan_alloc(size);
|
|
378
571
|
#endif
|
|
379
572
|
size = (size + 7) & ~(uint64_t)7;
|
|
573
|
+
#ifdef NISH_ARENA_RANGE
|
|
574
|
+
/* Compiled code bumps a compact class's 4-byte multiple, so `off` may sit
|
|
575
|
+
4 bytes past an 8-byte boundary: a block from here starts at the next. */
|
|
576
|
+
uint64_t at = (nish_arena.off + 7) & ~(uint64_t)7;
|
|
577
|
+
if (at + size <= nish_arena.cap) {
|
|
578
|
+
void *p = nish_arena.buf + at;
|
|
579
|
+
nish_arena.off = at + size;
|
|
580
|
+
return p;
|
|
581
|
+
}
|
|
582
|
+
#else
|
|
380
583
|
if (nish_arena.off + size <= nish_arena.cap) {
|
|
381
584
|
void *p = nish_arena.buf + nish_arena.off;
|
|
382
585
|
nish_arena.off += size;
|
|
383
586
|
return p;
|
|
384
587
|
}
|
|
588
|
+
#endif
|
|
385
589
|
return nish_arena_grow(size);
|
|
386
590
|
}
|
|
387
591
|
|
|
@@ -449,6 +653,18 @@ static void nish_grown_put(char *data, const void *owner, uint64_t native) {
|
|
|
449
653
|
}
|
|
450
654
|
#endif
|
|
451
655
|
void nish_free_arena(void);
|
|
656
|
+
/* The bytes of a chunk the arena reuses from its start, after a reset or a
|
|
657
|
+
release to a mark there. A scope may have marked that start, and a block
|
|
658
|
+
that began there and filled the chunk exactly would be one
|
|
659
|
+
`nish_array_grow_here` reallocates -- which would either keep the grown
|
|
660
|
+
chunk for good (the release keeps the chunk its mark is in) or move it from
|
|
661
|
+
under the mark, which then names no chunk and frees nothing. So a chunk
|
|
662
|
+
that growth could reallocate, one past 64 KB, is reused a word short, and no
|
|
663
|
+
block from its start fills it. A chunk of 64 KB or less is never
|
|
664
|
+
reallocated and keeps all of it: a recycled task region
|
|
665
|
+
(runtime-async.c) or a loop's reset chunk that a word short no longer fits
|
|
666
|
+
the same allocation again would take a fresh chunk every time. */
|
|
667
|
+
#define NISH_REUSE_CAP(cap) ((cap) > 65536 ? (cap) - 8 : (cap))
|
|
452
668
|
static void nish_free_until(nish_chunk *c, const nish_chunk *end) {
|
|
453
669
|
#ifdef NISH_ASAN
|
|
454
670
|
/* A freed block's address comes back from malloc: forget the grown ones. */
|
|
@@ -464,6 +680,8 @@ static void nish_free_until(nish_chunk *c, const nish_chunk *end) {
|
|
|
464
680
|
nish_chunk *n = c->next;
|
|
465
681
|
#ifdef NISH_ARENA_DEBUG
|
|
466
682
|
if (!(c->cap & NISH_CHUNK_CARVED)) nish_debug_drop(c);
|
|
683
|
+
#elif defined NISH_ARENA_RANGE && !defined NISH_ASAN
|
|
684
|
+
if (!(c->cap & NISH_CHUNK_CARVED)) nish_range_drop(c);
|
|
467
685
|
#else
|
|
468
686
|
if (!(c->cap & NISH_CHUNK_CARVED)) free(c);
|
|
469
687
|
#endif
|
|
@@ -493,7 +711,7 @@ void nish_reset_arena(void) {
|
|
|
493
711
|
}
|
|
494
712
|
nish_free_until(keep->next, 0);
|
|
495
713
|
keep->next = 0;
|
|
496
|
-
nish_arena = (struct nish_arena){ (char *)(keep + 1), 0, keep->cap, keep };
|
|
714
|
+
nish_arena = (struct nish_arena){ (char *)(keep + 1), 0, NISH_REUSE_CAP(keep->cap), keep };
|
|
497
715
|
}
|
|
498
716
|
|
|
499
717
|
void nish_free_arena(void) {
|
|
@@ -521,7 +739,8 @@ void nish_arena_release(uint64_t mark) {
|
|
|
521
739
|
}
|
|
522
740
|
if (!c) return;
|
|
523
741
|
nish_free_until(nish_arena.chunks, c);
|
|
524
|
-
|
|
742
|
+
uint64_t off = mark - (uintptr_t)(c + 1), cap = NISH_CHUNK_CAP(c);
|
|
743
|
+
nish_arena = (struct nish_arena){ (char *)(c + 1), off, off ? cap : NISH_REUSE_CAP(cap), c };
|
|
525
744
|
#ifdef NISH_ARENA_DEBUG
|
|
526
745
|
/* Never bump into what the mark gave back: the next block gets a chunk. */
|
|
527
746
|
nish_arena.cap = nish_arena.off;
|
|
@@ -551,6 +770,12 @@ void *nish_arena_keep(uint64_t mark, void *p) {
|
|
|
551
770
|
if (mark >= cb && mark <= cb + NISH_CHUNK_CAP(c)) break;
|
|
552
771
|
}
|
|
553
772
|
if (!c || (c == pc && mark > q)) return p;
|
|
773
|
+
#ifdef NISH_ARENA_RANGE
|
|
774
|
+
/* A compact class's 4-byte bump may have left the mark 4 past an 8-byte
|
|
775
|
+
boundary (`nish_alloc_struct`): the kept block moves to the next one.
|
|
776
|
+
`q` is 8-aligned, so a mark at or below it rounds to at most `q`. */
|
|
777
|
+
mark = (mark + 7) & ~(uint64_t)7;
|
|
778
|
+
#endif
|
|
554
779
|
/* mark <= q inside one chunk implies mark + size <= q + size <= its end, so
|
|
555
780
|
the second arm is only ever reached with `c` older than `pc`. */
|
|
556
781
|
if (mark + size <= (uintptr_t)(c + 1) + NISH_CHUNK_CAP(c)) {
|
|
@@ -715,7 +940,101 @@ double nish_parse_number(const nish_str *s, int32_t mode) {
|
|
|
715
940
|
* the extra compare moved register allocation in the hot loops around them
|
|
716
941
|
* (+0.2% to +2% instructions over bench/instructions.json's map rows). Out of
|
|
717
942
|
* line, every one of those rows runs fewer instructions than it did before, and
|
|
718
|
-
* the call costs nothing on a path that runs once per doubling.
|
|
943
|
+
* the call costs nothing on a path that runs once per doubling.
|
|
944
|
+
*
|
|
945
|
+
* A normal build first tries to grow the block where it is
|
|
946
|
+
* (`nish_array_grow_here`), so that an array built by `push` costs its final
|
|
947
|
+
* size rather than the sum of every size it passed through. */
|
|
948
|
+
#ifndef NISH_ASAN
|
|
949
|
+
/* How many chunks `nish_array_grow_here` looks through for the one an array
|
|
950
|
+
fills: a program building a few large columns at once keeps each in a chunk
|
|
951
|
+
of its own near the head of the list, and a longer walk would cost more than
|
|
952
|
+
the copy it might save on a small array. */
|
|
953
|
+
#define NISH_GROW_WALK 16
|
|
954
|
+
|
|
955
|
+
/* Grow `a`'s block from `old` to `want` bytes (both 8-rounded) without moving
|
|
956
|
+
* its elements to a fresh block, or answer 0 to have the caller copy them. Two
|
|
957
|
+
* cases are cheap and safe:
|
|
958
|
+
*
|
|
959
|
+
* - The block is the last one bumped from the current chunk, with room
|
|
960
|
+
* behind it: bump `off` past the new bytes. Nothing lies after the block,
|
|
961
|
+
* so nothing is overwritten.
|
|
962
|
+
* - The block is the whole of a malloc'd chunk, as every block over 64 KB is
|
|
963
|
+
* (`nish_arena_grow` gives it an exact chunk): `realloc` the chunk, which
|
|
964
|
+
* for a large block remaps its pages rather than copying them and returns
|
|
965
|
+
* the old block's memory, and relink it where it was in the list. The order
|
|
966
|
+
* of the list is what `nish_arena_release` and runtime-cross.c's
|
|
967
|
+
* `nish_since` read, and it is unchanged.
|
|
968
|
+
*
|
|
969
|
+
* Both are sound because no pointer into `data` is held across a push (the
|
|
970
|
+
* comment above), and because a scope that releases its arena never pushes on
|
|
971
|
+
* an array older than its mark (docs/LANGUAGE.md, "Memory model"), so the bytes
|
|
972
|
+
* a grow takes past the old block are never below a mark that is still to be
|
|
973
|
+
* released. A carved chunk (`NISH_CHUNK_CARVED`) lies inside an older one and
|
|
974
|
+
* is never reallocated; an array whose block is anywhere else -- a literal on
|
|
975
|
+
* the stack or in a constant, a shared-memory array, a block another thread's
|
|
976
|
+
* or region's arena handed out, one in the middle of a chunk -- simply takes
|
|
977
|
+
* the copy. */
|
|
978
|
+
static int nish_array_grow_here(nish_array *a, uint64_t old, uint64_t want) {
|
|
979
|
+
#ifdef NISH_ARENA_DEBUG
|
|
980
|
+
/* The debug arena's chunks are cut from its reservation, never malloc'd, so
|
|
981
|
+
none can be `realloc`ed; and a grown array always moves, which is what
|
|
982
|
+
lets a reference to the old block fault. */
|
|
983
|
+
(void)a, (void)old, (void)want;
|
|
984
|
+
return 0;
|
|
985
|
+
#endif
|
|
986
|
+
char *data = a->data, *buf = nish_arena.buf;
|
|
987
|
+
uint64_t more = want - old;
|
|
988
|
+
/* Under the compact-class range this bump needs no realign: it hands out no
|
|
989
|
+
new block, only lengthens the one that ends at `off`, by an 8-rounded
|
|
990
|
+
`more`; the block keeps the alignment it had, and every runtime bump
|
|
991
|
+
after it realigns `off` itself. An array from `nish_alloc_struct` ends
|
|
992
|
+
8-aligned, so an `off` compiled code left 4 past a boundary is never its
|
|
993
|
+
end, and that array takes the copy. */
|
|
994
|
+
if (buf && data >= buf && data + old == buf + nish_arena.off && more <= nish_arena.cap - nish_arena.off) {
|
|
995
|
+
nish_arena.off += more;
|
|
996
|
+
return 1;
|
|
997
|
+
}
|
|
998
|
+
/* A block of 64 KB or less never has a chunk to itself in the thread's arena
|
|
999
|
+
(`nish_arena_grow`'s floor), so it skips the walk and takes the copy, as it
|
|
1000
|
+
also does in a region whose chunks are smaller. Walking for every small
|
|
1001
|
+
array cost Bounce, which grows about ten thousand, 0.6% instructions.
|
|
1002
|
+
|
|
1003
|
+
The head is relinked by assigning the whole arena, never through a pointer
|
|
1004
|
+
to `nish_arena.chunks`: a link pointer the walk rewrites, or a phi or select
|
|
1005
|
+
of that field and a chunk's `next`, takes the arena's address, which costs
|
|
1006
|
+
every program LTO's GlobalsAA (see `nish_reset_arena`) -- Queens' setup
|
|
1007
|
+
reloaded the arena around each header store and filled an array with a
|
|
1008
|
+
`memset` it had folded, +0.35% instructions. The assignment makes the grown
|
|
1009
|
+
chunk the buffer, full, as the head already was: the buffer is always the
|
|
1010
|
+
head's (`nish_arena_grow`, a release, a reset and a region's adoption all
|
|
1011
|
+
leave it there), and a block that fills it has no room left after it. So a
|
|
1012
|
+
chunk further down is never the buffer, and its relink is the `next` alone. */
|
|
1013
|
+
if (old <= 65536) return 0;
|
|
1014
|
+
nish_chunk *prev = 0, *c = nish_arena.chunks;
|
|
1015
|
+
for (int i = 0; c && i < NISH_GROW_WALK; i++, prev = c, c = c->next) {
|
|
1016
|
+
if ((char *)(c + 1) != data) continue;
|
|
1017
|
+
if ((c->cap & NISH_CHUNK_CARVED) || c->cap != old) return 0;
|
|
1018
|
+
#if defined NISH_ARENA_RANGE && !defined NISH_ARENA_DEBUG
|
|
1019
|
+
/* A chunk of the compact-class range is no malloc's, so `realloc` may not
|
|
1020
|
+
touch it: an array that fills one of its class exactly (a power-of-two
|
|
1021
|
+
block of 128 KiB to 2 MiB) takes the copy. Its chunk keeps its class's
|
|
1022
|
+
`cap` for the release that puts it back on its list. */
|
|
1023
|
+
if (nish_range_owns(c)) return 0;
|
|
1024
|
+
#endif
|
|
1025
|
+
if (want > (uint64_t)(SIZE_MAX - sizeof(nish_chunk))) nish_oom();
|
|
1026
|
+
nish_chunk *n = realloc(c, sizeof *n + (size_t)want);
|
|
1027
|
+
if (!n) nish_oom();
|
|
1028
|
+
n->cap = want;
|
|
1029
|
+
a->data = (char *)(n + 1);
|
|
1030
|
+
if (prev) prev->next = n;
|
|
1031
|
+
else nish_arena = (struct nish_arena){ (char *)(n + 1), want, want, n };
|
|
1032
|
+
return 1;
|
|
1033
|
+
}
|
|
1034
|
+
return 0;
|
|
1035
|
+
}
|
|
1036
|
+
#endif
|
|
1037
|
+
|
|
719
1038
|
__attribute__((noinline)) void nish_array_grow(nish_array *a, uint64_t elem_size) {
|
|
720
1039
|
#ifdef NISH_ASAN
|
|
721
1040
|
/* One more slot, not double (see `nish_grown`): in place while a normal build
|
|
@@ -745,6 +1064,10 @@ __attribute__((noinline)) void nish_array_grow(nish_array *a, uint64_t elem_size
|
|
|
745
1064
|
char *data = nish_alloc_struct(room * elem_size);
|
|
746
1065
|
NISH_POISON(data + cap * elem_size, (room - cap) * elem_size);
|
|
747
1066
|
#else
|
|
1067
|
+
if (a->cap && nish_array_grow_here(a, (a->cap * elem_size + 7) & ~(uint64_t)7, (cap * elem_size + 7) & ~(uint64_t)7)) {
|
|
1068
|
+
a->cap = cap;
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
748
1071
|
char *data = nish_alloc_struct(cap * elem_size);
|
|
749
1072
|
#endif
|
|
750
1073
|
if (a->len) memcpy(data, a->data, a->len * elem_size);
|
package/runtime/sqlite/README.md
CHANGED
|
@@ -20,8 +20,13 @@ them.
|
|
|
20
20
|
|
|
21
21
|
## How it is linked
|
|
22
22
|
|
|
23
|
-
`
|
|
24
|
-
`
|
|
23
|
+
`nish-sqlite.c` starts with `#include "sqlite3.c"`, so SQLite and the
|
|
24
|
+
`nish/sqlite` adapter are one unit: the cursors call SQLite's internal
|
|
25
|
+
functions, which are `static` in the amalgamation
|
|
26
|
+
([docs/sqlite-cursor.md](../../docs/sqlite-cursor.md#the-build-decision)).
|
|
27
|
+
`scripts/build.sh` compiles every `*.c` here except `sqlite3.c` for a link
|
|
28
|
+
whose IR declares an `sqlite3_*` or `nishsqlite_*` function, and for no other
|
|
29
|
+
link. The options are
|
|
25
30
|
in `build.sh`, under "SQLite's compile-time options". `NISH_SQLITE_CFLAGS` adds
|
|
26
31
|
to them (`docs/guide/using-nish/cli.md`). `nish --link` and `nish run` compile
|
|
27
32
|
these units once per recipe into an entry of the runtime cache of their own
|
|
@@ -40,6 +45,14 @@ for the units and the files the cache key fingerprints (`sqliteUnits`,
|
|
|
40
45
|
3. Update the table above, the two file hashes included (`openssl dgst
|
|
41
46
|
-sha3-256 sqlite3.c sqlite3.h`), the version in `THIRD_PARTY_NOTICES.md`, and the
|
|
42
47
|
numbers in `tests/link/sqlite_link*/expected.out`.
|
|
43
|
-
4.
|
|
48
|
+
4. Re-check every internal SQLite function the cursor adapter in
|
|
49
|
+
`nish-sqlite.c` calls, the list in
|
|
50
|
+
[`docs/sqlite-cursor.md`](../../docs/sqlite-cursor.md#what-the-adapter-calls):
|
|
51
|
+
these are `SQLITE_PRIVATE`, not SQLite's API, and a release may change one
|
|
52
|
+
without notice. Diff each one's declaration and body against the old
|
|
53
|
+
`sqlite3.c`, including what it asserts about the mutex and the cursor's
|
|
54
|
+
state, and the `KeyInfo` fields the adapter fills by hand against
|
|
55
|
+
`sqlite3KeyInfoOfIndex`. Update the adapter and that list for any change.
|
|
56
|
+
5. Run `node tests/run.js link`, `node tests/run.js sqlite-link`,
|
|
44
57
|
`WASI_SYSROOT=/usr node tests/run.js wasi` and the third-party-licence
|
|
45
58
|
section.
|