@amritk/nish-aarch64-linux 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
@@ -102,6 +102,12 @@
102
102
  * `takeDatagramInto` or `quicListenerTakePaced`, `flight.ecn` for a flight.
103
103
  * One send carries one codepoint, so a flight ends before a datagram the
104
104
  * connection would mark otherwise. The listener's own answers go unmarked.
105
+ * On the receiving side, a carrier whose platform reports the bits a datagram
106
+ * arrived with (`udpRecvFrom`'s `meta[1]`) sets `conn.arrivalEcn` to them
107
+ * before handing the datagram over, `quicListenerReceiveSegments` included:
108
+ * one receive is one codepoint for all its segments. The connection then
109
+ * counts them per packet number space and its ACKs carry the counts as an
110
+ * ACK_ECN (§13.4.1).
105
111
  *
106
112
  * const flight = new QuicFlight();
107
113
  * const n: i32 = quicListenerTakeFlight(conn, now, buf, 0, toI32(buf.length), flight);
@@ -284,7 +290,12 @@ const quicListenerWipeRoom = (bytes: u8[], room: i32): void => {
284
290
 
285
291
  /** An empty array with room for `size` bytes, so pushing up to that many allocates nothing. */
286
292
  const quicListenerRoom = (size: i32): u8[] => {
287
- const out: u8[] = new Array<u8>(size)
293
+ // Every room is a datagram's or a connection ID's, under 65536 bytes, so it
294
+ // is allocated at once; a larger one would grow to its size push by push.
295
+ const out: u8[] = size > 0 && size <= 65535 ? new Array<u8>(toU16(size)) : []
296
+ while (toI32(out.length) < size) {
297
+ out.push(toU8(0))
298
+ }
288
299
  while (out.length > 0) {
289
300
  out.pop()
290
301
  }
@@ -293,7 +304,7 @@ const quicListenerRoom = (size: i32): u8[] => {
293
304
 
294
305
  /** Sets `bytes`' length to `length` in the room it already has: popped, or pushed with zeros. */
295
306
  const quicListenerResize = (bytes: u8[], length: i32): void => {
296
- while (toI32(bytes.length) > length) {
307
+ while (bytes.length > 0 && toI32(bytes.length) > length) {
297
308
  bytes.pop()
298
309
  }
299
310
  while (toI32(bytes.length) < length) {
@@ -309,10 +320,13 @@ const quicListenerRefill = (to: u8[], from: u8[], at: i32, length: i32): void =>
309
320
 
310
321
  /** Writes `value`'s low `size` bytes into `buf` at `at`, big-endian. */
311
322
  const quicListenerPutNumber = (buf: u8[], at: i32, value: i64, size: i32): void => {
312
- for (let k: i32 = 0; k < size; k += 1) {
313
- if (at + k >= 0 && at + k < toI32(buf.length)) {
314
- buf[at + k] = toU8(toI32((value >> (toI64(size - 1 - k) * 8)) & 255))
315
- }
323
+ if (at < 0 || size < 0 || size > 8) {
324
+ return
325
+ }
326
+ let k: i32 = 0
327
+ for (let i: i32 = at; i < toI32(buf.length) && k < size; i += 1) {
328
+ buf[i] = toU8(toI32((value >> (toI64(size - 1 - k) * 8)) & 255))
329
+ k += 1
316
330
  }
317
331
  }
318
332
 
@@ -332,10 +346,6 @@ const quicListenerRetryNonce = (): u8[] => [
332
346
  0x46, 0x15, 0x99, 0xd3, 0x5d, 0x63, 0x2b, 0xf2, 0x23, 0x98, 0x25, 0xbb,
333
347
  ]
334
348
 
335
- /** Whether `[at, at + len)` is a window inside an array of `length` elements: `p256WindowFits`' test. */
336
- const quicListenerWindowFits = (length: i32, at: i32, len: i32): boolean =>
337
- at >= 0 && len >= 0 && at <= length - len
338
-
339
349
  /**
340
350
  * The server's answers to datagrams no connection owns, under one
341
351
  * `QuicServerConfig`. Make one when the server starts, with the same
@@ -386,7 +396,7 @@ export class QuicListener {
386
396
  private retryKey: AesKey
387
397
  /** What `handle` answers into before it copies the answer out. */
388
398
  spare: QuicListenerAnswer
389
- private counter: i64 = 0
399
+ private counter: u64 = 0
390
400
  /** The latest time the caller gave, in milliseconds; time never runs backwards here. */
391
401
  private now: i64 = 0
392
402
  /** When the reset budget was last topped up (-1: never yet), and the stateless resets that may go now. */
@@ -394,9 +404,12 @@ export class QuicListener {
394
404
  private resetBudget: i32 = 0
395
405
  /** Where the unused bytes of `pool` start: `QUIC_LISTENER_POOL` when it is spent. */
396
406
  poolHead: i32 = 0
397
- /** Stateless resets sent, and ones the rate limit held back. */
407
+ /** Stateless resets sent, held at 2147483647. */
398
408
  resetsSent: i32 = 0
399
- /** Stateless resets the rate limit held back, which a log or test reads to see the limit working. */
409
+ /**
410
+ * Stateless resets the rate limit held back, held at 2147483647, which a
411
+ * log or test reads to see the limit working.
412
+ */
400
413
  resetsLimited: i32 = 0
401
414
  /** Whether the configuration and entropy were usable. */
402
415
  private usable: boolean = false
@@ -477,29 +490,38 @@ export class QuicListener {
477
490
  if (this.poolHead >= QUIC_LISTENER_POOL) {
478
491
  this.refill()
479
492
  }
480
- let b: i32 = 0
481
- if (this.poolHead >= 0 && this.poolHead < toI32(this.pool.length)) {
482
- b = toI32(this.pool[this.poolHead])
483
- this.pool[this.poolHead] = toU8(0)
493
+ const pool: u8[] = this.pool
494
+ const head: i32 = this.poolHead
495
+ // Never taken: `refill` leaves the head at 0 of a pool `QUIC_LISTENER_POOL` long.
496
+ if (head < 0 || head >= toI32(pool.length)) {
497
+ this.poolHead = QUIC_LISTENER_POOL
498
+ return 0
484
499
  }
485
- this.poolHead = this.poolHead + 1
500
+ const b: i32 = toI32(pool[head])
501
+ pool[head] = toU8(0)
502
+ this.poolHead = head + 1
486
503
  return b
487
504
  }
488
505
 
489
506
  /** Writes `count` unpredictable bytes into `out` at `at`. */
490
507
  private randomInto(out: u8[], at: i32, count: i32): void {
508
+ let i: i32 = at
491
509
  for (let k: i32 = 0; k < count; k += 1) {
492
510
  const b: i32 = this.randomByte()
493
- if (at + k >= 0 && at + k < toI32(out.length)) {
494
- out[at + k] = toU8(b)
511
+ if (i < toI32(out.length)) {
512
+ if (i >= 0) {
513
+ out[i] = toU8(b)
514
+ }
515
+ i += 1
495
516
  }
496
517
  }
497
518
  }
498
519
 
499
520
  /** The generator's next block, HMAC-SHA256 under the seed of the counter, into `pool`. */
500
521
  private refill(): void {
501
- quicListenerPutNumber(this.block, 0, this.counter, QUIC_LISTENER_BLOCK)
502
- this.counter = this.counter + 1
522
+ const counter: u64 = this.counter
523
+ quicListenerPutNumber(this.block, 0, toI64(counter), QUIC_LISTENER_BLOCK)
524
+ this.counter = counter + toU64(1)
503
525
  const mac: HmacSha256Scratch = this.kdf.sha256
504
526
  {
505
527
  using _scope = arena()
@@ -532,7 +554,8 @@ export class QuicListener {
532
554
  * `QUIC_LISTEN_RETRY`, `QUIC_LISTEN_INVALID_TOKEN` or `QUIC_LISTEN_STATELESS_RESET`.
533
555
  */
534
556
  handleWindow(datagram: u8[], at: i32, len: i32, address: u8[], now: i64, answer: QuicListenerAnswer): i32 {
535
- if (!quicListenerWindowFits(toI32(datagram.length), at, len)) {
557
+ // `[at, at + len)` inside `datagram`: `p256WindowFits`' test.
558
+ if (at < 0 || len < 0 || at > toI32(datagram.length) - len) {
536
559
  panic(
537
560
  `QuicListener.handleWindow: the window [${at}, ${at} + ${len}) is outside a datagram of ${toI32(datagram.length)} bytes`
538
561
  )
@@ -549,7 +572,8 @@ export class QuicListener {
549
572
  return answer.kind
550
573
  }
551
574
  const header: QuicHeader = this.header
552
- quicParseHeaderInto(header, datagram, at, at + len, QUIC_CONN_CID_LENGTH)
575
+ const end: i32 = toI32(toI64(at) + toI64(len))
576
+ quicParseHeaderInto(header, datagram, at, end, QUIC_CONN_CID_LENGTH)
553
577
  if (header.error === QUIC_ERR_VERSION) {
554
578
  // §5.2.2: answer an unsupported version only in a datagram that could
555
579
  // start a connection, and §6.1: never a Version Negotiation packet.
@@ -565,7 +589,12 @@ export class QuicListener {
565
589
  // RFC 9000 §21.11: an ID whose worker byte names a sibling in the
566
590
  // steered group is that sibling's, and a reset under the shared key
567
591
  // would end its live connection. Byte 0 at or above N is no worker's.
568
- const owner: i32 = toI32(datagram[header.dcidStart])
592
+ const dcidAt: i32 = header.dcidStart
593
+ // Never taken: a short header the parser read has its DCID inside the datagram.
594
+ if (dcidAt < 0 || dcidAt >= toI32(datagram.length)) {
595
+ return answer.kind
596
+ }
597
+ const owner: i32 = toI32(datagram[dcidAt])
569
598
  if (
570
599
  quicWorkerByte(this.config) >= 0 &&
571
600
  owner < this.config.workerCount &&
@@ -573,7 +602,7 @@ export class QuicListener {
573
602
  ) {
574
603
  return answer.kind
575
604
  }
576
- this.statelessReset(answer, len, datagram, header.dcidStart, header.dcidLength)
605
+ this.statelessReset(answer, len, datagram, dcidAt, header.dcidLength)
577
606
  return answer.kind
578
607
  }
579
608
  // §14.1, §7.2, §5.2.2: only an Initial in a full-sized datagram, sent to
@@ -582,7 +611,14 @@ export class QuicListener {
582
611
  return answer.kind
583
612
  }
584
613
  const tokenAt: i32 = header.tokenStart
585
- if (header.tokenLength === 0 || toI32(datagram[tokenAt]) !== QUIC_LISTENER_TOKEN_MARKER) {
614
+ // A token the parser read lies inside the datagram, so the bounds test
615
+ // fails only for no token.
616
+ const marked: boolean =
617
+ header.tokenLength !== 0 &&
618
+ tokenAt >= 0 &&
619
+ tokenAt < toI32(datagram.length) &&
620
+ toI32(datagram[tokenAt]) === QUIC_LISTENER_TOKEN_MARKER
621
+ if (!marked) {
586
622
  if (!this.config.retry) {
587
623
  answer.kind = QUIC_LISTEN_ACCEPT
588
624
  return answer.kind
@@ -590,15 +626,13 @@ export class QuicListener {
590
626
  this.retry(answer, datagram, header, address)
591
627
  return answer.kind
592
628
  }
593
- if (this.tokenValid(datagram, header, address)) {
629
+ const odcidLength: i32 = this.tokenOdcid(datagram, header, address)
630
+ // `tokenOdcid` answers a length only for a token whose head, 10 bytes
631
+ // (`QUIC_LISTENER_TOKEN_HEAD`, as a literal the prover reads), lies inside the datagram.
632
+ if (odcidLength >= 0 && tokenAt >= 0 && tokenAt <= toI32(datagram.length) - 10) {
594
633
  answer.kind = QUIC_LISTEN_ACCEPT
595
634
  answer.retried = true
596
- quicListenerRefill(
597
- answer.originalDcid,
598
- datagram,
599
- tokenAt + QUIC_LISTENER_TOKEN_HEAD,
600
- toI32(datagram[tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1])
601
- )
635
+ quicListenerRefill(answer.originalDcid, datagram, tokenAt + 10, odcidLength)
602
636
  quicListenerRefill(answer.retryScid, datagram, header.dcidStart, header.dcidLength)
603
637
  return answer.kind
604
638
  }
@@ -649,11 +683,15 @@ export class QuicListener {
649
683
  const unused: i32 = this.randomByte()
650
684
  const dcidLength: i32 = header.dcidLength
651
685
  const scidLength: i32 = header.scidLength
686
+ // Never taken: a long header's ID lengths are a byte each.
687
+ if (dcidLength < 0 || dcidLength > 255 || scidLength < 0 || scidLength > 255) {
688
+ return
689
+ }
652
690
  const reply: u8[] = answer.reply
653
691
  quicListenerResize(reply, 11 + dcidLength + scidLength)
654
- reply[0] = toU8((unused & 0x3f) | 0xc0)
692
+ quicListenerPutNumber(reply, 0, toI64((unused & 0x3f) | 0xc0), 1)
655
693
  quicListenerPutNumber(reply, 1, 0, 4)
656
- reply[5] = toU8(scidLength)
694
+ quicListenerPutNumber(reply, 5, toI64(scidLength), 1)
657
695
  quicPacketCopy(reply, 6, datagram, header.scidStart, scidLength)
658
696
  const dcidAt: i32 = 6 + scidLength
659
697
  quicListenerPutNumber(reply, dcidAt, toI64(dcidLength), 1)
@@ -704,35 +742,54 @@ export class QuicListener {
704
742
  * and still within its lifetime: the lengths add up, the MAC matches
705
743
  * (compared in constant time), and it was issued no later than now and
706
744
  * less than `retryTokenLifetime` ago. The token is read in `datagram`.
745
+ * Answers the length of the original DCID the token carries when it is
746
+ * good, else -1.
707
747
  */
708
- private tokenValid(datagram: u8[], header: QuicHeader, address: u8[]): boolean {
748
+ private tokenOdcid(datagram: u8[], header: QuicHeader, address: u8[]): i32 {
709
749
  const tokenAt: i32 = header.tokenStart
710
750
  const length: i32 = header.tokenLength
711
751
  if (length < QUIC_LISTENER_TOKEN_HEAD + QUIC_LISTENER_TOKEN_MAC_SIZE) {
712
- return false
752
+ return -1
753
+ }
754
+ // Never taken: the parser read the token inside the datagram. The prover
755
+ // reads a literal, so 10 is `QUIC_LISTENER_TOKEN_HEAD` and 9 its last byte.
756
+ if (tokenAt < 0 || tokenAt > toI32(datagram.length) - 10) {
757
+ return -1
758
+ }
759
+ const odcidLength: i32 = toI32(datagram[tokenAt + 9])
760
+ const timeAt: i32 = tokenAt + 1
761
+ if (odcidLength > QUIC_MAX_CID_LENGTH) {
762
+ return -1
713
763
  }
714
- const odcidLength: i32 = toI32(datagram[tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1])
715
764
  const head: i32 = QUIC_LISTENER_TOKEN_HEAD + odcidLength
716
- if (odcidLength > QUIC_MAX_CID_LENGTH || head + QUIC_LISTENER_TOKEN_MAC_SIZE !== length) {
717
- return false
765
+ if (head + QUIC_LISTENER_TOKEN_MAC_SIZE !== length) {
766
+ return -1
718
767
  }
719
768
  this.tokenMac(datagram, tokenAt, head, datagram, header.dcidStart, header.dcidLength, address)
720
769
  const same: boolean = timingSafeEqualAt(
721
770
  this.tag,
722
771
  0,
723
772
  datagram,
724
- tokenAt + head,
773
+ toI32(toI64(tokenAt) + toI64(head)),
725
774
  QUIC_LISTENER_TOKEN_MAC_SIZE
726
775
  )
727
776
  secureZero(this.tag)
728
777
  if (!same) {
729
- return false
778
+ return -1
730
779
  }
731
- let issued: i64 = 0
732
- for (let k: i32 = 1; k < QUIC_LISTENER_TOKEN_HEAD - 1; k += 1) {
733
- issued = (issued << 8) | toI64(datagram[tokenAt + k])
780
+ let issued: u64 = 0
781
+ let j: i32 = timeAt
782
+ for (let k: i32 = 1; k < QUIC_LISTENER_TOKEN_HEAD - 1 && j < toI32(datagram.length); k += 1) {
783
+ issued = (issued << toU64(8)) | toU64(datagram[j])
784
+ j += 1
734
785
  }
735
- return issued <= this.now && this.now - issued < this.config.retryTokenLifetime
786
+ // `now` never falls below 0, where it starts, and the lifetime is 1 or more (the config checks it).
787
+ const now: i64 = this.now
788
+ const lifetime: i64 = this.config.retryTokenLifetime
789
+ if (now < 0 || lifetime <= 0 || issued > toU64(now)) {
790
+ return -1
791
+ }
792
+ return toU64(now) - issued < toU64(lifetime) ? odcidLength : -1
736
793
  }
737
794
 
738
795
  /**
@@ -745,6 +802,10 @@ export class QuicListener {
745
802
  private retry(answer: QuicListenerAnswer, datagram: u8[], header: QuicHeader, address: u8[]): void {
746
803
  const scidLength: i32 = header.scidLength
747
804
  const odcidLength: i32 = header.dcidLength
805
+ // Never taken: a long header's ID lengths are a byte each.
806
+ if (scidLength < 0 || scidLength > 255 || odcidLength < 0 || odcidLength > 255) {
807
+ return
808
+ }
748
809
  const retryScidAt: i32 = 7 + scidLength
749
810
  const tokenAt: i32 = retryScidAt + QUIC_CONN_CID_LENGTH
750
811
  const macAt: i32 = tokenAt + QUIC_LISTENER_TOKEN_HEAD + odcidLength
@@ -755,20 +816,20 @@ export class QuicListener {
755
816
  const worker: i32 = quicWorkerByte(this.config)
756
817
  if (worker >= 0) {
757
818
  // The retried Initial is sent to this ID, so it steers back here.
758
- reply[retryScidAt] = toU8(worker)
819
+ quicListenerPutNumber(reply, retryScidAt, toI64(worker), 1)
759
820
  }
760
- reply[tokenAt] = toU8(QUIC_LISTENER_TOKEN_MARKER)
821
+ quicListenerPutNumber(reply, tokenAt, toI64(QUIC_LISTENER_TOKEN_MARKER), 1)
761
822
  quicListenerPutNumber(reply, tokenAt + 1, this.now, 8)
762
- reply[tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1] = toU8(odcidLength)
823
+ quicListenerPutNumber(reply, tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1, toI64(odcidLength), 1)
763
824
  quicPacketCopy(reply, tokenAt + QUIC_LISTENER_TOKEN_HEAD, datagram, header.dcidStart, odcidLength)
764
825
  this.tokenMac(reply, tokenAt, macAt - tokenAt, reply, retryScidAt, QUIC_CONN_CID_LENGTH, address)
765
826
  quicPacketCopy(reply, macAt, this.tag, 0, QUIC_LISTENER_TOKEN_MAC_SIZE)
766
827
  secureZero(this.tag)
767
- reply[0] = toU8((this.randomByte() & 15) | 0xf0)
828
+ quicListenerPutNumber(reply, 0, toI64((this.randomByte() & 15) | 0xf0), 1)
768
829
  quicListenerPutNumber(reply, 1, 1, 4)
769
- reply[5] = toU8(scidLength)
830
+ quicListenerPutNumber(reply, 5, toI64(scidLength), 1)
770
831
  quicPacketCopy(reply, 6, datagram, header.scidStart, scidLength)
771
- reply[retryScidAt - 1] = toU8(QUIC_CONN_CID_LENGTH)
832
+ quicListenerPutNumber(reply, retryScidAt - 1, toI64(QUIC_CONN_CID_LENGTH), 1)
772
833
  if (this.retryTag(reply, tagAt, datagram, header.dcidStart, odcidLength)) {
773
834
  answer.kind = QUIC_LISTEN_RETRY
774
835
  } else {
@@ -785,8 +846,12 @@ export class QuicListener {
785
846
  * Answers whether it was made.
786
847
  */
787
848
  private retryTag(reply: u8[], tagAt: i32, datagram: u8[], odcidAt: i32, odcidLength: i32): boolean {
849
+ // Never taken: `retry` holds the ID to a byte's length and its reply under 600 bytes.
850
+ if (odcidLength < 0 || odcidLength > 255 || tagAt < 0 || tagAt > 1024) {
851
+ return false
852
+ }
788
853
  using _scope = arena()
789
- const pseudo: u8[] = new Array<u8>(1 + odcidLength + tagAt)
854
+ const pseudo: u8[] = new Array<u8>(toU16(1 + odcidLength + tagAt))
790
855
  quicListenerPutNumber(pseudo, 0, toI64(odcidLength), 1)
791
856
  quicPacketCopy(pseudo, 1, datagram, odcidAt, odcidLength)
792
857
  quicPacketCopy(pseudo, 1 + odcidLength, reply, 0, tagAt)
@@ -857,7 +922,7 @@ export class QuicListener {
857
922
  0,
858
923
  this.none
859
924
  )
860
- const payloadLength: i32 = frameEnd - headerLength
925
+ const payloadLength: i32 = headerLength > 0 && frameEnd > 0 ? frameEnd - headerLength : -1
861
926
  const put: i32 =
862
927
  headerLength > 0 && frameEnd > 0
863
928
  ? quicPutLongHeader(
@@ -895,22 +960,39 @@ export class QuicListener {
895
960
 
896
961
  /** Takes one stateless reset from the rate limit's budget, topping it up first. Answers whether one was there. */
897
962
  private takeReset(): boolean {
898
- if (this.resetRefilled < 0) {
899
- this.resetRefilled = this.now
900
- this.resetBudget = QUIC_LISTENER_RESET_BURST
963
+ // `now` never falls below 0, where it starts, and `refilled` is never
964
+ // after it, so the u64 sums below are the i64 ones.
965
+ const now: i64 = this.now
966
+ let refilled: i64 = this.resetRefilled
967
+ let budget: i32 = this.resetBudget
968
+ if (refilled < 0) {
969
+ refilled = now
970
+ budget = QUIC_LISTENER_RESET_BURST
901
971
  }
902
- const earned: i64 = (this.now - this.resetRefilled) / QUIC_LISTENER_RESET_INTERVAL
903
- if (earned > 0) {
904
- const room: i64 = toI64(QUIC_LISTENER_RESET_BURST - this.resetBudget)
905
- this.resetBudget = this.resetBudget + toI32(earned < room ? earned : room)
906
- this.resetRefilled = this.resetRefilled + earned * QUIC_LISTENER_RESET_INTERVAL
972
+ if (now > refilled) {
973
+ // 100 is `QUIC_LISTENER_RESET_INTERVAL`, as a literal the prover reads.
974
+ const earned: u64 = (toU64(now) - toU64(refilled)) / toU64(100)
975
+ let k: u64 = 0
976
+ while (k < earned && budget >= 0 && budget < QUIC_LISTENER_RESET_BURST) {
977
+ budget += 1
978
+ k += toU64(1)
979
+ }
980
+ refilled = toI64(toU64(refilled) + earned * toU64(QUIC_LISTENER_RESET_INTERVAL))
907
981
  }
908
- if (this.resetBudget <= 0) {
909
- this.resetsLimited = this.resetsLimited + 1
982
+ this.resetRefilled = refilled
983
+ if (budget <= 0) {
984
+ this.resetBudget = budget
985
+ const limited: i32 = this.resetsLimited
986
+ if (limited < 2147483647) {
987
+ this.resetsLimited = limited + 1
988
+ }
910
989
  return false
911
990
  }
912
- this.resetBudget = this.resetBudget - 1
913
- this.resetsSent = this.resetsSent + 1
991
+ this.resetBudget = budget - 1
992
+ const sent: i32 = this.resetsSent
993
+ if (sent < 2147483647) {
994
+ this.resetsSent = sent + 1
995
+ }
914
996
  return true
915
997
  }
916
998
 
@@ -931,22 +1013,28 @@ export class QuicListener {
931
1013
  dcidAt: i32,
932
1014
  dcidLength: i32
933
1015
  ): void {
934
- if (n <= QUIC_LISTENER_MIN_RESET || !this.takeReset()) {
1016
+ // The prover reads literals: 21 is `QUIC_LISTENER_MIN_RESET`, 43
1017
+ // `QUIC_LISTENER_SHORT_TRIGGER` and 1200 `QUIC_CONN_DATAGRAM_SIZE`.
1018
+ if (n <= 21 || !this.takeReset()) {
935
1019
  return
936
1020
  }
937
1021
  let size: i32 = n - 1
938
- if (n > QUIC_LISTENER_SHORT_TRIGGER) {
939
- const most: i32 = n - 1 < QUIC_CONN_DATAGRAM_SIZE ? n - 1 : QUIC_CONN_DATAGRAM_SIZE
940
- const span: i32 = most - QUIC_LISTENER_SHORT_TRIGGER + 1
1022
+ if (n > 43) {
1023
+ // The sizes from 43 to the smaller of n - 1 and 1200.
1024
+ const span: i32 = n <= 1200 ? n - 43 : 1158
941
1025
  const high: i32 = this.randomByte()
942
- const r: i32 = (high << 8) | this.randomByte()
943
- size = QUIC_LISTENER_SHORT_TRIGGER + (span > 0 ? r % span : 0)
1026
+ const r: i32 = ((high & 255) << 8) | this.randomByte()
1027
+ const extra: i32 = span > 0 ? r % span : 0
1028
+ size = toI32(toI64(QUIC_LISTENER_SHORT_TRIGGER) + toI64(extra))
944
1029
  }
945
1030
  const reply: u8[] = answer.reply
946
- const tokenAt: i32 = size - QUIC_RESET_TOKEN_SIZE
1031
+ const tokenAt: i32 = toI32(toI64(size) - toI64(QUIC_RESET_TOKEN_SIZE))
947
1032
  quicListenerResize(reply, size)
948
1033
  this.randomInto(reply, QUIC_LISTENER_FROM, tokenAt)
949
- reply[0] = toU8((toI32(reply[0]) & 0x3f) | 0x40)
1034
+ // `size` is over 21 bytes, so the first is there.
1035
+ if (reply.length > 0) {
1036
+ reply[0] = toU8((toI32(reply[0]) & 0x3f) | 0x40)
1037
+ }
950
1038
  quicConnResetTokenInto(
951
1039
  this.kdf.sha256,
952
1040
  this.config.statelessResetKey,
@@ -981,6 +1069,19 @@ export const quicListenerTakePaced = (conn: QuicConnection, now: i64): u8[] | nu
981
1069
  return out
982
1070
  }
983
1071
 
1072
+ /**
1073
+ * `now + delay` for a pacer's delay, which is never below 0: summed in `u64`,
1074
+ * where it cannot overflow, and held at the largest `i64` when a `now` near it
1075
+ * would carry past; no caller's clock comes near.
1076
+ */
1077
+ const quicListenerLater = (now: i64, delay: i64): i64 => {
1078
+ if (delay <= 0) {
1079
+ return now
1080
+ }
1081
+ const sum: i64 = toI64(toU64(now) + toU64(delay))
1082
+ return now >= 0 && sum < 0 ? toI64((toU64(0) - toU64(1)) >> toU64(1)) : sum
1083
+ }
1084
+
984
1085
  /**
985
1086
  * When `quicListenerTakePaced` next lets `conn` send a full datagram, in the
986
1087
  * caller's milliseconds: `now` when it may already.
@@ -990,7 +1091,7 @@ export const quicListenerTakePaced = (conn: QuicConnection, now: i64): u8[] | nu
990
1091
  * @returns A time in milliseconds, `now` or later. It says nothing of whether a datagram is due.
991
1092
  */
992
1093
  export const quicListenerPaceTime = (conn: QuicConnection, now: i64): i64 =>
993
- now + conn.recovery.pacerDelay(now, QUIC_CONN_DATAGRAM_SIZE)
1094
+ quicListenerLater(now, conn.recovery.pacerDelay(now, QUIC_CONN_DATAGRAM_SIZE))
994
1095
 
995
1096
  /** The most credit a paced flight waits for before it starts: four datagrams. */
996
1097
  export const QUIC_LISTENER_FLIGHT_QUANTUM: i32 = 4800
@@ -1012,7 +1113,7 @@ const quicListenerQuantum = (conn: QuicConnection): i32 =>
1012
1113
  * @returns A time in milliseconds, `now` or later. It says nothing of whether a datagram is due.
1013
1114
  */
1014
1115
  export const quicListenerFlightTime = (conn: QuicConnection, now: i64): i64 =>
1015
- now + conn.recovery.pacerDelay(now, quicListenerQuantum(conn))
1116
+ quicListenerLater(now, conn.recovery.pacerDelay(now, quicListenerQuantum(conn)))
1016
1117
 
1017
1118
  /** The most datagrams one GSO send carries: Linux's `UDP_MAX_SEGMENTS`. */
1018
1119
  export const QUIC_LISTENER_FLIGHT_MAX: i32 = 64
@@ -1070,28 +1171,36 @@ export const quicListenerTakeFlight = (
1070
1171
  if (conn.recovery.pacerDelay(now, quicListenerQuantum(conn)) > 0) {
1071
1172
  return length
1072
1173
  }
1174
+ // `length` counts up from 0 by datagrams `takeDatagramWithin` fitted in
1175
+ // `cap - length`, so it stays within `cap`; the sums are taken in `i64`,
1176
+ // where they cannot overflow. A window past the end of an `i32` sums to a
1177
+ // negative index, which `takeDatagramWithin` refuses, as it does any
1178
+ // window outside `buf`.
1179
+ let count: i32 = 0
1073
1180
  while (
1074
- flight.count < QUIC_LISTENER_FLIGHT_MAX &&
1075
- length + QUIC_CONN_DATAGRAM_SIZE <= cap &&
1181
+ count < QUIC_LISTENER_FLIGHT_MAX &&
1182
+ toI64(length) + toI64(QUIC_CONN_DATAGRAM_SIZE) <= toI64(cap) &&
1076
1183
  conn.recovery.pacerDelay(now, conn.datagramSize()) <= 0
1077
1184
  ) {
1078
1185
  // The codepoint `takeDatagramInto` would mark the next datagram with: one send carries one.
1079
1186
  const next: i32 = conn.ecnMarking ? conn.recovery.ecnCodepoint() : 0
1080
- if (flight.count > 0 && next !== flight.ecn) {
1187
+ if (count > 0 && next !== flight.ecn) {
1081
1188
  return length
1082
1189
  }
1083
- const room: i32 = flight.count > 0 && flight.segment < cap - length ? flight.segment : cap - length
1084
- const n: i32 = conn.takeDatagramWithin(buf, at + length, room, now)
1190
+ const left: i32 = toI32(toI64(cap) - toI64(length))
1191
+ const room: i32 = count > 0 && flight.segment < left ? flight.segment : left
1192
+ const n: i32 = conn.takeDatagramWithin(buf, toI32(toI64(at) + toI64(length)), room, now)
1085
1193
  if (n <= 0) {
1086
1194
  return length
1087
1195
  }
1088
1196
  conn.recovery.onPaced(now, n)
1089
- if (flight.count === 0) {
1197
+ if (count === 0) {
1090
1198
  flight.segment = n
1091
1199
  flight.ecn = conn.datagramEcn
1092
1200
  }
1093
- flight.count = flight.count + 1
1094
- length = length + n
1201
+ count += 1
1202
+ flight.count = count
1203
+ length = toI32(toI64(length) + toI64(n))
1095
1204
  if (n < flight.segment || n < conn.datagramSize()) {
1096
1205
  return length
1097
1206
  }
@@ -1102,8 +1211,9 @@ export const quicListenerTakeFlight = (
1102
1211
  /**
1103
1212
  * Hands `conn` the datagrams of one receive, `buf[off .. off + len)`, at
1104
1213
  * `now`: one datagram when `segment` is 0, else a GRO receive cut every
1105
- * `segment` bytes (the last may be shorter). Each is read in place. Answers
1106
- * how many it handed over.
1214
+ * `segment` bytes (the last may be shorter). Each is read in place, with the
1215
+ * ECN bits the caller set in `conn.arrivalEcn`. Answers how many it handed
1216
+ * over.
1107
1217
  *
1108
1218
  * @param conn The connection the datagrams belong to.
1109
1219
  * @param buf The receive buffer; read, not kept.
@@ -1126,12 +1236,17 @@ export const quicListenerReceiveSegments = (
1126
1236
  conn.receiveWindow(buf, off, len, now)
1127
1237
  return 1
1128
1238
  }
1239
+ // `at` climbs from 0 to `len` by segments of at least a byte, so the sums,
1240
+ // taken in `i64`, fit, and `count` stays below `len`. A window past the end
1241
+ // of an `i32` sums to a negative index, which `receiveWindow` drops, as it
1242
+ // does any window outside `buf`.
1129
1243
  let count: i32 = 0
1130
1244
  let at: i32 = 0
1131
- while (at < len) {
1132
- const n: i32 = len - at < segment ? len - at : segment
1133
- conn.receiveWindow(buf, off + at, n, now)
1134
- at = at + n
1245
+ while (at < len && count < 2147483647) {
1246
+ const left: i32 = toI32(toI64(len) - toI64(at))
1247
+ const n: i32 = left < segment ? left : segment
1248
+ conn.receiveWindow(buf, toI32(toI64(off) + toI64(at)), n, now)
1249
+ at = toI32(toI64(at) + toI64(n))
1135
1250
  count += 1
1136
1251
  }
1137
1252
  return count