From: Andrew Burgess Date: Wed, 25 Jul 2018 16:33:08 +0000 (+0100) Subject: gdb: Check element of optimised out vla exists X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5ff2bbae198cbd615885f26aa7d065b8cec8c278;p=binutils-gdb.git gdb: Check element of optimised out vla exists If a vla is not in memory, and the upper bound is not defined, then we can't know that an array element exists or not, and we should not try to access the array element. One case where this happens is for arrays that have been optimised out, the array will then have VALUE_LVAL of not_lval, and an undefined upper bound, if we then try to access an element of this array we will index into random GDB memory. An argument could be made that even for arrays that are in inferior memory, if the upper bound is not defined then we should not try to access the array element, however, in some of the Fortran tests, it seems as though we do rely indexing from a base address into an array which has no bounds defined. In this case GDBs standard protection for detecting unreadable target memory prevents bad thing happening. gdb/ChangeLog: * valarith.c (value_subscripted_rvalue): If an array is not in memory, and we don't know the upper bound, then we can't know that the requested element exists or not. gdb/testsuite/ChangeLog: * gdb.base/vla-optimized-out.exp: Add new test. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 4d0593f1630..ebe1747ff0f 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2018-08-09 Andrew Burgess + + * valarith.c (value_subscripted_rvalue): If an array is not in + memory, and we don't know the upper bound, then we can't know that + the requested element exists or not. + 2018-08-08 Simon Marchi * target.c (str_comma_list_concat_elem): Fix typo in comment. diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 975705da10a..bd3c3bfec5f 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2018-08-09 Andrew Burgess + + * gdb.base/vla-optimized-out.exp: Add new test. + 2018-08-09 Andrew Burgess * gdb.base/vla-optimized-out-o3.exp: Delete. diff --git a/gdb/testsuite/gdb.base/vla-optimized-out.exp b/gdb/testsuite/gdb.base/vla-optimized-out.exp index 298b689bfbf..88c6fac9b98 100644 --- a/gdb/testsuite/gdb.base/vla-optimized-out.exp +++ b/gdb/testsuite/gdb.base/vla-optimized-out.exp @@ -44,6 +44,28 @@ proc vla_optimized_out {exe_suffix options} { gdb_test "p sizeof (a)" \ " = $sizeof_result" \ "printed size of optimized out vla" + + # At lower optimisation levels, the upper bound of the array is + # still defined, it's just the loctaion that tells GDB the array + # is optimised out. In that case, when we access an element that + # is within the bounds of the array an answer of '' + # is reasonable. + # + # At higher optimisation levels, the array bounds themselves have + # been removed. As such GDB can't be expected to know if the + # array contains _any_ elements at all. It seems reasonable in + # that case to reply with 'no such vector element'. + gdb_test "p a\[0\]" \ + "(= |no such vector element)" \ + "print out of range element of vla (0)" + + gdb_test "p a\[6\]" \ + "no such vector element" \ + "print out of range element of vla (6)" + + gdb_test "p a\[0xffffffff\]" \ + "no such vector element" \ + "print out of range element of vla (0xffffffff)" } foreach {test_prefix options} \ diff --git a/gdb/valarith.c b/gdb/valarith.c index 01ca50d3d21..807cdd5dbd4 100644 --- a/gdb/valarith.c +++ b/gdb/valarith.c @@ -189,8 +189,11 @@ value_subscripted_rvalue (struct value *array, LONGEST index, int lowerbound) ULONGEST elt_size = type_length_units (elt_type); ULONGEST elt_offs = elt_size * (index - lowerbound); - if (index < lowerbound || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type) - && elt_offs >= type_length_units (array_type))) + if (index < lowerbound + || (!TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type) + && elt_offs >= type_length_units (array_type)) + || (VALUE_LVAL (array) != lval_memory + && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (array_type))) { if (type_not_associated (array_type)) error (_("no such vector element (vector not associated)"));