Skip to content

Commit 4f40164

Browse files
authored
[EH] Deprecate EXPORT_EXCEPTION_HANDLING_HELPERS (#26499)
This effectively removes `EXPORT_EXCEPTION_HANDLING_HELPERS` setting. This marks the setting as deprecated not to crash users' builds right away in case they are using it. Even though it still exists as a deprecated setting, setting it to true will not change anything. It used to export `getExceptionMessage` and a few more functions (`in/decrementexceptionRefCount`), but after #26493, `getExceptionMessage` is exported anyway when exceptions are used and either `-sASSERTIONS` or `-sEXCEPTION_STACK_TRACES` is set, which are set by default at `-O0`. For Wasm EH, the dependency is automatically detected. For Emscripten EH, we had to add `getExceptionMessage` to deps of `__cxa_throw`. This adds `in/decrementexceptionRefCount` to deps of `__cxa_throw` (for Emscripten EH) and `__throw_exception_with_stack_trace` (for Wasm EH) and removes `EXPORT_EXCEPTION_HANDLING_HELPERS`. (You can use it but it won't do anything additionally)
1 parent 93519b6 commit 4f40164

File tree

8 files changed

+26
-31
lines changed

8 files changed

+26
-31
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ See docs/process.md for more on how version tagging works.
2020

