From patchwork Thu Oct 20 09:29:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 6097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4f06:0:0:0:0:0 with SMTP id c6csp175672wru; Thu, 20 Oct 2022 02:38:50 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7N/3eI/7r+FShOEqjSLps+pYNRC51q82CnP6B/EWaGj6aCEs0nt1MX6Yq20mNL+GyJ9kgl X-Received: by 2002:a05:6402:448:b0:45c:8de5:4fc with SMTP id p8-20020a056402044800b0045c8de504fcmr11328901edw.133.1666258730753; Thu, 20 Oct 2022 02:38:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666258730; cv=none; d=google.com; s=arc-20160816; b=nwpZ5Srrx8HTY2k7ooSulgHFn8t7pGBlR8qNalRDiYh3251Yb72BnTGcJzc/ofnoep 7VAcEEMq4kE7L7W9D8SMO7WxtvcPkhdGNNY3Y7zIxNBAiUEjxLkaP87UQLO26pSKaG80 58bWJVafS9nPrLUavVZZBnVdmBvr5VAeAhHMf1Olo8AWuK7vx3rjl0KdXA5bfshQEvFy 8fWCqbbIKm6oybTn5zsqLqMWOnqZChhXmygI1H/1kZYmgNV2pOh3vKEYK0l+5PnFI6+W FkPWz/n6OKQ/U9xFVgzSHrR/34e/xZ+R4qOYVxv3yy7uOTshHzJG1n5u7C6kWBCj8B7Z jYdA== 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:message-id :mime-version:subject:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=NYXcDCzoSIKE0zr+7VNjq1zVu5LfmTLvXVNbAe42odg=; b=0jpopUnURDi5JtYu5AbvHDVvUqI7OfpzOW84V79B8HSBvopav1DNeTvC29IEyA5NCY wtIl19nnHUqET5gZcFaZEYS6OgdRa0sljYNC6OuUEwMVby/6DRaYzYjdgPNvyhYC2ihI 74UyjQw2EHeR9h9uJeMLVbtwG9ASxzAXPZ1MaQItbjLXfCdyKahBb3RwxdGhMWWluKqe XhrghISM67hUe58HUi5UwGJ/gR72iZ3XA5FzlXUvMnpKxMrC5MtXBigfknvfQQgLvza/ /GjwI04hZZy0wuWwSk6l78H9EVwr/45kN6NX18gx9Y9G+24Q7zh0NMk43Q/YvvgQ8Aoo lbGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fi++sgMc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id fj3-20020a1709069c8300b0078db6f488cesi19749384ejc.112.2022.10.20.02.38.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Oct 2022 02:38:50 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fi++sgMc; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 CEA4138AA24B for ; Thu, 20 Oct 2022 09:32:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CEA4138AA24B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666258340; bh=NYXcDCzoSIKE0zr+7VNjq1zVu5LfmTLvXVNbAe42odg=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fi++sgMcxd5zGTivIfn1H/JehOwG4rsJ/Cz6Cz5XxlRl1zchhiazG7K6dl5AQGJZW 9xJkucBWNcU76IRZf1S853f5u2h4Uq5WIdL7xLzf4VCBttNGiP2NT/3cifztzdMyoD pr8sSO9GsIU60jyeK+75NeU+DS2GZ6kg3rNdbohM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 453B5392AC21 for ; Thu, 20 Oct 2022 09:29:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 453B5392AC21 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 48C8B22C1E for ; Thu, 20 Oct 2022 09:29:17 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 358DD13494 for ; Thu, 20 Oct 2022 09:29:17 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id OnzaC+0UUWMYPQAAMHmgww (envelope-from ) for ; Thu, 20 Oct 2022 09:29:17 +0000 Date: Thu, 20 Oct 2022 11:29:16 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c/107305 - avoid ICEing with invalid GIMPLE input to the GIMPLE FE MIME-Version: 1.0 Message-Id: <20221020092917.358DD13494@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener 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?1747198914844200429?= X-GMAIL-MSGID: =?utf-8?q?1747198914844200429?= The GIMPLE FE was designed to defer semantic error checking to the GIMPLE IL verifier. But that can end up causing spurious ICEs earlier and in fact it will report an internal error. The following tries to improve the situation by explicitely calling into the verifier from the parser and intructing it to not ICE but instead zap the parsed body after an error is discovered. Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed. PR c/107305 PR c/107306 gcc/c/ * gimple-parser.cc (c_parser_parse_gimple_body): Verify the parsed IL and zap the body on error. gcc/ * tree-cfg.h (verify_gimple_in_seq): Add parameter to indicate whether to emit an ICE. Add return value. (verify_gimple_in_cfg): Likewise. * tree-cfg.cc (verify_gimple_in_seq): Likewise. (verify_gimple_in_cfg): Likewise. gcc/testsuite/ * gcc.dg/gimplefe-error-15.c: New testcase. --- gcc/c/gimple-parser.cc | 10 ++++++++++ gcc/testsuite/gcc.dg/gimplefe-error-15.c | 13 +++++++++++++ gcc/tree-cfg.cc | 16 ++++++++++------ gcc/tree-cfg.h | 4 ++-- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/gimplefe-error-15.c diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc index 5a2da2cfa0e..18ed4d4236d 100644 --- a/gcc/c/gimple-parser.cc +++ b/gcc/c/gimple-parser.cc @@ -364,6 +364,16 @@ c_parser_parse_gimple_body (c_parser *cparser, char *gimple_pass, cgraph_node::get_create (cfun->decl); cgraph_edge::rebuild_edges (); } + + /* Perform IL validation and if any error is found abort compilation + of this function by zapping its body. */ + if ((cfun->curr_properties & PROP_cfg) + && verify_gimple_in_cfg (cfun, false, false)) + init_empty_tree_cfg (); + else if (!(cfun->curr_properties & PROP_cfg) + && verify_gimple_in_seq (gimple_body (current_function_decl), false)) + gimple_set_body (current_function_decl, NULL); + dump_function (TDI_gimple, current_function_decl); } diff --git a/gcc/testsuite/gcc.dg/gimplefe-error-15.c b/gcc/testsuite/gcc.dg/gimplefe-error-15.c new file mode 100644 index 00000000000..066cd845d31 --- /dev/null +++ b/gcc/testsuite/gcc.dg/gimplefe-error-15.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-fgimple" } */ + +unsigned a; +static double *d; +static _Bool b; +__GIMPLE int +foo (int n) +{ + b = __builtin_add_overflow (n, *d, &a); +} /* { dg-error "invalid argument" } */ + +/* { dg-message "" "" { target *-*-* } 0 } */ diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc index 9b2c0f6956c..d982988048f 100644 --- a/gcc/tree-cfg.cc +++ b/gcc/tree-cfg.cc @@ -5300,13 +5300,15 @@ verify_gimple_transaction (gtransaction *stmt) /* Verify the GIMPLE statements inside the statement list STMTS. */ -DEBUG_FUNCTION void -verify_gimple_in_seq (gimple_seq stmts) +DEBUG_FUNCTION bool +verify_gimple_in_seq (gimple_seq stmts, bool ice) { timevar_push (TV_TREE_STMT_VERIFY); - if (verify_gimple_in_seq_2 (stmts)) + bool res = verify_gimple_in_seq_2 (stmts); + if (res && ice) internal_error ("% failed"); timevar_pop (TV_TREE_STMT_VERIFY); + return res; } /* Return true when the T can be shared. */ @@ -5496,8 +5498,8 @@ collect_subblocks (hash_set *blocks, tree block) /* Verify the GIMPLE statements in the CFG of FN. */ -DEBUG_FUNCTION void -verify_gimple_in_cfg (struct function *fn, bool verify_nothrow) +DEBUG_FUNCTION bool +verify_gimple_in_cfg (struct function *fn, bool verify_nothrow, bool ice) { basic_block bb; bool err = false; @@ -5652,11 +5654,13 @@ verify_gimple_in_cfg (struct function *fn, bool verify_nothrow) eh_table->traverse *, verify_eh_throw_stmt_node> (&visited_throwing_stmts); - if (err || eh_error_found) + if (ice && (err || eh_error_found)) internal_error ("verify_gimple failed"); verify_histograms (); timevar_pop (TV_TREE_STMT_VERIFY); + + return (err || eh_error_found); } diff --git a/gcc/tree-cfg.h b/gcc/tree-cfg.h index 95ec93e3a91..8c22c3dbbe3 100644 --- a/gcc/tree-cfg.h +++ b/gcc/tree-cfg.h @@ -63,8 +63,8 @@ extern gphi *get_virtual_phi (basic_block); extern gimple *first_stmt (basic_block); extern gimple *last_stmt (basic_block); extern gimple *last_and_only_stmt (basic_block); -extern void verify_gimple_in_seq (gimple_seq); -extern void verify_gimple_in_cfg (struct function *, bool); +extern bool verify_gimple_in_seq (gimple_seq, bool = true); +extern bool verify_gimple_in_cfg (struct function *, bool, bool = true); extern tree gimple_block_label (basic_block); extern void add_phi_args_after_copy_bb (basic_block); extern void add_phi_args_after_copy (basic_block *, unsigned, edge);