Skip to content

Restore the wasm stack pointer after exec() and ffprobe() - #943

Open
Mrmaxmeier wants to merge 1 commit into
ffmpegwasm:mainfrom
Mrmaxmeier:fix/restore-stack-pointer-after-exec
Open

Restore the wasm stack pointer after exec() and ffprobe()#943
Mrmaxmeier wants to merge 1 commit into
ffmpegwasm:mainfrom
Mrmaxmeier:fix/restore-stack-pointer-after-exec

Conversation

@Mrmaxmeier

@Mrmaxmeier Mrmaxmeier commented Aug 17, 2026

Copy link
Copy Markdown

Hi,
I ran into memory corruption / WASM stack overflows in a setting where ffmpeg.wasm is 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.

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>
@netlify

netlify Bot commented Aug 17, 2026

Copy link
Copy Markdown

Deploy Preview for ffmpegwasm canceled.

Name Link
🔨 Latest commit 7141f8a
🔍 Latest deploy log https://app.netlify.com/projects/ffmpegwasm/deploys/6a8300479a640100080ebb7f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RuntimeError: memory access out of bounds - after a lot of mp3 conversion

1 participant