Bug 1408291

Summary: c++ optimization breaks exception handling
Product: [Fedora] Fedora Reporter: Merlin Mathesius <mmathesi>
Component: gtestAssignee: Terje Røsten <terje.rosten>
Status: CLOSED ERRATA QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 25CC: davejohansen, helio, jakub, jwakely, law, mpolacek, psabata, sgallagh, tagoh, terje.rosten, tstclair
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: gtest-1.7.0-8.fc25 gtest-1.7.0-8.fc24 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-02-09 04:20:59 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Merlin Mathesius 2016-12-22 19:29:56 UTC
Description of problem:
When attempting to rebuild the gtest-1.7.0 package in Fedora 25, we discovered the build was failing due to self-test failures. Further investigation revealed that the package build and self-tests would succeed if the default C++ optimization flag (-O2) was removed. It was found that executables compiled with C++  optimization would segfault instead of properly handling exceptions.

Version-Release number of selected component (if applicable):
gcc-c++-6.2.1-2.fc25.x86_64

How reproducible:
Every time

Steps to Reproduce:
1. sudo dnf install gcc-c++ cmake
2. mkdir workspace
3. cd workspace
4. wget https://github.com/google/googletest/archive/release-1.7.0/gtest-1.7.0.zip
5. unzip gtest-1.7.0.zip
6. GTEST_DIR=$(pwd)/googletest-release-1.7.0
7. mkdir build-unopt
8. pushd build-unopt
9. cmake -Dgtest_build_tests=ON ${GTEST_DIR}
10. VERBOSE=1 make CXX_FLAGS= gtest_catch_exceptions_ex_test_ gtest_catch_exceptions_no_ex_test_
11. ctest -R gtest_catch_exceptions_test
12. ./gtest_catch_exceptions_ex_test_
13. ./gtest_catch_exceptions_no_ex_test_
14. popd
15. mkdir build-optimized
16. pushd build-optimized
17. cmake -Dgtest_build_tests=ON ${GTEST_DIR}
18. VERBOSE=1 make CXX_FLAGS=-O gtest_catch_exceptions_ex_test_ gtest_catch_exceptions_no_ex_test_
19. ctest -R gtest_catch_exceptions_test
20. ./gtest_catch_exceptions_ex_test_
21. ./gtest_catch_exceptions_no_ex_test_

Actual results:

"ctest -R gtest_catch_exceptions_test" passes when executables are compiled without optimization, but fails when compiled with optimization.

When the compiled executables (./gtest_catch_exceptions_ex_test_ and ./gtest_catch_exceptions_no_ex_test_) are run directly from the command line, the UN-optimized versions run to completion with the gtest framework catching the exceptions. The optimized versions segfault--which is why the above "ctest ..." fails.

Expected results:

"ctest -R gtest_catch_exceptions_test" should always pass, regardless if the binaries are compiled with optimization or not.

The compiled executables should not segfault when run directly, regardless if the binaries are compiled with optimization or not.

Additional info:

Upstream googletest framework is aware of this problem:
    https://github.com/google/googletest/issues/845

This issue is causing gtest-1.7.0 to fail to build from source in Fedora:
    https://bugzilla.redhat.com/show_bug.cgi?id=1406937

Comment 1 Jonathan Wakely 2016-12-22 20:55:33 UTC
This is almost certainly a known bug in gtest, caused by calling member functions through a null pointer. See "Optimizations remove null pointer checks for this" at https://gcc.gnu.org/gcc-6/porting_to.html

Comment 2 Jonathan Wakely 2016-12-22 20:56:56 UTC
The stack trace in the github issue confirms it, it shows this=0x0 at the point of failure. This is not a gcc bug. The code has undefined behaviour.

Comment 3 Jonathan Wakely 2016-12-22 21:05:02 UTC
Using CXX_FLAGS="-O -fno-delete-null-pointer-checks" confirms it. Changing component to gtest, where the problem is.

Comment 4 Jonathan Wakely 2016-12-23 02:02:18 UTC
Patch here: https://github.com/google/googletest/pull/967.patch

Beware of bugs in the patch; I have only proved it correct, not tried it ;)

Comment 5 Fedora Update System 2017-01-23 18:54:56 UTC
gtest-1.7.0-8.fc25 has been submitted as an update to Fedora 25. https://bodhi.fedoraproject.org/updates/FEDORA-2017-0caadc8ba7

Comment 6 Fedora Update System 2017-01-23 18:55:09 UTC
gtest-1.7.0-8.fc24 has been submitted as an update to Fedora 24. https://bodhi.fedoraproject.org/updates/FEDORA-2017-cec378cd2f

Comment 7 Fedora Update System 2017-01-25 01:22:19 UTC
gtest-1.7.0-8.fc24 has been pushed to the Fedora 24 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2017-cec378cd2f

Comment 8 Fedora Update System 2017-01-28 19:19:03 UTC
gtest-1.7.0-8.fc25 has been pushed to the Fedora 25 testing repository. If problems still persist, please make note of it in this bug report.
See https://fedoraproject.org/wiki/QA:Updates_Testing for
instructions on how to install test updates.
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2017-0caadc8ba7

Comment 9 Fedora Update System 2017-02-09 04:20:59 UTC
gtest-1.7.0-8.fc25 has been pushed to the Fedora 25 stable repository. If problems still persist, please make note of it in this bug report.

Comment 10 Fedora Update System 2017-02-09 20:49:44 UTC
gtest-1.7.0-8.fc24 has been pushed to the Fedora 24 stable repository. If problems still persist, please make note of it in this bug report.