Skip to content

Commit

Permalink
[EH] Print message for uncaught exceptions (#18003)
Browse files Browse the repository at this point in the history
After #17979, when `ASSERTIONS` is set, uncaught exceptions carry stack
traces that are printed to the screen for debugging help. This adds
an exception message to the exception objects in addition to that.

The message it adds is produced by `__get_exception_message` function:
https://github.com/emscripten-core/emscripten/blob/f6c46570e3780e52050bf822a07b342ec4bdddbe/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp#L75-L111

If an exception is a subclass of `std::exception`, it returns
`std::exception::what()`. If not, it just prints its type.

If an exception is uncaught and its type is a subclass of
`std::exception`, now we print the `what()` message along with the stack
trace when `ASSERTIONS` is set. In case our exception is
`std::runtime_error` and the message is "my exception", when uncaught,
this prints:
```
exiting due to exception: [object WebAssembly.Exception],Error:
std::runtime_error,my exception
    at __cxa_throw (wasm://wasm/009a7c9a:wasm-function[1551]:0x24367)
    ...
```

Fixes #6330.
  • Loading branch information
aheejin committed Oct 10, 2022
1 parent 49bd2a3 commit 68a9f99
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ See docs/process.md for more on how version tagging works.
3.1.24 (in development)
-----------------------
- In Wasm exception mode (`-fwasm-exceptions`), when `ASSERTIONS` is enabled,
uncaught exceptions will display stack traces and what() message. (#17979 and
#18003)
uncaught exceptions will display stack traces. (#17979)
- It is now possible to specify indirect dependencies on JS library functions
directly in C/C++ source code. For example, in the case of a EM_JS or EM_ASM
Expand Down
3 changes: 2 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,7 @@ def get_full_import_name(name):
# using the JS API, which needs this C++ tag exported.
if settings.ASSERTIONS and settings.WASM_EXCEPTIONS:
settings.EXPORTED_FUNCTIONS += ['___cpp_exception']
settings.EXPORT_EXCEPTION_HANDLING_HELPERS = True

# Make `getExceptionMessage` and other necessary functions available for use.
if settings.EXPORT_EXCEPTION_HANDLING_HELPERS:
Expand All @@ -2642,7 +2643,7 @@ def get_full_import_name(name):
# What you need to do is different depending on the kind of EH you use
# (https://github.com/emscripten-core/emscripten/issues/17115).
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$getExceptionMessage', '$incrementExceptionRefcount', '$decrementExceptionRefcount']
settings.EXPORTED_FUNCTIONS += ['getExceptionMessage', '___get_exception_message']
settings.EXPORTED_FUNCTIONS += ['getExceptionMessage', '___get_exception_message', '_free']
if settings.WASM_EXCEPTIONS:
settings.EXPORTED_FUNCTIONS += ['___cpp_exception', '___cxa_increment_exception_refcount', '___cxa_decrement_exception_refcount', '___thrown_object_from_unwind_exception']

Expand Down
13 changes: 7 additions & 6 deletions src/library_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,11 @@ var LibraryExceptions = {
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$withStackSave'],
$getExceptionMessageCommon: function(ptr) {
return withStackSave(function() {
var type_addr_addr = stackAlloc(4);
var message_addr_addr = stackAlloc(4);
___get_exception_message(ptr, type_addr_addr, message_addr_addr);
var type_addr = HEAP32[type_addr_addr >> 2];
var message_addr = HEAP32[message_addr_addr >> 2];
var type_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
var message_addr_addr = stackAlloc({{{ POINTER_SIZE }}});
___get_exception_message({{{ to64('ptr') }}}, {{{ to64('type_addr_addr') }}}, {{{ to64('message_addr_addr') }}});
var type_addr = {{{ makeGetValue('type_addr_addr', 0, '*') }}};
var message_addr = {{{ makeGetValue('message_addr_addr', 0, '*') }}};
var type = UTF8ToString(type_addr);
_free(type_addr);
var message;
Expand Down Expand Up @@ -431,9 +431,10 @@ var LibraryExceptions = {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Exception
// In release builds, this function is not needed and the native
// _Unwind_RaiseException in libunwind is used instead.
__throw_exception_with_stack_trace__deps: ['$getCppExceptionTag'],
__throw_exception_with_stack_trace__deps: ['$getCppExceptionTag', '$getExceptionMessage'],
__throw_exception_with_stack_trace: function(ex) {
var e = new WebAssembly.Exception(getCppExceptionTag(), [ex], {traceStack: true});
e.message = getExceptionMessage(e);
// The generated stack trace will be in the form of:
//
// Error
Expand Down
4 changes: 2 additions & 2 deletions system/lib/libcxxabi/src/cxa_exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ exception.

#if defined(__USING_WASM_EXCEPTIONS__) && !defined(NDEBUG)
extern "C" {
void __throw_exception_with_stack_trace(_Unwind_Exception*, bool);
void __throw_exception_with_stack_trace(_Unwind_Exception*);
} // extern "C"
#endif

Expand Down Expand Up @@ -293,7 +293,7 @@ __cxa_throw(void *thrown_object, std::type_info *tinfo, void (*dest)(void *)) {
#else
// In debug mode, call a JS library function to use WebAssembly.Exception JS
// API, which enables us to include stack traces
__throw_exception_with_stack_trace(&exception_header->unwindHeader, true);
__throw_exception_with_stack_trace(&exception_header->unwindHeader);
#endif
#else
_Unwind_RaiseException(&exception_header->unwindHeader);
Expand Down
17 changes: 12 additions & 5 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7905,10 +7905,12 @@ def test_wasm_nope(self):
self.assertContained('no native wasm support detected', out)

@requires_v8
def test_wasm_exceptions_stack_trace(self):
def test_wasm_exceptions_stack_trace_and_message(self):
src = r'''
#include <stdexcept>
void bar() {
throw 3;
throw std::runtime_error("my message");
}
void foo() {
bar();
Expand All @@ -7921,8 +7923,8 @@ def test_wasm_exceptions_stack_trace(self):
emcc_args = ['-g', '-fwasm-exceptions']
self.v8_args.append('--experimental-wasm-eh')

# Stack trace example for this example code:
# exiting due to exception: [object WebAssembly.Exception],Error
# Stack trace and message example for this example code:
# exiting due to exception: [object WebAssembly.Exception],Error: std::runtime_error, my message
# 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)
Expand All @@ -7932,7 +7934,12 @@ def test_wasm_exceptions_stack_trace(self):
# at callMain (test.js:4567:15)
# at doRun (test.js:4621:23)
# at run (test.js:4636:5)
stack_trace_checks = ['at __cxa_throw', 'at bar', 'at foo', 'at main']
stack_trace_checks = [
'std::runtime_error,my message',
'at __cxa_throw',
'at bar',
'at foo',
'at main']

# We attach stack traces to exception objects only when ASSERTIONS is set
self.set_setting('ASSERTIONS')
Expand Down

0 comments on commit 68a9f99

Please sign in to comment.