@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
@@ -90,7 +90,6 @@ import {
90
90
  dnsBuildQuery,
91
91
  dnsParseReply,
92
92
  } from "nish/net/dns-message"
93
- import { setU16BE } from "nish/bytes"
94
93
  import { DnsConfig, DnsHosts, dnsHostsFind, dnsParseAddress } from "nish/net/dns-config"
95
94
 
96
95
  /** `result` for a lookup that has not finished. */
@@ -560,6 +559,12 @@ export class DnsResolver {
560
559
  ) {
561
560
  return -1
562
561
  }
562
+ // Every query buffer holds `DNS_QUERY_MAX_BYTES`, so this is never taken:
563
+ // it is the test the ID's two bytes below are written under.
564
+ const query: u8[] = queries[q]
565
+ if (toI32(query.length) < 2) {
566
+ return -1
567
+ }
563
568
  const kind: i32 = pending[q]
564
569
  pending[q] = 0
565
570
  this.cursor = q + 1 < n ? q + 1 : 0
@@ -568,12 +573,13 @@ export class DnsResolver {
568
573
  if (kind === DNS_CLOSE) {
569
574
  return q
570
575
  }
571
- const query: u8[] = queries[q]
572
- setU16BE(query, 0, toU16(id))
576
+ query[0] = toU8(id >> 8)
577
+ query[1] = toU8(id)
573
578
  const length: i32 = queryLength[q]
574
579
  const skip: i32 = kind === DNS_SEND_TCP ? 2 : 0
575
580
  if (skip === 2) {
576
- setU16BE(out, 0, toU16(length))
581
+ out[0] = toU8(length >> 8)
582
+ out[1] = toU8(length)
577
583
  }
578
584
  // A query's length is at most its buffer's, and `out` holds the longest
579
585
  // with its two-byte prefix, so `o` ends at `skip + length`.
@@ -1033,9 +1039,11 @@ const dnsResolverLoopback = (out: u8[], port: i32): void => {
1033
1039
  out[11] = toU8(255)
1034
1040
  out[12] = toU8(127)
1035
1041
  out[15] = toU8(1)
1036
- setU16BE(out, 16, toU16(port))
1042
+ out[16] = toU8(port >> 8)
1043
+ out[17] = toU8(port)
1037
1044
  out[DNS_ADDRESS_BYTES + 15] = toU8(1)
1038
- setU16BE(out, DNS_ADDRESS_BYTES + 16, toU16(port))
1045
+ out[DNS_ADDRESS_BYTES + 16] = toU8(port >> 8)
1046
+ out[DNS_ADDRESS_BYTES + 17] = toU8(port)
1039
1047
  }
1040
1048
 
1041
1049
  /**
@@ -1445,17 +1453,25 @@ const dnsResolverAsk = (r: DnsResolver, q: i32, qtype: i32, nowNanos: i64): void
1445
1453
  const query: u8[] = queries[q]
1446
1454
  const name: u8[] = names[q]
1447
1455
  const length: i32 = nameLength[q]
1448
- // `255` is DNS_MAX_NAME_BYTES: every name written to `names` fits it, so this never returns.
1449
- if (length < 0 || length > 255) {
1456
+ // `255` is DNS_MAX_NAME_BYTES: every name written to `names` fits it, and
1457
+ // every query buffer holds `DNS_QUERY_MAX_BYTES` (`16 + 255`), so this never
1458
+ // returns.
1459
+ if (length < 0 || length > 255 || toI32(query.length) < DNS_QUERY_MAX_BYTES) {
1450
1460
  return
1451
1461
  }
1452
1462
  // The bytes are dnsBuildQuery's: the ID, RD, one question, then the name, type and class IN.
1453
- setU16BE(query, 0, toU16(0))
1454
- setU16BE(query, 2, toU16(0x0100))
1455
- setU16BE(query, 4, toU16(1))
1456
- setU16BE(query, 6, toU16(0))
1457
- setU16BE(query, 8, toU16(0))
1458
- setU16BE(query, 10, toU16(0))
1463
+ query[0] = toU8(0)
1464
+ query[1] = toU8(0)
1465
+ query[2] = toU8(0x01)
1466
+ query[3] = toU8(0)
1467
+ query[4] = toU8(0)
1468
+ query[5] = toU8(1)
1469
+ query[6] = toU8(0)
1470
+ query[7] = toU8(0)
1471
+ query[8] = toU8(0)
1472
+ query[9] = toU8(0)
1473
+ query[10] = toU8(0)
1474
+ query[11] = toU8(0)
1459
1475
  let i: i32 = 0
1460
1476
  let o: i32 = 12
1461
1477
  while (i < length && i < toI32(name.length) && o >= 0 && o < toI32(query.length)) {
@@ -1463,8 +1479,11 @@ const dnsResolverAsk = (r: DnsResolver, q: i32, qtype: i32, nowNanos: i64): void
1463
1479
  i += 1
1464
1480
  o += 1
1465
1481
  }
1466
- setU16BE(query, 12 + length, toU16(qtype))
1467
- setU16BE(query, 14 + length, toU16(1))
1482
+ const typeAt: i32 = 12 + length
1483
+ query[typeAt] = toU8(qtype >> 8)
1484
+ query[typeAt + 1] = toU8(qtype)
1485
+ query[typeAt + 2] = toU8(0)
1486
+ query[typeAt + 3] = toU8(1)
1468
1487
  queryLength[q] = 16 + length
1469
1488
  // Only this name's reply may say NXDOMAIN, not an earlier candidate's or lookup's.
1470
1489
  nxdomain[q] = false
package/std/net/dns.ts CHANGED
@@ -781,7 +781,7 @@ export const dnsLookupWith = (
781
781
  const begun: i64 = monotonicNanos()
782
782
  // The budget and the time spent are kept apart, where `begun` plus the
783
783
  // budget could overflow: `now >= end` is `spent >= budget`. The clock only
784
- // moves on, so `spent` is its true value, which `u64` holds exactly.
784
+ // moves on, so `spent` is its true value.
785
785
  const budget: i64 = toI64(timeoutMs) * 1000000
786
786
  const lookup: i32 = client.start(name, port, begun)
787
787
  if (lookup < 0) {
@@ -792,7 +792,8 @@ export const dnsLookupWith = (
792
792
  let status: i32 = client.result(lookup, out, ttls)
793
793
  while (status === DNS_RESOLVE_PENDING) {
794
794
  const now: i64 = monotonicNanos()
795
- const spent: i64 = toI64(toU64(now) - toU64(begun))
795
+ // The clock counts up from boot; a reading behind `begun` has spent none.
796
+ const spent: i64 = begun >= 0 && now >= 0 && now >= begun ? now - begun : 0
796
797
  if (spent >= budget) {
797
798
  if (client.settle(lookup, now)) {
798
799
  status = client.result(lookup, out, ttls)
package/std/net/fetch.ts CHANGED
@@ -82,8 +82,8 @@
82
82
  *
83
83
  * Written from RFC 9110 and RFC 9112, not ported from another implementation.
84
84
  */
85
- import { base64Encode } from "nish/base64"
86
- import { copyBytes } from "nish/bytes"
85
+ import { base64EncodeIfFits } from "nish/base64"
86
+ import { tryCopyBytes } from "nish/bytes"
87
87
  import {
88
88
  HTTP1_BODY,
89
89
  HTTP1_CLOSED,
@@ -125,6 +125,7 @@ import {
125
125
  tcpConnect,
126
126
  } from "nish:net"
127
127
  import { cancelled, readable, readableWithin, sleep, writable, writableWithin } from "nish:async"
128
+ import { wrappingAdd } from "nish:unsafe"
128
129
  import { getenv, monotonicNanos, wallClockNanos } from "nish:process"
129
130
  import { PkiRoots, pkiLoadRoots, pkiLoadSystemRoots } from "nish/crypto/pki-roots"
130
131
  import { TlsClient, TlsClientConfig, tlsClientIsIpLiteral } from "nish/net/tls/client"
@@ -194,6 +195,8 @@ const HTTP_CLIENT_NS_PER_MS: i64 = 1000000
194
195
  const HTTP_CLIENT_ASYNC_BODY_MAX: i64 = 2147483647
195
196
  /** A deadline that never comes: 146 years of monotonic nanoseconds, far below the overflow of a sum with a timeout. */
196
197
  const HTTP_CLIENT_NEVER: i64 = 4611686018 * 1000000000
198
+ /** 2^62 - 1, over `HTTP_CLIENT_NEVER` and so over any deadline: so that adding a timeout to one cannot overflow. */
199
+ const HTTP_CLIENT_LOW62: i64 = 2147483648 * 2147483648 - 1
197
200
  /** netRead's and netWrite's answer when the socket has nothing ready. */
198
201
  const HTTP_CLIENT_WOULD_BLOCK: i32 = -11
199
202
  /** pollAdd's events: readable, writable. */
@@ -547,8 +550,9 @@ export class FetchSyncClient {
547
550
  // An unread body is still on the wire ahead of the next response.
548
551
  this.drop()
549
552
  }
550
- // Only compared for equality, so wrapping past 2^32 responses is harmless.
551
- this.generation = toI32(toU32(this.generation) + toU32(1))
553
+ // A generation is only compared for equality, so it counts mod 2^32: a
554
+ // wrap after 2^32 responses is the counter's answer, not an overflow.
555
+ this.generation = wrappingAdd(this.generation, 1)
552
556
  this.deadline = httpClientDeadline(this.options)
553
557
  const fault: HttpClientFault = new HttpClientFault()
554
558
  let url: Url | null = httpClientTarget(req.url, this.options, fault)
@@ -897,14 +901,26 @@ export class FetchSyncClient {
897
901
  */
898
902
  private readRaw(want: i32): i32 {
899
903
  const until: i64 = this.readDeadline()
900
- let n: i32 = netRead(this.fd, this.rbuf, 0, want)
904
+ const rbuf: u8[] = this.rbuf
905
+ // Every caller asks for at most `HTTP_CLIENT_READ_BYTES`, the buffer's
906
+ // size; tested before each read, since a call between takes what the
907
+ // prover knew of the length.
908
+ if (want < 0 || want > toI32(rbuf.length)) {
909
+ this.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
910
+ return -1
911
+ }
912
+ let n: i32 = netRead(this.fd, rbuf, 0, want)
901
913
  while (n === HTTP_CLIENT_WOULD_BLOCK) {
902
914
  const waited: i32 = this.wait(this.fd, HTTP_CLIENT_READABLE, until, HTTP_E_READ_TIMEOUT)
903
915
  if (waited !== HTTP_E_NONE) {
904
916
  this.failWait(waited, `the server sent nothing for ${this.options.readTimeoutMs} ms`)
905
917
  return -1
906
918
  }
907
- n = netRead(this.fd, this.rbuf, 0, want)
919
+ if (want < 0 || want > toI32(rbuf.length)) {
920
+ this.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
921
+ return -1
922
+ }
923
+ n = netRead(this.fd, rbuf, 0, want)
908
924
  }
909
925
  if (n >= 0) {
910
926
  return n
@@ -979,8 +995,13 @@ export class FetchSyncClient {
979
995
  while (left > 0 && at >= 0) {
980
996
  const n: i32 = conn.write(bytes, at, left)
981
997
  if (n > 0 && n <= left) {
982
- // At most `off + len`, inside `bytes`, so the unsigned sum cannot wrap.
983
- at = toI32(toU32(at) + toU32(n))
998
+ // At most `off + len`, inside `bytes`, so the window always has an
999
+ // end; the helper measures it so that the sum is proven to fit.
1000
+ at = httpClientWindowEnd(bytes, at, n)
1001
+ if (at < 0) {
1002
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1003
+ return false
1004
+ }
984
1005
  left -= n
985
1006
  } else if (n > 0 || n !== TLS_RECORD_AGAIN || conn.outputEnd === conn.outputStart) {
986
1007
  this.lost = true
@@ -996,22 +1017,33 @@ export class FetchSyncClient {
996
1017
 
997
1018
  /** Writes `bytes[off, off + len)` to `fd`, waiting up to `readTimeoutMs` each time it is full. */
998
1019
  private writeRaw(bytes: u8[], off: i32, len: i32): boolean {
999
- if (off < 0 || len < 0 || len > toI32(bytes.length) - off) {
1020
+ // Where the window ends, or -1 when it does not lie inside `bytes`.
1021
+ const end: i32 = httpClientWindowEnd(bytes, off, len)
1022
+ if (end < 0) {
1000
1023
  this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1001
1024
  return false
1002
1025
  }
1003
1026
  let at: i32 = off
1004
- let left: i32 = len
1005
1027
  // `at >= 0` is the loop's own, since the prover does not carry it round.
1006
- while (left > 0 && at >= 0) {
1028
+ while (at < end && at >= 0) {
1007
1029
  if (this.overdue()) {
1008
1030
  return false
1009
1031
  }
1010
- const n: i32 = netWrite(this.fd, bytes, at, left)
1011
- if (n > 0 && n <= left) {
1012
- // At most `off + len`, inside `bytes`, so the unsigned sum cannot wrap.
1013
- at = toI32(toU32(at) + toU32(n))
1014
- left -= n
1032
+ // The window's test again, after the calls that took what the prover
1033
+ // knew of it; it passes wherever the first one did.
1034
+ if (at < 0 || at > end || end > toI32(bytes.length)) {
1035
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1036
+ return false
1037
+ }
1038
+ const n: i32 = netWrite(this.fd, bytes, at, end - at)
1039
+ if (n > 0 && n <= end - at) {
1040
+ // Short of `end`, inside `bytes`; the helper measures it so that the
1041
+ // sum is proven to fit.
1042
+ at = httpClientWindowEnd(bytes, at, n)
1043
+ if (at < 0) {
1044
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1045
+ return false
1046
+ }
1015
1047
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
1016
1048
  const waited: i32 = this.wait(this.fd, HTTP_CLIENT_WRITABLE, this.readDeadline(), HTTP_E_READ_TIMEOUT)
1017
1049
  if (waited !== HTTP_E_NONE) {
@@ -1054,8 +1086,16 @@ export class FetchSyncClient {
1054
1086
  const one: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
1055
1087
  let last: i32 = 0
1056
1088
  for (let i: i32 = 0; i < usable && i < DNS_RESOLVE_MAX_ADDRESSES; i += 1) {
1057
- // i < DNS_RESOLVE_MAX_ADDRESSES, so the unsigned product stays inside addresses.
1058
- copyBytes(one, 0, addresses, toI32(toU32(i) * toU32(DNS_ADDRESS_BYTES)), DNS_ADDRESS_BYTES)
1089
+ // `i` is below `DNS_RESOLVE_MAX_ADDRESSES` (16), so the product stays
1090
+ // inside `addresses`; the test restates that with the literal the
1091
+ // bounds proof reads.
1092
+ if (i > 15) {
1093
+ break
1094
+ }
1095
+ // An address the list does not hold all of is one past its end.
1096
+ if (!tryCopyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)) {
1097
+ break
1098
+ }
1059
1099
  const fd: i32 = tcpConnect(one)
1060
1100
  if (fd < 0) {
1061
1101
  last = fd
@@ -1251,11 +1291,22 @@ export class FetchSyncClient {
1251
1291
  }
1252
1292
  }
1253
1293
  const take: i32 = n < this.pendLen ? n : this.pendLen
1254
- copyBytes(buf, off, this.parser.body, this.pendOff, take)
1255
- // take <= pendLen, and pendOff + pendLen ends inside the parser's body,
1256
- // so neither unsigned step can wrap.
1257
- this.pendOff = toI32(toU32(this.pendOff) + toU32(take))
1258
- this.pendLen = toI32(toU32(this.pendLen) - toU32(take))
1294
+ // `read` checked the caller's window and the parser keeps its body
1295
+ // window inside its buffer, so the copy fits; one that did not would be
1296
+ // a client out of step with its parser, answered as the test below is.
1297
+ if (!tryCopyBytes(buf, off, this.parser.body, this.pendOff, take)) {
1298
+ return -HTTP_E_IO
1299
+ }
1300
+ // The copy above fit `take` bytes at `pendOff` inside the parser's body,
1301
+ // so the window has an end, and `take` is at most `pendLen`. The helper
1302
+ // and the test restate both for the bounds proof.
1303
+ const pendEnd: i32 = httpClientWindowEnd(this.parser.body, this.pendOff, take)
1304
+ const pendLen: i32 = this.pendLen
1305
+ if (pendEnd < 0 || take < 0 || take > pendLen) {
1306
+ return -HTTP_E_IO
1307
+ }
1308
+ this.pendOff = pendEnd
1309
+ this.pendLen = pendLen - take
1259
1310
  return take
1260
1311
  }
1261
1312
 
@@ -1461,17 +1512,41 @@ export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): bool
1461
1512
 
1462
1513
  // --- helpers -----------------------------------------------------------------
1463
1514
 
1515
+ /**
1516
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
1517
+ * step subtracts a value that is not negative from one that is no larger than
1518
+ * `bytes.length`, so the bounds proof sees no sum that can overflow.
1519
+ */
1520
+ const httpClientWindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
1521
+ if (off < 0 || len < 0) {
1522
+ return -1
1523
+ }
1524
+ const size: i32 = toI32(bytes.length)
1525
+ const room: i32 = size - off
1526
+ if (room < 0) {
1527
+ return -1
1528
+ }
1529
+ const gap: i32 = room - len
1530
+ let end: i32 = -1
1531
+ if (gap >= 0) {
1532
+ end = size - gap
1533
+ }
1534
+ return end
1535
+ }
1536
+
1464
1537
  /**
1465
1538
  * `ms` milliseconds after `start` on the monotonic clock, or
1466
1539
  * `HTTP_CLIENT_NEVER` for a `start` outside [0, 2^62] or a negative `ms`,
1467
1540
  * which the clock and the options never give.
1468
1541
  */
1469
1542
  const httpClientAfter = (start: i64, ms: i32): i64 => {
1470
- if (start < toI64(0) || start > HTTP_CLIENT_NEVER || ms < 0) {
1543
+ if (start < 0 || start > HTTP_CLIENT_NEVER || ms < 0) {
1471
1544
  return HTTP_CLIENT_NEVER
1472
1545
  }
1473
- // start <= 2^62 and the product < 2^51, so the unsigned sum cannot wrap.
1474
- return toI64(toU64(start) + toU64(toI64(ms) * toI64(1000000)))
1546
+ // `start` is at most `HTTP_CLIENT_NEVER`, under 2^62, so the mask changes
1547
+ // nothing: it is how the bounds proof learns that top, which it keeps from
1548
+ // no comparison past 2^31. The product is under 2^51, so the sum fits.
1549
+ return (start & HTTP_CLIENT_LOW62) + toI64(ms) * toI64(1000000)
1475
1550
  }
1476
1551
 
1477
1552
  /**
@@ -1482,15 +1557,17 @@ const httpClientWaitMs = (end: i64, now: i64): i64 => {
1482
1557
  if (end <= now) {
1483
1558
  return toI64(0)
1484
1559
  }
1485
- if (now < toI64(0) || end > HTTP_CLIENT_NEVER) {
1560
+ if (now < 0 || end > HTTP_CLIENT_NEVER) {
1486
1561
  return HTTP_CLIENT_MAX_WAIT_MS
1487
1562
  }
1488
- // 0 <= now < end <= 2^62, so the unsigned difference cannot wrap.
1489
- const left: i64 = toI64(toU64(end) - toU64(now))
1563
+ // 0 <= now < end, so the difference fits.
1564
+ const left: i64 = end - now
1490
1565
  let ms: i64 = left / toI64(1000000)
1491
- if (left % toI64(1000000) > toI64(0)) {
1492
- // At most 2^62 / 10^6, far from wrapping.
1493
- ms = toI64(toU64(ms) + toU64(1))
1566
+ // A wait of `HTTP_CLIENT_MAX_WAIT_MS` (3600000) or more is cut to that
1567
+ // below whether it is rounded up or not, so only a shorter one is, and the
1568
+ // literal bound proves the step fits.
1569
+ if (left % toI64(1000000) > toI64(0) && ms < 3600000) {
1570
+ ms = ms + toI64(1)
1494
1571
  }
1495
1572
  return ms > HTTP_CLIENT_MAX_WAIT_MS ? HTTP_CLIENT_MAX_WAIT_MS : ms
1496
1573
  }
@@ -1584,8 +1661,14 @@ const httpClientDropField = (names: string[], values: string[], lower: string):
1584
1661
  names[keep] = name
1585
1662
  values[keep] = value
1586
1663
  }
1587
- // keep <= i, so the unsigned step cannot wrap.
1588
- keep = toI32(toU32(keep) + toU32(1))
1664
+ // `keep` is at most `i`, below the length, so the step fits. Where
1665
+ // they are equal it is written `i + 1`, the form the bounds proof
1666
+ // reads from the loop test.
1667
+ if (keep < i) {
1668
+ keep = keep + 1
1669
+ } else {
1670
+ keep = i + 1
1671
+ }
1589
1672
  }
1590
1673
  }
1591
1674
  while (toI32(names.length) > keep && toI32(names.length) > 0) {
@@ -1732,7 +1815,7 @@ const httpClientProxyUrl = (text: string): Url | null => {
1732
1815
  /**
1733
1816
  * The `Proxy-Authorization` value for a proxy URL's userinfo, `Basic` and
1734
1817
  * the decoded `user:password` in base64; "" when it has none; null when
1735
- * either half is not valid percent-encoding.
1818
+ * either half is not valid percent-encoding or the pair is too long to encode.
1736
1819
  */
1737
1820
  const httpClientProxyAuth = (proxy: Url): string | null => {
1738
1821
  if (proxy.username === "" && proxy.password === "") {
@@ -1743,7 +1826,26 @@ const httpClientProxyAuth = (proxy: Url): string | null => {
1743
1826
  if (user === null || password === null) {
1744
1827
  return null
1745
1828
  }
1746
- return "Basic " + base64Encode(httpFieldBytes(user + ":" + password))
1829
+ // Encoded here, not by `base64Encode`, so that a credential too long to
1830
+ // encode, past 1,610,612,733 bytes, answers null instead of panicking.
1831
+ const field: u8[] = httpFieldBytes(user + ":" + password)
1832
+ if (field.length > 1610612733) {
1833
+ return null
1834
+ }
1835
+ const n: i32 = toI32(field.length)
1836
+ if (n < 0 || n > 1610612733) {
1837
+ return null
1838
+ }
1839
+ // Not negative for that `n`; the test is what lets the compiler see it.
1840
+ const length: i32 = toI32((n + 2) / 3) * 4
1841
+ if (length < 0) {
1842
+ return null
1843
+ }
1844
+ const out: u8[] = new Array<u8>(length)
1845
+ if (base64EncodeIfFits(field, 0, n, out, 0) < 0) {
1846
+ return null
1847
+ }
1848
+ return "Basic " + String.fromBytes(out, 0, toI32(out.length))
1747
1849
  }
1748
1850
 
1749
1851
  /** The first value of the field `name`, in any case, from parallel name and value lists; null when there is none. */
@@ -1889,9 +1991,13 @@ const httpClientTlsVerify = (
1889
1991
  */
1890
1992
  const httpClientTlsClose = (conn: TlsRecordClient, fd: i32): void => {
1891
1993
  conn.close()
1892
- const len: i32 = httpClientTlsPending(conn)
1893
- if (fd >= 0 && len > 0) {
1894
- netWrite(fd, conn.output, conn.outputStart, len)
1994
+ const output: u8[] = conn.output
1995
+ const start: i32 = conn.outputStart
1996
+ const end: i32 = conn.outputEnd
1997
+ // The pending window, tested as `httpClientTlsPending` does; one outside
1998
+ // the buffer is nothing to send.
1999
+ if (fd >= 0 && start >= 0 && start < end && end <= toI32(output.length)) {
2000
+ netWrite(fd, output, start, end - start)
1895
2001
  }
1896
2002
  conn.wipe()
1897
2003
  }
@@ -1903,7 +2009,10 @@ const httpClientConnectHead = (host: string, port: i32, proxy: Url, fault: HttpC
1903
2009
  const values: string[] = [authority]
1904
2010
  const auth: string | null = httpClientProxyAuth(proxy)
1905
2011
  if (auth === null) {
1906
- fault.fail(HTTP_E_PROXY, "the proxy's user name or password is not valid percent-encoding")
2012
+ fault.fail(
2013
+ HTTP_E_PROXY,
2014
+ "the proxy's user name or password is not valid percent-encoding, or too long to encode"
2015
+ )
1907
2016
  return null
1908
2017
  }
1909
2018
  if (auth !== "") {
@@ -2136,7 +2245,10 @@ const httpClientRequestHead = (
2136
2245
  const proxy: Url | null = httpClientProxyUrl(httpProxy)
2137
2246
  const auth: string | null = proxy !== null ? httpClientProxyAuth(proxy) : ""
2138
2247
  if (auth === null) {
2139
- fault.fail(HTTP_E_PROXY, "the proxy's user name or password is not valid percent-encoding")
2248
+ fault.fail(
2249
+ HTTP_E_PROXY,
2250
+ "the proxy's user name or password is not valid percent-encoding, or too long to encode"
2251
+ )
2140
2252
  return null
2141
2253
  }
2142
2254
  if (auth !== "" && !hasProxyAuth) {
@@ -2210,9 +2322,9 @@ const httpClientResolve = (
2210
2322
  fault: HttpClientFault
2211
2323
  ): i32 => {
2212
2324
  let lookupMs: i64 = toI64(0)
2213
- if (start >= toI64(0) && start < until && until <= HTTP_CLIENT_NEVER) {
2214
- // 0 <= start < until <= 2^62, so the unsigned difference cannot wrap.
2215
- lookupMs = toI64(toU64(until) - toU64(start)) / HTTP_CLIENT_NS_PER_MS
2325
+ if (start >= 0 && start < until && until <= HTTP_CLIENT_NEVER) {
2326
+ // 0 <= start < until, so the difference fits.
2327
+ lookupMs = (until - start) / HTTP_CLIENT_NS_PER_MS
2216
2328
  }
2217
2329
  if (lookupMs < toI64(1)) {
2218
2330
  lookupMs = toI64(1)
@@ -2409,8 +2521,11 @@ export class FetchPool {
2409
2521
  let count: i32 = 0
2410
2522
  for (let i: i32 = 0; i < FETCH_POOL_SLOTS && i < toI32(fds.length); i += 1) {
2411
2523
  if (fds[i] >= 0) {
2412
- // At most i, so the unsigned step cannot wrap.
2413
- count = toI32(toU32(count) + toU32(1))
2524
+ // At most `i`, below `FETCH_POOL_SLOTS`, so the step fits; the test
2525
+ // restates that for the bounds proof.
2526
+ if (count < i + 1) {
2527
+ count = count + 1
2528
+ }
2414
2529
  }
2415
2530
  }
2416
2531
  return count
@@ -2436,7 +2551,8 @@ export class FetchPool {
2436
2551
  if (fd < 0 || routes[i] !== route) {
2437
2552
  continue
2438
2553
  }
2439
- const quiet: boolean = netRead(fd, this.probe, HTTP_CLIENT_ZERO, 1) === HTTP_CLIENT_WOULD_BLOCK
2554
+ const probe: u8[] = this.probe
2555
+ const quiet: boolean = probe.length > 0 && netRead(fd, probe, 0, 1) === HTTP_CLIENT_WOULD_BLOCK
2440
2556
  // Re-said after the read, which the prover takes as a write to any array.
2441
2557
  if (quiet && i < toI32(fds.length) && i < toI32(routes.length)) {
2442
2558
  fds[i] = -1
@@ -2458,8 +2574,11 @@ export class FetchPool {
2458
2574
  if (fds[i] < 0) {
2459
2575
  free = free < 0 ? i : free
2460
2576
  } else if (routes[i] === route) {
2461
- // At most i, so the unsigned step cannot wrap.
2462
- same = toI32(toU32(same) + toU32(1))
2577
+ // At most `i`, below `FETCH_POOL_SLOTS`, so the step fits; the test
2578
+ // restates that for the bounds proof.
2579
+ if (same < i + 1) {
2580
+ same = same + 1
2581
+ }
2463
2582
  }
2464
2583
  }
2465
2584
  // free, when found, is an index of both arrays; said again for the prover.
@@ -2706,7 +2825,14 @@ const httpClientReadRawAsync = async (conn: HttpClientConn, want: i32): Promise<
2706
2825
  if (conn.overdue()) {
2707
2826
  return -1
2708
2827
  }
2709
- let n: i32 = netRead(conn.fd, conn.rbuf, HTTP_CLIENT_ZERO, want)
2828
+ // Every caller asks for at most `HTTP_CLIENT_READ_BYTES`, the buffer's
2829
+ // size; tested before each read, as `readRaw` does, since an `await`
2830
+ // takes what the prover knew of the length.
2831
+ if (want < 0 || want > toI32(conn.rbuf.length)) {
2832
+ conn.fault.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
2833
+ return -1
2834
+ }
2835
+ let n: i32 = netRead(conn.fd, conn.rbuf, 0, want)
2710
2836
  while (n === HTTP_CLIENT_WOULD_BLOCK) {
2711
2837
  const ready = await httpClientWaitPeerAsync(conn, false)
2712
2838
  if (!ready) {
@@ -2715,7 +2841,11 @@ const httpClientReadRawAsync = async (conn: HttpClientConn, want: i32): Promise<
2715
2841
  if (conn.overdue()) {
2716
2842
  return -1
2717
2843
  }
2718
- n = netRead(conn.fd, conn.rbuf, HTTP_CLIENT_ZERO, want)
2844
+ if (want < 0 || want > toI32(conn.rbuf.length)) {
2845
+ conn.fault.fail(HTTP_E_IO, "a read asked for more than the read buffer holds")
2846
+ return -1
2847
+ }
2848
+ n = netRead(conn.fd, conn.rbuf, 0, want)
2719
2849
  }
2720
2850
  if (n < 0) {
2721
2851
  conn.lost = true
@@ -2743,10 +2873,22 @@ const httpClientWriteRawAsync = async (
2743
2873
  if (conn.overdue()) {
2744
2874
  return false
2745
2875
  }
2746
- const n: i32 = netWrite(conn.fd, bytes, at, left)
2876
+ // The window's test again, after the calls that took what the prover
2877
+ // knew of it; it passes wherever the first one did.
2878
+ const end: i32 = httpClientWindowEnd(bytes, at, left)
2879
+ if (end < 0 || at > end || end > toI32(bytes.length)) {
2880
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
2881
+ return false
2882
+ }
2883
+ const n: i32 = netWrite(conn.fd, bytes, at, end - at)
2747
2884
  if (n > 0 && n <= left) {
2748
- // At most `off + len`, inside `bytes`, so the unsigned sum cannot wrap.
2749
- at = toI32(toU32(at) + toU32(n))
2885
+ // At most `off + len`, inside `bytes`, so the window always has an
2886
+ // end; the helper measures it so that the sum is proven to fit.
2887
+ at = httpClientWindowEnd(bytes, at, n)
2888
+ if (at < 0) {
2889
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
2890
+ return false
2891
+ }
2750
2892
  left -= n
2751
2893
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
2752
2894
  const ready = await httpClientWaitPeerAsync(conn, true)
@@ -2962,8 +3104,13 @@ const httpClientWriteAsync = async (conn: HttpClientConn, bytes: u8[]): Promise<
2962
3104
  while (left > 0 && at >= 0) {
2963
3105
  const n: i32 = tls.write(bytes, at, left)
2964
3106
  if (n > 0 && n <= left) {
2965
- // At most `bytes.length`, so the unsigned sum cannot wrap.
2966
- at = toI32(toU32(at) + toU32(n))
3107
+ // At most `bytes.length`, so the window always has an end; the helper
3108
+ // measures it so that the sum is proven to fit.
3109
+ at = httpClientWindowEnd(bytes, at, n)
3110
+ if (at < 0) {
3111
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
3112
+ return false
3113
+ }
2967
3114
  left -= n
2968
3115
  } else if (n > 0 || n !== TLS_RECORD_AGAIN || tls.outputEnd === tls.outputStart) {
2969
3116
  conn.lost = true
@@ -3084,8 +3231,12 @@ const httpClientLookupAsync = async (
3084
3231
  }
3085
3232
  client.tick(after)
3086
3233
  status = client.result(lookup, addresses, ttls)
3087
- // slice stays within 1 to HTTP_CLIENT_DNS_SLICE_MS, so doubling it cannot wrap.
3088
- const twice: i32 = toI32(toU32(slice) * toU32(2))
3234
+ // `slice` stays within 1 to `HTTP_CLIENT_DNS_SLICE_MS` (16), so doubling
3235
+ // it fits; the test restates that with the literal the bounds proof reads.
3236
+ let twice: i32 = HTTP_CLIENT_DNS_SLICE_MS
3237
+ if (slice >= 0 && slice <= 16) {
3238
+ twice = slice * 2
3239
+ }
3089
3240
  slice = twice < HTTP_CLIENT_DNS_SLICE_MS ? twice : HTTP_CLIENT_DNS_SLICE_MS
3090
3241
  }
3091
3242
  netClose(loop)
@@ -3120,8 +3271,16 @@ const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32
3120
3271
  const one: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
3121
3272
  let last: i32 = 0
3122
3273
  for (let i: i32 = 0; i < usable && i < DNS_RESOLVE_MAX_ADDRESSES; i += 1) {
3123
- // i < DNS_RESOLVE_MAX_ADDRESSES, so the unsigned product stays inside addresses.
3124
- copyBytes(one, 0, addresses, toI32(toU32(i) * toU32(DNS_ADDRESS_BYTES)), DNS_ADDRESS_BYTES)
3274
+ // `i` is below `DNS_RESOLVE_MAX_ADDRESSES` (16), so the product stays
3275
+ // inside `addresses`; the test restates that with the literal the bounds
3276
+ // proof reads.
3277
+ if (i > 15) {
3278
+ break
3279
+ }
3280
+ // An address the list does not hold all of is one past its end.
3281
+ if (!tryCopyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)) {
3282
+ break
3283
+ }
3125
3284
  const fd: i32 = tcpConnect(one)
3126
3285
  if (fd < 0) {
3127
3286
  last = fd
@@ -3295,7 +3454,11 @@ const httpClientBodyAsync = async (conn: HttpClientConn, res: FetchResponse, lim
3295
3454
  while (toI32(buf.length) < want) {
3296
3455
  buf.push(0)
3297
3456
  }
3298
- copyBytes(buf, len, conn.parser.body, conn.parser.bodyOff, take)
3457
+ // The parser lent take bytes at bodyOff; a span its body does not hold is its fault, not the peer's.
3458
+ if (!tryCopyBytes(buf, len, conn.parser.body, conn.parser.bodyOff, take)) {
3459
+ conn.fault.fail(HTTP_E_IO, "the body bytes lie outside the parser's buffer")
3460
+ return -1
3461
+ }
3299
3462
  len = want
3300
3463
  }
3301
3464
  event = await httpClientPumpAsync(conn)