public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "sivachandra at gmail dot com" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug c++/17132] New: Improper evaluation of expressions involving virtual method invocations under EVAL_AVOID_SIDE_EFFECTS
Date: Wed, 09 Jul 2014 00:33:00 -0000	[thread overview]
Message-ID: <bug-17132-4717@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=17132

            Bug ID: 17132
           Summary: Improper evaluation of expressions involving virtual
                    method invocations under EVAL_AVOID_SIDE_EFFECTS
           Product: gdb
           Version: HEAD
            Status: NEW
          Severity: normal
          Priority: P2
         Component: c++
          Assignee: unassigned at sourceware dot org
          Reporter: sivachandra at gmail dot com

When GDB "evaluates" expressions under EVAL_AVOID_SIDE_EFFECTS it incorrectly
emits "Cannot access memory at address 0x0" in some cases. Examples:

/* Example 1  */

struct base
{
  virtual void func();
};

void base::func ()
{
}

base *b = 0;

Then in GDB:

(gdb) ptype b->func()
Cannot access memory at address 0x0

(gdb) p sizeof(b->func())
Cannot access memory at address 0x0

/* End example 1  */

/* Example 2  */

#include <memory>

class A
{
public:
 virtual int type (void);
};

int
A::type ()
{
 return 3;
}

int
main ()
{
 std::unique_ptr<A> a (new A);

 // Invoke A::type so that unique_ptr::operator-> gets generated.
 return a->type();
}

Within GDB, I see this:

(gdb) p a->type()
$1 = 3
(gdb) p 1 && a->type()
Cannot access memory at address 0x0
(gdb) p 1 + a->type()
$2 = 4
(gdb) p 1 || a->type()
Cannot access memory at address 0x0

It does not happen if A::type is not virtual.

/* End example 2  */

I traced this and what I find is that, since the expression involves invoking a
virtual method, find_overload_match tries to pick the implementation of this
method for the most derived type. And for doing that, it tries to read the
vtable for the object via its vptr. Since the object is NULL in example 1, it
trips.

Example 2 is slightly complicated. For logical operations && and ||, GDB
evaluates the first operand, and only evaluates the type of the second operand
as a first step. The reason being, if evaluating the logical operation does not
require invoking an overloaded operator, then the boolean value of the first
operand could determine the result of the operation even without requiring to
evaluate the second operand.

The observed error message is coming when GDB is trying to evaluate the type of
a->type(). At a high level, there are two operations in this expression: '->',
and invocation of method 'type'. Since GDB only wants the type of the
expression, it evaluates it under EVAL_AVOID_SIDE_EFFECTS. The intermediate
results are zeroed values of the correct type. But now, since 'type' is a
virtual function, GDB internally looks up the vtable of the zeroed intermediate
object to pick the implementation for the most derived type. This is where it
trips with "Cannot access memory at address 0x0".

-- 
You are receiving this mail because:
You are on the CC list for the bug.


             reply	other threads:[~2014-07-09  0:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-09  0:33 sivachandra at gmail dot com [this message]
2014-07-09  0:35 ` [Bug c++/17132] " sivachandra at gmail dot com
2014-08-16  1:33 ` cvs-commit at gcc dot gnu.org
2023-12-26 18:24 ` ssbssa at sourceware dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-17132-4717@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).