From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1734) id 312D93858D28; Sat, 4 Mar 2023 18:02:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 312D93858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677952977; bh=krwomc1hUxPNhTIc1F3HVODwS+GuzH6HTqxiQvQR8qc=; h=From:To:Subject:Date:From; b=dJvqNzxnX/jurOHbUZj5EqfBRjfJe4ByMwVA0CZYiuJoB8eI8DJawy3hgISurFOfw FA7I2JRoGst7GWOoWXuTXEN3Uk0g+nGhDm9Qi074ZRkSXLIQXdrWyE3tilJFKxRvex F6xcat37IS0VwnV5IHtOXBkLHGG/K15vE//NnLQs= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Marek Polacek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9218] c-family: avoid compile-time-hog in c_genericize [PR108880] X-Act-Checkin: gcc X-Git-Author: Marek Polacek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 3a2310d4b229e707bbc5440150bf180e0499273a X-Git-Newrev: 6ab4c8759754f34f5285924652238c829960cd4c Message-Id: <20230304180257.312D93858D28@sourceware.org> Date: Sat, 4 Mar 2023 18:02:57 +0000 (GMT) List-Id: https://gcc.gnu.org/g:6ab4c8759754f34f5285924652238c829960cd4c commit r12-9218-g6ab4c8759754f34f5285924652238c829960cd4c Author: Marek Polacek Date: Wed Feb 22 15:17:03 2023 -0500 c-family: avoid compile-time-hog in c_genericize [PR108880] This fixes a compile-time hog with UBSan. This only happened in cc1 but not cc1plus. The problem is ultimately that c_genericize_control_stmt/ STATEMENT_LIST -> walk_tree_1 doesn't use a hash_set to remember visited nodes, so it kept on recursing for a long time. We should be able to use the pset that c_genericize created. We just need to use walk_tree instead of walk_tree_w_d so that the pset is explicit. PR c/108880 gcc/c-family/ChangeLog: * c-gimplify.cc (c_genericize_control_stmt) : Pass pset to walk_tree_1. (c_genericize): Call walk_tree with an explicit pset. gcc/testsuite/ChangeLog: * c-c++-common/ubsan/pr108880.c: New test. (cherry picked from commit 1370014f2ea02ec185cf1199027575916f79fe63) Diff: --- gcc/c-family/c-gimplify.cc | 10 +++++++--- gcc/testsuite/c-c++-common/ubsan/pr108880.c | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc index 7e55fd73f81..5ec1dc5754f 100644 --- a/gcc/c-family/c-gimplify.cc +++ b/gcc/c-family/c-gimplify.cc @@ -510,12 +510,15 @@ c_genericize_control_stmt (tree *stmt_p, int *walk_subtrees, void *data, STATEMENT_LIST wouldn't be present at all the resulting expression wouldn't have TREE_SIDE_EFFECTS set, so make sure to clear it even on the STATEMENT_LIST in such cases. */ + hash_set *pset = (c_dialect_cxx () + ? nullptr + : static_cast *>(data)); for (i = tsi_start (stmt); !tsi_end_p (i); tsi_next (&i)) { tree t = tsi_stmt (i); if (TREE_CODE (t) != DEBUG_BEGIN_STMT && nondebug_stmts < 2) nondebug_stmts++; - walk_tree_1 (tsi_stmt_ptr (i), func, data, NULL, lh); + walk_tree_1 (tsi_stmt_ptr (i), func, data, pset, lh); if (TREE_CODE (t) != DEBUG_BEGIN_STMT && (nondebug_stmts > 1 || TREE_SIDE_EFFECTS (tsi_stmt (i)))) clear_side_effects = false; @@ -570,8 +573,9 @@ c_genericize (tree fndecl) bc_state_t save_state; push_cfun (DECL_STRUCT_FUNCTION (fndecl)); save_bc_state (&save_state); - walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl), - c_genericize_control_r, NULL); + hash_set pset; + walk_tree (&DECL_SAVED_TREE (fndecl), c_genericize_control_r, &pset, + &pset); restore_bc_state (&save_state); pop_cfun (); } diff --git a/gcc/testsuite/c-c++-common/ubsan/pr108880.c b/gcc/testsuite/c-c++-common/ubsan/pr108880.c new file mode 100644 index 00000000000..7d589edcd12 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/pr108880.c @@ -0,0 +1,13 @@ +/* PR c/108880 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=undefined" } */ + +long a; +short b, e; +char c; +int d, f, g; +void h() { + int i; + f &= i ^= (((g &= 0 / d / d % 8 << 0 << 2) % a >> e) / c >> b) / 1 % 8 << 3; +} +int main() {}