gdb: change functions returning value contents to use gdb::array_view

The bug fixed by this [1] patch was caused by an out-of-bounds access to
a value's content.  The code gets the value's content (just a pointer)
and then indexes it with a non-sensical index.

This made me think of changing functions that return value contents to
return array_views instead of a plain pointer.  This has the advantage
that when GDB is built with _GLIBCXX_DEBUG, accesses to the array_view
are checked, making bugs more apparent / easier to find.

This patch changes the return types of these functions, and updates
callers to call .data() on the result, meaning it's not changing
anything in practice.  Additional work will be needed (which can be done
little by little) to make callers propagate the use of array_view and
reap the benefits.

[1] https://sourceware.org/pipermail/gdb-patches/2021-September/182306.html

Change-Id: I5151f888f169e1c36abe2cbc57620110673816f3
This commit is contained in:
Simon Marchi 2021-10-04 20:47:06 -04:00
parent d9f82e9313
commit 50888e42dc
96 changed files with 453 additions and 417 deletions

View file

@ -360,7 +360,7 @@ extern void error_value_optimized_out (void);
get to the real subobject, if the value happens to represent
something embedded in a larger run-time object. */
extern gdb_byte *value_contents_raw (struct value *);
extern gdb::array_view<gdb_byte> value_contents_raw (struct value *);
/* Actual contents of the value. For use of this value; setting it
uses the stuff above. Not valid if lazy is nonzero. Target
@ -368,24 +368,24 @@ extern gdb_byte *value_contents_raw (struct value *);
value. Note that a value therefore extends beyond what is
declared here. */
extern const gdb_byte *value_contents (struct value *);
extern gdb_byte *value_contents_writeable (struct value *);
extern gdb::array_view<const gdb_byte> value_contents (struct value *);
extern gdb::array_view<gdb_byte> value_contents_writeable (struct value *);
/* The ALL variants of the above two macros do not adjust the returned
pointer by the embedded_offset value. */
extern gdb_byte *value_contents_all_raw (struct value *);
extern const gdb_byte *value_contents_all (struct value *);
extern gdb::array_view<gdb_byte> value_contents_all_raw (struct value *);
extern gdb::array_view<const gdb_byte> value_contents_all (struct value *);
/* Like value_contents_all, but does not require that the returned
bits be valid. This should only be used in situations where you
plan to check the validity manually. */
extern const gdb_byte *value_contents_for_printing (struct value *value);
extern gdb::array_view<const gdb_byte> value_contents_for_printing (struct value *value);
/* Like value_contents_for_printing, but accepts a constant value
pointer. Unlike value_contents_for_printing however, the pointed
value must _not_ be lazy. */
extern const gdb_byte *
extern gdb::array_view<const gdb_byte>
value_contents_for_printing_const (const struct value *value);
extern void value_fetch_lazy (struct value *val);