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: 60713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp850769wrd; Wed, 22 Feb 2023 14:38:44 -0800 (PST) X-Google-Smtp-Source: AK7set9m0sopJdTfmc2b6APpRqyxWl1FKRDt8XaZ69Zo2WQEdXlIvKwiJFwSxLRBHDvH1M6DDGCN X-Received: by 2002:a17:906:ecb2:b0:8b1:c5aa:4c8b with SMTP id qh18-20020a170906ecb200b008b1c5aa4c8bmr19020695ejb.77.1677105524551; Wed, 22 Feb 2023 14:38:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677105524; cv=none; d=google.com; s=arc-20160816; b=h6xrKQWJqIMOacR8FeitjZMTzFZWCzMmlAzjtaPobvsXERNnJbskwOqVBF7ttX1AoX uxXw3dALDEQIr4RTJZ2bT4846emfO6PlaLGeegpcv23QUu7hc+JU/BOMgWGLzao0Iqjl 7F9JGxpXz9ECPhTY4r4R8/kLLgv0XvFqChtEp0YF9GjZwkVzohg7TQNdg8uruItjQaBp ggMLWJ1uV91vxQcuAn7KrQWgpvG0IEaQRXIyGIrPhNQaKbBPPcwgOX4p9dkp2UBmKRqN W6Hcx3xFF6cdsOHUqWS6/sVVafbPF/SGUf++F0D8gRqvc4/mdhqCVkWm70H57+DYVt+9 1MdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=58RWolsQRKHxWExKtYJ/a/0nm3W9nZSyMGQTMD5lYs8=; b=1JqB4Cy2p5Zs7PZtNF6dqtjEhVuhPGRZD/QBJndo4oqyy8kWe9dG6eI1v18cm0ecEZ A52NrED2/lTx/ZlSK0H2cShToVPP/AntQMXimbxo4a+zsMh5qFkcllrSTSkuMLFDk8MG 8yAaQXO779BpzpMlhHunjsl5O9fn1wot22n8KXslEG63rp4KU+/fhnt7zPuIKYkg+sFz AzqDN0o8ZwX0h26TkCGhRV6HDYLrIDASLMIseEEriakx2hRvi8lcetPExt6U+WSqgnq6 iMHSKmCUtfw5r0UchBzMRgP/dMItZJY5KgNAqm3mIZNerGWjOY/hrJ7m8RcAW4IXjdG/ J2sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=nBAoieJI; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u19-20020a170906109300b008b17710026esi21601814eju.565.2023.02.22.14.38.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 14:38:44 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=nBAoieJI; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 507F938582B7 for ; Wed, 22 Feb 2023 22:38:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 507F938582B7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677105523; 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=nBAoieJIiymJ9D+a0LtpyCc2dOftcXDGpDCSzA2JUL6+msri0UfOFlwn+6o2t8BpZ uVMhDpqIqmIfyy5k94RznA4IBSCFCA/c6UUMgDYr+saGRw3Eel4Mcyn+udTrttRKob hL+IgR+3JZJ/ufUzl4HQ28xjPUBzn73bDIV6iFkA= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758572602678501338?= X-GMAIL-MSGID: =?utf-8?q?1758572602678501338?= 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() {}