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 723DC384AB6D for ; Thu, 9 May 2024 17:42:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 723DC384AB6D 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 723DC384AB6D 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=1715276575; cv=none; b=ony+EMyTL2652bc8+tZpYthVp5sKCj8bsh3hyywTUK/pW8d051tgrvFWPJB2kkGm1YMrySQnH5xL3jE2l4kXjfiNeM9JdyqkVoQ5BIPBFEKPBFgKJnokMPlUb6k44xdFj63+UCXhL/Qc7hdjccrsFHEnVCWrdu8bZanvqX5wOgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715276575; c=relaxed/simple; bh=BPDjrJGELz9TpXRKRtojnIAxl/+1T7BgEmNFjHs8Cac=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LHl9QkTLmxmXAPzljXNwxNg+kW8CuV1gyhNQcVj+ubYPWVh658V4OWNBZcsNbzfu0rCGbLRORSJc39LiHsHUhhVUpwnjw+1QWGyJA9z5J7LRh2073khOnycG54cwSJeZ1dIbr9d9cqmFz9wDGC/HK8Pj5GssHwGthJBBPbRhC44= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715276572; 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=nvphL2pQxJBxZXMYKbMwMFavTBNc9xKu23Iby0YhGDc=; b=YuCsbvOR7tqey5TH8ezicxCpxXFgn4tf2mak+mkJuSv/FQKUJ5yl8ZT4nKp5uAuHzW9zcV 2E3UcL1u9QorR0l8x0kyvbUFYZIUH7wQhF57bB4PdG9FUdpy/nURv0AMgv5uVROWA7Eyx1 frw46mcrJppSSgvFfydkFjcSkzMk85g= 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-584-Uz_KEEl1OO2x9zCKOTreRQ-1; Thu, 09 May 2024 13:42:50 -0400 X-MC-Unique: Uz_KEEl1OO2x9zCKOTreRQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 6D8BC185A783 for ; Thu, 9 May 2024 17:42:50 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.18.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 449AA746820; Thu, 9 May 2024 17:42:50 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 11/21] analyzer: fix deref-before-check false positives due to inlining [PR112790] Date: Thu, 9 May 2024 13:42:26 -0400 Message-Id: <20240509174236.2278921-12-dmalcolm@redhat.com> In-Reply-To: <20240509174236.2278921-1-dmalcolm@redhat.com> References: <20240509174236.2278921-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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: Backported from commit r14-6918-g5743e1899d5964 (moving testcase from c-c++-common to gcc.dg). gcc/analyzer/ChangeLog: PR analyzer/112790 * checker-event.cc (class inlining_info): Move to... * inlining-iterator.h (class inlining_info): ...here. * sm-malloc.cc: Include "analyzer/inlining-iterator.h". (maybe_complain_about_deref_before_check): Reject stmts that were inlined from another function. gcc/testsuite/ChangeLog: PR analyzer/112790 * gcc.dg/analyzer/deref-before-check-pr112790.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/checker-event.cc | 40 ------------------- gcc/analyzer/inlining-iterator.h | 40 +++++++++++++++++++ gcc/analyzer/sm-malloc.cc | 10 +++++ .../analyzer/deref-before-check-pr112790.c | 27 +++++++++++++ 4 files changed, 77 insertions(+), 40 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr112790.c diff --git a/gcc/analyzer/checker-event.cc b/gcc/analyzer/checker-event.cc index 3612df7bd1d..3cb2fb9175c 100644 --- a/gcc/analyzer/checker-event.cc +++ b/gcc/analyzer/checker-event.cc @@ -106,46 +106,6 @@ event_kind_to_string (enum event_kind ek) } } -/* A class for fixing up fndecls and stack depths in checker_event, based - on inlining records. - - The early inliner runs before the analyzer, which can lead to confusing - output. - - Tne base fndecl and depth within a checker_event are from call strings - in program_points, which reflect the call strings after inlining. - This class lets us offset the depth and fix up the reported fndecl and - stack depth to better reflect the user's original code. */ - -class inlining_info -{ -public: - inlining_info (location_t loc) - { - inlining_iterator iter (loc); - m_inner_fndecl = iter.get_fndecl (); - int num_frames = 0; - while (!iter.done_p ()) - { - m_outer_fndecl = iter.get_fndecl (); - num_frames++; - iter.next (); - } - if (num_frames > 1) - m_extra_frames = num_frames - 1; - else - m_extra_frames = 0; - } - - tree get_inner_fndecl () const { return m_inner_fndecl; } - int get_extra_frames () const { return m_extra_frames; } - -private: - tree m_outer_fndecl; - tree m_inner_fndecl; - int m_extra_frames; -}; - /* class checker_event : public diagnostic_event. */ /* checker_event's ctor. */ diff --git a/gcc/analyzer/inlining-iterator.h b/gcc/analyzer/inlining-iterator.h index 7d4798ce6ea..76ba1dc3cf4 100644 --- a/gcc/analyzer/inlining-iterator.h +++ b/gcc/analyzer/inlining-iterator.h @@ -106,4 +106,44 @@ private: tree m_next_abstract_origin; }; +/* A class for fixing up fndecls and stack depths in checker_event, based + on inlining records. + + The early inliner runs before the analyzer, which can lead to confusing + output. + + Tne base fndecl and depth within a checker_event are from call strings + in program_points, which reflect the call strings after inlining. + This class lets us offset the depth and fix up the reported fndecl and + stack depth to better reflect the user's original code. */ + +class inlining_info +{ +public: + inlining_info (location_t loc) + { + inlining_iterator iter (loc); + m_inner_fndecl = iter.get_fndecl (); + int num_frames = 0; + while (!iter.done_p ()) + { + m_outer_fndecl = iter.get_fndecl (); + num_frames++; + iter.next (); + } + if (num_frames > 1) + m_extra_frames = num_frames - 1; + else + m_extra_frames = 0; + } + + tree get_inner_fndecl () const { return m_inner_fndecl; } + int get_extra_frames () const { return m_extra_frames; } + +private: + tree m_outer_fndecl; + tree m_inner_fndecl; + int m_extra_frames; +}; + #endif /* GCC_ANALYZER_INLINING_ITERATOR_H */ diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc index 74701375409..64295cfb66e 100644 --- a/gcc/analyzer/sm-malloc.cc +++ b/gcc/analyzer/sm-malloc.cc @@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/program-state.h" #include "analyzer/checker-event.h" #include "analyzer/exploded-graph.h" +#include "analyzer/inlining-iterator.h" #if ENABLE_ANALYZER @@ -2147,6 +2148,15 @@ maybe_complain_about_deref_before_check (sm_context *sm_ctxt, if (checked_in_frame->get_index () > assumed_nonnull_in_frame->get_index ()) return; + /* Don't complain if STMT was inlined from another function, to avoid + similar false positives involving shared helper functions. */ + if (stmt->location) + { + inlining_info info (stmt->location); + if (info.get_extra_frames () > 0) + return; + } + tree diag_ptr = sm_ctxt->get_diagnostic_tree (ptr); if (diag_ptr) sm_ctxt->warn diff --git a/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr112790.c b/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr112790.c new file mode 100644 index 00000000000..8f74468f849 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr112790.c @@ -0,0 +1,27 @@ +/* Reproducer for false positive from -Wanalyzer-deref-before-check + seen on Linux kernel's block/bdev.c due to -fanalyzer mishandling + inlined functions. */ + +/* { dg-additional-options "-O2 -g -fno-delete-null-pointer-checks" } */ + +typedef unsigned char u8; +struct inode { + void *i_mapping; + u8 i_blkbits; +}; +struct block_device { + struct inode *bd_inode; +}; +int sync_blockdev(struct block_device *bdev); +int set_blocksize(struct block_device *bdev, u8 size) { + if (bdev->bd_inode->i_blkbits != size) { /* { dg-bogus "pointer 'bdev' is dereferenced here" } */ + sync_blockdev(bdev); + } + return 0; +} +extern int filemap_write_and_wait(void *); +int sync_blockdev(struct block_device *bdev) { + if (!bdev) /* { dg-bogus "check of 'bdev' for NULL after already dereferencing it" } */ + return 0; + return filemap_write_and_wait(bdev->bd_inode->i_mapping); +} -- 2.26.3