@amritk/nish-aarch64-linux 0.49.1 → 0.51.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.
Files changed (107) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +237 -160
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +94 -11
  7. package/runtime/nish.h +5 -0
  8. package/runtime/runtime-host.c +21 -5
  9. package/std/README.md +14 -11
  10. package/std/base64.ts +74 -35
  11. package/std/bytes.ts +207 -97
  12. package/std/cli-args.ts +150 -107
  13. package/std/compress/checksum.ts +68 -10
  14. package/std/compress/deflate.ts +213 -115
  15. package/std/compress/gzip.ts +124 -49
  16. package/std/compress/zlib.ts +57 -15
  17. package/std/crypto/aes.ts +51 -9
  18. package/std/crypto/base64url.ts +93 -66
  19. package/std/crypto/bcrypt.ts +25 -21
  20. package/std/crypto/ed25519.ts +12 -10
  21. package/std/crypto/hkdf.ts +405 -283
  22. package/std/crypto/hmac.ts +484 -149
  23. package/std/crypto/jwt.ts +51 -40
  24. package/std/crypto/p256.ts +14 -19
  25. package/std/crypto/pki-roots.ts +35 -12
  26. package/std/crypto/pki.ts +43 -55
  27. package/std/crypto/rsa-sign.ts +1635 -0
  28. package/std/crypto/rsa.ts +26 -4
  29. package/std/crypto/sha256.ts +212 -90
  30. package/std/crypto/sha512.ts +135 -49
  31. package/std/crypto/x25519.ts +12 -11
  32. package/std/crypto/x509.ts +164 -40
  33. package/std/gpu/sys.ts +490 -0
  34. package/std/json-equal.ts +33 -18
  35. package/std/json-format.ts +39 -8
  36. package/std/json-number.ts +15 -3
  37. package/std/json-tokenizer.ts +4 -3
  38. package/std/json-typed.ts +12 -6
  39. package/std/json-value.ts +236 -119
  40. package/std/json.ts +81 -46
  41. package/std/net/buffer-pool.ts +25 -14
  42. package/std/net/dns-config.ts +18 -5
  43. package/std/net/dns-message.ts +60 -27
  44. package/std/net/dns-resolver.ts +35 -16
  45. package/std/net/dns.ts +3 -2
  46. package/std/net/fetch.ts +224 -61
  47. package/std/net/handler.ts +189 -88
  48. package/std/net/hpack.ts +216 -103
  49. package/std/net/http-compress.ts +104 -42
  50. package/std/net/http-fields.ts +327 -31
  51. package/std/net/http-server.ts +326 -24
  52. package/std/net/http1-client-codec.ts +159 -55
  53. package/std/net/http1-codec.ts +427 -237
  54. package/std/net/http1-server.ts +220 -38
  55. package/std/net/http2-cleartext.ts +10 -4
  56. package/std/net/http2-frame.ts +2 -2
  57. package/std/net/http2-tls.ts +3 -3
  58. package/std/net/http2.ts +69 -15
  59. package/std/net/http3-frame.ts +11 -14
  60. package/std/net/http3-server.ts +139 -60
  61. package/std/net/http3.ts +140 -79
  62. package/std/net/multipart.ts +592 -0
  63. package/std/net/outbound.ts +24 -8
  64. package/std/net/postgres-async.ts +97 -64
  65. package/std/net/postgres-carrier.ts +28 -4
  66. package/std/net/postgres.ts +166 -65
  67. package/std/net/prefork.ts +86 -5
  68. package/std/net/protobuf.ts +6 -2
  69. package/std/net/qpack.ts +147 -40
  70. package/std/net/quic-conn-ack.ts +27 -7
  71. package/std/net/quic-conn-cid.ts +13 -7
  72. package/std/net/quic-datagram.ts +6 -5
  73. package/std/net/quic-frame.ts +67 -23
  74. package/std/net/quic-listener.ts +61 -39
  75. package/std/net/quic-packet.ts +91 -50
  76. package/std/net/quic-recovery.ts +11 -9
  77. package/std/net/quic-stream.ts +36 -27
  78. package/std/net/quic.ts +84 -54
  79. package/std/net/router.ts +136 -24
  80. package/std/net/serve.ts +59 -5
  81. package/std/net/static.ts +86 -36
  82. package/std/net/tls/client.ts +81 -34
  83. package/std/net/tls/codec.ts +3 -4
  84. package/std/net/tls/record.ts +80 -35
  85. package/std/net/tls/schedule.ts +339 -149
  86. package/std/net/tls/ticket.ts +6 -5
  87. package/std/net/tls-tcp.ts +69 -30
  88. package/std/net/tls.ts +194 -50
  89. package/std/net/upstream.ts +83 -38
  90. package/std/net/url.ts +170 -94
  91. package/std/net/websocket.ts +19 -20
  92. package/std/net/webtransport.ts +123 -33
  93. package/std/raster.ts +12 -2
  94. package/std/regex.ts +600 -388
  95. package/std/sqlite.ts +37 -19
  96. package/std/sync/ring.ts +232 -115
  97. package/std/sync/shm.ts +11 -4
  98. package/std/temporal.ts +232 -133
  99. package/std/term-io.ts +12 -6
  100. package/std/term-pixels.ts +176 -24
  101. package/std/term-rate.ts +8 -3
  102. package/std/term.ts +4 -4
  103. package/std/text.ts +38 -16
  104. package/std/yaml-json.ts +238 -106
  105. package/std/yaml-lines.ts +47 -27
  106. package/std/yaml-write.ts +58 -24
  107. package/std/yaml.ts +808 -395
