nish 0.42.0 → 0.44.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/README.md +10 -4
- package/docs/AI.md +282 -126
- package/package.json +9 -9
- package/runtime/core.d.ts +16 -0
- package/runtime/core.mjs +5 -0
- package/runtime/nish.d.ts +120 -19
- package/runtime/nish.mjs +3 -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/skills/writing-nish/SKILL.md +12 -4
- package/std/README.md +3 -3
- package/std/crypto/base64url.ts +102 -1
- package/std/crypto/scram.ts +142 -0
- package/std/crypto/sha1.ts +99 -41
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/net/handler.ts +1 -1
- package/std/net/http-server.ts +2 -3
- package/std/net/http1-server.ts +281 -51
- package/std/net/http3-server.ts +26 -15
- package/std/net/http3.ts +2 -2
- package/std/net/postgres-async.ts +64 -33
- package/std/net/postgres.ts +65 -76
- package/std/net/quic-conn-ack.ts +57 -7
- package/std/net/quic-frame.ts +104 -2
- package/std/net/quic-listener.ts +9 -2
- package/std/net/quic.ts +16 -1
- package/std/net/serve.ts +10 -8
- package/std/net/websocket.ts +188 -49
- package/std/sqlite.ts +325 -0
- package/std/text.ts +80 -11
package/runtime/runtime-uring.c
CHANGED
|
@@ -204,6 +204,7 @@ void nish_uring_destroy(struct nish_uring_set *s);
|
|
|
204
204
|
#define UR_OP_CLOSE 19
|
|
205
205
|
#define UR_OP_SEND 26
|
|
206
206
|
#define UR_OP_RECV 27
|
|
207
|
+
#define UR_OP_SHUTDOWN 34
|
|
207
208
|
#define UR_RECV_MULTISHOT (1u << 1)
|
|
208
209
|
#define UR_CANCEL_ALL (1u << 0)
|
|
209
210
|
#define UR_CANCEL_FD (1u << 1)
|
|
@@ -332,12 +333,14 @@ enum { UR_ACCEPT = 1, UR_RECV = 2, UR_SEND = 3, UR_CLOSE = 4, UR_CANCEL = 5 };
|
|
|
332
333
|
/* One send's bytes, owned by the set until the kernel has taken all of them.
|
|
333
334
|
* `next` queues it behind the slot's earlier sends; `prev_all`/`next_all`
|
|
334
335
|
* keep every record on one list so `nish_uring_destroy` can free those the
|
|
335
|
-
* kernel still held.
|
|
336
|
+
* kernel still held. `how` is -1 for bytes, or a shutdown's 0 to 2: that
|
|
337
|
+
* record carries none and goes to the kernel as a SHUTDOWN once the sends
|
|
338
|
+
* queued before it are done, so the peer reads them before the FIN. */
|
|
336
339
|
struct ur_send {
|
|
337
340
|
struct ur_send *next, *prev_all, *next_all;
|
|
338
341
|
uint32_t slot, gen;
|
|
339
342
|
uint32_t off, len;
|
|
340
|
-
int in_flight;
|
|
343
|
+
int in_flight, how;
|
|
341
344
|
uint8_t data[];
|
|
342
345
|
};
|
|
343
346
|
|
|
@@ -439,7 +442,7 @@ static int32_t ur_ops_ok(int fd) {
|
|
|
439
442
|
memset(&pr, 0, sizeof pr);
|
|
440
443
|
int e = ur_register(fd, UR_REGISTER_PROBE, &pr, 64);
|
|
441
444
|
if (e < 0) return e == -EINVAL ? -EOPNOTSUPP : e;
|
|
442
|
-
static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV};
|
|
445
|
+
static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_ASYNC_CANCEL, UR_OP_CLOSE, UR_OP_SEND, UR_OP_RECV, UR_OP_SHUTDOWN};
|
|
443
446
|
for (size_t i = 0; i < sizeof ops; i++) {
|
|
444
447
|
uint8_t op = ops[i];
|
|
445
448
|
if (op > pr.last_op || op >= pr.ops_len || !(pr.ops[op].flags & UR_OP_SUPPORTED)) return -EOPNOTSUPP;
|
|
@@ -625,10 +628,15 @@ static void ur_free_ring(struct ur_ring *r) {
|
|
|
625
628
|
}
|
|
626
629
|
|
|
627
630
|
/* Restrict a ring made with R_DISABLED to what the layer submits (WP63 §6.2):
|
|
628
|
-
* the
|
|
629
|
-
* opcode at all once it is enabled.
|
|
631
|
+
* the six opcodes and NOP, the three flags the layer sets, and no register
|
|
632
|
+
* opcode at all once it is enabled. The flags entry allows bits and requires
|
|
633
|
+
* none, so it is the layer, which always sets FIXED_FILE, that confines
|
|
634
|
+
* SHUTDOWN to the ring's own table: an SQE written into the ring by other
|
|
635
|
+
* means could name an ordinary descriptor, as it already could for SEND
|
|
636
|
+
* (NURING-39). */
|
|
630
637
|
static int ur_restrict(int fd) {
|
|
631
|
-
static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_RECV,
|
|
638
|
+
static const uint8_t ops[] = {UR_OP_ACCEPT, UR_OP_RECV, UR_OP_SEND, UR_OP_CLOSE,
|
|
639
|
+
UR_OP_ASYNC_CANCEL, UR_OP_NOP, UR_OP_SHUTDOWN};
|
|
632
640
|
struct ur_restriction res[sizeof ops + 1];
|
|
633
641
|
memset(res, 0, sizeof res);
|
|
634
642
|
for (size_t i = 0; i < sizeof ops; i++) {
|
|
@@ -961,7 +969,8 @@ static void ur_top_up(struct nish_uring_set *s) {
|
|
|
961
969
|
if (rings > s->stats.max_accepting_rings) s->stats.max_accepting_rings = rings;
|
|
962
970
|
}
|
|
963
971
|
|
|
964
|
-
/* Submit the slot's first queued send, from where the kernel left off
|
|
972
|
+
/* Submit the slot's first queued send, from where the kernel left off, or
|
|
973
|
+
* its shutdown. */
|
|
965
974
|
static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set *s) {
|
|
966
975
|
struct ur_send *q = r->sq_first[slot];
|
|
967
976
|
if (!q || q->in_flight) return;
|
|
@@ -970,12 +979,17 @@ static void ur_send_head(struct ur_ring *r, uint32_t slot, struct nish_uring_set
|
|
|
970
979
|
s->stalled_sends++;
|
|
971
980
|
return;
|
|
972
981
|
}
|
|
973
|
-
e->opcode = UR_OP_SEND;
|
|
974
982
|
e->fd = (int32_t)slot;
|
|
975
983
|
e->flags = UR_SQE_FIXED_FILE;
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
984
|
+
if (q->how >= 0) {
|
|
985
|
+
e->opcode = UR_OP_SHUTDOWN;
|
|
986
|
+
e->len = (uint32_t)q->how;
|
|
987
|
+
} else {
|
|
988
|
+
e->opcode = UR_OP_SEND;
|
|
989
|
+
e->addr = (uint64_t)(uintptr_t)(q->data + q->off);
|
|
990
|
+
e->len = q->len - q->off;
|
|
991
|
+
e->op_flags = MSG_NOSIGNAL;
|
|
992
|
+
}
|
|
979
993
|
e->user_data = (uint64_t)(uintptr_t)q | UR_SEND;
|
|
980
994
|
q->in_flight = 1;
|
|
981
995
|
}
|
|
@@ -1033,6 +1047,11 @@ static int ur_complete(struct nish_uring_set *s, int ri, const struct ur_cqe *c,
|
|
|
1033
1047
|
ur_unlink_all(s, q);
|
|
1034
1048
|
return 0;
|
|
1035
1049
|
}
|
|
1050
|
+
if (q->how >= 0) {
|
|
1051
|
+
/* A shutdown's answer is not a send's failure: -ENOTCONN from a peer
|
|
1052
|
+
* already gone says nothing a recv will not, so it is not reported. */
|
|
1053
|
+
res = 0;
|
|
1054
|
+
}
|
|
1036
1055
|
if (res > 0) r->qbytes[slot] -= (uint32_t)res < r->qbytes[slot] ? (uint32_t)res : r->qbytes[slot];
|
|
1037
1056
|
if (res > 0 && q->off + (uint32_t)res < q->len) {
|
|
1038
1057
|
q->off += (uint32_t)res;
|
|
@@ -1388,6 +1407,23 @@ static struct ur_ring *ur_open_slot(struct nish_uring_set *s, int32_t ring, int3
|
|
|
1388
1407
|
return (r->st[slot] & ST_OPEN) ? r : NULL;
|
|
1389
1408
|
}
|
|
1390
1409
|
|
|
1410
|
+
/* Put `q` on the set's list of records and behind the slot's queue, and
|
|
1411
|
+
* submit it if it is now the head. */
|
|
1412
|
+
static void ur_enqueue(struct nish_uring_set *s, struct ur_ring *r, uint32_t slot, struct ur_send *q) {
|
|
1413
|
+
q->next = NULL;
|
|
1414
|
+
q->prev_all = NULL;
|
|
1415
|
+
q->next_all = s->all;
|
|
1416
|
+
if (s->all) s->all->prev_all = q;
|
|
1417
|
+
s->all = q;
|
|
1418
|
+
if (r->sq_last[slot]) {
|
|
1419
|
+
r->sq_last[slot]->next = q;
|
|
1420
|
+
} else {
|
|
1421
|
+
r->sq_first[slot] = q;
|
|
1422
|
+
}
|
|
1423
|
+
r->sq_last[slot] = q;
|
|
1424
|
+
ur_send_head(r, slot, s);
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1391
1427
|
/* Queue `n` bytes for (ring, slot). They are copied, so the caller's buffer
|
|
1392
1428
|
* is free on return; a failure comes back later as a SEND_FAILED event. */
|
|
1393
1429
|
int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, const uint8_t *p, int32_t n) {
|
|
@@ -1398,34 +1434,49 @@ int32_t nish_uring_send(struct nish_uring_set *s, int32_t ring, int32_t slot, co
|
|
|
1398
1434
|
struct ur_send *q = malloc(sizeof *q + (size_t)n);
|
|
1399
1435
|
if (!q) return -ENOMEM;
|
|
1400
1436
|
memcpy(q->data, p, (size_t)n);
|
|
1401
|
-
q->next = NULL;
|
|
1402
|
-
q->prev_all = NULL;
|
|
1403
|
-
q->next_all = s->all;
|
|
1404
|
-
if (s->all) s->all->prev_all = q;
|
|
1405
|
-
s->all = q;
|
|
1406
1437
|
q->slot = (uint32_t)slot;
|
|
1407
1438
|
q->gen = ST_GEN(r->st[slot]);
|
|
1408
1439
|
q->off = 0;
|
|
1409
1440
|
q->len = (uint32_t)n;
|
|
1410
1441
|
q->in_flight = 0;
|
|
1442
|
+
q->how = -1;
|
|
1411
1443
|
r->qbytes[slot] += (uint32_t)n;
|
|
1412
|
-
|
|
1413
|
-
r->sq_last[slot]->next = q;
|
|
1414
|
-
} else {
|
|
1415
|
-
r->sq_first[slot] = q;
|
|
1416
|
-
}
|
|
1417
|
-
r->sq_last[slot] = q;
|
|
1418
|
-
ur_send_head(r, (uint32_t)slot, s);
|
|
1444
|
+
ur_enqueue(s, r, (uint32_t)slot, q);
|
|
1419
1445
|
return n;
|
|
1420
1446
|
}
|
|
1421
1447
|
|
|
1448
|
+
/* Queue a shutdown of (ring, slot)'s `how` side (0 read, 1 write, 2 both)
|
|
1449
|
+
* behind the bytes already queued for it, so they reach the peer first. Its
|
|
1450
|
+
* answer is not reported: a shutdown that fails, as on a peer already gone,
|
|
1451
|
+
* leaves a connection whose next read or write says so. */
|
|
1452
|
+
int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
|
|
1453
|
+
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1454
|
+
if (!r) return -EBADF;
|
|
1455
|
+
if ((uint32_t)how > 2) return -EINVAL;
|
|
1456
|
+
struct ur_send *q = malloc(sizeof *q);
|
|
1457
|
+
if (!q) return -ENOMEM;
|
|
1458
|
+
q->slot = (uint32_t)slot;
|
|
1459
|
+
q->gen = ST_GEN(r->st[slot]);
|
|
1460
|
+
q->off = q->len = 0;
|
|
1461
|
+
q->in_flight = 0;
|
|
1462
|
+
q->how = how;
|
|
1463
|
+
ur_enqueue(s, r, (uint32_t)slot, q);
|
|
1464
|
+
return 0;
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1422
1467
|
/* Cancel the slot's recv, close the slot, and move its generation on, so
|
|
1423
1468
|
* every completion still on its way for it is stale. The freed slot goes to
|
|
1424
|
-
* the accepting job. -EAGAIN when the SQ has no room
|
|
1425
|
-
*
|
|
1469
|
+
* the accepting job. -EAGAIN, with nothing changed, when the SQ has no room
|
|
1470
|
+
* for both entries or while the slot's shutdown is in flight: a SHUTDOWN runs
|
|
1471
|
+
* on io-wq, which looks the slot's file up only when it runs, and the cancel
|
|
1472
|
+
* below cannot match a request that has no file yet, so a slot freed before
|
|
1473
|
+
* it ran could take a new connection that the shutdown then reaches
|
|
1474
|
+
* (NURING-39). */
|
|
1426
1475
|
int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1427
1476
|
struct ur_ring *r = ur_open_slot(s, ring, slot);
|
|
1428
1477
|
if (!r) return -EBADF;
|
|
1478
|
+
const struct ur_send *h = r->sq_first[slot];
|
|
1479
|
+
if (h && h->in_flight && h->how >= 0) return -EAGAIN;
|
|
1429
1480
|
uint32_t head = __atomic_load_n(r->sq_head, __ATOMIC_ACQUIRE);
|
|
1430
1481
|
if (r->sq_local_tail - head + 2 > r->sq_entries) {
|
|
1431
1482
|
(void)ur_flush(r);
|
|
@@ -1566,6 +1617,13 @@ int32_t nish_uring_close(struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
|
1566
1617
|
(void)slot;
|
|
1567
1618
|
return -38;
|
|
1568
1619
|
}
|
|
1620
|
+
int32_t nish_uring_shutdown(struct nish_uring_set *s, int32_t ring, int32_t slot, int32_t how) {
|
|
1621
|
+
(void)s;
|
|
1622
|
+
(void)ring;
|
|
1623
|
+
(void)slot;
|
|
1624
|
+
(void)how;
|
|
1625
|
+
return -38;
|
|
1626
|
+
}
|
|
1569
1627
|
int64_t nish_uring_queued(const struct nish_uring_set *s, int32_t ring, int32_t slot) {
|
|
1570
1628
|
(void)s;
|
|
1571
1629
|
(void)ring;
|
package/runtime/runtime-wasm.c
CHANGED
|
@@ -78,6 +78,9 @@ extern unsigned char __heap_base;
|
|
|
78
78
|
* past 2^32 truncated to the `uintptr_t` `memory.grow` takes: a request of
|
|
79
79
|
* 2^48 bytes grew nothing, claimed 2^48 bytes of capacity, and the next block
|
|
80
80
|
* landed at `buf + off` reduced mod 2^32, on top of live data. */
|
|
81
|
+
/* No compact-class range here (`NISH_ARENA_RANGE`, runtime.c): a wasm
|
|
82
|
+
module's memory is one 32-bit space already, so this allocator and its
|
|
83
|
+
bump are unchanged and build.sh never passes the define to this file. */
|
|
81
84
|
void *nish_arena_grow(uint64_t size) {
|
|
82
85
|
if (!nish_arena.buf) {
|
|
83
86
|
uintptr_t base = ((uintptr_t)&__heap_base + 7) & ~(uintptr_t)7;
|
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);
|