From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id DCF403858C52 for ; Wed, 16 Aug 2023 12:56:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DCF403858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id D8CAE1F74C for ; Wed, 16 Aug 2023 12:56:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1692190588; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Q3R4emDPrxbIo/I5TTjkpXUTOpKpPQ2d0WRGkp6xPKg=; b=SGKfKzeAmV6mD1bOz7WMtlIRdhydHQbcfcc150GB4vy9GQYY1n143nLhR6RSa6so7bl60I lqP9sXGhaAc+drqJst/3v4FFJW4G1RzDPFimWp1I291J66Tt5e9EPmGRD/l1C3HTmOr5zO CFc75DW1QoqfZPeKDqyN1oMNqwUuDys= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1692190588; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Q3R4emDPrxbIo/I5TTjkpXUTOpKpPQ2d0WRGkp6xPKg=; b=Tv1kQSA50fnlFkMQm3e+G58YrRxZswEPKCtz7l5KvlnviIjz3j5Wd6LlzBZdjxv1/xMFJY onpjje/MYk6HPlBA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C890A1353E for ; Wed, 16 Aug 2023 12:56:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id U2UVMHzH3GTuRgAAMHmgww (envelope-from ) for ; Wed, 16 Aug 2023 12:56:28 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH] [gdb/symtab] Handle self-reference DIE Date: Wed, 16 Aug 2023 14:56:02 +0200 Message-Id: <20230816125602.23696-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,SPF_HELO_NONE,SPF_PASS,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: While working on a dwarf assembly test-case I accidentally created the following pathological dwarf: ... <1>: Abbrev Number: 3 (DW_TAG_class_type) DW_AT_name : c1 DW_AT_specification: <0xbe> ... and noticed gdb segfaulting during cooked index creating due to running out of stack. This is a regression from gdb-12, where gdb just hung. Fix this by inhibiting the scan_attributes self-recursion for self-references. The same test-case with -readnow makes gdb hang, so also fix this in dwarf2_attr and follow_die_ref. Note that this doesn't fix the same problems for the more complicated case of: ... <1>: Abbrev Number: 3 (DW_TAG_class_type) DW_AT_name : c1 DW_AT_specification: <0xc6> <1>: Abbrev Number: 4 (DW_TAG_class_type) DW_AT_name : c2 DW_AT_specification: <0xbe> ... but the approach for deciding whether to fix pathological dwarf cases is as per PR27981 comment 3: ... yes if it is cheap/obvious, and no if it is something complicated or expensive. ... and at this point I'm not sure whether fixing this will fall in the first category. Tested on x86_64-linux. --- gdb/dwarf2/read.c | 22 +++++++++-- gdb/testsuite/gdb.dwarf2/self-spec.exp | 52 ++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/self-spec.exp diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index a64f82bd24a..245117cc1c7 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -16375,9 +16375,15 @@ cooked_indexer::scan_attributes (dwarf2_per_cu_data *scanning_per_cu, new_info_ptr, &bytes_read); new_info_ptr += bytes_read; - scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr, - new_abbrev, name, linkage_name, flags, nullptr, - parent_entry, maybe_defer, true); + + if (new_reader->cu == reader->cu && new_info_ptr == watermark_ptr) + { + /* Self-reference, we're done. */ + } + else + scan_attributes (scanning_per_cu, new_reader, new_info_ptr, + new_info_ptr, new_abbrev, name, linkage_name, + flags, nullptr, parent_entry, maybe_defer, true); } } @@ -17888,7 +17894,11 @@ dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu) if (!spec) break; + struct die_info *prev_die = die; die = follow_die_ref (die, spec, &cu); + if (die == prev_die) + /* Self-reference, we're done. */ + break; } return NULL; @@ -20540,6 +20550,12 @@ follow_die_ref (struct die_info *src_die, const struct attribute *attr, struct dwarf2_cu *cu = *ref_cu; struct die_info *die; + if (attr->form != DW_FORM_GNU_ref_alt && src_die->sect_off == sect_off) + { + /* Self-reference, we're done. */ + return src_die; + } + die = follow_die_offset (sect_off, (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz), diff --git a/gdb/testsuite/gdb.dwarf2/self-spec.exp b/gdb/testsuite/gdb.dwarf2/self-spec.exp new file mode 100644 index 00000000000..71e7c1210a6 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/self-spec.exp @@ -0,0 +1,52 @@ +# Copyright 2023 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 . + +# Check that gdb doesn't hang or segfault on reading a DIE with a +# specification reference to itself. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile main.c .S + +# Create the DWARF. +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + cu {} { + compile_unit {{language @DW_LANG_C_plus_plus}} { + declare_labels c1 + c1: class_type { + {name c1} + {specification :$c1} + } + } + } +} + +if [prepare_for_testing "failed to prepare" $testfile "${asm_file} ${srcfile}" {}] { + return -1 +} + +set index [have_index $binfile] +require {string eq $index ""} + +require !readnow + +gdb_test "maint print objfiles $testfile" \ + "\r\n *qualified: *c1\r\n.*" \ + "class c1 in cooked index" + +gdb_test "maint expand-symtabs" base-commit: da1f552dc79476275af282b65c5317ab3b4dbd9a -- 2.35.3