From patchwork Thu May 9 17:42:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1933544 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=bd34M6km; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VZzt66jQwz20fc for ; Fri, 10 May 2024 03:44:46 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0C210387100B for ; Thu, 9 May 2024 17:44:45 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 33C37384AB6F for ; Thu, 9 May 2024 17:42:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 33C37384AB6F 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 33C37384AB6F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715276577; cv=none; b=mvn+GQ9op9DShRt9GsgDhUHXOhmvpfmenuba+GxisFo1R3b7JHEQTZaSvpVPG/2TtFCZPqsEaxx5Zo3hWnlWNEHpTeN34PsrEYWjO4VWMQ4X811Y0cNgytCwVqsypL4cbzYNCyE1CmNRBYYKuZ58SzfOOYD3fPdY+RF1NtelNO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715276577; c=relaxed/simple; bh=vOZV0uBH0BLy9UlhwYZwCNM0N1QU+gRTcYmJs5eOBZg=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=kdCtChiKdi3WLnWsan4MY6YPe4X5enUAvoXZpZCnR8XSoM93wGF+rfa73JzfUo7iuVLGqJAY9kHhOK5F2J9ojsSIXemdeNY8O5wr/AwolegJHOYWuwFuWeJ2P0ESiYr7iyJU50ce5H9e4o/H57sg9HHbKVt/IVjDipos1qima70= 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=4fcLMx/RX7K4kqjwtXb1/yNElzqM2wvgNGdXumvcjdg=; b=bd34M6km0KIaOlZ2XODcdjptIhBTzY7d9uZFExcKzpr/urJxhTRsDZgoUWXKpmp/Adh0iL 3N2Ftul5sGRvdqPkd2BBejqb0dGyVF206YB5Jx3rPstfhEovqVqUiqz5JQ75TP2ymoPm6G PNiHp3lU9Q3i1vniJB7ZKfmLolvEdzc= 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-423-T3p7hvVnMTesY4-GvjHN0Q-1; Thu, 09 May 2024 13:42:51 -0400 X-MC-Unique: T3p7hvVnMTesY4-GvjHN0Q-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 2BD95812296 for ; Thu, 9 May 2024 17:42:51 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.18.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id F4098746822; Thu, 9 May 2024 17:42:50 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 14/21] analyzer: fix skipping of debug stmts [PR113253] Date: Thu, 9 May 2024 13:42:29 -0400 Message-Id: <20240509174236.2278921-15-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 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 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 PR analyzer/113253 reports a case where the analyzer output varied with and without -g enabled. The root cause was that debug stmts were in the FOR_EACH_IMM_USE_FAST list for SSA names, leading to the analyzer's state purging logic differing between the -g and non-debugging cases, and thus leading to differences in the exploration of the user's code. Fix by skipping such stmts in the state-purging logic, and removing debug stmts when constructing the supergraph. gcc/analyzer/ChangeLog: PR analyzer/113253 * region-model.cc (region_model::on_stmt_pre): Add gcc_unreachable for debug statements. * state-purge.cc (state_purge_per_ssa_name::state_purge_per_ssa_name): Skip any debug stmts in the FOR_EACH_IMM_USE_FAST list. * supergraph.cc (supergraph::supergraph): Don't add debug stmts to the supernodes. gcc/testsuite/ChangeLog: PR analyzer/113253 * gcc.dg/analyzer/deref-before-check-pr113253.c: New test. (cherry picked from commit r14-8670-gcc7aebff74d896) Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 5 + gcc/analyzer/state-purge.cc | 9 + gcc/analyzer/supergraph.cc | 4 + .../analyzer/deref-before-check-pr113253.c | 154 ++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr113253.c diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index c98b09d5322..7e42fcdfd55 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -1237,6 +1237,11 @@ region_model::on_stmt_pre (const gimple *stmt, /* No-op for now. */ break; + case GIMPLE_DEBUG: + /* We should have stripped these out when building the supergraph. */ + gcc_unreachable (); + break; + case GIMPLE_ASSIGN: { const gassign *assign = as_a (stmt); diff --git a/gcc/analyzer/state-purge.cc b/gcc/analyzer/state-purge.cc index 3a73146d928..31a207436f0 100644 --- a/gcc/analyzer/state-purge.cc +++ b/gcc/analyzer/state-purge.cc @@ -329,6 +329,15 @@ state_purge_per_ssa_name::state_purge_per_ssa_name (const state_purge_map &map, map.log ("used by stmt: %s", pp_formatted_text (&pp)); } + if (is_gimple_debug (use_stmt)) + { + /* We skipped debug stmts when building the supergraph, + so ignore them now. */ + if (map.get_logger ()) + map.log ("skipping debug stmt"); + continue; + } + const supernode *snode = map.get_sg ().get_supernode_for_stmt (use_stmt); diff --git a/gcc/analyzer/supergraph.cc b/gcc/analyzer/supergraph.cc index a23ff15ece4..f07d68c60b8 100644 --- a/gcc/analyzer/supergraph.cc +++ b/gcc/analyzer/supergraph.cc @@ -182,6 +182,10 @@ supergraph::supergraph (logger *logger) for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) { gimple *stmt = gsi_stmt (gsi); + /* Discard debug stmts here, so we don't have to check for + them anywhere within the analyzer. */ + if (is_gimple_debug (stmt)) + continue; node_for_stmts->m_stmts.safe_push (stmt); m_stmt_to_node_t.put (stmt, node_for_stmts); m_stmt_uids.make_uid_unique (stmt); diff --git a/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr113253.c b/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr113253.c new file mode 100644 index 00000000000..d9015accd6a --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/deref-before-check-pr113253.c @@ -0,0 +1,154 @@ +/* Regression test for PR analyzer/113253 which was showing analyzer + differences with and without -g. + + C only: reduced reproducer doesn't easily work with C++. */ + +/* { dg-additional-options "-O2 -g" } */ + +typedef long int ptrdiff_t; +typedef unsigned long int uintptr_t; +typedef long int EMACS_INT; +enum +{ + EMACS_INT_WIDTH = 64, + VALBITS = EMACS_INT_WIDTH - 3, +}; +typedef struct Lisp_X* Lisp_Word; +enum Lisp_Type +{ + Lisp_Symbol = 0, + Lisp_Vectorlike = 5, +}; +typedef Lisp_Word Lisp_Object; +static inline EMACS_INT(XLI)(Lisp_Object o) +{ + return ((EMACS_INT)(o)); +} +static inline void*(XLP)(Lisp_Object o) +{ + return ((void*)(o)); +} +struct Lisp_Symbol +{}; +typedef uintptr_t Lisp_Word_tag; +extern struct Lisp_Symbol lispsym[1608]; +union vectorlike_header +{ + ptrdiff_t size; +}; +enum pvec_type +{ + PVEC_MARKER, +}; +enum More_Lisp_Bits +{ + PSEUDOVECTOR_SIZE_BITS = 12, + PSEUDOVECTOR_REST_BITS = 12, + PSEUDOVECTOR_AREA_BITS = PSEUDOVECTOR_SIZE_BITS + PSEUDOVECTOR_REST_BITS, + PVEC_TYPE_MASK = 0x3f << PSEUDOVECTOR_AREA_BITS +}; +static inline _Bool +PSEUDOVECTORP(Lisp_Object a, int code) +{ + return ( + ((((union vectorlike_header*)((uintptr_t)XLP((a)) - + (uintptr_t)( + (Lisp_Word_tag)(Lisp_Vectorlike) + << (((0x7fffffffffffffffL >> (3 - 1)) / 2 < + (9223372036854775807L)) + ? 0 + : VALBITS)))) + ->size & + (((9223372036854775807L) - (9223372036854775807L) / 2) | + PVEC_TYPE_MASK)) == + (((9223372036854775807L) - (9223372036854775807L) / 2) | + ((code) << PSEUDOVECTOR_AREA_BITS)))); +} +static inline Lisp_Object +make_lisp_symbol(struct Lisp_Symbol* sym) +{ + Lisp_Object a = ((Lisp_Word)( + ((Lisp_Word_tag)(Lisp_Symbol) + << (((0x7fffffffffffffffL >> (3 - 1)) / 2 < (9223372036854775807L)) + ? 0 + : VALBITS)))); + return a; +} +static inline Lisp_Object +builtin_lisp_symbol(int index) +{ + return make_lisp_symbol(&lispsym[index]); +} +static inline _Bool(BASE_EQ)(Lisp_Object x, Lisp_Object y) +{ + return (XLI(x) == XLI(y)); +} +static inline _Bool(NILP)(Lisp_Object x) +{ + return BASE_EQ(x, builtin_lisp_symbol(0)); +} +struct thread_state +{ + struct buffer* m_current_buffer; +}; +extern struct thread_state* current_thread; +struct Lisp_Marker +{ + struct buffer* buffer; +}; +static inline _Bool +MARKERP(Lisp_Object x) +{ + return PSEUDOVECTORP(x, PVEC_MARKER); +} +static inline struct Lisp_Marker* +XMARKER(Lisp_Object a) +{ + return (( + struct Lisp_Marker*)((uintptr_t)XLP(a) - + (uintptr_t)((Lisp_Word_tag)(Lisp_Vectorlike) + << (((0x7fffffffffffffffL >> (3 - 1)) / 2 < + (9223372036854775807L)) + ? 0 + : VALBITS)))); +} +extern void +unchain_marker(); +struct buffer +{ + Lisp_Object name_; +}; +static inline struct buffer* +XBUFFER(Lisp_Object a) +{ + return ( + (struct buffer*)((uintptr_t)XLP(a) - + (uintptr_t)((Lisp_Word_tag)(Lisp_Vectorlike) + << (((0x7fffffffffffffffL >> (3 - 1)) / 2 < + (9223372036854775807L)) + ? 0 + : VALBITS)))); +} +static inline _Bool +BUFFER_LIVE_P(struct buffer* b) +{ + return !NILP(((b)->name_)); +} +static inline struct buffer* +decode_buffer(Lisp_Object b) +{ + return NILP(b) ? (current_thread->m_current_buffer) : (XBUFFER(b)); +} +static struct buffer* +live_buffer(Lisp_Object buffer) +{ + struct buffer* b = decode_buffer(buffer); + return BUFFER_LIVE_P(b) ? b : ((void*)0); +} +Lisp_Object +set_marker_internal(Lisp_Object position, Lisp_Object buffer) +{ + struct buffer* b = live_buffer(buffer); + if (NILP(position) || (MARKERP(position) && !XMARKER(position)->buffer) || !b) /* { dg-bogus "Wanalyzer-deref-before-check" } */ + unchain_marker(); +}