nish 0.44.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 (111) hide show
  1. package/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/package.json +5 -5
  4. package/runtime/core/numbers.mjs +3 -1
  5. package/runtime/core/strings.mjs +90 -38
  6. package/runtime/core.d.ts +8 -0
  7. package/runtime/core.mjs +16 -0
  8. package/runtime/nish.d.ts +47 -5
  9. package/runtime/nish.mjs +4 -0
  10. package/std/README.md +3 -3
  11. package/std/array.ts +10 -2
  12. package/std/bytes.ts +239 -26
  13. package/std/cli-args.ts +1072 -606
  14. package/std/cli-flags.ts +180 -98
  15. package/std/cli.ts +21 -13
  16. package/std/collections.ts +245 -127
  17. package/std/compress/checksum.ts +32 -37
  18. package/std/compress/deflate.ts +1102 -542
  19. package/std/compress/gzip.ts +290 -145
  20. package/std/compress/zlib.ts +57 -21
  21. package/std/crypto/aes.ts +153 -75
  22. package/std/crypto/base64url.ts +84 -61
  23. package/std/crypto/bcrypt-batch.ts +164 -90
  24. package/std/crypto/bcrypt.ts +185 -107
  25. package/std/crypto/chacha20poly1305.ts +84 -68
  26. package/std/crypto/ct.ts +9 -7
  27. package/std/crypto/ed25519.ts +64 -27
  28. package/std/crypto/hkdf.ts +105 -63
  29. package/std/crypto/hmac.ts +95 -89
  30. package/std/crypto/jwt.ts +248 -126
  31. package/std/crypto/p256.ts +758 -444
  32. package/std/crypto/p384.ts +20 -13
  33. package/std/crypto/pki-roots.ts +95 -38
  34. package/std/crypto/pki.ts +164 -60
  35. package/std/crypto/rsa.ts +266 -223
  36. package/std/crypto/scram.ts +3 -4
  37. package/std/crypto/sha1.ts +107 -65
  38. package/std/crypto/sha256.ts +502 -422
  39. package/std/crypto/sha512.ts +204 -135
  40. package/std/crypto/x509-random.ts +9 -3
  41. package/std/crypto/x509.ts +331 -171
  42. package/std/json-equal.ts +35 -19
  43. package/std/json-format.ts +215 -108
  44. package/std/json-number.ts +126 -72
  45. package/std/json-pointer.ts +42 -13
  46. package/std/json-tokenizer.ts +498 -307
  47. package/std/json-typed.ts +115 -36
  48. package/std/json-value.ts +341 -152
  49. package/std/json.ts +300 -158
  50. package/std/net/buffer-pool.ts +17 -7
  51. package/std/net/dns-config.ts +427 -236
  52. package/std/net/dns-message.ts +114 -47
  53. package/std/net/dns-resolver.ts +736 -237
  54. package/std/net/dns.ts +205 -52
  55. package/std/net/fetch.ts +320 -184
  56. package/std/net/grpc.ts +948 -0
  57. package/std/net/handler.ts +1275 -217
  58. package/std/net/hpack.ts +395 -180
  59. package/std/net/http-compress.ts +502 -65
  60. package/std/net/http-fields.ts +85 -49
  61. package/std/net/http-server.ts +1764 -602
  62. package/std/net/http1-client-codec.ts +372 -161
  63. package/std/net/http1-codec.ts +665 -331
  64. package/std/net/http1-server.ts +660 -278
  65. package/std/net/http2-cleartext.ts +165 -95
  66. package/std/net/http2-frame.ts +232 -89
  67. package/std/net/http2-tls.ts +42 -12
  68. package/std/net/http2.ts +327 -101
  69. package/std/net/http3-frame.ts +68 -33
  70. package/std/net/http3-server.ts +647 -192
  71. package/std/net/http3.ts +667 -189
  72. package/std/net/oidc.ts +63 -24
  73. package/std/net/outbound.ts +207 -142
  74. package/std/net/postgres-async.ts +1165 -443
  75. package/std/net/postgres-carrier.ts +575 -372
  76. package/std/net/postgres.ts +1459 -559
  77. package/std/net/prefork.ts +254 -98
  78. package/std/net/protobuf.ts +1017 -0
  79. package/std/net/qpack.ts +143 -92
  80. package/std/net/quic-conn-ack.ts +50 -18
  81. package/std/net/quic-conn-cid.ts +66 -26
  82. package/std/net/quic-conn-params.ts +13 -7
  83. package/std/net/quic-datagram.ts +90 -27
  84. package/std/net/quic-frame.ts +225 -124
  85. package/std/net/quic-listener.ts +197 -89
  86. package/std/net/quic-packet.ts +289 -163
  87. package/std/net/quic-recovery.ts +279 -128
  88. package/std/net/quic-stream.ts +466 -167
  89. package/std/net/quic.ts +568 -257
  90. package/std/net/router.ts +450 -99
  91. package/std/net/serve.ts +651 -232
  92. package/std/net/static.ts +1458 -0
  93. package/std/net/tls/client.ts +140 -45
  94. package/std/net/tls/codec.ts +510 -260
  95. package/std/net/tls/record-client.ts +268 -76
  96. package/std/net/tls/record-server.ts +236 -79
  97. package/std/net/tls/record.ts +173 -47
  98. package/std/net/tls/schedule.ts +58 -39
  99. package/std/net/tls/ticket.ts +295 -103
  100. package/std/net/tls/verify.ts +1 -1
  101. package/std/net/tls-tcp.ts +218 -75
  102. package/std/net/tls.ts +260 -152
  103. package/std/net/upstream.ts +283 -86
  104. package/std/net/url.ts +277 -102
  105. package/std/net/websocket.ts +370 -186
  106. package/std/net/webtransport.ts +755 -436
  107. package/std/regex.ts +1659 -829
  108. package/std/sync/ring.ts +320 -156
  109. package/std/sync/shm.ts +53 -6
  110. package/std/text.ts +205 -124
  111. package/std/threads.ts +22 -2
@@ -133,7 +133,6 @@ import {
133
133
  QUIC_CONN_ENTROPY_SIZE,
134
134
  QUIC_CONN_LOCAL_CIDS,
135
135
  QUIC_CONN_MAX_STREAM_DATA,
136
- QUIC_CONN_MAX_STREAMS,
137
136
  QuicConnection,
138
137
  QuicServerConfig,
139
138
  } from "nish/net/quic"
@@ -175,9 +174,21 @@ const H3_SERVER_UNSUPPORTED: i32 = -95
175
174
  /** The largest datagram read: a GRO receive of up to 64 KB. */
176
175
  const H3_SERVER_RECEIVE_SIZE: i32 = 65536
177
176
 
177
+ /** The bytes of the largest flight `flush` writes at once: `QUIC_LISTENER_FLIGHT_MAX` datagrams. */
178
+ const H3_SERVER_FLIGHT_BYTES: i32 = QUIC_LISTENER_FLIGHT_MAX * QUIC_CONN_DATAGRAM_SIZE
179
+
178
180
  /** Index entries per slot: its local connection IDs, then the client's original DCID. */
179
181
  const H3_SERVER_KEYS: i32 = QUIC_CONN_LOCAL_CIDS + 1
180
182
 
183
+ /** The ID bytes an index keeps per slot: its entries, `QUIC_MAX_CID_LENGTH` bytes each. */
184
+ const H3_SERVER_SLOT_KEY_BYTES: i32 = H3_SERVER_KEYS * QUIC_MAX_CID_LENGTH
185
+
186
+ /** The most slots an index files: past it, one slot's ID bytes would end beyond an `i32` offset. */
187
+ const H3_SERVER_SLOT_LIMIT: i32 = 21474836
188
+
189
+ /** The index's largest table, in places: 2^30, more than twice `H3_SERVER_SLOT_LIMIT`'s entries. */
190
+ const H3_SERVER_TABLE_LIMIT: i32 = 1073741824
191
+
181
192
  /** Typed constants, since a bare literal is an `f64` under `--number-mode f64`. */
182
193
  const H3_SERVER_ZERO: i32 = 0
183
194
  const H3_SERVER_NONE: i64 = -1
@@ -190,8 +201,66 @@ const H3_SERVER_MASK: i64 = 0x7fffffffffff
190
201
  const H3_SERVER_FREE: i32 = 0
191
202
  const H3_SERVER_BUSY: i32 = 1
192
203
 
