Clang/LLVM 7.0.1 on Windows initializes inline static data member multiple times
| Bugzilla Link | 37903 |
| Resolution | FIXED |
| Resolved on | Oct 21, 2019 10:24 |
| Version | 8.0 |
| OS | Windows NT |
| CC | @zmodem,@jyu2-git,@MMory,@rnk,@tstellar |
Extended Description
Clang/LLVM 6.0.0 on Windows initializes an inline static data member once per TU. As far as I understand C++17 this is not correct.
The idea behind initialization of inline variables is that the compiler and/or linker ensure(s) such a variable be initialized exactly once in a program although it may be defined in multiple TUs. Just like with inline functions. See the following little program:
// header.h
#include
struct A { A() { std::cout << "ctor "; } ~A() { std::cout << "dtor "; } void f() { std::cout << "bla "; } };
struct S { inline static A a; // C++17 inline variable, thus also a definition };
// TU1.cpp
#include "header.h"
int main() { S::a.f(); }
// TU2.cpp include "header.h"
// TU3.cpp include "header.h"
// TU4.cpp include "header.h"
This program prints "ctor ctor ctor ctor bla dtor dtor dtor dtor ". That's four objects of A (in fact one per TU) instead of exactly one (as C++17 demands).
It should print "ctor bla dtor ". This is what MSVC does, by the way.
Please also see the discussion on stackoverflow: https://stackoverflow.com/questions/50982390/clang-llvm-6-0-0-on-windows-initializes-inline-static-data-member-multiple-times
mentioned in issue llvm/llvm-bugzilla-archive#41409
mentioned in issue llvm/llvm-bugzilla-archive#38171
mentioned in issue llvm/llvm-bugzilla-archive#38171
The 8.0.1 release was already put out in July, and there is no plan for a 8.0.1 release. Our longstanding release policy has been to do six month major releases followed by a single point release. At this point, it's time to focus on merging things for the 9.0.1 release, and since 9.0 already carries the fix, there's nothing to be done. The last time we did a second minor release was for 5.0.2, which was to make a spectre mitigation available to some BSDs that used that particular compiler version. I don't think this bug by itself merits making an 8.0.2 release.
+Tom, the one who manages the minor releases, in case disagrees with the above.
Proposed patch for Clang 7 This is Jennifer's fix as a patch applicable to the release/7.x branch.
Proposed patch for Clang 8 This is Jennifer's patch applicable for the release/8.x branch.
Proposed patch for Clang 7 This is Jennifer's fix as a patch applicable to the release/7.x branch.
The original fix of Jennifer Yu has made it into Clang 9, but not into Clang 8 and Clang 7. Hence the issue still remains at least in Clang 8 and Clang 7.
I am going to put Jennifer's fix into Phabricator for those versions if possible.
While the issue is about a rather new C++17 feature, the compiler generates code that might crash (which it actually does in our project) and I also see potential for vulnerabilities due to memory corruption. I don't know the release policy, but imho there should be another release for clang 8 to fix this issue. MS Visual Studio ships clang 8 with that issue and I am not too sure whether MS are intending to proceed to clang 9 anytime soon.
Great! Marking the bug as fixed then.
Fix has being commit with:committed rGc19f4f806972: Fix bug 37903:MS ABI: handle inline static data member and inline variable as… (authored by jyu2).
I think Reid said there was a patch in progress for this? Can I get cc'd too, I'm curious.
https://reviews.llvm.org/D60912, and I cc'd you on it.
I think Reid said there was a patch in progress for this? Can I get cc'd too, I'm curious.
*** Bug llvm/llvm-bugzilla-archive#41409 has been marked as a duplicate of this bug. ***
*** Bug llvm/llvm-bugzilla-archive#38171 has been marked as a duplicate of this bug. ***
Yes, MS latest VC works.
For clang we generate following for each TU 03D 00000040 SECT1 notype () Static | _GLOBAL__sub_I_x.cpp
That is why ctor get call multiple time.
So to Fix, we should what template static variable initializer. Should be External instart Static.
I will see what I can do for this.
It seems MS fix the problem
Solution by avatar image Blair Wang [MSFT] · Mar 05 at 08:20 AM
A fix for this issue has been released! Please install the Visual Studio 2019 RC from https://visualstudio.microsoft.com/downloads/ . Thank you for providing valuable feedback which has helped improve the product.
https://developercommunity.visualstudio.com/content/problem/226484/vc2017-1564-inline-variable-in-header-multiple-con.html
Let me check out.
+Jennifer Yu, who is working on issue 40323, which is pretty related.
I'm curious (and a little frightened): does that imply that "magic statics" possibly do not work on Windows?
No, typically "magic statics" refers to static local variables, which are initialized when first used, instead of in some arbitrary order before main. They use some kind of guard variable scheme. By "magic statics" I mean their guaranteed thread-safe construction and destruction which was added in C++11 (N2660).
Previously, it was up to the programmer to make sure (or at least try, not even Alexandrescu had the ultimate solution). Now the compiler is responsible for it (of course, I know that you know all this).
If it is controlled via a guard variable (as you write) and its scheme is a potential problem on x86 Windows (as Hans writes) then I wonder if it really works on Windows. Does it work?
+Jennifer Yu, who is working on issue 40323, which is pretty related.
I'm curious (and a little frightened): does that imply that "magic statics" possibly do not work on Windows?
No, typically "magic statics" refers to static local variables, which are initialized when first used, instead of in some arbitrary order before main. They use some kind of guard variable scheme.
I just want to note that the destructor is called as often as the constructor (or more general, uninitialization as often as initialization). Can uninitialization also be controlled via a guard variable?
Yes, at the same time as initialization (which only happens once because of the guard variable), a call to the destructor is registered with __cxa_atexit
So on Itanium I think we get it right, but not on Windows. I haven't had time to look into how it's supposed to work there yet. Thanks for the info. I'm curious (and a little frightened): does that imply that "magic statics" possibly do not work on Windows?
You can see the logic here is very specific to particular code patters: https://github.com/llvm/llvm-project/blob/20118355aa6c517327bbd116a8b69e53999f8232/clang/lib/CodeGen/CGDeclCXX.cpp#L470
It looks for selectany and template instantiations, not linkage in general, so it misses inline global variables. Shouldn't be too hard to fix.