From patchwork Wed Feb 14 12:32:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 85715 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 B5B25385DC2F for ; Wed, 14 Feb 2024 12:32:56 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2f.google.com (mail-io1-xd2f.google.com [IPv6:2607:f8b0:4864:20::d2f]) by sourceware.org (Postfix) with ESMTPS id 39F83385C422 for ; Wed, 14 Feb 2024 12:32:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 39F83385C422 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 39F83385C422 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707913953; cv=none; b=AdGdT3gG8hxNLb1RWvPfeafUfLgOWwQegcXXLiH1+EmI+D+9Y3lPECpxIa1QzYUddln9g4KLf0beRMd7OMqSwE4iV9wRts6LInvz/35EheIHpKtKyvhZp4mz0k2Venn+LqPBJxtZzEcRjgfeGu83x+MAzPuaS7zgUQilQ5yos80= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707913953; c=relaxed/simple; bh=2nxj3sl7ndGZ5NQKod2BdjCQ1TtkArKLGsYj2Hi0UUE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DgEKgDMxs10Yi9+hun8nZpO1BzWln8WJx5l5Zf10T5mBQbPHPz4yKKYSHAjPBWuRHxoLXGYyfGBxHiI/Yg0Yq278ILnI2fqQ7AnXvdEIoglmgjTCJQn+ocVdB6ByuRu9fQx2LkM8Xb5vwNaAk2eUO4EYMGOVz0WoYgKAol4SdJQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2f.google.com with SMTP id ca18e2360f4ac-7baa8097064so323951839f.3 for ; Wed, 14 Feb 2024 04:32:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1707913950; x=1708518750; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=hf3Ddz7BJKBy1T+Jf4gvHhWT+xC7U3G0RrqA0kMMTV4=; b=SR8aL6uKzXpGfcEtxK1+DvuQ1eyZ7XkkkZgluTiCyqiSyoxMVQ7iV2CQGFmwHptIcm XN4FaxOghzNMYm5dt8YqE74vgE/N9e2Kb7oBGCnvO8D5QsLIFOYm14edoSW4svRSp6Ux umX/1g1oTjYtuSz1mctgmhhd9DfU4cHnCtxT1X8A0P0gIQ8AAeryW8zwL3WuWn/8h4at u8FFTatoY6imGNj49LBx1yHol686c2B1s87u/FlfBt/FjpWhFWtd0P6nEabmqvkODeJ+ EYXbKCJ9CBrMsCH+g7ORFltGZG5RSCf6gnHyjXicAZDPW8KQ7HbhIqCXTlhUmoUBX6L+ 8f+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707913950; x=1708518750; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hf3Ddz7BJKBy1T+Jf4gvHhWT+xC7U3G0RrqA0kMMTV4=; b=D1Lx1eWV4gzhC0oVL7HW+UhdXBzKH7gx3FNP7vTQ41B1mZer14qUTdkTqeOoMWo7lf Hqth2t4/HisdJE+HTd7Z+LzhUcsp5bthC39ElvEHTGNBf830mO3Cr7zadlGtcs8u0/nS +CdyngbnIIjkNT91G8nXFF/lnqq8HHU4GPv0F/pSeMRjD2ae+dmpXzKVazAWftbL4dtR Z11YhLOZT8Vhb2FU8C24/cmor5pJNc4Y0LwmCYOPgjsl0zwKIDA9ooqVQ5Gml7C4lQhw 49MBWFV95KXeowyOXEt/f2RxFqbYNc3LjSjugebHuZgpwZrT+2HVVUzrqP2mexBa1x/u 6TYw== X-Gm-Message-State: AOJu0Yw2KaexA+C1QZTdoPBt0mLQ6JaP0aabp/uIq2P6nPd2+6kf8xrL K98nNxqLjbDS0zKpsvbkdRXPdvhgXTj0UFLiPxqfmlBDBTKoQVJ2hHl23/U77jm/R3JxmtJgd3s = X-Google-Smtp-Source: AGHT+IGaczb29d+rkHaHgaFn7LRMhAY97UjDStoykat/hpyj7iKuw9r4vO2whbWhNopT1f9iKjaOHw== X-Received: by 2002:a05:6602:641c:b0:7bf:b566:15d1 with SMTP id gn28-20020a056602641c00b007bfb56615d1mr3034089iob.20.1707913950457; Wed, 14 Feb 2024 04:32:30 -0800 (PST) Received: from localhost.localdomain (71-211-170-195.hlrn.qwest.net. [71.211.170.195]) by smtp.gmail.com with ESMTPSA id z14-20020a056638000e00b0046e2ce45dafsm2357539jao.165.2024.02.14.04.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 04:32:30 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Capture warnings when writing to the index cache Date: Wed, 14 Feb 2024 05:32:20 -0700 Message-ID: <20240214123220.2301600-1-tromey@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.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 PR symtab/30837 points out a race that can occur when writing to the index cache: a call to ada_encode can cause a warning, which is forbidden on a worker thread. This patch fixes the problem by arranging to capture any such warnings. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30837 --- gdb/dwarf2/cooked-index.c | 15 +++++++++++---- gdb/dwarf2/cooked-index.h | 9 ++++++--- gdb/dwarf2/read-debug-names.c | 2 +- gdb/dwarf2/read.c | 2 +- gdb/utils.h | 14 +++++++++++++- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c index dc8b6f08ff4..d42f7672260 100644 --- a/gdb/dwarf2/cooked-index.c +++ b/gdb/dwarf2/cooked-index.c @@ -614,7 +614,7 @@ cooked_index::wait (cooked_state desired_state, bool allow_quit) } void -cooked_index::set_contents (vec_type &&vec) +cooked_index::set_contents (vec_type &&vec, deferred_warnings *warn) { gdb_assert (m_vector.empty ()); m_vector = std::move (vec); @@ -628,10 +628,10 @@ cooked_index::set_contents (vec_type &&vec) finalization. However, that would take a slot in the global thread pool, and if enough such tasks were submitted at once, it would cause a livelock. */ - gdb::task_group finalizers ([this, ctx = std::move (ctx)] () + gdb::task_group finalizers ([this, warn, ctx = std::move (ctx)] () { m_state->set (cooked_state::FINALIZED); - maybe_write_index (m_per_bfd, ctx); + maybe_write_index (m_per_bfd, ctx, warn); }); for (auto &idx : m_vector) @@ -838,10 +838,17 @@ cooked_index::dump (gdbarch *arch) void cooked_index::maybe_write_index (dwarf2_per_bfd *per_bfd, - const index_cache_store_context &ctx) + const index_cache_store_context &ctx, + deferred_warnings *warn) { if (index_for_writing () != nullptr) { + /* Writing to the index cache may cause a warning to be + emitted. See PR symtab/30837. This arranges to capture all + such warnings. This is safe because we know the + deferred_warnings object isn't in use by any other thread at + this point. */ + scoped_restore_warning_hook defer (*warn); /* (maybe) store an index in the cache. */ global_index_cache.store (m_per_bfd, ctx); } diff --git a/gdb/dwarf2/cooked-index.h b/gdb/dwarf2/cooked-index.h index 629a5b6b9ee..4ed52d8871c 100644 --- a/gdb/dwarf2/cooked-index.h +++ b/gdb/dwarf2/cooked-index.h @@ -607,8 +607,10 @@ class cooked_index : public dwarf_scanner_base void start_reading (); /* Called by cooked_index_worker to set the contents of this index - and transition to the MAIN_AVAILABLE state. */ - void set_contents (vec_type &&vec); + and transition to the MAIN_AVAILABLE state. WARN is used to + collect any warnings that may arise when writing to the + cache. */ + void set_contents (vec_type &&vec, deferred_warnings *warn); /* A range over a vector of subranges. */ using range = range_chain; @@ -673,7 +675,8 @@ class cooked_index : public dwarf_scanner_base /* Maybe write the index to the index cache. */ void maybe_write_index (dwarf2_per_bfd *per_bfd, - const index_cache_store_context &); + const index_cache_store_context &ctx, + deferred_warnings *warn); /* The vector of cooked_index objects. This is stored because the entries are stored on the obstacks in those objects. */ diff --git a/gdb/dwarf2/read-debug-names.c b/gdb/dwarf2/read-debug-names.c index e2563e84fcf..e325711af3a 100644 --- a/gdb/dwarf2/read-debug-names.c +++ b/gdb/dwarf2/read-debug-names.c @@ -348,7 +348,7 @@ cooked_index_debug_names::do_reading () cooked_index *table = (gdb::checked_static_cast (per_bfd->index_table.get ())); - table->set_contents (std::move (indexes)); + table->set_contents (std::move (indexes), &m_warnings); bfd_thread_cleanup (); } diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 486be7e4921..4c2ea39565a 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -4916,7 +4916,7 @@ cooked_index_debug_info::done_reading () cooked_index *table = (gdb::checked_static_cast (per_bfd->index_table.get ())); - table->set_contents (std::move (indexes)); + table->set_contents (std::move (indexes), &m_warnings); } void diff --git a/gdb/utils.h b/gdb/utils.h index 2acd1fc4624..d7db1d84e2f 100644 --- a/gdb/utils.h +++ b/gdb/utils.h @@ -21,6 +21,7 @@ #include "exceptions.h" #include "gdbsupport/array-view.h" +#include "gdbsupport/function-view.h" #include "gdbsupport/scoped_restore.h" #include @@ -374,7 +375,7 @@ assign_return_if_changed (T &lval, const T &val) } /* A function that can be used to intercept warnings. */ -typedef void (*warning_hook_handler) (const char *, va_list); +typedef gdb::function_view warning_hook_handler; /* Set the thread-local warning hook, and restore the old value when finished. */ @@ -439,6 +440,17 @@ struct deferred_warnings m_warnings.emplace_back (std::move (msg)); } + /* A variant of 'warn' so that this object can be used as a warning + hook; see scoped_restore_warning_hook. Note that no locking is + done, so users have to be careful to only install this into a + single thread at a time. */ + void operator() (const char *format, va_list args) ATTRIBUTE_PRINTF (2, 0) + { + string_file msg (m_can_style); + msg.vprintf (format, args); + m_warnings.emplace_back (std::move (msg)); + } + /* Emit all warnings. */ void emit () const {