193
- /** The bucket of `time` on the wheel. */
194
- const http3ServerBucket = (time: i64): i32 => toI32(time % toI64(H3_SERVER_WHEEL))
204
+ /**
205
+ * The bucket of `time` on the wheel: 2048 is `H3_SERVER_WHEEL`, written out
206
+ * so the prover knows the divisor. Negative for a negative time not a multiple of it.
207
+ */
208
+ const http3ServerBucket = (time: i64): i32 => toI32(time % toI64(2048))
209
+
210
+ /** The words of the wheel's bitmap: `H3_SERVER_WHEEL / 64`. */
211
+ const H3_SERVER_WHEEL_WORDS: i32 = 32
212
+
213
+ /**
214
+ * `count` copies of `value`, built by `push`, so a negative `count` gives an
215
+ * empty array and no length is unchecked.
216
+ */
217
+ const http3ServerFilled = (count: i32, value: i32): i32[] => {
218
+ const out: i32[] = []
219
+ let i: i32 = 0
220
+ while (i < count) {
221
+ out.push(value)
222
+ i = i + 1
223
+ }
224
+ return out
225
+ }
226
+
227
+ /** `count` zero times, built as `http3ServerFilled` builds. */
228
+ const http3ServerZeroTimes = (count: i32): i64[] => {
229
+ const out: i64[] = []
230
+ let i: i32 = 0
231
+ while (i < count) {
232
+ out.push(toI64(0))
233
+ i = i + 1
234
+ }
235
+ return out
236
+ }
237
+
238
+ /**
239
+ * Milliseconds from `now` to `from + offset`, 0 when that is at or before
240
+ * `now`; a wait past the `i32` range keeps its low 32 bits, as `toI32` gives.
241
+ * The sum is never formed, since it can pass the `i64` range.
242
+ * @param from A millisecond; a negative one only with an `offset` of 0.
243
+ * @param offset Milliseconds after `from`, 0 to 2047; one outside gives -1.
244
+ * @param now The current millisecond.
245
+ * @returns The wait, or -1 for an `offset` out of range.
246
+ */
247
+ const http3ServerWait = (from: i64, offset: i32, now: i64): i32 => {
248
+ if (offset < 0 || offset >= H3_SERVER_WHEEL) {
249
+ // Never taken: `next` passes an offset below `H3_SERVER_WHEEL`.
250
+ return -1
251
+ }
252
+ let due: boolean = false
253
+ if (from < 0) {
254
+ due = from + toI64(offset) <= now
255
+ } else if (now >= 0) {
256
+ due = from - now <= toI64(0 - offset)
257
+ }
258
+ if (due) {
259
+ return H3_SERVER_ZERO
260
+ }
261
+ // The low 32 bits of `from + offset - now`, from the low 32 bits of each.
262
+ return toI32(toI64(toI32(from)) - toI64(toI32(now)) + toI64(offset))
263
+ }
195
264
 
196
265
  /**
197
266
  * The connection IDs of every slot, by hash: an open-addressed table keyed
@@ -211,18 +280,37 @@ export class Http3CidIndex {
211
280
 
212
281
  /**
213
282
  * An empty index for `slots` slots.
214
- * @param slots How many slots it files IDs for, each `QUIC_CONN_LOCAL_CIDS + 1` entries.
283
+ * @param slots How many slots it files IDs for, each `QUIC_CONN_LOCAL_CIDS + 1` entries; 0 or fewer
284
+ * files none.
215
285
  * @param salt The hash's key: its first eight bytes are copied (zeros past a shorter one); not kept.
216
286
  */
