[PATCH] Throw error when creating an overly large gdb-index file

Tom de Vries tdevries@suse.de
Fri Sep 15 09:09:46 GMT 2023


On 9/15/23 02:22, Kevin Buettner wrote:
> On Sat, 9 Sep 2023 10:20:19 +0200
> Tom de Vries <tdevries@suse.de> 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 <tdevries@suse.de>
> 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 <kevinb@redhat.com>
> 
> 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\
> 



More information about the Gdb-patches mailing list