From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2535 invoked by alias); 9 Jul 2014 00:33:16 -0000 Mailing-List: contact gdb-prs-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-prs-owner@sourceware.org Received: (qmail 2509 invoked by uid 48); 9 Jul 2014 00:33:16 -0000 From: "sivachandra at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gdb X-Bugzilla-Component: c++ X-Bugzilla-Version: HEAD X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: sivachandra at gmail dot com X-Bugzilla-Status: NEW X-Bugzilla-Priority: P2 X-Bugzilla-Assigned-To: unassigned at sourceware dot org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://sourceware.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2014-q3/txt/msg00027.txt.bz2 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 class A { public: virtual int type (void); }; int A::type () { return 3; } int main () { std::unique_ptr 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.