From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 7KbaIiH4i2jtMgEAWB0awg (envelope-from ) for ; Thu, 31 Jul 2025 19:11:29 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=gdcproject.org header.i=@gdcproject.org header.a=rsa-sha256 header.s=MBO0001 header.b=cIykio4t; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 69D061E11C; Thu, 31 Jul 2025 19:11:29 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-9.1 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL, RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.1 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 (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 1D0AF1E089 for ; Thu, 31 Jul 2025 19:11:21 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 837C23858C39 for ; Thu, 31 Jul 2025 23:11:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 837C23858C39 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gdcproject.org header.i=@gdcproject.org header.a=rsa-sha256 header.s=MBO0001 header.b=cIykio4t Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [IPv6:2001:67c:2050:0:465::102]) by sourceware.org (Postfix) with ESMTPS id 325FE3858D32 for ; Thu, 31 Jul 2025 23:10:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 325FE3858D32 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gdcproject.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gdcproject.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 325FE3858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2050:0:465::102 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1754003450; cv=none; b=SJoE0fFbovbxk+V6n57V3xAwU8FEHM2h3bY4W2oluSsM7VXhyUQUvqaZRp49k8jDb2ks9q4a5XsZOCwK72VET6Ndj5lwZywkO3uu56CifFYY4QTp4sjLTHDFOEJIFSRLLER3itAtMiMAyum9B7Nzw3zBa4n8kORZJ3OIm2ZKi84= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1754003450; c=relaxed/simple; bh=74h58BTuLb+GxRk0cj0wav87Pi1XTf+sHnOI5SB2Mis=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=htHuDKovTDB0XF2EfhNy2xEU0SV5V0t0AQ+2lUOOmLpd0zfR6AyqcPcoR4OruLkYbRnM7tQT1IunhfrRSgui0UQZwKKP/SfSOOwEVpPyh8YKXED/rhR39gJvNXHBciupHtnQp0uufI8Yc8H940XxluHGVp8ue4sf7YhyYNQcrQM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 325FE3858D32 Received: from smtp102.mailbox.org (smtp102.mailbox.org [10.196.197.102]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4btPvT6g74z9tkn; Fri, 1 Aug 2025 01:10:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gdcproject.org; s=MBO0001; t=1754003446; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/WcbscGsWlTC0qTC12D35X/LWDl829hgnCrdQ6yPw6s=; b=cIykio4tOINRcTKMflzIGgi2lfsjl6MlL+dYj7vxCTg4d0kAvIF9zQIWy6tOhqqaP+1uXi GjY2yPJIevTlPBG6EFwnwCRF6BUFmTx69JA2fW8T3i5bkoY2HIVFQGWUZ4+JpUecIQXtTs P+FLXEH7OTbd4xGT29SZ4uJOdXLO1Ofs/GR8UFxO/J+lz+O2mFl07bXEolIvIT4D5jrk9I dlb3uDeIAxpDQF62VFvbP5+iClkmttCj59/lMsUGhbZ/N0ARYvpGEp+XRVtYTZ2mX0/QO5 oQYQcPma14/DUbWBIbglkJZECaZpbLRELQ9knAsSG3jyCqXiQQI8s7gxXjEa3g== From: Iain Buclaw To: gdb-patches@sourceware.org Cc: tom@tromey.com, Iain Buclaw Subject: [PATCH v2] gdb, d: Fix PR 33200 - Segfault in d_lookup_symbol Date: Fri, 1 Aug 2025 01:09:46 +0200 Message-ID: <20250731230946.102908-1-ibuclaw@gdcproject.org> In-Reply-To: <1753985679.4lko1qk7ka.astroid@pulse.none> References: <1753985679.4lko1qk7ka.astroid@pulse.none> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org After some trial and error, I think I've managed to create a minimal Dwarf test case. --- If d_lookup_symbol tries to locate a member variable within a class or struct type with no a name, type->name() is NULL, so the assignment to std::string classname crashes gdb. This can happen in some older versions of gdc, where debug info was generated too eagerly in complex cases involving types referencing each other recursively. This patch adds a guard against this case, allowing the caller of d_lookup_symbol to search other blocks for the right symbol. --- gdb/d-namespace.c | 4 ++ gdb/testsuite/gdb.dlang/namelessclass.c | 31 ++++++++++ gdb/testsuite/gdb.dlang/namelessclass.exp | 75 +++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 gdb/testsuite/gdb.dlang/namelessclass.c create mode 100644 gdb/testsuite/gdb.dlang/namelessclass.exp diff --git a/gdb/d-namespace.c b/gdb/d-namespace.c index b5e046efaa7..cdfa67a051c 100644 --- a/gdb/d-namespace.c +++ b/gdb/d-namespace.c @@ -131,6 +131,10 @@ d_lookup_symbol (const struct language_defn *langdef, return {}; type = check_typedef (lang_this.symbol->type ()->target_type ()); + /* If type name is NULL, abandon trying to find this symbol. */ + if (type->name () == nullptr) + return {}; + classname = type->name (); nested = name; } diff --git a/gdb/testsuite/gdb.dlang/namelessclass.c b/gdb/testsuite/gdb.dlang/namelessclass.c new file mode 100644 index 00000000000..837e33f5764 --- /dev/null +++ b/gdb/testsuite/gdb.dlang/namelessclass.c @@ -0,0 +1,31 @@ +/* Copyright 2025 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* DWARF will describe this function as being inside an anonymous class within + a D module. */ + +void +doit (void *this) +{ + asm ("doit_label: .globl doit_label"); +} + +int +main (void) +{ + doit (0); + return 0; +} + diff --git a/gdb/testsuite/gdb.dlang/namelessclass.exp b/gdb/testsuite/gdb.dlang/namelessclass.exp new file mode 100644 index 00000000000..3246ae33849 --- /dev/null +++ b/gdb/testsuite/gdb.dlang/namelessclass.exp @@ -0,0 +1,75 @@ +# Copyright 2025 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test nameless classes that may be output by the compiler. +# PR gdb/33200 + +load_lib "d-support.exp" +load_lib "dwarf.exp" + +# Do not run in environments which do not support D. +require dwarf2_support allow_d_tests + +standard_testfile .c -dw.S + +# Make some DWARF for the test. +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + cu {} { + compile_unit { + {language @DW_LANG_D} + } { + declare_labels class_label class_ptr_label + set class_size 1 + set ptr_size 4 + + module { + {name namelessclass} + } { + class_label: class_type { + {byte_size $class_size sdata} + {name ""} + } + class_ptr_label: pointer_type { + {byte_size $ptr_size data1} + {type :$class_label} + } + subprogram { + {MACRO_AT_func {"doit"}} + {external 1 flag_present} + } { + formal_parameter { + {name "this"} + {type :$class_ptr_label} + } + } + } + } + } +} + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}] } { + return -1 +} + +gdb_test_no_output "set language d" + +if {![runto "doit"]} { + return -1 +} + +# Any output is accepted as valid as long as gdb does not segfault. +gdb_test "print do_not_segfault" ".*" -- 2.43.0