package/std/net/qpack.ts CHANGED
@@ -82,8 +82,8 @@
82
82
  import {
83
83
  HPACK_OK,
84
84
  HpackHuffman,
85
+ hpackHuffmanAppend,
85
86
  hpackHuffmanDecode,
86
- hpackHuffmanEncode,
87
87
  hpackHuffmanLength,
88
88
  } from "nish/net/hpack"
89
89
 
@@ -162,6 +162,12 @@ export const QPACK_REASON_ACKNOWLEDGMENT: i32 = 11
162
162
  /** An Insert Count Increment (§4.4.3). */
163
163
  export const QPACK_REASON_INCREMENT: i32 = 12
164
164
 
165
+ /**
166
+ * A `QpackDecoder` made with a negative `maxFieldSectionSize`, a limit no
167
+ * section can be held to: every `decode` refuses with it, before reading.
168
+ */
169
+ export const QPACK_REASON_LIMIT: i32 = 13
170
+
165
171
  /**
166
172
  * Where the instruction counters stop. A peer may send a legal one-byte
167
173
  * instruction as often as it likes, and an `i32` that overflows panics, so a
@@ -510,13 +516,23 @@ const qpackStaticValue = (index: i32): string => {
510
516
  return ""
511
517
  }
512
518
  }
513
- /** Panics unless `buf[off, off + len)` lies inside `buf`. */
519
+ /**
520
+ * Panics unless `buf[off, off + len)` lies inside `buf`. In the window contract's shape
521
+ * (`src/window-contract.ts`), so a caller that proved the window has proved this.
522
+ */
514
523
  const qpackCheckWindow = (where: string, buf: u8[], off: i32, len: i32): void => {
515
- if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(buf.length)) {
516
- panic(`${where}: the window [${off}, ${off} + ${len}) is outside a buffer of ${buf.length} bytes`)
524
+ const size: i32 = toI32(buf.length)
525
+ if (off < 0 || len < 0 || off > size || len > size - off) {
526
+ panic(`${where}: the window [${off}, ${off} + ${len}) is outside a buffer of ${size} bytes`)
517
527
  }
518
528
  }
519
529
 
530
+ /** Whether `buf[off, off + len)` lies inside `buf`: the test `qpackCheckWindow` panics on. */
531
+ const qpackWindowFits = (buf: u8[], off: i32, len: i32): boolean => {
532
+ const size: i32 = toI32(buf.length)
533
+ return off >= 0 && len >= 0 && off <= size && len <= size - off
534
+ }
535
+
520
536
  /** Whether `buf[off, off + len)` spells `text`, which is ASCII. */
