@amritk/nish-aarch64-linux 0.49.0 → 0.50.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 (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
package/std/net/fetch.ts CHANGED
@@ -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)
@@ -979,8 +983,13 @@ export class FetchSyncClient {
979
983
  while (left > 0 && at >= 0) {
980
984
  const n: i32 = conn.write(bytes, at, left)
981
985
  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))
986
+ // At most `off + len`, inside `bytes`, so the window always has an
987
+ // end; the helper measures it so that the sum is proven to fit.
988
+ at = httpClientWindowEnd(bytes, at, n)
989
+ if (at < 0) {
990
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
991
+ return false
992
+ }
984
993
  left -= n
985
994
  } else if (n > 0 || n !== TLS_RECORD_AGAIN || conn.outputEnd === conn.outputStart) {
986
995
  this.lost = true
@@ -1009,8 +1018,13 @@ export class FetchSyncClient {
1009
1018
  }
1010
1019
  const n: i32 = netWrite(this.fd, bytes, at, left)
1011
1020
  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))
1021
+ // At most `off + len`, inside `bytes`, so the window always has an
1022
+ // end; the helper measures it so that the sum is proven to fit.
1023
+ at = httpClientWindowEnd(bytes, at, n)
1024
+ if (at < 0) {
1025
+ this.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
1026
+ return false
1027
+ }
1014
1028
  left -= n
1015
1029
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
1016
1030
  const waited: i32 = this.wait(this.fd, HTTP_CLIENT_WRITABLE, this.readDeadline(), HTTP_E_READ_TIMEOUT)
@@ -1054,8 +1068,13 @@ export class FetchSyncClient {
1054
1068
  const one: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
1055
1069
  let last: i32 = 0
1056
1070
  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)
1071
+ // `i` is below `DNS_RESOLVE_MAX_ADDRESSES` (16), so the product stays
1072
+ // inside `addresses`; the test restates that with the literal the
1073
+ // bounds proof reads.
1074
+ if (i > 15) {
1075
+ break
1076
+ }
1077
+ copyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)
1059
1078
  const fd: i32 = tcpConnect(one)
1060
1079
  if (fd < 0) {
1061
1080
  last = fd
@@ -1252,10 +1271,16 @@ export class FetchSyncClient {
1252
1271
  }
1253
1272
  const take: i32 = n < this.pendLen ? n : this.pendLen
1254
1273
  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))
1274
+ // The copy above fit `take` bytes at `pendOff` inside the parser's body,
1275
+ // so the window has an end, and `take` is at most `pendLen`. The helper
1276
+ // and the test restate both for the bounds proof.
1277
+ const pendEnd: i32 = httpClientWindowEnd(this.parser.body, this.pendOff, take)
1278
+ const pendLen: i32 = this.pendLen
1279
+ if (pendEnd < 0 || take < 0 || take > pendLen) {
1280
+ return -HTTP_E_IO
1281
+ }
1282
+ this.pendOff = pendEnd
1283
+ this.pendLen = pendLen - take
1259
1284
  return take
1260
1285
  }
1261
1286
 
@@ -1461,17 +1486,41 @@ export const httpNoProxyMatch = (noProxy: string, host: string, port: i32): bool
1461
1486
 
1462
1487
  // --- helpers -----------------------------------------------------------------
1463
1488
 
1489
+ /**
1490
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
1491
+ * step subtracts a value that is not negative from one that is no larger than
1492
+ * `bytes.length`, so the bounds proof sees no sum that can overflow.
1493
+ */
1494
+ const httpClientWindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
1495
+ if (off < 0 || len < 0) {
1496
+ return -1
1497
+ }
1498
+ const size: i32 = toI32(bytes.length)
1499
+ const room: i32 = size - off
1500
+ if (room < 0) {
1501
+ return -1
1502
+ }
1503
+ const gap: i32 = room - len
1504
+ let end: i32 = -1
1505
+ if (gap >= 0) {
1506
+ end = size - gap
1507
+ }
1508
+ return end
1509
+ }
1510
+
1464
1511
  /**
1465
1512
  * `ms` milliseconds after `start` on the monotonic clock, or
1466
1513
  * `HTTP_CLIENT_NEVER` for a `start` outside [0, 2^62] or a negative `ms`,
1467
1514
  * which the clock and the options never give.
1468
1515
  */
