@amritk/nish-x86_64-linux 0.45.1 → 0.47.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 CHANGED
@@ -194,7 +194,9 @@ The attestation is a Sigstore-signed statement, made by
194
194
  `SHA256SUMS` was built by `.github/workflows/release.yml` at the release's tag
195
195
  on a GitHub-hosted runner. A failed verification refuses the install; with no
196
196
  `gh`, a signed-out one or one too old for those options, the check is skipped
197
- with a note saying so, and the SHA-256 still applies. Set
197
+ with a note saying so, and the SHA-256 still applies. A `gh` that has a token
198
+ but whose `gh auth status` fails (it asks GitHub's API) is tried three times,
199
+ a second or two apart, before it counts as unusable, and the note quotes it. Set
198
200
  `NISH_REQUIRE_ATTESTATION=1` to refuse instead of skipping: the install then
199
201
  fails unless `gh` verifies the attestation, and refuses a release of 0.21.0 or
200
202
  older, which carries none. The same command by hand, on a tarball you
package/bin/nish CHANGED
Binary file
package/docs/AI.md CHANGED
@@ -1601,11 +1601,12 @@ export const main = (): i32 => {
1601
1601
  };
1602
1602
  ```
1603
1603
 
1604
- - **A parameter is a scalar, a `CPtr`, a borrowed `string`, a `u8[]` buffer
1605
- with its count or a borrowed `declare class`; a return is a scalar, a `CPtr`
1606
- or a copied `string`.** The scalars are the integer and float widths,
1607
- `boolean` and `void`. Any other array, a Nish class or an interface is
1608
- rejected, in either position, and a `declare class` is never a return.
1604
+ - **A parameter is a scalar, a `CPtr`, a borrowed `string`, a buffer with its
1605
+ count or a borrowed `declare class`; a return is a scalar, a `CPtr` or a
1606
+ copied `string`.** The scalars are the integer and float widths, `boolean`
1607
+ and `void`. A buffer is a `u8[]`, `u16[]`, `u32[]`, `i32[]` or `f32[]`,
1608
+ readonly or not. Any other array, a Nish class or an interface is rejected,
1609
+ in either position, and a `declare class` is never a return.
1609
1610
  - **A `declare class` is a C struct, lent by pointer.** Its fields are laid out
1610
1611
  in declaration order at C's alignment, and `new Name()` zero-fills it. A
1611
1612
  field is `i32`, `i64`, `u8`, `u16`, `u32`, `u64`, `f32`, `f64`, `CPtr`,
@@ -1646,16 +1647,21 @@ export const main = (): i32 => {
1646
1647
  };
1647
1648
  ```
1648
1649
 
1649
- - **A run of `u8[]` / `readonly u8[]` parameters is followed by exactly one
1650
- `u64`**, the byte count C may touch in each (`memcpy(dst: u8[], src:
1651
- readonly u8[], n: u64)`). `readonly` is C's `const`. A run without its count
1652
- is NL2549, a count that is not `u64` NL2550, `i32[]` NL2551, `u8[] | null`
1653
- NL2552. Declare `fwrite` as `(buf: readonly u8[], n: u64, one: u64, f: CPtr)`
1654
- and pass `1` for `one`. A call passes the data pointer and first checks the
1655
- count against every buffer of the run: a larger count is an `ffi-length`
1656
- panic, and only `toU64(buf.length)` of the buffer itself is proved. C
1657
- borrows the bytes for the call and may write a `u8[]`; a spawned task may
1658
- not make the call.
1650
+ - **A run of buffer parameters is followed by exactly one `u64`**, the
1651
+ count of elements C may touch in each (`memcpy(dst: u8[], src: readonly
1652
+ u8[], n: u64)`, `scale(xs: f32[], n: u64, k: f32)`). The five element types
1653
+ are `u8`, `u16`, `u32`, `i32` and `f32` (C's `uint8_t`, `uint16_t`,
1654
+ `uint32_t`, `int32_t`, `float`); the count is in elements, so 4 for an
1655
+ `f32[]` of 4 is 16 bytes. `readonly` is C's `const`. A run without its count
1656
+ is NL2549, a count that is not `u64` NL2550, any other element (`f64[]`,
1657
+ `i64[]`) NL2551, `u8[] | null` NL2552, and a run that mixes element types
1658
+ (`f32[]` then `readonly u8[]`) NL2824: give each its own count. Declare
1659
+ `fwrite` as `(buf: readonly u8[], n: u64, one: u64, f: CPtr)` and pass `1`
1660
+ for `one`. A call passes the data pointer and first checks the count against
1661
+ every buffer of the run: a larger count is an `ffi-length` panic, and only
1662
+ `toU64(buf.length)` of the buffer itself is proved. C borrows the elements
1663
+ for the call and may write a buffer that is not `readonly`; a spawned task
1664
+ may not make the call.
1659
1665
 
1660
1666
  <!-- nish:err NL2549 -->
1661
1667
  ```ts
@@ -3073,8 +3079,7 @@ tag under one key (NL2721) as a different string (NL2720). A `tag` on a class
3073
3079
  no union holds does nothing, and a JSON call on that class refuses its
3074
3080
  enum-member field.
3075
3081
 
3076
- <!-- allow-panics: builds an array whose length it does not bound; indexes an array without a guard the checker reads; calls `std/json-typed.ts` code that keeps panic sites no proof removes yet -->
3077
- <!-- nish:ok --allow-panics -->
3082
+ <!-- nish:ok -->
3078
3083
  ```ts
3079
3084
  import { JsonWriter } from "nish/json";
3080
3085
  import { JsonLimits } from "nish/json-tokenizer";
@@ -3546,7 +3551,13 @@ its fix, and there are five:
3546
3551
  `--number-mode f64` — has no guard, so index with an `i32` or a `u32`),
