binutils-gdb/gdb/testsuite/gdb.ada
Joel Brobecker 53ba833325 [Ada] Crash when trying to set value of packed array element
Consider the following declaration:

   type Small is new Integer range 0 .. 2 ** 4 - 1;
   type Simple_Array is array (1 .. 4) of Small;
   pragma Pack (Simple_Array);

   SA : Simple_Array := (1, 2, 3, 4);

Trying to change the value of one of the elements in the packed array
causes the debugger to crash:

    (gdb) set sa(3) := 9
    [1]    4880 segmentation fault  gdb -q foo

The circumstances leading to the crash are as follow:

   . ada_evaluate_subexp creates a value corresponding to "sa(3)".

   . ada_evaluate_subexp then tries to assign 9 to this value, and
     for this calls value_assign (via ada_value_assign).

   . Because the array is packed, the destination value is 3 bits long,
     and as a result, value_assign uses the parent to determine that
     element byte address and offset:

      | if (value_bitsize (toval))
      |   {
      |     struct value *parent = value_parent (toval);
      |
      |     changed_addr = value_address (parent) + value_offset (toval);

The destination value (corresponding to "sa(3)") was incorrectly created
by ada-lang.c:ada_value_primitive_packed_val, because the "parent" was
left as NULL. So, when we try to dereference it to get the parent address,
GDB crashed.

The first part of the fix therefore consists in setting that field.
This required the addition of a new "setter" in value.[hc].  It fixes
the crash, but is still not sufficient for the assignment to actually
work.

The second part of the problem came from the fact that value_assign
seems to expect the "child"'s address to be equal to the parent's address,
with the difference being the offset. Unfortunately, this requirement was
not followed by ada_value_primitive_packed_val, so the second part of
the fix consisted in fixing that.

Still, this was not sufficient, because it caused a regression when
trying to perform an aggregate assignment of a packed array of packed
record.  The key element here is the nesting of packed entities.
Looking at the way ada_value_primitive_packed_val creates the value
of each sub-component, one can see that the value's offset is set
to the offset compared to the start of the parent. This was meant to
match what value_primitive_field does as well.

So, with our array of records, if the record offset was 2, and if
the field we're interested in that record is at offset 1, the record
value's offset would be set to 2, and the field value's offset would
be set to 1. But the address for both values would be left to the
array's address. This is where things start breaking down, because
the value_address function for our field value would return the
address of the array + 1, instead of + 3.

This is what causes the final issue, here, because ada-lang.c's
value_assign_to_component needs to compute the offset of the
subcomponent compared to the top-level aggregate's start address
(the array in our case). And it does so by subtracting the array's
address from the sub-component's address.  When you have two levels
of packed components, and the mid-level component is at an offset of
the top-level component, things didn't work, because the component's
address was miscomputed (the parent's offset is missing).

The fix consists is fixing value_address to match the work done by
value_primitive_field (where we ignore the parent's offset).

gdb/ChangeLog:

        * value.h (set_value_parent): Add declaration.
        * value.c (set_value_parent): New function.
        (value_address): If VALUE->PARENT is not NULL, then use it as
        the base address instead of VALUE->LOCATION.address.
        * ada-lang.c (ada_value_primitive_packed_val): Keep V's address
        the same as OBJ's address.  Adjust V's offset accordingly.
        Set V's parent.

gdb/testsuite/ChangeLog:

        * gdb.ada/set_pckd_arr_elt: New testcase.
2012-03-16 17:55:45 +00:00
..
aliased_array [Ada] Handle reference to array descriptors 2012-02-29 19:33:02 +00:00
array_bounds Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
array_return Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
array_subscript_addr Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
arrayidx Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
arrayparam Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
arrayptr Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
atomic_enum Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
bp_enum_homonym New Ada testcase (bp_enum_homonym). 2012-03-06 17:04:59 +00:00
bp_on_var New Ada testcase (bp_on_var.exp). 2012-03-06 17:33:32 +00:00
bp_range_type testcase for "gdb-ax.c: Add handling of TYPE_CODE_RANGE types" 2012-03-14 01:38:51 +00:00
call_pn Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
catch_ex Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
char_enum Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
char_param Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
complete Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
cond_lang Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
dyn_loc Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
enum_idx_packed [Ada] print packed arrays indexed by enumerated type 2012-02-29 19:34:40 +00:00
exec_changed Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
exprs Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
fixed_cmp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
fixed_points Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
formatted_ref Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
frame_args Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
fullname_bp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
fun_addr Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
fun_in_declare Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
funcall_param Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
homonym Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
info_locals_renaming Testcase: "info locals" with Ada renamings. 2012-03-02 19:31:53 +00:00
int_deref Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
interface Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
lang_switch Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
mi_catch_ex Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
mi_task_arg GDB/MI: crash printing "_task" (Ada) argument 2012-02-03 07:32:40 +00:00
mi_task_info Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
mod_from_name Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
nested Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
null_array Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
null_record Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
operator_bp New Ada testcase for breakpoints on operators. 2012-03-02 20:36:41 +00:00
packed_array Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
packed_tagged Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
print_chars Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
ptr_typedef Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
ptype_field Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
ptype_tagged_param Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
rec_return Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
ref_param Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
ref_tick_size Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
same_enum Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
set_pckd_arr_elt [Ada] Crash when trying to set value of packed array element 2012-03-16 17:55:45 +00:00
small_reg_param Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
start Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
str_ref_cmp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
sym_print_name Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
taft_type Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
tagged Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
tagged_not_init [Ada] avoid error message pollution with uninitialized tagged variable 2012-02-29 19:46:48 +00:00
task_bp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
tasks Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
tick_last_segv Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
type_coercion Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
uninitialized_vars Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
variant_record_packed_array Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
watch_arg Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
whatis_array_val [Ada] whatis not printing array type name for value from history 2012-02-29 19:29:12 +00:00
widewide Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
Makefile.in 2011-02-22 Michael Snyder <msnyder@vmware.com> 2011-02-22 20:52:49 +00:00
aliased_array.exp [Ada] Handle reference to array descriptors 2012-02-29 19:33:02 +00:00
array_bounds.exp * gdb.ada/array_bounds.exp: Get breakpoint for line 2012-03-08 22:19:48 +00:00
array_return.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
array_subscript_addr.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
arrayidx.exp gdb/testsuite/ 2012-03-03 18:03:31 +00:00
arrayparam.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
arrayptr.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
assign_1.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
atomic_enum.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
boolean_expr.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
bp_enum_homonym.exp New Ada testcase (bp_enum_homonym). 2012-03-06 17:04:59 +00:00
bp_on_var.exp New Ada testcase (bp_on_var.exp). 2012-03-06 17:33:32 +00:00
bp_range_type.exp testcase for "gdb-ax.c: Add handling of TYPE_CODE_RANGE types" 2012-03-14 01:38:51 +00:00
call_pn.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
catch_ex.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
char_enum.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
char_param.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
complete.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
cond_lang.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
dyn_loc.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
enum_idx_packed.exp [Ada] print packed arrays indexed by enumerated type 2012-02-29 19:34:40 +00:00
exec_changed.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
exprs.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
fixed_cmp.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
fixed_points.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
formatted_ref.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
frame_args.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
fullname_bp.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
fun_addr.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
fun_in_declare.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
funcall_param.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
gnat_ada.gpr Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
homonym.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
info_locals_renaming.exp Testcase: "info locals" with Ada renamings. 2012-03-02 19:31:53 +00:00
info_types.c Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
info_types.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
int_deref.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
interface.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
lang_switch.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
mi_catch_ex.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
mi_task_arg.exp * gdb.ada/operator_bp.exp: Clear debug-file-directory. 2012-03-05 21:21:13 +00:00
mi_task_info.exp 2012-01-18 Pedro Alves <palves@redhat.com> 2012-01-18 17:00:17 +00:00
mod_from_name.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
nested.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
null_array.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
null_record.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
operator_bp.exp * gdb.ada/operator_bp.exp: Clear debug-file-directory. 2012-03-05 21:21:13 +00:00
packed_array.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
packed_tagged.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
print_chars.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
print_pc.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
ptr_typedef.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
ptype_arith_binop.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
ptype_field.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
ptype_tagged_param.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
rec_return.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
ref_param.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
ref_tick_size.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
same_enum.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
set_pckd_arr_elt.exp [Ada] Crash when trying to set value of packed array element 2012-03-16 17:55:45 +00:00
small_reg_param.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
start.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
str_ref_cmp.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
sym_print_name.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
taft_type.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
tagged.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
tagged_not_init.exp [Ada] avoid error message pollution with uninitialized tagged variable 2012-02-29 19:46:48 +00:00
task_bp.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00
tasks.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
tick_last_segv.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
type_coercion.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
uninitialized_vars.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
variant_record_packed_array.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
watch_arg.exp 2012-01-16 Pedro Alves <palves@redhat.com> 2012-01-16 16:21:53 +00:00
whatis_array_val.exp [Ada] whatis not printing array type name for value from history 2012-02-29 19:29:12 +00:00
widewide.exp Copyright year update in most files of the GDB Project. 2012-01-04 08:17:56 +00:00