Skip to content

Commit b30efdb

Browse files
committed
fix(v18): pin libpg_query with checked scan allocations; report its out of memory as out of WASM memory
1 parent ab4aedb commit b30efdb

5 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎templates/full/index.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,20 @@ function ptrToString(ptr: number): string {
245245
return wasmModule.UTF8ToString(ptr);
246246
}
247247

248+
const NO_RESULT_OUT_OF_MEMORY = 'libpg_query returned no result: out of WASM memory (heap limit is WASM_MAXIMUM_MEMORY, 1 GiB); parse large inputs in smaller pieces';
249+
248250
function readDetailedResult(resultPtr: number): string {
249251
if (!resultPtr) {
250-
throw new Error('libpg_query returned no result: out of WASM memory (heap limit is WASM_MAXIMUM_MEMORY, 1 GiB); parse large inputs in smaller pieces');
252+
throw new Error(NO_RESULT_OUT_OF_MEMORY);
251253
}
252254
const ptrAt = (offset: number) => wasmModule.getValue(resultPtr + offset, 'i32');
253255
const str = (ptr: number) => (ptr ? wasmModule.UTF8ToString(ptr) : undefined);
254256
if (ptrAt(0)) {
255257
const message = str(ptrAt(4)) ?? 'Unknown error';
258+
// libpg_query reports a failed malloc as a plain "out of memory" error.
259+
if (message === 'out of memory') {
260+
throw new Error(NO_RESULT_OUT_OF_MEMORY);
261+
}
256262
const lineno = ptrAt(16);
257263
const cursorpos = ptrAt(20);
258264
throw new SqlError(message, {

‎versions/18/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ WASM_STACK_SIZE ?= 33554432
1919
# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates.
2020
LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git
2121
LIBPG_QUERY_TAG := 18-constructive
22-
LIBPG_QUERY_COMMIT := b0f5f331dc50d69597ec610de7b21afa47a66808
22+
LIBPG_QUERY_COMMIT := 8076f09aecc655a717e4a038e182a9dd41cc3eb8
2323

2424
CACHE_DIR := .cache
2525

‎versions/18/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"distTag": "pg18",
1616
"libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git",
1717
"libpgQueryTag": "18-constructive",
18-
"libpgQueryCommit": "b0f5f331dc50d69597ec610de7b21afa47a66808",
18+
"libpgQueryCommit": "8076f09aecc655a717e4a038e182a9dd41cc3eb8",
1919
"fullApi": true
2020
},
2121
"files": [

‎versions/18/src/index.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,20 @@ function ptrToString(ptr: number): string {
252252
return wasmModule.UTF8ToString(ptr);
253253
}
254254

255+
const NO_RESULT_OUT_OF_MEMORY = 'libpg_query returned no result: out of WASM memory (heap limit is WASM_MAXIMUM_MEMORY, 1 GiB); parse large inputs in smaller pieces';
256+
255257
function readDetailedResult(resultPtr: number): string {
256258
if (!resultPtr) {
257-
throw new Error('libpg_query returned no result: out of WASM memory (heap limit is WASM_MAXIMUM_MEMORY, 1 GiB); parse large inputs in smaller pieces');
259+
throw new Error(NO_RESULT_OUT_OF_MEMORY);
258260
}
259261
const ptrAt = (offset: number) => wasmModule.getValue(resultPtr + offset, 'i32');
260262
const str = (ptr: number) => (ptr ? wasmModule.UTF8ToString(ptr) : undefined);
261263
if (ptrAt(0)) {
262264
const message = str(ptrAt(4)) ?? 'Unknown error';
265+
// libpg_query reports a failed malloc as a plain "out of memory" error.
266+
if (message === 'out of memory') {
267+
throw new Error(NO_RESULT_OUT_OF_MEMORY);
268+
}
263269
const lineno = ptrAt(16);
264270
const cursorpos = ptrAt(20);
265271
throw new SqlError(message, {

‎versions/18/test/scan.test.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const query = require("../");
22
const { describe, it, before, after, beforeEach, afterEach } = require('node:test');
33
const assert = require('node:assert/strict');
4+
const { execFileSync } = require('node:child_process');
45

56
describe("Query Scanning", () => {
67
before(async () => {
@@ -364,6 +365,34 @@ describe("scan input limits", () => {
364365
assert.equal(query.scanSync("SELECT 1;\n".repeat(1000000)).tokens.length, 3000000);
365366
assert.throws(() => query.scanSync("SELECT 1;\n".repeat(2000000)), /out of WASM memory/);
366367
});
368+
369+
// About 50 MB (18.6M tokens) used to make libpg_query write the token protobuf
370+
// through a NULL pointer, abort, and break the next sync call. Runs in a child
371+
// process because the abort corrupts the instance.
372+
it("throws out of WASM memory at 50 MB, then sync and async calls work", () => {
373+
const script = `const query = require(${JSON.stringify(require.resolve("../"))});
374+
(async () => {
375+
await query.parse("SELECT 1");
376+
let message = "no error";
377+
try {
378+
query.scanSync("SELECT a, b FROM t WHERE c = 1;\\n".repeat(1690000));
379+
} catch (error) {
380+
message = error.message;
381+
}
382+
const sync = query.scanSync("SELECT 1").tokens.length;
383+
const async = (await query.scan("SELECT 1")).tokens.length;
384+
process.stdout.write("\\nresult=" + JSON.stringify({ message, sync, async }));
385+
})();`;
386+
const output = execFileSync(process.execPath, ["-e", script], {
387+
encoding: "utf8",
388+
stdio: ["ignore", "pipe", "ignore"],
389+
maxBuffer: 256 * 1024 * 1024,
390+
});
391+
const result = JSON.parse(output.match(/^result=(.*)$/m)[1]);
392+
assert.match(result.message, /out of WASM memory/);
393+
assert.equal(result.sync, 2);
394+
assert.equal(result.async, 2);
395+
});
367396
});
368397

369398
describe("scan control characters", () => {

0 commit comments

Comments
 (0)