@amritk/nish-x86_64-linux 0.42.0 → 0.45.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/bin/nish +0 -0
  2. package/docs/AI.md +287 -129
  3. package/package.json +1 -1
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +24 -0
  7. package/runtime/core.mjs +21 -0
  8. package/runtime/nish.d.ts +167 -24
  9. package/runtime/nish.mjs +7 -1
  10. package/runtime/runtime-cross.c +9 -0
  11. package/runtime/runtime-net.c +51 -13
  12. package/runtime/runtime-region.c +13 -0
  13. package/runtime/runtime-text.c +11 -0
  14. package/runtime/runtime-uring.c +83 -25
  15. package/runtime/runtime-wasm.c +3 -0
  16. package/runtime/runtime.c +329 -6
  17. package/runtime/sqlite/README.md +16 -3
  18. package/runtime/sqlite/nish-sqlite.c +940 -29
  19. package/scripts/build.sh +111 -29
  20. package/std/README.md +6 -6
  21. package/std/array.ts +10 -2
  22. package/std/bytes.ts +239 -26
  23. package/std/cli-args.ts +1072 -606
  24. package/std/cli-flags.ts +180 -98
  25. package/std/cli.ts +21 -13
  26. package/std/collections.ts +245 -127
  27. package/std/compress/checksum.ts +32 -37
  28. package/std/compress/deflate.ts +1102 -542
  29. package/std/compress/gzip.ts +290 -145
  30. package/std/compress/zlib.ts +57 -21
  31. package/std/crypto/aes.ts +153 -75
  32. package/std/crypto/base64url.ts +163 -39
  33. package/std/crypto/bcrypt-batch.ts +164 -90
  34. package/std/crypto/bcrypt.ts +185 -107
  35. package/std/crypto/chacha20poly1305.ts +84 -68
  36. package/std/crypto/ct.ts +9 -7
  37. package/std/crypto/ed25519.ts +64 -27
  38. package/std/crypto/hkdf.ts +105 -63
  39. package/std/crypto/hmac.ts +95 -89
  40. package/std/crypto/jwt.ts +248 -126
  41. package/std/crypto/p256.ts +758 -444
  42. package/std/crypto/p384.ts +20 -13
  43. package/std/crypto/pki-roots.ts +95 -38
  44. package/std/crypto/pki.ts +164 -60
  45. package/std/crypto/rsa.ts +266 -223
  46. package/std/crypto/scram.ts +141 -0
  47. package/std/crypto/sha1.ts +157 -57
  48. package/std/crypto/sha256.ts +502 -422
  49. package/std/crypto/sha512.ts +204 -135
  50. package/std/crypto/x509-random.ts +9 -3
  51. package/std/crypto/x509.ts +331 -171
  52. package/std/host/ring.ts +1 -1
  53. package/std/host/text.ts +1 -1
  54. package/std/json-equal.ts +35 -19
  55. package/std/json-format.ts +215 -108
  56. package/std/json-number.ts +126 -72
  57. package/std/json-pointer.ts +42 -13
  58. package/std/json-tokenizer.ts +498 -307
  59. package/std/json-typed.ts +115 -36
  60. package/std/json-value.ts +341 -152
  61. package/std/json.ts +300 -158
  62. package/std/net/buffer-pool.ts +17 -7
  63. package/std/net/dns-config.ts +427 -236
  64. package/std/net/dns-message.ts +114 -47
  65. package/std/net/dns-resolver.ts +736 -237
  66. package/std/net/dns.ts +205 -52
  67. package/std/net/fetch.ts +320 -184
  68. package/std/net/grpc.ts +948 -0
  69. package/std/net/handler.ts +1276 -218
  70. package/std/net/hpack.ts +395 -180
  71. package/std/net/http-compress.ts +502 -65
  72. package/std/net/http-fields.ts +85 -49
  73. package/std/net/http-server.ts +1764 -603
  74. package/std/net/http1-client-codec.ts +372 -161
  75. package/std/net/http1-codec.ts +665 -331
  76. package/std/net/http1-server.ts +881 -269
  77. package/std/net/http2-cleartext.ts +165 -95
  78. package/std/net/http2-frame.ts +232 -89
  79. package/std/net/http2-tls.ts +42 -12
  80. package/std/net/http2.ts +327 -101
  81. package/std/net/http3-frame.ts +68 -33
  82. package/std/net/http3-server.ts +672 -206
  83. package/std/net/http3.ts +669 -191
  84. package/std/net/oidc.ts +63 -24
  85. package/std/net/outbound.ts +207 -142
  86. package/std/net/postgres-async.ts +1223 -470
  87. package/std/net/postgres-carrier.ts +575 -372
  88. package/std/net/postgres.ts +1521 -632
  89. package/std/net/prefork.ts +254 -98
  90. package/std/net/protobuf.ts +1017 -0
  91. package/std/net/qpack.ts +143 -92
  92. package/std/net/quic-conn-ack.ts +104 -22
  93. package/std/net/quic-conn-cid.ts +66 -26
  94. package/std/net/quic-conn-params.ts +13 -7
  95. package/std/net/quic-datagram.ts +90 -27
  96. package/std/net/quic-frame.ts +324 -121
  97. package/std/net/quic-listener.ts +206 -91
  98. package/std/net/quic-packet.ts +289 -163
  99. package/std/net/quic-recovery.ts +279 -128
  100. package/std/net/quic-stream.ts +466 -167
  101. package/std/net/quic.ts +584 -258
  102. package/std/net/router.ts +450 -99
  103. package/std/net/serve.ts +661 -240
  104. package/std/net/static.ts +1458 -0
  105. package/std/net/tls/client.ts +140 -45
  106. package/std/net/tls/codec.ts +510 -260
  107. package/std/net/tls/record-client.ts +268 -76
  108. package/std/net/tls/record-server.ts +236 -79
  109. package/std/net/tls/record.ts +173 -47
  110. package/std/net/tls/schedule.ts +58 -39
  111. package/std/net/tls/ticket.ts +295 -103
  112. package/std/net/tls/verify.ts +1 -1
  113. package/std/net/tls-tcp.ts +218 -75
  114. package/std/net/tls.ts +260 -152
  115. package/std/net/upstream.ts +283 -86
  116. package/std/net/url.ts +277 -102
  117. package/std/net/websocket.ts +532 -209
  118. package/std/net/webtransport.ts +755 -436
  119. package/std/regex.ts +1659 -829
  120. package/std/sqlite.ts +325 -0
  121. package/std/sync/ring.ts +320 -156
  122. package/std/sync/shm.ts +53 -6
  123. package/std/text.ts +285 -135
  124. package/std/threads.ts +22 -2
package/std/net/fetch.ts CHANGED
@@ -184,6 +184,8 @@ const HTTP_CLIENT_MAX_HEADERS: i32 = 256
184
184
  const HTTP_CLIENT_DRAIN_BYTES: i32 = 65536
185
185
  /** The port a proxy URL without one is dialled on, as curl does. */