3547
3552
  `if (d !== 0 && d !== -1)` before `a / d` (a
3548
3553
  constant divisor needs nothing), `if (xs.length > 0)` before `xs.pop()`,
3549
- `if (n >= 0 && n <= 9)` before `n` enters `integer<0, 9>`.
3554
+ `if (n >= 0 && n <= 9)` before `n` enters `integer<0, 9>`, `if (n >= 0)`
3555
+ before `new Array<T>(n)` of an `i32` (a local of `integer<0, N>` needs
3556
+ nothing), `if (start >= 0 && start <= end && end <= bytes.length)` before
3557
+ `s.slice(start, end)`, `xs.slice(start, end)` or
3558
+ `String.fromBytes(bytes, start, end)`, `if (src.length <= dst.length)`
3559
+ before `dst.set(src, 0)`, and `if (n >= 0 && n <= buf.length)` before
3560
+ `writeBytes(buf, 0, n)` or `netRead(fd, buf, 0, n)`.
3550
3561
  - **Bound a signed `+ - *`, or say it wraps**: an `i32`/`i64` operation the
3551
3562
  checker cannot prove fits is an `overflow` site; bound its operands (a loop
3552
3563
  below a length, a guard on the value), compute in `u32`/`u64`, or write
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/nish-x86_64-linux",
3
- "version": "0.45.1",
3
+ "version": "0.47.0",
4
4
  "description": "The nish native compiler for Linux on x86_64",
5
5
  "license": "MIT",
6
6
  "os": [
package/runtime/nish.d.ts CHANGED
@@ -1998,6 +1998,27 @@ declare function pollRemove(loop: i32, fd: i32): i32;
1998
1998
  * the next wait), 0 on a timeout, or a negative errno.
1999
1999
  */
2000
2000
  declare function pollWait(loop: i32, ready: i32[], timeoutMs: i32): i32;
