@amritk/nish-aarch64-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
@@ -80,7 +80,12 @@
80
80
  * output buffer of `compressBound(maxLength)` bytes and
81
81
  * the field arrays `addFields` fills, all built once.
82
82
  */
83
- import { DEFLATE_ERR_OUTPUT_LIMIT, DEFLATE_FAST, compressBound } from "nish/compress/deflate"
83
+ import {
84
+ DEFLATE_ERR_OUTPUT_LIMIT,
85
+ DEFLATE_ERR_WINDOW,
86
+ DEFLATE_FAST,
87
+ compressBound,
88
+ } from "nish/compress/deflate"
84
89
  import { adler32 } from "nish/compress/checksum"
85
90
  import { GzipCompressor, GzipDecompressor, GzipFixedCompressor } from "nish/compress/gzip"
86
91
  import { ZlibDecompressor } from "nish/compress/zlib"
@@ -195,12 +200,15 @@ const httpSegmentsOf = (bytes: i32, size: u32): i32 => {
195
200
  if (bytes <= 0) {
196
201
  return 0
197
202
  }
198
- // `size` is 1 or more, so the quotient is at most `bytes - 1` and the sum
199
- // at most `bytes`: unsigned arithmetic gives the same count with no check.
200
- // Both callers pass 1 or more, so the test always holds; it is what shows
201
- // the division has a divisor.
203
+ // Both callers pass a `size` of 1 or more, so the test always holds; it is
204
+ // what shows the division has a divisor. The quotient is then at most
205
+ // `bytes - 1`, so `whole < bytes` always holds too, and it is what proves
206
+ // the count, at most `bytes`, cannot overflow.
202
207
  if (size > 0) {
203
- return toI32((toU32(bytes) - 1) / size + 1)
208
+ const whole: i32 = toI32(toU32(bytes - 1) / size)
209
+ if (whole >= 0 && whole < bytes) {
210
+ return whole + 1
211
+ }
204
212
  }
205
213
  return 0
206
214
  }
@@ -291,8 +299,12 @@ const httpQValue = (buf: u8[], start: i32, end: i32): i32 => {
291
299
  return -1
292
300
  }
293
301
  // At most three digits follow the point, each 9 or less at a scale of 100
294
- // or less, so `q` stays under 1000 and unsigned arithmetic cannot wrap.
295
- q = toI32(toU32(q) + toU32(d) * toU32(scale))
302
+ // or less, so `q` stays at most 1000 (`HTTP_Q_ONE`) and the test always
303
+ // holds; it is what proves the sum cannot overflow.
304
+ if (q < 0 || q > 1000 || scale < 0 || scale > 100) {
305
+ return -1
306
+ }
307
+ q = q + d * scale
296
308
  scale = scale / 10
297
309
  }
298
310
  return q
