-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++] Fix initialization-order-fiasco with iostream.cpp constructors #126995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc++] Fix initialization-order-fiasco with iostream.cpp constructors #126995
Conversation
Created using spr 1.3.4
@llvm/pr-subscribers-libcxx Author: Vitaly Buka (vitalybuka) ChangesAsan reports it after #124103. It's know case of false positive for Asan. In general order global constructors in different However, implementation may contain workaround for Asan will still falsely report such cases, as it We need to fix/workaround the issue in libc++, as Full diff: https://github.com/llvm/llvm-project/pull/126995.diff 1 Files Affected:
diff --git a/libcxx/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.global.pass.cpp b/libcxx/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.global.pass.cpp
new file mode 100644
index 0000000000000..b1ea8517850f4
--- /dev/null
+++ b/libcxx/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.global.pass.cpp
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <iostream>
+
+extern "C" const char *__asan_default_options() {
+ return "check_initialization_order=true:strict_init_order=true";
+}
+
+// Test that ios used from globals constructors doesn't trigger
+// Asan initialization-order-fiasco.
+
+struct Global {
+ Global() {
+ std::cout << "Hello!";
+ }
+} global;
+
+int main(int, char**)
+{
+ return 0;
+}
|
This is just test to see if it triggers any CI |
✅ With the latest revision this PR passed the C/C++ code formatter. |
...xx/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.global.pass.cpp
Show resolved
Hide resolved
Created using spr 1.3.4
@@ -38,13 +38,20 @@ union stream_data { | |||
#define CHAR_MANGLING_wchar_t "_W" | |||
#define CHAR_MANGLING(CharT) CHAR_MANGLING_##CharT | |||
|
|||
#ifdef _LIBCPP_COMPILER_CLANG_BASED | |||
# define STRING_DATA_CONSTINIT constinit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't figure out how to make GCC happy,
all the time: error: ‘constinit’ variable ‘std::__1::cin’ does not have a constant initializer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I'll try to figure something out tomorrow for this. Either a bug against one of the compilers or some code changes should happen here. I guess a CWG issue would also be possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we land this one in meantime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with that.
…rs (llvm#126995) Asan reports it after llvm#124103. It's know case of false positive for Asan. https://github.com/google/sanitizers/wiki/AddressSanitizerInitializationOrderFiasco#false-positives It's can be avoided with `constexpr` constructors. In general order global constructors in different modules is undefined. If global constructor uses external global, they can be not constructed yet. However, implementation may contain workaround for that, or the state of non-constructed global can be still valid. Asan will still falsely report such cases, as it has no machinery to detect correctness of such cases. We need to fix/workaround the issue in libc++, as it will affect many libc++ with Asan users.
Asan reports it after #124103.
It's know case of false positive for Asan.
https://github.com/google/sanitizers/wiki/AddressSanitizerInitializationOrderFiasco#false-positives
It's can be avoided with
constexpr
constructors.In general order global constructors in different
modules is undefined. If global constructor uses
external global, they can be not constructed yet.
However, implementation may contain workaround for
that, or the state of non-constructed global can
be still valid.
Asan will still falsely report such cases, as it
has no machinery to detect correctness of such
cases.
We need to fix/workaround the issue in libc++, as
it will affect many libc++ with Asan users.