2121
5.0.4 (in development)
2222
----------------------
23+
- `EXPORT_EXCEPTION_HANDLING_HELPERS` is deprecated and setting it will not do
24+
anything. `getExceptionMessage` is exported anyway when `ASSERTIONS` or
25+
`EXCEPTION_STACK_TRACES` is set, which are set by default at `-O0`.
2326
- The deprecated `EMSCRIPTEN` macro is now defined in `emscripten.h` rather than
2427
on the command line (`__EMSCRIPTEN__`, which is built into LLVM, should be
2528
used instead). (#26417)

site/source/docs/porting/exceptions.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ message part is empty. If the thrown value is an instance of ``MyException``
144144
that is a subclass of ``std::exception`` and its ``what`` message is ``My
145145
exception thrown``, this code will print ``MyException,My exception thrown``.
146146

147-
To use this function, you need to pass ``-sEXPORT_EXCEPTION_HANDLING_HELPERS``
148-
to the options. You need to enable either of Emscripten EH or Wasm EH to use
149-
this option.
147+
``getExceptionMessage`` is available when exceptions are used and either
148+
``-sASSERTIONS`` or ``-sEXCEPTION_STACK_TRACES`` is set, which are by default
149+
true at ``-O0``.
150150

151151
If the stack pointer has been moved due to stack allocations within the Wasm
152152
function before an exception is thrown, you can use ``stackSave()`` and
@@ -156,9 +156,8 @@ leaked.
156156
.. note:: If you catch a Wasm exception and do not rethrow it, you need to free
157157
the storage associated with the exception in JS using
158158
``decrementExceptionRefcount`` method because the exception catching code in
159-
Wasm does not have a chance to free it. See
160-
``test_EXPORT_EXCEPTION_HANDLING_HELPERS`` in test/test_core.py for an
161-
example usage.
159+
Wasm does not have a chance to free it. See ``test_getExceptionMessage`` in
160+
``test/test_core.py`` for an example usage.
162161

163162

164163
Using Exceptions and setjmp-longjmp Together

site/source/docs/tools_reference/settings_reference.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,8 @@ manipulate the refcount manually to avoid memory leaks.
11291129
See test_EXPORT_EXCEPTION_HANDLING_HELPERS in test/test_core.py for an
11301130
example usage.
11311131

1132+
.. note:: This setting is deprecated
1133+
11321134
Default value: false
11331135

11341136
.. _exception_stack_traces:
@@ -3417,6 +3419,7 @@ these settings please open a bug (or reply to one of the existing bugs).
34173419
- ``LEGALIZE_JS_FFI``: to disable JS type legalization use `-sWASM_BIGINT` or `-sSTANDALONE_WASM`
34183420
- ``ASYNCIFY_EXPORTS``: please use JSPI_EXPORTS instead
34193421
- ``LINKABLE``: under consideration for removal (https://github.com/emscripten-core/emscripten/issues/25262)
3422+
- ``EXPORT_EXCEPTION_HANDLING_HELPERS``: getExceptionMessage is automatically exported when ASSERTIONS or EXCEPTION_STACK_TRACES is 1 and throw is used
34203423

34213424
.. _legacy-settings:
34223425

src/lib/libexceptions.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ var LibraryExceptions = {
9191
// 'new CppException', whose constructor calls getExceptionMessage. We can't
9292
// track the dependency there, so we track it here.
9393
'$getExceptionMessage',
94+
// These functions can be necessary to prevent memory leaks from the JS
95+
// side. Even though they are not used it here directly, we export them when
96+
// 'throw' is used here.
97+
'$decrementExceptionRefcount', '$incrementExceptionRefcount',
9498
#endif
9599
],
96100
__cxa_throw: (ptr, type, destructor) => {
@@ -314,7 +318,12 @@ var LibraryExceptions = {
314318
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Exception
315319
// In release builds, this function is not needed and the native
316320
// _Unwind_RaiseException in libunwind is used instead.
317-
__throw_exception_with_stack_trace__deps: ['$getCppExceptionTag', '$getExceptionMessage'],
321+
__throw_exception_with_stack_trace__deps: [
322+
'$getCppExceptionTag', '$getExceptionMessage',
323+
// These functions can be necessary to prevent memory leaks from the JS
324+
// side. Even though they are not used it here directly, we export them
325+
// when 'throw' is used here.
326+
'$decrementExceptionRefcount', '$incrementExceptionRefcount'],
318327
__throw_exception_with_stack_trace: (ex) => {
319328
var e = new WebAssembly.Exception(getCppExceptionTag(), [ex], {traceStack: true});
320329
e.message = getExceptionMessage(e);

src/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ var DISABLE_EXCEPTION_THROWING = false;
767767
//
768768
// See test_EXPORT_EXCEPTION_HANDLING_HELPERS in test/test_core.py for an
769769
// example usage.
770+
// [deprecated]
770771
var EXPORT_EXCEPTION_HANDLING_HELPERS = false;
771772

772773
// When this is enabled, exceptions will contain stack traces and uncaught

test/test_core.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,9 +1499,8 @@ def test_exceptions_rethrow_missing(self):
14991499
self.do_runf('main.cpp', None, assert_returncode=NON_ZERO)
15001500

15011501
@with_all_eh_sjlj
1502-
def test_EXPORT_EXCEPTION_HANDLING_HELPERS(self):
1503-
self.set_setting('EXPORT_EXCEPTION_HANDLING_HELPERS')
1504-
1502+
def test_getExceptionMessage(self):
1503+
self.set_setting('ASSERTIONS')
15051504
self.maybe_closure()
15061505
create_file('main.cpp', '''
15071506
#include <emscripten.h>
@@ -1700,13 +1699,7 @@ def clear_all_relevant_settings(self):
17001699
self.assert_fail([EMCC, test_file('hello_world.cpp'), '-fwasm-exceptions'] + self.get_cflags(), expected)
17011700
clear_all_relevant_settings(self)
17021701

1703-
# EXPORT_EXCEPTION_HANDLING_HELPERS and EXCEPTION_STACK_TRACES requires
1704-
# either Emscripten EH or Wasm EH
1705-
self.set_setting('EXPORT_EXCEPTION_HANDLING_HELPERS')
1706-
expected = 'error: EXPORT_EXCEPTION_HANDLING_HELPERS requires either of -fexceptions or -fwasm-exceptions'
1707-
self.assert_fail([EMCC, test_file('hello_world.cpp')] + self.get_cflags(), expected)
1708-
clear_all_relevant_settings(self)
1709-
1702+
# EXCEPTION_STACK_TRACES requires either Emscripten EH or Wasm EH
17101703
self.set_setting('EXCEPTION_STACK_TRACES')
17111704
expected = 'error: EXCEPTION_STACK_TRACES requires either of -fexceptions or -fwasm-exceptions'
17121705
self.assert_fail([EMCC, test_file('hello_world.cpp')] + self.get_cflags(), expected)

tools/link.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,20 +1795,6 @@ def get_full_import_name(name):
17951795
if settings.DISABLE_EXCEPTION_CATCHING and not settings.WASM_EXCEPTIONS:
17961796
exit_with_error('EXCEPTION_STACK_TRACES requires either of -fexceptions or -fwasm-exceptions')
17971797

1798-
# Make `getExceptionMessage` and other necessary functions available for use.
1799-
if settings.EXPORT_EXCEPTION_HANDLING_HELPERS:
1800-
# If the user explicitly gave EXPORT_EXCEPTION_HANDLING_HELPERS=1 without
1801-
# enabling EH, errors out.
1802-
if settings.DISABLE_EXCEPTION_CATCHING and not settings.WASM_EXCEPTIONS:
1803-
exit_with_error('EXPORT_EXCEPTION_HANDLING_HELPERS requires either of -fexceptions or -fwasm-exceptions')
1804-
# We also export refcount incrementing and decrementing functions because if
1805-
# you catch an exception from JS, you may need to manipulate the refcount
1806-
# manually to avoid memory leaks. See test_EXPORT_EXCEPTION_HANDLING_HELPERS
1807-
# in test/test_core.py for an example usage.
1808-
settings.EXPORTED_FUNCTIONS += ['getExceptionMessage', 'incrementExceptionRefcount', 'decrementExceptionRefcount']
1809-
if settings.WASM_EXCEPTIONS:
1810-
settings.REQUIRED_EXPORTS += ['__cpp_exception']
1811-
18121798
if settings.SIDE_MODULE:
18131799
# For side modules, we ignore all REQUIRED_EXPORTS that might have been added above.
18141800
# They all come from either libc or compiler-rt. The exception is __wasm_call_ctors

tools/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
'LEGALIZE_JS_FFI': 'to disable JS type legalization use `-sWASM_BIGINT` or `-sSTANDALONE_WASM`',
115115
'ASYNCIFY_EXPORTS': 'please use JSPI_EXPORTS instead',
116116
'LINKABLE': 'under consideration for removal (https://github.com/emscripten-core/emscripten/issues/25262)',
117+
'EXPORT_EXCEPTION_HANDLING_HELPERS': 'getExceptionMessage is automatically exported when ASSERTIONS or EXCEPTION_STACK_TRACES is 1 and throw is used',
117118
}
118119

119120
# Settings that don't need to be externalized when serializing to json because they

0 commit comments

Comments
 (0)