@@ -512,7 +524,11 @@ const httpGzipToZlib = (out: u8[], n: i32, level: i32, src: u8[], off: i32, len:
512
524
  for (let i: i32 = 2; i < end && i >= 0 && i + 8 < toI32(out.length); i += 1) {
513
525
  out[i] = out[i + 8]
514
526
  }
515
- const a: u32 = adler32(src, off, len)
527
+ const a64: i64 = adler32(src, off, len)
528
+ if (a64 < 0) {
529
+ return HTTP_ZLIB_FAILED
530
+ }
531
+ const a: u32 = toU32(a64)
516
532
  out[end] = toU8(a >>> 24)
517
533
  out[end + 1] = toU8((a >>> 16) & 255)
518
534
  out[end + 2] = toU8((a >>> 8) & 255)
@@ -606,15 +622,9 @@ export class HttpCompressor {
606
622
  }
607
623
  this.gzip = new GzipCompressor(level)
608
624
  this.fixed = new GzipFixedCompressor()
609
- // Grown a byte at a time, once: a length the compiler cannot see is not
610
- // negative would cost `new Array` a check. No bench/instructions.json row
611
- // builds a compressor, so no counted figure includes this loop.
612
- const output: u8[] = []
625
+ // One zeroed allocation, sized once; the guard proves its length.
613
626
  const bound: i32 = compressBound(maxLength)
614
- while (toI32(output.length) < bound) {
615
- output.push(0)
616
- }
617
- this.output = output
627
+ this.output = bound > 0 ? new Array<u8>(bound) : []
618
628
  this.names = []
619
629
  this.values = []
620
630
  this.contentEncodingName = httpCompressBytes("content-encoding")
@@ -694,9 +704,13 @@ export class HttpCompressor {
694
704
  return -1
695
705
  }
696
706
  // The longest output kept; `output` holds `compressBound(maxLength)`, more.
697
- // `len` is 0 or more and the saving 1 or more, so the difference is an
698
- // `i32` and unsigned arithmetic gives it with no check.
699
- const keep: i32 = toI32(toU32(len) - toU32(this.leastSaving(len)))
707
+ // `len` is 0 or more and the saving 1 or more, so the test always holds;
708
+ // it is what proves the difference cannot overflow.
709
+ const saving: i32 = this.leastSaving(len)
710
+ let keep: i32 = -1
711
+ if (len >= 0 && saving >= 0) {
712
+ keep = len - saving
713
+ }
700
714
  let n: i32 = -1
701
715
  if (keep > 0) {
702
716
  const fixed: boolean =
@@ -706,9 +720,10 @@ export class HttpCompressor {
706
720
  let room: i32 = keep
707
721
  if (deflate) {
708
722
  // Below `size - HTTP_ZLIB_SHORTER`, `keep` plus that many is below
709
- // `size`, so the unsigned sum is the `i32` one.
723
+ // `size`; the test, with the constant's 12 spelled for the prover, is
724
+ // what proves the sum cannot overflow.
710
725
  const size: i32 = toI32(this.output.length)
711
- room = keep < size - HTTP_ZLIB_SHORTER ? toI32(toU32(keep) + toU32(HTTP_ZLIB_SHORTER)) : size
726
+ room = keep < size - 12 ? keep + 12 : size
712
727
  }
713
728
  n = fixed
714
729
  ? this.fixed.compress(body, off, len, this.output, toI32(0), room)
@@ -923,18 +938,38 @@ export class HttpCompressor {
923
938
  * uncompressed
924
939
  */
925
940
  http1IdentityHead(encodedHead: i32, encodedLength: i32, length: i32): i32 {
926
- // `encodedHead` is a head's length, and every field here a few bytes, so
927
- // the answer is an `i32`; unsigned arithmetic wraps on the way and lands
928
- // on the same bytes with no check.
929
- let head: u32 =
930
- toU32(encodedHead) -
931
- toU32(HTTP1_GZIP_FIELD_BYTES) +
932
- toU32(this.gzipValue.length) -
933
- toU32(this.codingValue.length)
941
+ // `encodedHead` is the length of a head just written, far below 2^30, each
942
+ // field here is a fixed word ("gzip", "deflate", "vary",
943
+ // "accept-encoding") and a count has at most ten digits, so the tests
944
+ // always hold; they are what prove the sums below cannot overflow.
945
+ const gzip: i32 = toI32(this.gzipValue.length)
946
+ const coding: i32 = toI32(this.codingValue.length)
947
+ const name: i32 = toI32(this.varyName.length)
948
+ const value: i32 = toI32(this.acceptEncodingValue.length)
949
+ const before: i32 = httpDecimalDigits(encodedLength)
950
+ const after: i32 = httpDecimalDigits(length)
951
+ if (encodedHead < 0 || encodedHead > 1073741824) {
952
+ return encodedHead
953
+ }
954
+ if (
955
+ gzip < 0 ||
956
+ gzip > 64 ||
957
+ coding < 0 ||
958
+ coding > 64 ||
959
+ name < 0 ||
960
+ name > 64 ||
961
+ value < 0 ||
962
+ value > 64
963
+ ) {
964
+ return encodedHead
965
+ }
966
+ if (before < 0 || before > 10 || after < 0 || after > 10) {
967
+ return encodedHead
968
+ }
934
969
  if (this.vary) {
935
- head -= toU32(this.varyName.length) + toU32(this.acceptEncodingValue.length) + 4
970
+ return encodedHead - HTTP1_GZIP_FIELD_BYTES + gzip - coding - (name + value + 4) - before + after
936
971
  }
937
- return toI32(head - toU32(httpDecimalDigits(encodedLength)) + toU32(httpDecimalDigits(length)))
972
+ return encodedHead - HTTP1_GZIP_FIELD_BYTES + gzip - coding - before + after
938
973
  }
939
974
 
940
975
  /**
@@ -1117,6 +1152,27 @@ export const HTTP_DECODE_BAD_CALL: i32 = -3
1117
1152
  /** The first output buffer `HttpBodyDecoder` makes, when its limit allows that many bytes. */
1118
1153
  const HTTP_DECODE_FIRST: i32 = 4096
1119
1154
 
1155
+ /**
1156
+ * Why `new HttpBodyDecoder(limit)` would panic: a `limit` under 1. A caller
1157
+ * that checks it first, and leaves on a fault, reaches no panic there.
1158
+ * @param limit the limit the decoder would be made with
1159
+ * @returns the fault, or "" when the limit is at least 1
1160
+ */
1161
+ export const httpBodyDecoderFault = (limit: i32): string => {
1162
+ if (limit < 1) {
1163
+ return `HttpBodyDecoder: a limit of ${limit} is under 1`
1164
+ }
1165
+ return ""
1166
+ }
1167
+
1168
+ /** Panics on the fault `httpBodyDecoderFault` names, the decoder's contract. */
1169
+ const httpCheckBodyDecoder = (limit: i32): void => {
1170
+ const fault: string = httpBodyDecoderFault(limit)
1171
+ if (fault.length > 0) {
1172
+ panic(fault)
1173
+ }
1174
+ }
1175
+
1120
1176
  /**
1121
1177
  * Decodes a request body sent with `Content-Encoding: gzip` or `deflate`
1122
1178
  * under a limit on what it decodes to, so a small body that inflates to
@@ -1142,13 +1198,11 @@ export class HttpBodyDecoder {
1142
1198
 
1143
1199
  /**
1144
1200
  * A decoder that refuses a body decoding to more than `limit` bytes; a
1145
- * `limit` under 1 panics.
1201
+ * `limit` under 1 panics, the fault `httpBodyDecoderFault` names first.
1146
1202
  * @param limit the most bytes one body may decode to
1147
1203
  */
1148
1204
  constructor(limit: i32) {
1149
- if (limit < 1) {
1150
- panic(`HttpBodyDecoder: a limit of ${limit} is under 1`)
1151
- }
1205
+ httpCheckBodyDecoder(limit)
1152
1206
  this.limit = limit
1153
1207
  this.output = []
1154
1208
  this.gunzip = new GzipDecompressor()
@@ -1199,11 +1253,13 @@ export class HttpBodyDecoder {
1199
1253
  if (off < 0 || len < 0 || off > toI32(src.length) - len) {
1200
1254
  return HTTP_DECODE_BAD_CALL
1201
1255
  }
1256
+ const stop: i32 = toI32(toI64(off) + toI64(len))
1202
1257
  const limit: i32 = this.limit
1203
1258
  // A first guess of four times the input, so most bodies decode at once.
1204
1259
  // Under the test, `len` is at most a quarter of `limit`, so four times it
1205
- // is an `i32` and the unsigned product is the same.
1206
- let size: i32 = len > limit >> 2 ? limit : toI32(toU32(len) * 4)
1260
+ // is an `i32`. The literal is the largest quarter an `i32` limit has, so
1261
+ // its test never decides the answer; it is what proves the product.
1262
+ let size: i32 = len < 0 || len > 536870911 || len > limit >> 2 ? limit : len * 4
1207
1263
  if (size < HTTP_DECODE_FIRST) {
1208
1264
  size = limit < HTTP_DECODE_FIRST ? limit : HTTP_DECODE_FIRST
1209
1265
  }
@@ -1217,13 +1273,18 @@ export class HttpBodyDecoder {
1217
1273
  while (toI32(output.length) < size) {
1218
1274
  output.push(0)
1219
1275
  }
1276
+ // The range decoders answer a misuse rather than panicking, so a body
1277
+ // decoded here reaches no panic; the test above has ruled it out.
1220
1278
  const end: i32 =
1221
1279
  coding === HTTP_CODING_GZIP
1222
- ? this.gunzip.decompress(src, off, len, this.output, toI32(0), limit)
1223
- : this.unzlib.decompress(src, off, len, this.output, toI32(0), limit)
1280
+ ? this.gunzip.decompressRange(src, off, stop, output, toI32(0), limit)
1281
+ : this.unzlib.decompressRange(src, off, stop, output, toI32(0), limit)
1224
1282
  if (end >= 0) {
1225
1283
  return end
1226
1284
  }
1285
+ if (end === DEFLATE_ERR_WINDOW) {
1286
+ return HTTP_DECODE_BAD_CALL
1287
+ }
1227
1288
  if (end !== DEFLATE_ERR_OUTPUT_LIMIT) {
1228
1289
  return HTTP_DECODE_MALFORMED
1229
1290
  }
@@ -1232,8 +1293,9 @@ export class HttpBodyDecoder {
1232
1293
  return HTTP_DECODE_TOO_LARGE
1233
1294
  }
1234
1295
  // Under the test, `have` is at most half of `limit`, so twice it is an
1235
- // `i32` and the unsigned product is the same.
1236
- size = have > limit >> 1 ? limit : toI32(toU32(have) * 2)
1296
+ // `i32`. The literal is the largest half an `i32` limit has, so its test
1297
+ // never decides the answer; it is what proves the product.
1298
+ size = have < 0 || have > 1073741823 || have > limit >> 1 ? limit : have * 2
1237
1299
  }
1238
1300
  }
1239
1301
  }