Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit e5ca03b

Browse files
committed
Fix PR gdb/20295: GDB segfaults printing bitfield member of optimized out value
With something like: struct A { int bitfield:4; } var; If 'var' ends up wholly-optimized out, printing 'var.bitfield' crashes gdb here: (top-gdb) bt #0 0x000000000058b89f in extract_unsigned_integer (addr=0x2 <error: Cannot access memory at address 0x2>, len=2, byte_order=BFD_ENDIAN_LITTLE) at /home/pedro/gdb/mygit/src/gdb/findvar.c:109 #1 0x00000000005a187a in unpack_bits_as_long (field_type=0x16cff70, valaddr=0x0, bitpos=16, bitsize=12) at /home/pedro/gdb/mygit/src/gdb/value.c:3347 #2 0x00000000005a1b9d in unpack_value_bitfield (dest_val=0x1b5d9d0, bitpos=16, bitsize=12, valaddr=0x0, embedded_offset=0, val=0x1b5d8d0) at /home/pedro/gdb/mygit/src/gdb/value.c:3441 #3 0x00000000005a2a5f in value_fetch_lazy (val=0x1b5d9d0) at /home/pedro/gdb/mygit/src/gdb/value.c:3958 #4 0x00000000005a10a7 in value_primitive_field (arg1=0x1b5d8d0, offset=0, fieldno=0, arg_type=0x16d04c0) at /home/pedro/gdb/mygit/src/gdb/value.c:3161 #5 0x00000000005b01e5 in do_search_struct_field (name=0x1727c60 "bitfield", arg1=0x1b5d8d0, offset=0, type=0x16d04c0, looking_for_baseclass=0, result_ptr=0x7fffffffcaf8, [...] unpack_value_bitfield is already optimized-out/unavailable -aware: (...) VALADDR points to the contents of VAL. If the VAL's contents required to extract the bitfield from are unavailable/optimized out, DEST_VAL is correspondingly marked unavailable/optimized out. however, it is not considering the case of the value having no contents buffer at all, as can happen through allocate_optimized_out_value. gdb/ChangeLog: 2016-08-09 Pedro Alves <[email protected]> * value.c (unpack_value_bitfield): Skip unpacking if the parent has no contents buffer to begin with. gdb/testsuite/ChangeLog: 2016-08-09 Pedro Alves <[email protected]> * gdb.dwarf2/bitfield-parent-optimized-out.exp: New file.
1 parent 02183cb commit e5ca03b

File tree

4 files changed

+111
-8
lines changed

4 files changed

+111
-8
lines changed

gdb/ChangeLog

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2016-08-09 Pedro Alves <[email protected]>
2+
3+
* value.c (unpack_value_bitfield): Skip unpacking if the parent
4+
has no contents buffer to begin with.
5+
16
2016-08-08 Pedro Alves <[email protected]>
27

38
* features/i386/amd64-avx-mpx-linux.c: Regenerate.

gdb/testsuite/ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2016-08-09 Pedro Alves <[email protected]>
2+
3+
* gdb.dwarf2/bitfield-parent-optimized-out.exp: New file.
4+
15
2016-08-03 Tom Tromey <[email protected]>
26

37
PR python/18565:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Copyright 2016 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# Test printing a bitfield of an optimized-out variable. Regression
17+
# test for PR gdb/20295.
18+
19+
load_lib dwarf.exp
20+
21+
# This test can only be run on targets which support DWARF-2 and use gas.
22+
if {![dwarf2_support]} {
23+
return 0
24+
}
25+
26+
standard_testfile main.c .S
27+
set executable ${testfile}
28+
29+
# Make some DWARF for the test.
30+
set asm_file [standard_output_file $srcfile2]
31+
Dwarf::assemble $asm_file {
32+
global srcdir subdir srcfile
33+
34+
cu { version 3 addr_size 4 } {
35+
compile_unit {} {
36+
declare_labels struct_label var_label int_label
37+
38+
int_label: base_type {
39+
{byte_size 4 sdata}
40+
{encoding @DW_ATE_signed}
41+
{name int}
42+
}
43+
44+
struct_label: structure_type {
45+
{name S}
46+
{byte_size 4 DW_FORM_sdata}
47+
} {
48+
member {
49+
{name bitfield}
50+
{type :$int_label}
51+
{bit_size 12 DW_FORM_sdata}
52+
{bit_offset 20 DW_FORM_sdata}
53+
{data_member_location 2 DW_FORM_sdata}
54+
}
55+
member {
56+
{name intfield}
57+
{type :$int_label}
58+
{data_member_location 2 DW_FORM_sdata}
59+
}
60+
}
61+
62+
subprogram {
63+
{MACRO_AT_func { main ${srcdir}/${subdir}/${srcfile} }}
64+
{type :$int_label}
65+
{external 1 flag}
66+
} {
67+
var_label: DW_TAG_variable {
68+
{name var}
69+
{location {} DW_FORM_block1}
70+
{type :$struct_label}
71+
}
72+
}
73+
}
74+
}
75+
}
76+
77+
if [prepare_for_testing ${testfile}.exp $executable "${asm_file} ${srcfile}" {}] {
78+
return -1
79+
}
80+
81+
if ![runto_main] {
82+
return -1
83+
}
84+
85+
gdb_test "p var" " = <optimized out>"
86+
gdb_test "p var.bitfield" " = <optimized out>"
87+
gdb_test "p var.intfield" " = <optimized out>"

gdb/value.c

+15-8
Original file line numberDiff line numberDiff line change
@@ -3430,17 +3430,24 @@ unpack_value_bitfield (struct value *dest_val,
34303430
enum bfd_endian byte_order;
34313431
int src_bit_offset;
34323432
int dst_bit_offset;
3433-
LONGEST num;
34343433
struct type *field_type = value_type (dest_val);
34353434

3436-
/* First, unpack and sign extend the bitfield as if it was wholly
3437-
available. Invalid/unavailable bits are read as zero, but that's
3438-
OK, as they'll end up marked below. */
34393435
byte_order = gdbarch_byte_order (get_type_arch (field_type));
3440-
num = unpack_bits_as_long (field_type, valaddr + embedded_offset,
3441-
bitpos, bitsize);
3442-
store_signed_integer (value_contents_raw (dest_val),
3443-
TYPE_LENGTH (field_type), byte_order, num);
3436+
3437+
/* First, unpack and sign extend the bitfield as if it was wholly
3438+
valid. Optimized out/unavailable bits are read as zero, but
3439+
that's OK, as they'll end up marked below. If the VAL is
3440+
wholly-invalid we may have skipped allocating its contents,
3441+
though. See allocate_optimized_out_value. */
3442+
if (valaddr != NULL)
3443+
{
3444+
LONGEST num;
3445+
3446+
num = unpack_bits_as_long (field_type, valaddr + embedded_offset,
3447+
bitpos, bitsize);
3448+
store_signed_integer (value_contents_raw (dest_val),
3449+
TYPE_LENGTH (field_type), byte_order, num);
3450+
}
34443451

34453452
/* Now copy the optimized out / unavailability ranges to the right
34463453
bits. */

0 commit comments

Comments
 (0)