@amritk/nish-x86_64-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
@@ -458,18 +458,40 @@ const http1ServerConfigFault = (config: Http1Config): string => {
458
458
  const one: i32 = 1
459
459
  const smallest: i32 = 64
460
460
  const output: i32 = 1024
461
- const caps: string[] = [
462
- http1ServerCapFault("maxTarget", config.maxTarget, one, big),
463
- http1ServerCapFault("maxHeaderBytes", config.maxHeaderBytes, smallest, big),
464
- http1ServerCapFault("maxHeaders", config.maxHeaders, one, big),
465
- http1ServerCapFault("maxBody", config.maxBody, H1_ZERO, big),
466
- http1ServerCapFault("chunkSize", config.chunkSize, one, big),
467
- http1ServerCapFault("outputSize", config.outputSize, output, big),
468
- http1ServerCapFault("maxMessage", config.maxMessage, H1_ZERO, big - H1_FRAME_HEADER),
469
- http1ServerCapFault("idleTimeout", config.idleTimeout, one, big),
470
- http1ServerCapFault("headTimeout", config.headTimeout, one, big),
471
- http1ServerCapFault("lingerTimeout", config.lingerTimeout, one, big),
472
- http1ServerCapFault("lingerBytes", config.lingerBytes, H1_ZERO, big),
461
+ // One cap at a time, the first fault answered: no list of the twelve
462
+ // answers is built, so a check that passes costs comparisons alone.
463
+ let fault: string = http1ServerCapFault("maxTarget", config.maxTarget, one, big)
464
+ if (fault.length === 0) {
465
+ fault = http1ServerCapFault("maxHeaderBytes", config.maxHeaderBytes, smallest, big)
466
+ }
467
+ if (fault.length === 0) {
468
+ fault = http1ServerCapFault("maxHeaders", config.maxHeaders, one, big)
469
+ }
470
+ if (fault.length === 0) {
471
+ fault = http1ServerCapFault("maxBody", config.maxBody, H1_ZERO, big)
472
+ }
473
+ if (fault.length === 0) {
474
+ fault = http1ServerCapFault("chunkSize", config.chunkSize, one, big)
475
+ }
476
+ if (fault.length === 0) {
477
+ fault = http1ServerCapFault("outputSize", config.outputSize, output, big)
478
+ }
479
+ if (fault.length === 0) {
480
+ fault = http1ServerCapFault("maxMessage", config.maxMessage, H1_ZERO, big - H1_FRAME_HEADER)
481
+ }
482
+ if (fault.length === 0) {
483
+ fault = http1ServerCapFault("idleTimeout", config.idleTimeout, one, big)
484
+ }
485
+ if (fault.length === 0) {
486
+ fault = http1ServerCapFault("headTimeout", config.headTimeout, one, big)
487
+ }
488
+ if (fault.length === 0) {
489
+ fault = http1ServerCapFault("lingerTimeout", config.lingerTimeout, one, big)
490
+ }
491
+ if (fault.length === 0) {
492
+ fault = http1ServerCapFault("lingerBytes", config.lingerBytes, H1_ZERO, big)
493
+ }
494
+ if (fault.length === 0) {
473
495
  // The buffers the peer's bytes fill grow by doubling and stop at 2^30
474
496
  // (`Http1Parser`'s input and head, `WsDecoder`'s input), so a cap whose
475
497
  // buffer could need more is refused here rather than reached by a peer:
@@ -477,19 +499,14 @@ const http1ServerConfigFault = (config: Http1Config): string => {
477
499
  // the input never holds more than the larger of the two lines or a chunk.
478
500
  // Each is at most 2^30 here, so the sum wraps only at 2^31, to a
479
501
  // negative the check refuses as it would the sum itself.
480
- http1ServerCapFault(
502
+ fault = http1ServerCapFault(
481
503
  "maxTarget + maxHeaderBytes",
482
504
  toI32(toI64(config.maxTarget) + toI64(config.maxHeaderBytes)),
483
505
  one,
484
506
  big - H1_LINE_OVERHEAD
485
- ),
486
- ]
487
- for (const fault of caps) {
488
- if (fault.length > 0) {
489
- return fault
490
- }
507
+ )
491
508
  }
492
- return ""
509
+ return fault
493
510
  }
494
511
 
495
512
  /** Panics unless every cap of `config` is one a connection can work with: a cap outside is the program's mistake. */
@@ -550,11 +567,49 @@ const http1ServerPast = (now: i64, since: i64, ms: i32): boolean =>
550
567
  now > since && toU64(now) - toU64(since) > toU64(ms) * toU64(1000000)
551
568
 
552
569
  /**
553
- * The `monotonicNanos` instant `ms` milliseconds after `at`. Worked in `u64`,
554
- * whose wrapping sum has the bits of the signed one: an instant is centuries
555
- * short of 2^63 and the timeout a checked cap, so neither wraps.
570
+ * The `monotonicNanos` instant `ms` milliseconds after `at`. An instant is
571
+ * centuries short of 2^63 and the timeout a checked cap, so the sum is exact;
572
+ * `http1ServerSum64` holds it at the `i64` range's ends only so that the
573
+ * bounds prover sees no sum that can overflow.
556
574
  */
557
- const http1ServerAfter = (at: i64, ms: i32): i64 => toI64(toU64(at) + toU64(ms) * toU64(1000000))
575
+ const http1ServerAfter = (at: i64, ms: i32): i64 => http1ServerSum64(at, toI64(ms) * 1000000)
576
+
577
+ /**
578
+ * `a + b`, held at the `i64` range's ends where the sum would leave it. Two
579
+ * values of opposite signs add in `i64`, as they cannot leave the range. Two
580
+ * of one sign add as magnitudes in `u64`, where they are below 2^64 and so
581
+ * exact: a sum of values at least 0 is the `u64` sum, and a sum of values
582
+ * below 0 is `-1 - ((-1 - a) + (-1 - b) + 1)`. Each magnitude is held below
583
+ * 2^63 before it is read back as an `i64`, so the read is exact too.
584
+ */
585
+ const http1ServerSum64 = (a: i64, b: i64): i64 => {
586
+ if (a >= 0) {
587
+ if (b < 0) {
588
+ return a + b
589
+ }
590
+ const sum: u64 = toU64(a) + toU64(b)
591
+ return toI64(http1ServerBelow2p63(sum))
592
+ }
593
+ if (b >= 0) {
594
+ return a + b
595
+ }
596
+ const past: u64 = toU64(-1 - a) + toU64(-1 - b) + toU64(1)
597
+ const k: i64 = toI64(http1ServerBelow2p63(past))
598
+ if (k < 0) {
599
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
600
+ return -1
601
+ }
602
+ return -1 - k
603
+ }
604
+
605
+ /** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
606
+ const http1ServerBelow2p63 = (x: u64): u64 => {
607
+ const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
608
+ if (x > most) {
609
+ return most
610
+ }
611
+ return x
612
+ }
558
613
 
559
614
  /** Copies `src[off .. off + n)` to `out` at `at`, which the caller has made sure holds it, and answers where it ends. */
560
615
  const http1ServerCopy = (out: u8[], at: i32, src: readonly u8[], off: i32, n: i32): i32 => {
@@ -776,6 +831,71 @@ export const http1ConfigChecked = (config: Http1Config): Result<Http1Config, str
776
831
  return fault.length > 0 ? Err(fault) : Ok(config)
777
832
  }
778
833
 
834
+ /** A copy of `config`, field for field: a snapshot whoever holds `config` cannot change. */
835
+ const http1ConfigCopy = (config: Http1Config): Http1Config => {
836
+ const kept: Http1Config = new Http1Config()
837
+ kept.maxTarget = config.maxTarget
838
+ kept.maxHeaderBytes = config.maxHeaderBytes
839
+ kept.maxHeaders = config.maxHeaders
840
+ kept.maxBody = config.maxBody
841
+ kept.chunkSize = config.chunkSize
842
+ kept.outputSize = config.outputSize
843
+ kept.maxMessage = config.maxMessage
844
+ kept.idleTimeout = config.idleTimeout
845
+ kept.headTimeout = config.headTimeout
846
+ kept.lingerTimeout = config.lingerTimeout
847
+ kept.lingerBytes = config.lingerBytes
848
+ kept.idleInput = config.idleInput
849
+ return kept
850
+ }
851
+
852
+ /**
853
+ * A connection under a copy of `config`, checked once, the twin of
854
+ * `Http1Connection`'s constructor that refuses a cap outside what the
855
+ * connection can work with as an `Err` rather than panicking. The connection
856
+ * keeps the copy, so a later change to `config` does not reach it.
857
+ *
858
+ * @param config the caps the connection is held to, copied and checked now
859
+ * @param date the `Date` its responses carry, shared with other connections; null makes it one of its own
860
+ * @returns the connection, or the first cap outside its range, named with the range it must lie in
861
+ */
862
+ export const http1ConnectionChecked = (
863
+ config: Http1Config,
864
+ date: HttpDate | null = null
865
+ ): Result<Http1Connection, string> => {
866
+ const kept: Http1Config = http1ConfigCopy(config)
867
+ const fault: string = http1ServerConfigFault(kept)
868
+ if (fault.length > 0) {
869
+ return Err(fault)
870
+ }
871
+ return Ok(new Http1Connection(kept, date))
872
+ }
873
+
874
+ /**
875
+ * A server pool under a copy of `config`, checked once, the twin of
876
+ * `Http1Server`'s constructor: a cap outside what a connection can work
877
+ * with is an `Err` here, where the constructor's pool would panic building
878
+ * its first connection. The pool keeps the copy, so a later change to
879
+ * `config` reaches none of its connections.
880
+ *
881
+ * @param config the caps every slot's connection is held to, copied and checked now
882
+ * @param listener the listening socket from `tcpListen`, which stays the program's to close
883
+ * @param size how many connections the pool serves at once; less than 1 counts as 1
884
+ * @returns the pool, or the first cap outside its range, named with the range it must lie in
885
+ */
886
+ export const http1ServerChecked = (
887
+ config: Http1Config,
888
+ listener: i32,
889
+ size: i32
890
+ ): Result<Http1Server, string> => {
891
+ const kept: Http1Config = http1ConfigCopy(config)
892
+ const fault: string = http1ServerConfigFault(kept)
893
+ if (fault.length > 0) {
894
+ return Err(fault)
895
+ }
896
+ return Ok(new Http1Server(kept, listener, size))
897
+ }
898
+
779
899
  /** The reason phrase a status is sent with; RFC 9112 §4 lets a client ignore it. */
780
900
  const http1ServerReason = (status: i32): string => {
781
901
  switch (status) {
@@ -1352,10 +1472,23 @@ export class Http1Connection {
1352
1472
  return 0
1353
1473
  }
1354
1474
  if (this.state === H1_S_WEBSOCKET) {
1355
- this.decoder().feed(buf, off, n)
1475
+ // `[off, off + n)` lies inside the window checked above, as `n <= len`;
1476
+ // spelled as `end - off` behind its test so the decoder's check is proven.
1477
+ const ws: WsDecoder = this.decoder()
1478
+ const end: i32 = toI32(toI64(off) + toI64(n))
1479
+ if (off < 0 || off > end || end > toI32(buf.length)) {
1480
+ this.abort()
1481
+ return 0
1482
+ }
1483
+ ws.feed(buf, off, end - off)
1356
1484
  } else {
1357
1485
  this.unpark()
1358
- this.parser.feed(buf, off, n)
1486
+ // In the window checked above, so `tryFeed` takes it; false would be a
1487
+ // window it refused, and the connection could not go on.
1488
+ if (!this.parser.tryFeed(buf, off, n)) {
1489
+ this.abort()
1490
+ return 0
1491
+ }
1359
1492
  }
1360
1493
  return n
1361
1494
  }
@@ -1601,7 +1734,17 @@ export class Http1Connection {
1601
1734
  const p: Http1Parser = this.parser
1602
1735
  const ws: WsDecoder = this.decoder()
1603
1736
  ws.reset()
1604
- ws.feed(p.buf, p.start, p.buffered())
1737
+ // What the parser holds past the head: its own buffer's window,
1738
+ // tested so the decoder's window check is proven. A parser that
1739
+ // broke it is a connection that cannot go on.
1740
+ const held: u8[] = p.buf
1741
+ const from: i32 = p.start
1742
+ const end: i32 = toI32(toI64(from) + toI64(p.buffered()))
1743
+ if (from < 0 || from > end || end > toI32(held.length)) {
1744
+ this.abort()
1745
+ return H1_ERROR
1746
+ }
1747
+ ws.feed(held, from, end - from)
1605
1748
  this.state = H1_S_WEBSOCKET
1606
1749
  return this.nextFrame()
1607
1750
  }
@@ -2790,7 +2933,9 @@ export class Http1Server {
2790
2933
  * @param size how many connections the pool serves at once; less than 1 counts as 1
2791
2934
  */
2792
2935
  constructor(config: Http1Config, listener: i32, size: i32) {
2793
- this.config = config
2936
+ // A copy: the caps each connection is built under are the ones given
2937
+ // here, whatever the caller sets on `config` later.
2938
+ this.config = http1ConfigCopy(config)
2794
2939
  this.listener = listener
2795
2940
  this.connections = []
2796
2941
  this.at = []
@@ -2810,11 +2955,21 @@ export class Http1Server {
2810
2955
  this.peer = new Array<u8>(H1_ADDRESS_SIZE)
2811
2956
  }
2812
2957
 
2813
- /** A new connection on this pool's outputs and `Date`, appended to `connections`. */
2814
- private build(): Http1Connection {
2815
- const conn: Http1Connection = new Http1Connection(this.config, this.date)
2958
+ /**
2959
+ * A new connection on this pool's outputs and `Date`, appended to
2960
+ * `connections`; null, building none, when the pool's caps are ones a
2961
+ * connection cannot work with.
2962
+ */
2963
+ private build(): Http1Connection | null {
2964
+ const config: Http1Config = this.config
2965
+ const date: HttpDate = this.date
2966
+ const fault: string = http1ServerConfigFault(config)
2967
+ if (fault.length > 0) {
2968
+ return null
2969
+ }
2970
+ const conn: Http1Connection = new Http1Connection(config, date)
2816
2971
  conn.pool = this.outputs
2817
- if (this.config.idleInput) {
2972
+ if (config.idleInput) {
2818
2973
  conn.inputs = this.inputs
2819
2974
  conn.heads = this.heads
2820
2975
  }
@@ -2885,6 +3040,23 @@ export class Http1Server {
2885
3040
  * @returns the slot's connection, which the pool keeps; read its events and write its response through it
2886
3041
  */
2887
3042
  connection(slot: i32): Http1Connection {
3043
+ const conn: Http1Connection | null = this.tryConnection(slot)
3044
+ // Null only for a free slot when no connection could be built: the
3045
+ // pool's caps are bad, which this constructor panics on.
3046
+ return conn !== null ? conn : new Http1Connection(this.config, this.date)
3047
+ }
3048
+
3049
+ /**
3050
+ * The connection `slot` holds, as `connection` answers it, or null where
3051
+ * `connection` would panic: the twin for a pool made by `new Http1Server`
3052
+ * whose caps no connection can work with (`http1ServerChecked` makes a
3053
+ * pool that never answers null). A free slot, or one out of range, names
3054
+ * the first connection built, as `connection`'s does.
3055
+ *
3056
+ * @param slot the slot `accept` answered
3057
+ * @returns the slot's connection, which the pool keeps; null when none could be built
3058
+ */
3059
+ tryConnection(slot: i32): Http1Connection | null {
2888
3060
  const conn: Http1Connection | null = this.attach(slot)
2889
3061
  if (conn !== null) {
2890
3062
  return conn
@@ -2972,7 +3144,11 @@ export class Http1Server {
2972
3144
  }
2973
3145
  const taken: Http1Connection | null = k >= 0 && k < toI32(connections.length) ? connections[k] : null
2974
3146
  // No spare: a new one, at the end of `connections`.
2975
- const conn: Http1Connection = taken !== null ? taken : this.build()
3147
+ const conn: Http1Connection | null = taken !== null ? taken : this.build()
3148
+ if (conn === null) {
3149
+ // Bad caps: no connection, so the slot reads as closed.
3150
+ return null
3151
+ }
2976
3152
  if (taken === null) {
2977
3153
  k = toI32(this.connections.length) - 1
2978
3154
  }
@@ -3072,9 +3248,9 @@ export class Http1Server {
3072
3248
  this.at[k] = -1
3073
3249
  this.rested[k] = this.clock()
3074
3250
  this.owner[k] = this.accepts
3075
- // A serial only keys a kept head to its peer: in u64, which wraps where
3076
- // an i64 would be a site, after more accepts than a server ever sees.
3077
- this.accepts = toI64(toU64(this.accepts) + toU64(1))
3251
+ // A serial only keys a kept head to its peer. It is held at 2^63 - 1
3252
+ // rather than overflowing, after more accepts than a server ever sees.
3253
+ this.accepts = http1ServerSum64(this.accepts, 1)
3078
3254
  // Once a connection, not a request, so a keep-alive request costs no
3079
3255
  // syscall: a path whose MSS moves after the handshake is not followed.
3080
3256
  const mss: i32 = netMss(fd)
@@ -3136,8 +3312,14 @@ export class Http1Server {
3136
3312
  }
3137
3313
  break
3138
3314
  }
3315
+ // `netRead` answers at most `want`; a count past the scratch is a
3316
+ // broken read, and the connection is aborted rather than fed it.
3317
+ if (n > toI32(scratch.length)) {
3318
+ conn.abort()
3319
+ break
3320
+ }
3139
3321
  moved = true
3140
- conn.feed(scratch, H1_ZERO, n)
3322
+ conn.feed(scratch, toI32(0), n)
3141
3323
  // A short read emptied the socket. The loop's readiness is
3142
3324
  // level-triggered, so more input, or the peer's close, wakes it again;
3143
3325
  // reading once more here would only answer -11, at the price of a
@@ -3699,7 +3881,7 @@ export class Http1TlsServer {
3699
3881
  * Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does. Answers the interest.
3700
3882
  *
3701
3883
  * @param slot the slot whose handshake waits for its signature
3702
- * @param key the certificate's P-256 private key, which stays the caller's to wipe
3884
+ * @param key the certificate's private key, P-256 or RSA as `signatureScheme` says, which stays the caller's to wipe
3703
3885
  * @returns the TLS interest bits the slot now waits for (`H1_WANT_` values), with `TLS_RECORD_DONE` once it should be closed
3704
3886
  */
3705
3887
  signP256(slot: i32, key: Secret<u8[]>): i32 {
@@ -421,8 +421,14 @@ export class Http2CleartextServer {
421
421
  this.close(slot)
422
422
  return false
423
423
  }
424
- // `netRead` answers at most the 24 - `have` bytes it was asked for, so the unsigned sum is at most 24.
425
- const end: i32 = toI32(toU32(have) + toU32(n))
424
+ // `netRead` answers at most the 24 - `have` bytes it was asked for, so the sum is at most 24. The test
425
+ // restates that with literals (`H2C_PREFACE_SIZE` is 24) for the bounds proof; a read that broke it
426
+ // would be a fault, and ends the connection as a failed read does.
427
+ if (have > 24 || n > 24) {
428
+ this.close(slot)
429
+ return false
430
+ }
431
+ const end: i32 = have + n
426
432
  for (let k: i32 = have; k < end; k++) {
427
433
  if (k < 0 || k >= toI32(preface.length) || k >= toI32(want.length)) {
428
434
  break
@@ -542,8 +548,8 @@ export class Http2CleartextServer {
542
548
  if (start < 0 || end < start) {
543
549
  break
544
550
  }
545
- // Both ends checked just above, so the unsigned difference is the span's length and does not wrap.
546
- const n: i32 = netWrite(s.fd, conn.output, start, toI32(toU32(end) - toU32(start)))
551
+ // Both ends checked just above, so the difference is the span's length and cannot overflow.
552
+ const n: i32 = netWrite(s.fd, conn.output, start, end - start)
547
553
  if (n === H2C_AGAIN) {
548
554
  break
549
555
  }
@@ -395,8 +395,8 @@ const http2FramePriority = (frame: Http2Frame, buf: u8[], at: i32): i32 => {
395
395
  (toI32(buf[at + 1]) << 16) |
396
396
  (toI32(buf[at + 2]) << 8) |
397
397
  toI32(buf[at + 3])
398
- // A byte is 0 to 255, so the weight is 1 to 256.
399
- frame.weight = toI32(toU32(buf[at + 4]) + toU32(1))
398
+ // A byte is 0 to 255, so the weight is 1 to 256 and the sum cannot overflow.
399
+ frame.weight = toI32(buf[at + 4]) + 1
400
400
  if (frame.dependency === frame.streamId) {
401
401
  frame.streamError = H2_PROTOCOL_ERROR
402
402
  }
@@ -304,7 +304,7 @@ export class Http2TlsServer {
304
304
  * Signs `slot`'s CertificateVerify with `key`, as `TlsTcpServer.signP256` does.
305
305
  *
306
306
  * @param slot The slot whose interest says `TLS_RECORD_SIGN`.
307
- * @param key The certificate's P-256 private key; borrowed for the call, still the caller's to wipe.
307
+ * @param key The certificate's private key, P-256 or RSA as `signatureScheme` says; borrowed for the call, still the caller's to wipe.
308
308
  * @returns The slot's interest, as `flush` answers it; `TLS_RECORD_DONE` for a free slot.
309
309
  */
310
310
  signP256(slot: i32, key: Secret<u8[]>): i32 {
@@ -399,8 +399,8 @@ export class Http2TlsServer {
399
399
  if (start < 0 || end < start) {
400
400
  break
401
401
  }
402
- // `start` is 0 or more and `end` at least `start`, so the unsigned difference is the held byte count.
403
- const n: i32 = this.tls.write(slot, conn.output, start, toI32(toU32(end) - toU32(start)))
402
+ // `start` is 0 or more and `end` at least `start`, so the difference is the held byte count and fits.
403
+ const n: i32 = this.tls.write(slot, conn.output, start, end - start)
404
404
  if (n <= 0) {
405
405
  break
406
406
  }
package/std/net/http2.ts CHANGED
@@ -457,27 +457,81 @@ const http2CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void =>
457
457
  }
458
458
 
459
459
  /**
460
- * How long before `now` the instant `since` was, both `monotonicNanos` readings. The difference is taken
461
- * modulo 2^64: two readings of one monotonic clock are under 2^63 apart, so it is the exact one.
460
+ * How long before `now` the instant `since` was, both `monotonicNanos` readings. Two readings of one
461
+ * monotonic clock are under 2^63 apart, so `http2Minus` gives the exact difference.
462
462
  */
463
- const http2Since = (now: i64, since: i64): i64 => toI64(toU64(now) - toU64(since))
463
+ const http2Since = (now: i64, since: i64): i64 => http2Minus(now, since)
464
464
 
465
465
  /**
466
- * The `monotonicNanos` instant `ms` milliseconds after `since`. The product is under 2^52 for an `i32`
467
- * `ms`; the sum is taken modulo 2^64, and a reading plus it stays far below 2^63.
466
+ * The `monotonicNanos` instant `ms` milliseconds after `since`. The product is under 2^52 in size for an
467
+ * `i32` `ms`, and a reading plus it stays far below 2^63, so `http2Plus` gives the exact sum.
468
468
  */
469
- const http2After = (since: i64, ms: i32): i64 => toI64(toU64(since) + toU64(toI64(ms) * toI64(1000000)))
469
+ const http2After = (since: i64, ms: i32): i64 => http2Plus(since, toI64(ms) * 1000000)
470
470
 
471
471
  /**
472
- * `a + b` for flow-control figures: a window, the bytes owed to one, or the change a SETTINGS makes to
473
- * every window. Each stays within 2^33 either side of 0, since a window that passes 2^31 - 1 fails its
474
- * stream or the connection and no frame moves one by more than 2^31 - 1, so the sum taken modulo 2^64
475
- * is the exact one.
472
+ * `a + b` for flow-control figures, clock readings and byte counts, held at the `i64` range's ends where
473
+ * the sum would leave it. None of them does: a flow-control figure (a window, the bytes owed to one, or
474
+ * the change a SETTINGS makes to every window) stays within 2^33 either side of 0, since a window that
475
+ * passes 2^31 - 1 fails its stream or the connection and no frame moves one by more than 2^31 - 1; a
476
+ * clock is centuries short of 2^63; and 2^63 bytes cannot arrive. Two values of opposite signs add in
477
+ * `i64`, as they cannot leave the range. Two of one sign add as magnitudes in `u64`, where they are below
478
+ * 2^64 and so exact: a sum of values at least 0 is the `u64` sum, and a sum of values below 0 is
479
+ * `-1 - ((-1 - a) + (-1 - b) + 1)`. Each magnitude is held below 2^63 before it is read back as an
480
+ * `i64`, so the read is exact too, and the bounds prover sees no sum that can overflow.
476
481
  */
477
- const http2Plus = (a: i64, b: i64): i64 => toI64(toU64(a) + toU64(b))
482
+ const http2Plus = (a: i64, b: i64): i64 => {
483
+ if (a >= 0) {
484
+ if (b < 0) {
485
+ return a + b
486
+ }
487
+ const sum: u64 = toU64(a) + toU64(b)
488
+ return toI64(http2Below2p63(sum))
489
+ }
490
+ if (b >= 0) {
491
+ return a + b
492
+ }
493
+ const past: u64 = toU64(-1 - a) + toU64(-1 - b) + toU64(1)
494
+ const k: i64 = toI64(http2Below2p63(past))
495
+ if (k < 0) {
496
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
497
+ return -1
498
+ }
499
+ return -1 - k
500
+ }
478
501
 
479
- /** `a - b` for flow-control figures, exact for the reason `http2Plus` gives. */
480
- const http2Minus = (a: i64, b: i64): i64 => toI64(toU64(a) - toU64(b))
502
+ /**
503
+ * `a - b` for the figures `http2Plus` takes, held at the range's ends the same way. Two values of one
504
+ * sign subtract in `i64`. Otherwise the difference is a sum of magnitudes: `a + (-1 - b) + 1` for `a` at
505
+ * least 0, and `-1 - ((-1 - a) + b)` for `a` below 0.
506
+ */
507
+ const http2Minus = (a: i64, b: i64): i64 => {
508
+ if (a >= 0) {
509
+ if (b >= 0) {
510
+ return a - b
511
+ }
512
+ const sum: u64 = toU64(a) + toU64(-1 - b) + toU64(1)
513
+ return toI64(http2Below2p63(sum))
514
+ }
515
+ if (b < 0) {
516
+ return a - b
517
+ }
518
+ const past: u64 = toU64(-1 - a) + toU64(b)
519
+ const k: i64 = toI64(http2Below2p63(past))
520
+ if (k < 0) {
521
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
522
+ return -1
523
+ }
524
+ return -1 - k
525
+ }
526
+
527
+ /** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
528
+ const http2Below2p63 = (x: u64): u64 => {
529
+ const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
530
+ if (x > most) {
531
+ return most
532
+ }
533
+ return x
534
+ }
481
535
 
482
536
  /** Whether one of `names` is `date` in any ASCII case (RFC 9110 §5.1). */
483
537
  const http2NamesHoldDate = (names: u8[][]): boolean => {
@@ -1486,8 +1540,8 @@ export class Http2Connection {
1486
1540
  return this.discarded(id) ? H2_NEED_MORE : this.refuseStream(id, H2_STREAM_CLOSED)
1487
1541
  }
1488
1542
  const end: boolean = f.has(H2_FLAG_END_STREAM)
1489
- // A count of body bytes: 2^63 of them cannot arrive, so the sum modulo 2^64 is the exact one.
1490
- const received: i64 = toI64(toU64(s.received) + toU64(toI64(f.contentLength)))
1543
+ // A count of body bytes: 2^63 of them cannot arrive, so `http2Plus` gives the exact sum.
1544
+ const received: i64 = http2Plus(s.received, toI64(f.contentLength))
1491
1545
  const declared: boolean = s.contentLength >= toI64(0)
1492
1546
  let error: i32 = H2_NO_ERROR
1493
1547
  if (s.remoteEnded) {
@@ -347,26 +347,20 @@ export class Http3Settings {
347
347
 
348
348
  /**
349
349
  * Panics unless `[off, off + len)` lies inside `buf`: a window outside its buffer is the program's mistake.
350
+ * A window guard (`docs/LANGUAGE.md`, `window`), so a caller that proves the window in range removes the
351
+ * panic site, and a function whose first statement hands it its own window passes that proof to its callers.
350
352
  * @param what The caller's name, which starts the panic message.
351
353
  * @param buf The buffer the window is meant to lie in.
352
354
  * @param off The window's first byte offset; negative panics.
353
355
  * @param len The window's length in bytes; negative panics, and so does an end past `buf.length`.
354
356
  */
355
357
  export const h3CheckWindow = (what: string, buf: u8[], off: i32, len: i32): void => {
356
- if (off < 0 || len < 0 || toI64(off) + toI64(len) > toI64(buf.length)) {
357
- panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${buf.length} bytes`)
358
+ const size: i32 = toI32(buf.length)
359
+ if (off < 0 || len < 0 || off > size || len > size - off) {
360
+ panic(`${what}: the window [${off}, ${off} + ${len}) is outside a buffer of ${size} bytes`)
358
361
  }
359
362
  }
360
363
 
361
- /**
362
- * `off + len`, after `h3CheckWindow` has panicked unless the window lies inside `buf`: the sum is then at
363
- * most `buf.length`, so the `i64` it is taken in always narrows back exactly.
364
- */
365
- const h3WindowEnd = (what: string, buf: u8[], off: i32, len: i32): i32 => {
366
- h3CheckWindow(what, buf, off, len)
367
- return toI32(toI64(off) + toI64(len))
368
- }
369
-
370
364
  /**
371
365
  * The offset just past the varint at `buf[at]`, or -1 when it does not end at or before `end` (or `at` is
372
366
  * outside `buf`). Taken in `i64`, so it narrows back exactly whenever it is at most `end`.
@@ -452,7 +446,8 @@ export const h3FrameHeaderSize = (type: i64, length: i64): i32 => {
452
446
  * @returns The header's size, 2 to 16 bytes, or 0 when the window ends before both varints are whole.
453
447
  */
454
448
  export const h3ReadFrameHeader = (h: Http3FrameHeader, buf: u8[], off: i32, len: i32): i32 => {
455
- const end: i32 = h3WindowEnd("h3ReadFrameHeader", buf, off, len)
449
+ h3CheckWindow("h3ReadFrameHeader", buf, off, len)
450
+ const end: i32 = toI32(toI64(off) + toI64(len))
456
451
  const type: i64 = quicVarintRead(buf, off, end)
457
452
  const at: i32 = h3VarintEnd(buf, off, end)
458
453
  if (type < 0 || at < 0) {
@@ -519,7 +514,8 @@ export const h3Greased = (value: i64): boolean => value >= 0x21 && value % H3_GR
519
514
  * error.
520
515
  */
521
516
  export const h3ReadSettings = (s: Http3Settings, buf: u8[], off: i32, len: i32): i64 => {
522
- const end: i32 = h3WindowEnd("h3ReadSettings", buf, off, len)
517
+ h3CheckWindow("h3ReadSettings", buf, off, len)
518
+ const end: i32 = toI32(toI64(off) + toI64(len))
523
519
  s.reset()
524
520
  let at: i32 = off
525
521
  while (at < end) {
@@ -624,7 +620,8 @@ export const h3PutIdFrame = (buf: u8[], at: i32, end: i32, type: i64, id: i64):
624
620
  * bytes after it.
625
621
  */
626
622
  export const h3ReadIdPayload = (buf: u8[], off: i32, len: i32): i64 => {
627
- const end: i32 = h3WindowEnd("h3ReadIdPayload", buf, off, len)
623
+ h3CheckWindow("h3ReadIdPayload", buf, off, len)
624
+ const end: i32 = toI32(toI64(off) + toI64(len))
628
625
  const id: i64 = quicVarintRead(buf, off, end)
629
626
  return id >= 0 && h3VarintEnd(buf, off, end) === end ? id : H3_FRAME_NONE
630
627
  }
@@ -12,7 +12,7 @@
12
12
  * import { QuicServerConfig } from "nish/net/quic";
13
13
  * import { QUIC_LISTENER_ENTROPY_SIZE } from "nish/net/quic-listener";
14
14
  *
15
- * // `quicConfig.alpn` is ["h3"]; `privateKey` is the P-256 key of its certificate.
15
+ * // `quicConfig.alpn` is ["h3"]; `privateKey` is its certificate's key, P-256 or RSA.
16
16
  * const run = (quicConfig: QuicServerConfig, privateKey: u8[], rounds: i32): void => {
17
17
  * const fd: i32 = udpBind("0.0.0.0", 4433, 0);
18
18
  * const listenerEntropy: u8[] = new Array<u8>(QUIC_LISTENER_ENTROPY_SIZE);
@@ -149,7 +149,7 @@ import {
149
149
  quicListenerFlightTime,
150
150
  quicListenerTakeFlight,
151
151
  } from "nish/net/quic-listener"
152
- import { TlsP256Signer } from "nish/net/tls"
152
+ import { TlsKeySigner } from "nish/net/tls"
153
153
 
154
154
  /** The timer wheel's buckets, one millisecond each: its horizon. */
155
155
  export const H3_SERVER_WHEEL: i32 = 2048
@@ -749,7 +749,7 @@ export class Http3Server {
749
749
  entropy: u8[]
750
750
  private flight: QuicFlight
751
751
  /** Signs every slot's CertificateVerify in `serve`, one for the server, keeping nothing per signature. */
752
- private signer: TlsP256Signer
752
+ private signer: TlsKeySigner
753
753
  /** Each slot's connection-ID index signature, so it is refiled only when its IDs changed. */
754
754
  private signatures: i64[]
755
755
  private state: i32[]
@@ -886,7 +886,7 @@ export class Http3Server {
886
886
  this.from = new Array<u8>(H3_SERVER_ADDRESS)
887
887
  this.meta = [H3_SERVER_ZERO, H3_SERVER_ZERO]
888
888
  this.flight = new QuicFlight()
889
- this.signer = new TlsP256Signer()
889
+ this.signer = new TlsKeySigner(quicConfig.signatureScheme)
890
890
  this.signatures = http3ServerZeroTimes(size)
891
891
  this.state = http3ServerFilled(size, H3_SERVER_ZERO)
892
892
  this.readyRing = http3ServerFilled(size, H3_SERVER_ZERO)
@@ -1092,7 +1092,7 @@ export class Http3Server {
1092
1092
  /**
1093
1093
  * Reads every datagram waiting on the socket, in at most
1094
1094
  * `H3_SERVER_RECEIVE_BURST` receives, and hands each to its slot or the listener,
1095
- * signing a handshake's CertificateVerify with `key` (ECDSA P-256) when one
1095
+ * signing a handshake's CertificateVerify with `key` (in `signatureScheme`) when one
1096
1096
  * asks. A GRO receive is cut into its datagrams in place. Answers how many
1097
1097
  * datagrams it read.
1098
1098
  * @param now The current millisecond on the caller's monotonic clock.
@@ -1102,18 +1102,15 @@ export class Http3Server {
1102
1102
  */
1103
1103
  receive(now: i64, key: Secret<u8[]>): i32 {
1104
1104
  let count: i32 = 0
1105
+ // The whole of `rx`, which is `H3_SERVER_RECEIVE_SIZE` bytes: read through
1106
+ // a local so the bounds prover sees the window is its own length.
1107
+ const rx: u8[] = this.rx
1105
1108
  for (let burst: i32 = 0; burst < H3_SERVER_RECEIVE_BURST; burst++) {
1106
- const n: i32 = udpRecvFrom(
1107
- this.fd,
1108
- this.rx,
1109
- H3_SERVER_ZERO,
1110
- H3_SERVER_RECEIVE_SIZE,
1111
- this.from,
1112
- this.meta
1113
- )
1109
+ const n: i32 = udpRecvFrom(this.fd, rx, 0, toI32(rx.length), this.from, this.meta)
1114
1110
  const meta: i32[] = this.meta
1115
1111
  // Past the first term, never taken: `udpRecvFrom` reads at most the
1116
- // `H3_SERVER_RECEIVE_SIZE` it is given, and `meta` is the constructor's two words.
1112
+ // length of `rx` it is given, `H3_SERVER_RECEIVE_SIZE`, and `meta` is the
1113
+ // constructor's two words.
1117
1114
  if (n < 0 || n > 65536 || toI32(meta.length) < 2) {
1118
1115
  return count
1119
1116
  }
@@ -1124,7 +1121,7 @@ export class Http3Server {
1124
1121
  while (at >= 0 && at < n && segment > 0) {
1125
1122
  const left: i32 = n - at
1126
1123
  const length: i32 = left < segment ? left : segment
1127
- this.datagram(this.rx, at, length, this.from, now, key)
1124
+ this.datagram(rx, at, length, this.from, now, key)
1128
1125
  count = h3Count(count)
1129
1126
  if (length < left) {
1130
1127
  at = at + length