fix: restore WASM stack in Database.exec to prevent stack leak (#630) - #631
Merged
Conversation
exec() allocates pzTail with stackAlloc(4) but never pairs it with stackSave()/stackRestore(), permanently consuming 16 bytes of the WASM stack per call (success or failure). PR #606 removed the stack save/restore when moving the SQL string to the heap but left the pzTail allocation in place. With the 5MB stack this exhausts the module after ~327k exec() calls, after which the module is corrupted. Fixes #630
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #630
Problem
Database.prototype.execcallsstackAlloc(4)forpzTailbut never pairs the allocation withstackSave()/stackRestore(). Everyexec()call permanently consumes 16 bytes of the WASM stack, whether it succeeds or throws.This is a regression from #606 (
ee67aeb): that PR moved the SQL string from the stack to the heap (stringToNewUTF8) and correctly removed the surroundingstackSave()/stackRestore(), but leftpzTail = stackAlloc(4)in place without cleanup.StatementIterator.next()does the samepzTailallocation but properly restores the stack in afinallyblock.With the 5MB stack this exhausts after ~327k
exec()calls (~16 bytes each), after which the module is corrupted (export/run/new Database all fail with unrelated errors) and the process can only be recovered by restarting. Long-lived processes usingexec()as their normal query path hit this deterministically.Fix
Restore the stack save/restore around
exec():This keeps #606's desirable heap-allocation of the SQL string while freeing only the small per-call
pzTailtemporary.Test
Added
test/test_issue630.jswhich assertsSQL.stackSave()is unchanged after 1000 successfulexec()calls and 100 failing ones. Verified the test fails without the fix (leak of 16000 after 1000 calls) and passes with it.All 24 tests pass on wasm, wasm-debug, asm, and lint is clean.