public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes.
@ 2015-08-21 23:28 waseemsarwar103 at yahoo dot com
  2015-08-22  8:46 ` [Bug c++/67315] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: waseemsarwar103 at yahoo dot com @ 2015-08-21 23:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67315

            Bug ID: 67315
           Summary: Strange 'this' pointer behavior when calling virtual
                    function with different optimization attributes.
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: waseemsarwar103 at yahoo dot com
  Target Milestone: ---

Created attachment 36238
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36238&action=edit
preprocessed file of the code in bug

The issue below only get produced on x86 (32 bit) system.

Compiler Version: gcc 4.9.0 (Target: i686-pc-linux-gnu)

System Type:  ubuntu 14.04.02
(Linux dev-virtual-machine 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15
17:45:15 UTC 2015 i686 i686 i686 GNU/Linux)

GCC Compilation Options: 
Configured with: ../gcc-4.9.0/configure --prefix=/home/was/gcc/gcc_install/
--with-gmp=/home/was/gcc/gcc_install/gcc_pre/
--with-mpfr=/home/was/gcc/gcc_install/gcc_pre/ --enable-shared
--with-system-zlib --enable-threads=posix
--with-mpc='/home/was/gcc/gcc_install/gcc_pre/1~'

Compilation command: 
g++- -ggdb -O3 -Wall -Werror -fstrict-aliasing -Wstrict-aliasing=2 -Wcast-align
-fPIC -fno-exceptions -fno-rtti -funsigned-char -DNOTLS -D__STDC_LIMIT_MACROS
-D__STDC_FORMAT_MACROS  -o incorrect_this.o test_incorrect_this.cpp

Comiler Output: Clean compilation. No error or warnings.

Description:
When following code is compiled with '-O3' but certain functions in the derived
class uses specific function attribute '(__attribute__((optimize("O0"))))' to
not apply global optimization to those functions, then calling the virtual
function from within those function causes 'this' pointer to be garbage and is
way off than the actual 'this' pointer.

If I compile the whole code with 'O0', the problem goes away. It seems like the
compiler is producing incompatible code when the function has optimization
level of 'O0' and calling a virtual function that was compiled with "O3". 

I have the following code that reproduces the problem with compiler version gcc
4.9.0, 4.9.2 and 4.9.3 on x86 systems. Observe 'this' pointer behavior in
virtual functions calls. 

Code: 
#include <stdio.h>

class ITest1
{
public:
        virtual void test11(void) = 0;
};

class ITest2
{
public:
        virtual void test21(void)
        {
                printf("Calling test21 %p\n", this);
        }
};

#define OPTIMIZE_SIZE __attribute__((optimize("O0")))

class Test : public ITest1, public ITest2
{
public:
        Test() : m_test(0)
        { }

        OPTIMIZE_SIZE void init()
        {
                m_test = 4;
                printf("Init %p \n", this);
                printf("m_test = %d\n", m_test);

                // All following functions have strange 'this' pointer value.
                test11();
                test21();
        }


        void test11(void)
        {
                printf("Calling test11 %p\n", this);
                //printf("m_test = %d \n", m_test);
        }

        void test12(void)
        {
                printf("Calling test12 %p\n", this);
        }


private:
        int m_test;
};

int main()
{
        Test *test = new Test();
        test->init();
}

Output:

root@dev-virtual-machine:/home/dev/framework/test_incorrect_this#
./incorrect_this.o
Init 0x96d9a10
m_test = 4
Calling test11 0xc
Calling test21 0xc

Observe that this pointer has changed to completely different value pointing to
garbage. 

Please help me understand the problem and potential fix to avoid this kind of
scenario. Thanks.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/67315] Strange 'this' pointer behavior when calling virtual function with different optimization attributes.
  2015-08-21 23:28 [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes waseemsarwar103 at yahoo dot com
@ 2015-08-22  8:46 ` redi at gcc dot gnu.org
  2015-08-22  8:57 ` [Bug c++/67315] [4.9 Regression] " redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2015-08-22  8:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67315

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|critical                    |normal


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/67315] [4.9 Regression] Strange 'this' pointer behavior when calling virtual function with different optimization attributes.
  2015-08-21 23:28 [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes waseemsarwar103 at yahoo dot com
  2015-08-22  8:46 ` [Bug c++/67315] " redi at gcc dot gnu.org
