From patchwork Fri Oct 18 00:02:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 99111 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 180BA3858C2B for ; Fri, 18 Oct 2024 00:04:40 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta40.uswest2.a.cloudfilter.net (omta40.uswest2.a.cloudfilter.net [35.89.44.39]) by sourceware.org (Postfix) with ESMTPS id 8C5EC3858C42 for ; Fri, 18 Oct 2024 00:02:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C5EC3858C42 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8C5EC3858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.39 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729209793; cv=none; b=gRyXh5neQtD1sFa4F68zWZJ1N8pQxk7KXiVcR4HPNwUbqADA5tmjhpPpqcYu7HfhcBoULFW8X/wrvtxcWu3OcZws91W/0hxm+ZBUDCe+CRD49aXw5v8i8836at58iCrv7VvaAbTnRk5qBn8NcSaqfLaTW+vi18n9X6+cr20ta5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729209793; c=relaxed/simple; bh=982FfJwFXlD24WTsla7eCnsn6Tj5684k85EzAO40zC8=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=I8LQzL0JP0JHQG4pUX1kvXFlRsW5aLs2uT6EAeu1esgiS9IsX872IC/7PITEvbmTH7uZb32dfIyAxRxVzSofCskxmHNQAXfK7BtukCbvhdRxXHnKLHG6f3cDGLh5EMhIDTMCbh0cQQF+MKgTNfmH5OiGPjP0L+mzXt2aU3u8EYg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-6004a.ext.cloudfilter.net ([10.0.30.197]) by cmsmtp with ESMTPS id 1TXCtinCavH7l1aS2t948Z; Fri, 18 Oct 2024 00:02:42 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id 1aS0tH7ytWdNZ1aS1tCGHg; Fri, 18 Oct 2024 00:02:41 +0000 X-Authority-Analysis: v=2.4 cv=FtTO/Hrq c=1 sm=1 tr=0 ts=6711a5a1 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=DAUX931o1VcA:10 a=ItBw4LHWJt0A:10 a=Gnwpio9NvWRAsuPnYAsA:9 a=QEXdDO2ut3YA:10 a=6Ogn3jAGHLSNbaov7Orx:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=inHRy4To0F8UJWzZ29fwdtTARAYB1Vy3vMFiZfoO70Q=; b=F1aqzavO+NJ0KJAVrrhipSxfbb WA1wFk6PHnUfGRZ/5jMH4PWPvZBF9efoDnp0CWzFZJ6sAi52snZIwuYJQudJR5lel1LeEU+0T1/oF ZBROhhybXGOhNpRRvDMeEwjEt; Received: from c-67-190-86-1.hsd1.co.comcast.net ([67.190.86.1]:50290 helo=prentzel.local) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1t1aS0-0025wO-2A for gdb-patches@sourceware.org; Thu, 17 Oct 2024 18:02:40 -0600 From: Tom Tromey Date: Thu, 17 Oct 2024 18:02:37 -0600 Subject: [PATCH 1/2] Use gdb_bfd_get_full_section_contents in auto-load.c MIME-Version: 1.0 Message-Id: <20241017-fix-race-pr-symtab-31626-v1-1-3a53459e9651@tromey.com> References: <20241017-fix-race-pr-symtab-31626-v1-0-3a53459e9651@tromey.com> In-Reply-To: <20241017-fix-race-pr-symtab-31626-v1-0-3a53459e9651@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.14.2 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 67.190.86.1 X-Source-L: No X-Exim-ID: 1t1aS0-0025wO-2A X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-67-190-86-1.hsd1.co.comcast.net (prentzel.local) [67.190.86.1]:50290 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfAaMrIYA4UT/KfyrW11C/36wWPLsN3/VjWTxp4TN9HnCqNp3m2kKBlMiuEdOkMpnuAwv+fbZymVpBI2BZ0+a0Ltp9TQ9OMI6wu+4v9xzr94HkyddQmfW U6btSWhxVa8Q0So9h6AuLRlPjs97NMG3JIwlM0YWP/BaWmyIHKvrwAuVw4kIxkaaOhsEyirZGuBR90MdQ537EBLJrIoyuZiL5zI= X-Spam-Status: No, score=-3018.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_MSPIKE_H2, 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 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~patchwork=sourceware.org@sourceware.org This changes auto-load.c ot use gdb_bfd_get_full_section_contents. This shouldn't change any behavior, but makes it easier to add locking in a subsequent patch. --- gdb/auto-load.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/gdb/auto-load.c b/gdb/auto-load.c index e753333b1cdfe6a12b1885d1468ef92ef51776af..d4307eac6273803ed7024491169701e8a55fcc6b 100644 --- a/gdb/auto-load.c +++ b/gdb/auto-load.c @@ -1114,25 +1114,22 @@ auto_load_section_scripts (struct objfile *objfile, const char *section_name) { bfd *abfd = objfile->obfd.get (); asection *scripts_sect; - bfd_byte *data = NULL; scripts_sect = bfd_get_section_by_name (abfd, section_name); if (scripts_sect == NULL || (bfd_section_flags (scripts_sect) & SEC_HAS_CONTENTS) == 0) return; - if (!bfd_get_full_section_contents (abfd, scripts_sect, &data)) + gdb::byte_vector data; + if (!gdb_bfd_get_full_section_contents (abfd, scripts_sect, &data)) warning (_("Couldn't read %s section of %ps"), section_name, styled_string (file_name_style.style (), bfd_get_filename (abfd))); else { - gdb::unique_xmalloc_ptr data_holder (data); - - char *p = (char *) data; - source_section_scripts (objfile, section_name, p, - p + bfd_section_size (scripts_sect)); + const char *p = (const char *) data.data (); + source_section_scripts (objfile, section_name, p, p + data.size ()); } } From patchwork Fri Oct 18 00:02:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 99110 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 6E2A43858C35 for ; Fri, 18 Oct 2024 00:03:45 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from omta036.useast.a.cloudfilter.net (omta036.useast.a.cloudfilter.net [44.202.169.35]) by sourceware.org (Postfix) with ESMTPS id 633DA3858D20 for ; Fri, 18 Oct 2024 00:02:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 633DA3858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 633DA3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=44.202.169.35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729209793; cv=none; b=TbTLY3exXVn31ekT7EiAjd+yeR5nJMdbfR9SptqPNYXjna3KzujOCtx4gdj/GSinjo8zpUG1yEX4KSlGz7wuMzf1GHuggGC77OQoR3p5BwjDfyU/13BHzBLwSbtPFENL0AuUBX7ub0KuvvX/AkOU0ykewGpQIJRO1FA3ncPuVQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729209793; c=relaxed/simple; bh=Nv5jTajPQGYlgSOdPuGITEn4H1tFYWtjtELrWTxaFp8=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=oVP0O+uYFsD1gbPkUDNWbUXhdKftqOhOwirZZ6x8/+Hqjwy8WXJO7fr+ggn/64uWUrEuvRTJ1+SpZQsJ0pbxn+NhZSFGhQfJizworO78xArjAi/tbjr6w+t2XWGG9upCEU8nJnuKDfygJVbcTRktjqPdmUvEbDnqiMa4FgrPQcM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-5006a.ext.cloudfilter.net ([10.0.29.179]) by cmsmtp with ESMTPS id 1TdhtQOuJiA191aS2tpz1f; Fri, 18 Oct 2024 00:02:42 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id 1aS1tUxwKRuF71aS1toiki; Fri, 18 Oct 2024 00:02:41 +0000 X-Authority-Analysis: v=2.4 cv=YdO75RRf c=1 sm=1 tr=0 ts=6711a5a1 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=IkcTkHD0fZMA:10 a=DAUX931o1VcA:10 a=ItBw4LHWJt0A:10 a=CCpqsmhAAAAA:8 a=QU_moPmdLw-veQOo3qEA:9 a=QEXdDO2ut3YA:10 a=ul9cdbp4aOFLsgKbc677:22 a=6Ogn3jAGHLSNbaov7Orx:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=5hLKFiTq5iyzQR3+fDgx+GK9Y+Z050gZRsykUEc3BRM=; b=fbqATddp61QOcez5ZYgw7wABtU wFaOmxHSNOisT5sXCBW2wFHWJr4LD/t1hAnNIqJ+vUVNHy7Bn1P8PoHmnJzc64bqmlZBQitBvw7Zf x4z4mF9A7RnjGf43z+psfVYcM; Received: from c-67-190-86-1.hsd1.co.comcast.net ([67.190.86.1]:50290 helo=prentzel.local) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1t1aS0-0025wO-2k for gdb-patches@sourceware.org; Thu, 17 Oct 2024 18:02:40 -0600 From: Tom Tromey Date: Thu, 17 Oct 2024 18:02:38 -0600 Subject: [PATCH 2/2] Add locking when reading BFD sections MIME-Version: 1.0 Message-Id: <20241017-fix-race-pr-symtab-31626-v1-2-3a53459e9651@tromey.com> References: <20241017-fix-race-pr-symtab-31626-v1-0-3a53459e9651@tromey.com> In-Reply-To: <20241017-fix-race-pr-symtab-31626-v1-0-3a53459e9651@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.14.2 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 67.190.86.1 X-Source-L: No X-Exim-ID: 1t1aS0-0025wO-2k X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-67-190-86-1.hsd1.co.comcast.net (prentzel.local) [67.190.86.1]:50290 X-Source-Auth: tom+tromey.com X-Email-Count: 3 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfO9T/JMhcD/HvFPvSCHcgWQwsil4RunhTpsdKEJrg5SXQz8xwIyKtNtsTCda7fbpy74o2s1hSDR0It0y17O2yWO0QEMHaA7wWhEAHQrwRc9TJgBjp9Df ZtjO8lynTsQNnfU7Z3ihuha8Bzpb8jtkZ+viM2n/yrMgPINXdCeb5fVBkbzFeKtGHaIO1QOe2qo/M/3vRkg1a2GKINJO6icD/zM= X-Spam-Status: No, score=-3020.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 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~patchwork=sourceware.org@sourceware.org This adds some per-BFD locking to gdb_bfd_map_section and gdb_bfd_get_full_section_contents. It turned out that the background DWARF reader could race with the auto-load code, because the reader might try to mmap a section when the main thread was trying to read in .debug_gdb_scripts. The current BFD threading model is that only BFD globals will be locked, so any multi-threaded use of a BFD has to be handled specially by the application. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31626 --- gdb/gdb_bfd.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c index b142f985dcd99e6d29f4a6b295ebff50ca851293..fff71d0c3bdb900e180ec136429bf66c030fd1bc 100644 --- a/gdb/gdb_bfd.c +++ b/gdb/gdb_bfd.c @@ -144,6 +144,19 @@ struct gdb_bfd_data /* The registry. */ registry registry_fields; + +#if CXX_STD_THREAD + /* Most of the locking needed for multi-threaded operation is + handled by BFD itself. However, the current BFD model is that + locking is only needed for global operations -- but it turned out + that the background DWARF reader could race with the auto-load + code reading the .debug_gdb_scripts section from the same BFD. + + This lock is the fix: wrappers for important BFD functions will + acquire this lock before performing operations that might modify + the state of this BFD. */ + std::mutex per_bfd_mutex; +#endif }; registry * @@ -777,6 +790,11 @@ gdb_bfd_map_section (asection *sectp, bfd_size_type *size) abfd = sectp->owner; +#if CXX_STD_THREAD + gdb_bfd_data *gdata = (gdb_bfd_data *) bfd_usrdata (abfd); + std::lock_guard guard (gdata->per_bfd_mutex); +#endif + descriptor = get_section_descriptor (sectp); /* If the data was already read for this BFD, just reuse it. */ @@ -1108,6 +1126,11 @@ bool gdb_bfd_get_full_section_contents (bfd *abfd, asection *section, gdb::byte_vector *contents) { +#if CXX_STD_THREAD + gdb_bfd_data *gdata = (gdb_bfd_data *) bfd_usrdata (abfd); + std::lock_guard guard (gdata->per_bfd_mutex); +#endif + bfd_size_type section_size = bfd_section_size (section); contents->resize (section_size);