From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id 703DF3858C52 for ; Fri, 15 Sep 2023 09:09:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 703DF3858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de 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 A0D381F899; Fri, 15 Sep 2023 09:09:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1694768990; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9q9dpxXVmZ/k28ZUcCwg9gPcTlS+tvw0yu/bJe18Ib8=; b=mJv0k5QXfqCQ0DvOSiqpYKCZeAHONVJRbKk2p3d4yCphGIa7X/w9oAU91YAHy1zyYerzCQ z7nJzpKJ+sYmdGnvormDxdrhmf+z3cBajhzz/PYQHphEYIBFG2JWpP+iJu3Sj9o1pxTmMf y8eFI967Z+JuzmKrSWSUF1irDnsoQLw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1694768990; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9q9dpxXVmZ/k28ZUcCwg9gPcTlS+tvw0yu/bJe18Ib8=; b=fOTBu6WZZcS3cYl9c9mVIYZntZ266U5NWvcVSNY109xTZon6V0UYascL/VAiLh/YRG7wr+ YJwubo4mEHCeM5Dg== 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 8C0411358A; Fri, 15 Sep 2023 09:09:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id TIX/IF4fBGXjVwAAMHmgww (envelope-from ); Fri, 15 Sep 2023 09:09:50 +0000 Message-ID: <5f11fbbf-5b28-46fe-ab0c-154d41cb6bbd@suse.de> Date: Fri, 15 Sep 2023 11:09:46 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Throw error when creating an overly large gdb-index file Content-Language: en-US To: Kevin Buettner Cc: gdb-patches@sourceware.org References: <20230909025521.3128935-2-kevinb@redhat.com> <20230914172224.68a2d70c@f37-zws-nv> From: Tom de Vries In-Reply-To: <20230914172224.68a2d70c@f37-zws-nv> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 List-Id: On 9/15/23 02:22, Kevin Buettner wrote: > On Sat, 9 Sep 2023 10:20:19 +0200 > Tom de Vries wrote: > >> On 9/9/23 04:55, Kevin Buettner via Gdb-patches wrote: >>> I wish I could provide a test case, but due to the sizes of both the >>> input and output files, I think that testing resources would be strained >>> or exceeded in many environments. >> >> How about this unit test approach? This fails on master, and could be >> updated to catch the error thrown by the patch. > > I like it. I made several tweaks resulting in the version below. > Does it look okay to you? > I see that the virtual / override bits were indeed necessary, thanks for catching that. The m_pretend_size was missing zero-initialization, so I've added that. Also I've added a test that checks that using the maximum size does not generate an error. I've also build on a 32-bit system, and noticed that the unit test fails because the error doesn't trigger. I've proposed a fix that includes the unit test here ( https://sourceware.org/pipermail/gdb-patches/2023-September/202493.html ). Thanks, - Tom > From 1999dc14bf8c498c3f0db83eeb8f7e6d7376e8ff Mon Sep 17 00:00:00 2001 > From: Tom de Vries > Date: Sat, 9 Sep 2023 10:15:01 +0200 > Subject: [PATCH] Add unit test for overly large gdb-index file > > This commit adds a 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'. > > Co-Authored-By: Kevin Buettner > > diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c > index 3827a810130..fd9605700c8 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 (); > } > @@ -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,70 @@ 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; > +}; > + > +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; > + > + /* Test that an overly large index will throw an error. */ > + symtab_vec.set_pretend_size (~(offset_type)0); > + 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); > +} > + > +} /* 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\ >