diff --git a/stl/inc/__msvc_sanitizer_annotate_container.hpp b/stl/inc/__msvc_sanitizer_annotate_container.hpp index dbfd83b311..e9711aab5a 100644 --- a/stl/inc/__msvc_sanitizer_annotate_container.hpp +++ b/stl/inc/__msvc_sanitizer_annotate_container.hpp @@ -128,16 +128,17 @@ _STL_DISABLE_CLANG_WARNINGS #pragma detect_mismatch("annotate_optional", "0") #endif // ^^^ !defined(_INSERT_OPTIONAL_ANNOTATION) ^^^ +extern "C" { #ifdef _ACTIVATE_STRING_ANNOTATION -#pragma comment(lib, "stl_asan") +__declspec(selectany) extern const bool _Asan_string_should_annotate = true; #pragma detect_mismatch("annotate_string", "1") #endif // ^^^ defined(_ACTIVATE_STRING_ANNOTATION) ^^^ #ifdef _ACTIVATE_VECTOR_ANNOTATION -#pragma comment(lib, "stl_asan") +__declspec(selectany) extern const bool _Asan_vector_should_annotate = true; #pragma detect_mismatch("annotate_vector", "1") #endif // ^^^ defined(_ACTIVATE_VECTOR_ANNOTATION) ^^^ #ifdef _ACTIVATE_OPTIONAL_ANNOTATION -#pragma comment(lib, "stl_asan") +__declspec(selectany) extern const bool _Asan_optional_should_annotate = true; #pragma detect_mismatch("annotate_optional", "1") #endif // ^^^ defined(_ACTIVATE_OPTIONAL_ANNOTATION) ^^^ @@ -145,7 +146,6 @@ _STL_DISABLE_CLANG_WARNINGS #undef _ACTIVATE_VECTOR_ANNOTATION #undef _ACTIVATE_OPTIONAL_ANNOTATION -extern "C" { #ifdef _INSERT_VECTOR_ANNOTATION extern const bool _Asan_vector_should_annotate; #endif diff --git a/stl/src/asan.cpp b/stl/src/asan.cpp index e49b05f27f..b496e1c6aa 100644 --- a/stl/src/asan.cpp +++ b/stl/src/asan.cpp @@ -1,10 +1,14 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -namespace std { - extern "C" { - extern const bool _Asan_string_should_annotate = true; - extern const bool _Asan_vector_should_annotate = true; - extern const bool _Asan_optional_should_annotate = true; - } // extern "C" -} // namespace std +extern "C" { +// TRANSITION, ABI: preserved for compatibility with old headers, which +// added stl_asan.lib to the link line. We use __declspec(selectany) to be +// compatible with the new headers that define these variables as +// __declspec(selectany) in __msvc_sanitizer_annotate_container.hpp. +// The new method is preferred because previously enabling just string +// would also enable vector and optional. See GH-6186 for details. +__declspec(selectany) extern const bool _Asan_string_should_annotate = true; +__declspec(selectany) extern const bool _Asan_vector_should_annotate = true; +__declspec(selectany) extern const bool _Asan_optional_should_annotate = true; +} // extern "C" diff --git a/tests/std/test.lst b/tests/std/test.lst index 74cfcda8f4..588ba0bd94 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -282,6 +282,7 @@ tests\GH_005800_stable_sort_large_alignment tests\GH_005816_numeric_limits_traps tests\GH_005968_headers_provide_begin_end tests\GH_005974_asan_annotate_optional +tests\GH_006186_asan_annotate_partial tests\LWG2381_num_get_floating_point tests\LWG2510_tag_classes tests\LWG2597_complex_branch_cut diff --git a/tests/std/tests/GH_006186_asan_annotate_partial/env.lst b/tests/std/tests/GH_006186_asan_annotate_partial/env.lst new file mode 100644 index 0000000000..2c694ae2c1 --- /dev/null +++ b/tests/std/tests/GH_006186_asan_annotate_partial/env.lst @@ -0,0 +1,40 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# This test matrix is the usual test matrix, with all currently unsupported options removed, crossed with the ASan flags. + +# TRANSITION, google/sanitizers#328: clang-cl does not support /MDd or /MTd with ASan +RUNALL_INCLUDE ..\prefix.lst +RUNALL_CROSSLIST +PM_CL="/fsanitize=address /DTEST_ENSURE_VECTOR_ENABLED /DTEST_ENSURE_STRING_ENABLED /DTEST_ENSURE_OPTIONAL_ENABLED" +PM_CL="/fsanitize=address /D_DISABLE_VECTOR_ANNOTATION /DTEST_ENSURE_STRING_ENABLED /DTEST_ENSURE_OPTIONAL_ENABLED" +PM_CL="/fsanitize=address /DTEST_ENSURE_VECTOR_ENABLED /D_DISABLE_STRING_ANNOTATION /DTEST_ENSURE_OPTIONAL_ENABLED" +PM_CL="/fsanitize=address /DTEST_ENSURE_VECTOR_ENABLED /DTEST_ENSURE_STRING_ENABLED /D_DISABLE_OPTIONAL_ANNOTATION" +PM_CL="/D_ANNOTATE_STL" +RUNALL_CROSSLIST +PM_CL="/Zi /wd4611 /w14640 /Zc:threadSafeInit-" PM_LINK="/debug" +RUNALL_CROSSLIST +PM_CL="/BE /c /EHsc /MD /std:c++14" +PM_CL="/BE /c /EHsc /MDd /std:c++17 /permissive-" +PM_CL="/BE /c /EHsc /MT /std:c++20 /permissive-" +PM_CL="/BE /c /EHsc /MTd /std:c++latest /permissive-" +PM_CL="/EHsc /MD /std:c++14" +PM_CL="/EHsc /MD /std:c++17" +PM_CL="/EHsc /MD /std:c++20" +PM_CL="/EHsc /MD /std:c++latest /permissive- /Zc:char8_t- /Zc:preprocessor" +PM_CL="/EHsc /MD /std:c++latest /permissive- /Zc:noexceptTypes-" +PM_CL="/EHsc /MDd /std:c++14 /fp:except /Zc:preprocessor" +PM_CL="/EHsc /MDd /std:c++17 /permissive-" +PM_CL="/EHsc /MDd /std:c++20 /permissive-" +PM_CL="/EHsc /MDd /std:c++latest /permissive- /Zc:wchar_t-" +PM_CL="/EHsc /MDd /std:c++latest /permissive-" +PM_CL="/EHsc /MT /std:c++latest /permissive- /analyze:only /analyze:autolog-" +PM_CL="/EHsc /MT /std:c++latest /permissive-" +PM_CL="/EHsc /MTd /std:c++latest /permissive" +PM_CL="/EHsc /MTd /std:c++latest /permissive- /analyze:only /analyze:autolog-" +PM_CL="/EHsc /MTd /std:c++latest /permissive- /fp:strict" +PM_CL="/EHsc /MTd /std:c++latest /permissive-" +PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MD /std:c++14" +PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MD /std:c++17" +PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MT /std:c++20 /permissive-" +PM_COMPILER="clang-cl" PM_CL="-fno-ms-compatibility -fno-delayed-template-parsing -Wno-unqualified-std-cast-call /EHsc /MT /std:c++latest /permissive- /fp:strict" diff --git a/tests/std/tests/GH_006186_asan_annotate_partial/test.cpp b/tests/std/tests/GH_006186_asan_annotate_partial/test.cpp new file mode 100644 index 0000000000..2096f12df8 --- /dev/null +++ b/tests/std/tests/GH_006186_asan_annotate_partial/test.cpp @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +// REQUIRES: x64 || x86 || arm64 + +#if defined(__clang__) && defined(_M_ARM64) // TRANSITION, LLVM-184902, fixed in Clang 23 +#pragma comment(linker, "/INFERASANLIBS") +int main() {} +#else // ^^^ workaround / no workaround vvv + +#include +#include // include __msvc_sanitizer_annotate_container.hpp + +extern "C" { +extern const bool _Asan_vector_should_annotate; +extern const bool _Asan_string_should_annotate; +extern const bool _Asan_optional_should_annotate; +} // extern "C" + +int main() { +#ifdef TEST_ENSURE_VECTOR_ENABLED + assert(_Asan_vector_should_annotate == true); +#else + assert(_Asan_vector_should_annotate == false); +#endif + +#ifdef TEST_ENSURE_STRING_ENABLED + assert(_Asan_string_should_annotate == true); +#else + assert(_Asan_string_should_annotate == false); +#endif + +#ifdef TEST_ENSURE_OPTIONAL_ENABLED + assert(_Asan_optional_should_annotate == true); +#else + assert(_Asan_optional_should_annotate == false); +#endif +} + +#endif // ^^^ no workaround ^^^