@ 2015-08-22  8:57 ` redi at gcc dot gnu.org
  2015-08-25  8:25 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2015-08-22  8:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67315

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-08-22
      Known to work|                            |4.8.4, 5.2.0, 6.0
            Summary|Strange 'this' pointer      |[4.9 Regression] Strange
                   |behavior when calling       |'this' pointer behavior
                   |virtual function with       |when calling virtual
                   |different optimization      |function with different
                   |attributes.                 |optimization attributes.
     Ever confirmed|0                           |1
      Known to fail|                            |4.9.3

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I can reproduce this with any 4.9.x release, but not with 5.x or trunk.

Doesn't happen for x86_64 only 32-bit. Only -O2 is needed.

$ ~/gcc/4.9.3/bin/g++ v.cc -Wall -O2 -m32
$ ./a.out
Init 0x8218a10 
m_test = 4
Calling test11 0xf76a94ec
Calling test21 0xf76a94ec
$ valgrind ./a.out
==12566== Memcheck, a memory error detector
==12566== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==12566== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==12566== Command: ./a.out
==12566== 
Init 0x41c7a58 
m_test = 4
==12566== Conditional jump or move depends on uninitialised value(s)
==12566==    at 0x465C6B0C: vfprintf (in /usr/lib/libc-2.21.so)
==12566==    by 0x465CF455: printf (in /usr/lib/libc-2.21.so)
==12566==    by 0x8048630: Test::test11() (in /tmp/a.out)
==12566==    by 0x8048696: Test::init() (in /tmp/a.out)
==12566==    by 0x80484F3: main (in /tmp/a.out)
==12566== 
Calling test11 (nil)
==12566== Conditional jump or move depends on uninitialised value(s)
==12566==    at 0x465C6B0C: vfprintf (in /usr/lib/libc-2.21.so)
==12566==    by 0x465CF455: printf (in /usr/lib/libc-2.21.so)
==12566==    by 0x8048610: ITest2::test21() (in /tmp/a.out)
==12566==    by 0x80486C6: Test::init() (in /tmp/a.out)
==12566==    by 0x80484F3: main (in /tmp/a.out)
==12566== 
Calling test21 (nil)
==12566== 
==12566== HEAP SUMMARY:
==12566==     in use at exit: 18,956 bytes in 2 blocks
==12566==   total heap usage: 2 allocs, 0 frees, 18,956 bytes allocated
==12566== 
==12566== LEAK SUMMARY:
==12566==    definitely lost: 12 bytes in 1 blocks
==12566==    indirectly lost: 0 bytes in 0 blocks
==12566==      possibly lost: 0 bytes in 0 blocks
==12566==    still reachable: 18,944 bytes in 1 blocks
==12566==         suppressed: 0 bytes in 0 blocks
==12566== Rerun with --leak-check=full to see details of leaked memory
==12566== 
==12566== For counts of detected and suppressed errors, rerun with: -v
==12566== Use --track-origins=yes to see where uninitialised values come from
==12566== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/67315] [4.9 Regression] Strange 'this' pointer behavior when calling virtual function with different optimization attributes.
  2015-08-21 23:28 [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes waseemsarwar103 at yahoo dot com
  2015-08-22  8:46 ` [Bug c++/67315] " redi at gcc dot gnu.org
  2015-08-22  8:57 ` [Bug c++/67315] [4.9 Regression] " redi at gcc dot gnu.org
@ 2015-08-25  8:25 ` rguenth at gcc dot gnu.org
  2015-08-25  8:26 ` rguenth at gcc dot gnu.org
  2015-08-25  8:58 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-08-25  8:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67315

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.9.4


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/67315] [4.9 Regression] Strange 'this' pointer behavior when calling virtual function with different optimization attributes.
  2015-08-21 23:28 [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes waseemsarwar103 at yahoo dot com
                   ` (2 preceding siblings ...)
  2015-08-25  8:25 ` rguenth at gcc dot gnu.org
@ 2015-08-25  8:26 ` rguenth at gcc dot gnu.org
  2015-08-25  8:58 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-08-25  8:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67315

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think this is a dup of the bug with the i386 backend issue of local calling
conventions and its interaction with optimization attributes ("optimize"
setting).
Somebody find it ... ;)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/67315] [4.9 Regression] Strange 'this' pointer behavior when calling virtual function with different optimization attributes.
  2015-08-21 23:28 [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes waseemsarwar103 at yahoo dot com
                   ` (3 preceding siblings ...)
  2015-08-25  8:26 ` rguenth at gcc dot gnu.org
@ 2015-08-25  8:58 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2015-08-25  8:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67315

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
PR 54068 ?


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-25  8:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-21 23:28 [Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes waseemsarwar103 at yahoo dot com
2015-08-22  8:46 ` [Bug c++/67315] " redi at gcc dot gnu.org
2015-08-22  8:57 ` [Bug c++/67315] [4.9 Regression] " redi at gcc dot gnu.org
2015-08-25  8:25 ` rguenth at gcc dot gnu.org
2015-08-25  8:26 ` rguenth at gcc dot gnu.org
2015-08-25  8:58 ` redi at gcc dot gnu.org

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).