Skip to content
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++] Avoid calling setlocale in do_unshift when unnecessary #117153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

m417z
Copy link

@m417z m417z commented Nov 21, 2024

This is an attempt to mitigate #110954.

As part of the libc++.dll initialization, static DoIOSInit is initialized:

ios_base::Init::Init() {
static DoIOSInit init_the_streams; // gets initialized once
}

When the dll is unloaded or on process shutdown, DoIOSInit::~DoIOSInit is called. It ends up calling flush:

wcout_ptr->flush();

Which calls pubsync:

if (this->rdbuf()->pubsync() == -1)

Which ends up calling do_unshift:

codecvt<wchar_t, char, mbstate_t>::result codecvt<wchar_t, char, mbstate_t>::do_unshift(
state_type& st, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const {
to_nxt = to;
extern_type tmp[MB_LEN_MAX];
size_t n = __locale::__wcrtomb(tmp, intern_type(), &st, __l_);

Which, as can be seen, unconditionally calls __locale::__wcrtomb, which ends up calling setlocale via __libcpp_locale_guard.

All this means that setlocale is called on process shutdown even if wcout is never used, or even if nothing stream-related is used. Calling setlocale on process shutdown causes problems, as described in the mentioned issue.

This PR is an attempt to avoid calling setlocale in the vast majority of cases, when there's no output to be flushed. It's not a complete fix to the issue, but it will make it much less common, and it will at least allow to flush output manually to avoid the issue if streams are used.

@m417z m417z requested a review from a team as a code owner November 21, 2024 12:58
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-libcxx

Author: Michael Maltsev (m417z)

Changes

This is an attempt to mitigate #110954.

As part of the libc++.dll initialization, static DoIOSInit is initialized:

ios_base::Init::Init() {
static DoIOSInit init_the_streams; // gets initialized once
}

When the dll is unloaded or on process shutdown, DoIOSInit::~DoIOSInit is called. It ends up calling flush:

wcout_ptr->flush();

Which calls pubsync:

if (this->rdbuf()->pubsync() == -1)

Which ends up calling do_unshift:

codecvt<wchar_t, char, mbstate_t>::result codecvt<wchar_t, char, mbstate_t>::do_unshift(
state_type& st, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const {
to_nxt = to;
extern_type tmp[MB_LEN_MAX];
size_t n = __locale::__wcrtomb(tmp, intern_type(), &st, __l_);

Which, as can be seen, unconditionally calls __locale::__wcrtomb, which ends up calling setlocale via __libcpp_locale_guard.

All this means that setlocale is called on process shutdown even if wcout is never used, or even if nothing stream-related is used. Calling setlocale on process shutdown causes problems, as described in the mentioned issue.

This PR is an attempt to avoid calling setlocale in the vast majority of cases, when there's no output to be flushed. It's not a complete fix to the issue, but it will make it much less common, and it will at least allow to flush output manually to avoid the issue if streams are used.


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

1 Files Affected:

  • (modified) libcxx/src/locale.cpp (+2)
diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp
index a1e10401f0b299..5ecd99c53cd516 100644
--- a/libcxx/src/locale.cpp
+++ b/libcxx/src/locale.cpp
@@ -1475,6 +1475,8 @@ codecvt<wchar_t, char, mbstate_t>::result codecvt<wchar_t, char, mbstate_t>::do_
 codecvt<wchar_t, char, mbstate_t>::result codecvt<wchar_t, char, mbstate_t>::do_unshift(
     state_type& st, extern_type* to, extern_type* to_end, extern_type*& to_nxt) const {
   to_nxt = to;
+  if (std::mbsinit(&st))
+    return ok;
   extern_type tmp[MB_LEN_MAX];
   size_t n = __locale::__wcrtomb(tmp, intern_type(), &st, __l_);
   if (n == size_t(-1) || n == 0) // on error

@philnik777 philnik777 changed the title Avoid calling setlocale in do_unshift when unnecessary [libc++] Avoid calling setlocale in do_unshift when unnecessary Nov 21, 2024
@cpplearner
Copy link
Contributor

I wonder if we should remove the unshift call in sync().

template <class _CharT, class _Traits>
int basic_filebuf<_CharT, _Traits>::sync() {
if (__file_ == nullptr)
return 0;
if (!__cv_)
__throw_bad_cast();
if (__cm_ & ios_base::out) {
if (this->pptr() != this->pbase())
if (overflow() == traits_type::eof())
return -1;
codecvt_base::result __r;
do {
char* __extbe;
__r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
return -1;
} while (__r == codecvt_base::partial);

The standard doesn't require such a call. [filebuf.virtuals]/19:

int sync() override;

Effects: If a put area exists, calls filebuf​::​overflow to write the characters to the file, then flushes the file as if by calling fflush(file). If a get area exists, the effect is implementation-defined.

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