217
287
  constructor(slots: i32, salt: u8[]) {
218
- const entries: i32 = slots * H3_SERVER_KEYS
219
- let size: i32 = 8
220
- while (size < entries * 2) {
221
- size = size * 2
222
- }
223
- this.table = new Array<i32>(size)
224
- this.keys = new Array<u8>(entries * QUIC_MAX_CID_LENGTH)
225
- this.lengths = new Array<i32>(entries)
288
+ // Each table is built by push, one after another, so it grows at the
289
+ // top of the arena; seeded with one slot's worth, its capacity is exact
290
+ // at a power-of-two slot count.
291
+ const lengths: i32[] = slots > 0 ? new Array<i32>(H3_SERVER_KEYS) : []
292
+ for (let s: i32 = 1; s < slots && s < H3_SERVER_SLOT_LIMIT; s++) {
293
+ for (let j: i32 = 0; j < H3_SERVER_KEYS; j++) {
294
+ lengths.push(0)
295
+ }
296
+ }
297
+ const keys: u8[] = slots > 0 ? new Array<u8>(H3_SERVER_SLOT_KEY_BYTES) : []
298
+ for (let s: i32 = 1; s < slots && s < H3_SERVER_SLOT_LIMIT; s++) {
299
+ for (let k: i32 = 0; k < H3_SERVER_SLOT_KEY_BYTES; k++) {
300
+ keys.push(0)
301
+ }
302
+ }
303
+ const entries: i32 = toI32(lengths.length)
304
+ const table: i32[] = new Array<i32>(8)
305
+ while (toI32(table.length) < H3_SERVER_TABLE_LIMIT && toI32(table.length) / 2 < entries) {
306
+ const size: i32 = toI32(table.length)
307
+ for (let h: i32 = 0; h < size; h++) {
308
+ table.push(0)
309
+ }
310
+ }
311
+ this.table = table
312
+ this.keys = keys
313
+ this.lengths = lengths
226
314
  this.salt = new Array<u8>(8)
227
315
  for (let k: i32 = 0; k < 8 && k < toI32(salt.length); k++) {
228
316
  this.salt[k] = salt[k]
@@ -231,11 +319,19 @@ export class Http3CidIndex {
231
319
 
232
320
  /** The bucket the ID `buf[at .. at + length)` starts its probe at. */
233
321
  private bucket(buf: u8[], at: i32, length: i32): i32 {
322
+ const salt: u8[] = this.salt
234
323
  let h: i64 = toI64(length)
235
- for (let k: i32 = 0; k < length && at + k >= 0 && at + k < toI32(buf.length); k++) {
324
+ // `i` is `at + k`, the byte read.
325
+ let i: i32 = at
326
+ for (let k: i32 = 0; k < length && i >= 0 && i < toI32(buf.length); k++) {
236
327
  const j: i32 = k & 7
237
- const mixed: i64 = toI64(toI32(buf[at + k]) ^ toI32(this.salt[j]))
328
+ if (j >= toI32(salt.length)) {
329
+ // Never taken: the constructor gives `salt` eight bytes.
330
+ break
331
+ }
332
+ const mixed: i64 = toI64(toI32(buf[i]) ^ toI32(salt[j]))
238
333
  h = (((h << 7) & H3_SERVER_MASK_LOW) ^ (h >> 29) ^ mixed) & H3_SERVER_MASK
334
+ i++
239
335
  }
240
336
  h = h ^ (h >> 17) ^ (h >> 31)
241
337
  return toI32(h & toI64(toI32(this.table.length) - 1))
@@ -243,14 +339,21 @@ export class Http3CidIndex {
243
339
 
244
340
  /** Whether entry `e` holds the ID `buf[at .. at + length)`. */
245
341
  private holds(e: i32, buf: u8[], at: i32, length: i32): boolean {
246
- if (e < 0 || e >= toI32(this.lengths.length) || this.lengths[e] !== length) {
342
+ const keys: u8[] = this.keys
343
+ const lengths: i32[] = this.lengths
344
+ // 107374182 is `I32_MAX / QUIC_MAX_CID_LENGTH`, past `H3_SERVER_SLOT_LIMIT`'s entries.
345
+ if (e < 0 || e >= 107374182 || e >= toI32(lengths.length) || lengths[e] !== length) {
247
346
  return false
248
347
  }
249
- const base: i32 = e * QUIC_MAX_CID_LENGTH
348
+ // `p` and `i` are `e * QUIC_MAX_CID_LENGTH + k` and `at + k`, the bytes compared.
349
+ let p: i32 = e * QUIC_MAX_CID_LENGTH
350
+ let i: i32 = at
250
351
  for (let k: i32 = 0; k < length; k++) {
251
- if (at + k < 0 || at + k >= toI32(buf.length) || this.keys[base + k] !== buf[at + k]) {
352
+ if (i < 0 || i >= toI32(buf.length) || p < 0 || p >= toI32(keys.length) || keys[p] !== buf[i]) {
252
353
  return false
253
354
  }
355
+ p++
356
+ i++
254
357
  }
255
358
  return true
256
359
  }
@@ -266,10 +369,11 @@ export class Http3CidIndex {
266
369
  const size: i32 = toI32(this.table.length)
267
370
  let h: i32 = this.bucket(buf, at, length)
268
371
  for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe++) {
269
- const e: i32 = this.table[h] - 1
270
- if (e < 0) {
372
+ const filed: i32 = this.table[h]
373
+ if (filed <= 0) {
271
374
  return -1
272
375
  }
376
+ const e: i32 = filed - 1
273
377
  if (this.holds(e, buf, at, length)) {
274
378
  return e / H3_SERVER_KEYS
275
379
  }
@@ -288,17 +392,34 @@ export class Http3CidIndex {
288
392
  * @param length The ID's length in bytes, 1 to `QUIC_MAX_CID_LENGTH`; any other files nothing.
289
393
  */
290
394
  insert(e: i32, buf: u8[], at: i32, length: i32): void {
291
- if (length <= 0 || length > QUIC_MAX_CID_LENGTH || e < 0 || e >= toI32(this.lengths.length)) {
395
+ const keys: u8[] = this.keys
396
+ const lengths: i32[] = this.lengths
397
+ // 107374182 is `I32_MAX / QUIC_MAX_CID_LENGTH`, past `H3_SERVER_SLOT_LIMIT`'s entries.
398
+ if (
399
+ length <= 0 ||
400
+ length > QUIC_MAX_CID_LENGTH ||
401
+ e < 0 ||
402
+ e >= toI32(lengths.length) ||
403
+ e >= 107374182
404
+ ) {
292
405
  return
293
406
  }
294
407
  if (this.find(buf, at, length) >= 0) {
295
408
  return
296
409
  }
297
- const base: i32 = e * QUIC_MAX_CID_LENGTH
298
- for (let k: i32 = 0; k < length && at + k >= 0 && at + k < toI32(buf.length); k++) {
299
- this.keys[base + k] = buf[at + k]
410
+ // `p` and `i` are `e * QUIC_MAX_CID_LENGTH + k` and `at + k`, the bytes copied.
411
+ let p: i32 = e * QUIC_MAX_CID_LENGTH
412
+ let i: i32 = at
413
+ for (let k: i32 = 0; k < length && i >= 0 && i < toI32(buf.length); k++) {
414
+ if (p < 0 || p >= toI32(keys.length)) {
415
+ // Never taken: each entry has `QUIC_MAX_CID_LENGTH` bytes of `keys`.
416
+ return
417
+ }
418
+ keys[p] = buf[i]
419
+ p++
420
+ i++
300
421
  }
301
- this.lengths[e] = length
422
+ lengths[e] = length
302
423
  const size: i32 = toI32(this.table.length)
303
424
  let h: i32 = this.bucket(buf, at, length)
304
425
  for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe++) {
@@ -315,13 +436,14 @@ export class Http3CidIndex {
315
436
  * @param e The entry, numbered as `insert` numbers it; one out of range or not filed is ignored.
316
437
  */
317
438
  remove(e: i32): void {
318
- if (e < 0 || e >= toI32(this.lengths.length) || this.lengths[e] === 0) {
439
+ const lengths: i32[] = this.lengths
440
+ // 107374182 is `I32_MAX / QUIC_MAX_CID_LENGTH`, past `H3_SERVER_SLOT_LIMIT`'s entries.
441
+ if (e < 0 || e >= toI32(lengths.length) || e >= 107374182 || lengths[e] === 0) {
319
442
  return
320
443
  }
321
444
  const size: i32 = toI32(this.table.length)
322
445
  const mask: i32 = size - 1
323
- const base: i32 = e * QUIC_MAX_CID_LENGTH
324
- let h: i32 = this.bucket(this.keys, base, this.lengths[e])
446
+ let h: i32 = this.bucket(this.keys, e * QUIC_MAX_CID_LENGTH, lengths[e])
325
447
  let hole: i32 = -1
326
448
  for (let probe: i32 = 0; probe < size && h >= 0 && h < size; probe++) {
327
449
  if (this.table[h] === 0) {
@@ -333,17 +455,27 @@ export class Http3CidIndex {
333
455
  }
334
456
  h = (h + 1) & mask
335
457
  }
336
- this.lengths[e] = 0
337
- if (hole < 0) {
458
+ lengths[e] = 0
459
+ // `hole` is -1 or a place the probe found, below `size`.
460
+ if (hole < 0 || hole >= size) {
338
461
  return
339
462
  }
340
463
  let next: i32 = (hole + 1) & mask
341
464
  for (let probe: i32 = 0; probe < size && next >= 0 && next < size && hole >= 0 && hole < size; probe++) {
342
- const other: i32 = this.table[next] - 1
343
- if (other < 0) {
465
+ const filed: i32 = this.table[next]
466
+ if (filed <= 0) {
467
+ break
468
+ }
469
+ const other: i32 = filed - 1
470
+ if (other >= toI32(lengths.length) || other >= 107374182) {
471
+ // Never taken: only `insert` files an entry, and only one it numbers.
472
+ break
473
+ }
474
+ const home: i32 = this.bucket(this.keys, other * QUIC_MAX_CID_LENGTH, lengths[other])
475
+ if (home < 0 || home >= size) {
476
+ // Never taken: `bucket` answers a place of the table.
344
477
  break
345
478
  }
346
- const home: i32 = this.bucket(this.keys, other * QUIC_MAX_CID_LENGTH, this.lengths[other])
347
479
  // The entry may move into the hole when its home is not between the hole and it.
348
480
  if (((hole - home) & mask) < ((next - home) & mask)) {
349
481
  this.table[hole] = this.table[next]
@@ -377,16 +509,15 @@ export class Http3Wheel {
377
509
 
378
510
  /**
379
511
  * An empty wheel whose cursor is not started.
380
- * @param slots How many slots it files, 0 to `slots - 1`.
512
+ * @param slots How many slots it files, 0 to `slots - 1`; 0 or fewer files none.
381
513
  */
382
514
  constructor(slots: i32) {
383
515
  this.head = new Array<i32>(H3_SERVER_WHEEL)
384
- this.nextOf = new Array<i32>(slots)
385
- this.prevOf = new Array<i32>(slots)
386
- this.bucketOf = new Array<i32>(slots)
387
- this.bucketOf.fill(-1)
388
- this.dueAt = new Array<i64>(slots)
389
- this.bits = new Array<i64>(H3_SERVER_WHEEL / 64)
516
+ this.nextOf = http3ServerFilled(slots, H3_SERVER_ZERO)
517
+ this.prevOf = http3ServerFilled(slots, H3_SERVER_ZERO)
518
+ this.bucketOf = http3ServerFilled(slots, -1)
519
+ this.dueAt = http3ServerZeroTimes(slots)
520
+ this.bits = new Array<i64>(H3_SERVER_WHEEL_WORDS)
390
521
  }
391
522
 
392
523
  /** Marks bucket `b` filled or not. */
@@ -404,27 +535,41 @@ export class Http3Wheel {
404
535
  * @param slot The slot; one out of range or not filed is ignored.
405
536
  */
406
537
  unfile(slot: i32): void {
407
- if (slot < 0 || slot >= toI32(this.bucketOf.length)) {
538
+ const head = this.head
539
+ const nextOf = this.nextOf
540
+ const prevOf = this.prevOf
541
+ const bucketOf = this.bucketOf
542
+ if (
543
+ slot < 0 ||
544
+ slot >= toI32(bucketOf.length) ||
545
+ slot >= toI32(nextOf.length) ||
546
+ slot >= toI32(prevOf.length)
547
+ ) {
548
+ // Past the first two, never taken: the constructor gives each per-slot array `slots` entries.
549
+ return
550
+ }
551
+ const b: i32 = bucketOf[slot]
552
+ if (b < 0 || b >= toI32(head.length)) {
408
553
  return
409
554
  }
410
- const b: i32 = this.bucketOf[slot]
411
- if (b < 0 || b >= H3_SERVER_WHEEL) {
555
+ const prev: i32 = prevOf[slot]
556
+ const next: i32 = nextOf[slot]
557
+ if (next >= toI32(prevOf.length)) {
558
+ // Never taken: `file` links a slot only to a slot or -1.
412
559
  return
413
560
  }
414
- const prev: i32 = this.prevOf[slot]
415
- const next: i32 = this.nextOf[slot]
416
- if (prev >= 0 && prev < toI32(this.nextOf.length)) {
417
- this.nextOf[prev] = next
561
+ if (prev >= 0 && prev < toI32(nextOf.length)) {
562
+ nextOf[prev] = next
418
563
  } else {
419
- this.head[b] = next + 1
564
+ head[b] = next + 1
420
565
  }
421
- if (next >= 0 && next < toI32(this.prevOf.length)) {
422
- this.prevOf[next] = prev
566
+ if (next >= 0) {
567
+ prevOf[next] = prev
423
568
  }
424
- if (this.head[b] === 0) {
569
+ if (head[b] === 0) {
425
570
  this.flag(b, false)
426
571
  }
427
- this.bucketOf[slot] = -1
572
+ bucketOf[slot] = -1
428
573
  }
429
574
 
430
575
  /**
@@ -437,30 +582,52 @@ export class Http3Wheel {
437
582
  */
438
583
  file(slot: i32, due: i64, now: i64): void {
439
584
  this.unfile(slot)
440
- if (due < 0 || slot < 0 || slot >= toI32(this.bucketOf.length)) {
585
+ const head = this.head
586
+ const nextOf = this.nextOf
587
+ const prevOf = this.prevOf
588
+ const bucketOf = this.bucketOf
589
+ const dueAt = this.dueAt
590
+ if (
591
+ due < 0 ||
592
+ slot < 0 ||
593
+ slot >= toI32(bucketOf.length) ||
594
+ slot >= toI32(nextOf.length) ||
595
+ slot >= toI32(prevOf.length) ||
596
+ slot >= toI32(dueAt.length)
597
+ ) {
598
+ // Past the first three, never taken: the constructor gives each per-slot array `slots` entries.
441
599
  return
442
600
  }
443
601
  if (this.cursor < 0) {
444
602
  this.cursor = now
445
603
  }
446
- let at: i64 = due < this.cursor ? this.cursor : due
447
- const horizon: i64 = this.cursor + toI64(H3_SERVER_WHEEL - 1)
448
- if (at > horizon) {
449
- at = horizon
604
+ const cursor: i64 = this.cursor
605
+ let b: i32 = -1
606
+ if (cursor < 0) {
607
+ // A cursor before the clock's zero: its horizon is below the wheel, so the sum cannot overflow.
608
+ const horizon: i64 = cursor + toI64(H3_SERVER_WHEEL - 1)
609
+ b = http3ServerBucket(due > horizon ? horizon : due)
610
+ } else if (due - cursor >= toI64(H3_SERVER_WHEEL)) {
611
+ // Past the horizon, `cursor + H3_SERVER_WHEEL - 1`, whose bucket is the one before the
612
+ // cursor's; found from that bucket, since the sum can overflow.
613
+ const at: i32 = http3ServerBucket(cursor)
614
+ b = at > 0 ? at - 1 : H3_SERVER_WHEEL - 1
615
+ } else {
616
+ b = http3ServerBucket(due < cursor ? cursor : due)
450
617
  }
451
- const b: i32 = http3ServerBucket(at)
452
- if (b < 0 || b >= H3_SERVER_WHEEL) {
618
+ if (b < 0 || b >= toI32(head.length)) {
453
619
  return
454
620
  }
455
- const first: i32 = this.head[b] - 1
456
- this.nextOf[slot] = first
457
- this.prevOf[slot] = -1
458
- if (first >= 0 && first < toI32(this.prevOf.length)) {
459
- this.prevOf[first] = slot
621
+ const filed: i32 = head[b]
622
+ const first: i32 = filed > 0 ? filed - 1 : -1
623
+ nextOf[slot] = first
624
+ prevOf[slot] = -1
625
+ if (first >= 0 && first < toI32(prevOf.length)) {
626
+ prevOf[first] = slot
460
627
  }
461
- this.head[b] = slot + 1
462
- this.bucketOf[slot] = b
463
- this.dueAt[slot] = due
628
+ head[b] = slot + 1
629
+ bucketOf[slot] = b
630
+ dueAt[slot] = due
464
631
  this.flag(b, true)
465
632
  }
466
633
 
@@ -471,11 +638,17 @@ export class Http3Wheel {
471
638
  * @returns The slot taken, or -1 when the bucket is empty.
472
639
  */
473
640
  take(time: i64): i32 {
641
+ const head = this.head
474
642
  const b: i32 = http3ServerBucket(time)
475
- const slot: i32 = b >= 0 && b < H3_SERVER_WHEEL ? this.head[b] - 1 : -1
476
- if (slot >= 0) {
477
- this.unfile(slot)
643
+ if (b < 0 || b >= toI32(head.length)) {
644
+ return -1
645
+ }
646
+ const filed: i32 = head[b]
647
+ if (filed <= 0) {
648
+ return -1
478
649
  }
650
+ const slot: i32 = filed - 1
651
+ this.unfile(slot)
479
652
  return slot
480
653
  }
481
654
 
@@ -486,25 +659,38 @@ export class Http3Wheel {
486
659
  * when every bucket is empty.
487
660
  */
488
661
  next(now: i64): i32 {
662
+ const head = this.head
663
+ const bits = this.bits
489
664
  // From the cursor: a bucket before it has run, and one at `now` after a
490
665
  // tick is the horizon's, a full turn of the wheel away.
491
666
  const from: i64 = this.cursor < 0 ? now : this.cursor
492
- const end: i64 = from + toI64(H3_SERVER_WHEEL)
493
- let time: i64 = from
494
- while (time < end) {
495
- const b: i32 = http3ServerBucket(time)
667
+ const start: i32 = http3ServerBucket(from)
668
+ if (start < 0 || start > 2047) {
669
+ // Below 0 only for a time before the clock's zero, which has no bucket; never above 2047.
670
+ return -1
671
+ }
672
+ // The milliseconds past `from` searched, to `H3_SERVER_WHEEL`. 2048 and 2047 are it and its
673
+ // mask, written out so the prover bounds the sums below.
674
+ let offset: i32 = 0
675
+ while (offset < 2048) {
676
+ if (from < 0 && offset > 0) {
677
+ // Past a negative `from`, every time up to the clock's zero has no bucket.
678
+ return -1
679
+ }
680
+ const b: i32 = (start + offset) & 2047
496
681
  const word: i32 = b >> 6
497
- if (word < 0 || word >= toI32(this.bits.length)) {
682
+ if (word >= toI32(bits.length) || b >= toI32(head.length)) {
683
+ // Never taken: the constructor gives `head` a place per bucket and `bits` a bit.
498
684
  return -1
499
685
  }
500
- if (this.bits[word] === 0) {
686
+ if (bits[word] === 0) {
501
687
  // An empty word: on to the next one.
502
688
  const word64: i32 = 64
503
- time = time + toI64(word64 - (b & 63))
504
- } else if (this.head[b] !== 0) {
505
- return time <= now ? H3_SERVER_ZERO : toI32(time - now)
689
+ offset = offset + (word64 - (b & 63))
690
+ } else if (head[b] !== 0) {
691
+ return http3ServerWait(from, offset, now)
506
692
  } else {
507
- time = time + 1
693
+ offset = offset + 1
508
694
  }
509
695
  }
510
696
  return -1
@@ -680,32 +866,34 @@ export class Http3Server {
680
866
  const bidi: i64 = quicConfig.maxStreamsBidi
681
867
  const uni: i64 = quicConfig.maxStreamsUni
682
868
  const local: i64 = quicConfig.localStreams
869
+ // `QUIC_CONN_MAX_STREAMS`, written out so the prover knows each count's bound in the sum below.
870
+ const most: i64 = 1024
683
871
  if (
684
872
  streamData >= 1 &&
685
873
  streamData <= QUIC_CONN_MAX_STREAM_DATA &&
686
874
  bidi >= 0 &&
687
- bidi <= QUIC_CONN_MAX_STREAMS &&
875
+ bidi <= most &&
688
876
  uni >= 0 &&
689
- uni <= QUIC_CONN_MAX_STREAMS &&
877
+ uni <= most &&
690
878
  local >= 0 &&
691
- local <= QUIC_CONN_MAX_STREAMS &&
692
- bidi + uni + local > 0
879
+ local <= most &&
880
+ (bidi > 0 || uni > 0 || local > 0)
693
881
  ) {
694
882
  this.streamPools = new QuicStreamPools(toI32(streamData), toI32(bidi + uni + local), size)
695
883
  }
696
884
  this.rx = new Array<u8>(H3_SERVER_RECEIVE_SIZE)
697
- this.tx = new Array<u8>(QUIC_LISTENER_FLIGHT_MAX * QUIC_CONN_DATAGRAM_SIZE)
885
+ this.tx = new Array<u8>(H3_SERVER_FLIGHT_BYTES)
698
886
  this.from = new Array<u8>(H3_SERVER_ADDRESS)
699
887
  this.meta = [H3_SERVER_ZERO, H3_SERVER_ZERO]
700
888
  this.flight = new QuicFlight()
701
889
  this.signer = new TlsP256Signer()
702
- this.signatures = new Array<i64>(size)
703
- this.state = new Array<i32>(size)
704
- this.readyRing = new Array<i32>(size)
705
- this.dirtyStack = new Array<i32>(size)
890
+ this.signatures = http3ServerZeroTimes(size)
891
+ this.state = http3ServerFilled(size, H3_SERVER_ZERO)
892
+ this.readyRing = http3ServerFilled(size, H3_SERVER_ZERO)
893
+ this.dirtyStack = http3ServerFilled(size, H3_SERVER_ZERO)
706
894
  // Released slots only: one never used is taken at `used`.
707
- this.free = new Array<i32>(size)
708
- this.queued = new Array<i32>(size)
895
+ this.free = http3ServerFilled(size, H3_SERVER_ZERO)
896
+ this.queued = http3ServerFilled(size, H3_SERVER_ZERO)
709
897
  }
710
898
 
711
899
  /**
@@ -760,7 +948,13 @@ export class Http3Server {
760
948
  * @returns 0 to `size()`.
761
949
  */
762
950
  busy(): i32 {
763
- return this.used - this.freeCount
951
+ const used: i32 = this.used
952
+ const freed: i32 = this.freeCount
953
+ // Never taken: both count up from 0.
954
+ if (used < 0 || freed < 0) {
955
+ return H3_SERVER_ZERO
956
+ }
957
+ return used - freed
764
958
  }
765
959
 
766
960
  /**
@@ -782,7 +976,15 @@ export class Http3Server {
782
976
  */
783
977
  connection(slot: i32): Http3Connection {
784
978
  this.date.current()
785
- return this.conns[this.slotAt(slot)]
979
+ const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
980
+ const conns: Http3Connection[] = this.conns
981
+ // `build` grows `conns` and `quics` together, so this builds as `slotAt` does.
982
+ for (;;) {
983
+ if (at >= 0 && at < toI32(conns.length)) {
984
+ return conns[at]
985
+ }
986
+ this.build()
987
+ }
786
988
  }
787
989
 
788
990
  /**
@@ -791,7 +993,14 @@ export class Http3Server {
791
993
  * @returns The slot's QUIC connection, kept by the pool and reset for each connection the slot holds.
792
994
  */
793
995
  quic(slot: i32): QuicConnection {
794
- return this.quics[this.slotAt(slot)]
996
+ const at: i32 = slot >= 0 && slot < this.slots ? slot : H3_SERVER_ZERO
997
+ const quics: QuicConnection[] = this.quics
998
+ for (;;) {
999
+ if (at >= 0 && at < toI32(quics.length)) {
1000
+ return quics[at]
1001
+ }
1002
+ this.build()
1003
+ }
795
1004
  }
796
1005
 
797
1006
  /**
@@ -801,24 +1010,48 @@ export class Http3Server {
801
1010
  * @param slot The slot; one that holds no connection is ignored.
802
1011
  */
803
1012
  touch(slot: i32): void {
804
- if (!this.holds(slot)) {
1013
+ const queued: i32[] = this.queued
1014
+ const ring: i32[] = this.readyRing
1015
+ // Past `holds`, never taken: `queued` is `size` long, as `state` is.
1016
+ if (!this.holds(slot) || slot < 0 || slot >= toI32(queued.length)) {
805
1017
  return
806
1018
  }
807
- if ((this.queued[slot] & 1) === 0 && this.readyCount < toI32(this.readyRing.length)) {
808
- const at: i32 = (this.readyHead + this.readyCount) % toI32(this.readyRing.length)
809
- this.readyRing[at] = slot
810
- this.readyCount = this.readyCount + 1
811
- this.queued[slot] = this.queued[slot] | 1
1019
+ const head: i32 = this.readyHead
1020
+ const count: i32 = this.readyCount
1021
+ const length: i32 = toI32(ring.length)
1022
+ // Never false past its first two terms: `ready` keeps `head` in the ring, which is at most 65536 long.
1023
+ if (
1024
+ (queued[slot] & 1) === 0 &&
1025
+ count < length &&
1026
+ head >= 0 &&
1027
+ head <= 65535 &&
1028
+ count >= 0 &&
1029
+ count <= 65535
1030
+ ) {
1031
+ // `(head + count) % length`, both below `length`.
1032
+ const sum: i32 = head + count
1033
+ const at: i32 = sum >= length ? sum - length : sum
1034
+ if (at >= 0 && at < length) {
1035
+ ring[at] = slot
1036
+ this.readyCount = count + 1
1037
+ queued[slot] = queued[slot] | 1
1038
+ }
812
1039
  }
813
1040
  this.dirty(slot)
814
1041
  }
815
1042
 
816
1043
  /** Puts `slot` on the dirty stack, for `flush`. */
817
1044
  private dirty(slot: i32): void {
818
- if ((this.queued[slot] & 2) === 0 && this.dirtyCount < toI32(this.dirtyStack.length)) {
819
- this.dirtyStack[this.dirtyCount] = slot
820
- this.dirtyCount = this.dirtyCount + 1
821
- this.queued[slot] = this.queued[slot] | 2
1045
+ const queued: i32[] = this.queued
1046
+ const stack: i32[] = this.dirtyStack
1047
+ const count: i32 = this.dirtyCount
1048
+ // Never false past its second term: every caller passes a slot `holds`, and the count starts at 0.
1049
+ if (slot >= 0 && slot < toI32(queued.length) && count >= 0) {
1050
+ if ((queued[slot] & 2) === 0 && count < toI32(stack.length)) {
1051
+ stack[count] = slot
1052
+ this.dirtyCount = count + 1
1053
+ queued[slot] = queued[slot] | 2
1054
+ }
822
1055
  }
823
1056
  }
824
1057
 
@@ -829,16 +1062,31 @@ export class Http3Server {
829
1062
  */
830
1063
  ready(): i32 {
831
1064
  this.date.current()
832
- while (this.readyCount > 0) {
833
- const slot: i32 = this.readyRing[this.readyHead]
834
- this.readyHead = (this.readyHead + 1) % toI32(this.readyRing.length)
835
- this.readyCount = this.readyCount - 1
836
- this.queued[slot] = this.queued[slot] & ~1
1065
+ const ring: i32[] = this.readyRing
1066
+ const queued: i32[] = this.queued
1067
+ for (;;) {
1068
+ const count: i32 = this.readyCount
1069
+ if (count <= 0) {
1070
+ return -1
1071
+ }
1072
+ const head: i32 = this.readyHead
1073
+ const length: i32 = toI32(ring.length)
1074
+ // Never taken: `touch` and this loop keep `head` in the ring.
1075
+ if (head < 0 || head >= length) {
1076
+ return -1
1077
+ }
1078
+ const slot: i32 = ring[head]
1079
+ // `(head + 1) % length`.
1080
+ this.readyHead = head + 1 < length ? head + 1 : H3_SERVER_ZERO
1081
+ this.readyCount = count - 1
1082
+ // Never false: `touch` puts only a slot it holds on the ring.
1083
+ if (slot >= 0 && slot < toI32(queued.length)) {
1084
+ queued[slot] = queued[slot] & ~1
1085
+ }
837
1086
  if (this.holds(slot)) {
838
1087
  return slot
839
1088
  }
840
1089
  }
841
- return -1
842
1090
  }
843
1091
 
844
1092
  /**
@@ -863,18 +1111,26 @@ export class Http3Server {
863
1111
  this.from,
864
1112
  this.meta
865
1113
  )
866
- if (n < 0) {
1114
+ const meta: i32[] = this.meta
1115
+ // 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.
1117
+ if (n < 0 || n > 65536 || toI32(meta.length) < 2) {
867
1118
  return count
868
1119
  }
869
- const segment: i32 = this.meta[0] > 0 ? this.meta[0] : n
1120
+ const segment: i32 = meta[0] > 0 ? meta[0] : n
870
1121
  // One receive is one ECN codepoint for every GRO segment of it.
871
- this.arrivalEcn = this.meta[1] & 3
1122
+ this.arrivalEcn = meta[1] & 3
872
1123
  let at: i32 = 0
873
- while (at < n && segment > 0) {
874
- const length: i32 = n - at < segment ? n - at : segment
1124
+ while (at >= 0 && at < n && segment > 0) {
1125
+ const left: i32 = n - at
1126
+ const length: i32 = left < segment ? left : segment
875
1127
  this.datagram(this.rx, at, length, this.from, now, key)
876
- count = count + 1
877
- at = at + length
1128
+ count = h3Count(count)
1129
+ if (length < left) {
1130
+ at = at + length
1131
+ } else {
1132
+ at = n
1133
+ }
878
1134
  }
879
1135
  }
880
1136
  return count
@@ -893,20 +1149,27 @@ export class Http3Server {
893
1149
  * @param key The certificate's private key, as `receive` takes it.
894
1150
  */
895
1151
  datagram(buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
896
- if (len < 1 || at < 0 || at + len > toI32(buf.length)) {
1152
+ const end: i64 = toI64(at) + toI64(len)
1153
+ // The fourth term never decides: a window of at least a byte inside `buf` starts in it.
1154
+ if (len < 1 || at < 0 || end > toI64(buf.length) || at >= toI32(buf.length)) {
897
1155
  return
898
1156
  }
899
1157
  // RFC 8999: a long header carries the DCID's length; a short one is ours, of our length.
900
1158
  let dcidAt: i32 = at + 1
901
1159
  let dcidLength: i32 = QUIC_CONN_CID_LENGTH
902
1160
  if ((toI32(buf[at]) & 0x80) !== 0) {
903
- if (len < 6) {
1161
+ if (len < 6 || at >= toI32(buf.length) - 5) {
1162
+ return
1163
+ }
1164
+ const lengthAt: i32 = at + 5
1165
+ // Never taken, nor the second term above: a window of six bytes or more holds its sixth.
1166
+ if (lengthAt >= toI32(buf.length)) {
904
1167
  return
905
1168
  }
906
- dcidLength = toI32(buf[at + 5])
907
- dcidAt = at + 6
1169
+ dcidLength = toI32(buf[lengthAt])
1170
+ dcidAt = lengthAt + 1
908
1171
  }
909
- if (dcidLength > QUIC_MAX_CID_LENGTH || dcidAt + dcidLength > at + len) {
1172
+ if (dcidLength > QUIC_MAX_CID_LENGTH || toI64(dcidAt) + toI64(dcidLength) > end) {
910
1173
  return
911
1174
  }
912
1175
  const slot: i32 = this.index.find(buf, dcidAt, dcidLength)
@@ -937,29 +1200,62 @@ export class Http3Server {
937
1200
  // A slot released before is taken first, then the next never used, so the
938
1201
  // first accepts take slots 0, 1, 2 ... and a pool that never fills never
939
1202
  // builds its last slots.
1203
+ // The slot is chosen, checked, and only then taken, so a check that fails leaves it where it was.
940
1204
  let slot: i32 = 0
941
- if (this.freeCount > 0) {
942
- this.freeCount = this.freeCount - 1
943
- slot = this.free[this.freeCount]
944
- } else if (this.used < this.slots) {
945
- slot = this.slotAt(this.used)
946
- this.used = this.used + 1
1205
+ const free: i32[] = this.free
1206
+ const freed: i32 = this.freeCount
1207
+ const used: i32 = this.used
1208
+ // `freeCount` and `used` once the slot is taken.
1209
+ let freeAfter: i32 = freed
1210
+ let usedAfter: i32 = used
1211
+ if (freed > 0) {
1212
+ // Never taken: `release` pushes at most one entry per slot onto `free`, `size` long.
1213
+ if (freed > toI32(free.length)) {
1214
+ return
1215
+ }
1216
+ slot = free[freed - 1]
1217
+ freeAfter = freed - 1
1218
+ } else if (used < this.slots) {
1219
+ // Builds the slot if it is not yet; building it again on a later call is a no-op.
1220
+ slot = this.slotAt(used)
1221
+ usedAfter = used + 1
947
1222
  } else {
948
1223
  return
949
1224
  }
1225
+ const quics: QuicConnection[] = this.quics
1226
+ const conns: Http3Connection[] = this.conns
1227
+ const addresses: u8[][] = this.addresses
1228
+ const state: i32[] = this.state
1229
+ const signatures: i64[] = this.signatures
950
1230
  crypto.getRandomValues(this.entropy)
951
- const quic: QuicConnection = this.quics[slot]
1231
+ // Never taken: a released slot was built, `slotAt` builds the one it answers, and
1232
+ // `state` and `signatures` are `size` long.
1233
+ if (
1234
+ slot < 0 ||
1235
+ slot >= toI32(quics.length) ||
1236
+ slot >= toI32(conns.length) ||
1237
+ slot >= toI32(addresses.length) ||
1238
+ slot >= toI32(state.length) ||
1239
+ slot >= toI32(signatures.length)
1240
+ ) {
1241
+ return
1242
+ }
1243
+ this.freeCount = freeAfter
1244
+ this.used = usedAfter
1245
+ const quic: QuicConnection = quics[slot]
1246
+ const conn: Http3Connection = conns[slot]
1247
+ const to: u8[] = addresses[slot]
1248
+ // Marked busy before its connections restart, which read neither array.
1249
+ state[slot] = H3_SERVER_BUSY
1250
+ signatures[slot] = H3_SERVER_NONE
952
1251
  quic.reset(this.entropy)
953
- this.conns[slot].restart()
1252
+ conn.restart()
954
1253
  if (answer.retried) {
955
1254
  quic.acceptRetry(answer.originalDcid, answer.retryScid)
956
1255
  }
957
- const to: u8[] = this.addresses[slot]
958
1256
  for (let k: i32 = 0; k < toI32(to.length) && k < toI32(address.length); k++) {
959
1257
  to[k] = address[k]
960
1258
  }
961
- this.state[slot] = H3_SERVER_BUSY
962
- this.signatures[slot] = H3_SERVER_NONE
963
1259
  this.accepted = h3Count(this.accepted)
964
1260
  this.serve(slot, buf, at, len, address, now, key)
965
1261
  }
@@ -976,6 +1272,10 @@ export class Http3Server {
976
1272
  * not start a connection, are also held to `takeAnswer`'s budget.
977
1273
  */
978
1274
  private worthListening(buf: u8[], at: i32, len: i32, now: i64): boolean {
1275
+ // Never taken: `datagram` passes a window of at least a byte inside `buf`.
1276
+ if (at < 0 || at >= toI32(buf.length)) {
1277
+ return false
1278
+ }
979
1279
  const first: i32 = toI32(buf[at])
980
1280
  if ((first & 0x80) === 0) {
981
1281
  return len > QUIC_LISTENER_MIN_RESET && this.takeAnswer(now)
@@ -983,14 +1283,26 @@ export class Http3Server {
983
1283
  if (len < QUIC_CONN_DATAGRAM_SIZE) {
984
1284
  return false
985
1285
  }
1286
+ // Never taken, nor the test in the loop: the window, `QUIC_CONN_DATAGRAM_SIZE` bytes or more,
1287
+ // holds its first six.
1288
+ if (at >= toI32(buf.length) - 5) {
1289
+ return false
1290
+ }
1291
+ const lengthAt: i32 = at + 5
1292
+ if (lengthAt >= toI32(buf.length)) {
1293
+ return false
1294
+ }
986
1295
  let version: i32 = 0
987
1296
  for (let k: i32 = 1; k <= 4; k++) {
988
- version = (version << 8) | toI32(buf[at + k])
1297
+ const byteAt: i32 = at + k
1298
+ if (byteAt < toI32(buf.length)) {
1299
+ version = (version << 8) | toI32(buf[byteAt])
1300
+ }
989
1301
  }
990
1302
  if (version !== 1) {
991
1303
  return version !== 0 && this.takeAnswer(now)
992
1304
  }
993
- if ((first & 0x30) !== 0 || toI32(buf[at + 5]) < 8) {
1305
+ if ((first & 0x30) !== 0 || toI32(buf[lengthAt]) < 8) {
994
1306
  return false
995
1307
  }
996
1308
  if (this.full()) {
@@ -1008,21 +1320,42 @@ export class Http3Server {
1008
1320
  * garbage costs no answer and no MAC past the budget (H3-3); counted in `limited`.
1009
1321
  */
1010
1322
  private takeAnswer(now: i64): boolean {
1011
- if (this.answerRefilled < 0 || now < this.answerRefilled) {
1012
- this.answerRefilled = now
1013
- this.answerBudget = H3_SERVER_ANSWER_BURST
1014
- }
1015
- const earned: i64 = (now - this.answerRefilled) / H3_SERVER_ANSWER_INTERVAL
1016
- if (earned > 0) {
1017
- const room: i64 = toI64(H3_SERVER_ANSWER_BURST - this.answerBudget)
1018
- this.answerBudget = this.answerBudget + toI32(earned < room ? earned : room)
1019
- this.answerRefilled = this.answerRefilled + earned * H3_SERVER_ANSWER_INTERVAL
1323
+ let refilled: i64 = this.answerRefilled
1324
+ let budget: i32 = this.answerBudget
1325
+ if (refilled < 0 || now < refilled) {
1326
+ refilled = now
1327
+ budget = H3_SERVER_ANSWER_BURST
1328
+ } else if (now >= 0) {
1329
+ // Always taken: `now` is at least `refilled`, which is not negative here.
1330
+ const elapsed: i64 = now - refilled
1331
+ const earned: i64 = elapsed / H3_SERVER_ANSWER_INTERVAL
1332
+ // The second term never decides: an interval earned is `H3_SERVER_ANSWER_INTERVAL` elapsed.
1333
+ if (earned > 0 && elapsed > 0) {
1334
+ // The budget is 0 to `H3_SERVER_ANSWER_BURST`, 16, so 16 intervals or more always fill it;
1335
+ // written out so the prover bounds the sum below.
1336
+ const gain: i32 = earned < 16 ? toI32(earned) : 16
1337
+ if (gain < 16 && gain >= 0 && budget >= 0 && budget < 16) {
1338
+ const sum: i32 = budget + gain
1339
+ budget = sum < H3_SERVER_ANSWER_BURST ? sum : H3_SERVER_ANSWER_BURST
1340
+ } else {
1341
+ budget = H3_SERVER_ANSWER_BURST
1342
+ }
1343
+ // `refilled + earned * H3_SERVER_ANSWER_INTERVAL`: the part of an interval left over counts
1344
+ // toward the next answer.
1345
+ const rest: i64 = elapsed % H3_SERVER_ANSWER_INTERVAL
1346
+ // Always taken: `elapsed` is positive, so its remainder is under the interval.
1347
+ if (rest >= 0 && rest < H3_SERVER_ANSWER_INTERVAL) {
1348
+ refilled = now - rest
1349
+ }
1350
+ }
1020
1351
  }
1021
- if (this.answerBudget <= 0) {
1352
+ this.answerRefilled = refilled
1353
+ if (budget <= 0) {
1354
+ this.answerBudget = budget
1022
1355
  this.limited = h3Count(this.limited)
1023
1356
  return false
1024
1357
  }
1025
- this.answerBudget = this.answerBudget - 1
1358
+ this.answerBudget = budget - 1
1026
1359
  return true
1027
1360
  }
1028
1361
 
@@ -1044,7 +1377,7 @@ export class Http3Server {
1044
1377
  * handshake asks, and refiles its IDs and timer. The connection decides
1045
1378
  * from the address whether the client moved (RFC 9000 §9.3); an address
1046
1379
  * that is not 18 bytes long leaves it where it is.
1047
- * @param slot A slot that holds a connection, as `datagram` finds it; the slot is not checked.
1380
+ * @param slot A slot that holds a connection, as `datagram` finds it; one outside the pool is ignored.
1048
1381
  * @param buf The bytes holding the datagram, read in place.
1049
1382
  * @param at Where the datagram starts in `buf`.
1050
1383
  * @param len Its length in bytes.
@@ -1053,7 +1386,11 @@ export class Http3Server {
1053
1386
  * @param key The certificate's private key, as `receive` takes it.
1054
1387
  */
1055
1388
  serve(slot: i32, buf: u8[], at: i32, len: i32, address: u8[], now: i64, key: Secret<u8[]>): void {
1056
- const quic: QuicConnection = this.quics[slot]
1389
+ const quics: QuicConnection[] = this.quics
1390
+ if (slot < 0 || slot >= toI32(quics.length)) {
1391
+ return
1392
+ }
1393
+ const quic: QuicConnection = quics[slot]
1057
1394
  quic.recovery.micros = this.micros
1058
1395
  quic.arrivalEcn = this.arrivalEcn
1059
1396
  quic.receiveWindowFrom(buf, at, len, address, now)
@@ -1070,31 +1407,41 @@ export class Http3Server {
1070
1407
 
1071
1408
  /** Files `slot`'s connection IDs in the index again when they changed. */
1072
1409
  private refile(slot: i32): void {
1073
- const quic: QuicConnection = this.quics[slot]
1410
+ const quics: QuicConnection[] = this.quics
1411
+ if (slot < 0 || slot >= toI32(quics.length)) {
1412
+ // Never taken: every caller passes a slot of the pool.
1413
+ return
1414
+ }
1415
+ const quic: QuicConnection = quics[slot]
1074
1416
  const signature: i64 =
1075
1417
  (quic.cids.nextLocal << 16) |
1076
1418
  (toI64(quic.cids.activeLocal()) << 8) |
1077
1419
  (toI64(quic.originalDcidLength) << 1) |
1078
1420
  (quic.handshakeComplete ? toI64(1) : toI64(0))
1079
- if (signature === this.signatures[slot]) {
1421
+ const signatures: i64[] = this.signatures
1422
+ // `slot` is under 65536, the most slots the constructor takes, so its first entry below fits.
1423
+ if (slot >= toI32(signatures.length) || slot > 65535) {
1424
+ // Never taken: `signatures` has an entry per slot.
1425
+ return
1426
+ }
1427
+ if (signature === signatures[slot]) {
1080
1428
  return
1081
1429
  }
1082
- this.signatures[slot] = signature
1430
+ signatures[slot] = signature
1083
1431
  this.unindex(slot)
1084
- const first: i32 = slot * H3_SERVER_KEYS
1085
- for (let j: i32 = 0; j < QUIC_CONN_LOCAL_CIDS && j < toI32(quic.cids.local.length); j++) {
1086
- const entry = quic.cids.local[j]
1432
+ // 5 is `H3_SERVER_KEYS` and 4 `QUIC_CONN_LOCAL_CIDS`, written out so the entries' bound is known.
1433
+ const keys: i32 = 5
1434
+ const cids: i32 = 4
1435
+ const first: i32 = slot * keys
1436
+ const local = quic.cids.local
1437
+ for (let j: i32 = 0; j < cids && j < toI32(local.length); j++) {
1438
+ const entry = local[j]
1087
1439
  if (entry.used) {
1088
1440
  this.index.insert(first + j, entry.cid, H3_SERVER_ZERO, entry.length)
1089
1441
  }
1090
1442
  }
1091
1443
  if (!quic.handshakeComplete && quic.originalDcidLength > 0) {
1092
- this.index.insert(
1093
- first + QUIC_CONN_LOCAL_CIDS,
1094
- quic.originalDcid,
1095
- H3_SERVER_ZERO,
1096
- quic.originalDcidLength
1097
- )
1444
+ this.index.insert(first + cids, quic.originalDcid, H3_SERVER_ZERO, quic.originalDcidLength)
1098
1445
  }
1099
1446
  }
1100
1447
 
@@ -1119,9 +1466,14 @@ export class Http3Server {
1119
1466
  mark = H3_SERVER_ZERO
1120
1467
  }
1121
1468
  const step: i32 = segment > 0 ? segment : len
1469
+ if (at < 0 || len < 0) {
1470
+ // Never taken: `udpSendTo` above took `buf[at .. at + len)`.
1471
+ return false
1472
+ }
1122
1473
  let p: i32 = at
1123
- while (p < at + len) {
1124
- const n: i32 = at + len - p < step ? at + len - p : step
1474
+ let left: i32 = len
1475
+ while (left > 0) {
1476
+ const n: i32 = left < step ? left : step
1125
1477
  let one: i32 = udpSendTo(this.fd, buf, p, n, to, H3_SERVER_ZERO, mark)
1126
1478
  if (one === H3_SERVER_UNSUPPORTED && mark !== 0) {
1127
1479
  // No ECN here: this datagram and the rest go unmarked and no slot
@@ -1135,7 +1487,13 @@ export class Http3Server {
1135
1487
  this.sendErrors = h3Count(this.sendErrors)
1136
1488
  return false
1137
1489
  }
1138
- p = p + n
1490
+ if (n < 0 || n > left) {
1491
+ // Never taken: `n` is the smaller of `left` and a positive `step`.
1492
+ return false
1493
+ }
1494
+ left = left - n
1495
+ // `p + n`, at most `at + len`, which `udpSendTo` took, so it fits an i32.
1496
+ p = toI32(toI64(p) + toI64(n))
1139
1497
  }
1140
1498
  return true
1141
1499
  }
@@ -1156,10 +1514,21 @@ export class Http3Server {
1156
1514
  * @param now The current millisecond on the caller's monotonic clock.
1157
1515
  */
1158
1516
  flush(now: i64): void {
1159
- while (this.dirtyCount > 0) {
1160
- this.dirtyCount = this.dirtyCount - 1
1161
- const slot: i32 = this.dirtyStack[this.dirtyCount]
1162
- this.queued[slot] = this.queued[slot] & ~2
1517
+ const stack: i32[] = this.dirtyStack
1518
+ const queued: i32[] = this.queued
1519
+ for (;;) {
1520
+ const count: i32 = this.dirtyCount
1521
+ // Past the first term, never taken: `dirty` pushes only below the stack's length.
1522
+ if (count <= 0 || count > toI32(stack.length)) {
1523
+ return
1524
+ }
1525
+ const top: i32 = count - 1
1526
+ this.dirtyCount = top
1527
+ const slot: i32 = stack[top]
1528
+ // Always taken: the stack holds slots of the pool.
1529
+ if (slot >= 0 && slot < toI32(queued.length)) {
1530
+ queued[slot] = queued[slot] & ~2
1531
+ }
1163
1532
  if (this.holds(slot)) {
1164
1533
  this.flushSlot(slot, now)
1165
1534
  }
@@ -1168,9 +1537,22 @@ export class Http3Server {
1168
1537
 
1169
1538
  /** `flush` of one slot. */
1170
1539
  private flushSlot(slot: i32, now: i64): void {
1171
- const quic: QuicConnection = this.quics[slot]
1540
+ const quics: QuicConnection[] = this.quics
1541
+ const conns: Http3Connection[] = this.conns
1542
+ const addresses: u8[][] = this.addresses
1543
+ if (
1544
+ slot < 0 ||
1545
+ slot >= toI32(quics.length) ||
1546
+ slot >= toI32(conns.length) ||
1547
+ slot >= toI32(addresses.length)
1548
+ ) {
1549
+ // Never taken: `flush` passes a slot that holds a connection, which `build` made in all three.
1550
+ return
1551
+ }
1552
+ const quic: QuicConnection = quics[slot]
1553
+ const conn: Http3Connection = conns[slot]
1554
+ const to: u8[] = addresses[slot]
1172
1555
  quic.recovery.micros = this.micros
1173
- const conn: Http3Connection = this.conns[slot]
1174
1556
  if (conn.isDone() && !quic.closed()) {
1175
1557
  quic.close(H3_NO_ERROR)
1176
1558
  }
@@ -1184,7 +1566,6 @@ export class Http3Server {
1184
1566
  if (probe > 0) {
1185
1567
  this.send(this.tx, H3_SERVER_ZERO, probe, quic.probeAddress, H3_SERVER_ZERO, H3_SERVER_ZERO)
1186
1568
  }
1187
- const to: u8[] = this.addresses[slot]
1188
1569
  if (quic.pathKnown) {
1189
1570
  const current: u8[] = quic.sendAddress()
1190
1571
  for (let k: i32 = 0; k < toI32(to.length) && k < toI32(current.length); k++) {
@@ -1241,8 +1622,14 @@ export class Http3Server {
1241
1622
 
1242
1623
  /** Takes every one of `slot`'s connection IDs out of the index. */
1243
1624
  private unindex(slot: i32): void {
1244
- const first: i32 = slot * H3_SERVER_KEYS
1245
- for (let j: i32 = 0; j < H3_SERVER_KEYS; j++) {
1625
+ // Never taken: a slot is 0 to 65535, under the most slots the constructor takes.
1626
+ if (slot < 0 || slot > 65535) {
1627
+ return
1628
+ }
1629
+ // 5 is `H3_SERVER_KEYS`, written out so the entries' bound is known.
1630
+ const keys: i32 = 5
1631
+ const first: i32 = slot * keys
1632
+ for (let j: i32 = 0; j < keys; j++) {
1246
1633
  this.index.remove(first + j)
1247
1634
  }
1248
1635
  }
@@ -1251,11 +1638,21 @@ export class Http3Server {
1251
1638
  private release(slot: i32): void {
1252
1639
  this.unindex(slot)
1253
1640
  this.wheel.unfile(slot)
1254
- this.conns[slot].release()
1255
- this.state[slot] = H3_SERVER_FREE
1256
- if (this.freeCount < toI32(this.free.length)) {
1257
- this.free[this.freeCount] = slot
1258
- this.freeCount = this.freeCount + 1
1641
+ const conns: Http3Connection[] = this.conns
1642
+ const state: i32[] = this.state
1643
+ if (slot < 0 || slot >= toI32(conns.length) || slot >= toI32(state.length)) {
1644
+ // Never taken: every caller passes a slot that holds a connection.
1645
+ return
1646
+ }
1647
+ // Marked free before the connection lets go of its buffers, which read
1648
+ // nothing of the server's.
1649
+ state[slot] = H3_SERVER_FREE
1650
+ conns[slot].release()
1651
+ const free: i32[] = this.free
1652
+ const count: i32 = this.freeCount
1653
+ if (count >= 0 && count < toI32(free.length)) {
1654
+ free[count] = slot
1655
+ this.freeCount = count + 1
1259
1656
  }
1260
1657
  }
1261
1658
 
@@ -1277,11 +1674,13 @@ export class Http3Server {
1277
1674
  */
1278
1675
  stop(): void {
1279
1676
  this.stopped = true
1677
+ const conns: Http3Connection[] = this.conns
1280
1678
  for (let slot: i32 = 0; slot < toI32(this.quics.length); slot++) {
1281
1679
  if (this.holds(slot)) {
1282
1680
  this.release(slot)
1283
- } else {
1284
- this.conns[slot].release()
1681
+ } else if (slot < toI32(conns.length)) {
1682
+ // Always taken: `build` grows `conns` and `quics` together.
1683
+ conns[slot].release()
1285
1684
  }
1286
1685
  }
1287
1686
  this.listener.release()
@@ -1297,35 +1696,91 @@ export class Http3Server {
1297
1696
  * @param now The current millisecond on the caller's monotonic clock.
1298
1697
  */
1299
1698
  tick(now: i64): void {
1699
+ // `now + 1`, where the cursor ends once `now` has run. Below zero it fits;
1700
+ // from zero it is put back together from the millisecond's high and low
1701
+ // parts, whose ranges show the sum fits.
1702
+ let after: i64 = 0
1703
+ if (now < 0) {
1704
+ after = now + 1
1705
+ } else {
1706
+ const high: i64 = now / 1048576
1707
+ const low: i64 = now % 1048576
1708
+ if (high >= 1073741824) {
1709
+ // Never taken: a millisecond of `monotonicNanos() / 1000000` is under
1710
+ // 2^44, so `high` is under 2^24.
1711
+ return
1712
+ }
1713
+ after = high * 1048576 + low + 1
1714
+ }
1300
1715
  const wheel: Http3Wheel = this.wheel
1301
- if (wheel.cursor < 0) {
1302
- wheel.cursor = now
1716
+ let cursor: i64 = wheel.cursor
1717
+ if (cursor < 0) {
1718
+ cursor = now
1303
1719
  }
1304
1720
  // Late by more than a turn: every bucket is run once, from a turn ago, so a
1305
- // slot filed again lands after `now` and not behind the cursor.
1306
- if (now - wheel.cursor >= toI64(H3_SERVER_WHEEL)) {
1307
- wheel.cursor = now - toI64(H3_SERVER_WHEEL - 1)
1721
+ // slot filed again lands after `now` and not behind the cursor. 2048 is
1722
+ // `H3_SERVER_WHEEL`, written out so the prover bounds the sums, and compared
1723
+ // on each side of zero so no difference overflows.
1724
+ if (now >= 0) {
1725
+ if (now - 2048 >= cursor) {
1726
+ cursor = now - 2047
1727
+ }
1728
+ } else if (cursor < 0) {
1729
+ const behind: i64 = now - cursor
1730
+ if (behind >= 2048) {
1731
+ // `now - 2047`.
1732
+ cursor = cursor + (behind - 2047)
1733
+ }
1308
1734
  }
1309
- let steps: i32 = 0
1310
- while (wheel.cursor <= now && steps < H3_SERVER_WHEEL) {
1311
- let slot: i32 = wheel.take(wheel.cursor)
1735
+ wheel.cursor = cursor
1736
+ for (let steps: i32 = 0; steps < H3_SERVER_WHEEL; steps++) {
1737
+ const at: i64 = wheel.cursor
1738
+ if (at > now) {
1739
+ break
1740
+ }
1741
+ let slot: i32 = wheel.take(at)
1312
1742
  while (slot >= 0) {
1313
1743
  if (this.holds(slot)) {
1314
- if (wheel.dueAt[slot] > now) {
1744
+ const dueAt: i64[] = wheel.dueAt
1745
+ const quics: QuicConnection[] = this.quics
1746
+ if (slot >= toI32(dueAt.length) || slot >= toI32(quics.length)) {
1747
+ // Never taken: `holds` is true only of a slot of the pool.
1748
+ } else if (dueAt[slot] > now) {
1315
1749
  // Filed at the horizon: not due yet, so filed again.
1316
- wheel.file(slot, wheel.dueAt[slot], now)
1750
+ wheel.file(slot, dueAt[slot], now)
1317
1751
  } else {
1318
- this.quics[slot].handleTimer(now)
1752
+ quics[slot].handleTimer(now)
1319
1753
  this.dirty(slot)
1320
1754
  }
1321
1755
  }
1322
1756
  slot = wheel.take(wheel.cursor)
1323
1757
  }
1324
- wheel.cursor = wheel.cursor + 1
1325
- steps++
1758
+ // On to the next millisecond, `next + 1`, written so that no sum can
1759
+ // overflow. At `now` the cursor ends at `after`, and the loop with it.
1760
+ const next: i64 = wheel.cursor
1761
+ if (next === now) {
1762
+ wheel.cursor = after
1763
+ break
1764
+ }
1765
+ if (next < 0 && next < now) {
1766
+ wheel.cursor = next + 1
1767
+ } else if (next >= 0 && now >= 0) {
1768
+ // Both from zero, so the distance left fits.
1769
+ const ahead: i64 = now - next
1770
+ if (ahead >= 1) {
1771
+ wheel.cursor = now - (ahead - 1)
1772
+ } else {
1773
+ // Never taken: the bucket just run was at `now` or before.
1774
+ break
1775
+ }
1776
+ } else {
1777
+ // Never taken: the bucket just run was at `now` or before, and running
1778
+ // it moves the cursor only from before zero to `now`.
1779
+ break
1780
+ }
1326
1781
  }
1327
1782
  if (wheel.cursor <= now) {
1328
- wheel.cursor = now + 1
1783
+ wheel.cursor = after
1329
1784
  }
1330
1785
  }
1331
1786