@amritk/nish-aarch64-linux 0.49.0 → 0.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/INSTALL.md +1 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +233 -159
  4. package/package.json +1 -1
  5. package/runtime/gfx/nish-gfx.h +373 -61
  6. package/runtime/nish.d.ts +12 -9
  7. package/std/README.md +8 -5
  8. package/std/base64.ts +74 -1
  9. package/std/bytes.ts +201 -93
  10. package/std/compress/deflate.ts +20 -15
  11. package/std/crypto/base64url.ts +93 -66
  12. package/std/crypto/bcrypt.ts +25 -21
  13. package/std/crypto/hmac.ts +1 -1
  14. package/std/crypto/jwt.ts +32 -19
  15. package/std/crypto/p256.ts +9 -12
  16. package/std/crypto/pki-roots.ts +7 -4
  17. package/std/crypto/pki.ts +43 -55
  18. package/std/crypto/rsa-sign.ts +1632 -0
  19. package/std/crypto/rsa.ts +26 -4
  20. package/std/crypto/sha256.ts +135 -68
  21. package/std/crypto/x25519.ts +12 -11
  22. package/std/crypto/x509.ts +164 -40
  23. package/std/gpu/sys.ts +490 -0
  24. package/std/json-equal.ts +33 -18
  25. package/std/json-format.ts +39 -8
  26. package/std/json-number.ts +15 -3
  27. package/std/json-tokenizer.ts +4 -3
  28. package/std/json-typed.ts +29 -12
  29. package/std/json-value.ts +222 -104
  30. package/std/json.ts +81 -46
  31. package/std/net/dns-config.ts +18 -5
  32. package/std/net/dns-message.ts +60 -27
  33. package/std/net/dns-resolver.ts +35 -16
  34. package/std/net/dns.ts +3 -2
  35. package/std/net/fetch.ts +120 -38
  36. package/std/net/handler.ts +178 -66
  37. package/std/net/hpack.ts +192 -98
  38. package/std/net/http-compress.ts +83 -30
  39. package/std/net/http-fields.ts +327 -31
  40. package/std/net/http-server.ts +317 -20
  41. package/std/net/http1-client-codec.ts +137 -40
  42. package/std/net/http1-codec.ts +391 -202
  43. package/std/net/http1-server.ts +220 -38
  44. package/std/net/http2-cleartext.ts +10 -4
  45. package/std/net/http2-frame.ts +2 -2
  46. package/std/net/http2-tls.ts +3 -3
  47. package/std/net/http2.ts +69 -15
  48. package/std/net/http3-frame.ts +11 -14
  49. package/std/net/http3-server.ts +12 -15
  50. package/std/net/http3.ts +41 -29
  51. package/std/net/multipart.ts +592 -0
  52. package/std/net/outbound.ts +24 -8
  53. package/std/net/postgres-async.ts +92 -54
  54. package/std/net/postgres-carrier.ts +28 -4
  55. package/std/net/postgres.ts +166 -65
  56. package/std/net/prefork.ts +86 -5
  57. package/std/net/protobuf.ts +6 -2
  58. package/std/net/qpack.ts +66 -25
  59. package/std/net/quic-conn-ack.ts +27 -7
  60. package/std/net/quic-conn-cid.ts +13 -7
  61. package/std/net/quic-datagram.ts +6 -5
  62. package/std/net/quic-frame.ts +67 -23
  63. package/std/net/quic-listener.ts +19 -12
  64. package/std/net/quic-recovery.ts +11 -9
  65. package/std/net/quic-stream.ts +7 -5
  66. package/std/net/quic.ts +36 -31
  67. package/std/net/router.ts +136 -24
  68. package/std/net/serve.ts +59 -5
  69. package/std/net/static.ts +81 -33
  70. package/std/net/tls/record.ts +26 -13
  71. package/std/net/tls/ticket.ts +6 -5
  72. package/std/net/tls-tcp.ts +69 -30
  73. package/std/net/tls.ts +152 -38
  74. package/std/net/upstream.ts +65 -19
  75. package/std/net/url.ts +97 -78
  76. package/std/net/websocket.ts +33 -15
  77. package/std/net/webtransport.ts +28 -12
  78. package/std/regex.ts +600 -388
  79. package/std/sqlite.ts +37 -19
  80. package/std/sync/ring.ts +232 -115
  81. package/std/sync/shm.ts +11 -4
  82. package/std/temporal.ts +232 -133
  83. package/std/term-io.ts +12 -6
  84. package/std/term-pixels.ts +158 -22
  85. package/std/term-rate.ts +8 -3
  86. package/std/term.ts +4 -4
  87. package/std/text.ts +33 -11
  88. package/std/threads.ts +52 -8
  89. package/std/yaml-json.ts +238 -106
  90. package/std/yaml-lines.ts +47 -27
  91. package/std/yaml-write.ts +58 -24
  92. package/std/yaml.ts +808 -395
