From patchwork Wed Mar 20 22:36:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1914248 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OWl3YyIh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4V0Nk5482Cz1yWs for ; Thu, 21 Mar 2024 09:36:43 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B74DD3858C56 for ; Wed, 20 Mar 2024 22:36:41 +0000 (GMT) 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 A58D03858D1E for ; Wed, 20 Mar 2024 22:36:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A58D03858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A58D03858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710974184; cv=none; b=p//Y1Xd2lTcCaVUG5OAxDHLt5l/AxZz7AvNUsJiXW5OEAqsEtvoPAq3Q3wkNdrwGF2Yk5pGwlJ+OSIkiSb4aMZAmVAq7xlDvBfsee0lg1/eR3uCTSRvc6g9vlNvrPOxJReq5oZsBNZkHxoCiRMVt+cyqLtYm32q5i2oZwTkbF54= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710974184; c=relaxed/simple; bh=APA6hKOKcwf4PyCn3NvgHqaS/6v+pMkhJDNcgPY8R0o=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Qw/unSiwW4Q7qau/wUucn+OsAzGtbU81q/lbgIzHp5vbHJdtgESLZ0sV+1TUxFhtW1CooR2qFDkBdQWG/jhVO1ZYnc8Dc5uNSCUybtNo83gyPRf5AEYmVF42HFbCXcXiFB/zQ/SYC34yqTXQznZaHBGo/pR9FJ31SlSWsyhVrIM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710974178; 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; bh=1aQ38+RHQKxz+hnUdNy/Hipm8i6Q6O0V/mZnowFeY90=; b=OWl3YyIhA4UBO7EmmZ1yVQvIowRWnGQlQbTEc7dCjSJFMC0wmz8HT9YGDJtZ2b8mNmX8s0 qlgkdhNqXCtsUxSspfvNuJGMExL01t9+eUt2zbqcSCC84cFn+bFO9PWoLXSElOCGTSGj3A 3piHqZTvJgdUaBvk+p/t9NUSC7swMHs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-184-jGbzarYqOAaOG9nytQjitA-1; Wed, 20 Mar 2024 18:36:15 -0400 X-MC-Unique: jGbzarYqOAaOG9nytQjitA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B001E803EC1 for ; Wed, 20 Mar 2024 22:36:15 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 47B818173; Wed, 20 Mar 2024 22:36:15 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fix -Wanalyzer-deref-before-check false positive seen in loop header macro [PR109251] Date: Wed, 20 Mar 2024 18:36:09 -0400 Message-Id: <20240320223609.864290-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r14-9586-g9093f275e0a343. gcc/analyzer/ChangeLog: PR analyzer/109251 * sm-malloc.cc (deref_before_check::emit): Reject cases where the check is in a loop header within a macro expansion. (deref_before_check::loop_header_p): New. gcc/testsuite/ChangeLog: PR analyzer/109251 * c-c++-common/analyzer/deref-before-check-pr109251-1.c: New test. * c-c++-common/analyzer/deref-before-check-pr109251-2.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/sm-malloc.cc | 30 ++++++++++ .../analyzer/deref-before-check-pr109251-1.c | 60 +++++++++++++++++++ .../analyzer/deref-before-check-pr109251-2.c | 37 ++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-1.c create mode 100644 gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-2.c diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc index a518816b2b8..4e11d6dfc63 100644 --- a/gcc/analyzer/sm-malloc.cc +++ b/gcc/analyzer/sm-malloc.cc @@ -1563,6 +1563,21 @@ public: if (linemap_location_from_macro_definition_p (line_table, check_loc)) return false; + /* Reject warning if the check is in a loop header within a + macro expansion. This rejects cases like: + | deref of x; + | [...snip...] + | FOR_EACH(x) { + | [...snip...] + | } + where the FOR_EACH macro tests for non-nullness of x, since + the user is hoping to encapsulate the details of iteration + in the macro, and the extra check on the first iteration + would just be noise if we reported it. */ + if (loop_header_p (m_check_enode->get_point ()) + && linemap_location_from_macro_expansion_p (line_table, check_loc)) + return false; + /* Reject if m_deref_expr is sufficiently different from m_arg for cases where the dereference is spelled differently from the check, which is probably two different ways to get the @@ -1618,6 +1633,21 @@ public: } private: + static bool loop_header_p (const program_point &point) + { + const supernode *snode = point.get_supernode (); + if (!snode) + return false; + for (auto &in_edge : snode->m_preds) + { + if (const cfg_superedge *cfg_in_edge + = in_edge->dyn_cast_cfg_superedge ()) + if (cfg_in_edge->back_edge_p ()) + return true; + } + return false; + } + static bool sufficiently_similar_p (tree expr_a, tree expr_b) { pretty_printer *pp_a = global_dc->printer->clone (); diff --git a/gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-1.c b/gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-1.c new file mode 100644 index 00000000000..769cffae6d7 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-1.c @@ -0,0 +1,60 @@ +/* Reduced from linux-5.10.162's kernel/sched/fair.c, + with !CONFIG_FAIR_GROUP_SCHED. */ + +#define NULL ((void*)0) + +struct load_weight +{ + unsigned long weight; + /* [...snip...] */ +}; + +struct sched_entity +{ + struct load_weight load; + /* [...snip...] */ + unsigned int on_rq; + /* [...snip...] */ +}; + +struct cfs_rq +{ + /* [...snip...] */ + unsigned int nr_running; + /* [...snip...] */ +}; + +extern int +__calc_delta(int delta_exec, unsigned long weight /* [...snip...] */); + +/* !CONFIG_FAIR_GROUP_SCHED */ +#define for_each_sched_entity(se) \ + for (; se; se = (struct sched_entity *)NULL) + +extern struct cfs_rq* +cfs_rq_of(struct sched_entity* se); + +extern int +__sched_period(unsigned long nr_running); + +int +sched_slice(struct cfs_rq* cfs_rq, struct sched_entity* se) +{ + unsigned int nr_running = cfs_rq->nr_running; + int slice; + + /* [...snip...] */ + + slice = __sched_period(nr_running + !se->on_rq); + + for_each_sched_entity(se) { + /* [...snip...] */ + cfs_rq = cfs_rq_of(se); + /* [...snip...] */ + slice = __calc_delta(slice, se->load.weight); + } + + /* [...snip...] */ + + return slice; +} diff --git a/gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-2.c b/gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-2.c new file mode 100644 index 00000000000..8e85a47d315 --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/deref-before-check-pr109251-2.c @@ -0,0 +1,37 @@ +struct node +{ + struct node *next; + int val; +}; + +int test_loop_1 (struct node *n) +{ + int total = 0; + if (n->val = 42) + return -1; + for (struct node *iter = n; iter; iter=iter->next) + total += iter->val; + return total; +} + +int test_loop_2 (struct node *n) +{ + int total = 0; + if (n->val = 42) + return -1; + for (; n; n=n->next) + total += n->val; + return total; +} + +#define FOR_EACH_NODE(ITER) for (; (ITER); (ITER)=(ITER)->next) + +int test_loop_3 (struct node *n) +{ + int total = 0; + if (n->val = 42) + return -1; + FOR_EACH_NODE (n) + total += n->val; + return total; +}