Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
94b9746
test(wasm): parse 18.1.5's depths and report V8 stack overflows as st…
pyramation Nov 26, 2026
0699b7b
test(wasm): check parsePlPgSQL's function list and drop a fingerprint…
pyramation Nov 26, 2026
22fd760
fix(wasm): report host stack overflows as stack depth errors and use …
pyramation Nov 26, 2026
09c299f
test(wasm): keep the stack depth errors in the heap-release test
pyramation Nov 26, 2026
cbdb2a1
fix(wasm): format the overflow's stack so a kept error doesn't pin th…
pyramation Nov 26, 2026
02aca43
test(release-diff): allow 18.2.7's capped depth limits; lower SQL dep…
pyramation Nov 26, 2026
ffc70bd
test(wasm): normalize deep nesting on an instance that replaced an ov…
pyramation Nov 26, 2026
63fc46c
fix(wasm): pin libpg_query with normalize off the per-level PG_TRY path
pyramation Nov 26, 2026
03f49a2
chore(v18): pin libpg_query to 18-constructive f9f1b67 (constructive-…
pyramation Nov 26, 2026
550ad9e
Merge #213 branch
pyramation Nov 26, 2026
9addfe5
Merge main; test fingerprint and normalize depth on V8's optimizing c…
pyramation Nov 26, 2026
c59f3b6
Pin libpg_query to the fingerprint frame fix
pyramation Nov 26, 2026
f1c8786
Merge remote-tracking branch 'origin/main' into devin/1795672086-norm…
pyramation Nov 26, 2026
577b528
test(v18): run the fingerprint and normalize depth tests on Node 20 a…
pyramation Nov 26, 2026
fcb0d4b
chore(v18): pin libpg_query to the fingerprint fix merged with 18-con…
pyramation Nov 26, 2026
914744d
Merge remote-tracking branch 'origin/main' into devin/1795672086-norm…
pyramation Nov 26, 2026
28f947a
chore(v18): pin libpg_query to 18-constructive with both depth fixes
pyramation Nov 26, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jobs:
- { name: 'v16', path: 'versions/16', version: '16' }
- { name: 'v17', path: 'versions/17', version: '17' }
- { name: 'v18', path: 'versions/18', version: '18' }
# Node 20 cannot run the fingerprint and normalize depth tests.
include:
- { os: ubuntu-latest, node: '22.x', package: { name: 'v18', path: 'versions/18', version: '18' } }
fail-fast: false
Expand Down
2 changes: 1 addition & 1 deletion versions/18/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ WASM_STACK_SIZE ?= 33554432
# byte-for-byte. Set in package.json `x-publish` and applied by copy:templates.
LIBPG_QUERY_REPO := https://github.com/constructive-io/libpg_query.git
LIBPG_QUERY_TAG := 18-constructive
LIBPG_QUERY_COMMIT := f9f1b676f629a831bd2c5f7b34a2dc41d9cdf967
LIBPG_QUERY_COMMIT := 91d254f764b270a02f8445d3b49030eb4fda0302

CACHE_DIR := .cache

Expand Down
2 changes: 1 addition & 1 deletion versions/18/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"distTag": "pg18",
"libpgQueryRepo": "https://github.com/constructive-io/libpg_query.git",
"libpgQueryTag": "18-constructive",
"libpgQueryCommit": "f9f1b676f629a831bd2c5f7b34a2dc41d9cdf967",
"libpgQueryCommit": "91d254f764b270a02f8445d3b49030eb4fda0302",
"fullApi": true
},
"files": [
Expand Down
10 changes: 10 additions & 0 deletions versions/18/release-diff.allow.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
"published": "RangeError: Maximum call stack size exceeded",
"candidate": "stack depth limit exceeded",
"reason": "A host stack overflow inside the parser or JSON.parse is now reported as a stack depth error instead of a raw RangeError"
},
{
"id": [
"edge:*:normalize",
"edge:*:fingerprint"
],
"baseline": "18.2.7",
"published": "RangeError: Maximum call stack size exceeded",
"candidate": "ok",
"reason": "Normalize no longer goes through a JavaScript call per nesting level, and the fingerprint walk no longer has one very large frame per level, so both handle deeper SQL before V8's stack runs out"
}
]
}
17 changes: 6 additions & 11 deletions versions/18/test/limits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ const list = (item, separator) => (n) => Array.from({ length: n }, (_, i) => ite
// fails here whatever the last release did. Raise a number when the build
// gains depth; lower one only for a change the release notes describe.
const SQL = {
'chained +': [(n) => 'SELECT 1' + '+1'.repeat(n), { parse: 4000, fingerprint: 5000, normalize: 500 }],
'chained ||': [(n) => "SELECT 'a'" + " || 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 500 }],
'chained +': [(n) => 'SELECT 1' + '+1'.repeat(n), { parse: 4000, fingerprint: 5000, normalize: 1500 }],
'chained ||': [(n) => "SELECT 'a'" + " || 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1500 }],
'chained AND': [(n) => 'SELECT 1 WHERE ' + list(() => 'a = 1', ' AND ')(n), { parse: 5000, fingerprint: 5000, normalize: 5000 }],
'chained OR': [(n) => 'SELECT 1 WHERE ' + list(() => 'a = 1', ' OR ')(n), { parse: 5000, fingerprint: 5000, normalize: 5000 }],
'chained casts': [(n) => 'SELECT 1' + '::int'.repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1000 }],
'chained ->': [(n) => 'SELECT j' + " -> 'a'".repeat(n), { parse: 2500, fingerprint: 5000, normalize: 1000 }],
'chained unary minus': [(n) => 'SELECT ' + '- '.repeat(n) + 'a', { parse: 2500, fingerprint: 4000, normalize: 1000 }],
'chained NOT': [(n) => 'SELECT ' + 'NOT '.repeat(n) + 'true', { parse: 2500, fingerprint: 4000, normalize: 500 }],
'nested parentheses': [(n) => 'SELECT ' + nest('(', '1', ')')(n), { parse: 4000, fingerprint: 4000, normalize: 4000 }],
'nested function calls': [(n) => 'SELECT ' + nest('f(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested COALESCE': [(n) => 'SELECT ' + nest('COALESCE(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested function calls': [(n) => 'SELECT ' + nest('f(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 750 }],
'nested COALESCE': [(n) => 'SELECT ' + nest('COALESCE(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 750 }],
'nested ROW': [(n) => 'SELECT ' + nest('ROW(', '1', ')')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested arrays': [(n) => 'SELECT ' + nest('ARRAY[', '1', ']')(n), { parse: 2000, fingerprint: 2000, normalize: 500 }],
'nested CASE': [(n) => 'SELECT ' + nest('CASE WHEN a THEN ', '1', ' END')(n), { parse: 1000, fingerprint: 1000, normalize: 1000 }],
Expand All @@ -42,11 +42,6 @@ const PLPGSQL = {
const plpgsql = (body) =>
`CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ DECLARE x int; BEGIN ${body} END $$`;

// Node 20 runs out of stack in fingerprint and normalize at a few dozen
// levels once V8 has optimized them, on every release including 18.1.5.
const node20 = Number(process.versions.node.split('.')[0]) < 22 &&
'Node 20 overflows its stack in optimized fingerprint and normalize code';

describe('Depth every exported function handles', { timeout: 300000 }, () => {
before(async () => {
await lib.loadModule();
Expand All @@ -59,13 +54,13 @@ describe('Depth every exported function handles', { timeout: 300000 }, () => {
assert.equal((await lib.parse(sql)).stmts.length, 1);
});

it(`fingerprint and fingerprintSync: ${shape}, ${depth.fingerprint} deep`, { skip: node20 }, async () => {
it(`fingerprint and fingerprintSync: ${shape}, ${depth.fingerprint} deep`, async () => {
const sql = make(depth.fingerprint);
assert.match(lib.fingerprintSync(sql), /^[0-9a-f]{16}$/);
assert.match(await lib.fingerprint(sql), /^[0-9a-f]{16}$/);
});

it(`normalize and normalizeSync: ${shape}, ${depth.normalize} deep`, { skip: node20 }, async () => {
it(`normalize and normalizeSync: ${shape}, ${depth.normalize} deep`, async () => {
const sql = make(depth.normalize);
assert.ok(lib.normalizeSync(sql).length > 0);
assert.ok((await lib.normalize(sql)).length > 0);
Expand Down
35 changes: 34 additions & 1 deletion versions/18/test/wasm-recovery.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,39 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
}
});

// Each nesting level of normalize used to cross from WASM into JS and back,
// which V8 only makes cheap once an instance is warm: a fresh instance, like
// the one that replaces an overflowed one, stopped at about 770 terms.
it('normalizes deep nesting on an instance that just replaced an overflowed one', async () => {
await loadModule();
const chained = 'SELECT 1' + '+1'.repeat(2000);
const normalized = 'SELECT $1' + Array.from({ length: 2000 }, (_, i) => `+$${i + 2}`).join('');
for (let i = 0; i < 3; i++) {
assert.throws(() => lib.normalizeSync(DEEP), /stack depth limit exceeded/);
assert.equal(lib.normalizeSync(chained), normalized);
}
});

// --no-liftoff runs the module on V8's optimizing compiler from the start,
// which is where a long-running process ends up. On Node 20 its frame for
// the fingerprint walk was so large that 30 chained terms overflowed.
it('fingerprints and normalizes nesting on V8\'s optimizing compiler', () => {
const script = `
const lib = require(${JSON.stringify(require.resolve('../wasm/index.cjs'))});
const nest = (open, n) => 'SELECT ' + open.repeat(n) + '1' + ')'.repeat(n);
lib.loadModule().then(() => {
const chained = 'SELECT 1' + '+1'.repeat(1000);
lib.fingerprintSync(chained);
lib.fingerprintSync(nest('f(', 500));
lib.fingerprintSync(nest('(SELECT ', 500));
lib.normalizeSync(chained);
lib.normalizeSync(nest('f(', 500));
lib.normalizeSync(nest('(SELECT ', 250));
});
`;
execFileSync(process.execPath, ['--no-liftoff', '-e', script], { stdio: 'pipe' });
});

// V8's native stack can run out inside a WASM call before libpg_query's own
// check, e.g. in normalize, whose frames are larger there.
it('reports a stack depth error, not a RangeError, for nesting too deep to parse', async () => {
Expand All @@ -64,7 +97,7 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => {
['parseSync', DEEP],
['parseSync', subqueries(3000)],
['normalizeSync', DEEP],
['normalizeSync', subqueries(1000)],
['normalizeSync', subqueries(3000)],
];
for (const [name, sql] of tooDeep) {
assert.throws(() => lib[name](sql), (error) => {
Expand Down
Loading