Repository navigation
fix(wasm): parse 18.1.5's depths and report V8 stack overflows as stack depth errors #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
94b9746
0699b7b
22fd760
09c299f
cbdb2a1
02aca43
03f49a2
3374529
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,45 @@ | ||
| { | ||
| "allow": [] | ||
| "allow": [ | ||
| { | ||
| "id": [ | ||
| "edge:*:parse", | ||
| "edge:*:normalize", | ||
| "edge:*:fingerprint" | ||
| ], | ||
| "baseline": "18.2.7", | ||
| "published": "stack depth limit exceeded", | ||
| "candidate": "ok", | ||
| "reason": "18.2.7 capped the stack depth check at 512 kB of the WASM stack; the check now uses the whole stack, so deep SQL parses again as in 18.1.5" | ||
| }, | ||
| { | ||
| "id": [ | ||
| "edge:*:parse", | ||
| "edge:*:scan" | ||
| ], | ||
| "baseline": "18.2.7", | ||
| "published": "stack depth limit exceeded", | ||
| "candidate": "RangeError: Maximum call stack size exceeded", | ||
| "reason": "Same change: the candidate parses these now, and the RangeError is this script's JSON.stringify overflowing on the very deep parse tree" | ||
| }, | ||
| { | ||
| "id": "edge:plpgsql-*:parsePlPgSQL", | ||
| "baseline": "18.2.7", | ||
| "published": "Error: stack depth limit exceeded", | ||
| "candidate": "ok", | ||
| "reason": "The 512 kB cap stopped PL/pgSQL at about 350 nested blocks; without it they parse again as in 18.1.5" | ||
| }, | ||
| { | ||
| "id": [ | ||
| "edge:*:parse", | ||
| "edge:*:normalize", | ||
| "edge:*:fingerprint", | ||
| "edge:*:scan", | ||
| "edge:plpgsql-*:parsePlPgSQL" | ||
| ], | ||
| "baseline": "18.2.7", | ||
| "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" | ||
| } | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,47 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { | |
| } | ||
| }); | ||
|
|
||
| // 18.1.5 parsed these; 18.2.7's 512kB WASM limit rejected them. | ||
| it('parses nesting that 18.1.5 parsed', async () => { | ||
| await loadModule(); | ||
| // Nested subqueries and function calls overflow V8's stack on macOS near the old limit. | ||
| const chained = 'SELECT 1' + '+1'.repeat(4000); | ||
| assert.doesNotThrow(() => assert.equal(parseSync(chained).stmts.length, 1), 'chained operators'); | ||
| const blocks = { | ||
| 'nested IF blocks': ['IF true THEN ', 'NULL;', ' END IF;'], | ||
| 'nested BEGIN blocks': ['BEGIN ', 'NULL;', ' END;'], | ||
| 'nested loops': ['LOOP ', 'EXIT;', ' END LOOP;'], | ||
| }; | ||
| for (const [name, [open, body, close]] of Object.entries(blocks)) { | ||
| const sql = 'CREATE FUNCTION f() RETURNS void LANGUAGE plpgsql AS $$ BEGIN ' + | ||
| open.repeat(1000) + body + close.repeat(1000) + ' END $$'; | ||
| assert.doesNotThrow(() => assert.equal(lib.parsePlPgSQLSync(sql).plpgsql_funcs.length, 1), name); | ||
| } | ||
| }); | ||
|
|
||
| // 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 () => { | ||
| await loadModule(); | ||
| const subqueries = (n) => 'SELECT ' + '(SELECT '.repeat(n) + '1' + ')'.repeat(n); | ||
| const tooDeep = [ | ||
| ['parseSync', DEEP], | ||
| ['parseSync', subqueries(3000)], | ||
| ['normalizeSync', DEEP], | ||
| ['normalizeSync', subqueries(1000)], | ||
| ]; | ||
| for (const [name, sql] of tooDeep) { | ||
| assert.throws(() => lib[name](sql), (error) => { | ||
| assert.ok(error instanceof lib.SqlError, `${name}: ${error}`); | ||
| assert.equal(error.message, 'stack depth limit exceeded'); | ||
| return true; | ||
| }); | ||
| assert.equal(parseSync('SELECT 1').stmts.length, 1); | ||
| } | ||
| await assert.rejects(lib.normalize(DEEP), lib.SqlError); | ||
| assert.equal((await parse('SELECT 1')).stmts.length, 1); | ||
| }); | ||
|
|
||
| it('frees replaced WASM heaps even when callers keep the errors', () => { | ||
| const script = `const lib = require(${JSON.stringify(require.resolve('../wasm/index.cjs'))}); | ||
| (async () => { | ||
|
|
@@ -53,8 +94,9 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { | |
| gc(); gc(); | ||
| const base = process.memoryUsage().external; | ||
| const deep = 'SELECT 1' + '+1'.repeat(100000); | ||
| const kept = []; | ||
| for (let i = 0; i < 30; i++) { | ||
| try { lib.parseSync(deep); } catch {} | ||
| try { lib.parseSync(deep); } catch (error) { kept.push(error); } | ||
|
Comment on lines
+97
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the retained errors after the final garbage collection. The loop stores errors in After the final
|
||
| await lib.loadModule(); | ||
| } | ||
| const huge = 'SELECT 1;\\n'.repeat(5000000); | ||
|
|
@@ -206,7 +248,7 @@ describe('Recovery after the WASM parser throws', { timeout: 120000 }, () => { | |
| }); | ||
|
|
||
| // Every entry point, with the next WASM call made to throw: a RangeError | ||
| // stands for a stack overflow, a non-Error object for Emscripten's ExitStatus. | ||
| // stands for a trap or a stack overflow, a non-Error object for Emscripten's ExitStatus. | ||
| describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => { | ||
| const PLPGSQL = 'CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END $$ LANGUAGE plpgsql'; | ||
| const entryPoints = [ | ||
|
|
@@ -281,7 +323,7 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => | |
| await isolated.loadModule(); | ||
| const loadsBefore = loads; | ||
| for (let i = 0; i < 60; i++) { | ||
| throwNext = new RangeError('Maximum call stack size exceeded'); | ||
| throwNext = new RangeError('WASM trap'); | ||
| assert.throws(() => isolated.parseSync('SELECT 1'), RangeError); | ||
| const failed = used; | ||
| assert.equal(isolated.parseSync('SELECT 1').stmts.length, 1); | ||
|
|
@@ -292,10 +334,10 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => | |
|
|
||
| it('makes loadModule() and queued calls wait again when a queued call breaks the fresh instance', async () => { | ||
| await isolated.loadModule(); | ||
| throwNext = new RangeError('Maximum call stack size exceeded'); | ||
| throwNext = new RangeError('WASM trap'); | ||
| assert.throws(() => isolated.parseSync('SELECT 1'), RangeError); | ||
| const failing = isolated.parse('SELECT 1'); | ||
| throwNext = new RangeError('Maximum call stack size exceeded'); | ||
| throwNext = new RangeError('WASM trap'); | ||
| const ready = isolated.loadModule(); | ||
| const after = isolated.parse('SELECT 1'); | ||
| await assert.rejects(failing, RangeError); | ||
|
|
@@ -308,10 +350,10 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => | |
|
|
||
| it('makes loadModule() wait again when a call queued ahead of it breaks the fresh instance', async () => { | ||
| await isolated.loadModule(); | ||
| throwNext = new RangeError('Maximum call stack size exceeded'); | ||
| throwNext = new RangeError('WASM trap'); | ||
| assert.throws(() => isolated.parseSync('SELECT 1'), RangeError); | ||
| const failing = isolated.parse('SELECT 1'); | ||
| throwNext = new RangeError('Maximum call stack size exceeded'); | ||
| throwNext = new RangeError('WASM trap'); | ||
| const ready = isolated.loadModule(); | ||
| await assert.rejects(failing, RangeError); | ||
| await ready; | ||
|
|
@@ -342,10 +384,18 @@ describe('Recovery from a throw in each entry point', { timeout: 120000 }, () => | |
| const skip = typeof isolated[name] !== 'function' && 'not in this build'; | ||
|
|
||
| it(`${name}: rethrows the original Error and recovers`, { skip }, async () => { | ||
| const thrown = new RangeError('Maximum call stack size exceeded'); | ||
| const thrown = new RangeError('WASM trap'); | ||
| assert.equal(await failOnce(name, query, thrown), thrown); | ||
| }); | ||
|
|
||
| it(`${name}: reports a V8 stack overflow as a stack depth error and recovers`, { skip }, async () => { | ||
| const thrown = new RangeError('Maximum call stack size exceeded'); | ||
| const caught = await failOnce(name, query, thrown); | ||
| assert.ok(caught instanceof isolated.SqlError); | ||
| assert.equal(caught.message, 'stack depth limit exceeded'); | ||
| assert.equal(caught.cause, thrown); | ||
| }); | ||
|
Comment on lines
+391
to
+397
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 bug · high Tests expect RangeError-to-SqlError conversion that The new tests require a V8 stack-overflow 📋 Prompt for AI AgentsIn versions/18/src/index.ts, inside |
||
|
|
||
| it(`${name}: wraps a non-Error throw in an Error and recovers`, { skip }, async () => { | ||
| const thrown = { name: 'ExitStatus', message: 'Program terminated with exit(1)', status: 1 }; | ||
| const caught = await failOnce(name, query, thrown); | ||
|
|
@@ -368,7 +418,7 @@ function loadTrapping(failLoad) { | |
| if (trapHeapSize === undefined) return parseRaw(...callArgs); | ||
| if (trapHeapSize) module.HEAPU8 = { length: trapHeapSize }; | ||
| trapHeapSize = undefined; | ||
| throw new RangeError('Maximum call stack size exceeded'); | ||
| throw new RangeError('WASM trap'); | ||
| }; | ||
| }, failLoad); | ||
| isolated.trapNext = (heapSize = 0) => { trapHeapSize = heapSize; }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The release-diff allowlist entry cannot separate serialization
RangeErrors from APIRangeErrors.The entry at line 21 matches on the candidate message
RangeError: Maximum call stack size exceeded. The matcher inscripts/release-diff/allowlist.jscompares only the input-and-function id and the error summary.scripts/release-diff/collect.jscomputesparseandscaninside one function that calls the WASM API and thenJSON.stringify. Both steps produce the same candidate summary. A rawRangeErrorfrom the API call on aparseorscaninput can match this entry and be reported as an allowed difference. The gate then passes when the PR requires aSqlErrorthere.The fix must make the entry match only the serialization failure. An id pattern cannot do this because both steps run under the same
parseorscanid. Choose a way to record the serialization phase separately, then match the entry on that record. Until then, the entry should not allow the candidateRangeErrorforparseorscanresults.