@amritk/nish-aarch64-linux 0.44.0 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/INSTALL.md +3 -1
- package/bin/nish +0 -0
- package/docs/AI.md +18 -16
- package/package.json +1 -1
- package/runtime/core/numbers.mjs +3 -1
- package/runtime/core/strings.mjs +90 -38
- package/runtime/core.d.ts +8 -0
- package/runtime/core.mjs +16 -0
- package/runtime/nish.d.ts +88 -5
- package/runtime/nish.h +10 -0
- package/runtime/nish.mjs +5 -0
- package/runtime/runtime-host.c +15 -0
- package/runtime/runtime-net.c +223 -4
- package/runtime/runtime-os.c +15 -1
- package/runtime/runtime-uring.c +291 -31
- package/runtime/sqlite/nish-sqlite.c +301 -128
- package/std/README.md +15 -15
- 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 +84 -61
- 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 +3 -4
- package/std/crypto/sha1.ts +107 -65
- 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/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 +1315 -222
- 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 -602
- package/std/net/http1-client-codec.ts +372 -161
- package/std/net/http1-codec.ts +665 -331
- package/std/net/http1-server.ts +660 -278
- 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 +647 -192
- package/std/net/http3.ts +667 -189
- package/std/net/oidc.ts +63 -24
- package/std/net/outbound.ts +207 -142
- package/std/net/postgres-async.ts +1165 -443
- package/std/net/postgres-carrier.ts +575 -372
- package/std/net/postgres.ts +1459 -559
- 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 +50 -18
- 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 +225 -124
- package/std/net/quic-listener.ts +197 -89
- 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 +568 -257
- package/std/net/router.ts +450 -99
- package/std/net/serve.ts +651 -232
- 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 +370 -186
- package/std/net/webtransport.ts +755 -436
- package/std/regex.ts +1659 -829
- package/std/sqlite.ts +171 -0
- package/std/sync/ring.ts +320 -156
- package/std/sync/shm.ts +53 -6
- package/std/text.ts +205 -124
- package/std/threads.ts +22 -2
package/std/net/postgres.ts
CHANGED
|
@@ -462,8 +462,10 @@ export class PgError {
|
|
|
462
462
|
|
|
463
463
|
/** A fresh copy of `bytes`; the password holder keeps one. */
|
|
464
464
|
const pgCopyBytes = (bytes: u8[]): u8[] => {
|
|
465
|
-
const copy: u8[] =
|
|
466
|
-
|
|
465
|
+
const copy: u8[] = []
|
|
466
|
+
for (const b of bytes) {
|
|
467
|
+
copy.push(b)
|
|
468
|
+
}
|
|
467
469
|
return copy
|
|
468
470
|
}
|
|
469
471
|
|
|
@@ -550,13 +552,14 @@ const pgBytesOf = (text: string): u8[] => {
|
|
|
550
552
|
/** How many bytes `value` takes in decimal, its sign included. */
|
|
551
553
|
const pgDecimalLength = (value: i64): i32 => {
|
|
552
554
|
// Counted on the negative side, where the most negative i64 fits, two digits a step.
|
|
553
|
-
|
|
555
|
+
// A `u32` count wraps rather than checks; it never passes 20.
|
|
556
|
+
let n: u32 = value < 0 ? 2 : 1
|
|
554
557
|
let v: i64 = value < 0 ? value : -value
|
|
555
558
|
while (v <= -100) {
|
|
556
559
|
v = v / 100
|
|
557
560
|
n += 2
|
|
558
561
|
}
|
|
559
|
-
return v <= -10 ? n + 1 : n
|
|
562
|
+
return toI32(v <= -10 ? n + 1 : n)
|
|
560
563
|
}
|
|
561
564
|
|
|
562
565
|
/**
|
|
@@ -564,12 +567,13 @@ const pgDecimalLength = (value: i64): i32 => {
|
|
|
564
567
|
* `pgDecimalLength(value)`, and answers `at + n`. The digits go once, into
|
|
565
568
|
* their places, from the last, two a step from the magnitude as a `u64`
|
|
566
569
|
* (which holds the most negative i64's, and whose `/` and `%` are never
|
|
567
|
-
* checked).
|
|
568
|
-
* 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.
|
|
569
573
|
*/
|
|
570
574
|
const pgPutDecimal = (out: u8[], at: i32, value: i64, n: i32): i32 => {
|
|
571
575
|
if (!(at >= 0 && at < 2147483620 && n >= 1 && n <= 20 && at + n <= toI32(out.length))) {
|
|
572
|
-
|
|
576
|
+
return -1
|
|
573
577
|
}
|
|
574
578
|
const first: i32 = value < 0 ? at + 1 : at
|
|
575
579
|
let m: u64 = value < 0 ? toU64(0) - toU64(value) : toU64(value)
|
|
@@ -661,28 +665,33 @@ const pgIsText = (b: u8[], from: i32, end: i32): boolean => {
|
|
|
661
665
|
} else {
|
|
662
666
|
return false
|
|
663
667
|
}
|
|
664
|
-
|
|
668
|
+
// `more` bytes must follow `i` before `end`.
|
|
669
|
+
if (toI64(more) >= toI64(end) - toI64(i)) {
|
|
665
670
|
return false
|
|
666
671
|
}
|
|
667
|
-
|
|
668
|
-
|
|
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])
|
|
669
676
|
if (d < low || d > high) {
|
|
670
677
|
return false
|
|
671
678
|
}
|
|
672
679
|
low = 0x80
|
|
673
680
|
high = 0xbf
|
|
681
|
+
k++
|
|
674
682
|
}
|
|
675
|
-
i
|
|
683
|
+
i = k
|
|
676
684
|
}
|
|
677
685
|
return true
|
|
678
686
|
}
|
|
679
687
|
|
|
680
|
-
/**
|
|
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
|
+
*/
|
|
681
693
|
const pgFind = (b: u8[], from: i32, end: i32, byte: i32): i32 => {
|
|
682
|
-
|
|
683
|
-
panic("nish/net/postgres: pgFind outside the buffer")
|
|
684
|
-
}
|
|
685
|
-
for (let i: i32 = from; i < end; i++) {
|
|
694
|
+
for (let i: i32 = from; i >= 0 && i < end && i < toI32(b.length); i++) {
|
|
686
695
|
if (toI32(b[i]) === byte) {
|
|
687
696
|
return i
|
|
688
697
|
}
|
|
@@ -696,10 +705,13 @@ const pgBytesAre = (b: u8[], start: i32, length: i32, text: string): boolean =>
|
|
|
696
705
|
if (length !== n || start < 0 || start > toI32(b.length) - n) {
|
|
697
706
|
return false
|
|
698
707
|
}
|
|
708
|
+
// `k` walks `b` from `start`, which the guard keeps `n` bytes from the end.
|
|
709
|
+
let k: i32 = start
|
|
699
710
|
for (let i: i32 = 0; i < n; i++) {
|
|
700
|
-
if (toI32(b
|
|
711
|
+
if (k >= toI32(b.length) || toI32(b[k]) !== toI32(text.charCodeAt(i))) {
|
|
701
712
|
return false
|
|
702
713
|
}
|
|
714
|
+
k++
|
|
703
715
|
}
|
|
704
716
|
return true
|
|
705
717
|
}
|
|
@@ -724,8 +736,17 @@ export class PgText {
|
|
|
724
736
|
* window, a byte that is not a digit, or an overflow.
|
|
725
737
|
*/
|
|
726
738
|
int(b: u8[], start: i32, length: i32): i64 {
|
|
727
|
-
|
|
728
|
-
|
|
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
|
+
) {
|
|
729
750
|
this.failed = true
|
|
730
751
|
return 0
|
|
731
752
|
}
|
|
@@ -782,26 +803,28 @@ export class PgText {
|
|
|
782
803
|
const v: i64 = head * scale + tail
|
|
783
804
|
return negative ? -v : v
|
|
784
805
|
}
|
|
785
|
-
//
|
|
786
|
-
|
|
787
|
-
//
|
|
788
|
-
const
|
|
789
|
-
|
|
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)
|
|
790
813
|
while (i < end) {
|
|
791
814
|
const d: i32 = toI32(b[i]) - 48
|
|
792
815
|
if (d < 0 || d > 9) {
|
|
793
816
|
this.failed = true
|
|
794
817
|
return 0
|
|
795
818
|
}
|
|
796
|
-
const digit:
|
|
797
|
-
if (acc
|
|
819
|
+
const digit: u64 = toU64(d)
|
|
820
|
+
if (acc > maxTenth || (acc === maxTenth && digit > lastDigit)) {
|
|
798
821
|
this.failed = true
|
|
799
822
|
return 0
|
|
800
823
|
}
|
|
801
|
-
acc = acc * 10
|
|
824
|
+
acc = acc * toU64(10) + digit
|
|
802
825
|
i++
|
|
803
826
|
}
|
|
804
|
-
return negative ? acc :
|
|
827
|
+
return negative ? toI64(toU64(0) - acc) : toI64(acc)
|
|
805
828
|
}
|
|
806
829
|
|
|
807
830
|
/**
|
|
@@ -841,12 +864,19 @@ export class PgText {
|
|
|
841
864
|
* for a column it does not read or no room in `out`.
|
|
842
865
|
*/
|
|
843
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)
|
|
844
872
|
if (
|
|
845
873
|
start < 0 ||
|
|
846
874
|
length < 19 ||
|
|
847
|
-
start > toI32(b.length) -
|
|
875
|
+
start > toI32(b.length) - 19 ||
|
|
876
|
+
last > toI64(b.length) ||
|
|
877
|
+
end > toI32(b.length) ||
|
|
848
878
|
at < 0 ||
|
|
849
|
-
at
|
|
879
|
+
at > toI32(out.length) - 24
|
|
850
880
|
) {
|
|
851
881
|
this.failed = true
|
|
852
882
|
return -1
|
|
@@ -855,15 +885,10 @@ export class PgText {
|
|
|
855
885
|
// belong and are not digits, so the other fourteen bytes are digits
|
|
856
886
|
// exactly when the part holds five non-digits.
|
|
857
887
|
let p: i32 = start + 19
|
|
858
|
-
const end: i32 = start + length
|
|
859
|
-
if (p < 0 || end > toI32(b.length)) {
|
|
860
|
-
this.failed = true
|
|
861
|
-
return -1
|
|
862
|
-
}
|
|
863
888
|
let others: i32 = 0
|
|
864
889
|
for (let i: i32 = start; i < p && i < toI32(b.length); i++) {
|
|
865
890
|
const c: i32 = toI32(b[i])
|
|
866
|
-
if (c < 48 || c > 57) {
|
|
891
|
+
if ((c < 48 || c > 57) && others < 19) {
|
|
867
892
|
others++
|
|
868
893
|
}
|
|
869
894
|
}
|
|
@@ -878,18 +903,24 @@ export class PgText {
|
|
|
878
903
|
this.failed = true
|
|
879
904
|
return -1
|
|
880
905
|
}
|
|
881
|
-
|
|
906
|
+
// Three digits at most, so the `u32` (which wraps rather than checks)
|
|
907
|
+
// stays below 1000.
|
|
908
|
+
let millis: u32 = 0
|
|
882
909
|
if (p < end && toI32(b[p]) === 46) {
|
|
883
910
|
p++
|
|
884
911
|
let digits: i32 = 0
|
|
885
912
|
while (p < end && toI32(b[p]) >= 48 && toI32(b[p]) <= 57) {
|
|
913
|
+
if (digits >= 6) {
|
|
914
|
+
this.failed = true
|
|
915
|
+
return -1
|
|
916
|
+
}
|
|
886
917
|
if (digits < 3) {
|
|
887
|
-
millis = millis * 10 +
|
|
918
|
+
millis = millis * 10 + toU32(b[p]) - 48
|
|
888
919
|
}
|
|
889
920
|
digits++
|
|
890
921
|
p++
|
|
891
922
|
}
|
|
892
|
-
if (digits < 1
|
|
923
|
+
if (digits < 1) {
|
|
893
924
|
this.failed = true
|
|
894
925
|
return -1
|
|
895
926
|
}
|
|
@@ -898,7 +929,13 @@ export class PgText {
|
|
|
898
929
|
digits++
|
|
899
930
|
}
|
|
900
931
|
}
|
|
901
|
-
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
|
+
) {
|
|
902
939
|
p = end
|
|
903
940
|
}
|
|
904
941
|
// The year's four bytes are digits, so their OR is '0' exactly when the year is 0000.
|
|
@@ -918,20 +955,19 @@ export class PgText {
|
|
|
918
955
|
}
|
|
919
956
|
}
|
|
920
957
|
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
for (let i: i32 = width - 1; i >= 0; i--) {
|
|
933
|
-
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)
|
|
934
969
|
v = v / 10
|
|
970
|
+
k--
|
|
935
971
|
}
|
|
936
972
|
}
|
|
937
973
|
|
|
@@ -992,7 +1028,7 @@ export class PgBinary {
|
|
|
992
1028
|
* @returns True for 1, false for 0; false with `failed` set otherwise.
|
|
993
1029
|
*/
|
|
994
1030
|
bool(b: u8[], start: i32, length: i32): boolean {
|
|
995
|
-
if (length === 1 &&
|
|
1031
|
+
if (length === 1 && start >= 0 && start < toI32(b.length)) {
|
|
996
1032
|
const c: i32 = toI32(b[start])
|
|
997
1033
|
if (c === 1) {
|
|
998
1034
|
return true
|
|
@@ -1058,35 +1094,40 @@ export class PgBinary {
|
|
|
1058
1094
|
this.failed = true
|
|
1059
1095
|
return -1
|
|
1060
1096
|
}
|
|
1061
|
-
//
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
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) {
|
|
1070
1107
|
this.failed = true
|
|
1071
1108
|
return -1
|
|
1072
1109
|
}
|
|
1073
1110
|
// The civil date of a day count: eras of 400 years (146097 days), each
|
|
1074
|
-
// year starting on 1 March so that a leap day is its last.
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
const
|
|
1080
|
-
const
|
|
1081
|
-
const
|
|
1082
|
-
const
|
|
1083
|
-
const
|
|
1084
|
-
const
|
|
1085
|
-
|
|
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)
|
|
1086
1127
|
out[at + 4] = toU8(45)
|
|
1087
|
-
pgDigits(out, at + 5,
|
|
1128
|
+
pgDigits(out, at + 5, month, 2)
|
|
1088
1129
|
out[at + 7] = toU8(45)
|
|
1089
|
-
pgDigits(out, at + 8,
|
|
1130
|
+
pgDigits(out, at + 8, day, 2)
|
|
1090
1131
|
out[at + 10] = toU8(84)
|
|
1091
1132
|
pgDigits(out, at + 11, millis / 3600000, 2)
|
|
1092
1133
|
out[at + 13] = toU8(58)
|
|
@@ -1159,8 +1200,17 @@ export class PgBinaryArray {
|
|
|
1159
1200
|
this.seen = 0
|
|
1160
1201
|
this.start = 0
|
|
1161
1202
|
this.length = 0
|
|
1162
|
-
|
|
1163
|
-
|
|
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
|
+
) {
|
|
1164
1214
|
this.refuse()
|
|
1165
1215
|
return
|
|
1166
1216
|
}
|
|
@@ -1185,16 +1235,19 @@ export class PgBinaryArray {
|
|
|
1185
1235
|
}
|
|
1186
1236
|
this.elementOid = oid
|
|
1187
1237
|
this.hasNull = hasNull === 1
|
|
1188
|
-
|
|
1189
|
-
this.
|
|
1238
|
+
const pos: i32 = start + PG_ARRAY_HEADER
|
|
1239
|
+
this.pos = pos
|
|
1240
|
+
this.end = end
|
|
1190
1241
|
if (ndim === 1) {
|
|
1191
|
-
const dim: i32 = getI32BE(b,
|
|
1192
|
-
|
|
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) {
|
|
1193
1246
|
this.refuse()
|
|
1194
1247
|
return
|
|
1195
1248
|
}
|
|
1196
1249
|
this.count = dim
|
|
1197
|
-
this.pos
|
|
1250
|
+
this.pos = pos + PG_ARRAY_DIM
|
|
1198
1251
|
}
|
|
1199
1252
|
}
|
|
1200
1253
|
|
|
@@ -1215,32 +1268,40 @@ export class PgBinaryArray {
|
|
|
1215
1268
|
if (this.bad) {
|
|
1216
1269
|
return PG_ARRAY_BAD
|
|
1217
1270
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
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) {
|
|
1220
1276
|
return PG_ARRAY_END
|
|
1221
1277
|
}
|
|
1222
1278
|
return this.refuse()
|
|
1223
1279
|
}
|
|
1224
|
-
|
|
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)) {
|
|
1225
1284
|
return this.refuse()
|
|
1226
1285
|
}
|
|
1227
|
-
const n: i32 = getI32BE(this.b,
|
|
1228
|
-
|
|
1229
|
-
this.
|
|
1286
|
+
const n: i32 = getI32BE(this.b, pos)
|
|
1287
|
+
const at: i32 = toI32(p)
|
|
1288
|
+
this.pos = at
|
|
1289
|
+
this.seen = seen + 1
|
|
1230
1290
|
if (n === -1) {
|
|
1231
1291
|
if (!this.hasNull) {
|
|
1232
1292
|
return this.refuse()
|
|
1233
1293
|
}
|
|
1234
|
-
this.start =
|
|
1294
|
+
this.start = at
|
|
1235
1295
|
this.length = 0
|
|
1236
1296
|
return PG_ARRAY_NULL
|
|
1237
1297
|
}
|
|
1238
|
-
|
|
1298
|
+
const q: i64 = toI64(at) + toI64(n)
|
|
1299
|
+
if (n !== this.size || q > toI64(end)) {
|
|
1239
1300
|
return this.refuse()
|
|
1240
1301
|
}
|
|
1241
|
-
this.start =
|
|
1302
|
+
this.start = at
|
|
1242
1303
|
this.length = n
|
|
1243
|
-
this.pos
|
|
1304
|
+
this.pos = toI32(q)
|
|
1244
1305
|
return PG_ARRAY_ELEMENT
|
|
1245
1306
|
}
|
|
1246
1307
|
|
|
@@ -1298,12 +1359,26 @@ export class PgArrayWalker {
|
|
|
1298
1359
|
begin(b: u8[], start: i32, length: i32): void {
|
|
1299
1360
|
this.b = b
|
|
1300
1361
|
this.first = true
|
|
1301
|
-
this.
|
|
1302
|
-
|
|
1303
|
-
|
|
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
|
|
1304
1378
|
}
|
|
1379
|
+
this.bad = toI32(b[start]) !== 123 || toI32(b[end - 1]) !== 125
|
|
1305
1380
|
this.pos = start + 1
|
|
1306
|
-
this.end =
|
|
1381
|
+
this.end = end - 1
|
|
1307
1382
|
}
|
|
1308
1383
|
|
|
1309
1384
|
private refuse(): i32 {
|
|
@@ -1323,49 +1398,50 @@ export class PgArrayWalker {
|
|
|
1323
1398
|
}
|
|
1324
1399
|
const b: u8[] = this.b
|
|
1325
1400
|
const end: i32 = this.end
|
|
1326
|
-
|
|
1401
|
+
let p: i32 = this.pos
|
|
1402
|
+
if (p < 0 || p > end || end > toI32(b.length)) {
|
|
1327
1403
|
return this.refuse()
|
|
1328
1404
|
}
|
|
1405
|
+
if (p >= end) {
|
|
1406
|
+
this.first = false
|
|
1407
|
+
return PG_ARRAY_END
|
|
1408
|
+
}
|
|
1329
1409
|
if (this.first) {
|
|
1330
1410
|
this.first = false
|
|
1331
|
-
if (this.pos === end) {
|
|
1332
|
-
return PG_ARRAY_END
|
|
1333
|
-
}
|
|
1334
1411
|
} else {
|
|
1335
|
-
if (
|
|
1336
|
-
return PG_ARRAY_END
|
|
1337
|
-
}
|
|
1338
|
-
if (toI32(b[this.pos]) !== 44) {
|
|
1412
|
+
if (toI32(b[p]) !== 44) {
|
|
1339
1413
|
return this.refuse()
|
|
1340
1414
|
}
|
|
1341
|
-
|
|
1342
|
-
}
|
|
1343
|
-
let p: i32 = this.pos
|
|
1344
|
-
if (p < 0) {
|
|
1345
|
-
return this.refuse()
|
|
1415
|
+
p++
|
|
1346
1416
|
}
|
|
1347
1417
|
this.escaped = false
|
|
1348
1418
|
if (p < end && toI32(b[p]) === 34) {
|
|
1349
1419
|
p++
|
|
1350
|
-
|
|
1420
|
+
const first: i32 = p
|
|
1351
1421
|
while (p < end && toI32(b[p]) !== 34) {
|
|
1352
1422
|
if (toI32(b[p]) === 92) {
|
|
1353
1423
|
this.escaped = true
|
|
1354
1424
|
p++
|
|
1425
|
+
// An escape needs a byte after it before the closing quote.
|
|
1426
|
+
if (p >= end) {
|
|
1427
|
+
return this.refuse()
|
|
1428
|
+
}
|
|
1355
1429
|
}
|
|
1356
1430
|
p++
|
|
1357
1431
|
}
|
|
1358
1432
|
if (p >= end) {
|
|
1359
1433
|
return this.refuse()
|
|
1360
1434
|
}
|
|
1361
|
-
this.
|
|
1362
|
-
this.
|
|
1363
|
-
|
|
1435
|
+
this.start = first
|
|
1436
|
+
this.length = p - first
|
|
1437
|
+
p++
|
|
1438
|
+
this.pos = p
|
|
1439
|
+
if (p < end && toI32(b[p]) !== 44) {
|
|
1364
1440
|
return this.refuse()
|
|
1365
1441
|
}
|
|
1366
1442
|
return PG_ARRAY_ELEMENT
|
|
1367
1443
|
}
|
|
1368
|
-
|
|
1444
|
+
const first: i32 = p
|
|
1369
1445
|
while (p < end && toI32(b[p]) !== 44) {
|
|
1370
1446
|
const c: i32 = toI32(b[p])
|
|
1371
1447
|
if (c === 123 || c === 125 || c === 34 || c === 92 || c === 32) {
|
|
@@ -1373,12 +1449,15 @@ export class PgArrayWalker {
|
|
|
1373
1449
|
}
|
|
1374
1450
|
p++
|
|
1375
1451
|
}
|
|
1376
|
-
this.
|
|
1452
|
+
this.start = first
|
|
1453
|
+
this.length = p - first
|
|
1377
1454
|
this.pos = p
|
|
1378
|
-
if (
|
|
1455
|
+
if (p === first) {
|
|
1379
1456
|
return this.refuse()
|
|
1380
1457
|
}
|
|
1381
|
-
|
|
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)) {
|
|
1382
1461
|
const n: i32 = toI32(b[p - 4]) | 32
|
|
1383
1462
|
const u: i32 = toI32(b[p - 3]) | 32
|
|
1384
1463
|
const l1: i32 = toI32(b[p - 2]) | 32
|
|
@@ -1400,24 +1479,25 @@ export class PgArrayWalker {
|
|
|
1400
1479
|
*/
|
|
1401
1480
|
copy(out: u8[], at: i32): i32 {
|
|
1402
1481
|
const b: u8[] = this.b
|
|
1403
|
-
let n: i32 = 0
|
|
1404
1482
|
let p: i32 = this.start
|
|
1405
|
-
const
|
|
1406
|
-
|
|
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)) {
|
|
1407
1486
|
return -1
|
|
1408
1487
|
}
|
|
1488
|
+
let k: i32 = at
|
|
1409
1489
|
while (p < end) {
|
|
1410
1490
|
if (toI32(b[p]) === 92) {
|
|
1411
1491
|
p++
|
|
1412
1492
|
}
|
|
1413
|
-
if (p >= end ||
|
|
1493
|
+
if (p >= end || k >= toI32(out.length)) {
|
|
1414
1494
|
return -1
|
|
1415
1495
|
}
|
|
1416
|
-
out[
|
|
1417
|
-
|
|
1496
|
+
out[k] = b[p]
|
|
1497
|
+
k++
|
|
1418
1498
|
p++
|
|
1419
1499
|
}
|
|
1420
|
-
return
|
|
1500
|
+
return k - at
|
|
1421
1501
|
}
|
|
1422
1502
|
}
|
|
1423
1503
|
|
|
@@ -1531,7 +1611,7 @@ export class PgConnection {
|
|
|
1531
1611
|
}
|
|
1532
1612
|
this.clientNonce = base64StdPadEncode(head)
|
|
1533
1613
|
|
|
1534
|
-
const at: i32 =
|
|
1614
|
+
const at: i32 = 0
|
|
1535
1615
|
this.putI32(PG_ZERO)
|
|
1536
1616
|
this.putI32(PG_PROTOCOL)
|
|
1537
1617
|
this.putParam("user", config.user)
|
|
@@ -1548,7 +1628,8 @@ export class PgConnection {
|
|
|
1548
1628
|
this.putParam("options", config.options)
|
|
1549
1629
|
}
|
|
1550
1630
|
this.putByte(PG_ZERO)
|
|
1551
|
-
|
|
1631
|
+
// The message is the first in the buffer, so its length is the buffer's end.
|
|
1632
|
+
setI32BE(this.out, at, this.outEnd)
|
|
1552
1633
|
}
|
|
1553
1634
|
|
|
1554
1635
|
// --- Buffers -----------------------------------------------------------------
|
|
@@ -1577,7 +1658,12 @@ export class PgConnection {
|
|
|
1577
1658
|
* is full, which `next()` mends by compacting or growing it.
|
|
1578
1659
|
*/
|
|
1579
1660
|
inputRoom(): i32 {
|
|
1580
|
-
|
|
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
|
|
1581
1667
|
}
|
|
1582
1668
|
|
|
1583
1669
|
/**
|
|
@@ -1586,10 +1672,12 @@ export class PgConnection {
|
|
|
1586
1672
|
* @param n The bytes the read wrote, from 0 to `inputRoom()`.
|
|
1587
1673
|
*/
|
|
1588
1674
|
received(n: i32): void {
|
|
1589
|
-
|
|
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)) {
|
|
1590
1678
|
panic("nish/net/postgres: received more than inputRoom()")
|
|
1591
1679
|
}
|
|
1592
|
-
this.inEnd
|
|
1680
|
+
this.inEnd = toI32(last)
|
|
1593
1681
|
}
|
|
1594
1682
|
|
|
1595
1683
|
/**
|
|
@@ -1603,13 +1691,14 @@ export class PgConnection {
|
|
|
1603
1691
|
* caller's to give again after `next()`.
|
|
1604
1692
|
*/
|
|
1605
1693
|
receive(src: u8[], start: i32, length: i32): i32 {
|
|
1606
|
-
if (start < 0 || length < 0 || length >
|
|
1694
|
+
if (start < 0 || length < 0 || toI64(start) + toI64(length) > toI64(src.length)) {
|
|
1607
1695
|
panic("nish/net/postgres: receive() outside its source")
|
|
1608
1696
|
}
|
|
1609
1697
|
const room: i32 = this.inputRoom()
|
|
1610
1698
|
const n: i32 = length < room ? length : room
|
|
1611
1699
|
copyBytes(this.input, this.inEnd, src, start, n)
|
|
1612
|
-
|
|
1700
|
+
// `inputRoom` keeps the end inside the buffer, so the sum fits an `i32`.
|
|
1701
|
+
this.inEnd = toI32(toI64(this.inEnd) + toI64(n))
|
|
1613
1702
|
return n
|
|
1614
1703
|
}
|
|
1615
1704
|
|
|
@@ -1647,10 +1736,12 @@ export class PgConnection {
|
|
|
1647
1736
|
* @param n The bytes the write took.
|
|
1648
1737
|
*/
|
|
1649
1738
|
sent(n: i32): void {
|
|
1650
|
-
|
|
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())) {
|
|
1651
1742
|
panic("nish/net/postgres: sent more than was queued")
|
|
1652
1743
|
}
|
|
1653
|
-
this.outStart
|
|
1744
|
+
this.outStart = toI32(last)
|
|
1654
1745
|
if (this.outStart === this.outEnd) {
|
|
1655
1746
|
this.outStart = 0
|
|
1656
1747
|
this.outEnd = 0
|
|
@@ -1724,15 +1815,20 @@ export class PgConnection {
|
|
|
1724
1815
|
if (this.state === PG_STATE_FAILED) {
|
|
1725
1816
|
return PG_FAILED
|
|
1726
1817
|
}
|
|
1727
|
-
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
|
|
1728
1827
|
if (avail < 5) {
|
|
1729
1828
|
this.compact()
|
|
1730
1829
|
return PG_NEED_MORE
|
|
1731
1830
|
}
|
|
1732
|
-
|
|
1733
|
-
const b: u8[] = this.input
|
|
1734
|
-
// inStart and inEnd stay within the buffer, so this test never fails; it proves the indices.
|
|
1735
|
-
if (at < 0 || at + 4 >= toI32(b.length)) {
|
|
1831
|
+
if (at > toI32(b.length) - 5) {
|
|
1736
1832
|
return this.fail(PG_ERR_FRAME)
|
|
1737
1833
|
}
|
|
1738
1834
|
const type: i32 = toI32(b[at])
|
|
@@ -1748,18 +1844,23 @@ export class PgConnection {
|
|
|
1748
1844
|
if (avail - 1 < length) {
|
|
1749
1845
|
this.compact()
|
|
1750
1846
|
const size: i32 = toI32(this.input.length)
|
|
1751
|
-
if (size
|
|
1752
|
-
// Doubled, up to one message of maxMessage, so that a run of growing
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
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))
|
|
1756
1855
|
grown.set(this.input, 0)
|
|
1757
1856
|
this.input = grown
|
|
1758
1857
|
}
|
|
1759
1858
|
return PG_NEED_MORE
|
|
1760
1859
|
}
|
|
1761
|
-
|
|
1762
|
-
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)
|
|
1763
1864
|
if (event !== PG_NEED_MORE) {
|
|
1764
1865
|
return event
|
|
1765
1866
|
}
|
|
@@ -1769,10 +1870,12 @@ export class PgConnection {
|
|
|
1769
1870
|
/** Moves the unread input, at most one partial message, to the start of the buffer. */
|
|
1770
1871
|
private compact(): void {
|
|
1771
1872
|
const start: i32 = this.inStart
|
|
1772
|
-
|
|
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) {
|
|
1773
1876
|
return
|
|
1774
1877
|
}
|
|
1775
|
-
const n: i32 =
|
|
1878
|
+
const n: i32 = end - start
|
|
1776
1879
|
copyBytes(this.input, 0, this.input, start, n)
|
|
1777
1880
|
this.inStart = 0
|
|
1778
1881
|
this.inEnd = n
|
|
@@ -1791,12 +1894,21 @@ export class PgConnection {
|
|
|
1791
1894
|
/** One message, `[body, end)` its contents; answers its event, or `PG_NEED_MORE` when it has none. */
|
|
1792
1895
|
private dispatch(type: i32, body: i32, end: i32): i32 {
|
|
1793
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
|
|
1794
1903
|
if (type === 82) {
|
|
1795
1904
|
// 'R': an authentication request.
|
|
1796
|
-
if (state >= PG_STATE_STARTING ||
|
|
1797
|
-
return this.fail(
|
|
1905
|
+
if (state >= PG_STATE_STARTING || size < 4) {
|
|
1906
|
+
return this.fail(size < 4 ? PG_ERR_FRAME : PG_ERR_UNEXPECTED)
|
|
1907
|
+
}
|
|
1908
|
+
if (body > toI32(b.length) - 4) {
|
|
1909
|
+
return this.fail(PG_ERR_FRAME)
|
|
1798
1910
|
}
|
|
1799
|
-
return this.authenticate(getI32BE(
|
|
1911
|
+
return this.authenticate(getI32BE(b, body), body + 4, end)
|
|
1800
1912
|
}
|
|
1801
1913
|
if (type === 69 || type === 78) {
|
|
1802
1914
|
// 'E' and 'N': an error or a notice.
|
|
@@ -1805,8 +1917,9 @@ export class PgConnection {
|
|
|
1805
1917
|
return this.fail(PG_ERR_FRAME)
|
|
1806
1918
|
}
|
|
1807
1919
|
if (type === 78) {
|
|
1808
|
-
|
|
1809
|
-
|
|
1920
|
+
const notices: i32 = this.notices
|
|
1921
|
+
if (notices >= 0 && notices < 2147483647) {
|
|
1922
|
+
this.notices = notices + 1
|
|
1810
1923
|
}
|
|
1811
1924
|
this.lastNotice = error
|
|
1812
1925
|
return PG_NOTICE
|
|
@@ -1823,27 +1936,42 @@ export class PgConnection {
|
|
|
1823
1936
|
}
|
|
1824
1937
|
if (type === 83) {
|
|
1825
1938
|
// 'S': ParameterStatus, name and value.
|
|
1826
|
-
const nameEnd: i32 = pgFind(
|
|
1827
|
-
|
|
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) {
|
|
1828
1942
|
return this.fail(PG_ERR_FRAME)
|
|
1829
1943
|
}
|
|
1830
|
-
|
|
1831
|
-
|
|
1944
|
+
const value: i32 = nameEnd + 1
|
|
1945
|
+
const valueEnd: i32 = end - 1
|
|
1946
|
+
if (valueEnd < 0 || valueEnd < value || pgFind(b, value, end, 0) !== valueEnd) {
|
|
1947
|
+
return this.fail(PG_ERR_FRAME)
|
|
1948
|
+
}
|
|
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")) {
|
|
1832
1953
|
return this.fail(PG_ERR_ENCODING)
|
|
1833
1954
|
}
|
|
1834
|
-
} else if (pgBytesAre(
|
|
1955
|
+
} else if (pgBytesAre(b, body, nameLength, "DateStyle")) {
|
|
1835
1956
|
// "ISO, MDY": the output style comes first, the input order after the comma.
|
|
1836
|
-
|
|
1837
|
-
const iso: boolean = valueLength >= 3 && pgBytesAre(this.input, nameEnd + 1, 3, "ISO")
|
|
1838
|
-
if (!iso || (valueLength > 3 && toI32(this.input[nameEnd + 4]) !== PG_COMMA)) {
|
|
1957
|
+
if (valueLength < 3 || !pgBytesAre(b, value, 3, "ISO")) {
|
|
1839
1958
|
return this.fail(PG_ERR_SETTING)
|
|
1840
1959
|
}
|
|
1841
|
-
|
|
1842
|
-
|
|
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")) {
|
|
1843
1971
|
return this.fail(PG_ERR_SETTING)
|
|
1844
1972
|
}
|
|
1845
|
-
} else if (pgBytesAre(
|
|
1846
|
-
this.serverVersion = String.fromBytes(
|
|
1973
|
+
} else if (pgBytesAre(b, body, nameLength, "server_version")) {
|
|
1974
|
+
this.serverVersion = String.fromBytes(b, value, valueEnd)
|
|
1847
1975
|
}
|
|
1848
1976
|
return PG_NEED_MORE
|
|
1849
1977
|
}
|
|
@@ -1856,18 +1984,18 @@ export class PgConnection {
|
|
|
1856
1984
|
}
|
|
1857
1985
|
if (type === 75) {
|
|
1858
1986
|
// 'K': BackendKeyData, the process id and the cancel key.
|
|
1859
|
-
if (state !== PG_STATE_STARTING ||
|
|
1987
|
+
if (state !== PG_STATE_STARTING || size !== 8) {
|
|
1860
1988
|
return this.fail(state !== PG_STATE_STARTING ? PG_ERR_UNEXPECTED : PG_ERR_FRAME)
|
|
1861
1989
|
}
|
|
1862
|
-
this.backendPid = getI32BE(
|
|
1990
|
+
this.backendPid = getI32BE(b, body)
|
|
1863
1991
|
return PG_NEED_MORE
|
|
1864
1992
|
}
|
|
1865
1993
|
if (type === 90) {
|
|
1866
1994
|
// 'Z': ReadyForQuery, and the transaction status.
|
|
1867
|
-
if (
|
|
1995
|
+
if (size !== 1 || body >= toI32(b.length)) {
|
|
1868
1996
|
return this.fail(PG_ERR_FRAME)
|
|
1869
1997
|
}
|
|
1870
|
-
const status: i32 = toI32(
|
|
1998
|
+
const status: i32 = toI32(b[body])
|
|
1871
1999
|
if (status !== 73 && status !== 84 && status !== 69) {
|
|
1872
2000
|
return this.fail(PG_ERR_FRAME)
|
|
1873
2001
|
}
|
|
@@ -1876,10 +2004,11 @@ export class PgConnection {
|
|
|
1876
2004
|
this.state = PG_STATE_READY
|
|
1877
2005
|
return PG_READY
|
|
1878
2006
|
}
|
|
1879
|
-
|
|
2007
|
+
const inFlight: i32 = this.inFlight
|
|
2008
|
+
if (inFlight <= 0 || (inFlight === 1 && this.groupOpen)) {
|
|
1880
2009
|
return this.fail(PG_ERR_UNEXPECTED)
|
|
1881
2010
|
}
|
|
1882
|
-
this.inFlight
|
|
2011
|
+
this.inFlight = inFlight - 1
|
|
1883
2012
|
this.groupFailed = this.errorInGroup
|
|
1884
2013
|
this.errorInGroup = false
|
|
1885
2014
|
return PG_GROUP_DONE
|
|
@@ -1892,11 +2021,11 @@ export class PgConnection {
|
|
|
1892
2021
|
}
|
|
1893
2022
|
if (type === 67) {
|
|
1894
2023
|
// 'C': CommandComplete, the tag.
|
|
1895
|
-
if (
|
|
2024
|
+
if (size < 1 || pgFind(b, body, end, 0) !== end - 1) {
|
|
1896
2025
|
return this.fail(PG_ERR_FRAME)
|
|
1897
2026
|
}
|
|
1898
2027
|
this.tagStart = body
|
|
1899
|
-
this.tagLength =
|
|
2028
|
+
this.tagLength = size - 1
|
|
1900
2029
|
return PG_COMPLETE
|
|
1901
2030
|
}
|
|
1902
2031
|
if (type === 73) {
|
|
@@ -1914,7 +2043,7 @@ export class PgConnection {
|
|
|
1914
2043
|
}
|
|
1915
2044
|
if (type === 116) {
|
|
1916
2045
|
// 't': ParameterDescription.
|
|
1917
|
-
if (
|
|
2046
|
+
if (size < 2 || size !== toI32(getU16BE(b, body)) * 4 + 2) {
|
|
1918
2047
|
return this.fail(PG_ERR_FRAME)
|
|
1919
2048
|
}
|
|
1920
2049
|
return PG_NEED_MORE
|
|
@@ -1935,10 +2064,13 @@ export class PgConnection {
|
|
|
1935
2064
|
|
|
1936
2065
|
/** Makes the column arrays hold at least `n`. */
|
|
1937
2066
|
private reserveColumns(n: i32): void {
|
|
1938
|
-
|
|
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) {
|
|
1939
2071
|
return
|
|
1940
2072
|
}
|
|
1941
|
-
const size: i32 = n <
|
|
2073
|
+
const size: i32 = n < have * 2 ? have * 2 : n
|
|
1942
2074
|
const starts: i32[] = new Array<i32>(size)
|
|
1943
2075
|
const lengths: i32[] = new Array<i32>(size)
|
|
1944
2076
|
const types: i32[] = new Array<i32>(size)
|
|
@@ -1952,35 +2084,40 @@ export class PgConnection {
|
|
|
1952
2084
|
|
|
1953
2085
|
/** 'D': DataRow, each column's length (-1 for NULL) and bytes, which must fill the message exactly. */
|
|
1954
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
|
+
}
|
|
1955
2092
|
if (end - body < 2) {
|
|
1956
2093
|
return this.fail(PG_ERR_FRAME)
|
|
1957
2094
|
}
|
|
1958
|
-
const b: u8[] = this.input
|
|
1959
2095
|
const n: i32 = toI32(getU16BE(b, body))
|
|
2096
|
+
let p: i32 = body + 2
|
|
1960
2097
|
if (n > this.config.maxColumns) {
|
|
1961
2098
|
return this.fail(PG_ERR_TOO_BIG)
|
|
1962
2099
|
}
|
|
1963
2100
|
this.reserveColumns(n)
|
|
1964
2101
|
const starts: i32[] = this.colStarts
|
|
1965
2102
|
const lengths: i32[] = this.colLengths
|
|
1966
|
-
let p: i32 = body + 2
|
|
1967
2103
|
// reserveColumns made both arrays hold n; the length tests prove the indices.
|
|
1968
2104
|
for (let c: i32 = 0; c < n && c < toI32(starts.length) && c < toI32(lengths.length); c++) {
|
|
1969
2105
|
// The buffer tests never fail on a frame next() cut from it; they prove the indices.
|
|
1970
|
-
if (
|
|
2106
|
+
if (p < 0 || end < p || end - p < 4 || p > toI32(b.length) - 4) {
|
|
1971
2107
|
return this.fail(PG_ERR_FRAME)
|
|
1972
2108
|
}
|
|
1973
2109
|
// getI32BE written out: a call per column was a fifth of the row's cost.
|
|
1974
2110
|
const length: i32 =
|
|
1975
2111
|
(toI32(b[p]) << 24) | (toI32(b[p + 1]) << 16) | (toI32(b[p + 2]) << 8) | toI32(b[p + 3])
|
|
1976
|
-
p
|
|
2112
|
+
p = p + 4
|
|
1977
2113
|
if (length < -1 || length > end - p) {
|
|
1978
2114
|
return this.fail(PG_ERR_FRAME)
|
|
1979
2115
|
}
|
|
1980
2116
|
starts[c] = p
|
|
1981
2117
|
lengths[c] = length
|
|
1982
2118
|
if (length > 0) {
|
|
1983
|
-
|
|
2119
|
+
// The value ends inside the frame, so the sum fits an `i32`.
|
|
2120
|
+
p = toI32(toI64(p) + toI64(length))
|
|
1984
2121
|
}
|
|
1985
2122
|
}
|
|
1986
2123
|
if (p !== end) {
|
|
@@ -1992,22 +2129,29 @@ export class PgConnection {
|
|
|
1992
2129
|
|
|
1993
2130
|
/** 'T': RowDescription; each field is a name and 18 bytes, the type's OID 6 bytes into them. */
|
|
1994
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
|
+
}
|
|
1995
2137
|
if (end - body < 2) {
|
|
1996
2138
|
return this.fail(PG_ERR_FRAME)
|
|
1997
2139
|
}
|
|
1998
|
-
const b: u8[] = this.input
|
|
1999
2140
|
const n: i32 = toI32(getU16BE(b, body))
|
|
2141
|
+
let p: i32 = body + 2
|
|
2000
2142
|
if (n > this.config.maxColumns) {
|
|
2001
2143
|
return this.fail(PG_ERR_TOO_BIG)
|
|
2002
2144
|
}
|
|
2003
2145
|
this.reserveColumns(n)
|
|
2004
|
-
|
|
2005
|
-
|
|
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++) {
|
|
2006
2149
|
const nameEnd: i32 = pgFind(b, p, end, 0)
|
|
2007
|
-
|
|
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) {
|
|
2008
2152
|
return this.fail(PG_ERR_FRAME)
|
|
2009
2153
|
}
|
|
2010
|
-
|
|
2154
|
+
types[c] = getI32BE(b, nameEnd + 7)
|
|
2011
2155
|
p = nameEnd + 19
|
|
2012
2156
|
}
|
|
2013
2157
|
if (p !== end) {
|
|
@@ -2020,7 +2164,7 @@ export class PgConnection {
|
|
|
2020
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. */
|
|
2021
2165
|
private readError(body: i32, end: i32): PgError | null {
|
|
2022
2166
|
const b: u8[] = this.input
|
|
2023
|
-
if (body < 0 || end > toI32(b.length)) {
|
|
2167
|
+
if (body < 0 || end < 0 || end > toI32(b.length)) {
|
|
2024
2168
|
return null
|
|
2025
2169
|
}
|
|
2026
2170
|
let severity: string = ""
|
|
@@ -2044,7 +2188,7 @@ export class PgConnection {
|
|
|
2044
2188
|
break
|
|
2045
2189
|
}
|
|
2046
2190
|
const valueEnd: i32 = pgFind(b, p + 1, end, 0)
|
|
2047
|
-
if (valueEnd
|
|
2191
|
+
if (valueEnd < 0 || valueEnd >= end) {
|
|
2048
2192
|
return null
|
|
2049
2193
|
}
|
|
2050
2194
|
const value: string = String.fromBytes(b, p + 1, valueEnd)
|
|
@@ -2116,7 +2260,7 @@ export class PgConnection {
|
|
|
2116
2260
|
/** AuthenticationSASL: the mechanisms, each NUL-terminated, then a 0; SCRAM-SHA-256 must be among them. */
|
|
2117
2261
|
private scramStart(body: i32, end: i32): i32 {
|
|
2118
2262
|
const b: u8[] = this.input
|
|
2119
|
-
if (body < 0 || end > toI32(b.length)) {
|
|
2263
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length)) {
|
|
2120
2264
|
return this.fail(PG_ERR_FRAME)
|
|
2121
2265
|
}
|
|
2122
2266
|
let offered: boolean = false
|
|
@@ -2132,7 +2276,7 @@ export class PgConnection {
|
|
|
2132
2276
|
break
|
|
2133
2277
|
}
|
|
2134
2278
|
const nameEnd: i32 = pgFind(b, p, end, 0)
|
|
2135
|
-
if (nameEnd
|
|
2279
|
+
if (nameEnd < 0 || nameEnd < p || nameEnd >= end) {
|
|
2136
2280
|
return this.fail(PG_ERR_FRAME)
|
|
2137
2281
|
}
|
|
2138
2282
|
if (pgBytesAre(b, p, nameEnd - p, "SCRAM-SHA-256")) {
|
|
@@ -2167,16 +2311,23 @@ export class PgConnection {
|
|
|
2167
2311
|
*/
|
|
2168
2312
|
private scramContinue(body: i32, end: i32): i32 {
|
|
2169
2313
|
const b: u8[] = this.input
|
|
2170
|
-
if (body < 0 || end > toI32(b.length)) {
|
|
2314
|
+
if (body < 0 || end < 0 || end < body || end > toI32(b.length)) {
|
|
2171
2315
|
return this.fail(PG_ERR_FRAME)
|
|
2172
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.
|
|
2173
2319
|
const nonceEnd: i32 = pgFind(b, body, end, 44)
|
|
2174
|
-
|
|
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)
|
|
2175
2325
|
if (
|
|
2176
|
-
|
|
2177
|
-
saltEnd
|
|
2326
|
+
saltEnd < 0 ||
|
|
2327
|
+
saltEnd < saltStart ||
|
|
2328
|
+
saltEnd >= end ||
|
|
2178
2329
|
!pgBytesAre(b, body, 2, "r=") ||
|
|
2179
|
-
!pgBytesAre(b,
|
|
2330
|
+
!pgBytesAre(b, saltStart, 2, "s=") ||
|
|
2180
2331
|
end - saltEnd < 4 ||
|
|
2181
2332
|
!pgBytesAre(b, saltEnd + 1, 2, "i=")
|
|
2182
2333
|
) {
|
|
@@ -2184,12 +2335,14 @@ export class PgConnection {
|
|
|
2184
2335
|
}
|
|
2185
2336
|
// The nonce extends ours with printable characters other than a comma.
|
|
2186
2337
|
const ours: i32 = toI32(this.clientNonce.length)
|
|
2187
|
-
|
|
2338
|
+
const nonceAt: i32 = body + 2
|
|
2339
|
+
if (ours < 0 || nonceEnd - nonceAt <= ours || !pgBytesAre(b, nonceAt, ours, this.clientNonce)) {
|
|
2188
2340
|
return this.fail(PG_ERR_SCRAM)
|
|
2189
2341
|
}
|
|
2190
|
-
|
|
2191
|
-
const
|
|
2192
|
-
|
|
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) {
|
|
2193
2346
|
return this.fail(PG_ERR_SCRAM)
|
|
2194
2347
|
}
|
|
2195
2348
|
for (let i: i32 = nonceStart; i < nonceEnd && i < toI32(b.length); i++) {
|
|
@@ -2207,12 +2360,18 @@ export class PgConnection {
|
|
|
2207
2360
|
if (d < 0 || d > 9 || iterations > this.config.maxIterations) {
|
|
2208
2361
|
return this.fail(PG_ERR_SCRAM)
|
|
2209
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
|
+
}
|
|
2210
2367
|
iterations = iterations * 10 + d
|
|
2211
2368
|
}
|
|
2212
2369
|
if (iterations < this.config.minIterations || iterations > this.config.maxIterations) {
|
|
2213
2370
|
return this.fail(PG_ERR_SCRAM)
|
|
2214
2371
|
}
|
|
2215
|
-
const salt: u8[] | null = base64StdPadDecode(
|
|
2372
|
+
const salt: u8[] | null = base64StdPadDecode(
|
|
2373
|
+
String.fromBytes(b, toI32(toI64(saltStart) + toI64(2)), saltEnd)
|
|
2374
|
+
)
|
|
2216
2375
|
if (salt === null || salt.length === 0) {
|
|
2217
2376
|
return this.fail(PG_ERR_SCRAM)
|
|
2218
2377
|
}
|
|
@@ -2224,7 +2383,7 @@ export class PgConnection {
|
|
|
2224
2383
|
if (salted === null) {
|
|
2225
2384
|
return this.fail(PG_ERR_SCRAM)
|
|
2226
2385
|
}
|
|
2227
|
-
const withoutProof: string = `c=biws,r=${String.fromBytes(b,
|
|
2386
|
+
const withoutProof: string = `c=biws,r=${String.fromBytes(b, nonceAt, nonceEnd)}`
|
|
2228
2387
|
const authMessage: u8[] = pgBytesOf(
|
|
2229
2388
|
`${this.firstBare()},${String.fromBytes(b, body, end)},${withoutProof}`
|
|
2230
2389
|
)
|
|
@@ -2245,10 +2404,10 @@ export class PgConnection {
|
|
|
2245
2404
|
/** AuthenticationSASLFinal: exactly `v=` and the ServerSignature, compared in constant time. */
|
|
2246
2405
|
private scramFinal(body: i32, end: i32): i32 {
|
|
2247
2406
|
const b: u8[] = this.input
|
|
2248
|
-
if (end - body < 2 || !pgBytesAre(b, body, 2, "v=")) {
|
|
2407
|
+
if (body < 0 || end < 0 || end - body < 2 || !pgBytesAre(b, body, 2, "v=")) {
|
|
2249
2408
|
return this.fail(PG_ERR_SCRAM)
|
|
2250
2409
|
}
|
|
2251
|
-
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))
|
|
2252
2411
|
if (signature === null || signature.length !== 32) {
|
|
2253
2412
|
return this.fail(PG_ERR_SCRAM)
|
|
2254
2413
|
}
|
|
@@ -2268,12 +2427,21 @@ export class PgConnection {
|
|
|
2268
2427
|
* buffer, the unsent bytes move to its start first: a builder call asks
|
|
2269
2428
|
* before it takes any offset into the buffer, so nothing it holds moves.
|
|
2270
2429
|
*/
|
|
2271
|
-
private room(size:
|
|
2272
|
-
const
|
|
2273
|
-
|
|
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
|
+
) {
|
|
2274
2442
|
return false
|
|
2275
2443
|
}
|
|
2276
|
-
if (
|
|
2444
|
+
if (start > 0 && toI64(n) + toI64(end) > toI64(this.out.length)) {
|
|
2277
2445
|
this.compactOutput()
|
|
2278
2446
|
}
|
|
2279
2447
|
return true
|
|
@@ -2284,8 +2452,13 @@ export class PgConnection {
|
|
|
2284
2452
|
* `terminate`) the way `room` does for the rest, so that the buffer is not
|
|
2285
2453
|
* grown past `maxOutput` while sent bytes still fill its start.
|
|
2286
2454
|
*/
|
|
2287
|
-
private tailRoom(size:
|
|
2288
|
-
|
|
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)) {
|
|
2289
2462
|
this.compactOutput()
|
|
2290
2463
|
}
|
|
2291
2464
|
}
|
|
@@ -2293,24 +2466,31 @@ export class PgConnection {
|
|
|
2293
2466
|
/** Moves the unsent output to the start of the buffer, the open bind's offsets with it. */
|
|
2294
2467
|
private compactOutput(): void {
|
|
2295
2468
|
const shift: i32 = this.outStart
|
|
2296
|
-
|
|
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) {
|
|
2297
2472
|
return
|
|
2298
2473
|
}
|
|
2299
|
-
|
|
2300
|
-
this.
|
|
2474
|
+
const n: i32 = end - shift
|
|
2475
|
+
copyBytes(this.out, 0, this.out, shift, n)
|
|
2476
|
+
this.outEnd = n
|
|
2301
2477
|
this.outStart = 0
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
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
|
|
2305
2484
|
}
|
|
2306
|
-
|
|
2307
|
-
|
|
2485
|
+
const arrayAt: i32 = this.arrayAt
|
|
2486
|
+
if (arrayAt >= 0) {
|
|
2487
|
+
this.arrayAt = arrayAt - shift
|
|
2308
2488
|
}
|
|
2309
2489
|
}
|
|
2310
2490
|
|
|
2311
2491
|
/** Makes the output buffer at least `want` bytes long. */
|
|
2312
|
-
private reserve(want:
|
|
2313
|
-
if (want >
|
|
2492
|
+
private reserve(want: i64): void {
|
|
2493
|
+
if (want > toI64(this.out.length)) {
|
|
2314
2494
|
this.grow(want)
|
|
2315
2495
|
}
|
|
2316
2496
|
}
|
|
@@ -2319,38 +2499,76 @@ export class PgConnection {
|
|
|
2319
2499
|
* `reserve` when the buffer is short: kept apart so that the test above,
|
|
2320
2500
|
* which every put makes, is small enough to inline into it.
|
|
2321
2501
|
*/
|
|
2322
|
-
private grow(want:
|
|
2323
|
-
const
|
|
2324
|
-
const grown: u8[] = new Array<u8>(
|
|
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
|
+
)
|
|
2325
2507
|
grown.set(this.out, 0)
|
|
2326
2508
|
this.out = grown
|
|
2327
2509
|
}
|
|
2328
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
|
+
|
|
2329
2514
|
private putByte(v: i32): void {
|
|
2330
|
-
this.
|
|
2331
|
-
|
|
2332
|
-
|
|
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
|
|
2333
2529
|
}
|
|
2334
2530
|
|
|
2335
2531
|
private putI32(v: i32): void {
|
|
2336
|
-
this.
|
|
2337
|
-
|
|
2338
|
-
|
|
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
|
|
2339
2546
|
}
|
|
2340
2547
|
|
|
2341
2548
|
private putI16(v: i32): void {
|
|
2342
|
-
this.
|
|
2343
|
-
|
|
2344
|
-
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
|
|
2345
2557
|
}
|
|
2346
2558
|
|
|
2347
2559
|
private putString(text: string): void {
|
|
2348
2560
|
const n: i32 = toI32(text.length)
|
|
2349
|
-
this.
|
|
2350
|
-
|
|
2351
|
-
|
|
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++
|
|
2352
2570
|
}
|
|
2353
|
-
this.outEnd
|
|
2571
|
+
this.outEnd = k
|
|
2354
2572
|
}
|
|
2355
2573
|
|
|
2356
2574
|
private putCString(text: string): void {
|
|
@@ -2373,7 +2591,12 @@ export class PgConnection {
|
|
|
2373
2591
|
|
|
2374
2592
|
/** Writes the length of the message started at `at`. */
|
|
2375
2593
|
private endMessage(at: i32): void {
|
|
2376
|
-
|
|
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)
|
|
2377
2600
|
}
|
|
2378
2601
|
|
|
2379
2602
|
/**
|
|
@@ -2387,13 +2610,18 @@ export class PgConnection {
|
|
|
2387
2610
|
}
|
|
2388
2611
|
|
|
2389
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. */
|
|
2390
|
-
private canOpen(size:
|
|
2613
|
+
private canOpen(size: i64): boolean {
|
|
2391
2614
|
if (!this.canSend() || this.bindAt >= 0 || !this.room(size)) {
|
|
2392
2615
|
return false
|
|
2393
2616
|
}
|
|
2394
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
|
+
}
|
|
2395
2623
|
this.groupOpen = true
|
|
2396
|
-
this.inFlight
|
|
2624
|
+
this.inFlight = inFlight + 1
|
|
2397
2625
|
}
|
|
2398
2626
|
return true
|
|
2399
2627
|
}
|
|
@@ -2421,7 +2649,10 @@ export class PgConnection {
|
|
|
2421
2649
|
return false
|
|
2422
2650
|
}
|
|
2423
2651
|
const sqlLength: i32 = toI32(sql.length)
|
|
2424
|
-
if (
|
|
2652
|
+
if (
|
|
2653
|
+
sqlLength > PG_MAX_CAP ||
|
|
2654
|
+
!this.canOpen(toI64(16) + toI64(2) * toI64(nameLength) + toI64(sqlLength) + toI64(4) * toI64(types))
|
|
2655
|
+
) {
|
|
2425
2656
|
return false
|
|
2426
2657
|
}
|
|
2427
2658
|
const at: i32 = this.startMessage(PG_FE_PARSE)
|
|
@@ -2447,7 +2678,7 @@ export class PgConnection {
|
|
|
2447
2678
|
* @returns True when the Close was queued.
|
|
2448
2679
|
*/
|
|
2449
2680
|
closeStatement(name: string): boolean {
|
|
2450
|
-
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))) {
|
|
2451
2682
|
return false
|
|
2452
2683
|
}
|
|
2453
2684
|
const at: i32 = this.startMessage(PG_FE_CLOSE)
|
|
@@ -2492,7 +2723,11 @@ export class PgConnection {
|
|
|
2492
2723
|
private openBind(name: string, binary: boolean): boolean {
|
|
2493
2724
|
const opens: boolean = !this.groupOpen
|
|
2494
2725
|
const extra: i32 = binary ? PG_BINARY_TAIL : 0
|
|
2495
|
-
if (
|
|
2726
|
+
if (
|
|
2727
|
+
pgHasNul(name) ||
|
|
2728
|
+
toI32(name.length) > PG_MAX_CAP ||
|
|
2729
|
+
!this.canOpen(toI64(11) + toI64(extra) + toI64(name.length))
|
|
2730
|
+
) {
|
|
2496
2731
|
return false
|
|
2497
2732
|
}
|
|
2498
2733
|
this.bindOpened = opens
|
|
@@ -2518,11 +2753,19 @@ export class PgConnection {
|
|
|
2518
2753
|
this.noArrayOpen()
|
|
2519
2754
|
// Once the bind holds a binary parameter, every parameter owes its format code.
|
|
2520
2755
|
const owed: i32 = this.binaryParamCount > 0 ? 2 : 0
|
|
2521
|
-
|
|
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
|
+
) {
|
|
2522
2764
|
return -1
|
|
2523
2765
|
}
|
|
2524
|
-
|
|
2525
|
-
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
|
|
2526
2769
|
const at: i32 = this.outEnd
|
|
2527
2770
|
this.putI32(PG_ZERO)
|
|
2528
2771
|
return at
|
|
@@ -2536,25 +2779,38 @@ export class PgConnection {
|
|
|
2536
2779
|
*/
|
|
2537
2780
|
private startBinaryParam(size: i32): i32 {
|
|
2538
2781
|
this.noArrayOpen()
|
|
2539
|
-
const
|
|
2540
|
-
|
|
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) {
|
|
2541
2786
|
return -1
|
|
2542
2787
|
}
|
|
2543
|
-
|
|
2544
|
-
|
|
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
|
|
2545
2795
|
} else {
|
|
2546
|
-
|
|
2796
|
+
params.push(count)
|
|
2547
2797
|
}
|
|
2548
|
-
this.binaryParamCount
|
|
2549
|
-
|
|
2550
|
-
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
|
|
2551
2802
|
const at: i32 = this.outEnd
|
|
2552
2803
|
this.putI32(PG_ZERO)
|
|
2553
2804
|
return at
|
|
2554
2805
|
}
|
|
2555
2806
|
|
|
2556
2807
|
private endParam(at: i32): void {
|
|
2557
|
-
|
|
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)
|
|
2558
2814
|
}
|
|
2559
2815
|
|
|
2560
2816
|
private noArrayOpen(): void {
|
|
@@ -2602,9 +2858,14 @@ export class PgConnection {
|
|
|
2602
2858
|
if (at < 0) {
|
|
2603
2859
|
return false
|
|
2604
2860
|
}
|
|
2605
|
-
this.
|
|
2606
|
-
|
|
2607
|
-
|
|
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))
|
|
2608
2869
|
this.endParam(at)
|
|
2609
2870
|
return true
|
|
2610
2871
|
}
|
|
@@ -2615,10 +2876,12 @@ export class PgConnection {
|
|
|
2615
2876
|
* @returns True when it was written; false as every `param...` call is.
|
|
2616
2877
|
*/
|
|
2617
2878
|
paramNull(): boolean {
|
|
2618
|
-
|
|
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) {
|
|
2619
2882
|
return false
|
|
2620
2883
|
}
|
|
2621
|
-
setI32BE(this.out,
|
|
2884
|
+
setI32BE(this.out, at, -1)
|
|
2622
2885
|
return true
|
|
2623
2886
|
}
|
|
2624
2887
|
|
|
@@ -2643,8 +2906,12 @@ export class PgConnection {
|
|
|
2643
2906
|
/** `value` in decimal, `pgDecimalLength(value)` bytes. */
|
|
2644
2907
|
private putDecimal(value: i64): void {
|
|
2645
2908
|
const n: i32 = pgDecimalLength(value)
|
|
2646
|
-
this.reserve(this.outEnd + n)
|
|
2647
|
-
|
|
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
|
+
}
|
|
2648
2915
|
}
|
|
2649
2916
|
|
|
2650
2917
|
/**
|
|
@@ -2664,10 +2931,16 @@ export class PgConnection {
|
|
|
2664
2931
|
// Each loop is bounded by `values.length` as well as by the window's end,
|
|
2665
2932
|
// which the guard above keeps inside it: that names the bound the compiler
|
|
2666
2933
|
// proves `values[i]` against, where the guard alone does not reach it.
|
|
2667
|
-
|
|
2668
|
-
|
|
2669
|
-
|
|
2670
|
-
|
|
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
|
+
) {
|
|
2671
2944
|
size += toI64(pgDecimalLength(values[i]))
|
|
2672
2945
|
}
|
|
2673
2946
|
const at: i32 = this.startParam(size > toI64(PG_MAX_CAP) ? PG_MAX_CAP + 1 : toI32(size))
|
|
@@ -2698,13 +2971,20 @@ export class PgConnection {
|
|
|
2698
2971
|
*/
|
|
2699
2972
|
paramTextArray(items: string[]): boolean {
|
|
2700
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.
|
|
2701
2975
|
let size: i64 = 2
|
|
2702
2976
|
for (const item of items) {
|
|
2703
|
-
size += toI64(item.length) + 3
|
|
2704
2977
|
const n: i32 = toI32(item.length)
|
|
2978
|
+
if (size < 0 || size > 1073741823 || n < 0) {
|
|
2979
|
+
break
|
|
2980
|
+
}
|
|
2981
|
+
size += toI64(n) + toI64(3)
|
|
2705
2982
|
for (let i: i32 = 0; i < n; i++) {
|
|
2706
2983
|
const c: i32 = toI32(item.charCodeAt(i))
|
|
2707
2984
|
if (c === 34 || c === 92) {
|
|
2985
|
+
if (size < 0 || size > 1073741823) {
|
|
2986
|
+
break
|
|
2987
|
+
}
|
|
2708
2988
|
size += 1
|
|
2709
2989
|
}
|
|
2710
2990
|
}
|
|
@@ -2758,7 +3038,8 @@ export class PgConnection {
|
|
|
2758
3038
|
*/
|
|
2759
3039
|
paramInt4Array(values: i64[], start: i32, count: i32): boolean {
|
|
2760
3040
|
pgIntWindow(values, start, count)
|
|
2761
|
-
|
|
3041
|
+
// `pgIntWindow` kept the window inside `values`, so its end fits an `i32`.
|
|
3042
|
+
const end: i32 = toI32(toI64(start) + toI64(count))
|
|
2762
3043
|
for (let i: i32 = start; i >= 0 && i < end && i < toI32(values.length); i++) {
|
|
2763
3044
|
if (!pgFitsInt4(values[i])) {
|
|
2764
3045
|
panic("nish/net/postgres: paramInt4Array() with an element outside int4")
|
|
@@ -2783,27 +3064,48 @@ export class PgConnection {
|
|
|
2783
3064
|
|
|
2784
3065
|
private paramBinaryInts(values: i64[], start: i32, count: i32, oid: i32): boolean {
|
|
2785
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
|
+
}
|
|
2786
3072
|
// The header, its one dimension when there are elements, then each element's length word and bytes.
|
|
2787
|
-
|
|
2788
|
-
|
|
2789
|
-
|
|
2790
|
-
|
|
2791
|
-
|
|
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))
|
|
2792
3081
|
if (at < 0) {
|
|
2793
3082
|
return false
|
|
2794
3083
|
}
|
|
2795
|
-
this.reserve(this.outEnd + toI32(size))
|
|
3084
|
+
this.reserve(toI64(this.outEnd) + toI64(toI32(size)))
|
|
2796
3085
|
this.putArrayHeader(oid, count)
|
|
2797
|
-
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.
|
|
2798
3091
|
for (let i: i32 = start; i >= 0 && i < end && i < toI32(values.length); i++) {
|
|
2799
|
-
setI32BE(this.out, this.outEnd, width)
|
|
2800
3092
|
if (width === 4) {
|
|
2801
|
-
|
|
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
|
|
2802
3099
|
} else {
|
|
2803
|
-
|
|
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
|
|
2804
3106
|
}
|
|
2805
|
-
this.outEnd += 4 + width
|
|
2806
3107
|
}
|
|
3108
|
+
this.outEnd = p
|
|
2807
3109
|
this.endParam(at)
|
|
2808
3110
|
return true
|
|
2809
3111
|
}
|
|
@@ -2861,8 +3163,12 @@ export class PgConnection {
|
|
|
2861
3163
|
|
|
2862
3164
|
/** Whether the open array has room for an element of `size` bytes, its comma, and the `}` it owes. */
|
|
2863
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
|
+
}
|
|
2864
3170
|
const total: i64 = size + toI64(this.arrayCount > 0 ? 2 : 1)
|
|
2865
|
-
return total <= toI64(PG_MAX_CAP) && this.room(
|
|
3171
|
+
return total <= toI64(PG_MAX_CAP) && this.room(total)
|
|
2866
3172
|
}
|
|
2867
3173
|
|
|
2868
3174
|
/**
|
|
@@ -2880,12 +3186,14 @@ export class PgConnection {
|
|
|
2880
3186
|
if (start < 0 || length < 0 || length > toI32(bytes.length) - start) {
|
|
2881
3187
|
panic("nish/net/postgres: arrayElement() outside its source")
|
|
2882
3188
|
}
|
|
2883
|
-
// Both loops are bounded by `bytes.length` too, as `paramIntArray`'s are
|
|
2884
|
-
|
|
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))
|
|
2885
3192
|
let escapes: i32 = 0
|
|
2886
3193
|
for (let i: i32 = start; i < end && i < toI32(bytes.length); i++) {
|
|
2887
3194
|
const c: i32 = toI32(bytes[i])
|
|
2888
|
-
|
|
3195
|
+
// An escape per byte at most, so fewer than `length` before this one.
|
|
3196
|
+
if ((c === PG_QUOTE || c === PG_BACKSLASH) && escapes < length) {
|
|
2889
3197
|
escapes += 1
|
|
2890
3198
|
}
|
|
2891
3199
|
}
|
|
@@ -2894,10 +3202,15 @@ export class PgConnection {
|
|
|
2894
3202
|
}
|
|
2895
3203
|
this.elementComma()
|
|
2896
3204
|
this.putByte(PG_QUOTE)
|
|
2897
|
-
|
|
3205
|
+
const at: i32 = this.outEnd
|
|
3206
|
+
this.reserve(toI64(at) + toI64(length) + toI64(escapes) + toI64(1))
|
|
2898
3207
|
if (escapes === 0) {
|
|
2899
|
-
|
|
2900
|
-
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))
|
|
2901
3214
|
} else {
|
|
2902
3215
|
for (let i: i32 = start; i < end && i < toI32(bytes.length); i++) {
|
|
2903
3216
|
const c: i32 = toI32(bytes[i])
|
|
@@ -2927,10 +3240,11 @@ export class PgConnection {
|
|
|
2927
3240
|
}
|
|
2928
3241
|
|
|
2929
3242
|
private elementComma(): void {
|
|
2930
|
-
|
|
3243
|
+
const count: i32 = this.arrayCount
|
|
3244
|
+
if (count > 0) {
|
|
2931
3245
|
this.putByte(PG_COMMA)
|
|
2932
3246
|
}
|
|
2933
|
-
this.arrayCount
|
|
3247
|
+
this.arrayCount = pgCountUp(count)
|
|
2934
3248
|
}
|
|
2935
3249
|
|
|
2936
3250
|
/**
|
|
@@ -3004,18 +3318,23 @@ export class PgConnection {
|
|
|
3004
3318
|
if (width === 4 && !pgFitsInt4(value)) {
|
|
3005
3319
|
panic("nish/net/postgres: binaryArrayInt() with an element outside int4")
|
|
3006
3320
|
}
|
|
3007
|
-
if (!this.room(4 + width)) {
|
|
3321
|
+
if (!this.room(toI64(4) + toI64(width))) {
|
|
3008
3322
|
return false
|
|
3009
3323
|
}
|
|
3010
3324
|
this.putI32(width)
|
|
3011
3325
|
if (width === 4) {
|
|
3012
3326
|
this.putI32(toI32(value))
|
|
3013
3327
|
} else {
|
|
3014
|
-
this.
|
|
3015
|
-
|
|
3016
|
-
|
|
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))
|
|
3017
3336
|
}
|
|
3018
|
-
this.arrayCount
|
|
3337
|
+
this.arrayCount = pgCountUp(this.arrayCount)
|
|
3019
3338
|
return true
|
|
3020
3339
|
}
|
|
3021
3340
|
|
|
@@ -3027,12 +3346,12 @@ export class PgConnection {
|
|
|
3027
3346
|
*/
|
|
3028
3347
|
binaryArrayNull(): boolean {
|
|
3029
3348
|
this.binaryArrayOpen()
|
|
3030
|
-
if (!this.room(
|
|
3349
|
+
if (!this.room(toI64(4))) {
|
|
3031
3350
|
return false
|
|
3032
3351
|
}
|
|
3033
3352
|
this.putI32(toI32(-1))
|
|
3034
3353
|
this.arrayNulls = true
|
|
3035
|
-
this.arrayCount
|
|
3354
|
+
this.arrayCount = pgCountUp(this.arrayCount)
|
|
3036
3355
|
return true
|
|
3037
3356
|
}
|
|
3038
3357
|
|
|
@@ -3047,17 +3366,21 @@ export class PgConnection {
|
|
|
3047
3366
|
}
|
|
3048
3367
|
if (this.arrayOid !== 0) {
|
|
3049
3368
|
// The header follows the parameter's length word: ndim, the NULL flag, the OID, then the dimension.
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
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
|
+
}
|
|
3057
3380
|
}
|
|
3058
3381
|
this.arrayOid = 0
|
|
3059
3382
|
} else {
|
|
3060
|
-
this.tailRoom(
|
|
3383
|
+
this.tailRoom(toI64(1))
|
|
3061
3384
|
this.putByte(PG_RBRACE)
|
|
3062
3385
|
}
|
|
3063
3386
|
this.endParam(this.arrayAt)
|
|
@@ -3080,7 +3403,7 @@ export class PgConnection {
|
|
|
3080
3403
|
panic("nish/net/postgres: bindEnd() while an array is open")
|
|
3081
3404
|
}
|
|
3082
3405
|
const binary: boolean = this.bindBinary
|
|
3083
|
-
this.tailRoom(PG_TAIL + this.bindOwed)
|
|
3406
|
+
this.tailRoom(toI64(PG_TAIL) + toI64(this.bindOwed))
|
|
3084
3407
|
const at: i32 = this.bindAt
|
|
3085
3408
|
if (this.binaryParamCount > 0) {
|
|
3086
3409
|
this.putFormatCodes()
|
|
@@ -3114,9 +3437,11 @@ export class PgConnection {
|
|
|
3114
3437
|
return false
|
|
3115
3438
|
}
|
|
3116
3439
|
this.dropBind()
|
|
3117
|
-
|
|
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) {
|
|
3118
3443
|
this.groupOpen = false
|
|
3119
|
-
this.inFlight
|
|
3444
|
+
this.inFlight = inFlight - 1
|
|
3120
3445
|
}
|
|
3121
3446
|
return true
|
|
3122
3447
|
}
|
|
@@ -3137,18 +3462,41 @@ export class PgConnection {
|
|
|
3137
3462
|
*/
|
|
3138
3463
|
private putFormatCodes(): void {
|
|
3139
3464
|
const from: i32 = this.paramCountAt
|
|
3140
|
-
const
|
|
3141
|
-
this.
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
|
|
3146
|
-
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
|
|
3147
3471
|
}
|
|
3148
|
-
|
|
3149
|
-
|
|
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
|
|
3150
3477
|
}
|
|
3151
|
-
|
|
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
|
|
3152
3500
|
}
|
|
3153
3501
|
|
|
3154
3502
|
/**
|
|
@@ -3162,7 +3510,7 @@ export class PgConnection {
|
|
|
3162
3510
|
return false
|
|
3163
3511
|
}
|
|
3164
3512
|
this.groupOpen = false
|
|
3165
|
-
this.tailRoom(
|
|
3513
|
+
this.tailRoom(toI64(5))
|
|
3166
3514
|
const at: i32 = this.startMessage(PG_FE_SYNC)
|
|
3167
3515
|
this.endMessage(at)
|
|
3168
3516
|
return true
|
|
@@ -3177,7 +3525,7 @@ export class PgConnection {
|
|
|
3177
3525
|
if (this.bindAt >= 0) {
|
|
3178
3526
|
this.dropBind()
|
|
3179
3527
|
}
|
|
3180
|
-
this.tailRoom(
|
|
3528
|
+
this.tailRoom(toI64(5))
|
|
3181
3529
|
const at: i32 = this.startMessage(PG_FE_TERMINATE)
|
|
3182
3530
|
this.endMessage(at)
|
|
3183
3531
|
}
|
|
@@ -3223,7 +3571,8 @@ export class PgConnection {
|
|
|
3223
3571
|
return ""
|
|
3224
3572
|
}
|
|
3225
3573
|
const start: i32 = this.colStarts[c]
|
|
3226
|
-
|
|
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)))
|
|
3227
3576
|
}
|
|
3228
3577
|
|
|
3229
3578
|
/**
|
|
@@ -3292,10 +3641,20 @@ export class PgConnection {
|
|
|
3292
3641
|
*/
|
|
3293
3642
|
tagIs(word: string): boolean {
|
|
3294
3643
|
const n: i32 = toI32(word.length)
|
|
3295
|
-
|
|
3644
|
+
const start: i32 = this.tagStart
|
|
3645
|
+
const b: u8[] = this.input
|
|
3646
|
+
if (n > this.tagLength || !pgBytesAre(b, start, n, word)) {
|
|
3296
3647
|
return false
|
|
3297
3648
|
}
|
|
3298
|
-
|
|
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)) {
|
|
3655
|
+
return false
|
|
3656
|
+
}
|
|
3657
|
+
return toI32(b[at]) === 32
|
|
3299
3658
|
}
|
|
3300
3659
|
|
|
3301
3660
|
/**
|
|
@@ -3306,18 +3665,32 @@ export class PgConnection {
|
|
|
3306
3665
|
* tag has none (`BEGIN`) or it does not fit an i64.
|
|
3307
3666
|
*/
|
|
3308
3667
|
tagRows(): i64 {
|
|
3309
|
-
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
|
+
}
|
|
3310
3679
|
let p: i32 = end
|
|
3311
|
-
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
|
+
}
|
|
3312
3685
|
p--
|
|
3313
3686
|
}
|
|
3314
|
-
if (p === end || p
|
|
3687
|
+
if (p === end || p <= start || p <= 0 || p > toI32(b.length) || toI32(b[p - 1]) !== 32) {
|
|
3315
3688
|
return -1
|
|
3316
3689
|
}
|
|
3317
3690
|
// Read without touching `text.failed`, which belongs to the program's own decoding.
|
|
3318
3691
|
const failed: boolean = this.text.failed
|
|
3319
3692
|
this.text.failed = false
|
|
3320
|
-
const rows: i64 = this.text.int(
|
|
3693
|
+
const rows: i64 = this.text.int(b, p, end - p)
|
|
3321
3694
|
const overflowed: boolean = this.text.failed
|
|
3322
3695
|
this.text.failed = failed
|
|
3323
3696
|
return overflowed ? -1 : rows
|
|
@@ -3382,18 +3755,91 @@ class PgPoolSlot {
|
|
|
3382
3755
|
this.leads = new Array<i32>(ring)
|
|
3383
3756
|
}
|
|
3384
3757
|
|
|
3385
|
-
/**
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
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
|
|
3391
3784
|
}
|
|
3392
3785
|
|
|
3393
|
-
/**
|
|
3786
|
+
/**
|
|
3787
|
+
* Drops the oldest group in flight; nothing when none is.
|
|
3788
|
+
* @internal Used by `PgPool.next` in this module.
|
|
3789
|
+
*/
|
|
3394
3790
|
pop(): void {
|
|
3395
|
-
|
|
3396
|
-
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
|
|
3397
3843
|
}
|
|
3398
3844
|
}
|
|
3399
3845
|
|
|
@@ -3543,14 +3989,29 @@ export class PgPool {
|
|
|
3543
3989
|
* after 2^31-1; or `PG_POOL_FULL`.
|
|
3544
3990
|
*/
|
|
3545
3991
|
request(): i32 {
|
|
3546
|
-
const
|
|
3547
|
-
|
|
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)) {
|
|
3548
4006
|
return PG_POOL_FULL
|
|
3549
4007
|
}
|
|
3550
4008
|
const ticket: i32 = this.nextTicket
|
|
3551
|
-
this.nextTicket =
|
|
3552
|
-
|
|
3553
|
-
|
|
4009
|
+
this.nextTicket = 0
|
|
4010
|
+
if (ticket < 2147483647) {
|
|
4011
|
+
this.nextTicket = ticket + 1
|
|
4012
|
+
}
|
|
4013
|
+
queue[at] = ticket
|
|
4014
|
+
this.queueCount = count + 1
|
|
3554
4015
|
return ticket
|
|
3555
4016
|
}
|
|
3556
4017
|
|
|
@@ -3572,7 +4033,13 @@ export class PgPool {
|
|
|
3572
4033
|
inFlight(): i32 {
|
|
3573
4034
|
let n: i32 = 0
|
|
3574
4035
|
for (const slot of this.slots) {
|
|
3575
|
-
|
|
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
|
|
3576
4043
|
}
|
|
3577
4044
|
return n
|
|
3578
4045
|
}
|
|
@@ -3594,47 +4061,74 @@ export class PgPool {
|
|
|
3594
4061
|
if (this.building >= 0 || this.queueCount === 0) {
|
|
3595
4062
|
return PG_POOL_NONE
|
|
3596
4063
|
}
|
|
4064
|
+
const slots: PgPoolSlot[] = this.slots
|
|
3597
4065
|
const cap: i32 = this.config.maxInFlight
|
|
3598
4066
|
const pack: boolean = this.config.pack
|
|
3599
4067
|
let best: i32 = -1
|
|
3600
4068
|
let load: i32 = 0
|
|
3601
4069
|
let starting: i32 = 0
|
|
3602
4070
|
let idle: i32 = -1
|
|
3603
|
-
for (let s: i32 = 0; s <
|
|
3604
|
-
const slot: PgPoolSlot =
|
|
4071
|
+
for (let s: i32 = 0; s < toI32(slots.length); s++) {
|
|
4072
|
+
const slot: PgPoolSlot = slots[s]
|
|
3605
4073
|
const conn: PgConnection | null = slot.conn
|
|
3606
4074
|
if (slot.state === PG_SLOT_READY && conn !== null) {
|
|
3607
|
-
|
|
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
|
+
) {
|
|
3608
4082
|
best = s
|
|
3609
4083
|
load = conn.inFlight
|
|
3610
4084
|
}
|
|
3611
4085
|
} else if (slot.state === PG_SLOT_WANTED || slot.state === PG_SLOT_STARTING) {
|
|
3612
|
-
|
|
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
|
+
}
|
|
3613
4090
|
} else if (slot.state === PG_SLOT_IDLE && idle < 0) {
|
|
3614
4091
|
idle = s
|
|
3615
4092
|
}
|
|
3616
4093
|
}
|
|
3617
4094
|
if (best < 0) {
|
|
3618
|
-
|
|
3619
|
-
|
|
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
|
|
3620
4098
|
}
|
|
3621
4099
|
return PG_POOL_NONE
|
|
3622
4100
|
}
|
|
3623
|
-
const
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
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
|
+
}
|
|
3627
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
|
|
3628
4124
|
let leads: i32 = 0
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
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
|
|
3636
4131
|
}
|
|
3637
|
-
slot.push(ticket, leads)
|
|
3638
4132
|
this.building = best
|
|
3639
4133
|
this.ticket = ticket
|
|
3640
4134
|
return best
|
|
@@ -3653,17 +4147,24 @@ export class PgPool {
|
|
|
3653
4147
|
if (s < 0) {
|
|
3654
4148
|
return false
|
|
3655
4149
|
}
|
|
3656
|
-
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
|
+
}
|
|
3657
4156
|
const conn: PgConnection = this.connection(s)
|
|
3658
4157
|
if (conn.sync()) {
|
|
3659
4158
|
this.building = -1
|
|
3660
4159
|
return true
|
|
3661
4160
|
}
|
|
3662
|
-
|
|
4161
|
+
const count: i32 = slot.count
|
|
4162
|
+
if (conn.inFlight < count) {
|
|
3663
4163
|
// No message opened the group. A failed connection reports the request
|
|
3664
4164
|
// lost with the rest; a live one would never answer it, so it is dropped.
|
|
3665
|
-
|
|
3666
|
-
|
|
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
|
|
3667
4168
|
}
|
|
3668
4169
|
this.building = -1
|
|
3669
4170
|
}
|
|
@@ -3676,8 +4177,9 @@ export class PgPool {
|
|
|
3676
4177
|
* @returns The lowest wanted slot, or `PG_POOL_NONE` when no slot is wanted.
|
|
3677
4178
|
*/
|
|
3678
4179
|
wanted(): i32 {
|
|
3679
|
-
|
|
3680
|
-
|
|
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) {
|
|
3681
4183
|
return s
|
|
3682
4184
|
}
|
|
3683
4185
|
}
|
|
@@ -3696,7 +4198,8 @@ export class PgPool {
|
|
|
3696
4198
|
* `PgConnection` constructor takes them; a new one for every connection.
|
|
3697
4199
|
*/
|
|
3698
4200
|
connect(slot: i32, nonce: u8[]): void {
|
|
3699
|
-
|
|
4201
|
+
const s: PgPoolSlot | null = this.slotAt(slot)
|
|
4202
|
+
if (s === null || s.state !== PG_SLOT_WANTED) {
|
|
3700
4203
|
panic("nish/net/postgres: PgPool.connect on a slot that wants no connection")
|
|
3701
4204
|
}
|
|
3702
4205
|
if (this.password.gone) {
|
|
@@ -3706,7 +4209,6 @@ export class PgPool {
|
|
|
3706
4209
|
if (this.config.tls !== PG_TLS_DISABLE && carried === null) {
|
|
3707
4210
|
panic("nish/net/postgres: PgConfig.tls asks for TLS, which only PgCarrier speaks")
|
|
3708
4211
|
}
|
|
3709
|
-
const s: PgPoolSlot = this.slots[slot]
|
|
3710
4212
|
s.conn = new PgConnection(carried !== null ? carried : this.config, this.password.bytes, nonce)
|
|
3711
4213
|
s.state = PG_SLOT_STARTING
|
|
3712
4214
|
s.queued = 0
|
|
@@ -3724,7 +4226,8 @@ export class PgPool {
|
|
|
3724
4226
|
* @returns The slot's connection, owned by the pool: a later `connect` on the slot replaces it.
|
|
3725
4227
|
*/
|
|
3726
4228
|
connection(slot: i32): PgConnection {
|
|
3727
|
-
const
|
|
4229
|
+
const s: PgPoolSlot | null = this.slotAt(slot)
|
|
4230
|
+
const conn: PgConnection | null = s !== null ? s.conn : null
|
|
3728
4231
|
if (conn === null) {
|
|
3729
4232
|
panic("nish/net/postgres: PgPool.connection on a slot with no connection")
|
|
3730
4233
|
}
|
|
@@ -3748,31 +4251,44 @@ export class PgPool {
|
|
|
3748
4251
|
*/
|
|
3749
4252
|
next(slot: i32): i32 {
|
|
3750
4253
|
const conn: PgConnection = this.connection(slot)
|
|
3751
|
-
const s: PgPoolSlot = this.
|
|
3752
|
-
|
|
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) {
|
|
3753
4257
|
this.ticket = -1
|
|
3754
4258
|
return PG_NEED_MORE
|
|
3755
4259
|
}
|
|
3756
4260
|
while (s.state === PG_SLOT_STARTING || s.state === PG_SLOT_READY) {
|
|
3757
|
-
this.ticket = s.
|
|
4261
|
+
this.ticket = s.oldest()
|
|
3758
4262
|
const event: i32 = conn.next()
|
|
3759
4263
|
if (event === PG_READY) {
|
|
3760
4264
|
s.state = PG_SLOT_READY
|
|
3761
|
-
} else if (event === PG_DESCRIBED
|
|
3762
|
-
s.
|
|
3763
|
-
s.
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
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
|
+
}
|
|
3769
4283
|
} else if (event === PG_GROUP_DONE) {
|
|
3770
4284
|
s.pop()
|
|
3771
4285
|
} else if (event === PG_FAILED) {
|
|
3772
4286
|
if (s.state === PG_SLOT_STARTING && this.queueCount > 0 && !this.anyReady()) {
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
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
|
+
}
|
|
3776
4292
|
}
|
|
3777
4293
|
if (this.building === slot) {
|
|
3778
4294
|
this.building = -1
|
|
@@ -3783,7 +4299,7 @@ export class PgPool {
|
|
|
3783
4299
|
return event
|
|
3784
4300
|
}
|
|
3785
4301
|
if (s.state === PG_SLOT_DRAINING && s.count > 0) {
|
|
3786
|
-
this.ticket = s.
|
|
4302
|
+
this.ticket = s.oldest()
|
|
3787
4303
|
s.pop()
|
|
3788
4304
|
return PG_LOST
|
|
3789
4305
|
}
|
|
@@ -3792,6 +4308,31 @@ export class PgPool {
|
|
|
3792
4308
|
return PG_FAILED
|
|
3793
4309
|
}
|
|
3794
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
|
+
|
|
3795
4336
|
private anyReady(): boolean {
|
|
3796
4337
|
for (const slot of this.slots) {
|
|
3797
4338
|
if (slot.state === PG_SLOT_READY) {
|
|
@@ -4163,13 +4704,17 @@ export class PgBatcher {
|
|
|
4163
4704
|
* @internal Used by `PgCarrier.next` in std/net/postgres-carrier.ts.
|
|
4164
4705
|
*/
|
|
4165
4706
|
shed(): i32 {
|
|
4166
|
-
|
|
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)) {
|
|
4167
4712
|
return -1
|
|
4168
4713
|
}
|
|
4169
|
-
const tag: i32 =
|
|
4170
|
-
this.head =
|
|
4171
|
-
this.count
|
|
4172
|
-
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) {
|
|
4173
4718
|
this.storeEnd = 0
|
|
4174
4719
|
}
|
|
4175
4720
|
return tag
|
|
@@ -4189,12 +4734,13 @@ export class PgBatcher {
|
|
|
4189
4734
|
panic("nish/net/postgres: PgBatcher.addInt on a text batcher")
|
|
4190
4735
|
}
|
|
4191
4736
|
const q: i32 = this.slotToAdd()
|
|
4192
|
-
|
|
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)) {
|
|
4193
4740
|
return false
|
|
4194
4741
|
}
|
|
4195
|
-
|
|
4196
|
-
this.added(q, tag, pgIntHash(key), pgDecimalLength(key))
|
|
4197
|
-
return true
|
|
4742
|
+
ints[q] = key
|
|
4743
|
+
return this.added(q, tag, pgIntHash(key), pgDecimalLength(key))
|
|
4198
4744
|
}
|
|
4199
4745
|
|
|
4200
4746
|
/**
|
|
@@ -4220,80 +4766,153 @@ export class PgBatcher {
|
|
|
4220
4766
|
if (start < 0 || length < 0 || length > toI32(bytes.length) - start) {
|
|
4221
4767
|
panic("nish/net/postgres: PgBatcher.addText outside its source")
|
|
4222
4768
|
}
|
|
4223
|
-
|
|
4769
|
+
// Inside `bytes`, by the test above.
|
|
4770
|
+
const end: i32 = toI32(toI64(start) + toI64(length))
|
|
4224
4771
|
if (!pgIsText(bytes, start, end)) {
|
|
4225
4772
|
return false
|
|
4226
4773
|
}
|
|
4227
4774
|
// The element as `arrayElement` writes it: quoted, `"` and `\` escaped;
|
|
4228
4775
|
// and FNV-1a, whose unsigned multiply wraps by definition.
|
|
4229
|
-
let
|
|
4776
|
+
let escapes: i32 = 0
|
|
4230
4777
|
let h: u32 = 2166136261
|
|
4231
4778
|
for (let i: i32 = start; i < end && i < toI32(bytes.length); i++) {
|
|
4232
4779
|
const c: u8 = bytes[i]
|
|
4233
|
-
if (toI32(c) === PG_QUOTE || toI32(c) === PG_BACKSLASH) {
|
|
4234
|
-
|
|
4780
|
+
if ((toI32(c) === PG_QUOTE || toI32(c) === PG_BACKSLASH) && escapes < length) {
|
|
4781
|
+
escapes += 1
|
|
4235
4782
|
}
|
|
4236
4783
|
h = (h ^ toU32(c)) * 16777619
|
|
4237
4784
|
}
|
|
4238
|
-
|
|
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)) {
|
|
4239
4787
|
return false
|
|
4240
4788
|
}
|
|
4789
|
+
const size: i32 = toI32(toI64(length) + toI64(escapes) + toI64(2))
|
|
4241
4790
|
const q: i32 = this.slotToAdd()
|
|
4242
4791
|
if (q < 0) {
|
|
4243
4792
|
return false
|
|
4244
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)
|
|
4245
4798
|
let at: i32 = this.storeEnd
|
|
4246
|
-
|
|
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) {
|
|
4247
4805
|
// The waiting keys are moved to the front, the room the taken ones left after them.
|
|
4248
4806
|
// Refused first when even that would not make room, so a full store is not moved for nothing.
|
|
4249
|
-
const
|
|
4250
|
-
|
|
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)) {
|
|
4251
4814
|
return false
|
|
4252
4815
|
}
|
|
4253
|
-
copyBytes(
|
|
4816
|
+
copyBytes(store, 0, store, from, at - from)
|
|
4817
|
+
let w: i32 = head
|
|
4254
4818
|
for (let i: i32 = 0; i < this.count; i++) {
|
|
4255
|
-
|
|
4256
|
-
|
|
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
|
+
}
|
|
4257
4831
|
}
|
|
4258
4832
|
at -= from
|
|
4259
4833
|
this.storeEnd = at
|
|
4260
4834
|
}
|
|
4261
|
-
copyBytes(
|
|
4262
|
-
|
|
4263
|
-
this.
|
|
4264
|
-
|
|
4265
|
-
|
|
4266
|
-
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)
|
|
4267
4841
|
}
|
|
4268
4842
|
|
|
4269
4843
|
/** The ring slot the next request goes in, or -1 when the queue is full. */
|
|
4270
4844
|
private slotToAdd(): i32 {
|
|
4271
4845
|
const size: i32 = toI32(this.qTags.length)
|
|
4272
|
-
|
|
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))
|
|
4273
4855
|
}
|
|
4274
4856
|
|
|
4275
|
-
|
|
4276
|
-
|
|
4277
|
-
|
|
4278
|
-
|
|
4279
|
-
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
|
|
4280
4879
|
}
|
|
4281
4880
|
|
|
4282
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).
|
|
4283
4884
|
private sameKey(a: i32, b: i32): boolean {
|
|
4284
4885
|
if (this.keyType === PG_BATCH_INT) {
|
|
4285
|
-
|
|
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]
|
|
4286
4891
|
}
|
|
4287
|
-
const
|
|
4288
|
-
|
|
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)) {
|
|
4289
4896
|
return false
|
|
4290
4897
|
}
|
|
4291
|
-
|
|
4292
|
-
|
|
4898
|
+
if (a >= toI32(lengths.length) || b >= toI32(lengths.length)) {
|
|
4899
|
+
return false
|
|
4900
|
+
}
|
|
4901
|
+
const n: i32 = lengths[a]
|
|
4902
|
+
if (n !== lengths[b]) {
|
|
4903
|
+
return false
|
|
4904
|
+
}
|
|
4905
|
+
let x: i32 = starts[a]
|
|
4906
|
+
let y: i32 = starts[b]
|
|
4293
4907
|
for (let i: i32 = 0; i < n; i++) {
|
|
4294
|
-
if (
|
|
4908
|
+
if (x < 0 || y < 0 || x >= toI32(store.length) || y >= toI32(store.length)) {
|
|
4909
|
+
return false
|
|
4910
|
+
}
|
|
4911
|
+
if (store[x] !== store[y]) {
|
|
4295
4912
|
return false
|
|
4296
4913
|
}
|
|
4914
|
+
x += 1
|
|
4915
|
+
y += 1
|
|
4297
4916
|
}
|
|
4298
4917
|
return true
|
|
4299
4918
|
}
|
|
@@ -4321,8 +4940,9 @@ export class PgBatcher {
|
|
|
4321
4940
|
}
|
|
4322
4941
|
const before: i32 = this.count
|
|
4323
4942
|
const r: i32 = this.take(conn)
|
|
4324
|
-
|
|
4325
|
-
|
|
4943
|
+
const after: i32 = this.count
|
|
4944
|
+
if (r >= 0 && before >= 0 && after >= 0) {
|
|
4945
|
+
clock.started(before - after, nowUs)
|
|
4326
4946
|
}
|
|
4327
4947
|
return r
|
|
4328
4948
|
}
|
|
@@ -4342,108 +4962,212 @@ export class PgBatcher {
|
|
|
4342
4962
|
* `next` for this execution's events; or -1.
|
|
4343
4963
|
*/
|
|
4344
4964
|
take(conn: PgConnection): i32 {
|
|
4345
|
-
|
|
4965
|
+
const count: i32 = this.count
|
|
4966
|
+
if (count <= 0) {
|
|
4346
4967
|
return -1
|
|
4347
4968
|
}
|
|
4969
|
+
const runs: PgBatchRun[] = this.runs
|
|
4348
4970
|
let r: i32 = -1
|
|
4349
|
-
for (let i: i32 = 0; i < toI32(
|
|
4350
|
-
if (!
|
|
4971
|
+
for (let i: i32 = 0; i < toI32(runs.length); i++) {
|
|
4972
|
+
if (!runs[i].open) {
|
|
4351
4973
|
r = i
|
|
4352
4974
|
break
|
|
4353
4975
|
}
|
|
4354
4976
|
}
|
|
4355
|
-
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))) {
|
|
4356
4982
|
return -1
|
|
4357
4983
|
}
|
|
4358
4984
|
if (!conn.arrayStart()) {
|
|
4359
4985
|
conn.bindCancel()
|
|
4360
4986
|
return -1
|
|
4361
4987
|
}
|
|
4362
|
-
const
|
|
4363
|
-
|
|
4364
|
-
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
|
|
4365
5001
|
let keys: i32 = 0
|
|
4366
5002
|
let taken: i32 = 0
|
|
4367
5003
|
let bytes: i32 = 2
|
|
4368
|
-
|
|
4369
|
-
|
|
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
|
+
}
|
|
4370
5011
|
// Probe for the key: a slot holding it, or the empty slot it would take;
|
|
4371
5012
|
// -1 after `PG_BATCH_PROBES` occupied ones, when the key is bound again,
|
|
4372
5013
|
// its own ordinal and rows, and left out of the table.
|
|
4373
|
-
|
|
5014
|
+
const hash: i32 = hashes[q]
|
|
5015
|
+
let slot: i32 = hash & mask
|
|
4374
5016
|
let k: i32 = -1
|
|
4375
5017
|
let probes: i32 = 0
|
|
4376
|
-
while (
|
|
4377
|
-
|
|
4378
|
-
|
|
4379
|
-
if (this.qHashes[e] === this.qHashes[q] && this.sameKey(e, q)) {
|
|
4380
|
-
k = seen
|
|
5018
|
+
while (probes < PG_BATCH_PROBES) {
|
|
5019
|
+
if (slot < 0 || slot >= toI32(table.length)) {
|
|
5020
|
+
probes = PG_BATCH_PROBES
|
|
4381
5021
|
break
|
|
4382
5022
|
}
|
|
4383
|
-
|
|
4384
|
-
if (
|
|
4385
|
-
|
|
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
|
|
5030
|
+
break
|
|
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
|
|
4386
5035
|
break
|
|
4387
5036
|
}
|
|
5037
|
+
probes += 1
|
|
4388
5038
|
slot = (slot + 1) & mask
|
|
4389
5039
|
}
|
|
5040
|
+
if (probes >= PG_BATCH_PROBES) {
|
|
5041
|
+
slot = -1
|
|
5042
|
+
}
|
|
4390
5043
|
if (k < 0) {
|
|
4391
|
-
|
|
4392
|
-
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)) {
|
|
4393
5065
|
break
|
|
4394
5066
|
}
|
|
4395
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
|
|
4399
|
-
|
|
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
|
|
4400
5073
|
}
|
|
4401
|
-
this.keySlots[k] = slot
|
|
4402
|
-
this.keyEntries[k] = q
|
|
4403
|
-
this.keyCounts[k] = 0
|
|
4404
5074
|
}
|
|
4405
|
-
|
|
4406
|
-
|
|
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
|
|
4407
5085
|
taken += 1
|
|
5086
|
+
q += 1
|
|
5087
|
+
if (q === toI32(tags.length)) {
|
|
5088
|
+
q = 0
|
|
5089
|
+
}
|
|
4408
5090
|
}
|
|
4409
|
-
for (let k: i32 = 0; k < keys && k < toI32(
|
|
4410
|
-
const slot: i32 =
|
|
4411
|
-
if (slot >= 0) {
|
|
4412
|
-
|
|
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
|
|
4413
5095
|
}
|
|
4414
5096
|
}
|
|
4415
5097
|
if (taken === 0) {
|
|
4416
5098
|
conn.bindCancel()
|
|
4417
5099
|
return -1
|
|
4418
5100
|
}
|
|
4419
|
-
conn.arrayEnd()
|
|
4420
|
-
conn.bindEnd()
|
|
4421
5101
|
// The requests grouped by key, each key's in the order they were added.
|
|
4422
|
-
const run: PgBatchRun = this.runs[r]
|
|
4423
5102
|
if (toI32(run.tags.length) < taken) {
|
|
4424
|
-
// Grown together: `first` holds one more than `tags`, which bounds the keys too
|
|
4425
|
-
|
|
4426
|
-
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)))
|
|
4427
5107
|
if (!this.sorted) {
|
|
4428
|
-
run.had = new Array<boolean>(toI32(
|
|
5108
|
+
run.had = new Array<boolean>(toI32(takenKeys.length))
|
|
4429
5109
|
}
|
|
4430
5110
|
}
|
|
4431
|
-
|
|
4432
|
-
|
|
4433
|
-
|
|
4434
|
-
|
|
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
|
|
4435
5128
|
}
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
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
|
+
}
|
|
4440
5161
|
}
|
|
5162
|
+
conn.arrayEnd()
|
|
5163
|
+
conn.bindEnd()
|
|
4441
5164
|
run.reset(keys)
|
|
4442
|
-
|
|
4443
|
-
run.
|
|
4444
|
-
|
|
4445
|
-
this.
|
|
4446
|
-
|
|
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) {
|
|
4447
5171
|
this.storeEnd = 0
|
|
4448
5172
|
}
|
|
4449
5173
|
return run.handle
|
|
@@ -4459,15 +5183,17 @@ export class PgBatcher {
|
|
|
4459
5183
|
if (toI32(this.takenKeys.length) >= need) {
|
|
4460
5184
|
return
|
|
4461
5185
|
}
|
|
5186
|
+
// `need` is at most `maxRequests`, itself at most `PG_BATCH_MAX_REQUESTS` (65536).
|
|
4462
5187
|
let size: i32 = PG_BATCH_MIN_GROWTH
|
|
4463
|
-
while (size < need) {
|
|
5188
|
+
while (size > 0 && size < need && size < 65536) {
|
|
4464
5189
|
size *= 2
|
|
4465
5190
|
}
|
|
4466
5191
|
if (size > this.maxRequests) {
|
|
4467
5192
|
size = this.maxRequests
|
|
4468
5193
|
}
|
|
5194
|
+
// Twice `size`, rounded up to a power of two: at most 2^17.
|
|
4469
5195
|
let tableSize: i32 = 2
|
|
4470
|
-
while (tableSize
|
|
5196
|
+
while (tableSize > 0 && tableSize / 2 < size && tableSize < 131072) {
|
|
4471
5197
|
tableSize *= 2
|
|
4472
5198
|
}
|
|
4473
5199
|
this.table = new Array<i32>(tableSize)
|
|
@@ -4478,20 +5204,37 @@ export class PgBatcher {
|
|
|
4478
5204
|
}
|
|
4479
5205
|
|
|
4480
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.
|
|
4481
5208
|
private bindKey(conn: PgConnection, q: i32): boolean {
|
|
4482
5209
|
if (this.keyType === PG_BATCH_INT) {
|
|
4483
|
-
|
|
5210
|
+
const ints: i64[] = this.qInts
|
|
5211
|
+
if (q < 0 || q >= toI32(ints.length)) {
|
|
5212
|
+
return false
|
|
5213
|
+
}
|
|
5214
|
+
return conn.arrayInt(ints[q])
|
|
4484
5215
|
}
|
|
4485
|
-
|
|
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
|
|
5220
|
+
}
|
|
5221
|
+
return conn.arrayElement(this.store, starts[q], lengths[q])
|
|
4486
5222
|
}
|
|
4487
5223
|
|
|
4488
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. */
|
|
4489
5225
|
private run(h: i32): PgBatchRun | null {
|
|
4490
|
-
const
|
|
4491
|
-
|
|
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)) {
|
|
4492
5230
|
panic("nish/net/postgres: a PgBatcher run out of range")
|
|
4493
5231
|
}
|
|
4494
|
-
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]
|
|
4495
5238
|
if (run.handle !== h || !run.open) {
|
|
4496
5239
|
return null
|
|
4497
5240
|
}
|
|
@@ -4502,8 +5245,9 @@ export class PgBatcher {
|
|
|
4502
5245
|
private quietRun(h: i32, what: string): PgBatchRun | null {
|
|
4503
5246
|
const run: PgBatchRun | null = this.run(h)
|
|
4504
5247
|
if (run === null) {
|
|
4505
|
-
|
|
4506
|
-
|
|
5248
|
+
const stray: i32 = this.stray
|
|
5249
|
+
if (stray < 2147483647) {
|
|
5250
|
+
this.stray = stray + 1
|
|
4507
5251
|
}
|
|
4508
5252
|
return null
|
|
4509
5253
|
}
|
|
@@ -4536,7 +5280,11 @@ export class PgBatcher {
|
|
|
4536
5280
|
if (run.failed) {
|
|
4537
5281
|
return false
|
|
4538
5282
|
}
|
|
4539
|
-
const
|
|
5283
|
+
const columns: i32 = conn.columns
|
|
5284
|
+
let last: i32 = -1
|
|
5285
|
+
if (columns > 0) {
|
|
5286
|
+
last = columns - 1
|
|
5287
|
+
}
|
|
4540
5288
|
let ordinal: i64 = 0
|
|
4541
5289
|
let bad: boolean = last < 0 || conn.isNull(last)
|
|
4542
5290
|
if (!bad && this.binary) {
|
|
@@ -4555,17 +5303,25 @@ export class PgBatcher {
|
|
|
4555
5303
|
conn.text.failed = failed
|
|
4556
5304
|
}
|
|
4557
5305
|
// Ordinals count from 1 and, sorted, come in order: a row may name the current key again, never one before it.
|
|
4558
|
-
|
|
4559
|
-
if (
|
|
4560
|
-
|
|
4561
|
-
|
|
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
|
|
4562
5315
|
}
|
|
4563
5316
|
run.failed = true
|
|
4564
5317
|
return false
|
|
4565
5318
|
}
|
|
4566
|
-
|
|
4567
|
-
|
|
4568
|
-
|
|
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
|
|
4569
5325
|
}
|
|
4570
5326
|
return true
|
|
4571
5327
|
}
|
|
@@ -4597,14 +5353,28 @@ export class PgBatcher {
|
|
|
4597
5353
|
}
|
|
4598
5354
|
}
|
|
4599
5355
|
|
|
4600
|
-
// Names
|
|
4601
|
-
|
|
4602
|
-
|
|
4603
|
-
|
|
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) {
|
|
4604
5364
|
return false
|
|
4605
5365
|
}
|
|
4606
|
-
|
|
4607
|
-
|
|
5366
|
+
const from: i32 = first[key]
|
|
5367
|
+
const to: i32 = first[key + 1]
|
|
5368
|
+
if (from < 0 || to < 0 || to < from || a >= to - from) {
|
|
5369
|
+
return false
|
|
5370
|
+
}
|
|
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]
|
|
4608
5378
|
return true
|
|
4609
5379
|
}
|
|
4610
5380
|
|
|
@@ -4635,20 +5405,22 @@ export class PgBatcher {
|
|
|
4635
5405
|
} else if (run.ended) {
|
|
4636
5406
|
limit = run.keys
|
|
4637
5407
|
}
|
|
4638
|
-
|
|
4639
|
-
|
|
5408
|
+
let cur: i32 = run.cur
|
|
5409
|
+
while (cur < limit && cur < toI32(run.tags.length)) {
|
|
5410
|
+
if (this.emit(run, cur)) {
|
|
4640
5411
|
if (run.failed) {
|
|
4641
5412
|
return PG_BATCH_FAILED
|
|
4642
5413
|
}
|
|
4643
5414
|
return run.curRows ? PG_BATCH_DONE : PG_BATCH_MISSING
|
|
4644
5415
|
}
|
|
4645
|
-
|
|
5416
|
+
cur += 1
|
|
5417
|
+
run.cur = cur
|
|
4646
5418
|
run.at = 0
|
|
4647
5419
|
run.curRows = false
|
|
4648
5420
|
}
|
|
4649
|
-
if (run.target >= 0 &&
|
|
5421
|
+
if (run.target >= 0 && cur < toI32(run.tags.length)) {
|
|
4650
5422
|
run.curRows = true
|
|
4651
|
-
if (this.emit(run)) {
|
|
5423
|
+
if (this.emit(run, cur)) {
|
|
4652
5424
|
return PG_BATCH_ROW
|
|
4653
5425
|
}
|
|
4654
5426
|
run.target = -1
|
|
@@ -4663,11 +5435,8 @@ export class PgBatcher {
|
|
|
4663
5435
|
// `next` with `sorted` false: the row's `PG_BATCH_ROW` events at once, and
|
|
4664
5436
|
// every last event only once the execution is over, `cur` walking the keys.
|
|
4665
5437
|
private nextUnsorted(run: PgBatchRun): i32 {
|
|
4666
|
-
if (run.target >= 0
|
|
4667
|
-
|
|
4668
|
-
if (at < run.first[run.target + 1]) {
|
|
4669
|
-
run.at += 1
|
|
4670
|
-
this.tag = run.tags[at]
|
|
5438
|
+
if (run.target >= 0) {
|
|
5439
|
+
if (this.emit(run, run.target)) {
|
|
4671
5440
|
return PG_BATCH_ROW
|
|
4672
5441
|
}
|
|
4673
5442
|
run.target = -1
|
|
@@ -4676,14 +5445,17 @@ export class PgBatcher {
|
|
|
4676
5445
|
if (!run.ended) {
|
|
4677
5446
|
return PG_BATCH_NONE
|
|
4678
5447
|
}
|
|
4679
|
-
|
|
4680
|
-
|
|
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)) {
|
|
4681
5452
|
if (run.failed) {
|
|
4682
5453
|
return PG_BATCH_FAILED
|
|
4683
5454
|
}
|
|
4684
|
-
return
|
|
5455
|
+
return had[cur] ? PG_BATCH_DONE : PG_BATCH_MISSING
|
|
4685
5456
|
}
|
|
4686
|
-
|
|
5457
|
+
cur += 1
|
|
5458
|
+
run.cur = cur
|
|
4687
5459
|
run.at = 0
|
|
4688
5460
|
}
|
|
4689
5461
|
run.open = false
|
|
@@ -4837,26 +5609,46 @@ export class PgBatchClock {
|
|
|
4837
5609
|
/**
|
|
4838
5610
|
* Records an item queued at `nowUs`; false when `queueSize` are waiting. A
|
|
4839
5611
|
* time before the last one given counts as the last one, so a clock that
|
|
4840
|
-
* 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.
|
|
4841
5615
|
*
|
|
4842
5616
|
* @param nowUs The time the item was queued, in microseconds from the program's fixed point.
|
|
4843
5617
|
* @returns True when the item was recorded.
|
|
4844
5618
|
*/
|
|
4845
5619
|
arrived(nowUs: i64): boolean {
|
|
4846
|
-
const
|
|
4847
|
-
|
|
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) {
|
|
4848
5626
|
return false
|
|
4849
5627
|
}
|
|
4850
|
-
const
|
|
5628
|
+
const last: i64 = this.last
|
|
5629
|
+
const now: i64 = pgClockTime(nowUs < last ? last : nowUs)
|
|
4851
5630
|
this.last = now
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
const
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
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
|
|
4860
5652
|
}
|
|
4861
5653
|
}
|
|
4862
5654
|
return true
|
|
@@ -4893,7 +5685,7 @@ export class PgBatchClock {
|
|
|
4893
5685
|
const target: i32 = this.target()
|
|
4894
5686
|
if (this.count >= target) {
|
|
4895
5687
|
reason = target > this.minCount ? PG_CLOCK_AVERAGE : PG_CLOCK_COUNT
|
|
4896
|
-
} else if (
|
|
5688
|
+
} else if (this.waited(nowUs) >= this.maxWaitUs) {
|
|
4897
5689
|
reason = PG_CLOCK_TIMER
|
|
4898
5690
|
} else if (inFlight <= 0 && this.quiet(nowUs)) {
|
|
4899
5691
|
reason = PG_CLOCK_IDLE
|
|
@@ -4909,10 +5701,14 @@ export class PgBatchClock {
|
|
|
4909
5701
|
* @returns That time in the caller's microseconds, or -1 when none waits.
|
|
4910
5702
|
*/
|
|
4911
5703
|
deadline(): i64 {
|
|
4912
|
-
|
|
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) {
|
|
4913
5709
|
return -1
|
|
4914
5710
|
}
|
|
4915
|
-
return
|
|
5711
|
+
return toI64(toU64(oldest) + toU64(wait))
|
|
4916
5712
|
}
|
|
4917
5713
|
|
|
4918
5714
|
/**
|
|
@@ -4925,20 +5721,35 @@ export class PgBatchClock {
|
|
|
4925
5721
|
* measured to it, and is 0 when it is before the oldest item's arrival.
|
|
4926
5722
|
*/
|
|
4927
5723
|
started(size: i32, nowUs: i64): void {
|
|
4928
|
-
|
|
5724
|
+
const count: i32 = this.count
|
|
5725
|
+
if (size < 1 || size > count || size > this.maxCount) {
|
|
4929
5726
|
panic("nish/net/postgres: PgBatchClock.started with a size out of range")
|
|
4930
5727
|
}
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
|
|
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
|
|
4934
5738
|
}
|
|
4935
|
-
|
|
4936
|
-
|
|
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
|
|
4937
5744
|
const n: f64 = toF64(size)
|
|
4938
5745
|
this.average = this.lastSize === 0 ? n : this.average + this.weight * (n - this.average)
|
|
4939
5746
|
this.lastSize = size
|
|
4940
|
-
this.lastReason =
|
|
4941
|
-
|
|
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
|
+
}
|
|
4942
5753
|
this.due = PG_CLOCK_WAIT
|
|
4943
5754
|
}
|
|
4944
5755
|
|
|
@@ -4950,10 +5761,12 @@ export class PgBatchClock {
|
|
|
4950
5761
|
* @returns The count since the clock was made.
|
|
4951
5762
|
*/
|
|
4952
5763
|
startsFor(reason: i32): i64 {
|
|
4953
|
-
|
|
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)) {
|
|
4954
5767
|
panic("nish/net/postgres: PgBatchClock.startsFor with a reason out of range")
|
|
4955
5768
|
}
|
|
4956
|
-
return
|
|
5769
|
+
return starts[reason]
|
|
4957
5770
|
}
|
|
4958
5771
|
|
|
4959
5772
|
/** Whether fewer than `lightCount` items arrived in the `lightWindowUs` before `nowUs`. */
|
|
@@ -4966,10 +5779,63 @@ export class PgBatchClock {
|
|
|
4966
5779
|
return true
|
|
4967
5780
|
}
|
|
4968
5781
|
// `lightAt` is the next to write, so it holds the oldest of the last `lightCount`.
|
|
4969
|
-
|
|
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
|
|
4970
5818
|
}
|
|
4971
5819
|
}
|
|
4972
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
|
|
5835
|
+
}
|
|
5836
|
+
return t
|
|
5837
|
+
}
|
|
5838
|
+
|
|
4973
5839
|
/** `PgBatchTurns`: no slot to wake. */
|
|
4974
5840
|
export const PG_TURNS_NONE: i32 = -1
|
|
4975
5841
|
/** `PgBatchTurns.push`: the item was not recorded, `queueSize` are waiting. */
|
|
@@ -4996,8 +5862,7 @@ export const PG_TURNS_FULL: i32 = -2
|
|
|
4996
5862
|
export class PgBatchTurns {
|
|
4997
5863
|
/** The clock the turns read and record into. */
|
|
4998
5864
|
readonly clock: PgBatchClock
|
|
4999
|
-
/**
|
|
5000
|
-
private readonly size: i32
|
|
5865
|
+
/** One per slot: whether it holds a batch. */
|
|
5001
5866
|
private readonly busy: boolean[]
|
|
5002
5867
|
private running: i32 = 0
|
|
5003
5868
|
private woken: i32 = -1
|
|
@@ -5014,7 +5879,6 @@ export class PgBatchTurns {
|
|
|
5014
5879
|
panic("nish/net/postgres: a PgBatchTurns size out of range")
|
|
5015
5880
|
}
|
|
5016
5881
|
this.clock = clock
|
|
5017
|
-
this.size = size
|
|
5018
5882
|
this.busy = new Array<boolean>(size)
|
|
5019
5883
|
}
|
|
5020
5884
|
|
|
@@ -5024,8 +5888,9 @@ export class PgBatchTurns {
|
|
|
5024
5888
|
* @returns The lowest-numbered free slot, or `PG_TURNS_NONE` when every slot holds a batch.
|
|
5025
5889
|
*/
|
|
5026
5890
|
collector(): i32 {
|
|
5027
|
-
|
|
5028
|
-
|
|
5891
|
+
const busy: boolean[] = this.busy
|
|
5892
|
+
for (let i: i32 = 0; i < toI32(busy.length); i++) {
|
|
5893
|
+
if (!busy[i]) {
|
|
5029
5894
|
return i
|
|
5030
5895
|
}
|
|
5031
5896
|
}
|
|
@@ -5096,8 +5961,15 @@ export class PgBatchTurns {
|
|
|
5096
5961
|
started(slot: i32, size: i32, nowUs: i64): i32 {
|
|
5097
5962
|
this.free(slot)
|
|
5098
5963
|
this.clock.started(size, nowUs)
|
|
5099
|
-
|
|
5100
|
-
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
|
|
5101
5973
|
if (this.woken === slot) {
|
|
5102
5974
|
this.woken = -1
|
|
5103
5975
|
}
|
|
@@ -5113,11 +5985,16 @@ export class PgBatchTurns {
|
|
|
5113
5985
|
* @returns The slot to wake, or `PG_TURNS_NONE`.
|
|
5114
5986
|
*/
|
|
5115
5987
|
finished(slot: i32, nowUs: i64): i32 {
|
|
5116
|
-
|
|
5988
|
+
const busy: boolean[] = this.busy
|
|
5989
|
+
if (slot < 0 || slot >= toI32(busy.length) || !busy[slot]) {
|
|
5117
5990
|
panic("nish/net/postgres: PgBatchTurns.finished on a slot holding no batch")
|
|
5118
5991
|
}
|
|
5119
|
-
|
|
5120
|
-
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
|
+
}
|
|
5121
5998
|
return this.poll(nowUs)
|
|
5122
5999
|
}
|
|
5123
6000
|
|
|
@@ -5135,7 +6012,8 @@ export class PgBatchTurns {
|
|
|
5135
6012
|
}
|
|
5136
6013
|
|
|
5137
6014
|
private free(slot: i32): void {
|
|
5138
|
-
|
|
6015
|
+
const busy: boolean[] = this.busy
|
|
6016
|
+
if (slot < 0 || slot >= toI32(busy.length) || busy[slot]) {
|
|
5139
6017
|
panic("nish/net/postgres: PgBatchTurns slot out of range or holding a batch")
|
|
5140
6018
|
}
|
|
5141
6019
|
}
|
|
@@ -5208,10 +6086,12 @@ export const pgTarget = (host: string, port: i32): PgTarget => {
|
|
|
5208
6086
|
if (port < 1 || port > 65535) {
|
|
5209
6087
|
return pgTargetFailed(PG_TARGET_BAD_PORT)
|
|
5210
6088
|
}
|
|
5211
|
-
|
|
5212
|
-
if (
|
|
6089
|
+
// biome-ignore lint/style/useExplicitLengthCheck: `< 1` is the form that proves `charCodeAt(0)` below
|
|
6090
|
+
if (host.length < 1) {
|
|
5213
6091
|
return pgTargetFailed(PG_TARGET_BAD_HOST)
|
|
5214
6092
|
}
|
|
6093
|
+
const n: i32 = toI32(host.length)
|
|
6094
|
+
const first: i32 = toI32(host.charCodeAt(0))
|
|
5215
6095
|
for (let i: i32 = 0; i < n; i++) {
|
|
5216
6096
|
const c: i32 = toI32(host.charCodeAt(i))
|
|
5217
6097
|
if (c === 0 || c === 44) {
|
|
@@ -5221,7 +6101,6 @@ export const pgTarget = (host: string, port: i32): PgTarget => {
|
|
|
5221
6101
|
const t = new PgTarget()
|
|
5222
6102
|
t.host = host
|
|
5223
6103
|
t.port = port
|
|
5224
|
-
const first: i32 = toI32(host.charCodeAt(0))
|
|
5225
6104
|
if (first === 47 || first === 64) {
|
|
5226
6105
|
const path: string = `${host}/.s.PGSQL.${port}`
|
|
5227
6106
|
// An `@` name's NUL-free bytes after the `@` fill what a path's NUL would.
|
|
@@ -5248,7 +6127,8 @@ const pgPortValue = (text: string): i32 => {
|
|
|
5248
6127
|
let v: i32 = 0
|
|
5249
6128
|
for (let i: i32 = 0; i < n; i++) {
|
|
5250
6129
|
const c: i32 = toI32(text.charCodeAt(i))
|
|
5251
|
-
|
|
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) {
|
|
5252
6132
|
return -1
|
|
5253
6133
|
}
|
|
5254
6134
|
v = v * 10 + (c - 48)
|
|
@@ -5343,18 +6223,31 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5343
6223
|
let hostEnd: i32 = authorityEnd
|
|
5344
6224
|
let portStart: i32 = -1
|
|
5345
6225
|
let bracketed: boolean = false
|
|
5346
|
-
|
|
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
|
+
) {
|
|
5347
6235
|
let close: i32 = hostStart + 1
|
|
5348
6236
|
while (close >= 0 && close < authorityEnd && close < n && toI32(url.charCodeAt(close)) !== 93) {
|
|
5349
6237
|
close += 1
|
|
5350
6238
|
}
|
|
5351
|
-
|
|
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) {
|
|
5352
6242
|
return pgTargetFailed(PG_TARGET_BAD_HOST)
|
|
5353
6243
|
}
|
|
5354
6244
|
bracketed = true
|
|
6245
|
+
nameStart = hostStart + 1
|
|
6246
|
+
nameEnd = close
|
|
5355
6247
|
hostEnd = close + 1
|
|
5356
6248
|
if (hostEnd < authorityEnd) {
|
|
5357
|
-
|
|
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) {
|
|
5358
6251
|
return pgTargetFailed(PG_TARGET_BAD_HOST)
|
|
5359
6252
|
}
|
|
5360
6253
|
portStart = hostEnd + 1
|
|
@@ -5369,7 +6262,7 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5369
6262
|
}
|
|
5370
6263
|
}
|
|
5371
6264
|
const decodedHost: string | null = bracketed
|
|
5372
|
-
? pgDecoded(url,
|
|
6265
|
+
? pgDecoded(url, nameStart, nameEnd)
|
|
5373
6266
|
: pgDecoded(url, hostStart, hostEnd)
|
|
5374
6267
|
if (decodedHost === null) {
|
|
5375
6268
|
return pgTargetFailed(PG_TARGET_BAD_ESCAPE)
|
|
@@ -5387,7 +6280,10 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5387
6280
|
}
|
|
5388
6281
|
}
|
|
5389
6282
|
// The query: `key=value` pairs split at `&`.
|
|
5390
|
-
let i: i32 =
|
|
6283
|
+
let i: i32 = n
|
|
6284
|
+
if (queryStart < n) {
|
|
6285
|
+
i = queryStart + 1
|
|
6286
|
+
}
|
|
5391
6287
|
while (i >= 0 && i < n) {
|
|
5392
6288
|
let end: i32 = i
|
|
5393
6289
|
let eq: i32 = -1
|
|
@@ -5398,7 +6294,8 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5398
6294
|
end += 1
|
|
5399
6295
|
}
|
|
5400
6296
|
if (end > i) {
|
|
5401
|
-
|
|
6297
|
+
// `eq` is a `=` found before `end`; the test states that bound for the prover.
|
|
6298
|
+
if (eq < 0 || eq >= end) {
|
|
5402
6299
|
return pgTargetFailed(PG_TARGET_BAD_URL)
|
|
5403
6300
|
}
|
|
5404
6301
|
const key: string | null = pgDecoded(url, i, eq)
|
|
@@ -5415,6 +6312,9 @@ export const pgTargetUrl = (url: string): PgTarget => {
|
|
|
5415
6312
|
}
|
|
5416
6313
|
}
|
|
5417
6314
|
}
|
|
6315
|
+
if (end >= n) {
|
|
6316
|
+
break
|
|
6317
|
+
}
|
|
5418
6318
|
i = end + 1
|
|
5419
6319
|
}
|
|
5420
6320
|
return pgTarget(host, port)
|