From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de 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) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1666258157; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=NYXcDCzoSIKE0zr+7VNjq1zVu5LfmTLvXVNbAe42odg=; b=BakNSSV+XFLdgHm5mRtbXPA1KuktvVJ/8b4rkFvtBfvrPs/Q0hVflLRTg6T2/5LPCB67sD 0O5rwAPg+jcLE+eEB+5OlQlBO4Pf+gq7G0kZHqs8Atdm2iZLEB9HosVmzpZd3zz4T/BXgv YwvLzbLd+Ms3SPy8xXOrWzU45vus2NI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1666258157; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version:content-type:content-type; bh=NYXcDCzoSIKE0zr+7VNjq1zVu5LfmTLvXVNbAe42odg=; b=17WkdwgQ+DAwNMRmETM2wC6bRsOHNWl2ZRwXBebDcK20DZ4WJwEcFowhR02T/Q0NEBdLjM 6RSFn4tFG0YughBw== 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) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH] c/107305 - avoid ICEing with invalid GIMPLE input to the GIMPLE FE MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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 List-Id: 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); -- 2.35.3