From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 9iuaBzMfBGWThhYAWB0awg (envelope-from ) for ; Fri, 15 Sep 2023 05:09:07 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=mlLi6MrE; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0D6C51E0C3; Fri, 15 Sep 2023 05:09:07 -0400 (EDT) Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id EF6221E028 for ; Fri, 15 Sep 2023 05:09:04 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 697153858284 for ; Fri, 15 Sep 2023 09:09:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 697153858284 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1694768944; bh=vKuj41gnv0I/Hncam6hYPxAyvsQ4pI9zZHR5UdyuiQM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=mlLi6MrE0oLTf5FefOtvF6A8sykz1DGCXeogsJc39uvb+Rq/Apjo1NQHnWwbfF8aX QQbj9LDnTtnWwrNEo5JDRIr4JhsSZ88vGyn8jghAlimiO9KGEx1nfbyWhezfyBW4Sn 9fHHxZJZSmo1CKM4K90xrYvFiHtR309i02d2DgNs= Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 67CC13858D35 for ; Fri, 15 Sep 2023 09:08:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 67CC13858D35 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 559961F890; Fri, 15 Sep 2023 09:08:42 +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 3CED11358A; Fri, 15 Sep 2023 09:08:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id GgWzDRofBGVQVwAAMHmgww (envelope-from ); Fri, 15 Sep 2023 09:08:42 +0000 To: gdb-patches@sourceware.org Subject: [PATCH] [gdb/symtab] Fix overly large gdb-index file check for 32-bit Date: Fri, 15 Sep 2023 11:08:37 +0200 Message-Id: <20230915090837.16945-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=-11.9 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 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: , From: Tom de Vries via Gdb-patches Reply-To: Tom de Vries Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" Add a unit test which checks that write_gdb_index_1 will throw an error when the size of the file would exceed the maximum value capable of being represented by 'offset_type'. The unit test fails on 32-bit systems due to wrapping overflow. Fix this by changing the type of total_len in write_gdbindex_1 from size_t to uint64_t. Tested on x86_64-linux. Co-Authored-By: Kevin Buettner --- gdb/dwarf2/index-write.c | 84 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 3827a810130..1b5d4c10b0c 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -137,7 +137,7 @@ class data_buf } /* Return the size of the buffer. */ - size_t size () const + virtual size_t size () const { return m_vec.size (); } @@ -1083,7 +1083,7 @@ write_gdbindex_1 (FILE *out_file, { data_buf contents; const offset_type size_of_header = 6 * sizeof (offset_type); - size_t total_len = size_of_header; + uint64_t total_len = size_of_header; /* The version number. */ contents.append_offset (8); @@ -1117,6 +1117,9 @@ write_gdbindex_1 (FILE *out_file, if (total_len > max_size) error (_("gdb-index maximum file size of %zu exceeded"), max_size); + if (out_file == nullptr) + return; + contents.file_write (out_file); cu_list.file_write (out_file); types_cu_list.file_write (out_file); @@ -1537,10 +1540,87 @@ save_gdb_index_command (const char *arg, int from_tty) } } +#if GDB_SELF_TEST +#include "gdbsupport/selftest.h" + +namespace selftests { + +class pretend_data_buf : public data_buf +{ +public: + /* Set the pretend size. */ + void set_pretend_size (size_t s) { + m_pretend_size = s; + } + + /* Override size method of data_buf, returning the pretend size instead. */ + size_t size () const override { + return m_pretend_size; + } + +private: + size_t m_pretend_size = 0; +}; + +static void +gdb_index () +{ + pretend_data_buf cu_list; + pretend_data_buf types_cu_list; + pretend_data_buf addr_vec; + pretend_data_buf symtab_vec; + pretend_data_buf constant_pool; + + const size_t size_of_header = 6 * sizeof (offset_type); + + /* Test that an overly large index will throw an error. */ + symtab_vec.set_pretend_size (~(offset_type)0 - size_of_header); + constant_pool.set_pretend_size (1); + + bool saw_exception = false; + try + { + write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec, + symtab_vec, constant_pool); + } + catch (const gdb_exception_error &e) + { + SELF_CHECK (e.reason == RETURN_ERROR); + SELF_CHECK (e.error == GENERIC_ERROR); + SELF_CHECK (e.message->find (_("gdb-index maximum file size of")) + != std::string::npos); + SELF_CHECK (e.message->find (_("exceeded")) != std::string::npos); + saw_exception = true; + } + SELF_CHECK (saw_exception); + + /* Test that the largest possible index will not throw an error. */ + constant_pool.set_pretend_size (0); + + saw_exception = false; + try + { + write_gdbindex_1 (nullptr, cu_list, types_cu_list, addr_vec, + symtab_vec, constant_pool); + } + catch (const gdb_exception_error &e) + { + saw_exception = true; + } + SELF_CHECK (!saw_exception); +} + +} /* selftests namespace. */ +#endif + void _initialize_dwarf_index_write (); void _initialize_dwarf_index_write () { +#if GDB_SELF_TEST + selftests::register_test ("gdb_index", selftests::gdb_index); +#endif + cmd_list_element *c = add_cmd ("gdb-index", class_files, save_gdb_index_command, _("\ Save a gdb-index file.\n\ base-commit: 95fc47d5c6b363b9b195baf1850b0ba95438ce69 -- 2.35.3