From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id DF79E385734C; Thu, 11 Aug 2022 02:50:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF79E385734C Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alexandre Oliva To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/aoliva/heads/testme)] hardcfr: add checking at exceptions and noreturn calls: tweaks X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: 0ab4e8d54edbf52d34639dedcea3cb19eddb1b7a X-Git-Newrev: f321a92432dfa2abaf0171dc7b0284c247a92c35 Message-Id: <20220811025009.DF79E385734C@sourceware.org> Date: Thu, 11 Aug 2022 02:50:09 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Aug 2022 02:50:10 -0000 https://gcc.gnu.org/g:f321a92432dfa2abaf0171dc7b0284c247a92c35 commit f321a92432dfa2abaf0171dc7b0284c247a92c35 Author: Alexandre Oliva Date: Wed Aug 10 22:56:28 2022 -0300 hardcfr: add checking at exceptions and noreturn calls: tweaks Diff: --- gcc/gimple-harden-control-flow.cc | 138 ++++++++++++++++++++++---------------- 1 file changed, 82 insertions(+), 56 deletions(-) diff --git a/gcc/gimple-harden-control-flow.cc b/gcc/gimple-harden-control-flow.cc index 5066a43fe6c..837f498c86d 100644 --- a/gcc/gimple-harden-control-flow.cc +++ b/gcc/gimple-harden-control-flow.cc @@ -669,7 +669,7 @@ public: /* Add to BB code to set its bit in VISITED, and add to RTCFG or CKSEQ the data or code needed to check BB's predecessors and successors. Do NOT change the CFG. */ - void visit (basic_block bb) + void visit (basic_block bb, bool noreturn) { /* Set the bit in VISITED when entering the block. */ gimple_stmt_iterator gsi = gsi_after_labels (bb); @@ -689,10 +689,13 @@ public: rtcfg = tree_cons (NULL_TREE, build_int_cst (vword_type, 0), rtcfg); /* Then, successors. */ - for (int i = EDGE_COUNT (bb->succs); i--; ) - if (push_rtcfg_pair (EDGE_SUCC (bb, i)->dest, bb, - EXIT_BLOCK_PTR_FOR_FN (cfun))) - break; + if (!noreturn + || !push_rtcfg_pair (EXIT_BLOCK_PTR_FOR_FN (cfun), + bb, EXIT_BLOCK_PTR_FOR_FN (cfun))) + for (int i = EDGE_COUNT (bb->succs); i--; ) + if (push_rtcfg_pair (EDGE_SUCC (bb, i)->dest, bb, + EXIT_BLOCK_PTR_FOR_FN (cfun))) + break; rtcfg = tree_cons (NULL_TREE, build_int_cst (vword_type, 0), rtcfg); } else @@ -713,6 +716,8 @@ public: gassign *blkruns = gimple_build_assign (ckpart, unshare_expr (bit)); gimple_seq_add_stmt (&ckseq, blkruns); + if (noreturn) + build_block_check (EXIT_BLOCK_PTR_FOR_FN (cfun)); for (int i = 0, e = EDGE_COUNT (bb->succs); i < e; i++) build_block_check (EDGE_SUCC (bb, i)->dest); @@ -729,10 +734,12 @@ public: unsigned int pass_harden_control_flow_redundancy::execute (function *fun) { + bool param_check_at_escaping_exceptions = true; + bool param_check_before_noreturn_calls = true; basic_block bb_eh_cleanup = NULL; basic_block bb; - if (flag_exceptions) + if (param_check_at_escaping_exceptions && flag_exceptions) { int lp_eh_cleanup = -1; @@ -768,6 +775,11 @@ pass_harden_control_flow_redundancy::execute (function *fun) if (!stmt_ends_bb_p (stmt)) split_block (bb, stmt); + /* A noreturn call needs not be associated with the + cleanup handler, because we'll add checking before + the call. */ + else if (EDGE_COUNT (bb->succs) == 0) + continue; if (!bb_eh_cleanup) { @@ -791,14 +803,24 @@ pass_harden_control_flow_redundancy::execute (function *fun) gresx *resx = gimple_build_resx (new_r->index); gsi_insert_before (&ehgsi, resx, GSI_SAME_STMT); } - else + else if (dom_info_available_p (CDI_DOMINATORS)) { - // Update immedite dominator and loop? + basic_block immdom; + immdom = get_immediate_dominator (CDI_DOMINATORS, + bb_eh_cleanup); + if (!dominated_by_p (CDI_DOMINATORS, immdom, bb)) + { + immdom = nearest_common_dominator (CDI_DOMINATORS, + immdom, bb); + set_immediate_dominator (CDI_DOMINATORS, + bb_eh_cleanup, immdom); + } } add_stmt_to_eh_lp (stmt, lp_eh_cleanup); /* Finally, wire the EH cleanup block into the CFG. */ - make_eh_edges (stmt); } + make_eh_edges (stmt); + } } } @@ -809,57 +831,61 @@ pass_harden_control_flow_redundancy::execute (function *fun) int count_noreturn = 0; auto_sbitmap noreturn_blocks (last_basic_block_for_fn (fun)); bitmap_clear (noreturn_blocks); - FOR_EACH_BB_FN (bb, fun) - { - if (EDGE_COUNT (bb->succs) == 0) - { - if (bitmap_set_bit (noreturn_blocks, bb->index)) - count_noreturn++; + if (!param_check_before_noreturn_calls) + FOR_EACH_BB_FN (bb, fun) + { + if (EDGE_COUNT (bb->succs) == 0) + { + if (bitmap_set_bit (noreturn_blocks, bb->index)) + count_noreturn++; + continue; + } + + /* If there are no exceptions, then any noreturn call must have + zero successor edges. Otherwise, check for blocks without + non-EH successors, but skip those with resx stmts and edges + (i.e., those other than that in bb_eh_cleanup), since those + will go through bb_eh_cleanup, that will have been counted as + noreturn above because it has no successors. */ + gcc_checking_assert (bb != bb_eh_cleanup); + if (!flag_exceptions) continue; - } - /* If there are no exceptions, then any noreturn call must have - zero successor edges. Otherwise, check for blocks without - non-EH successors, but skip those with resx stmts and edges - (i.e., those other than that in bb_eh_cleanup), since those - will go through bb_eh_cleanup, that will have been counted as - noreturn above because it has no successors. */ - gcc_checking_assert (bb != bb_eh_cleanup); - if (!flag_exceptions) - continue; - - bool found_non_eh_edge = false; - bool found_eh_edge = false; - edge e; - edge_iterator ei; - FOR_EACH_EDGE (e, ei, bb->succs) - { - if ((e->flags & EDGE_EH)) - found_eh_edge = true; - else - found_non_eh_edge = true; - if (found_non_eh_edge && found_eh_edge) - break; - } + bool found_non_eh_edge = false; + bool found_eh_edge = false; + edge e; + edge_iterator ei; + FOR_EACH_EDGE (e, ei, bb->succs) + { + if ((e->flags & EDGE_EH)) + found_eh_edge = true; + else + found_non_eh_edge = true; + if (found_non_eh_edge && found_eh_edge) + break; + } - if (found_non_eh_edge) - continue; + if (found_non_eh_edge) + continue; - if (found_eh_edge) - { - /* We don't wish to check before (re?)raises, those will - have checking performed at bb_eh_cleanup. The one - exception is bb_eh_cleanup itself. */ - gimple_stmt_iterator gsi = gsi_last_bb (bb); - gcc_checking_assert (!gsi_end_p (gsi)); - gimple *stmt = gsi_stmt (gsi); - if (is_a (stmt)) - continue; - } + if (found_eh_edge) + { + /* We don't wish to check before (re?)raises, those will + have checking performed at bb_eh_cleanup. The one + exception is bb_eh_cleanup itself. */ + gimple_stmt_iterator gsi = gsi_last_bb (bb); + gcc_checking_assert (!gsi_end_p (gsi)); + gimple *stmt = gsi_stmt (gsi); + if (is_a (stmt)) + continue; + } - if (bitmap_set_bit (noreturn_blocks, bb->index)) - count_noreturn++; - } + if (bitmap_set_bit (noreturn_blocks, bb->index)) + count_noreturn++; + } + else if (bb_eh_cleanup + && bitmap_set_bit (noreturn_blocks, bb_eh_cleanup->index)) + count_noreturn++; gcc_checking_assert (!bb_eh_cleanup || bitmap_bit_p (noreturn_blocks, bb_eh_cleanup->index)); @@ -874,7 +900,7 @@ pass_harden_control_flow_redundancy::execute (function *fun) rt_bb_visited vstd (count_noreturn); FOR_EACH_BB_FN (bb, fun) - vstd.visit (bb); + vstd.visit (bb, bitmap_bit_p (noreturn_blocks, bb->index)); vstd.check (count_noreturn, noreturn_blocks);