Skip to content
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

Implement stack overflow protection for webassembly #130397

Open
markshannon opened this issue Feb 21, 2025 · 4 comments
Open

Implement stack overflow protection for webassembly #130397

markshannon opened this issue Feb 21, 2025 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-wasi type-feature A feature request or enhancement

Comments

@markshannon
Copy link
Member

Implementing stack overflow protection for webassembly is tricky, as there are two stacks:

  1. The hidden webassembly stack
  2. The stack used for C stack objects that can have their address taken

We need to avoid overflowing either. It generally seems that the first stack is the one most vulnerable to overflow, so perhaps a simple counter would work.

@brettcannon
@hoodmane

@hoodmane
Copy link
Contributor

If the spilled stack ever overflows, we can just recompile and make it bigger whereas the wasm stack size is determined by the runtime and can't easily be made bigger. At least for Emscripten run in the browser. Perhaps with wasmtime we can easily ask the runtime to make the stack bigger.

@markshannon
Copy link
Member Author

This is about runtime detection of stack limits, rather than increasing the stack size.

Is it possible to get the size of the wasm stack from the browser or wasmtime?
Also is it possible to get the current stack depth?

Alternatively, is it possible to catch a stack overflow in JS? We could maybe write a JS helper function to probe the stack.

@tomasr8 tomasr8 added interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-wasi labels Feb 21, 2025
@hoodmane
Copy link
Contributor

It's possible to catch a js stack overflow. I've used the probe approach before but unfortunately the ratio of the number of js stack frames it takes to overflow vs the number of wasm frames is inconsistent between V8 and spider monkey. They have similar apparent wasm stack sizes but spider monkey can handle many more nested js frames than V8 iirc.

@hoodmane
Copy link
Contributor

I think in node there are ways to query the stack size and depth but it is not part of the web standard and I think browsers don't include any such APIs unless you start the browser in a debug mode.

@picnixz picnixz added the type-feature A feature request or enhancement label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-wasi type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants