Skip to content

[libcxx] Initialize vcruntime __std_exception_data in the exception copy ctor #144329

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
merged 1 commit into from
Aug 5, 2025

Conversation

mstorsjo
Copy link
Member

This fixes failures in a number of tests, in the
clang-cl-no-vcruntime configuration (where libcxx provides dummy, no-op replacements of some vcruntime base exception classes), if building with optimization enabled.

Previously, with optimization enabled, the compiler concluded that these fields would be uninitialized at the points of asserts in the tests.

This fixes the following tests in this configuration:

llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.dynamic/alloc.errors/bad.alloc/bad_alloc.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.dynamic/alloc.errors/new.badlength/bad_array_new_length.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.exception/bad.exception/bad_exception.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.exception/exception/exception.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.rtti/bad.cast/bad_cast.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.rtti/bad.typeid/bad_typeid.pass.cpp

@mstorsjo mstorsjo requested a review from a team as a code owner June 16, 2025 10:50
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jun 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 16, 2025

@llvm/pr-subscribers-libcxx

Author: Martin Storsjö (mstorsjo)

Changes

This fixes failures in a number of tests, in the
clang-cl-no-vcruntime configuration (where libcxx provides dummy, no-op replacements of some vcruntime base exception classes), if building with optimization enabled.

Previously, with optimization enabled, the compiler concluded that these fields would be uninitialized at the points of asserts in the tests.

This fixes the following tests in this configuration:

llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.dynamic/alloc.errors/bad.alloc/bad_alloc.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.dynamic/alloc.errors/new.badlength/bad_array_new_length.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.exception/bad.exception/bad_exception.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.exception/exception/exception.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.rtti/bad.cast/bad_cast.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.rtti/bad.typeid/bad_typeid.pass.cpp

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

1 Files Affected:

  • (modified) libcxx/include/__exception/exception.h (+1-1)
diff --git a/libcxx/include/__exception/exception.h b/libcxx/include/__exception/exception.h
index f7dab6e83ad14..161cc49979e4a 100644
--- a/libcxx/include/__exception/exception.h
+++ b/libcxx/include/__exception/exception.h
@@ -48,7 +48,7 @@ class exception { // base of all library exceptions
     __data_._DoFree = true;
   }
 
-  exception(exception const&) _NOEXCEPT {}
+  exception(exception const&) _NOEXCEPT : __data_() {}
 
   exception& operator=(exception const&) _NOEXCEPT { return *this; }
 

@mstorsjo mstorsjo force-pushed the libcxx-vcruntime-exception-init branch from 10913df to 38f3ff4 Compare June 18, 2025 19:05
@mstorsjo
Copy link
Member Author

Ping; this one should be super trivial.

We provide a dummy no-op implementation of std::exception for the clang-cl-no-vcruntime case (when compiling with -D_HAS_EXCEPTIONS=0). Previously, the copy constructor left the structure members uninitialized, which the compiler used for doing unexpected conclusions when compiling with optimizations enabled. So even if the class is a dummy no-op, make sure to actually initialize the struct members.

@mstorsjo
Copy link
Member Author

Ping

@@ -48,7 +48,7 @@ class exception { // base of all library exceptions
__data_._DoFree = true;
}

exception(exception const&) _NOEXCEPT {}
exception(exception const&) _NOEXCEPT : __data_() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we don't do a proper copy? What happens if two TUs are linked where we provide our own implementation while the other is provided by vcruntime?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we don't do a proper copy?

To properly do a copy, the real vcruntime here calls the __std_exception_copy helper function (to do a proper deep copy of the contained data structure). This dummy implementation here, used when compiling in the no-vcruntime setup, is intentionally a no-op - which is usually fine. However here the too-much-no-op case makes the compiler do incorrect conclusions when compiling with optimizations enabled.

What happens if two TUs are linked where we provide our own implementation while the other is provided by vcruntime?

Not sure it this dummy implementation is being mangled differently to avoid collisions, or if that's a latent bug for anybody doing mixed bugs. Clearly a potential issue - but mostly orthogonal to this patch.

…opy ctor

This fixes failures in a number of tests, in the
clang-cl-no-vcruntime configuration (where libcxx provides dummy,
no-op replacements of some vcruntime base exception classes), if
building with optimization enabled.

Previously, with optimization enabled, the compiler concluded that
these fields would be uninitialized at the points of asserts in
the tests.

This fixes the following tests in this configuration:

    llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.dynamic/alloc.errors/bad.alloc/bad_alloc.pass.cpp
    llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.dynamic/alloc.errors/new.badlength/bad_array_new_length.pass.cpp
    llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.exception/bad.exception/bad_exception.pass.cpp
    llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.exception/exception/exception.pass.cpp
    llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.rtti/bad.cast/bad_cast.pass.cpp
    llvm-libc++-shared-no-vcruntime-clangcl.cfg.in :: std/language.support/support.rtti/bad.typeid/bad_typeid.pass.cpp
@mstorsjo mstorsjo force-pushed the libcxx-vcruntime-exception-init branch from 38f3ff4 to 2f83bba Compare August 5, 2025 12:43
@mstorsjo mstorsjo merged commit f355a57 into llvm:main Aug 5, 2025
70 of 76 checks passed
@mstorsjo mstorsjo deleted the libcxx-vcruntime-exception-init branch August 5, 2025 19:11
krishna2803 pushed a commit to krishna2803/llvm-project that referenced this pull request Aug 12, 2025
…opy ctor (llvm#144329)

This fixes failures in a number of tests, in the
clang-cl-no-vcruntime configuration (where libcxx provides dummy, no-op
replacements of some vcruntime base exception classes), if building with
optimization enabled.

Previously, with optimization enabled, the compiler concluded that these
fields would be uninitialized at the points of asserts in the tests.

This fixes the following tests in this configuration:

llvm-libc++-shared-no-vcruntime-clangcl.cfg.in ::
std/language.support/support.dynamic/alloc.errors/bad.alloc/bad_alloc.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in ::
std/language.support/support.dynamic/alloc.errors/new.badlength/bad_array_new_length.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in ::
std/language.support/support.exception/bad.exception/bad_exception.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in ::
std/language.support/support.exception/exception/exception.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in ::
std/language.support/support.rtti/bad.cast/bad_cast.pass.cpp
llvm-libc++-shared-no-vcruntime-clangcl.cfg.in ::
std/language.support/support.rtti/bad.typeid/bad_typeid.pass.cpp
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