@@ -285,8 +285,14 @@ export const PG_SSL_REQUEST_CODE: i32 = 80877103
285
285
  */
286
286
  export const pgSslRequest = (): u8[] => {
287
287
  const out: u8[] = new Array<u8>(8)
288
- setI32BE(out, 0, 8)
289
- setI32BE(out, 4, PG_SSL_REQUEST_CODE)
288
+ out[0] = toU8(0)
289
+ out[1] = toU8(0)
290
+ out[2] = toU8(0)
291
+ out[3] = toU8(8)
292
+ out[4] = toU8(PG_SSL_REQUEST_CODE >> 24)
293
+ out[5] = toU8(PG_SSL_REQUEST_CODE >> 16)
294
+ out[6] = toU8(PG_SSL_REQUEST_CODE >> 8)
295
+ out[7] = toU8(PG_SSL_REQUEST_CODE)
290
296
  return out
291
297
  }
292
298
 
@@ -576,6 +582,7 @@ const pgPutDecimal = (out: u8[], at: i32, value: i64, n: i32): i32 => {
576
582
  return -1
577
583
  }
578
584
  const first: i32 = value < 0 ? at + 1 : at
585
+ // Negated as a `u64`: the magnitude of the least `i64` has no `i64`.
579
586
  let m: u64 = value < 0 ? toU64(0) - toU64(value) : toU64(value)
580
587
  // Where the last two digits start.
581
588
  let j: i32 = at + n - 2
@@ -824,7 +831,12 @@ export class PgText {
824
831
  acc = acc * toU64(10) + digit
825
832
  i++
826
833
  }
827
- return negative ? toI64(toU64(0) - acc) : toI64(acc)
834
+ if (!negative) {
835
+ return toI64(acc)
836
+ }
837
+ // 2^63 reads back as the most negative i64, which is already the answer.
838
+ const magnitude: i64 = toI64(acc)
839
+ return magnitude < 0 ? magnitude : -magnitude
828
840
  }
829
841
 
830
842
  /**
@@ -986,15 +998,6 @@ export class PgBinary {
986
998
  this.failed = false
987
999
  }
988
1000
 
989
- // Whether `[start, start + length)` is inside `b`; sets `failed` when not.
990
- private inside(b: u8[], start: i32, length: i32): boolean {
991
- if (start < 0 || length < 0 || start > toI32(b.length) - length) {
992
- this.failed = true
993
- return false
994
- }
995
- return true
996
- }
997
-
998
1001
  /**
999
1002
  * An `int2`, `int4` or `int8` column, told apart by its length (2, 4 or 8
1000
1003
  * bytes, big-endian two's complement); 0 and `failed` for any other length,
@@ -1006,18 +1009,22 @@ export class PgBinary {
1006
1009
  * @returns The value, sign-extended to 64 bits, or 0 with `failed` set.
1007
1010
  */
