Skip to content

[EH] Stack trace support for Wasm EH #17466

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

Closed
aheejin opened this issue Jul 18, 2022 · 2 comments · Fixed by #17979
Closed

[EH] Stack trace support for Wasm EH #17466

aheejin opened this issue Jul 18, 2022 · 2 comments · Fixed by #17979
Assignees

Comments

@aheejin
Copy link
Member

aheejin commented Jul 18, 2022

Currently, both in Emscripten and Wasm EH, if you throw an exception, stack traces are not attached by default. This was, at least for Wasm EH, an intentional decision, because Wasm exception itself can be used not only for errors but for other control flow structures with which slowdown can be problematic. Native C++ exceptions don't have stack traces either.

But it is still convenient to see stack traces when debugging. I think we can attach stack traces when in debug / assert mode, by throwing an Error object instead of a raw string in case of Emscripten EH, and turning the optional traceStack option on in the JS API for Wasm EH.

Related request: #17437

@aheejin aheejin self-assigned this Jul 18, 2022
@aheejin
Copy link
Member Author

aheejin commented Sep 30, 2022

I think I can make this work for Wasm EH, but I'm not sure if the same is possible for Emscripten EH. If we throw a JS Error object in Emscripten EH, that's not going to be recognized as a C++ exception in our wasm code... Maybe we should only solve this for Wasm EH and recommend people to use it more.

@aheejin aheejin changed the title [EH] Stack trace support for Emscripten / Wasm EH [EH] Stack trace support for Wasm EH Sep 30, 2022
@kripken
Copy link
Member

kripken commented Sep 30, 2022

+1 for fixing only in Wasm EH. Even if people can't ship with it yet on all browsers, for local debugging a developer can use a recent browser with Wasm EH.

aheejin added a commit to aheejin/emscripten that referenced this issue Oct 3, 2022
This embeds stack traces into `WebAssembly.Exception` objects when
`ASSERTION` is set. To do this, we now have a separate debug version of
libc++abi, whose `__cxa_throw` doesn't call libunwind's
`_Unwind_RaiseException`, which uses Wasm's `throw` instruction directly
to throw exceptions, but rather calls out to a helper JS function that
creates and throws `WebAssembly.Exception`. That JS function uses the
optional `stack` property of `WebAssembly.Exception` constructor to
attach stack traces to objects.

Without `ASSERTION` set, when an exception is thrown and uncaught, we
only see something like
```
exiting due to exception: [object WebAssembly.Exception]
```
And this is what we've seen so far as well.

With this patch, when `ASSERTION` is set, we see stack traces like
```
exiting due to exception: [object WebAssembly.Exception],Error
    at ___throwCppWebAssemblyException (test.js:1139:13)
    at __cxa_throw (wasm://wasm/009a7c9a:wasm-function[1551]:0x24367)
    at bar() (wasm://wasm/009a7c9a:wasm-function[12]:0xf53)
    at foo() (wasm://wasm/009a7c9a:wasm-function[19]:0x154e)
    at __original_main (wasm://wasm/009a7c9a:wasm-function[20]:0x15a6)
    at main (wasm://wasm/009a7c9a:wasm-function[56]:0x25be)
    at test.js:833:22
    at callMain (test.js:4567:15)
    at doRun (test.js:4621:23)
    at run (test.js:4636:5)
```

Fixes emscripten-core#17466.
aheejin added a commit that referenced this issue Oct 6, 2022
This embeds stack traces into `WebAssembly.Exception` objects when
`ASSERTIONS` is set. To do this, we now have a separate debug version of
libc++abi, whose `__cxa_throw` doesn't call libunwind's
`_Unwind_RaiseException`, which uses Wasm's `throw` instruction directly
to throw exceptions, but rather calls out to a helper JS function that
creates and throws `WebAssembly.Exception`. That JS function uses the
optional `stack` property of `WebAssembly.Exception` constructor to
attach stack traces to objects.

Without `ASSERTIONS` set, when an exception is thrown and uncaught, we
only see something like
```
exiting due to exception: [object WebAssembly.Exception]
```
And this is what we've seen so far as well.

With this patch, when `ASSERTIONS` is set, we see stack traces like
```
exiting due to exception: [object WebAssembly.Exception],Error
    at __cxa_throw (wasm://wasm/009a7c9a:wasm-function[1551]:0x24367)
    at bar() (wasm://wasm/009a7c9a:wasm-function[12]:0xf53)
    at foo() (wasm://wasm/009a7c9a:wasm-function[19]:0x154e)
    at __original_main (wasm://wasm/009a7c9a:wasm-function[20]:0x15a6)
    at main (wasm://wasm/009a7c9a:wasm-function[56]:0x25be)
    at test.js:833:22
    at callMain (test.js:4567:15)
    at doRun (test.js:4621:23)
    at run (test.js:4636:5)
```

Fixes #17466.
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 a pull request may close this issue.

2 participants