From patchwork Wed Sep 6 13:44:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 137578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp2320079vqo; Wed, 6 Sep 2023 06:45:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHAUaYADMX49mlsZQ4xrd490bQKrpZ56eySR9em2oB/rwIhyovPelW3GGtoeM0JaihAGHCy X-Received: by 2002:a17:906:768f:b0:9a1:d67c:b4eb with SMTP id o15-20020a170906768f00b009a1d67cb4ebmr2275001ejm.48.1694007927318; Wed, 06 Sep 2023 06:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694007927; cv=none; d=google.com; s=arc-20160816; b=oSLSO08nOf0bf8+xm7qmtc8XyUxUigDLY9RJPd1aY/v2ymS76cRxodsFMbIERPY4Ys MCpie4rfaQ+QObKcdT/j9Ufszj3/aIMKr5ba9tGM9HQFfKpYMEj1AbZBXWzAOKo7iYqN dSEd/KSA8ZfFIUp+X7t89BmsU+dB7rL3nxOHJzz0Svk/u8XlvVWEQVbHpIBZW9I7woQA BJRjxAD7lIEjpX3+k09CNQS4rDTHhnc8n+AKj//c67GSE9UWpNYASVz5f9nOvK70ILmw fp6kTpq0ep5+rAMKc9MgIFO3s/nq2nd7AeHT4+tJJgZnOUmH+vW7NbzJ0/a5HbOCjcUs JARw== 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 :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=eAWqUYrJENlOuUXU9y3KukWShk0pSZfRawj/RVPKT70=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=I8WNSu5fTWV1CBLJbYOeZHNtcpVH9iwJG/QG6m3NEp8Zzt9Lkuec9e+Z+BncCkuLkb cDnineRlGzwpkIXVHTeBIsEdWtidiJoCUTfrkv0K2nek8NuuFRFmMwJ0GosZeUu6bLFk P2f0TQxF3J7QZfXpwOu6WdpVppx0ud1G3Z8BF1RkrVltCXeFHQ21uVJ7xvlolR/i/Vmv IPJNNoKAyuylUulqfppOjVxxQbOP860yKtHqk4TQ6gGr2IpVPJgWN9OHvUT3NA9Uwy3w ihSKqZIpjhNikxl1TppfMtc5AUz3kTo0w02pS0+w1xDRM2S5izVSiH9jM27a9OWOnl6k KcwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NIpoTQTG; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i9-20020a1709061cc900b00992d0de8760si9082501ejh.911.2023.09.06.06.45.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Sep 2023 06:45:27 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=NIpoTQTG; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 2352B3857736 for ; Wed, 6 Sep 2023 13:45:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2352B3857736 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694007914; bh=eAWqUYrJENlOuUXU9y3KukWShk0pSZfRawj/RVPKT70=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=NIpoTQTGPd1Q4pE1DmoUpBGXepGPK7ZGHTXB7cKABzU4FQl85rzRO/Mk3YtzFPEzl yQAkHniYG4W1JmVlnj82jnn3nQDBobrtbYqfEbB5stf1RQgVKI5Pr5EpPa2PvSASOH Z7G6btgj24R5wB9TmAa+nPC7SdlpsasQmFnzJEV8= 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 521EB3858C3A for ; Wed, 6 Sep 2023 13:44:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 521EB3858C3A Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-578-uVvqnSfSNHWMm67NOUhR1Q-1; Wed, 06 Sep 2023 09:44:25 -0400 X-MC-Unique: uVvqnSfSNHWMm67NOUhR1Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0F290877289 for ; Wed, 6 Sep 2023 13:44:25 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.34.57]) by smtp.corp.redhat.com (Postfix) with ESMTP id CDA35C03292; Wed, 6 Sep 2023 13:44:24 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [pushed] analyzer: add ctxt to fill_region/zero_fill_region Date: Wed, 6 Sep 2023 09:44:22 -0400 Message-Id: <20230906134422.682275-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 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_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776296056501307239 X-GMAIL-MSGID: 1776296056501307239 I noticed that region_model's fill_region/zero_fill_region member functions weren't checking that the write to the region was valid. Fixed thusly. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-3739-gb923978a6ec447. gcc/analyzer/ChangeLog: * kf.cc (kf_calloc::impl_call_pre): Pass ctxt to zero_fill_region. (kf_memset::impl_call_pre): Move responsibility for calling check_region_for_write to fill_region. * region-model.cc (region_model::on_assignment): Pass ctxt to zero_fill_region. (region_model::fill_region): Add "ctxt" param, using it to call check_region_for_write. (region_model::zero_fill_region): Likewise. * region-model.h (region_model::fill_region): Add "ctxt" param. (region_model::zero_fill_region): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/plugin/analyzer_cpython_plugin.c: Pass ctxt to zero_fill_region. --- gcc/analyzer/kf.cc | 7 ++----- gcc/analyzer/region-model.cc | 19 ++++++++++++++----- gcc/analyzer/region-model.h | 7 +++++-- .../gcc.dg/plugin/analyzer_cpython_plugin.c | 2 +- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/gcc/analyzer/kf.cc b/gcc/analyzer/kf.cc index e5bd7459f271..a62227729991 100644 --- a/gcc/analyzer/kf.cc +++ b/gcc/analyzer/kf.cc @@ -358,7 +358,7 @@ kf_calloc::impl_call_pre (const call_details &cd) const = model->get_or_create_region_for_heap_alloc (prod_sval, cd.get_ctxt ()); const region *sized_reg = mgr->get_sized_region (new_reg, NULL_TREE, prod_sval); - model->zero_fill_region (sized_reg); + model->zero_fill_region (sized_reg, cd.get_ctxt ()); if (cd.get_lhs_type ()) { const svalue *ptr_sval @@ -650,10 +650,7 @@ kf_memset::impl_call_pre (const call_details &cd) const const region *sized_dest_reg = mgr->get_sized_region (dest_reg, NULL_TREE, num_bytes_sval); - model->check_region_for_write (sized_dest_reg, - nullptr, - cd.get_ctxt ()); - model->fill_region (sized_dest_reg, fill_value_u8); + model->fill_region (sized_dest_reg, fill_value_u8, cd.get_ctxt ()); cd.maybe_set_lhs (dest_sval); } diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 82bc3b2c3826..6be0ad72aaae 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -1204,7 +1204,7 @@ region_model::on_assignment (const gassign *assign, region_model_context *ctxt) /* Any CONSTRUCTOR that survives to this point is either just a zero-init of everything, or a vector. */ if (!CONSTRUCTOR_NO_CLEARING (rhs1)) - zero_fill_region (lhs_reg); + zero_fill_region (lhs_reg, ctxt); unsigned ix; tree index; tree val; @@ -3929,19 +3929,28 @@ region_model::purge_region (const region *reg) m_store.purge_region (m_mgr->get_store_manager(), reg); } -/* Fill REG with SVAL. */ +/* Fill REG with SVAL. + Use CTXT to report any warnings associated with the write + (e.g. out-of-bounds). */ void -region_model::fill_region (const region *reg, const svalue *sval) +region_model::fill_region (const region *reg, + const svalue *sval, + region_model_context *ctxt) { + check_region_for_write (reg, nullptr, ctxt); m_store.fill_region (m_mgr->get_store_manager(), reg, sval); } -/* Zero-fill REG. */ +/* Zero-fill REG. + Use CTXT to report any warnings associated with the write + (e.g. out-of-bounds). */ void -region_model::zero_fill_region (const region *reg) +region_model::zero_fill_region (const region *reg, + region_model_context *ctxt) { + check_region_for_write (reg, nullptr, ctxt); m_store.zero_fill_region (m_mgr->get_store_manager(), reg); } diff --git a/gcc/analyzer/region-model.h b/gcc/analyzer/region-model.h index bb50ff12b12e..625f68805361 100644 --- a/gcc/analyzer/region-model.h +++ b/gcc/analyzer/region-model.h @@ -370,8 +370,11 @@ class region_model void set_value (tree lhs, tree rhs, region_model_context *ctxt); void clobber_region (const region *reg); void purge_region (const region *reg); - void fill_region (const region *reg, const svalue *sval); - void zero_fill_region (const region *reg); + void fill_region (const region *reg, + const svalue *sval, + region_model_context *ctxt); + void zero_fill_region (const region *reg, + region_model_context *ctxt); void write_bytes (const region *dest_reg, const svalue *num_bytes_sval, const svalue *sval, diff --git a/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c b/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c index bf1982e79c37..a364c8a678b9 100644 --- a/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c +++ b/gcc/testsuite/gcc.dg/plugin/analyzer_cpython_plugin.c @@ -1031,7 +1031,7 @@ kf_PyList_New::impl_call_post (const call_details &cd) const const region *ob_item_sized_region = model->get_or_create_region_for_heap_alloc (prod_sval, cd.get_ctxt ()); - model->zero_fill_region (ob_item_sized_region); + model->zero_fill_region (ob_item_sized_region, cd.get_ctxt ()); const svalue *ob_item_ptr_sval = mgr->get_ptr_svalue (pyobj_ptr_ptr, ob_item_sized_region); const svalue *ob_item_unmergeable