@amritk/nish-aarch64-linux 0.42.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.
- package/bin/nish +0 -0
- package/docs/AI.md +287 -129
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +24 -0
- package/runtime/core.mjs +21 -0
- package/runtime/nish.d.ts +167 -24
- package/runtime/nish.mjs +7 -1
- package/runtime/runtime-cross.c +9 -0
- package/runtime/runtime-net.c +51 -13
- package/runtime/runtime-region.c +13 -0
- package/runtime/runtime-text.c +11 -0
- package/runtime/runtime-uring.c +83 -25
- package/runtime/runtime-wasm.c +3 -0
- package/runtime/runtime.c +329 -6
- package/runtime/sqlite/README.md +16 -3
- package/runtime/sqlite/nish-sqlite.c +940 -29
- package/scripts/build.sh +111 -29
- package/std/README.md +6 -6
- package/std/array.ts +10 -2
- package/std/bytes.ts +239 -26
- package/std/cli-args.ts +1072 -606
- package/std/cli-flags.ts +180 -98
- package/std/cli.ts +21 -13
- package/std/collections.ts +245 -127
- package/std/compress/checksum.ts +32 -37
- package/std/compress/deflate.ts +1102 -542
- package/std/compress/gzip.ts +290 -145
- package/std/compress/zlib.ts +57 -21
- package/std/crypto/aes.ts +153 -75
- package/std/crypto/base64url.ts +163 -39
- package/std/crypto/bcrypt-batch.ts +164 -90
- package/std/crypto/bcrypt.ts +185 -107
- package/std/crypto/chacha20poly1305.ts +84 -68
- package/std/crypto/ct.ts +9 -7
- package/std/crypto/ed25519.ts +64 -27
- package/std/crypto/hkdf.ts +105 -63
- package/std/crypto/hmac.ts +95 -89
- package/std/crypto/jwt.ts +248 -126
- package/std/crypto/p256.ts +758 -444
- package/std/crypto/p384.ts +20 -13
- package/std/crypto/pki-roots.ts +95 -38
- package/std/crypto/pki.ts +164 -60
- package/std/crypto/rsa.ts +266 -223
- package/std/crypto/scram.ts +141 -0
- package/std/crypto/sha1.ts +157 -57
- package/std/crypto/sha256.ts +502 -422
- package/std/crypto/sha512.ts +204 -135
- package/std/crypto/x509-random.ts +9 -3
- package/std/crypto/x509.ts +331 -171
- package/std/host/ring.ts +1 -1
- package/std/host/text.ts +1 -1
- package/std/json-equal.ts +35 -19
- package/std/json-format.ts +215 -108
- package/std/json-number.ts +126 -72
- package/std/json-pointer.ts +42 -13
- package/std/json-tokenizer.ts +498 -307
- package/std/json-typed.ts +115 -36
- package/std/json-value.ts +341 -152
- package/std/json.ts +300 -158
- package/std/net/buffer-pool.ts +17 -7
- package/std/net/dns-config.ts +427 -236
- package/std/net/dns-message.ts +114 -47
- package/std/net/dns-resolver.ts +736 -237
- package/std/net/dns.ts +205 -52
- package/std/net/fetch.ts +320 -184
- package/std/net/grpc.ts +948 -0
- package/std/net/handler.ts +1276 -218
- package/std/net/hpack.ts +395 -180
- package/std/net/http-compress.ts +502 -65
- package/std/net/http-fields.ts +85 -49
- package/std/net/http-server.ts +1764 -603
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +881 -269
- package/std/net/http2-cleartext.ts +165 -95
- package/std/net/http2-frame.ts +232 -89
- package/std/net/http2-tls.ts +42 -12
- package/std/net/http2.ts +327 -101
- package/std/net/http3-frame.ts +68 -33
- package/std/net/http3-server.ts +672 -206
- package/std/net/http3.ts +669 -191
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1223 -470
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1521 -632
- package/std/net/prefork.ts +254 -98
- package/std/net/protobuf.ts +1017 -0
- package/std/net/qpack.ts +143 -92
- package/std/net/quic-conn-ack.ts +104 -22
- package/std/net/quic-conn-cid.ts +66 -26
- package/std/net/quic-conn-params.ts +13 -7
- package/std/net/quic-datagram.ts +90 -27
- package/std/net/quic-frame.ts +324 -121
- package/std/net/quic-listener.ts +206 -91
- package/std/net/quic-packet.ts +289 -163
- package/std/net/quic-recovery.ts +279 -128
- package/std/net/quic-stream.ts +466 -167
- package/std/net/quic.ts +584 -258
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +661 -240
- package/std/net/static.ts +1458 -0
- package/std/net/tls/client.ts +140 -45
- package/std/net/tls/codec.ts +510 -260
- package/std/net/tls/record-client.ts +268 -76
- package/std/net/tls/record-server.ts +236 -79
- package/std/net/tls/record.ts +173 -47
- package/std/net/tls/schedule.ts +58 -39
- package/std/net/tls/ticket.ts +295 -103
- package/std/net/tls/verify.ts +1 -1
- package/std/net/tls-tcp.ts +218 -75
- package/std/net/tls.ts +260 -152
- package/std/net/upstream.ts +283 -86
- package/std/net/url.ts +277 -102
- package/std/net/websocket.ts +532 -209
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +325 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +285 -135
- package/std/threads.ts +22 -2
package/std/net/postgres.ts
CHANGED
|
@@ -116,8 +116,8 @@
|
|
|
116
116
|
* `int4[]` or `int8[]` column in its binary layout, and refuses any other array
|
|
117
117
|
* (a `text[]` or a multi-dimensional one among them).
|
|
118
118
|
*
|
|
119
|
-
* **Authentication.** SCRAM-SHA-256 (RFC 5802, RFC 7677) or none (`trust`)
|
|
120
|
-
* password arrives as a `Secret<u8[]>`; the connection keeps a copy in a holder
|
|
119
|
+
* **Authentication.** SCRAM-SHA-256 (RFC 5802, RFC 7677) or none (`trust`), the
|
|
120
|
+
* key derivation and proofs from `nish/crypto/scram`. The password arrives as a `Secret<u8[]>`; the connection keeps a copy in a holder
|
|
121
121
|
* that is wiped as soon as the proof is computed, or when the connection fails
|
|
122
122
|
* or is disposed. The server's signature is checked in constant time before
|
|
123
123
|
* `AuthenticationOk` is believed, and an `AuthenticationOk` that skips the end
|
|
@@ -203,9 +203,12 @@
|
|
|
203
203
|
import { copyBytes, getF64BE, getI32BE, getI64BE, getU16BE, setI32BE, setI64BE, setU16BE } from "nish/bytes"
|
|
204
204
|
import { base64StdPadDecode, base64StdPadEncode } from "nish/crypto/base64url"
|
|
205
205
|
import { timingSafeEqual } from "nish/crypto/ct"
|
|
206
|
-
import {
|
|
207
|
-
|
|
208
|
-
|
|
206
|
+
import {
|
|
207
|
+
scramSha256ClientProof,
|
|
208
|
+
scramSha256SaltedPassword,
|
|
209
|
+
scramSha256ServerSignature,
|
|
210
|
+
} from "nish/crypto/scram"
|
|
211
|
+
import { Secret, expose, secret, wipe } from "nish:secret"
|
|
209
212
|
import { urlDecode } from "nish/net/url"
|
|
210
213
|
|
|
211
214
|
/** `next()` needs more input before it can say anything. */
|
|
@@ -459,8 +462,10 @@ export class PgError {
|
|
|
459
462
|
|
|
460
463
|
/** A fresh copy of `bytes`; the password holder keeps one. */
|
|
461
464
|
const pgCopyBytes = (bytes: u8[]): u8[] => {
|
|
462
|
-
const copy: u8[] =
|
|
463
|
-
|
|
465
|
+
const copy: u8[] = []
|
|
466
|
+
for (const b of bytes) {
|
|
467
|
+
copy.push(b)
|
|
468
|
+
}
|
|
464
469
|
return copy
|
|
465
470
|
}
|
|
466
471
|
|
|
@@ -547,13 +552,14 @@ const pgBytesOf = (text: string): u8[] => {
|
|
|
547
552
|
/** How many bytes `value` takes in decimal, its sign included. */
|
|
548
553
|
const pgDecimalLength = (value: i64): i32 => {
|
|
549
554
|
// Counted on the negative side, where the most negative i64 fits, two digits a step.
|
|
550
|
-
|
|
555
|
+
// A `u32` count wraps rather than checks; it never passes 20.
|
|
556
|
+
let n: u32 = value < 0 ? 2 : 1
|
|
551
557
|
let v: i64 = value < 0 ? value : -value
|
|
552
558
|
while (v <= -100) {
|
|
553
559
|
v = v / 100
|
|
554
560
|
n += 2
|
|
555
561
|
}
|
|
556
|
-
return v <= -10 ? n + 1 : n
|
|
562
|
+
return toI32(v <= -10 ? n + 1 : n)
|
|
557
563
|
}
|
|
558
564
|
|
|
559
565
|
/**
|
|
@@ -561,12 +567,13 @@ const pgDecimalLength = (value: i64): i32 => {
|
|
|
561
567
|
* `pgDecimalLength(value)`, and answers `at + n`. The digits go once, into
|
|
562
568
|
* their places, from the last, two a step from the magnitude as a `u64`
|
|
563
569
|
* (which holds the most negative i64's, and whose `/` and `%` are never
|
|
564
|
-
* checked).
|
|
565
|
-
* restates it in the shape that proves its two
|
|
570
|
+
* checked). Answers -1, and writes nothing, unless the window is inside
|
|
571
|
+
* `out`; each step's guard restates it in the shape that proves its two
|
|
572
|
+
* stores.
|
|
566
573
|
*/
|
|
567
574
|
const pgPutDecimal = (out: u8[], at: i32, value: i64, n: i32): i32 => {
|
|
568
575
|
if (!(at >= 0 && at < 2147483620 && n >= 1 && n <= 20 && at + n <= toI32(out.length))) {
|
|
569
|
-
|
|
576
|
+
return -1
|
|
570
577
|
}
|
|
571
578
|
const first: i32 = value < 0 ? at + 1 : at
|
|
572
579
|
let m: u64 = value < 0 ? toU64(0) - toU64(value) : toU64(value)
|
|
@@ -658,28 +665,33 @@ const pgIsText = (b: u8[], from: i32, end: i32): boolean => {
|
|
|
658
665
|
} else {
|
|
659
666
|
return false
|
|
660
667
|
}
|
|
661
|
-
|
|
668
|
+
// `more` bytes must follow `i` before `end`.
|
|
669
|
+
if (toI64(more) >= toI64(end) - toI64(i)) {
|
|
662
670
|
return false
|
|
663
671
|
}
|
|
664
|
-
|
|
665
|
-
|
|
672
|
+
// `k` walks the continuation bytes, `i + 1` to `i + more`.
|
|
673
|
+
let k: i32 = i + 1
|
|
674
|
+
while (k < toI32(b.length) && k - i <= more) {
|
|
675
|
+
const d: i32 = toI32(b[k])
|
|
666
676
|
if (d < low || d > high) {
|
|
667
677
|
return false
|
|
668
678
|
}
|
|
669
679
|
low = 0x80
|
|
670
680
|
high = 0xbf
|
|
681
|
+
k++
|
|
671
682
|
}
|
|
672
|
-
i
|
|
683
|
+
i = k
|
|
673
684
|
}
|
|
674
685
|
return true
|
|
675
686
|
}
|
|
676
687
|
|
|
677
|
-
/**
|
|
688
|
+
/**
|
|
689
|
+
* The first `byte` in `b[from, end)`, or `end` when there is none; past
|
|
690
|
+
* `b`'s end nothing is found, so a window outside it reads as one without
|
|
691
|
+
* the byte, which every caller refuses as a malformed message.
|
|
692
|
+
*/
|
|
678
693
|
const pgFind = (b: u8[], from: i32, end: i32, byte: i32): i32 => {
|
|
679
|
-
|
|
680
|
-
panic("nish/net/postgres: pgFind outside the buffer")
|
|
681
|
-
}
|
|
682
|
-
for (let i: i32 = from; i < end; i++) {
|
|
694
|
+
for (let i: i32 = from; i >= 0 && i < end && i < toI32(b.length); i++) {
|
|
683
695
|
if (toI32(b[i]) === byte) {
|
|
684
696
|
return i
|
|
685
697
|
}
|
|
@@ -693,69 +705,17 @@ const pgBytesAre = (b: u8[], start: i32, length: i32, text: string): boolean =>
|
|
|
693
705
|
if (length !== n || start < 0 || start > toI32(b.length) - n) {
|
|
694
706
|
return false
|
|
695
707
|
}
|
|
708
|
+
// `k` walks `b` from `start`, which the guard keeps `n` bytes from the end.
|
|
709
|
+
let k: i32 = start
|
|
696
710
|
for (let i: i32 = 0; i < n; i++) {
|
|
697
|
-
if (toI32(b
|
|
711
|
+
if (k >= toI32(b.length) || toI32(b[k]) !== toI32(text.charCodeAt(i))) {
|
|
698
712
|
return false
|
|
699
713
|
}
|
|
714
|
+
k++
|
|
700
715
|
}
|
|
701
716
|
return true
|
|
702
717
|
}
|
|
703
718
|
|
|
704
|
-
/**
|
|
705
|
-
* The salted password, PBKDF2-HMAC-SHA-256 over the password and the salt
|
|
706
|
-
* (RFC 5802 §3, `Hi`), 32 bytes, or `null` when it cannot be computed.
|
|
707
|
-
*
|
|
708
|
-
* The caller has bounded `iterations` by `maxIterations`, since the time is
|
|
709
|
-
* linear in it.
|
|
710
|
-
*/
|
|
711
|
-
const pgSaltedPassword = (password: Secret<u8[]>, salt: u8[], iterations: i32): Secret<u8[]> | null =>
|
|
712
|
-
pbkdf2HmacSha256(password, salt, iterations, 32)
|
|
713
|
-
|
|
714
|
-
/**
|
|
715
|
-
* SHA-256 of `data` through a hasher of its own, whose state, block and
|
|
716
|
-
* schedule are wiped before it returns: the one-shot `sha256` leaves the last
|
|
717
|
-
* block of its input in a hasher nothing wipes, and here the input is
|
|
718
|
-
* ClientKey.
|
|
719
|
-
*/
|
|
720
|
-
const pgSha256Wiped = (data: u8[]): u8[] => {
|
|
721
|
-
const h = new Sha256()
|
|
722
|
-
h.update(data, toI32(0), toI32(data.length))
|
|
723
|
-
const digest: u8[] = h.digest()
|
|
724
|
-
wipe(h.state)
|
|
725
|
-
wipe(h.block)
|
|
726
|
-
wipe(h.schedule)
|
|
727
|
-
h.fill = 0
|
|
728
|
-
h.total = 0
|
|
729
|
-
h.finished = true
|
|
730
|
-
return digest
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
/**
|
|
734
|
-
* RFC 5802 §3 from the salted password and the AuthMessage: the ClientProof
|
|
735
|
-
* (32 bytes) followed by the ServerSignature (32 bytes). Every key in between
|
|
736
|
-
* is wiped before it returns.
|
|
737
|
-
*/
|
|
738
|
-
const pgScramKeys = (salted: u8[], authMessage: u8[]): u8[] => {
|
|
739
|
-
const clientKey: u8[] = hmacSha256(salted, pgBytesOf("Client Key"))
|
|
740
|
-
const storedKey: u8[] = pgSha256Wiped(clientKey)
|
|
741
|
-
const clientSignature: u8[] = hmacSha256(storedKey, authMessage)
|
|
742
|
-
const serverKey: u8[] = hmacSha256(salted, pgBytesOf("Server Key"))
|
|
743
|
-
const serverSignature: u8[] = hmacSha256(serverKey, authMessage)
|
|
744
|
-
const out: u8[] = new Array<u8>(64)
|
|
745
|
-
if (clientKey.length < 32 || clientSignature.length < 32 || serverSignature.length < 32) {
|
|
746
|
-
panic("nish/net/postgres: an HMAC-SHA-256 shorter than 32 bytes")
|
|
747
|
-
}
|
|
748
|
-
for (let i: i32 = 0; i < 32; i++) {
|
|
749
|
-
out[i] = clientKey[i] ^ clientSignature[i]
|
|
750
|
-
out[32 + i] = serverSignature[i]
|
|
751
|
-
}
|
|
752
|
-
wipe(clientKey)
|
|
753
|
-
wipe(storedKey)
|
|
754
|
-
wipe(clientSignature)
|
|
755
|
-
wipe(serverKey)
|
|
756
|
-
return out
|
|
757
|
-
}
|
|
758
|
-
|
|
759
719
|
/** Decoders for PostgreSQL's text format, in place; a bad input sets `failed`, which stays set. */
|
|
760
720
|
export class PgText {
|
|
761
721
|
/** Whether a decode since the last `reset()` was given something it could not read. */
|
|
@@ -776,8 +736,17 @@ export class PgText {
|
|
|
776
736
|
* window, a byte that is not a digit, or an overflow.
|
|
777
737
|
*/
|
|
778
738
|
int(b: u8[], start: i32, length: i32): i64 {
|
|
779
|
-
|
|
780
|
-
|
|
739
|
+
// Two `i32`s summed in `i64` cannot overflow; past the buffer is refused,
|
|
740
|
+
// and the test on `end` states for the prover what the one on `last` proves.
|
|
741
|
+
const last: i64 = toI64(start) + toI64(length)
|
|
742
|
+
const end: i32 = toI32(last)
|
|
743
|
+
if (
|
|
744
|
+
length <= 0 ||
|
|
745
|
+
start < 0 ||
|
|
746
|
+
start >= toI32(b.length) ||
|
|
747
|
+
last > toI64(b.length) ||
|
|
748
|
+
end > toI32(b.length)
|
|
749
|
+
) {
|
|
781
750
|
this.failed = true
|
|
782
751
|
return 0
|
|
783
752
|
}
|
|
@@ -790,26 +759,72 @@ export class PgText {
|
|
|
790
759
|
this.failed = true
|
|
791
760
|
return 0
|
|
792
761
|
}
|
|
793
|
-
//
|
|
794
|
-
|
|
795
|
-
//
|
|
796
|
-
|
|
797
|
-
|
|
762
|
+
// A column of at most eighteen bytes holds at most eighteen digits, below
|
|
763
|
+
// 10^18 and so inside i64 whatever they are, and is read without the
|
|
764
|
+
// overflow test. It is read as two runs, each below 10^9, which is how the
|
|
765
|
+
// prover sees that nothing here can wrap: `head` takes digits until it
|
|
766
|
+
// passes 10^8, nine of them at least, so `tail` takes nine at most. The
|
|
767
|
+
// range tests on `head`, `tail` and `scale` cannot fail; they state those
|
|
768
|
+
// bounds.
|
|
769
|
+
if (length <= 18) {
|
|
770
|
+
let head: i64 = 0
|
|
771
|
+
while (i < end && head < 100000000) {
|
|
772
|
+
const d: i32 = toI32(b[i]) - 48
|
|
773
|
+
if (d < 0 || d > 9 || head < 0) {
|
|
774
|
+
this.failed = true
|
|
775
|
+
return 0
|
|
776
|
+
}
|
|
777
|
+
head = head * 10 + toI64(d)
|
|
778
|
+
i++
|
|
779
|
+
}
|
|
780
|
+
let tail: i64 = 0
|
|
781
|
+
let scale: i64 = 1
|
|
782
|
+
while (i < end) {
|
|
783
|
+
const d: i32 = toI32(b[i]) - 48
|
|
784
|
+
if (d < 0 || d > 9 || tail < 0 || tail >= 100000000 || scale < 1 || scale > 100000000) {
|
|
785
|
+
this.failed = true
|
|
786
|
+
return 0
|
|
787
|
+
}
|
|
788
|
+
tail = tail * 10 + toI64(d)
|
|
789
|
+
scale = scale * 10
|
|
790
|
+
i++
|
|
791
|
+
}
|
|
792
|
+
if (
|
|
793
|
+
head < 0 ||
|
|
794
|
+
head >= 1000000000 ||
|
|
795
|
+
tail < 0 ||
|
|
796
|
+
tail >= 1000000000 ||
|
|
797
|
+
scale < 1 ||
|
|
798
|
+
scale > 1000000000
|
|
799
|
+
) {
|
|
800
|
+
this.failed = true
|
|
801
|
+
return 0
|
|
802
|
+
}
|
|
803
|
+
const v: i64 = head * scale + tail
|
|
804
|
+
return negative ? -v : v
|
|
805
|
+
}
|
|
806
|
+
// The magnitude is accumulated in `u64`, which wraps rather than checks,
|
|
807
|
+
// and is held to 2^63 for a negative value and 2^63 - 1 otherwise, so
|
|
808
|
+
// that the most negative i64 fits.
|
|
809
|
+
const lastDigit: u64 = negative ? toU64(8) : toU64(7)
|
|
810
|
+
// 2^63 divided by ten, which a literal cannot spell exactly.
|
|
811
|
+
const maxTenth: u64 = toU64(922337203) * toU64(1000000000) + toU64(685477580)
|
|
812
|
+
let acc: u64 = toU64(0)
|
|
798
813
|
while (i < end) {
|
|
799
814
|
const d: i32 = toI32(b[i]) - 48
|
|
800
815
|
if (d < 0 || d > 9) {
|
|
801
816
|
this.failed = true
|
|
802
817
|
return 0
|
|
803
818
|
}
|
|
804
|
-
const digit:
|
|
805
|
-
if (acc
|
|
819
|
+
const digit: u64 = toU64(d)
|
|
820
|
+
if (acc > maxTenth || (acc === maxTenth && digit > lastDigit)) {
|
|
806
821
|
this.failed = true
|
|
807
822
|
return 0
|
|
808
823
|
}
|
|
809
|
-
acc = acc * 10
|
|
824
|
+
acc = acc * toU64(10) + digit
|
|
810
825
|
i++
|
|
811
826
|
}
|
|
812
|
-
return negative ? acc :
|
|
827
|
+
return negative ? toI64(toU64(0) - acc) : toI64(acc)
|
|
813
828
|
}
|
|
814
829
|
|
|
815
830
|
/**
|
|
@@ -849,12 +864,19 @@ export class PgText {
|
|
|
849
864
|
* for a column it does not read or no room in `out`.
|
|
850
865
|
*/
|
|
851
866
|
timestamp(b: u8[], start: i32, length: i32, out: u8[], at: i32): i32 {
|
|
867
|
+
// Two `i32`s summed in `i64` cannot overflow. The tests against
|
|
868
|
+
// `length - 19` and `length - 24` are what the prover reads the fixed
|
|
869
|
+
// part's and the output's reach from; the others refuse the rest.
|
|
870
|
+
const last: i64 = toI64(start) + toI64(length)
|
|
871
|
+
const end: i32 = toI32(last)
|
|
852
872
|
if (
|
|
853
873
|
start < 0 ||
|
|
854
874
|
length < 19 ||
|
|
855
|
-
start > toI32(b.length) -
|
|
875
|
+
start > toI32(b.length) - 19 ||
|
|
876
|
+
last > toI64(b.length) ||
|
|
877
|
+
end > toI32(b.length) ||
|
|
856
878
|
at < 0 ||
|
|
857
|
-
at
|
|
879
|
+
at > toI32(out.length) - 24
|
|
858
880
|
) {
|
|
859
881
|
this.failed = true
|
|
860
882
|
return -1
|
|
@@ -863,15 +885,10 @@ export class PgText {
|
|
|
863
885
|
// belong and are not digits, so the other fourteen bytes are digits
|
|
864
886
|
// exactly when the part holds five non-digits.
|
|
865
887
|
let p: i32 = start + 19
|
|
866
|
-
const end: i32 = start + length
|
|
867
|
-
if (p < 0 || end > toI32(b.length)) {
|
|
868
|
-
this.failed = true
|
|
869
|
-
return -1
|
|
870
|
-
}
|
|
871
888
|
let others: i32 = 0
|
|
872
889
|
for (let i: i32 = start; i < p && i < toI32(b.length); i++) {
|
|
873
890
|
const c: i32 = toI32(b[i])
|
|
874
|
-
if (c < 48 || c > 57) {
|
|
891
|
+
if ((c < 48 || c > 57) && others < 19) {
|
|
875
892
|
others++
|
|
876
893
|
}
|
|
877
894
|
}
|
|
@@ -886,18 +903,24 @@ export class PgText {
|
|
|
886
903
|
this.failed = true
|
|
887
904
|
return -1
|
|
888
905
|
}
|
|
889
|
-
|
|
906
|
+
// Three digits at most, so the `u32` (which wraps rather than checks)
|
|
907
|
+
// stays below 1000.
|
|
908
|
+
let millis: u32 = 0
|
|
890
909
|
if (p < end && toI32(b[p]) === 46) {
|
|
891
910
|
p++
|
|
892
911
|
let digits: i32 = 0
|
|
893
912
|
while (p < end && toI32(b[p]) >= 48 && toI32(b[p]) <= 57) {
|
|
913
|
+
if (digits >= 6) {
|
|
914
|
+
this.failed = true
|
|
915
|
+
return -1
|
|
916
|
+
}
|
|
894
917
|
if (digits < 3) {
|
|
895
|
-
millis = millis * 10 +
|
|
918
|
+
millis = millis * 10 + toU32(b[p]) - 48
|
|
896
919
|
}
|
|
897
920
|
digits++
|
|
898
921
|
p++
|
|
899
922
|
}
|
|
900
|
-
if (digits < 1
|
|
923
|
+
if (digits < 1) {
|
|
901
924
|
this.failed = true
|
|
902
925
|
return -1
|
|
903
926
|
}
|
|
@@ -906,7 +929,13 @@ export class PgText {
|
|
|
906
929
|
digits++
|
|
907
930
|
}
|
|
908
931
|
}
|
|
909
|
-
if (
|
|
932
|
+
if (
|
|
933
|
+
p < toI32(b.length) - 2 &&
|
|
934
|
+
p + 3 === end &&
|
|
935
|
+
toI32(b[p]) === 43 &&
|
|
936
|
+
toI32(b[p + 1]) === 48 &&
|
|
937
|
+
toI32(b[p + 2]) === 48
|
|
938
|
+
) {
|
|
910
939
|
p = end
|
|
911
940
|
}
|
|
912
941
|
// The year's four bytes are digits, so their OR is '0' exactly when the year is 0000.
|
|
@@ -926,20 +955,19 @@ export class PgText {
|
|
|
926
955
|
}
|
|
927
956
|
}
|
|
928
957
|
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
for (let i: i32 = width - 1; i >= 0; i--) {
|
|
941
|
-
out[at + i] = toU8((v % 10) + 48)
|
|
958
|
+
/**
|
|
959
|
+
* `value`'s last `width` decimal digits into `out[at, at + width)`; the part
|
|
960
|
+
* of the window outside `out` is not written, and every caller's is inside.
|
|
961
|
+
*/
|
|
962
|
+
const pgDigits = (out: u8[], at: i32, value: u32, width: i32): void => {
|
|
963
|
+
// A `u32` wraps rather than checks; the digits are its remainders.
|
|
964
|
+
let v: u32 = value
|
|
965
|
+
// Two `i32`s summed in `i64` cannot overflow.
|
|
966
|
+
let k: i32 = toI32(toI64(at) + toI64(width) - 1)
|
|
967
|
+
while (k >= at && k >= 0 && k < toI32(out.length)) {
|
|
968
|
+
out[k] = toU8((v % 10) + 48)
|
|
942
969
|
v = v / 10
|
|
970
|
+
k--
|
|
943
971
|
}
|
|
944
972
|
}
|
|
945
973
|
|
|
@@ -1000,7 +1028,7 @@ export class PgBinary {
|
|
|
1000
1028
|
* @returns True for 1, false for 0; false with `failed` set otherwise.
|
|
1001
1029
|
*/
|
|
1002
1030
|
bool(b: u8[], start: i32, length: i32): boolean {
|
|
1003
|
-
if (length === 1 &&
|
|
1031
|
+
if (length === 1 && start >= 0 && start < toI32(b.length)) {
|
|
1004
1032
|
const c: i32 = toI32(b[start])
|
|
1005
1033
|
if (c === 1) {
|
|
1006
1034
|
return true
|
|
@@ -1066,35 +1094,40 @@ export class PgBinary {
|
|
|
1066
1094
|
this.failed = true
|
|
1067
1095
|
return -1
|
|
1068
1096
|
}
|
|
1069
|
-
//
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1097
|
+
// Shifted by 730120 days, a whole number of them, so that every day
|
|
1098
|
+
// written, from 0001-01-01 (day -730119 counted from 2000-01-01) to
|
|
1099
|
+
// 9999-12-31 (day 2921939), is non-negative and plain division floors:
|
|
1100
|
+
// a time before 2000 keeps its day and is cut toward the past. `u64`
|
|
1101
|
+
// wraps rather than checks; a time more than 730120 days before 2000
|
|
1102
|
+
// wraps past 2^63 and is refused with every other day outside that span.
|
|
1103
|
+
const shifted: u64 = toU64(micros) + toU64(730120) * toU64(86400000) * toU64(1000)
|
|
1104
|
+
const ms: u64 = shifted / 1000
|
|
1105
|
+
const day0: u64 = ms / 86400000
|
|
1106
|
+
if (day0 < 1 || day0 > 3652059) {
|
|
1078
1107
|
this.failed = true
|
|
1079
1108
|
return -1
|
|
1080
1109
|
}
|
|
1081
1110
|
// The civil date of a day count: eras of 400 years (146097 days), each
|
|
1082
|
-
// year starting on 1 March so that a leap day is its last.
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
const
|
|
1088
|
-
const
|
|
1089
|
-
const
|
|
1090
|
-
const
|
|
1091
|
-
const
|
|
1092
|
-
const
|
|
1093
|
-
|
|
1111
|
+
// year starting on 1 March so that a leap day is its last. The days from
|
|
1112
|
+
// 0000-03-01, where that year starts, to 2000-01-01, where PostgreSQL's
|
|
1113
|
+
// binary times count from, are 730425: 305 more than the shift.
|
|
1114
|
+
// Every value below is a small non-negative number, so `u32`, which wraps
|
|
1115
|
+
// rather than checks, computes it exactly.
|
|
1116
|
+
const z: u32 = toU32(day0) + 305
|
|
1117
|
+
const era: u32 = z / 146097
|
|
1118
|
+
const doe: u32 = z - era * 146097
|
|
1119
|
+
const yoe: u32 = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365
|
|
1120
|
+
const doy: u32 = doe - (365 * yoe + yoe / 4 - yoe / 100)
|
|
1121
|
+
const mp: u32 = (5 * doy + 2) / 153
|
|
1122
|
+
const day: u32 = doy - (153 * mp + 2) / 5 + 1
|
|
1123
|
+
const month: u32 = mp < 10 ? mp + 3 : mp - 9
|
|
1124
|
+
const year: u32 = yoe + era * 400 + (month <= 2 ? 1 : 0)
|
|
1125
|
+
const millis: u32 = toU32(ms % 86400000)
|
|
1126
|
+
pgDigits(out, at, year, 4)
|
|
1094
1127
|
out[at + 4] = toU8(45)
|
|
1095
|
-
pgDigits(out, at + 5,
|
|
1128
|
+
pgDigits(out, at + 5, month, 2)
|
|
1096
1129
|
out[at + 7] = toU8(45)
|
|
1097
|
-
pgDigits(out, at + 8,
|
|
1130
|
+
pgDigits(out, at + 8, day, 2)
|
|
1098
1131
|
out[at + 10] = toU8(84)
|
|
1099
1132
|
pgDigits(out, at + 11, millis / 3600000, 2)
|
|
1100
1133
|
out[at + 13] = toU8(58)
|
|
@@ -1167,8 +1200,17 @@ export class PgBinaryArray {
|
|
|
1167
1200
|
this.seen = 0
|
|
1168
1201
|
this.start = 0
|
|
1169
1202
|
this.length = 0
|
|
1170
|
-
|
|
1171
|
-
|
|
1203
|
+
// Two `i32`s summed in `i64` cannot overflow; the test on `end` and the
|
|
1204
|
+
// one against the header state for the prover what the one on `last` proves.
|
|
1205
|
+
const last: i64 = toI64(start) + toI64(length)
|
|
1206
|
+
const end: i32 = toI32(last)
|
|
1207
|
+
if (
|
|
1208
|
+
start < 0 ||
|
|
1209
|
+
length < PG_ARRAY_HEADER ||
|
|
1210
|
+
last > toI64(b.length) ||
|
|
1211
|
+
end > toI32(b.length) ||
|
|
1212
|
+
start > toI32(b.length) - 12
|
|
1213
|
+
) {
|
|
1172
1214
|
this.refuse()
|
|
1173
1215
|
return
|
|
1174
1216
|
}
|
|
@@ -1193,16 +1235,19 @@ export class PgBinaryArray {
|
|
|
1193
1235
|
}
|
|
1194
1236
|
this.elementOid = oid
|
|
1195
1237
|
this.hasNull = hasNull === 1
|
|
1196
|
-
|
|
1197
|
-
this.
|
|
1238
|
+
const pos: i32 = start + PG_ARRAY_HEADER
|
|
1239
|
+
this.pos = pos
|
|
1240
|
+
this.end = end
|
|
1198
1241
|
if (ndim === 1) {
|
|
1199
|
-
const dim: i32 = getI32BE(b,
|
|
1200
|
-
|
|
1242
|
+
const dim: i32 = getI32BE(b, pos)
|
|
1243
|
+
// 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) {
|
|
1201
1246
|
this.refuse()
|
|
1202
1247
|
return
|
|
1203
1248
|
}
|
|
1204
1249
|
this.count = dim
|
|
1205
|
-
this.pos
|
|
1250
|
+
this.pos = pos + PG_ARRAY_DIM
|
|
1206
1251
|
}
|
|
1207
1252
|
}
|
|
1208
1253
|
|
|
@@ -1223,32 +1268,40 @@ export class PgBinaryArray {
|
|
|
1223
1268
|
if (this.bad) {
|
|
1224
1269
|
return PG_ARRAY_BAD
|
|
1225
1270
|
}
|
|
1226
|
-
|
|
1227
|
-
|
|
1271
|
+
const seen: i32 = this.seen
|
|
1272
|
+
const pos: i32 = this.pos
|
|
1273
|
+
const end: i32 = this.end
|
|
1274
|
+
if (seen < 0 || seen >= this.count) {
|
|
1275
|
+
if (seen >= 0 && pos === end) {
|
|
1228
1276
|
return PG_ARRAY_END
|
|
1229
1277
|
}
|
|
1230
1278
|
return this.refuse()
|
|
1231
1279
|
}
|
|
1232
|
-
|
|
1280
|
+
// `begin` keeps `seen` and `pos` from going negative; summed in `i64`,
|
|
1281
|
+
// a length cannot overflow on its way to the test against `end`.
|
|
1282
|
+
const p: i64 = toI64(pos) + toI64(4)
|
|
1283
|
+
if (pos < 0 || p > toI64(end)) {
|
|
1233
1284
|
return this.refuse()
|
|
1234
1285
|
}
|
|
1235
|
-
const n: i32 = getI32BE(this.b,
|
|
1236
|
-
|
|
1237
|
-
this.
|
|
1286
|
+
const n: i32 = getI32BE(this.b, pos)
|
|
1287
|
+
const at: i32 = toI32(p)
|
|
1288
|
+
this.pos = at
|
|
1289
|
+
this.seen = seen + 1
|
|
1238
1290
|
if (n === -1) {
|
|
1239
1291
|
if (!this.hasNull) {
|
|
1240
1292
|
return this.refuse()
|
|
1241
1293
|
}
|
|
1242
|
-
this.start =
|
|
1294
|
+
this.start = at
|
|
1243
1295
|
this.length = 0
|
|
1244
1296
|
return PG_ARRAY_NULL
|
|
1245
1297
|
}
|
|
1246
|
-
|
|
1298
|
+
const q: i64 = toI64(at) + toI64(n)
|
|
1299
|
+
if (n !== this.size || q > toI64(end)) {
|
|
1247
1300
|
return this.refuse()
|
|
1248
1301
|
}
|
|
1249
|
-
this.start =
|
|
1302
|
+
this.start = at
|
|
1250
1303
|
this.length = n
|
|
1251
|
-
this.pos
|
|
1304
|
+
this.pos = toI32(q)
|
|
1252
1305
|
return PG_ARRAY_ELEMENT
|
|
1253
1306
|
}
|
|
1254
1307
|
|
|
@@ -1306,12 +1359,26 @@ export class PgArrayWalker {
|
|
|
1306
1359
|
begin(b: u8[], start: i32, length: i32): void {
|
|
1307
1360
|
this.b = b
|
|
1308
1361
|
this.first = true
|
|
1309
|
-
this.
|
|
1310
|
-
|
|
1311
|
-
|
|
1362
|
+
this.pos = 0
|
|
1363
|
+
this.end = 0
|
|
1364
|
+
// Two `i32`s summed in `i64` cannot overflow; the tests on `end` state for
|
|
1365
|
+
// the prover what the ones on `start`, `length` and `last` prove.
|
|
1366
|
+
const last: i64 = toI64(start) + toI64(length)
|
|
1367
|
+
const end: i32 = toI32(last)
|
|
1368
|
+
if (
|
|
1369
|
+
start < 0 ||
|
|
1370
|
+
length < 2 ||
|
|
1371
|
+
last > toI64(b.length) ||
|
|
1372
|
+
end > toI32(b.length) ||
|
|
1373
|
+
end < 2 ||
|
|
1374
|
+
start >= toI32(b.length)
|
|
1375
|
+
) {
|
|
1376
|
+
this.bad = true
|
|
1377
|
+
return
|
|
1312
1378
|
}
|
|
1379
|
+
this.bad = toI32(b[start]) !== 123 || toI32(b[end - 1]) !== 125
|
|
1313
1380
|
this.pos = start + 1
|
|
1314
|
-
this.end =
|
|
1381
|
+
this.end = end - 1
|
|
1315
1382
|
}
|
|
1316
1383
|
|
|
1317
1384
|
private refuse(): i32 {
|
|
@@ -1331,49 +1398,50 @@ export class PgArrayWalker {
|
|
|
1331
1398
|
}
|
|
1332
1399
|
const b: u8[] = this.b
|
|
1333
1400
|
const end: i32 = this.end
|
|
1334
|
-
|
|
1401
|
+
let p: i32 = this.pos
|
|
1402
|
+
if (p < 0 || p > end || end > toI32(b.length)) {
|
|
1335
1403
|
return this.refuse()
|
|
1336
1404
|
}
|
|
1405
|
+
if (p >= end) {
|
|
1406
|
+
this.first = false
|
|
1407
|
+
return PG_ARRAY_END
|
|
1408
|
+
}
|
|
1337
1409
|
if (this.first) {
|
|
1338
1410
|
this.first = false
|
|
1339
|
-
if (this.pos === end) {
|
|
1340
|
-
return PG_ARRAY_END
|
|
1341
|
-
}
|
|
1342
1411
|
} else {
|
|
1343
|
-
if (
|
|
1344
|
-
return PG_ARRAY_END
|
|
1345
|
-
}
|
|
1346
|
-
if (toI32(b[this.pos]) !== 44) {
|
|
1412
|
+
if (toI32(b[p]) !== 44) {
|
|
1347
1413
|
return this.refuse()
|
|
1348
1414
|
}
|
|
1349
|
-
|
|
1350
|
-
}
|
|
1351
|
-
let p: i32 = this.pos
|
|
1352
|
-
if (p < 0) {
|
|
1353
|
-
return this.refuse()
|
|
1415
|
+
p++
|
|
1354
1416
|
}
|
|
1355
1417
|
this.escaped = false
|
|
1356
1418
|
if (p < end && toI32(b[p]) === 34) {
|
|
1357
1419
|
p++
|
|
1358
|
-
|
|
1420
|
+
const first: i32 = p
|
|
1359
1421
|
while (p < end && toI32(b[p]) !== 34) {
|
|
1360
1422
|
if (toI32(b[p]) === 92) {
|
|
1361
1423
|
this.escaped = true
|
|
1362
1424
|
p++
|
|
1425
|
+
// An escape needs a byte after it before the closing quote.
|
|
1426
|
+
if (p >= end) {
|
|
1427
|
+
return this.refuse()
|
|
1428
|
+
}
|
|
1363
1429
|
}
|
|
1364
1430
|
p++
|
|
1365
1431
|
}
|
|
1366
1432
|
if (p >= end) {
|
|
1367
1433
|
return this.refuse()
|
|
1368
1434
|
}
|
|
1369
|
-
this.
|
|
1370
|
-
this.
|
|
1371
|
-
|
|
1435
|
+
this.start = first
|
|
1436
|
+
this.length = p - first
|
|
1437
|
+
p++
|
|
1438
|
+
this.pos = p
|
|
1439
|
+
if (p < end && toI32(b[p]) !== 44) {
|
|
1372
1440
|
return this.refuse()
|
|
1373
1441
|
}
|
|
1374
1442
|
return PG_ARRAY_ELEMENT
|
|
1375
1443
|
}
|
|
1376
|
-
|
|
1444
|
+
const first: i32 = p
|
|
1377
1445
|
while (p < end && toI32(b[p]) !== 44) {
|
|
1378
1446
|
const c: i32 = toI32(b[p])
|
|
1379
1447
|
if (c === 123 || c === 125 || c === 34 || c === 92 || c === 32) {
|
|
@@ -1381,12 +1449,15 @@ export class PgArrayWalker {
|
|
|
1381
1449
|
}
|
|
1382
1450
|
p++
|
|
1383
1451
|
}
|
|
1384
|
-
this.
|
|
1452
|
+
this.start = first
|
|
1453
|
+
this.length = p - first
|
|
1385
1454
|
this.pos = p
|
|
1386
|
-
if (
|
|
1455
|
+
if (p === first) {
|
|
1387
1456
|
return this.refuse()
|
|
1388
1457
|
}
|
|
1389
|
-
|
|
1458
|
+
// The scan stopped at or before `end`, inside the buffer; the test on `p`
|
|
1459
|
+
// states that bound for the prover.
|
|
1460
|
+
if (this.length === 4 && p >= 4 && p <= toI32(b.length)) {
|
|
1390
1461
|
const n: i32 = toI32(b[p - 4]) | 32
|
|
1391
1462
|
const u: i32 = toI32(b[p - 3]) | 32
|
|
1392
1463
|
const l1: i32 = toI32(b[p - 2]) | 32
|
|
@@ -1408,24 +1479,25 @@ export class PgArrayWalker {
|
|
|
1408
1479
|
*/
|
|
1409
1480
|
copy(out: u8[], at: i32): i32 {
|
|
1410
1481
|
const b: u8[] = this.b
|
|
1411
|
-
let n: i32 = 0
|
|
1412
1482
|
let p: i32 = this.start
|
|
1413
|
-
const
|
|
1414
|
-
|
|
1483
|
+
const last: i64 = toI64(p) + toI64(this.length)
|
|
1484
|
+
const end: i32 = toI32(last)
|
|
1485
|
+
if (p < 0 || at < 0 || last > toI64(b.length) || end > toI32(b.length)) {
|
|
1415
1486
|
return -1
|
|
1416
1487
|
}
|
|
1488
|
+
let k: i32 = at
|
|
1417
1489
|
while (p < end) {
|
|
1418
1490
|
if (toI32(b[p]) === 92) {
|
|
1419
1491
|
p++
|
|
1420
1492
|
}
|
|
1421
|
-
if (p >= end ||
|
|
1493
|
+
if (p >= end || k >= toI32(out.length)) {
|
|
1422
1494
|
return -1
|
|
1423
1495
|
}
|
|
1424
|
-
out[
|
|
1425
|
-
|
|
1496
|
+
out[k] = b[p]
|
|
1497
|
+
k++
|
|
1426
1498
|
p++
|
|
1427
1499
|
}
|
|
1428
|
-
return
|
|
1500
|
+
return k - at
|
|
1429
1501
|
}
|
|
1430
1502
|
}
|
|
1431
1503
|
|
|
@@ -1539,7 +1611,7 @@ export class PgConnection {
|
|
|
1539
1611
|
}
|
|
1540
1612
|
this.clientNonce = base64StdPadEncode(head)
|
|
1541
1613
|
|
|
1542
|
-
const at: i32 =
|
|
1614
|
+
const at: i32 = 0
|
|
1543
1615
|
this.putI32(PG_ZERO)
|
|
1544
1616
|
this.putI32(PG_PROTOCOL)
|
|
1545
1617
|
this.putParam("user", config.user)
|
|
@@ -1556,7 +1628,8 @@ export class PgConnection {
|
|
|
1556
1628
|
this.putParam("options", config.options)
|
|
1557
1629
|
}
|
|
1558
1630
|
this.putByte(PG_ZERO)
|
|
1559
|
-
|
|
1631
|
+
// The message is the first in the buffer, so its length is the buffer's end.
|
|
1632
|
+
setI32BE(this.out, at, this.outEnd)
|
|
1560
1633
|
}
|
|
1561
1634
|
|
|
1562
1635
|
// --- Buffers -----------------------------------------------------------------
|
|
@@ -1585,7 +1658,12 @@ export class PgConnection {
|
|
|
1585
1658
|
* is full, which `next()` mends by compacting or growing it.
|
|
1586
1659
|
*/
|
|
1587
1660
|
inputRoom(): i32 {
|
|
1588
|
-
|
|
1661
|
+
const end: i32 = this.inEnd
|
|
1662
|
+
// `inEnd` stays inside the buffer; the test states that bound for the prover.
|
|
1663
|
+
if (end < 0 || end > toI32(this.input.length)) {
|
|
1664
|
+
return 0
|
|
1665
|
+
}
|
|
1666
|
+
return toI32(this.input.length) - end
|
|
1589
1667
|
}
|
|
1590
1668
|
|
|
1591
1669
|
/**
|
|
@@ -1594,10 +1672,12 @@ export class PgConnection {
|
|
|
1594
1672
|
* @param n The bytes the read wrote, from 0 to `inputRoom()`.
|
|
1595
1673
|
*/
|
|
1596
1674
|
received(n: i32): void {
|
|
1597
|
-
|
|
1675
|
+
// Summed in `i64`, the end cannot overflow on its way to the test.
|
|
1676
|
+
const last: i64 = toI64(this.inEnd) + toI64(n)
|
|
1677
|
+
if (n < 0 || n > this.inputRoom() || last > toI64(this.input.length)) {
|
|
1598
1678
|
panic("nish/net/postgres: received more than inputRoom()")
|
|
1599
1679
|
}
|
|
1600
|
-
this.inEnd
|
|
1680
|
+
this.inEnd = toI32(last)
|
|
1601
1681
|
}
|
|
1602
1682
|
|
|
1603
1683
|
/**
|
|
@@ -1611,13 +1691,14 @@ export class PgConnection {
|
|
|
1611
1691
|
* caller's to give again after `next()`.
|
|
1612
1692
|
*/
|
|
1613
1693
|
receive(src: u8[], start: i32, length: i32): i32 {
|
|
1614
|
-
if (start < 0 || length < 0 || length >
|
|
1694
|
+
if (start < 0 || length < 0 || toI64(start) + toI64(length) > toI64(src.length)) {
|
|
1615
1695
|
panic("nish/net/postgres: receive() outside its source")
|
|
1616
1696
|
}
|
|
1617
1697
|
const room: i32 = this.inputRoom()
|
|
1618
1698
|
const n: i32 = length < room ? length : room
|
|
1619
1699
|
copyBytes(this.input, this.inEnd, src, start, n)
|
|
1620
|
-
|
|
1700
|
+
// `inputRoom` keeps the end inside the buffer, so the sum fits an `i32`.
|
|
1701
|
+
this.inEnd = toI32(toI64(this.inEnd) + toI64(n))
|
|
1621
1702
|
return n
|
|
1622
1703
|
}
|
|
1623
1704
|
|
|
@@ -1655,10 +1736,12 @@ export class PgConnection {
|
|
|
1655
1736
|
* @param n The bytes the write took.
|
|
1656
1737
|
*/
|
|
1657
1738
|
sent(n: i32): void {
|
|
1658
|
-
|
|
1739
|
+
// Summed in `i64`, the start cannot overflow on its way to the test.
|
|
1740
|
+
const last: i64 = toI64(this.outStart) + toI64(n)
|
|
1741
|
+
if (n < 0 || last > toI64(this.outputEnd())) {
|
|
1659
1742
|
panic("nish/net/postgres: sent more than was queued")
|
|
1660
1743
|
}
|
|
1661
|
-
this.outStart
|
|
1744
|
+
this.outStart = toI32(last)
|
|
1662
1745
|
if (this.outStart === this.outEnd) {
|
|
1663
1746
|
this.outStart = 0
|
|
1664
1747
|
this.outEnd = 0
|
|
@@ -1732,15 +1815,20 @@ export class PgConnection {
|
|
|
1732
1815
|
if (this.state === PG_STATE_FAILED) {
|
|
1733
1816
|
return PG_FAILED
|
|
1734
1817
|
}
|
|
1735
|
-
const
|
|
1818
|
+
const at: i32 = this.inStart
|
|
1819
|
+
const inEnd: i32 = this.inEnd
|
|
1820
|
+
const b: u8[] = this.input
|
|
1821
|
+
// inStart and inEnd stay within the buffer, so these tests never fail;
|
|
1822
|
+
// they prove the subtraction and the indices.
|
|
1823
|
+
if (at < 0 || inEnd < 0 || inEnd < at || inEnd > toI32(b.length)) {
|
|
1824
|
+
return this.fail(PG_ERR_FRAME)
|
|
1825
|
+
}
|
|
1826
|
+
const avail: i32 = inEnd - at
|
|
1736
1827
|
if (avail < 5) {
|
|
1737
1828
|
this.compact()
|
|
1738
1829
|
return PG_NEED_MORE
|
|
1739
1830
|
}
|
|
1740
|
-
|
|
1741
|
-
const b: u8[] = this.input
|
|
1742
|
-
// inStart and inEnd stay within the buffer, so this test never fails; it proves the indices.
|
|
1743
|
-
if (at < 0 || at + 4 >= toI32(b.length)) {
|
|
1831
|
+
if (at > toI32(b.length) - 5) {
|
|
1744
1832
|
return this.fail(PG_ERR_FRAME)
|
|
1745
1833
|
}
|
|
1746
1834
|
const type: i32 = toI32(b[at])
|
|
@@ -1756,18 +1844,23 @@ export class PgConnection {
|
|
|
1756
1844
|
if (avail - 1 < length) {
|
|
1757
1845
|
this.compact()
|
|
1758
1846
|
const size: i32 = toI32(this.input.length)
|
|
1759
|
-
if (size
|
|
1760
|
-
// Doubled, up to one message of maxMessage, so that a run of growing
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
const
|
|
1847
|
+
if (size <= length) {
|
|
1848
|
+
// Doubled, up to one message of maxMessage, so that a run of growing
|
|
1849
|
+
// messages copies each byte a bounded number of times. Reckoned in
|
|
1850
|
+
// `i64`, where no `i32` doubled or plus one overflows.
|
|
1851
|
+
const cap: i64 = toI64(this.config.maxMessage) + toI64(1)
|
|
1852
|
+
const want: i64 = toI64(length) + toI64(1)
|
|
1853
|
+
const doubled: i64 = toI64(size) * toI64(2) < cap ? toI64(size) * toI64(2) : cap
|
|
1854
|
+
const grown: u8[] = new Array<u8>(toI32(doubled > want ? doubled : want))
|
|
1764
1855
|
grown.set(this.input, 0)
|
|
1765
1856
|
this.input = grown
|
|
1766
1857
|
}
|
|
1767
1858
|
return PG_NEED_MORE
|
|
1768
1859
|
}
|
|
1769
|
-
|
|
1770
|
-
const
|
|
1860
|
+
// The whole message is in `[at, inEnd)`, so its end fits an `i32`.
|
|
1861
|
+
const end: i32 = toI32(toI64(at) + toI64(1) + toI64(length))
|
|
1862
|
+
this.inStart = end
|
|
1863
|
+
const event: i32 = this.dispatch(type, at + 5, end)
|
|
1771
1864
|
if (event !== PG_NEED_MORE) {
|
|
1772
1865
|
return event
|
|
1773
1866
|
}
|
|
@@ -1777,10 +1870,12 @@ export class PgConnection {
|
|
|
1777
1870
|
/** Moves the unread input, at most one partial message, to the start of the buffer. */
|
|
1778
1871
|
private compact(): void {
|
|
1779
1872
|
const start: i32 = this.inStart
|
|
1780
|
-
|
|
1873
|
+
const end: i32 = this.inEnd
|
|
1874
|
+
// `inEnd` never falls below `inStart`; the test states that for the prover.
|
|
1875
|
+
if (start <= 0 || end < 0 || end < start) {
|
|
1781
1876
|
return
|
|
1782
1877
|
}
|
|
1783
|
-
const n: i32 =
|
|
1878
|
+
const n: i32 = end - start
|
|
1784
1879
|
copyBytes(this.input, 0, this.input, start, n)
|
|
1785
1880
|
this.inStart = 0
|
|
1786
1881
|
this.inEnd = n
|
|
@@ -1799,12 +1894,21 @@ export class PgConnection {
|
|
|
1799
1894
|
/** One message, `[body, end)` its contents; answers its event, or `PG_NEED_MORE` when it has none. */
|
|
1800
1895
|
private dispatch(type: i32, body: i32, end: i32): i32 {
|
|
1801
1896
|
const state: i32 = this.state
|
|
1897
|
+
const b: u8[] = this.input
|
|
1898
|
+
// `next` hands over a message inside the buffer; the test states that bound for the prover.
|
|
1899
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length)) {
|
|
1900
|
+
return this.fail(PG_ERR_FRAME)
|
|
1901
|
+
}
|
|
1902
|
+
const size: i32 = end - body
|
|
1802
1903
|
if (type === 82) {
|
|
1803
1904
|
// 'R': an authentication request.
|
|
1804
|
-
if (state >= PG_STATE_STARTING ||
|
|
1805
|
-
return this.fail(
|
|
1905
|
+
if (state >= PG_STATE_STARTING || size < 4) {
|
|
1906
|
+
return this.fail(size < 4 ? PG_ERR_FRAME : PG_ERR_UNEXPECTED)
|
|
1806
1907
|
}
|
|
1807
|
-
|
|
1908
|
+
if (body > toI32(b.length) - 4) {
|
|
1909
|
+
return this.fail(PG_ERR_FRAME)
|
|
1910
|
+
}
|
|
1911
|
+
return this.authenticate(getI32BE(b, body), body + 4, end)
|
|
1808
1912
|
}
|
|
1809
1913
|
if (type === 69 || type === 78) {
|
|
1810
1914
|
// 'E' and 'N': an error or a notice.
|
|
@@ -1813,8 +1917,9 @@ export class PgConnection {
|
|
|
1813
1917
|
return this.fail(PG_ERR_FRAME)
|
|
1814
1918
|
}
|
|
1815
1919
|
if (type === 78) {
|
|
1816
|
-
|
|
1817
|
-
|
|
1920
|
+
const notices: i32 = this.notices
|
|
1921
|
+
if (notices >= 0 && notices < 2147483647) {
|
|
1922
|
+
this.notices = notices + 1
|
|
1818
1923
|
}
|
|
1819
1924
|
this.lastNotice = error
|
|
1820
1925
|
return PG_NOTICE
|
|
@@ -1831,27 +1936,42 @@ export class PgConnection {
|
|
|
1831
1936
|
}
|
|
1832
1937
|
if (type === 83) {
|
|
1833
1938
|
// 'S': ParameterStatus, name and value.
|
|
1834
|
-
const nameEnd: i32 = pgFind(
|
|
1835
|
-
|
|
1939
|
+
const nameEnd: i32 = pgFind(b, body, end, 0)
|
|
1940
|
+
// pgFind answers inside `[body, end]`; the test states that bound for the prover.
|
|
1941
|
+
if (nameEnd < 0 || nameEnd < body || nameEnd >= end) {
|
|
1942
|
+
return this.fail(PG_ERR_FRAME)
|
|
1943
|
+
}
|
|
1944
|
+
const value: i32 = nameEnd + 1
|
|
1945
|
+
const valueEnd: i32 = end - 1
|
|
1946
|
+
if (valueEnd < 0 || valueEnd < value || pgFind(b, value, end, 0) !== valueEnd) {
|
|
1836
1947
|
return this.fail(PG_ERR_FRAME)
|
|
1837
1948
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1949
|
+
const nameLength: i32 = nameEnd - body
|
|
1950
|
+
const valueLength: i32 = valueEnd - value
|
|
1951
|
+
if (pgBytesAre(b, body, nameLength, "client_encoding")) {
|
|
1952
|
+
if (!pgBytesAre(b, value, valueLength, "UTF8")) {
|
|
1840
1953
|
return this.fail(PG_ERR_ENCODING)
|
|
1841
1954
|
}
|
|
1842
|
-
} else if (pgBytesAre(
|
|
1955
|
+
} else if (pgBytesAre(b, body, nameLength, "DateStyle")) {
|
|
1843
1956
|
// "ISO, MDY": the output style comes first, the input order after the comma.
|
|
1844
|
-
|
|
1845
|
-
const iso: boolean = valueLength >= 3 && pgBytesAre(this.input, nameEnd + 1, 3, "ISO")
|
|
1846
|
-
if (!iso || (valueLength > 3 && toI32(this.input[nameEnd + 4]) !== PG_COMMA)) {
|
|
1957
|
+
if (valueLength < 3 || !pgBytesAre(b, value, 3, "ISO")) {
|
|
1847
1958
|
return this.fail(PG_ERR_SETTING)
|
|
1848
1959
|
}
|
|
1849
|
-
|
|
1850
|
-
|
|
1960
|
+
if (valueLength > 3) {
|
|
1961
|
+
// The value runs past its fourth byte; the test states that bound for the prover.
|
|
1962
|
+
if (value > toI32(b.length) - 4) {
|
|
1963
|
+
return this.fail(PG_ERR_SETTING)
|
|
1964
|
+
}
|
|
1965
|
+
if (toI32(b[value + 3]) !== PG_COMMA) {
|
|
1966
|
+
return this.fail(PG_ERR_SETTING)
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
} else if (pgBytesAre(b, body, nameLength, "TimeZone")) {
|
|
1970
|
+
if (!pgBytesAre(b, value, valueLength, "UTC")) {
|
|
1851
1971
|
return this.fail(PG_ERR_SETTING)
|
|
1852
1972
|
}
|
|
1853
|
-
} else if (pgBytesAre(
|
|
1854
|
-
this.serverVersion = String.fromBytes(
|
|
1973
|
+
} else if (pgBytesAre(b, body, nameLength, "server_version")) {
|
|
1974
|
+
this.serverVersion = String.fromBytes(b, value, valueEnd)
|
|
1855
1975
|
}
|
|
1856
1976
|
return PG_NEED_MORE
|
|
1857
1977
|
}
|
|
@@ -1864,18 +1984,18 @@ export class PgConnection {
|
|
|
1864
1984
|
}
|
|
1865
1985
|
if (type === 75) {
|
|
1866
1986
|
// 'K': BackendKeyData, the process id and the cancel key.
|
|
1867
|
-
if (state !== PG_STATE_STARTING ||
|
|
1987
|
+
if (state !== PG_STATE_STARTING || size !== 8) {
|
|
1868
1988
|
return this.fail(state !== PG_STATE_STARTING ? PG_ERR_UNEXPECTED : PG_ERR_FRAME)
|
|
1869
1989
|
}
|
|
1870
|
-
this.backendPid = getI32BE(
|
|
1990
|
+
this.backendPid = getI32BE(b, body)
|
|
1871
1991
|
return PG_NEED_MORE
|
|
1872
1992
|
}
|
|
1873
1993
|
if (type === 90) {
|
|
1874
1994
|
// 'Z': ReadyForQuery, and the transaction status.
|
|
1875
|
-
if (
|
|
1995
|
+
if (size !== 1 || body >= toI32(b.length)) {
|
|
1876
1996
|
return this.fail(PG_ERR_FRAME)
|
|
1877
1997
|
}
|
|
1878
|
-
const status: i32 = toI32(
|
|
1998
|
+
const status: i32 = toI32(b[body])
|
|
1879
1999
|
if (status !== 73 && status !== 84 && status !== 69) {
|
|
1880
2000
|
return this.fail(PG_ERR_FRAME)
|
|
1881
2001
|
}
|
|
@@ -1884,10 +2004,11 @@ export class PgConnection {
|
|
|
1884
2004
|
this.state = PG_STATE_READY
|
|
1885
2005
|
return PG_READY
|
|
1886
2006
|
}
|
|
1887
|
-
|
|
2007
|
+
const inFlight: i32 = this.inFlight
|
|
2008
|
+
if (inFlight <= 0 || (inFlight === 1 && this.groupOpen)) {
|
|
1888
2009
|
return this.fail(PG_ERR_UNEXPECTED)
|
|
1889
2010
|
}
|
|
1890
|
-
this.inFlight
|
|
2011
|
+
this.inFlight = inFlight - 1
|
|
1891
2012
|
this.groupFailed = this.errorInGroup
|
|
1892
2013
|
this.errorInGroup = false
|
|
1893
2014
|
return PG_GROUP_DONE
|
|
@@ -1900,11 +2021,11 @@ export class PgConnection {
|
|
|
1900
2021
|
}
|
|
1901
2022
|
if (type === 67) {
|
|
1902
2023
|
// 'C': CommandComplete, the tag.
|
|
1903
|
-
if (
|
|
2024
|
+
if (size < 1 || pgFind(b, body, end, 0) !== end - 1) {
|
|
1904
2025
|
return this.fail(PG_ERR_FRAME)
|
|
1905
2026
|
}
|
|
1906
2027
|
this.tagStart = body
|
|
1907
|
-
this.tagLength =
|
|
2028
|
+
this.tagLength = size - 1
|
|
1908
2029
|
return PG_COMPLETE
|
|
1909
2030
|
}
|
|
1910
2031
|
if (type === 73) {
|
|
@@ -1922,7 +2043,7 @@ export class PgConnection {
|
|
|
1922
2043
|
}
|
|
1923
2044
|
if (type === 116) {
|
|
1924
2045
|
// 't': ParameterDescription.
|
|
1925
|
-
if (
|
|
2046
|
+
if (size < 2 || size !== toI32(getU16BE(b, body)) * 4 + 2) {
|
|
1926
2047
|
return this.fail(PG_ERR_FRAME)
|
|
1927
2048
|
}
|
|
1928
2049
|
return PG_NEED_MORE
|
|
@@ -1943,10 +2064,13 @@ export class PgConnection {
|
|
|
1943
2064
|
|
|
1944
2065
|
/** Makes the column arrays hold at least `n`. */
|
|
1945
2066
|
private reserveColumns(n: i32): void {
|
|
1946
|
-
|
|
2067
|
+
const have: i32 = toI32(this.colStarts.length)
|
|
2068
|
+
// A row or description holds at most `maxColumns`, 65535, so the arrays
|
|
2069
|
+
// stay under it too; the test states that for the prover.
|
|
2070
|
+
if (n <= have || have > 65535) {
|
|
1947
2071
|
return
|
|
1948
2072
|
}
|
|
1949
|
-
const size: i32 = n <
|
|
2073
|
+
const size: i32 = n < have * 2 ? have * 2 : n
|
|
1950
2074
|
const starts: i32[] = new Array<i32>(size)
|
|
1951
2075
|
const lengths: i32[] = new Array<i32>(size)
|
|
1952
2076
|
const types: i32[] = new Array<i32>(size)
|
|
@@ -1960,35 +2084,40 @@ export class PgConnection {
|
|
|
1960
2084
|
|
|
1961
2085
|
/** 'D': DataRow, each column's length (-1 for NULL) and bytes, which must fill the message exactly. */
|
|
1962
2086
|
private readRow(body: i32, end: i32): i32 {
|
|
2087
|
+
const b: u8[] = this.input
|
|
2088
|
+
// dispatch hands over a frame inside the buffer; the test states that bound for the prover.
|
|
2089
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length) || body > toI32(b.length) - 2) {
|
|
2090
|
+
return this.fail(PG_ERR_FRAME)
|
|
2091
|
+
}
|
|
1963
2092
|
if (end - body < 2) {
|
|
1964
2093
|
return this.fail(PG_ERR_FRAME)
|
|
1965
2094
|
}
|
|
1966
|
-
const b: u8[] = this.input
|
|
1967
2095
|
const n: i32 = toI32(getU16BE(b, body))
|
|
2096
|
+
let p: i32 = body + 2
|
|
1968
2097
|
if (n > this.config.maxColumns) {
|
|
1969
2098
|
return this.fail(PG_ERR_TOO_BIG)
|
|
1970
2099
|
}
|
|
1971
2100
|
this.reserveColumns(n)
|
|
1972
2101
|
const starts: i32[] = this.colStarts
|
|
1973
2102
|
const lengths: i32[] = this.colLengths
|
|
1974
|
-
let p: i32 = body + 2
|
|
1975
2103
|
// reserveColumns made both arrays hold n; the length tests prove the indices.
|
|
1976
2104
|
for (let c: i32 = 0; c < n && c < toI32(starts.length) && c < toI32(lengths.length); c++) {
|
|
1977
2105
|
// The buffer tests never fail on a frame next() cut from it; they prove the indices.
|
|
1978
|
-
if (
|
|
2106
|
+
if (p < 0 || end < p || end - p < 4 || p > toI32(b.length) - 4) {
|
|
1979
2107
|
return this.fail(PG_ERR_FRAME)
|
|
1980
2108
|
}
|
|
1981
2109
|
// getI32BE written out: a call per column was a fifth of the row's cost.
|
|
1982
2110
|
const length: i32 =
|
|
1983
2111
|
(toI32(b[p]) << 24) | (toI32(b[p + 1]) << 16) | (toI32(b[p + 2]) << 8) | toI32(b[p + 3])
|
|
1984
|
-
p
|
|
2112
|
+
p = p + 4
|
|
1985
2113
|
if (length < -1 || length > end - p) {
|
|
1986
2114
|
return this.fail(PG_ERR_FRAME)
|
|
1987
2115
|
}
|
|
1988
2116
|
starts[c] = p
|
|
1989
2117
|
lengths[c] = length
|
|
1990
2118
|
if (length > 0) {
|
|
1991
|
-
|
|
2119
|
+
// The value ends inside the frame, so the sum fits an `i32`.
|
|
2120
|
+
p = toI32(toI64(p) + toI64(length))
|
|
1992
2121
|
}
|
|
1993
2122
|
}
|
|
1994
2123
|
if (p !== end) {
|
|
@@ -2000,22 +2129,29 @@ export class PgConnection {
|
|
|
2000
2129
|
|
|
2001
2130
|
/** 'T': RowDescription; each field is a name and 18 bytes, the type's OID 6 bytes into them. */
|
|
2002
2131
|
private readDescription(body: i32, end: i32): i32 {
|
|
2132
|
+
const b: u8[] = this.input
|
|
2133
|
+
// dispatch hands over a frame inside the buffer; the test states that bound for the prover.
|
|
2134
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length) || body > toI32(b.length) - 2) {
|
|
2135
|
+
return this.fail(PG_ERR_FRAME)
|
|
2136
|
+
}
|
|
2003
2137
|
if (end - body < 2) {
|
|
2004
2138
|
return this.fail(PG_ERR_FRAME)
|
|
2005
2139
|
}
|
|
2006
|
-
const b: u8[] = this.input
|
|
2007
2140
|
const n: i32 = toI32(getU16BE(b, body))
|
|
2141
|
+
let p: i32 = body + 2
|
|
2008
2142
|
if (n > this.config.maxColumns) {
|
|
2009
2143
|
return this.fail(PG_ERR_TOO_BIG)
|
|
2010
2144
|
}
|
|
2011
2145
|
this.reserveColumns(n)
|
|
2012
|
-
|
|
2013
|
-
|
|
2146
|
+
const types: i32[] = this.colTypes
|
|
2147
|
+
// reserveColumns made the array hold n; the length test proves the index.
|
|
2148
|
+
for (let c: i32 = 0; c < n && c < toI32(types.length); c++) {
|
|
2014
2149
|
const nameEnd: i32 = pgFind(b, p, end, 0)
|
|
2015
|
-
|
|
2150
|
+
// pgFind answers at or past `p`, so `nameEnd` is not negative; the test states that for the prover.
|
|
2151
|
+
if (nameEnd < 0 || end < 0 || end - nameEnd < 19 || nameEnd > toI32(b.length) - 19) {
|
|
2016
2152
|
return this.fail(PG_ERR_FRAME)
|
|
2017
2153
|
}
|
|
2018
|
-
|
|
2154
|
+
types[c] = getI32BE(b, nameEnd + 7)
|
|
2019
2155
|
p = nameEnd + 19
|
|
2020
2156
|
}
|
|
2021
2157
|
if (p !== end) {
|
|
@@ -2028,7 +2164,7 @@ export class PgConnection {
|
|
|
2028
2164
|
/** 'E' and 'N': the fields, each a type byte and a string, then a 0; `null` when they do not fill the message exactly. */
|
|
2029
2165
|
private readError(body: i32, end: i32): PgError | null {
|
|
2030
2166
|
const b: u8[] = this.input
|
|
2031
|
-
if (body < 0 || end > toI32(b.length)) {
|
|
2167
|
+
if (body < 0 || end < 0 || end > toI32(b.length)) {
|
|
2032
2168
|
return null
|
|
2033
2169
|
}
|
|
2034
2170
|
let severity: string = ""
|
|
@@ -2052,7 +2188,7 @@ export class PgConnection {
|
|
|
2052
2188
|
break
|
|
2053
2189
|
}
|
|
2054
2190
|
const valueEnd: i32 = pgFind(b, p + 1, end, 0)
|
|
2055
|
-
if (valueEnd
|
|
2191
|
+
if (valueEnd < 0 || valueEnd >= end) {
|
|
2056
2192
|
return null
|
|
2057
2193
|
}
|
|
2058
2194
|
const value: string = String.fromBytes(b, p + 1, valueEnd)
|
|
@@ -2124,7 +2260,7 @@ export class PgConnection {
|
|
|
2124
2260
|
/** AuthenticationSASL: the mechanisms, each NUL-terminated, then a 0; SCRAM-SHA-256 must be among them. */
|
|
2125
2261
|
private scramStart(body: i32, end: i32): i32 {
|
|
2126
2262
|
const b: u8[] = this.input
|
|
2127
|
-
if (body < 0 || end > toI32(b.length)) {
|
|
2263
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length)) {
|
|
2128
2264
|
return this.fail(PG_ERR_FRAME)
|
|
2129
2265
|
}
|
|
2130
2266
|
let offered: boolean = false
|
|
@@ -2140,7 +2276,7 @@ export class PgConnection {
|
|
|
2140
2276
|
break
|
|
2141
2277
|
}
|
|
2142
2278
|
const nameEnd: i32 = pgFind(b, p, end, 0)
|
|
2143
|
-
if (nameEnd
|
|
2279
|
+
if (nameEnd < 0 || nameEnd < p || nameEnd >= end) {
|
|
2144
2280
|
return this.fail(PG_ERR_FRAME)
|
|
2145
2281
|
}
|
|
2146
2282
|
if (pgBytesAre(b, p, nameEnd - p, "SCRAM-SHA-256")) {
|
|
@@ -2175,16 +2311,23 @@ export class PgConnection {
|
|
|
2175
2311
|
*/
|
|
2176
2312
|
private scramContinue(body: i32, end: i32): i32 {
|
|
2177
2313
|
const b: u8[] = this.input
|
|
2178
|
-
if (body < 0 || end > toI32(b.length)) {
|
|
2314
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length)) {
|
|
2179
2315
|
return this.fail(PG_ERR_FRAME)
|
|
2180
2316
|
}
|
|
2317
|
+
// Each comma lies inside the body, the salt's after the nonce's, so every
|
|
2318
|
+
// position below is inside the body; the tests state that for the prover.
|
|
2181
2319
|
const nonceEnd: i32 = pgFind(b, body, end, 44)
|
|
2182
|
-
|
|
2320
|
+
if (nonceEnd < 0 || nonceEnd < body || nonceEnd >= end || body > toI32(b.length) - 2) {
|
|
2321
|
+
return this.fail(PG_ERR_SCRAM)
|
|
2322
|
+
}
|
|
2323
|
+
const saltStart: i32 = nonceEnd + 1
|
|
2324
|
+
const saltEnd: i32 = pgFind(b, saltStart, end, 44)
|
|
2183
2325
|
if (
|
|
2184
|
-
|
|
2185
|
-
saltEnd
|
|
2326
|
+
saltEnd < 0 ||
|
|
2327
|
+
saltEnd < saltStart ||
|
|
2328
|
+
saltEnd >= end ||
|
|
2186
2329
|
!pgBytesAre(b, body, 2, "r=") ||
|
|
2187
|
-
!pgBytesAre(b,
|
|
2330
|
+
!pgBytesAre(b, saltStart, 2, "s=") ||
|
|
2188
2331
|
end - saltEnd < 4 ||
|
|
2189
2332
|
!pgBytesAre(b, saltEnd + 1, 2, "i=")
|
|
2190
2333
|
) {
|
|
@@ -2192,12 +2335,14 @@ export class PgConnection {
|
|
|
2192
2335
|
}
|
|
2193
2336
|
// The nonce extends ours with printable characters other than a comma.
|
|
2194
2337
|
const ours: i32 = toI32(this.clientNonce.length)
|
|
2195
|
-
|
|
2338
|
+
const nonceAt: i32 = body + 2
|
|
2339
|
+
if (ours < 0 || nonceEnd - nonceAt <= ours || !pgBytesAre(b, nonceAt, ours, this.clientNonce)) {
|
|
2196
2340
|
return this.fail(PG_ERR_SCRAM)
|
|
2197
2341
|
}
|
|
2198
|
-
|
|
2199
|
-
const
|
|
2200
|
-
|
|
2342
|
+
// Both lie before a comma inside the body, so they fit an `i32`.
|
|
2343
|
+
const nonceStart: i32 = toI32(toI64(nonceAt) + toI64(ours))
|
|
2344
|
+
const digitsStart: i32 = toI32(toI64(saltEnd) + toI64(3))
|
|
2345
|
+
if (nonceStart < 0) {
|
|
2201
2346
|
return this.fail(PG_ERR_SCRAM)
|
|
2202
2347
|
}
|
|
2203
2348
|
for (let i: i32 = nonceStart; i < nonceEnd && i < toI32(b.length); i++) {
|
|
@@ -2215,12 +2360,18 @@ export class PgConnection {
|
|
|
2215
2360
|
if (d < 0 || d > 9 || iterations > this.config.maxIterations) {
|
|
2216
2361
|
return this.fail(PG_ERR_SCRAM)
|
|
2217
2362
|
}
|
|
2363
|
+
// The digits are bounded by `PG_MAX_ITERATIONS` (written out so the prover sees it), so this fits.
|
|
2364
|
+
if (iterations < 0 || iterations > 214748363) {
|
|
2365
|
+
return this.fail(PG_ERR_SCRAM)
|
|
2366
|
+
}
|
|
2218
2367
|
iterations = iterations * 10 + d
|
|
2219
2368
|
}
|
|
2220
2369
|
if (iterations < this.config.minIterations || iterations > this.config.maxIterations) {
|
|
2221
2370
|
return this.fail(PG_ERR_SCRAM)
|
|
2222
2371
|
}
|
|
2223
|
-
const salt: u8[] | null = base64StdPadDecode(
|
|
2372
|
+
const salt: u8[] | null = base64StdPadDecode(
|
|
2373
|
+
String.fromBytes(b, toI32(toI64(saltStart) + toI64(2)), saltEnd)
|
|
2374
|
+
)
|
|
2224
2375
|
if (salt === null || salt.length === 0) {
|
|
2225
2376
|
return this.fail(PG_ERR_SCRAM)
|
|
2226
2377
|
}
|
|
@@ -2228,28 +2379,18 @@ export class PgConnection {
|
|
|
2228
2379
|
if (password.gone) {
|
|
2229
2380
|
return this.fail(PG_ERR_SCRAM)
|
|
2230
2381
|
}
|
|
2231
|
-
const salted: Secret<u8[]> | null =
|
|
2382
|
+
const salted: Secret<u8[]> | null = scramSha256SaltedPassword(password.bytes, salt, iterations)
|
|
2232
2383
|
if (salted === null) {
|
|
2233
2384
|
return this.fail(PG_ERR_SCRAM)
|
|
2234
2385
|
}
|
|
2235
|
-
const withoutProof: string = `c=biws,r=${String.fromBytes(b,
|
|
2386
|
+
const withoutProof: string = `c=biws,r=${String.fromBytes(b, nonceAt, nonceEnd)}`
|
|
2236
2387
|
const authMessage: u8[] = pgBytesOf(
|
|
2237
2388
|
`${this.firstBare()},${String.fromBytes(b, body, end)},${withoutProof}`
|
|
2238
2389
|
)
|
|
2239
|
-
const
|
|
2390
|
+
const proof: u8[] = scramSha256ClientProof(salted, authMessage)
|
|
2391
|
+
this.serverSignature = scramSha256ServerSignature(salted, authMessage)
|
|
2240
2392
|
wipe(salted)
|
|
2241
2393
|
this.dispose()
|
|
2242
|
-
const proof: u8[] = new Array<u8>(32)
|
|
2243
|
-
const signature: u8[] = new Array<u8>(32)
|
|
2244
|
-
if (toI32(keys.length) < 64) {
|
|
2245
|
-
panic("nish/net/postgres: the SCRAM keys are not 64 bytes")
|
|
2246
|
-
}
|
|
2247
|
-
for (let i: i32 = 0; i < 32; i++) {
|
|
2248
|
-
proof[i] = keys[i]
|
|
2249
|
-
signature[i] = keys[32 + i]
|
|
2250
|
-
}
|
|
2251
|
-
wipe(keys)
|
|
2252
|
-
this.serverSignature = signature
|
|
2253
2394
|
// The proof goes on the wire as it is, so its base64 string is no secret.
|
|
2254
2395
|
const encoded: string = base64StdPadEncode(proof)
|
|
2255
2396
|
wipe(proof)
|
|
@@ -2263,10 +2404,10 @@ export class PgConnection {
|
|
|
2263
2404
|
/** AuthenticationSASLFinal: exactly `v=` and the ServerSignature, compared in constant time. */
|
|
2264
2405
|
private scramFinal(body: i32, end: i32): i32 {
|
|
2265
2406
|
const b: u8[] = this.input
|
|
2266
|
-
if (end - body < 2 || !pgBytesAre(b, body, 2, "v=")) {
|
|
2407
|
+
if (body < 0 || end < 0 || end - body < 2 || !pgBytesAre(b, body, 2, "v=")) {
|
|
2267
2408
|
return this.fail(PG_ERR_SCRAM)
|
|
2268
2409
|
}
|
|
2269
|
-
const signature: u8[] | null = base64StdPadDecode(String.fromBytes(b, body + 2, end))
|
|
2410
|
+
const signature: u8[] | null = base64StdPadDecode(String.fromBytes(b, toI32(toI64(body) + toI64(2)), end))
|
|
2270
2411
|
if (signature === null || signature.length !== 32) {
|
|
2271
2412
|
return this.fail(PG_ERR_SCRAM)
|
|
2272
2413
|
}
|
|
@@ -2286,12 +2427,21 @@ export class PgConnection {
|
|
|
2286
2427
|
* buffer, the unsent bytes move to its start first: a builder call asks
|
|
2287
2428
|
* before it takes any offset into the buffer, so nothing it holds moves.
|
|
2288
2429
|
*/
|
|
2289
|
-
private room(size:
|
|
2290
|
-
const
|
|
2291
|
-
|
|
2430
|
+
private room(size: i64): boolean {
|
|
2431
|
+
const start: i32 = this.outStart
|
|
2432
|
+
const end: i32 = this.outEnd
|
|
2433
|
+
if (size < toI64(0) || size > toI64(PG_MAX_CAP)) {
|
|
2434
|
+
return false
|
|
2435
|
+
}
|
|
2436
|
+
const n: i32 = toI32(size)
|
|
2437
|
+
// Summed in `i64`, where no `i32` term overflows on its way to the test.
|
|
2438
|
+
if (
|
|
2439
|
+
toI64(n) + toI64(end) - toI64(start) + toI64(PG_TAIL) + toI64(this.bindOwed) >
|
|
2440
|
+
toI64(this.config.maxOutput)
|
|
2441
|
+
) {
|
|
2292
2442
|
return false
|
|
2293
2443
|
}
|
|
2294
|
-
if (
|
|
2444
|
+
if (start > 0 && toI64(n) + toI64(end) > toI64(this.out.length)) {
|
|
2295
2445
|
this.compactOutput()
|
|
2296
2446
|
}
|
|
2297
2447
|
return true
|
|
@@ -2302,8 +2452,13 @@ export class PgConnection {
|
|
|
2302
2452
|
* `terminate`) the way `room` does for the rest, so that the buffer is not
|
|
2303
2453
|
* grown past `maxOutput` while sent bytes still fill its start.
|
|
2304
2454
|
*/
|
|
2305
|
-
private tailRoom(size:
|
|
2306
|
-
|
|
2455
|
+
private tailRoom(size: i64): void {
|
|
2456
|
+
// A tail message is a few bytes and what the bind owes, which `room` held
|
|
2457
|
+
// under `maxOutput`; the test states that bound for the prover.
|
|
2458
|
+
if (size < toI64(0) || size > toI64(PG_MAX_CAP)) {
|
|
2459
|
+
return
|
|
2460
|
+
}
|
|
2461
|
+
if (this.outStart > 0 && toI64(toI32(size)) + toI64(this.outEnd) > toI64(this.out.length)) {
|
|
2307
2462
|
this.compactOutput()
|
|
2308
2463
|
}
|
|
2309
2464
|
}
|
|
@@ -2311,57 +2466,109 @@ export class PgConnection {
|
|
|
2311
2466
|
/** Moves the unsent output to the start of the buffer, the open bind's offsets with it. */
|
|
2312
2467
|
private compactOutput(): void {
|
|
2313
2468
|
const shift: i32 = this.outStart
|
|
2314
|
-
|
|
2469
|
+
const end: i32 = this.outEnd
|
|
2470
|
+
// The sent bytes end at or before the queued ones; the test states that for the prover.
|
|
2471
|
+
if (shift <= 0 || end < 0 || end < shift) {
|
|
2315
2472
|
return
|
|
2316
2473
|
}
|
|
2317
|
-
|
|
2318
|
-
this.
|
|
2474
|
+
const n: i32 = end - shift
|
|
2475
|
+
copyBytes(this.out, 0, this.out, shift, n)
|
|
2476
|
+
this.outEnd = n
|
|
2319
2477
|
this.outStart = 0
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2478
|
+
// An open bind and its array lie in the unsent bytes, past the shift.
|
|
2479
|
+
const bindAt: i32 = this.bindAt
|
|
2480
|
+
const countAt: i32 = this.paramCountAt
|
|
2481
|
+
if (bindAt >= 0 && countAt >= 0) {
|
|
2482
|
+
this.bindAt = bindAt - shift
|
|
2483
|
+
this.paramCountAt = countAt - shift
|
|
2323
2484
|
}
|
|
2324
|
-
|
|
2325
|
-
|
|
2485
|
+
const arrayAt: i32 = this.arrayAt
|
|
2486
|
+
if (arrayAt >= 0) {
|
|
2487
|
+
this.arrayAt = arrayAt - shift
|
|
2326
2488
|
}
|
|
2327
2489
|
}
|
|
2328
2490
|
|
|
2329
2491
|
/** Makes the output buffer at least `want` bytes long. */
|
|
2330
|
-
private reserve(want:
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
return
|
|
2492
|
+
private reserve(want: i64): void {
|
|
2493
|
+
if (want > toI64(this.out.length)) {
|
|
2494
|
+
this.grow(want)
|
|
2334
2495
|
}
|
|
2335
|
-
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2498
|
+
/**
|
|
2499
|
+
* `reserve` when the buffer is short: kept apart so that the test above,
|
|
2500
|
+
* which every put makes, is small enough to inline into it.
|
|
2501
|
+
*/
|
|
2502
|
+
private grow(want: i64): void {
|
|
2503
|
+
const doubled: i64 = toI64(this.out.length) * toI64(2)
|
|
2504
|
+
const grown: u8[] = new Array<u8>(
|
|
2505
|
+
toI32(want < doubled && doubled <= toI64(PG_MAX_CAP) * toI64(2) ? doubled : want)
|
|
2506
|
+
)
|
|
2336
2507
|
grown.set(this.out, 0)
|
|
2337
2508
|
this.out = grown
|
|
2338
2509
|
}
|
|
2339
2510
|
|
|
2511
|
+
// Each put reserves its bytes and then writes them: the end lies inside the
|
|
2512
|
+
// buffer after `reserve`, and the test states that bound for the prover.
|
|
2513
|
+
|
|
2340
2514
|
private putByte(v: i32): void {
|
|
2341
|
-
this.
|
|
2342
|
-
|
|
2343
|
-
|
|
2515
|
+
const end: i32 = this.outEnd
|
|
2516
|
+
const b: u8[] = this.out
|
|
2517
|
+
if (end >= 0 && end < toI32(b.length)) {
|
|
2518
|
+
b[end] = toU8(v)
|
|
2519
|
+
this.outEnd = end + 1
|
|
2520
|
+
return
|
|
2521
|
+
}
|
|
2522
|
+
this.reserve(toI64(end) + toI64(1))
|
|
2523
|
+
const grown: u8[] = this.out
|
|
2524
|
+
if (end < 0 || end >= toI32(grown.length)) {
|
|
2525
|
+
return
|
|
2526
|
+
}
|
|
2527
|
+
grown[end] = toU8(v)
|
|
2528
|
+
this.outEnd = end + 1
|
|
2344
2529
|
}
|
|
2345
2530
|
|
|
2346
2531
|
private putI32(v: i32): void {
|
|
2347
|
-
this.
|
|
2348
|
-
|
|
2349
|
-
|
|
2532
|
+
const end: i32 = this.outEnd
|
|
2533
|
+
const b: u8[] = this.out
|
|
2534
|
+
if (end >= 0 && end <= toI32(b.length) - 4) {
|
|
2535
|
+
setI32BE(b, end, v)
|
|
2536
|
+
this.outEnd = end + 4
|
|
2537
|
+
return
|
|
2538
|
+
}
|
|
2539
|
+
this.reserve(toI64(end) + toI64(4))
|
|
2540
|
+
const grown: u8[] = this.out
|
|
2541
|
+
if (end < 0 || end > toI32(grown.length) - 4) {
|
|
2542
|
+
return
|
|
2543
|
+
}
|
|
2544
|
+
setI32BE(grown, end, v)
|
|
2545
|
+
this.outEnd = end + 4
|
|
2350
2546
|
}
|
|
2351
2547
|
|
|
2352
2548
|
private putI16(v: i32): void {
|
|
2353
|
-
this.
|
|
2354
|
-
|
|
2355
|
-
this.
|
|
2549
|
+
const end: i32 = this.outEnd
|
|
2550
|
+
this.reserve(toI64(end) + toI64(2))
|
|
2551
|
+
const b: u8[] = this.out
|
|
2552
|
+
if (end < 0 || end > toI32(b.length) - 2) {
|
|
2553
|
+
return
|
|
2554
|
+
}
|
|
2555
|
+
setU16BE(b, end, toU16(v))
|
|
2556
|
+
this.outEnd = end + 2
|
|
2356
2557
|
}
|
|
2357
2558
|
|
|
2358
2559
|
private putString(text: string): void {
|
|
2359
2560
|
const n: i32 = toI32(text.length)
|
|
2360
|
-
this.
|
|
2361
|
-
|
|
2362
|
-
|
|
2561
|
+
let k: i32 = this.outEnd
|
|
2562
|
+
this.reserve(toI64(k) + toI64(n))
|
|
2563
|
+
const b: u8[] = this.out
|
|
2564
|
+
if (k < 0) {
|
|
2565
|
+
return
|
|
2566
|
+
}
|
|
2567
|
+
for (let i: i32 = 0; i < n && k < toI32(b.length); i++) {
|
|
2568
|
+
b[k] = toU8(text.charCodeAt(i))
|
|
2569
|
+
k++
|
|
2363
2570
|
}
|
|
2364
|
-
this.outEnd
|
|
2571
|
+
this.outEnd = k
|
|
2365
2572
|
}
|
|
2366
2573
|
|
|
2367
2574
|
private putCString(text: string): void {
|
|
@@ -2384,7 +2591,12 @@ export class PgConnection {
|
|
|
2384
2591
|
|
|
2385
2592
|
/** Writes the length of the message started at `at`. */
|
|
2386
2593
|
private endMessage(at: i32): void {
|
|
2387
|
-
|
|
2594
|
+
const end: i32 = this.outEnd
|
|
2595
|
+
// 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) {
|
|
2597
|
+
return
|
|
2598
|
+
}
|
|
2599
|
+
setI32BE(this.out, at + 1, end - at - 1)
|
|
2388
2600
|
}
|
|
2389
2601
|
|
|
2390
2602
|
/**
|
|
@@ -2398,13 +2610,18 @@ export class PgConnection {
|
|
|
2398
2610
|
}
|
|
2399
2611
|
|
|
2400
2612
|
/** Opens a group if none is, for a message of `size` bytes; false, and no group opened, when none may be or there is no room. */
|
|
2401
|
-
private canOpen(size:
|
|
2613
|
+
private canOpen(size: i64): boolean {
|
|
2402
2614
|
if (!this.canSend() || this.bindAt >= 0 || !this.room(size)) {
|
|
2403
2615
|
return false
|
|
2404
2616
|
}
|
|
2405
2617
|
if (!this.groupOpen) {
|
|
2618
|
+
// canSend allowed a new group only under maxInFlight; the test states that for the prover.
|
|
2619
|
+
const inFlight: i32 = this.inFlight
|
|
2620
|
+
if (inFlight >= this.config.maxInFlight) {
|
|
2621
|
+
return false
|
|
2622
|
+
}
|
|
2406
2623
|
this.groupOpen = true
|
|
2407
|
-
this.inFlight
|
|
2624
|
+
this.inFlight = inFlight + 1
|
|
2408
2625
|
}
|
|
2409
2626
|
return true
|
|
2410
2627
|
}
|
|
@@ -2432,7 +2649,10 @@ export class PgConnection {
|
|
|
2432
2649
|
return false
|
|
2433
2650
|
}
|
|
2434
2651
|
const sqlLength: i32 = toI32(sql.length)
|
|
2435
|
-
if (
|
|
2652
|
+
if (
|
|
2653
|
+
sqlLength > PG_MAX_CAP ||
|
|
2654
|
+
!this.canOpen(toI64(16) + toI64(2) * toI64(nameLength) + toI64(sqlLength) + toI64(4) * toI64(types))
|
|
2655
|
+
) {
|
|
2436
2656
|
return false
|
|
2437
2657
|
}
|
|
2438
2658
|
const at: i32 = this.startMessage(PG_FE_PARSE)
|
|
@@ -2458,7 +2678,7 @@ export class PgConnection {
|
|
|
2458
2678
|
* @returns True when the Close was queued.
|
|
2459
2679
|
*/
|
|
2460
2680
|
closeStatement(name: string): boolean {
|
|
2461
|
-
if (pgHasNul(name) || toI32(name.length) > PG_MAX_CAP || !this.canOpen(7 +
|
|
2681
|
+
if (pgHasNul(name) || toI32(name.length) > PG_MAX_CAP || !this.canOpen(toI64(7) + toI64(name.length))) {
|
|
2462
2682
|
return false
|
|
2463
2683
|
}
|
|
2464
2684
|
const at: i32 = this.startMessage(PG_FE_CLOSE)
|
|
@@ -2503,7 +2723,11 @@ export class PgConnection {
|
|
|
2503
2723
|
private openBind(name: string, binary: boolean): boolean {
|
|
2504
2724
|
const opens: boolean = !this.groupOpen
|
|
2505
2725
|
const extra: i32 = binary ? PG_BINARY_TAIL : 0
|
|
2506
|
-
if (
|
|
2726
|
+
if (
|
|
2727
|
+
pgHasNul(name) ||
|
|
2728
|
+
toI32(name.length) > PG_MAX_CAP ||
|
|
2729
|
+
!this.canOpen(toI64(11) + toI64(extra) + toI64(name.length))
|
|
2730
|
+
) {
|
|
2507
2731
|
return false
|
|
2508
2732
|
}
|
|
2509
2733
|
this.bindOpened = opens
|
|
@@ -2529,11 +2753,19 @@ export class PgConnection {
|
|
|
2529
2753
|
this.noArrayOpen()
|
|
2530
2754
|
// Once the bind holds a binary parameter, every parameter owes its format code.
|
|
2531
2755
|
const owed: i32 = this.binaryParamCount > 0 ? 2 : 0
|
|
2532
|
-
|
|
2756
|
+
const count: i32 = this.paramCount
|
|
2757
|
+
if (
|
|
2758
|
+
this.bindAt < 0 ||
|
|
2759
|
+
count < 0 ||
|
|
2760
|
+
count >= 65535 ||
|
|
2761
|
+
size > PG_MAX_CAP ||
|
|
2762
|
+
!this.room(toI64(4) + toI64(size) + toI64(owed))
|
|
2763
|
+
) {
|
|
2533
2764
|
return -1
|
|
2534
2765
|
}
|
|
2535
|
-
|
|
2536
|
-
this.
|
|
2766
|
+
// `room` held what the bind owes under `maxOutput`, so the sum fits an `i32`.
|
|
2767
|
+
this.bindOwed = toI32(toI64(this.bindOwed) + toI64(owed))
|
|
2768
|
+
this.paramCount = count + 1
|
|
2537
2769
|
const at: i32 = this.outEnd
|
|
2538
2770
|
this.putI32(PG_ZERO)
|
|
2539
2771
|
return at
|
|
@@ -2547,25 +2779,38 @@ export class PgConnection {
|
|
|
2547
2779
|
*/
|
|
2548
2780
|
private startBinaryParam(size: i32): i32 {
|
|
2549
2781
|
this.noArrayOpen()
|
|
2550
|
-
const
|
|
2551
|
-
|
|
2782
|
+
const count: i32 = this.paramCount
|
|
2783
|
+
const binary: i32 = this.binaryParamCount
|
|
2784
|
+
// Each binary parameter is one of the bind's, so both counts stay under 65535.
|
|
2785
|
+
if (this.bindAt < 0 || count < 0 || count >= 65535 || binary < 0 || binary > count || binary >= 65535) {
|
|
2552
2786
|
return -1
|
|
2553
2787
|
}
|
|
2554
|
-
|
|
2555
|
-
|
|
2788
|
+
const owed: i32 = binary > 0 ? toI32(2) : toI32(2) * (count + toI32(1))
|
|
2789
|
+
if (size > PG_MAX_CAP || !this.room(toI64(4) + toI64(size) + toI64(owed))) {
|
|
2790
|
+
return -1
|
|
2791
|
+
}
|
|
2792
|
+
const params: i32[] = this.binaryParams
|
|
2793
|
+
if (binary < toI32(params.length)) {
|
|
2794
|
+
params[binary] = count
|
|
2556
2795
|
} else {
|
|
2557
|
-
|
|
2796
|
+
params.push(count)
|
|
2558
2797
|
}
|
|
2559
|
-
this.binaryParamCount
|
|
2560
|
-
|
|
2561
|
-
this.
|
|
2798
|
+
this.binaryParamCount = binary + 1
|
|
2799
|
+
// `room` held what the bind owes under `maxOutput`, so the sum fits an `i32`.
|
|
2800
|
+
this.bindOwed = toI32(toI64(this.bindOwed) + toI64(owed))
|
|
2801
|
+
this.paramCount = count + 1
|
|
2562
2802
|
const at: i32 = this.outEnd
|
|
2563
2803
|
this.putI32(PG_ZERO)
|
|
2564
2804
|
return at
|
|
2565
2805
|
}
|
|
2566
2806
|
|
|
2567
2807
|
private endParam(at: i32): void {
|
|
2568
|
-
|
|
2808
|
+
const end: i32 = this.outEnd
|
|
2809
|
+
// `startParam` wrote the length word at `at`; the test states that for the prover.
|
|
2810
|
+
if (at < 0 || end < 0 || end - at < 4 || at > toI32(this.out.length) - 4) {
|
|
2811
|
+
return
|
|
2812
|
+
}
|
|
2813
|
+
setI32BE(this.out, at, end - at - 4)
|
|
2569
2814
|
}
|
|
2570
2815
|
|
|
2571
2816
|
private noArrayOpen(): void {
|
|
@@ -2613,9 +2858,14 @@ export class PgConnection {
|
|
|
2613
2858
|
if (at < 0) {
|
|
2614
2859
|
return false
|
|
2615
2860
|
}
|
|
2616
|
-
this.
|
|
2617
|
-
|
|
2618
|
-
|
|
2861
|
+
const end: i32 = this.outEnd
|
|
2862
|
+
this.reserve(toI64(end) + toI64(length))
|
|
2863
|
+
// `reserve` made the room; the test states that bound for the prover.
|
|
2864
|
+
if (end < 0 || end > toI32(this.out.length) - length) {
|
|
2865
|
+
return false
|
|
2866
|
+
}
|
|
2867
|
+
copyBytes(this.out, end, bytes, start, length)
|
|
2868
|
+
this.outEnd = toI32(toI64(end) + toI64(length))
|
|
2619
2869
|
this.endParam(at)
|
|
2620
2870
|
return true
|
|
2621
2871
|
}
|
|
@@ -2626,10 +2876,12 @@ export class PgConnection {
|
|
|
2626
2876
|
* @returns True when it was written; false as every `param...` call is.
|
|
2627
2877
|
*/
|
|
2628
2878
|
paramNull(): boolean {
|
|
2629
|
-
|
|
2879
|
+
const at: i32 = this.startParam(toI32(0))
|
|
2880
|
+
// `startParam` wrote the length word at `at`; the test states that bound for the prover.
|
|
2881
|
+
if (at < 0 || at > toI32(this.out.length) - 4) {
|
|
2630
2882
|
return false
|
|
2631
2883
|
}
|
|
2632
|
-
setI32BE(this.out,
|
|
2884
|
+
setI32BE(this.out, at, -1)
|
|
2633
2885
|
return true
|
|
2634
2886
|
}
|
|
2635
2887
|
|
|
@@ -2654,8 +2906,12 @@ export class PgConnection {
|
|
|
2654
2906
|
/** `value` in decimal, `pgDecimalLength(value)` bytes. */
|
|
2655
2907
|
private putDecimal(value: i64): void {
|
|
2656
2908
|
const n: i32 = pgDecimalLength(value)
|
|
2657
|
-
this.reserve(this.outEnd + n)
|
|
2658
|
-
|
|
2909
|
+
this.reserve(toI64(this.outEnd) + toI64(n))
|
|
2910
|
+
// `reserve` made the room, so the window is inside `out` and the answer is not -1.
|
|
2911
|
+
const end: i32 = pgPutDecimal(this.out, this.outEnd, value, n)
|
|
2912
|
+
if (end >= 0) {
|
|
2913
|
+
this.outEnd = end
|
|
2914
|
+
}
|
|
2659
2915
|
}
|
|
2660
2916
|
|
|
2661
2917
|
/**
|
|
@@ -2675,10 +2931,16 @@ export class PgConnection {
|
|
|
2675
2931
|
// Each loop is bounded by `values.length` as well as by the window's end,
|
|
2676
2932
|
// which the guard above keeps inside it: that names the bound the compiler
|
|
2677
2933
|
// proves `values[i]` against, where the guard alone does not reach it.
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2934
|
+
// The window is inside `values`, so its end fits an `i32`.
|
|
2935
|
+
const end: i32 = toI32(toI64(start) + toI64(count))
|
|
2936
|
+
// Its exact size first, so that room is asked for once and a refusal writes nothing. The
|
|
2937
|
+
// count stops once past `PG_MAX_CAP` (2^30 - 1), which `startParam` refuses whatever the rest.
|
|
2938
|
+
let size: i64 = count > 0 ? toI64(count) + toI64(1) : toI64(2)
|
|
2939
|
+
for (
|
|
2940
|
+
let i: i32 = start;
|
|
2941
|
+
i >= 0 && i < end && i < toI32(values.length) && size >= 0 && size <= 1073741823;
|
|
2942
|
+
i++
|
|
2943
|
+
) {
|
|
2682
2944
|
size += toI64(pgDecimalLength(values[i]))
|
|
2683
2945
|
}
|
|
2684
2946
|
const at: i32 = this.startParam(size > toI64(PG_MAX_CAP) ? PG_MAX_CAP + 1 : toI32(size))
|
|
@@ -2709,13 +2971,20 @@ export class PgConnection {
|
|
|
2709
2971
|
*/
|
|
2710
2972
|
paramTextArray(items: string[]): boolean {
|
|
2711
2973
|
// Its exact size first, so that room is asked for once and a refusal writes nothing.
|
|
2974
|
+
// The count stops once past `PG_MAX_CAP` (2^30 - 1), which `startParam` refuses whatever the rest.
|
|
2712
2975
|
let size: i64 = 2
|
|
2713
2976
|
for (const item of items) {
|
|
2714
|
-
size += toI64(item.length) + 3
|
|
2715
2977
|
const n: i32 = toI32(item.length)
|
|
2978
|
+
if (size < 0 || size > 1073741823 || n < 0) {
|
|
2979
|
+
break
|
|
2980
|
+
}
|
|
2981
|
+
size += toI64(n) + toI64(3)
|
|
2716
2982
|
for (let i: i32 = 0; i < n; i++) {
|
|
2717
2983
|
const c: i32 = toI32(item.charCodeAt(i))
|
|
2718
2984
|
if (c === 34 || c === 92) {
|
|
2985
|
+
if (size < 0 || size > 1073741823) {
|
|
2986
|
+
break
|
|
2987
|
+
}
|
|
2719
2988
|
size += 1
|
|
2720
2989
|
}
|
|
2721
2990
|
}
|
|
@@ -2769,7 +3038,8 @@ export class PgConnection {
|
|
|
2769
3038
|
*/
|
|
2770
3039
|
paramInt4Array(values: i64[], start: i32, count: i32): boolean {
|
|
2771
3040
|
pgIntWindow(values, start, count)
|
|
2772
|
-
|
|
3041
|
+
// `pgIntWindow` kept the window inside `values`, so its end fits an `i32`.
|
|
3042
|
+
const end: i32 = toI32(toI64(start) + toI64(count))
|
|
2773
3043
|
for (let i: i32 = start; i >= 0 && i < end && i < toI32(values.length); i++) {
|
|
2774
3044
|
if (!pgFitsInt4(values[i])) {
|
|
2775
3045
|
panic("nish/net/postgres: paramInt4Array() with an element outside int4")
|
|
@@ -2794,27 +3064,48 @@ export class PgConnection {
|
|
|
2794
3064
|
|
|
2795
3065
|
private paramBinaryInts(values: i64[], start: i32, count: i32, oid: i32): boolean {
|
|
2796
3066
|
const width: i32 = oid === PG_OID_INT4 ? 4 : 8
|
|
3067
|
+
this.noArrayOpen()
|
|
3068
|
+
// More elements than `maxOutput` holds bytes is past any bind's room, as `startBinaryParam` answers.
|
|
3069
|
+
if (count < 0 || count > PG_MAX_CAP) {
|
|
3070
|
+
return false
|
|
3071
|
+
}
|
|
2797
3072
|
// The header, its one dimension when there are elements, then each element's length word and bytes.
|
|
2798
|
-
|
|
2799
|
-
|
|
2800
|
-
|
|
2801
|
-
|
|
2802
|
-
|
|
3073
|
+
let size: i64 = toI64(PG_ARRAY_HEADER)
|
|
3074
|
+
if (count > 0) {
|
|
3075
|
+
size = width === 4 ? toI64(20) + toI64(count) * toI64(8) : toI64(20) + toI64(count) * toI64(12)
|
|
3076
|
+
}
|
|
3077
|
+
if (size > toI64(PG_MAX_CAP)) {
|
|
3078
|
+
return false
|
|
3079
|
+
}
|
|
3080
|
+
const at: i32 = this.startBinaryParam(toI32(size))
|
|
2803
3081
|
if (at < 0) {
|
|
2804
3082
|
return false
|
|
2805
3083
|
}
|
|
2806
|
-
this.reserve(this.outEnd + toI32(size))
|
|
3084
|
+
this.reserve(toI64(this.outEnd) + toI64(toI32(size)))
|
|
2807
3085
|
this.putArrayHeader(oid, count)
|
|
2808
|
-
const
|
|
3086
|
+
const b: u8[] = this.out
|
|
3087
|
+
let p: i32 = this.outEnd
|
|
3088
|
+
// `pgIntWindow` held the window inside `values`, so its end fits an `i32`.
|
|
3089
|
+
const end: i32 = toI32(toI64(start) + toI64(count))
|
|
3090
|
+
// `reserve` made room for every element; the loop's tests state that bound for the prover.
|
|
2809
3091
|
for (let i: i32 = start; i >= 0 && i < end && i < toI32(values.length); i++) {
|
|
2810
|
-
setI32BE(this.out, this.outEnd, width)
|
|
2811
3092
|
if (width === 4) {
|
|
2812
|
-
|
|
3093
|
+
if (p < 0 || p > toI32(b.length) - 8) {
|
|
3094
|
+
break
|
|
3095
|
+
}
|
|
3096
|
+
setI32BE(b, p, width)
|
|
3097
|
+
setI32BE(b, p + 4, toI32(values[i]))
|
|
3098
|
+
p = p + 8
|
|
2813
3099
|
} else {
|
|
2814
|
-
|
|
3100
|
+
if (p < 0 || p > toI32(b.length) - 12) {
|
|
3101
|
+
break
|
|
3102
|
+
}
|
|
3103
|
+
setI32BE(b, p, width)
|
|
3104
|
+
setI64BE(b, p + 4, values[i])
|
|
3105
|
+
p = p + 12
|
|
2815
3106
|
}
|
|
2816
|
-
this.outEnd += 4 + width
|
|
2817
3107
|
}
|
|
3108
|
+
this.outEnd = p
|
|
2818
3109
|
this.endParam(at)
|
|
2819
3110
|
return true
|
|
2820
3111
|
}
|
|
@@ -2872,8 +3163,12 @@ export class PgConnection {
|
|
|
2872
3163
|
|
|
2873
3164
|
/** Whether the open array has room for an element of `size` bytes, its comma, and the `}` it owes. */
|
|
2874
3165
|
private elementRoom(size: i64): boolean {
|
|
3166
|
+
// An element past `PG_MAX_CAP` (2^30 - 1) has no room in any buffer.
|
|
3167
|
+
if (size < 0 || size > 1073741823) {
|
|
3168
|
+
return false
|
|
3169
|
+
}
|
|
2875
3170
|
const total: i64 = size + toI64(this.arrayCount > 0 ? 2 : 1)
|
|
2876
|
-
return total <= toI64(PG_MAX_CAP) && this.room(
|
|
3171
|
+
return total <= toI64(PG_MAX_CAP) && this.room(total)
|
|
2877
3172
|
}
|
|
2878
3173
|
|
|
2879
3174
|
/**
|
|
@@ -2891,12 +3186,14 @@ export class PgConnection {
|
|
|
2891
3186
|
if (start < 0 || length < 0 || length > toI32(bytes.length) - start) {
|
|
2892
3187
|
panic("nish/net/postgres: arrayElement() outside its source")
|
|
2893
3188
|
}
|
|
2894
|
-
// Both loops are bounded by `bytes.length` too, as `paramIntArray`'s are
|
|
2895
|
-
|
|
3189
|
+
// Both loops are bounded by `bytes.length` too, as `paramIntArray`'s are;
|
|
3190
|
+
// the window is inside `bytes`, so its end fits an `i32`.
|
|
3191
|
+
const end: i32 = toI32(toI64(start) + toI64(length))
|
|
2896
3192
|
let escapes: i32 = 0
|
|
2897
3193
|
for (let i: i32 = start; i < end && i < toI32(bytes.length); i++) {
|
|
2898
3194
|
const c: i32 = toI32(bytes[i])
|
|
2899
|
-
|
|
3195
|
+
// An escape per byte at most, so fewer than `length` before this one.
|
|
3196
|
+
if ((c === PG_QUOTE || c === PG_BACKSLASH) && escapes < length) {
|
|
2900
3197
|
escapes += 1
|
|
2901
3198
|
}
|
|
2902
3199
|
}
|
|
@@ -2905,10 +3202,15 @@ export class PgConnection {
|
|
|
2905
3202
|
}
|
|
2906
3203
|
this.elementComma()
|
|
2907
3204
|
this.putByte(PG_QUOTE)
|
|
2908
|
-
|
|
3205
|
+
const at: i32 = this.outEnd
|
|
3206
|
+
this.reserve(toI64(at) + toI64(length) + toI64(escapes) + toI64(1))
|
|
2909
3207
|
if (escapes === 0) {
|
|
2910
|
-
|
|
2911
|
-
this.
|
|
3208
|
+
// `reserve` made the room; the test states that bound for the prover.
|
|
3209
|
+
if (at < 0 || at > toI32(this.out.length) - length) {
|
|
3210
|
+
return false
|
|
3211
|
+
}
|
|
3212
|
+
copyBytes(this.out, at, bytes, start, length)
|
|
3213
|
+
this.outEnd = toI32(toI64(at) + toI64(length))
|
|
2912
3214
|
} else {
|
|
2913
3215
|
for (let i: i32 = start; i < end && i < toI32(bytes.length); i++) {
|
|
2914
3216
|
const c: i32 = toI32(bytes[i])
|
|
@@ -2938,10 +3240,11 @@ export class PgConnection {
|
|
|
2938
3240
|
}
|
|
2939
3241
|
|
|
2940
3242
|
private elementComma(): void {
|
|
2941
|
-
|
|
3243
|
+
const count: i32 = this.arrayCount
|
|
3244
|
+
if (count > 0) {
|
|
2942
3245
|
this.putByte(PG_COMMA)
|
|
2943
3246
|
}
|
|
2944
|
-
this.arrayCount
|
|
3247
|
+
this.arrayCount = pgCountUp(count)
|
|
2945
3248
|
}
|
|
2946
3249
|
|
|
2947
3250
|
/**
|
|
@@ -3015,18 +3318,23 @@ export class PgConnection {
|
|
|
3015
3318
|
if (width === 4 && !pgFitsInt4(value)) {
|
|
3016
3319
|
panic("nish/net/postgres: binaryArrayInt() with an element outside int4")
|
|
3017
3320
|
}
|
|
3018
|
-
if (!this.room(4 + width)) {
|
|
3321
|
+
if (!this.room(toI64(4) + toI64(width))) {
|
|
3019
3322
|
return false
|
|
3020
3323
|
}
|
|
3021
3324
|
this.putI32(width)
|
|
3022
3325
|
if (width === 4) {
|
|
3023
3326
|
this.putI32(toI32(value))
|
|
3024
3327
|
} else {
|
|
3025
|
-
this.
|
|
3026
|
-
|
|
3027
|
-
|
|
3328
|
+
const at: i32 = this.outEnd
|
|
3329
|
+
this.reserve(toI64(at) + toI64(8))
|
|
3330
|
+
// `room` and `reserve` made the eight bytes; the test states that bound for the prover.
|
|
3331
|
+
if (at < 0 || at > toI32(this.out.length) - 8) {
|
|
3332
|
+
return false
|
|
3333
|
+
}
|
|
3334
|
+
setI64BE(this.out, at, value)
|
|
3335
|
+
this.outEnd = toI32(toI64(at) + toI64(8))
|
|
3028
3336
|
}
|
|
3029
|
-
this.arrayCount
|
|
3337
|
+
this.arrayCount = pgCountUp(this.arrayCount)
|
|
3030
3338
|
return true
|
|
3031
3339
|
}
|
|
3032
3340
|
|
|
@@ -3038,12 +3346,12 @@ export class PgConnection {
|
|
|
3038
3346
|
*/
|
|
3039
3347
|
binaryArrayNull(): boolean {
|
|
3040
3348
|
this.binaryArrayOpen()
|
|
3041
|
-
if (!this.room(
|
|
3349
|
+
if (!this.room(toI64(4))) {
|
|
3042
3350
|
return false
|
|
3043
3351
|
}
|
|
3044
3352
|
this.putI32(toI32(-1))
|
|
3045
3353
|
this.arrayNulls = true
|
|
3046
|
-
this.arrayCount
|
|
3354
|
+
this.arrayCount = pgCountUp(this.arrayCount)
|
|
3047
3355
|
return true
|
|
3048
3356
|
}
|
|
3049
3357
|
|
|
@@ -3058,17 +3366,21 @@ export class PgConnection {
|
|
|
3058
3366
|
}
|
|
3059
3367
|
if (this.arrayOid !== 0) {
|
|
3060
3368
|
// The header follows the parameter's length word: ndim, the NULL flag, the OID, then the dimension.
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3369
|
+
// `binaryArrayStart` wrote all 24 bytes inside `out`; the test states that bound for the prover.
|
|
3370
|
+
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
|
|
3376
|
+
} else {
|
|
3377
|
+
setI32BE(this.out, header + 4, this.arrayNulls ? 1 : 0)
|
|
3378
|
+
setI32BE(this.out, header + PG_ARRAY_HEADER, this.arrayCount)
|
|
3379
|
+
}
|
|
3068
3380
|
}
|
|
3069
3381
|
this.arrayOid = 0
|
|
3070
3382
|
} else {
|
|
3071
|
-
this.tailRoom(
|
|
3383
|
+
this.tailRoom(toI64(1))
|
|
3072
3384
|
this.putByte(PG_RBRACE)
|
|
3073
3385
|
}
|
|
3074
3386
|
this.endParam(this.arrayAt)
|
|
@@ -3091,7 +3403,7 @@ export class PgConnection {
|
|
|
3091
3403
|
panic("nish/net/postgres: bindEnd() while an array is open")
|
|
3092
3404
|
}
|
|
3093
3405
|
const binary: boolean = this.bindBinary
|
|
3094
|
-
this.tailRoom(PG_TAIL + this.bindOwed)
|
|
3406
|
+
this.tailRoom(toI64(PG_TAIL) + toI64(this.bindOwed))
|
|
3095
3407
|
const at: i32 = this.bindAt
|
|
3096
3408
|
if (this.binaryParamCount > 0) {
|
|
3097
3409
|
this.putFormatCodes()
|
|
@@ -3125,9 +3437,11 @@ export class PgConnection {
|
|
|
3125
3437
|
return false
|
|
3126
3438
|
}
|
|
3127
3439
|
this.dropBind()
|
|
3128
|
-
|
|
3440
|
+
// The bind's group is in flight, so the count is positive.
|
|
3441
|
+
const inFlight: i32 = this.inFlight
|
|
3442
|
+
if (this.bindOpened && inFlight > 0) {
|
|
3129
3443
|
this.groupOpen = false
|
|
3130
|
-
this.inFlight
|
|
3444
|
+
this.inFlight = inFlight - 1
|
|
3131
3445
|
}
|
|
3132
3446
|
return true
|
|
3133
3447
|
}
|
|
@@ -3148,18 +3462,41 @@ export class PgConnection {
|
|
|
3148
3462
|
*/
|
|
3149
3463
|
private putFormatCodes(): void {
|
|
3150
3464
|
const from: i32 = this.paramCountAt
|
|
3151
|
-
const
|
|
3152
|
-
this.
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
setU16BE(this.out, from + 2 * i, toU16(0))
|
|
3465
|
+
const count: i32 = this.paramCount
|
|
3466
|
+
const end: i32 = this.outEnd
|
|
3467
|
+
// The count word lies before the parameters, a bind holds at most 65535 of
|
|
3468
|
+
// them, and the end lies past the count; the test states that for the prover.
|
|
3469
|
+
if (from < 2 || count < 0 || count > 65535 || end < 0 || end < from) {
|
|
3470
|
+
return
|
|
3158
3471
|
}
|
|
3159
|
-
|
|
3160
|
-
|
|
3472
|
+
const codes: i32 = count * 2
|
|
3473
|
+
this.reserve(toI64(end) + toI64(codes))
|
|
3474
|
+
const b: u8[] = this.out
|
|
3475
|
+
if (end > toI32(b.length) - codes) {
|
|
3476
|
+
return
|
|
3161
3477
|
}
|
|
3162
|
-
|
|
3478
|
+
// `reserve` made the room, so both ends fit an `i32`.
|
|
3479
|
+
const moved: i32 = toI32(toI64(from) + toI64(codes))
|
|
3480
|
+
copyBytes(b, moved, b, from, end - from)
|
|
3481
|
+
this.outEnd = toI32(toI64(end) + toI64(codes))
|
|
3482
|
+
setU16BE(b, from - 2, toU16(count))
|
|
3483
|
+
// Every code lies in `[from, from + codes)`, inside `b`.
|
|
3484
|
+
for (let i: i32 = 0; i < count; i++) {
|
|
3485
|
+
const at: i32 = toI32(toI64(from) + toI64(i) * toI64(2))
|
|
3486
|
+
if (at > toI32(b.length) - 2) {
|
|
3487
|
+
break
|
|
3488
|
+
}
|
|
3489
|
+
setU16BE(b, at, toU16(0))
|
|
3490
|
+
}
|
|
3491
|
+
const params: i32[] = this.binaryParams
|
|
3492
|
+
for (let k: i32 = 0; k < this.binaryParamCount && k < toI32(params.length); k++) {
|
|
3493
|
+
const param: i32 = params[k]
|
|
3494
|
+
if (param < 0 || param >= count) {
|
|
3495
|
+
break
|
|
3496
|
+
}
|
|
3497
|
+
setU16BE(b, toI32(toI64(from) + toI64(param) * toI64(2)), toU16(PG_FORMAT_BINARY))
|
|
3498
|
+
}
|
|
3499
|
+
this.paramCountAt = moved
|
|
3163
3500
|
}
|
|
3164
3501
|
|
|
3165
3502
|
/**
|
|
@@ -3173,7 +3510,7 @@ export class PgConnection {
|
|
|
3173
3510
|
return false
|
|
3174
3511
|
}
|
|
3175
3512
|
this.groupOpen = false
|
|
3176
|
-
this.tailRoom(
|
|
3513
|
+
this.tailRoom(toI64(5))
|
|
3177
3514
|
const at: i32 = this.startMessage(PG_FE_SYNC)
|
|
3178
3515
|
this.endMessage(at)
|
|
3179
3516
|
return true
|
|
@@ -3188,7 +3525,7 @@ export class PgConnection {
|
|
|
3188
3525
|
if (this.bindAt >= 0) {
|
|
3189
3526
|
this.dropBind()
|
|
3190
3527
|
}
|
|
3191
|
-
this.tailRoom(
|
|
3528
|
+
this.tailRoom(toI64(5))
|
|
3192
3529
|
const at: i32 = this.startMessage(PG_FE_TERMINATE)
|
|
3193
3530
|
this.endMessage(at)
|
|
3194
3531
|
}
|
|
@@ -3234,7 +3571,8 @@ export class PgConnection {
|
|
|
3234
3571
|
return ""
|
|
3235
3572
|
}
|
|
3236
3573
|
const start: i32 = this.colStarts[c]
|
|
3237
|
-
|
|
3574
|
+
// `readRow` held the column inside the row, so its end fits an `i32`.
|
|
3575
|
+
return String.fromBytes(this.input, start, toI32(toI64(start) + toI64(length)))
|
|
3238
3576
|
}
|
|
3239
3577
|
|
|
3240
3578
|
/**
|
|
@@ -3303,10 +3641,20 @@ export class PgConnection {
|
|
|
3303
3641
|
*/
|
|
3304
3642
|
tagIs(word: string): boolean {
|
|
3305
3643
|
const n: i32 = toI32(word.length)
|
|
3306
|
-
|
|
3644
|
+
const start: i32 = this.tagStart
|
|
3645
|
+
const b: u8[] = this.input
|
|
3646
|
+
if (n > this.tagLength || !pgBytesAre(b, start, n, word)) {
|
|
3647
|
+
return false
|
|
3648
|
+
}
|
|
3649
|
+
if (n === this.tagLength) {
|
|
3650
|
+
return true
|
|
3651
|
+
}
|
|
3652
|
+
// The tag lies inside the buffer, so the byte after the word does; the test states that for the prover.
|
|
3653
|
+
const at: i32 = toI32(toI64(start) + toI64(n))
|
|
3654
|
+
if (at < 0 || at >= toI32(b.length)) {
|
|
3307
3655
|
return false
|
|
3308
3656
|
}
|
|
3309
|
-
return
|
|
3657
|
+
return toI32(b[at]) === 32
|
|
3310
3658
|
}
|
|
3311
3659
|
|
|
3312
3660
|
/**
|
|
@@ -3317,18 +3665,32 @@ export class PgConnection {
|
|
|
3317
3665
|
* tag has none (`BEGIN`) or it does not fit an i64.
|
|
3318
3666
|
*/
|
|
3319
3667
|
tagRows(): i64 {
|
|
3320
|
-
const
|
|
3668
|
+
const start: i32 = this.tagStart
|
|
3669
|
+
const b: u8[] = this.input
|
|
3670
|
+
const last: i64 = toI64(start) + toI64(this.tagLength)
|
|
3671
|
+
// The tag lies inside the buffer; the test states that bound for the prover.
|
|
3672
|
+
if (start < 0 || last < toI64(start) || last > toI64(b.length)) {
|
|
3673
|
+
return -1
|
|
3674
|
+
}
|
|
3675
|
+
const end: i32 = toI32(last)
|
|
3676
|
+
if (end < 0) {
|
|
3677
|
+
return -1
|
|
3678
|
+
}
|
|
3321
3679
|
let p: i32 = end
|
|
3322
|
-
while (p >
|
|
3680
|
+
while (p > start && p > 0 && p <= toI32(b.length)) {
|
|
3681
|
+
const digit: i32 = toI32(b[p - 1])
|
|
3682
|
+
if (digit < 48 || digit > 57) {
|
|
3683
|
+
break
|
|
3684
|
+
}
|
|
3323
3685
|
p--
|
|
3324
3686
|
}
|
|
3325
|
-
if (p === end || p
|
|
3687
|
+
if (p === end || p <= start || p <= 0 || p > toI32(b.length) || toI32(b[p - 1]) !== 32) {
|
|
3326
3688
|
return -1
|
|
3327
3689
|
}
|
|
3328
3690
|
// Read without touching `text.failed`, which belongs to the program's own decoding.
|
|
3329
3691
|
const failed: boolean = this.text.failed
|
|
3330
3692
|
this.text.failed = false
|
|
3331
|
-
const rows: i64 = this.text.int(
|
|
3693
|
+
const rows: i64 = this.text.int(b, p, end - p)
|
|
3332
3694
|
const overflowed: boolean = this.text.failed
|
|
3333
3695
|
this.text.failed = failed
|
|
3334
3696
|
return overflowed ? -1 : rows
|
|
@@ -3393,18 +3755,91 @@ class PgPoolSlot {
|
|
|
3393
3755
|
this.leads = new Array<i32>(ring)
|
|
3394
3756
|
}
|
|
3395
3757
|
|
|
3396
|
-
/**
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3758
|
+
/**
|
|
3759
|
+
* Records a group at the ring's tail; false, and nothing recorded, only when the ring is full,
|
|
3760
|
+
* which the pool never lets it be: `dispatch` hands a slot a group only while its ring has room.
|
|
3761
|
+
* @internal Used by `PgPool.dispatch` and `PgPool.next` in this module.
|
|
3762
|
+
*/
|
|
3763
|
+
push(ticket: i32, leads: i32): boolean {
|
|
3764
|
+
const tickets: i32[] = this.tickets
|
|
3765
|
+
const leadCounts: i32[] = this.leads
|
|
3766
|
+
const head: i32 = this.head
|
|
3767
|
+
const count: i32 = this.count
|
|
3768
|
+
const size: i32 = toI32(tickets.length)
|
|
3769
|
+
// `head` is a slot of the ring and `count` below its size; the test states those bounds for the prover.
|
|
3770
|
+
if (count < 0 || count >= size || head < 0 || head >= size) {
|
|
3771
|
+
return false
|
|
3772
|
+
}
|
|
3773
|
+
// Below twice the ring's size, at most 2048.
|
|
3774
|
+
const sum: i64 = toI64(head) + toI64(count)
|
|
3775
|
+
const at: i32 = toI32(sum < toI64(size) ? sum : sum - toI64(size))
|
|
3776
|
+
// `at` is a slot of the ring, which both arrays are; the test states that for the prover.
|
|
3777
|
+
if (at < 0 || at >= toI32(tickets.length) || at >= toI32(leadCounts.length)) {
|
|
3778
|
+
return false
|
|
3779
|
+
}
|
|
3780
|
+
tickets[at] = ticket
|
|
3781
|
+
leadCounts[at] = leads
|
|
3782
|
+
this.count = count + 1
|
|
3783
|
+
return true
|
|
3402
3784
|
}
|
|
3403
3785
|
|
|
3404
|
-
/**
|
|
3786
|
+
/**
|
|
3787
|
+
* Drops the oldest group in flight; nothing when none is.
|
|
3788
|
+
* @internal Used by `PgPool.next` in this module.
|
|
3789
|
+
*/
|
|
3405
3790
|
pop(): void {
|
|
3406
|
-
|
|
3407
|
-
this.count
|
|
3791
|
+
const head: i32 = this.head
|
|
3792
|
+
const count: i32 = this.count
|
|
3793
|
+
const size: i32 = toI32(this.tickets.length)
|
|
3794
|
+
// `head` is a slot of the ring; the test states that bound for the prover.
|
|
3795
|
+
if (count <= 0 || head < 0 || head >= size) {
|
|
3796
|
+
return
|
|
3797
|
+
}
|
|
3798
|
+
const after: i32 = head + 1
|
|
3799
|
+
this.head = after === size ? 0 : after
|
|
3800
|
+
this.count = count - 1
|
|
3801
|
+
}
|
|
3802
|
+
|
|
3803
|
+
/**
|
|
3804
|
+
* The ticket of the oldest group in flight; -1 when none is.
|
|
3805
|
+
* @internal Used by `PgPool.next` in this module.
|
|
3806
|
+
*/
|
|
3807
|
+
oldest(): i32 {
|
|
3808
|
+
const tickets: i32[] = this.tickets
|
|
3809
|
+
const head: i32 = this.head
|
|
3810
|
+
// `head` is a slot of the ring; the test states that bound for the prover.
|
|
3811
|
+
if (this.count <= 0 || head < 0 || head >= toI32(tickets.length)) {
|
|
3812
|
+
return -1
|
|
3813
|
+
}
|
|
3814
|
+
return tickets[head]
|
|
3815
|
+
}
|
|
3816
|
+
|
|
3817
|
+
/**
|
|
3818
|
+
* How many of the pool's prepares still lead the oldest group in flight; 0 when none is.
|
|
3819
|
+
* @internal Used by `PgPool.next` in this module.
|
|
3820
|
+
*/
|
|
3821
|
+
leading(): i32 {
|
|
3822
|
+
const leadCounts: i32[] = this.leads
|
|
3823
|
+
const head: i32 = this.head
|
|
3824
|
+
// `head` is a slot of the ring; the test states that bound for the prover.
|
|
3825
|
+
if (this.count <= 0 || head < 0 || head >= toI32(leadCounts.length)) {
|
|
3826
|
+
return 0
|
|
3827
|
+
}
|
|
3828
|
+
return leadCounts[head]
|
|
3829
|
+
}
|
|
3830
|
+
|
|
3831
|
+
/**
|
|
3832
|
+
* Sets how many prepares lead the oldest group in flight; nothing when none is.
|
|
3833
|
+
* @internal Used by `PgPool.next` in this module.
|
|
3834
|
+
*/
|
|
3835
|
+
setLeading(n: i32): void {
|
|
3836
|
+
const leadCounts: i32[] = this.leads
|
|
3837
|
+
const head: i32 = this.head
|
|
3838
|
+
// `head` is a slot of the ring; the test states that bound for the prover.
|
|
3839
|
+
if (this.count <= 0 || head < 0 || head >= toI32(leadCounts.length)) {
|
|
3840
|
+
return
|
|
3841
|
+
}
|
|
3842
|
+
leadCounts[head] = n
|
|
3408
3843
|
}
|
|
3409
3844
|
}
|
|
3410
3845
|
|
|
@@ -3554,14 +3989,29 @@ export class PgPool {
|
|
|
3554
3989
|
* after 2^31-1; or `PG_POOL_FULL`.
|
|
3555
3990
|
*/
|
|
3556
3991
|
request(): i32 {
|
|
3557
|
-
const
|
|
3558
|
-
|
|
3992
|
+
const queue: i32[] = this.queue
|
|
3993
|
+
const room: i32 = toI32(queue.length)
|
|
3994
|
+
const head: i32 = this.queueHead
|
|
3995
|
+
const count: i32 = this.queueCount
|
|
3996
|
+
// Full at `count === room`; below that, `count` is not negative and `head` is a slot of the
|
|
3997
|
+
// ring, which the test states for the prover.
|
|
3998
|
+
if (count < 0 || count >= room || head < 0 || head >= room) {
|
|
3999
|
+
return PG_POOL_FULL
|
|
4000
|
+
}
|
|
4001
|
+
// Below twice the queue's size, at most 2^21.
|
|
4002
|
+
const sum: i64 = toI64(head) + toI64(count)
|
|
4003
|
+
const at: i32 = toI32(sum < toI64(room) ? sum : sum - toI64(room))
|
|
4004
|
+
// `at` is a slot of the ring; the test states that bound for the prover.
|
|
4005
|
+
if (at < 0 || at >= toI32(queue.length)) {
|
|
3559
4006
|
return PG_POOL_FULL
|
|
3560
4007
|
}
|
|
3561
4008
|
const ticket: i32 = this.nextTicket
|
|
3562
|
-
this.nextTicket =
|
|
3563
|
-
|
|
3564
|
-
|
|
4009
|
+
this.nextTicket = 0
|
|
4010
|
+
if (ticket < 2147483647) {
|
|
4011
|
+
this.nextTicket = ticket + 1
|
|
4012
|
+
}
|
|
4013
|
+
queue[at] = ticket
|
|
4014
|
+
this.queueCount = count + 1
|
|
3565
4015
|
return ticket
|
|
3566
4016
|
}
|
|
3567
4017
|
|
|
@@ -3583,7 +4033,13 @@ export class PgPool {
|
|
|
3583
4033
|
inFlight(): i32 {
|
|
3584
4034
|
let n: i32 = 0
|
|
3585
4035
|
for (const slot of this.slots) {
|
|
3586
|
-
|
|
4036
|
+
const count: i32 = slot.count
|
|
4037
|
+
// A slot's ring holds at most `maxInFlight`, at most 1024, and the pool at most 1024
|
|
4038
|
+
// slots, so the sum stays at most 2^20; the test states those bounds for the prover.
|
|
4039
|
+
if (count < 0 || count > 1024 || n > 1047552) {
|
|
4040
|
+
return n
|
|
4041
|
+
}
|
|
4042
|
+
n += count
|
|
3587
4043
|
}
|
|
3588
4044
|
return n
|
|
3589
4045
|
}
|
|
@@ -3605,47 +4061,74 @@ export class PgPool {
|
|
|
3605
4061
|
if (this.building >= 0 || this.queueCount === 0) {
|
|
3606
4062
|
return PG_POOL_NONE
|
|
3607
4063
|
}
|
|
4064
|
+
const slots: PgPoolSlot[] = this.slots
|
|
3608
4065
|
const cap: i32 = this.config.maxInFlight
|
|
3609
4066
|
const pack: boolean = this.config.pack
|
|
3610
4067
|
let best: i32 = -1
|
|
3611
4068
|
let load: i32 = 0
|
|
3612
4069
|
let starting: i32 = 0
|
|
3613
4070
|
let idle: i32 = -1
|
|
3614
|
-
for (let s: i32 = 0; s <
|
|
3615
|
-
const slot: PgPoolSlot =
|
|
4071
|
+
for (let s: i32 = 0; s < toI32(slots.length); s++) {
|
|
4072
|
+
const slot: PgPoolSlot = slots[s]
|
|
3616
4073
|
const conn: PgConnection | null = slot.conn
|
|
3617
4074
|
if (slot.state === PG_SLOT_READY && conn !== null) {
|
|
3618
|
-
|
|
4075
|
+
// The slot's ring must have room too: a group dropped on a failed connection stays on
|
|
4076
|
+
// it until the failure drains it.
|
|
4077
|
+
if (
|
|
4078
|
+
conn.inFlight < cap &&
|
|
4079
|
+
slot.count < toI32(slot.tickets.length) &&
|
|
4080
|
+
(best < 0 || (!pack && conn.inFlight < load))
|
|
4081
|
+
) {
|
|
3619
4082
|
best = s
|
|
3620
4083
|
load = conn.inFlight
|
|
3621
4084
|
}
|
|
3622
4085
|
} else if (slot.state === PG_SLOT_WANTED || slot.state === PG_SLOT_STARTING) {
|
|
3623
|
-
|
|
4086
|
+
// At most one for each slot, so below the slot count; the test states that for the prover.
|
|
4087
|
+
if (starting < toI32(slots.length)) {
|
|
4088
|
+
starting += 1
|
|
4089
|
+
}
|
|
3624
4090
|
} else if (slot.state === PG_SLOT_IDLE && idle < 0) {
|
|
3625
4091
|
idle = s
|
|
3626
4092
|
}
|
|
3627
4093
|
}
|
|
3628
4094
|
if (best < 0) {
|
|
3629
|
-
|
|
3630
|
-
|
|
4095
|
+
const spare: PgPoolSlot | null = this.slotAt(idle)
|
|
4096
|
+
if (spare !== null && toI64(this.queueCount) > toI64(starting) * toI64(cap)) {
|
|
4097
|
+
spare.state = PG_SLOT_WANTED
|
|
3631
4098
|
}
|
|
3632
4099
|
return PG_POOL_NONE
|
|
3633
4100
|
}
|
|
3634
|
-
const
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
4101
|
+
const slot: PgPoolSlot | null = this.slotAt(best)
|
|
4102
|
+
const ticket: i32 = this.dequeue()
|
|
4103
|
+
// `best` is a slot the loop found, and a request waits; the test states both for the prover.
|
|
4104
|
+
if (slot === null || ticket < 0) {
|
|
4105
|
+
return PG_POOL_NONE
|
|
4106
|
+
}
|
|
3638
4107
|
const conn: PgConnection = this.connection(best)
|
|
4108
|
+
const names: string[] = this.names
|
|
4109
|
+
const sqls: string[] = this.sqls
|
|
4110
|
+
const types: i32[][] = this.types
|
|
4111
|
+
const start: i32 = slot.queued
|
|
4112
|
+
let k: i32 = start
|
|
4113
|
+
// The three lists grow together, so one bound holds for all; the tests state it for each.
|
|
4114
|
+
while (
|
|
4115
|
+
k >= 0 &&
|
|
4116
|
+
k < toI32(names.length) &&
|
|
4117
|
+
k < toI32(sqls.length) &&
|
|
4118
|
+
k < toI32(types.length) &&
|
|
4119
|
+
conn.prepare(names[k], sqls[k], types[k])
|
|
4120
|
+
) {
|
|
4121
|
+
k += 1
|
|
4122
|
+
}
|
|
4123
|
+
slot.queued = k
|
|
3639
4124
|
let leads: i32 = 0
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
3645
|
-
|
|
3646
|
-
leads += 1
|
|
4125
|
+
if (start >= 0 && k >= 0 && k >= start) {
|
|
4126
|
+
leads = k - start
|
|
4127
|
+
}
|
|
4128
|
+
// The loop above chose `best` only with room on its ring; the test states that for the prover.
|
|
4129
|
+
if (!slot.push(ticket, leads)) {
|
|
4130
|
+
return PG_POOL_NONE
|
|
3647
4131
|
}
|
|
3648
|
-
slot.push(ticket, leads)
|
|
3649
4132
|
this.building = best
|
|
3650
4133
|
this.ticket = ticket
|
|
3651
4134
|
return best
|
|
@@ -3664,17 +4147,24 @@ export class PgPool {
|
|
|
3664
4147
|
if (s < 0) {
|
|
3665
4148
|
return false
|
|
3666
4149
|
}
|
|
3667
|
-
const slot: PgPoolSlot = this.
|
|
4150
|
+
const slot: PgPoolSlot | null = this.slotAt(s)
|
|
4151
|
+
// `building` names a slot `dispatch` handed a request to; the test states that for the prover.
|
|
4152
|
+
if (slot === null) {
|
|
4153
|
+
this.building = -1
|
|
4154
|
+
return false
|
|
4155
|
+
}
|
|
3668
4156
|
const conn: PgConnection = this.connection(s)
|
|
3669
4157
|
if (conn.sync()) {
|
|
3670
4158
|
this.building = -1
|
|
3671
4159
|
return true
|
|
3672
4160
|
}
|
|
3673
|
-
|
|
4161
|
+
const count: i32 = slot.count
|
|
4162
|
+
if (conn.inFlight < count) {
|
|
3674
4163
|
// No message opened the group. A failed connection reports the request
|
|
3675
4164
|
// lost with the rest; a live one would never answer it, so it is dropped.
|
|
3676
|
-
|
|
3677
|
-
|
|
4165
|
+
// The request is on the slot's ring, so `count` is above 0; the test states that for the prover.
|
|
4166
|
+
if (conn.failure === PG_ERR_NONE && count > 0) {
|
|
4167
|
+
slot.count = count - 1
|
|
3678
4168
|
}
|
|
3679
4169
|
this.building = -1
|
|
3680
4170
|
}
|
|
@@ -3687,8 +4177,9 @@ export class PgPool {
|
|
|
3687
4177
|
* @returns The lowest wanted slot, or `PG_POOL_NONE` when no slot is wanted.
|
|
3688
4178
|
*/
|
|
3689
4179
|
wanted(): i32 {
|
|
3690
|
-
|
|
3691
|
-
|
|
4180
|
+
const slots: PgPoolSlot[] = this.slots
|
|
4181
|
+
for (let s: i32 = 0; s < toI32(slots.length); s++) {
|
|
4182
|
+
if (slots[s].state === PG_SLOT_WANTED) {
|
|
3692
4183
|
return s
|
|
3693
4184
|
}
|
|
3694
4185
|
}
|
|
@@ -3707,7 +4198,8 @@ export class PgPool {
|
|
|
3707
4198
|
* `PgConnection` constructor takes them; a new one for every connection.
|
|
3708
4199
|
*/
|
|
3709
4200
|
connect(slot: i32, nonce: u8[]): void {
|
|
3710
|
-
|
|
4201
|
+
const s: PgPoolSlot | null = this.slotAt(slot)
|
|
4202
|
+
if (s === null || s.state !== PG_SLOT_WANTED) {
|
|
3711
4203
|
panic("nish/net/postgres: PgPool.connect on a slot that wants no connection")
|
|
3712
4204
|
}
|
|
3713
4205
|
if (this.password.gone) {
|
|
@@ -3717,7 +4209,6 @@ export class PgPool {
|
|
|
3717
4209
|
if (this.config.tls !== PG_TLS_DISABLE && carried === null) {
|
|
3718
4210
|
panic("nish/net/postgres: PgConfig.tls asks for TLS, which only PgCarrier speaks")
|
|
3719
4211
|
}
|
|
3720
|
-
const s: PgPoolSlot = this.slots[slot]
|
|
3721
4212
|
s.conn = new PgConnection(carried !== null ? carried : this.config, this.password.bytes, nonce)
|
|
3722
4213
|
s.state = PG_SLOT_STARTING
|
|
3723
4214
|
s.queued = 0
|
|
@@ -3735,7 +4226,8 @@ export class PgPool {
|
|
|
3735
4226
|
* @returns The slot's connection, owned by the pool: a later `connect` on the slot replaces it.
|
|
3736
4227
|
*/
|
|
3737
4228
|
connection(slot: i32): PgConnection {
|
|
3738
|
-
const
|
|
4229
|
+
const s: PgPoolSlot | null = this.slotAt(slot)
|
|
4230
|
+
const conn: PgConnection | null = s !== null ? s.conn : null
|
|
3739
4231
|
if (conn === null) {
|
|
3740
4232
|
panic("nish/net/postgres: PgPool.connection on a slot with no connection")
|
|
3741
4233
|
}
|
|
@@ -3759,31 +4251,44 @@ export class PgPool {
|
|
|
3759
4251
|
*/
|
|
3760
4252
|
next(slot: i32): i32 {
|
|
3761
4253
|
const conn: PgConnection = this.connection(slot)
|
|
3762
|
-
const s: PgPoolSlot = this.
|
|
3763
|
-
|
|
4254
|
+
const s: PgPoolSlot | null = this.slotAt(slot)
|
|
4255
|
+
// `connection(slot)` refused a slot out of range; the test states that for the prover.
|
|
4256
|
+
if (s === null || s.state === PG_SLOT_IDLE || s.state === PG_SLOT_WANTED) {
|
|
3764
4257
|
this.ticket = -1
|
|
3765
4258
|
return PG_NEED_MORE
|
|
3766
4259
|
}
|
|
3767
4260
|
while (s.state === PG_SLOT_STARTING || s.state === PG_SLOT_READY) {
|
|
3768
|
-
this.ticket = s.
|
|
4261
|
+
this.ticket = s.oldest()
|
|
3769
4262
|
const event: i32 = conn.next()
|
|
3770
4263
|
if (event === PG_READY) {
|
|
3771
4264
|
s.state = PG_SLOT_READY
|
|
3772
|
-
} else if (event === PG_DESCRIBED
|
|
3773
|
-
s.
|
|
3774
|
-
s.
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
4265
|
+
} else if (event === PG_DESCRIBED || event === PG_ERROR) {
|
|
4266
|
+
// Only an event about a prepare leading the oldest group is the pool's own.
|
|
4267
|
+
const lead: i32 = s.leading()
|
|
4268
|
+
if (lead > 0 && event === PG_DESCRIBED) {
|
|
4269
|
+
s.setLeading(lead - 1)
|
|
4270
|
+
const described: i32 = s.described
|
|
4271
|
+
// Only a queued prepare is described, so `described` stays below `queued`; the test
|
|
4272
|
+
// states that for the prover.
|
|
4273
|
+
if (described < s.queued) {
|
|
4274
|
+
s.described = described + 1
|
|
4275
|
+
}
|
|
4276
|
+
continue
|
|
4277
|
+
}
|
|
4278
|
+
if (lead > 0) {
|
|
4279
|
+
// The rest of the group's prepares were skipped: the next request prepares them again.
|
|
4280
|
+
s.setLeading(toI32(0))
|
|
4281
|
+
s.queued = s.described
|
|
4282
|
+
}
|
|
3780
4283
|
} else if (event === PG_GROUP_DONE) {
|
|
3781
4284
|
s.pop()
|
|
3782
4285
|
} else if (event === PG_FAILED) {
|
|
3783
4286
|
if (s.state === PG_SLOT_STARTING && this.queueCount > 0 && !this.anyReady()) {
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
4287
|
+
// A starting slot was given no request, so its ring is empty and takes this one.
|
|
4288
|
+
const ticket: i32 = this.dequeue()
|
|
4289
|
+
if (ticket >= 0) {
|
|
4290
|
+
s.push(ticket, toI32(0))
|
|
4291
|
+
}
|
|
3787
4292
|
}
|
|
3788
4293
|
if (this.building === slot) {
|
|
3789
4294
|
this.building = -1
|
|
@@ -3794,7 +4299,7 @@ export class PgPool {
|
|
|
3794
4299
|
return event
|
|
3795
4300
|
}
|
|
3796
4301
|
if (s.state === PG_SLOT_DRAINING && s.count > 0) {
|
|
3797
|
-
this.ticket = s.
|
|
4302
|
+
this.ticket = s.oldest()
|
|
3798
4303
|
s.pop()
|
|
3799
4304
|
return PG_LOST
|
|
3800
4305
|
}
|
|
@@ -3803,6 +4308,31 @@ export class PgPool {
|
|
|
3803
4308
|
return PG_FAILED
|
|
3804
4309
|
}
|
|
3805
4310
|
|
|
4311
|
+
/** The slot at `slot`, or null when `slot` is not one of the pool's. */
|
|
4312
|
+
private slotAt(slot: i32): PgPoolSlot | null {
|
|
4313
|
+
const slots: PgPoolSlot[] = this.slots
|
|
4314
|
+
if (slot < 0 || slot >= toI32(slots.length)) {
|
|
4315
|
+
return null
|
|
4316
|
+
}
|
|
4317
|
+
return slots[slot]
|
|
4318
|
+
}
|
|
4319
|
+
|
|
4320
|
+
/** Takes the oldest waiting request off the queue and answers its ticket; -1 when none waits. */
|
|
4321
|
+
private dequeue(): i32 {
|
|
4322
|
+
const queue: i32[] = this.queue
|
|
4323
|
+
const head: i32 = this.queueHead
|
|
4324
|
+
const count: i32 = this.queueCount
|
|
4325
|
+
// `queueHead` is a slot of the queue's ring; the test states that bound for the prover.
|
|
4326
|
+
if (count <= 0 || head < 0 || head >= toI32(queue.length)) {
|
|
4327
|
+
return -1
|
|
4328
|
+
}
|
|
4329
|
+
const ticket: i32 = queue[head]
|
|
4330
|
+
const after: i32 = head + 1
|
|
4331
|
+
this.queueHead = after === toI32(queue.length) ? 0 : after
|
|
4332
|
+
this.queueCount = count - 1
|
|
4333
|
+
return ticket
|
|
4334
|
+
}
|
|
4335
|
+
|
|
3806
4336
|
private anyReady(): boolean {
|
|
3807
4337
|
for (const slot of this.slots) {
|
|
3808
4338
|
if (slot.state === PG_SLOT_READY) {
|
|
@@ -4174,13 +4704,17 @@ export class PgBatcher {
|
|
|
4174
4704
|
* @internal Used by `PgCarrier.next` in std/net/postgres-carrier.ts.
|
|
4175
4705
|
*/
|
|
4176
4706
|
shed(): i32 {
|
|
4177
|
-
|
|
4707
|
+
const count: i32 = this.count
|
|
4708
|
+
const tags: i32[] = this.qTags
|
|
4709
|
+
const head: i32 = this.head
|
|
4710
|
+
// `head` is a slot of the ring while a request waits; the test states that bound for the prover.
|
|
4711
|
+
if (count <= 0 || head < 0 || head >= toI32(tags.length)) {
|
|
4178
4712
|
return -1
|
|
4179
4713
|
}
|
|
4180
|
-
const tag: i32 =
|
|
4181
|
-
this.head =
|
|
4182
|
-
this.count
|
|
4183
|
-
if (
|
|
4714
|
+
const tag: i32 = tags[head]
|
|
4715
|
+
this.head = head + 1 === toI32(tags.length) ? 0 : head + 1
|
|
4716
|
+
this.count = count - 1
|
|
4717
|
+
if (count === 1) {
|
|
4184
4718
|
this.storeEnd = 0
|
|
4185
4719
|
}
|
|
4186
4720
|
return tag
|
|
@@ -4200,12 +4734,13 @@ export class PgBatcher {
|
|
|
4200
4734
|
panic("nish/net/postgres: PgBatcher.addInt on a text batcher")
|
|
4201
4735
|
}
|
|
4202
4736
|
const q: i32 = this.slotToAdd()
|
|
4203
|
-
|
|
4737
|
+
const ints: i64[] = this.qInts
|
|
4738
|
+
// `slotToAdd` answers a slot of the ring, which `qInts` is on an int8 batcher; the test states that for the prover.
|
|
4739
|
+
if (q < 0 || q >= toI32(ints.length)) {
|
|
4204
4740
|
return false
|
|
4205
4741
|
}
|
|
4206
|
-
|
|
4207
|
-
this.added(q, tag, pgIntHash(key), pgDecimalLength(key))
|
|
4208
|
-
return true
|
|
4742
|
+
ints[q] = key
|
|
4743
|
+
return this.added(q, tag, pgIntHash(key), pgDecimalLength(key))
|
|
4209
4744
|
}
|
|
4210
4745
|
|
|
4211
4746
|
/**
|
|
@@ -4231,80 +4766,153 @@ export class PgBatcher {
|
|
|
4231
4766
|
if (start < 0 || length < 0 || length > toI32(bytes.length) - start) {
|
|
4232
4767
|
panic("nish/net/postgres: PgBatcher.addText outside its source")
|
|
4233
4768
|
}
|
|
4234
|
-
|
|
4769
|
+
// Inside `bytes`, by the test above.
|
|
4770
|
+
const end: i32 = toI32(toI64(start) + toI64(length))
|
|
4235
4771
|
if (!pgIsText(bytes, start, end)) {
|
|
4236
4772
|
return false
|
|
4237
4773
|
}
|
|
4238
4774
|
// The element as `arrayElement` writes it: quoted, `"` and `\` escaped;
|
|
4239
4775
|
// and FNV-1a, whose unsigned multiply wraps by definition.
|
|
4240
|
-
let
|
|
4776
|
+
let escapes: i32 = 0
|
|
4241
4777
|
let h: u32 = 2166136261
|
|
4242
4778
|
for (let i: i32 = start; i < end && i < toI32(bytes.length); i++) {
|
|
4243
4779
|
const c: u8 = bytes[i]
|
|
4244
|
-
if (toI32(c) === PG_QUOTE || toI32(c) === PG_BACKSLASH) {
|
|
4245
|
-
|
|
4780
|
+
if ((toI32(c) === PG_QUOTE || toI32(c) === PG_BACKSLASH) && escapes < length) {
|
|
4781
|
+
escapes += 1
|
|
4246
4782
|
}
|
|
4247
4783
|
h = (h ^ toU32(c)) * 16777619
|
|
4248
4784
|
}
|
|
4249
|
-
|
|
4785
|
+
// With the array's two braces, within `maxBytes`, so the size fits an `i32`.
|
|
4786
|
+
if (toI64(length) + toI64(escapes) + toI64(4) > toI64(this.maxBytes)) {
|
|
4250
4787
|
return false
|
|
4251
4788
|
}
|
|
4789
|
+
const size: i32 = toI32(toI64(length) + toI64(escapes) + toI64(2))
|
|
4252
4790
|
const q: i32 = this.slotToAdd()
|
|
4253
4791
|
if (q < 0) {
|
|
4254
4792
|
return false
|
|
4255
4793
|
}
|
|
4794
|
+
const store: u8[] = this.store
|
|
4795
|
+
const starts: i32[] = this.qStarts
|
|
4796
|
+
const lengths: i32[] = this.qLengths
|
|
4797
|
+
const cap: i32 = toI32(store.length)
|
|
4256
4798
|
let at: i32 = this.storeEnd
|
|
4257
|
-
|
|
4799
|
+
// `storeEnd` lies in the store, and `q` is a slot of the ring, which `qStarts` and
|
|
4800
|
+
// `qLengths` are on a text batcher; the test states those bounds for the prover.
|
|
4801
|
+
if (at < 0 || at > cap || q >= toI32(starts.length) || q >= toI32(lengths.length)) {
|
|
4802
|
+
return false
|
|
4803
|
+
}
|
|
4804
|
+
if (length > cap - at) {
|
|
4258
4805
|
// The waiting keys are moved to the front, the room the taken ones left after them.
|
|
4259
4806
|
// Refused first when even that would not make room, so a full store is not moved for nothing.
|
|
4260
|
-
const
|
|
4261
|
-
|
|
4807
|
+
const head: i32 = this.head
|
|
4808
|
+
let from: i32 = at
|
|
4809
|
+
if (this.count > 0 && head >= 0 && head < toI32(starts.length)) {
|
|
4810
|
+
from = starts[head]
|
|
4811
|
+
}
|
|
4812
|
+
// The oldest waiting key starts in the store's used part; the test states that for the prover.
|
|
4813
|
+
if (from < 0 || from > at || toI64(length) + toI64(at) - toI64(from) > toI64(cap)) {
|
|
4262
4814
|
return false
|
|
4263
4815
|
}
|
|
4264
|
-
copyBytes(
|
|
4816
|
+
copyBytes(store, 0, store, from, at - from)
|
|
4817
|
+
let w: i32 = head
|
|
4265
4818
|
for (let i: i32 = 0; i < this.count; i++) {
|
|
4266
|
-
|
|
4267
|
-
|
|
4819
|
+
// `w` walks the ring from `head`, and each waiting key starts at or after the oldest's.
|
|
4820
|
+
if (w < 0 || w >= toI32(starts.length)) {
|
|
4821
|
+
break
|
|
4822
|
+
}
|
|
4823
|
+
const s: i32 = starts[w]
|
|
4824
|
+
if (s >= from && s >= 0) {
|
|
4825
|
+
starts[w] = s - from
|
|
4826
|
+
}
|
|
4827
|
+
w += 1
|
|
4828
|
+
if (w === toI32(starts.length)) {
|
|
4829
|
+
w = 0
|
|
4830
|
+
}
|
|
4268
4831
|
}
|
|
4269
4832
|
at -= from
|
|
4270
4833
|
this.storeEnd = at
|
|
4271
4834
|
}
|
|
4272
|
-
copyBytes(
|
|
4273
|
-
|
|
4274
|
-
this.
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
return
|
|
4835
|
+
copyBytes(store, at, bytes, start, length)
|
|
4836
|
+
// `copyBytes` checked that the key fits after `at`.
|
|
4837
|
+
this.storeEnd = toI32(toI64(at) + toI64(length))
|
|
4838
|
+
starts[q] = at
|
|
4839
|
+
lengths[q] = length
|
|
4840
|
+
return this.added(q, tag, toI32(h >> 1), size)
|
|
4278
4841
|
}
|
|
4279
4842
|
|
|
4280
4843
|
/** The ring slot the next request goes in, or -1 when the queue is full. */
|
|
4281
4844
|
private slotToAdd(): i32 {
|
|
4282
4845
|
const size: i32 = toI32(this.qTags.length)
|
|
4283
|
-
|
|
4846
|
+
const head: i32 = this.head
|
|
4847
|
+
const count: i32 = this.count
|
|
4848
|
+
// `head` is a slot of the ring and `count` at most its size; the test states those bounds for the prover.
|
|
4849
|
+
if (count < 0 || count >= size || head < 0 || head >= size) {
|
|
4850
|
+
return -1
|
|
4851
|
+
}
|
|
4852
|
+
// Below twice the ring's size, at most 2^21.
|
|
4853
|
+
const at: i64 = toI64(head) + toI64(count)
|
|
4854
|
+
return toI32(at < toI64(size) ? at : at - toI64(size))
|
|
4284
4855
|
}
|
|
4285
4856
|
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
this.
|
|
4857
|
+
/** Records the request `slotToAdd` placed at `q`; false, and nothing recorded, only when `q` is not a slot of the ring. */
|
|
4858
|
+
private added(q: i32, tag: i32, hash: i32, size: i32): boolean {
|
|
4859
|
+
const tags: i32[] = this.qTags
|
|
4860
|
+
const hashes: i32[] = this.qHashes
|
|
4861
|
+
const sizes: i32[] = this.qSizes
|
|
4862
|
+
const count: i32 = this.count
|
|
4863
|
+
// `q` is a free slot of the ring, which all three are; the test states that for the prover.
|
|
4864
|
+
if (
|
|
4865
|
+
q < 0 ||
|
|
4866
|
+
q >= toI32(tags.length) ||
|
|
4867
|
+
q >= toI32(hashes.length) ||
|
|
4868
|
+
q >= toI32(sizes.length) ||
|
|
4869
|
+
count < 0 ||
|
|
4870
|
+
count >= toI32(tags.length)
|
|
4871
|
+
) {
|
|
4872
|
+
return false
|
|
4873
|
+
}
|
|
4874
|
+
tags[q] = tag
|
|
4875
|
+
hashes[q] = hash
|
|
4876
|
+
sizes[q] = size
|
|
4877
|
+
this.count = count + 1
|
|
4878
|
+
return true
|
|
4291
4879
|
}
|
|
4292
4880
|
|
|
4293
4881
|
/** Whether the keys of the queued requests `a` and `b` are equal. */
|
|
4882
|
+
// Both are slots of the ring holding waiting keys, which lie in the store; the tests
|
|
4883
|
+
// state those bounds for the prover (false outside them binds the key again, as a long probe does).
|
|
4294
4884
|
private sameKey(a: i32, b: i32): boolean {
|
|
4295
4885
|
if (this.keyType === PG_BATCH_INT) {
|
|
4296
|
-
|
|
4886
|
+
const ints: i64[] = this.qInts
|
|
4887
|
+
if (a < 0 || b < 0 || a >= toI32(ints.length) || b >= toI32(ints.length)) {
|
|
4888
|
+
return false
|
|
4889
|
+
}
|
|
4890
|
+
return ints[a] === ints[b]
|
|
4891
|
+
}
|
|
4892
|
+
const starts: i32[] = this.qStarts
|
|
4893
|
+
const lengths: i32[] = this.qLengths
|
|
4894
|
+
const store: u8[] = this.store
|
|
4895
|
+
if (a < 0 || b < 0 || a >= toI32(starts.length) || b >= toI32(starts.length)) {
|
|
4896
|
+
return false
|
|
4897
|
+
}
|
|
4898
|
+
if (a >= toI32(lengths.length) || b >= toI32(lengths.length)) {
|
|
4899
|
+
return false
|
|
4297
4900
|
}
|
|
4298
|
-
const n: i32 =
|
|
4299
|
-
if (n !==
|
|
4901
|
+
const n: i32 = lengths[a]
|
|
4902
|
+
if (n !== lengths[b]) {
|
|
4300
4903
|
return false
|
|
4301
4904
|
}
|
|
4302
|
-
|
|
4303
|
-
|
|
4905
|
+
let x: i32 = starts[a]
|
|
4906
|
+
let y: i32 = starts[b]
|
|
4304
4907
|
for (let i: i32 = 0; i < n; i++) {
|
|
4305
|
-
if (
|
|
4908
|
+
if (x < 0 || y < 0 || x >= toI32(store.length) || y >= toI32(store.length)) {
|
|
4306
4909
|
return false
|
|
4307
4910
|
}
|
|
4911
|
+
if (store[x] !== store[y]) {
|
|
4912
|
+
return false
|
|
4913
|
+
}
|
|
4914
|
+
x += 1
|
|
4915
|
+
y += 1
|
|
4308
4916
|
}
|
|
4309
4917
|
return true
|
|
4310
4918
|
}
|
|
@@ -4332,8 +4940,9 @@ export class PgBatcher {
|
|
|
4332
4940
|
}
|
|
4333
4941
|
const before: i32 = this.count
|
|
4334
4942
|
const r: i32 = this.take(conn)
|
|
4335
|
-
|
|
4336
|
-
|
|
4943
|
+
const after: i32 = this.count
|
|
4944
|
+
if (r >= 0 && before >= 0 && after >= 0) {
|
|
4945
|
+
clock.started(before - after, nowUs)
|
|
4337
4946
|
}
|
|
4338
4947
|
return r
|
|
4339
4948
|
}
|
|
@@ -4353,108 +4962,212 @@ export class PgBatcher {
|
|
|
4353
4962
|
* `next` for this execution's events; or -1.
|
|
4354
4963
|
*/
|
|
4355
4964
|
take(conn: PgConnection): i32 {
|
|
4356
|
-
|
|
4965
|
+
const count: i32 = this.count
|
|
4966
|
+
if (count <= 0) {
|
|
4357
4967
|
return -1
|
|
4358
4968
|
}
|
|
4969
|
+
const runs: PgBatchRun[] = this.runs
|
|
4359
4970
|
let r: i32 = -1
|
|
4360
|
-
for (let i: i32 = 0; i < toI32(
|
|
4361
|
-
if (!
|
|
4971
|
+
for (let i: i32 = 0; i < toI32(runs.length); i++) {
|
|
4972
|
+
if (!runs[i].open) {
|
|
4362
4973
|
r = i
|
|
4363
4974
|
break
|
|
4364
4975
|
}
|
|
4365
4976
|
}
|
|
4366
|
-
if (r < 0 ||
|
|
4977
|
+
if (r < 0 || r >= toI32(runs.length)) {
|
|
4978
|
+
return -1
|
|
4979
|
+
}
|
|
4980
|
+
const run: PgBatchRun = runs[r]
|
|
4981
|
+
if (!(this.binary ? conn.bindStartBinary(this.name) : conn.bindStart(this.name))) {
|
|
4367
4982
|
return -1
|
|
4368
4983
|
}
|
|
4369
4984
|
if (!conn.arrayStart()) {
|
|
4370
4985
|
conn.bindCancel()
|
|
4371
4986
|
return -1
|
|
4372
4987
|
}
|
|
4373
|
-
const
|
|
4374
|
-
|
|
4375
|
-
const
|
|
4988
|
+
const tags: i32[] = this.qTags
|
|
4989
|
+
const hashes: i32[] = this.qHashes
|
|
4990
|
+
const sizes: i32[] = this.qSizes
|
|
4991
|
+
const maxRequests: i32 = this.maxRequests
|
|
4992
|
+
const maxBytes: i32 = this.maxBytes
|
|
4993
|
+
this.grow(count < maxRequests ? count : maxRequests)
|
|
4994
|
+
const table: i32[] = this.table
|
|
4995
|
+
const keySlots: i32[] = this.keySlots
|
|
4996
|
+
const keyEntries: i32[] = this.keyEntries
|
|
4997
|
+
const keyCounts: i32[] = this.keyCounts
|
|
4998
|
+
const takenKeys: i32[] = this.takenKeys
|
|
4999
|
+
// A power of two, at least 2, by `grow`.
|
|
5000
|
+
const mask: i32 = toI32(table.length) - 1
|
|
4376
5001
|
let keys: i32 = 0
|
|
4377
5002
|
let taken: i32 = 0
|
|
4378
5003
|
let bytes: i32 = 2
|
|
4379
|
-
|
|
4380
|
-
|
|
5004
|
+
// `q` walks the ring from `head`, one slot per request taken; the tests on it, and on the
|
|
5005
|
+
// key-table entries below, state for the prover the bounds the queue and `grow` keep.
|
|
5006
|
+
let q: i32 = this.head
|
|
5007
|
+
while (taken < count && taken < maxRequests && taken < toI32(takenKeys.length)) {
|
|
5008
|
+
if (q < 0 || q >= toI32(tags.length) || q >= toI32(hashes.length) || q >= toI32(sizes.length)) {
|
|
5009
|
+
break
|
|
5010
|
+
}
|
|
4381
5011
|
// Probe for the key: a slot holding it, or the empty slot it would take;
|
|
4382
5012
|
// -1 after `PG_BATCH_PROBES` occupied ones, when the key is bound again,
|
|
4383
5013
|
// its own ordinal and rows, and left out of the table.
|
|
4384
|
-
|
|
5014
|
+
const hash: i32 = hashes[q]
|
|
5015
|
+
let slot: i32 = hash & mask
|
|
4385
5016
|
let k: i32 = -1
|
|
4386
5017
|
let probes: i32 = 0
|
|
4387
|
-
while (
|
|
4388
|
-
|
|
4389
|
-
|
|
4390
|
-
if (this.qHashes[e] === this.qHashes[q] && this.sameKey(e, q)) {
|
|
4391
|
-
k = seen
|
|
5018
|
+
while (probes < PG_BATCH_PROBES) {
|
|
5019
|
+
if (slot < 0 || slot >= toI32(table.length)) {
|
|
5020
|
+
probes = PG_BATCH_PROBES
|
|
4392
5021
|
break
|
|
4393
5022
|
}
|
|
4394
|
-
|
|
4395
|
-
if (
|
|
4396
|
-
|
|
5023
|
+
const held: i32 = table[slot]
|
|
5024
|
+
if (held <= 0) {
|
|
5025
|
+
break
|
|
5026
|
+
}
|
|
5027
|
+
const seen: i32 = held - 1
|
|
5028
|
+
if (seen >= toI32(keyEntries.length)) {
|
|
5029
|
+
probes = PG_BATCH_PROBES
|
|
4397
5030
|
break
|
|
4398
5031
|
}
|
|
5032
|
+
const e: i32 = keyEntries[seen]
|
|
5033
|
+
if (e >= 0 && e < toI32(hashes.length) && hashes[e] === hash && this.sameKey(e, q)) {
|
|
5034
|
+
k = seen
|
|
5035
|
+
break
|
|
5036
|
+
}
|
|
5037
|
+
probes += 1
|
|
4399
5038
|
slot = (slot + 1) & mask
|
|
4400
5039
|
}
|
|
5040
|
+
if (probes >= PG_BATCH_PROBES) {
|
|
5041
|
+
slot = -1
|
|
5042
|
+
}
|
|
4401
5043
|
if (k < 0) {
|
|
4402
|
-
|
|
4403
|
-
if (
|
|
5044
|
+
let comma: i32 = 0
|
|
5045
|
+
if (keys > 0) {
|
|
5046
|
+
comma = 1
|
|
5047
|
+
}
|
|
5048
|
+
const size: i32 = sizes[q]
|
|
5049
|
+
const added: i32 = keys
|
|
5050
|
+
// A key is one of the taken, fewer than `grow`'s arrays hold.
|
|
5051
|
+
if (
|
|
5052
|
+
toI64(size) + toI64(comma) > toI64(maxBytes) - toI64(bytes) ||
|
|
5053
|
+
added < 0 ||
|
|
5054
|
+
added >= toI32(keySlots.length) ||
|
|
5055
|
+
added >= toI32(keyEntries.length) ||
|
|
5056
|
+
added >= toI32(keyCounts.length)
|
|
5057
|
+
) {
|
|
5058
|
+
break
|
|
5059
|
+
}
|
|
5060
|
+
// Past `keys` until the bind succeeds, so unread if it does not.
|
|
5061
|
+
keySlots[added] = slot
|
|
5062
|
+
keyEntries[added] = q
|
|
5063
|
+
keyCounts[added] = 0
|
|
5064
|
+
if (!this.bindKey(conn, q)) {
|
|
4404
5065
|
break
|
|
4405
5066
|
}
|
|
4406
|
-
|
|
4407
|
-
|
|
4408
|
-
|
|
4409
|
-
|
|
4410
|
-
|
|
5067
|
+
// At most `maxBytes`, by the test above.
|
|
5068
|
+
bytes = toI32(toI64(bytes) + toI64(size) + toI64(comma))
|
|
5069
|
+
k = added
|
|
5070
|
+
keys = added + 1
|
|
5071
|
+
if (slot >= 0 && slot < toI32(table.length)) {
|
|
5072
|
+
table[slot] = keys
|
|
4411
5073
|
}
|
|
4412
|
-
this.keySlots[k] = slot
|
|
4413
|
-
this.keyEntries[k] = q
|
|
4414
|
-
this.keyCounts[k] = 0
|
|
4415
5074
|
}
|
|
4416
|
-
|
|
4417
|
-
|
|
5075
|
+
// A key's count is of the requests taken so far.
|
|
5076
|
+
if (k < 0 || k >= toI32(keyCounts.length)) {
|
|
5077
|
+
break
|
|
5078
|
+
}
|
|
5079
|
+
const kc: i32 = keyCounts[k]
|
|
5080
|
+
if (kc < 0 || kc >= count || taken >= toI32(takenKeys.length)) {
|
|
5081
|
+
break
|
|
5082
|
+
}
|
|
5083
|
+
keyCounts[k] = kc + 1
|
|
5084
|
+
takenKeys[taken] = k
|
|
4418
5085
|
taken += 1
|
|
5086
|
+
q += 1
|
|
5087
|
+
if (q === toI32(tags.length)) {
|
|
5088
|
+
q = 0
|
|
5089
|
+
}
|
|
4419
5090
|
}
|
|
4420
|
-
for (let k: i32 = 0; k < keys && k < toI32(
|
|
4421
|
-
const slot: i32 =
|
|
4422
|
-
if (slot >= 0) {
|
|
4423
|
-
|
|
5091
|
+
for (let k: i32 = 0; k < keys && k < toI32(keySlots.length); k++) {
|
|
5092
|
+
const slot: i32 = keySlots[k]
|
|
5093
|
+
if (slot >= 0 && slot < toI32(table.length)) {
|
|
5094
|
+
table[slot] = 0
|
|
4424
5095
|
}
|
|
4425
5096
|
}
|
|
4426
5097
|
if (taken === 0) {
|
|
4427
5098
|
conn.bindCancel()
|
|
4428
5099
|
return -1
|
|
4429
5100
|
}
|
|
4430
|
-
conn.arrayEnd()
|
|
4431
|
-
conn.bindEnd()
|
|
4432
5101
|
// The requests grouped by key, each key's in the order they were added.
|
|
4433
|
-
const run: PgBatchRun = this.runs[r]
|
|
4434
5102
|
if (toI32(run.tags.length) < taken) {
|
|
4435
|
-
// Grown together: `first` holds one more than `tags`, which bounds the keys too
|
|
4436
|
-
|
|
4437
|
-
run.
|
|
5103
|
+
// Grown together: `first` holds one more than `tags`, which bounds the keys too;
|
|
5104
|
+
// `takenKeys` holds at most `PG_BATCH_MAX_REQUESTS` (65536).
|
|
5105
|
+
run.tags = new Array<i32>(toI32(takenKeys.length))
|
|
5106
|
+
run.first = new Array<i32>(toI32(toI64(takenKeys.length) + toI64(1)))
|
|
4438
5107
|
if (!this.sorted) {
|
|
4439
|
-
run.had = new Array<boolean>(toI32(
|
|
5108
|
+
run.had = new Array<boolean>(toI32(takenKeys.length))
|
|
4440
5109
|
}
|
|
4441
5110
|
}
|
|
4442
|
-
|
|
4443
|
-
|
|
4444
|
-
|
|
4445
|
-
|
|
5111
|
+
const first: i32[] = run.first
|
|
5112
|
+
const runTags: i32[] = run.tags
|
|
5113
|
+
const uses: i32 = run.uses
|
|
5114
|
+
const n: i32 = toI32(runs.length)
|
|
5115
|
+
// `first` outgrows the keys and `tags` the requests, just above; `uses` counts below
|
|
5116
|
+
// `PG_BATCH_USES` (2^20) and there are at most `PG_BATCH_MAX_RUNS` (1024) runs.
|
|
5117
|
+
// The test states those bounds for the prover.
|
|
5118
|
+
if (
|
|
5119
|
+
keys >= toI32(first.length) ||
|
|
5120
|
+
toI32(first.length) < 1 ||
|
|
5121
|
+
taken > toI32(runTags.length) ||
|
|
5122
|
+
uses < 0 ||
|
|
5123
|
+
uses >= 1048576 ||
|
|
5124
|
+
n > 1024
|
|
5125
|
+
) {
|
|
5126
|
+
conn.bindCancel()
|
|
5127
|
+
return -1
|
|
4446
5128
|
}
|
|
4447
|
-
|
|
4448
|
-
|
|
4449
|
-
|
|
4450
|
-
|
|
5129
|
+
first[0] = 0
|
|
5130
|
+
let f: i32 = 0
|
|
5131
|
+
for (let k: i32 = 0; k < keys && k < toI32(keyCounts.length); k++) {
|
|
5132
|
+
if (k >= toI32(first.length) - 1) {
|
|
5133
|
+
break
|
|
5134
|
+
}
|
|
5135
|
+
const c: i32 = keyCounts[k]
|
|
5136
|
+
keyCounts[k] = f
|
|
5137
|
+
// The counts sum to `taken`.
|
|
5138
|
+
f = toI32(toI64(f) + toI64(c))
|
|
5139
|
+
first[k + 1] = f
|
|
5140
|
+
}
|
|
5141
|
+
let p: i32 = this.head
|
|
5142
|
+
for (let i: i32 = 0; i < taken && i < toI32(takenKeys.length); i++) {
|
|
5143
|
+
// `p` walks the ring as `q` did, and each key's next place lies inside its group.
|
|
5144
|
+
if (p < 0 || p >= toI32(tags.length)) {
|
|
5145
|
+
break
|
|
5146
|
+
}
|
|
5147
|
+
const k: i32 = takenKeys[i]
|
|
5148
|
+
if (k < 0 || k >= toI32(keyCounts.length)) {
|
|
5149
|
+
break
|
|
5150
|
+
}
|
|
5151
|
+
const at: i32 = keyCounts[k]
|
|
5152
|
+
if (at < 0 || at >= toI32(runTags.length)) {
|
|
5153
|
+
break
|
|
5154
|
+
}
|
|
5155
|
+
runTags[at] = tags[p]
|
|
5156
|
+
keyCounts[k] = at + 1
|
|
5157
|
+
p += 1
|
|
5158
|
+
if (p === toI32(tags.length)) {
|
|
5159
|
+
p = 0
|
|
5160
|
+
}
|
|
4451
5161
|
}
|
|
5162
|
+
conn.arrayEnd()
|
|
5163
|
+
conn.bindEnd()
|
|
4452
5164
|
run.reset(keys)
|
|
4453
|
-
|
|
4454
|
-
run.
|
|
4455
|
-
|
|
4456
|
-
this.
|
|
4457
|
-
|
|
5165
|
+
// Below 2^20 * 1024 + 1024, by the test above.
|
|
5166
|
+
run.handle = toI32(toI64(uses) * toI64(n) + toI64(r))
|
|
5167
|
+
run.uses = uses + 1 === PG_BATCH_USES ? 0 : uses + 1
|
|
5168
|
+
this.head = q
|
|
5169
|
+
this.count = count - taken
|
|
5170
|
+
if (count === taken) {
|
|
4458
5171
|
this.storeEnd = 0
|
|
4459
5172
|
}
|
|
4460
5173
|
return run.handle
|
|
@@ -4470,15 +5183,17 @@ export class PgBatcher {
|
|
|
4470
5183
|
if (toI32(this.takenKeys.length) >= need) {
|
|
4471
5184
|
return
|
|
4472
5185
|
}
|
|
5186
|
+
// `need` is at most `maxRequests`, itself at most `PG_BATCH_MAX_REQUESTS` (65536).
|
|
4473
5187
|
let size: i32 = PG_BATCH_MIN_GROWTH
|
|
4474
|
-
while (size < need) {
|
|
5188
|
+
while (size > 0 && size < need && size < 65536) {
|
|
4475
5189
|
size *= 2
|
|
4476
5190
|
}
|
|
4477
5191
|
if (size > this.maxRequests) {
|
|
4478
5192
|
size = this.maxRequests
|
|
4479
5193
|
}
|
|
5194
|
+
// Twice `size`, rounded up to a power of two: at most 2^17.
|
|
4480
5195
|
let tableSize: i32 = 2
|
|
4481
|
-
while (tableSize
|
|
5196
|
+
while (tableSize > 0 && tableSize / 2 < size && tableSize < 131072) {
|
|
4482
5197
|
tableSize *= 2
|
|
4483
5198
|
}
|
|
4484
5199
|
this.table = new Array<i32>(tableSize)
|
|
@@ -4489,20 +5204,37 @@ export class PgBatcher {
|
|
|
4489
5204
|
}
|
|
4490
5205
|
|
|
4491
5206
|
/** Writes the queued request `q`'s key as the open array's next element; false when `conn` has no room. */
|
|
5207
|
+
// `q` is a slot of the ring holding a waiting key; the tests state that for the prover.
|
|
4492
5208
|
private bindKey(conn: PgConnection, q: i32): boolean {
|
|
4493
5209
|
if (this.keyType === PG_BATCH_INT) {
|
|
4494
|
-
|
|
5210
|
+
const ints: i64[] = this.qInts
|
|
5211
|
+
if (q < 0 || q >= toI32(ints.length)) {
|
|
5212
|
+
return false
|
|
5213
|
+
}
|
|
5214
|
+
return conn.arrayInt(ints[q])
|
|
5215
|
+
}
|
|
5216
|
+
const starts: i32[] = this.qStarts
|
|
5217
|
+
const lengths: i32[] = this.qLengths
|
|
5218
|
+
if (q < 0 || q >= toI32(starts.length) || q >= toI32(lengths.length)) {
|
|
5219
|
+
return false
|
|
4495
5220
|
}
|
|
4496
|
-
return conn.arrayElement(this.store,
|
|
5221
|
+
return conn.arrayElement(this.store, starts[q], lengths[q])
|
|
4497
5222
|
}
|
|
4498
5223
|
|
|
4499
5224
|
/** The run handle `h` names while its execution's events are owed; null once it is spent. Panics on a handle `take` cannot give. */
|
|
4500
5225
|
private run(h: i32): PgBatchRun | null {
|
|
4501
|
-
const
|
|
4502
|
-
|
|
5226
|
+
const runs: PgBatchRun[] = this.runs
|
|
5227
|
+
const n: i32 = toI32(runs.length)
|
|
5228
|
+
// `n * PG_BATCH_USES` (2^20) in an `i64`.
|
|
5229
|
+
if (n <= 0 || h < 0 || toI64(h) >= toI64(n) * toI64(1048576)) {
|
|
4503
5230
|
panic("nish/net/postgres: a PgBatcher run out of range")
|
|
4504
5231
|
}
|
|
4505
|
-
const
|
|
5232
|
+
const at: i32 = h % n
|
|
5233
|
+
// In [0, n), as `h` and `n` are positive; the test states that for the prover.
|
|
5234
|
+
if (at < 0 || at >= toI32(runs.length)) {
|
|
5235
|
+
return null
|
|
5236
|
+
}
|
|
5237
|
+
const run: PgBatchRun = runs[at]
|
|
4506
5238
|
if (run.handle !== h || !run.open) {
|
|
4507
5239
|
return null
|
|
4508
5240
|
}
|
|
@@ -4513,8 +5245,9 @@ export class PgBatcher {
|
|
|
4513
5245
|
private quietRun(h: i32, what: string): PgBatchRun | null {
|
|
4514
5246
|
const run: PgBatchRun | null = this.run(h)
|
|
4515
5247
|
if (run === null) {
|
|
4516
|
-
|
|
4517
|
-
|
|
5248
|
+
const stray: i32 = this.stray
|
|
5249
|
+
if (stray < 2147483647) {
|
|
5250
|
+
this.stray = stray + 1
|
|
4518
5251
|
}
|
|
4519
5252
|
return null
|
|
4520
5253
|
}
|
|
@@ -4547,7 +5280,11 @@ export class PgBatcher {
|
|
|
4547
5280
|
if (run.failed) {
|
|
4548
5281
|
return false
|
|
4549
5282
|
}
|
|
4550
|
-
const
|
|
5283
|
+
const columns: i32 = conn.columns
|
|
5284
|
+
let last: i32 = -1
|
|
5285
|
+
if (columns > 0) {
|
|
5286
|
+
last = columns - 1
|
|
5287
|
+
}
|
|
4551
5288
|
let ordinal: i64 = 0
|
|
4552
5289
|
let bad: boolean = last < 0 || conn.isNull(last)
|
|
4553
5290
|
if (!bad && this.binary) {
|
|
@@ -4566,17 +5303,25 @@ export class PgBatcher {
|
|
|
4566
5303
|
conn.text.failed = failed
|
|
4567
5304
|
}
|
|
4568
5305
|
// Ordinals count from 1 and, sorted, come in order: a row may name the current key again, never one before it.
|
|
4569
|
-
|
|
4570
|
-
if (
|
|
4571
|
-
|
|
4572
|
-
|
|
5306
|
+
let least: i64 = 1
|
|
5307
|
+
if (this.sorted) {
|
|
5308
|
+
const cur: i32 = run.cur
|
|
5309
|
+
least = toI64(cur) + toI64(1)
|
|
5310
|
+
}
|
|
5311
|
+
if (bad || ordinal < least || ordinal < 1 || ordinal > toI64(run.keys)) {
|
|
5312
|
+
const refused: i32 = this.refused
|
|
5313
|
+
if (refused < 2147483647) {
|
|
5314
|
+
this.refused = refused + 1
|
|
4573
5315
|
}
|
|
4574
5316
|
run.failed = true
|
|
4575
5317
|
return false
|
|
4576
5318
|
}
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
|
|
5319
|
+
// From 0 to `keys` - 1, by the test above.
|
|
5320
|
+
const target: i32 = toI32(ordinal - 1)
|
|
5321
|
+
run.target = target
|
|
5322
|
+
const had: boolean[] = run.had
|
|
5323
|
+
if (!this.sorted && target >= 0 && target < toI32(had.length)) {
|
|
5324
|
+
had[target] = true
|
|
4580
5325
|
}
|
|
4581
5326
|
return true
|
|
4582
5327
|
}
|
|
@@ -4608,14 +5353,28 @@ export class PgBatcher {
|
|
|
4608
5353
|
}
|
|
4609
5354
|
}
|
|
4610
5355
|
|
|
4611
|
-
// Names
|
|
4612
|
-
|
|
4613
|
-
|
|
4614
|
-
|
|
5356
|
+
// Names key `key`'s next unanswered request as `tag`; false when it has none left.
|
|
5357
|
+
// `key` is one of the run's, `first` holds one more than its keys, and `at` counts the key's
|
|
5358
|
+
// answered requests, inside its group of `tags`: the tests state those bounds for the prover.
|
|
5359
|
+
private emit(run: PgBatchRun, key: i32): boolean {
|
|
5360
|
+
const first: i32[] = run.first
|
|
5361
|
+
const tags: i32[] = run.tags
|
|
5362
|
+
const a: i32 = run.at
|
|
5363
|
+
if (key < 0 || key >= toI32(first.length) - 1 || a < 0) {
|
|
5364
|
+
return false
|
|
5365
|
+
}
|
|
5366
|
+
const from: i32 = first[key]
|
|
5367
|
+
const to: i32 = first[key + 1]
|
|
5368
|
+
if (from < 0 || to < 0 || to < from || a >= to - from) {
|
|
4615
5369
|
return false
|
|
4616
5370
|
}
|
|
4617
|
-
|
|
4618
|
-
|
|
5371
|
+
// Below `to`, by the test above.
|
|
5372
|
+
const at: i32 = toI32(toI64(from) + toI64(a))
|
|
5373
|
+
if (at < 0 || at >= toI32(tags.length)) {
|
|
5374
|
+
return false
|
|
5375
|
+
}
|
|
5376
|
+
run.at = a + 1
|
|
5377
|
+
this.tag = tags[at]
|
|
4619
5378
|
return true
|
|
4620
5379
|
}
|
|
4621
5380
|
|
|
@@ -4646,20 +5405,22 @@ export class PgBatcher {
|
|
|
4646
5405
|
} else if (run.ended) {
|
|
4647
5406
|
limit = run.keys
|
|
4648
5407
|
}
|
|
4649
|
-
|
|
4650
|
-
|
|
5408
|
+
let cur: i32 = run.cur
|
|
5409
|
+
while (cur < limit && cur < toI32(run.tags.length)) {
|
|
5410
|
+
if (this.emit(run, cur)) {
|
|
4651
5411
|
if (run.failed) {
|
|
4652
5412
|
return PG_BATCH_FAILED
|
|
4653
5413
|
}
|
|
4654
5414
|
return run.curRows ? PG_BATCH_DONE : PG_BATCH_MISSING
|
|
4655
5415
|
}
|
|
4656
|
-
|
|
5416
|
+
cur += 1
|
|
5417
|
+
run.cur = cur
|
|
4657
5418
|
run.at = 0
|
|
4658
5419
|
run.curRows = false
|
|
4659
5420
|
}
|
|
4660
|
-
if (run.target >= 0 &&
|
|
5421
|
+
if (run.target >= 0 && cur < toI32(run.tags.length)) {
|
|
4661
5422
|
run.curRows = true
|
|
4662
|
-
if (this.emit(run)) {
|
|
5423
|
+
if (this.emit(run, cur)) {
|
|
4663
5424
|
return PG_BATCH_ROW
|
|
4664
5425
|
}
|
|
4665
5426
|
run.target = -1
|
|
@@ -4674,11 +5435,8 @@ export class PgBatcher {
|
|
|
4674
5435
|
// `next` with `sorted` false: the row's `PG_BATCH_ROW` events at once, and
|
|
4675
5436
|
// every last event only once the execution is over, `cur` walking the keys.
|
|
4676
5437
|
private nextUnsorted(run: PgBatchRun): i32 {
|
|
4677
|
-
if (run.target >= 0
|
|
4678
|
-
|
|
4679
|
-
if (at < run.first[run.target + 1]) {
|
|
4680
|
-
run.at += 1
|
|
4681
|
-
this.tag = run.tags[at]
|
|
5438
|
+
if (run.target >= 0) {
|
|
5439
|
+
if (this.emit(run, run.target)) {
|
|
4682
5440
|
return PG_BATCH_ROW
|
|
4683
5441
|
}
|
|
4684
5442
|
run.target = -1
|
|
@@ -4687,14 +5445,17 @@ export class PgBatcher {
|
|
|
4687
5445
|
if (!run.ended) {
|
|
4688
5446
|
return PG_BATCH_NONE
|
|
4689
5447
|
}
|
|
4690
|
-
|
|
4691
|
-
|
|
5448
|
+
const had: boolean[] = run.had
|
|
5449
|
+
let cur: i32 = run.cur
|
|
5450
|
+
while (cur >= 0 && cur < run.keys && cur < toI32(had.length)) {
|
|
5451
|
+
if (this.emit(run, cur)) {
|
|
4692
5452
|
if (run.failed) {
|
|
4693
5453
|
return PG_BATCH_FAILED
|
|
4694
5454
|
}
|
|
4695
|
-
return
|
|
5455
|
+
return had[cur] ? PG_BATCH_DONE : PG_BATCH_MISSING
|
|
4696
5456
|
}
|
|
4697
|
-
|
|
5457
|
+
cur += 1
|
|
5458
|
+
run.cur = cur
|
|
4698
5459
|
run.at = 0
|
|
4699
5460
|
}
|
|
4700
5461
|
run.open = false
|
|
@@ -4848,26 +5609,46 @@ export class PgBatchClock {
|
|
|
4848
5609
|
/**
|
|
4849
5610
|
* Records an item queued at `nowUs`; false when `queueSize` are waiting. A
|
|
4850
5611
|
* time before the last one given counts as the last one, so a clock that
|
|
4851
|
-
* steps back cannot make an item older than its neighbours.
|
|
5612
|
+
* steps back cannot make an item older than its neighbours. The clock's
|
|
5613
|
+
* times run from 0 to 2^52 microseconds (about 142 years); a time
|
|
5614
|
+
* outside that, here or in any other call, counts as the nearest end.
|
|
4852
5615
|
*
|
|
4853
5616
|
* @param nowUs The time the item was queued, in microseconds from the program's fixed point.
|
|
4854
5617
|
* @returns True when the item was recorded.
|
|
4855
5618
|
*/
|
|
4856
5619
|
arrived(nowUs: i64): boolean {
|
|
4857
|
-
const
|
|
4858
|
-
|
|
5620
|
+
const times: i64[] = this.times
|
|
5621
|
+
const ring: i32 = toI32(times.length)
|
|
5622
|
+
const head: i32 = this.head
|
|
5623
|
+
const count: i32 = this.count
|
|
5624
|
+
// Full at `count === ring`; below it, `head` is a slot of the ring, which the test states for the prover.
|
|
5625
|
+
if (count < 0 || count >= ring || head < 0 || head >= ring) {
|
|
4859
5626
|
return false
|
|
4860
5627
|
}
|
|
4861
|
-
const
|
|
5628
|
+
const last: i64 = this.last
|
|
5629
|
+
const now: i64 = pgClockTime(nowUs < last ? last : nowUs)
|
|
4862
5630
|
this.last = now
|
|
4863
|
-
|
|
4864
|
-
|
|
4865
|
-
const
|
|
4866
|
-
|
|
4867
|
-
|
|
4868
|
-
|
|
4869
|
-
|
|
4870
|
-
|
|
5631
|
+
// Below twice the ring's size, at most 2^21.
|
|
5632
|
+
const sum: i64 = toI64(head) + toI64(count)
|
|
5633
|
+
const at: i32 = toI32(sum < toI64(ring) ? sum : sum - toI64(ring))
|
|
5634
|
+
// `at` is a slot of the ring; the test states that bound for the prover.
|
|
5635
|
+
if (at < 0 || at >= toI32(times.length)) {
|
|
5636
|
+
return false
|
|
5637
|
+
}
|
|
5638
|
+
times[at] = now
|
|
5639
|
+
this.count = count + 1
|
|
5640
|
+
const light: i64[] = this.light
|
|
5641
|
+
const lights: i32 = toI32(light.length)
|
|
5642
|
+
const lightAt: i32 = this.lightAt
|
|
5643
|
+
// `lightAt` is a slot of the light ring when it has any; the test states that bound for the
|
|
5644
|
+
// prover, and skips an empty ring.
|
|
5645
|
+
if (lightAt >= 0 && lightAt < toI32(light.length)) {
|
|
5646
|
+
light[lightAt] = now
|
|
5647
|
+
const after: i32 = lightAt + 1
|
|
5648
|
+
this.lightAt = after === lights ? 0 : after
|
|
5649
|
+
const seen: i32 = this.lightSeen
|
|
5650
|
+
if (seen < lights) {
|
|
5651
|
+
this.lightSeen = seen + 1
|
|
4871
5652
|
}
|
|
4872
5653
|
}
|
|
4873
5654
|
return true
|
|
@@ -4904,7 +5685,7 @@ export class PgBatchClock {
|
|
|
4904
5685
|
const target: i32 = this.target()
|
|
4905
5686
|
if (this.count >= target) {
|
|
4906
5687
|
reason = target > this.minCount ? PG_CLOCK_AVERAGE : PG_CLOCK_COUNT
|
|
4907
|
-
} else if (
|
|
5688
|
+
} else if (this.waited(nowUs) >= this.maxWaitUs) {
|
|
4908
5689
|
reason = PG_CLOCK_TIMER
|
|
4909
5690
|
} else if (inFlight <= 0 && this.quiet(nowUs)) {
|
|
4910
5691
|
reason = PG_CLOCK_IDLE
|
|
@@ -4920,10 +5701,14 @@ export class PgBatchClock {
|
|
|
4920
5701
|
* @returns That time in the caller's microseconds, or -1 when none waits.
|
|
4921
5702
|
*/
|
|
4922
5703
|
deadline(): i64 {
|
|
4923
|
-
|
|
5704
|
+
const oldest: i64 = this.oldest()
|
|
5705
|
+
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.
|
|
5708
|
+
if (oldest < 0 || oldest > 4503599627370496 || wait < 0 || wait > 60000000) {
|
|
4924
5709
|
return -1
|
|
4925
5710
|
}
|
|
4926
|
-
return
|
|
5711
|
+
return toI64(toU64(oldest) + toU64(wait))
|
|
4927
5712
|
}
|
|
4928
5713
|
|
|
4929
5714
|
/**
|
|
@@ -4936,20 +5721,35 @@ export class PgBatchClock {
|
|
|
4936
5721
|
* measured to it, and is 0 when it is before the oldest item's arrival.
|
|
4937
5722
|
*/
|
|
4938
5723
|
started(size: i32, nowUs: i64): void {
|
|
4939
|
-
|
|
5724
|
+
const count: i32 = this.count
|
|
5725
|
+
if (size < 1 || size > count || size > this.maxCount) {
|
|
4940
5726
|
panic("nish/net/postgres: PgBatchClock.started with a size out of range")
|
|
4941
5727
|
}
|
|
4942
|
-
|
|
4943
|
-
|
|
4944
|
-
|
|
5728
|
+
// Measured to the oldest item before the batch takes it.
|
|
5729
|
+
const waited: i64 = this.waited(nowUs)
|
|
5730
|
+
const ring: i32 = toI32(this.times.length)
|
|
5731
|
+
const head: i32 = this.head
|
|
5732
|
+
const starts: i64[] = this.starts
|
|
5733
|
+
const due: i32 = this.due
|
|
5734
|
+
// `head` is a slot of the ring, which holds `count` items, and `due` one of the `PG_CLOCK_*`;
|
|
5735
|
+
// the test states those bounds for the prover.
|
|
5736
|
+
if (head < 0 || head >= ring || count < 0 || count > ring || due < 0 || due >= toI32(starts.length)) {
|
|
5737
|
+
return
|
|
4945
5738
|
}
|
|
4946
|
-
|
|
4947
|
-
|
|
5739
|
+
// Below twice the ring's size, at most 2^21: `size` is at most `count`.
|
|
5740
|
+
const sum: i64 = toI64(head) + toI64(size)
|
|
5741
|
+
this.head = toI32(sum < toI64(ring) ? sum : sum - toI64(ring))
|
|
5742
|
+
this.count = count - size
|
|
5743
|
+
this.lastWaitUs = waited < 0 ? 0 : waited
|
|
4948
5744
|
const n: f64 = toF64(size)
|
|
4949
5745
|
this.average = this.lastSize === 0 ? n : this.average + this.weight * (n - this.average)
|
|
4950
5746
|
this.lastSize = size
|
|
4951
|
-
this.lastReason =
|
|
4952
|
-
|
|
5747
|
+
this.lastReason = due
|
|
5748
|
+
const started: i64 = starts[due]
|
|
5749
|
+
// 2^52 batches cannot start in the clock's 2^52 microseconds, so the unsigned add cannot wrap.
|
|
5750
|
+
if (started >= 0 && started < 4503599627370496) {
|
|
5751
|
+
starts[due] = toI64(toU64(started) + toU64(1))
|
|
5752
|
+
}
|
|
4953
5753
|
this.due = PG_CLOCK_WAIT
|
|
4954
5754
|
}
|
|
4955
5755
|
|
|
@@ -4961,10 +5761,12 @@ export class PgBatchClock {
|
|
|
4961
5761
|
* @returns The count since the clock was made.
|
|
4962
5762
|
*/
|
|
4963
5763
|
startsFor(reason: i32): i64 {
|
|
4964
|
-
|
|
5764
|
+
const starts: i64[] = this.starts
|
|
5765
|
+
// `starts` holds one count for each reason, `PG_CLOCK_WAIT` (0) to `PG_CLOCK_IDLE`.
|
|
5766
|
+
if (reason < 0 || reason >= toI32(starts.length)) {
|
|
4965
5767
|
panic("nish/net/postgres: PgBatchClock.startsFor with a reason out of range")
|
|
4966
5768
|
}
|
|
4967
|
-
return
|
|
5769
|
+
return starts[reason]
|
|
4968
5770
|
}
|
|
4969
5771
|
|
|
4970
5772
|
/** Whether fewer than `lightCount` items arrived in the `lightWindowUs` before `nowUs`. */
|
|
@@ -4977,8 +5779,61 @@ export class PgBatchClock {
|
|
|
4977
5779
|
return true
|
|
4978
5780
|
}
|
|
4979
5781
|
// `lightAt` is the next to write, so it holds the oldest of the last `lightCount`.
|
|
4980
|
-
|
|
5782
|
+
const light: i64[] = this.light
|
|
5783
|
+
const lightAt: i32 = this.lightAt
|
|
5784
|
+
// `lightAt` is a slot of the light ring, and its times clock times; the tests state those
|
|
5785
|
+
// bounds for the prover.
|
|
5786
|
+
if (lightAt < 0 || lightAt >= toI32(light.length)) {
|
|
5787
|
+
return false
|
|
5788
|
+
}
|
|
5789
|
+
const then: i64 = light[lightAt]
|
|
5790
|
+
const now: i64 = pgClockTime(nowUs)
|
|
5791
|
+
if (then < 0 || then > 4503599627370496 || now < 0 || now > 4503599627370496) {
|
|
5792
|
+
return false
|
|
5793
|
+
}
|
|
5794
|
+
return now - then >= this.lightWindowUs
|
|
5795
|
+
}
|
|
5796
|
+
|
|
5797
|
+
/** The arrival time of the oldest waiting item; -1 when none waits. */
|
|
5798
|
+
private oldest(): i64 {
|
|
5799
|
+
const times: i64[] = this.times
|
|
5800
|
+
const head: i32 = this.head
|
|
5801
|
+
// `head` is a slot of the ring; the test states that bound for the prover.
|
|
5802
|
+
if (this.count <= 0 || head < 0 || head >= toI32(times.length)) {
|
|
5803
|
+
return -1
|
|
5804
|
+
}
|
|
5805
|
+
return times[head]
|
|
5806
|
+
}
|
|
5807
|
+
|
|
5808
|
+
/** How long the oldest waiting item has waited at `nowUs`, below 0 when it arrived later; -1 when none waits. */
|
|
5809
|
+
private waited(nowUs: i64): i64 {
|
|
5810
|
+
const oldest: i64 = this.oldest()
|
|
5811
|
+
const now: i64 = pgClockTime(nowUs)
|
|
5812
|
+
// Both are clock times, the oldest one recorded by `arrived`; the tests state those bounds
|
|
5813
|
+
// for the prover.
|
|
5814
|
+
if (oldest < 0 || oldest > 4503599627370496 || now < 0 || now > 4503599627370496) {
|
|
5815
|
+
return -1
|
|
5816
|
+
}
|
|
5817
|
+
return now - oldest
|
|
5818
|
+
}
|
|
5819
|
+
}
|
|
5820
|
+
|
|
5821
|
+
/**
|
|
5822
|
+
* `count + 1` for an array's element count. An element takes at least two
|
|
5823
|
+
* bytes of a buffer held below 2^30, so the count never nears 2^31 - 1; the
|
|
5824
|
+
* test states that bound for the prover.
|
|
5825
|
+
*/
|
|
5826
|
+
const pgCountUp = (count: i32): i32 => (count >= 0 && count < 2147483647 ? count + 1 : count)
|
|
5827
|
+
|
|
5828
|
+
/** `t` as a `PgBatchClock` time: from 0 to 2^52 microseconds, a time outside counting as the nearest end. */
|
|
5829
|
+
const pgClockTime = (t: i64): i64 => {
|
|
5830
|
+
if (t < 0) {
|
|
5831
|
+
return 0
|
|
5832
|
+
}
|
|
5833
|
+
if (t > 4503599627370496) {
|
|
5834
|
+
return 4503599627370496
|
|
4981
5835
|
}
|
|
5836
|
+
return t
|
|
4982
5837
|
}
|
|
4983
5838
|
|
|
4984
5839
|
/** `PgBatchTurns`: no slot to wake. */
|
|
@@ -5007,8 +5862,7 @@ export const PG_TURNS_FULL: i32 = -2
|
|
|
5007
5862
|
export class PgBatchTurns {
|
|
5008
5863
|
/** The clock the turns read and record into. */
|
|
5009
5864
|
readonly clock: PgBatchClock
|
|
5010
|
-
/**
|
|
5011
|
-
private readonly size: i32
|
|
5865
|
+
/** One per slot: whether it holds a batch. */
|
|
5012
5866
|
private readonly busy: boolean[]
|
|
5013
5867
|
private running: i32 = 0
|
|
5014
5868
|
private woken: i32 = -1
|
|
@@ -5025,7 +5879,6 @@ export class PgBatchTurns {
|
|
|
5025
5879
|
panic("nish/net/postgres: a PgBatchTurns size out of range")
|
|
5026
5880
|
}
|
|
5027
5881
|
this.clock = clock
|
|
5028
|
-
this.size = size
|
|
5029
5882
|
this.busy = new Array<boolean>(size)
|
|
5030
5883
|
}
|
|
5031
5884
|
|
|
@@ -5035,8 +5888,9 @@ export class PgBatchTurns {
|
|
|
5035
5888
|
* @returns The lowest-numbered free slot, or `PG_TURNS_NONE` when every slot holds a batch.
|
|
5036
5889
|
*/
|
|
5037
5890
|
collector(): i32 {
|
|
5038
|
-
|
|
5039
|
-
|
|
5891
|
+
const busy: boolean[] = this.busy
|
|
5892
|
+
for (let i: i32 = 0; i < toI32(busy.length); i++) {
|
|
5893
|
+
if (!busy[i]) {
|
|
5040
5894
|
return i
|
|
5041
5895
|
}
|
|
5042
5896
|
}
|
|
@@ -5107,8 +5961,15 @@ export class PgBatchTurns {
|
|
|
5107
5961
|
started(slot: i32, size: i32, nowUs: i64): i32 {
|
|
5108
5962
|
this.free(slot)
|
|
5109
5963
|
this.clock.started(size, nowUs)
|
|
5110
|
-
|
|
5111
|
-
this.running
|
|
5964
|
+
const busy: boolean[] = this.busy
|
|
5965
|
+
const running: i32 = this.running
|
|
5966
|
+
// `free` refused a slot out of range, and `running` counts the busy slots, so it is below
|
|
5967
|
+
// their number while this one is free; the test states those bounds for the prover.
|
|
5968
|
+
if (slot < 0 || slot >= toI32(busy.length) || running < 0 || running >= toI32(busy.length)) {
|
|
5969
|
+
return PG_TURNS_NONE
|
|
5970
|
+
}
|
|
5971
|
+
busy[slot] = true
|
|
5972
|
+
this.running = running + 1
|
|
5112
5973
|
if (this.woken === slot) {
|
|
5113
5974
|
this.woken = -1
|
|
5114
5975
|
}
|
|
@@ -5124,11 +5985,16 @@ export class PgBatchTurns {
|
|
|
5124
5985
|
* @returns The slot to wake, or `PG_TURNS_NONE`.
|
|
5125
5986
|
*/
|
|
5126
5987
|
finished(slot: i32, nowUs: i64): i32 {
|
|
5127
|
-
|
|
5988
|
+
const busy: boolean[] = this.busy
|
|
5989
|
+
if (slot < 0 || slot >= toI32(busy.length) || !busy[slot]) {
|
|
5128
5990
|
panic("nish/net/postgres: PgBatchTurns.finished on a slot holding no batch")
|
|
5129
5991
|
}
|
|
5130
|
-
|
|
5131
|
-
this.running
|
|
5992
|
+
busy[slot] = false
|
|
5993
|
+
const running: i32 = this.running
|
|
5994
|
+
// `running` counts the busy slots, this one among them; the test states that for the prover.
|
|
5995
|
+
if (running > 0) {
|
|
5996
|
+
this.running = running - 1
|
|
5997
|
+
}
|
|
5132
5998
|
return this.poll(nowUs)
|
|
5133
5999
|
}
|
|
5134
6000
|
|
|
@@ -5146,7 +6012,8 @@ export class PgBatchTurns {
|
|
|
5146
6012
|
}
|
|
5147
6013
|
|
|
5148
6014
|
private free(slot: i32): void {
|
|
5149
|
-
|
|
6015
|
+
const busy: boolean[] = this.busy
|
|
6016
|
+
if (slot < 0 || slot >= toI32(busy.length) || busy[slot]) {
|
|
5150
6017
|
panic("nish/net/postgres: PgBatchTurns slot out of range or holding a batch")
|
|
5151
6018
|
}
|
|
5152
6019
|
}
|
|
@@ -5219,10 +6086,12 @@ export const pgTarget = (host: string, port: i32): PgTarget => {
|
|
|
5219
6086
|
if (port < 1 || port > 65535) {
|
|
5220
6087
|
return pgTargetFailed(PG_TARGET_BAD_PORT)
|
|
5221
6088
|
}
|
|
5222
|
-
|
|
5223
|
-
if (
|
|
6089
|
+
// biome-ignore lint/style/useExplicitLengthCheck: `< 1` is the form that proves `charCodeAt(0)` below
|
|
6090
|
+
if (host.length < 1) {
|
|
5224
6091
|
return pgTargetFailed(PG_TARGET_BAD_HOST)
|
|
5225
6092
|
}
|
|
6093
|
+
const n: i32 = toI32(host.length)
|
|
6094
|
+
const first: i32 = toI32(host.charCodeAt(0))
|
|
5226
6095
|
for (let i: i32 = 0; i < n; i++) {
|
|
5227
6096
|
const c: i32 = toI32(host.charCodeAt(i))
|
|
5228
6097
|
if (c === 0 || c === 44) {
|
|
@@ -5232,7 +6101,6 @@ export const pgTarget = (host: string, port: i32): PgTarget => {
|
|
|
5232
6101
|
const t = new PgTarget()
|
|
5233
6102
|
t.host = host
|
|
5234
6103
|
t.port = port
|
|
5235
|
-
const first: i32 = toI32(host.charCodeAt(0))
|
|
5236
6104
|
if (first === 47 || first === 64) {
|
|
5237
6105
|
const path: string = `${host}/.s.PGSQL.${port}`
|
|
5238
6106
|
// An `@` name's NUL-free bytes after the `@` fill what a path's NUL would.
|
|
@@ -5259,7 +6127,8 @@ const pgPortValue = (text: string): i32 => {
|
|
|
5259
6127
|
let v: i32 = 0
|
|
5260
6128
|
for (let i: i32 = 0; i < n; i++) {
|
|
5261
6129
|
const c: i32 = toI32(text.charCodeAt(i))
|
|
5262
|
-
|
|
6130
|
+
// Past 6553 another digit is past 65535; `v` is never negative, which the test states for the prover.
|
|
6131
|
+
if (c < 48 || c > 57 || v < 0 || v > 6553) {
|
|
5263
6132
|
return -1
|
|
5264
6133
|
}
|
|
5265
6134
|
v = v * 10 + (c - 48)
|
|
@@ -5354,18 +6223,31 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5354
6223
|
let hostEnd: i32 = authorityEnd
|
|
5355
6224
|
let portStart: i32 = -1
|
|
5356
6225
|
let bracketed: boolean = false
|
|
5357
|
-
|
|
6226
|
+
// Inside an IPv6 address's brackets.
|
|
6227
|
+
let nameStart: i32 = hostStart
|
|
6228
|
+
let nameEnd: i32 = authorityEnd
|
|
6229
|
+
if (
|
|
6230
|
+
hostStart >= 0 &&
|
|
6231
|
+
hostStart < authorityEnd &&
|
|
6232
|
+
hostStart < n &&
|
|
6233
|
+
toI32(url.charCodeAt(hostStart)) === 91
|
|
6234
|
+
) {
|
|
5358
6235
|
let close: i32 = hostStart + 1
|
|
5359
6236
|
while (close >= 0 && close < authorityEnd && close < n && toI32(url.charCodeAt(close)) !== 93) {
|
|
5360
6237
|
close += 1
|
|
5361
6238
|
}
|
|
5362
|
-
|
|
6239
|
+
// The loop stops at the `]` or at `authorityEnd`, which is at most `n`; the test states that
|
|
6240
|
+
// bound for the prover.
|
|
6241
|
+
if (close < 0 || close >= authorityEnd) {
|
|
5363
6242
|
return pgTargetFailed(PG_TARGET_BAD_HOST)
|
|
5364
6243
|
}
|
|
5365
6244
|
bracketed = true
|
|
6245
|
+
nameStart = hostStart + 1
|
|
6246
|
+
nameEnd = close
|
|
5366
6247
|
hostEnd = close + 1
|
|
5367
6248
|
if (hostEnd < authorityEnd) {
|
|
5368
|
-
|
|
6249
|
+
// `hostEnd` is below `n` as `authorityEnd` is at most `n`; the test states that for the prover.
|
|
6250
|
+
if (hostEnd >= n || toI32(url.charCodeAt(hostEnd)) !== 58) {
|
|
5369
6251
|
return pgTargetFailed(PG_TARGET_BAD_HOST)
|
|
5370
6252
|
}
|
|
5371
6253
|
portStart = hostEnd + 1
|
|
@@ -5380,7 +6262,7 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5380
6262
|
}
|
|
5381
6263
|
}
|
|
5382
6264
|
const decodedHost: string | null = bracketed
|
|
5383
|
-
? pgDecoded(url,
|
|
6265
|
+
? pgDecoded(url, nameStart, nameEnd)
|
|
5384
6266
|
: pgDecoded(url, hostStart, hostEnd)
|
|
5385
6267
|
if (decodedHost === null) {
|
|
5386
6268
|
return pgTargetFailed(PG_TARGET_BAD_ESCAPE)
|
|
@@ -5398,7 +6280,10 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5398
6280
|
}
|
|
5399
6281
|
}
|
|
5400
6282
|
// The query: `key=value` pairs split at `&`.
|
|
5401
|
-
let i: i32 =
|
|
6283
|
+
let i: i32 = n
|
|
6284
|
+
if (queryStart < n) {
|
|
6285
|
+
i = queryStart + 1
|
|
6286
|
+
}
|
|
5402
6287
|
while (i >= 0 && i < n) {
|
|
5403
6288
|
let end: i32 = i
|
|
5404
6289
|
let eq: i32 = -1
|
|
@@ -5409,7 +6294,8 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5409
6294
|
end += 1
|
|
5410
6295
|
}
|
|
5411
6296
|
if (end > i) {
|
|
5412
|
-
|
|
6297
|
+
// `eq` is a `=` found before `end`; the test states that bound for the prover.
|
|
6298
|
+
if (eq < 0 || eq >= end) {
|
|
5413
6299
|
return pgTargetFailed(PG_TARGET_BAD_URL)
|
|
5414
6300
|
}
|
|
5415
6301
|
const key: string | null = pgDecoded(url, i, eq)
|
|
@@ -5426,6 +6312,9 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5426
6312
|
}
|
|
5427
6313
|
}
|
|
5428
6314
|
}
|
|
6315
|
+
if (end >= n) {
|
|
6316
|
+
break
|
|
6317
|
+
}
|
|
5429
6318
|
i = end + 1
|
|
5430
6319
|
}
|
|
5431
6320
|
return pgTarget(host, port)
|