1469
1516
  const httpClientAfter = (start: i64, ms: i32): i64 => {
1470
- if (start < toI64(0) || start > HTTP_CLIENT_NEVER || ms < 0) {
1517
+ if (start < 0 || start > HTTP_CLIENT_NEVER || ms < 0) {
1471
1518
  return HTTP_CLIENT_NEVER
1472
1519
  }
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)))
1520
+ // `start` is at most `HTTP_CLIENT_NEVER`, under 2^62, so the mask changes
1521
+ // nothing: it is how the bounds proof learns that top, which it keeps from
1522
+ // no comparison past 2^31. The product is under 2^51, so the sum fits.
1523
+ return (start & HTTP_CLIENT_LOW62) + toI64(ms) * toI64(1000000)
1475
1524
  }
1476
1525
 
1477
1526
  /**
@@ -1482,15 +1531,17 @@ const httpClientWaitMs = (end: i64, now: i64): i64 => {
1482
1531
  if (end <= now) {
1483
1532
  return toI64(0)
1484
1533
  }
1485
- if (now < toI64(0) || end > HTTP_CLIENT_NEVER) {
1534
+ if (now < 0 || end > HTTP_CLIENT_NEVER) {
1486
1535
  return HTTP_CLIENT_MAX_WAIT_MS
1487
1536
  }
1488
- // 0 <= now < end <= 2^62, so the unsigned difference cannot wrap.
1489
- const left: i64 = toI64(toU64(end) - toU64(now))
1537
+ // 0 <= now < end, so the difference fits.
1538
+ const left: i64 = end - now
1490
1539
  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))
1540
+ // A wait of `HTTP_CLIENT_MAX_WAIT_MS` (3600000) or more is cut to that
1541
+ // below whether it is rounded up or not, so only a shorter one is, and the
1542
+ // literal bound proves the step fits.
1543
+ if (left % toI64(1000000) > toI64(0) && ms < 3600000) {
1544
+ ms = ms + toI64(1)
1494
1545
  }
1495
1546
  return ms > HTTP_CLIENT_MAX_WAIT_MS ? HTTP_CLIENT_MAX_WAIT_MS : ms
1496
1547
  }
@@ -1584,8 +1635,14 @@ const httpClientDropField = (names: string[], values: string[], lower: string):
1584
1635
  names[keep] = name
1585
1636
  values[keep] = value
1586
1637
  }
1587
- // keep <= i, so the unsigned step cannot wrap.
1588
- keep = toI32(toU32(keep) + toU32(1))
1638
+ // `keep` is at most `i`, below the length, so the step fits. Where
1639
+ // they are equal it is written `i + 1`, the form the bounds proof
1640
+ // reads from the loop test.
1641
+ if (keep < i) {
1642
+ keep = keep + 1
1643
+ } else {
1644
+ keep = i + 1
1645
+ }
1589
1646
  }
1590
1647
  }
1591
1648
  while (toI32(names.length) > keep && toI32(names.length) > 0) {
@@ -2210,9 +2267,9 @@ const httpClientResolve = (
2210
2267
  fault: HttpClientFault
2211
2268
  ): i32 => {
2212
2269
  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
2270
+ if (start >= 0 && start < until && until <= HTTP_CLIENT_NEVER) {
2271
+ // 0 <= start < until, so the difference fits.
2272
+ lookupMs = (until - start) / HTTP_CLIENT_NS_PER_MS
2216
2273
  }
2217
2274
  if (lookupMs < toI64(1)) {
2218
2275
  lookupMs = toI64(1)
@@ -2409,8 +2466,11 @@ export class FetchPool {
2409
2466
  let count: i32 = 0
2410
2467
  for (let i: i32 = 0; i < FETCH_POOL_SLOTS && i < toI32(fds.length); i += 1) {
2411
2468
  if (fds[i] >= 0) {
2412
- // At most i, so the unsigned step cannot wrap.
2413
- count = toI32(toU32(count) + toU32(1))
2469
+ // At most `i`, below `FETCH_POOL_SLOTS`, so the step fits; the test
2470
+ // restates that for the bounds proof.
2471
+ if (count < i + 1) {
2472
+ count = count + 1
2473
+ }
2414
2474
  }
2415
2475
  }
2416
2476
  return count
@@ -2458,8 +2518,11 @@ export class FetchPool {
2458
2518
  if (fds[i] < 0) {
2459
2519
  free = free < 0 ? i : free
2460
2520
  } else if (routes[i] === route) {
2461
- // At most i, so the unsigned step cannot wrap.
2462
- same = toI32(toU32(same) + toU32(1))
2521
+ // At most `i`, below `FETCH_POOL_SLOTS`, so the step fits; the test
2522
+ // restates that for the bounds proof.
2523
+ if (same < i + 1) {
2524
+ same = same + 1
2525
+ }
2463
2526
  }
2464
2527
  }
2465
2528
  // free, when found, is an index of both arrays; said again for the prover.
@@ -2745,8 +2808,13 @@ const httpClientWriteRawAsync = async (
2745
2808
  }
2746
2809
  const n: i32 = netWrite(conn.fd, bytes, at, left)
2747
2810
  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))
2811
+ // At most `off + len`, inside `bytes`, so the window always has an
2812
+ // end; the helper measures it so that the sum is proven to fit.
2813
+ at = httpClientWindowEnd(bytes, at, n)
2814
+ if (at < 0) {
2815
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
2816
+ return false
2817
+ }
2750
2818
  left -= n
2751
2819
  } else if (n === HTTP_CLIENT_WOULD_BLOCK || n === 0) {
2752
2820
  const ready = await httpClientWaitPeerAsync(conn, true)
@@ -2962,8 +3030,13 @@ const httpClientWriteAsync = async (conn: HttpClientConn, bytes: u8[]): Promise<
2962
3030
  while (left > 0 && at >= 0) {
2963
3031
  const n: i32 = tls.write(bytes, at, left)
2964
3032
  if (n > 0 && n <= left) {
2965
- // At most `bytes.length`, so the unsigned sum cannot wrap.
2966
- at = toI32(toU32(at) + toU32(n))
3033
+ // At most `bytes.length`, so the window always has an end; the helper
3034
+ // measures it so that the sum is proven to fit.
3035
+ at = httpClientWindowEnd(bytes, at, n)
3036
+ if (at < 0) {
3037
+ conn.fault.fail(HTTP_E_IO, "the bytes to write lie outside their buffer")
3038
+ return false
3039
+ }
2967
3040
  left -= n
2968
3041
  } else if (n > 0 || n !== TLS_RECORD_AGAIN || tls.outputEnd === tls.outputStart) {
2969
3042
  conn.lost = true
@@ -3084,8 +3157,12 @@ const httpClientLookupAsync = async (
3084
3157
  }
3085
3158
  client.tick(after)
3086
3159
  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))
3160
+ // `slice` stays within 1 to `HTTP_CLIENT_DNS_SLICE_MS` (16), so doubling
3161
+ // it fits; the test restates that with the literal the bounds proof reads.
3162
+ let twice: i32 = HTTP_CLIENT_DNS_SLICE_MS
3163
+ if (slice >= 0 && slice <= 16) {
3164
+ twice = slice * 2
3165
+ }
3089
3166
  slice = twice < HTTP_CLIENT_DNS_SLICE_MS ? twice : HTTP_CLIENT_DNS_SLICE_MS
3090
3167
  }
3091
3168
  netClose(loop)
@@ -3120,8 +3197,13 @@ const httpClientDialAsync = async (conn: HttpClientConn, host: string, port: i32
3120
3197
  const one: u8[] = new Array<u8>(DNS_ADDRESS_BYTES)
3121
3198
  let last: i32 = 0
3122
3199
  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)
3200
+ // `i` is below `DNS_RESOLVE_MAX_ADDRESSES` (16), so the product stays
3201
+ // inside `addresses`; the test restates that with the literal the bounds
3202
+ // proof reads.
3203
+ if (i > 15) {
3204
+ break
3205
+ }
3206
+ copyBytes(one, 0, addresses, i * DNS_ADDRESS_BYTES, DNS_ADDRESS_BYTES)
3125
3207
  const fd: i32 = tcpConnect(one)
3126
3208
  if (fd < 0) {
3127
3209
  last = fd