2001
+ /**
2002
+ * Wait as `pollWait` does, up to `timeoutMs` for the first event, and then up to `maxWaitMs`
2003
+ * more until `minEvents` pairs are ready, so a busy ring loop takes its events in batches. An
2004
+ * idle loop is not woken every `maxWaitMs`: the second wait starts only once an event is in.
2005
+ * The batch counts the slots and listeners of a `NET_LISTEN_RING` listener; a loop with no ring
2006
+ * listener, and Darwin, answer as `pollWait` does. A descriptor that becomes ready during the
2007
+ * second wait, `signalFd()` included, is reported at most `maxWaitMs` late. `minEvents` of 1 or
2008
+ * less, or a `maxWaitMs` of 0 or less, is `pollWait`. Not callable in a `scope()` task or a
2009
+ * parallel body. A call that writes `ready` is refused in a scope's region on an array a task may
2010
+ * read, as `fill` is. Refused at compile time on wasm32 and the WASI profile (NL2404).
2011
+ * Capability: net.
2012
+ *
2013
+ * @param loop the loop from `pollCreate`.
2014
+ * @param ready receives the pairs; shorter than 2 is -22. Its length caps the batch.
2015
+ * @param timeoutMs the most milliseconds to wait for the first event; negative: forever (-1).
2016
+ * @param minEvents the pairs a batch waits for once the first is in.
2017
+ * @param maxWaitMs the most milliseconds the batch waits after the first event.
2018
+ * @returns how many pairs, at most `ready.length / 2` and at most 64, 0 on a timeout, or a
2019
+ * negative errno.
2020
+ */
2021
+ declare function pollWaitBatch(loop: i32, ready: i32[], timeoutMs: i32, minEvents: i32, maxWaitMs: i32): i32;
2001
2022
  /**
2002
2023
  * Set one socket option from a whitelist: there is no raw level and option number, so a program
2003
2024
  * cannot pass a level, a name and a length through to the kernel. The ids are the runtime's own
@@ -3453,6 +3474,26 @@ declare module "nish:net" {
3453
3474
  * for a `ready` shorter than 2.
3454
3475
  */
3455
3476
  export function pollWait(loop: i32, ready: i32[], timeoutMs: i32): i32;
3477
+ /**
3478
+ * Wait as `pollWait` does for the first event, and then, on a loop holding
3479
+ * a `NET_LISTEN_RING` listener's slots, up to `maxWaitMs` more until
3480
+ * `minEvents` pairs are ready. An idle loop is not woken every `maxWaitMs`.
3481
+ * A descriptor that becomes ready during the second wait is reported at
3482
+ * most `maxWaitMs` late. A loop with no ring listener, and Darwin, answer
3483
+ * as `pollWait` does.
3484
+ *
3485
+ * @param loop The loop.
3486
+ * @param ready The array the pairs are written into; its length caps the
3487
+ * batch.
3488
+ * @param timeoutMs The most milliseconds to wait for the first event; any
3489
+ * negative number waits forever.
3490
+ * @param minEvents The pairs a batch waits for once the first is in.
3491
+ * @param maxWaitMs The most milliseconds the batch waits after the first
3492
+ * event.
3493
+ * @returns The count of pairs, at most `ready.length / 2` and at most 64,
3494
+ * `0` on a timeout, or `-22` for a `ready` shorter than 2.
3495
+ */
3496
+ export function pollWaitBatch(loop: i32, ready: i32[], timeoutMs: i32, minEvents: i32, maxWaitMs: i32): i32;
3456
3497
  /**
3457
3498
  * Set one socket option from a whitelist. There is no way to pass a raw
3458
3499
  * level and option number, because that would be an untyped call into the
package/runtime/nish.h CHANGED
@@ -303,6 +303,7 @@ int nish_f64_format(double v, char out[NISH_F64_MAX]);
303
303
  end, or -1, with nothing written, when `at < 0` or they do not fit before
304
304
  `len`. `buf` may be null only when nothing fits. */
305
305
  int32_t nish_json_put_f64(uint8_t *buf, int32_t len, int32_t at, double v);
