Skip to content

Commit 2415024

Browse files
committed
c++: use __cxa_call_terminate for MUST_NOT_THROW [PR97720]
[except.handle]/7 says that when we enter std::terminate due to a throw, that is considered an active handler. We already implemented that properly for the case of not finding a handler (__cxa_throw calls __cxa_begin_catch before std::terminate) and the case of finding a callsite with no landing pad (the personality function calls __cxa_call_terminate which calls __cxa_begin_catch), but for the case of a throw in a try/catch in a noexcept function, we were emitting a cleanup that calls std::terminate directly without ever calling __cxa_begin_catch to handle the exception. A straightforward way to fix this seems to be calling __cxa_call_terminate instead. However, that requires exporting it from libstdc++, which we have not previously done. Despite the name, it isn't actually part of the ABI standard. Nor is __cxa_call_unexpected, as far as I can tell, but that one is also used by clang. For this case they use __clang_call_terminate; it seems reasonable to me for us to stick with __cxa_call_terminate. I also change __cxa_call_terminate to take void* for simplicity in the front end (and consistency with __cxa_call_unexpected) but that isn't necessary if it's undesirable for some reason. This patch does not fix the issue that representing the noexcept as a cleanup is wrong, and confuses the handler search; since it looks like a cleanup in the EH tables, the unwinder keeps looking until it finds the catch in main(), which it should never have gotten to. Without the try/catch in main, the unwinder would reach the end of the stack and say no handler was found. The noexcept is a handler, and should be treated as one, as it is when the landing pad is omitted. The best fix for that issue seems to me to be to represent an ERT_MUST_NOT_THROW after an ERT_TRY in an action list as though it were an ERT_ALLOWED_EXCEPTIONS (since indeed it is an exception-specification). The actual code generation shouldn't need to change (apart from the change made by this patch), only the action table entry. PR c++/97720 gcc/cp/ChangeLog: * cp-tree.h (enum cp_tree_index): Add CPTI_CALL_TERMINATE_FN. (call_terminate_fn): New macro. * cp-gimplify.cc (gimplify_must_not_throw_expr): Use it. * except.cc (init_exception_processing): Set it. (cp_protect_cleanup_actions): Return it. gcc/ChangeLog: * tree-eh.cc (lower_resx): Pass the exception pointer to the failure_decl. * except.h: Tweak comment. libstdc++-v3/ChangeLog: * libsupc++/eh_call.cc (__cxa_call_terminate): Take void*. * config/abi/pre/gnu.ver: Add it. gcc/testsuite/ChangeLog: * g++.dg/eh/terminate2.C: New test.
1 parent 3991b2f commit 2415024

File tree

8 files changed

+63
-5
lines changed

8 files changed

+63
-5
lines changed

gcc/cp/cp-gimplify.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ gimplify_must_not_throw_expr (tree *expr_p, gimple_seq *pre_p)
343343
gimple *mnt;
344344

345345
gimplify_and_add (body, &try_);
346-
mnt = gimple_build_eh_must_not_throw (terminate_fn);
346+
mnt = gimple_build_eh_must_not_throw (call_terminate_fn);
347347
gimple_seq_add_stmt_without_update (&catch_, mnt);
348348
mnt = gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH);
349349

gcc/cp/cp-tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ enum cp_tree_index
217217
definitions. */
218218
CPTI_ALIGN_TYPE,
219219
CPTI_TERMINATE_FN,
220+
CPTI_CALL_TERMINATE_FN,
220221
CPTI_CALL_UNEXPECTED_FN,
221222

222223
/* These are lazily inited. */
@@ -358,6 +359,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX];
358359
/* Exception handling function declarations. */
359360
#define terminate_fn cp_global_trees[CPTI_TERMINATE_FN]
360361
#define call_unexpected_fn cp_global_trees[CPTI_CALL_UNEXPECTED_FN]
362+
#define call_terminate_fn cp_global_trees[CPTI_CALL_TERMINATE_FN]
361363
#define get_exception_ptr_fn cp_global_trees[CPTI_GET_EXCEPTION_PTR_FN]
362364
#define begin_catch_fn cp_global_trees[CPTI_BEGIN_CATCH_FN]
363365
#define end_catch_fn cp_global_trees[CPTI_END_CATCH_FN]

