diff --git a/src/lib/libemval.js b/src/lib/libemval.js index 139bfa28a3f64..5fae02f8371f1 100644 --- a/src/lib/libemval.js +++ b/src/lib/libemval.js @@ -452,7 +452,6 @@ ${functionBody} #if !DISABLE_EXCEPTION_CATCHING var info = new ExceptionInfo(object.excPtr); info.set_caught(false); - info.set_rethrown(false); exceptionLast = object; #endif incrementUncaughtExceptionCount(); diff --git a/src/lib/libexceptions.js b/src/lib/libexceptions.js index 2bc614264fbfc..d23af04d6ce21 100644 --- a/src/lib/libexceptions.js +++ b/src/lib/libexceptions.js @@ -54,15 +54,6 @@ var LibraryExceptions = { return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.caught, 'i8') }}} != 0; } - set_rethrown(rethrown) { - rethrown = rethrown ? 1 : 0; - {{{ makeSetValue('this.ptr', C_STRUCTS.__cxa_exception.rethrown, 'rethrown', 'i8') }}}; - } - - get_rethrown() { - return {{{ makeGetValue('this.ptr', C_STRUCTS.__cxa_exception.rethrown, 'i8') }}} != 0; - } - // Initialize native structure fields. Should be called once after allocated. init(type, destructor) { #if EXCEPTION_DEBUG @@ -116,9 +107,6 @@ var LibraryExceptions = { __Unwind_RaiseException(ptr); }, - // This exception will be caught twice, but while begin_catch runs twice, - // we early-exit from end_catch when the exception has been rethrown, so - // pop that here from the caught exceptions. __cxa_rethrow__deps: ['$exceptionCaught', '$uncaughtExceptionCount', #if !DISABLE_EXCEPTION_CATCHING '$exceptionLast', @@ -132,7 +120,6 @@ var LibraryExceptions = { } var info = exceptionCaught.at(-1); var ptr = info.excPtr; - info.set_rethrown(true); info.set_caught(false); uncaughtExceptionCount++; #if !DISABLE_EXCEPTION_CATCHING @@ -156,7 +143,6 @@ var LibraryExceptions = { info.set_caught(true); uncaughtExceptionCount--; } - info.set_rethrown(false); exceptionCaught.push(info); #if EXCEPTION_DEBUG dbg('__cxa_begin_catch ' + [ptrToString(ptr), 'stack', exceptionCaught]); @@ -227,7 +213,6 @@ var LibraryExceptions = { dbg('__cxa_rethrow_primary_exception: ' + ptrToString(ptr)); #endif var info = new ExceptionInfo(ptr); - info.set_rethrown(true); info.set_caught(false); uncaughtExceptionCount++; #if !DISABLE_EXCEPTION_CATCHING diff --git a/src/struct_info_cxx.json b/src/struct_info_cxx.json index f5b03ee3e940b..8834891902d21 100644 --- a/src/struct_info_cxx.json +++ b/src/struct_info_cxx.json @@ -6,7 +6,6 @@ "exceptionDestructor", "exceptionType", "caught", - "rethrown", "adjustedPtr" ] } diff --git a/src/struct_info_generated.json b/src/struct_info_generated.json index 2785d909cddc2..3b332aa27188d 100644 --- a/src/struct_info_generated.json +++ b/src/struct_info_generated.json @@ -927,8 +927,7 @@ "adjustedPtr": 16, "caught": 12, "exceptionDestructor": 8, - "exceptionType": 4, - "rethrown": 13 + "exceptionType": 4 }, "__wasi_fdstat_t": { "__size__": 24, diff --git a/src/struct_info_generated_wasm64.json b/src/struct_info_generated_wasm64.json index 6e6037da5af95..8bd29989a9ff1 100644 --- a/src/struct_info_generated_wasm64.json +++ b/src/struct_info_generated_wasm64.json @@ -927,8 +927,7 @@ "adjustedPtr": 32, "caught": 24, "exceptionDestructor": 16, - "exceptionType": 8, - "rethrown": 25 + "exceptionType": 8 }, "__wasi_fdstat_t": { "__size__": 24, diff --git a/system/lib/libcxxabi/src/cxa_exception.h b/system/lib/libcxxabi/src/cxa_exception.h index e7dc8d899a11d..9e0411ce4a499 100644 --- a/system/lib/libcxxabi/src/cxa_exception.h +++ b/system/lib/libcxxabi/src/cxa_exception.h @@ -27,7 +27,6 @@ struct _LIBCXXABI_HIDDEN __cxa_exception { // In wasm, destructors return 'this' as in ARM void* (*exceptionDestructor)(void *); uint8_t caught; - uint8_t rethrown; void *adjustedPtr; // Add padding to ensure that the size of __cxa_exception is a multiple of // the maximum useful alignment for the target machine. This ensures that diff --git a/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp b/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp index b251112ae9d2c..304abace086cb 100644 --- a/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp +++ b/system/lib/libcxxabi/src/cxa_exception_emscripten.cpp @@ -4,10 +4,9 @@ // TODO(sbc): consider merging them // // Notable changes: - // __cxa_allocate_exception doesn't add get_cxa_exception_offset -// __cxa_decrement_exception_refcount doesn't call the destructor if rethrown -// Both of these changes are mirrored from the historical JS implementation of -// these functions. +// __cxa_allocate_exception doesn't add get_cxa_exception_offset +// This change is mirrored from the historical JS implementation of +// this function. // //===----------------------------------------------------------------------===// @@ -144,10 +143,9 @@ void __cxa_decrement_exception_refcount(void *thrown_object) throw() { if (thrown_object != NULL ) { __cxa_exception* exception_header = cxa_exception_from_thrown_object(thrown_object); - DEBUG("DEC: %p refcnt=%zu rethrown=%d", thrown_object, - exception_header->referenceCount, exception_header->rethrown); + DEBUG("DEC: %p refcnt=%zu", thrown_object, exception_header->referenceCount); assert(exception_header->referenceCount > 0); - if (std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(-1)) == 0 && !exception_header->rethrown) + if (std::__libcpp_atomic_add(&exception_header->referenceCount, size_t(-1)) == 0) { DEBUG("DEL: %p (dtor=%p)", thrown_object, exception_header->exceptionDestructor); if (NULL != exception_header->exceptionDestructor) diff --git a/test/codesize/test_codesize_cxx_except.json b/test/codesize/test_codesize_cxx_except.json index 7adbe27b1e474..b8d06afd9c035 100644 --- a/test/codesize/test_codesize_cxx_except.json +++ b/test/codesize/test_codesize_cxx_except.json @@ -1,10 +1,10 @@ { - "a.out.js": 22820, - "a.out.js.gz": 9041, - "a.out.nodebug.wasm": 176407, - "a.out.nodebug.wasm.gz": 58760, - "total": 199227, - "total_gz": 67801, + "a.out.js": 22796, + "a.out.js.gz": 9032, + "a.out.nodebug.wasm": 176397, + "a.out.nodebug.wasm.gz": 58755, + "total": 199193, + "total_gz": 67787, "sent": [ "__cxa_begin_catch", "__cxa_end_catch", diff --git a/test/codesize/test_codesize_cxx_mangle.json b/test/codesize/test_codesize_cxx_mangle.json index 6d6c7c3e9b335..d064434a7d548 100644 --- a/test/codesize/test_codesize_cxx_mangle.json +++ b/test/codesize/test_codesize_cxx_mangle.json @@ -1,10 +1,10 @@ { - "a.out.js": 22870, - "a.out.js.gz": 9061, - "a.out.nodebug.wasm": 242680, - "a.out.nodebug.wasm.gz": 80978, - "total": 265550, - "total_gz": 90039, + "a.out.js": 22846, + "a.out.js.gz": 9052, + "a.out.nodebug.wasm": 242670, + "a.out.nodebug.wasm.gz": 80973, + "total": 265516, + "total_gz": 90025, "sent": [ "__cxa_begin_catch", "__cxa_end_catch", diff --git a/test/core/test_exceptions_refcount_2.cpp b/test/core/test_exceptions_refcount_2.cpp new file mode 100644 index 0000000000000..80836f064b578 --- /dev/null +++ b/test/core/test_exceptions_refcount_2.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +class MyException : public std::exception { +public: + ~MyException() { puts("~MyException()"); } +}; + +EMSCRIPTEN_KEEPALIVE extern "C" void rethrow() { + try { + throw MyException(); + } catch (...) { + throw; + } +} + +int main() { + EM_ASM({ + try { + _rethrow(); + } catch (e) { + decrementExceptionRefcount(e); + } + }); +} diff --git a/test/core/test_exceptions_refcount_2.out b/test/core/test_exceptions_refcount_2.out new file mode 100644 index 0000000000000..328c3735f30bb --- /dev/null +++ b/test/core/test_exceptions_refcount_2.out @@ -0,0 +1 @@ +~MyException() diff --git a/test/test_core.py b/test/test_core.py index ccd3a7bc2f33f..d95f8338eba2e 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -1478,6 +1478,11 @@ def test_exceptions_destroy_virtual(self): def test_exceptions_refcount(self): self.do_core_test('test_exceptions_refcount.cpp') + @with_all_eh_sjlj + def test_exceptions_refcount_2(self): + self.set_setting('ASSERTIONS') + self.do_core_test('test_exceptions_refcount_2.cpp') + @with_all_eh_sjlj def test_exceptions_primary(self): self.do_core_test('test_exceptions_primary.cpp')