521
537
  const qpackSameText = (buf: u8[], off: i32, len: i32, text: string): boolean => {
522
538
  if (len !== toI32(text.length)) {
@@ -551,53 +567,75 @@ const qpackPushBytes = (out: u8[], src: u8[], off: i32, len: i32): void => {
551
567
  }
552
568
 
553
569
  /**
554
- * Appends `value` as a §4.1.1 integer with a `prefixBits`-bit prefix, the first
555
- * byte's high bits being `flags`. A value outside 0 to 2^62 - 1, or a prefix
556
- * outside 1 to 8 bits, panics: both are the program's choice.
570
+ * Appends `value` as a §4.1.1 integer whose prefix is the low bits of the
571
+ * first byte, `max` being the prefix with every bit set (63 for a 6-bit
572
+ * prefix) and `flags` the bits above it. Every caller passes a literal
573
+ * `max` and a value that is a count, an index or a checked stream ID, so
574
+ * the integer is one §4.1.1 can carry; a `u64` cannot be negative.
557
575
  */
558
- const qpackPushInteger = (out: u8[], flags: i32, prefixBits: i32, value: i64): void => {
559
- if (prefixBits < 1 || prefixBits > 8 || value < 0 || value > QPACK_MAX_INTEGER) {
560
- panic(`qpackPushInteger: a value of ${value} with a ${prefixBits}-bit prefix`)
561
- }
562
- const max: i32 = (toI32(255) >> (toI32(8) - prefixBits)) & toI32(255)
563
- if (value < toI64(max)) {
576
+ const qpackPushInteger = (out: u8[], flags: i32, max: i32, value: u64): void => {
577
+ const prefix: u64 = toU64(max)
578
+ if (value < prefix) {
564
579
  out.push(toU8(flags | toI32(value)))
565
580
  return
566
581
  }
567
582
  out.push(toU8(flags | max))
568
- let rest: i64 = value - toI64(max)
569
- while (rest >= toI64(128)) {
570
- out.push(toU8(toI32(rest & toI64(127)) | 128))
571
- rest = rest >> toI64(7)
583
+ let rest: u64 = value - prefix
584
+ while (rest >= toU64(128)) {
585
+ out.push(toU8(toI32(rest & toU64(127)) | 128))
586
+ rest = rest >> toU64(7)
572
587
  }
573
588
  out.push(toU8(toI32(rest)))
574
589
  }
575
590
 
576
591
  /**
577
- * Appends `src[off, off + len)` as a §4.1.2 string literal with a
578
- * `prefixBits`-bit prefix: the Huffman flag is the prefix's top bit, the
579
- * length the `prefixBits - 1` bits below it, and `flags` the bits above.
580
- * When `huffman` is true the string is Huffman-coded if that makes it
581
- * shorter, and sent raw otherwise, as an empty string always is.
592
+ * The bytes the string `src[off, off + len)` takes on the wire before its
593
+ * length prefix: its Huffman length when `huffman` is true and that is
594
+ * shorter, else `len`. The caller has checked the window.
595
+ */
596
+ const qpackStringLength = (h: HpackHuffman, src: u8[], off: i32, len: i32, huffman: boolean): i32 => {
597
+ // Never taken: `encodeLine` checked the window. Widened, so the sum of two
598
+ // `i32`s cannot wrap.
599
+ const stop: i64 = toI64(off) + toI64(len)
600
+ if (!huffman || off < 0 || len < 0 || stop > toI64(src.length)) {
601
+ return len
602
+ }
603
+ // At most the length, so the `i32` holds it exactly; `[off, end)` is the
604
+ // window, spelled so the bounds prover reads it.
605
+ const end: i32 = toI32(stop)
606
+ if (off > end || end > toI32(src.length)) {
607
+ return len
608
+ }
609
+ const coded: i32 = hpackHuffmanLength(h, src, off, end - off)
610
+ return coded < len ? coded : len
611
+ }
612
+
613
+ /**
614
+ * Appends `src[off, off + len)` as a §4.1.2 string literal: the Huffman
615
+ * flag is the bit `huffmanBit`, the length an integer whose prefix is `max`
616
+ * (every bit below the flag set), and `flags` the bits above.
617
+ * `coded` is what `qpackStringLength` answered for the same window, read
618
+ * before anything was written: the string is Huffman-coded when it is less
619
+ * than `len`, and sent raw otherwise. It checks nothing and cannot panic, so
620
+ * it may follow a push to an array the window shares.
582
621
  */
583
622
  const qpackPushString = (
584
623
  h: HpackHuffman,
585
624
  out: u8[],
586
625
  flags: i32,
587
- prefixBits: i32,
626
+ huffmanBit: i32,
627
+ max: i32,
588
628
  src: u8[],
589
629
  off: i32,
590
630
  len: i32,
591
- huffman: boolean
631
+ coded: i32
592
632
  ): void => {
593
- const lengthBits: i32 = prefixBits - 1
594
- const coded: i32 = huffman ? hpackHuffmanLength(h, src, off, len) : len
595
633
  if (coded < len) {
596
- qpackPushInteger(out, flags | (1 << lengthBits), lengthBits, toI64(coded))
597
- hpackHuffmanEncode(h, src, off, len, out)
634
+ qpackPushInteger(out, flags | huffmanBit, max, toU64(coded))
635
+ hpackHuffmanAppend(h, src, off, len, out)
598
636
  return
599
637
  }
600
- qpackPushInteger(out, flags, lengthBits, toI64(len))
638
+ qpackPushInteger(out, flags, max, toU64(len))
601
639
  qpackPushBytes(out, src, off, len)
602
640
  }
603
641
 
@@ -610,7 +648,10 @@ const qpackPushString = (
610
648
  * @param streamId The ID of the request stream whose section the peer's encoder may forget, 0 to 2^62 - 1.
611
649
  */
612
650
  export const qpackPushStreamCancellation = (out: u8[], streamId: i64): void => {
613
- qpackPushInteger(out, 64, 6, streamId)
651
+ if (streamId < 0 || streamId > QPACK_MAX_INTEGER) {
652
+ panic(`qpackPushStreamCancellation: a stream ID of ${streamId}, outside 0 to 2^62 - 1`)
653
+ }
654
+ qpackPushInteger(out, 64, 63, toU64(streamId))
614
655
  }
615
656
 
616
657
  /** The byte at `d.at` of the section being decoded, or -1 when the section has none left there. */
@@ -741,7 +782,18 @@ const qpackReadString = (d: QpackDecoder, src: u8[], huffmanBit: i32, lengthMax:
741
782
  }
742
783
  // At most `end - start`, so the `i32` holds it exactly.
743
784
  const length: i32 = toI32(n)
744
- d.at = toI32(toI64(start) + toI64(length))
785
+ // `decode` checked `[at, end)` inside `src`, so a string inside it is too: never taken.
786
+ const stop: i64 = toI64(start) + toI64(length)
787
+ if (start < 0 || length < 0 || stop > toI64(src.length)) {
788
+ return QPACK_REASON_TRUNCATED
789
+ }
790
+ // At most the length, so the `i32` holds it exactly; `[start, end)` is the
791
+ // string, spelled so the bounds prover reads it.
792
+ const end: i32 = toI32(stop)
793
+ if (start > end || end > toI32(src.length)) {
794
+ return QPACK_REASON_TRUNCATED
795
+ }
796
+ d.at = end
745
797
  // `room` never passes the limit it starts at; one that did would be refused like a string past it.
746
798
  const room: i64 = d.room
747
799
  if (room < 0 || room > toI64(d.maxFieldSectionSize)) {
@@ -755,7 +807,11 @@ const qpackReadString = (d: QpackDecoder, src: u8[], huffmanBit: i32, lengthMax:
755
807
  }
756
808
  const before: i32 = toI32(d.bytes.length)
757
809
  if (huffman) {
758
- if (hpackHuffmanDecode(d.huffman, src, start, length, d.bytes) !== HPACK_OK) {
810
+ // `qpackHuffmanFits` only reads `src`, so it still holds `[start, end)`: never taken.
811
+ if (end > toI32(src.length)) {
812
+ return QPACK_REASON_TRUNCATED
813
+ }
814
+ if (hpackHuffmanDecode(d.huffman, src, start, end - start, d.bytes) !== HPACK_OK) {
759
815
  return QPACK_REASON_HUFFMAN
760
816
  }
761
817
  } else {
@@ -834,12 +890,10 @@ export class QpackDecoder {
834
890
  /**
835
891
  * Makes a decoder with no fields, no connection error and no capacity instructions counted.
836
892
  * @param maxFieldSectionSize The largest field section `decode` accepts, as RFC 9114 §4.2.2
837
- * counts it: each field's name and value in bytes plus 32. A negative limit panics.
893
+ * counts it: each field's name and value in bytes plus 32. A negative limit is kept, and every
894
+ * `decode` answers QPACK_DECOMPRESSION_FAILED for it with `QPACK_REASON_LIMIT`.
838
895
  */
839
896
  constructor(maxFieldSectionSize: i32) {
840
- if (maxFieldSectionSize < 0) {
841
- panic(`QpackDecoder: a field-section limit of ${maxFieldSectionSize}`)
842
- }
843
897
  this.huffman = new HpackHuffman()
844
898
  this.maxFieldSectionSize = maxFieldSectionSize
845
899
  this.failed = QPACK_OK
@@ -1004,6 +1058,9 @@ export class QpackDecoder {
1004
1058
  * each told apart by its first byte's pattern (§4.5.2 to §4.5.6).
1005
1059
  */
1006
1060
  private decodeLines(src: u8[]): i64 {
1061
+ if (this.maxFieldSectionSize < 0) {
1062
+ return this.refuse(QPACK_REASON_LIMIT)
1063
+ }
1007
1064
  const insertCount: i64 = qpackReadInteger(this, src, 255)
1008
1065
  if (insertCount < 0) {
1009
1066
  return this.refuseInteger(insertCount)
@@ -1193,11 +1250,50 @@ export class QpackEncoder {
1193
1250
  ): void {
1194
1251
  qpackCheckWindow("QpackEncoder.encodeField", name, nameOff, nameLen)
1195
1252
  qpackCheckWindow("QpackEncoder.encodeField", value, valueOff, valueLen)
1253
+ this.encodeLine(out, name, nameOff, nameLen, value, valueOff, valueLen, neverIndexed, huffman)
1254
+ }
1255
+
1256
+ /**
1257
+ * `encodeField` for a field whose name and value are each a whole array,
1258
+ * which has no window to check and so cannot panic.
1259
+ * @param out The buffer the section is being written to; the field line goes after what it holds.
1260
+ * @param name The name, all of it; only read, and lowercase to match a static entry.
1261
+ * @param value The value, all of it; only read.
1262
+ * @param neverIndexed Whether to send the field as a literal with the N bit set.
1263
+ * @param huffman Whether a string may be Huffman-coded; it is only when that is shorter.
1264
+ */
1265
+ encodeWholeField(out: u8[], name: u8[], value: u8[], neverIndexed: boolean, huffman: boolean): void {
1266
+ const zero: i32 = 0
1267
+ this.encodeLine(
1268
+ out,
1269
+ name,
1270
+ zero,
1271
+ toI32(name.length),
1272
+ value,
1273
+ zero,
1274
+ toI32(value.length),
1275
+ neverIndexed,
1276
+ huffman
1277
+ )
1278
+ }
1279
+
1280
+ /** The field line `encodeField` appends, for windows the caller has checked. */
1281
+ private encodeLine(
1282
+ out: u8[],
1283
+ name: u8[],
1284
+ nameOff: i32,
1285
+ nameLen: i32,
1286
+ value: u8[],
1287
+ valueOff: i32,
1288
+ valueLen: i32,
1289
+ neverIndexed: boolean,
1290
+ huffman: boolean
1291
+ ): void {
1196
1292
  let nameIndex: i32 = -1
1197
1293
  for (let i: i32 = 0; i < QPACK_STATIC_LENGTH; i += 1) {
1198
1294
  if (qpackSameText(name, nameOff, nameLen, qpackStaticName(i))) {
1199
1295
  if (!neverIndexed && qpackSameText(value, valueOff, valueLen, qpackStaticValue(i))) {
1200
- qpackPushInteger(out, 192, 6, toI64(i))
1296
+ qpackPushInteger(out, 192, 63, toU64(i))
1201
1297
  return
1202
1298
  }
1203
1299
  if (nameIndex < 0) {
@@ -1210,13 +1306,24 @@ export class QpackEncoder {
1210
1306
  }
1211
1307
  }
1212
1308
  }
1309
+ // Never taken: `encodeField` checked both windows and `encodeWholeField`
1310
+ // passes whole arrays. Checked here, before the first byte goes out, so
1311
+ // a window outside its array writes no part of the field.
1312
+ if (!qpackWindowFits(name, nameOff, nameLen) || !qpackWindowFits(value, valueOff, valueLen)) {
1313
+ return
1314
+ }
1315
+ // Both lengths are read now: `out` may be `name` or `value`, and once a
1316
+ // byte is pushed the window facts no longer hold for the bounds prover.
1317
+ const nameCoded: i32 =
1318
+ nameIndex >= 0 ? 0 : qpackStringLength(this.huffman, name, nameOff, nameLen, huffman)
1319
+ const valueCoded: i32 = qpackStringLength(this.huffman, value, valueOff, valueLen, huffman)
1213
1320
  // `01 N 1` for a static name reference, `001 N` for a literal name.
1214
1321
  if (nameIndex >= 0) {
1215
- qpackPushInteger(out, neverIndexed ? 112 : 80, 4, toI64(nameIndex))
1322
+ qpackPushInteger(out, neverIndexed ? 112 : 80, 15, toU64(nameIndex))
1216
1323
  } else {
1217
- qpackPushString(this.huffman, out, neverIndexed ? 48 : 32, 4, name, nameOff, nameLen, huffman)
1324
+ qpackPushString(this.huffman, out, neverIndexed ? 48 : 32, 8, 7, name, nameOff, nameLen, nameCoded)
1218
1325
  }
1219
- qpackPushString(this.huffman, out, 0, 8, value, valueOff, valueLen, huffman)
1326
+ qpackPushString(this.huffman, out, 0, 128, 127, value, valueOff, valueLen, valueCoded)
1220
1327
  }
1221
1328
 
1222
1329
  /**
@@ -41,13 +41,31 @@ import { quicPushAck, quicPushAckEcn, quicPutAck, quicPutAckEcn } from "nish/net
41
41
  /** The most disjoint ranges a `QuicAckRanges` keeps, and so the most an ACK frame it writes lists. */
42
42
  export const QUIC_ACK_MAX_RANGES: i32 = 32
43
43
 
44
+ /**
45
+ * One more than `n`, or -1 when `n` is below 0 or already `QUIC_MAX_VARINT`,
46
+ * the largest packet number or count a varint carries (RFC 9000 §16).
47
+ * Counted down from `QUIC_MAX_VARINT`, so the sum is one `src/bounds.ts`
48
+ * proves, never a wrap.
49
+ */
50
+ const quicAckNext = (n: i64): i64 => {
51
+ if (n < 0) {
52
+ return -1
53
+ }
54
+ const left: i64 = QUIC_MAX_VARINT - n
55
+ if (left < 1) {
56
+ return -1
57
+ }
58
+ return QUIC_MAX_VARINT - (left - 1)
59
+ }
60
+
44
61
  /**
45
62
  * One more than the ECN count `n`, or `n` once it is the largest a varint
46
63
  * carries, since an ACK_ECN frame could not write a larger one.
47
64
  */
48
- const quicAckCountUp = (n: i64): i64 =>
49
- // Below `QUIC_MAX_VARINT`, so the unsigned sum cannot wrap.
50
- n < QUIC_MAX_VARINT ? toI64(toU64(n) + 1) : n
65
+ const quicAckCountUp = (n: i64): i64 => {
66
+ const next: i64 = quicAckNext(n)
67
+ return next < 0 ? n : next
68
+ }
51
69
 
52
70
  /**
53
71
  * The packet numbers received in one space. `ranges` holds `count` pairs
@@ -188,8 +206,8 @@ export class QuicAckRanges {
188
206
  while (k < this.count && this.low(k) > pn) {
189
207
  k += 1
190
208
  }
191
- // `pn` is at most 2^62 - 1 here, so one past it cannot wrap.
192
- const joinsAbove: boolean = k > 0 && this.low(k - 1) === toI64(toU64(pn) + 1)
209
+ // One past `pn`, or -1 at `QUIC_MAX_VARINT`, which no range starts at.
210
+ const joinsAbove: boolean = k > 0 && this.low(k - 1) === quicAckNext(pn)
193
211
  const joinsBelow: boolean = k < this.count && this.high(k) === pn - 1
194
212
  if (joinsAbove && joinsBelow) {
195
213
  // `pn` closes the hole between two ranges: merge them into the upper one.
@@ -268,8 +286,10 @@ export class QuicAckRanges {
268
286
  this.set(j, this.low(j - 1), this.high(j - 1))
269
287
  }
270
288
  this.set(k, pn, pn)
271
- // `count` is below `QUIC_ACK_MAX_RANGES` here, so one more cannot wrap.
272
- this.count = toI32(toU32(count) + 1)
289
+ if (count < 0 || count >= QUIC_ACK_MAX_RANGES) {
290
+ return
291
+ }
292
+ this.count = count + 1
273
293
  }
274
294
 
275
295
  /**
@@ -249,8 +249,13 @@ export class QuicCidTable {
249
249
  entry.used = true
250
250
  // Sequence 0 travels in the long header's SCID, never in a frame.
251
251
  entry.announced = sequence === 0
252
- // Below `QUIC_MAX_VARINT`, so one more cannot wrap.
253
- this.nextLocal = toI64(toU64(sequence) + 1)
252
+ // Counted down from `QUIC_MAX_VARINT`: `sequence` is below it, so one
253
+ // more is a sum `src/bounds.ts` proves. The -1 at `QUIC_MAX_VARINT` is
254
+ // never reached: `addLocal` issues at most `QUIC_CONN_MAX_ISSUED_CIDS`
255
+ // IDs, far below it, so `retireLocal`'s `sequence >= nextLocal` keeps
256
+ // its meaning.
257
+ const left: i64 = QUIC_MAX_VARINT - sequence
258
+ this.nextLocal = left < 1 ? -1 : QUIC_MAX_VARINT - (left - 1)
254
259
  return sequence
255
260
  }
256
261
 
@@ -531,15 +536,16 @@ export class QuicCidTable {
531
536
  }
532
537
 
533
538
  /**
534
- * The ring slot `k` places after `retireHead`, or 0 for an empty ring. Unsigned, because the head and
535
- * `k` are each below 2^31, so the sum cannot wrap.
539
+ * The ring slot `k` places after `retireHead`, or 0 for an empty ring or a negative head or `k`.
540
+ * Summed in i64, where two i32 values cannot overflow.
536
541
  */
537
542
  private retireSlot(k: i32): i32 {
538
- const size: u32 = toU32(this.retirePending.length)
539
- if (size === 0) {
543
+ const size: i64 = toI64(this.retirePending.length)
544
+ const head: i32 = this.retireHead
545
+ if (size <= 0 || head < 0 || k < 0) {
540
546
  return 0
541
547
  }
542
- return toI32((toU32(this.retireHead) + toU32(k)) % size)
548
+ return toI32((toI64(head) + toI64(k)) % size)
543
549
  }
544
550
 
545
551
  /**
@@ -100,15 +100,16 @@ export class QuicDatagramQueue {
100
100
  }
101
101
 
102
102
  /**
103
- * The entry `j` places after `head`, wrapped around the ring; 0 for a ring of no entries.
104
- * Unsigned, because `head` and `j` are each below 2^31, so the sum cannot wrap.
103
+ * The entry `j` places after `head`, wrapped around the ring; 0 for a ring of no entries or a
104
+ * negative head or `j`. Summed in i64, where two i32 values cannot overflow.
105
105
  */
106
106
  private slot(j: i32): i32 {
107
- const entries: u32 = toU32(this.lengths.length)
108
- if (entries === 0) {
107
+ const entries: i64 = toI64(this.lengths.length)
108
+ const head: i32 = this.head
109
+ if (entries <= 0 || head < 0 || j < 0) {
109
110
  return 0
110
111
  }
111
- return toI32((toU32(this.head) + toU32(j)) % entries)
112
+ return toI32((toI64(head) + toI64(j)) % entries)
112
113
  }
113
114
 
114
115
  /** Empties the ring, for a connection slot reused for another peer. */
@@ -371,6 +371,48 @@ const quicFrameReset = (frame: QuicFrame, at: i32): void => {
371
371
  frame.resetTokenStart = at
372
372
  }
373
373
 
374
+ /**
375
+ * The Gap field of an ACK frame (§19.3.1) between a range whose smallest
376
+ * packet number is `above` and the next range down, whose largest is
377
+ * `largest`: the packets missing between them, less one. -1 when either is
378
+ * below 0 or the ranges touch or are out of order, so no range is below zero.
379
+ */
380
+ const quicAckGap = (above: i64, largest: i64): i64 => {
381
+ if (above < 2 || largest < 0) {
382
+ return -1
383
+ }
384
+ const room: i64 = above - 2
385
+ return largest > room ? -1 : room - largest
386
+ }
387
+
388
+ /**
389
+ * The ACK Range Length of `[smallest, largest]` (§19.3.1): how many packet
390
+ * numbers below `largest` the range holds. -1 when `smallest` is below 0 or
391
+ * above `largest`.
392
+ */
393
+ const quicAckSpan = (smallest: i64, largest: i64): i64 => {
394
+ if (smallest < 0 || largest < 0 || largest < smallest) {
395
+ return -1
396
+ }
397
+ return largest - smallest
398
+ }
399
+
400
+ /**
401
+ * The next count up from the varint `n`, or -1 when `n` is below 0 or already
402
+ * `QUIC_MAX_VARINT`. Counted down from `QUIC_MAX_VARINT`, so the sum is one
403
+ * `src/bounds.ts` proves, never a wrap.
404
+ */
405
+ const quicFrameNext = (n: i64): i64 => {
406
+ if (n < 0) {
407
+ return -1
408
+ }
409
+ const left: i64 = QUIC_MAX_VARINT - n
410
+ if (left < 1) {
411
+ return -1
412
+ }
413
+ return QUIC_MAX_VARINT - (left - 1)
414
+ }
415
+
374
416
  /**
375
417
  * The ranges of an ACK frame (§19.3.1): the first range down from `largest`,
376
418
  * then each Gap and ACK Range Length pair below it. A range that reaches
@@ -384,9 +426,8 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
384
426
  if (r.failed) {
385
427
  return QUIC_ERROR_FRAME_ENCODING
386
428
  }
387
- // Varints are below 2^62, so the difference is exact in wrapping u64 and a
388
- // negative answer means the range reached below zero.
389
- let smallest: i64 = toI64(toU64(frame.largest) - toU64(first))
429
+ // `largest - first`, or -1 for a First ACK Range reaching below zero.
430
+ let smallest: i64 = quicAckSpan(first, frame.largest)
390
431
  if (smallest < 0) {
391
432
  return QUIC_ERROR_FRAME_ENCODING
392
433
  }
@@ -400,15 +441,13 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
400
441
  if (r.failed) {
401
442
  return QUIC_ERROR_FRAME_ENCODING
402
443
  }
403
- // The next range's largest is two below this one's smallest, less the gap.
404
- // `smallest` is 0 or more and the varints below 2^62, so wrapping u64
405
- // is exact and a negative answer is a range below zero.
406
- const top: i64 = toI64(toU64(smallest) - toU64(gap) - 2)
407
- if (top < 0) {
408
- return QUIC_ERROR_FRAME_ENCODING
409
- }
410
- const bottom: i64 = toI64(toU64(top) - toU64(length))
411
- if (bottom < 0) {
444
+ // The next range's largest is two below this one's smallest, less the
445
+ // gap (`smallest - 2 - gap`), and its smallest `length` below that
446
+ // (`top - length`): the subtractions the writer's helpers do, read back.
447
+ // Either is -1 for a range below zero.
448
+ const top: i64 = quicAckGap(smallest, gap)
449
+ const bottom: i64 = quicAckSpan(length, top)
450
+ if (top < 0 || bottom < 0) {
412
451
  return QUIC_ERROR_FRAME_ENCODING
413
452
  }
414
453
  if (stored < QUIC_FRAME_MAX_ACK_RANGES) {
@@ -419,8 +458,15 @@ const quicFrameReadAck = (r: QuicFrameReader, d: u8[], frame: QuicFrame, ecn: bo
419
458
  smallest = bottom
420
459
  }
421
460
  frame.ackRangeCount = stored
422
- // `count` is a varint, below 2^62.
423
- frame.ackRangeTotal = toI64(toU64(count) + 1)
461
+ // `count` is a varint, at most `QUIC_MAX_VARINT`, and the total is one
462
+ // more. A count of `QUIC_MAX_VARINT` lists more ranges than a frame holds:
463
+ // it is refused here as FRAME_ENCODING, where the loop above would have
464
+ // refused it later as a short read or a range below zero.
465
+ const total: i64 = quicFrameNext(count)
466
+ if (total < 0) {
467
+ return QUIC_ERROR_FRAME_ENCODING
468
+ }
469
+ frame.ackRangeTotal = total
424
470
  if (ecn) {
425
471
  frame.ect0 = quicFrameVarint(r, d)
426
472
  frame.ect1 = quicFrameVarint(r, d)
@@ -863,8 +909,7 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
863
909
  if (size > 2147483615) {
864
910
  return 0
865
911
  }
866
- // `smallest` is 0 or more and at most `largest`, so the difference is exact.
867
- const span: i64 = toI64(toU64(largest) - toU64(smallest))
912
+ const span: i64 = quicAckSpan(smallest, largest)
868
913
  if (k === 0) {
869
914
  size =
870
915
  size +
@@ -873,9 +918,8 @@ export const quicAckSize = (ranges: i64[], rangeCount: i32, ackDelay: i64): i32
873
918
  quicVarintSize(toI64(rangeCount - 1)) +
874
919
  quicVarintSize(span)
875
920
  } else {
876
- // Gap: the packets missing between this range and the one above, less one (§19.3.1). Both
877
- // are 0 to 2^62 − 1, so wrapping u64 is exact and a negative gap is ranges out of order.
878
- const gap: i64 = toI64(toU64(previousSmallest) - toU64(largest) - 2)
921
+ // A negative gap is ranges that touch or are out of order.
922
+ const gap: i64 = quicAckGap(previousSmallest, largest)
879
923
  if (gap < 0) {
880
924
  return 0
881
925
  }
@@ -918,8 +962,8 @@ export const quicPutAck = (
918
962
  let previousSmallest: i64 = 0
919
963
  for (let k: i32 = 0; k < rangeCount; k += 1) {
920
964
  // `quicAckSize` held `rangeCount` to half the array and checked every
921
- // pair, so the slots fit, the guard below never skips a pair, and the
922
- // differences are exact in wrapping u64.
965
+ // pair, so the slots fit, the guard below never skips a pair, and no
966
+ // gap or span is the -1 that would refuse the varint.
923
967
  const pair: i32 = toI32(toI64(k) * 2)
924
968
  const next: i32 = toI32(toI64(pair) + 1)
925
969
  if (pair >= 0 && pair < toI32(ranges.length) && next >= 0 && next < toI32(ranges.length)) {
@@ -930,9 +974,9 @@ export const quicPutAck = (
930
974
  cursor = quicFramePutVarint(buf, cursor, ackDelay)
931
975
  cursor = quicFramePutVarint(buf, cursor, toI64(rangeCount) - 1)
932
976
  } else {
933
- cursor = quicFramePutVarint(buf, cursor, toI64(toU64(previousSmallest) - toU64(largest) - 2))
977
+ cursor = quicFramePutVarint(buf, cursor, quicAckGap(previousSmallest, largest))
934
978
  }
935
- cursor = quicFramePutVarint(buf, cursor, toI64(toU64(largest) - toU64(smallest)))
979
+ cursor = quicFramePutVarint(buf, cursor, quicAckSpan(smallest, largest))
936
980
  previousSmallest = smallest
937
981
  }
938
982
  }