Bug 62258 - [4.9/5 Regression] uncaught_exception() equals to `true' after rethrow_exception()
Summary: [4.9/5 Regression] uncaught_exception() equals to `true' after rethrow_except...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.9.1
: P2 normal
Target Milestone: 4.9.4
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
: 61643 (view as bug list)
Depends on:
Blocks:
 
Reported: 2014-08-25 18:06 UTC by Dmitry Prokoptsev
Modified: 2016-09-22 14:55 UTC (History)
9 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.8.0, 4.8.2, 6.0
Known to fail: 4.8.3, 4.8.5, 4.9.2, 5.1.0
Last reconfirmed: 2015-02-02 00:00:00


Attachments
A simple test case. (159 bytes, text/x-c++src)
2014-08-25 18:06 UTC, Dmitry Prokoptsev
Details
Proposed patch (313 bytes, patch)
2014-08-25 18:11 UTC, Dmitry Prokoptsev
Details | Diff
Workaround code (1.84 KB, text/plain)
2015-02-06 10:04 UTC, Mateusz Kwiatkowski
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Prokoptsev 2014-08-25 18:06:12 UTC
After using std::rethrow_exception() (and catching the exception), all subsequent calls to std::uncaught_exception() return `true', which is obviously not the way it was meant to work.

A simple test case is attached.
Comment 1 Dmitry Prokoptsev 2014-08-25 18:06:50 UTC
Created attachment 33394 [details]
A simple test case.
Comment 2 Dmitry Prokoptsev 2014-08-25 18:11:32 UTC
Created attachment 33395 [details]
Proposed patch

I believe the problem is that std::rethrow_exception() does not update `__cxa_eh_globals::uncaughtExceptions' (which remains zero), while __cxa_begin_catch() decrements it to -1.

A proposed patch is attached.
Comment 3 Deniz Bahadir 2014-08-29 10:27:28 UTC
I can confirm this bug.
It even occurs with GCC 4.8.2 (on Ubuntu 14.04).
Comment 4 Ai Azuma 2014-10-07 08:57:06 UTC
PR61643 shares the root cause with this PR. The behavior of the test case in PR61643 completely agrees with the analysis in Comment 2, and the proposed patch makes the test case in PR61643 work properly.

I confirmed that this bug was reproduced with 4.8.4 20140925, 4.9.2 20140924 and 5.0.0 20140928.
Comment 5 Ai Azuma 2014-10-07 09:02:11 UTC
*** Bug 61643 has been marked as a duplicate of this bug. ***
Comment 6 Piotr Bartosiewicz 2015-01-20 10:12:24 UTC
I can also confirm this bug (I have a test case with std::async and future, but it internally uses std::rethrow_exception).

Known to work: gcc 4.6.3, 4.7.2, 4.8.1
Known to fail: gcc 4.8.2, 4.9.2, clang 3.5.0
So this is a regression.
Comment 7 M. Hanselmann 2015-01-26 16:29:44 UTC
Confirmed using GCC 5.0.0 20150125 built from source (see below) and 4.7.2 (Debian 4.7.2-5; Debian Wheezy). Tested using the test case in comment 1.

