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

[ASan] Disable annotations on unsupported platforms #4058

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions stl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,12 @@ add_stl_dlls("d" Debug)

function(add_stl_statics FLAVOR_SUFFIX REL_OR_DBG IDL_VALUE)
add_library(libcpmt${FLAVOR_SUFFIX}_eha OBJECT ${EHA_SOURCES})
target_compile_definitions(libcpmt${FLAVOR_SUFFIX}_eha PRIVATE _ANNOTATE_VECTOR _ANNOTATE_STRING "_ITERATOR_DEBUG_LEVEL=${IDL_VALUE}")
target_compile_definitions(libcpmt${FLAVOR_SUFFIX}_eha PRIVATE _ANNOTATE_STL "_ITERATOR_DEBUG_LEVEL=${IDL_VALUE}")
target_compile_options(libcpmt${FLAVOR_SUFFIX}_eha PRIVATE /EHa)
target_stl_compile_options(libcpmt${FLAVOR_SUFFIX}_eha ${REL_OR_DBG})

add_library(libcpmt${FLAVOR_SUFFIX} STATIC ${HEADERS} ${IMPLIB_SOURCES} ${SOURCES} ${INITIALIZER_SOURCES} ${STATIC_SOURCES})
target_compile_definitions(libcpmt${FLAVOR_SUFFIX} PRIVATE _ANNOTATE_VECTOR _ANNOTATE_STRING "_ITERATOR_DEBUG_LEVEL=${IDL_VALUE}")
target_compile_definitions(libcpmt${FLAVOR_SUFFIX} PRIVATE _ANNOTATE_STL "_ITERATOR_DEBUG_LEVEL=${IDL_VALUE}")
strega-nil-ms marked this conversation as resolved.
Show resolved Hide resolved
target_compile_options(libcpmt${FLAVOR_SUFFIX} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:/EHsc>")
target_link_libraries(libcpmt${FLAVOR_SUFFIX} PRIVATE Boost::math stl_alias_objects libcpmt${FLAVOR_SUFFIX}_eha)
target_stl_compile_options(libcpmt${FLAVOR_SUFFIX} ${REL_OR_DBG})
Expand Down
80 changes: 61 additions & 19 deletions stl/inc/__msvc_sanitizer_annotate_container.hpp
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,51 @@ _STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new

#if !defined(_M_CEE_PURE) && !(defined(_DISABLE_STRING_ANNOTATION) && defined(_DISABLE_VECTOR_ANNOTATION))
// The following macros change the behavior of this file:
// - _DISABLE_STL_ANNOTATION: Disable ASan annotations in the standard library
// (this will be auto-defined on unsupported platforms)
// + _DISABLE_STRING_ANNOTATION: same, but for only `basic_string`
// + _DISABLE_VECTOR_ANNOTATION: same, but for only `vector`
// - _ENABLE_STL_ANNOTATION_ON_UNSUPPORTED_PLATFORMS: Don't auto-disable ASan annotations
// - _ANNOTATE_STL: Even when ASan annotations are disabled, insert the code for annotating into the STL anyways;
// this is useful when building static libraries which may be linked against both ASan and non-ASan binaries.
// + _ANNOTATE_STRING: same, but only for `basic_string`
// + _ANNOTATE_VECTOR: same, but only for `vector`

#if !defined(_DISABLE_STL_ANNOTATION) && !defined(_ENABLE_STL_ANNOTATION_ON_UNSUPPORTED_PLATFORMS)

#if defined(_M_ARM64EC) || defined(_M_ARM64) || defined(_M_ARM) || defined(_M_CEE_PURE)
#define _DISABLE_STL_ANNOTATION
#endif // ^^^ unsupported platform ^^^

#endif // ^^^ !defined(_DISABLE_STL_ANNOTATION) && !defined(_ENABLE_STL_ANNOTATION_ON_UNSUPPORTED_PLATFORMS) ^^^

#ifdef _DISABLE_STL_ANNOTATION

#ifdef _ENABLE_STL_ANNOTATION_ON_UNSUPPORTED_PLATFORMS
#error _ENABLE_STL_ANNOTATION_ON_UNSUPPORTED_PLATFORMS and _DISABLE_STL_ANNOTATION are mutually exclusive
#endif // ^^^ defined(_ENABLE_STL_ANNOTATION_ON_UNSUPPORTED_PLATFORMS) ^^^

#ifndef _DISABLE_STRING_ANNOTATION
#define _DISABLE_STRING_ANNOTATION
#endif // ^^^ !defined(_DISABLE_STRING_ANNOTATION) ^^^
#ifndef _DISABLE_VECTOR_ANNOTATION
#define _DISABLE_VECTOR_ANNOTATION
#endif // ^^^ !defined(_DISABLE_VECTOR_ANNOTATION) ^^^

#endif // ^^^ defined(_DISABLE_STL_ANNOTATION) ^^^

#ifdef _ANNOTATE_STL

#ifndef _ANNOTATE_STRING
#define _ANNOTATE_STRING
#endif // ^^^ !defined(_ANNOTATE_STRING) ^^^

#ifndef _ANNOTATE_VECTOR
#define _ANNOTATE_VECTOR
#endif // ^^^ !defined(_ANNOTATE_VECTOR) ^^^

#endif // ^^^ defined(_ANNOTATE_STL) ^^^

#ifdef __SANITIZE_ADDRESS__

Expand All @@ -34,41 +78,41 @@ _STL_DISABLE_CLANG_WARNINGS
#pragma comment(linker, "/INFERASANLIBS")
#endif // __has_feature(address_sanitizer)

#else // ^^^ defined(__clang__) / !defined(__clang__) && !defined(__SANITIZE_ADDRESS__) vvv
#endif // ^^^ defined(__clang__) ^^^

#ifdef _ANNOTATE_STRING
#define _INSERT_STRING_ANNOTATION
#endif // defined(_ANNOTATE_STRING)
#ifdef _ANNOTATE_VECTOR
#define _INSERT_VECTOR_ANNOTATION
#endif // defined(_ANNOTATE_VECTOR)

#endif // ^^^ !defined(__clang__) && !defined(__SANITIZE_ADDRESS__) ^^^

#ifdef _DISABLE_STRING_ANNOTATION
#undef _ACTIVATE_STRING_ANNOTATION
#undef _INSERT_STRING_ANNOTATION
#endif // defined(_DISABLE_STRING_ANNOTATION)
#endif // ^^^ defined(_DISABLE_STRING_ANNOTATION) ^^^
#ifdef _DISABLE_VECTOR_ANNOTATION
#undef _ACTIVATE_VECTOR_ANNOTATION
#undef _INSERT_VECTOR_ANNOTATION
#endif // defined(_DISABLE_VECTOR_ANNOTATION)
#endif // ^^^ defined(_DISABLE_VECTOR_ANNOTATION) ^^^

#ifdef _ANNOTATE_STRING
#define _INSERT_STRING_ANNOTATION
#endif // ^^^ defined(_ANNOTATE_STRING) ^^^
#ifdef _ANNOTATE_VECTOR
#define _INSERT_VECTOR_ANNOTATION
#endif // ^^^ defined(_ANNOTATE_VECTOR) ^^^


#ifndef _INSERT_STRING_ANNOTATION
#pragma detect_mismatch("annotate_string", "0")
#endif // !defined(_INSERT_STRING_ANNOTATION)
#endif // ^^^ !defined(_INSERT_STRING_ANNOTATION) ^^^
#ifndef _INSERT_VECTOR_ANNOTATION
#pragma detect_mismatch("annotate_vector", "0")
#endif // !defined(_INSERT_VECTOR_ANNOTATION)
#endif // ^^^ !defined(_INSERT_VECTOR_ANNOTATION) ^^^

#ifdef _ACTIVATE_STRING_ANNOTATION
#pragma comment(lib, "stl_asan")
#pragma detect_mismatch("annotate_string", "1")
#endif // defined(_ACTIVATE_STRING_ANNOTATION)
#endif // ^^^ defined(_ACTIVATE_STRING_ANNOTATION) ^^^
#ifdef _ACTIVATE_VECTOR_ANNOTATION
#pragma comment(lib, "stl_asan")
#pragma detect_mismatch("annotate_vector", "1")
#endif // defined(_ACTIVATE_VECTOR_ANNOTATION)
#endif // ^^^ defined(_ACTIVATE_VECTOR_ANNOTATION) ^^^

#undef _ACTIVATE_STRING_ANNOTATION
#undef _ACTIVATE_VECTOR_ANNOTATION
Expand Down Expand Up @@ -121,9 +165,7 @@ void __cdecl __sanitizer_annotate_contiguous_container(
#error Unknown architecture
#endif // ^^^ unknown architecture ^^^

#endif // insert asan annotations

#endif // !defined(_M_CEE_PURE) && asan not disabled
#endif // ^^^ insert ASan annotations ^^^

#pragma pop_macro("new")
_STL_RESTORE_CLANG_WARNINGS
Expand Down
2 changes: 1 addition & 1 deletion stl/msbuild/stl_base/libcp.settings.targets
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

<PropertyGroup>
<ClProgramDataBaseFileName>$(OutputLibPdbPath)$(OutputName)$(PdbVerName).pdb</ClProgramDataBaseFileName>
<ClDefines>$(ClDefines);_VCRT_ALLOW_INTERNALS;_ANNOTATE_VECTOR;_ANNOTATE_STRING</ClDefines>
<ClDefines>$(ClDefines);_VCRT_ALLOW_INTERNALS;_ANNOTATE_STL</ClDefines>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)\stl.files.settings.targets"/>
Expand Down