Skip to content

Commit 68e3bb0

Browse files
authored
Move __cxa_allocate_exception to native code. NFC (#18292)
1 parent 6d6344d commit 68e3bb0

33 files changed

+46
-71
lines changed

emcc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2620,7 +2620,6 @@ def check_memory_setting(setting):
26202620
settings.USE_WEBGPU or \
26212621
settings.OFFSCREENCANVAS_SUPPORT or \
26222622
settings.LEGACY_GL_EMULATION or \
2623-
not settings.DISABLE_EXCEPTION_CATCHING or \
26242623
settings.ASYNCIFY or \
26252624
settings.WASMFS or \
26262625
settings.DEMANGLE_SUPPORT or \
@@ -2646,6 +2645,7 @@ def check_memory_setting(setting):
26462645
# setThrew(). We cannot handle this using deps_info as the invokes are not
26472646
# emitted because of library function usage, but by codegen itself.
26482647
'setThrew',
2648+
'__cxa_free_exception',
26492649
]
26502650

26512651
if settings.ASYNCIFY:

src/library_exceptions.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,29 +163,6 @@ var LibraryExceptions = {
163163
}
164164
},
165165

166-
// Exceptions
167-
__cxa_allocate_exception__sig: 'pp',
168-
__cxa_allocate_exception: function(size) {
169-
// Thrown object is prepended by exception metadata block
170-
return _malloc(size + {{{ C_STRUCTS.__cxa_exception.__size__ }}}) + {{{ C_STRUCTS.__cxa_exception.__size__ }}};
171-
},
172-
173-
__cxa_free_exception__deps: ['$ExceptionInfo'],
174-
__cxa_free_exception__sig: 'vp',
175-
__cxa_free_exception: function(ptr) {
176-
#if ABORTING_MALLOC || ASSERTIONS
177-
try {
178-
#endif
179-
return _free(new ExceptionInfo(ptr).ptr);
180-
#if ABORTING_MALLOC || ASSERTIONS
181-
} catch(e) {
182-
#if ASSERTIONS
183-
err('exception during __cxa_free_exception: ' + e);
184-
#endif
185-
}
186-
#endif
187-
},
188-
189166
__cxa_increment_exception_refcount__deps: ['$exception_addRef', '$ExceptionInfo'],
190167
__cxa_increment_exception_refcount__sig: 'vp',
191168
__cxa_increment_exception_refcount: function(ptr) {

system/lib/libcxxabi/src/cxa_exception_emscripten.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ __gxx_personality_v0(int version,
3232
}
3333
#endif // !defined(__USING_WASM_EXCEPTIONS__)
3434

35-
#if defined(__USING_EMSCRIPTEN_EXCEPTIONS__) || \
36-
defined(__USING_WASM_EXCEPTIONS__)
37-
3835
using namespace __cxxabiv1;
3936

4037
// Some utility routines are copied from cxa_exception.cpp
@@ -50,6 +47,9 @@ thrown_object_from_cxa_exception(__cxa_exception* exception_header) {
5047
return static_cast<void*>(exception_header + 1);
5148
}
5249

50+
#if defined(__USING_EMSCRIPTEN_EXCEPTIONS__) || \
51+
defined(__USING_WASM_EXCEPTIONS__)
52+
5353
// Get the exception object from the unwind pointer.
5454
// Relies on the structure layout, where the unwind pointer is right in
5555
// front of the user's exception object
@@ -130,3 +130,21 @@ char* __get_exception_terminate_message(void* thrown_object) {
130130
}
131131

132132
#endif // __USING_EMSCRIPTEN_EXCEPTIONS__ || __USING_WASM_EXCEPTIONS__
133+
134+
#ifndef __USING_WASM_EXCEPTIONS__
135+
136+
namespace __cxxabiv1 {
137+
138+
void* __cxa_allocate_exception(size_t size) _NOEXCEPT {
139+
// Thrown object is prepended by exception metadata block
140+
__cxa_exception* ex = (__cxa_exception*)malloc(size + sizeof(__cxa_exception));
141+
return thrown_object_from_cxa_exception(ex);
142+
}
143+
144+
void __cxa_free_exception(void *thrown_object) _NOEXCEPT {
145+
free(cxa_exception_from_thrown_object(thrown_object));
146+
}
147+
148+
}
149+
150+
#endif // !__USING_WASM_EXCEPTIONS__

test/other/metadce/test_metadce_cxx_ctors1.exports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ dynCall_iiiiijj
88
dynCall_jiji
99
dynCall_viijii
1010
main
11-
malloc
1211
memory
1312
stackAlloc
1413
stackRestore

test/other/metadce/test_metadce_cxx_ctors1.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
env.__cxa_allocate_exception
21
env.__cxa_throw
32
env.abort
43
env.emscripten_memcpy_big
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
26638
1+
26496

test/other/metadce/test_metadce_cxx_ctors1.sent

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
__cxa_allocate_exception
21
__cxa_throw
32
abort
43
emscripten_memcpy_big
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
123215
1+
123176

test/other/metadce/test_metadce_cxx_ctors2.exports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ dynCall_iiiiijj
77
dynCall_jiji
88
dynCall_viijii
99
main
10-
malloc
1110
memory
1211
stackAlloc
1312
stackRestore

test/other/metadce/test_metadce_cxx_ctors2.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
env.__cxa_allocate_exception
21
env.__cxa_throw
32
env.abort
43
env.emscripten_memcpy_big
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
26602
1+
26460

test/other/metadce/test_metadce_cxx_ctors2.sent

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
__cxa_allocate_exception
21
__cxa_throw
32
abort
43
emscripten_memcpy_big
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
123129
1+
123082

test/other/metadce/test_metadce_cxx_except.exports

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
__cxa_can_catch
2+
__cxa_free_exception
23
__cxa_is_pointer_type
34
__errno_location
45
__indirect_function_table
@@ -9,10 +10,8 @@ dynCall_iiiiijj
910
dynCall_jiiii
1011
dynCall_jiji
1112
dynCall_viijii
12-
free
1313
getTempRet0
1414
main
15-
malloc
1615
memory
1716
setTempRet0
1817
setThrew

test/other/metadce/test_metadce_cxx_except.imports

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
env.__cxa_allocate_exception
21
env.__cxa_begin_catch
32
env.__cxa_end_catch
43
env.__cxa_find_matching_catch_2
54
env.__cxa_find_matching_catch_3
6-
env.__cxa_free_exception
75
env.__cxa_rethrow
86
env.__cxa_throw
97
env.__cxa_uncaught_exceptions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31469
1+
31300

test/other/metadce/test_metadce_cxx_except.sent

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
__cxa_allocate_exception
21
__cxa_begin_catch
32
__cxa_end_catch
43
__cxa_find_matching_catch_2
54
__cxa_find_matching_catch_3
6-
__cxa_free_exception
75
__cxa_rethrow
86
__cxa_throw
97
__cxa_uncaught_exceptions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
164691
1+
164662

test/other/metadce/test_metadce_cxx_except_wasm.exports

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ dynCall_iiiiij
77
dynCall_iiiiijj
88
dynCall_jiji
99
dynCall_viijii
10-
free
1110
main
12-
malloc
1311
memory
14-
setThrew
1512
stackAlloc
1613
stackRestore
1714
stackSave
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
26200
1+
25974
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
136628
1+
136567

test/other/metadce/test_metadce_cxx_mangle.exports

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
__cxa_can_catch
22
__cxa_demangle
3+
__cxa_free_exception
34
__cxa_is_pointer_type
45
__errno_location
56
__indirect_function_table

test/other/metadce/test_metadce_cxx_mangle.imports

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
env.__cxa_allocate_exception
21
env.__cxa_begin_catch
32
env.__cxa_end_catch
43
env.__cxa_find_matching_catch_2
54
env.__cxa_find_matching_catch_3
6-
env.__cxa_free_exception
75
env.__cxa_rethrow
86
env.__cxa_throw
97
env.__cxa_uncaught_exceptions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
31568
1+
31544

test/other/metadce/test_metadce_cxx_mangle.sent

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
__cxa_allocate_exception
21
__cxa_begin_catch
32
__cxa_end_catch
43
__cxa_find_matching_catch_2
54
__cxa_find_matching_catch_3
6-
__cxa_free_exception
75
__cxa_rethrow
86
__cxa_throw
97
__cxa_uncaught_exceptions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
219853
1+
219840

test/other/metadce/test_metadce_cxx_noexcept.exports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ dynCall_iiiiijj
88
dynCall_jiji
99
dynCall_viijii
1010
main
11-
malloc
1211
memory
1312
stackAlloc
1413
stackRestore

test/other/metadce/test_metadce_cxx_noexcept.imports

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
env.__cxa_allocate_exception
21
env.__cxa_throw
32
env.abort
43
env.emscripten_memcpy_big
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
26638
1+
26496

test/other/metadce/test_metadce_cxx_noexcept.sent

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
__cxa_allocate_exception
21
__cxa_throw
32
abort
43
emscripten_memcpy_big
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
125959
1+
125909

test/test_core.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,10 @@ def test_exceptions_allowed(self):
14541454
if '-fsanitize=leak' not in self.emcc_args:
14551455
self.assertGreater(size - empty_size, 0.01 * size)
14561456
# full disable can remove a little bit more
1457-
self.assertLess(disabled_size, fake_size)
1457+
# For some reason this no longer holds true at high optimizations
1458+
# levels: https://github.com/emscripten-core/emscripten/issues/18312
1459+
if not any(o in self.emcc_args for o in ('-O3', '-Oz', '-Os')):
1460+
self.assertLess(disabled_size, fake_size)
14581461

14591462
@no_wasm64('MEMORY64 does not yet support exceptions')
14601463
def test_exceptions_allowed_2(self):
@@ -1649,7 +1652,7 @@ def test_EXPORT_EXCEPTION_HANDLING_HELPERS(self):
16491652
self.emcc_args.append('-D__USING_EMSCRIPTEN_EXCEPTION__')
16501653

16511654
self.maybe_closure()
1652-
src = '''
1655+
create_file('main.cpp', '''
16531656
#include <emscripten.h>
16541657
#include <exception>
16551658
#include <stdexcept>
@@ -1702,7 +1705,7 @@ class myexception : public exception {
17021705
}
17031706
});
17041707
}
1705-
'''
1708+
''')
17061709
expected = '''\
17071710
int,
17081711
char,
@@ -1711,7 +1714,7 @@ class myexception : public exception {
17111714
char const*,
17121715
'''
17131716

