diff --git a/libcxx/include/variant b/libcxx/include/variant index 5f2d03b7227b8..723ff5e28300c 100644 --- a/libcxx/include/variant +++ b/libcxx/include/variant @@ -261,6 +261,10 @@ namespace std { #include #include +#if __has_feature(memory_sanitizer) +# include +#endif + // standard-mandated includes // [variant.syn] @@ -781,6 +785,10 @@ _LIBCPP_VARIANT_DESTRUCTOR( _Trait::_TriviallyAvailable, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__dtor() = default, _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy() noexcept { +# if __has_feature(memory_sanitizer) + if (!std::is_constant_evaluated()) + __sanitizer_dtor_callback(&this->__data, sizeof(this->__data)); +# endif this->__index = __variant_npos<__index_t>; } _LIBCPP_EAT_SEMICOLON); diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant_msan.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant_msan.pass.cpp new file mode 100644 index 0000000000000..a18a8d1d29391 --- /dev/null +++ b/libcxx/test/std/utilities/variant/variant.variant/variant_msan.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14 + +// + +// template class variant; + +#include + +#if __has_feature(memory_sanitizer) +# include +#endif + +int main(int, char**) { +#if __has_feature(memory_sanitizer) + std::variant v; + v.emplace(); + double& d = std::get(v); + v.emplace(); + if (__msan_test_shadow(&d, sizeof(d)) == -1) { + // Unexpected: The entire range is accessible. + return 1; + } +#endif + + return 0; +}