gcc/cp/except.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ init_exception_processing (void)
6464
tmp = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
6565
call_unexpected_fn
6666
= push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp);
67+
call_terminate_fn
68+
= push_library_fn (get_identifier ("__cxa_call_terminate"), tmp, NULL_TREE,
69+
ECF_NORETURN | ECF_COLD | ECF_NOTHROW);
6770
}
6871

6972
/* Returns an expression to be executed if an unhandled exception is
@@ -76,7 +79,7 @@ cp_protect_cleanup_actions (void)
7679
7780
When the destruction of an object during stack unwinding exits
7881
using an exception ... void terminate(); is called. */
79-
return terminate_fn;
82+
return call_terminate_fn;
8083
}
8184

8285
static tree

gcc/except.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ struct GTY(()) eh_region_d
155155
struct eh_region_u_must_not_throw {
156156
/* A function decl to be invoked if this region is actually reachable
157157
from within the function, rather than implementable from the runtime.
158-
The normal way for this to happen is for there to be a CLEANUP region
158+
The normal way for this to happen is for there to be a TRY region
159159
contained within this MUST_NOT_THROW region. Note that if the
160160
runtime handles the MUST_NOT_THROW region, we have no control over
161161
what termination function is called; it will be decided by the

gcc/testsuite/g++.dg/eh/terminate2.C

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// PR c++/97720
2+
// { dg-do run }
3+
4+
// Test that there is an active exception when we reach the terminate handler.
5+
6+
#include <exception>
7+
#include <cstdlib>
8+
9+
void bad_guy() throw() {
10+
try { throw 0; }
11+
catch (float) { }
12+
// Don't catch int.
13+
}
14+
15+
void level1() {
16+
bad_guy();
17+
throw "dead code";
18+
}
19+
20+
void my_term()
21+
{
22+
try { throw; }
23+
catch(...) { std::exit(0); }
24+
}
25+
26+
int main() {
27+
std::set_terminate (my_term);
28+
try { level1(); }
29+
catch (int) { }
30+
}

gcc/tree-eh.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3382,8 +3382,22 @@ lower_resx (basic_block bb, gresx *stmt,
33823382
lab = gimple_block_label (new_bb);
33833383
gsi2 = gsi_start_bb (new_bb);
33843384

3385+
/* Handle failure fns that expect either no arguments or the
3386+
exception pointer. */
33853387
fn = dst_r->u.must_not_throw.failure_decl;
3386-
x = gimple_build_call (fn, 0);
3388+
if (TYPE_ARG_TYPES (TREE_TYPE (fn)) != void_list_node)
3389+
{
3390+
tree epfn = builtin_decl_implicit (BUILT_IN_EH_POINTER);
3391+
src_nr = build_int_cst (integer_type_node, src_r->index);
3392+
x = gimple_build_call (epfn, 1, src_nr);
3393+
tree var = create_tmp_var (ptr_type_node);
3394+
var = make_ssa_name (var, x);
3395+
gimple_call_set_lhs (x, var);
3396+
gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
3397+
x = gimple_build_call (fn, 1, var);
3398+
}
3399+
else
3400+
x = gimple_build_call (fn, 0);
33873401
gimple_set_location (x, dst_r->u.must_not_throw.failure_loc);
33883402
gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING);
33893403

libstdc++-v3/config/abi/pre/gnu.ver

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2841,6 +2841,13 @@ CXXABI_1.3.14 {
28412841

28422842
} CXXABI_1.3.13;
28432843

2844+
CXXABI_1.3.15 {
2845+
2846+
global:
2847+
__cxa_call_terminate;
2848+
2849+
} CXXABI_1.3.14;
2850+
28442851
# Symbols in the support library (libsupc++) supporting transactional memory.
28452852
CXXABI_TM_1 {
28462853

libstdc++-v3/libsupc++/eh_call.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ using namespace __cxxabiv1;
3636
// terminate.
3737

3838
extern "C" void
39-
__cxa_call_terminate(_Unwind_Exception* ue_header) throw ()
39+
__cxa_call_terminate(void* ue_header_in) throw ()
4040
{
41+
_Unwind_Exception* ue_header
42+
= reinterpret_cast<_Unwind_Exception*>(ue_header_in);
4143

4244
if (ue_header)
4345
{

0 commit comments

Comments
 (0)