186
186
  const HTTP_CLIENT_PROXY_PORT: i32 = 1080
187
+ /** The bytes a lookup's answer array holds: every address it may give, `DNS_ADDRESS_BYTES` each. */
188
+ const HTTP_CLIENT_ADDRESS_ROOM: i32 = DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES
187
189
  /** The longest single poll wait, so a deadline far away never overflows an `i32` of milliseconds. */
188
190
  const HTTP_CLIENT_MAX_WAIT_MS: i64 = 3600000
189
191
  /** Nanoseconds in a millisecond. */
@@ -422,46 +424,25 @@ export class HttpBodyReader {
422
424
  * @returns The body, or null on an error or a body past `limit`.
423
425
  */
424
426
  readAll(limit: i32): u8[] | null {
425
- let size: i32 = 4096
426
- if (limit < 0) {
427
- size = 0
428
- } else if (limit < 4096) {
429
- size = limit
430
- }
431
- let out: u8[] = new Array<u8>(size)
432
- let len: i32 = 0
427
+ // Built by `push`, so no length is computed for `new Array`.
428
+ const out: u8[] = []
433
429
  const chunk: u8[] = new Array<u8>(HTTP_CLIENT_READ_BYTES)
434
- let n: i32 = this.read(chunk, HTTP_CLIENT_ZERO, HTTP_CLIENT_READ_BYTES)
430
+ let n: i32 = this.client.readBody(this.generation, chunk, HTTP_CLIENT_ZERO, HTTP_CLIENT_READ_BYTES)
435
431
  while (n > 0) {
436
- if (n > limit - len) {
432
+ // In i64, where two i32 values cannot wrap.
433
+ if (toI64(n) > toI64(limit) - toI64(out.length)) {
437
434
  this.client.refuseBody(limit)
438
435
  return null
439
436
  }
440
- if (len + n > toI32(out.length)) {
441
- let cap: i32 = toI32(out.length) * 2
442
- if (cap < len + n) {
443
- cap = len + n
444
- }
445
- if (cap > limit) {
446
- cap = limit
447
- }
448
- const grown: u8[] = new Array<u8>(cap)
449
- copyBytes(grown, 0, out, 0, len)
450
- out = grown
437
+ for (let i: i32 = 0; i < n && i < toI32(chunk.length); i += 1) {
438
+ out.push(chunk[i])
451
439
  }
452
- copyBytes(out, len, chunk, 0, n)
453
- len += n
454
- n = this.read(chunk, HTTP_CLIENT_ZERO, HTTP_CLIENT_READ_BYTES)
440
+ n = this.client.readBody(this.generation, chunk, HTTP_CLIENT_ZERO, HTTP_CLIENT_READ_BYTES)
455
441
  }
456
442
  if (n < 0) {
457
443
  return null
458
444
  }
459
- if (len === toI32(out.length)) {
460
- return out
461
- }
462
- const exact: u8[] = new Array<u8>(len)
463
- copyBytes(exact, 0, out, 0, len)
464
- return exact
445
+ return out
465
446
  }
466
447
  }
467
448
 
@@ -566,7 +547,8 @@ export class FetchSyncClient {
566
547
  // An unread body is still on the wire ahead of the next response.
567
548
  this.drop()
568
549
  }
569
- this.generation += 1
550
+ // Only compared for equality, so wrapping past 2^32 responses is harmless.
551
+ this.generation = toI32(toU32(this.generation) + toU32(1))
570
552
  this.deadline = httpClientDeadline(this.options)
571
553
  const fault: HttpClientFault = new HttpClientFault()
572
554
  let url: Url | null = httpClientTarget(req.url, this.options, fault)
@@ -824,13 +806,14 @@ export class FetchSyncClient {
824
806
  res.status = this.parser.status
825
807
  res.reason = this.parser.reason
826
808
  res.url = httpClientUrlText(url)
827
- for (
828
- let i: i32 = 0;
829
- i < toI32(this.parser.names.length) && i < toI32(this.parser.values.length);
830
- i += 1
831
- ) {
832
- res.names.push(this.parser.names[i])
833
- res.values.push(this.parser.values[i])
809
+ const heads: string[] = this.parser.names
810
+ const vals: string[] = this.parser.values
811
+ for (let i: i32 = 0; i < toI32(heads.length) && i < toI32(vals.length); i += 1) {
812
+ // Both read before either push, which the prover takes as a write to any array.
813
+ const name: string = heads[i]
814
+ const value: string = vals[i]
815
+ res.names.push(name)
816
+ res.values.push(value)
834
817
  }
835
818
  this.bodyOpen = true
836
819
  this.bodyError = HTTP_E_NONE
@@ -927,7 +910,7 @@ export class FetchSyncClient {
927
910
  return n
928
911
  }
929
912
  this.lost = true
930
- this.fail(HTTP_E_IO, `reading from the server failed (errno ${-n})`)
913
+ this.fail(HTTP_E_IO, `reading from the server failed (errno ${-toI64(n)})`)
931
914
  return -1
932
915
  }
933
916
 
@@ -955,13 +938,20 @@ export class FetchSyncClient {
955
938
 
956
939
  /** Sends everything `conn` has sealed, waiting as `writeRaw` does; false with the error set. */
957
940
  private flushTls(conn: TlsRecordClient): boolean {
958
- while (conn.outputEnd > conn.outputStart) {
959
- const len: i32 = conn.outputEnd - conn.outputStart
941
+ let len: i32 = httpClientTlsPending(conn)
942
+ while (len > 0) {
960
943
  if (!this.writeRaw(conn.output, conn.outputStart, len)) {
961
944
  conn.abort()
962
945
  return false
963
946
  }
964
947
  conn.consume(len)
948
+ len = httpClientTlsPending(conn)
949
+ }
950
+ if (len < 0) {
951
+ conn.abort()
952
+ this.lost = true
953
+ this.fail(HTTP_E_TLS, "the TLS connection's sealed output lies outside its buffer")
954
+ return false
965
955
  }
966
956
  return true
967
957
  }
@@ -979,13 +969,20 @@ export class FetchSyncClient {
979
969
  if (conn === null) {
980
970
  return this.writeRaw(bytes, off, len)
981
971
  }
972
+ if (off < 0 || len < 0 || len > toI32(bytes.length) - off) {
973
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
974
+ return false
975
+ }
982
976
  let at: i32 = off
983
- const end: i32 = off + len
984
- while (at < end) {
985
- const n: i32 = conn.write(bytes, at, end - at)
986
- if (n > 0) {
987
- at += n
988
- } else if (n !== TLS_RECORD_AGAIN || conn.outputEnd === conn.outputStart) {
977
+ let left: i32 = len
978
+ // `at >= 0` is the loop's own, since the prover does not carry it round.
979
+ while (left > 0 && at >= 0) {
980
+ const n: i32 = conn.write(bytes, at, left)
981
+ 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))
984
+ left -= n
985
+ } else if (n > 0 || n !== TLS_RECORD_AGAIN || conn.outputEnd === conn.outputStart) {
989
986
  this.lost = true
990
987
  this.failTls(conn)
991
988
  return false
@@ -999,15 +996,22 @@ export class FetchSyncClient {
999
996
 
1000
997
  /** Writes `bytes[off, off + len)` to `fd`, waiting up to `readTimeoutMs` each time it is full. */
1001
998
  private writeRaw(bytes: u8[], off: i32, len: i32): boolean {
999
+ if (off < 0 || len < 0 || len > toI32(bytes.length) - off) {
1000
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1001
+ return false
1002
+ }
1002
1003
  let at: i32 = off
1003
- const end: i32 = off + len
1004
- while (at < end) {
1004
+ let left: i32 = len
1005
+ // `at >= 0` is the loop's own, since the prover does not carry it round.
1006
+ while (left > 0 && at >= 0) {
1005
1007
  if (this.overdue()) {
1006
1008
  return false
1007
1009
  }
1008
- const n: i32 = netWrite(this.fd, bytes, at, end - at)
1009
- if (n > 0) {
1010
- at += n
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
1011
1015
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
1012
1016
  const waited: i32 = this.wait(this.fd, HTTP_CLIENT_WRITABLE, this.readDeadline(), HTTP_E_READ_TIMEOUT)
1013
1017
  if (waited !== HTTP_E_NONE) {
@@ -1016,7 +1020,7 @@ export class FetchSyncClient {
1016
1020
  }
1017
1021
  } else {
1018
1022
  this.lost = true
1019
- this.fail(HTTP_E_IO, `writing to the server failed (errno ${-n})`)
1023
+ this.fail(HTTP_E_IO, `writing to the server failed (errno ${-toI64(n)})`)
1020
1024
  return false
1021
1025
  }
1022
1026
  }
@@ -1030,7 +1034,7 @@ export class FetchSyncClient {
1030
1034
  private dial(host: string, port: i32): i32 {
1031
1035
  const start: i64 = monotonicNanos()
1032
1036
  const budget: i32 = this.options.connectTimeoutMs
1033
- let until: i64 = budget > 0 ? start + toI64(budget) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
1037
+ let until: i64 = budget > 0 ? httpClientAfter(start, budget) : HTTP_CLIENT_NEVER
1034
1038
  let code: i32 = HTTP_E_CONNECT_TIMEOUT
1035
1039
  if (this.deadline < HTTP_CLIENT_NEVER && this.deadline <= until) {
1036
1040
  until = this.deadline
@@ -1040,7 +1044,7 @@ export class FetchSyncClient {
1040
1044
  this.fail(code, `no time was left to connect to ${host}`)
1041
1045
  return -1
1042
1046
  }
1043
- const addresses: u8[] = new Array<u8>(DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES)
1047
+ const addresses: u8[] = new Array<u8>(HTTP_CLIENT_ADDRESS_ROOM)
1044
1048
  const fault: HttpClientFault = new HttpClientFault()
1045
1049
  const usable: i32 = httpClientResolve(host, port, this.options, start, until, code, addresses, fault)
1046
1050
  if (usable < 0) {
@@ -1050,7 +1054,8 @@ export class FetchSyncClient {
1050
1054
  const one: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
1051
1055
  let last: i32 = 0
1052
1056
  for (let i: i32 = 0; i < usable && i < DNS_RESOLVE_MAX_ADDRESSES; i += 1) {
1053
- copyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)
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)
1054
1059
  const fd: i32 = tcpConnect(one)
1055
1060
  if (fd < 0) {
1056
1061
  last = fd
@@ -1071,7 +1076,7 @@ export class FetchSyncClient {
1071
1076
  netClose(fd)
1072
1077
  last = result
1073
1078
  }
1074
- this.fail(HTTP_E_REFUSED, `could not connect to ${host} port ${port} (errno ${-last})`)
1079
+ this.fail(HTTP_E_REFUSED, `could not connect to ${host} port ${port} (errno ${-toI64(last)})`)
1075
1080
  return -1
1076
1081
  }
1077
1082
 
@@ -1093,10 +1098,7 @@ export class FetchSyncClient {
1093
1098
  }
1094
1099
  let now: i64 = monotonicNanos()
1095
1100
  while (now < end) {
1096
- let ms: i64 = (end - now + HTTP_CLIENT_NS_PER_MS - toI64(1)) / HTTP_CLIENT_NS_PER_MS
1097
- if (ms > HTTP_CLIENT_MAX_WAIT_MS) {
1098
- ms = HTTP_CLIENT_MAX_WAIT_MS
1099
- }
1101
+ const ms: i64 = httpClientWaitMs(end, now)
1100
1102
  const count: i32 = pollWait(this.loop, this.ready, toI32(ms))
1101
1103
  if (count > 0) {
1102
1104
  return HTTP_E_NONE
@@ -1168,7 +1170,7 @@ export class FetchSyncClient {
1168
1170
  /** When the wait for the server's next byte ends: `readTimeoutMs` from now. */
1169
1171
  private readDeadline(): i64 {
1170
1172
  const ms: i32 = this.options.readTimeoutMs
1171
- return ms > 0 ? monotonicNanos() + toI64(ms) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
1173
+ return ms > 0 ? httpClientAfter(monotonicNanos(), ms) : HTTP_CLIENT_NEVER
1172
1174
  }
1173
1175
 
1174
1176
  /** Closes the connection, if any, and forgets the open body. */
@@ -1225,14 +1227,14 @@ export class FetchSyncClient {
1225
1227
  return -HTTP_E_IO
1226
1228
  }
1227
1229
  if (!this.bodyOpen) {
1228
- return -this.bodyError
1230
+ return httpClientNegate(this.bodyError)
1229
1231
  }
1230
1232
  while (this.pendLen === 0) {
1231
1233
  const event: i32 = this.pump()
1232
1234
  if (event < 0) {
1233
1235
  this.release()
1234
1236
  this.bodyError = this.error
1235
- return -this.error
1237
+ return httpClientNegate(this.error)
1236
1238
  }
1237
1239
  if (event === HTTP1_END) {
1238
1240
  this.bodyOpen = false
@@ -1250,8 +1252,10 @@ export class FetchSyncClient {
1250
1252
  }
1251
1253
  const take: i32 = n < this.pendLen ? n : this.pendLen
1252
1254
  copyBytes(buf, off, this.parser.body, this.pendOff, take)
1253
- this.pendOff += take
1254
- this.pendLen -= 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))
1255
1259
  return take
1256
1260
  }
1257
1261
 
@@ -1393,13 +1397,22 @@ export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): bool
1393
1397
  const literal: boolean = tlsClientIsIpLiteral(name)
1394
1398
  let start: i32 = 0
1395
1399
  const n: i32 = toI32(noProxy.length)
1396
- while (start <= n) {
1400
+ let more: boolean = true
1401
+ while (more && start <= n) {
1397
1402
  let end: i32 = toI32(noProxy.indexOf(",", start))
1398
- if (end < 0) {
1403
+ if (end < 0 || end > n) {
1399
1404
  end = n
1400
1405
  }
1401
- const entry: string = httpClientLower(httpClientTrim(noProxy.slice(start, end)))
1402
- start = end + 1
1406
+ // Never true (`indexOf` answers at or after `start`); it bounds `substring`.
1407
+ if (start < 0 || start > end || start > toI32(noProxy.length) || end > toI32(noProxy.length)) {
1408
+ return false
1409
+ }
1410
+ const entry: string = httpClientLower(httpClientTrim(noProxy.substring(start, end)))
1411
+ if (end < n) {
1412
+ start = end + 1
1413
+ } else {
1414
+ more = false
1415
+ }
1403
1416
  if (entry === "*") {
1404
1417
  return true
1405
1418
  }
@@ -1408,11 +1421,11 @@ export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): bool
1408
1421
  const colon: i32 = toI32(entry.indexOf(":"))
1409
1422
  if (entry.startsWith("[")) {
1410
1423
  const close: i32 = toI32(entry.indexOf("]"))
1411
- if (close < 0) {
1424
+ if (close < 1 || close > toI32(entry.length)) {
1412
1425
  continue
1413
1426
  }
1414
- entryHost = entry.slice(1, close)
1415
- if (close + 1 < toI32(entry.length)) {
1427
+ entryHost = entry.substring(1, close)
1428
+ if (close < toI32(entry.length) - 1) {
1416
1429
  if (entry.charCodeAt(close + 1) !== 58) {
1417
1430
  continue
1418
1431
  }
@@ -1421,8 +1434,12 @@ export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): bool
1421
1434
  continue
1422
1435
  }
1423
1436
  }
1424
- } else if (colon >= 0 && entry.indexOf(":", colon + 1) < 0) {
1425
- entryHost = entry.slice(0, colon)
1437
+ } else if (
1438
+ colon >= 0 &&
1439
+ colon < toI32(entry.length) /* indexOf's own bound, so colon + 1 fits */ &&
1440
+ entry.indexOf(":", colon + 1) < 0
1441
+ ) {
1442
+ entryHost = entry.substring(0, colon)
1426
1443
  entryPort = httpClientPort(entry.substring(colon + 1, toI32(entry.length)))
1427
1444
  if (entryPort < 0) {
1428
1445
  continue
@@ -1444,6 +1461,60 @@ export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): bool
1444
1461
 
1445
1462
  // --- helpers -----------------------------------------------------------------
1446
1463
 
1464
+ /**
1465
+ * `ms` milliseconds after `start` on the monotonic clock, or
1466
+ * `HTTP_CLIENT_NEVER` for a `start` outside [0, 2^62] or a negative `ms`,
1467
+ * which the clock and the options never give.
1468
+ */
1469
+ const httpClientAfter = (start: i64, ms: i32): i64 => {
1470
+ if (start < toI64(0) || start > HTTP_CLIENT_NEVER || ms < 0) {
1471
+ return HTTP_CLIENT_NEVER
1472
+ }
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)))
1475
+ }
1476
+
1477
+ /**
1478
+ * The whole milliseconds, rounded up, from `now` until `end`, at most
1479
+ * `HTTP_CLIENT_MAX_WAIT_MS`: 0 when `end` has passed.
1480
+ */
1481
+ const httpClientWaitMs = (end: i64, now: i64): i64 => {
1482
+ if (end <= now) {
1483
+ return toI64(0)
1484
+ }
1485
+ if (now < toI64(0) || end > HTTP_CLIENT_NEVER) {
1486
+ return HTTP_CLIENT_MAX_WAIT_MS
1487
+ }
1488
+ // 0 <= now < end <= 2^62, so the unsigned difference cannot wrap.
1489
+ const left: i64 = toI64(toU64(end) - toU64(now))
1490
+ 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))
1494
+ }
1495
+ return ms > HTTP_CLIENT_MAX_WAIT_MS ? HTTP_CLIENT_MAX_WAIT_MS : ms
1496
+ }
1497
+
1498
+ /**
1499
+ * The negated `HTTP_E_*` code a read answers for failure `code`: 0 for
1500
+ * `HTTP_E_NONE`. Codes are never negative; one that were would be `HTTP_E_IO`.
1501
+ */
1502
+ const httpClientNegate = (code: i32): i32 => (code >= 0 ? -code : -HTTP_E_IO)
1503
+
1504
+ /**
1505
+ * How many bytes `conn` has sealed and not yet sent: 0 for none, or -1 when
1506
+ * `[outputStart, outputEnd)` does not lie inside `output`, which a working
1507
+ * TLS connection never lets happen.
1508
+ */
1509
+ const httpClientTlsPending = (conn: TlsRecordClient): i32 => {
1510
+ const start: i32 = conn.outputStart
1511
+ const end: i32 = conn.outputEnd
1512
+ if (start < 0 || end < 0 || end > toI32(conn.output.length)) {
1513
+ return -1
1514
+ }
1515
+ return end > start ? end - start : 0
1516
+ }
1517
+
1447
1518
  /** A parser with the options' limits. */
1448
1519
  const httpClientParser = (options: HttpOptions): Http1ResponseParser =>
1449
1520
  new Http1ResponseParser(
@@ -1503,19 +1574,26 @@ const httpClientIdempotent = (method: string): boolean =>
1503
1574
 
1504
1575
  /** Removes every field named `lower`, in any case. */
1505
1576
  const httpClientDropField = (names: string[], values: string[], lower: string): void => {
1506
- let i: i32 = 0
1507
- while (i < toI32(names.length) && i < toI32(values.length)) {
1508
- if (httpClientNameIs(names[i], lower)) {
1509
- for (let k: i32 = i; k + 1 < toI32(names.length) && k + 1 < toI32(values.length); k += 1) {
1510
- names[k] = names[k + 1]
1511
- values[k] = values[k + 1]
1577
+ // Every kept field moves down to `keep`, in order, then the tail goes.
1578
+ let keep: i32 = 0
1579
+ for (let i: i32 = 0; i < toI32(names.length) && i < toI32(values.length); i += 1) {
1580
+ const name: string = names[i]
1581
+ const value: string = values[i]
1582
+ if (!httpClientNameIs(name, lower)) {
1583
+ if (keep < i && keep >= 0) {
1584
+ names[keep] = name
1585
+ values[keep] = value
1512
1586
  }
1513
- names.pop()
1514
- values.pop()
1515
- } else {
1516
- i += 1
1587
+ // keep <= i, so the unsigned step cannot wrap.
1588
+ keep = toI32(toU32(keep) + toU32(1))
1517
1589
  }
1518
1590
  }
1591
+ while (toI32(names.length) > keep && toI32(names.length) > 0) {
1592
+ names.pop()
1593
+ }
1594
+ while (toI32(values.length) > keep && toI32(values.length) > 0) {
1595
+ values.pop()
1596
+ }
1519
1597
  }
1520
1598
 
1521
1599
  /** Whether the field name `name` is `lower`, a lower-case name, in any case; it allocates nothing. */
@@ -1547,22 +1625,29 @@ const httpClientLower = (s: string): string => {
1547
1625
  if (!upper) {
1548
1626
  return s
1549
1627
  }
1550
- const out: u8[] = new Array<u8>(n)
1551
- for (let i: i32 = 0; i < toI32(out.length) && i < toI32(s.length); i += 1) {
1628
+ // A byte at a time: `String.fromBytes` keeps a range check no proof removes.
1629
+ const parts: string[] = []
1630
+ for (let i: i32 = 0; i < toI32(s.length); i += 1) {
1552
1631
  const c: i32 = toI32(s.charCodeAt(i))
1553
- out[i] = c >= 65 && c <= 90 ? toU8(c + 32) : toU8(c)
1632
+ parts.push(String.fromCharCode(c >= 65 && c <= 90 ? c + 32 : c))
1554
1633
  }
1555
- return String.fromBytes(out, 0, n)
1634
+ return parts.join("")
1556
1635
  }
1557
1636
 
1558
1637
  /** `s` without spaces and tabs at either end. */
1559
1638
  const httpClientTrim = (s: string): string => {
1560
1639
  let a: i32 = 0
1561
1640
  let b: i32 = toI32(s.length)
1562
- while (a < b && (s.charCodeAt(a) === 32 || s.charCodeAt(a) === 9)) {
1641
+ // The bounds against s.length restate what a <= b <= s.length already holds.
1642
+ while (a < b && a >= 0 && a < toI32(s.length) && (s.charCodeAt(a) === 32 || s.charCodeAt(a) === 9)) {
1563
1643
  a += 1
1564
1644
  }
1565
- while (b > a && (s.charCodeAt(b - 1) === 32 || s.charCodeAt(b - 1) === 9)) {
1645
+ while (
1646
+ b > a &&
1647
+ b > 0 &&
1648
+ b <= toI32(s.length) &&
1649
+ (s.charCodeAt(b - 1) === 32 || s.charCodeAt(b - 1) === 9)
1650
+ ) {
1566
1651
  b -= 1
1567
1652
  }
1568
1653
  return s.substring(a, b)
@@ -1575,7 +1660,7 @@ const httpClientTrimDots = (s: string): string => {
1575
1660
  if (a < b && s.charCodeAt(a) === 46) {
1576
1661
  a += 1
1577
1662
  }
1578
- if (b > a && s.charCodeAt(b - 1) === 46) {
1663
+ if (b > a && b > 0 && b <= toI32(s.length) && s.charCodeAt(b - 1) === 46) {
1579
1664
  b -= 1
1580
1665
  }
1581
1666
  return s.substring(a, b)
@@ -1590,7 +1675,8 @@ const httpClientPort = (s: string): i32 => {
1590
1675
  let v: i32 = 0
1591
1676
  for (let i: i32 = 0; i < n; i += 1) {
1592
1677
  const c: i32 = toI32(s.charCodeAt(i))
1593
- if (c < 48 || c > 57) {
1678
+ // Past 6553 the port can only end above 65535; the test keeps v * 10 in range.
1679
+ if (c < 48 || c > 57 || v < 0 || v > 6553) {
1594
1680
  return -1
1595
1681
  }
1596
1682
  v = v * 10 + c - 48
@@ -1691,7 +1777,7 @@ const httpClientEnvProxies = (options: HttpOptions): string[] => {
1691
1777
  /** When a fetch starting now must be done by, on the monotonic clock; `HTTP_CLIENT_NEVER` with no total timeout. */
1692
1778
  const httpClientDeadline = (options: HttpOptions): i64 => {
1693
1779
  const total: i32 = httpClientTotalMs(options)
1694
- return total > 0 ? monotonicNanos() + toI64(total) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
1780
+ return total > 0 ? httpClientAfter(monotonicNanos(), total) : HTTP_CLIENT_NEVER
1695
1781
  }
1696
1782
 
1697
1783
  /** A failure a shared step found: an `HTTP_E_*` code and its sentence, before the code's name is put in front. */
@@ -1803,8 +1889,9 @@ const httpClientTlsVerify = (
1803
1889
  */
1804
1890
  const httpClientTlsClose = (conn: TlsRecordClient, fd: i32): void => {
1805
1891
  conn.close()
1806
- if (fd >= 0 && conn.outputEnd > conn.outputStart) {
1807
- netWrite(fd, conn.output, conn.outputStart, conn.outputEnd - conn.outputStart)
1892
+ const len: i32 = httpClientTlsPending(conn)
1893
+ if (fd >= 0 && len > 0) {
1894
+ netWrite(fd, conn.output, conn.outputStart, len)
1808
1895
  }
1809
1896
  conn.wipe()
1810
1897
  }
@@ -1892,7 +1979,9 @@ const httpClientRedirect = (
1892
1979
  ): Url | null => {
1893
1980
  const next: Url | null = urlResolve(url, location)
1894
1981
  const maxRedirects: i32 = httpClientMaxRedirects(options)
1895
- if (hop.hops >= maxRedirects) {
1982
+ // A local, so `hops < maxRedirects` still holds where it is stepped.
1983
+ const hops: i32 = hop.hops
1984
+ if (hops >= maxRedirects) {
1896
1985
  fault.fail(
1897
1986
  HTTP_E_TOO_MANY_REDIRECTS,
1898
1987
  `more than ${maxRedirects} redirects, the last to ${httpClientRedact(next)}`
@@ -1936,7 +2025,7 @@ const httpClientRedirect = (
1936
2025
  hop.body = null
1937
2026
  httpClientDropField(hop.names, hop.values, "content-type")
1938
2027
  }
1939
- hop.hops += 1
2028
+ hop.hops = hops + 1
1940
2029
  return next
1941
2030
  }
1942
2031
 
@@ -2120,7 +2209,11 @@ const httpClientResolve = (
2120
2209
  addresses: u8[],
2121
2210
  fault: HttpClientFault
2122
2211
  ): i32 => {
2123
- let lookupMs: i64 = (until - start) / HTTP_CLIENT_NS_PER_MS
2212
+ 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
2216
+ }
2124
2217
  if (lookupMs < toI64(1)) {
2125
2218
  lookupMs = toI64(1)
2126
2219
  } else if (lookupMs > toI64(2147483647)) {
@@ -2260,7 +2353,12 @@ export class FetchResponse {
2260
2353
  * @returns The body's text.
2261
2354
  */
2262
2355
  text(): string {
2263
- return String.fromBytes(this.body, HTTP_CLIENT_ZERO, toI32(this.body.length))
2356
+ // A byte at a time: `String.fromBytes` keeps a range check no proof removes.
2357
+ const parts: string[] = []
2358
+ for (const b of this.body) {
2359
+ parts.push(String.fromCharCode(b))
2360
+ }
2361
+ return parts.join("")
2264
2362
  }
2265
2363
 
2266
2364
  /**
@@ -2307,10 +2405,12 @@ export class FetchPool {
2307
2405
  * @returns The count, over every route.
2308
2406
  */
2309
2407
  idle(): i32 {
2408
+ const fds: i32[] = this.fds
2310
2409
  let count: i32 = 0
2311
- for (let i: i32 = 0; i < FETCH_POOL_SLOTS; i += 1) {
2312
- if (this.fds[i] >= 0) {
2313
- count += 1
2410
+ for (let i: i32 = 0; i < FETCH_POOL_SLOTS && i < toI32(fds.length); i += 1) {
2411
+ if (fds[i] >= 0) {
2412
+ // At most i, so the unsigned step cannot wrap.
2413
+ count = toI32(toU32(count) + toU32(1))
2314
2414
  }
2315
2415
  }
2316
2416
  return count
@@ -2329,14 +2429,18 @@ export class FetchPool {
2329
2429
  * @internal
2330
2430
  */
2331
2431
  take(route: string): i32 {
2332
- for (let i: i32 = 0; i < FETCH_POOL_SLOTS; i += 1) {
2333
- if (this.fds[i] < 0 || this.routes[i] !== route) {
2432
+ const fds: i32[] = this.fds
2433
+ const routes: string[] = this.routes
2434
+ for (let i: i32 = 0; i < FETCH_POOL_SLOTS && i < toI32(fds.length) && i < toI32(routes.length); i += 1) {
2435
+ const fd: i32 = fds[i]
2436
+ if (fd < 0 || routes[i] !== route) {
2334
2437
  continue
2335
2438
  }
2336
- const fd: i32 = this.fds[i]
2337
- if (netRead(fd, this.probe, HTTP_CLIENT_ZERO, 1) === HTTP_CLIENT_WOULD_BLOCK) {
2338
- this.fds[i] = -1
2339
- this.routes[i] = ""
2439
+ const quiet: boolean = netRead(fd, this.probe, HTTP_CLIENT_ZERO, 1) === HTTP_CLIENT_WOULD_BLOCK
2440
+ // Re-said after the read, which the prover takes as a write to any array.
2441
+ if (quiet && i < toI32(fds.length) && i < toI32(routes.length)) {
2442
+ fds[i] = -1
2443
+ routes[i] = ""
2340
2444
  return fd
2341
2445
  }
2342
2446
  this.drop(i)
@@ -2346,29 +2450,44 @@ export class FetchPool {
2346
2450
 
2347
2451
  /** Keeps `fd` for `route` when there is room, else closes it. @internal */
2348
2452
  put(route: string, fd: i32): void {
2453
+ const fds: i32[] = this.fds
2454
+ const routes: string[] = this.routes
2349
2455
  let same: i32 = 0
2350
2456
  let free: i32 = -1
2351
- for (let i: i32 = 0; i < FETCH_POOL_SLOTS; i += 1) {
2352
- if (this.fds[i] < 0) {
2457
+ for (let i: i32 = 0; i < FETCH_POOL_SLOTS && i < toI32(fds.length) && i < toI32(routes.length); i += 1) {
2458
+ if (fds[i] < 0) {
2353
2459
  free = free < 0 ? i : free
2354
- } else if (this.routes[i] === route) {
2355
- same += 1
2460
+ } else if (routes[i] === route) {
2461
+ // At most i, so the unsigned step cannot wrap.
2462
+ same = toI32(toU32(same) + toU32(1))
2356
2463
  }
2357
2464
  }
2358
- if (same >= this.maxIdlePerOrigin || free < 0) {
2465
+ // free, when found, is an index of both arrays; said again for the prover.
2466
+ if (
2467
+ same >= this.maxIdlePerOrigin ||
2468
+ free < 0 ||
2469
+ free >= toI32(fds.length) ||
2470
+ free >= toI32(routes.length)
2471
+ ) {
2359
2472
  netClose(fd)
2360
2473
  return
2361
2474
  }
2362
- this.fds[free] = fd
2363
- this.routes[free] = route
2475
+ fds[free] = fd
2476
+ routes[free] = route
2364
2477
  }
2365
2478
 
2366
2479
  private drop(i: i32): void {
2367
- if (this.fds[i] >= 0) {
2368
- netClose(this.fds[i])
2480
+ const fds: i32[] = this.fds
2481
+ const routes: string[] = this.routes
2482
+ // Every caller passes a slot index; one past either array names no connection.
2483
+ if (i < 0 || i >= toI32(fds.length) || i >= toI32(routes.length)) {
2484
+ return
2369
2485
  }
2370
- this.fds[i] = -1
2371
- this.routes[i] = ""
2486
+ if (fds[i] >= 0) {
2487
+ netClose(fds[i])
2488
+ }
2489
+ fds[i] = -1
2490
+ routes[i] = ""
2372
2491
  }
2373
2492
  }
2374
2493
 
@@ -2435,16 +2554,13 @@ const httpClientWaitAsync = async (fd: i32, write: boolean, until: i64, code: i3
2435
2554
  ready = await readable(fd)
2436
2555
  }
2437
2556
  } else {
2438
- const left: i64 = until - monotonicNanos()
2439
- if (left <= 0) {
2557
+ const ms: i64 = httpClientWaitMs(until, monotonicNanos())
2558
+ if (ms <= toI64(0)) {
2440
2559
  return code
2441
2560
  }
2442
- let ms: i64 = (left + HTTP_CLIENT_NS_PER_MS - 1) / HTTP_CLIENT_NS_PER_MS
2443
- if (ms > HTTP_CLIENT_MAX_WAIT_MS) {
2444
- ms = HTTP_CLIENT_MAX_WAIT_MS
2445
- }
2446
2561
  bounded = true
2447
- sliceEnd = monotonicNanos() + ms * HTTP_CLIENT_NS_PER_MS
2562
+ // ms is at most HTTP_CLIENT_MAX_WAIT_MS, so it fits an i32.
2563
+ sliceEnd = httpClientAfter(monotonicNanos(), toI32(ms))
2448
2564
  if (write) {
2449
2565
  ready = await writableWithin(fd, toI32(ms))
2450
2566
  } else {
@@ -2563,7 +2679,7 @@ class HttpClientConn {
2563
2679
  */
2564
2680
  const httpClientWaitPeerAsync = async (conn: HttpClientConn, write: boolean): Promise<boolean> => {
2565
2681
  const ms: i32 = conn.options.readTimeoutMs
2566
- let until: i64 = ms > 0 ? monotonicNanos() + toI64(ms) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
2682
+ let until: i64 = ms > 0 ? httpClientAfter(monotonicNanos(), ms) : HTTP_CLIENT_NEVER
2567
2683
  let code: i32 = HTTP_E_READ_TIMEOUT
2568
2684
  if (conn.deadline < until) {
2569
2685
  until = conn.deadline
@@ -2603,7 +2719,7 @@ const httpClientReadRawAsync = async (conn: HttpClientConn, want: i32): Promise<
2603
2719
  }
2604
2720
  if (n < 0) {
2605
2721
  conn.lost = true
2606
- conn.fault.fail(HTTP_E_IO, `reading from the server failed (errno ${-n})`)
2722
+ conn.fault.fail(HTTP_E_IO, `reading from the server failed (errno ${-toI64(n)})`)
2607
2723
  return -1
2608
2724
  }
2609
2725
  return n
@@ -2616,15 +2732,22 @@ const httpClientWriteRawAsync = async (
2616
2732
  off: i32,
2617
2733
  len: i32
2618
2734
  ): Promise<boolean> => {
2735
+ if (off < 0 || len < 0 || len > toI32(bytes.length) - off) {
2736
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
2737
+ return false
2738
+ }
2619
2739
  let at: i32 = off
2620
- const end: i32 = off + len
2621
- while (at < end) {
2740
+ let left: i32 = len
2741
+ // `at >= 0` is the loop's own, since the prover does not carry it round.
2742
+ while (left > 0 && at >= 0) {
2622
2743
  if (conn.overdue()) {
2623
2744
  return false
2624
2745
  }
2625
- const n: i32 = netWrite(conn.fd, bytes, at, end - at)
2626
- if (n > 0) {
2627
- at += n
2746
+ const n: i32 = netWrite(conn.fd, bytes, at, left)
2747
+ 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))
2750
+ left -= n
2628
2751
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
2629
2752
  const ready = await httpClientWaitPeerAsync(conn, true)
2630
2753
  if (!ready) {
@@ -2632,7 +2755,7 @@ const httpClientWriteRawAsync = async (
2632
2755
  }
2633
2756
  } else {
2634
2757
  conn.lost = true
2635
- conn.fault.fail(HTTP_E_IO, `writing to the server failed (errno ${-n})`)
2758
+ conn.fault.fail(HTTP_E_IO, `writing to the server failed (errno ${-toI64(n)})`)
2636
2759
  return false
2637
2760
  }
2638
2761
  }
@@ -2663,14 +2786,21 @@ const httpClientReceiveTlsAsync = async (conn: HttpClientConn, tls: TlsRecordCli
2663
2786
 
2664
2787
  /** Sends everything `tls` has sealed, parking the task while the socket is full; false with the fault set. */
2665
2788
  const httpClientFlushTlsAsync = async (conn: HttpClientConn, tls: TlsRecordClient): Promise<boolean> => {
2666
- while (tls.outputEnd > tls.outputStart) {
2667
- const len: i32 = tls.outputEnd - tls.outputStart
2789
+ let len: i32 = httpClientTlsPending(tls)
2790
+ while (len > 0) {
2668
2791
  const wrote = await httpClientWriteRawAsync(conn, tls.output, tls.outputStart, len)
2669
2792
  if (!wrote) {
2670
2793
  tls.abort()
2671
2794
  return false
2672
2795
  }
2673
2796
  tls.consume(len)
2797
+ len = httpClientTlsPending(tls)
2798
+ }
2799
+ if (len < 0) {
2800
+ tls.abort()
2801
+ conn.lost = true
2802
+ conn.fault.fail(HTTP_E_TLS, "the TLS connection's sealed output lies outside its buffer")
2803
+ return false
2674
2804
  }
2675
2805
  return true
2676
2806
  }
@@ -2827,12 +2957,15 @@ const httpClientWriteAsync = async (conn: HttpClientConn, bytes: u8[]): Promise<
2827
2957
  return wrote
2828
2958
  }
2829
2959
  let at: i32 = 0
2830
- const end: i32 = toI32(bytes.length)
2831
- while (at < end) {
2832
- const n: i32 = tls.write(bytes, at, end - at)
2833
- if (n > 0) {
2834
- at += n
2835
- } else if (n !== TLS_RECORD_AGAIN || tls.outputEnd === tls.outputStart) {
2960
+ let left: i32 = toI32(bytes.length)
2961
+ // `at >= 0` is the loop's own, since the prover does not carry it round.
2962
+ while (left > 0 && at >= 0) {
2963
+ const n: i32 = tls.write(bytes, at, left)
2964
+ if (n > 0 && n <= left) {
2965
+ // At most `bytes.length`, so the unsigned sum cannot wrap.
2966
+ at = toI32(toU32(at) + toU32(n))
2967
+ left -= n
2968
+ } else if (n > 0 || n !== TLS_RECORD_AGAIN || tls.outputEnd === tls.outputStart) {
2836
2969
  conn.lost = true
2837
2970
  httpClientTlsFault(tls, conn.fault)
2838
2971
  return false
@@ -2871,16 +3004,18 @@ const httpClientLookupAsync = async (
2871
3004
  ttls: i32[]
2872
3005
  ): Promise<i32> => {
2873
3006
  let high: i32 = 0
2874
- const name: u8[] = new Array<u8>(host.length)
3007
+ // Built by `push`, so no length is computed for `new Array`.
3008
+ const name: u8[] = []
2875
3009
  for (let i: i32 = 0; i < toI32(host.length); i += 1) {
2876
3010
  const c: i32 = toI32(host.charCodeAt(i))
2877
3011
  high = high | c
2878
- if (i < toI32(name.length)) {
2879
- name[i] = toU8(c & 0x7f)
2880
- }
3012
+ name.push(toU8(c & 0x7f))
2881
3013
  }
2882
3014
  if (high <= 127 && dnsParseAddress(name, 0, toI32(name.length), addresses, 0, port)) {
2883
- ttls[0] = 0
3015
+ // Every caller's ttls has DNS_RESOLVE_MAX_ADDRESSES slots.
3016
+ if (toI32(ttls.length) > 0) {
3017
+ ttls[0] = 0
3018
+ }
2884
3019
  return 1
2885
3020
  }
2886
3021
  const hosts: DnsHosts = new DnsHosts()
@@ -2913,7 +3048,7 @@ const httpClientLookupAsync = async (
2913
3048
  }
2914
3049
  let own: i32 = slice
2915
3050
  if (until < HTTP_CLIENT_NEVER) {
2916
- const left: i64 = (until - now + HTTP_CLIENT_NS_PER_MS - toI64(1)) / HTTP_CLIENT_NS_PER_MS
3051
+ const left: i64 = httpClientWaitMs(until, now)
2917
3052
  if (left < toI64(own)) {
2918
3053
  own = toI32(left)
2919
3054
  }
@@ -2934,14 +3069,24 @@ const httpClientLookupAsync = async (
2934
3069
  status = HTTP_CLIENT_LOOKUP_WAIT
2935
3070
  break
2936
3071
  }
2937
- for (let k: i32 = 0; k < count && 2 * k + 1 < toI32(ready.length); k += 1) {
2938
- if (client.owns(ready[2 * k])) {
2939
- client.ready(ready[2 * k], ready[2 * k + 1], after)
3072
+ // ready holds 4 pairs; the literal bound keeps 2 * k + 1 from wrapping.
3073
+ for (let k: i32 = 0; k < count && k < 4; k += 1) {
3074
+ const at: i32 = 2 * k
3075
+ // Said in the body, after the calls the prover takes as a write to any array.
3076
+ if (at < 0 || at + 1 >= toI32(ready.length)) {
3077
+ break
3078
+ }
3079
+ const fd: i32 = ready[at]
3080
+ const events: i32 = ready[at + 1]
3081
+ if (client.owns(fd)) {
3082
+ client.ready(fd, events, after)
2940
3083
  }
2941
3084
  }
2942
3085
  client.tick(after)
2943
3086
  status = client.result(lookup, addresses, ttls)
2944
- slice = slice * 2 < HTTP_CLIENT_DNS_SLICE_MS ? slice * 2 : HTTP_CLIENT_DNS_SLICE_MS
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))
3089
+ slice = twice < HTTP_CLIENT_DNS_SLICE_MS ? twice : HTTP_CLIENT_DNS_SLICE_MS
2945
3090
  }
2946
3091
  netClose(loop)
2947
3092
  return status
@@ -2955,7 +3100,7 @@ const httpClientLookupAsync = async (
2955
3100
  const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32): Promise<i32> => {
2956
3101
  const start: i64 = monotonicNanos()
2957
3102
  const budget: i32 = conn.options.connectTimeoutMs
2958
- let until: i64 = budget > 0 ? start + toI64(budget) * HTTP_CLIENT_NS_PER_MS : HTTP_CLIENT_NEVER
3103
+ let until: i64 = budget > 0 ? httpClientAfter(start, budget) : HTTP_CLIENT_NEVER
2959
3104
  let code: i32 = HTTP_E_CONNECT_TIMEOUT
2960
3105
  if (conn.deadline < HTTP_CLIENT_NEVER && conn.deadline <= until) {
2961
3106
  until = conn.deadline
@@ -2965,7 +3110,7 @@ const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32
2965
3110
  conn.fault.fail(code, httpClientWaitText(code, conn.options, `no time was left to connect to ${host}`))
2966
3111
  return -1
2967
3112
  }
2968
- const addresses: u8[] = new Array<u8>(DNS_RESOLVE_MAX_ADDRESSES * DNS_ADDRESS_BYTES)
3113
+ const addresses: u8[] = new Array<u8>(HTTP_CLIENT_ADDRESS_ROOM)
2969
3114
  const ttls: i32[] = new Array<i32>(DNS_RESOLVE_MAX_ADDRESSES)
2970
3115
  const count = await httpClientLookupAsync(host, port, conn.options.dns, until, addresses, ttls)
2971
3116
  const usable: i32 = httpClientAnswers(host, count, conn.options, until, code, addresses, conn.fault)
@@ -2975,7 +3120,8 @@ const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32
2975
3120
  const one: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
2976
3121
  let last: i32 = 0
2977
3122
  for (let i: i32 = 0; i < usable && i < DNS_RESOLVE_MAX_ADDRESSES; i += 1) {
2978
- copyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)
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)
2979
3125
  const fd: i32 = tcpConnect(one)
2980
3126
  if (fd < 0) {
2981
3127
  last = fd
@@ -3001,7 +3147,7 @@ const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32
3001
3147
  netClose(fd)
3002
3148
  last = result
3003
3149
  }
3004
- conn.fault.fail(HTTP_E_REFUSED, `could not connect to ${host} port ${port} (errno ${-last})`)
3150
+ conn.fault.fail(HTTP_E_REFUSED, `could not connect to ${host} port ${port} (errno ${-toI64(last)})`)
3005
3151
  return -1
3006
3152
  }
3007
3153
 
@@ -3121,7 +3267,7 @@ const httpClientSendAsync = async (conn: HttpClientConn, url: Url, hop: HttpClie
3121
3267
  * passed `limit` (the connection is then not reusable), -1 with the fault set.
3122
3268
  */
3123
3269
  const httpClientBodyAsync = async (conn: HttpClientConn, res: FetchResponse, limit: i64): Promise<i32> => {
3124
- let buf: u8[] = new Array<u8>(0)
3270
+ const buf: u8[] = new Array<u8>(0)
3125
3271
  let len: i32 = 0
3126
3272
  let event: i32 = await httpClientPumpAsync(conn)
3127
3273
  while (event !== HTTP1_END) {
@@ -3142,30 +3288,19 @@ const httpClientBodyAsync = async (conn: HttpClientConn, res: FetchResponse, lim
3142
3288
  )
3143
3289
  return -1
3144
3290
  }
3145
- if (need > toI64(buf.length)) {
3146
- let room: i64 = toI64(buf.length) * toI64(2)
3147
- if (room < need) {
3148
- room = need
3149
- }
3150
- if (room > HTTP_CLIENT_ASYNC_BODY_MAX) {
3151
- room = HTTP_CLIENT_ASYNC_BODY_MAX
3152
- }
3153
- const grown: u8[] = new Array<u8>(toI32(room))
3154
- copyBytes(grown, 0, buf, 0, len)
3155
- buf = grown
3291
+ // need <= HTTP_CLIENT_ASYNC_BODY_MAX, the largest i32, so it narrows exactly.
3292
+ const want: i32 = toI32(need)
3293
+ // Grown by push, whose own doubling keeps it linear, so no i32 length goes to new Array;
3294
+ // buf then holds exactly the body's bytes and needs no trimming copy at the end.
3295
+ while (toI32(buf.length) < want) {
3296
+ buf.push(0)
3156
3297
  }
3157
3298
  copyBytes(buf, len, conn.parser.body, conn.parser.bodyOff, take)
3158
- len += take
3299
+ len = want
3159
3300
  }
3160
3301
  event = await httpClientPumpAsync(conn)
3161
3302
  }
3162
- if (len === toI32(buf.length)) {
3163
- res.body = buf
3164
- } else {
3165
- const exact: u8[] = new Array<u8>(len)
3166
- copyBytes(exact, 0, buf, 0, len)
3167
- res.body = exact
3168
- }
3303
+ res.body = buf
3169
3304
  return 1
3170
3305
  }
3171
3306
 
@@ -3192,13 +3327,14 @@ const httpClientExchangeAsync = async (
3192
3327
  res.status = conn.parser.status
3193
3328
  res.reason = conn.parser.reason
3194
3329
  res.url = httpClientUrlText(url)
3195
- for (
3196
- let i: i32 = 0;
3197
- i < toI32(conn.parser.names.length) && i < toI32(conn.parser.values.length);
3198
- i += 1
3199
- ) {
3200
- res.names.push(conn.parser.names[i])
3201
- res.values.push(conn.parser.values[i])
3330
+ const heads: string[] = conn.parser.names
3331
+ const vals: string[] = conn.parser.values
3332
+ for (let i: i32 = 0; i < toI32(heads.length) && i < toI32(vals.length); i += 1) {
3333
+ // Both read before either push, which the prover takes as a write to any array.
3334
+ const name: string = heads[i]
3335
+ const value: string = vals[i]
3336
+ res.names.push(name)
3337
+ res.values.push(value)
3202
3338
  }
3203
3339
  // A redirect's own body is read only far enough to keep its connection.
3204
3340
  const hopping: boolean =