@amritk/nish-x86_64-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
@@ -151,6 +151,11 @@ export const HTTP1_E_INFORMATIONAL_LIMIT: i32 = 17
151
151
  * phrase and fields.
152
152
  */
153
153
  export const HTTP1_E_MEMORY_LIMIT: i32 = 18
154
+ /**
155
+ * `error`: a `feed` whose window `data[off .. off + len)` is not inside
156
+ * `data`, a mistake of the program rather than of the server.
157
+ */
158
+ export const HTTP1_E_WINDOW: i32 = 19
154
159
 
155
160
  // --- The writer -------------------------------------------------------------
156
161
 
@@ -166,13 +171,40 @@ const http1ClientDigits = (n: i64): i32 => {
166
171
  return toI32(digits)
167
172
  }
168
173
 
174
+ /**
175
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
176
+ * step subtracts a non-negative value from one that is no larger than
177
+ * `bytes.length`, so the bounds prover sees no sum that can overflow.
178
+ */
179
+ const http1ClientWindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
180
+ if (off < 0 || len < 0) {
181
+ return -1
182
+ }
183
+ const size: i32 = toI32(bytes.length)
184
+ const room: i32 = size - off
185
+ if (room < 0) {
186
+ return -1
187
+ }
188
+ const gap: i32 = room - len
189
+ let end: i32 = -1
190
+ if (gap >= 0) {
191
+ end = size - gap
192
+ }
193
+ return end
194
+ }
195
+
169
196
  /** Writes the decimal digits of `n`, which is not negative, at `out[at ..)`, which holds them, and answers where they end. */
