From patchwork Wed May 3 17:58:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 68704 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 09F77385770E for ; Wed, 3 May 2023 17:58:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09F77385770E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1683136732; bh=M0CElmccCEepmrSeNKOnsTS+BoQclVDAX5hOydHAbzY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=v3AJfpy5U5W1KW/9FJapxRo+fKsx5dWF2i8uc1ob0zBYrPVFZL4Tiqjp4wlm9vpJy 51nvkIwrVENYXSgYQA+d8JRw44+Ysa4dm0XaJQu+V6l8zJFSyi17i2XjnRaP0t9l/L N+CYiXtaa36UEcQGdDJg8AKeuKydDyGfu0eecjDk= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 62CE73858D28 for ; Wed, 3 May 2023 17:58:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 62CE73858D28 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 903DB20183; Wed, 3 May 2023 17:58:27 +0000 (UTC) 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 7883C13584; Wed, 3 May 2023 17:58:27 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id gvRGHMOgUmQtMwAAMHmgww (envelope-from ); Wed, 03 May 2023 17:58:27 +0000 To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] [gdb/build] Fix frame_list position in frame.c Date: Wed, 3 May 2023 19:58:26 +0200 Message-Id: <20230503175826.4242-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, 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: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom de Vries via Gdb-patches From: Tom de Vries Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" In commit 995a34b1772 ("Guard against frame.c destructors running before frame-info.c's") the following problem was addressed. The frame_info_ptr destructor: ... ~frame_info_ptr () { frame_list.erase (frame_list.iterator_to (*this)); } ... uses frame_list, which is a static member of class frame_info_ptr, instantiated in frame-info.c: ... intrusive_list frame_info_ptr::frame_list; ... Then there's a static frame_info_pointer variable named selected_frame in frame.c: ... static frame_info_ptr selected_frame; ... Because the destructor of selected_frame uses frame_list, its destructor needs to be called before the destructor of frame_list. But because they're in different compilation units, the initialization order and consequently destruction order is not guarantueed. The commit fixed this by handling the case that the destructor of frame_list is called first, adding a check on is_linked (): ... ~frame_info_ptr () { - frame_list.erase (frame_list.iterator_to (*this)); + /* If this node has static storage, it may be deleted after + frame_list. Attempting to erase ourselves would then trigger + internal errors, so make sure we are still linked first. */ + if (is_linked ()) + frame_list.erase (frame_list.iterator_to (*this)); } ... However, since then frame_list has been moved into frame.c, and initialization/destruction order is guarantueed inside a compilation unit. Revert aforementioned commit, and fix the destruction order problem by moving frame_list before selected_frame. Reverting the commit is another way of fixing the already fixed Wdangling-pointer warning reported in PR build/30413, in a different way than commit 9b0ccb1ebae ("Pass const frame_info_ptr reference for skip_[language_]trampoline"). Tested on x86_64-linux. PR build/30413 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30413 --- gdb/frame.c | 11 +++++++---- gdb/frame.h | 9 ++++----- 2 files changed, 11 insertions(+), 9 deletions(-) base-commit: 2ad00a4b42f89b61fdab24940b67713daf81c988 diff --git a/gdb/frame.c b/gdb/frame.c index 36fb02f3c8e..531eadf3d54 100644 --- a/gdb/frame.c +++ b/gdb/frame.c @@ -1733,6 +1733,13 @@ get_current_frame (void) static frame_id selected_frame_id = null_frame_id; static int selected_frame_level = -1; +/* See frame.h. This definition should come before any definition of a static + frame_info_ptr, to ensure that frame_list is destroyed after any static + frame_info_ptr. This is necessary because the destructor of frame_info_ptr + uses frame_list. */ + +intrusive_list frame_info_ptr::frame_list; + /* The cached frame_info object pointing to the selected frame. Looked up on demand by get_selected_frame. */ static frame_info_ptr selected_frame; @@ -3275,10 +3282,6 @@ maintenance_print_frame_id (const char *args, int from_tty) /* See frame-info-ptr.h. */ -intrusive_list frame_info_ptr::frame_list; - -/* See frame-info-ptr.h. */ - frame_info_ptr::frame_info_ptr (struct frame_info *ptr) : m_ptr (ptr) { diff --git a/gdb/frame.h b/gdb/frame.h index 6ed8db0af56..ed19dfdc090 100644 --- a/gdb/frame.h +++ b/gdb/frame.h @@ -254,11 +254,10 @@ class frame_info_ptr : public intrusive_list_node ~frame_info_ptr () { - /* If this node has static storage, it may be deleted after - frame_list. Attempting to erase ourselves would then trigger - internal errors, so make sure we are still linked first. */ - if (is_linked ()) - frame_list.erase (frame_list.iterator_to (*this)); + /* If this node has static storage, it should be be deleted before + frame_list. Verify this by checking that it is still in the list. */ + gdb_assert (is_linked ()); + frame_list.erase (frame_list.iterator_to (*this)); } frame_info_ptr &operator= (const frame_info_ptr &other)