This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
fix bogus virtual base pointer adjustment
- From: Pedro Alves <pedro at codesourcery dot com>
- To: gdb-patches at sourceware dot org
- Date: Wed, 2 Feb 2011 18:07:32 +0000
- Subject: fix bogus virtual base pointer adjustment
This code is obviously bogus for:
1) assuming endianness/layout of the value contents buffer
2) assuming vbase offset is always < 256
3) directly changing a value in-place in a value-print
routine (thus corrupting values in the value history).
Before:
print virtual_middle_b
$7 = (Virtual *) 0x603640
(gdb) PASS: gdb.cp/virtbase.exp: print virtual_middle_b
print $
$8 = (VirtualMiddleB *) 0x603640
(gdb) FAIL: gdb.cp/virtbase.exp: print $
print *$$
$9 = {<error reading variable>
(gdb) FAIL: gdb.cp/virtbase.exp: print *$$
After:
print virtual_middle_b
$7 = (Virtual *) 0x603140
(gdb) PASS: gdb.cp/virtbase.exp: print virtual_middle_b
print $
$8 = (Virtual *) 0x603140
(gdb) PASS: gdb.cp/virtbase.exp: print $
print *$$
$9 = (Virtual) {<VirtualMiddleA> = {<VirtualBase> = {_vptr.VirtualBase = 0x401380, x = 0}, _vptr.VirtualMiddleA = 0x401358, y = {0 <repeats 300 times>}}, <VirtualMiddleB> = {_vptr.VirtualMiddleB = 0x4013b0, y = 0}, _vptr.Virtual = 0x401328, z = 0}
Tested on x86_64-linux and applied. New test included.
--
Pedro Alves
2011-02-02 Pedro Alves <pedro@codesourcery.com>
gdb/
* c-valprint.c (c_value_print): When doing virtual base pointer
adjustment, create a new value with adjusted contents rather than
changing the contents of the value being printed (and getting it
wrong).
gdb/testsuite/
* gdb.cp/virtbase.cc (VirtualBase, VirtualMiddleA, VirtualMiddleB)
(Virtual): New structs.
(virtual_o, virtual_middle_b): New globals.
* gdb.cp/virtbase.exp: New tests.
---
gdb/c-valprint.c | 3 +--
gdb/testsuite/gdb.cp/virtbase.cc | 31 +++++++++++++++++++++++++++++++
gdb/testsuite/gdb.cp/virtbase.exp | 15 +++++++++++++++
3 files changed, 47 insertions(+), 2 deletions(-)
Index: src/gdb/testsuite/gdb.cp/virtbase.cc
===================================================================
--- src.orig/gdb/testsuite/gdb.cp/virtbase.cc 2011-02-02 12:29:02.657898995 +0000
+++ src/gdb/testsuite/gdb.cp/virtbase.cc 2011-02-02 16:32:15.007899002 +0000
@@ -88,6 +88,37 @@ public:
RTTI_data() : data(1) {}
};
+/* These classes are for testing pointer adjustment when printing a
+ pointer into a virtual base, with print object on. */
+struct VirtualBase {
+ int x;
+
+ virtual ~VirtualBase() {}
+};
+
+struct VirtualMiddleA : public virtual VirtualBase {
+ /* Make sure the vbase offset of Virtual::VirtualBaseB is larger
+ than what fits in one byte. */
+ int y[300];
+
+ virtual ~VirtualMiddleA() {}
+};
+
+struct VirtualMiddleB : public virtual VirtualBase {
+ int y;
+
+ virtual ~VirtualMiddleB() {}
+};
+
+struct Virtual : public virtual VirtualMiddleA, public virtual VirtualMiddleB {
+ int z;
+
+ virtual ~Virtual() {}
+};
+
+Virtual virtual_o;
+VirtualMiddleB *virtual_middle_b = &virtual_o;
+
int main() {
ph::Derived tst;
tst.get_y();
Index: src/gdb/testsuite/gdb.cp/virtbase.exp
===================================================================
--- src.orig/gdb/testsuite/gdb.cp/virtbase.exp 2011-02-02 12:29:02.657898995 +0000
+++ src/gdb/testsuite/gdb.cp/virtbase.exp 2011-02-02 16:32:15.007899002 +0000
@@ -65,3 +65,18 @@ gdb_test "print/x b->mA" " = 0xaaaaaaaa"
# https://bugzilla.redhat.com/show_bug.cgi?id=606660
# `set print object on' is expected.
gdb_test "print rtti_data" " = .*, data = 1\}"
+
+# Printing a pointer into a virtual base of a larger object used to do
+# pointer adjusment directly into the value being printed, in-place
+# (and did it wrong, too). Print the pointer, and then access the
+# value history to check the pointer value is not changed. If it had
+# been changed, then we'd not be able to find the real type anymore.
+gdb_test "print virtual_middle_b" \
+ " = \\(Virtual \\*\\) $hex" \
+ "print pointer to virtual base at non-zero offset of larger object"
+gdb_test "print $" \
+ " = \\(Virtual \\*\\) $hex" \
+ "print same pointer from history value"
+gdb_test "print *$$" \
+ " = \\(Virtual\\) {<VirtualMiddleA> = {<VirtualBase> = {_vptr.VirtualBase = $hex, x = 0}, _vptr.VirtualMiddleA = $hex, y = \\{0 <repeats 300 times>\\}}, <VirtualMiddleB> = {_vptr.VirtualMiddleB = $hex, y = 0}, _vptr.Virtual = $hex, z = 0}" \
+ "print whole pointed-to object, starting from the virtual base pointer"
Index: src/gdb/c-valprint.c
===================================================================
--- src.orig/gdb/c-valprint.c 2011-02-02 12:29:02.897899000 +0000
+++ src/gdb/c-valprint.c 2011-02-02 16:32:15.007899003 +0000
@@ -706,8 +706,7 @@ c_value_print (struct value *val, struct
type = lookup_reference_type (real_type);
}
/* JYG: Need to adjust pointer value. */
- /* NOTE: cagney/2005-01-02: THIS IS BOGUS. */
- value_contents_writeable (val)[0] -= top;
+ val = value_from_pointer (type, value_as_address (val) - top);
/* Note: When we look up RTTI entries, we don't get any
information on const or volatile attributes. */