From 3cf21a0340ccd7fbcd7b950bce81bb7f5ba39259 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 18 Feb 2022 17:15:47 -0500 Subject: [PATCH 01/30] Wrote format_exception function and hard coded inclusion into system_libs --- system/lib/format_exception.cpp | 45 +++++++++++++++++++++++++++++++++ tools/system_libs.py | 10 +++++++- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 system/lib/format_exception.cpp diff --git a/system/lib/format_exception.cpp b/system/lib/format_exception.cpp new file mode 100644 index 000000000000..bc02d71dcadd --- /dev/null +++ b/system/lib/format_exception.cpp @@ -0,0 +1,45 @@ +#define __USING_EMSCRIPTEN_EXCEPTIONS__ +#include "cxa_exception.h" +#include "cxxabi.h" +#include +#include + +extern "C" { + +#define DEMANGLED_BUF_SIZE 100 + +int __cxa_can_catch(const std::type_info* catchType, + const std::type_info* excpType, + void** thrown); + +int format_exception(char** result, void* exc_ptr) { + __cxxabiv1::__cxa_exception* exc_info = + (__cxxabiv1::__cxa_exception*)exc_ptr - 1; + std::type_info* exc_type = exc_info->exceptionType; + const char* exc_name = exc_type->name(); + + int status = 0; + char* demangled_buf = __cxxabiv1::__cxa_demangle(exc_name, 0, 0, &status); + if (status == 0 && demangled_buf) { + exc_name = demangled_buf; + } + + int can_catch = __cxa_can_catch(&typeid(std::exception), exc_type, &exc_ptr); + int ret; + if (can_catch) { + const char* exc_what = ((std::exception*)exc_ptr)->what(); + ret = asprintf(result, "Cpp Exception %s: %s", exc_name, exc_what); + } else { + ret = asprintf(result, + "Cpp Exception: The exception is an object of type '%s' at " + "address %p which does not inherit from std::exception", + exc_name, + exc_ptr); + } + + if (demangled_buf) { + free(demangled_buf); + } + return ret; +} +} \ No newline at end of file diff --git a/tools/system_libs.py b/tools/system_libs.py index ceef98bad779..b51f5e525260 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -615,7 +615,7 @@ def __init__(self, **kwargs): def get_cflags(self): cflags = super().get_cflags() if self.is_wasm: - # DISABLE_EXCEPTION_THROWING=0 is the default, which is for Emscripten + # DISABLE_EXCEPTIOExceptionsN_THROWING=0 is the default, which is for Emscripten # EH/SjLj, so we should reverse it. cflags += ['-sSUPPORT_LONGJMP=wasm', '-sDISABLE_EXCEPTION_THROWING=1', @@ -1087,6 +1087,13 @@ def can_use(self): return super().can_use() and settings.USE_PTHREADS +class libformatexception(Library): + name = 'libformatexception' + cflags = ['-Os'] + src_dir = 'system/lib' + src_files = ['format_exception.cpp'] + includes = ["system/lib/libcxxabi/src"] + class libcxxabi(NoExceptLibrary, MTLibrary): name = 'libc++abi' cflags = [ @@ -1718,6 +1725,7 @@ def add_library(libname): need_whole_archive = lib.name in force_include and lib.get_ext() == '.a' libs_to_link.append((lib.get_link_flag(), need_whole_archive)) + add_library('libformatexception') if '-nostartfiles' not in args: if settings.USE_PTHREADS: add_library('crtbegin') From 2ea06c83b1a0dda760ed4921f4b82ecd99d6b1cd Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 18 Feb 2022 19:55:52 -0500 Subject: [PATCH 02/30] Add Javascript wrapper, miscellaneous cleanup --- src/library_exceptions.js | 11 +++++++++++ src/settings.js | 7 +++++++ system/lib/format_exception.cpp | 2 -- tools/system_libs.py | 7 ++++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 97942e5c6863..e436bb059997 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -428,6 +428,17 @@ var LibraryExceptions = { catchInfo.free(); {{{ makeThrow('ptr') }}} }, + + $formatException__deps: ["format_exception"], + $formatException: function(excPtr){ + var stackTop = stackSave(); + var result_ptr = stackAlloc(4); + {{{ exportedAsmFunc('_format_exception') }}}(result_ptr, excPtr); + var result = UTF8ToString({{{ makeGetValue('result_ptr', '0', '*') }}}); + stackRestore(stackTop); + _free(result_ptr); + return result; + } }; // In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. diff --git a/src/settings.js b/src/settings.js index 80354a27b8c8..b08b0cdcc76b 100644 --- a/src/settings.js +++ b/src/settings.js @@ -339,6 +339,13 @@ var EXCEPTION_DEBUG = 0; // [link] var DEMANGLE_SUPPORT = 0; +// If 1, expose a formatException function that can be used to format a top +// level C++ exception. If the thrown exception was +// throw new std::runtime_error("a big problem occurred") +// It returns a string like: "Cpp Exception std::runtime_error: A big problem occured." +// [link] +var FORMAT_EXCEPTION_SUPPORT = 0; + // Print out when we enter a library call (library*.js). You can also unset // Runtime.debug at runtime for logging to cease, and can set it when you want // it back. A simple way to set it in C++ is diff --git a/system/lib/format_exception.cpp b/system/lib/format_exception.cpp index bc02d71dcadd..295afcf4e428 100644 --- a/system/lib/format_exception.cpp +++ b/system/lib/format_exception.cpp @@ -1,11 +1,9 @@ -#define __USING_EMSCRIPTEN_EXCEPTIONS__ #include "cxa_exception.h" #include "cxxabi.h" #include #include extern "C" { - #define DEMANGLED_BUF_SIZE 100 int __cxa_can_catch(const std::type_info* catchType, diff --git a/tools/system_libs.py b/tools/system_libs.py index b51f5e525260..b829c18e51fb 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1089,10 +1089,10 @@ def can_use(self): class libformatexception(Library): name = 'libformatexception' - cflags = ['-Os'] src_dir = 'system/lib' src_files = ['format_exception.cpp'] - includes = ["system/lib/libcxxabi/src"] + includes = ['system/lib/libcxxabi/src'] + cflags = ['-Os', '-D__USING_EMSCRIPTEN_EXCEPTIONS__'] class libcxxabi(NoExceptLibrary, MTLibrary): name = 'libc++abi' @@ -1725,7 +1725,6 @@ def add_library(libname): need_whole_archive = lib.name in force_include and lib.get_ext() == '.a' libs_to_link.append((lib.get_link_flag(), need_whole_archive)) - add_library('libformatexception') if '-nostartfiles' not in args: if settings.USE_PTHREADS: add_library('crtbegin') @@ -1784,6 +1783,8 @@ def add_library(libname): add_library('libc++abi') if settings.EXCEPTION_HANDLING: add_library('libunwind') + if settings.FORMAT_EXCEPTION_SUPPORT: + add_library("libformatexception") if settings.USE_ASAN: force_include.append('libasan_rt') From 693f677ca28622af94c398f1193505bf735741b0 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 18 Feb 2022 20:05:46 -0500 Subject: [PATCH 03/30] Fix formatting --- tools/system_libs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/system_libs.py b/tools/system_libs.py index b829c18e51fb..491b2282fb09 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1094,6 +1094,7 @@ class libformatexception(Library): includes = ['system/lib/libcxxabi/src'] cflags = ['-Os', '-D__USING_EMSCRIPTEN_EXCEPTIONS__'] + class libcxxabi(NoExceptLibrary, MTLibrary): name = 'libc++abi' cflags = [ From 95ef456736d61bc1a2c15cdd317d0d7f3c130564 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 10:20:33 -0500 Subject: [PATCH 04/30] No need for exportedAsmFunction --- src/library_exceptions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index e436bb059997..e29b2327d593 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -433,7 +433,7 @@ var LibraryExceptions = { $formatException: function(excPtr){ var stackTop = stackSave(); var result_ptr = stackAlloc(4); - {{{ exportedAsmFunc('_format_exception') }}}(result_ptr, excPtr); + _format_exception(result_ptr, excPtr); var result = UTF8ToString({{{ makeGetValue('result_ptr', '0', '*') }}}); stackRestore(stackTop); _free(result_ptr); From 103dca8d6d9042e00e0dba7e315df406f0d167d0 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:45:17 -0500 Subject: [PATCH 05/30] Use withStackSave --- src/library_exceptions.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index e29b2327d593..0a34ca020080 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -429,16 +429,14 @@ var LibraryExceptions = { {{{ makeThrow('ptr') }}} }, - $formatException__deps: ["format_exception"], - $formatException: function(excPtr){ - var stackTop = stackSave(); + $formatException__deps: ["format_exception", "$withStackSave"], + $formatException: withStackSave(function(excPtr){ var result_ptr = stackAlloc(4); _format_exception(result_ptr, excPtr); var result = UTF8ToString({{{ makeGetValue('result_ptr', '0', '*') }}}); - stackRestore(stackTop); _free(result_ptr); return result; - } + }), }; // In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. From cc31016e5a99e7e83e2c515da1aaa731940fdc7d Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:45:55 -0500 Subject: [PATCH 06/30] Free the right thing --- src/library_exceptions.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 0a34ca020080..8ed504363853 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -433,8 +433,9 @@ var LibraryExceptions = { $formatException: withStackSave(function(excPtr){ var result_ptr = stackAlloc(4); _format_exception(result_ptr, excPtr); - var result = UTF8ToString({{{ makeGetValue('result_ptr', '0', '*') }}}); - _free(result_ptr); + var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; + var result = UTF8ToString(utf8_addr); + _free(utf_addr); return result; }), }; From 5ca4f183ff46ce518a88892ac6d3873ac7803aff Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:46:34 -0500 Subject: [PATCH 07/30] Use stackAlloc({{{ POINTER_SIZE }}}); --- src/library_exceptions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 8ed504363853..8825d6c4cbc0 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -431,7 +431,7 @@ var LibraryExceptions = { $formatException__deps: ["format_exception", "$withStackSave"], $formatException: withStackSave(function(excPtr){ - var result_ptr = stackAlloc(4); + var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); _format_exception(result_ptr, excPtr); var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; var result = UTF8ToString(utf8_addr); From aa15cc8a6ad14cb4e048bd38a36658418a71eb86 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:48:39 -0500 Subject: [PATCH 08/30] Remove accidental change --- tools/system_libs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/system_libs.py b/tools/system_libs.py index 491b2282fb09..1b482819f3ac 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -615,7 +615,7 @@ def __init__(self, **kwargs): def get_cflags(self): cflags = super().get_cflags() if self.is_wasm: - # DISABLE_EXCEPTIOExceptionsN_THROWING=0 is the default, which is for Emscripten + # DISABLE_EXCEPTION_THROWING=0 is the default, which is for Emscripten # EH/SjLj, so we should reverse it. cflags += ['-sSUPPORT_LONGJMP=wasm', '-sDISABLE_EXCEPTION_THROWING=1', From 493e22f0b3728651979f1b0582a29f8ba5318681 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:49:35 -0500 Subject: [PATCH 09/30] Add final newline to format_exception.cpp --- system/lib/format_exception.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/lib/format_exception.cpp b/system/lib/format_exception.cpp index 295afcf4e428..45a479689b58 100644 --- a/system/lib/format_exception.cpp +++ b/system/lib/format_exception.cpp @@ -40,4 +40,4 @@ int format_exception(char** result, void* exc_ptr) { } return ret; } -} \ No newline at end of file +} From c7d126729ff5fa1a21d3e0f2b666a756cea63817 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:50:11 -0500 Subject: [PATCH 10/30] Add emscripten_ prefix to format_exception --- src/library_exceptions.js | 2 +- system/lib/format_exception.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 8825d6c4cbc0..bdf8625a14e3 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -432,7 +432,7 @@ var LibraryExceptions = { $formatException__deps: ["format_exception", "$withStackSave"], $formatException: withStackSave(function(excPtr){ var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); - _format_exception(result_ptr, excPtr); + _emscripten_format_exception(result_ptr, excPtr); var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; var result = UTF8ToString(utf8_addr); _free(utf_addr); diff --git a/system/lib/format_exception.cpp b/system/lib/format_exception.cpp index 45a479689b58..abdce817a4b2 100644 --- a/system/lib/format_exception.cpp +++ b/system/lib/format_exception.cpp @@ -10,7 +10,7 @@ int __cxa_can_catch(const std::type_info* catchType, const std::type_info* excpType, void** thrown); -int format_exception(char** result, void* exc_ptr) { +int emscripten_format_exception(char** result, void* exc_ptr) { __cxxabiv1::__cxa_exception* exc_info = (__cxxabiv1::__cxa_exception*)exc_ptr - 1; std::type_info* exc_type = exc_info->exceptionType; From 0231037c70735d1916428d4890db6b142186a00f Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 16:57:10 -0500 Subject: [PATCH 11/30] Add format_exception.cpp to libcxxabi --- system/lib/{ => libcxxabi}/format_exception.cpp | 0 tools/system_libs.py | 3 +++ 2 files changed, 3 insertions(+) rename system/lib/{ => libcxxabi}/format_exception.cpp (100%) diff --git a/system/lib/format_exception.cpp b/system/lib/libcxxabi/format_exception.cpp similarity index 100% rename from system/lib/format_exception.cpp rename to system/lib/libcxxabi/format_exception.cpp diff --git a/tools/system_libs.py b/tools/system_libs.py index 1b482819f3ac..2ef6e8636be2 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1146,6 +1146,9 @@ def get_files(self): 'cxa_personality.cpp' ] + if settings.FORMAT_EXCEPTION_SUPPORT: + filenames += ["format_exception.cpp"] + return files_in_path( path='system/lib/libcxxabi/src', filenames=filenames) From af456b1e9a626dbbf7d1f77bae637041e92d09e4 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 17:09:00 -0500 Subject: [PATCH 12/30] Use withStackSave correctly --- src/library_exceptions.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index bdf8625a14e3..061d1cda3f63 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -430,14 +430,16 @@ var LibraryExceptions = { }, $formatException__deps: ["format_exception", "$withStackSave"], - $formatException: withStackSave(function(excPtr){ - var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); - _emscripten_format_exception(result_ptr, excPtr); - var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; - var result = UTF8ToString(utf8_addr); - _free(utf_addr); - return result; - }), + $formatException: function(excPtr){ + return withStackSave(function(){ + var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); + _emscripten_format_exception(result_ptr, excPtr); + var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; + var result = UTF8ToString(utf8_addr); + _free(utf_addr); + return result; + }); + }, }; // In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. From e233943d7f6ebd8da5ab95b9b8a9ff9fb698447e Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 17:10:10 -0500 Subject: [PATCH 13/30] Remove libformatexception library --- tools/system_libs.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tools/system_libs.py b/tools/system_libs.py index 2ef6e8636be2..18892103b942 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1087,14 +1087,6 @@ def can_use(self): return super().can_use() and settings.USE_PTHREADS -class libformatexception(Library): - name = 'libformatexception' - src_dir = 'system/lib' - src_files = ['format_exception.cpp'] - includes = ['system/lib/libcxxabi/src'] - cflags = ['-Os', '-D__USING_EMSCRIPTEN_EXCEPTIONS__'] - - class libcxxabi(NoExceptLibrary, MTLibrary): name = 'libc++abi' cflags = [ @@ -1787,8 +1779,6 @@ def add_library(libname): add_library('libc++abi') if settings.EXCEPTION_HANDLING: add_library('libunwind') - if settings.FORMAT_EXCEPTION_SUPPORT: - add_library("libformatexception") if settings.USE_ASAN: force_include.append('libasan_rt') From 83a1a4aef43b81ae63504c6901e07d13abceded3 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 17:28:25 -0500 Subject: [PATCH 14/30] Various fixes --- emcc.py | 4 ++++ src/library_exceptions.js | 4 ++-- system/lib/libcxxabi/{ => src}/format_exception.cpp | 0 tools/system_libs.py | 6 ++---- 4 files changed, 8 insertions(+), 6 deletions(-) rename system/lib/libcxxabi/{ => src}/format_exception.cpp (100%) diff --git a/emcc.py b/emcc.py index c243d53d0492..772edeb2b1a2 100755 --- a/emcc.py +++ b/emcc.py @@ -2074,6 +2074,10 @@ def default_setting(name, new_default): else: settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) + if settings.FORMAT_EXCEPTION_SUPPORT: + settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] + settings.EXPORTED_FUNCTIONS += ["_emscripten_format_exception"] + if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME: # when the filesystem is forced, we export by default methods that filesystem usage # may need, including filesystem usage from standalone file packager output (i.e. diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 061d1cda3f63..a6ffde78c597 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -429,14 +429,14 @@ var LibraryExceptions = { {{{ makeThrow('ptr') }}} }, - $formatException__deps: ["format_exception", "$withStackSave"], + $formatException__deps: ["emscripten_format_exception", "$withStackSave"], $formatException: function(excPtr){ return withStackSave(function(){ var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); _emscripten_format_exception(result_ptr, excPtr); var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; var result = UTF8ToString(utf8_addr); - _free(utf_addr); + _free(utf8_addr); return result; }); }, diff --git a/system/lib/libcxxabi/format_exception.cpp b/system/lib/libcxxabi/src/format_exception.cpp similarity index 100% rename from system/lib/libcxxabi/format_exception.cpp rename to system/lib/libcxxabi/src/format_exception.cpp diff --git a/tools/system_libs.py b/tools/system_libs.py index 18892103b942..0c0dfefd2697 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1127,7 +1127,8 @@ def get_files(self): 'stdlib_exception.cpp', 'stdlib_stdexcept.cpp', 'stdlib_typeinfo.cpp', - 'private_typeinfo.cpp' + 'private_typeinfo.cpp', + 'format_exception.cpp' ] if self.eh_mode == Exceptions.NONE: filenames += ['cxa_noexception.cpp'] @@ -1138,9 +1139,6 @@ def get_files(self): 'cxa_personality.cpp' ] - if settings.FORMAT_EXCEPTION_SUPPORT: - filenames += ["format_exception.cpp"] - return files_in_path( path='system/lib/libcxxabi/src', filenames=filenames) From 5e765ab142836abff308a1c5bf6db51cb31a41e3 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 18:00:03 -0500 Subject: [PATCH 15/30] Add test --- emcc.py | 3 +- src/library_exceptions.js | 2 +- tests/test_core.py | 59 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/emcc.py b/emcc.py index 772edeb2b1a2..1162bf3f1489 100755 --- a/emcc.py +++ b/emcc.py @@ -2076,7 +2076,8 @@ def default_setting(name, new_default): if settings.FORMAT_EXCEPTION_SUPPORT: settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] - settings.EXPORTED_FUNCTIONS += ["_emscripten_format_exception"] + settings.EXPORTED_FUNCTIONS += ["_emscripten_format_exception", "_free"] + settings.EXPORTED_RUNTIME_METHODS += ["formatException"] if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME: # when the filesystem is forced, we export by default methods that filesystem usage diff --git a/src/library_exceptions.js b/src/library_exceptions.js index a6ffde78c597..0f071771e2db 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -429,7 +429,7 @@ var LibraryExceptions = { {{{ makeThrow('ptr') }}} }, - $formatException__deps: ["emscripten_format_exception", "$withStackSave"], + $formatException__deps: ["emscripten_format_exception", "$withStackSave", "free"], $formatException: function(excPtr){ return withStackSave(function(){ var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); diff --git a/tests/test_core.py b/tests/test_core.py index 7dfac2dc7102..33f2a860584c 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1543,6 +1543,65 @@ def test_exceptions_rethrow_missing(self): create_file('main.cpp', 'int main() { throw; }') self.do_runf('main.cpp', None, assert_returncode=NON_ZERO) + @with_both_eh_sjlj + def test_format_exception(self): + # needs to flush stdio streams + self.set_setting('EXIT_RUNTIME') + self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_throw_exc']) + self.set_setting('FORMAT_EXCEPTION_SUPPORT') + self.maybe_closure() + self.do_run( + """ + #include + #include + #include + using namespace std; + + class myexception : public exception + { + virtual const char* what() const throw() { return "My exception happened"; } + } myex; + + extern "C" void + throw_exc(int x) + { + if (x == 1) { + throw 1000; + } + if (x == 2) { + throw 'c'; + } + if (x == 3) { + throw runtime_error("abc"); + } + if (x == 4) { + throw myex; + } + if (x == 5) { + throw "abc"; + } + } + + int + main(){ + EM_ASM({ + for(let i = 1; i < 6; i++){ + try { + Module["_throw_exc"](i); + } catch(p){ + console.log(Module["formatException"](p)); + } + } + }); + } + """, + "Cpp Exception: The exception is an object of type 'int' at address 0x503868 which does not inherit from std::exception\n" + "Cpp Exception: The exception is an object of type 'char' at address 0x503880 which does not inherit from std::exception\n" + "Cpp Exception std::runtime_error: abc\n" + "Cpp Exception myexception: My exception happened\n" + "Cpp Exception: The exception is an object of type 'char const*' at address 0x5038e8 which does not inherit from std::exception\n" + ) + @with_both_eh_sjlj def test_bad_typeid(self): self.do_run(r''' From 123560005c5da30b06934535bc5d8820c345fd49 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 18:06:57 -0500 Subject: [PATCH 16/30] Remove addresses from test comparison --- tests/test_core.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 33f2a860584c..6d0a26b63b9e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1589,17 +1589,17 @@ class myexception : public exception try { Module["_throw_exc"](i); } catch(p){ - console.log(Module["formatException"](p)); + console.log(Module["formatException"](p).replace(/0x[0-9a-f]*/, "xxx")); } } }); } """, - "Cpp Exception: The exception is an object of type 'int' at address 0x503868 which does not inherit from std::exception\n" - "Cpp Exception: The exception is an object of type 'char' at address 0x503880 which does not inherit from std::exception\n" + "Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception\n" + "Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception\n" "Cpp Exception std::runtime_error: abc\n" "Cpp Exception myexception: My exception happened\n" - "Cpp Exception: The exception is an object of type 'char const*' at address 0x5038e8 which does not inherit from std::exception\n" + "Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception\n" ) @with_both_eh_sjlj From a83007e375133243f989f64c874d3fef73636400 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 19:09:36 -0500 Subject: [PATCH 17/30] Try to fix test --- tests/test_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_core.py b/tests/test_core.py index 6d0a26b63b9e..c3f5e4a66c81 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1543,12 +1543,12 @@ def test_exceptions_rethrow_missing(self): create_file('main.cpp', 'int main() { throw; }') self.do_runf('main.cpp', None, assert_returncode=NON_ZERO) - @with_both_eh_sjlj def test_format_exception(self): # needs to flush stdio streams self.set_setting('EXIT_RUNTIME') self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_throw_exc']) self.set_setting('FORMAT_EXCEPTION_SUPPORT') + self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) self.maybe_closure() self.do_run( """ From 5f19ef5c80abd0b6a115799415ea82a33c697d67 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 19:54:19 -0500 Subject: [PATCH 18/30] Fix wasm-exceptions test --- system/lib/libcxxabi/src/format_exception.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/lib/libcxxabi/src/format_exception.cpp b/system/lib/libcxxabi/src/format_exception.cpp index abdce817a4b2..b1babd43350c 100644 --- a/system/lib/libcxxabi/src/format_exception.cpp +++ b/system/lib/libcxxabi/src/format_exception.cpp @@ -3,6 +3,8 @@ #include #include +#ifdef __USING_EMSCRIPTEN_EXCEPTIONS__ + extern "C" { #define DEMANGLED_BUF_SIZE 100 @@ -41,3 +43,5 @@ int emscripten_format_exception(char** result, void* exc_ptr) { return ret; } } + +#endif // __USING_EMSCRIPTEN_EXCEPTIONS__ From 86b703b67b92e1db2fde3d4f256aff864fc6d81c Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 20 Feb 2022 22:02:28 -0500 Subject: [PATCH 19/30] Fix test --- src/library_exceptions.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 0f071771e2db..eec90ff0f0bc 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -429,6 +429,7 @@ var LibraryExceptions = { {{{ makeThrow('ptr') }}} }, +#if !DISABLE_EXCEPTION_CATCHING $formatException__deps: ["emscripten_format_exception", "$withStackSave", "free"], $formatException: function(excPtr){ return withStackSave(function(){ @@ -440,6 +441,7 @@ var LibraryExceptions = { return result; }); }, +#endif }; // In LLVM, exceptions generate a set of functions of form __cxa_find_matching_catch_1(), __cxa_find_matching_catch_2(), etc. From a773e9bd31525b991365c8a5c875da86026652ca Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 09:34:07 -0500 Subject: [PATCH 20/30] Fix INCLUDE_FULL_LIBRARY test --- emcc.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/emcc.py b/emcc.py index 1162bf3f1489..ea114a5bf870 100755 --- a/emcc.py +++ b/emcc.py @@ -2074,8 +2074,12 @@ def default_setting(name, new_default): else: settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) - if settings.FORMAT_EXCEPTION_SUPPORT: - settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] + if ( + settings.FORMAT_EXCEPTION_SUPPORT + or "$formatException" in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE + or settings.INCLUDE_FULL_LIBRARY + ): + settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ["$formatException"] settings.EXPORTED_FUNCTIONS += ["_emscripten_format_exception", "_free"] settings.EXPORTED_RUNTIME_METHODS += ["formatException"] From 16f160d38264fe1c160eb2ffa7c4d1bfb5a7a5ad Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 09:39:40 -0500 Subject: [PATCH 21/30] Fix flake8 --- emcc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/emcc.py b/emcc.py index ea114a5bf870..7d6655590374 100755 --- a/emcc.py +++ b/emcc.py @@ -2075,9 +2075,9 @@ def default_setting(name, new_default): settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) if ( - settings.FORMAT_EXCEPTION_SUPPORT - or "$formatException" in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE - or settings.INCLUDE_FULL_LIBRARY + settings.FORMAT_EXCEPTION_SUPPORT or + "$formatException" in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or + settings.INCLUDE_FULL_LIBRARY ): settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ["$formatException"] settings.EXPORTED_FUNCTIONS += ["_emscripten_format_exception", "_free"] From fbcf6da21d9349e40fc920ec3b90948475d4b3f0 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 21:47:37 -0500 Subject: [PATCH 22/30] Formatting fixes from sbc100 --- src/library_exceptions.js | 4 +-- system/lib/libcxxabi/src/format_exception.cpp | 1 - tests/test_core.py | 31 ++++++++----------- tools/system_libs.py | 2 +- 4 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index eec90ff0f0bc..3ed38377ebd4 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -431,8 +431,8 @@ var LibraryExceptions = { #if !DISABLE_EXCEPTION_CATCHING $formatException__deps: ["emscripten_format_exception", "$withStackSave", "free"], - $formatException: function(excPtr){ - return withStackSave(function(){ + $formatException: function(excPtr) { + return withStackSave(function(){ var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); _emscripten_format_exception(result_ptr, excPtr); var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; diff --git a/system/lib/libcxxabi/src/format_exception.cpp b/system/lib/libcxxabi/src/format_exception.cpp index b1babd43350c..5972ff7a6881 100644 --- a/system/lib/libcxxabi/src/format_exception.cpp +++ b/system/lib/libcxxabi/src/format_exception.cpp @@ -6,7 +6,6 @@ #ifdef __USING_EMSCRIPTEN_EXCEPTIONS__ extern "C" { -#define DEMANGLED_BUF_SIZE 100 int __cxa_can_catch(const std::type_info* catchType, const std::type_info* excpType, diff --git a/tests/test_core.py b/tests/test_core.py index c3f5e4a66c81..6abb3de1be2e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1550,21 +1550,17 @@ def test_format_exception(self): self.set_setting('FORMAT_EXCEPTION_SUPPORT') self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) self.maybe_closure() - self.do_run( - """ + self.do_run(''' #include #include #include using namespace std; - class myexception : public exception - { + class myexception : public exception { virtual const char* what() const throw() { return "My exception happened"; } } myex; - extern "C" void - throw_exc(int x) - { + extern "C" void throw_exc(int x) { if (x == 1) { throw 1000; } @@ -1582,25 +1578,24 @@ class myexception : public exception } } - int - main(){ + int main() { EM_ASM({ - for(let i = 1; i < 6; i++){ + for (let i = 1; i < 6; i++){ try { Module["_throw_exc"](i); - } catch(p){ + } catch(p) { console.log(Module["formatException"](p).replace(/0x[0-9a-f]*/, "xxx")); } } }); } - """, - "Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception\n" - "Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception\n" - "Cpp Exception std::runtime_error: abc\n" - "Cpp Exception myexception: My exception happened\n" - "Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception\n" - ) + ''', ''' + Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception + Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception + Cpp Exception std::runtime_error: abc + Cpp Exception myexception: My exception happened + Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception + ''') @with_both_eh_sjlj def test_bad_typeid(self): diff --git a/tools/system_libs.py b/tools/system_libs.py index 0c0dfefd2697..31035e5fa850 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1128,7 +1128,7 @@ def get_files(self): 'stdlib_stdexcept.cpp', 'stdlib_typeinfo.cpp', 'private_typeinfo.cpp', - 'format_exception.cpp' + 'format_exception.cpp', ] if self.eh_mode == Exceptions.NONE: filenames += ['cxa_noexception.cpp'] From 8ad79321cb258a12270a89f8f6649f8812cd88cd Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 21:52:09 -0500 Subject: [PATCH 23/30] Fix test --- tests/test_core.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 6abb3de1be2e..496fde36cf52 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1550,7 +1550,7 @@ def test_format_exception(self): self.set_setting('FORMAT_EXCEPTION_SUPPORT') self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) self.maybe_closure() - self.do_run(''' + src = ''' #include #include #include @@ -1589,13 +1589,16 @@ class myexception : public exception { } }); } - ''', ''' - Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception - Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception - Cpp Exception std::runtime_error: abc - Cpp Exception myexception: My exception happened - Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception - ''') + ''' + expected = ''' +Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception +Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception +Cpp Exception std::runtime_error: abc +Cpp Exception myexception: My exception happened +Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception + '''.strip() + "\n" + + self.do_run(src, expected) @with_both_eh_sjlj def test_bad_typeid(self): From 68c129e568b3c1c8b6cde49abb9b6bf06a55e913 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 21:58:03 -0500 Subject: [PATCH 24/30] Return char* instead of returning by value to avoid stackAlloc --- src/library_exceptions.js | 12 ++++-------- system/lib/libcxxabi/src/format_exception.cpp | 18 +++++++++--------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index 3ed38377ebd4..d840be63cc71 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -432,14 +432,10 @@ var LibraryExceptions = { #if !DISABLE_EXCEPTION_CATCHING $formatException__deps: ["emscripten_format_exception", "$withStackSave", "free"], $formatException: function(excPtr) { - return withStackSave(function(){ - var result_ptr = stackAlloc({{{ POINTER_SIZE }}}); - _emscripten_format_exception(result_ptr, excPtr); - var utf8_addr = {{{ makeGetValue('result_ptr', '0', '*') }}}; - var result = UTF8ToString(utf8_addr); - _free(utf8_addr); - return result; - }); + var utf8_addr = _emscripten_format_exception(excPtr); + var result = UTF8ToString(utf8_addr); + _free(utf8_addr); + return result; }, #endif }; diff --git a/system/lib/libcxxabi/src/format_exception.cpp b/system/lib/libcxxabi/src/format_exception.cpp index 5972ff7a6881..5d93d666ad82 100644 --- a/system/lib/libcxxabi/src/format_exception.cpp +++ b/system/lib/libcxxabi/src/format_exception.cpp @@ -11,7 +11,7 @@ int __cxa_can_catch(const std::type_info* catchType, const std::type_info* excpType, void** thrown); -int emscripten_format_exception(char** result, void* exc_ptr) { +char* emscripten_format_exception(void* exc_ptr) { __cxxabiv1::__cxa_exception* exc_info = (__cxxabiv1::__cxa_exception*)exc_ptr - 1; std::type_info* exc_type = exc_info->exceptionType; @@ -24,22 +24,22 @@ int emscripten_format_exception(char** result, void* exc_ptr) { } int can_catch = __cxa_can_catch(&typeid(std::exception), exc_type, &exc_ptr); - int ret; + char* result = NULL; if (can_catch) { const char* exc_what = ((std::exception*)exc_ptr)->what(); - ret = asprintf(result, "Cpp Exception %s: %s", exc_name, exc_what); + asprintf(&result, "Cpp Exception %s: %s", exc_name, exc_what); } else { - ret = asprintf(result, - "Cpp Exception: The exception is an object of type '%s' at " - "address %p which does not inherit from std::exception", - exc_name, - exc_ptr); + asprintf(&result, + "Cpp Exception: The exception is an object of type '%s' at " + "address %p which does not inherit from std::exception", + exc_name, + exc_ptr); } if (demangled_buf) { free(demangled_buf); } - return ret; + return result; } } From c11d0361a1cd73dd89abbbbb2066255950e0477f Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 22:01:14 -0500 Subject: [PATCH 25/30] Remove FORMAT_EXCEPTION_SUPPORT setting --- emcc.py | 9 ++++----- src/settings.js | 7 ------- tests/test_core.py | 2 +- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/emcc.py b/emcc.py index 7d6655590374..60a828be0ee6 100755 --- a/emcc.py +++ b/emcc.py @@ -2075,13 +2075,12 @@ def default_setting(name, new_default): settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) if ( - settings.FORMAT_EXCEPTION_SUPPORT or "$formatException" in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or - settings.INCLUDE_FULL_LIBRARY + settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING ): - settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ["$formatException"] - settings.EXPORTED_FUNCTIONS += ["_emscripten_format_exception", "_free"] - settings.EXPORTED_RUNTIME_METHODS += ["formatException"] + settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] + settings.EXPORTED_FUNCTIONS += ['_emscripten_format_exception', '_free'] + settings.EXPORTED_RUNTIME_METHODS += ['formatException'] if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME: # when the filesystem is forced, we export by default methods that filesystem usage diff --git a/src/settings.js b/src/settings.js index b08b0cdcc76b..80354a27b8c8 100644 --- a/src/settings.js +++ b/src/settings.js @@ -339,13 +339,6 @@ var EXCEPTION_DEBUG = 0; // [link] var DEMANGLE_SUPPORT = 0; -// If 1, expose a formatException function that can be used to format a top -// level C++ exception. If the thrown exception was -// throw new std::runtime_error("a big problem occurred") -// It returns a string like: "Cpp Exception std::runtime_error: A big problem occured." -// [link] -var FORMAT_EXCEPTION_SUPPORT = 0; - // Print out when we enter a library call (library*.js). You can also unset // Runtime.debug at runtime for logging to cease, and can set it when you want // it back. A simple way to set it in C++ is diff --git a/tests/test_core.py b/tests/test_core.py index 496fde36cf52..42ebdf51f9bb 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1547,8 +1547,8 @@ def test_format_exception(self): # needs to flush stdio streams self.set_setting('EXIT_RUNTIME') self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_throw_exc']) - self.set_setting('FORMAT_EXCEPTION_SUPPORT') self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) + self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$formatException']) self.maybe_closure() src = ''' #include From 9c0b0a6cb3c9e8d231eb5e752096e7ba8fb0b5d1 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 22:19:03 -0500 Subject: [PATCH 26/30] Address more review comments --- src/library_exceptions.js | 2 +- tests/test_core.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/library_exceptions.js b/src/library_exceptions.js index d840be63cc71..a8a85b3737ff 100644 --- a/src/library_exceptions.js +++ b/src/library_exceptions.js @@ -430,7 +430,7 @@ var LibraryExceptions = { }, #if !DISABLE_EXCEPTION_CATCHING - $formatException__deps: ["emscripten_format_exception", "$withStackSave", "free"], + $formatException__deps: ['emscripten_format_exception', 'free'], $formatException: function(excPtr) { var utf8_addr = _emscripten_format_exception(excPtr); var result = UTF8ToString(utf8_addr); diff --git a/tests/test_core.py b/tests/test_core.py index 42ebdf51f9bb..ddf54b1c6a38 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1590,13 +1590,13 @@ class myexception : public exception { }); } ''' - expected = ''' + expected = '''\ Cpp Exception: The exception is an object of type 'int' at address xxx which does not inherit from std::exception Cpp Exception: The exception is an object of type 'char' at address xxx which does not inherit from std::exception Cpp Exception std::runtime_error: abc Cpp Exception myexception: My exception happened Cpp Exception: The exception is an object of type 'char const*' at address xxx which does not inherit from std::exception - '''.strip() + "\n" +''' self.do_run(src, expected) From 439c09fcbaddbac95eb8c9189b30aeb0fc361b2a Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 22:21:08 -0500 Subject: [PATCH 27/30] Address more review comments --- emcc.py | 2 +- tests/test_core.py | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/emcc.py b/emcc.py index 60a828be0ee6..0b1566359e4f 100755 --- a/emcc.py +++ b/emcc.py @@ -2075,7 +2075,7 @@ def default_setting(name, new_default): settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) if ( - "$formatException" in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or + '$formatException' in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING ): settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] diff --git a/tests/test_core.py b/tests/test_core.py index ddf54b1c6a38..8a1e4b153843 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1546,7 +1546,6 @@ def test_exceptions_rethrow_missing(self): def test_format_exception(self): # needs to flush stdio streams self.set_setting('EXIT_RUNTIME') - self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_throw_exc']) self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$formatException']) self.maybe_closure() @@ -1560,7 +1559,7 @@ class myexception : public exception { virtual const char* what() const throw() { return "My exception happened"; } } myex; - extern "C" void throw_exc(int x) { + EMSCRIPTEN_KEEPALIVE extern "C" void throw_exc(int x) { if (x == 1) { throw 1000; } From 77376ea606185d74e4cd190670ae8636e7a7c714 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 22:21:57 -0500 Subject: [PATCH 28/30] Remove unneeded setting --- tests/test_core.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 8a1e4b153843..6c42691f6c41 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1544,8 +1544,6 @@ def test_exceptions_rethrow_missing(self): self.do_runf('main.cpp', None, assert_returncode=NON_ZERO) def test_format_exception(self): - # needs to flush stdio streams - self.set_setting('EXIT_RUNTIME') self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$formatException']) self.maybe_closure() From 8dad972fb0107712df2c6b5fe2dcf674e4133d05 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Mon, 21 Feb 2022 22:25:51 -0500 Subject: [PATCH 29/30] Add formatException to EXPORTED_FUNCTIONS rather than EXPORTED_RUNTIME_METHODS --- emcc.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/emcc.py b/emcc.py index 0b1566359e4f..4e15c949f80d 100755 --- a/emcc.py +++ b/emcc.py @@ -2079,8 +2079,7 @@ def default_setting(name, new_default): settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING ): settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] - settings.EXPORTED_FUNCTIONS += ['_emscripten_format_exception', '_free'] - settings.EXPORTED_RUNTIME_METHODS += ['formatException'] + settings.EXPORTED_FUNCTIONS += ['formatException', '_emscripten_format_exception', '_free'] if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME: # when the filesystem is forced, we export by default methods that filesystem usage From dcc15f55e7ba6f5d276d0955e810dd80ab96bfc1 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 23 Feb 2022 18:22:29 -0500 Subject: [PATCH 30/30] Edits from code review --- emcc.py | 10 ++++------ tests/test_core.py | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/emcc.py b/emcc.py index 4e15c949f80d..483bb7d53acf 100755 --- a/emcc.py +++ b/emcc.py @@ -2074,12 +2074,10 @@ def default_setting(name, new_default): else: settings.JS_LIBRARIES.append((0, 'library_pthread_stub.js')) - if ( - '$formatException' in settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or - settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING - ): - settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$formatException'] - settings.EXPORTED_FUNCTIONS += ['formatException', '_emscripten_format_exception', '_free'] + # TODO: Move this into the library JS file once it becomes possible. + # See https://github.com/emscripten-core/emscripten/pull/15982 + if settings.INCLUDE_FULL_LIBRARY and not settings.DISABLE_EXCEPTION_CATCHING: + settings.EXPORTED_FUNCTIONS += ['_emscripten_format_exception', '_free'] if settings.FORCE_FILESYSTEM and not settings.MINIMAL_RUNTIME: # when the filesystem is forced, we export by default methods that filesystem usage diff --git a/tests/test_core.py b/tests/test_core.py index 6c42691f6c41..8e4f273207d3 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1546,6 +1546,7 @@ def test_exceptions_rethrow_missing(self): def test_format_exception(self): self.set_setting('DISABLE_EXCEPTION_CATCHING', 0) self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$formatException']) + self.set_setting('EXPORTED_FUNCTIONS', ['_main', 'formatException', '_emscripten_format_exception', '_free']) self.maybe_closure() src = ''' #include