@amritk/nish-x86_64-linux 0.48.0 → 0.49.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/bin/nish CHANGED
Binary file
package/docs/AI.md CHANGED
@@ -1677,7 +1677,9 @@ export const main = (): i32 => {
1677
1677
  write; a C function that does either is declared to take a `CPtr` instead.
1678
1678
  `string | null` passes `NULL` for `null`. A string with a NUL byte in it
1679
1679
  panics (`ffi-nul`) before the call, because C would read only up to it; a
1680
- literal with no NUL is not checked.
1680
+ literal with no NUL is not checked, nor is a local after
1681
+ `if (s.indexOf("\u0000") >= 0) { return … }`, which is how a
1682
+ `--deny-panics` program hands C a string it did not write.
1681
1683
 
1682
1684
  <!-- nish:ok -->
1683
1685
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/nish-x86_64-linux",
3
- "version": "0.48.0",
3
+ "version": "0.49.0",
4
4
  "description": "The nish native compiler for Linux on x86_64",
5
5
  "license": "MIT",
6
6
  "os": [
@@ -35,6 +35,23 @@ for the units and the files the cache key fingerprints (`sqliteUnits`,
35
35
  `sqliteFingerprint`), so a file added here needs no list updated.
36
36
  `tests/run.js` ("sqlite-link") checks the two hashes above against the files.
37
37
 
38
+ ## The page cache
39
+
40
+ `nish-sqlite.c` installs SQLite's own page cache, pcache1, through
41
+ `SQLITE_CONFIG_PCACHE2` from a constructor, before anything can start SQLite,
42
+ with its fetch, rekey, truncate and destroy wrapped. pcache1 keeps the largest
43
+ page number it holds, `iMaxKey`, so that the truncate after a commit visits
44
+ only the hash slots past the database's end; once that span is as wide as the
45
+ hash table, the truncate visits every slot. A B-tree split that hands its new
46
+ pages numbers out of order swaps two of them through the lock-byte page's
47
+ number (`balance_nonroot`), and that rekey left `iMaxKey` there: every later
48
+ commit's truncate then walked the whole hash table, which grows with the
49
+ cache. The wrappers record the cache's largest key while a page is parked
50
+ above it and put `iMaxKey` back when the page moves down. Where they cannot
51
+ tell, they leave pcache1's value, which is too large at worst, and too large
52
+ costs only the walk. `tests/runtime/sqlite-pcache-test.c` (the "sqlite-link"
53
+ section) checks it after every commit of random index keys.
54
+
38
55
  ## Updating
39
56
 
40
57
  1. Download the new `sqlite-amalgamation-<NNNNNNN>.zip` from
@@ -54,6 +54,103 @@
54
54
 
55
55
  #include "../nish.h"
56
56
 
57
+ /* SQLite's page cache, pcache1, with its largest key kept honest.
58
+ *
59
+ * pcache1 keeps `iMaxKey`, the largest page number it has held since the
60
+ * last truncate, so that a truncate after a commit scans only the hash
61
+ * slots from the database's end to `iMaxKey`; when that span is as wide as
62
+ * the hash table, it scans the whole table. When a B-tree split hands its
63
+ * new pages page numbers out of order, `balance_nonroot` swaps two of them
64
+ * through a free key, the lock-byte page's (262,145 at 4 KiB pages), and
65
+ * the rekey into it raises `iMaxKey` there for good. Every commit after
66
+ * such a split then scans every slot of the hash table, which grows with
67
+ * the cache: an index written at random keys splits often enough that, with
68
+ * a 64 MiB cache, that scan measured 1.5% of a write-heavy server's CPU.
69
+ *
70
+ * sqlite3.c is never edited (README.md), so the fix is here: the same
71
+ * pcache1, installed through SQLITE_CONFIG_PCACHE2 before SQLite starts,
72
+ * with three of its methods wrapped. A rekey that raises the largest key
73
+ * parks a page above every other; until the page leaves that key, every
74
+ * fetch and rekey of the cache records the largest key it brings in. When
75
+ * the parked page moves down again and still holds the largest key, the
76
+ * largest key goes back to the larger of what was recorded and the page's
77
+ * new number. Anything else - a fetch above the parked key, a truncate, the
78
+ * cache destroyed, a second cache parking meanwhile - leaves pcache1's own
79
+ * value standing, which is too large at worst, and too large costs only the
80
+ * scan. One record serves a thread: a split's three rekeys run back to back
81
+ * on one connection. */
82
+ typedef struct nishsqlite_park {
83
+ PCache1 *cache; /* the cache with a parked page, NULL when none */
84
+ unsigned int key; /* the parked page's key */
85
+ unsigned int max; /* the cache's largest key apart from the parked page */
86
+ } nishsqlite_park;
87
+
88
+ static NISH_TLS nishsqlite_park nishsqlite_parked;
89
+
90
+ static sqlite3_pcache_page *nishsqlite_pcache_fetch(sqlite3_pcache *p, unsigned int key, int create) {
91
+ sqlite3_pcache_page *page = pcache1Fetch(p, key, create);
92
+ if (nishsqlite_parked.cache == (PCache1 *)p && page != NULL && key > nishsqlite_parked.max) {
93
+ nishsqlite_parked.max = key;
94
+ }
95
+ return page;
96
+ }
97
+
98
+ static void nishsqlite_pcache_rekey(sqlite3_pcache *p, sqlite3_pcache_page *page, unsigned int from,
99
+ unsigned int to) {
100
+ PCache1 *cache = (PCache1 *)p;
101
+ unsigned int before = cache->iMaxKey;
102
+ pcache1Rekey(p, page, from, to);
103
+ if (to > before) {
104
+ nishsqlite_parked.cache = cache;
105
+ nishsqlite_parked.key = to;
106
+ nishsqlite_parked.max = before;
107
+ } else if (nishsqlite_parked.cache == cache) {
108
+ if (from == nishsqlite_parked.key) {
109
+ if (cache->iMaxKey == from) {
110
+ cache->iMaxKey = nishsqlite_parked.max > to ? nishsqlite_parked.max : to;
111
+ }
112
+ nishsqlite_parked.cache = NULL;
113
+ } else if (to > nishsqlite_parked.max) {
114
+ nishsqlite_parked.max = to;
115
+ }
116
+ }
117
+ }
118
+
119
+ static void nishsqlite_pcache_truncate(sqlite3_pcache *p, unsigned int limit) {
120
+ if (nishsqlite_parked.cache == (PCache1 *)p) {
121
+ nishsqlite_parked.cache = NULL;
122
+ }
123
+ pcache1Truncate(p, limit);
124
+ }
125
+
126
+ static void nishsqlite_pcache_destroy(sqlite3_pcache *p) {
127
+ if (nishsqlite_parked.cache == (PCache1 *)p) {
128
+ nishsqlite_parked.cache = NULL;
129
+ }
130
+ pcache1Destroy(p);
131
+ }
132
+
133
+ /* Before main, so before any thread can start SQLite: sqlite3_config is
134
+ * refused once SQLite is initialised and is not itself thread-safe. */
135
+ __attribute__((constructor)) static void nishsqlite_pcache_install(void) {
136
+ static const sqlite3_pcache_methods2 methods = {
137
+ 1,
138
+ 0,
139
+ pcache1Init,
140
+ pcache1Shutdown,
141
+ pcache1Create,
142
+ pcache1Cachesize,
143
+ pcache1Pagecount,
144
+ nishsqlite_pcache_fetch,
145
+ pcache1Unpin,
146
+ nishsqlite_pcache_rekey,
147
+ nishsqlite_pcache_truncate,
148
+ nishsqlite_pcache_destroy,
149
+ pcache1Shrink,
150
+ };
151
+ sqlite3_config(SQLITE_CONFIG_PCACHE2, &methods);
152
+ }
153
+
57
154
  typedef struct nishsqlite_slot {
58
155
  void *p; /* the connection, statement or cursor; NULL while free */
59
156
  int32_t gen; /* 1..32767, the high bits of the slot's handle */
@@ -134,6 +134,7 @@ import { TLS_RECORD_DONE, TLS_RECORD_SIGN } from "nish/net/tls/record-server"
134
134
  import { TLS_TCP_POOL_FULL } from "nish/net/tls-tcp"
135
135
  import { URL_DECODE_ALLOW_NUL, URL_DECODE_ERR_SPACE, URL_DECODE_FORM, percentDecode } from "nish/net/url"
136
136
  import { netLocalPort, pollAdd, pollCreate, pollModify, pollRemove, pollWait, udpRecvFrom } from "nish:net"
137
+ import { netRead } from "nish:net"
137
138
  import { Secret } from "nish:secret"
138
139
 
139
140
  /** The base `digits` writes a content-length in, and the byte of its digit 0. */
@@ -145,9 +146,8 @@ const HTTP_SERVER_TOO_LARGE: i32 = 413
145
146
  /** The status a request gets when no request record is free. */
146
147
  const HTTP_SERVER_UNAVAILABLE: i32 = 503
147
148
 
148
- // A request's life: free, reading its body, whole but parked behind a full
149
- // output, queued for `take`, held by the program, streaming its response,
150
- // and flushing the response's last bytes.
149
+ // A request's life: free, reading its body, whole but parked behind a full output, queued for `take`,
150
+ // held by the program, streaming its response, and flushing the response's last bytes.
151
151
  const HTTP_SERVER_FREE: i32 = 0
152
152
  const HTTP_SERVER_READING: i32 = 1
153
153
  const HTTP_SERVER_PARKED: i32 = 2
@@ -900,6 +900,22 @@ const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void =>
900
900
  }
901
901
  }
902
902
 
903
+ // The HTTP/2 connection preface (RFC 9113 §3.4), which `listenHttp1`'s `h2c` reads a connection's first bytes against.
904
+ const HTTP_SERVER_H2C_PREFACE: string = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
905
+ const HTTP_SERVER_H2C_PREFACE_SIZE: i32 = 24
906
+
907
+ /**
908
+ * Feeds `conn` the `[off, end)` of `buf` that an h2c sniff read ahead, behind
909
+ * the window's range test, spelled so the prover reads it: the sniff stops at
910
+ * its head's 24 bytes, so the test always holds. -1 should it ever fail.
911
+ */
912
+ const httpServerFeedHead = (conn: Http1Connection, buf: u8[], off: i32, end: i32): i32 => {
913
+ if (off >= 0 && off <= end && end <= toI32(buf.length)) {
914
+ return conn.feed(buf, off, end - off)
915
+ }
916
+ return toI32(-1)
917
+ }
918
+
903
919
  /**
904
920
  * One HTTP/1.1 slot's bookkeeping beside the carrier's connection in it: the
905
921
  * slot number, its carrier, its generation, its request, and whether it is
@@ -907,10 +923,14 @@ const httpServerLimit = (name: string, value: i32, low: i32, high: i32): void =>
907
923
  * `listenHttp1Tls` make one per carrier slot.
908
924
  */
909
925
  class HttpServerH1Slot {
926
+ /** The request the slot is reading or answering, null for none. */
927
+ current: HttpServerRequest | null = null
928
+ /** The slot's first bytes while `sniff`: 24 on a listener that takes h2c peers, none on any other. */
929
+ head: u8[] = []
930
+ /** When the slot was accepted, in `monotonicNanos`: while `sniff`, its `prefaceTimeout` starts here. */
931
+ since: i64 = 0
910
932
  /** The slot's index in the server's `h1Slots`, which is also its poll token. */
911
933
  slot: i32
912
- /** Whether the slot is on the TLS carrier (`listenHttp1Tls`) rather than the cleartext one. */
913
- tls: boolean
914
934
  /** The slot's number in its own carrier. */
915
935
  carrier: i32
916
936
  /** Its carrier's `outputSize`. */
@@ -919,12 +939,6 @@ class HttpServerH1Slot {
919
939
  armed: i32 = 0
920
940
  /** Bumped each time the slot closes, so an answer still owed to the connection before is dropped; it wraps. */
921
941
  gen: i32 = 0
922
- /** The request the slot is reading or answering, null for none. */
923
- current: HttpServerRequest | null = null
924
- /** Whether the carrier's connection in the slot is open. */
925
- open: boolean = false
926
- /** Whether it stopped reading for want of a free request and waits on `starved`. */
927
- starved: boolean = false
928
942
  /**
929
943
  * 0 while no `drive` of it runs, 1 while one does, 2 once a call inside it
930
944
  * (an answer screened while it reads) asked for another pass. The nested
@@ -932,6 +946,20 @@ class HttpServerH1Slot {
932
946
  * requests one write pipelines.
933
947
  */
934
948
  driving: i32 = 0
949
+ /** How many of `head` are read. */
950
+ got: i32 = 0
951
+ /** Whether the slot is on the TLS carrier (`listenHttp1Tls`) rather than the cleartext one. */
952
+ tls: boolean
953
+ /** Whether the carrier's connection in the slot is open. */
954
+ open: boolean = false
955
+ /** Whether it stopped reading for want of a free request and waits on `starved`. */
956
+ starved: boolean = false
957
+ /**
958
+ * Whether the slot is on a listener that takes h2c peers and has not yet
959
+ * read the byte that decides (`listenHttp1`'s `h2c`): `head` holds what it
960
+ * has read of the preface so far.
961
+ */
962
+ sniff: boolean = false
935
963
 
936
964
  constructor(slot: i32, tls: boolean, carrier: i32, outputSize: i32) {
937
965
  this.slot = slot
@@ -1190,18 +1218,41 @@ export class HttpServer {
1190
1218
  * its `maxHeaderBytes` and 44 bytes of request line) or more fields than
1191
1219
  * `limits.maxFields`, or when `slots`, with the TLS listener's if it came
1192
1220
  * first, pass 1048576. `config.maxBody` is set to `limits.maxBody`.
1221
+ *
1222
+ * With `h2c`, the listener also takes cleartext HTTP/2 peers by prior
1223
+ * knowledge: each connection's first bytes are read, up to 24, against the
1224
+ * HTTP/2 preface. One whose 24 bytes are the preface is served as h2c
1225
+ * with `h2c`'s caps, as `listenH2c` serves it (up to `slots` such
1226
+ * connections, beside the HTTP/1.1 ones); one whose first byte to differ
1227
+ * comes sooner, or that ends first, is served as HTTP/1.1, with what was
1228
+ * read as the start of its input. One that sends part of the preface and
1229
+ * then nothing is closed `limits.prefaceTimeout` after it was accepted.
1230
+ * There is no `Upgrade: h2c`. Without `h2c`, nothing is read ahead and
1231
+ * every connection is HTTP/1.1. Panics also, with `h2c`, as `listenH2c`
1232
+ * does on its `config` and `slots`, and when an h2c listener is already
1233
+ * configured.
1193
1234
  * @param fd the listening socket's descriptor, still the program's to close
1194
1235
  * @param config the carrier's caps and timeouts, kept by the carrier
1195
1236
  * @param slots the most connections held at once
1237
+ * @param h2c the h2c carrier's caps, to take h2c peers on this listener; null, the default, for none
1196
1238
  */
1197
- listenHttp1(fd: i32, config: Http1Config, slots: i32): void {
1239
+ listenHttp1(fd: i32, config: Http1Config, slots: i32, h2c: Http2Config | null = null): void {
1198
1240
  if (this.h1Fd >= 0) {
1199
1241
  panic("HttpServer.listenHttp1: an HTTP/1.1 listener is already configured")
1200
1242
  }
1201
1243
  this.checkHttp1("listenHttp1", config, slots)
1244
+ if (h2c !== null) {
1245
+ this.clearHttp2("listenHttp1", fd, h2c, slots)
1246
+ }
1202
1247
  this.h1 = new Http1Server(config, fd, slots)
1203
1248
  this.h1Fd = fd
1204
1249
  this.h1Base = this.h1Grow(slots, false, config.outputSize)
1250
+ if (h2c !== null) {
1251
+ const table: HttpServerH1Slot[] = this.h1Slots
1252
+ for (let k: i32 = this.h1Base; k >= 0 && k < toI32(table.length); k++) {
1253
+ table[k].head = new Array<u8>(HTTP_SERVER_H2C_PREFACE_SIZE)
1254
+ }
1255
+ }
1205
1256
  pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H1_LISTENER)
1206
1257
  }
1207
1258
 
@@ -1287,10 +1338,20 @@ export class HttpServer {
1287
1338
  * @param slots the most connections held at once
1288
1339
  */
1289
1340
  listenH2c(fd: i32, config: Http2Config, slots: i32): void {
1290
- if (this.h2cFd >= 0) {
1291
- panic("HttpServer.listenH2c: an h2c listener is already configured")
1341
+ this.clearHttp2("listenH2c", fd, config, slots)
1342
+ this.h2cFd = fd
1343
+ pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H2C_LISTENER)
1344
+ }
1345
+
1346
+ /**
1347
+ * The h2c carrier, on `fd` and with `slots` slots, for `listenH2c` or for
1348
+ * `listenHttp1`'s `h2c`: there is one at most, so a second panics.
1349
+ */
1350
+ private clearHttp2(name: string, fd: i32, config: Http2Config, slots: i32): void {
1351
+ if (this.h2c !== null) {
1352
+ panic(`HttpServer.${name}: an h2c listener is already configured`)
1292
1353
  }
1293
- this.checkHttp2("listenH2c", config, slots)
1354
+ this.checkHttp2(name, config, slots)
1294
1355
  const base: i32 = this.h2Grow(slots, this.perOf(config))
1295
1356
  this.h2cBase = base
1296
1357
  const clear: boolean[] = this.h2Clear
@@ -1298,8 +1359,6 @@ export class HttpServer {
1298
1359
  clear[v] = true
1299
1360
  }
1300
1361
  this.h2c = new Http2CleartextServer(config, fd, slots, this.prefaceTimeout)
1301
- this.h2cFd = fd
1302
- pollAdd(this.loop, fd, H1_WANT_READ, HTTP_SERVER_H2C_LISTENER)
1303
1362
  }
1304
1363
 
1305
1364
  /** The checks every HTTP/2 listener's configuration passes. */
@@ -1801,6 +1860,9 @@ export class HttpServer {
1801
1860
  s.open = true
1802
1861
  s.current = null
1803
1862
  s.armed = H1_WANT_READ
1863
+ s.sniff = toI32(s.head.length) === HTTP_SERVER_H2C_PREFACE_SIZE
1864
+ s.got = 0
1865
+ s.since = this.turnAt
1804
1866
  pollAdd(this.loop, fd, H1_WANT_READ, s.slot)
1805
1867
  }
1806
1868
 
@@ -1824,6 +1886,9 @@ export class HttpServer {
1824
1886
  }
1825
1887
  } else {
1826
1888
  const h1: Http1Server | null = this.h1
1889
+ if (h1 !== null && s.sniff && !this.h1Sniff(s, h1)) {
1890
+ return
1891
+ }
1827
1892
  if (h1 !== null) {
1828
1893
  if (read) {
1829
1894
  h1.readable(s.carrier)
@@ -1835,6 +1900,102 @@ export class HttpServer {
1835
1900
  this.drive(s)
1836
1901
  }
1837
1902
 
1903
+ /**
1904
+ * A ready socket on a listener that takes h2c peers, not yet decided:
1905
+ * reads into `s.head` up to the preface's 24 bytes. The first byte that
1906
+ * differs from the preface, the peer's end or a failed read gives the slot
1907
+ * to HTTP/1.1, with what was read as its first input; 24 that match move
1908
+ * the socket to the h2c carrier. Answers whether the slot is HTTP/1.1's
1909
+ * now, for `h1Serve` to read on; false while it waits for more, or once it
1910
+ * is h2c's or closed.
1911
+ */
1912
+ private h1Sniff(s: HttpServerH1Slot, h1: Http1Server): boolean {
1913
+ const fd: i32 = h1.fd(s.carrier)
1914
+ const head: u8[] = s.head
1915
+ const want: string = HTTP_SERVER_H2C_PREFACE
1916
+ let got: i32 = s.got
1917
+ while (got >= 0 && got < toI32(head.length)) {
1918
+ const n: i32 = netRead(fd, head, got, toI32(head.length) - got)
1919
+ if (n === HTTP_SERVER_ACCEPT_NONE) {
1920
+ return false
1921
+ }
1922
+ if (n <= 0) {
1923
+ // The peer's end or a failure: HTTP/1.1 reads it again and answers it as it does today.
1924
+ return this.h1Decided(s, h1)
1925
+ }
1926
+ // `netRead` answers at most the bytes it was asked for, so the unsigned sum is at most 24.
1927
+ const end: i32 = toI32(toU32(got) + toU32(n))
1928
+ for (let k: i32 = got; k < end && k < toI32(head.length) && k < toI32(want.length); k++) {
1929
+ if (toI32(head[k]) !== toI32(want.charCodeAt(k))) {
1930
+ s.got = end
1931
+ return this.h1Decided(s, h1)
1932
+ }
1933
+ }
1934
+ got = end
1935
+ s.got = end
1936
+ }
1937
+ this.h1ToH2c(s, h1)
1938
+ return false
1939
+ }
1940
+
1941
+ /**
1942
+ * Slot `s` is HTTP/1.1's: what it read ahead is its connection's first
1943
+ * input, stamped as `readable` stamps what it reads. False, with the slot
1944
+ * closed, should the connection not take it all.
1945
+ */
1946
+ private h1Decided(s: HttpServerH1Slot, h1: Http1Server): boolean {
1947
+ s.sniff = false
1948
+ const head: u8[] = s.head
1949
+ const got: i32 = s.got
1950
+ if (got <= 0) {
1951
+ return true
1952
+ }
1953
+ const conn: Http1Connection = h1.connection(s.carrier)
1954
+ // A fresh connection has room for a head, past the 24 bytes this hands it.
1955
+ const taken: i32 = httpServerFeedHead(conn, head, toI32(0), got)
1956
+ conn.lastActive = this.turnAt
1957
+ conn.headArrived(this.turnAt)
1958
+ if (taken !== got) {
1959
+ this.h1Close(s)
1960
+ return false
1961
+ }
1962
+ return true
1963
+ }
1964
+
1965
+ /**
1966
+ * Slot `s` read the whole preface: its socket leaves HTTP/1.1's loop and
1967
+ * carrier, unclosed, for the h2c carrier, which takes the 24 bytes as
1968
+ * `listenH2c`'s accept would have read them, and is served at once.
1969
+ */
1970
+ private h1ToH2c(s: HttpServerH1Slot, h1: Http1Server): void {
1971
+ s.sniff = false
1972
+ const h2c: Http2CleartextServer | null = this.h2c
1973
+ // `listenHttp1` makes the h2c carrier with the sniffing records, and an
1974
+ // unread slot is released, so neither close is reached; each keeps the socket owned.
1975
+ if (h2c === null) {
1976
+ this.h1Close(s)
1977
+ return
1978
+ }
1979
+ const freed: i32 = h1.release(s.carrier)
1980
+ if (freed < 0) {
1981
+ this.h1Close(s)
1982
+ return
1983
+ }
1984
+ pollRemove(this.loop, freed)
1985
+ this.h1Forget(s)
1986
+ const slot: i32 = h2c.adopt(freed, s.head)
1987
+ if (slot < 0) {
1988
+ return
1989
+ }
1990
+ const v: i64 = toI64(this.h2cBase) + toI64(slot)
1991
+ const at: i32 = v <= 2147483647 ? toI32(v) : toI32(-1)
1992
+ if (this.h2Opened(at, freed)) {
1993
+ this.h2Serve(at, H1_WANT_READ, null)
1994
+ } else {
1995
+ h2c.close(slot)
1996
+ }
1997
+ }
1998
+
1838
1999
  /** Whether `s`'s carrier holds a connection in it. */
1839
2000
  private h1Holds(s: HttpServerH1Slot): boolean {
1840
2001
  if (s.tls) {
@@ -4671,6 +4832,15 @@ export class HttpServer {
4671
4832
  */
4672
4833
  private expireAll(now: i64): void {
4673
4834
  for (const s of this.h1Slots) {
4835
+ if (
4836
+ s.open &&
4837
+ s.sniff &&
4838
+ s.got > 0 &&
4839
+ httpServerAfter(s.since, toI64(this.prefaceTimeout) * HTTP_SERVER_NANOS_PER_MS) <= now
4840
+ ) {
4841
+ // Part of the preface and then silence: the slot has held its 24 bytes long enough.
4842
+ this.h1Close(s)
4843
+ }
4674
4844
  const r: HttpServerRequest | null = s.current
4675
4845
  if (r !== null && this.late(r, now) && this.h1Holds(s)) {
4676
4846
  this.h1Close(s)
@@ -3351,6 +3351,27 @@ export class Http1Server {
3351
3351
  return true
3352
3352
  }
3353
3353
 
3354
+ /**
3355
+ * Frees `slot` without closing its socket, which is the caller's from now
3356
+ * on: for a program that moves a connection this pool has accepted but
3357
+ * not yet read to another carrier. A slot that has read, and so holds a
3358
+ * connection, is not released.
3359
+ *
3360
+ * @param slot the slot to free, as `accept` answered it
3361
+ * @returns the slot's socket, now the caller's to close; -1 for a free or unknown slot, or one that has read
3362
+ */
3363
+ release(slot: i32): i32 {
3364
+ const fds: i32[] = this.fds
3365
+ const at: i32[] = this.at
3366
+ if (slot < 0 || slot >= toI32(fds.length) || slot >= toI32(at.length) || fds[slot] < 0 || at[slot] >= 0) {
3367
+ return -1
3368
+ }
3369
+ const fd: i32 = fds[slot]
3370
+ fds[slot] = H1_FREE
3371
+ this.free.push(slot)
3372
+ return fd
3373
+ }
3374
+
3354
3375
  /**
3355
3376
  * Closes `slot`'s socket, whatever is left unsent, and frees the slot. Its
3356
3377
  * connection, when it holds one, is aborted and restarted, and is spare.
@@ -3,7 +3,9 @@
3
3
  * (h2c, RFC 9113 §3.3): a pool of `Http2Connection`s, one per accepted
4
4
  * socket, for a program that owns its readiness loop. The client starts with
5
5
  * the 24-byte connection preface; there is no `Upgrade: h2c` (RFC 9113 §3.1
6
- * deprecates it) and no fallback to HTTP/1.1 on the same port.
6
+ * deprecates it), and the pool serves no HTTP/1.1 on its own port. One port
7
+ * for both is `nish/net/http-server`'s `listenHttp1` with `h2c`: the façade
8
+ * reads the preface itself and hands a matching socket over with `adopt`.
7
9
  *
8
10
  * **No confidentiality, no integrity.** Anyone on the path reads and edits
9
11
  * the traffic. Serve h2c only upstream of a TLS-terminating proxy, on
@@ -339,6 +341,33 @@ export class Http2CleartextServer {
339
341
  return s.at
340
342
  }
341
343
 
344
+ /**
345
+ * Takes `fd`, a connection the program accepted and has read the whole
346
+ * 24-byte preface from, into a free slot, as `accept` does, and feeds
347
+ * `preface` to its connection: the slot starts past its preface, so
348
+ * `expire` never closes it for one. The bytes are the connection's to
349
+ * check, which refuses any other 24 as a bad preface (RFC 9113 §3.4).
350
+ *
351
+ * @param fd The socket, which is this pool's from now on, whatever it answers.
352
+ * @param preface The 24 bytes read from `fd`, in order; a shorter array panics, as `feed` does.
353
+ * @returns The slot, 0 or more, that now holds the connection; or `H2C_POOL_FULL` when every slot is busy
354
+ * (the socket is closed with nothing sent).
355
+ */
356
+ adopt(fd: i32, preface: u8[]): i32 {
357
+ const s: H2cSlot | null = this.take()
358
+ if (s === null) {
359
+ netClose(fd)
360
+ return H2C_POOL_FULL
361
+ }
362
+ s.conn.restart()
363
+ s.fd = fd
364
+ s.got = H2C_PREFACE_SIZE
365
+ s.since = monotonicNanos()
366
+ s.ended = false
367
+ s.conn.feed(preface, H2C_ZERO, H2C_PREFACE_SIZE)
368
+ return s.at
369
+ }
370
+
342
371
  /** A free slot for `accept`: a closed one first, then the next never used; null when every slot is busy. */
343
372
  private take(): H2cSlot | null {
344
373
  const free: H2cSlot[] = this.free
package/std/sync/shm.ts CHANGED
@@ -175,6 +175,9 @@ const shmDone = (rc: i32): Result<void, ShmError> => {
175
175
  * Whether `name` keeps `createRegion`'s rules: 1 to 30 of
176
176
  * `A-Z a-z 0-9 . _ -`, not starting with `.`. The runtime checks them too;
177
177
  * checking here answers a NUL byte as `SHM_INVALID` before C would read it.
178
+ * Each caller also asks `name.indexOf("\u0000") < 0` itself, which is what
179
+ * proves the name NUL-free at the `declare function` call, so no `ffi-nul`
180
+ * panic is left there.
178
181
  */
179
182
  const shmNameOk = (name: string): boolean => {
180
183
  const n: i32 = toI32(name.length)
@@ -211,7 +214,7 @@ const shmNameOk = (name: string): boolean => {
211
214
  * system gave
212
215
  */
213
216
  export const createRegion = (name: string, bytes: i32): Result<SharedRegion, ShmError> => {
214
- if (!shmNameOk(name)) {
217
+ if (name.indexOf("\u0000") >= 0 || !shmNameOk(name)) {
215
218
  return Err(new ShmError(SHM_INVALID))
216
219
  }
217
220
  const h: i32 = nishshm_create(name, bytes)
@@ -235,7 +238,7 @@ export const createRegion = (name: string, bytes: i32): Result<SharedRegion, Shm
235
238
  * system gave
236
239
  */
237
240
  export const openRegion = (name: string): Result<SharedRegion, ShmError> => {
238
- if (!shmNameOk(name)) {
241
+ if (name.indexOf("\u0000") >= 0 || !shmNameOk(name)) {
239
242
  return Err(new ShmError(SHM_INVALID))
240
243
  }
241
244
  const h: i32 = nishshm_open(name)
@@ -254,7 +257,7 @@ export const openRegion = (name: string): Result<SharedRegion, ShmError> => {
254
257
  * `SHM_INVALID`, code 95 on wasi, or the errno the system gave
255
258
  */
256
259
  export const unlinkRegion = (name: string): Result<void, ShmError> => {
257
- if (!shmNameOk(name)) {
260
+ if (name.indexOf("\u0000") >= 0 || !shmNameOk(name)) {
258
261
  return Err(new ShmError(SHM_INVALID))
259
262
  }
260
263
  return shmDone(nishshm_unlink(name))