From patchwork Mon Sep 21 22:53:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1368648 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=a6LvBCVg; dkim-atps=neutral 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 ozlabs.org (Postfix) with ESMTPS id 4BwKVQ3Qmzz9sTC for ; Tue, 22 Sep 2020 08:53:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8C903381DCC3; Mon, 21 Sep 2020 22:53:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C903381DCC3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600728812; bh=CbRGwBhPQhaYQ0CyslKRjaysnvhkLKd0ojCCFTL0rH4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=a6LvBCVgfZ8QDedSV45wofTAKlXBmp72eBmlneW35jhgV+3irezHzxQqFQ+bfSbeC 71/h78T7ezPGKKpzuhqjepbYOdGPfqR/UfkRhZDVpeOh+352QKIBmD40m3wtQ2I3T1 yeillKxHZbwFaYblf4HCjcsavCAB2rxBlnfxrSgA= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 1CE8A385783D for ; Mon, 21 Sep 2020 22:53:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1CE8A385783D Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-401-loC2-4NQNGanlyr-_mPNbA-1; Mon, 21 Sep 2020 18:53:27 -0400 X-MC-Unique: loC2-4NQNGanlyr-_mPNbA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B29A2186840B for ; Mon, 21 Sep 2020 22:53:26 +0000 (UTC) Received: from t470.redhat.com (ovpn-112-230.phx2.redhat.com [10.3.112.230]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5DA0778825; Mon, 21 Sep 2020 22:53:26 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: fix ICE on bogus decl of memset [PR97130] Date: Mon, 21 Sep 2020 18:53:25 -0400 Message-Id: <20200921225325.13868-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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@gcc.gnu.org Sender: "Gcc-patches" Verify that arguments are pointers before calling handling code that calls deref_rvalue on them. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to master as r11-3341-g1e19ecd79b45af6df87a6869d1936b857c9f71fc. gcc/analyzer/ChangeLog: PR analyzer/97130 * region-model-impl-calls.cc (call_details::get_arg_type): New. * region-model.cc (region_model::on_call_pre): Check that the initial arg is a pointer before calling impl_call_memset and impl_call_strlen. * region-model.h (call_details::get_arg_type): New decl. gcc/testsuite/ChangeLog: PR analyzer/97130 * gcc.dg/analyzer/pr97130.c: New test. --- gcc/analyzer/region-model-impl-calls.cc | 8 ++++++++ gcc/analyzer/region-model.cc | 6 ++++-- gcc/analyzer/region-model.h | 1 + gcc/testsuite/gcc.dg/analyzer/pr97130.c | 10 ++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/pr97130.c diff --git a/gcc/analyzer/region-model-impl-calls.cc b/gcc/analyzer/region-model-impl-calls.cc index 6582ffb3c95..423f74a4152 100644 --- a/gcc/analyzer/region-model-impl-calls.cc +++ b/gcc/analyzer/region-model-impl-calls.cc @@ -103,6 +103,14 @@ call_details::get_arg_tree (unsigned idx) const return gimple_call_arg (m_call, idx); } +/* Get the type of argument IDX. */ + +tree +call_details::get_arg_type (unsigned idx) const +{ + return TREE_TYPE (gimple_call_arg (m_call, idx)); +} + /* Get argument IDX at the callsite as an svalue. */ const svalue * diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 1312391557d..6f04904a74e 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -737,12 +737,14 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt) /* No side-effects (tracking stream state is out-of-scope for the analyzer). */ } - else if (is_named_call_p (callee_fndecl, "memset", call, 3)) + else if (is_named_call_p (callee_fndecl, "memset", call, 3) + && POINTER_TYPE_P (cd.get_arg_type (0))) { impl_call_memset (cd); return false; } - else if (is_named_call_p (callee_fndecl, "strlen", call, 1)) + else if (is_named_call_p (callee_fndecl, "strlen", call, 1) + && POINTER_TYPE_P (cd.get_arg_type (0))) { if (impl_call_strlen (cd)) return false; diff --git a/gcc/analyzer/region-model.h b/gcc/analyzer/region-model.h index 1bb9798ae58..4859df369cf 100644 --- a/gcc/analyzer/region-model.h +++ b/gcc/analyzer/region-model.h @@ -2482,6 +2482,7 @@ public: bool maybe_set_lhs (const svalue *result) const; tree get_arg_tree (unsigned idx) const; + tree get_arg_type (unsigned idx) const; const svalue *get_arg_svalue (unsigned idx) const; void dump_to_pp (pretty_printer *pp, bool simple) const; diff --git a/gcc/testsuite/gcc.dg/analyzer/pr97130.c b/gcc/testsuite/gcc.dg/analyzer/pr97130.c new file mode 100644 index 00000000000..f437b763c94 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/pr97130.c @@ -0,0 +1,10 @@ +/* { dg-additional-options "-Wno-builtin-declaration-mismatch" } */ + +void * +memset (int, int, __SIZE_TYPE__); + +void +mp (int xl) +{ + memset (xl, 0, sizeof xl); +}