diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 463cb4f..7633227 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/versions/18/Makefile b/versions/18/Makefile index 1674ee4..ddd63c7 100644 --- a/versions/18/Makefile +++ b/versions/18/Makefile @@ -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 diff --git a/versions/18/package.json b/versions/18/package.json index 01e03a6..ff3a87b 100644 --- a/versions/18/package.json +++ b/versions/18/package.json @@ -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": [ diff --git a/versions/18/release-diff.allow.json b/versions/18/release-diff.allow.json index 20d8b73..4e39b06 100644 --- a/versions/18/release-diff.allow.json +++ b/versions/18/release-diff.allow.json @@ -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" } ] } diff --git a/versions/18/test/limits.test.js b/versions/18/test/limits.test.js index 38fa793..908fdef 100644 --- a/versions/18/test/limits.test.js +++ b/versions/18/test/limits.test.js @@ -10,8 +10,8 @@ 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 }], @@ -19,8 +19,8 @@ const SQL = { '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 }], @@ -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(); @@ -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); diff --git a/versions/18/test/wasm-recovery.test.js b/versions/18/test/wasm-recovery.test.js index ce9470f..b6cd376 100644 --- a/versions/18/test/wasm-recovery.test.js +++ b/versions/18/test/wasm-recovery.test.js @@ -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 () => { @@ -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) => {