From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2140) id 1A543385700D; Fri, 9 Jun 2023 06:17:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1A543385700D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686291443; bh=Hhcfrh+oeu/t8LDR2mJPH4C+2fp3mHEEjHSDgeva2wo=; h=From:To:Subject:Date:From; b=cS1K5Si4RSG9Dya6Co5vdrsXet8jdWm+kDRySnyTdBA5/37qw9HarSXYHjZ5Qg3Au WvARLJvkWXNZxFsH/opI5lzhl+tq1gAj9mGBfoTkDej3yzcL3jCwvO+QjPx49CjI7/ zx7F3Xc0TzUxU5cOm6FubGAGI6znv07yYLs3c9Ac= 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)] detect infinite loops earlier in returning_call_p X-Act-Checkin: gcc X-Git-Author: Alexandre Oliva X-Git-Refname: refs/users/aoliva/heads/testme X-Git-Oldrev: 4b9c64afbc0e35df2fe5e5cc87a90ebe35564fb5 X-Git-Newrev: 6269d7a415f9fad7a49f03c8b4a2387d21a9ccc9 Message-Id: <20230609061723.1A543385700D@sourceware.org> Date: Fri, 9 Jun 2023 06:17:23 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6269d7a415f9fad7a49f03c8b4a2387d21a9ccc9 commit 6269d7a415f9fad7a49f03c8b4a2387d21a9ccc9 Author: Alexandre Oliva Date: Thu Jun 8 05:55:04 2023 -0300 detect infinite loops earlier in returning_call_p An infinite loop could create a path as long as the block count in returning_call_p, and then fail the backwards check if a call is found before emptying the path. Return as soon as the path exceeds the block count, and search for duplicate blocks before allocating more memory for the path, so as to cut the looping short. for gcc/ChangeLog * gimple-harden-control-flow.cc (returning_call_p): Detect infinite loops sooner. Diff: --- gcc/gimple-harden-control-flow.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/gcc/gimple-harden-control-flow.cc b/gcc/gimple-harden-control-flow.cc index 3998fd0d293..044671cab79 100644 --- a/gcc/gimple-harden-control-flow.cc +++ b/gcc/gimple-harden-control-flow.cc @@ -19,6 +19,7 @@ along with GCC; see the file COPYING3. If not see . */ #include "config.h" +#define INCLUDE_ALGORITHM /* find */ #include "system.h" #include "coretypes.h" #include "backend.h" @@ -211,14 +212,19 @@ returning_call_p (gcall *call) return false; /* Quickly check that there's a path to exit compatible with a - returning call. Detect infinite loops through the counter. */ - basic_block bb = gimple_bb (call); + returning call. Detect infinite loops by limiting the path + length to the basic block count, and by looking for duplicate + blocks before allocating more memory for the path, for amortized + O(n). */ auto_vec path; - for (int i = n_basic_blocks_for_fn (cfun); - bb != EXIT_BLOCK_PTR_FOR_FN (cfun) && i--; + for (basic_block bb = gimple_bb (call); + bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = single_succ (bb)) if (!single_succ_p (bb) - || (single_succ_edge (bb)->flags & EDGE_EH) != 0) + || (single_succ_edge (bb)->flags & EDGE_EH) != 0 + || n_basic_blocks_for_fn (cfun) - path.length () <= NUM_FIXED_BLOCKS + || (path.length () == path.allocated () + && std::find (path.begin (), path.end (), bb) != path.end ())) return false; else path.safe_push (bb);