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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions libcxx/src/iostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD

template <class StreamT, class BufferT>
union stream_data {
stream_data() {}
~stream_data() {}
constexpr stream_data() {}
constexpr ~stream_data() {}
struct {
// The stream has to be the first element, since that's referenced by the stream declarations in <iostream>
StreamT stream;
Expand All @@ -38,13 +38,19 @@ 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.

#else
# define STRING_DATA_CONSTINIT
#endif

#ifdef _LIBCPP_ABI_MICROSOFT
# define STREAM(StreamT, BufferT, CharT, var) \
stream_data<StreamT<CharT>, BufferT<CharT>> var __asm__( \
STRING_DATA_CONSTINIT stream_data<StreamT<CharT>, BufferT<CharT>> var __asm__( \
"?" #var "@" ABI_NAMESPACE_STR "@std@@3V?$" #StreamT \
"@" CHAR_MANGLING(CharT) "U?$char_traits@" CHAR_MANGLING(CharT) "@" ABI_NAMESPACE_STR "@std@@@12@A")
#else
# define STREAM(StreamT, BufferT, CharT, var) stream_data<StreamT<CharT>, BufferT<CharT>> var
# define STREAM(StreamT, BufferT, CharT, var) STRING_DATA_CONSTINIT stream_data<StreamT<CharT>, BufferT<CharT>> var
#endif

// These definitions and the declarations in <iostream> technically cause ODR violations, since they have different
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// 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>

// FIXME: Remove after issue https://github.com/llvm/llvm-project/issues/127348 resolved.
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; }