This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/67315] New: Strange 'this' pointer behavior when calling virtual function with different optimization attributes.


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]