Skip to content

[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

Conversation

vitalybuka
Copy link
Collaborator

@vitalybuka vitalybuka commented Feb 13, 2025

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.

Created using spr 1.3.4
@vitalybuka vitalybuka requested a review from a team as a code owner February 13, 2025 01:44
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Feb 13, 2025
@vitalybuka vitalybuka marked this pull request as draft February 13, 2025 01:45
@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-libcxx

Author: Vitaly Buka (vitalybuka)

Changes

Asan reports it after #124103.

It's know case of false positive for Asan.
https://github.com/google/sanitizers/wiki/AddressSanitizerInitializationOrderFiasco#false-positives

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.


Full diff: https://github.com/llvm/llvm-project/pull/126995.diff

1 Files Affected:

  • (added) libcxx/test/std/input.output/iostreams.base/ios.base/ios.types/ios_Init/ios_Init.global.pass.cpp (+27)
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;
+}

@vitalybuka
Copy link
Collaborator Author

This is just test to see if it triggers any CI
I checked that https://lab.llvm.org/buildbot/#/builders/sanitizer-x86_64-linux-bootstrap-asan will fail this test without the fix.

Copy link

github-actions bot commented Feb 13, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@vitalybuka
Copy link
Collaborator Author

Created using spr 1.3.4
@vitalybuka vitalybuka marked this pull request as ready for review February 13, 2025 05:34
Created using spr 1.3.4
Created using spr 1.3.4
Created using spr 1.3.4
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
Copy link
Collaborator Author

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

Copy link
Contributor

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.

Copy link
Collaborator Author

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?

Copy link
Contributor

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.

Created using spr 1.3.4
Created using spr 1.3.4
Created using spr 1.3.4
Created using spr 1.3.4
@vitalybuka vitalybuka merged commit c6d95c4 into main Feb 16, 2025
76 checks passed
@vitalybuka vitalybuka deleted the users/vitalybuka/spr/libc-fix-initialization-order-fiasco-with-iostreamcpp-constructors branch February 16, 2025 03:54
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants