@amritk/nish-x86_64-linux 0.49.1 → 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 (91) 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 +12 -6
  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 +16 -9
  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/yaml-json.ts +238 -106
  89. package/std/yaml-lines.ts +47 -27
  90. package/std/yaml-write.ts +58 -24
  91. package/std/yaml.ts +808 -395
@@ -166,13 +166,40 @@ const http1ClientDigits = (n: i64): i32 => {
166
166
  return toI32(digits)
167
167
  }
168
168
 
169
+ /**
170
+ * `off + len` when `bytes[off, off + len)` lies inside `bytes`, else -1. Each
171
+ * step subtracts a non-negative value from one that is no larger than
172
+ * `bytes.length`, so the bounds prover sees no sum that can overflow.
173
+ */
174
+ const http1ClientWindowEnd = (bytes: u8[], off: i32, len: i32): i32 => {
175
+ if (off < 0 || len < 0) {
176
+ return -1
177
+ }
178
+ const size: i32 = toI32(bytes.length)
179
+ const room: i32 = size - off
180
+ if (room < 0) {
181
+ return -1
182
+ }
183
+ const gap: i32 = room - len
184
+ let end: i32 = -1
185
+ if (gap >= 0) {
186
+ end = size - gap
187
+ }
188
+ return end
189
+ }
190
+
169
191
  /** Writes the decimal digits of `n`, which is not negative, at `out[at ..)`, which holds them, and answers where they end. */
