@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/hpack.ts CHANGED
@@ -79,6 +79,8 @@
79
79
  * one against the appendix's table.
80
80
  */
81
81
 
82
+ import { wrappingAdd } from "nish:unsafe"
83
+
82
84
  /** A decode that read the whole block and answered every field. */
83
85
  export const HPACK_OK: i32 = 0
84
86
 
@@ -386,29 +388,60 @@ const hpackSameAsWindow = (bytes: u8[], store: u8[], at: i32, n: i32): boolean =
386
388
  if (toI32(bytes.length) !== n) {
387
389
  return false
388
390
  }
391
+ // `j` walks the window alongside `i`, and steps on only from inside `store`, so it never passes
392
+ // `store.length`; a window that leaves `store` reads as a difference.
393
+ let j: i32 = at
389
394
  for (let i: i32 = 0; i < n && i < toI32(bytes.length); i += 1) {
390
- // `at` and `i` are each below 2^31, so the unsigned sum cannot wrap; a sum past `store`, a window
391
- // the caller has checked, reads as a difference.
392
- const j: i32 = toI32(toU32(at) + toU32(i))
393
- if (at < 0 || j < 0 || j >= toI32(store.length) || bytes[i] !== store[j]) {
395
+ if (j < 0 || j >= toI32(store.length) || bytes[i] !== store[j]) {
394
396
  return false
395
397
  }
398
+ j += 1
396
399
  }
397
400
  return true
398
401
  }
399
402
 
400
403
  /**
401
- * What an entry of this name and value costs in the table (§4.1), in `i64`
402
- * because two lengths of up to 2^31 - 1 and the overhead do not fit an `i32`.
404
+ * The dynamic table index of the combined index `index`, which must be past the static table's 61 entries,
405
+ * or -1 for one that is not. The literal is `HPACK_STATIC_LENGTH`, spelled out because the bounds prover
406
+ * reads only a literal, and it is what proves the difference cannot overflow.
403
407
  */
404
- const hpackEntrySize = (name: u8[], value: u8[]): i64 =>
405
- toI64(name.length) + toI64(value.length) + toI64(HPACK_ENTRY_OVERHEAD)
408
+ const hpackDynamicIndex = (index: i32): i32 => {
409
+ let i: i32 = -1
410
+ if (index > 61) {
411
+ i = index - 61
412
+ }
413
+ return i
414
+ }
406
415
 
