From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 B24A53858C53 for ; Tue, 17 Jan 2023 17:37:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B24A53858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673977064; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4gWXrUBzDPzSDg8T9hl5oUjrPzqvuUpSXyajblYljU8=; b=ce8+M4qhs2ua3Q3+gpUcbfAiLSzudN4i8wkVJqsSDiyl24NR7NNbjLZ8mg+etOaO9zQ/j1 7R8ywqsnWV/jTwMBcn/ub4iF+o7lEaqioAoQ79OrAzi6pTcTGi5RabFrTzf2IsxNUJ9I3f F5sN953Jo+B+hZuIiZtpAW3fMr4zCxM= Received: from mail-ua1-f69.google.com (mail-ua1-f69.google.com [209.85.222.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-463-oKzaYMY4OZCbAE_SBV0uqw-1; Tue, 17 Jan 2023 12:37:40 -0500 X-MC-Unique: oKzaYMY4OZCbAE_SBV0uqw-1 Received: by mail-ua1-f69.google.com with SMTP id n14-20020a9f314e000000b004114b0c125fso12893498uab.8 for ; Tue, 17 Jan 2023 09:37:34 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4gWXrUBzDPzSDg8T9hl5oUjrPzqvuUpSXyajblYljU8=; b=MZyhq4UrEhq9zY9IWv6Uy321GexuG9ul1qxdm6UpiCLnOz5jUtwlt4cxMEGFRjHNGl d/gKAAAavu6wJs4sMByOQk3cgVVsZPf7M1T7BtikYk2JU5YA2h1GKNcovy7npznTy9TG iJoTMyRHanb4Wd2o9R+uUgKsYfAqmx3lXgxzTVVBi9zRZ7GdVr7hWRxBmfqX44cHEgmk DpPxaxXweLR56EI8vgmPFeSBe26Q+sCCxzurP9P/f9hGrlFaITt+llbwCmPg4vHCQVIn eCCs/PVtoK2kSBSYEzkw75X5VVvFW8h1yaPXiRB/Noe/HVH0GmEMXvcFyFB46vhk0hEh sktw== X-Gm-Message-State: AFqh2kqwg2J1SjGSz2HHS8WwfYWa+Os5m4W1c2iYP2ywBK7JUga4ERgm epN7f9Lk4Kz7JMeDhu/5N8AZK7XUwA+d0Otav6JpOB/9JllADlU6FnRUjnFDwprjAuKnLm6Mr2M IttUGMh3ZU1iuQICVtg== X-Received: by 2002:a1f:6a42:0:b0:3e1:a5b3:4d66 with SMTP id f63-20020a1f6a42000000b003e1a5b34d66mr1371216vkc.8.1673977054132; Tue, 17 Jan 2023 09:37:34 -0800 (PST) X-Google-Smtp-Source: AMrXdXu74WxgYCvNsxCdXsWvJKCTZcfjNt/Pvk9ZFCPcSCvXLlMOT9ILRVL+AYgUVq1XrAb7uhigTw== X-Received: by 2002:a1f:6a42:0:b0:3e1:a5b3:4d66 with SMTP id f63-20020a1f6a42000000b003e1a5b34d66mr1371198vkc.8.1673977053729; Tue, 17 Jan 2023 09:37:33 -0800 (PST) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id y4-20020a05620a44c400b007069375e0f4sm2392055qkp.122.2023.01.17.09.37.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 17 Jan 2023 09:37:33 -0800 (PST) Message-ID: Date: Tue, 17 Jan 2023 12:37:31 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH] tree-optimization/104475 - bogus -Wstringop-overflow To: Richard Biener , gcc-patches@gcc.gnu.org Cc: Jakub Jelinek References: <20221207112501.989ED136B4@imap1.suse-dmz.suse.de> From: Jason Merrill In-Reply-To: <20221207112501.989ED136B4@imap1.suse-dmz.suse.de> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 List-Id: On 12/7/22 06:25, Richard Biener wrote: > The following avoids a bogus -Wstringop-overflow diagnostic by > properly recognizing that &d->m_mutex cannot be nullptr in C++ > even if m_mutex is at offset zero. The frontend already diagnoses > a &d->m_mutex != nullptr comparison and the following transfers > this knowledge to the middle-end which sees &d->m_mutex as > simple pointer arithmetic. The new ADDR_NONZERO flag on an > ADDR_EXPR is used to carry this information and it's checked in > the tree_expr_nonzero_p API which causes this to be folded early. > > To avoid the bogus diagnostic this avoids separating the nullptr > path via jump-threading by eliminating the nullptr check. > > I'd appreciate C++ folks picking this up and put the flag on > the appropriate ADDR_EXPRs - I've tried avoiding to put it on > all of them and didn't try hard to mimick what -Waddress warns > on (the code is big, maybe some refactoring would help but also > not sure what exactly the C++ standard constraints are here). This is allowed by the standard, at least after CWG2535, but we need to check -fsanitize=null before asserting that the address is non-null. With that elaboration, a flag on the ADDR_EXPR may not be a convenient way to express the property? > Bootstrapped and tested on x86_64-unknown-linux-gnu. > > Thanks, > Richard. > > PR tree-optimization/104475 > gcc/ > * tree-core.h: Document use of nothrow_flag on ADDR_EXPR. > * tree.h (ADDR_NONZERO): New. > * fold-const.cc (tree_single_nonzero_warnv_p): Check > ADDR_NONZERO. > > gcc/cp/ > * typeck.cc (cp_build_addr_expr_1): Set ADDR_NONZERO > on the built address if it is of a COMPONENT_REF. > > * g++.dg/opt/pr104475.C: New testcase. > --- > gcc/cp/typeck.cc | 3 +++ > gcc/fold-const.cc | 4 +++- > gcc/testsuite/g++.dg/opt/pr104475.C | 12 ++++++++++++ > gcc/tree-core.h | 3 +++ > gcc/tree.h | 4 ++++ > 5 files changed, 25 insertions(+), 1 deletion(-) > create mode 100644 gcc/testsuite/g++.dg/opt/pr104475.C > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 7dfe5acc67e..3563750803e 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -7232,6 +7232,9 @@ cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) > gcc_assert (same_type_ignoring_top_level_qualifiers_p > (TREE_TYPE (object), decl_type_context (field))); > val = build_address (arg); > + if (TREE_CODE (val) == ADDR_EXPR > + && TREE_CODE (TREE_OPERAND (val, 0)) == COMPONENT_REF) > + ADDR_NONZERO (val) = 1; > } > > if (TYPE_PTR_P (argtype) > diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc > index e80be8049e1..cdfe3f50ae3 100644 > --- a/gcc/fold-const.cc > +++ b/gcc/fold-const.cc > @@ -15308,8 +15308,10 @@ tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) > > case ADDR_EXPR: > { > - tree base = TREE_OPERAND (t, 0); > + if (ADDR_NONZERO (t)) > + return true; > > + tree base = TREE_OPERAND (t, 0); > if (!DECL_P (base)) > base = get_base_address (base); > > diff --git a/gcc/testsuite/g++.dg/opt/pr104475.C b/gcc/testsuite/g++.dg/opt/pr104475.C > new file mode 100644 > index 00000000000..013c70302c6 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/opt/pr104475.C > @@ -0,0 +1,12 @@ > +// { dg-do compile } > +// { dg-require-effective-target c++11 } > +// { dg-options "-O -Waddress -fdump-tree-original" } > + > +struct X { int i; }; > + > +bool foo (struct X *p) > +{ > + return &p->i != nullptr; /* { dg-warning "never be NULL" } */ > +} > + > +/* { dg-final { scan-tree-dump "return = 1;" "original" } } */ > diff --git a/gcc/tree-core.h b/gcc/tree-core.h > index e146b133dbd..303e25b5df6 100644 > --- a/gcc/tree-core.h > +++ b/gcc/tree-core.h > @@ -1376,6 +1376,9 @@ struct GTY(()) tree_base { > TREE_THIS_NOTRAP in > INDIRECT_REF, MEM_REF, TARGET_MEM_REF, ARRAY_REF, ARRAY_RANGE_REF > > + ADDR_NONZERO in > + ADDR_EXPR > + > SSA_NAME_IN_FREE_LIST in > SSA_NAME > > diff --git a/gcc/tree.h b/gcc/tree.h > index 23223ca0c87..1c810c0b21b 100644 > --- a/gcc/tree.h > +++ b/gcc/tree.h > @@ -876,6 +876,10 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int, > (TREE_CHECK5 (NODE, INDIRECT_REF, MEM_REF, TARGET_MEM_REF, ARRAY_REF, \ > ARRAY_RANGE_REF)->base.nothrow_flag) > > +/* Nozero means this ADDR_EXPR is not equal to NULL. */ > +#define ADDR_NONZERO(NODE) \ > + (TREE_CHECK (NODE, ADDR_EXPR)->base.nothrow_flag) > + > /* In a VAR_DECL, PARM_DECL or FIELD_DECL, or any kind of ..._REF node, > nonzero means it may not be the lhs of an assignment. > Nonzero in a FUNCTION_DECL means this function should be treated