170
192
  const http1ClientPutDecimal = (out: u8[], at: i32, n: i64): i32 => {
171
- // Unsigned sums: the caller counted room for the digits at `at`, so `end`
172
- // is at most `out.length`, and `end - 1` is at least `at - 1`.
173
- const end: i32 = toI32(toU32(at) + toU32(http1ClientDigits(n)))
193
+ // An `i64` has 1 to 19 digits, and the caller counted room for them at
194
+ // `at`, inside an array, so `at` is far below 2^31 - 20. The tests restate
195
+ // both for the bounds proof; a call outside them writes nothing.
196
+ const digits: i32 = http1ClientDigits(n)
197
+ if (digits < 1 || digits > 19 || at < 0 || at > 2147483628) {
198
+ return at
199
+ }
200
+ const end: i32 = at + digits
174
201
  let v: i64 = n
175
- for (let k: i32 = toI32(toU32(end) - toU32(1)); k >= at && k >= 0 && k < toI32(out.length); k -= 1) {
202
+ for (let k: i32 = end - 1; k >= at && k >= 0 && k < toI32(out.length); k -= 1) {
176
203
  // `v % 10` is a digit, 0 to 9, so adding '0' cannot wrap.
177
204
  out[k] = toU8(toU32(toI32(v % toI64(10))) + toU32(48))
178
205
  v = v / toI64(10)
@@ -584,6 +611,8 @@ const HTTP1_CLIENT_LINE: i32 = -1
584
611
 
585
612
  /** The largest `maxBody`: 2^53, so that sums of it cannot overflow an `i64`. */
586
613
  const HTTP1_CLIENT_MAX_BODY: i64 = 9007199254740992
614
+ /** 2^62 - 1, over any body total: so that adding an `i32` to one cannot overflow. */
615
+ const HTTP1_CLIENT_TOTAL_MASK: i64 = HTTP1_CLIENT_MAX_BODY * 512 - 1
587
616
 
588
617
  /** `limit` clamped into 0 to 2^30. */
589
618
  const http1ClientClamp = (limit: i32): i32 => {
@@ -820,8 +849,14 @@ export class Http1ResponseParser {
820
849
  http1ClientFail(this, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes fed and not read")
821
850
  return
822
851
  }
823
- // Unsigned: the copy above fit `len` bytes after `end` inside `buf`.
824
- this.end = toI32(toU32(this.end) + toU32(len))
852
+ // The copy above fit `len` bytes after `end` inside `buf`, so the window
853
+ // end is always found; it is asked again so the sum is proven.
854
+ const to: i32 = http1ClientWindowEnd(this.buf, this.end, len)
855
+ if (to < 0) {
856
+ http1ClientFail(this, HTTP1_E_MEMORY_LIMIT, "more than 2^30 bytes fed and not read")
857
+ return
858
+ }
859
+ this.end = to
825
860
  }
826
861
 
827
862
  /**
@@ -933,8 +968,15 @@ export class Http1ResponseParser {
933
968
  * @returns The count of unread input bytes.
934
969
  */
935
970
  buffered(): i32 {
936
- // Unsigned: the parser keeps `start <= end`, so the difference cannot wrap.
937
- return toI32(toU32(this.end) - toU32(this.start))
971
+ // The parser keeps `0 <= start <= end`, so the count is `end - start`;
972
+ // the test restates that for the bounds proof.
973
+ const start: i32 = this.start
974
+ const end: i32 = this.end
975
+ let count: i32 = 0
976
+ if (end > start && start >= 0) {
977
+ count = end - start
978
+ }
979
+ return count
938
980
  }
939
981
 
940
982
  /**
@@ -991,9 +1033,13 @@ const http1ClientReserve = (p: Http1ResponseParser, len: i32): boolean => {
991
1033
  return false
992
1034
  }
993
1035
  p.buf = buf
994
- // Unsigned: `scan` and `start` move down together, and `scan` is past `start`
995
- // whenever it is read.
996
- p.scan = toI32(toU32(p.scan) - toU32(from))
1036
+ // `scan` and `start` move down together, and `scan` is past `start`
1037
+ // whenever it is read. `scan` is an offset into `buf`, never negative, and
1038
+ // `from` was asked above not to be, so the difference fits.
1039
+ const scan: i32 = p.scan
1040
+ if (scan >= 0) {
1041
+ p.scan = scan - from
1042
+ }
997
1043
  p.start = 0
998
1044
  p.end = toI32(live)
999
1045
  return true
@@ -1195,11 +1241,13 @@ const http1ClientFieldLine = (p: Http1ResponseParser, keep: boolean): i32 => {
1195
1241
  return http1ClientFail(p, HTTP1_E_FIELD, "a control character in a field value")
1196
1242
  }
1197
1243
  }
1198
- // Counting stops at the limit (at most 2^31 - 1), so the sum does not wrap.
1199
- p.fieldCount = toI32(toU32(p.fieldCount) + toU32(1))
1200
- if (p.fieldCount > p.maxHeaders) {
1244
+ // A count already at the limit refuses the field before it is counted, so
1245
+ // the count stays below `maxHeaders` and the sum cannot overflow.
1246
+ const fields: i32 = p.fieldCount
1247
+ if (fields >= p.maxHeaders) {
1201
1248
  return http1ClientFail(p, HTTP1_E_COUNT_LIMIT, "more fields than the limit")
1202
1249
  }
1250
+ p.fieldCount = fields + 1
1203
1251
  // A trailer is checked and counted, but not kept: nothing here reads one.
1204
1252
  if (keep) {
1205
1253
  let valueStart: i32 = nameEnd + 1
@@ -1257,19 +1305,23 @@ const http1ClientLengthList = (head: u8[], from: i32, to: i32, seen: i64): i64 =
1257
1305
  if (a === b) {
1258
1306
  return toI64(-1)
1259
1307
  }
1260
- let v: i64 = toI64(0)
1308
+ // The digits gather in a `u64`, as the other digit loops in `std/`
1309
+ // do. The test keeps the value at most 2^53, so it never wraps, and
1310
+ // the bounds proof, which bounds no product past 2^31, has no signed
1311
+ // multiply to check.
1312
+ let u: u64 = toU64(0)
1261
1313
  for (let j: i32 = a; j >= 0 && j < b && j < toI32(head.length); j += 1) {
1262
1314
  const c: i32 = toI32(head[j])
1263
1315
  if (c < 48 || c > 57) {
1264
1316
  return toI64(-1)
1265
1317
  }
1266
- if (v <= (HTTP1_CLIENT_MAX_BODY - toI64(c - 48)) / toI64(10)) {
1267
- // Unsigned: `v` starts at 0 and the test keeps it at most 2^53.
1268
- v = toI64(toU64(v) * toU64(10) + toU64(c - 48))
1318
+ if (u <= toU64(HTTP1_CLIENT_MAX_BODY - toI64(c - 48)) / toU64(10)) {
1319
+ u = u * toU64(10) + toU64(c - 48)
1269
1320
  } else {
1270
- v = HTTP1_CLIENT_MAX_BODY + toI64(1)
1321
+ u = toU64(HTTP1_CLIENT_MAX_BODY) + toU64(1)
1271
1322
  }
1272
1323
  }
1324
+ const v: i64 = toI64(u)
1273
1325
  if (found >= toI64(0) && (found !== v || v > HTTP1_CLIENT_MAX_BODY)) {
1274
1326
  return toI64(-2)
1275
1327
  }
@@ -1377,7 +1429,7 @@ const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lowe
1377
1429
  }
1378
1430
  // Unsigned, as are the sizes below: `a <= b` and `at <= used` were just
1379
1431
  // asked, and `size` starts at `used` or more, so no difference wraps.
1380
- const n: i32 = toI32(toU32(b) - toU32(a))
1432
+ const n: i32 = b - a
1381
1433
  if (toU32(n) > toU32(used) - toU32(at)) {
1382
1434
  let size: u32 = used < 256 ? toU32(256) : toU32(used)
1383
1435
  while (size - toU32(at) < toU32(n)) {
@@ -1395,8 +1447,12 @@ const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lowe
1395
1447
  if (!tryCopyBytes(p.head, at, buf, a, n)) {
1396
1448
  return -1
1397
1449
  }
1398
- // Unsigned: the copy above fit `n` bytes after `at` inside `head`.
1399
- const stop: i32 = toI32(toU32(at) + toU32(n))
1450
+ // The copy above fit `n` bytes after `at` inside `head`, so the window end
1451
+ // is always found; it is asked again so the sum is proven.
1452
+ const stop: i32 = http1ClientWindowEnd(p.head, at, n)
1453
+ if (stop < 0) {
1454
+ return -1
1455
+ }
1400
1456
  if (lower) {
1401
1457
  const head: u8[] = p.head
1402
1458
  for (let k: i32 = at; k >= 0 && k < stop && k < toI32(head.length); k += 1) {
@@ -1413,9 +1469,14 @@ const http1ClientKeep = (p: Http1ResponseParser, buf: u8[], a: i32, b: i32, lowe
1413
1469
  /** Records the field whose name starts at `name` and whose value is `head[value .. valueEnd)`. */
1414
1470
  const http1ClientSpan = (p: Http1ResponseParser, name: i32, value: i32, valueEnd: i32): void => {
1415
1471
  const spans: i32[] = p.spans
1416
- // Unsigned: `spans` holds 4 entries a field, so `spanCount * 4` fits an
1417
- // `i32` length and neither the product nor the count wraps.
1418
- const at: i32 = toI32(toU32(p.spanCount) * toU32(4))
1472
+ // `spans` holds 4 entries a field, so `spanCount * 4` fits an `i32` length:
1473
+ // `spanCount` is at most 2^29 - 1, which the test restates for the bounds
1474
+ // proof. A count outside it would only append.
1475
+ const count: i32 = p.spanCount
1476
+ let at: i32 = -1
1477
+ if (count >= 0 && count <= 536870911) {
1478
+ at = count * 4
1479
+ }
1419
1480
  if (at >= 0 && at <= toI32(spans.length) - 4) {
1420
1481
  spans[at] = name
1421
1482
  spans[at + 1] = value
@@ -1427,7 +1488,11 @@ const http1ClientSpan = (p: Http1ResponseParser, name: i32, value: i32, valueEnd
1427
1488
  spans.push(value)
1428
1489
  spans.push(valueEnd)
1429
1490
  }
1430
- p.spanCount = toI32(toU32(p.spanCount) + toU32(1))
1491
+ // For the same reason the count never reaches 2^31 - 1; the test restates
1492
+ // that for the bounds proof.
1493
+ if (count < 2147483647) {
1494
+ p.spanCount = count + 1
1495
+ }
1431
1496
  }
1432
1497
 
1433
1498
  /** Whether `head[a .. b)` is `word`, byte for byte. */
@@ -1631,7 +1696,10 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1631
1696
  const a: i32 = p.lineStart
1632
1697
  const b: i32 = p.lineEnd < toI32(buf.length) ? p.lineEnd : toI32(buf.length)
1633
1698
  const room: i64 = http1ClientRoom(p)
1634
- let size: i64 = toI64(0)
1699
+ // The size gathers in a `u64`, as the other digit loops in `std/` do. The
1700
+ // test below keeps it at most `room`, so it never wraps, and the bounds
1701
+ // proof, which bounds no product past 2^31, has no signed multiply to check.
1702
+ let size: u64 = toU64(0)
1635
1703
  let over: boolean = false
1636
1704
  let i: i32 = a
1637
1705
  while (i >= 0 && i < b && i < toI32(buf.length)) {
@@ -1640,12 +1708,12 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1640
1708
  break
1641
1709
  }
1642
1710
  if (!over) {
1643
- // Unsigned: the first test leaves `digit` at most `room`.
1644
- if (toI64(digit) > room || size > toI64(toU64(room) - toU64(toI64(digit))) / toI64(16)) {
1711
+ // The first two tests leave `digit` at most `room`, so the difference
1712
+ // fits and is not negative.
1713
+ if (room < 0 || toI64(digit) > room || size > toU64(room - toI64(digit)) / toU64(16)) {
1645
1714
  over = true
1646
1715
  } else {
1647
- // Unsigned: `size` starts at 0 and the test keeps it at most `room`.
1648
- size = toI64(toU64(size) * toU64(16) + toU64(digit))
1716
+ size = size * toU64(16) + toU64(digit)
1649
1717
  }
1650
1718
  }
1651
1719
  i += 1
@@ -1678,8 +1746,8 @@ const http1ClientChunkSize = (p: Http1ResponseParser): i32 => {
1678
1746
  if (over) {
1679
1747
  return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a chunked body over the limit")
1680
1748
  }
1681
- p.left = size
1682
- p.state = size === toI64(0) ? HTTP1_CLIENT_S_TRAILERS : HTTP1_CLIENT_S_CHUNK_DATA
1749
+ p.left = toI64(size)
1750
+ p.state = size === toU64(0) ? HTTP1_CLIENT_S_TRAILERS : HTTP1_CLIENT_S_CHUNK_DATA
1683
1751
  p.sectionBytes = 0
1684
1752
  return HTTP1_NEED_MORE
1685
1753
  }
@@ -1699,15 +1767,34 @@ const http1ClientBodyWindow = (p: Http1ResponseParser, after: i32): i32 => {
1699
1767
  const ready: i32 = end - start
1700
1768
  const left: i64 = p.left
1701
1769
  const take: i32 = toI64(ready) < left ? ready : toI32(left)
1770
+ // `left` only falls from a length, a chunk size or the room, none of them
1771
+ // negative, and by at most itself; `take` is at most `ready` and `left`;
1772
+ // `bodyTotal` grows from 0 by what `maxBody` (at most 2^53) allows. The
1773
+ // tests restate that for the bounds proof, so none of the sums below
1774
+ // overflows.
1775
+ const total: i64 = p.bodyTotal
1776
+ if (left < 0 || take < 0 || take > ready || total < 0 || total > HTTP1_CLIENT_TOTAL_MASK) {
1777
+ return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a body over the limit")
1778
+ }
1779
+ // What stays buffered: not negative, as `take` is at most `ready`, and at
1780
+ // most `ready`, so at most `end`. The test restates the last for the
1781
+ // bounds proof, which keeps no relation between `ready` and `end`.
1782
+ const rest: i32 = ready - take
1783
+ if (rest < 0 || rest > end) {
1784
+ return http1ClientFail(p, HTTP1_E_BODY_LIMIT, "a body over the limit")
1785
+ }
1702
1786
  p.body = p.buf
1703
1787
  p.bodyOff = start
1704
1788
  p.bodyLen = take
1705
- // Unsigned: `take` is at most `ready`, `left`, and the room under
1706
- // `maxBody` (at most 2^53), so none of these wraps.
1707
- p.start = toI32(toU32(start) + toU32(take))
1789
+ // `end - rest` is `start + take`, spelled as a difference of values that
1790
+ // are not negative.
1791
+ p.start = end - rest
1708
1792
  p.scan = p.start
1709
- p.left = toI64(toU64(left) - toU64(toI64(take)))
1710
- p.bodyTotal = toI64(toU64(p.bodyTotal) + toU64(toI64(take)))
1793
+ p.left = left - toI64(take)
1794
+ // `total` is at most the mask, tested above, so the mask changes nothing:
1795
+ // it is how the bounds proof learns that top, which it keeps from no
1796
+ // comparison past 2^31.
1797
+ p.bodyTotal = (total & HTTP1_CLIENT_TOTAL_MASK) + toI64(take)
1711
1798
  if (p.left === toI64(0)) {
1712
1799
  p.state = after
1713
1800
  }
@@ -1716,9 +1803,19 @@ const http1ClientBodyWindow = (p: Http1ResponseParser, after: i32): i32 => {
1716
1803
 
1717
1804
  /**
1718
1805
  * The body bytes `maxBody` still allows: `maxBody - bodyTotal`, negative
1719
- * when a caller lowered `maxBody` under it (unsigned, then read signed).
1806
+ * when a caller lowered `maxBody` under it. Both are kept at or above 0, so
1807
+ * the difference fits; were either negative, the answer is -1, which every
1808
+ * caller reads as no room, as it reads any negative room.
1720
1809
  */
1721
- const http1ClientRoom = (p: Http1ResponseParser): i64 => toI64(toU64(p.maxBody) - toU64(p.bodyTotal))
1810
+ const http1ClientRoom = (p: Http1ResponseParser): i64 => {
1811
+ const max: i64 = p.maxBody
1812
+ const total: i64 = p.bodyTotal
1813
+ let room: i64 = toI64(-1)
1814
+ if (max >= 0 && total >= 0) {
1815
+ room = max - total
1816
+ }
1817
+ return room
1818
+ }
1722
1819
 
1723
1820
  /** A body read until the server closes: every byte fed, up to `maxBody`. */
1724
1821
  const http1ClientCloseBody = (p: Http1ResponseParser): i32 => {