Output:
$ g++-5-20150125 -std=c++11 -o test62258 test62258.c -Wall && ./test62258
test62258: test62258.c:11: int main(): Assertion `!std::uncaught_exception()' failed.
Aborted

$ g++-5-20150125 -v
Using built-in specs.
COLLECT_GCC=g++-5-20150125
COLLECT_LTO_WRAPPER=/home/user/gcc50/bin/../libexec/gcc/x86_64-unknown-linux-gnu/5.0.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /home/user/gcc50build/src-gcc-5-20150125/configure --target=x86_64-unknown-linux-gnu --prefix=/home/user/gcc50build/target-x86_64-unknown-linux-gnu --program-suffix=-5-20150125 --disable-nls --enable-c99 --enable-checking=release --enable-gnu-unique-object --enable-gold --enable-languages=c,c++ --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-linker-build-id --enable-long-long --enable-multiarch --enable-multilib --enable-shared --enable-targets=all --enable-threads=posix --with-system-zlib --with-tune=generic
Thread model: posix
gcc version 5.0.0 20150125 (experimental) (GCC)
Comment 8 Mateusz Kwiatkowski 2015-02-06 10:04:11 UTC
Created attachment 34683 [details]
Workaround code

For all programmers out there that may encounter this bug, I attach a workaround code.

Linking your program with it should make rethrow_exception() usable even if the library on the system is buggy.
Comment 9 M. Hanselmann 2015-02-06 19:50:39 UTC
A few days ago I submitted a patch for this issue based on Dmitry's patch. It seems as if we can get it in after the 5.0 release.

https://gcc.gnu.org/ml/gcc-patches/2015-02/msg00027.html
Comment 10 Jonathan Wakely 2015-04-07 14:59:27 UTC
doko, this fails for me with 4.8.1, and 4.7.4 and 4.8.0

There are no changes between 4.8.1 and 4.8.2 that could have affected this.

Are you sure it's a regression?
Comment 11 Jonathan Wakely 2015-04-27 19:45:02 UTC
Author: redi
Date: Mon Apr 27 19:44:30 2015
New Revision: 222480

URL: https://gcc.gnu.org/viewcvs?rev=222480&root=gcc&view=rev
Log:
2015-04-27  Dmitry Prokoptsev  <dprokoptsev@gmail.com>
	    Michael Hanselmann  <public@hansmi.ch>

	PR libstdc++/62258
	* libsupc++/eh_ptr.cc (rethrow_exception): Increment count of
	uncaught exceptions.
	* testsuite/18_support/exception_ptr/62258.cc: New.

Added:
    trunk/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/libsupc++/eh_ptr.cc
Comment 12 Jonathan Wakely 2015-04-27 21:35:36 UTC
Fixed on trunk so far
Comment 13 Jonathan Wakely 2015-04-27 21:36:16 UTC
doko, I'm still curious if you can verify if you see a regression here or not.
Comment 14 Richard Biener 2015-06-23 08:19:45 UTC
The gcc-4_8-branch is being closed, re-targeting regressions to 4.9.3.
Comment 15 Jakub Jelinek 2015-06-26 19:55:52 UTC
GCC 4.9.3 has been released.
Comment 16 Richard Biener 2015-06-30 10:16:04 UTC
I can confirm it works with 4.8.0 to 4.8.2 but fails since 4.8.3 (just verified using -static-libstdc++ to not fool myself with the installed runtime).
Comment 17 Deniz Bahadir 2015-07-17 09:06:51 UTC
Did this fix make it into the latest GCC 5.2.0 release?
Comment 18 Richard Biener 2015-07-21 10:25:24 UTC
No.
Comment 19 Jonathan Wakely 2015-09-03 15:10:57 UTC
Author: redi
Date: Thu Sep  3 15:10:25 2015
New Revision: 227451

URL: https://gcc.gnu.org/viewcvs?rev=227451&root=gcc&view=rev
Log:
Backport fix for PR libstdc++/62258 from mainline

2015-04-27  Dmitry Prokoptsev  <dprokoptsev@gmail.com>
	    Michael Hanselmann  <public@hansmi.ch>

	PR libstdc++/62258
	* libsupc++/eh_ptr.cc (rethrow_exception): Increment count of
	uncaught exceptions.
	* testsuite/18_support/exception_ptr/62258.cc: New.

Added:
    branches/gcc-5-branch/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc
Modified:
    branches/gcc-5-branch/libstdc++-v3/ChangeLog
    branches/gcc-5-branch/libstdc++-v3/libsupc++/eh_ptr.cc
Comment 20 Jonathan Wakely 2015-09-03 16:02:39 UTC
Author: redi
Date: Thu Sep  3 16:02:07 2015
New Revision: 227456

URL: https://gcc.gnu.org/viewcvs?rev=227456&root=gcc&view=rev
Log:
Backport fix for PR libstdc++/62258 from mainline

2015-04-27  Dmitry Prokoptsev  <dprokoptsev@gmail.com>
	    Michael Hanselmann  <public@hansmi.ch>

	PR libstdc++/62258
	* libsupc++/eh_ptr.cc (rethrow_exception): Increment count of
	uncaught exceptions.
	* testsuite/18_support/exception_ptr/62258.cc: New.

Added:
    branches/gcc-4_9-branch/libstdc++-v3/testsuite/18_support/exception_ptr/62258.cc
Modified:
    branches/gcc-4_9-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_9-branch/libstdc++-v3/libsupc++/eh_ptr.cc
Comment 21 Jonathan Wakely 2015-09-03 16:06:01 UTC
Fixed in 4.9.4, 5.3 and 6.0
Comment 22 Jonathan Wakely 2016-09-22 14:55:30 UTC
For the record, the 4.8.3 regression was introduced by r207131 and the 4.7.4 regression by r208991, with the fixes for PR 41174 and PR 59224.

The fix should apply cleanly and be safe for the 4.7 and 4.8 branches (although they are closed upstream of course).