Skip to content

Commit

Permalink
Make JS exception catching code conditional. NFC (#22380)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Aug 15, 2024
1 parent 8dc404a commit eb4d36d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ jobs:
test_targets: "
lto2.test_dylink_syslibs_all
lto2.test_float_builtins
lto2.test_emscripten_lazy_load_code_unconditional
lto0.test_exceptions_allowed_uncaught
lto0.test_longjmp_standalone_standalone
lto0.test_embind_i64_val
Expand Down
41 changes: 30 additions & 11 deletions src/library_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ var LibraryExceptions = {

llvm_eh_typeid_for: (type) => type,

#if !DISABLE_EXCEPTION_CATCHING
__cxa_begin_catch__deps: ['$exceptionCaught', '__cxa_increment_exception_refcount',
'$uncaughtExceptionCount'],
__cxa_begin_catch: (ptr) => {
Expand Down Expand Up @@ -283,7 +284,17 @@ var LibraryExceptions = {
{{{ makeThrow('exceptionLast') }}}
},

#endif
$incrementExceptionRefcount__deps: ['__cxa_increment_exception_refcount'],
$incrementExceptionRefcount: (ptr) => ___cxa_increment_exception_refcount(ptr),

$decrementExceptionRefcount__deps: ['__cxa_decrement_exception_refcount'],
$decrementExceptionRefcount: (ptr) => ___cxa_decrement_exception_refcount(ptr),

$getExceptionMessage__deps: ['$getExceptionMessageCommon'],
$getExceptionMessage: (ptr) => getExceptionMessageCommon(ptr),
#endif // !DISABLE_EXCEPTION_CATCHING
#endif // !WASM_EXCEPTIONS

#if WASM_EXCEPTIONS || !DISABLE_EXCEPTION_CATCHING
$getExceptionMessageCommon__deps: ['__get_exception_message', 'free', '$stackSave', '$stackRestore', '$stackAlloc'],
$getExceptionMessageCommon: (ptr) => {
Expand Down Expand Up @@ -358,19 +369,23 @@ var LibraryExceptions = {
var ptr = getCppExceptionThrownObjectFromWebAssemblyException(ex);
return getExceptionMessageCommon(ptr);
},
#endif // WASM_EXCEPTIONS
};

#elif !DISABLE_EXCEPTION_CATCHING
$incrementExceptionRefcount__deps: ['__cxa_increment_exception_refcount'],
$incrementExceptionRefcount: (ptr) => ___cxa_increment_exception_refcount(ptr),

$decrementExceptionRefcount__deps: ['__cxa_decrement_exception_refcount'],
$decrementExceptionRefcount: (ptr) => ___cxa_decrement_exception_refcount(ptr),

$getExceptionMessage__deps: ['$getExceptionMessageCommon'],
$getExceptionMessage: (ptr) => getExceptionMessageCommon(ptr),
function addStub(name) {
LibraryManager.library[name] = function() { abort(); };
LibraryManager.library[`${name}__sig`] = '';
#if !INCLUDE_FULL_LIBRARY
LibraryManager.library[`${name}__deps`] = [function() {
error(`DISABLE_EXCEPTION_CATCHING was set, which means no C++ exception catching support code is linked in, but such support is required by symbol '${name}'. Either set DISABLE_EXCEPTION_CATCHING=0 (if you do want exception catching) or compile all source files with DISABLE_EXCEPTION_CATCHING=1.`);
}];
#endif
}

#if DISABLE_EXCEPTION_CATCHING
addStub('__cxa_begin_catch');
addStub('__cxa_end_catch');
#endif
};

#if !WASM_EXCEPTIONS
// In LLVM, exceptions generate a set of functions of form
Expand All @@ -379,6 +394,9 @@ var LibraryExceptions = {
// a single function '__cxa_find_matching_catch' that variadically processes all
// of these functions using JS 'arguments' object.
addCxaCatch = (n) => {
#if DISABLE_EXCEPTION_CATCHING
addStub(`__cxa_find_matching_catch_${n}`);
#else
const args = [];
// Confusingly, the actual number of asrgument is n - 2. According to the llvm
// code in WebAssemblyLowerEmscriptenEHSjLj.cpp:
Expand All @@ -395,6 +413,7 @@ addCxaCatch = (n) => {
LibraryManager.library[`__cxa_find_matching_catch_${n}__sig`] = sig;
LibraryManager.library[`__cxa_find_matching_catch_${n}__deps`] = ['$findMatchingCatch'];
LibraryManager.library[`__cxa_find_matching_catch_${n}`] = eval(`(${args}) => findMatchingCatch([${argString}])`);
#endif
};

// Add the first 2-5 catch handlers preemptively. Others get added on demand in
Expand Down
9 changes: 7 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -11577,12 +11577,12 @@ def test_fignore_exceptions(self):
# enabling exceptions at link and compile works
'on': (['-fexceptions'], ['-fexceptions'], True),
# just compile isn't enough as the JS runtime lacks support
'compile_only': (['-fexceptions'], [], False),
'compile_only': (['-fexceptions'], [], False, True),
# just link isn't enough as codegen didn't emit exceptions support
'link_only': ([], ['-fexceptions'], False),
'standalone': (['-fexceptions'], ['-fexceptions', '-sSTANDALONE_WASM', '-sWASM_BIGINT'], True),
})
def test_f_exception(self, compile_flags, link_flags, expect_caught):
def test_f_exception(self, compile_flags, link_flags, expect_caught, expect_link_failure=False):
create_file('src.cpp', r'''
#include <stdio.h>
int main () {
Expand All @@ -11595,6 +11595,11 @@ def test_f_exception(self, compile_flags, link_flags, expect_caught):
}
''')
self.run_process([EMXX, 'src.cpp', '-c', '-o', 'src.o'] + compile_flags)
if expect_link_failure:
err = self.expect_fail([EMXX, 'src.o'] + link_flags)
self.assertContained("error: DISABLE_EXCEPTION_CATCHING was set, which means no C++ exception catching support code is linked in, but such support is required by symbol '__cxa_begin_catch'", err)
return

self.run_process([EMXX, 'src.o'] + link_flags)
result = self.run_js('a.out.js', assert_returncode=0 if expect_caught else NON_ZERO)
if not expect_caught:
Expand Down
1 change: 1 addition & 0 deletions tools/maint/gen_sig_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ def extract_sig_info(sig_info, extra_settings=None, extra_cflags=None, cxx=False
# Enable as many settings as we can here to ensure the maximum number
# of JS symbols are included.
'STACK_OVERFLOW_CHECK': 1,
'DISABLE_EXCEPTION_CATCHING': 0,
'USE_SDL': 1,
'USE_GLFW': 0,
'FETCH': 1,
Expand Down

0 comments on commit eb4d36d

Please sign in to comment.