407
- /** Panics unless `[off, off + len)` lies inside `buf`; the window is the caller's. */
408
- const hpackCheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
409
- if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(buf.length)) {
410
- panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${toI32(buf.length)} bytes`)
416
+ /**
417
+ * `off + len` when `buf[off, off + len)` lies inside `buf`, else -1. The sum is taken in `i64`, where two
418
+ * `i32`s cannot overflow; one no larger than `buf.length` fits an `i32` again.
419
+ */
420
+ const hpackWindowEnd = (buf: u8[], off: i32, len: i32): i32 => {
421
+ const end: i64 = toI64(off) + toI64(len)
422
+ if (off < 0 || len < 0 || end > toI64(buf.length)) {
423
+ return -1
411
424
  }
425
+ return toI32(end)
426
+ }
427
+
428
+ /**
429
+ * Panics with `message` unless `[off, off + len)` lies inside `buf`. In the window contract's shape
430
+ * (`src/window-contract.ts`), so a caller that proved the window has proved this. The message is
431
+ * the caller's constant rather than one built from the window: building it would open an arena scope
432
+ * on every call, failing or not, wherever this inlines.
433
+ */
434
+ const hpackCheckWindow = (message: string, buf: u8[], off: i32, len: i32): void => {
435
+ const size: i32 = toI32(buf.length)
436
+ if (off < 0 || len < 0 || off > size || len > size - off) {
437
+ panic(message)
438
+ }
439
+ }
440
+
441
+ /** Panics unless `[off, off + len)` lies inside `buf`, and answers the window's end, `off + len`. */
442
+ const hpackCheckedWindowEnd = (message: string, buf: u8[], off: i32, len: i32): i32 => {
443
+ hpackCheckWindow(message, buf, off, len)
444
+ return hpackWindowEnd(buf, off, len)
412
445
  }
413
446
 
414
447
  /**
@@ -454,7 +487,7 @@ export class HpackHuffman {
454
487
  * Where each length's symbols start in `symbols`, by length.
455
488
  * @internal Read by `hpackHuffmanDecode`; not for callers.
456
489
  */
457
- baseRank: i32[]
490
+ baseRank: u32[]
458
491
 
459
492
  /** Rebuilds the code from Appendix B's 257 code lengths. Build one and reuse it: each build allocates. */
460
493
  constructor() {
@@ -465,23 +498,25 @@ export class HpackHuffman {
465
498
  this.symbols = new Array<i32>(257)
466
499
  this.firstCode = new Array<i32>(31)
467
500
  this.countAt = new Array<i32>(31)
468
- this.baseRank = new Array<i32>(31)
501
+ this.baseRank = new Array<u32>(31)
469
502
  // Every bound below is the array's own length, which the constants fix at 257 symbols and 31 code
470
503
  // lengths, so no test ever stops a loop early.
471
504
  const table: string = HPACK_HUFFMAN_LENGTHS
472
505
  for (let sym: i32 = 0; sym < toI32(this.lengths.length) && sym < toI32(table.length); sym += 1) {
473
506
  this.lengths[sym] = toI32(table.charCodeAt(sym)) - 60
474
507
  }
475
- // How many symbols each length has. Appendix B's lengths are 5 to 30, so every one lands in a row.
508
+ // How many symbols each length has, counted in the unsigned `baseRank` rows, which 257 symbols cannot
509
+ // wrap. Appendix B's lengths are 5 to 30, so every one lands in a row.
476
510
  for (let sym: i32 = 0; sym < toI32(this.lengths.length); sym += 1) {
477
511
  const len: i32 = this.lengths[sym]
478
- if (len >= 0 && len < toI32(this.countAt.length)) {
479
- this.countAt[len] = toI32(toU32(this.countAt[len]) + 1)
512
+ if (len >= 0 && len < toI32(this.baseRank.length)) {
513
+ this.baseRank[len] += 1
480
514
  }
481
515
  }
482
516
  // Canonical order is by length, then by symbol: each length's first code and rank follow from the
483
517
  // counts of the shorter ones. Unsigned: the code is below 2^30 and the rank at most 257, so neither
484
- // can wrap. Each count goes back to 0, to count again as the pass after hands the codes out.
518
+ // can wrap. Each count becomes its length's first rank, the next slot of `symbols` the pass after
519
+ // fills.
485
520
  let code: u32 = 0
486
521
  let rank: u32 = 0
487
522
  for (
@@ -492,34 +527,46 @@ export class HpackHuffman {
492
527
  len < toI32(this.countAt.length);
493
528
  len += 1
494
529
  ) {
530
+ const count: u32 = this.baseRank[len]
495
531
  this.firstCode[len] = toI32(code)
496
- this.baseRank[len] = toI32(rank)
497
- const count: u32 = toU32(this.countAt[len])
498
- this.countAt[len] = 0
532
+ this.baseRank[len] = rank
533
+ this.countAt[len] = toI32(rank)
499
534
  code = (code + count) << 1
500
535
  rank += count
501
536
  }
502
- // Walking the symbols in order hands each length its codes in symbol order, as the canonical code
503
- // does, and leaves every count as it was. Appendix B has 257 symbols, so the rank never reaches
504
- // past `symbols`.
537
+ // Walking the symbols in order hands each length its slots of `symbols` and its codes in symbol
538
+ // order, as the canonical code does, with the count holding the next slot and the first code the next
539
+ // code. Appendix B has 257 symbols and codes below 2^30, so the tests never refuse a symbol; they are
540
+ // what keep the slot in `symbols` and prove the increments cannot overflow.
505
541
  for (let sym: i32 = 0; sym < toI32(this.lengths.length) && sym < toI32(this.codes.length); sym += 1) {
506
542
  const len: i32 = this.lengths[sym]
507
- if (
508
- len >= 5 &&
509
- len <= HPACK_HUFFMAN_MAX_BITS &&
510
- len < toI32(this.firstCode.length) &&
511
- len < toI32(this.baseRank.length) &&
512
- len < toI32(this.countAt.length)
513
- ) {
514
- const done: u32 = toU32(this.countAt[len])
515
- const at: i32 = toI32(toU32(this.baseRank[len]) + done)
516
- if (at >= 0 && at < toI32(this.symbols.length)) {
517
- this.codes[sym] = toI32(toU32(this.firstCode[len]) + done)
543
+ if (len >= 5 && len < toI32(this.countAt.length) && len < toI32(this.firstCode.length)) {
544
+ const at: i32 = this.countAt[len]
545
+ const next: i32 = this.firstCode[len]
546
+ if (at >= 0 && at < toI32(this.symbols.length) && next < 1073741824) {
518
547
  this.symbols[at] = sym
519
- this.countAt[len] = toI32(done + 1)
548
+ this.codes[sym] = next
549
+ this.countAt[len] = at + 1
550
+ this.firstCode[len] = next + 1
520
551
  }
521
552
  }
522
553
  }
554
+ // The same walk over the lengths as above puts each first code back, and each count, from the slot
555
+ // past its run less the run's first rank. Unsigned, as above.
556
+ code = 0
557
+ rank = 0
558
+ for (
559
+ let len: i32 = 5;
560
+ len <= HPACK_HUFFMAN_MAX_BITS && len < toI32(this.firstCode.length) && len < toI32(this.countAt.length);
561
+ len += 1
562
+ ) {
563
+ const end: u32 = toU32(this.countAt[len])
564
+ const count: u32 = end - rank
565
+ this.firstCode[len] = toI32(code)
566
+ this.countAt[len] = toI32(count)
567
+ code = (code + count) << 1
568
+ rank = end
569
+ }
523
570
  }
524
571
  }
525
572
 
@@ -536,7 +583,7 @@ export class HpackHuffman {
536
583
  * internal fields).
537
584
  */
538
585
  export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i32): i32 => {
539
- hpackCheckWindow("hpackHuffmanLength", src, off, len)
586
+ hpackCheckWindow("hpackHuffmanLength: the window is outside its buffer", src, off, len)
540
587
  // Unsigned: at most 30 bits for each of fewer than 2^31 octets, far below 2^64.
541
588
  let bits: u64 = 0
542
589
  // Widened, so `off + len` cannot wrap. The window check has already proved
@@ -573,7 +620,23 @@ export const hpackHuffmanLength = (h: HpackHuffman, src: u8[], off: i32, len: i3
573
620
  * @param out Where the coded bytes go: appended after what it holds, `hpackHuffmanLength` of them.
574
621
  */
575
622
  export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
576
- hpackCheckWindow("hpackHuffmanEncode", src, off, len)
623
+ hpackCheckWindow("hpackHuffmanEncode: the window is outside its buffer", src, off, len)
624
+ hpackHuffmanAppend(h, src, off, len, out)
625
+ }
626
+
627
+ /**
628
+ * `hpackHuffmanEncode` with no window check, for a caller that checked the
629
+ * window before it wrote anything and has since pushed to an array the
630
+ * window may share: it codes only the octets of `src[off, off + len)` that
631
+ * lie inside `src`, so it cannot panic, and for a window inside `src` it
632
+ * appends what `hpackHuffmanEncode` does.
633
+ * @param h The code to encode with.
634
+ * @param src The octets to encode; only read.
635
+ * @param off Where the window starts, an index into `src`; a negative one codes nothing.
636
+ * @param len How many octets from `off` on to encode.
637
+ * @param out Where the coded bytes go, appended after what it holds.
638
+ */
639
+ export const hpackHuffmanAppend = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): void => {
577
640
  let acc: u64 = 0
578
641
  // Unsigned: fewer than eight waiting bits plus one code of at most 31, so it never wraps.
579
642
  let bits: u32 = 0
@@ -620,7 +683,7 @@ export const hpackHuffmanEncode = (h: HpackHuffman, src: u8[], off: i32, len: i3
620
683
  * which only a table cut short (by writing its internal fields) allows.
621
684
  */
622
685
  export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i32, out: u8[]): i32 => {
623
- hpackCheckWindow("hpackHuffmanDecode", src, off, len)
686
+ hpackCheckWindow("hpackHuffmanDecode: the window is outside its buffer", src, off, len)
624
687
  // Unsigned, and `bits` never passes 30: the code is complete, so thirty bits always end a symbol,
625
688
  // and the row test below gives up before a 31st.
626
689
  let code: u32 = 0
@@ -644,13 +707,13 @@ export const hpackHuffmanDecode = (h: HpackHuffman, src: u8[], off: i32, len: i3
644
707
  ) {
645
708
  return HPACK_ERR_HUFFMAN_PADDING
646
709
  }
647
- // Both sides fit an `i32`, so the `i64` difference cannot wrap.
710
+ // The difference is taken in `i64`, where it cannot overflow: the code is under 2^30 and every
711
+ // table entry an `i32`. Past the test `at` is in `[0, 2^31)`, which `toU32` keeps whole, and the
712
+ // slot is `u32` like the rank it starts from; a slot outside `symbols` matches no code.
648
713
  const at: i64 = toI64(code) - toI64(h.firstCode[row])
649
714
  if (at >= 0 && at < toI64(h.countAt[row])) {
650
- // `at` is below `h.countAt[row]`, so in `[0, 2^31)`; the unsigned sum of two such cannot wrap,
651
- // and one past `symbols` matches no code.
652
- const slot: i32 = toI32(toU32(h.baseRank[row]) + toU32(at))
653
- if (slot < 0 || slot >= toI32(h.symbols.length)) {
715
+ const slot: u32 = h.baseRank[row] + toU32(at)
716
+ if (slot >= toU32(h.symbols.length)) {
654
717
  return HPACK_ERR_HUFFMAN_PADDING
655
718
  }
656
719
  const sym: i32 = h.symbols[slot]
@@ -696,12 +759,9 @@ export class HpackCursor {
696
759
  * @param len How many bytes from `off` on the cursor may read.
697
760
  */
698
761
  constructor(buf: u8[], off: i32, len: i32) {
699
- hpackCheckWindow("HpackCursor", buf, off, len)
700
762
  this.buf = buf
701
763
  this.pos = off
702
- // The window check proved `off` and `len` non-negative with a sum at most `buf.length`, so the unsigned
703
- // sum does not wrap.
704
- this.end = toI32(toU32(off) + toU32(len))
764
+ this.end = hpackCheckedWindowEnd("HpackCursor: the window is outside its buffer", buf, off, len)
705
765
  }
706
766
 
707
767
  /**
@@ -709,12 +769,9 @@ export class HpackCursor {
709
769
  * @internal Called by `HpackDecoder.decode` for each block; not for callers.
710
770
  */
711
771
  reset(buf: u8[], off: i32, len: i32): void {
712
- hpackCheckWindow("HpackCursor", buf, off, len)
713
772
  this.buf = buf
714
773
  this.pos = off
715
- // The window check proved `off` and `len` non-negative with a sum at most `buf.length`, so the unsigned
716
- // sum does not wrap.
717
- this.end = toI32(toU32(off) + toU32(len))
774
+ this.end = hpackCheckedWindowEnd("HpackCursor: the window is outside its buffer", buf, off, len)
718
775
  }
719
776
  }
720
777
 
@@ -734,14 +791,19 @@ export const hpackEncodeInteger = (out: u8[], flags: i32, prefixBits: i32, value
734
791
  if (prefixBits < 1 || prefixBits > 8 || value < 0) {
735
792
  panic(`hpackEncodeInteger: a value of ${value} with a ${prefixBits}-bit prefix, outside 1 to 8`)
736
793
  }
737
- // Unsigned: the prefix is 1 to 8 bits, so `max` is 1 to 255, and `value` is at least `max` below.
738
- const max: i32 = toI32((toU32(1) << toU32(prefixBits)) - toU32(1))
794
+ // The prefix is 1 to 8 bits, so `max` is 1 to 255, and `value` is at least `max` below.
795
+ const max: i32 = toI32(255) >> (8 - prefixBits)
739
796
  if (value < max) {
740
797
  out.push(toU8(flags | value))
741
798
  return
742
799
  }
743
800
  out.push(toU8(flags | max))
744
- let rest: i32 = toI32(toU32(value) - toU32(max))
801
+ // `value` is at least `max` here and `max` is positive, so the difference is ordered; the test is what
802
+ // proves it.
803
+ let rest: i32 = 0
804
+ if (max >= 0 && value >= max) {
805
+ rest = value - max
806
+ }
745
807
  while (rest >= 128) {
746
808
  out.push(toU8((rest & 127) | 128))
747
809
  rest = rest >> 7
@@ -774,7 +836,7 @@ export const hpackDecodeInteger = (c: HpackCursor, prefixBits: i32): i32 => {
774
836
  if (at >= c.end || at < 0 || at >= toI32(buf.length)) {
775
837
  return HPACK_ERR_TRUNCATED
776
838
  }
777
- const max: i32 = toI32((toU32(1) << toU32(prefixBits)) - toU32(1))
839
+ const max: i32 = toI32(255) >> (8 - prefixBits)
778
840
  const first: i32 = toI32(buf[at]) & max
779
841
  c.pos = at + 1
780
842
  if (first < max) {
@@ -993,11 +1055,9 @@ export class HpackTable {
993
1055
  valueStart(i: i32): i32 {
994
1056
  const start: i32 = this.nameStart(i)
995
1057
  const length: i32 = this.nameLength(i)
996
- if (start < 0 || length < 0) {
997
- return -1
998
- }
999
- // Both are non-negative, and the name lies inside `bytes`, so the unsigned sum does not wrap.
1000
- return toI32(toU32(start) + toU32(length))
1058
+ // The name lies inside `bytes`, so its end is the window's end, which cannot overflow; a name
1059
+ // outside `bytes`, which only a caller writing the public fields makes, has no value.
1060
+ return hpackWindowEnd(this.bytes, start, length)
1001
1061
  }
1002
1062
 
1003
1063
  /**
@@ -1010,8 +1070,13 @@ export class HpackTable {
1010
1070
  if (k < 0 || start < 0) {
1011
1071
  return -1
1012
1072
  }
1013
- // The value ends at or after it starts, so the unsigned difference does not wrap.
1014
- return toI32(toU32(this.endOf(k)) - toU32(start))
1073
+ // The value ends at or after it starts, so the difference is ordered; the test is what proves it.
1074
+ const end: i32 = this.endOf(k)
1075
+ let length: i32 = 0
1076
+ if (end > start) {
1077
+ length = end - start
1078
+ }
1079
+ return length
1015
1080
  }
1016
1081
 
1017
1082
  /**
@@ -1050,11 +1115,23 @@ export class HpackTable {
1050
1115
  * @internal Called by `add`, `resize` and `HpackDecoder`; not for callers.
1051
1116
  */
1052
1117
  evictTo(limit: i32): void {
1053
- while (toI64(this.size) > toI64(limit) && this.count() > 0) {
1054
- // The size counts every live entry's cost, so the unsigned difference does not wrap; and `first` is
1055
- // below the slot count, under 2^30, while `count()` is positive.
1056
- this.size = toI32(toU32(this.size) - toU32(this.costOf(this.first)))
1057
- this.first = toI32(toU32(this.first) + 1)
1118
+ while (toI64(this.size) > toI64(limit)) {
1119
+ // `count()` is positive exactly when `first` is a slot below the slot count, which is what proves
1120
+ // the step on cannot overflow.
1121
+ const f: i32 = this.first
1122
+ if (f < 0 || f >= toI32(this.entries.length) >> 1) {
1123
+ break
1124
+ }
1125
+ // The size counts every live entry's cost, so the difference is ordered; the test is what proves
1126
+ // it, and a size short of the cost, which only a caller writing the public fields makes, empties.
1127
+ const size: i32 = this.size
1128
+ const cost: i32 = this.costOf(f)
1129
+ let left: i32 = 0
1130
+ if (cost >= 0 && size > cost) {
1131
+ left = size - cost
1132
+ }
1133
+ this.size = left
1134
+ this.first = f + 1
1058
1135
  }
1059
1136
  this.compact()
1060
1137
  }
@@ -1073,11 +1150,15 @@ export class HpackTable {
1073
1150
  if (f > 0 && f <= n && f >= n - f) {
1074
1151
  const live: i32 = toI32(toU32(n - f) << 1)
1075
1152
  const from: i32 = toI32(toU32(f) << 1)
1153
+ // `at` runs alongside `k` from `from`, and `from + live` is the slot count doubled, at most
1154
+ // `entries.length`, so the walk ends where the old index test did.
1155
+ let at: i32 = from
1076
1156
  for (let k: i32 = 0; k < live && k < toI32(entries.length); k += 1) {
1077
- const at: i32 = toI32(toU32(from) + toU32(k))
1078
- if (at >= 0 && at < toI32(entries.length)) {
1079
- entries[k] = entries[at]
1157
+ if (at < 0 || at >= toI32(entries.length)) {
1158
+ break
1080
1159
  }
1160
+ entries[k] = entries[at]
1161
+ at += 1
1081
1162
  }
1082
1163
  while (toI32(entries.length) > live && toI32(entries.length) > 0) {
1083
1164
  entries.pop()
@@ -1096,12 +1177,18 @@ export class HpackTable {
1096
1177
  while (toI32(bytes.length) > keep && toI32(bytes.length) > 0) {
1097
1178
  bytes.pop()
1098
1179
  }
1099
- // Every live entry starts at or after `start`, so the unsigned difference does not wrap.
1180
+ // Every live entry starts at or after `start`, so the difference is ordered; the test is what
1181
+ // proves it.
1100
1182
  const slots: i32 = toI32(entries.length) >> 1
1101
1183
  for (let k: i32 = this.first; k >= 0 && k < slots; k += 1) {
1102
1184
  const at: i32 = toI32(toU32(k) << 1)
1103
1185
  if (at >= 0 && at < toI32(entries.length)) {
1104
- entries[at] = toI32(toU32(entries[at]) - toU32(start))
1186
+ const was: i32 = entries[at]
1187
+ let moved: i32 = 0
1188
+ if (was > start) {
1189
+ moved = was - start
1190
+ }
1191
+ entries[at] = moved
1105
1192
  }
1106
1193
  }
1107
1194
  }
@@ -1120,15 +1207,29 @@ export class HpackTable {
1120
1207
  * and value are both empty.
1121
1208
  */
1122
1209
  add(name: u8[], value: u8[]): i32 {
1123
- const cost: i64 = hpackEntrySize(name, value)
1124
- if (cost > toI64(this.maxSize)) {
1210
+ // `limit` is `maxSize` less the cost, taken one ordered difference at a time; the entry does not fit
1211
+ // exactly when one of them would go below zero, which is the cost passing `maxSize`.
1212
+ const nameLength: i32 = toI32(name.length)
1213
+ const valueLength: i32 = toI32(value.length)
1214
+ const max: i32 = this.maxSize
1215
+ let limit: i32 = -1
1216
+ if (max >= 32) {
1217
+ const afterOverhead: i32 = max - 32
1218
+ if (nameLength >= 0 && nameLength <= afterOverhead) {
1219
+ const afterName: i32 = afterOverhead - nameLength
1220
+ if (valueLength >= 0 && valueLength <= afterName) {
1221
+ limit = afterName - valueLength
1222
+ }
1223
+ }
1224
+ }
1225
+ if (limit < 0) {
1125
1226
  const nothing: i32 = 0
1126
1227
  this.evictTo(nothing)
1127
1228
  return nothing
1128
1229
  }
1129
- // The cost is from 32 to `maxSize`, so neither unsigned sum nor difference below wraps: the size after
1130
- // eviction is at most `maxSize - cost`, and the octets pushed are the cost less 32.
1131
- this.evictTo(toI32(toU32(this.maxSize) - toU32(cost)))
1230
+ // The size after eviction is at most `limit`, so the entry's cost, `maxSize - limit`, takes it to at
1231
+ // most `maxSize`.
1232
+ this.evictTo(limit)
1132
1233
  this.entries.push(toI32(this.bytes.length))
1133
1234
  this.entries.push(toI32(name.length))
1134
1235
  let pushed: u32 = 0
@@ -1140,7 +1241,18 @@ export class HpackTable {
1140
1241
  this.bytes.push(b)
1141
1242
  pushed += 1
1142
1243
  }
1143
- this.size = toI32(toU32(this.size) + toU32(cost))
1244
+ // The new size is `maxSize` less the room eviction left, `limit - size`; both differences are ordered,
1245
+ // and a size past `limit`, which only a caller writing the public fields makes, counts no room.
1246
+ const size: i32 = this.size
1247
+ let room: i32 = 0
1248
+ if (size >= 0 && limit > size) {
1249
+ room = limit - size
1250
+ }
1251
+ let grown: i32 = 0
1252
+ if (room >= 0 && max > room) {
1253
+ grown = max - room
1254
+ }
1255
+ this.size = grown
1144
1256
  return toI32(pushed)
1145
1257
  }
1146
1258
 
@@ -1262,11 +1374,12 @@ export class HpackWork {
1262
1374
  }
1263
1375
  // `classOf` answers below `HPACK_FIELD_CLASSES`, so the room is at most 2^23 and an unsigned shift holds it.
1264
1376
  const room: u32 = toU32(1) << toU32(j)
1265
- // A length is made at most 65535 at once; a larger class's room is filled out by pushes, which leave the same room.
1266
- const made: u8[] = new Array<u8>(toU16(room < toU32(65535) ? room : toU32(65535)))
1267
- while (toU32(made.length) < room) {
1268
- made.push(0)
1377
+ // One allocation of the class's room; the guard proves its length.
1378
+ const size: i32 = room < toU32(8388609) ? toI32(room) : 0
1379
+ if (size <= 0) {
1380
+ return []
1269
1381
  }
1382
+ const made: u8[] = new Array<u8>(size)
1270
1383
  hpackEmpty(made)
1271
1384
  return made
1272
1385
  }
@@ -1396,8 +1509,7 @@ export class HpackDecoder {
1396
1509
  hpackAppendText(out, hpackStaticName(index))
1397
1510
  return
1398
1511
  }
1399
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1400
- const i: i32 = toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))
1512
+ const i: i32 = hpackDynamicIndex(index)
1401
1513
  hpackAppend(out, this.table.bytes, this.table.nameStart(i), this.table.nameLength(i))
1402
1514
  this.countCopied(this.table.nameLength(i))
1403
1515
  }
@@ -1408,8 +1520,7 @@ export class HpackDecoder {
1408
1520
  hpackAppendText(out, hpackStaticValue(index))
1409
1521
  return
1410
1522
  }
1411
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1412
- const i: i32 = toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))
1523
+ const i: i32 = hpackDynamicIndex(index)
1413
1524
  hpackAppend(out, this.table.bytes, this.table.valueStart(i), this.table.valueLength(i))
1414
1525
  this.countCopied(this.table.valueLength(i))
1415
1526
  }
@@ -1419,8 +1530,7 @@ export class HpackDecoder {
1419
1530
  if (index <= HPACK_STATIC_LENGTH) {
1420
1531
  return toI32(hpackStaticName(index).length)
1421
1532
  }
1422
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1423
- return this.table.nameLength(toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH)))
1533
+ return this.table.nameLength(hpackDynamicIndex(index))
1424
1534
  }
1425
1535
 
1426
1536
  /** What the entry at `index` counts towards a header list, without building it. */
@@ -1432,14 +1542,17 @@ export class HpackDecoder {
1432
1542
  toI64(HPACK_ENTRY_OVERHEAD)
1433
1543
  )
1434
1544
  }
1435
- // `index` is past HPACK_STATIC_LENGTH here, so the unsigned difference does not wrap.
1436
- return toI64(this.table.entrySize(toI32(toU32(index) - toU32(HPACK_STATIC_LENGTH))))
1545
+ return toI64(this.table.entrySize(hpackDynamicIndex(index)))
1437
1546
  }
1438
1547
 
1439
- /** Adds `n` octets, when there are any, to `tableCopied`; the count would need 2^63 octets to wrap. */
1548
+ /**
1549
+ * Adds `n` octets, when there are any, to `tableCopied`. The count is a statistic that tests read as a
1550
+ * difference between two reads, never a length or a decision, so it wraps mod 2^64 rather than panic:
1551
+ * the difference stays exact across a wrap, and reaching one takes 2^63 octets.
1552
+ */
1440
1553
  private countCopied(n: i32): void {
1441
1554
  if (n > 0) {
1442
- this.tableCopied = toI64(toU64(this.tableCopied) + toU64(n))
1555
+ this.tableCopied = wrappingAdd(this.tableCopied, toI64(n))
1443
1556
  }
1444
1557
  }
1445
1558
 
@@ -1741,16 +1854,17 @@ export class HpackEncoder {
1741
1854
  return i
1742
1855
  }
1743
1856
  }
1744
- // Unsigned counters: the dynamic table holds at most `maxSize / 32` entries, so neither they nor the index wrap.
1745
- const n: u32 = toU32(this.table.count())
1746
- for (let k: u32 = 1; k <= n; k += 1) {
1857
+ // The dynamic table holds at most `maxSize / 32` entries, under 2^26, so the literal cap never stops
1858
+ // the walk; it is what proves neither the counter nor the index can overflow. The 61 is
1859
+ // `HPACK_STATIC_LENGTH`, spelled out because the bounds prover reads only a literal.
1860
+ const n: i32 = this.table.count()
1861
+ for (let i: i32 = 1; i <= n && i < 1073741824; i += 1) {
1747
1862
  const t: HpackTable = this.table
1748
- const i: i32 = toI32(k)
1749
1863
  if (
1750
1864
  hpackSameAsWindow(name, t.bytes, t.nameStart(i), t.nameLength(i)) &&
1751
1865
  hpackSameAsWindow(value, t.bytes, t.valueStart(i), t.valueLength(i))
1752
1866
  ) {
1753
- return toI32(toU32(HPACK_STATIC_LENGTH) + k)
1867
+ return 61 + i
1754
1868
  }
1755
1869
  }
1756
1870
  return 0
@@ -1764,12 +1878,11 @@ export class HpackEncoder {
1764
1878
  return i
1765
1879
  }
1766
1880
  }
1767
- // Unsigned counters, as in `findField`.
1768
- const n: u32 = toU32(this.table.count())
1769
- for (let k: u32 = 1; k <= n; k += 1) {
1770
- const i: i32 = toI32(k)
1881
+ // The literal cap and the 61 are as in `findField`.
1882
+ const n: i32 = this.table.count()
1883
+ for (let i: i32 = 1; i <= n && i < 1073741824; i += 1) {
1771
1884
  if (hpackSameAsWindow(name, this.table.bytes, this.table.nameStart(i), this.table.nameLength(i))) {
1772
- return toI32(toU32(HPACK_STATIC_LENGTH) + k)
1885
+ return 61 + i
1773
1886
  }
1774
1887
  }
1775
1888
  return 0