306
+ int32_t nish_json_put_string(uint8_t *buf, int32_t len, int32_t at, const uint8_t *src, int64_t from, int64_t to);
306
307
  nish_str *nish_str_from_i64(int64_t v);
307
308
  /* Unsigned decimal (WP15). u8/u16/u32 are zero-extended by the caller, so one
308
309
  symbol serves every unsigned width. */
@@ -793,6 +794,15 @@ double nish_stat_mtime(const nish_str *path);
793
794
  * or a read that fails. */
794
795
  int32_t nish_signal_fd(void);
795
796
  int32_t nish_read_signal(int32_t fd);
797
+ /* The runtime's own, with no builtin: the read end `signalFd()` made, or -1
798
+ * while none is made, without making one. A loop's wait asks it to tell the
799
+ * signal descriptor from any other (runtime-net.c, the direct ring wait). */
800
+ int32_t nish_signal_fd_made(void);
801
+ /* Every closer that takes a program's descriptor number calls this first:
802
+ * once the signal read end is closed, `nish_signal_fd_made` answers -1 for
803
+ * good, since its number may be reused (docs/security/net-uring.md,
804
+ * NURING-49). */
805
+ void nish_signal_fd_closing(int32_t fd);
796
806
  /* Who owns a path, and whether it runs (docs/security/runtime.md, RT-9; the
797
807
  * builtins `lstatOwnerModeSync`, `geteuid` and `isExecutableSync`, #386).
798
808
  * `nish_lstat_owner_mode(path)`: `lstat`, so a
@@ -927,6 +937,7 @@ int32_t nish_poll_add(int32_t loop, int32_t fd, int32_t events, int32_t token);
927
937
  int32_t nish_poll_modify(int32_t loop, int32_t fd, int32_t events, int32_t token);
928
938
  int32_t nish_poll_remove(int32_t loop, int32_t fd);
929
939
  int32_t nish_poll_wait(int32_t loop, nish_array *ready, int32_t timeout_ms);
940
+ int32_t nish_poll_wait_batch(int32_t loop, nish_array *ready, int32_t timeout_ms, int32_t min_events, int32_t max_wait_ms);
930
941
  int32_t nish_unix_listen(const nish_str *path, int32_t backlog, int32_t flags);
931
942
  int32_t nish_unix_listen_mode(const nish_str *path, int32_t backlog, int32_t flags, int32_t mode);
932
943
  int32_t nish_unix_connect(const nish_str *path);
package/runtime/nish.mjs CHANGED
@@ -159,6 +159,7 @@ for (const name of [
159
159
  "pollModify",
160
160
  "pollRemove",
161
161
  "pollWait",
162
+ "pollWaitBatch",
162
163
  "netSetOption",
163
164
  "netMss",
164
165
  "unixListen",
@@ -247,14 +247,28 @@ static inline unsigned nish_run_stops_sse2(int cls, __m128i v) {
247
247
  return (unsigned)_mm_movemask_epi8(_mm_or_si128(stop, v));
248
248
  }
249
249
 
250
+ static inline unsigned nish_plain_stops_sse2(const uint8_t *p) {
251
+ __m128i v = _mm_loadu_si128((const __m128i *)p);
252
+ __m128i stop = _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
253
+ _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v));
254
+ return (unsigned)_mm_movemask_epi8(stop);
255
+ }
256
+
257
+ /* A span of 16 bytes or more ends with one block that overlaps the last one
258
+ rather than a byte at a time: it lies inside the span, and the bytes it
259
+ shares with the blocks already read are masked off, so a stop among them
260
+ is not found twice. */
250
261
  static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to) {
262
+ int64_t from = i;
251
263
  for (; to - i >= 16; i += 16) {
252
- __m128i v = _mm_loadu_si128((const __m128i *)(p + i));
253
- __m128i stop = _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
254
- _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v));
255
- unsigned m = (unsigned)_mm_movemask_epi8(stop);
264
+ unsigned m = nish_plain_stops_sse2(p + i);
256
265
  if (m != 0) return i + __builtin_ctz(m);
