@amritk/nish-x86_64-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.
- package/INSTALL.md +74 -129
- package/bin/nish +0 -0
- package/docs/AI.md +455 -65
- package/package.json +1 -1
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +320 -33
- package/runtime/nish.h +84 -10
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/page-frame.d.ts +123 -0
- package/runtime/page-frame.mjs +736 -0
- package/runtime/page-game.d.ts +132 -0
- package/runtime/page-game.mjs +738 -0
- package/runtime/runtime-aes.c +97 -12
- package/runtime/runtime-async.c +43 -10
- package/runtime/runtime-bytes.c +88 -4
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-net.c +64 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +94 -10
- package/runtime/runtime-sandbox.c +404 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-wasm.c +437 -0
- package/runtime/runtime.c +5 -0
- package/runtime/vite.mjs +1 -1
- package/scripts/build.sh +42 -8
- package/std/README.md +78 -91
- package/std/array.ts +36 -3
- package/std/collections.ts +51 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/hmac.ts +37 -10
- package/std/crypto/p256.ts +1629 -2732
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +151 -13
- package/std/crypto/sha256.ts +34 -6
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +120 -10
- package/std/host/game.ts +273 -0
- package/std/input.ts +56 -0
- package/std/json-tokenizer.ts +344 -111
- package/std/json-typed.ts +256 -12
- package/std/json-value.ts +144 -3
- package/std/json.ts +63 -8
- package/std/net/fetch.ts +22 -4
- package/std/net/handler.ts +130 -16
- package/std/net/http-server.ts +3382 -0
- package/std/net/http1-client-codec.ts +6 -2
- package/std/net/http1-codec.ts +636 -162
- package/std/net/http1-server.ts +569 -42
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +131 -14
- package/std/net/http3-server.ts +29 -3
- package/std/net/http3.ts +180 -7
- package/std/net/postgres-async.ts +1304 -86
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +122 -7
- package/std/net/prefork.ts +195 -11
- package/std/net/quic-datagram.ts +30 -0
- package/std/net/quic-listener.ts +38 -12
- package/std/net/quic-packet.ts +19 -6
- package/std/net/quic-recovery.ts +373 -27
- package/std/net/quic-stream.ts +195 -37
- package/std/net/quic.ts +516 -22
- package/std/net/router.ts +407 -38
- package/std/net/serve.ts +1022 -110
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls/verify.ts +19 -1
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/url.ts +83 -35
- package/std/net/websocket.ts +129 -19
- package/std/net/webtransport.ts +185 -1
- package/std/raster.ts +837 -0
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/release.ts +593 -0
- package/std/secret.ts +26 -59
- package/std/synth.ts +143 -0
- package/std/text.ts +233 -0
- package/std/threads.ts +298 -0
- package/std/yaml-json.ts +368 -28
- package/std/yaml.ts +122 -27
package/runtime/runtime-aes.c
CHANGED
|
@@ -19,8 +19,9 @@
|
|
|
19
19
|
* Nish code in std/crypto/aes.ts instead: the compiler keeps that code and
|
|
20
20
|
* branches on `nish_aes_hw` at each call, so a table-free C AES here would only
|
|
21
21
|
* be a second copy of the code under review. Every other target, wasm32
|
|
22
|
-
* included,
|
|
23
|
-
*
|
|
22
|
+
* included, and a toolchain without the intrinsic headers compile
|
|
23
|
+
* `nish_aes_hw` to `return 0` and the kernels to a trap nothing reaches, so a
|
|
24
|
+
* link that names them still resolves.
|
|
24
25
|
*
|
|
25
26
|
* The round-key array, `rk`, is the `8 * (rounds + 1)` words (88 or 120 u64s)
|
|
26
27
|
* the bitsliced schedule uses, laid out differently on this path. One process
|
|
@@ -55,9 +56,12 @@
|
|
|
55
56
|
* state on the stack on purpose. The two stack buffers they do have, the
|
|
56
57
|
* keystream of a partial last block and a zero-padded last block for GHASH, are
|
|
57
58
|
* zeroed before return by volatile stores, the mechanism `nish:secret`'s wipe
|
|
58
|
-
* uses, which tests/run.js holds against `clang -O2`.
|
|
59
|
-
*
|
|
60
|
-
*
|
|
59
|
+
* uses, which tests/run.js holds against `clang -O2`. Every kernel ends with
|
|
60
|
+
* `nish_aes_clear`, which zeroes the vector registers and the caller-saved
|
|
61
|
+
* general ones, and no kernel spills a vector register: tests/run.js reads both
|
|
62
|
+
* in the `-O2` assembly. What that cannot reach -- the `debug` profile's `-O0`
|
|
63
|
+
* stack, and a signal frame taken mid-kernel -- is in
|
|
64
|
+
* docs/security/crypto-aead.md, AEAD-4.
|
|
61
65
|
*
|
|
62
66
|
* The algorithms are FIPS 197 (the key expansion, with SubWord taken from the
|
|
63
67
|
* AES instructions) and SP 800-38D (GCTR and GHASH); the GHASH multiply is the
|
|
@@ -70,12 +74,19 @@
|
|
|
70
74
|
|
|
71
75
|
#include "nish.h"
|
|
72
76
|
|
|
73
|
-
#if defined(
|
|
77
|
+
#if defined(__has_include)
|
|
78
|
+
#define NISH_AES_HAS(h) __has_include(h)
|
|
79
|
+
#else
|
|
80
|
+
#define NISH_AES_HAS(h) 0
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
#if defined(__x86_64__) && NISH_AES_HAS(<cpuid.h>) && NISH_AES_HAS(<tmmintrin.h>) && \
|
|
84
|
+
NISH_AES_HAS(<wmmintrin.h>)
|
|
74
85
|
#include <cpuid.h>
|
|
75
86
|
#include <tmmintrin.h>
|
|
76
87
|
#include <wmmintrin.h>
|
|
77
88
|
#define NISH_AES_X86 1
|
|
78
|
-
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
89
|
+
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && NISH_AES_HAS(<arm_neon.h>)
|
|
79
90
|
#include <arm_neon.h>
|
|
80
91
|
#define NISH_AES_ARM 1
|
|
81
92
|
#endif
|
|
@@ -133,6 +144,64 @@ NISH_AES_INLINE void nish_aes_wipe16(void *p) {
|
|
|
133
144
|
v[1] = 0;
|
|
134
145
|
}
|
|
135
146
|
|
|
147
|
+
/* Zeroes, as a kernel's last act, every register a kernel may leave a round
|
|
148
|
+
key, H, a counter block, keystream or GHASH state in: the vector registers a
|
|
149
|
+
call may clobber and the general ones (SubWord's key words and a partial
|
|
150
|
+
block's keystream bytes pass through those). Each kernel ends with it, so
|
|
151
|
+
what it computed leaves with the call rather than staying until later code
|
|
152
|
+
overwrites it (docs/security/crypto-aead.md, AEAD-4). The registers are named
|
|
153
|
+
by number, not by the kernel's own use, so a change of register allocation
|
|
154
|
+
cannot open a gap; the clobber list tells the compiler each is gone, and the
|
|
155
|
+
`memory` clobber keeps every store of the kernel before it. The callee-saved
|
|
156
|
+
ones are left: the epilogue restores the caller's values into them, and a
|
|
157
|
+
kernel that used them would show it in tests/run.js's reading. On x86-64
|
|
158
|
+
that is all sixteen XMM registers, or with AVX in the target (`--cpu
|
|
159
|
+
x86-64-v3`) the whole of every YMM register, which `vzeroall` clears; on
|
|
160
|
+
AArch64 v0 to v7 and v16 to v31, since AAPCS64 has the callee keep v8 to v15's
|
|
161
|
+
low halves. Every instruction is a zeroing idiom: no input, so no secret
|
|
162
|
+
reaches it, and no flag a branch reads after it. It is one function the
|
|
163
|
+
kernels jump to as their last act (a tail call at `-O2`, after their
|
|
164
|
+
epilogue), not inlined into each: the same clear in a quarter of the bytes. */
|
|
165
|
+
static __attribute__((noinline)) NISH_AES_TARGET void nish_aes_clear(void) {
|
|
166
|
+
#if defined(NISH_AES_X86)
|
|
167
|
+
#if defined(__AVX__)
|
|
168
|
+
#define NISH_AES_CLEAR_VEC "vzeroall\n\t"
|
|
169
|
+
#else
|
|
170
|
+
#define NISH_AES_CLEAR_VEC \
|
|
171
|
+
"pxor %%xmm0, %%xmm0\n\tpxor %%xmm1, %%xmm1\n\tpxor %%xmm2, %%xmm2\n\tpxor %%xmm3, %%xmm3\n\t" \
|
|
172
|
+
"pxor %%xmm4, %%xmm4\n\tpxor %%xmm5, %%xmm5\n\tpxor %%xmm6, %%xmm6\n\tpxor %%xmm7, %%xmm7\n\t" \
|
|
173
|
+
"pxor %%xmm8, %%xmm8\n\tpxor %%xmm9, %%xmm9\n\tpxor %%xmm10, %%xmm10\n\tpxor %%xmm11, %%xmm11\n\t" \
|
|
174
|
+
"pxor %%xmm12, %%xmm12\n\tpxor %%xmm13, %%xmm13\n\tpxor %%xmm14, %%xmm14\n\tpxor %%xmm15, %%xmm15\n\t"
|
|
175
|
+
#endif
|
|
176
|
+
__asm__ volatile(NISH_AES_CLEAR_VEC
|
|
177
|
+
"xorl %%eax, %%eax\n\txorl %%ecx, %%ecx\n\txorl %%edx, %%edx\n\txorl %%esi, %%esi\n\t"
|
|
178
|
+
"xorl %%edi, %%edi\n\txorl %%r8d, %%r8d\n\txorl %%r9d, %%r9d\n\txorl %%r10d, %%r10d\n\t"
|
|
179
|
+
"xorl %%r11d, %%r11d"
|
|
180
|
+
:
|
|
181
|
+
:
|
|
182
|
+
: "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11",
|
|
183
|
+
"xmm12", "xmm13", "xmm14", "xmm15", "rax", "rcx", "rdx", "rsi", "rdi", "r8", "r9", "r10", "r11",
|
|
184
|
+
"cc", "memory");
|
|
185
|
+
#undef NISH_AES_CLEAR_VEC
|
|
186
|
+
#else
|
|
187
|
+
__asm__ volatile(
|
|
188
|
+
"movi v0.16b, #0\n\tmovi v1.16b, #0\n\tmovi v2.16b, #0\n\tmovi v3.16b, #0\n\t"
|
|
189
|
+
"movi v4.16b, #0\n\tmovi v5.16b, #0\n\tmovi v6.16b, #0\n\tmovi v7.16b, #0\n\t"
|
|
190
|
+
"movi v16.16b, #0\n\tmovi v17.16b, #0\n\tmovi v18.16b, #0\n\tmovi v19.16b, #0\n\t"
|
|
191
|
+
"movi v20.16b, #0\n\tmovi v21.16b, #0\n\tmovi v22.16b, #0\n\tmovi v23.16b, #0\n\t"
|
|
192
|
+
"movi v24.16b, #0\n\tmovi v25.16b, #0\n\tmovi v26.16b, #0\n\tmovi v27.16b, #0\n\t"
|
|
193
|
+
"movi v28.16b, #0\n\tmovi v29.16b, #0\n\tmovi v30.16b, #0\n\tmovi v31.16b, #0\n\t"
|
|
194
|
+
"mov x0, xzr\n\tmov x1, xzr\n\tmov x2, xzr\n\tmov x3, xzr\n\tmov x4, xzr\n\tmov x5, xzr\n\t"
|
|
195
|
+
"mov x6, xzr\n\tmov x7, xzr\n\tmov x8, xzr\n\tmov x9, xzr\n\tmov x10, xzr\n\tmov x11, xzr\n\t"
|
|
196
|
+
"mov x12, xzr\n\tmov x13, xzr\n\tmov x14, xzr\n\tmov x15, xzr\n\tmov x16, xzr\n\tmov x17, xzr"
|
|
197
|
+
:
|
|
198
|
+
:
|
|
199
|
+
: "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24",
|
|
200
|
+
"v25", "v26", "v27", "v28", "v29", "v30", "v31", "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9",
|
|
201
|
+
"x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "memory");
|
|
202
|
+
#endif
|
|
203
|
+
}
|
|
204
|
+
|
|
136
205
|
/* The block read as a big-endian 128-bit number, as two lanes low then high:
|
|
137
206
|
the order GHASH's multiply works in. Its own inverse. */
|
|
138
207
|
NISH_AES_INLINE nish_aes_u64x2 nish_aes_reflect(nish_aes_u8x16 b) {
|
|
@@ -318,10 +387,12 @@ NISH_AES_TARGET void nish_aes_schedule(const nish_array *key, int32_t rounds, ni
|
|
|
318
387
|
nish_aes_store(rk->data + NISH_AES_HMID + 16 * i, (nish_aes_u8x16)nish_aes_mid(p));
|
|
319
388
|
p = nish_aes_mul(p, h);
|
|
320
389
|
}
|
|
390
|
+
nish_aes_clear();
|
|
321
391
|
}
|
|
322
392
|
|
|
323
393
|
NISH_AES_TARGET void nish_aes_block(const nish_array *rk, int32_t rounds, const nish_array *src, nish_array *dst) {
|
|
324
394
|
nish_aes_store(dst->data, nish_aes_encrypt(rk->data, rounds, nish_aes_load(src->data)));
|
|
395
|
+
nish_aes_clear();
|
|
325
396
|
}
|
|
326
397
|
|
|
327
398
|
NISH_AES_TARGET void nish_aes_ctr(const nish_array *rk, int32_t rounds, const nish_array *j0, const nish_array *src,
|
|
@@ -356,6 +427,7 @@ NISH_AES_TARGET void nish_aes_ctr(const nish_array *rk, int32_t rounds, const ni
|
|
|
356
427
|
for (uint32_t i = 0; i < rest; i++) out[at + i] = (char)(in[at + i] ^ ks[i]);
|
|
357
428
|
nish_aes_wipe16(ks);
|
|
358
429
|
}
|
|
430
|
+
nish_aes_clear();
|
|
359
431
|
}
|
|
360
432
|
|
|
361
433
|
NISH_AES_TARGET void nish_aes_ghash(nish_array *y, uint64_t hHi, uint64_t hLo, const nish_array *rk,
|
|
@@ -373,17 +445,29 @@ NISH_AES_TARGET void nish_aes_ghash(nish_array *y, uint64_t hHi, uint64_t hLo, c
|
|
|
373
445
|
blocks of the serial recurrence, and its products add before the one
|
|
374
446
|
reduction they share. */
|
|
375
447
|
if (blocks >= 4) {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
448
|
+
/* The four middle operands packed two to a register, H's and H^2's in
|
|
449
|
+
`hm[0]`, H^3's and H^4's in `hm[1]`: a middle product reads one lane of
|
|
450
|
+
each side, and a block's own middle operand holds the same word in both,
|
|
451
|
+
so the low lane's product or the high lane's picks the power. Six
|
|
452
|
+
registers rather than eight leave the loop enough to hold every power
|
|
453
|
+
without a spill, which no wipe could reach, on every target
|
|
454
|
+
tests/run.js reads ("the AES kernels clear ..."). */
|
|
455
|
+
nish_aes_u64x2 hp[4], hm[2];
|
|
456
|
+
for (int32_t i = 0; i < 4; i++) hp[i] = (nish_aes_u64x2)nish_aes_load(k + NISH_AES_HPOW + 16 * i);
|
|
457
|
+
for (int32_t i = 0; i < 2; i++) {
|
|
458
|
+
hm[i] = __builtin_shufflevector((nish_aes_u64x2)nish_aes_load(k + NISH_AES_HMID + 32 * i),
|
|
459
|
+
(nish_aes_u64x2)nish_aes_load(k + NISH_AES_HMID + 32 * i + 16), 0, 2);
|
|
380
460
|
}
|
|
381
461
|
for (; n + 4 <= blocks; n += 4) {
|
|
382
462
|
nish_aes_u64x2 sum[3] = {{0, 0}, {0, 0}, {0, 0}};
|
|
383
463
|
for (uint32_t j = 0; j < 4; j++) {
|
|
384
464
|
nish_aes_u64x2 x = nish_aes_reflect(nish_aes_load(in + 16 * (uint64_t)(n + j)));
|
|
385
465
|
if (j == 0) x ^= acc;
|
|
386
|
-
|
|
466
|
+
const uint32_t power = 3 - j;
|
|
467
|
+
const nish_aes_u64x2 xm = nish_aes_mid(x);
|
|
468
|
+
sum[0] ^= nish_aes_clmul_lo(x, hp[power]);
|
|
469
|
+
sum[1] ^= nish_aes_clmul_hi(x, hp[power]);
|
|
470
|
+
sum[2] ^= (power & 1) != 0 ? nish_aes_clmul_hi(xm, hm[power >> 1]) : nish_aes_clmul_lo(xm, hm[power >> 1]);
|
|
387
471
|
}
|
|
388
472
|
acc = nish_aes_mul_finish(sum);
|
|
389
473
|
}
|
|
@@ -399,6 +483,7 @@ NISH_AES_TARGET void nish_aes_ghash(nish_array *y, uint64_t hHi, uint64_t hLo, c
|
|
|
399
483
|
nish_aes_wipe16(pad);
|
|
400
484
|
}
|
|
401
485
|
nish_aes_store(y->data, (nish_aes_u8x16)__builtin_shufflevector(acc, acc, 1, 0));
|
|
486
|
+
nish_aes_clear();
|
|
402
487
|
}
|
|
403
488
|
|
|
404
489
|
#if defined(NISH_AES_X86)
|
package/runtime/runtime-async.c
CHANGED
|
@@ -49,16 +49,30 @@
|
|
|
49
49
|
* number is reused for. The duplicate is closed when the wait fires.
|
|
50
50
|
*
|
|
51
51
|
* **Regions.** Each spawned task owns a region (runtime-region.c), entered
|
|
52
|
-
* whenever the task runs and
|
|
52
|
+
* whenever the task runs and emptied when it finishes, so whatever a task
|
|
53
53
|
* allocates is given back when it ends. The driver enters and leaves around
|
|
54
|
-
* each resume (`nish_async_enter`, `nish_async_leave`).
|
|
54
|
+
* each resume (`nish_async_enter`, `nish_async_leave`). A node keeps its
|
|
55
|
+
* region from its first task on: `nish_async_done` enters it once more and
|
|
56
|
+
* rewinds it (`nish_reset_arena`) rather than freeing it, so the node's next
|
|
57
|
+
* task (a later spawn of the same async function: a node never changes
|
|
58
|
+
* kind) makes no region, and bumps from the newest chunk the last one left
|
|
59
|
+
* when that chunk is at most 64 KiB (`NISH_ASYNC_KEEP`), so a task that fits
|
|
60
|
+
* it asks `malloc` for nothing. The region keeps its handle and generation:
|
|
61
|
+
* the handle never leaves this file, and a task's region has no children
|
|
62
|
+
* (NL2351), so no copy of it is made stale by living on. A larger chunk is
|
|
63
|
+
* freed, and once the free nodes keep 16 MiB between them
|
|
64
|
+
* (`NISH_ASYNC_KEEP_TOTAL`) a finishing task keeps none and its region starts
|
|
65
|
+
* again from its first chunk size, so a burst of large tasks does not pin its
|
|
66
|
+
* high-water mark. The kept chunk is not zeroed: arena memory never is, and
|
|
67
|
+
* the compiler initialises what the language promises
|
|
68
|
+
* (docs/security/async.md, ASYNC-20).
|
|
55
69
|
*
|
|
56
70
|
* **Cancellation.** `nish_async_cancel` marks a task cancelled
|
|
57
71
|
* (`NISH_TASK_CANCELLED`, a bit beside the queued bit) and fails each wait it
|
|
58
72
|
* has pending, so every parked await answers false and the task is queued; a
|
|
59
73
|
* wait the task makes afterwards is made already failed. Nothing is unwound:
|
|
60
74
|
* the task runs on from each failed await to its end, its `using` bindings
|
|
61
|
-
* dispose as they do on any return, and `nish_async_done`
|
|
75
|
+
* dispose as they do on any return, and `nish_async_done` empties its region.
|
|
62
76
|
* `nish_async_cancel_after` arms a deadline that cancels a task when it
|
|
63
77
|
* passes, a timer in the heap that `done` takes out, and
|
|
64
78
|
* `nish_async_wait_fd_within` a descriptor wait that fails when its own
|
|
@@ -106,9 +120,9 @@ typedef struct nish_task {
|
|
|
106
120
|
struct nish_task *next; /* the ready queue's link, or the free list's */
|
|
107
121
|
int32_t kind; /* the spawned function's kind; -1 for `main` */
|
|
108
122
|
uint32_t gen; /* moves on each time the node is reused */
|
|
109
|
-
uint64_t region; /* the task's region; 0 for `main` and a
|
|
123
|
+
uint64_t region; /* the task's region; 0 for `main` and a node never yet used */
|
|
110
124
|
uint32_t index; /* the node's place in the task table, for ever */
|
|
111
|
-
uint32_t flags; /* NISH_TASK_QUEUED, NISH_TASK_CANCELLED */
|
|
125
|
+
uint32_t flags; /* NISH_TASK_QUEUED, NISH_TASK_CANCELLED; on the free list, the kept bytes */
|
|
112
126
|
} nish_task;
|
|
113
127
|
|
|
114
128
|
/* On the ready queue, so it is queued once. */
|
|
@@ -174,6 +188,16 @@ static uint32_t nish_async_fd_waits;
|
|
|
174
188
|
static int32_t nish_async_allocated;
|
|
175
189
|
static int32_t nish_async_late;
|
|
176
190
|
static int32_t nish_async_live; /* spawned tasks not yet done, each holding a region */
|
|
191
|
+
/* What a finished task's region may keep for the node's next task: its newest
|
|
192
|
+
chunk when that is no larger than NISH_ASYNC_KEEP, and nothing once the
|
|
193
|
+
free nodes together keep NISH_ASYNC_KEEP_TOTAL, so the most the free lists
|
|
194
|
+
hold is that total and one chunk. A free node's `flags` hold what its
|
|
195
|
+
region kept, from bit NISH_TASK_KEPT_SHIFT up. */
|
|
196
|
+
#define NISH_ASYNC_KEEP ((uint64_t)1 << 16)
|
|
197
|
+
#define NISH_ASYNC_KEEP_TOTAL ((uint64_t)1 << 24)
|
|
198
|
+
#define NISH_TASK_KEPT_SHIFT 8
|
|
199
|
+
static uint64_t nish_async_kept; /* bytes the free nodes' regions keep */
|
|
200
|
+
extern NISH_TLS uint64_t nish_region_chunk; /* runtime.c: the entered region's next chunk size */
|
|
177
201
|
|
|
178
202
|
enum { NISH_ASYNC_OOM, NISH_ASYNC_POLLER, NISH_ASYNC_TOO_MANY, NISH_ASYNC_WAIT };
|
|
179
203
|
#define NISH_ASYNC_MSG(text) { sizeof text - 1, text }
|
|
@@ -267,8 +291,9 @@ void *nish_async_spawn(int32_t kind, uint64_t bytes) {
|
|
|
267
291
|
slab->free = t->next;
|
|
268
292
|
t->next = NULL;
|
|
269
293
|
t->kind = kind;
|
|
294
|
+
nish_async_kept -= t->flags >> NISH_TASK_KEPT_SHIFT;
|
|
270
295
|
t->flags = 0;
|
|
271
|
-
t->region = nish_region_new();
|
|
296
|
+
if (!t->region) t->region = nish_region_new();
|
|
272
297
|
nish_async_live++;
|
|
273
298
|
return t;
|
|
274
299
|
}
|
|
@@ -291,8 +316,8 @@ void nish_async_leave(void *node, void *prev) {
|
|
|
291
316
|
|
|
292
317
|
static void nish_async_disarm(nish_task *t);
|
|
293
318
|
|
|
294
|
-
/* A task finished: take out its deadline,
|
|
295
|
-
back, a generation on. */
|
|
319
|
+
/* A task finished: take out its deadline, empty its region for the node's
|
|
320
|
+
next task and put its node back, a generation on. */
|
|
296
321
|
void nish_async_done(void *node) {
|
|
297
322
|
nish_task *t = node;
|
|
298
323
|
if (t->kind < 0) return;
|
|
@@ -313,8 +338,16 @@ void nish_async_done(void *node) {
|
|
|
313
338
|
}
|
|
314
339
|
t->flags &= ~NISH_TASK_QUEUED;
|
|
315
340
|
}
|
|
316
|
-
|
|
317
|
-
|
|
341
|
+
nish_region_enter(t->region);
|
|
342
|
+
nish_reset_arena();
|
|
343
|
+
if (nish_arena.cap > (nish_async_kept < NISH_ASYNC_KEEP_TOTAL ? NISH_ASYNC_KEEP : 0)) {
|
|
344
|
+
nish_free_arena();
|
|
345
|
+
nish_region_chunk = 8;
|
|
346
|
+
}
|
|
347
|
+
uint64_t kept = nish_arena.cap;
|
|
348
|
+
nish_region_leave(t->region, NULL);
|
|
349
|
+
nish_async_kept += kept;
|
|
350
|
+
t->flags = (uint32_t)kept << NISH_TASK_KEPT_SHIFT;
|
|
318
351
|
nish_async_live--;
|
|
319
352
|
t->gen = t->gen == UINT32_MAX ? 1 : t->gen + 1;
|
|
320
353
|
nish_slab *slab = &nish_async_slabs[t->kind];
|
package/runtime/runtime-bytes.c
CHANGED
|
@@ -176,6 +176,55 @@ static inline int64_t nish_run_resume(int cls, const uint8_t *p, int64_t i, int6
|
|
|
176
176
|
}
|
|
177
177
|
#endif
|
|
178
178
|
|
|
179
|
+
#if defined(NISH_SIMD_SSE2)
|
|
180
|
+
/* The x86 paths check a text block's UTF-8 a block at a time rather than a
|
|
181
|
+
sequence at a time, once its first stop is a high byte. A block starts on a
|
|
182
|
+
sequence boundary, so with each byte `v` beside the three before it in the
|
|
183
|
+
block (`p1`, `p2`, `p3`, zero before the block's first byte), the block's
|
|
184
|
+
bytes are well formed exactly where none of these holds:
|
|
185
|
+
|
|
186
|
+
- a byte is a continuation (0x80 to 0xbf) where no lead of the three before
|
|
187
|
+
it reaches it, or is not one where one does: a sequence cut short, a
|
|
188
|
+
stray continuation or one too many (a lead at 0xc0 reaches one byte on,
|
|
189
|
+
at 0xe0 two, at 0xf0 three);
|
|
190
|
+
- the byte is a lead no sequence has: 0xc0 or 0xc1, which only spell an
|
|
191
|
+
overlong two-byte form, or 0xf5 and up, past U+10FFFF;
|
|
192
|
+
- it is the second byte of a sequence outside the range its lead narrows it
|
|
193
|
+
to, the same four `nish_run_utf8` narrows: below 0xa0 after 0xe0 or 0x90
|
|
194
|
+
after 0xf0 (an overlong form), above 0x9f after 0xed (a surrogate) or
|
|
195
|
+
0x8f after 0xf4 (past U+10FFFF).
|
|
196
|
+
|
|
197
|
+
Each holds at a byte no later than the first stop that follows the bad
|
|
198
|
+
sequence's lead, so a block whose first ASCII stop has nothing wrong at or
|
|
199
|
+
before it is well formed up to it. This is compare and shift work alone, in
|
|
200
|
+
the spirit of Keiser and Lemire's lookup validator ("Validating UTF-8 In
|
|
201
|
+
Less Than One Instruction Per Byte", 2021) but without its table lookups,
|
|
202
|
+
which SSE2 has no byte shuffle for, and written here rather than taken from
|
|
203
|
+
any implementation of it. */
|
|
204
|
+
#define NISH_RUN_BAD(v, p1, p2, p3) \
|
|
205
|
+
(((((p1) >= 0xc0) | ((p2) >= 0xe0) | ((p3) >= 0xf0)) != (((v) & 0xc0) == 0x80)) | (((v) & 0xfe) == 0xc0) | \
|
|
206
|
+
((v) >= 0xf5) | (((p1) == 0xe0) & ((v) < 0xa0)) | (((p1) == 0xf0) & ((v) < 0x90)) | \
|
|
207
|
+
(((p1) == 0xed) & ((v) > 0x9f)) | (((p1) == 0xf4) & ((v) > 0x8f)))
|
|
208
|
+
|
|
209
|
+
typedef uint8_t nish_u8x16 __attribute__((vector_size(16)));
|
|
210
|
+
|
|
211
|
+
/* How many of the bytes before `e` start a sequence that `e` cuts off, in a
|
|
212
|
+
block whose UTF-8 is otherwise well formed: the block after starts at its
|
|
213
|
+
lead. */
|
|
214
|
+
static inline int64_t nish_run_cut(const uint8_t *p, int64_t e) {
|
|
215
|
+
if (p[e - 1] >= 0xc0) return 1;
|
|
216
|
+
if (p[e - 2] >= 0xe0) return 2;
|
|
217
|
+
return p[e - 3] >= 0xf0 ? 3 : 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* The bytes of a 16-byte block that break its UTF-8, as a movemask. */
|
|
221
|
+
static inline unsigned nish_run_bad_sse2(__m128i x) {
|
|
222
|
+
nish_u8x16 v = (nish_u8x16)x, p1 = (nish_u8x16)_mm_slli_si128(x, 1), p2 = (nish_u8x16)_mm_slli_si128(x, 2),
|
|
223
|
+
p3 = (nish_u8x16)_mm_slli_si128(x, 3);
|
|
224
|
+
return (unsigned)_mm_movemask_epi8((__m128i)NISH_RUN_BAD(v, p1, p2, p3));
|
|
225
|
+
}
|
|
226
|
+
#endif
|
|
227
|
+
|
|
179
228
|
/* Each vector path computes a mask of the block's stops -- the bytes outside
|
|
180
229
|
the class, where for text every byte with its top bit set counts as a stop
|
|
181
230
|
-- and either moves on a whole block or jumps to the first stop. */
|
|
@@ -209,21 +258,43 @@ static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to)
|
|
|
209
258
|
return nish_plain_scalar(cls, p, i, to);
|
|
210
259
|
}
|
|
211
260
|
|
|
212
|
-
|
|
261
|
+
/* Sixteen bytes a block. With `yield` set, a block with no stop at all ends
|
|
262
|
+
the walk with `~i`, its start complemented, so the AVX2 path can take back
|
|
263
|
+
a run that has gone back to ASCII; a run's end is never negative. */
|
|
264
|
+
__attribute__((noinline)) static int64_t nish_run_sse2(int cls, const uint8_t *p, int64_t from, int64_t to, int yield) {
|
|
213
265
|
int64_t i = from;
|
|
214
266
|
while (to - i >= 16) {
|
|
215
|
-
|
|
267
|
+
__m128i v = _mm_loadu_si128((const __m128i *)(p + i));
|
|
268
|
+
unsigned m = nish_run_stops_sse2(cls, v);
|
|
216
269
|
if (m == 0) {
|
|
270
|
+
if (yield) return ~i;
|
|
217
271
|
i += 16;
|
|
218
272
|
continue;
|
|
219
273
|
}
|
|
220
274
|
int64_t at = i + __builtin_ctz(m);
|
|
275
|
+
/* A text block whose first stop is a high byte: its UTF-8 checked whole,
|
|
276
|
+
up to its first ASCII stop or, with none, to its end less a sequence
|
|
277
|
+
the end cuts off. Anything wrong goes to the step below, which finds
|
|
278
|
+
the exact byte. A high byte is never an ASCII stop, so those are the
|
|
279
|
+
stops less the bytes whose sign bit is set. */
|
|
280
|
+
if (cls == NISH_RUN_TEXT && p[at] >= 0x80) {
|
|
281
|
+
unsigned s = m & ~(unsigned)_mm_movemask_epi8(v), bad = nish_run_bad_sse2(v);
|
|
282
|
+
if (s != 0 && (bad & (s ^ (s - 1))) == 0) return i + __builtin_ctz(s);
|
|
283
|
+
if (s == 0 && bad == 0) {
|
|
284
|
+
i += 16 - nish_run_cut(p, i + 16);
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
221
288
|
i = nish_run_resume(cls, p, at, to);
|
|
222
289
|
if (i == at) return at;
|
|
223
290
|
}
|
|
224
291
|
return nish_run_scalar(cls, p, i, to);
|
|
225
292
|
}
|
|
226
293
|
|
|
294
|
+
static int64_t nish_run_base(int cls, const uint8_t *p, int64_t from, int64_t to) {
|
|
295
|
+
return nish_run_sse2(cls, p, from, to, 0);
|
|
296
|
+
}
|
|
297
|
+
|
|
227
298
|
__attribute__((target("avx2"))) static inline unsigned nish_run_stops_avx2(int cls, __m256i v) {
|
|
228
299
|
if (cls == NISH_RUN_BLANK) {
|
|
229
300
|
__m256i in = _mm256_or_si256(
|
|
@@ -242,6 +313,19 @@ __attribute__((target("avx2"))) static inline unsigned nish_run_stops_avx2(int c
|
|
|
242
313
|
return (unsigned)_mm256_movemask_epi8(_mm256_or_si256(stop, v));
|
|
243
314
|
}
|
|
244
315
|
|
|
316
|
+
__attribute__((target("avx2"))) static int64_t nish_run_avx2(int cls, const uint8_t *p, int64_t from, int64_t to);
|
|
317
|
+
|
|
318
|
+
/* Past UTF-8 the base path's block check takes the run, text that has one
|
|
319
|
+
multi-byte sequence tending to have more, until a block of it is ASCII
|
|
320
|
+
again and the AVX2 path takes it back. Out of line and reached by tail
|
|
321
|
+
calls both ways, so the ASCII loop saves no registers and the stack does
|
|
322
|
+
not grow with the number of sequences. */
|
|
323
|
+
__attribute__((noinline, target("avx2"))) static int64_t nish_run_avx2_utf8(int cls, const uint8_t *p, int64_t from, int64_t to) {
|
|
324
|
+
int64_t i = nish_run_sse2(cls, p, from, to, 1);
|
|
325
|
+
if (i >= 0) return i;
|
|
326
|
+
__attribute__((musttail)) return nish_run_avx2(cls, p, ~i, to);
|
|
327
|
+
}
|
|
328
|
+
|
|
245
329
|
/* Thirty-two bytes a block, then the base path for what is left. */
|
|
246
330
|
__attribute__((target("avx2"))) static int64_t nish_run_avx2(int cls, const uint8_t *p, int64_t from, int64_t to) {
|
|
247
331
|
int64_t i = from;
|
|
@@ -252,8 +336,8 @@ __attribute__((target("avx2"))) static int64_t nish_run_avx2(int cls, const uint
|
|
|
252
336
|
continue;
|
|
253
337
|
}
|
|
254
338
|
int64_t at = i + __builtin_ctz(m);
|
|
255
|
-
|
|
256
|
-
|
|
339
|
+
if (cls != NISH_RUN_TEXT || p[at] < 0x80) return at;
|
|
340
|
+
__attribute__((musttail)) return nish_run_avx2_utf8(cls, p, at, to);
|
|
257
341
|
}
|
|
258
342
|
return nish_run_base(cls, p, i, to);
|
|
259
343
|
}
|