Bug 32281 - A problem of gcc4.1.0(O3 optimize)
Summary: A problem of gcc4.1.0(O3 optimize)
Status: RESOLVED DUPLICATE of bug 21920
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2007-06-11 03:38 UTC by still
Modified: 2007-06-11 12:23 UTC (History)
12 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description still 2007-06-11 03:38:34 UTC
When I user gcc4.1.0 to compile mysql4.1.22, I find some errors. I¡¯m not sure whether it¡¯s a gcc bug or not, so I need your help.

       The version of gcc:

       gcc -v

Using built-in specs.

Target: i586-suse-linux

Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,objc,fortran,java,ada --enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.1.0 --enable-ssp --disable-libssp --enable-java-awt=gtk --enable-gtk-cairo --disable-libjava-multilib --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit --enable-libstdcxx-allocator=new --without-system-libunwind --with-cpu=generic --host=i586-suse-linux

Thread model: posix

gcc version 4.1.0 (SUSE Linux)

Linux verson:

Linux #1 SMP Sat Jan 13 19:17:08 CST 2007 i686 i686 i386 GNU/Linux

Mysql4.1.22 is from http://mysql.he.net/Downloads/MySQL-4.1/mysql-4.1.22.tar.gz .


The error is in function mysql_stmt_execute(THD *thd, char *packet, uint packet_length)( mysql-4.1.22/sql/sql_prepare.cc:1786).

The file is complied by these arguments:

g++ -DMYSQL_SERVER -DDEFAULT_MYSQL_HOME="\"/data/home/c4b/still/bin/mysql/\"" -DDATADIR="\"/data/home/c4b/still/bin/mysql//var\"" -DSHAREDIR="\"/data/home/c4b/still/bin/mysql//share/mysql\"" -DHAVE_CONFIG_H -I. -I. -I.. -I../innobase/include -I../include -I../include -I../regex -I.     -O3 -DDBUG_OFF    -fno-implicit-templates -fno-exceptions -fno-rtti -MT sql_prepare.o -MD -MP -MF ".deps/sql_prepare.Tpo" -g -c -o sql_prepare.o sql_prepare.cc


In line 1822-1824

1822    if (setup_conversion_functions(stmt, (uchar **) &packet, packet_end) ||

   1823         stmt->set_params(stmt, null_array, (uchar *) packet, packet_end,

   1824                          &expanded_query))

And the function ¡°setup_conversion_functions¡± is compiled as inline function.

The lase sentence of in function setup_conversion_functions is *data= read_pos;


The three sentences is compiled to 

0x08197bff <_Z18mysql_stmt_executeP3THDPcj+703>:         mov    0xc(%ebp),%ecx

0x08197c02 <_Z18mysql_stmt_executeP3THDPcj+706>:        mov    0xffffffc0(%ebp),%ebx

0x08197c05 <_Z18mysql_stmt_executeP3THDPcj+709>:        mov    0xffffffb4(%ebp),%eax

0x08197c08 <_Z18mysql_stmt_executeP3THDPcj+712>:        mov    %ecx,0x8(%esp)

0x08197c0c <_Z18mysql_stmt_executeP3THDPcj+716>:        mov    0xffffffd0(%ebp),%edx

0x08197c0f <_Z18mysql_stmt_executeP3THDPcj+719>:        mov    0xffffffb8(%ebp),%ecx

0x08197c12 <_Z18mysql_stmt_executeP3THDPcj+722>:        mov    %ebx,0xc(%ebp)      //*data= read_pos

0x08197c15 <_Z18mysql_stmt_executeP3THDPcj+725>:        lea    0xffffffdc(%ebp),%ebx

0x08197c18 <_Z18mysql_stmt_executeP3THDPcj+728>:        mov    %ebx,0x10(%esp)

0x08197c1c <_Z18mysql_stmt_executeP3THDPcj+732>:        mov    %eax,0xc(%esp)

0x08197c20 <_Z18mysql_stmt_executeP3THDPcj+736>:        mov    %edx,0x4(%esp)

0x08197c24 <_Z18mysql_stmt_executeP3THDPcj+740>:        mov    %ecx,(%esp)

0x08197c27 <_Z18mysql_stmt_executeP3THDPcj+743>:        call   *0x764(%ecx)


0xc(%ebp) is the address of &packet(in function mysql_stmt_execute) and also the address of *data(in function setup_conversion_functions).

In 703 and 712, we can see the value of 0xc(%ebp) is push to stack for the third argument of function stmt->set_params.

The sentence 722 is for *data= read_pos, Move the read_pos to *data(address 0xc(%ebp)).


So the third argument of function stmt->set_params use the old value not the new value. 



Am I right£¿Wait for your reply, and thank you very much.



Best wishes,

Comment 1 Andrew Pinski 2007-06-11 03:41:47 UTC
So packet is char*, and you are accessing it as uchar*, so this code is violating C/C++ aliasing rules.

*** This bug has been marked as a duplicate of 21920 ***
Comment 2 still 2007-06-11 06:07:35 UTC
Thank you.

But if i compiled it without -O3, it work fine.
If I compiled it under gcc3.3 with -O3, it also work fine.

The same program with different optimize has different, so i think it should not be like this.
Comment 3 still 2007-06-11 07:11:48 UTC
I think if "&packet" should be changed, it should change before the function ¡°stmt->set_params¡±. And in fact it is changed after "stmt->set_params¡±, so I think it's a logical error.

Best wishes,
Comment 4 Richard Biener 2007-06-11 12:23:41 UTC
It is still violating aliasing rules.  That only -O3 fails is because -O3 enables
-finline-functions, so it should as well fail with lower optimization settings with -finline-functions.  Also -fno-strict-aliasing will fix it.

*** This bug has been marked as a duplicate of 21920 ***