https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99613
Bug ID: 99613
Summary: Static variable destruction order race condition
Product: gcc
Version: 7.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: michalz at nvidia dot com
Target Milestone: ---
Created attachment 50394
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50394&action=edit
Race condition demo
Function static variables should be destroyed in the reverse order of
initialization. However, the lock is released before calling atexit to register
the destructor, allowing for a race condition to occur if multiple threads are
accessing static variables for the first time.
The bug affects all versions of GCC that I've tried (6.4, 7.3, 8.2, 9.3, 10.2)
on multiple platforms.
The error in output assembly can be seen when compiling the following code:
struct S{
S();
~S();
};
S& foo(){
static S s;
return s;
}
The attached file contains a working example that triggers the race condition
(it's a race condition, so sometimes it succeeds).