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.129.124]) by sourceware.org (Postfix) with ESMTPS id 307503858401 for ; Thu, 24 Aug 2023 14:39:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 307503858401 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=1692887947; 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=Z4E2knWIGXsB7WSi2FY93w6+ERrhewuZo5XJNbbKtq0=; b=gLobRWq7REe2qO5n8cfbqn/d+GA1zOvpbUVCEsq0QqiU6eeVT0cD3dBg+dQZdE1k2CLvzn eTYIStDueFnE/F5azI+DB1nNW2xUqR5p8V5TGFeKyVVAyGUnZzOD85Tx8Ezl0GWe/29jhU 3qT/e8uJXeLxo9tL0ijsqcN5VdTex24= 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-107-6LAL6cVmNGuCC0Pyc1Fa_w-1; Thu, 24 Aug 2023 10:39:06 -0400 X-MC-Unique: 6LAL6cVmNGuCC0Pyc1Fa_w-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 3F8C73C14AA9 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 1B0FC40D283A; Thu, 24 Aug 2023 14:39:06 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 2/9] analyzer: handle symbolic bindings in scan_for_null_terminator [PR105899] Date: Thu, 24 Aug 2023 10:38:56 -0400 Message-Id: <20230824143903.3161185-3-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: PR analyzer/105899 * region-model.cc (iterable_cluster::iterable_cluster): Add symbolic binding keys to m_symbolic_bindings. (iterable_cluster::has_symbolic_bindings_p): New. (iterable_cluster::m_symbolic_bindings): New field. (region_model::scan_for_null_terminator): Treat clusters with symbolic bindings as having unknown strlen. gcc/testsuite/ChangeLog: PR analyzer/105899 * gcc.dg/analyzer/sprintf-1.c: Include "analyzer-decls.h". (test_strlen_1): New. --- gcc/analyzer/region-model.cc | 15 +++++++++++++++ gcc/testsuite/gcc.dg/analyzer/sprintf-1.c | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 99817aee3a93..7a2f81f36e0f 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -3420,6 +3420,8 @@ public: if (concrete_key->get_byte_range (&fragment_bytes)) m_fragments.safe_push (fragment (fragment_bytes, sval)); } + else + m_symbolic_bindings.safe_push (key); } m_fragments.qsort (fragment::cmp_ptrs); } @@ -3440,8 +3442,14 @@ public: return false; } + bool has_symbolic_bindings_p () const + { + return !m_symbolic_bindings.is_empty (); + } + private: auto_vec m_fragments; + auto_vec m_symbolic_bindings; }; /* Simulate reading the bytes at BYTES from BASE_REG. @@ -3610,6 +3618,13 @@ region_model::scan_for_null_terminator (const region *reg, /* No binding for this base_region, or no binding at src_byte_offset (or a symbolic binding). */ + if (c.has_symbolic_bindings_p ()) + { + if (out_sval) + *out_sval = m_mgr->get_or_create_unknown_svalue (NULL_TREE); + return m_mgr->get_or_create_unknown_svalue (size_type_node); + } + /* TODO: the various special-cases seen in region_model::get_store_value. */ diff --git a/gcc/testsuite/gcc.dg/analyzer/sprintf-1.c b/gcc/testsuite/gcc.dg/analyzer/sprintf-1.c index f8dc806d6192..e7c2b3089c5b 100644 --- a/gcc/testsuite/gcc.dg/analyzer/sprintf-1.c +++ b/gcc/testsuite/gcc.dg/analyzer/sprintf-1.c @@ -1,6 +1,8 @@ /* See e.g. https://en.cppreference.com/w/c/io/fprintf and https://www.man7.org/linux/man-pages/man3/sprintf.3.html */ +#include "analyzer-decls.h" + extern int sprintf(char* dst, const char* fmt, ...) __attribute__((__nothrow__)); @@ -64,3 +66,12 @@ test_fmt_not_terminated (char *dst) return sprintf (dst, fmt); /* { dg-warning "stack-based buffer over-read" } */ /* { dg-message "while looking for null terminator for argument 2 \\('&fmt'\\) of 'sprintf'..." "event" { target *-*-* } .-1 } */ } + +void +test_strlen_1 (void) +{ + char buf[10]; + sprintf (buf, "msg: %s\n", "abc"); + __analyzer_eval (__builtin_strlen (buf) == 8); /* { dg-warning "UNKNOWN" } */ + // TODO: ideally would be TRUE +} -- 2.26.3