From patchwork Fri Oct 11 14:35:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 98740 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 E23B43857003 for ; Fri, 11 Oct 2024 14:37:11 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 42BAC3858D26 for ; Fri, 11 Oct 2024 14:35:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42BAC3858D26 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 42BAC3858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728657346; cv=none; b=DMjM22n2b3in8/PalX48ikCXXawmTnQ6B4embw8y7f8snBZ9U27SC5h8f2Oy9Q2SrlbBYx96PHLPOCIFqvcpwUr1eM7icu6FmRHXTxaPGRGSMJLlspYKoOrN8w1GRq9iYIDRLsqBQoeWFMfUlShnJKVZGrL4gJizxwiSuqZvY1I= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728657346; c=relaxed/simple; bh=SJks7EL4f+Q5crL4E31aWKe2ngYqVBNwYdMJrPrPepY=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-Id:MIME-Version; b=nrlSCpcDSzB65NVDIOaM8Z0Uu7SfbH28CoDsvsBIE03vUhEjBD1eD1GGpcRkvf0bNrrIVeeG2rxlArI7dc+bWHWXAkZFBMIaqkxbkdQJu4h3TmyVYdZDpjqby3F6dzcUcLtWZcippXgSXXz57Qx/p/es8k9AOcmLRd26iC+x4uk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (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 smtp-out1.suse.de (Postfix) with ESMTPS id 26C2A22026 for ; Fri, 11 Oct 2024 14:35:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1728657342; 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=fr9yscAWYS3Qk1dODytC8yrg+pBq7yalKJmnok+3O+U=; b=m6z531JaetFDZIB1dNmTDNWcArn2TfDp1evXh+qYU0bdxZc0qEHIi2RxFTQIUbpS9zOqS6 ZwwH6r8XITH/YGPyZeJno82+kJGzY0zK9Hs7tAAh5Mtlc93W6D/YYYe+djPV1wzO6mn9L/ UyiJsPBvc8BBHQRWavrKwPwoSvhpBRo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1728657342; 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=fr9yscAWYS3Qk1dODytC8yrg+pBq7yalKJmnok+3O+U=; b=iaRFSB2OmbK8UtoFFLbx27ex/xrZ1uymiXw75SlXj+mR/IHXw84K2muGfTpKG4F70qAnZE rJ688KMQ4zSjhICQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1728657342; 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=fr9yscAWYS3Qk1dODytC8yrg+pBq7yalKJmnok+3O+U=; b=m6z531JaetFDZIB1dNmTDNWcArn2TfDp1evXh+qYU0bdxZc0qEHIi2RxFTQIUbpS9zOqS6 ZwwH6r8XITH/YGPyZeJno82+kJGzY0zK9Hs7tAAh5Mtlc93W6D/YYYe+djPV1wzO6mn9L/ UyiJsPBvc8BBHQRWavrKwPwoSvhpBRo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1728657342; 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=fr9yscAWYS3Qk1dODytC8yrg+pBq7yalKJmnok+3O+U=; b=iaRFSB2OmbK8UtoFFLbx27ex/xrZ1uymiXw75SlXj+mR/IHXw84K2muGfTpKG4F70qAnZE rJ688KMQ4zSjhICQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (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 imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 0BEE2136E0 for ; Fri, 11 Oct 2024 14:35:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id NfpcAb43CWfYFwAAD6G6ig (envelope-from ) for ; Fri, 11 Oct 2024 14:35:42 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v2 1/5] [gdb] Don't create registry keys in destructor Date: Fri, 11 Oct 2024 16:35:40 +0200 Message-Id: <20241011143544.15400-1-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 X-Spam-Score: -2.80 X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:mid]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[] X-Spam-Level: X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, 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 Creating a registry key using emplace calls new: ... DATA *result = new DATA (std::forward (args)...); ... which can throw a bad alloc, which will terminate gdb if called from a destructor. Fix this in a few places. Tested on aarch64-linux. --- gdb/ada-tasks.c | 33 +++++++++++++++++++++++---------- gdb/objfiles.c | 21 +++++++++++++++------ gdb/solib-svr4.c | 17 ++++++++++++----- gdb/source.c | 23 +++++++++++++++-------- gdb/source.h | 3 ++- gdb/symtab.c | 18 ++++++++++++------ 6 files changed, 79 insertions(+), 36 deletions(-) base-commit: 5bc1b13676bc5eeefc50d25379991f5f9255eb80 diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c index d050b269815..6d925e8e86e 100644 --- a/gdb/ada-tasks.c +++ b/gdb/ada-tasks.c @@ -287,18 +287,21 @@ task_to_str (int taskno, const ada_task_info *task_info) } /* Return the ada-tasks module's data for the given program space (PSPACE). - If none is found, add a zero'ed one now. - - This function always returns a valid object. */ + If none is found, add a zero'ed one now, unless !CREATE. */ static struct ada_tasks_pspace_data * -get_ada_tasks_pspace_data (struct program_space *pspace) +get_ada_tasks_pspace_data (struct program_space *pspace, bool create = true) { struct ada_tasks_pspace_data *data; data = ada_tasks_pspace_data_handle.get (pspace); if (data == NULL) - data = ada_tasks_pspace_data_handle.emplace (pspace); + { + if (!create) + return nullptr; + + data = ada_tasks_pspace_data_handle.emplace (pspace); + } return data; } @@ -316,13 +319,18 @@ get_ada_tasks_pspace_data (struct program_space *pspace) not use tasking. */ static struct ada_tasks_inferior_data * -get_ada_tasks_inferior_data (struct inferior *inf) +get_ada_tasks_inferior_data (struct inferior *inf, bool create = true) { struct ada_tasks_inferior_data *data; data = ada_tasks_inferior_data_handle.get (inf); if (data == NULL) - data = ada_tasks_inferior_data_handle.emplace (inf); + { + if (!create) + return nullptr; + + data = ada_tasks_inferior_data_handle.emplace (inf); + } return data; } @@ -1447,7 +1455,10 @@ ada_task_list_changed (struct inferior *inf) static void ada_tasks_invalidate_pspace_data (struct program_space *pspace) { - get_ada_tasks_pspace_data (pspace)->initialized_p = 0; + auto data = get_ada_tasks_pspace_data (pspace, false); + if (data == nullptr) + return; + data->initialized_p = 0; } /* Invalidate the per-inferior data. */ @@ -1455,8 +1466,10 @@ ada_tasks_invalidate_pspace_data (struct program_space *pspace) static void ada_tasks_invalidate_inferior_data (struct inferior *inf) { - struct ada_tasks_inferior_data *data = get_ada_tasks_inferior_data (inf); - + struct ada_tasks_inferior_data *data + = get_ada_tasks_inferior_data (inf, false); + if (data == nullptr) + return; data->known_tasks_kind = ADA_TASKS_UNKNOWN; data->task_list_valid_p = false; } diff --git a/gdb/objfiles.c b/gdb/objfiles.c index 0e076fe36be..d25d34cef6a 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -82,17 +82,22 @@ objfile_pspace_info::~objfile_pspace_info () xfree (sections); } -/* Get the current svr4 data. If none is found yet, add it now. This - function always returns a valid object. */ +/* Get the current svr4 data. If none is found yet, add it now, unless + !CREATE. */ static struct objfile_pspace_info * -get_objfile_pspace_data (struct program_space *pspace) +get_objfile_pspace_data (struct program_space *pspace, bool create = true) { struct objfile_pspace_info *info; info = objfiles_pspace_data.get (pspace); if (info == NULL) - info = objfiles_pspace_data.emplace (pspace); + { + if (!create) + return nullptr; + + info = objfiles_pspace_data.emplace (pspace); + } return info; } @@ -563,14 +568,18 @@ objfile::~objfile () { symtab_and_line cursal - = get_current_source_symtab_and_line (this->pspace ()); + = get_current_source_symtab_and_line (this->pspace (), false); if (cursal.symtab && cursal.symtab->compunit ()->objfile () == this) clear_current_source_symtab_and_line (this->pspace ()); } /* Rebuild section map next time we need it. */ - get_objfile_pspace_data (m_pspace)->section_map_dirty = 1; + { + auto info = get_objfile_pspace_data (m_pspace, false); + if (info != nullptr) + info->section_map_dirty = 1; + } } diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 7999a8e6c7e..f61af431b35 100644 --- a/gdb/solib-svr4.c +++ b/gdb/solib-svr4.c @@ -426,16 +426,21 @@ free_probes_table (struct svr4_info *info) info->probes_table.reset (nullptr); } -/* Get the svr4 data for program space PSPACE. If none is found yet, add it now. - This function always returns a valid object. */ +/* Get the svr4 data for program space PSPACE. If none is found yet, add it + now, unless !CREATE. */ static struct svr4_info * -get_svr4_info (program_space *pspace) +get_svr4_info (program_space *pspace, bool create = true) { struct svr4_info *info = solib_svr4_pspace_data.get (pspace); if (info == NULL) - info = solib_svr4_pspace_data.emplace (pspace); + { + if (!create) + return nullptr; + + info = solib_svr4_pspace_data.emplace (pspace); + } return info; } @@ -1631,7 +1636,9 @@ probes_table_htab_remove_objfile_probes (void **slot, void *info) static void probes_table_remove_objfile_probes (struct objfile *objfile) { - svr4_info *info = get_svr4_info (objfile->pspace ()); + svr4_info *info = get_svr4_info (objfile->pspace (), false); + if (info == nullptr) + return; if (info->probes_table != nullptr) htab_traverse_noresize (info->probes_table.get (), probes_table_htab_remove_objfile_probes, objfile); diff --git a/gdb/source.c b/gdb/source.c index 292d2bf71c5..8476e94bdce 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -219,29 +219,34 @@ get_lines_to_list (void) } /* A helper to return the current source location object for PSPACE, - creating it if it does not exist. */ + creating it if it does not exist, unless !CREATE. */ static current_source_location * -get_source_location (program_space *pspace) +get_source_location (program_space *pspace, bool create = true) { current_source_location *loc = current_source_key.get (pspace); if (loc == nullptr) - loc = current_source_key.emplace (pspace); + { + if (!create) + return nullptr; + + loc = current_source_key.emplace (pspace); + } return loc; } /* See source.h. */ symtab_and_line -get_current_source_symtab_and_line (program_space *pspace) +get_current_source_symtab_and_line (program_space *pspace, bool create) { symtab_and_line cursal; - current_source_location *loc = get_source_location (pspace); + current_source_location *loc = get_source_location (pspace, create); cursal.pspace = pspace; - cursal.symtab = loc->symtab (); - cursal.line = loc->line (); + cursal.symtab = loc == nullptr ? nullptr : loc->symtab (); + cursal.line = loc == nullptr ? 0 : loc->line (); cursal.pc = 0; cursal.end = 0; @@ -300,7 +305,9 @@ set_current_source_symtab_and_line (const symtab_and_line &sal) void clear_current_source_symtab_and_line (program_space *pspace) { - current_source_location *loc = get_source_location (pspace); + current_source_location *loc = current_source_key.get (pspace); + if (loc == nullptr) + return; loc->set (nullptr, 0); } diff --git a/gdb/source.h b/gdb/source.h index 541ee3b7a12..fc5cb8bad5b 100644 --- a/gdb/source.h +++ b/gdb/source.h @@ -108,9 +108,10 @@ extern int get_first_line_listed (void); extern int get_lines_to_list (void); /* Return the current source file for listing and next line to list. + Don't create a current source location object, unless CREATE. NOTE: The returned sal pc and end fields are not valid. */ extern symtab_and_line get_current_source_symtab_and_line - (program_space *pspace); + (program_space *pspace, bool create = true); /* If the current source file for listing is not set, try and get a default. Usually called before get_current_source_symtab_and_line() is called. diff --git a/gdb/symtab.c b/gdb/symtab.c index a479e920c60..6c6f4da966e 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -102,7 +102,7 @@ static struct block_symbol const domain_search_flags domain); static void set_main_name (program_space *pspace, const char *name, - language lang); + language lang, bool create = true); /* Type of the data stored on the program space. */ @@ -1756,7 +1756,7 @@ symtab_all_objfiles_removed (program_space *pspace) symbol_cache_flush (pspace); /* Forget everything we know about the main function. */ - set_main_name (pspace, nullptr, language_unknown); + set_main_name (pspace, nullptr, language_unknown, false); } /* This module's 'free_objfile' observer. */ @@ -6445,15 +6445,18 @@ make_source_files_completion_list (const char *text, const char *word) /* Return the "main_info" object for the current program space. If the object has not yet been created, create it and fill in some - default values. */ + default values, unless !CREATE. */ static main_info * -get_main_info (program_space *pspace) +get_main_info (program_space *pspace, bool create = true) { main_info *info = main_progspace_key.get (pspace); if (info == NULL) { + if (!create) + return nullptr; + /* It may seem strange to store the main name in the progspace and also in whatever objfile happens to see a main name in its debug info. The reason for this is mainly historical: @@ -6467,9 +6470,12 @@ get_main_info (program_space *pspace) } static void -set_main_name (program_space *pspace, const char *name, enum language lang) +set_main_name (program_space *pspace, const char *name, enum language lang, + bool create) { - main_info *info = get_main_info (pspace); + main_info *info = get_main_info (pspace, create); + if (info == nullptr) + return; if (!info->name_of_main.empty ()) {