From patchwork Thu Apr 14 22:46:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1617475 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=Img/SQSZ; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KfZMk6YbDz9sG2 for ; Fri, 15 Apr 2022 08:46:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B9A33857360 for ; Thu, 14 Apr 2022 22:46:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B9A33857360 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649976416; bh=WHCFuAt+Mpef/dDPXjC/HPSe/iBqwa7hGILv0HIeswo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Img/SQSZ7pJcZFtpsuLjb3YSQz2H2qPqf9BwD55aB2yGSw0OXTe8wXv1eKS2vEVpr 3GCjPEKiBanERCRsZu3m0Q5nfk8w7OOHo27vKobt02CCpIPR/lag0ewvueWUoPZBpS ROJLxv+mWW36OhwAbfXubDvgJjiktOGphFKqKVCE= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 80860385803E for ; Thu, 14 Apr 2022 22:46:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 80860385803E Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-54-GDRMEAOjNiqQGFMR-YCY-g-1; Thu, 14 Apr 2022 18:46:13 -0400 X-MC-Unique: GDRMEAOjNiqQGFMR-YCY-g-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D829A3C23FA2 for ; Thu, 14 Apr 2022 22:46:12 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.12.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id B674A40D0179; Thu, 14 Apr 2022 22:46:12 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: fix escaping of pointer arithmetic [PR105264] Date: Thu, 14 Apr 2022 18:46:11 -0400 Message-Id: <20220414224611.1106049-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" PR analyzer/105264 reports that the analyzer can fail to treat (PTR + IDX) and PTR[IDX] as referring to the same memory under some situations. There are various ways in which this can happen when IDX is a symbolic value, due to having several ways in which such memory regions can be referred to symbolically. I attempted to fix this by being smarter when folding svalues and regions, but this fix seems too fiddly to attempt in stage 4. Instead, this less ambitious patch fixes a false positive from -Wanalyzer-use-of-uninitialized-value by making the analyzer's escape analysis smarter, so that it treats *PTR as escaping when (PTR + OFFSET) is passed to an external function, and thus it treats *PTR as possibly-initialized (the "passing &PTR[IDX]" case was already working). Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r12-8169-ga358e4b60815b4. gcc/analyzer/ChangeLog: PR analyzer/105264 * region-model-reachability.cc (reachable_regions::handle_parm): Use maybe_get_deref_base_region rather than just region_svalue, to handle pointer arithmetic also. * svalue.cc (svalue::maybe_get_deref_base_region): New. * svalue.h (svalue::maybe_get_deref_base_region): New decl. gcc/testsuite/ChangeLog: PR analyzer/105264 * gcc.dg/analyzer/torture/symbolic-10.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model-reachability.cc | 8 +--- gcc/analyzer/svalue.cc | 42 +++++++++++++++++++ gcc/analyzer/svalue.h | 2 + .../gcc.dg/analyzer/torture/symbolic-10.c | 40 ++++++++++++++++++ 4 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/torture/symbolic-10.c diff --git a/gcc/analyzer/region-model-reachability.cc b/gcc/analyzer/region-model-reachability.cc index b876b8f0363..12d09c3e500 100644 --- a/gcc/analyzer/region-model-reachability.cc +++ b/gcc/analyzer/region-model-reachability.cc @@ -252,12 +252,8 @@ reachable_regions::handle_parm (const svalue *sval, tree param_type) m_mutable_svals.add (sval); else m_reachable_svals.add (sval); - if (const region_svalue *parm_ptr - = sval->dyn_cast_region_svalue ()) - { - const region *pointee_reg = parm_ptr->get_pointee (); - add (pointee_reg, is_mutable); - } + if (const region *base_reg = sval->maybe_get_deref_base_region ()) + add (base_reg, is_mutable); /* Treat all svalues within a compound_svalue as reachable. */ if (const compound_svalue *compound_sval = sval->dyn_cast_compound_svalue ()) diff --git a/gcc/analyzer/svalue.cc b/gcc/analyzer/svalue.cc index 536bc288dbf..a1403f0fbef 100644 --- a/gcc/analyzer/svalue.cc +++ b/gcc/analyzer/svalue.cc @@ -651,6 +651,48 @@ svalue::all_zeroes_p () const return false; } +/* If this svalue is a pointer, attempt to determine the base region it points + to. Return NULL on any problems. */ + +const region * +svalue::maybe_get_deref_base_region () const +{ + const svalue *iter = this; + while (1) + { + switch (iter->get_kind ()) + { + default: + return NULL; + + case SK_REGION: + { + const region_svalue *region_sval + = as_a (iter); + return region_sval->get_pointee ()->get_base_region (); + } + + case SK_BINOP: + { + const binop_svalue *binop_sval + = as_a (iter); + switch (binop_sval->get_op ()) + { + case POINTER_PLUS_EXPR: + /* If we have a symbolic value expressing pointer arithmetic, + use the LHS. */ + iter = binop_sval->get_arg0 (); + continue; + + default: + return NULL; + } + return NULL; + } + } + } +} + /* class region_svalue : public svalue. */ /* Implementation of svalue::dump_to_pp vfunc for region_svalue. */ diff --git a/gcc/analyzer/svalue.h b/gcc/analyzer/svalue.h index 4bbe8588b8d..29ea2ee6408 100644 --- a/gcc/analyzer/svalue.h +++ b/gcc/analyzer/svalue.h @@ -175,6 +175,8 @@ public: per-type and thus it's meaningless for them to "have state". */ virtual bool can_have_associated_state_p () const { return true; } + const region *maybe_get_deref_base_region () const; + protected: svalue (complexity c, tree type) : m_complexity (c), m_type (type) diff --git a/gcc/testsuite/gcc.dg/analyzer/torture/symbolic-10.c b/gcc/testsuite/gcc.dg/analyzer/torture/symbolic-10.c new file mode 100644 index 00000000000..b2f3a8a1d86 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/torture/symbolic-10.c @@ -0,0 +1,40 @@ +/* Verify that -fanalyzer considers that mmfs escapes when passing either: + *(mmfs + i) + and + (&mmfs[i]) + to an external function (for symbolic i). */ + +typedef struct s_mmfile { + char *ptr; + long size; +} mmfile_t; + +void init_mmfile(mmfile_t *ptr); + +long test__init_via_ptr_arith__read_via_array_idx(int i) +{ + mmfile_t mmfs[3]; + init_mmfile(mmfs + i); + return mmfs[i].size; /* { dg-bogus "uninit" } */ +} + +long test__init_via_array_idx__read_via_ptr_arith(int i) +{ + mmfile_t mmfs[3]; + init_mmfile(&mmfs[i]); + return (mmfs + i)->size; /* { dg-bogus "uninit" } */ +} + +long test__ptr_arith_for_both(int i) +{ + mmfile_t mmfs[3]; + init_mmfile(mmfs + i); + return (mmfs + i)->size; /* { dg-bogus "uninit" } */ +} + +long test__array_idx_for_both(int i) +{ + mmfile_t mmfs[3]; + init_mmfile(&mmfs[i]); + return mmfs[i].size; /* { dg-bogus "uninit" } */ +}