257
266
  }
267
+ if (i < to && to - from >= 16) {
268
+ int64_t at = to - 16;
269
+ unsigned m = nish_plain_stops_sse2(p + at) & (~0u << (i - at));
270
+ return m != 0 ? at + __builtin_ctz(m) : to;
271
+ }
258
272
  return nish_plain_scalar(cls, p, i, to);
259
273
  }
260
274
 
@@ -357,13 +371,24 @@ static inline uint64_t nish_run_stops_neon(int cls, uint8x16_t v) {
357
371
  return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
358
372
  }
359
373
 
374
+ static inline uint64_t nish_plain_stops_neon(const uint8_t *p) {
375
+ uint8x16_t v = vld1q_u8(p);
376
+ uint8x16_t stop = vorrq_u8(vorrq_u8(vceqq_u8(v, vdupq_n_u8(0x22)), vceqq_u8(v, vdupq_n_u8(0x5c))), vcleq_u8(v, vdupq_n_u8(0x1f)));
377
+ return vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
378
+ }
379
+
380
+ /* The SSE2 path's overlapping last block, four mask bits a byte. */
360
381
  static int64_t nish_plain_base(int cls, const uint8_t *p, int64_t i, int64_t to) {
382
+ int64_t from = i;
361
383
  for (; to - i >= 16; i += 16) {
362
- uint8x16_t v = vld1q_u8(p + i);
363
- uint8x16_t stop = vorrq_u8(vorrq_u8(vceqq_u8(v, vdupq_n_u8(0x22)), vceqq_u8(v, vdupq_n_u8(0x5c))), vcleq_u8(v, vdupq_n_u8(0x1f)));
364
- uint64_t m = vget_lane_u64(vreinterpret_u64_u8(vshrn_n_u16(vreinterpretq_u16_u8(stop), 4)), 0);
384
+ uint64_t m = nish_plain_stops_neon(p + i);
365
385
  if (m != 0) return i + (int64_t)(__builtin_ctzll(m) >> 2);
366
386
  }
387
+ if (i < to && to - from >= 16) {
388
+ int64_t at = to - 16;
389
+ uint64_t m = nish_plain_stops_neon(p + at) & (~0ull << (4 * (i - at)));
390
+ return m != 0 ? at + (int64_t)(__builtin_ctzll(m) >> 2) : to;
391
+ }
367
392
  return nish_plain_scalar(cls, p, i, to);
368
393
  }
369
394
 
