@amritk/nish-aarch64-linux 0.37.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +28 -16
- package/bin/nish +0 -0
- package/docs/AI.md +380 -47
- package/package.json +1 -1
- package/runtime/core/arrays.d.ts +4 -0
- package/runtime/core/arrays.mjs +33 -0
- package/runtime/core/host.d.ts +11 -0
- package/runtime/core/host.mjs +35 -0
- package/runtime/nish.d.ts +645 -66
- package/runtime/nish.h +101 -15
- package/runtime/nish.mjs +69 -6
- package/runtime/node.mjs +117 -0
- package/runtime/runtime-aes.c +12 -4
- package/runtime/runtime-async.c +267 -51
- package/runtime/runtime-checksum.c +207 -15
- package/runtime/runtime-cross.c +2 -1
- package/runtime/runtime-net.c +32 -3
- package/runtime/runtime-os.c +174 -27
- package/runtime/runtime-region.c +170 -8
- package/runtime/runtime-sandbox.c +377 -17
- package/runtime/runtime-sha256.c +11 -4
- package/runtime/runtime-text.c +1068 -0
- package/runtime/runtime-wasm.c +18 -3
- package/runtime/runtime.c +55 -961
- package/runtime/vite.mjs +48 -11
- package/scripts/build.sh +113 -36
- package/std/LICENSE-fdlibm +8 -0
- package/std/README.md +7 -4
- package/std/array.ts +266 -0
- package/std/compress/checksum.ts +13 -0
- package/std/compress/deflate.ts +0 -2
- package/std/crypto/p256.ts +247 -74
- package/std/crypto/p384.ts +3 -1
- package/std/crypto/pki.ts +35 -9
- package/std/crypto/x25519.ts +153 -64
- package/std/crypto/x509.ts +113 -10
- package/std/host/text.ts +86 -0
- package/std/json-tokenizer.ts +20 -0
- package/std/json-typed.ts +217 -8
- package/std/math.ts +743 -41
- package/std/net/dns-resolver.ts +46 -6
- package/std/net/dns.ts +50 -13
- package/std/net/fetch.ts +691 -129
- package/std/net/handler.ts +142 -16
- package/std/net/http-compress.ts +17 -9
- package/std/net/http-fields.ts +19 -4
- package/std/net/http1-codec.ts +517 -154
- package/std/net/http1-server.ts +508 -25
- package/std/net/http2-cleartext.ts +526 -0
- package/std/net/http2-tls.ts +48 -0
- package/std/net/http2.ts +84 -0
- package/std/net/postgres-async.ts +2223 -0
- package/std/net/postgres-carrier.ts +343 -10
- package/std/net/postgres.ts +129 -11
- 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 +134 -19
- package/std/net/quic-stream.ts +174 -36
- package/std/net/quic.ts +418 -18
- package/std/net/router.ts +39 -17
- package/std/net/serve.ts +867 -0
- package/std/net/tls/codec.ts +110 -32
- package/std/net/tls/record-server.ts +37 -7
- package/std/net/tls-tcp.ts +56 -1
- package/std/net/tls.ts +196 -50
- package/std/net/websocket.ts +55 -18
- package/std/net/webtransport.ts +179 -1
- package/std/regex.ts +69 -47
- package/std/region.ts +62 -11
- package/std/secret.ts +26 -59
- package/std/text.ts +464 -5
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
/* Nish runtime, the checksum half: CRC-32 (ISO 3309, gzip's, RFC 1952 §8) on
|
|
2
|
-
* the CPU's carry-less multiply or CRC instructions
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* `
|
|
2
|
+
* the CPU's carry-less multiply or CRC instructions, and Adler-32 (RFC 1950,
|
|
3
|
+
* zlib's) on SSSE3 or NEON vectors (docs/wp38-simd.md §3.1).
|
|
4
|
+
* std/compress/checksum.ts is the checksum; this is a faster body for its two
|
|
5
|
+
* private walkers, `crc32Walk` and `adler32Walk`, which the compiler calls in
|
|
6
|
+
* their place when `nish_crc32_hw` or `nish_adler32_hw` says this machine can
|
|
7
|
+
* run it (src/kernels.ts). The CRC-32 half comes first; the Adler-32 half,
|
|
8
|
+
* after it, says what its kernels are.
|
|
6
9
|
*
|
|
7
10
|
* A translation unit of its own for the reason runtime-aes.c is one: it carries
|
|
8
11
|
* its own measured `.text*` ceiling in tests/runtime-budgets.js
|
|
9
12
|
* (RUNTIME_CHECKSUM_TEXT_BUDGET), and a program that never checksums links
|
|
10
13
|
* none of it once section GC has run.
|
|
11
14
|
*
|
|
12
|
-
* Two kernels, each compiled for its extensions alone with `target(...)`,
|
|
13
|
-
* binary stays a baseline binary:
|
|
15
|
+
* Two CRC-32 kernels, each compiled for its extensions alone with `target(...)`,
|
|
16
|
+
* so the binary stays a baseline binary:
|
|
14
17
|
*
|
|
15
18
|
* x86-64 PCLMULQDQ folding, four 128-bit lanes at a step, then a Barrett
|
|
16
19
|
* reduction to 32 bits; SSE4.1 for the lane extract. Never SSE4.2's
|
|
@@ -36,7 +39,7 @@
|
|
|
36
39
|
* x^e mod P, or floor(x^64 / P), bit-reflected over 33 bits; tests/simd/crc32.c
|
|
37
40
|
* computes each one again from P and holds this file to it.
|
|
38
41
|
*
|
|
39
|
-
*
|
|
42
|
+
* Neither checksum is a secret's here, so nothing is wiped and nothing is held
|
|
40
43
|
* to constant time (docs/security/compress.md).
|
|
41
44
|
*/
|
|
42
45
|
#include <stdint.h>
|
|
@@ -44,28 +47,29 @@
|
|
|
44
47
|
#include "nish.h"
|
|
45
48
|
|
|
46
49
|
#if defined(__has_include)
|
|
47
|
-
#define
|
|
50
|
+
#define NISH_CHECKSUM_HAS(h) __has_include(h)
|
|
48
51
|
#else
|
|
49
|
-
#define
|
|
52
|
+
#define NISH_CHECKSUM_HAS(h) 0
|
|
50
53
|
#endif
|
|
51
54
|
|
|
52
|
-
#if defined(__x86_64__) &&
|
|
53
|
-
|
|
55
|
+
#if defined(__x86_64__) && NISH_CHECKSUM_HAS(<cpuid.h>) && NISH_CHECKSUM_HAS(<wmmintrin.h>) && \
|
|
56
|
+
NISH_CHECKSUM_HAS(<smmintrin.h>)
|
|
54
57
|
#include <cpuid.h>
|
|
55
58
|
#include <smmintrin.h>
|
|
56
59
|
#include <wmmintrin.h>
|
|
57
60
|
#define NISH_CRC32_X86 1
|
|
58
|
-
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ &&
|
|
61
|
+
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && NISH_CHECKSUM_HAS(<arm_acle.h>)
|
|
59
62
|
#include <arm_acle.h>
|
|
60
63
|
#define NISH_CRC32_ARM 1
|
|
61
64
|
#endif
|
|
62
65
|
|
|
63
|
-
#if defined(NISH_CRC32_X86) || defined(NISH_CRC32_ARM)
|
|
64
|
-
|
|
65
66
|
/* Named here rather than through <stdlib.h>, as runtime-aes.c names it: the
|
|
66
|
-
file compiles for aarch64 on hosts with no aarch64 libc headers.
|
|
67
|
+
file compiles for aarch64 on hosts with no aarch64 libc headers. A stub build
|
|
68
|
+
declares it and never calls it, so it asks the C library for nothing. */
|
|
67
69
|
char *getenv(const char *name);
|
|
68
70
|
|
|
71
|
+
#if defined(NISH_CRC32_X86) || defined(NISH_CRC32_ARM)
|
|
72
|
+
|
|
69
73
|
/* The fold constants (the header comment says what each is): K1 and K2 fold a
|
|
70
74
|
lane 512 bits on, K3 and K4 128 bits on, K5 64 bits on; P is the polynomial
|
|
71
75
|
and U floor(x^64 / P), both reflected, for the Barrett reduction. */
|
|
@@ -268,3 +272,191 @@ uint32_t nish_crc32_update(uint32_t crc, const nish_array *buf, int32_t off, int
|
|
|
268
272
|
}
|
|
269
273
|
|
|
270
274
|
#endif
|
|
275
|
+
|
|
276
|
+
/* ---- Adler-32 (RFC 1950 §9, zlib's) ------------------------------------------
|
|
277
|
+
|
|
278
|
+
The same seam for std/compress/checksum.ts's `adler32Walk`: `nish_adler32_hw`
|
|
279
|
+
answers once per process, `NISH_ADLER32=soft` pins the Nish loop, and the
|
|
280
|
+
compiler calls `nish_adler32_update` in the walk's place when the answer is 1.
|
|
281
|
+
|
|
282
|
+
Two kernels on one blocking: 32-byte blocks, at most NMAX / 32 of them between
|
|
283
|
+
reductions, s2 given the earlier blocks' s1 once per block (summed in `ps` and
|
|
284
|
+
shifted once a stretch) and the block's bytes weighted 32..1, s1 the bytes.
|
|
285
|
+
|
|
286
|
+
x86-64 SSSE3, behind `target("ssse3")` and cpuid leaf 1 ECX bit 9:
|
|
287
|
+
`pmaddubsw` then `pmaddwd` for the weighted sum, `psadbw` for s1.
|
|
288
|
+
AArch64 NEON, baseline on every AArch64, so no target and no question
|
|
289
|
+
asked: pairwise widening adds for s1, the block's byte columns
|
|
290
|
+
summed in 16 bits (173 blocks * 255 fits) and weighted once a
|
|
291
|
+
stretch by widening multiply-adds.
|
|
292
|
+
|
|
293
|
+
Elsewhere (wasm32, i686, armv7, a big-endian AArch64, a toolchain without the
|
|
294
|
+
headers) the stub below. Written from RFC 1950 and the Intel and Arm
|
|
295
|
+
intrinsic references (bench/adler32/kernel.c measured them first); the
|
|
296
|
+
blocking is the standard technique zlib-ng and Chromium also use, and no
|
|
297
|
+
upstream code was copied. A checksum, not a secret: nothing is wiped. */
|
|
298
|
+
|
|
299
|
+
#if defined(__x86_64__) && NISH_CHECKSUM_HAS(<cpuid.h>) && NISH_CHECKSUM_HAS(<tmmintrin.h>)
|
|
300
|
+
#include <cpuid.h>
|
|
301
|
+
#include <tmmintrin.h>
|
|
302
|
+
#define NISH_ADLER32_X86 1
|
|
303
|
+
#elif defined(__aarch64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && NISH_CHECKSUM_HAS(<arm_neon.h>)
|
|
304
|
+
#include <arm_neon.h>
|
|
305
|
+
#define NISH_ADLER32_ARM 1
|
|
306
|
+
#endif
|
|
307
|
+
|
|
308
|
+
#if defined(NISH_ADLER32_X86) || defined(NISH_ADLER32_ARM)
|
|
309
|
+
|
|
310
|
+
#define NISH_ADLER32_BASE 65521u
|
|
311
|
+
/* The most bytes before s2 can pass 2^32 - 1 from s1, s2 < BASE (zlib's NMAX). */
|
|
312
|
+
#define NISH_ADLER32_NMAX 5552
|
|
313
|
+
#define NISH_ADLER32_BLOCK 32
|
|
314
|
+
|
|
315
|
+
#define NISH_ADLER32_UNSET 0
|
|
316
|
+
#define NISH_ADLER32_KERNEL 1
|
|
317
|
+
#define NISH_ADLER32_SOFT 2
|
|
318
|
+
|
|
319
|
+
/* The bytes after the last whole block, fewer than 32, then the reduction. */
|
|
320
|
+
static uint32_t nish_adler32_tail(uint32_t s1, uint32_t s2, const uint8_t *p, uint64_t n) {
|
|
321
|
+
for (uint64_t i = 0; i < n; i++) {
|
|
322
|
+
s1 += p[i];
|
|
323
|
+
s2 += s1;
|
|
324
|
+
}
|
|
325
|
+
return ((s2 % NISH_ADLER32_BASE) << 16) | (s1 % NISH_ADLER32_BASE);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
#if defined(NISH_ADLER32_X86)
|
|
329
|
+
|
|
330
|
+
#define NISH_ADLER32_TARGET __attribute__((target("ssse3")))
|
|
331
|
+
|
|
332
|
+
static inline __attribute__((always_inline)) NISH_ADLER32_TARGET uint32_t nish_adler32_hsum(__m128i v) {
|
|
333
|
+
v = _mm_add_epi32(v, _mm_shuffle_epi32(v, _MM_SHUFFLE(1, 0, 3, 2)));
|
|
334
|
+
v = _mm_add_epi32(v, _mm_shuffle_epi32(v, _MM_SHUFFLE(2, 3, 0, 1)));
|
|
335
|
+
return (uint32_t)_mm_cvtsi128_si32(v);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/* The weighted sums cannot overflow a lane: a lane's share of s2 is at most
|
|
339
|
+
s2's whole, which NMAX bounds even from an unreduced 0xffff seed. */
|
|
340
|
+
static NISH_ADLER32_TARGET uint32_t nish_adler32_ssse3(uint32_t adler, const uint8_t *p, uint64_t n) {
|
|
341
|
+
uint32_t s1 = adler & 0xffff, s2 = adler >> 16;
|
|
342
|
+
const __m128i tap1 = _mm_setr_epi8(32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17);
|
|
343
|
+
const __m128i tap2 = _mm_setr_epi8(16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
|
|
344
|
+
const __m128i zero = _mm_setzero_si128();
|
|
345
|
+
const __m128i ones = _mm_set1_epi16(1);
|
|
346
|
+
while (n >= NISH_ADLER32_BLOCK) {
|
|
347
|
+
uint64_t blocks = n / NISH_ADLER32_BLOCK;
|
|
348
|
+
if (blocks > NISH_ADLER32_NMAX / NISH_ADLER32_BLOCK) blocks = NISH_ADLER32_NMAX / NISH_ADLER32_BLOCK;
|
|
349
|
+
n -= blocks * NISH_ADLER32_BLOCK;
|
|
350
|
+
s2 += s1 * (uint32_t)(blocks * NISH_ADLER32_BLOCK);
|
|
351
|
+
__m128i ps = zero, vs1 = zero, vs2 = zero;
|
|
352
|
+
do {
|
|
353
|
+
const __m128i b1 = _mm_loadu_si128((const __m128i *)p);
|
|
354
|
+
const __m128i b2 = _mm_loadu_si128((const __m128i *)(p + 16));
|
|
355
|
+
ps = _mm_add_epi32(ps, vs1);
|
|
356
|
+
vs1 = _mm_add_epi32(vs1, _mm_sad_epu8(b1, zero));
|
|
357
|
+
vs1 = _mm_add_epi32(vs1, _mm_sad_epu8(b2, zero));
|
|
358
|
+
vs2 = _mm_add_epi32(vs2, _mm_madd_epi16(_mm_maddubs_epi16(b1, tap1), ones));
|
|
359
|
+
vs2 = _mm_add_epi32(vs2, _mm_madd_epi16(_mm_maddubs_epi16(b2, tap2), ones));
|
|
360
|
+
p += NISH_ADLER32_BLOCK;
|
|
361
|
+
} while (--blocks);
|
|
362
|
+
vs2 = _mm_add_epi32(vs2, _mm_slli_epi32(ps, 5));
|
|
363
|
+
s1 = (s1 + nish_adler32_hsum(vs1)) % NISH_ADLER32_BASE;
|
|
364
|
+
s2 = (s2 + nish_adler32_hsum(vs2)) % NISH_ADLER32_BASE;
|
|
365
|
+
}
|
|
366
|
+
return nish_adler32_tail(s1, s2, p, n);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
static int32_t nish_adler32_cpu(void) {
|
|
370
|
+
unsigned a, b, c, d;
|
|
371
|
+
return __get_cpuid(1, &a, &b, &c, &d) && (c & bit_SSSE3) != 0 ? NISH_ADLER32_KERNEL : NISH_ADLER32_SOFT;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
#else
|
|
375
|
+
|
|
376
|
+
static uint32_t nish_adler32_neon(uint32_t adler, const uint8_t *p, uint64_t n) {
|
|
377
|
+
static const uint16_t taps[32] = {32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
|
|
378
|
+
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
|
|
379
|
+
uint32_t s1 = adler & 0xffff, s2 = adler >> 16;
|
|
380
|
+
while (n >= NISH_ADLER32_BLOCK) {
|
|
381
|
+
uint64_t blocks = n / NISH_ADLER32_BLOCK;
|
|
382
|
+
if (blocks > NISH_ADLER32_NMAX / NISH_ADLER32_BLOCK) blocks = NISH_ADLER32_NMAX / NISH_ADLER32_BLOCK;
|
|
383
|
+
n -= blocks * NISH_ADLER32_BLOCK;
|
|
384
|
+
s2 += s1 * (uint32_t)(blocks * NISH_ADLER32_BLOCK);
|
|
385
|
+
uint32x4_t ps = vdupq_n_u32(0), vs1 = vdupq_n_u32(0);
|
|
386
|
+
uint16x8_t c1 = vdupq_n_u16(0), c2 = c1, c3 = c1, c4 = c1;
|
|
387
|
+
do {
|
|
388
|
+
const uint8x16_t b1 = vld1q_u8(p);
|
|
389
|
+
const uint8x16_t b2 = vld1q_u8(p + 16);
|
|
390
|
+
ps = vaddq_u32(ps, vs1);
|
|
391
|
+
vs1 = vpadalq_u16(vs1, vpadalq_u8(vpaddlq_u8(b1), b2));
|
|
392
|
+
c1 = vaddw_u8(c1, vget_low_u8(b1));
|
|
393
|
+
c2 = vaddw_u8(c2, vget_high_u8(b1));
|
|
394
|
+
c3 = vaddw_u8(c3, vget_low_u8(b2));
|
|
395
|
+
c4 = vaddw_u8(c4, vget_high_u8(b2));
|
|
396
|
+
p += NISH_ADLER32_BLOCK;
|
|
397
|
+
} while (--blocks);
|
|
398
|
+
uint32x4_t vs2 = vshlq_n_u32(ps, 5);
|
|
399
|
+
vs2 = vmlal_u16(vs2, vget_low_u16(c1), vld1_u16(taps));
|
|
400
|
+
vs2 = vmlal_u16(vs2, vget_high_u16(c1), vld1_u16(taps + 4));
|
|
401
|
+
vs2 = vmlal_u16(vs2, vget_low_u16(c2), vld1_u16(taps + 8));
|
|
402
|
+
vs2 = vmlal_u16(vs2, vget_high_u16(c2), vld1_u16(taps + 12));
|
|
403
|
+
vs2 = vmlal_u16(vs2, vget_low_u16(c3), vld1_u16(taps + 16));
|
|
404
|
+
vs2 = vmlal_u16(vs2, vget_high_u16(c3), vld1_u16(taps + 20));
|
|
405
|
+
vs2 = vmlal_u16(vs2, vget_low_u16(c4), vld1_u16(taps + 24));
|
|
406
|
+
vs2 = vmlal_u16(vs2, vget_high_u16(c4), vld1_u16(taps + 28));
|
|
407
|
+
s1 = (s1 + vaddvq_u32(vs1)) % NISH_ADLER32_BASE;
|
|
408
|
+
s2 = (s2 + vaddvq_u32(vs2)) % NISH_ADLER32_BASE;
|
|
409
|
+
}
|
|
410
|
+
return nish_adler32_tail(s1, s2, p, n);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
static int32_t nish_adler32_cpu(void) {
|
|
414
|
+
return NISH_ADLER32_KERNEL;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
#endif
|
|
418
|
+
|
|
419
|
+
/* Fixed by the first call, as nish_crc32_answer is. */
|
|
420
|
+
static int32_t nish_adler32_answer;
|
|
421
|
+
|
|
422
|
+
static int32_t nish_adler32_path(void) {
|
|
423
|
+
int32_t answer = __atomic_load_n(&nish_adler32_answer, __ATOMIC_RELAXED);
|
|
424
|
+
if (answer == NISH_ADLER32_UNSET) {
|
|
425
|
+
const char *pin = getenv("NISH_ADLER32");
|
|
426
|
+
int soft = pin != 0 && pin[0] == 's' && pin[1] == 'o' && pin[2] == 'f' && pin[3] == 't' && pin[4] == 0;
|
|
427
|
+
answer = soft ? NISH_ADLER32_SOFT : nish_adler32_cpu();
|
|
428
|
+
__atomic_store_n(&nish_adler32_answer, answer, __ATOMIC_RELAXED);
|
|
429
|
+
}
|
|
430
|
+
return answer;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
int32_t nish_adler32_hw(void) {
|
|
434
|
+
return nish_adler32_path() != NISH_ADLER32_SOFT;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
uint32_t nish_adler32_update(uint32_t adler, const nish_array *buf, int32_t off, int32_t len) {
|
|
438
|
+
/* The walk hands an empty window's value back as it came, reduced or not;
|
|
439
|
+
the kernels always reduce, so an empty window never reaches them. */
|
|
440
|
+
if (len == 0) return adler;
|
|
441
|
+
const uint8_t *p = (const uint8_t *)buf->data + off;
|
|
442
|
+
#if defined(NISH_ADLER32_X86)
|
|
443
|
+
return nish_adler32_ssse3(adler, p, (uint64_t)len);
|
|
444
|
+
#else
|
|
445
|
+
return nish_adler32_neon(adler, p, (uint64_t)len);
|
|
446
|
+
#endif
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
#else
|
|
450
|
+
|
|
451
|
+
/* No kernel for this target: the Nish loop always, and a kernel that a program
|
|
452
|
+
whose every call is behind `nish_adler32_hw` never reaches. */
|
|
453
|
+
int32_t nish_adler32_hw(void) {
|
|
454
|
+
return 0;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
uint32_t nish_adler32_update(uint32_t adler, const nish_array *buf, int32_t off, int32_t len) {
|
|
458
|
+
(void)adler, (void)buf, (void)off, (void)len;
|
|
459
|
+
__builtin_trap();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#endif
|
package/runtime/runtime-cross.c
CHANGED
|
@@ -77,7 +77,8 @@ static void *nish_cross_block(uint64_t size) {
|
|
|
77
77
|
* value. */
|
|
78
78
|
static int nish_since(uint64_t mark, uintptr_t p) {
|
|
79
79
|
for (const nish_xchunk *c = (const nish_xchunk *)nish_arena.chunks; c; c = c->next) {
|
|
80
|
-
|
|
80
|
+
/* Bit 0 of `cap` marks a carved chunk (runtime.c's NISH_CHUNK_CARVED). */
|
|
81
|
+
uintptr_t base = (uintptr_t)(c + 1), end = base + (c->cap & ~(uint64_t)1);
|
|
81
82
|
int marked = mark >= base && mark <= end;
|
|
82
83
|
if (p >= (marked ? (uintptr_t)mark : base) && p < end) return 1;
|
|
83
84
|
if (marked) return 0;
|
package/runtime/runtime-net.c
CHANGED
|
@@ -259,10 +259,31 @@ static int32_t nish_net_bound(const nish_str *host, int32_t port, int type, int3
|
|
|
259
259
|
{BPF_RET | BPF_A, 0, 0, 0},
|
|
260
260
|
};
|
|
261
261
|
static const struct sock_fprog steer = {2, cpu};
|
|
262
|
+
/* `udpBind` flag 4's program (docs/h3-multicore.md §3.1): byte 0 of the
|
|
263
|
+
Destination Connection ID, from byte 1 of a short header or byte 6 of a
|
|
264
|
+
long one, modulo the group's count in bits 8-15 of the flags, so every
|
|
265
|
+
datagram names a socket by its DCID alone and the kernel's address hash
|
|
266
|
+
is never used. A load past a runt's end returns 0: socket 0. The count
|
|
267
|
+
is the call's, so the program is built here and not shared. */
|
|
268
|
+
static const struct sock_filter dcid0[] = {
|
|
269
|
+
{BPF_LD | BPF_B | BPF_ABS, 0, 0, 0},
|
|
270
|
+
{BPF_JMP | BPF_JSET | BPF_K, 2, 0, 0x80},
|
|
271
|
+
{BPF_LD | BPF_B | BPF_ABS, 0, 0, 1},
|
|
272
|
+
{BPF_JMP | BPF_JA, 0, 0, 1},
|
|
273
|
+
{BPF_LD | BPF_B | BPF_ABS, 0, 0, 6},
|
|
274
|
+
{BPF_ALU | BPF_MOD | BPF_K, 0, 0, 0},
|
|
275
|
+
{BPF_RET | BPF_A, 0, 0, 0},
|
|
276
|
+
};
|
|
277
|
+
struct sock_filter dcid[7];
|
|
278
|
+
memcpy(dcid, dcid0, sizeof dcid);
|
|
279
|
+
dcid[5].k = (uint32_t)((arg >> 8) & 255);
|
|
280
|
+
const struct sock_fprog quic = {7, dcid};
|
|
281
|
+
int udp_steer = type == SOCK_DGRAM && (arg & 4);
|
|
262
282
|
#endif
|
|
263
283
|
if (bad != 0 || bind(fd, &s.sa, n) != 0 || (type == SOCK_STREAM && listen(fd, arg) != 0)
|
|
264
284
|
#if defined(__linux__)
|
|
265
285
|
|| ((opts & 4) && setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &steer, sizeof steer) != 0)
|
|
286
|
+
|| (udp_steer && setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &quic, sizeof quic) != 0)
|
|
266
287
|
#endif
|
|
267
288
|
) {
|
|
268
289
|
int e = errno;
|
|
@@ -519,11 +540,19 @@ int32_t nish_net_mss(int32_t fd) {
|
|
|
519
540
|
|
|
520
541
|
/* `udpBind(host, port, flags)`: a datagram socket, non-blocking and
|
|
521
542
|
close-on-exec, bound like `tcpListen`'s, with `SO_REUSEPORT` for flag 1
|
|
522
|
-
and `UDP_GRO` for flag 2
|
|
543
|
+
and `UDP_GRO` for flag 2. Flag 4 steers QUIC (docs/h3-multicore.md): it
|
|
544
|
+
needs flag 1 and a group count N from 1 to 255 in bits 8-15, refuses
|
|
545
|
+
flag 2, and attaches a reuseport program that hands each datagram to the
|
|
546
|
+
group's socket at index (DCID byte 0) mod N. A refused attach closes the
|
|
547
|
+
socket and answers the error. Any other bit, flag 4 without 1, with 2 or
|
|
548
|
+
with a count of 0, or a count without 4, is -22; Linux alone has 2 and 4,
|
|
549
|
+
elsewhere they are -95. */
|
|
523
550
|
int32_t nish_udp_bind(const nish_str *host, int32_t port, int32_t flags) {
|
|
524
|
-
|
|
551
|
+
/* Without flag 4, 0 to 3; with it, flag 1 alone below it and a count. */
|
|
552
|
+
if ((flags & 4) ? (uint32_t)flags > 0xffff || flags < 0x100 || (flags & 0xfb) != 1 : (uint32_t)flags > 3)
|
|
553
|
+
return -22;
|
|
525
554
|
#if !defined(__linux__)
|
|
526
|
-
if (flags &
|
|
555
|
+
if (flags & 6) return -95;
|
|
527
556
|
#endif
|
|
528
557
|
return nish_net_bound(host, port, SOCK_DGRAM, flags, flags & 1);
|
|
529
558
|
}
|
package/runtime/runtime-os.c
CHANGED
|
@@ -250,6 +250,84 @@ int32_t nish_write_file_bytes(const nish_str *path, const nish_array *bytes) {
|
|
|
250
250
|
return r < 0 ? r : (int32_t)bytes->len;
|
|
251
251
|
}
|
|
252
252
|
|
|
253
|
+
/* ---- Streaming reads (WP44, docs/wp44-streaming-files.md): `fileOpen`,
|
|
254
|
+
`fileReadAt`, `fileSize` and `fileClose`. A program reads a regular file a
|
|
255
|
+
window at a time into a `u8[]` it owns, so nothing here allocates and the
|
|
256
|
+
arena follows the window, not the file. Every failure is a negated errno in
|
|
257
|
+
Linux's numbering whatever the platform, through `nish_fs_err`, so Darwin's
|
|
258
|
+
and WASI's numbers (WASI's `ENOENT` is 44) come out as Linux's and the Node
|
|
259
|
+
shim, which maps `err.code` by the same names, answers the same bytes. */
|
|
260
|
+
|
|
261
|
+
/* The errno set of docs/wp44-streaming-files.md §3, by name; any other code,
|
|
262
|
+
WASI's `ENOTCAPABLE` among them (§9 Q7), is -5, as the shim collapses it. */
|
|
263
|
+
static int32_t nish_fs_err(int e) {
|
|
264
|
+
switch (e) {
|
|
265
|
+
case ENOENT: return -2;
|
|
266
|
+
case ENXIO: return -6;
|
|
267
|
+
case EBADF: return -9;
|
|
268
|
+
case EACCES: return -13;
|
|
269
|
+
case ENOTDIR: return -20;
|
|
270
|
+
case EISDIR: return -21;
|
|
271
|
+
case EINVAL: return -22;
|
|
272
|
+
case ENFILE: return -23;
|
|
273
|
+
case EMFILE: return -24;
|
|
274
|
+
case ESPIPE: return -29;
|
|
275
|
+
case ENAMETOOLONG: return -36;
|
|
276
|
+
case ELOOP: return -40;
|
|
277
|
+
default: return -5;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/* `fileOpen(path)`: a descriptor for a regular file, or the errno. A path
|
|
282
|
+
holding a NUL, or none at all, answers -2, as a missing file does, before
|
|
283
|
+
any system call (RT-3): `nish_cpath` gives the first as "", and WASI would
|
|
284
|
+
resolve "" to the preopened directory rather than refuse it. `O_CLOEXEC`
|
|
285
|
+
keeps the descriptor out of a spawned child (RT-12); `O_NONBLOCK` only stops
|
|
286
|
+
a FIFO with no writer from hanging the open, and changes nothing for the
|
|
287
|
+
regular file that is all this hands out: a directory is closed again and
|
|
288
|
+
answers -21, anything else -22 (§9 Q6). `fstat` on the descriptor, so the
|
|
289
|
+
answer is about the file opened. */
|
|
290
|
+
int32_t nish_file_open(const nish_str *path) {
|
|
291
|
+
const char *p = nish_cpath(path);
|
|
292
|
+
if (p != path->data || path->len == 0) return -2;
|
|
293
|
+
int fd = open(p, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
|
|
294
|
+
if (fd < 0) return nish_fs_err(errno);
|
|
295
|
+
struct stat st;
|
|
296
|
+
int32_t r = fstat(fd, &st) != 0 ? nish_fs_err(errno) : S_ISREG(st.st_mode) ? 0 : S_ISDIR(st.st_mode) ? -21 : -22;
|
|
297
|
+
if (r == 0) return fd;
|
|
298
|
+
close(fd);
|
|
299
|
+
return r;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/* `fileReadAt(fd, buf, off, len, pos)`: `pread` into `buf[off, off + len)`,
|
|
303
|
+
which the compiled call has range-checked as `netRead`'s. A `pos` below 0 or
|
|
304
|
+
a `pos + len` past 2^63 - 1 is -22 before the kernel is asked, as Linux
|
|
305
|
+
would answer the second and Node could not be trusted to answer the first;
|
|
306
|
+
`len` 0 is 0. A negative `fd` is -9 (`EBADF`) here and in the two calls
|
|
307
|
+
below, before the kernel is asked: WASI's descriptor is unsigned, so -1
|
|
308
|
+
would reach the host as 2^32 - 1, which uvwasi answers -22. An `EINTR` before a byte moves is retried (RT-13), and the
|
|
309
|
+
count may still be short of `len` before the end. */
|
|
310
|
+
int32_t nish_file_read_at(int32_t fd, nish_array *buf, int64_t off, int64_t len, int64_t pos) {
|
|
311
|
+
if (pos < 0 || len > INT64_MAX - pos) return -22;
|
|
312
|
+
if (len == 0) return 0;
|
|
313
|
+
if (fd < 0) return -9;
|
|
314
|
+
ssize_t n;
|
|
315
|
+
while ((n = pread(fd, buf->data + off, (size_t)len, (off_t)pos)) < 0 && errno == EINTR) {
|
|
316
|
+
}
|
|
317
|
+
return n < 0 ? nish_fs_err(errno) : (int32_t)n;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/* `fileSize(fd)`: `fstat`'s size now, which a later read may find changed. */
|
|
321
|
+
int64_t nish_file_size(int32_t fd) {
|
|
322
|
+
if (fd < 0) return -9;
|
|
323
|
+
struct stat st;
|
|
324
|
+
return fstat(fd, &st) == 0 ? (int64_t)st.st_size : nish_fs_err(errno);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/* `fileClose(fd)`: never retried on `EINTR`, because Linux has released the
|
|
328
|
+
number by then and a retry could close a descriptor opened since. */
|
|
329
|
+
int32_t nish_file_close(int32_t fd) { return fd < 0 ? -9 : close(fd) == 0 ? 0 : nish_fs_err(errno); }
|
|
330
|
+
|
|
253
331
|
/* ---- Files and directories with an explicit mode (docs/security/runtime.md,
|
|
254
332
|
RT-24 to RT-26): `writeFileSyncMode`, `createFileExclusiveSync` and
|
|
255
333
|
`mkdirSyncMode`. Each answers a value and never exits, as the writers just
|
|
@@ -354,6 +432,13 @@ _Bool nish_mkdir(const nish_str *path) {
|
|
|
354
432
|
return mkdir(nish_cpath(path), 0777) == 0 || nish_is_dir(path);
|
|
355
433
|
}
|
|
356
434
|
|
|
435
|
+
#ifdef __linux__
|
|
436
|
+
/* Set by `nish_sandbox` (runtime-sandbox.c) when the program has an exec
|
|
437
|
+
allowlist (SBX-1); null otherwise, so a spawn outside `--sandbox` is the
|
|
438
|
+
one `posix_spawnp` call it always was. */
|
|
439
|
+
int32_t (*nish_spawn_sandboxed)(const char *file, const int *dup, int base, int n, const void *actions, char **argv);
|
|
440
|
+
#endif
|
|
441
|
+
|
|
357
442
|
/* `posix_spawnp` is one libc call where fork/execvp/waitpid would be three,
|
|
358
443
|
it searches PATH, and glibc reports a failed exec through its return
|
|
359
444
|
value rather than through a child that has already run.
|
|
@@ -366,7 +451,9 @@ _Bool nish_mkdir(const nish_str *path) {
|
|
|
366
451
|
that spawns nothing loses it to `--gc-sections`. The answer is the child's
|
|
367
452
|
pid, or a negative errno: -EINVAL for an empty argv or an argument holding a
|
|
368
453
|
NUL, and `posix_spawnp`'s own error otherwise (-ENOENT for a command that is
|
|
369
|
-
not on PATH, on glibc).
|
|
454
|
+
not on PATH, on glibc). Under `--sandbox` with an exec allowlist the child
|
|
455
|
+
execs the checked file through `/proc/self/fd/N`, not the name in `argv[0]`,
|
|
456
|
+
and that is the one exec its subtree gets. */
|
|
370
457
|
static int32_t nish_spawn_start(const nish_array *argv, const nish_str *out, const nish_str *err, const int *dup,
|
|
371
458
|
int base, int n) {
|
|
372
459
|
#ifdef __wasi__
|
|
@@ -413,6 +500,16 @@ static int32_t nish_spawn_start(const nish_array *argv, const nish_str *out, con
|
|
|
413
500
|
if (to[k]) posix_spawn_file_actions_addopen(&fa, k + 1, nish_cpath(to[k]), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0644);
|
|
414
501
|
}
|
|
415
502
|
pid_t pid;
|
|
503
|
+
#ifdef __linux__
|
|
504
|
+
/* The sandbox starts the child itself: it executes the file checked, and
|
|
505
|
+
nothing after. It is told which descriptors the actions read and write,
|
|
506
|
+
and keeps every one of its own off those numbers. */
|
|
507
|
+
if (nish_spawn_sandboxed) {
|
|
508
|
+
int32_t started = nish_spawn_sandboxed(v[0], dup, base, n, &fa, v);
|
|
509
|
+
posix_spawn_file_actions_destroy(&fa);
|
|
510
|
+
return started;
|
|
511
|
+
}
|
|
512
|
+
#endif
|
|
416
513
|
failed = posix_spawnp(&pid, v[0], &fa, 0, v, environ);
|
|
417
514
|
posix_spawn_file_actions_destroy(&fa);
|
|
418
515
|
return failed ? -failed : (int32_t)pid;
|
|
@@ -510,6 +607,28 @@ int32_t nish_spawn_with_fds(const nish_array *argv, const nish_array *fds) {
|
|
|
510
607
|
#endif
|
|
511
608
|
}
|
|
512
609
|
|
|
610
|
+
#define NISH_PIPED_CAP (16u << 20)
|
|
611
|
+
#ifndef __wasi__
|
|
612
|
+
static int nish_pipe(int *p) {
|
|
613
|
+
#ifdef __linux__
|
|
614
|
+
if (pipe2(p, O_CLOEXEC)) return -1;
|
|
615
|
+
#else
|
|
616
|
+
if (pipe(p)) return -1;
|
|
617
|
+
#endif
|
|
618
|
+
for (int k = 0; k < 2; k++) {
|
|
619
|
+
#ifdef __linux__
|
|
620
|
+
if (p[k] > 2) continue;
|
|
621
|
+
#endif
|
|
622
|
+
int moved = fcntl(p[k], F_DUPFD_CLOEXEC, 3);
|
|
623
|
+
close(p[k]);
|
|
624
|
+
p[k] = moved;
|
|
625
|
+
if (moved < 0) return -1;
|
|
626
|
+
}
|
|
627
|
+
return 0;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
#endif
|
|
631
|
+
|
|
513
632
|
/* `spawnPiped(argv, stdin, stdout, stderr)`: run the child as `nish_spawn`
|
|
514
633
|
does, with `stdin`'s bytes on its standard input and its standard output
|
|
515
634
|
and error appended to `stdout` and `stderr`, and answer its status. -1 is
|
|
@@ -542,36 +661,32 @@ int32_t nish_spawn_with_fds(const nish_array *argv, const nish_array *fds) {
|
|
|
542
661
|
appends to it without feeding the output back in, and the pointer at each
|
|
543
662
|
write, because that array moves when it grows and its stdin bytes stay
|
|
544
663
|
its first `n`. */
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
}
|
|
562
|
-
return 0;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
#endif
|
|
566
|
-
|
|
567
|
-
int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err) {
|
|
664
|
+
/* `spawnPipedWithin(argv, stdin, stdout, stderr, timeoutMs)`: the same run,
|
|
665
|
+
killed when `timeout_ms` milliseconds have passed since it started, and a
|
|
666
|
+
negative `timeout_ms` is no deadline, as `pollWait`'s is. The answers are
|
|
667
|
+
`spawnPiped`'s, plus -3 for a run the deadline ended: the child is killed
|
|
668
|
+
with SIGKILL and reaped, and the arrays keep the bytes read before the kill.
|
|
669
|
+
|
|
670
|
+
The deadline is CLOCK_MONOTONIC's, so a wall clock set back cannot stretch
|
|
671
|
+
it. Each `poll` waits the milliseconds left, rounded up, and an EINTR goes
|
|
672
|
+
round the loop to recompute them. A child that closes both outputs and then
|
|
673
|
+
hangs leaves the loop with no stream to wait on, so with a deadline the
|
|
674
|
+
final wait is a `WNOHANG` poll with a backoff of 1 ms doubling to 64 ms, up
|
|
675
|
+
to the deadline. Only the child's pid is killed, never a group: a grandchild
|
|
676
|
+
that holds a pipe open keeps the loop waiting until the deadline, and is not
|
|
677
|
+
killed with the child (docs/security/runtime.md, RT-49). */
|
|
678
|
+
int32_t nish_spawn_piped_within(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err,
|
|
679
|
+
int32_t timeout_ms) {
|
|
568
680
|
#ifdef __wasi__
|
|
569
681
|
(void)argv;
|
|
570
682
|
(void)in;
|
|
571
683
|
(void)out;
|
|
572
684
|
(void)err;
|
|
685
|
+
(void)timeout_ms;
|
|
573
686
|
return -1;
|
|
574
687
|
#else
|
|
688
|
+
int timed = timeout_ms >= 0;
|
|
689
|
+
int64_t deadline = timed ? nish_monotonic_nanos() + (int64_t)timeout_ms * 1000000 : 0;
|
|
575
690
|
uint64_t n = in->len, sent = 0, got[3] = { 0, 0, 0 };
|
|
576
691
|
int p[6] = { -1, -1, -1, -1, -1, -1 };
|
|
577
692
|
int32_t rc = -1;
|
|
@@ -604,7 +719,16 @@ int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_arra
|
|
|
604
719
|
}
|
|
605
720
|
nish_array *to[3] = { 0, out, err };
|
|
606
721
|
while (!rc && (f[0].fd >= 0 || f[1].fd >= 0 || f[2].fd >= 0)) {
|
|
607
|
-
|
|
722
|
+
int wait = -1;
|
|
723
|
+
if (timed) {
|
|
724
|
+
int64_t left = deadline - nish_monotonic_nanos();
|
|
725
|
+
if (left <= 0) {
|
|
726
|
+
rc = -3;
|
|
727
|
+
break;
|
|
728
|
+
}
|
|
729
|
+
wait = (int)((left + 999999) / 1000000);
|
|
730
|
+
}
|
|
731
|
+
if (poll(f, 3, wait) < 0) {
|
|
608
732
|
if (errno != EINTR) rc = -1;
|
|
609
733
|
continue;
|
|
610
734
|
}
|
|
@@ -648,8 +772,26 @@ int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_arra
|
|
|
648
772
|
if (rc) kill(pid, SIGKILL);
|
|
649
773
|
for (int k = 0; k < 3; k++)
|
|
650
774
|
if (f[k].fd >= 0) close(f[k].fd);
|
|
651
|
-
int status;
|
|
652
|
-
|
|
775
|
+
int status = 0, reaped = 0;
|
|
776
|
+
for (long nap = 1000000; timed && !rc && !reaped;) {
|
|
777
|
+
pid_t w = waitpid(pid, &status, WNOHANG);
|
|
778
|
+
if (w == pid) {
|
|
779
|
+
reaped = 1;
|
|
780
|
+
} else if (w < 0 && errno != EINTR) {
|
|
781
|
+
rc = -1;
|
|
782
|
+
} else {
|
|
783
|
+
int64_t left = deadline - nish_monotonic_nanos();
|
|
784
|
+
if (left <= 0) {
|
|
785
|
+
rc = -3;
|
|
786
|
+
kill(pid, SIGKILL);
|
|
787
|
+
} else {
|
|
788
|
+
struct timespec ts = { 0, left < nap ? (long)left : nap };
|
|
789
|
+
nanosleep(&ts, 0);
|
|
790
|
+
if (nap < 64000000) nap *= 2;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
while (!reaped && waitpid(pid, &status, 0) < 0) {
|
|
653
795
|
if (errno != EINTR) {
|
|
654
796
|
rc = -1;
|
|
655
797
|
break;
|
|
@@ -663,6 +805,11 @@ done:
|
|
|
663
805
|
#endif
|
|
664
806
|
}
|
|
665
807
|
|
|
808
|
+
/* `spawnPiped` is `spawnPipedWithin` with no deadline. */
|
|
809
|
+
int32_t nish_spawn_piped(const nish_array *argv, const nish_array *in, nish_array *out, nish_array *err) {
|
|
810
|
+
return nish_spawn_piped_within(argv, in, out, err, -1);
|
|
811
|
+
}
|
|
812
|
+
|
|
666
813
|
/* `kill(pid, sig)`: 0, or a negative errno. Only the four signals a program
|
|
667
814
|
asks a child to stop or report with are sent -- 0 (is it there), SIGINT,
|
|
668
815
|
SIGKILL and SIGTERM, whose numbers are the same on Linux and Darwin -- and
|