aboutsummaryrefslogtreecommitdiff
path: root/gdb/value.c
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@polymtl.ca>2021-11-08 16:06:07 -0500
committerSimon Marchi <simon.marchi@polymtl.ca>2021-12-03 16:37:36 -0500
commit4bce7cdaf481901edbc5ee47d953ea7e8efb56ca (patch)
treef085df5b718a9438b5d5174b5e3c9be15e2cea7c /gdb/value.c
parent7509b82979550970342a4494d727b3fb06bffd65 (diff)
downloadfsf-binutils-gdb-4bce7cdaf481901edbc5ee47d953ea7e8efb56ca.zip
fsf-binutils-gdb-4bce7cdaf481901edbc5ee47d953ea7e8efb56ca.tar.gz
fsf-binutils-gdb-4bce7cdaf481901edbc5ee47d953ea7e8efb56ca.tar.bz2
gdbsupport: add array_view copy function
An assertion was recently added to array_view::operator[] to ensure we don't do out of bounds accesses. However, when the array_view is copied to or from using memcpy, it bypasses that safety. To address this, add a `copy` free function that copies data from an array view to another, ensuring that the destination and source array views have the same size. When copying to or from parts of an array_view, we are expected to use gdb::array_view::slice, which does its own bounds check. With all that, any copy operation that goes out of bounds should be caught by an assertion at runtime. copy is implemented using std::copy and std::copy_backward, which, at least on libstdc++, appears to pick memmove when copying trivial data. So in the end there shouldn't be much difference vs using a bare memcpy, as we do right now. When copying non-trivial data, std::copy and std::copy_backward assigns each element in a loop. To properly support overlapping ranges, we must use std::copy or std::copy_backward, depending on whether the destination is before the source or vice-versa. std::copy and std::copy_backward don't support copying exactly overlapping ranges (where the source range is equal to the destination range). But in this case, no copy is needed anyway, so we do nothing. The order of parameters of the new copy function is based on std::copy and std::copy_backward, where the source comes before the destination. Change a few randomly selected spots to use the new function, to show how it can be used. Add a test for the new function, testing both with arrays of a trivial type (int) and of a non-trivial type (foo). Test non-overlapping ranges as well as three kinds of overlapping ranges: source before dest, dest before source, and dest == source. Change-Id: Ibeaca04e0028410fd44ce82f72e60058d6230a03
Diffstat (limited to 'gdb/value.c')
-rw-r--r--gdb/value.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/gdb/value.c b/gdb/value.c
index 7649b02..4e66329 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -1344,9 +1344,13 @@ value_contents_copy_raw (struct value *dst, LONGEST dst_offset,
TARGET_CHAR_BIT * length));
/* Copy the data. */
- memcpy (value_contents_all_raw (dst).data () + dst_offset * unit_size,
- value_contents_all_raw (src).data () + src_offset * unit_size,
- length * unit_size);
+ gdb::array_view<gdb_byte> dst_contents
+ = value_contents_all_raw (dst).slice (dst_offset * unit_size,
+ length * unit_size);
+ gdb::array_view<const gdb_byte> src_contents
+ = value_contents_all_raw (src).slice (src_offset * unit_size,
+ length * unit_size);
+ copy (src_contents, dst_contents);
/* Copy the meta-data, adjusted. */
src_bit_offset = src_offset * unit_size * HOST_CHAR_BIT;
@@ -1721,13 +1725,11 @@ value_copy (struct value *arg)
val->stack = arg->stack;
val->is_zero = arg->is_zero;
val->initialized = arg->initialized;
+
if (!value_lazy (val))
- {
- memcpy (value_contents_all_raw (val).data (),
- value_contents_all_raw (arg).data (),
- TYPE_LENGTH (value_enclosing_type (arg)));
+ copy (value_contents_all_raw (arg),
+ value_contents_all_raw (val));
- }
val->unavailable = arg->unavailable;
val->optimized_out = arg->optimized_out;
val->parent = arg->parent;
@@ -1772,9 +1774,7 @@ value_non_lval (struct value *arg)
struct type *enc_type = value_enclosing_type (arg);
struct value *val = allocate_value (enc_type);
- memcpy (value_contents_all_raw (val).data (),
- value_contents_all (arg).data (),
- TYPE_LENGTH (enc_type));
+ copy (value_contents_all (arg), value_contents_all_raw (val));
val->type = arg->type;
set_value_embedded_offset (val, value_embedded_offset (arg));
set_value_pointed_to_offset (val, value_pointed_to_offset (arg));