@@ -483,3 +508,138 @@ int64_t nish_bytes_skip_json_text(const uint8_t *p, int64_t from, int64_t to) {
483
508
  int64_t nish_bytes_skip_json_plain(const uint8_t *p, int64_t from, int64_t to) {
484
509
  return NISH_PLAIN_PATH(NISH_RUN_PLAIN, p, from, to);
485
510
  }
511
+
512
+ /* One byte JSON escapes, at `buf[o]`: `\"`, `\\`, the five short escapes, or
513
+ `\u00XX` in lower-case hex, as std/json.ts's `jsonShortEscape` and
514
+ `jsonHexByte` spell them. Answers the new end, or -1 when it does not fit
515
+ before `cap`. */
516
+ static int64_t nish_json_escape_byte(uint8_t *buf, int64_t o, int64_t cap, uint8_t c) {
517
+ uint8_t letter = c == 0x22 || c == 0x5c ? c
518
+ : c == 0x0a ? 'n'
519
+ : c == 0x09 ? 't'
520
+ : c == 0x0d ? 'r'
521
+ : c == 0x08 ? 'b'
522
+ : c == 0x0c ? 'f'
523
+ : 0;
524
+ if (letter) {
525
+ if (cap - o < 2) return -1;
526
+ buf[o] = 0x5c;
527
+ buf[o + 1] = letter;
528
+ return o + 2;
529
+ }
530
+ if (cap - o < 6) return -1;
531
+ static const char hex[] = "0123456789abcdef";
532
+ buf[o] = 0x5c;
533
+ buf[o + 1] = 'u';
534
+ buf[o + 2] = '0';
535
+ buf[o + 3] = '0';
536
+ buf[o + 4] = (uint8_t)hex[c >> 4];
537
+ buf[o + 5] = (uint8_t)hex[c & 15];
538
+ return o + 6;
539
+ }
540
+
541
+ #if defined(NISH_SIMD_SSE2)
542
+ /* The stops among the `n` bytes at `p`, 4 <= n <= 16, as bits from 0 for
543
+ p[0]: read as two loads that may overlap, of 4, 8 or 16 bytes, never
544
+ outside the `n`. */
545
+ static inline unsigned nish_json_stops_short(const uint8_t *p, int64_t n) {
546
+ __m128i v;
547
+ unsigned lo, hi;
548
+ if (n >= 8) {
549
+ v = _mm_unpacklo_epi64(_mm_loadl_epi64((const __m128i *)p), _mm_loadl_epi64((const __m128i *)(p + n - 8)));
550
+ unsigned m = (unsigned)_mm_movemask_epi8(_mm_or_si128(
551
+ _mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
552
+ _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v)));
553
+ lo = m & 0xff;
554
+ hi = (m >> 8) << (n - 8);
555
+ } else {
556
+ uint32_t a, b;
557
+ __builtin_memcpy(&a, p, 4);
558
+ __builtin_memcpy(&b, p + n - 4, 4);
559
+ v = _mm_unpacklo_epi32(_mm_cvtsi32_si128((int)a), _mm_cvtsi32_si128((int)b));
560
+ unsigned m = (unsigned)_mm_movemask_epi8(_mm_or_si128(
561
+ _mm_or_si128(_mm_cmpeq_epi8(v, _mm_set1_epi8(0x22)), _mm_cmpeq_epi8(v, _mm_set1_epi8(0x5c))),
562
+ _mm_cmpeq_epi8(_mm_min_epu8(v, _mm_set1_epi8(0x1f)), v)));
563
+ lo = m & 0xf;
564
+ hi = ((m >> 4) & 0xf) << (n - 4);
565
+ }
566
+ return lo | hi;
567
+ }
568
+ #endif
569
+
570
+ /* The first byte of `p[i, to)` JSON escapes, or `to`: `skipJsonPlain`'s
571
+ answer. With SSE2 every window of 4 bytes or more is read in blocks that
572
+ lie inside it, the last overlapping the one before. */
573
+ static inline int64_t nish_json_plain_end(const uint8_t *p, int64_t i, int64_t to) {
574
+ #if defined(NISH_SIMD_SSE2)
575
+ int64_t n = to - i;
576
+ if (n >= 16) {
577
+ for (; to - i >= 16; i += 16) {
578
+ unsigned m = nish_plain_stops_sse2(p + i);
579
+ if (m != 0) return i + __builtin_ctz(m);
580
+ }
581
+ if (i == to) return to;
582
+ int64_t at = to - 16;
583
+ unsigned m = nish_plain_stops_sse2(p + at) & (~0u << (i - at));
584
+ return m != 0 ? at + __builtin_ctz(m) : to;
585
+ }
586
+ if (n >= 4) {
587
+ unsigned m = nish_json_stops_short(p + i, n);
588
+ return m != 0 ? i + __builtin_ctz(m) : to;
589
+ }
590
+ #endif
591
+ while (i < to && p[i] >= 0x20 && p[i] != 0x22 && p[i] != 0x5c) i++;
592
+ return i;
593
+ }
594
+
595
+ /* `n` bytes from `s` to `d`, which do not overlap, in moves of 16, 8 or 4
596
+ bytes that may overlap one another, never outside the `n`. */
597
+ static inline void nish_json_copy(uint8_t *d, const uint8_t *s, int64_t n) {
598
+ if (n >= 16) {
599
+ int64_t k = 0;
600
+ for (; n - k > 16; k += 16) __builtin_memcpy(d + k, s + k, 16);
601
+ __builtin_memcpy(d + n - 16, s + n - 16, 16);
602
+ } else if (n >= 8) {
603
+ uint64_t a, b;
604
+ __builtin_memcpy(&a, s, 8);
605
+ __builtin_memcpy(&b, s + n - 8, 8);
606
+ __builtin_memcpy(d, &a, 8);
607
+ __builtin_memcpy(d + n - 8, &b, 8);
608
+ } else if (n >= 4) {
609
+ uint32_t a, b;
610
+ __builtin_memcpy(&a, s, 4);
611
+ __builtin_memcpy(&b, s + n - 4, 4);
612
+ __builtin_memcpy(d, &a, 4);
613
+ __builtin_memcpy(d + n - 4, &b, 4);
614
+ } else {
615
+ for (int64_t k = 0; k < n; k++) d[k] = s[k];
616
+ }
617
+ }
618
+
619
+ /* `jsonPutString`'s walk (std/json.ts; the emitter makes the call,
620
+ src/kernels.ts): `src[from, to)` as a JSON string, its quotes and escapes
621
+ included, written into `buf` from `at`. Answers the new end, or -1 when
622
+ `at` is negative or the text does not fit before `len`, the array's length
623
+ as the Nish code's `toI32` reads it; the bytes from `at` may then have been
624
+ written, and the caller's cursor has not moved. The caller has checked
625
+ `0 <= from <= to <= src's length`. A run of bytes that need no escape is
626
+ found by `nish_json_plain_end` and copied in one piece, so a string with
627
+ nothing to escape is one scan and one copy. */
628
+ int32_t nish_json_put_string(uint8_t *buf, int32_t len, int32_t at, const uint8_t *src, int64_t from, int64_t to) {
629
+ int64_t cap = len, o = at, i = from;
630
+ if (at < 0 || o >= cap) return -1;
631
+ buf[o++] = 0x22;
632
+ while (i < to) {
633
+ int64_t j = nish_json_plain_end(src, i, to);
634
+ if (cap - o < j - i) return -1;
635
+ nish_json_copy(buf + o, src + i, j - i);
636
+ o += j - i;
637
+ if (j == to) break;
638
+ o = nish_json_escape_byte(buf, o, cap, src[j]);
639
+ if (o < 0) return -1;
640
+ i = j + 1;
641
+ }
642
+ if (o >= cap) return -1;
643
+ buf[o++] = 0x22;
644
+ return (int32_t)o;
645
+ }
@@ -212,6 +212,21 @@ int32_t nish_signal_fd(void) {
212
212
  return (int32_t)(cur >> 32);
213
213
  }
214
214
 
215
+ /* Set once a closer (`netClose`, `fileClose`) is handed the read end: from
216
+ then on its number may name any descriptor opened since, so
217
+ `nish_signal_fd_made` answers -1 rather than a number that is no longer the
218
+ signal pipe (docs/security/net-uring.md, NURING-48 and NURING-49). */
219
+ static int32_t nish_signal_closed;
220
+
221
+ int32_t nish_signal_fd_made(void) {
222
+ int64_t cur = __atomic_load_n(&nish_signal_pipe, __ATOMIC_ACQUIRE);
223
+ return cur < 0 || __atomic_load_n(&nish_signal_closed, __ATOMIC_ACQUIRE) ? -1 : (int32_t)(cur >> 32);
224
+ }
225
+
226
+ void nish_signal_fd_closing(int32_t fd) {
227
+ if (fd >= 0 && fd == nish_signal_fd_made()) __atomic_store_n(&nish_signal_closed, 1, __ATOMIC_RELEASE);
228
+ }
229
+
215
230
  /* Block until a signal's byte arrives and answer it: 15 or 2, or -1 for any
216
231
  `fd` that is not the signal descriptor and for a read that fails. */
217
232
  int32_t nish_read_signal(int32_t fd) {