Restore the wasm stack pointer after exec() and ffprobe() - #943
Open
Mrmaxmeier wants to merge 1 commit into
Open
Restore the wasm stack pointer after exec() and ffprobe()#943Mrmaxmeier wants to merge 1 commit into
Mrmaxmeier wants to merge 1 commit into
Conversation
ffmpeg and ffprobe finish by calling exit(), which Emscripten implements
by throwing. exec() and ffprobe() swallow that exception, but nothing
unwinds the WebAssembly stack, so the stack pointer stays wherever the C
code left it. Every call therefore leaks part of the stack.
With the 64 KB default stack of the current release, exec("-h") leaks
768 B a call and the module traps with "memory access out of bounds" in
<100 calls.
This saves the stack pointer before entering wasm and restore it in a
finally block, which is what Emscripten does in its own invoke_* helpers
for exactly this situation.
stackSave is exported so the tests can assert the stack pointer is where
it started after a successful command, a failing command, and ffprobe.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
✅ Deploy Preview for ffmpegwasm canceled.
|
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.
Hi,
I ran into memory corruption / WASM stack overflows in a setting where
ffmpeg.wasmis called repeatedly in one session (Ente Photos web import).ffmpeg and ffprobe finish by calling exit(), which Emscripten implements by throwing. exec() and ffprobe() swallow that exception, but nothing unwinds the WebAssembly stack, so the stack pointer stays wherever the C code left it.
With the 64 KB default stack of the current release (since bumped to 5 MB in b409e36), exec("-h") leaks 768 B a call and the module traps with "memory access out of bounds" in <100 calls.
This saves the stack pointer before entering wasm and restore it in a finally block, which is what Emscripten does in its own invoke_* helpers for exactly this situation.
This might fix #563.