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.133.124]) by sourceware.org (Postfix) with ESMTPS id 48F51385841C for ; Thu, 24 Aug 2023 14:39:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 48F51385841C 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=1692887949; 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=pfNN9YxmNTfH5ihTJs57LCqj+0dbbJAOoXPldfC9+mg=; b=FzMzeKAzSiDtnVWVTHiAock/CK2aSo7Kcvjlf9Yw7P6HDrNWDDlCCAbJNBr721g4zIrhFy s7kFzHxeI6TVN7Ct1A559+m/Bb9UJk3WOBnHlz3t3td+QnJEbH8m+H86BriqAt9H0zwfGy lHwW1vHdIoeviGRmY6vEBKi2pc7qZ4Y= Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-212-T6j2sL12OGuGGvTrQVzwvw-1; Thu, 24 Aug 2023 10:39:07 -0400 X-MC-Unique: T6j2sL12OGuGGvTrQVzwvw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E29C82807D6C for ; Thu, 24 Aug 2023 14:39:06 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.117]) by smtp.corp.redhat.com (Postfix) with ESMTP id B346040D2840; Thu, 24 Aug 2023 14:39:06 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 5/9] analyzer: reimplement kf_memcpy_memmove Date: Thu, 24 Aug 2023 10:38:59 -0400 Message-Id: <20230824143903.3161185-6-dmalcolm@redhat.com> In-Reply-To: <20230824143903.3161185-1-dmalcolm@redhat.com> References: <20230824143903.3161185-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 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 List-Id: gcc/analyzer/ChangeLog: * kf.cc (kf_memcpy_memmove::impl_call_pre): Reimplement using region_model::copy_bytes. * region-model.cc (region_model::read_bytes): New. (region_model::copy_bytes): New. * region-model.h (region_model::read_bytes): New decl. (region_model::copy_bytes): New decl. --- gcc/analyzer/kf.cc | 14 ++++---------- gcc/analyzer/region-model.cc | 35 +++++++++++++++++++++++++++++++++++ gcc/analyzer/region-model.h | 9 +++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/gcc/analyzer/kf.cc b/gcc/analyzer/kf.cc index 6b33cd159dac..3eddbe200387 100644 --- a/gcc/analyzer/kf.cc +++ b/gcc/analyzer/kf.cc @@ -541,7 +541,6 @@ kf_memcpy_memmove::impl_call_pre (const call_details &cd) const const svalue *num_bytes_sval = cd.get_arg_svalue (2); region_model *model = cd.get_model (); - region_model_manager *mgr = cd.get_manager (); const region *dest_reg = model->deref_rvalue (dest_ptr_sval, cd.get_arg_tree (0), cd.get_ctxt ()); @@ -550,15 +549,10 @@ kf_memcpy_memmove::impl_call_pre (const call_details &cd) const cd.maybe_set_lhs (dest_ptr_sval); - const region *sized_src_reg - = mgr->get_sized_region (src_reg, NULL_TREE, num_bytes_sval); - const region *sized_dest_reg - = mgr->get_sized_region (dest_reg, NULL_TREE, num_bytes_sval); - const svalue *src_contents_sval - = model->get_store_value (sized_src_reg, cd.get_ctxt ()); - model->check_for_poison (src_contents_sval, cd.get_arg_tree (1), - sized_src_reg, cd.get_ctxt ()); - model->set_value (sized_dest_reg, src_contents_sval, cd.get_ctxt ()); + model->copy_bytes (dest_reg, + src_reg, cd.get_arg_tree (1), + num_bytes_sval, + cd.get_ctxt ()); } /* Handler for "memset" and "__builtin_memset". */ diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 1fe66f4719fa..00c306ab7dae 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -3794,6 +3794,41 @@ region_model::write_bytes (const region *dest_reg, set_value (sized_dest_reg, sval, ctxt); } +/* Read NUM_BYTES_SVAL from SRC_REG. + Use CTXT to report any warnings associated with the copy + (e.g. out-of-bounds reads, copying of uninitialized values, etc). */ + +const svalue * +region_model::read_bytes (const region *src_reg, + tree src_ptr_expr, + const svalue *num_bytes_sval, + region_model_context *ctxt) const +{ + const region *sized_src_reg + = m_mgr->get_sized_region (src_reg, NULL_TREE, num_bytes_sval); + const svalue *src_contents_sval = get_store_value (sized_src_reg, ctxt); + check_for_poison (src_contents_sval, src_ptr_expr, + sized_src_reg, ctxt); + return src_contents_sval; +} + +/* Copy NUM_BYTES_SVAL bytes from SRC_REG to DEST_REG. + Use CTXT to report any warnings associated with the copy + (e.g. out-of-bounds reads/writes, copying of uninitialized values, + etc). */ + +void +region_model::copy_bytes (const region *dest_reg, + const region *src_reg, + tree src_ptr_expr, + const svalue *num_bytes_sval, + region_model_context *ctxt) +{ + const svalue *data_sval + = read_bytes (src_reg, src_ptr_expr, num_bytes_sval, ctxt); + write_bytes (dest_reg, num_bytes_sval, data_sval, ctxt); +} + /* Mark REG as having unknown content. */ void diff --git a/gcc/analyzer/region-model.h b/gcc/analyzer/region-model.h index 41df1885ad5b..b1c705e22c28 100644 --- a/gcc/analyzer/region-model.h +++ b/gcc/analyzer/region-model.h @@ -371,6 +371,15 @@ class region_model const svalue *num_bytes_sval, const svalue *sval, region_model_context *ctxt); + const svalue *read_bytes (const region *src_reg, + tree src_ptr_expr, + const svalue *num_bytes_sval, + region_model_context *ctxt) const; + void copy_bytes (const region *dest_reg, + const region *src_reg, + tree src_ptr_expr, + const svalue *num_bytes_sval, + region_model_context *ctxt); void mark_region_as_unknown (const region *reg, uncertainty_t *uncertainty); tristate eval_condition (const svalue *lhs, -- 2.26.3