Skip to content

Commit ebe882c

Browse files
authored
Better message when a side module needs exceptions (#9698)
The main module needs to be built with exceptions support for a side module to use it, both for system libs and for the current JS support. This informs users they need to do that if a side module throws and the main wasn't built properly to support that. Helps #9691
1 parent b0eaaa5 commit ebe882c

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

src/parseTools.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,11 +1320,13 @@ function makeStructuralReturn(values, inAsm) {
13201320
}
13211321

13221322
function makeThrow(what) {
1323-
if (ASSERTIONS) {
1324-
return 'throw ' + what + (DISABLE_EXCEPTION_CATCHING == 1 ? ' + " - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch."' : '') + ';';
1325-
} else {
1326-
return 'throw ' + what + ';';
1323+
if (ASSERTIONS && DISABLE_EXCEPTION_CATCHING == 1) {
1324+
what += ' + " - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch."';
1325+
if (MAIN_MODULE) {
1326+
what += ' + " (note: in dynamic linking, if a side module wants exceptions, the main module must be built with that support)"';
1327+
}
13271328
}
1329+
return 'throw ' + what + ';';
13281330
}
13291331

13301332
function makeSignOp(value, type, op, force, ignore) {

tests/test_other.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6497,11 +6497,11 @@ def test_ld_library_path(self):
64976497
self.assertContained('Ok', out)
64986498

64996499
def test_dlopen_rtld_global(self):
6500-
# TODO: wasm support. this test checks RTLD_GLOBAL where a module is loaded
6501-
# before the module providing a global it needs is. in asm.js we use JS
6502-
# to create a redirection function. In wasm we just have wasm, so we
6503-
# need to introspect the wasm module. Browsers may add that eventually,
6504-
# or we could ship a little library that does it.
6500+
# This test checks RTLD_GLOBAL where a module is loaded
6501+
# before the module providing a global it needs is. in asm.js we use JS
6502+
# to create a redirection function. In wasm we just have wasm, so we
6503+
# need to introspect the wasm module. Browsers may add that eventually,
6504+
# or we could ship a little library that does it.
65056505
create_test_file('hello1.c', r'''
65066506
#include <stdio.h>
65076507
@@ -6565,6 +6565,59 @@ def test_dlopen_rtld_global(self):
65656565
self.assertContained('hello1_val by hello1:3', out)
65666566
self.assertContained('hello1_val by hello2:3', out)
65676567

6568+
@no_fastcomp()
6569+
def test_main_module_without_exceptions_message(self):
6570+
# A side module that needs exceptions needs a main module with that
6571+
# support enabled; show a clear message in that case.
6572+
create_test_file('side.cpp', r'''
6573+
#include <exception>
6574+
#include <stdio.h>
6575+
6576+
extern "C" void test_throw() {
6577+
try {
6578+
throw 42;
6579+
} catch(int x) {
6580+
printf("catch %d.\n", x);
6581+
return;
6582+
}
6583+
puts("bad location");
6584+
}
6585+
''')
6586+
create_test_file('main.cpp', r'''
6587+
#include <assert.h>
6588+
#include <stdio.h>
6589+
#include <stdlib.h>
6590+
#include <string.h>
6591+
#include <dlfcn.h>
6592+
6593+
typedef void (*voidf)();
6594+
6595+
int main() {
6596+
void* h = dlopen ("libside.wasm", RTLD_NOW|RTLD_GLOBAL);
6597+
assert(h);
6598+
voidf f = (voidf)dlsym(h, "test_throw");
6599+
assert(f);
6600+
f();
6601+
return 0;
6602+
}
6603+
''')
6604+
run_process([PYTHON, EMCC, '-o', 'libside.wasm', 'side.cpp', '-s', 'SIDE_MODULE=1', '-fexceptions'])
6605+
6606+
def build_main(args):
6607+
print(args)
6608+
with env_modify({'EMCC_FORCE_STDLIBS': 'libc++abi'}):
6609+
run_process([PYTHON, EMCC, 'main.cpp', '-s', 'MAIN_MODULE=1', '-s', 'EXPORT_ALL',
6610+
'--embed-file', 'libside.wasm'] + args)
6611+
6612+
build_main([])
6613+
out = run_js('a.out.js', assert_returncode=None, stderr=STDOUT)
6614+
self.assertContained('Exception catching is disabled, this exception cannot be caught.', out)
6615+
self.assertContained('note: in dynamic linking, if a side module wants exceptions, the main module must be built with that support', out)
6616+
6617+
build_main(['-fexceptions'])
6618+
out = run_js('a.out.js')
6619+
self.assertContained('catch 42', out)
6620+
65686621
def test_debug_asmLastOpts(self):
65696622
create_test_file('src.c', r'''
65706623
#include <stdio.h>

0 commit comments

Comments
 (0)