1008
1011
  int(b: u8[], start: i32, length: i32): i64 {
1009
- if ((length !== 2 && length !== 4 && length !== 8) || !this.inside(b, start, length)) {
1010
- this.failed = true
1011
- return 0
1012
- }
1013
- if (length === 8) {
1014
- return getI64BE(b, start)
1015
- }
1016
- if (length === 4) {
1017
- return toI64(getI32BE(b, start))
1012
+ // Each width's window is tested where it is read: the getter proves it
1013
+ // from that test.
1014
+ if (start >= 0) {
1015
+ if (length === 8 && start <= toI32(b.length) - 8) {
1016
+ return getI64BE(b, start)
1017
+ }
1018
+ if (length === 4 && start <= toI32(b.length) - 4) {
1019
+ return toI64(getI32BE(b, start))
1020
+ }
1021
+ if (length === 2 && start <= toI32(b.length) - 2) {
1022
+ const v: i32 = toI32(getU16BE(b, start))
1023
+ return toI64(v >= 32768 ? v - 65536 : v)
1024
+ }
1018
1025
  }
1019
- const v: i32 = toI32(getU16BE(b, start))
1020
- return toI64(v >= 32768 ? v - 65536 : v)
1026
+ this.failed = true
1027
+ return 0
1021
1028
  }
1022
1029
 
1023
1030
  /**
@@ -1051,7 +1058,7 @@ export class PgBinary {
1051
1058
  * for a column that is not eight bytes inside `b`.
1052
1059
  */
1053
1060
  float(b: u8[], start: i32, length: i32): f64 {
1054
- if (length !== 8 || !this.inside(b, start, length)) {
1061
+ if (length !== 8 || start < 0 || start > toI32(b.length) - 8) {
1055
1062
  this.failed = true
1056
1063
  return 0
1057
1064
  }
@@ -1070,7 +1077,7 @@ export class PgBinary {
1070
1077
  * @returns 24, the bytes written, or -1 with `failed` set.
1071
1078
  */
1072
1079
  timestamp(b: u8[], start: i32, length: i32, out: u8[], at: i32): i32 {
1073
- if (length !== 8 || !this.inside(b, start, length)) {
1080
+ if (length !== 8 || start < 0 || start > toI32(b.length) - 8) {
1074
1081
  this.failed = true
1075
1082
  return -1
1076
1083
  }
@@ -1215,8 +1222,18 @@ export class PgBinaryArray {
1215
1222
  return
1216
1223
  }
1217
1224
  const ndim: i32 = getI32BE(b, start)
1218
- const hasNull: i32 = getI32BE(b, start + 4)
1219
- const oid: i32 = getI32BE(b, start + 8)
1225
+ // Read here rather than with `getI32BE`, whose call before would end what
1226
+ // the prover knows of `b`.
1227
+ const hasNull: i32 =
1228
+ (toI32(b[start + 4]) << 24) |
1229
+ (toI32(b[start + 5]) << 16) |
1230
+ (toI32(b[start + 6]) << 8) |
1231
+ toI32(b[start + 7])
1232
+ const oid: i32 =
1233
+ (toI32(b[start + 8]) << 24) |
1234
+ (toI32(b[start + 9]) << 16) |
1235
+ (toI32(b[start + 10]) << 8) |
1236
+ toI32(b[start + 11])
1220
1237
  this.size = 0
1221
1238
  if (oid === PG_OID_INT8) {
1222
1239
  this.size = 8
@@ -1239,10 +1256,14 @@ export class PgBinaryArray {
1239
1256
  this.pos = pos
1240
1257
  this.end = end
1241
1258
  if (ndim === 1) {
1242
- const dim: i32 = getI32BE(b, pos)
1243
1259
  // The length test above keeps the dimension inside the column; this one
1244
- // states that bound for the prover.
1245
- if (dim < 0 || pos > toI32(b.length) - 8) {
1260
+ // states that bound for the prover, where `getI32BE` reads it.
1261
+ if (pos < 0 || pos > toI32(b.length) - 8) {
1262
+ this.refuse()
1263
+ return
1264
+ }
1265
+ const dim: i32 = getI32BE(b, pos)
1266
+ if (dim < 0) {
1246
1267
  this.refuse()
1247
1268
  return
1248
1269
  }
@@ -1280,10 +1301,13 @@ export class PgBinaryArray {
1280
1301
  // `begin` keeps `seen` and `pos` from going negative; summed in `i64`,
1281
1302
  // a length cannot overflow on its way to the test against `end`.
1282
1303
  const p: i64 = toI64(pos) + toI64(4)
1283
- if (pos < 0 || p > toI64(end)) {
1304
+ const b: u8[] = this.b
1305
+ // `end` lies in `b`, so the last test is never taken: it is the window
1306
+ // `getI32BE` proves from.
1307
+ if (pos < 0 || p > toI64(end) || pos > toI32(b.length) - 4) {
1284
1308
  return this.refuse()
1285
1309
  }
1286
- const n: i32 = getI32BE(this.b, pos)
1310
+ const n: i32 = getI32BE(b, pos)
1287
1311
  const at: i32 = toI32(p)
1288
1312
  this.pos = at
1289
1313
  this.seen = seen + 1
@@ -1629,7 +1653,13 @@ export class PgConnection {
1629
1653
  }
1630
1654
  this.putByte(PG_ZERO)
1631
1655
  // The message is the first in the buffer, so its length is the buffer's end.
1632
- setI32BE(this.out, at, this.outEnd)
1656
+ // `putI32` wrote the first word, so the test is never taken; it states the bound for the prover.
1657
+ const out: u8[] = this.out
1658
+ const length: i32 = this.outEnd
1659
+ if (toI32(out.length) < 4) {
1660
+ return
1661
+ }
1662
+ setI32BE(out, at, length)
1633
1663
  }
1634
1664
 
1635
1665
  // --- Buffers -----------------------------------------------------------------
@@ -1984,7 +2014,7 @@ export class PgConnection {
1984
2014
  }
1985
2015
  if (type === 75) {
1986
2016
  // 'K': BackendKeyData, the process id and the cancel key.
1987
- if (state !== PG_STATE_STARTING || size !== 8) {
2017
+ if (state !== PG_STATE_STARTING || size !== 8 || body < 0 || body > toI32(b.length) - 4) {
1988
2018
  return this.fail(state !== PG_STATE_STARTING ? PG_ERR_UNEXPECTED : PG_ERR_FRAME)
1989
2019
  }
1990
2020
  this.backendPid = getI32BE(b, body)
@@ -2043,7 +2073,7 @@ export class PgConnection {
2043
2073
  }
2044
2074
  if (type === 116) {
2045
2075
  // 't': ParameterDescription.
2046
- if (size < 2 || size !== toI32(getU16BE(b, body)) * 4 + 2) {
2076
+ if (size < 2 || body < 0 || body > toI32(b.length) - 2 || size !== toI32(getU16BE(b, body)) * 4 + 2) {
2047
2077
  return this.fail(PG_ERR_FRAME)
2048
2078
  }
2049
2079
  return PG_NEED_MORE
@@ -2151,7 +2181,12 @@ export class PgConnection {
2151
2181
  if (nameEnd < 0 || end < 0 || end - nameEnd < 19 || nameEnd > toI32(b.length) - 19) {
2152
2182
  return this.fail(PG_ERR_FRAME)
2153
2183
  }
2154
- types[c] = getI32BE(b, nameEnd + 7)
2184
+ // Never taken after the test above; it states the type's window for the prover.
2185
+ const typeAt: i32 = nameEnd + 7
2186
+ if (typeAt > toI32(b.length) - 4) {
2187
+ return this.fail(PG_ERR_FRAME)
2188
+ }
2189
+ types[c] = getI32BE(b, typeAt)
2155
2190
  p = nameEnd + 19
2156
2191
  }
2157
2192
  if (p !== end) {
@@ -2549,10 +2584,11 @@ export class PgConnection {
2549
2584
  const end: i32 = this.outEnd
2550
2585
  this.reserve(toI64(end) + toI64(2))
2551
2586
  const b: u8[] = this.out
2587
+ const bits: u16 = toU16(v)
2552
2588
  if (end < 0 || end > toI32(b.length) - 2) {
2553
2589
  return
2554
2590
  }
2555
- setU16BE(b, end, toU16(v))
2591
+ setU16BE(b, end, bits)
2556
2592
  this.outEnd = end + 2
2557
2593
  }
2558
2594
 
@@ -2593,10 +2629,16 @@ export class PgConnection {
2593
2629
  private endMessage(at: i32): void {
2594
2630
  const end: i32 = this.outEnd
2595
2631
  // The message's type byte and length word lie before the end; the test states that for the prover.
2596
- if (at < 0 || end < 0 || end - at < 5 || at > toI32(this.out.length) - 5) {
2632
+ const out: u8[] = this.out
2633
+ if (at < 0 || end < 0 || end - at < 5 || at > toI32(out.length) - 5) {
2634
+ return
2635
+ }
2636
+ // Never taken after the test above; it states the length word's window for the prover.
2637
+ const lengthAt: i32 = at + 1
2638
+ if (lengthAt > toI32(out.length) - 4) {
2597
2639
  return
2598
2640
  }
2599
- setI32BE(this.out, at + 1, end - at - 1)
2641
+ setI32BE(out, lengthAt, end - at - 1)
2600
2642
  }
2601
2643
 
2602
2644
  /**
@@ -3089,20 +3131,28 @@ export class PgConnection {
3089
3131
  const end: i32 = toI32(toI64(start) + toI64(count))
3090
3132
  // `reserve` made room for every element; the loop's tests state that bound for the prover.
3091
3133
  for (let i: i32 = start; i >= 0 && i < end && i < toI32(values.length); i++) {
3134
+ // The length word is `width`, 4 or 8, so its first three bytes are zero.
3135
+ if (p < 0 || p > toI32(b.length) - 8) {
3136
+ break
3137
+ }
3138
+ b[p] = 0
3139
+ b[p + 1] = 0
3140
+ b[p + 2] = 0
3141
+ b[p + 3] = toU8(width)
3142
+ const valueAt: i32 = p + 4
3092
3143
  if (width === 4) {
3093
- if (p < 0 || p > toI32(b.length) - 8) {
3144
+ // Never taken after the test above; it states the value's window for the prover.
3145
+ if (valueAt > toI32(b.length) - 4) {
3094
3146
  break
3095
3147
  }
3096
- setI32BE(b, p, width)
3097
- setI32BE(b, p + 4, toI32(values[i]))
3098
- p = p + 8
3148
+ setI32BE(b, valueAt, toI32(values[i]))
3149
+ p = valueAt + 4
3099
3150
  } else {
3100
- if (p < 0 || p > toI32(b.length) - 12) {
3151
+ if (valueAt > toI32(b.length) - 8) {
3101
3152
  break
3102
3153
  }
3103
- setI32BE(b, p, width)
3104
- setI64BE(b, p + 4, values[i])
3105
- p = p + 12
3154
+ setI64BE(b, valueAt, values[i])
3155
+ p = valueAt + 8
3106
3156
  }
3107
3157
  }
3108
3158
  this.outEnd = p
@@ -3367,15 +3417,32 @@ export class PgConnection {
3367
3417
  if (this.arrayOid !== 0) {
3368
3418
  // The header follows the parameter's length word: ndim, the NULL flag, the OID, then the dimension.
3369
3419
  // `binaryArrayStart` wrote all 24 bytes inside `out`; the test states that bound for the prover.
3420
+ // Each word is written a byte at a time, big-endian, under that one test.
3370
3421
  const at: i32 = this.arrayAt
3371
- if (at >= 0 && at <= toI32(this.out.length) - 24) {
3372
- const header: i32 = at + 4
3373
- if (this.arrayCount === 0) {
3374
- setI32BE(this.out, header, PG_ZERO)
3375
- this.outEnd = header + PG_ARRAY_HEADER
3422
+ const out: u8[] = this.out
3423
+ if (at >= 0 && at <= toI32(out.length) - 24) {
3424
+ // ndim sits at `at + 4`, the NULL flag at `at + 8` and the dimension's length at `at + 16`.
3425
+ const count: i32 = this.arrayCount
3426
+ const nulls: u8 = this.arrayNulls ? 1 : 0
3427
+ if (count === 0) {
3428
+ out[at + 4] = 0
3429
+ out[at + 5] = 0
3430
+ out[at + 6] = 0
3431
+ out[at + 7] = 0
3432
+ this.outEnd = at + 4 + PG_ARRAY_HEADER
3376
3433
  } else {
3377
- setI32BE(this.out, header + 4, this.arrayNulls ? 1 : 0)
3378
- setI32BE(this.out, header + PG_ARRAY_HEADER, this.arrayCount)
3434
+ out[at + 8] = 0
3435
+ out[at + 9] = 0
3436
+ out[at + 10] = 0
3437
+ out[at + 11] = nulls
3438
+ // Never false after the test above; it states the length's window for the prover.
3439
+ const lengthAt: i32 = at + 16
3440
+ if (lengthAt <= toI32(out.length) - 4) {
3441
+ out[lengthAt] = toU8(count >> 24)
3442
+ out[lengthAt + 1] = toU8(count >> 16)
3443
+ out[lengthAt + 2] = toU8(count >> 8)
3444
+ out[lengthAt + 3] = toU8(count)
3445
+ }
3379
3446
  }
3380
3447
  }
3381
3448
  this.arrayOid = 0
@@ -3408,7 +3475,14 @@ export class PgConnection {
3408
3475
  if (this.binaryParamCount > 0) {
3409
3476
  this.putFormatCodes()
3410
3477
  }
3411
- setU16BE(this.out, this.paramCountAt, toU16(this.paramCount))
3478
+ // The count word lies inside `out`; the test is never taken and states that for the prover.
3479
+ const out: u8[] = this.out
3480
+ const countAt: i32 = this.paramCountAt
3481
+ const count: i32 = this.paramCount
3482
+ if (countAt >= 0 && countAt <= toI32(out.length) - 2) {
3483
+ out[countAt] = toU8(count >> 8)
3484
+ out[countAt + 1] = toU8(count)
3485
+ }
3412
3486
  if (binary) {
3413
3487
  // One format code, which the server applies to every column.
3414
3488
  this.putI16(toI32(1))
@@ -3472,21 +3546,24 @@ export class PgConnection {
3472
3546
  const codes: i32 = count * 2
3473
3547
  this.reserve(toI64(end) + toI64(codes))
3474
3548
  const b: u8[] = this.out
3475
- if (end > toI32(b.length) - codes) {
3549
+ if (end > toI32(b.length) - codes || from > toI32(b.length)) {
3476
3550
  return
3477
3551
  }
3478
3552
  // `reserve` made the room, so both ends fit an `i32`.
3479
3553
  const moved: i32 = toI32(toI64(from) + toI64(codes))
3480
3554
  copyBytes(b, moved, b, from, end - from)
3481
3555
  this.outEnd = toI32(toI64(end) + toI64(codes))
3482
- setU16BE(b, from - 2, toU16(count))
3556
+ // The count, then the codes, each written a byte at a time, big-endian.
3557
+ b[from - 2] = toU8(count >> 8)
3558
+ b[from - 1] = toU8(count)
3483
3559
  // Every code lies in `[from, from + codes)`, inside `b`.
3484
3560
  for (let i: i32 = 0; i < count; i++) {
3485
3561
  const at: i32 = toI32(toI64(from) + toI64(i) * toI64(2))
3486
- if (at > toI32(b.length) - 2) {
3562
+ if (at < 0 || at > toI32(b.length) - 2) {
3487
3563
  break
3488
3564
  }
3489
- setU16BE(b, at, toU16(0))
3565
+ b[at] = 0
3566
+ b[at + 1] = 0
3490
3567
  }
3491
3568
  const params: i32[] = this.binaryParams
3492
3569
  for (let k: i32 = 0; k < this.binaryParamCount && k < toI32(params.length); k++) {
@@ -3494,7 +3571,12 @@ export class PgConnection {
3494
3571
  if (param < 0 || param >= count) {
3495
3572
  break
3496
3573
  }
3497
- setU16BE(b, toI32(toI64(from) + toI64(param) * toI64(2)), toU16(PG_FORMAT_BINARY))
3574
+ const at: i32 = toI32(toI64(from) + toI64(param) * toI64(2))
3575
+ if (at < 0 || at > toI32(b.length) - 2) {
3576
+ break
3577
+ }
3578
+ b[at] = toU8(PG_FORMAT_BINARY >> 8)
3579
+ b[at + 1] = toU8(PG_FORMAT_BINARY)
3498
3580
  }
3499
3581
  this.paramCountAt = moved
3500
3582
  }
@@ -5465,6 +5547,25 @@ export class PgBatcher {
5465
5547
 
5466
5548
  // --- Batch timing --------------------------------------------------------------
5467
5549
 
5550
+ /** 2^62 - 1, past any clock reading in microseconds; a clock sum stops there. */
5551
+ const PG_CLOCK_MOST: i64 = 1073741824 * 4294967296 - 1
5552
+
5553
+ /**
5554
+ * `a + b` for two non-negative clock values, stopping at `PG_CLOCK_MOST`;
5555
+ * -1 for a negative one. Taken below the top, so it cannot overflow.
5556
+ */
5557
+ const pgClockSum = (a: i64, b: i64): i64 => {
5558
+ if (a < 0 || b < 0) {
5559
+ return -1
5560
+ }
5561
+ const room: i64 = PG_CLOCK_MOST - a
5562
+ if (room < 0) {
5563
+ return PG_CLOCK_MOST
5564
+ }
5565
+ const left: i64 = room - b
5566
+ return left > 0 ? PG_CLOCK_MOST - left : PG_CLOCK_MOST
5567
+ }
5568
+
5468
5569
  /** `PgBatchClock.check`: not yet; wait for an item or for `deadline()`. */
5469
5570
  export const PG_CLOCK_WAIT: i32 = 0
5470
5571
  /** `PgBatchClock.check`: `minCount` items are waiting, and the average asks no more. */
@@ -5703,12 +5804,12 @@ export class PgBatchClock {
5703
5804
  deadline(): i64 {
5704
5805
  const oldest: i64 = this.oldest()
5705
5806
  const wait: i64 = this.maxWaitUs
5706
- // The times are clock times and `maxWaitUs` is at most a minute, so the sum is below 2^53
5707
- // and the unsigned add cannot wrap; the test states those bounds.
5807
+ // The times are clock times and `maxWaitUs` is at most a minute, so the sum is below 2^53;
5808
+ // the test states those bounds.
5708
5809
  if (oldest < 0 || oldest > 4503599627370496 || wait < 0 || wait > 60000000) {
5709
5810
  return -1
5710
5811
  }
5711
- return toI64(toU64(oldest) + toU64(wait))
5812
+ return pgClockSum(oldest, wait)
5712
5813
  }
5713
5814
 
5714
5815
  /**
@@ -5746,9 +5847,9 @@ export class PgBatchClock {
5746
5847
  this.lastSize = size
5747
5848
  this.lastReason = due
5748
5849
  const started: i64 = starts[due]
5749
- // 2^52 batches cannot start in the clock's 2^52 microseconds, so the unsigned add cannot wrap.
5850
+ // 2^52 batches cannot start in the clock's 2^52 microseconds.
5750
5851
  if (started >= 0 && started < 4503599627370496) {
5751
- starts[due] = toI64(toU64(started) + toU64(1))
5852
+ starts[due] = pgClockSum(started, 1)
5752
5853
  }
5753
5854
  this.due = PG_CLOCK_WAIT
5754
5855
  }
@@ -561,15 +561,65 @@ const preforkNowMs = (): i64 => monotonicNanos() / 1000000
561
561
 
562
562
  // The clock in milliseconds lies within 2^63 / 10^6 of 0, about 9.2 * 10^12,
563
563
  // so a sum or difference of two such times, or of one and an i32, is far
564
- // inside the i64 range. The 64-bit unsigned arithmetic below wraps rather
565
- // than panics, and on values that never pass the range its result, read
566
- // back as an i64, is the exact one.
564
+ // inside the i64 range. The helpers below still hold a result at the range's
565
+ // ends rather than overflow, so that the bounds prover sees no sum that can:
566
+ // two values whose sum or difference cannot leave the range are worked in
567
+ // `i64`, and two that could are worked as magnitudes in `u64`, where they are
568
+ // below 2^64 and so exact, and held below 2^63 before they are read back.
567
569
 
568
570
  /** `now` plus `ms` milliseconds. */
569
- const preforkAfter = (now: i64, ms: i32): i64 => toI64(toU64(now) + toU64(toI64(ms)))
571
+ const preforkAfter = (now: i64, ms: i32): i64 => {
572
+ const n: i64 = toI64(ms)
573
+ if (now >= 0) {
574
+ if (n < 0) {
575
+ return now + n
576
+ }
577
+ const sum: u64 = toU64(now) + toU64(n)
578
+ return toI64(preforkBelow2p63(sum))
579
+ }
580
+ if (n >= 0) {
581
+ return now + n
582
+ }
583
+ // `now + n` is `-1 - ((-1 - now) + (-1 - n) + 1)`.
584
+ const past: u64 = toU64(-1 - now) + toU64(-1 - n) + toU64(1)
585
+ const k: i64 = toI64(preforkBelow2p63(past))
586
+ if (k < 0) {
587
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
588
+ return -1
589
+ }
590
+ return -1 - k
591
+ }
570
592
 
571
593
  /** The milliseconds from `then` to `now`. */
572
- const preforkSince = (now: i64, then: i64): i64 => toI64(toU64(now) - toU64(then))
594
+ const preforkSince = (now: i64, then: i64): i64 => {
595
+ if (now >= 0) {
596
+ if (then >= 0) {
597
+ return now - then
598
+ }
599
+ const sum: u64 = toU64(now) + toU64(-1 - then) + toU64(1)
600
+ return toI64(preforkBelow2p63(sum))
601
+ }
602
+ if (then < 0) {
603
+ return now - then
604
+ }
605
+ // `now - then` is `-1 - ((-1 - now) + then)`.
606
+ const past: u64 = toU64(-1 - now) + toU64(then)
607
+ const k: i64 = toI64(preforkBelow2p63(past))
608
+ if (k < 0) {
609
+ // Not taken: `k` is below 2^63. The test is what proves `-1 - k` fits.
610
+ return -1
611
+ }
612
+ return -1 - k
613
+ }
614
+
615
+ /** `x`, or 2^63 - 1 where `x` is larger: the most an `i64` holds. */
616
+ const preforkBelow2p63 = (x: u64): u64 => {
617
+ const most: u64 = (toU64(1) << toU64(63)) - toU64(1)
618
+ if (x > most) {
619
+ return most
620
+ }
621
+ return x
622
+ }
573
623
 
574
624
  /** The signals the supervisor stops its workers with. */
575
625
  const PREFORK_SIGTERM: i32 = 15
@@ -1394,6 +1444,26 @@ export const preforkSupervise = <S>(
1394
1444
  return ran.value
1395
1445
  }
1396
1446
 
1447
+ /**
1448
+ * `preforkSupervise` for a caller built without `--allow-panics`: a config
1449
+ * that breaks its contract is an `Err` with the reason, before any worker
1450
+ * starts, where `preforkSupervise` panics.
1451
+ *
1452
+ * @param args the process's arguments, `process.argv`
1453
+ * @param config the supervisor's settings, checked in the supervisor and
1454
+ * each worker alike
1455
+ * @param state the program's own value, passed to every `onChannel` call
1456
+ * @param onChannel what the supervisor runs when worker `index`'s channel
1457
+ * `fd` wakes it, with `pollWait`'s `events`
1458
+ * @returns `Ok` with what `preforkSupervise` answers, or `Err` with why the config was refused
1459
+ */
1460
+ export const tryPreforkSupervise = <S>(
1461
+ args: string[],
1462
+ config: PreforkConfig,
1463
+ state: S,
1464
+ onChannel: (state: S, index: i32, fd: i32, events: i32) => void
1465
+ ): Result<i32, string> => preforkRun(args, config, state, onChannel)
1466
+
1397
1467
  /**
1398
1468
  * `preforkWith` with the default `PreforkConfig`.
1399
1469
  *
@@ -1402,3 +1472,14 @@ export const preforkSupervise = <S>(
1402
1472
  * `PREFORK_STOPPED` or `PREFORK_SPAWN_FAILED`
1403
1473
  */
1404
1474
  export const preforkWorkers = (args: string[]): i32 => preforkWith(args, new PreforkConfig())
1475
+
1476
+ /**
1477
+ * `preforkWorkers` for a caller built without `--allow-panics`: the
1478
+ * supervisor's refusal of its config is an `Err` here, where
1479
+ * `preforkWorkers` panics.
1480
+ *
1481
+ * @param args the process's arguments, `process.argv`
1482
+ * @returns `Ok` with what `preforkWorkers` answers, or `Err` with why the config was refused
1483
+ */
1484
+ export const tryPreforkWorkers = (args: string[]): Result<i32, string> =>
1485
+ tryPreforkWith(args, new PreforkConfig())
@@ -415,7 +415,9 @@ export class ProtoReader {
415
415
  */
416
416
  sint64(): i64 {
417
417
  const v: u64 = this.uint64()
418
- return toI64((v >> toU64(1)) ^ (toU64(0) - (v & toU64(1))))
418
+ // All ones when the low bit is set and none when it is clear: the subtraction wraps by its `u64` type.
419
+ const sign: u64 = toU64(0) - (v & toU64(1))
420
+ return toI64((v >> toU64(1)) ^ sign)
419
421
  }
420
422
 
421
423
  /**
@@ -426,7 +428,9 @@ export class ProtoReader {
426
428
  */
427
429
  sint32(): i32 {
428
430
  const v: u32 = toU32(this.uint64())
429
- return toI32((v >> toU32(1)) ^ (toU32(0) - (v & toU32(1))))
431
+ // All ones when the low bit is set and none when it is clear: the subtraction wraps by its `u32` type.
432
+ const sign: u32 = toU32(0) - (v & toU32(1))
433
+ return toI32((v >> toU32(1)) ^ sign)
430
434
  }
431
435
 
432
436
  /**