1714-
self.do_run(src, expected)
1717+
self.do_runf('main.cpp', expected)
17151718

17161719
@with_both_eh_sjlj
17171720
def test_bad_typeid(self):
@@ -4700,8 +4703,8 @@ def test_dylink_postsets_chunking(self):
47004703
@parameterized({
47014704
'libcxx': ('libc,libc++,libmalloc,libc++abi',),
47024705
'all': ('1',),
4703-
'missing': ('libc,libmalloc', False, False, False),
4704-
'missing_assertions': ('libc,libmalloc', False, False, True),
4706+
'missing': ('libc,libmalloc,libc++abi', False, False, False),
4707+
'missing_assertions': ('libc,libmalloc,libc++abi', False, False, True),
47054708
})
47064709
def test_dylink_syslibs(self, syslibs, expect_pass=True, need_reverse=True, assertions=True):
47074710
# one module uses libcxx, need to force its inclusion when it isn't the main

tools/deps_info.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@
6161
'SDL_PushEvent': ['malloc', 'free'],
6262
'SDL_free': ['free'],
6363
'SDL_malloc': ['malloc', 'free'],
64-
'__cxa_allocate_exception': ['malloc'],
65-
'__cxa_end_catch': ['setThrew', 'free'],
66-
'__cxa_free_exception': ['free'],
6764
'_embind_register_class': ['free'],
6865
'_embind_register_enum_value': ['free'],
6966
'_embind_register_function': ['free'],
@@ -193,7 +190,7 @@
193190

194191
def get_deps_info():
195192
if not settings.WASM_EXCEPTIONS and settings.LINK_AS_CXX:
196-
_deps_info['__cxa_begin_catch'] = ['__cxa_is_pointer_type']
193+
_deps_info['__cxa_begin_catch'] = ['__cxa_is_pointer_type', '__cxa_free_exception']
197194
_deps_info['__cxa_throw'] = ['__cxa_is_pointer_type']
198195
_deps_info['__cxa_find_matching_catch'] = ['__cxa_can_catch', 'setTempRet0']
199196
_deps_info['__cxa_find_matching_catch_1'] = ['__cxa_can_catch', 'setTempRet0']

0 commit comments

Comments
 (0)