170
197
  const http1ClientPutDecimal = (out: u8[], at: i32, n: i64): i32 => {
171
- // Unsigned sums: the caller counted room for the digits at `at`, so `end`
172
- // is at most `out.length`, and `end - 1` is at least `at - 1`.
173
- const end: i32 = toI32(toU32(at) + toU32(http1ClientDigits(n)))
198
+ // An `i64` has 1 to 19 digits, and the caller counted room for them at
199
+ // `at`, inside an array, so `at` is far below 2^31 - 20. The tests restate
200
+ // both for the bounds proof; a call outside them writes nothing.
201
+ const digits: i32 = http1ClientDigits(n)
202
+ if (digits < 1 || digits > 19 || at < 0 || at > 2147483628) {
203
+ return at
204
+ }
205
+ const end: i32 = at + digits
174
206
  let v: i64 = n
175
- for (let k: i32 = toI32(toU32(end) - toU32(1)); k >= at && k >= 0 && k < toI32(out.length); k -= 1) {
207
+ for (let k: i32 = end - 1; k >= at && k >= 0 && k < toI32(out.length); k -= 1) {
176
208
  // `v % 10` is a digit, 0 to 9, so adding '0' cannot wrap.
177
209
  out[k] = toU8(toU32(toI32(v % toI64(10))) + toU32(48))
178
210
  v = v / toI64(10)
@@ -551,16 +583,12 @@ const http1ClientHeadArray = (
551
583
  return out
552
584
  }
553
585
 
554
- /**
555
- * `n` zero bytes, appended one at a time: the prover reads only a constant
556
- * length of `new Array` as in range, and these lengths are computed.
557
- */
586
+ /** `n` zero bytes, none when `n` is below 1: one zeroed allocation, not a `push` per byte. */
558
587
  const http1ClientZeros = (n: i32): u8[] => {
559
- const out: u8[] = []
560
- for (let k: i32 = 0; k < n; k += 1) {
561
- out.push(toU8(0))
588
+ if (n <= 0) {
589
+ return []
562
590
  }
563
- return out
591
+ return new Array<u8>(n)
564
592
  }
565
593
 
566
594
  // --- The parser -------------------------------------------------------------
@@ -584,6 +612,8 @@ const HTTP1_CLIENT_LINE: i32 = -1
584
612
 
585
613
  /** The largest `maxBody`: 2^53, so that sums of it cannot overflow an `i64`. */
586
614
  const HTTP1_CLIENT_MAX_BODY: i64 = 9007199254740992
615
+ /** 2^62 - 1, over any body total: so that adding an `i32` to one cannot overflow. */
616
+ const HTTP1_CLIENT_TOTAL_MASK: i64 = HTTP1_CLIENT_MAX_BODY * 512 - 1
587
617
 
588
618
  /** `limit` clamped into 0 to 2^30. */
589
619
  const http1ClientClamp = (limit: i32): i32 => {
@@ -800,8 +830,10 @@ export class Http1ResponseParser {
800
830
 
801
831
  /**
802
832
  * Appends `data[off .. off + len)` to the input. A window outside `data`
803
- * panics. After `finish()`, or once the parser has closed or refused, the
804
- * input is dropped; in a tunnel it is kept for `tunnelBytes()`. Input that
833
+ * is refused with `HTTP1_E_WINDOW`, as final as any refusal, whatever the
834
+ * parser's state, except that a parser that has already refused keeps its
835
+ * first error. After `finish()`, or once the parser has closed or refused,
836
+ * the input is dropped; in a tunnel it is kept for `tunnelBytes()`. Input that
805
837
  * would leave more than 2^30 bytes fed and not read is refused with
806
838
  * `HTTP1_E_MEMORY_LIMIT`.
807
839
  * @param data The bytes read from the connection.
@@ -811,7 +843,10 @@ export class Http1ResponseParser {
811
843
  feed(data: u8[], off: i32, len: i32): void {
812
844
  const size: i32 = toI32(data.length)
813
845
  if (off < 0 || len < 0 || off > size || len > size - off) {
814
- panic("Http1ResponseParser: the window is outside the buffer")
846
+ if (this.state !== HTTP1_CLIENT_S_ERROR) {
847
+ http1ClientFail(this, HTTP1_E_WINDOW, "a feed window outside its buffer")
848
+ }
849
+ return
815
850
  }
816
851
  if (this.closed || this.state === HTTP1_CLIENT_S_CLOSED || this.state === HTTP1_CLIENT_S_ERROR) {
817
852
  return
@@ -820,8 +855,14 @@ export class Http1ResponseParser {
820
855
  http1ClientFail(this, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes fed and not read")
821
856
  return
822
857
  }
823
- // Unsigned: the copy above fit `len` bytes after `end` inside `buf`.
824
- this.end = toI32(toU32(this.end) + toU32(len))
858
+ // The copy above fit `len` bytes after `end` inside `buf`, so the window
859
+ // end is always found; it is asked again so the sum is proven.
860
+ const to: i32 = http1ClientWindowEnd(this.buf, this.end, len)
861
+ if (to < 0) {
862
+ http1ClientFail(this, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes fed and not read")
863
+ return
864
+ }
865
+ this.end = to
825
866
  }
826
867
 
827
868
  /**
@@ -933,8 +974,15 @@ export class Http1ResponseParser {
933
974
  * @returns The count of unread input bytes.
934
975
  */
935
976
  buffered(): i32 {
936
- // Unsigned: the parser keeps `start <= end`, so the difference cannot wrap.
937
- return toI32(toU32(this.end) - toU32(this.start))
977
+ // The parser keeps `0 <= start <= end`, so the count is `end - start`;
978
+ // the test restates that for the bounds proof.
979
+ const start: i32 = this.start
980
+ const end: i32 = this.end
981
+ let count: i32 = 0
982
+ if (end > start && start >= 0) {
983
+ count = end - start
984
+ }
985
+ return count
938
986
  }
939
987
 
940
988
  /**
@@ -948,11 +996,12 @@ export class Http1ResponseParser {
948
996
  if (this.state !== HTTP1_CLIENT_S_TUNNEL || from < 0 || to <= from || to > toI32(this.buf.length)) {
949
997
  return []
950
998
  }
951
- const buf: u8[] = this.buf
952
- const out: u8[] = []
953
- for (let k: i32 = from; k >= 0 && k < to && k < toI32(buf.length); k += 1) {
954
- out.push(buf[k])
999
+ const count: i32 = to - from
1000
+ if (count <= 0) {
1001
+ return []
955
1002
  }
1003
+ const out: u8[] = new Array<u8>(count)
1004
+ tryCopyBytes(out, 0, this.buf, from, count)
956
1005
  return out
957
1006
  }
958
1007
  }
@@ -991,9 +1040,13 @@ const http1ClientReserve = (p: Http1ResponseParser, len: i32): boolean => {
991
1040
  return false
992
1041
  }
993
1042
  p.buf = buf
994
- // Unsigned: `scan` and `start` move down together, and `scan` is past `start`
995
- // whenever it is read.
996
- p.scan = toI32(toU32(p.scan) - toU32(from))
1043
+ // `scan` and `start` move down together, and `scan` is past `start`
1044
+ // whenever it is read. `scan` is an offset into `buf`, never negative, and
1045
+ // `from` was asked above not to be, so the difference fits.
1046
+ const scan: i32 = p.scan
1047
+ if (scan >= 0) {
1048
+ p.scan = scan - from
1049
+ }
997
1050
  p.start = 0
998
1051
  p.end = toI32(live)
999
1052
  return true
@@ -1195,11 +1248,13 @@ const http1ClientFieldLine = (p: Http1ResponseParser, keep: boolean): i32 => {
1195
1248
  return http1ClientFail(p, HTTP1_E_FIELD, "a control character in a field value")
1196
1249
  }
1197
1250
  }
1198
- // Counting stops at the limit (at most 2^31 - 1), so the sum does not wrap.
1199
- p.fieldCount = toI32(toU32(p.fieldCount) + toU32(1))
1200
- if (p.fieldCount > p.maxHeaders) {
1251
+ // A count already at the limit refuses the field before it is counted, so
1252
+ // the count stays below `maxHeaders` and the sum cannot overflow.
1253
+ const fields: i32 = p.fieldCount
1254
+ if (fields >= p.maxHeaders) {
1201
1255
  return http1ClientFail(p, HTTP1_E_COUNT_LIMIT, "more fields than the limit")
1202
1256
  }
1257
+ p.fieldCount = fields + 1
1203
1258
  // A trailer is checked and counted, but not kept: nothing here reads one.
1204
1259
  if (keep) {
1205
1260
  let valueStart: i32 = nameEnd + 1
@@ -1257,19 +1312,23 @@ const http1ClientLengthList = (head: u8[], from: i32, to: i32, seen: i64): i64 =
1257
1312
  if (a === b) {
1258
1313
  return toI64(-1)
1259
1314
  }
1260
- let v: i64 = toI64(0)
1315
+ // The digits gather in a `u64`, as the other digit loops in `std/`
1316
+ // do. The test keeps the value at most 2^53, so it never wraps, and
1317
+ // the bounds proof, which bounds no product past 2^31, has no signed
1318
+ // multiply to check.
1319
+ let u: u64 = toU64(0)
1261
1320
  for (let j: i32 = a; j >= 0 && j < b && j < toI32(head.length); j += 1) {
1262
1321
  const c: i32 = toI32(head[j])
1263
1322
  if (c < 48 || c > 57) {
1264
1323
  return toI64(-1)
1265
1324
  }
1266
- if (v <= (HTTP1_CLIENT_MAX_BODY - toI64(c - 48)) / toI64(10)) {
1267
- // Unsigned: `v` starts at 0 and the test keeps it at most 2^53.
1268
- v = toI64(toU64(v) * toU64(10) + toU64(c - 48))
1325
+ if (u <= toU64(HTTP1_CLIENT_MAX_BODY - toI64(c - 48)) / toU64(10)) {
1326
+ u = u * toU64(10) + toU64(c - 48)
1269
1327
  } else {
1270
- v = HTTP1_CLIENT_MAX_BODY + toI64(1)
1328
+ u = toU64(HTTP1_CLIENT_MAX_BODY) + toU64(1)
1271
1329
  }
1272
1330
  }
1331
+ const v: i64 = toI64(u)
1273
1332
  if (found >= toI64(0) && (found !== v || v > HTTP1_CLIENT_MAX_BODY)) {
1274
1333
  return toI64(-2)
1275
1334
  }
@@ -1377,7 +1436,7 @@ const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lowe
1377
1436
  }
1378
1437
  // Unsigned, as are the sizes below: `a <= b` and `at <= used` were just
1379
1438
  // asked, and `size` starts at `used` or more, so no difference wraps.
1380
- const n: i32 = toI32(toU32(b) - toU32(a))
1439
+ const n: i32 = b - a
1381
1440
  if (toU32(n) > toU32(used) - toU32(at)) {
1382
1441
  let size: u32 = used < 256 ? toU32(256) : toU32(used)
1383
1442
  while (size - toU32(at) < toU32(n)) {
@@ -1395,8 +1454,12 @@ const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lowe
1395
1454
  if (!tryCopyBytes(p.head, at, buf, a, n)) {
1396
1455
  return -1
1397
1456
  }
1398
- // Unsigned: the copy above fit `n` bytes after `at` inside `head`.
1399
- const stop: i32 = toI32(toU32(at) + toU32(n))
1457
+ // The copy above fit `n` bytes after `at` inside `head`, so the window end
1458
+ // is always found; it is asked again so the sum is proven.
1459
+ const stop: i32 = http1ClientWindowEnd(p.head, at, n)
1460
+ if (stop < 0) {
1461
+ return -1
1462
+ }
1400
1463
  if (lower) {
1401
1464
  const head: u8[] = p.head
1402
1465
  for (let k: i32 = at; k >= 0 && k < stop && k < toI32(head.length); k += 1) {
@@ -1413,9 +1476,14 @@ const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lowe
1413
1476
  /** Records the field whose name starts at `name` and whose value is `head[value .. valueEnd)`. */
1414
1477
  const http1ClientSpan = (p: Http1ResponseParser, name: i32, value: i32, valueEnd: i32): void => {
1415
1478
  const spans: i32[] = p.spans
1416
- // Unsigned: `spans` holds 4 entries a field, so `spanCount * 4` fits an
1417
- // `i32` length and neither the product nor the count wraps.
1418
- const at: i32 = toI32(toU32(p.spanCount) * toU32(4))
1479
+ // `spans` holds 4 entries a field, so `spanCount * 4` fits an `i32` length:
1480
+ // `spanCount` is at most 2^29 - 1, which the test restates for the bounds
1481
+ // proof. A count outside it would only append.
1482
+ const count: i32 = p.spanCount
1483
+ let at: i32 = -1
1484
+ if (count >= 0 && count <= 536870911) {
1485
+ at = count * 4
1486
+ }
1419
1487
  if (at >= 0 && at <= toI32(spans.length) - 4) {
1420
1488
  spans[at] = name
1421
1489
  spans[at + 1] = value
@@ -1427,7 +1495,11 @@ const http1ClientSpan = (p: Http1ResponseParser, name: i32, value: i32, valueEnd
1427
1495
  spans.push(value)
1428
1496
  spans.push(valueEnd)
1429
1497
  }
1430
- p.spanCount = toI32(toU32(p.spanCount) + toU32(1))
1498
+ // For the same reason the count never reaches 2^31 - 1; the test restates
1499
+ // that for the bounds proof.
1500
+ if (count < 2147483647) {
1501
+ p.spanCount = count + 1
1502
+ }
1431
1503
  }
1432
1504
 
1433
1505
  /** Whether `head[a .. b)` is `word`, byte for byte. */
@@ -1631,7 +1703,10 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1631
1703
  const a: i32 = p.lineStart
1632
1704
  const b: i32 = p.lineEnd < toI32(buf.length) ? p.lineEnd : toI32(buf.length)
1633
1705
  const room: i64 = http1ClientRoom(p)
1634
- let size: i64 = toI64(0)
1706
+ // The size gathers in a `u64`, as the other digit loops in `std/` do. The
1707
+ // test below keeps it at most `room`, so it never wraps, and the bounds
1708
+ // proof, which bounds no product past 2^31, has no signed multiply to check.
1709
+ let size: u64 = toU64(0)
1635
1710
  let over: boolean = false
1636
1711
  let i: i32 = a
1637
1712
  while (i >= 0 && i < b && i < toI32(buf.length)) {
@@ -1640,12 +1715,12 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1640
1715
  break
1641
1716
  }
1642
1717
  if (!over) {
1643
- // Unsigned: the first test leaves `digit` at most `room`.
1644
- if (toI64(digit) > room || size > toI64(toU64(room) - toU64(toI64(digit))) / toI64(16)) {
1718
+ // The first two tests leave `digit` at most `room`, so the difference
1719
+ // fits and is not negative.
1720
+ if (room < 0 || toI64(digit) > room || size > toU64(room - toI64(digit)) / toU64(16)) {
1645
1721
  over = true
1646
1722
  } else {
1647
- // Unsigned: `size` starts at 0 and the test keeps it at most `room`.
1648
- size = toI64(toU64(size) * toU64(16) + toU64(digit))
1723
+ size = size * toU64(16) + toU64(digit)
1649
1724
  }
1650
1725
  }
1651
1726
  i += 1
@@ -1678,8 +1753,8 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1678
1753
  if (over) {
1679
1754
  return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a chunked body over the limit")
1680
1755
  }
1681
- p.left = size
1682
- p.state = size === toI64(0) ? HTTP1_CLIENT_S_TRAILERS : HTTP1_CLIENT_S_CHUNK_DATA
1756
+ p.left = toI64(size)
1757
+ p.state = size === toU64(0) ? HTTP1_CLIENT_S_TRAILERS : HTTP1_CLIENT_S_CHUNK_DATA
1683
1758
  p.sectionBytes = 0
1684
1759
  return HTTP1_NEED_MORE
1685
1760
  }
@@ -1699,15 +1774,34 @@ const http1ClientBodyWindow = (p: Http1ResponseParser, after: i32): i32 => {
1699
1774
  const ready: i32 = end - start
1700
1775
  const left: i64 = p.left
1701
1776
  const take: i32 = toI64(ready) < left ? ready : toI32(left)
1777
+ // `left` only falls from a length, a chunk size or the room, none of them
1778
+ // negative, and by at most itself; `take` is at most `ready` and `left`;
1779
+ // `bodyTotal` grows from 0 by what `maxBody` (at most 2^53) allows. The
1780
+ // tests restate that for the bounds proof, so none of the sums below
1781
+ // overflows.
1782
+ const total: i64 = p.bodyTotal
1783
+ if (left < 0 || take < 0 || take > ready || total < 0 || total > HTTP1_CLIENT_TOTAL_MASK) {
1784
+ return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a body over the limit")
1785
+ }
1786
+ // What stays buffered: not negative, as `take` is at most `ready`, and at
1787
+ // most `ready`, so at most `end`. The test restates the last for the
1788
+ // bounds proof, which keeps no relation between `ready` and `end`.
1789
+ const rest: i32 = ready - take
1790
+ if (rest < 0 || rest > end) {
1791
+ return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a body over the limit")
1792
+ }
1702
1793
  p.body = p.buf
1703
1794
  p.bodyOff = start
1704
1795
  p.bodyLen = take
1705
- // Unsigned: `take` is at most `ready`, `left`, and the room under
1706
- // `maxBody` (at most 2^53), so none of these wraps.
1707
- p.start = toI32(toU32(start) + toU32(take))
1796
+ // `end - rest` is `start + take`, spelled as a difference of values that
1797
+ // are not negative.
1798
+ p.start = end - rest
1708
1799
  p.scan = p.start
1709
- p.left = toI64(toU64(left) - toU64(toI64(take)))
1710
- p.bodyTotal = toI64(toU64(p.bodyTotal) + toU64(toI64(take)))
1800
+ p.left = left - toI64(take)
1801
+ // `total` is at most the mask, tested above, so the mask changes nothing:
1802
+ // it is how the bounds proof learns that top, which it keeps from no
1803
+ // comparison past 2^31.
1804
+ p.bodyTotal = (total & HTTP1_CLIENT_TOTAL_MASK) + toI64(take)
1711
1805
  if (p.left === toI64(0)) {
1712
1806
  p.state = after
1713
1807
  }
@@ -1716,9 +1810,19 @@ const http1ClientBodyWindow = (p: Http1ResponseParser, after: i32): i32 => {
1716
1810
 
1717
1811
  /**
1718
1812
  * The body bytes `maxBody` still allows: `maxBody - bodyTotal`, negative
1719
- * when a caller lowered `maxBody` under it (unsigned, then read signed).
1813
+ * when a caller lowered `maxBody` under it. Both are kept at or above 0, so
1814
+ * the difference fits; were either negative, the answer is -1, which every
1815
+ * caller reads as no room, as it reads any negative room.
1720
1816
  */
1721
- const http1ClientRoom = (p: Http1ResponseParser): i64 => toI64(toU64(p.maxBody) - toU64(p.bodyTotal))
1817
+ const http1ClientRoom = (p: Http1ResponseParser): i64 => {
1818
+ const max: i64 = p.maxBody
1819
+ const total: i64 = p.bodyTotal
1820
+ let room: i64 = toI64(-1)
1821
+ if (max >= 0 && total >= 0) {
1822
+ room = max - total
1823
+ }
1824
+ return room
1825
+ }
1722
1826
 
1723
1827
  /** A body read until the server closes: every byte fed, up to `maxBody`. */
1724
1828
  const http1ClientCloseBody = (p: Http1ResponseParser): i32 => {