From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18739 invoked by alias); 30 Sep 2014 21:49:06 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 18727 invoked by uid 89); 30 Sep 2014 21:49:05 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-HELO: mail-la0-f48.google.com Received: from mail-la0-f48.google.com (HELO mail-la0-f48.google.com) (209.85.215.48) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Tue, 30 Sep 2014 21:49:03 +0000 Received: by mail-la0-f48.google.com with SMTP id q1so9554261lam.35 for ; Tue, 30 Sep 2014 14:48:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=SJrbOfsyPmy4xDZwHdAdUU5p6peScmEiTje8P3hf12c=; b=DPx75sgRHKmGILCoMSlDinqORsD0WL9jTPmR5W1tEJHB8wZ4OCgaA7bt70WaRIF84K ZzI9Mr1ETHivjegGaM2WffQxjXZoeKgZHQ1hINp9Z1uCXgLiiHn8dwmCTYEtQtA0fsO3 zOBh7+j+FbG8KKefkiALoyZfshbjTqIq5axi34r4CIofRk+KQ9J2H+rSUlkSW3rr5/tK Na9QvqoJRdQAQo0i/PmvmeRjjSXGV7xExeVH9HY0z35vz2QG/Rua8m6eulyj/pY+AZdf cyrf0YD76p9sQILueQ07zTBZZinMY0axpqD5tT5cohXFnsymRIavZXbv4HAK55ggF5+a C5HQ== X-Gm-Message-State: ALoCoQmeywLYeDwZI/S7uMpNrSs1virrB/4hyEqtn1ZKj6/gIJgiZVsUdm3qBAcwqn6u297EW4If MIME-Version: 1.0 X-Received: by 10.112.170.165 with SMTP id an5mr38841961lbc.6.1412113739869; Tue, 30 Sep 2014 14:48:59 -0700 (PDT) Received: by 10.25.30.79 with HTTP; Tue, 30 Sep 2014 14:48:59 -0700 (PDT) In-Reply-To: <1411355243-10812-1-git-send-email-patrick@parcs.ath.cx> References: <1411355243-10812-1-git-send-email-patrick@parcs.ath.cx> Date: Tue, 30 Sep 2014 21:49:00 -0000 Message-ID: Subject: Re: [PATCH 1/2] Fix C++ virtual method pointer resolution From: Patrick Palka To: gdb-patches@sourceware.org Cc: Patrick Palka Content-Type: text/plain; charset=UTF-8 X-IsSubscribed: yes X-SW-Source: 2014-09/txt/msg00911.txt.bz2 On Sun, Sep 21, 2014 at 11:07 PM, Patrick Palka wrote: > C++ virtual method pointer resolution is currently broken, in that a > virtual method pointer will sometimes resolve to the wrong symbol. A > minimal testcase is the following program: > > struct x > { > virtual void f (); > virtual void g (); > } > > void x::f () { } > void x::g () { } > > (gdb) print &x::f > $1 = &virtual x::f() > (gdb) print &x::g > $1 = &virtual x::f() > > As you can see, &x::f correctly resolves to x::f(), but &x::g > incorrectly resolves to x::f() instead of x::g(). > > The issue lies in the initial creation of the virtual method pointer as > seen by GDB. In gnuv3_make_method_ptr() we fail to shift the vtable > offset when storing the first word of a virtual method pointer. This is > important because functions that read this first word (namely the > callers of gnuv3_decode_method_ptr()) expect that the vtable offset is a > multiple of "sizeof (vtable_ptrdiff_t)". Also it ensures that the vbit > tag does not collide with the bits used to store the actual offset. > > So when writing the virtual method pointer contents we need to shift the > vtable offset so as to be in symmetry with what the readers of the > vtable offset do -- which is, xor the vbit tag and then shift back the > offset. (The prominent readers of the vtable offset are > gnuv3_print_method_ptr() and gnuv3_method_ptr_to_value().) > > gdb/ChangeLog > * gnu-v3-abi.c (gnuv3_make_method_ptr): Shift the vtable offset > before setting the virtual bit. > > gdb/testsuite/ChangeLog > * gdb.cp/method-ptr.exp: New test. > * gdb.cp/method-ptr.cc: New testcase. > > Tested on x86_64-unknown-linux-gnu. > --- > gdb/gnu-v3-abi.c | 7 ++++- > gdb/testsuite/gdb.cp/method-ptr.cc | 58 +++++++++++++++++++++++++++++++++++ > gdb/testsuite/gdb.cp/method-ptr.exp | 60 +++++++++++++++++++++++++++++++++++++ > 3 files changed, 124 insertions(+), 1 deletion(-) > create mode 100644 gdb/testsuite/gdb.cp/method-ptr.cc > create mode 100644 gdb/testsuite/gdb.cp/method-ptr.exp > > diff --git a/gdb/gnu-v3-abi.c b/gdb/gnu-v3-abi.c > index d5ed355..ccb0be6 100644 > --- a/gdb/gnu-v3-abi.c > +++ b/gdb/gnu-v3-abi.c > @@ -683,7 +683,12 @@ gnuv3_make_method_ptr (struct type *type, gdb_byte *contents, > > if (!gdbarch_vbit_in_delta (gdbarch)) > { > - store_unsigned_integer (contents, size, byte_order, value | is_virtual); > + if (is_virtual != 0) > + { > + value = value * TYPE_LENGTH (vtable_ptrdiff_type (gdbarch)); > + value = value | 1; > + } > + store_unsigned_integer (contents, size, byte_order, value); > store_unsigned_integer (contents + size, size, byte_order, 0); > } > else > diff --git a/gdb/testsuite/gdb.cp/method-ptr.cc b/gdb/testsuite/gdb.cp/method-ptr.cc > new file mode 100644 > index 0000000..db47484 > --- /dev/null > +++ b/gdb/testsuite/gdb.cp/method-ptr.cc > @@ -0,0 +1,58 @@ > +/* This testcase is part of GDB, the GNU debugger. > + > + Copyright 2014 Free Software Foundation, Inc. > + > + This program is free software; you can redistribute it and/or modify > + it under the terms of the GNU General Public License as published by > + the Free Software Foundation; either version 3 of the License, or > + (at your option) any later version. > + > + This program is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + GNU General Public License for more details. > + > + You should have received a copy of the GNU General Public License > + along with this program. If not, see . */ > + > +struct x > +{ > + virtual void f (); > + virtual void g (); > + virtual void h (); > +}; > + > +void x::f () { } > +void x::g () { } > +void x::h () { } > + > +struct y : x > +{ > + virtual void f (); > + > + virtual void j (); > + virtual void k (); > +}; > + > +void y::f () { } > +void y::j () { } > +void y::k () { } > + > +struct z : y > +{ > + virtual void g (); > + virtual void j (); > + > + virtual void l (); > + virtual void m (); > +}; > + > +void z::g () { } > +void z::j () { } > +void z::l () { } > +void z::m () { } > + > +int > +main () > +{ > +} > diff --git a/gdb/testsuite/gdb.cp/method-ptr.exp b/gdb/testsuite/gdb.cp/method-ptr.exp > new file mode 100644 > index 0000000..732b861 > --- /dev/null > +++ b/gdb/testsuite/gdb.cp/method-ptr.exp > @@ -0,0 +1,60 @@ > +# Copyright 2014 Free Software Foundation, Inc. > + > +# This program is free software; you can redistribute it and/or modify > +# it under the terms of the GNU General Public License as published by > +# the Free Software Foundation; either version 3 of the License, or > +# (at your option) any later version. > +# > +# This program is distributed in the hope that it will be useful, > +# but WITHOUT ANY WARRANTY; without even the implied warranty of > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +# GNU General Public License for more details. > +# > +# You should have received a copy of the GNU General Public License > +# along with this program. If not, see . > + > +# This file is part of the gdb testsuite > + > +if [skip_cplus_tests] { continue } > + > +standard_testfile .cc > + > +if [prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}] { > + return -1 > +} > + > +if ![runto_main] { > + return -1 > +} > + > +get_debug_format > + > +if ![test_debug_format "DWARF 2"] { > + return 0 > +} > + > +# Check that the virtual method pointer NAME resolves to symbol SYMBOL. > +proc check_virtual_method_ptr_resolution { name symbol } { > + global decimal > + > + # Printing the expression &NAME should show the resolved symbol SYMBOL. > + gdb_test "print &$name" "\\$$decimal = &virtual $symbol\\(\\)\\s" > +} > + > +check_virtual_method_ptr_resolution "x::f" "x::f" > +check_virtual_method_ptr_resolution "x::g" "x::g" > +check_virtual_method_ptr_resolution "x::h" "x::h" > + > +check_virtual_method_ptr_resolution "y::f" "y::f" > +check_virtual_method_ptr_resolution "y::g" "x::g" > +check_virtual_method_ptr_resolution "y::h" "x::h" > +check_virtual_method_ptr_resolution "y::j" "y::j" > +check_virtual_method_ptr_resolution "y::k" "y::k" > + > +check_virtual_method_ptr_resolution "z::f" "y::f" > +check_virtual_method_ptr_resolution "z::g" "z::g" > +check_virtual_method_ptr_resolution "z::h" "x::h" > +check_virtual_method_ptr_resolution "z::j" "z::j" > +check_virtual_method_ptr_resolution "z::k" "y::k" > +check_virtual_method_ptr_resolution "z::l" "z::l" > +check_virtual_method_ptr_resolution "z::m" "z::m" > -- > 2.1.1.273.g97b8860 > Ping.