Bug #70483 Server gets segmentation fault if compiled -O2
Submitted: 2 Oct 2013 6:35 Modified: 11 Feb 2014 15:26
Reporter: Duncan Roe Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:5.6.14 OS:Linux (Slackware 14 + kernel 3.10.4)
Assigned to: Paul DuBois CPU Architecture:Any
Tags: -O2, segfault, segmentation

[2 Oct 2013 6:35] Duncan Roe
Description:
Building from source, if have -O2 in CFLAGS & CXXFLAGS then server crashes almost straight away. If compile without -O2 then it runs normally. Also get this problem with 5.7.2-m12. 5.5.27 is OK. It may be relevant CPU has 2 cores, although gdb says there is only 1 thread at time of failure.

How to repeat:
I compiled 5.6.14 with gcc4.7.1 and 5.7.2-m12 with gcc-4.8.1, so anything in-between should be fine. Build line was:

CFLAGS='-O2 -march=i486 -mtune=i686 -g3 -ggdb' \
CXXFLAGS='-O2 -march=i486 -mtune=i686 -g3 -ggdb' CXX=g++ \
cmake -DBUILD_CONFIG=mysql_release -DWITH_DEFAULT_FEATURE_SET=ON \
-DINSTALL_LAYOUT=RPM -DCMAKE_INSTALL_PREFIX=/usr -DINSTALL_LIBDIR=lib \
-DINSTALL_SBINDIR=libexec -DINSTALL_INCLUDEDIR=include/mysql \
-DINSTALL_MYSQLSHAREDIR=share/mysql \
-DINSTALL_SQLBENCHDIR= -DINSTALL_MYSQLTESTDIR=mysql-test -DINSTALL_INFODIR=info \
-DINSTALL_MANDIR=man -DINSTALL_PLUGINDIR=lib/mysql/plugin \
-DINSTALL_SCRIPTDIR=bin \
..

where head of source tree was "..".

Suggested fix:
For now, build w/out -O2.
Have no idea why it should fail where it does (will be attaching gdb sessions)
[2 Oct 2013 6:52] MySQL Verification Team
Hi,  please let us know details of a crash.  Do you have stack trace in error log or core-file ?  How do you crash server?  Does mysql-test testsuite run successfully?
[2 Oct 2013 6:52] Duncan Roe
Debug on server failure

Attachment: mysql5614_with_-O2 (application/octet-stream, text), 9.86 KiB.

[2 Oct 2013 6:55] Duncan Roe
Debug session when server runs normally

Attachment: mysql5614_no_opt (application/octet-stream, text), 11.67 KiB.

[2 Oct 2013 7:03] Duncan Roe
server crashes on ./rc.mysqld start

Attachment: rc.mysqld (application/octet-stream, text), 2.52 KiB.

[2 Oct 2013 7:12] Duncan Roe
In regard to the 2 gdb sessions, I suspect the pfs pointer is wrong. At 5.7.2 it is zero (NULL) so definitely wrong. You can diff the 2 sessions to compare contents of structure addressed by pfs
[2 Oct 2013 7:21] Duncan Roe
script used to build failing server

Attachment: actual_build_script (application/octet-stream, text), 6.57 KiB.

[2 Oct 2013 7:25] Duncan Roe
The build script expects tar.gz or whatever to be in same dirctory as itself. It will bomb out at end because no makepkg or slack_desc but you will have a built tree in /tmp or you can run make install again in the source tree (also in /tmp)
[3 Oct 2013 1:33] Duncan Roe
Output from running test suite

Attachment: test_suite_output (application/octet-stream, text), 20.55 KiB.

[3 Oct 2013 1:35] Duncan Roe
Test suite fails on first test that is neither skipped nor disabled. It cannot start the server
[11 Oct 2013 22:24] Sveta Smirnova
Thank you for the report.

