nish 0.44.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 (111) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/package.json +5 -5
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -290,7 +290,12 @@ const quicListenerWipeRoom = (bytes: u8[], room: i32): void => {
290
290
 
291
291
  /** An empty array with room for `size` bytes, so pushing up to that many allocates nothing. */
292
292
  const quicListenerRoom = (size: i32): u8[] => {
293
- 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
+ }
294
299
  while (out.length > 0) {
295
300
  out.pop()
296
301
  }
@@ -299,7 +304,7 @@ const quicListenerRoom = (size: i32): u8[] => {
299
304
 
300
305
  /** Sets `bytes`' length to `length` in the room it already has: popped, or pushed with zeros. */
301
306
  const quicListenerResize = (bytes: u8[], length: i32): void => {
302
- while (toI32(bytes.length) > length) {
307
+ while (bytes.length > 0 && toI32(bytes.length) > length) {
303
308
  bytes.pop()
304
309
  }
305
310
  while (toI32(bytes.length) < length) {
@@ -315,10 +320,13 @@ const quicListenerRefill = (to: u8[], from: u8[], at: i32, length: i32): void =>
315
320
 
316
321
  /** Writes `value`'s low `size` bytes into `buf` at `at`, big-endian. */
317
322
  const quicListenerPutNumber = (buf: u8[], at: i32, value: i64, size: i32): void => {
318
- for (let k: i32 = 0; k < size; k += 1) {
319
- if (at + k >= 0 && at + k < toI32(buf.length)) {
320
- buf[at + k] = toU8(toI32((value >> (toI64(size - 1 - k) * 8)) & 255))
321
- }
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
322
330
  }
323
331
  }
324
332
 
@@ -338,10 +346,6 @@ const quicListenerRetryNonce = (): u8[] => [
338
346
  0x46, 0x15, 0x99, 0xd3, 0x5d, 0x63, 0x2b, 0xf2, 0x23, 0x98, 0x25, 0xbb,
339
347
  ]
340
348
 
341
- /** Whether `[at, at + len)` is a window inside an array of `length` elements: `p256WindowFits`' test. */
342
- const quicListenerWindowFits = (length: i32, at: i32, len: i32): boolean =>
343
- at >= 0 && len >= 0 && at <= length - len
344
-
345
349
  /**
346
350
  * The server's answers to datagrams no connection owns, under one
347
351
  * `QuicServerConfig`. Make one when the server starts, with the same
@@ -392,7 +396,7 @@ export class QuicListener {
392
396
  private retryKey: AesKey
393
397
  /** What `handle` answers into before it copies the answer out. */
394
398
  spare: QuicListenerAnswer
395
- private counter: i64 = 0
399
+ private counter: u64 = 0
396
400
  /** The latest time the caller gave, in milliseconds; time never runs backwards here. */
397
401
  private now: i64 = 0
398
402
  /** When the reset budget was last topped up (-1: never yet), and the stateless resets that may go now. */
@@ -400,9 +404,12 @@ export class QuicListener {
400
404
  private resetBudget: i32 = 0
401
405
  /** Where the unused bytes of `pool` start: `QUIC_LISTENER_POOL` when it is spent. */
402
406
  poolHead: i32 = 0
403
- /** Stateless resets sent, and ones the rate limit held back. */
407
+ /** Stateless resets sent, held at 2147483647. */
404
408
  resetsSent: i32 = 0
405
- /** 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
+ */
406
413
  resetsLimited: i32 = 0
407
414
  /** Whether the configuration and entropy were usable. */
408
415
  private usable: boolean = false
@@ -483,29 +490,38 @@ export class QuicListener {
483
490
  if (this.poolHead >= QUIC_LISTENER_POOL) {
484
491
  this.refill()
485
492
  }
486
- let b: i32 = 0
487
- if (this.poolHead >= 0 && this.poolHead < toI32(this.pool.length)) {
488
- b = toI32(this.pool[this.poolHead])
489
- 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
490
499
  }
491
- this.poolHead = this.poolHead + 1
500
+ const b: i32 = toI32(pool[head])
501
+ pool[head] = toU8(0)
502
+ this.poolHead = head + 1
492
503
  return b
493
504
  }
494
505
 
495
506
  /** Writes `count` unpredictable bytes into `out` at `at`. */
496
507
  private randomInto(out: u8[], at: i32, count: i32): void {
508
+ let i: i32 = at
497
509
  for (let k: i32 = 0; k < count; k += 1) {
498
510
  const b: i32 = this.randomByte()
499
- if (at + k >= 0 && at + k < toI32(out.length)) {
500
- out[at + k] = toU8(b)
511
+ if (i < toI32(out.length)) {
512
+ if (i >= 0) {
513
+ out[i] = toU8(b)
514
+ }
515
+ i += 1
501
516
  }
502
517
  }
503
518
  }
504
519
 
505
520
  /** The generator's next block, HMAC-SHA256 under the seed of the counter, into `pool`. */
506
521
  private refill(): void {
507
- quicListenerPutNumber(this.block, 0, this.counter, QUIC_LISTENER_BLOCK)
508
- 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)
509
525
  const mac: HmacSha256Scratch = this.kdf.sha256
510
526
  {
511
527
  using _scope = arena()
@@ -538,7 +554,8 @@ export class QuicListener {
538
554
  * `QUIC_LISTEN_RETRY`, `QUIC_LISTEN_INVALID_TOKEN` or `QUIC_LISTEN_STATELESS_RESET`.
539
555
  */
540
556
  handleWindow(datagram: u8[], at: i32, len: i32, address: u8[], now: i64, answer: QuicListenerAnswer): i32 {
541
- 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) {
542
559
  panic(
543
560
  `QuicListener.handleWindow: the window [${at}, ${at} + ${len}) is outside a datagram of ${toI32(datagram.length)} bytes`
544
561
  )
@@ -555,7 +572,8 @@ export class QuicListener {
555
572
  return answer.kind
556
573
  }
557
574
  const header: QuicHeader = this.header
558
- 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)
559
577
  if (header.error === QUIC_ERR_VERSION) {
560
578
  // §5.2.2: answer an unsupported version only in a datagram that could
561
579
  // start a connection, and §6.1: never a Version Negotiation packet.
@@ -571,7 +589,12 @@ export class QuicListener {
571
589
  // RFC 9000 §21.11: an ID whose worker byte names a sibling in the
572
590
  // steered group is that sibling's, and a reset under the shared key
573
591
  // would end its live connection. Byte 0 at or above N is no worker's.
574
- 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])
575
598
  if (
576
599
  quicWorkerByte(this.config) >= 0 &&
577
600
  owner < this.config.workerCount &&
@@ -579,7 +602,7 @@ export class QuicListener {
579
602
  ) {
580
603
  return answer.kind
581
604
  }
582
- this.statelessReset(answer, len, datagram, header.dcidStart, header.dcidLength)
605
+ this.statelessReset(answer, len, datagram, dcidAt, header.dcidLength)
583
606
  return answer.kind
584
607
  }
585
608
  // §14.1, §7.2, §5.2.2: only an Initial in a full-sized datagram, sent to
@@ -588,7 +611,14 @@ export class QuicListener {
588
611
  return answer.kind
589
612
  }
590
613
  const tokenAt: i32 = header.tokenStart
591
- 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) {
592
622
  if (!this.config.retry) {
593
623
  answer.kind = QUIC_LISTEN_ACCEPT
594
624
  return answer.kind
@@ -596,15 +626,13 @@ export class QuicListener {
596
626
  this.retry(answer, datagram, header, address)
597
627
  return answer.kind
598
628
  }
599
- 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) {
600
633
  answer.kind = QUIC_LISTEN_ACCEPT
601
634
  answer.retried = true
602
- quicListenerRefill(
603
- answer.originalDcid,
604
- datagram,
605
- tokenAt + QUIC_LISTENER_TOKEN_HEAD,
606
- toI32(datagram[tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1])
607
- )
635
+ quicListenerRefill(answer.originalDcid, datagram, tokenAt + 10, odcidLength)
608
636
  quicListenerRefill(answer.retryScid, datagram, header.dcidStart, header.dcidLength)
609
637
  return answer.kind
610
638
  }
@@ -655,11 +683,15 @@ export class QuicListener {
655
683
  const unused: i32 = this.randomByte()
656
684
  const dcidLength: i32 = header.dcidLength
657
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
+ }
658
690
  const reply: u8[] = answer.reply
659
691
  quicListenerResize(reply, 11 + dcidLength + scidLength)
660
- reply[0] = toU8((unused & 0x3f) | 0xc0)
692
+ quicListenerPutNumber(reply, 0, toI64((unused & 0x3f) | 0xc0), 1)
661
693
  quicListenerPutNumber(reply, 1, 0, 4)
662
- reply[5] = toU8(scidLength)
694
+ quicListenerPutNumber(reply, 5, toI64(scidLength), 1)
663
695
  quicPacketCopy(reply, 6, datagram, header.scidStart, scidLength)
664
696
  const dcidAt: i32 = 6 + scidLength
665
697
  quicListenerPutNumber(reply, dcidAt, toI64(dcidLength), 1)
@@ -710,35 +742,54 @@ export class QuicListener {
710
742
  * and still within its lifetime: the lengths add up, the MAC matches
711
743
  * (compared in constant time), and it was issued no later than now and
712
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.
713
747
  */
714
- private tokenValid(datagram: u8[], header: QuicHeader, address: u8[]): boolean {
748
+ private tokenOdcid(datagram: u8[], header: QuicHeader, address: u8[]): i32 {
715
749
  const tokenAt: i32 = header.tokenStart
716
750
  const length: i32 = header.tokenLength
717
751
  if (length < QUIC_LISTENER_TOKEN_HEAD + QUIC_LISTENER_TOKEN_MAC_SIZE) {
718
- 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
719
763
  }
720
- const odcidLength: i32 = toI32(datagram[tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1])
721
764
  const head: i32 = QUIC_LISTENER_TOKEN_HEAD + odcidLength
722
- if (odcidLength > QUIC_MAX_CID_LENGTH || head + QUIC_LISTENER_TOKEN_MAC_SIZE !== length) {
723
- return false
765
+ if (head + QUIC_LISTENER_TOKEN_MAC_SIZE !== length) {
766
+ return -1
724
767
  }
725
768
  this.tokenMac(datagram, tokenAt, head, datagram, header.dcidStart, header.dcidLength, address)
726
769
  const same: boolean = timingSafeEqualAt(
727
770
  this.tag,
728
771
  0,
729
772
  datagram,
730
- tokenAt + head,
773
+ toI32(toI64(tokenAt) + toI64(head)),
731
774
  QUIC_LISTENER_TOKEN_MAC_SIZE
732
775
  )
733
776
  secureZero(this.tag)
734
777
  if (!same) {
735
- return false
778
+ return -1
736
779
  }
737
- let issued: i64 = 0
738
- for (let k: i32 = 1; k < QUIC_LISTENER_TOKEN_HEAD - 1; k += 1) {
739
- 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
740
785
  }
741
- 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
742
793
  }
743
794
 
744
795
  /**
@@ -751,6 +802,10 @@ export class QuicListener {
751
802
  private retry(answer: QuicListenerAnswer, datagram: u8[], header: QuicHeader, address: u8[]): void {
752
803
  const scidLength: i32 = header.scidLength
753
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
+ }
754
809
  const retryScidAt: i32 = 7 + scidLength
755
810
  const tokenAt: i32 = retryScidAt + QUIC_CONN_CID_LENGTH
756
811
  const macAt: i32 = tokenAt + QUIC_LISTENER_TOKEN_HEAD + odcidLength
@@ -761,20 +816,20 @@ export class QuicListener {
761
816
  const worker: i32 = quicWorkerByte(this.config)
762
817
  if (worker >= 0) {
763
818
  // The retried Initial is sent to this ID, so it steers back here.
764
- reply[retryScidAt] = toU8(worker)
819
+ quicListenerPutNumber(reply, retryScidAt, toI64(worker), 1)
765
820
  }
766
- reply[tokenAt] = toU8(QUIC_LISTENER_TOKEN_MARKER)
821
+ quicListenerPutNumber(reply, tokenAt, toI64(QUIC_LISTENER_TOKEN_MARKER), 1)
767
822
  quicListenerPutNumber(reply, tokenAt + 1, this.now, 8)
768
- reply[tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1] = toU8(odcidLength)
823
+ quicListenerPutNumber(reply, tokenAt + QUIC_LISTENER_TOKEN_HEAD - 1, toI64(odcidLength), 1)
769
824
  quicPacketCopy(reply, tokenAt + QUIC_LISTENER_TOKEN_HEAD, datagram, header.dcidStart, odcidLength)
770
825
  this.tokenMac(reply, tokenAt, macAt - tokenAt, reply, retryScidAt, QUIC_CONN_CID_LENGTH, address)
771
826
  quicPacketCopy(reply, macAt, this.tag, 0, QUIC_LISTENER_TOKEN_MAC_SIZE)
772
827
  secureZero(this.tag)
773
- reply[0] = toU8((this.randomByte() & 15) | 0xf0)
828
+ quicListenerPutNumber(reply, 0, toI64((this.randomByte() & 15) | 0xf0), 1)
774
829
  quicListenerPutNumber(reply, 1, 1, 4)
775
- reply[5] = toU8(scidLength)
830
+ quicListenerPutNumber(reply, 5, toI64(scidLength), 1)
776
831
  quicPacketCopy(reply, 6, datagram, header.scidStart, scidLength)
777
- reply[retryScidAt - 1] = toU8(QUIC_CONN_CID_LENGTH)
832
+ quicListenerPutNumber(reply, retryScidAt - 1, toI64(QUIC_CONN_CID_LENGTH), 1)
778
833
  if (this.retryTag(reply, tagAt, datagram, header.dcidStart, odcidLength)) {
779
834
  answer.kind = QUIC_LISTEN_RETRY
780
835
  } else {
@@ -791,8 +846,12 @@ export class QuicListener {
791
846
  * Answers whether it was made.
792
847
  */
793
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
+ }
794
853
  using _scope = arena()
795
- const pseudo: u8[] = new Array<u8>(1 + odcidLength + tagAt)
854
+ const pseudo: u8[] = new Array<u8>(toU16(1 + odcidLength + tagAt))
796
855
  quicListenerPutNumber(pseudo, 0, toI64(odcidLength), 1)
797
856
  quicPacketCopy(pseudo, 1, datagram, odcidAt, odcidLength)
798
857
  quicPacketCopy(pseudo, 1 + odcidLength, reply, 0, tagAt)
@@ -863,7 +922,7 @@ export class QuicListener {
863
922
  0,
864
923
  this.none
865
924
  )
866
- const payloadLength: i32 = frameEnd - headerLength
925
+ const payloadLength: i32 = headerLength > 0 && frameEnd > 0 ? frameEnd - headerLength : -1
867
926
  const put: i32 =
868
927
  headerLength > 0 && frameEnd > 0
869
928
  ? quicPutLongHeader(
@@ -901,22 +960,39 @@ export class QuicListener {
901
960
 
902
961
  /** Takes one stateless reset from the rate limit's budget, topping it up first. Answers whether one was there. */
903
962
  private takeReset(): boolean {
904
- if (this.resetRefilled < 0) {
905
- this.resetRefilled = this.now
906
- 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
907
971
  }
908
- const earned: i64 = (this.now - this.resetRefilled) / QUIC_LISTENER_RESET_INTERVAL
909
- if (earned > 0) {
910
- const room: i64 = toI64(QUIC_LISTENER_RESET_BURST - this.resetBudget)
911
- this.resetBudget = this.resetBudget + toI32(earned < room ? earned : room)
912
- 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))
913
981
  }
914
- if (this.resetBudget <= 0) {
915
- 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
+ }
916
989
  return false
917
990
  }
918
- this.resetBudget = this.resetBudget - 1
919
- 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
+ }
920
996
  return true
921
997
  }
922
998
 
@@ -937,22 +1013,28 @@ export class QuicListener {
937
1013
  dcidAt: i32,
938
1014
  dcidLength: i32
939
1015
  ): void {
940
- 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()) {
941
1019
  return
942
1020
  }
943
1021
  let size: i32 = n - 1
944
- if (n > QUIC_LISTENER_SHORT_TRIGGER) {
945
- const most: i32 = n - 1 < QUIC_CONN_DATAGRAM_SIZE ? n - 1 : QUIC_CONN_DATAGRAM_SIZE
946
- 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
947
1025
  const high: i32 = this.randomByte()
948
- const r: i32 = (high << 8) | this.randomByte()
949
- 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))
950
1029
  }
951
1030
  const reply: u8[] = answer.reply
952
- const tokenAt: i32 = size - QUIC_RESET_TOKEN_SIZE
1031
+ const tokenAt: i32 = toI32(toI64(size) - toI64(QUIC_RESET_TOKEN_SIZE))
953
1032
  quicListenerResize(reply, size)
954
1033
  this.randomInto(reply, QUIC_LISTENER_FROM, tokenAt)
955
- 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
+ }
956
1038
  quicConnResetTokenInto(
957
1039
  this.kdf.sha256,
958
1040
  this.config.statelessResetKey,
@@ -987,6 +1069,19 @@ export const quicListenerTakePaced = (conn: QuicConnection, now: i64): u8[] | nu
987
1069
  return out
988
1070
  }
989
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
+
990
1085
  /**
991
1086
  * When `quicListenerTakePaced` next lets `conn` send a full datagram, in the
992
1087
  * caller's milliseconds: `now` when it may already.
@@ -996,7 +1091,7 @@ export const quicListenerTakePaced = (conn: QuicConnection, now: i64): u8[] | nu
996
1091
  * @returns A time in milliseconds, `now` or later. It says nothing of whether a datagram is due.
997
1092
  */
998
1093
  export const quicListenerPaceTime = (conn: QuicConnection, now: i64): i64 =>
999
- now + conn.recovery.pacerDelay(now, QUIC_CONN_DATAGRAM_SIZE)
1094
+ quicListenerLater(now, conn.recovery.pacerDelay(now, QUIC_CONN_DATAGRAM_SIZE))
1000
1095
 
1001
1096
  /** The most credit a paced flight waits for before it starts: four datagrams. */
1002
1097
  export const QUIC_LISTENER_FLIGHT_QUANTUM: i32 = 4800
@@ -1018,7 +1113,7 @@ const quicListenerQuantum = (conn: QuicConnection): i32 =>
1018
1113
  * @returns A time in milliseconds, `now` or later. It says nothing of whether a datagram is due.
1019
1114
  */
1020
1115
  export const quicListenerFlightTime = (conn: QuicConnection, now: i64): i64 =>
1021
- now + conn.recovery.pacerDelay(now, quicListenerQuantum(conn))
1116
+ quicListenerLater(now, conn.recovery.pacerDelay(now, quicListenerQuantum(conn)))
1022
1117
 
1023
1118
  /** The most datagrams one GSO send carries: Linux's `UDP_MAX_SEGMENTS`. */
1024
1119
  export const QUIC_LISTENER_FLIGHT_MAX: i32 = 64
@@ -1076,28 +1171,36 @@ export const quicListenerTakeFlight = (
1076
1171
  if (conn.recovery.pacerDelay(now, quicListenerQuantum(conn)) > 0) {
1077
1172
  return length
1078
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
1079
1180
  while (
1080
- flight.count < QUIC_LISTENER_FLIGHT_MAX &&
1081
- length + QUIC_CONN_DATAGRAM_SIZE <= cap &&
1181
+ count < QUIC_LISTENER_FLIGHT_MAX &&
1182
+ toI64(length) + toI64(QUIC_CONN_DATAGRAM_SIZE) <= toI64(cap) &&
1082
1183
  conn.recovery.pacerDelay(now, conn.datagramSize()) <= 0
1083
1184
  ) {
1084
1185
  // The codepoint `takeDatagramInto` would mark the next datagram with: one send carries one.
1085
1186
  const next: i32 = conn.ecnMarking ? conn.recovery.ecnCodepoint() : 0
1086
- if (flight.count > 0 && next !== flight.ecn) {
1187
+ if (count > 0 && next !== flight.ecn) {
1087
1188
  return length
1088
1189
  }
1089
- const room: i32 = flight.count > 0 && flight.segment < cap - length ? flight.segment : cap - length
1090
- 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)
1091
1193
  if (n <= 0) {
1092
1194
  return length
1093
1195
  }
1094
1196
  conn.recovery.onPaced(now, n)
1095
- if (flight.count === 0) {
1197
+ if (count === 0) {
1096
1198
  flight.segment = n
1097
1199
  flight.ecn = conn.datagramEcn
1098
1200
  }
1099
- flight.count = flight.count + 1
1100
- length = length + n
1201
+ count += 1
1202
+ flight.count = count
1203
+ length = toI32(toI64(length) + toI64(n))
1101
1204
  if (n < flight.segment || n < conn.datagramSize()) {
1102
1205
  return length
1103
1206
  }
@@ -1133,12 +1236,17 @@ export const quicListenerReceiveSegments = (
1133
1236
  conn.receiveWindow(buf, off, len, now)
1134
1237
  return 1
1135
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`.
1136
1243
  let count: i32 = 0
1137
1244
  let at: i32 = 0
1138
- while (at < len) {
1139
- const n: i32 = len - at < segment ? len - at : segment
1140
- conn.receiveWindow(buf, off + at, n, now)
1141
- 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))
1142
1250
  count += 1
1143
1251
  }
1144
1252
  return count