From patchwork Tue Nov 8 02:57:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 16823 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2445217wru; Mon, 7 Nov 2022 18:58:27 -0800 (PST) X-Google-Smtp-Source: AMsMyM5xndgyTXZMeOjD8vDznaLtxMdT0OomhXephrUliMlpuv/CYebjdx0Wc8+kDaHQmC3nra+Y X-Received: by 2002:a05:6402:22c7:b0:463:cc1:42a2 with SMTP id dm7-20020a05640222c700b004630cc142a2mr49615297edb.217.1667876307505; Mon, 07 Nov 2022 18:58:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667876307; cv=none; d=google.com; s=arc-20160816; b=iFJjtSqK6sye2y6pJoSDHaI3zhN6f0PNSRhgfVVbwTOHPDjr6f7t3wR2wU6e1gnk0e DS6QZbOmp/l+GMWJYAw3sQ+SHWeeSv8hj2AloV5mzBFWDg+/4cVq6y3R2djm9QEcyePo ikMDlwq871YZVpSjiTxbQdh00AltOVDUGpQBUa81ovBt+Pqoefnr+McGTNM/jysnqxM+ JRtVf+huMAy1IC6QPf3Q3Oua4gaAQRgGcub5Bg/UOApz2C7UDRLNsPOZ7tRJPv2CL0lt iIJecFAqHOIX53s30udqGStoqGOT7XmXsr/lyAsuGjegiPeJxcj5TSYDOSNm5LrhzUue Kcpg== 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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=0COJb6dE/L+gZtNnzbeMSbwAEGN39AGf+HnTLjHWSSo=; b=Iv/wroFml1ODa9EUEAKy+gwtwSNrqE1Wym8SPaYtMFc5IdZmiDDPKseRQpMzrReLb/ SV1TS/2wKE+WlpvS4HlAXIDVpsvc9X9MYHZt3M6GED46BHrt5C2kLQB00+bpp1EvpHSU 9K99BpUKZeoLWhKR27exBjrwUe3db75ZVVOWNb1Zkyc5BfGorG1t7xyvQ8I2Dom4QK+V vfJeds7R+69ONxRjrrilG44uJTuosmZTp0V7OHYXUS44ZNm34R/gMkccoehUpwPB15wu PmHfTx+zflL4D2twhD6X2Y1gs+ml3UqZw83fdXBHV3HppOXaw9fRcmIKsCnadOxn1m6y 99hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fa3X7TYT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id z11-20020a1709064e0b00b0073155abc1b8si9295442eju.154.2022.11.07.18.58.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 18:58:27 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fa3X7TYT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 B655B38582AF for ; Tue, 8 Nov 2022 02:58:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B655B38582AF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667876298; bh=0COJb6dE/L+gZtNnzbeMSbwAEGN39AGf+HnTLjHWSSo=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=fa3X7TYT7NVC1RuvLx90ms8+VcRA4MNgb/ZazVprd7TU61z2aSaFErVTUlqBc3Wrl wqTbWZgEA3ikH5TLFRm87fxbTUioyUrRpsQICP3vIiVxxCswlL+QxS9Yf4bl5HrY1o EK5NNVQ17V5LnuIJXTfX0k0oVpowkuZmt/5rzKgw= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 148AA3858D35 for ; Tue, 8 Nov 2022 02:57:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 148AA3858D35 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-39-idK3LJLGOICW5qWtjqK2AQ-1; Mon, 07 Nov 2022 21:57:27 -0500 X-MC-Unique: idK3LJLGOICW5qWtjqK2AQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F1883101A54E for ; Tue, 8 Nov 2022 02:57:26 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6A972166B29; Tue, 8 Nov 2022 02:57:26 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] analyzer: fix "when 'strchr' returns non-NULL" message Date: Mon, 7 Nov 2022 21:57:25 -0500 Message-Id: <20221108025725.2493707-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 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_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 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748895067472928553?= X-GMAIL-MSGID: =?utf-8?q?1748895067472928553?= Tweak analyzer handling of strchr, so that we show the when 'strchr' returns non-NULL message for that execution path. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-3768-g55e042407ef307. gcc/analyzer/ChangeLog: * region-model-impl-calls.cc (region_model::impl_call_strchr): Move to on_call_post. Handle both outcomes using bifurcation, rather than just the "not found" case. * region-model.cc (region_model::on_call_pre): Move BUILT_IN_STRCHR and "strchr" to... (region_model::on_call_post): ...here. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/strchr-1.c (test_literal): Detect writing to a string literal. Verify that we emit the "when '__builtin_strchr' returns non-NULL" message. Signed-off-by: David Malcolm --- gcc/analyzer/region-model-impl-calls.cc | 14 +++++++------- gcc/analyzer/region-model.cc | 14 ++++++++++++-- gcc/testsuite/gcc.dg/analyzer/strchr-1.c | 3 ++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/gcc/analyzer/region-model-impl-calls.cc b/gcc/analyzer/region-model-impl-calls.cc index 30fa765c4b4..46dbbb53bdc 100644 --- a/gcc/analyzer/region-model-impl-calls.cc +++ b/gcc/analyzer/region-model-impl-calls.cc @@ -1013,7 +1013,7 @@ region_model::impl_call_realloc (const call_details &cd) } } -/* Handle the on_call_pre part of "strchr" and "__builtin_strchr". */ +/* Handle the on_call_post part of "strchr" and "__builtin_strchr". */ void region_model::impl_call_strchr (const call_details &cd) @@ -1075,13 +1075,13 @@ region_model::impl_call_strchr (const call_details &cd) bool m_found; }; - /* Bifurcate state, creating a "not found" out-edge. */ + /* Body of region_model::impl_call_strchr. */ if (cd.get_ctxt ()) - cd.get_ctxt ()->bifurcate (make_unique (cd, false)); - - /* The "unbifurcated" state is the "found" case. */ - strchr_call_info found (cd, true); - found.update_model (this, NULL, cd.get_ctxt ()); + { + cd.get_ctxt ()->bifurcate (make_unique (cd, false)); + cd.get_ctxt ()->bifurcate (make_unique (cd, true)); + cd.get_ctxt ()->terminate_path (); + } } /* Handle the on_call_pre part of "strcpy" and "__builtin_strcpy_chk". */ diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index edf3412a817..e182d2e0e1a 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -2223,7 +2223,7 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt, case BUILT_IN_REALLOC: return false; case BUILT_IN_STRCHR: - impl_call_strchr (cd); + /* Handle in "on_call_post". */ return false; case BUILT_IN_STRCPY: case BUILT_IN_STRCPY_CHK: @@ -2341,7 +2341,7 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt, else if (is_named_call_p (callee_fndecl, "strchr", call, 2) && POINTER_TYPE_P (cd.get_arg_type (0))) { - impl_call_strchr (cd); + /* Handle in "on_call_post". */ return false; } else if (is_named_call_p (callee_fndecl, "strlen", call, 1) @@ -2418,6 +2418,12 @@ region_model::on_call_post (const gcall *call, impl_call_pipe (cd); return; } + else if (is_named_call_p (callee_fndecl, "strchr", call, 2) + && POINTER_TYPE_P (cd.get_arg_type (0))) + { + impl_call_strchr (cd); + return; + } /* Was this fndecl referenced by __attribute__((malloc(FOO)))? */ if (lookup_attribute ("*dealloc", DECL_ATTRIBUTES (callee_fndecl))) @@ -2435,6 +2441,10 @@ region_model::on_call_post (const gcall *call, impl_call_realloc (cd); return; + case BUILT_IN_STRCHR: + impl_call_strchr (cd); + return; + case BUILT_IN_VA_END: impl_call_va_end (cd); return; diff --git a/gcc/testsuite/gcc.dg/analyzer/strchr-1.c b/gcc/testsuite/gcc.dg/analyzer/strchr-1.c index dfe1bc9ea1a..bfa48916ca2 100644 --- a/gcc/testsuite/gcc.dg/analyzer/strchr-1.c +++ b/gcc/testsuite/gcc.dg/analyzer/strchr-1.c @@ -3,12 +3,13 @@ const char* test_literal (int x) { - char *p = __builtin_strchr ("123", x); + char *p = __builtin_strchr ("123", x); /* { dg-message "when '__builtin_strchr' returns non-NULL" } */ if (p) { __analyzer_eval (*p == x); /* { dg-message "UNKNOWN" } */ /* TODO: this ought to be TRUE, but it's unclear that it's worth stashing this constraint. */ + *p = 'A'; /* { dg-warning "write to string literal" } */ } return p; }