It is not repeatable with -O2 flag and 64-bit binaries for me. Need to re-check on i386 machine.
[14 Oct 2013 1:08] Duncan Roe
Yes this was a 32-bit build. Sorry I did not mention that earlier
[2 Dec 2013 18:35] Sveta Smirnova
Thank you for the feedback.

Finally test failure verified on 64-bit machine using cmake options:

CFLAGS="-m32 -O2" CXXFLAGS="-m32 -O2" cmake . -DCMAKE_INSTALL_PREFIX=/home/sveta/build/mysql-5.6 -DENABLE_DTRACE=0
[2 Dec 2013 21:30] Sveta Smirnova
Backtrace:

/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld[0x84a2eee]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld(_Z21find_or_create_digestP10PFS_threadP18PSI_digest_storagePKcj+0x95)[0x8505c85]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld[0x853662a]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld(_Z16dispatch_command19enum_server_commandP3THDPcj+0x3b0)[0x82d3990]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld(_Z10do_commandP3THD+0xd1)[0x82d5861]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld(_Z24do_handle_one_connectionP3THD+0x1b4)[0x82a6ec4]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld(handle_one_connection+0x3c)[0x82a6f1c]
/home/ssmirnov/pan10/mysql-5.6.14/sql/mysqld(pfs_spawn_thread+0x193)[0x8534563]
/lib32/libpthread.so.0(+0x6d25)[0xf76d9d25]
/lib32/libc.so.6(clone+0x5e)[0xf74ebdfe]
[2 Dec 2013 21:31] Sveta Smirnova
Sorry, pasted cmake from wrong machine:

CFLAGS="-L/usr/lib32 -m32 -O2  -march=i486 -mtune=i686 -g3 -ggdb" CXXFLAGS="-m32 -O2 -L/usr/lib32  -march=i486 -mtune=i686 -g3 -ggdb" cmake . -DCMAKE_INSTALL_PREFIX=$HOME/pan10/build/mysql-5.6.14  -DCURSES_LIBRARY=/usr/lib32/libncurses.so -DCURSES_INCLUDE_PATH=/usr/include
[12 Dec 2013 14:27] Tor Didriksen
Posted by developer:
 
when configuring like this
CFLAGS= .... CXXFLAGS=...
you override some essential flags from cmake/build_configurations/compiler_options.cmake

if you configure like this:
cmake -DCMAKE_C_FLAGS="-m32 -mtune=i686 -march=i486" -DCMAKE_CXX_FLAGS="-m32 -mtune=i686 -march=i486"

you will build with these flags:
-m32 -mtune=i686 -march=i486 -Wall -Wno-unused-parameter -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing 
the two last ones are both essential.

As you can see, flags on the command line are prepended to the builtins,
so you will get -O3.
[12 Dec 2013 17:48] Sveta Smirnova
Tor,

do we have any documentation about specifying flags for cmake and, especially, about the fact that overriding cmake/build_configurations/compiler_options.cmake is forbidden?
[13 Dec 2013 10:07] Tor Didriksen
http://dev.mysql.com/doc/mysql-sourcebuild-excerpt/5.5/en/installing-source-distribution.h...
[13 Dec 2013 10:10] Tor Didriksen
The builtin defaults are carefully selected/tuned for each platform
that we support.
[11 Feb 2014 15:26] Paul DuBois
Thank you for your bug report. This issue has been addressed in the documentation. The updated documentation will appear on our website shortly, and will be included in the next release of the relevant products.

To specify C/C++ compiler flags when building using CMake, use CMAKE_C_FLAGS and CMAKE_CXX_FLAGS rather than CFLAGS/CXXFLAGS. I've updated the documentation to reflect that. For example:

http://dev.mysql.com/doc/mysql-sourcebuild-excerpt/5.6/en/source-configuration-options.htm...
http://dev.mysql.com/doc/mysql-sourcebuild-excerpt/5.6/en/compilation-problems.html

Also:
http://dev.mysql.com/doc/internals/en/controlling-compiler-flags.html