diff --git a/stl/inc/__msvc_sanitizer_annotate_container.hpp b/stl/inc/__msvc_sanitizer_annotate_container.hpp index dbfd83b311..042f053ac6 100644 --- a/stl/inc/__msvc_sanitizer_annotate_container.hpp +++ b/stl/inc/__msvc_sanitizer_annotate_container.hpp @@ -129,15 +129,15 @@ _STL_DISABLE_CLANG_WARNINGS #endif // ^^^ !defined(_INSERT_OPTIONAL_ANNOTATION) ^^^ #ifdef _ACTIVATE_STRING_ANNOTATION -#pragma comment(lib, "stl_asan") +extern "C" __declspec(selectany) 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") +extern "C" __declspec(selectany) 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") +extern "C" __declspec(selectany) const bool _Asan_optional_should_annotate = true; #pragma detect_mismatch("annotate_optional", "1") #endif // ^^^ defined(_ACTIVATE_OPTIONAL_ANNOTATION) ^^^ diff --git a/stl/src/asan.cpp b/stl/src/asan.cpp index e49b05f27f..6921d52b3b 100644 --- a/stl/src/asan.cpp +++ b/stl/src/asan.cpp @@ -3,8 +3,13 @@ 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; + // Retained for compatibility with old headers that add stl_asan.lib to the link. + // Use __declspec(selectany) to be compatible with new version 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. + extern __declspec(selectany) const bool _Asan_string_should_annotate = true; + extern __declspec(selectany) const bool _Asan_vector_should_annotate = true; + extern __declspec(selectany) const bool _Asan_optional_should_annotate = true; } // extern "C" } // namespace std 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..d5adb5844f --- /dev/null +++ b/tests/std/tests/GH_006186_asan_annotate_partial/env.lst @@ -0,0 +1,41 @@ +# 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-" +# TRANSITION, we don't use /ALTERNATENAME for Clang (see GH-5224) so we cannot test /D_ANNOTATE_VECTOR without -fsanitize=address +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..55483ef1cc --- /dev/null +++ b/tests/std/tests/GH_006186_asan_annotate_partial/test.cpp @@ -0,0 +1,37 @@ +// 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" const bool _Asan_vector_should_annotate; +extern "C" const bool _Asan_string_should_annotate; +extern "C" const bool _Asan_optional_should_annotate; + +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 ^^^