From patchwork Wed Feb 22 22:37:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1746582 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=NVSxVjDr; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PMWJt5cCjz245s for ; Thu, 23 Feb 2023 09:38:21 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1234B3858404 for ; Wed, 22 Feb 2023 22:38:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1234B3858404 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677105499; bh=58RWolsQRKHxWExKtYJ/a/0nm3W9nZSyMGQTMD5lYs8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=NVSxVjDre4ma/x2md9RHtNVmYK9LBxDq/qc7wqJVh3WWWXVltyedRpQDfSEaOqMNY Yi3/xVdHiIO6RQ3JdDB3M8awAaZMvI3VZqnXKsQMEstkNQLEsNDifOaeu+i/LJXhIP eyoq4/t9VIp4i3Q4LVTeb/0kpZl62a22W+T2algc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C5AC63858C83 for ; Wed, 22 Feb 2023 22:37:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C5AC63858C83 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-653-4sxjaEBPM5aIRmftPtFjaQ-1; Wed, 22 Feb 2023 17:37:53 -0500 X-MC-Unique: 4sxjaEBPM5aIRmftPtFjaQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E0B10833948 for ; Wed, 22 Feb 2023 22:37:52 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.18.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id B992F2166B29; Wed, 22 Feb 2023 22:37:52 +0000 (UTC) To: GCC Patches Cc: Jakub Jelinek Subject: [PATCH] c-family: avoid compile-time-hog in c_genericize [PR108880] Date: Wed, 22 Feb 2023 17:37:45 -0500 Message-Id: <20230222223745.166070-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? 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. --- gcc/c-family/c-gimplify.cc | 10 +++++++--- gcc/testsuite/c-c++-common/ubsan/pr108880.c | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/ubsan/pr108880.c base-commit: 31303c9b5bab200754cdb7ef8cd91ae4918f3018 diff --git a/gcc/c-family/c-gimplify.cc b/gcc/c-family/c-gimplify.cc index b57546ad7a0..74b276b2b26 100644 --- a/gcc/c-family/c-gimplify.cc +++ b/gcc/c-family/c-gimplify.cc @@ -511,12 +511,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; @@ -572,8 +575,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() {}