From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id CD4613858296 for ; Wed, 15 Mar 2023 10:49:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CD4613858296 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 relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id DE97A2190B for ; Wed, 15 Mar 2023 10:49:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1678877359; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=4p15iaPvESIwqtBDNuDKWu15SWKSVbcTvlKpakf2Zx8=; b=SWakG+woZkxslFQgIhfzyKoxa1NaCj/+QkMG9LTApjlbhhXxiG1DW9aIRB9QpAWYhIDvFs g10AcXmhsQkxFpWs/spxjavVdij+U6m78dNmANjNOtPxnBhYKXhBY8txQrv0zJf6drDCmh hYkQ2Cm4CNDVmAFwSn09EE0qGrR1Sh0= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1678877359; h=from:from:reply-to:date:date:to:to:cc:mime-version:mime-version: content-type:content-type; bh=4p15iaPvESIwqtBDNuDKWu15SWKSVbcTvlKpakf2Zx8=; b=8YZyNAS/vA8eKRA0XP218y2EngatdNTSrkM68SJIXtltcQAmOiKzGXPGoryiZhsHjKA+LU D+ycq54aDlSBtfBQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id D47CB2C141 for ; Wed, 15 Mar 2023 10:49:19 +0000 (UTC) Date: Wed, 15 Mar 2023 10:49:19 +0000 (UTC) From: Richard Biener To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] tree-optimization/109123 - run -Wuse-afer-free only early User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,MISSING_MID,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: Message-ID: <20230315104919.nxIlQRn-Qw9ZbnA7foeijjLRT6DLgy2HswcoORqO_Q0@z> The following switches the -Wuse-after-free diagnostics from emitted during the late access warning passes to the early access warning passes to make sure we run before passes performing code motion run which are the source of a lot of false positives on use-after-free not involving memory operations. The patch also fixes issues in c-c++-common/Wuse-after-free-6.c and g++.dg/warn/Wuse-after-free3.C. Bootstrapped and tested on x86_64-unknown-linux-gnu (without 1/2 sofar, but its testcase XFAILed). OK? Thanks, Richard. PR tree-optimization/109123 * gimple-ssa-warn-access.cc (pass_waccess::warn_invalid_pointer): Do not emit -Wuse-after-free late. (pass_waccess::check_call): Always check call pointer uses. * gcc.dg/Wuse-after-free-pr109123.c: New testcase. * c-c++-common/Wuse-after-free-6.c: Un-XFAIL case. * g++.dg/warn/Wuse-after-free3.C: Remove expected duplicate diagnostic. --- gcc/gimple-ssa-warn-access.cc | 28 ++++++------- .../c-c++-common/Wuse-after-free-6.c | 2 +- gcc/testsuite/g++.dg/warn/Wuse-after-free3.C | 3 +- .../gcc.dg/Wuse-after-free-pr109123.c | 41 +++++++++++++++++++ 4 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wuse-after-free-pr109123.c diff --git a/gcc/gimple-ssa-warn-access.cc b/gcc/gimple-ssa-warn-access.cc index a8ad7a6df65..d0809d321ea 100644 --- a/gcc/gimple-ssa-warn-access.cc +++ b/gcc/gimple-ssa-warn-access.cc @@ -3907,7 +3907,8 @@ pass_waccess::warn_invalid_pointer (tree ref, gimple *use_stmt, if (is_gimple_call (inval_stmt)) { - if ((equality && warn_use_after_free < 3) + if (!m_early_checks_p + || (equality && warn_use_after_free < 3) || (maybe && warn_use_after_free < 2) || warning_suppressed_p (use_stmt, OPT_Wuse_after_free)) return; @@ -4300,19 +4301,18 @@ pass_waccess::check_call (gcall *stmt) if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)) check_builtin (stmt); - if (!m_early_checks_p) - if (tree callee = gimple_call_fndecl (stmt)) - { - /* Check for uses of the pointer passed to either a standard - or a user-defined deallocation function. */ - unsigned argno = fndecl_dealloc_argno (callee); - if (argno < (unsigned) call_nargs (stmt)) - { - tree arg = call_arg (stmt, argno); - if (TREE_CODE (arg) == SSA_NAME) - check_pointer_uses (stmt, arg); - } - } + if (tree callee = gimple_call_fndecl (stmt)) + { + /* Check for uses of the pointer passed to either a standard + or a user-defined deallocation function. */ + unsigned argno = fndecl_dealloc_argno (callee); + if (argno < (unsigned) call_nargs (stmt)) + { + tree arg = call_arg (stmt, argno); + if (TREE_CODE (arg) == SSA_NAME) + check_pointer_uses (stmt, arg); + } + } check_call_access (stmt); check_call_dangling (stmt); diff --git a/gcc/testsuite/c-c++-common/Wuse-after-free-6.c b/gcc/testsuite/c-c++-common/Wuse-after-free-6.c index 581b1a0a024..0c17a2545f4 100644 --- a/gcc/testsuite/c-c++-common/Wuse-after-free-6.c +++ b/gcc/testsuite/c-c++-common/Wuse-after-free-6.c @@ -53,7 +53,7 @@ void* warn_cond_return_after_free (void *p, int c) free (p); // PHI handling not fully implemented. if (c) - return p; // { dg-warning "pointer 'p' may be used" "pr??????" { xfail *-*-* } } + return p; // { dg-warning "pointer 'p' may be used" } return 0; } diff --git a/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C b/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C index 1862ac8b09d..e5b157865bf 100644 --- a/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C +++ b/gcc/testsuite/g++.dg/warn/Wuse-after-free3.C @@ -1,7 +1,6 @@ // PR target/104213 // { dg-do compile } // { dg-options "-Wuse-after-free" } -// FIXME: We should not output the warning twice. struct A { @@ -13,4 +12,4 @@ A::~A () { operator delete (this); f (); // { dg-warning "used after" } -} // { dg-warning "used after" } +} diff --git a/gcc/testsuite/gcc.dg/Wuse-after-free-pr109123.c b/gcc/testsuite/gcc.dg/Wuse-after-free-pr109123.c new file mode 100644 index 00000000000..ece066dd28b --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wuse-after-free-pr109123.c @@ -0,0 +1,41 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -Wall" } */ + +typedef long unsigned int size_t; +extern void *realloc (void *__ptr, size_t __size) + __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__warn_unused_result__)) __attribute__ ((__alloc_size__ (2))); +struct vector_objective; +typedef struct vector_objective vector_objective; +struct vector_objective { double *_begin; double *_end; double *_capacity; }; +static inline size_t vector_objective_size(const vector_objective * v) { + return v->_end - v->_begin; /* { dg-bogus "used after" } */ +} +static inline size_t vector_objective_capacity(const vector_objective * v) { + return v->_capacity - v->_begin; +} +static inline void vector_objective_reserve(vector_objective * v, size_t n) { + size_t old_capacity = vector_objective_capacity(v); + size_t old_size = vector_objective_size(v); + if (n > old_capacity) { + v->_begin = realloc(v->_begin, sizeof(double) * n); + v->_end = v->_begin + old_size; + v->_capacity = v->_begin + n; + } +} +static inline void vector_objective_push_back(vector_objective * v, double x) { + if (v->_end == v->_capacity) + vector_objective_reserve (v, (vector_objective_capacity (v) == 0) ? 8 : 2 * vector_objective_capacity (v)); + *(v->_end) = x; + v->_end++; +} + +typedef struct { + vector_objective xy; +} eaf_polygon_t; + +int +rectangle_add(eaf_polygon_t * regions, double lx) +{ + vector_objective_push_back(®ions->xy, lx); + return 0; +} -- 2.35.3