From 08dbc29b8f4b348cd08761bf676c010d62f2d062 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 22 Jun 2021 11:12:13 -0700 Subject: [PATCH 01/28] First pass to enable linking in ASAN for coreclr builds. --- eng/native/configurecompiler.cmake | 24 +++++++++++-------- eng/native/functions.cmake | 4 ++++ src/coreclr/enablesanitizers.sh | 24 ++++--------------- .../utilcode/clrhost_nodependencies.cpp | 10 ++++---- src/native/common/asanoptions.c | 8 +++++++ 5 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 src/native/common/asanoptions.c diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 60d2b2b80296f..ded28787084b0 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -105,6 +105,15 @@ if (MSVC) add_linker_flag(/NODEFAULTLIB:libucrt.lib RELEASE) add_linker_flag(/DEFAULTLIB:ucrt.lib RELEASE) + # Configure ASAN for MSVC + + # /RTC1 is added by default by CMake, so remove it. + string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + string(REPLACE "/RTC1" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + + add_compile_options($<$,$>:-fsanitize=address>) + add_linker_flag(/INFERASANLIBS DEBUG CHECKED) + elseif (CLR_CMAKE_HOST_UNIX) # Set the values to display when interactively configuring CMAKE_BUILD_TYPE set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO") @@ -112,26 +121,21 @@ elseif (CLR_CMAKE_HOST_UNIX) # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE) + # Configure ASAN for Clang/GCC + add_compile_options($<$,$,$>>:-fsanitize=address>) + add_linker_flag(-fsanitize=address DEBUG CHECKED) + set(CLR_SANITIZE_CXX_OPTIONS "") set(CLR_SANITIZE_LINK_OPTIONS "") # set the CLANG sanitizer flags for debug build if(UPPERCASE_CMAKE_BUILD_TYPE STREQUAL DEBUG OR UPPERCASE_CMAKE_BUILD_TYPE STREQUAL CHECKED) # obtain settings from running enablesanitizers.sh - string(FIND "$ENV{DEBUG_SANITIZERS}" "asan" __ASAN_POS) string(FIND "$ENV{DEBUG_SANITIZERS}" "ubsan" __UBSAN_POS) - if ((${__ASAN_POS} GREATER -1) OR (${__UBSAN_POS} GREATER -1)) + if (${__UBSAN_POS} GREATER -1) list(APPEND CLR_SANITIZE_CXX_OPTIONS -fsanitize-blacklist=${CMAKE_CURRENT_SOURCE_DIR}/sanitizerblacklist.txt) set (CLR_CXX_SANITIZERS "") set (CLR_LINK_SANITIZERS "") - if (${__ASAN_POS} GREATER -1) - list(APPEND CLR_CXX_SANITIZERS address) - list(APPEND CLR_LINK_SANITIZERS address) - set(CLR_SANITIZE_CXX_FLAGS "${CLR_SANITIZE_CXX_FLAGS}address,") - set(CLR_SANITIZE_LINK_FLAGS "${CLR_SANITIZE_LINK_FLAGS}address,") - add_definitions(-DHAS_ASAN) - message("Address Sanitizer (asan) enabled") - endif () if (${__UBSAN_POS} GREATER -1) # all sanitizier flags are enabled except alignment (due to heavy use of __unaligned modifier) list(APPEND CLR_CXX_SANITIZERS diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 6ff45a86fbb61..7a43c2327d760 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -603,6 +603,10 @@ function(link_natvis_sources_for_target targetName linkKind) endforeach() endfunction() +function(add_sanitizer_options targetName) + target_sources(${targetName} "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.c>") +endfunction() + function(add_executable_clr targetName) if(NOT WIN32) add_executable(${ARGV} ${VERSION_FILE_PATH}) diff --git a/src/coreclr/enablesanitizers.sh b/src/coreclr/enablesanitizers.sh index c79ef8c69ddf3..a5f88be30636d 100755 --- a/src/coreclr/enablesanitizers.sh +++ b/src/coreclr/enablesanitizers.sh @@ -9,8 +9,7 @@ if [ $# -eq 0 ]; then else echo " cd $(dirname $0);. enablesanitizers.sh [options]; cd -" fi - echo "Usage: [asan] [ubsan] [lsan] [all] [off] [clangx.y]" - echo " asan: optional argument to enable Address Sanitizer." + echo "Usage: [ubsan] [lsan] [all] [off] [clangx.y]" echo " ubsan: optional argument to enable Undefined Behavior Sanitizer." echo " lsan - optional argument to enable memory Leak Sanitizer." echo " all - optional argument to enable asan, ubsan and lsan." @@ -21,7 +20,6 @@ else __ClangMajorVersion=3 __ClangMinorVersion=6 - __EnableASan=0 __EnableUBSan=0 __EnableLSan=0 __TurnOff=0 @@ -32,18 +30,13 @@ else do lowerI="$(echo $i | tr "[:upper:]" "[:lower:]")" case $lowerI in - asan) - __EnableASan=1 - ;; ubsan) __EnableUBSan=1 ;; lsan) - __EnableASan=1 __EnableLSan=1 ;; all) - __EnableASan=1 __EnableUBSan=1 __EnableLSan=1 ;; @@ -83,22 +76,16 @@ else unset DEBUG_SANITIZERS echo "Setting DEBUG_SANITIZERS=" else - # for now, specify alloc_dealloc_mismatch=0 as there are too many error reports that are not an issue. - # Also specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers - ASAN_OPTIONS="symbolize=1 alloc_dealloc_mismatch=0 use_sigaltstack=0" # when Clang 3.8 available, add: suppressions=$(readlink -f sanitizersuppressions.txt) UBSAN_OPTIONS="print_stacktrace=1" - if [[ "$__EnableASan" == 1 ]]; then - __Options="$__Options asan" - fi - if [[ "$__EnableUBSan" == 1 ]]; then + if [ $__EnableUBSan == 1 ]; then __Options="$__Options ubsan" fi - if [[ "$__EnableLSan" == 1 ]]; then - ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=1" + if [ $__EnableLSan == 1 ]; then + LSAN_OPTIONS="detect_leaks=1" else - ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=0" + LSAN_OPTIONS="detect_leaks=0" fi # passed to build.sh @@ -128,7 +115,6 @@ else unset __ClangMajorVersion unset __ClangMinorVersion - unset __EnableASan unset __EnableUBSan unset __EnableLSan unset __TurnOff diff --git a/src/coreclr/utilcode/clrhost_nodependencies.cpp b/src/coreclr/utilcode/clrhost_nodependencies.cpp index 7d2fceb3005e1..0531b66607786 100644 --- a/src/coreclr/utilcode/clrhost_nodependencies.cpp +++ b/src/coreclr/utilcode/clrhost_nodependencies.cpp @@ -12,6 +12,11 @@ #include "clrnt.h" #include "contract.h" +#if defined(_MSC_VER) +# if defined(__SANITIZE_ADDRESS__) +# define HAS_ADDRESS_SANITIZER +# endif +#endif #if defined __llvm__ # if defined(__has_feature) && __has_feature(address_sanitizer) # define HAS_ADDRESS_SANITIZER @@ -38,10 +43,6 @@ void DisableThrowCheck() dbg_fDisableThrowCheck = TRUE; } -#ifdef HAS_ADDRESS_SANITIZER -// use the functionality from address santizier (which does not throw exceptions) -#else - #define CLRThrowsExceptionWorker() RealCLRThrowsExceptionWorker(__FUNCTION__, __FILE__, __LINE__) static void RealCLRThrowsExceptionWorker(_In_z_ const char *szFunction, @@ -58,7 +59,6 @@ static void RealCLRThrowsExceptionWorker(_In_z_ const char *szFunction, CONTRACT_THROWSEX(szFunction, szFile, lineNum); } -#endif // HAS_ADDRESS_SANITIZER #endif //_DEBUG_IMPL #if defined(_DEBUG_IMPL) && defined(ENABLE_CONTRACTS_IMPL) diff --git a/src/native/common/asanoptions.c b/src/native/common/asanoptions.c new file mode 100644 index 0000000000000..d4585ba5ae3c4 --- /dev/null +++ b/src/native/common/asanoptions.c @@ -0,0 +1,8 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// for now, specify alloc_dealloc_mismatch=0 as there are too many error reports that are not an issue. +// Also specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers +const char *__asan_default_options() { + return "symbolize=1:alloc_dealloc_mismatch=0:use_sigaltstack=0"; +} From b3e53395de4d8a9bbf2bfb806649fa24e233ab6b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 22 Jun 2021 11:55:16 -0700 Subject: [PATCH 02/28] Remove /RTC1 from the linker flags as well. --- eng/native/configurecompiler.cmake | 2 ++ 1 file changed, 2 insertions(+) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index ded28787084b0..6a1f762bda072 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -110,6 +110,8 @@ if (MSVC) # /RTC1 is added by default by CMake, so remove it. string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") string(REPLACE "/RTC1" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") + string(REPLACE "/RTC1" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") + string(REPLACE "/RTC1" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") add_compile_options($<$,$>:-fsanitize=address>) add_linker_flag(/INFERASANLIBS DEBUG CHECKED) From 5f2a39e60f902a7926418a26fff6991108cbc4a1 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 22 Jun 2021 14:35:21 -0700 Subject: [PATCH 03/28] Get native code building (barring the VS support lib) --- eng/native/configurecompiler.cmake | 13 +++++++------ eng/native/ijw/IJW.cmake | 5 ----- src/coreclr/dlls/clretwrc/CMakeLists.txt | 4 ++++ src/coreclr/dlls/mscorrc/CMakeLists.txt | 4 ++++ 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 6a1f762bda072..a720bac7e92f6 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -108,12 +108,13 @@ if (MSVC) # Configure ASAN for MSVC # /RTC1 is added by default by CMake, so remove it. - string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - string(REPLACE "/RTC1" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}") - string(REPLACE "/RTC1" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") - string(REPLACE "/RTC1" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") + string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") + string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") + string(REPLACE "/RTC1" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}") + string(REPLACE "/RTC1" "" CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}") - add_compile_options($<$,$>:-fsanitize=address>) + add_compile_options($<$,$,$>>:-fsanitize=address>) + add_compile_options($<$,$,$>>:-fno-sanitize-address-vcasan-lib>) add_linker_flag(/INFERASANLIBS DEBUG CHECKED) elseif (CLR_CMAKE_HOST_UNIX) @@ -124,7 +125,7 @@ elseif (CLR_CMAKE_HOST_UNIX) string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE) # Configure ASAN for Clang/GCC - add_compile_options($<$,$,$>>:-fsanitize=address>) + add_compile_options($<$,$>:-fsanitize=address>) add_linker_flag(-fsanitize=address DEBUG CHECKED) set(CLR_SANITIZE_CXX_OPTIONS "") diff --git a/eng/native/ijw/IJW.cmake b/eng/native/ijw/IJW.cmake index 3de8a673c8be0..3055820ebd9c7 100644 --- a/eng/native/ijw/IJW.cmake +++ b/eng/native/ijw/IJW.cmake @@ -40,11 +40,6 @@ if (CLR_CMAKE_HOST_WIN32) # IJW requires the CRT as a dll, not linked in set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$,$>:Debug>DLL) - # CMake enables /RTC1 and /EHsc by default, but they're not compatible with /clr, so remove them - if(CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1") - string(REPLACE "/RTC1" " " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") - endif() - remove_ijw_incompatible_options("${CMAKE_CXX_FLAGS}" CMAKE_CXX_FLAGS) set(CLR_SDK_REF_PACK_OUTPUT "") diff --git a/src/coreclr/dlls/clretwrc/CMakeLists.txt b/src/coreclr/dlls/clretwrc/CMakeLists.txt index 2b749fd7b3736..e557c5ccb2265 100644 --- a/src/coreclr/dlls/clretwrc/CMakeLists.txt +++ b/src/coreclr/dlls/clretwrc/CMakeLists.txt @@ -14,6 +14,10 @@ if(CLR_CMAKE_HOST_WIN32) string(REPLACE "/CETCOMPAT" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) endif (CLR_CMAKE_HOST_ARCH_AMD64) + # remove asan from resource-only libraries + string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}) + string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_CHECKED ${CMAKE_SHARED_LINKER_FLAGS_CHECKED}) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NOENTRY") endif(CLR_CMAKE_HOST_WIN32) diff --git a/src/coreclr/dlls/mscorrc/CMakeLists.txt b/src/coreclr/dlls/mscorrc/CMakeLists.txt index 8011997c1e55f..71d95d938d53d 100644 --- a/src/coreclr/dlls/mscorrc/CMakeLists.txt +++ b/src/coreclr/dlls/mscorrc/CMakeLists.txt @@ -15,6 +15,10 @@ if(CLR_CMAKE_HOST_WIN32) string(REPLACE "/CETCOMPAT" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) endif (CLR_CMAKE_HOST_ARCH_AMD64) + # remove asan from resource-only libraries + string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}) + string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_CHECKED ${CMAKE_SHARED_LINKER_FLAGS_CHECKED}) + add_library_clr(mscorrc SHARED include.rc ) From 33a7348574ab96038d0e4750f5c93c9263f37f4f Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 23 Jun 2021 13:20:10 -0700 Subject: [PATCH 04/28] Fix a few issues with ASAN and set up the asan options file correctly. --- eng/native/functions.cmake | 8 ++++++-- src/coreclr/inc/utilcode.h | 14 ++++++++++++++ src/coreclr/md/runtime/metamodel.cpp | 2 +- src/coreclr/utilcode/clrhost_nodependencies.cpp | 11 ----------- .../common/{asanoptions.c => asanoptions.cpp} | 0 5 files changed, 21 insertions(+), 14 deletions(-) rename src/native/common/{asanoptions.c => asanoptions.cpp} (100%) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 7a43c2327d760..ca893f00a0bf8 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -603,8 +603,8 @@ function(link_natvis_sources_for_target targetName linkKind) endforeach() endfunction() -function(add_sanitizer_options targetName) - target_sources(${targetName} "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.c>") +function(add_sanitizer_options_file targetName) + target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.cpp>") endfunction() function(add_executable_clr targetName) @@ -614,6 +614,7 @@ function(add_executable_clr targetName) else() add_executable(${ARGV}) endif(NOT WIN32) + add_sanitizer_options_file(${targetName}) if(NOT CLR_CMAKE_KEEP_NATIVE_SYMBOLS) strip_symbols(${ARGV0} symbolFile) endif() @@ -625,6 +626,9 @@ function(add_library_clr targetName kind) else() add_library(${ARGV}) endif() + if("${kind}" STREQUAL "SHARED") + add_sanitizer_options_file(${targetName}) + endif() if("${kind}" STREQUAL "SHARED" AND NOT CLR_CMAKE_KEEP_NATIVE_SYMBOLS) strip_symbols(${ARGV0} symbolFile) endif() diff --git a/src/coreclr/inc/utilcode.h b/src/coreclr/inc/utilcode.h index 52ecef889327f..eb5c3298666ed 100644 --- a/src/coreclr/inc/utilcode.h +++ b/src/coreclr/inc/utilcode.h @@ -178,6 +178,20 @@ typedef LPSTR LPUTF8; #define sizeofmember(c,m) (sizeof(((c*)0)->m)) #endif + +#if defined(_MSC_VER) +# if defined(__SANITIZE_ADDRESS__) +# define HAS_ADDRESS_SANITIZER +# define DISABLE_ASAN __declspec(no_sanitize_address) +# endif +#endif +#if defined __llvm__ +# if defined(__has_feature) && __has_feature(address_sanitizer) +# define HAS_ADDRESS_SANITIZER +# define DISABLE_ASAN __attribute__((no_sanitize("address"))) +# endif +#endif + //=--------------------------------------------------------------------------= // Prefast helpers. // diff --git a/src/coreclr/md/runtime/metamodel.cpp b/src/coreclr/md/runtime/metamodel.cpp index b13ee6a736aaf..4285c564455ea 100644 --- a/src/coreclr/md/runtime/metamodel.cpp +++ b/src/coreclr/md/runtime/metamodel.cpp @@ -1193,7 +1193,7 @@ CMiniMdBase::FindSharedColDefs( // Determines where the Table Def's Column Definitions used shared memory or // allocated memory //***************************************************************************** -BOOL +BOOL DISABLE_ASAN CMiniMdBase::UsesAllocatedMemory( CMiniColDef *pCols) { diff --git a/src/coreclr/utilcode/clrhost_nodependencies.cpp b/src/coreclr/utilcode/clrhost_nodependencies.cpp index 0531b66607786..104cb0115044f 100644 --- a/src/coreclr/utilcode/clrhost_nodependencies.cpp +++ b/src/coreclr/utilcode/clrhost_nodependencies.cpp @@ -12,17 +12,6 @@ #include "clrnt.h" #include "contract.h" -#if defined(_MSC_VER) -# if defined(__SANITIZE_ADDRESS__) -# define HAS_ADDRESS_SANITIZER -# endif -#endif -#if defined __llvm__ -# if defined(__has_feature) && __has_feature(address_sanitizer) -# define HAS_ADDRESS_SANITIZER -# endif -#endif - #ifdef _DEBUG_IMPL // diff --git a/src/native/common/asanoptions.c b/src/native/common/asanoptions.cpp similarity index 100% rename from src/native/common/asanoptions.c rename to src/native/common/asanoptions.cpp From 11820a92d01d7e2581e87d6227b39d37e8d60ee9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 11:41:28 -0700 Subject: [PATCH 05/28] Provide explicit no-sanitizer variants of standalone jits so running crossgen2 on LKG dotnet still works. Switch all target_link_libraries to the explicit form so we can use the explicit form in functions.cmake. --- eng/native/configurecompiler.cmake | 8 -- eng/native/functions.cmake | 88 +++++++++++-------- src/coreclr/CMakeLists.txt | 2 + src/coreclr/debug/createdump/CMakeLists.txt | 2 + src/coreclr/dlls/dbgshim/CMakeLists.txt | 2 +- src/coreclr/dlls/mscordbi/CMakeLists.txt | 4 +- src/coreclr/dlls/mscorrc/CMakeLists.txt | 2 + src/coreclr/gc/CMakeLists.txt | 2 +- src/coreclr/gc/sample/CMakeLists.txt | 2 +- src/coreclr/gcinfo/CMakeLists.txt | 18 ++++ src/coreclr/hosts/corerun/CMakeLists.txt | 7 +- src/coreclr/hosts/coreshim/CMakeLists.txt | 1 + src/coreclr/ilasm/CMakeLists.txt | 2 + src/coreclr/ildasm/exe/CMakeLists.txt | 2 + src/coreclr/jit/CMakeLists.txt | 76 +++++++++------- src/coreclr/nativeresources/CMakeLists.txt | 9 ++ src/coreclr/pal/src/CMakeLists.txt | 58 +++++++++++- .../lttngprovider/CMakeLists.txt | 1 + src/coreclr/pal/tests/palsuite/CMakeLists.txt | 1 + .../palsuite/eventprovider/CMakeLists.txt | 2 +- .../pal_sxs/test1/CMakeLists.txt | 3 + src/coreclr/palrt/CMakeLists.txt | 8 ++ .../tools/GenClrDebugResource/CMakeLists.txt | 1 + .../tools/InjectResource/CMakeLists.txt | 1 + .../tools/aot/jitinterface/CMakeLists.txt | 2 + src/coreclr/utilcode/CMakeLists.txt | 13 ++- src/mono/dlls/dbgshim/CMakeLists.txt | 2 +- src/mono/dlls/mscordbi/CMakeLists.txt | 2 +- .../apphost/standalone/CMakeLists.txt | 2 +- .../corehost/apphost/static/CMakeLists.txt | 1 + src/native/corehost/comhost/CMakeLists.txt | 4 +- src/native/corehost/common.cmake | 8 +- .../corehost/fxr/standalone/CMakeLists.txt | 4 +- .../hostpolicy/standalone/CMakeLists.txt | 2 +- src/native/corehost/ijwhost/CMakeLists.txt | 2 +- .../corehost/test/comsxs/CMakeLists.txt | 2 +- .../corehost/test/fx_ver/CMakeLists.txt | 2 +- .../corehost/test/nativehost/CMakeLists.txt | 8 +- .../CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + src/native/libs/System.Native/CMakeLists.txt | 1 + .../System.Net.Security.Native/CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + 45 files changed, 253 insertions(+), 110 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index a720bac7e92f6..16fb0aa7d19d0 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -113,10 +113,6 @@ if (MSVC) string(REPLACE "/RTC1" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG}") string(REPLACE "/RTC1" "" CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG}") - add_compile_options($<$,$,$>>:-fsanitize=address>) - add_compile_options($<$,$,$>>:-fno-sanitize-address-vcasan-lib>) - add_linker_flag(/INFERASANLIBS DEBUG CHECKED) - elseif (CLR_CMAKE_HOST_UNIX) # Set the values to display when interactively configuring CMAKE_BUILD_TYPE set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "DEBUG;CHECKED;RELEASE;RELWITHDEBINFO") @@ -124,10 +120,6 @@ elseif (CLR_CMAKE_HOST_UNIX) # Use uppercase CMAKE_BUILD_TYPE for the string comparisons below string(TOUPPER ${CMAKE_BUILD_TYPE} UPPERCASE_CMAKE_BUILD_TYPE) - # Configure ASAN for Clang/GCC - add_compile_options($<$,$>:-fsanitize=address>) - add_linker_flag(-fsanitize=address DEBUG CHECKED) - set(CLR_SANITIZE_CXX_OPTIONS "") set(CLR_SANITIZE_LINK_OPTIONS "") diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index ca893f00a0bf8..9c187f54b07b0 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -586,50 +586,64 @@ function(add_linker_flag Flag) endfunction() function(link_natvis_sources_for_target targetName linkKind) - if (NOT CLR_CMAKE_HOST_WIN32) - return() + if (NOT CLR_CMAKE_HOST_WIN32) + return() + endif() + foreach(source ${ARGN}) + if (NOT IS_ABSOLUTE "${source}") + convert_to_absolute_path(source ${source}) endif() - foreach(source ${ARGN}) - if (NOT IS_ABSOLUTE "${source}") - convert_to_absolute_path(source ${source}) - endif() - get_filename_component(extension "${source}" EXT) - if ("${extension}" STREQUAL ".natvis") - message("Embedding natvis ${source}") - # Since natvis embedding is only supported on Windows - # we can use target_link_options since our minimum version is high enough - target_link_options(${targetName} "${linkKind}" "-NATVIS:${source}") - endif() - endforeach() + get_filename_component(extension "${source}" EXT) + if ("${extension}" STREQUAL ".natvis") + # Since natvis embedding is only supported on Windows + # we can use target_link_options since our minimum version is high enough + target_link_options(${targetName} "${linkKind}" "-NATVIS:${source}") + endif() + endforeach() endfunction() -function(add_sanitizer_options_file targetName) - target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.cpp>") +function(add_sanitizers targetName) + if (CLR_CMAKE_ENABLE_ASAN) + # TODO Add ASAN flags here + get_target_property(kind "${targetName}" KIND) + if ("${kind}" STREQUAL "SHARED" OR "${kind}" STREQUAL "EXECUTABLE") + target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.cpp>") + endif() + if (MSVC) + target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) + target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fno-sanitize-address-vcasan-lib>) + target_link_libraries("${targetName}" PRIVATE $<$,$>:/INFERASANLIBS>) + add_linker_flag(/INFERASANLIBS DEBUG CHECKED) + else() + target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) + target_link_libraries("${targetName}" PRIVATE $<$,$>:-fsanitize=address>) + endif() + else() + message("ASAN Disabled for target ${targetName}") + endif() endfunction() function(add_executable_clr targetName) - if(NOT WIN32) - add_executable(${ARGV} ${VERSION_FILE_PATH}) - disable_pax_mprotect(${ARGV}) - else() - add_executable(${ARGV}) - endif(NOT WIN32) - add_sanitizer_options_file(${targetName}) - if(NOT CLR_CMAKE_KEEP_NATIVE_SYMBOLS) - strip_symbols(${ARGV0} symbolFile) - endif() + if(NOT WIN32) + add_executable(${ARGV} ${VERSION_FILE_PATH}) + disable_pax_mprotect(${ARGV}) + else() + add_executable(${ARGV}) + endif(NOT WIN32) + add_sanitizers(${targetName}) + if(NOT CLR_CMAKE_KEEP_NATIVE_SYMBOLS) + strip_symbols(${ARGV0} symbolFile) + endif() endfunction() function(add_library_clr targetName kind) - if(NOT WIN32 AND "${kind}" STREQUAL "SHARED") - add_library(${ARGV} ${VERSION_FILE_PATH}) - else() - add_library(${ARGV}) - endif() - if("${kind}" STREQUAL "SHARED") - add_sanitizer_options_file(${targetName}) - endif() - if("${kind}" STREQUAL "SHARED" AND NOT CLR_CMAKE_KEEP_NATIVE_SYMBOLS) - strip_symbols(${ARGV0} symbolFile) - endif() + if(NOT WIN32 AND "${kind}" STREQUAL "SHARED") + add_library(${ARGV} ${VERSION_FILE_PATH}) + else() + add_library(${ARGV}) + endif() + add_sanitizers(${targetName}) + if("${kind}" STREQUAL "SHARED" AND NOT CLR_CMAKE_KEEP_NATIVE_SYMBOLS) + strip_symbols(${ARGV0} symbolFile) + endif() endfunction() diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 7d1ce8d705d3d..d850fb5278d51 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -24,6 +24,8 @@ if(MSVC) set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc. endif (MSVC) +set(CLR_CMAKE_ENABLE_ASAN ON) + # Set commonly used directory names set(CLR_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(VM_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vm) diff --git a/src/coreclr/debug/createdump/CMakeLists.txt b/src/coreclr/debug/createdump/CMakeLists.txt index f0093b7cb6660..e8a4715acdb26 100644 --- a/src/coreclr/debug/createdump/CMakeLists.txt +++ b/src/coreclr/debug/createdump/CMakeLists.txt @@ -24,6 +24,7 @@ if(CLR_CMAKE_HOST_WIN32) ) target_link_libraries(createdump + PRIVATE kernel32.lib ${STATIC_MT_CRT_LIB} advapi32.lib @@ -82,6 +83,7 @@ else() endif(CLR_CMAKE_HOST_OSX) target_link_libraries(createdump + PRIVATE corguids dbgutil # share the PAL in the dac module diff --git a/src/coreclr/dlls/dbgshim/CMakeLists.txt b/src/coreclr/dlls/dbgshim/CMakeLists.txt index 5f323b243399c..6cdd11f3cdab5 100644 --- a/src/coreclr/dlls/dbgshim/CMakeLists.txt +++ b/src/coreclr/dlls/dbgshim/CMakeLists.txt @@ -81,7 +81,7 @@ else() ) endif(CLR_CMAKE_HOST_WIN32) -target_link_libraries(dbgshim ${DBGSHIM_LIBRARIES}) +target_link_libraries(dbgshim PRIVATE ${DBGSHIM_LIBRARIES}) # add the install targets install_clr(TARGETS dbgshim DESTINATIONS . sharedFramework COMPONENT runtime) diff --git a/src/coreclr/dlls/mscordbi/CMakeLists.txt b/src/coreclr/dlls/mscordbi/CMakeLists.txt index 0c3666364372a..b1937ba3bd499 100644 --- a/src/coreclr/dlls/mscordbi/CMakeLists.txt +++ b/src/coreclr/dlls/mscordbi/CMakeLists.txt @@ -87,7 +87,7 @@ if(CLR_CMAKE_HOST_WIN32) ${STATIC_MT_VCRT_LIB} ) - target_link_libraries(mscordbi ${COREDBI_LIBRARIES}) + target_link_libraries(mscordbi PRIVATE ${COREDBI_LIBRARIES}) elseif(CLR_CMAKE_HOST_UNIX) @@ -100,7 +100,7 @@ elseif(CLR_CMAKE_HOST_UNIX) # COREDBI_LIBRARIES is mentioned twice because ld is one pass linker and will not find symbols # if they are defined after they are used. Having all libs twice makes sure that ld will actually # find all symbols. - target_link_libraries(mscordbi ${COREDBI_LIBRARIES} ${COREDBI_LIBRARIES}) + target_link_libraries(mscordbi PRIVATE ${COREDBI_LIBRARIES} ${COREDBI_LIBRARIES}) add_dependencies(mscordbi mscordaccore) diff --git a/src/coreclr/dlls/mscorrc/CMakeLists.txt b/src/coreclr/dlls/mscorrc/CMakeLists.txt index 71d95d938d53d..f4986925c9c0a 100644 --- a/src/coreclr/dlls/mscorrc/CMakeLists.txt +++ b/src/coreclr/dlls/mscorrc/CMakeLists.txt @@ -1,5 +1,7 @@ include_directories("../../pal/prebuilt/corerror") +set(CLR_CMAKE_ENABLE_ASAN OFF) + add_definitions(-DFX_VER_INTERNALNAME_STR=mscorrc.dll) if(CLR_CMAKE_HOST_WIN32) diff --git a/src/coreclr/gc/CMakeLists.txt b/src/coreclr/gc/CMakeLists.txt index a6c4073d0b148..744d13e01a5fb 100644 --- a/src/coreclr/gc/CMakeLists.txt +++ b/src/coreclr/gc/CMakeLists.txt @@ -109,7 +109,7 @@ convert_to_absolute_path(GC_SOURCES ${GC_SOURCES}) add_library_clr(clrgc SHARED ${GC_SOURCES}) add_dependencies(clrgc eventing_headers) -target_link_libraries(clrgc ${GC_LINK_LIBRARIES}) +target_link_libraries(clrgc PRIVATE ${GC_LINK_LIBRARIES}) install_clr(TARGETS clrgc DESTINATIONS . COMPONENT runtime) if(CLR_CMAKE_HOST_UNIX) diff --git a/src/coreclr/gc/sample/CMakeLists.txt b/src/coreclr/gc/sample/CMakeLists.txt index cf8c1694961e8..94a736e8c8126 100644 --- a/src/coreclr/gc/sample/CMakeLists.txt +++ b/src/coreclr/gc/sample/CMakeLists.txt @@ -63,5 +63,5 @@ add_executable_clr(gcsample ) if(CLR_CMAKE_TARGET_WIN32) - target_link_libraries(gcsample ${GC_LINK_LIBRARIES}) + target_link_libraries(gcsample PRIVATE ${GC_LINK_LIBRARIES}) endif() diff --git a/src/coreclr/gcinfo/CMakeLists.txt b/src/coreclr/gcinfo/CMakeLists.txt index 8c966bb3403b5..b8a281aea69d9 100644 --- a/src/coreclr/gcinfo/CMakeLists.txt +++ b/src/coreclr/gcinfo/CMakeLists.txt @@ -81,3 +81,21 @@ create_gcinfo_lib(TARGET gcinfo_win_x86 OS win ARCH x86) if (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) create_gcinfo_lib(TARGET gcinfo_unix_x86 OS unix ARCH x86) endif (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) + +set(CLR_CMAKE_ENABLE_ASAN OFF) + +if (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) + create_gcinfo_lib(TARGET gcinfo_unix_arm64_nosanitizer OS unix ARCH arm64) + create_gcinfo_lib(TARGET gcinfo_unix_x64_nosanitizer OS unix ARCH x64) + create_gcinfo_lib(TARGET gcinfo_win_arm64_nosanitizer OS win ARCH arm64) + create_gcinfo_lib(TARGET gcinfo_win_x64_nosanitizer OS win ARCH x64) +endif (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) + +create_gcinfo_lib(TARGET gcinfo_unix_armel_nosanitizer OS unix ARCH armel) +create_gcinfo_lib(TARGET gcinfo_unix_arm_nosanitizer OS unix ARCH arm) +create_gcinfo_lib(TARGET gcinfo_win_arm_nosanitizer OS win ARCH arm) +create_gcinfo_lib(TARGET gcinfo_win_x86_nosanitizer OS win ARCH x86) + +if (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) + create_gcinfo_lib(TARGET gcinfo_unix_x86_nosanitizer OS unix ARCH x86) +endif (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) \ No newline at end of file diff --git a/src/coreclr/hosts/corerun/CMakeLists.txt b/src/coreclr/hosts/corerun/CMakeLists.txt index c5ce0861651c7..86ae704780d96 100644 --- a/src/coreclr/hosts/corerun/CMakeLists.txt +++ b/src/coreclr/hosts/corerun/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable_clr(corerun if(CLR_CMAKE_HOST_WIN32) target_link_libraries(corerun + PRIVATE advapi32.lib oleaut32.lib uuid.lib @@ -27,13 +28,13 @@ if(CLR_CMAKE_HOST_WIN32) ${STATIC_MT_VCRT_LIB} ) else(CLR_CMAKE_HOST_WIN32) - target_link_libraries(corerun ${CMAKE_DL_LIBS}) + target_link_libraries(corerun PRIVATE ${CMAKE_DL_LIBS}) # Required to expose symbols for global symbol discovery - target_link_libraries(corerun -rdynamic) + target_link_libraries(corerun PRIVATE -rdynamic) # Android implements pthread natively if(NOT CLR_CMAKE_TARGET_ANDROID) - target_link_libraries(corerun pthread) + target_link_libraries(corerun PRIVATE pthread) endif() endif(CLR_CMAKE_HOST_WIN32) diff --git a/src/coreclr/hosts/coreshim/CMakeLists.txt b/src/coreclr/hosts/coreshim/CMakeLists.txt index e25caf7dfa5d4..03d5755c8ee7e 100644 --- a/src/coreclr/hosts/coreshim/CMakeLists.txt +++ b/src/coreclr/hosts/coreshim/CMakeLists.txt @@ -13,6 +13,7 @@ add_library_clr(CoreShim ) target_link_libraries(CoreShim + PRIVATE utilcodestaticnohost advapi32.lib oleaut32.lib diff --git a/src/coreclr/ilasm/CMakeLists.txt b/src/coreclr/ilasm/CMakeLists.txt index cdbe2c84953c3..40d465b0c11d9 100644 --- a/src/coreclr/ilasm/CMakeLists.txt +++ b/src/coreclr/ilasm/CMakeLists.txt @@ -109,6 +109,7 @@ endif(CLR_CMAKE_TARGET_WIN32) if(CLR_CMAKE_HOST_UNIX) target_link_libraries(ilasm + PRIVATE ${ILASM_LINK_LIBRARIES} utilcodestaticnohost mscorrc @@ -118,6 +119,7 @@ if(CLR_CMAKE_HOST_UNIX) ) else() target_link_libraries(ilasm + PRIVATE ${ILASM_LINK_LIBRARIES} ole32 oleaut32 diff --git a/src/coreclr/ildasm/exe/CMakeLists.txt b/src/coreclr/ildasm/exe/CMakeLists.txt index 66bd5aaff9ad2..7cc5ea90b667d 100644 --- a/src/coreclr/ildasm/exe/CMakeLists.txt +++ b/src/coreclr/ildasm/exe/CMakeLists.txt @@ -99,6 +99,7 @@ endif(CLR_CMAKE_HOST_WIN32) if(CLR_CMAKE_HOST_UNIX) target_link_libraries(ildasm + PRIVATE ${ILDASM_LINK_LIBRARIES} mscorrc coreclrpal @@ -107,6 +108,7 @@ if(CLR_CMAKE_HOST_UNIX) ) else() target_link_libraries(ildasm + PRIVATE ${ILDASM_LINK_LIBRARIES} ole32 oleaut32 diff --git a/src/coreclr/jit/CMakeLists.txt b/src/coreclr/jit/CMakeLists.txt index 9a088b2e9a267..b4fa78cb485f4 100644 --- a/src/coreclr/jit/CMakeLists.txt +++ b/src/coreclr/jit/CMakeLists.txt @@ -17,13 +17,18 @@ function(create_standalone_jit) set(multiValueArgs DESTINATIONS) cmake_parse_arguments(TARGETDETAILS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + set(sanitizer_suffix "") + if (NOT CLR_CMAKE_ENABLE_ASAN) + set(sanitizer_suffix "_nosanitizer") + endif() + if(TARGETDETAILS_OS STREQUAL "unix_osx") if (NOT (TARGETDETAILS_ARCH STREQUAL "arm64")) message(FATAL_ERROR "Only arm64 Apple has a special ABI, use just unix for x64 Mac OS." ) endif() - set(JIT_ARCH_LINK_LIBRARIES gcinfo_unix_arm64) + set(JIT_ARCH_LINK_LIBRARIES gcinfo_unix_arm64${sanitizer_suffix}) else() - set(JIT_ARCH_LINK_LIBRARIES gcinfo_${TARGETDETAILS_OS}_${TARGETDETAILS_ARCH}) + set(JIT_ARCH_LINK_LIBRARIES gcinfo_${TARGETDETAILS_OS}_${TARGETDETAILS_ARCH}${sanitizer_suffix}) endif() if(TARGETDETAILS_ARCH STREQUAL "x64") @@ -446,41 +451,41 @@ endif() add_custom_target(jit_exports DEPENDS ${JIT_EXPORTS_FILE}) -set(JIT_LINK_LIBRARIES - utilcodestaticnohost -) - -set(JIT_ARCH_LINK_LIBRARIES - gcinfo -) - -if(CLR_CMAKE_HOST_UNIX) - list(APPEND JIT_LINK_LIBRARIES - mscorrc - coreclrpal - palrt - ) -else() - list(APPEND JIT_LINK_LIBRARIES - ${STATIC_MT_CRT_LIB} - ${STATIC_MT_VCRT_LIB} - kernel32.lib - advapi32.lib - ole32.lib - oleaut32.lib - uuid.lib - user32.lib - version.lib - shlwapi.lib - bcrypt.lib - crypt32.lib - RuntimeObject.lib - ) -endif(CLR_CMAKE_HOST_UNIX) - # Shared function for generating JIT # optional arguments: DESTINATIONS path function(add_jit jitName) + set(sanitizer_suffix "") + if (NOT CLR_CMAKE_ENABLE_ASAN) + set(sanitizer_suffix "_nosanitizer") + endif() + + set(JIT_LINK_LIBRARIES + utilcodestaticnohost${sanitizer_suffix} + ) + + if(CLR_CMAKE_HOST_UNIX) + list(APPEND JIT_LINK_LIBRARIES + mscorrc + coreclrpal${sanitizer_suffix} + palrt${sanitizer_suffix} + ) + else() + list(APPEND JIT_LINK_LIBRARIES + ${STATIC_MT_CRT_LIB} + ${STATIC_MT_VCRT_LIB} + kernel32.lib + advapi32.lib + ole32.lib + oleaut32.lib + uuid.lib + user32.lib + version.lib + shlwapi.lib + bcrypt.lib + crypt32.lib + RuntimeObject.lib + ) + endif(CLR_CMAKE_HOST_UNIX) set_source_files_properties(${JIT_EXPORTS_FILE} PROPERTIES GENERATED TRUE) @@ -521,6 +526,7 @@ function(add_jit jitName) set_property(TARGET ${jitName} APPEND_STRING PROPERTY LINK_DEPENDS ${JIT_EXPORTS_FILE}) target_link_libraries(${jitName} + PRIVATE ${JIT_LINK_LIBRARIES} ${JIT_ARCH_LINK_LIBRARIES} ) @@ -552,6 +558,8 @@ install_clr(TARGETS clrjit DESTINATIONS . sharedFramework COMPONENT jit) # Enable profile guided optimization add_pgo(clrjit) +set(CLR_CMAKE_ENABLE_ASAN OFF) + if (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) create_standalone_jit(TARGET clrjit_universal_arm64_${ARCH_HOST_NAME} OS universal ARCH arm64 DESTINATIONS .) create_standalone_jit(TARGET clrjit_unix_x64_${ARCH_HOST_NAME} OS unix ARCH x64 DESTINATIONS .) diff --git a/src/coreclr/nativeresources/CMakeLists.txt b/src/coreclr/nativeresources/CMakeLists.txt index 6959ca2497ad5..e092f84b9dbcb 100644 --- a/src/coreclr/nativeresources/CMakeLists.txt +++ b/src/coreclr/nativeresources/CMakeLists.txt @@ -6,3 +6,12 @@ add_library_clr(nativeresourcestring ) install_clr (TARGETS nativeresourcestring DESTINATIONS lib) + +set(CLR_CMAKE_ENABLE_ASAN OFF) + +add_library_clr(nativeresourcestring_nosanitizer + STATIC + resourcestring.cpp +) + +install_clr (TARGETS nativeresourcestring_nosanitizer DESTINATIONS lib) diff --git a/src/coreclr/pal/src/CMakeLists.txt b/src/coreclr/pal/src/CMakeLists.txt index be8d4d68b6e74..440462480017d 100644 --- a/src/coreclr/pal/src/CMakeLists.txt +++ b/src/coreclr/pal/src/CMakeLists.txt @@ -273,12 +273,30 @@ if(CLR_CMAKE_TARGET_LINUX) target_sources(tracepointprovider INTERFACE $) endif(CLR_CMAKE_TARGET_LINUX) +# Define a no-sanitizer version of the PAL for our standalone DLLs that are loaded by unsanitized exes. +set (CLR_CMAKE_ENABLE_ASAN OFF) +add_library(coreclrpal_nosanitizer + STATIC + ${SOURCES} + ${ARCH_SOURCES} + ${PLATFORM_SOURCES} + ${LIBUNWIND_OBJECTS} +) + if(CLR_CMAKE_TARGET_OSX) find_library(COREFOUNDATION CoreFoundation) find_library(CORESERVICES CoreServices) find_library(SECURITY Security) find_library(SYSTEM System) target_link_libraries(coreclrpal + PUBLIC + ${COREFOUNDATION} + ${CORESERVICES} + ${SECURITY} + ${SYSTEM} + ) + target_link_libraries(coreclrpal_nosanitizer + PUBLIC ${COREFOUNDATION} ${CORESERVICES} ${SECURITY} @@ -288,6 +306,13 @@ endif(CLR_CMAKE_TARGET_OSX) if(CLR_CMAKE_TARGET_FREEBSD) target_link_libraries(coreclrpal + PUBLIC + pthread + rt + ${UNWIND_LIBS} + ) + target_link_libraries(coreclrpal_nosanitizer + PUBLIC pthread rt ${UNWIND_LIBS} @@ -298,6 +323,13 @@ if(CLR_CMAKE_TARGET_LINUX) # On Android, we don't need to link with gcc_s, pthread and rt if(NOT CLR_CMAKE_TARGET_ANDROID) target_link_libraries(coreclrpal + PUBLIC + gcc_s + pthread + rt + ) + target_link_libraries(coreclrpal_nosanitizer + PUBLIC gcc_s pthread rt @@ -306,14 +338,23 @@ if(CLR_CMAKE_TARGET_LINUX) target_link_libraries(coreclrpal ${ANDROID_GLOB} ${LZMA}) + target_link_libraries(coreclrpal_nosanitizer + ${ANDROID_GLOB} + ${LZMA}) endif(NOT CLR_CMAKE_TARGET_ANDROID) target_link_libraries(coreclrpal + PUBLIC + dl + ) + target_link_libraries(coreclrpal_nosanitizer + PUBLIC dl ) if(CLR_CMAKE_USE_SYSTEM_LIBUNWIND) - target_link_libraries(coreclrpal ${UNWIND_LIBS}) + target_link_libraries(coreclrpal PUBLIC ${UNWIND_LIBS}) + target_link_libraries(coreclrpal_nosanitizer PUBLIC ${UNWIND_LIBS}) endif(CLR_CMAKE_USE_SYSTEM_LIBUNWIND) endif(CLR_CMAKE_TARGET_LINUX) @@ -325,6 +366,14 @@ if(CLR_CMAKE_TARGET_NETBSD) add_definitions(-D_KMEMUSER) find_library(KVM kvm) target_link_libraries(coreclrpal + PUBLIC + pthread + rt + ${UNWIND} + ${KVM} + ) + target_link_libraries(coreclrpal_nosanitizer + PUBLIC pthread rt ${UNWIND} @@ -334,6 +383,12 @@ endif(CLR_CMAKE_TARGET_NETBSD) if(CLR_CMAKE_TARGET_SUNOS) target_link_libraries(coreclrpal + PUBLIC + pthread + rt + ) + target_link_libraries(coreclrpal_nosanitizer + PUBLIC pthread rt ) @@ -345,3 +400,4 @@ endif(FEATURE_EVENT_TRACE) # Install the static PAL library for VS install_clr (TARGETS coreclrpal DESTINATIONS lib) +install_clr (TARGETS coreclrpal_nosanitizer DESTINATIONS lib) diff --git a/src/coreclr/pal/src/eventprovider/lttngprovider/CMakeLists.txt b/src/coreclr/pal/src/eventprovider/lttngprovider/CMakeLists.txt index 8773cd797bcfb..d2562ef8e32dc 100644 --- a/src/coreclr/pal/src/eventprovider/lttngprovider/CMakeLists.txt +++ b/src/coreclr/pal/src/eventprovider/lttngprovider/CMakeLists.txt @@ -63,6 +63,7 @@ add_dependencies(coreclrtraceptprovider generated_eventing_headers) find_library(LTTNG NAMES lttng-ust) target_link_libraries(coreclrtraceptprovider + PUBLIC ${LTTNG} ) diff --git a/src/coreclr/pal/tests/palsuite/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/CMakeLists.txt index f14b2442afa78..5694e436b56cc 100644 --- a/src/coreclr/pal/tests/palsuite/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/CMakeLists.txt @@ -920,6 +920,7 @@ add_executable_clr(paltests add_dependencies(paltests coreclrpal) target_link_libraries(paltests + PUBLIC ${COMMON_TEST_LIBRARIES} ) diff --git a/src/coreclr/pal/tests/palsuite/eventprovider/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/eventprovider/CMakeLists.txt index fdf326edb8378..1546971e22f2e 100644 --- a/src/coreclr/pal/tests/palsuite/eventprovider/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/eventprovider/CMakeLists.txt @@ -37,7 +37,7 @@ if(FEATURE_EVENT_TRACE) endif(CLR_CMAKE_HOST_LINUX) endif(FEATURE_EVENT_TRACE) -target_link_libraries(eventprovidertest ${EVENT_PROVIDER_DEPENDENCIES} coreclrpal) +target_link_libraries(eventprovidertest PUBLIC ${EVENT_PROVIDER_DEPENDENCIES} coreclrpal) add_dependencies(eventprovidertest eventing_headers) install (TARGETS eventprovidertest DESTINATION paltests/eventprovider COMPONENT paltests EXCLUDE_FROM_ALL) diff --git a/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/CMakeLists.txt b/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/CMakeLists.txt index 1df233fc68cd8..3ed017082e5e9 100644 --- a/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/CMakeLists.txt +++ b/src/coreclr/pal/tests/palsuite/exception_handling/pal_sxs/test1/CMakeLists.txt @@ -38,6 +38,7 @@ add_dependencies(paltest_pal_sxs_test1_dll1 ) target_link_libraries(paltest_pal_sxs_test1_dll1 + PUBLIC ${COMMON_TEST_LIBRARIES} m coreclrpal @@ -69,6 +70,7 @@ add_dependencies(paltest_pal_sxs_test1_dll2 ) target_link_libraries(paltest_pal_sxs_test1_dll2 + PUBLIC ${COMMON_TEST_LIBRARIES} ) @@ -84,6 +86,7 @@ add_dependencies(paltest_pal_sxs_test1 ) target_link_libraries(paltest_pal_sxs_test1 + PUBLIC paltest_pal_sxs_test1_dll1 paltest_pal_sxs_test1_dll2 ) diff --git a/src/coreclr/palrt/CMakeLists.txt b/src/coreclr/palrt/CMakeLists.txt index f3042f2875352..024d1a8efc762 100644 --- a/src/coreclr/palrt/CMakeLists.txt +++ b/src/coreclr/palrt/CMakeLists.txt @@ -16,5 +16,13 @@ add_library_clr(palrt ${PALRT_SOURCES} ) +set(CLR_CMAKE_ENABLE_ASAN OFF) + +add_library_clr(palrt_nosanitizer + STATIC + ${PALRT_SOURCES} +) + # Install the static PAL library for VS install_clr(TARGETS palrt DESTINATIONS lib) +install_clr(TARGETS palrt_nosanitizer DESTINATIONS lib) diff --git a/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt b/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt index 2ea56e1ea871d..62e8af333741d 100644 --- a/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt +++ b/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt @@ -2,6 +2,7 @@ set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) add_executable(GenClrDebugResource GenClrDebugResource.cpp) target_link_libraries(GenClrDebugResource + PUBLIC ${STATIC_MT_CRT_LIB} ${STATIC_MT_VCRT_LIB} ) diff --git a/src/coreclr/tools/InjectResource/CMakeLists.txt b/src/coreclr/tools/InjectResource/CMakeLists.txt index 51f2b19349b82..bd3d07081e35f 100644 --- a/src/coreclr/tools/InjectResource/CMakeLists.txt +++ b/src/coreclr/tools/InjectResource/CMakeLists.txt @@ -5,6 +5,7 @@ set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) add_executable(InjectResource InjectResource.cpp) target_link_libraries(InjectResource + PUBLIC ${STATIC_MT_CRT_LIB} ${STATIC_MT_VCRT_LIB} ) diff --git a/src/coreclr/tools/aot/jitinterface/CMakeLists.txt b/src/coreclr/tools/aot/jitinterface/CMakeLists.txt index 0b1cdc33fd591..99702bbf31507 100644 --- a/src/coreclr/tools/aot/jitinterface/CMakeLists.txt +++ b/src/coreclr/tools/aot/jitinterface/CMakeLists.txt @@ -1,5 +1,7 @@ project(jitinterface) +set(CLR_CMAKE_ENABLE_ASAN OFF) + set(NATIVE_SOURCES jithost.cpp jitinterface.cpp diff --git a/src/coreclr/utilcode/CMakeLists.txt b/src/coreclr/utilcode/CMakeLists.txt index 8c57742cb6315..ef0acc9a380dd 100644 --- a/src/coreclr/utilcode/CMakeLists.txt +++ b/src/coreclr/utilcode/CMakeLists.txt @@ -95,9 +95,13 @@ add_library(utilcode INTERFACE) target_sources(utilcode INTERFACE $) add_library_clr(utilcodestaticnohost STATIC ${UTILCODE_STATICNOHOST_SOURCES}) +set(CLR_CMAKE_ENABLE_ASAN OFF) +add_library_clr(utilcodestaticnohost_nosanitizer STATIC ${UTILCODE_STATICNOHOST_SOURCES}) + if(CLR_CMAKE_HOST_UNIX) - target_link_libraries(utilcodestaticnohost nativeresourcestring) - target_link_libraries(utilcode_dac nativeresourcestring) + target_link_libraries(utilcodestaticnohost_nosanitizer PUBLIC nativeresourcestring_nosanitizer) + target_link_libraries(utilcodestaticnohost PUBLIC nativeresourcestring) + target_link_libraries(utilcode_dac PUBLIC nativeresourcestring) target_link_libraries(utilcode INTERFACE nativeresourcestring) add_dependencies(utilcode_dac coreclrpal) add_dependencies(utilcode_obj coreclrpal) @@ -105,8 +109,10 @@ endif(CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_HOST_WIN32) + target_compile_definitions(utilcodestaticnohost_nosanitizer PRIVATE _CRTIMP=) # use static version of crt target_compile_definitions(utilcodestaticnohost PRIVATE _CRTIMP=) # use static version of crt + link_natvis_sources_for_target(utilcodestaticnohost_nosanitizer INTERFACE utilcode.natvis) link_natvis_sources_for_target(utilcodestaticnohost INTERFACE utilcode.natvis) link_natvis_sources_for_target(utilcode_dac INTERFACE utilcode.natvis) link_natvis_sources_for_target(utilcode INTERFACE utilcode.natvis) @@ -115,9 +121,12 @@ endif(CLR_CMAKE_HOST_WIN32) set_target_properties(utilcode_dac PROPERTIES DAC_COMPONENT TRUE) target_compile_definitions(utilcode_dac PRIVATE SELF_NO_HOST) target_compile_definitions(utilcodestaticnohost PRIVATE SELF_NO_HOST) +target_compile_definitions(utilcodestaticnohost_nosanitizer PRIVATE SELF_NO_HOST) add_dependencies(utilcode_dac ${UTILCODE_DEPENDENCIES}) add_dependencies(utilcode_obj ${UTILCODE_DEPENDENCIES}) add_dependencies(utilcodestaticnohost ${UTILCODE_DEPENDENCIES}) +add_dependencies(utilcodestaticnohost_nosanitizer ${UTILCODE_DEPENDENCIES}) target_precompile_headers(utilcode_dac PRIVATE [["stdafx.h"]]) target_precompile_headers(utilcode_obj PRIVATE [["stdafx.h"]]) target_precompile_headers(utilcodestaticnohost PRIVATE [["stdafx.h"]]) +target_precompile_headers(utilcodestaticnohost_nosanitizer PRIVATE [["stdafx.h"]]) diff --git a/src/mono/dlls/dbgshim/CMakeLists.txt b/src/mono/dlls/dbgshim/CMakeLists.txt index f403a0acc778d..adb41b47612d8 100644 --- a/src/mono/dlls/dbgshim/CMakeLists.txt +++ b/src/mono/dlls/dbgshim/CMakeLists.txt @@ -81,5 +81,5 @@ if(CLR_CMAKE_HOST_UNIX) ) endif() -target_link_libraries(dbgshim ${DBGSHIM_LIBRARIES} monoapi) +target_link_libraries(dbgshim PRIVATE ${DBGSHIM_LIBRARIES} monoapi) install(TARGETS dbgshim DESTINATION lib) diff --git a/src/mono/dlls/mscordbi/CMakeLists.txt b/src/mono/dlls/mscordbi/CMakeLists.txt index a056ea03ca5db..c570777dca93a 100644 --- a/src/mono/dlls/mscordbi/CMakeLists.txt +++ b/src/mono/dlls/mscordbi/CMakeLists.txt @@ -152,5 +152,5 @@ if(CLR_CMAKE_HOST_UNIX) ) endif() -target_link_libraries(mscordbi ${COREDBI_LIBRARIES} monoapi) +target_link_libraries(mscordbi PRIVATE ${COREDBI_LIBRARIES} monoapi) install(TARGETS mscordbi DESTINATION lib) diff --git a/src/native/corehost/apphost/standalone/CMakeLists.txt b/src/native/corehost/apphost/standalone/CMakeLists.txt index d7a52a8534f99..2a4c0b82c0f1f 100644 --- a/src/native/corehost/apphost/standalone/CMakeLists.txt +++ b/src/native/corehost/apphost/standalone/CMakeLists.txt @@ -49,5 +49,5 @@ endif() # Specify non-default Windows libs to be used for Arm/Arm64 builds if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)) - target_link_libraries(apphost shell32.lib) + target_link_libraries(apphost PRIVATE shell32.lib) endif() diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index 6ca84101e1b7e..a29ec862e8897 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -232,6 +232,7 @@ set_property(TARGET singlefilehost PROPERTY ENABLE_EXPORTS 1) target_link_libraries( singlefilehost + PRIVATE ${NATIVE_LIBS} ${START_WHOLE_ARCHIVE} diff --git a/src/native/corehost/comhost/CMakeLists.txt b/src/native/corehost/comhost/CMakeLists.txt index 36251ad79598b..9f777bff56997 100644 --- a/src/native/corehost/comhost/CMakeLists.txt +++ b/src/native/corehost/comhost/CMakeLists.txt @@ -38,8 +38,8 @@ if (CLR_CMAKE_TARGET_WIN32) list(APPEND WINLIBS Ole32.lib OleAut32.lib) endif() - target_link_libraries(comhost ${WINLIBS}) + target_link_libraries(comhost PRIVATE ${WINLIBS}) endif() install_with_stripped_symbols(comhost TARGETS corehost) -target_link_libraries(comhost libhostcommon) +target_link_libraries(comhost PRIVATE libhostcommon) diff --git a/src/native/corehost/common.cmake b/src/native/corehost/common.cmake index 717c3b5d89cf2..de14c1e0c853d 100644 --- a/src/native/corehost/common.cmake +++ b/src/native/corehost/common.cmake @@ -38,19 +38,19 @@ function(set_common_libs TargetType) # Libraries used for exe projects if (${TargetType} STREQUAL "exe") if((CLR_CMAKE_TARGET_LINUX OR CLR_CMAKE_TARGET_FREEBSD) AND NOT CLR_CMAKE_TARGET_ANDROID) - target_link_libraries (${DOTNET_PROJECT_NAME} "pthread") + target_link_libraries (${DOTNET_PROJECT_NAME} PRIVATE "pthread") endif() - target_link_libraries (${DOTNET_PROJECT_NAME} ${CMAKE_DL_LIBS}) + target_link_libraries (${DOTNET_PROJECT_NAME} PRIVATE ${CMAKE_DL_LIBS}) endif() if (NOT ${TargetType} STREQUAL "lib-static") # Specify the import library to link against for Arm32 build since the default set is minimal if (CLR_CMAKE_TARGET_ARCH_ARM) if (CLR_CMAKE_TARGET_WIN32) - target_link_libraries(${DOTNET_PROJECT_NAME} shell32.lib advapi32.lib) + target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE shell32.lib advapi32.lib) else() - target_link_libraries(${DOTNET_PROJECT_NAME} atomic.a) + target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE atomic.a) endif() endif() endif() diff --git a/src/native/corehost/fxr/standalone/CMakeLists.txt b/src/native/corehost/fxr/standalone/CMakeLists.txt index 5e30750ab4a60..44f4c82f46cf5 100644 --- a/src/native/corehost/fxr/standalone/CMakeLists.txt +++ b/src/native/corehost/fxr/standalone/CMakeLists.txt @@ -55,8 +55,8 @@ else() install(TARGETS libhostfxr DESTINATION corehost) endif(WIN32) -target_link_libraries(hostfxr libhostcommon) +target_link_libraries(hostfxr PRIVATE libhostcommon) if (CLR_CMAKE_TARGET_ARCH_ARMV6) - target_link_libraries(${DOTNET_PROJECT_NAME} atomic) + target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE atomic) endif() diff --git a/src/native/corehost/hostpolicy/standalone/CMakeLists.txt b/src/native/corehost/hostpolicy/standalone/CMakeLists.txt index a0cbbe87f691d..74bea97c58cc1 100644 --- a/src/native/corehost/hostpolicy/standalone/CMakeLists.txt +++ b/src/native/corehost/hostpolicy/standalone/CMakeLists.txt @@ -33,4 +33,4 @@ if(CLR_CMAKE_HOST_UNIX) endif(CLR_CMAKE_HOST_UNIX) install_with_stripped_symbols(hostpolicy TARGETS corehost) -target_link_libraries(hostpolicy libhostcommon) +target_link_libraries(hostpolicy PRIVATE libhostcommon) diff --git a/src/native/corehost/ijwhost/CMakeLists.txt b/src/native/corehost/ijwhost/CMakeLists.txt index 036ae1d386e4c..6a4fdbb4d4ce1 100644 --- a/src/native/corehost/ijwhost/CMakeLists.txt +++ b/src/native/corehost/ijwhost/CMakeLists.txt @@ -49,7 +49,7 @@ include(../lib.cmake) # Specify non-default Windows libs to be used for Arm/Arm64 builds if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)) - target_link_libraries(ijwhost Ole32.lib) + target_link_libraries(ijwhost PRIVATE Ole32.lib) endif() install_with_stripped_symbols(ijwhost TARGETS corehost) diff --git a/src/native/corehost/test/comsxs/CMakeLists.txt b/src/native/corehost/test/comsxs/CMakeLists.txt index f24ca824215e9..673e7f07bdde0 100644 --- a/src/native/corehost/test/comsxs/CMakeLists.txt +++ b/src/native/corehost/test/comsxs/CMakeLists.txt @@ -13,6 +13,6 @@ set(SOURCES include(../testexe.cmake) -target_link_libraries(comsxs ole32 oleaut32) +target_link_libraries(comsxs PRIVATE ole32 oleaut32) install_with_stripped_symbols(comsxs TARGETS corehost_test) diff --git a/src/native/corehost/test/fx_ver/CMakeLists.txt b/src/native/corehost/test/fx_ver/CMakeLists.txt index e2849e9dddce5..ed39b965966c6 100644 --- a/src/native/corehost/test/fx_ver/CMakeLists.txt +++ b/src/native/corehost/test/fx_ver/CMakeLists.txt @@ -15,4 +15,4 @@ include(${CMAKE_CURRENT_LIST_DIR}/../../hostmisc/hostmisc.cmake) include(../testexe.cmake) -target_link_libraries(${DOTNET_PROJECT_NAME} libhostcommon) +target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE libhostcommon) diff --git a/src/native/corehost/test/nativehost/CMakeLists.txt b/src/native/corehost/test/nativehost/CMakeLists.txt index d8b23ca12ce56..527ab578d3991 100644 --- a/src/native/corehost/test/nativehost/CMakeLists.txt +++ b/src/native/corehost/test/nativehost/CMakeLists.txt @@ -48,17 +48,17 @@ include(../../hostmisc/hostmisc.cmake) include(../testexe.cmake) -target_link_libraries(${DOTNET_PROJECT_NAME} nethost) +target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE nethost) if (CLR_CMAKE_TARGET_WIN32) - target_link_libraries(${DOTNET_PROJECT_NAME} delayimp.lib) + target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE delayimp.lib) endif() # Specify non-default Windows libs to be used for Arm/Arm64 builds if (CLR_CMAKE_TARGET_WIN32 AND (CLR_CMAKE_TARGET_ARCH_ARM OR CLR_CMAKE_TARGET_ARCH_ARM64)) - target_link_libraries(${DOTNET_PROJECT_NAME} Ole32.lib OleAut32.lib) + target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE Ole32.lib OleAut32.lib) endif() if (CLR_CMAKE_TARGET_ARCH_ARMV6) - target_link_libraries(${DOTNET_PROJECT_NAME} atomic) + target_link_libraries(${DOTNET_PROJECT_NAME} PRIVATE atomic) endif() diff --git a/src/native/libs/System.Globalization.Native/CMakeLists.txt b/src/native/libs/System.Globalization.Native/CMakeLists.txt index d140506599692..61b24fd3a319c 100644 --- a/src/native/libs/System.Globalization.Native/CMakeLists.txt +++ b/src/native/libs/System.Globalization.Native/CMakeLists.txt @@ -83,6 +83,7 @@ if (GEN_SHARED_LIB) ) target_link_libraries(System.Globalization.Native + PRIVATE dl ) diff --git a/src/native/libs/System.IO.Compression.Native/CMakeLists.txt b/src/native/libs/System.IO.Compression.Native/CMakeLists.txt index 83259d9ae29db..fb2d00ff2c0cb 100644 --- a/src/native/libs/System.IO.Compression.Native/CMakeLists.txt +++ b/src/native/libs/System.IO.Compression.Native/CMakeLists.txt @@ -45,6 +45,7 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER) ) target_link_libraries(System.IO.Compression.Native + PRIVATE ${NATIVE_LIBS_EXTRA} ) diff --git a/src/native/libs/System.Native/CMakeLists.txt b/src/native/libs/System.Native/CMakeLists.txt index 45bfea5a8e7b6..b6375cd7b30d2 100644 --- a/src/native/libs/System.Native/CMakeLists.txt +++ b/src/native/libs/System.Native/CMakeLists.txt @@ -86,6 +86,7 @@ if (GEN_SHARED_LIB) ) target_link_libraries(System.Native + PRIVATE ${NATIVE_LIBS_EXTRA} ) diff --git a/src/native/libs/System.Net.Security.Native/CMakeLists.txt b/src/native/libs/System.Net.Security.Native/CMakeLists.txt index 6fbe18f76872e..26f4df628c106 100644 --- a/src/native/libs/System.Net.Security.Native/CMakeLists.txt +++ b/src/native/libs/System.Net.Security.Native/CMakeLists.txt @@ -32,6 +32,7 @@ set_target_properties(System.Net.Security.Native-Static PROPERTIES OUTPUT_NAME S if (GEN_SHARED_LIB) target_link_libraries(System.Net.Security.Native + PRIVATE ${NATIVE_LIBS_EXTRA} ) diff --git a/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt index c4136a588f856..5b525d7a033e8 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native.Android/CMakeLists.txt @@ -43,6 +43,7 @@ add_library(System.Security.Cryptography.Native.Android-Static set_target_properties(System.Security.Cryptography.Native.Android-Static PROPERTIES OUTPUT_NAME System.Security.Cryptography.Native.Android CLEAN_DIRECT_OUTPUT 1) target_link_libraries(System.Security.Cryptography.Native.Android + PRIVATE -llog ) diff --git a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt index 77aa423237c35..61758c3dec3bf 100644 --- a/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native.Apple/CMakeLists.txt @@ -71,6 +71,7 @@ set_target_properties(System.Security.Cryptography.Native.Apple-Static PROPERTIE if (GEN_SHARED_LIB) target_link_libraries(System.Security.Cryptography.Native.Apple + PRIVATE ${NATIVE_LIBS_EXTRA} ) diff --git a/src/native/libs/System.Security.Cryptography.Native/CMakeLists.txt b/src/native/libs/System.Security.Cryptography.Native/CMakeLists.txt index a9987063b5416..27bf28c2feb05 100644 --- a/src/native/libs/System.Security.Cryptography.Native/CMakeLists.txt +++ b/src/native/libs/System.Security.Cryptography.Native/CMakeLists.txt @@ -111,6 +111,7 @@ if (GEN_SHARED_LIB) endif() target_link_libraries(System.Security.Cryptography.Native.OpenSsl + PRIVATE ${NATIVE_LIBS_EXTRA} ) endif() From fbc456e69ec85e07cc54808eaaf0a2a3cc123991 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 11:51:45 -0700 Subject: [PATCH 06/28] Define DISABLE_ASAN no-op fallback --- src/coreclr/inc/utilcode.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/coreclr/inc/utilcode.h b/src/coreclr/inc/utilcode.h index eb5c3298666ed..d11bf6efc2f0f 100644 --- a/src/coreclr/inc/utilcode.h +++ b/src/coreclr/inc/utilcode.h @@ -184,12 +184,13 @@ typedef LPSTR LPUTF8; # define HAS_ADDRESS_SANITIZER # define DISABLE_ASAN __declspec(no_sanitize_address) # endif -#endif -#if defined __llvm__ +#elif defined(__llvm__) # if defined(__has_feature) && __has_feature(address_sanitizer) # define HAS_ADDRESS_SANITIZER # define DISABLE_ASAN __attribute__((no_sanitize("address"))) # endif +#else +# define DISABLE_ASAN #endif //=--------------------------------------------------------------------------= From bf287df13c4207149d96b6f648b415bcb40067fe Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 11:52:50 -0700 Subject: [PATCH 07/28] Dont link to object libraries. --- eng/native/functions.cmake | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 9c187f54b07b0..972d9a862d5f5 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -616,7 +616,9 @@ function(add_sanitizers targetName) add_linker_flag(/INFERASANLIBS DEBUG CHECKED) else() target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) - target_link_libraries("${targetName}" PRIVATE $<$,$>:-fsanitize=address>) + if (NOT "${kind}" STREQUAL "OBJECT_LIBRARY") + target_link_libraries("${targetName}" PRIVATE $<$,$>:-fsanitize=address>) + endif() endif() else() message("ASAN Disabled for target ${targetName}") From 34e3b695c482ddff5beb655e63390781d5303536 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 13:31:37 -0700 Subject: [PATCH 08/28] Fix fallbacks --- src/coreclr/inc/utilcode.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/coreclr/inc/utilcode.h b/src/coreclr/inc/utilcode.h index d11bf6efc2f0f..c6eca9a037088 100644 --- a/src/coreclr/inc/utilcode.h +++ b/src/coreclr/inc/utilcode.h @@ -183,11 +183,15 @@ typedef LPSTR LPUTF8; # if defined(__SANITIZE_ADDRESS__) # define HAS_ADDRESS_SANITIZER # define DISABLE_ASAN __declspec(no_sanitize_address) +# else +# define DISABLE_ASAN # endif #elif defined(__llvm__) # if defined(__has_feature) && __has_feature(address_sanitizer) # define HAS_ADDRESS_SANITIZER # define DISABLE_ASAN __attribute__((no_sanitize("address"))) +# else +# define DISABLE_ASAN # endif #else # define DISABLE_ASAN From 50c57b86673f45f02de2d61b2fc700bdef6b923f Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 13:55:19 -0700 Subject: [PATCH 09/28] Fix Windows build. --- eng/native/functions.cmake | 5 +---- src/coreclr/dlls/clretwrc/CMakeLists.txt | 6 ++---- src/coreclr/dlls/mscorrc/CMakeLists.txt | 4 ---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 972d9a862d5f5..0f69589514847 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -612,16 +612,13 @@ function(add_sanitizers targetName) if (MSVC) target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fno-sanitize-address-vcasan-lib>) - target_link_libraries("${targetName}" PRIVATE $<$,$>:/INFERASANLIBS>) - add_linker_flag(/INFERASANLIBS DEBUG CHECKED) + target_link_libraries("${targetName}" PRIVATE $<$,$>:-INFERASANLIBS>) else() target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) if (NOT "${kind}" STREQUAL "OBJECT_LIBRARY") target_link_libraries("${targetName}" PRIVATE $<$,$>:-fsanitize=address>) endif() endif() - else() - message("ASAN Disabled for target ${targetName}") endif() endfunction() diff --git a/src/coreclr/dlls/clretwrc/CMakeLists.txt b/src/coreclr/dlls/clretwrc/CMakeLists.txt index e557c5ccb2265..78ebef24c9e83 100644 --- a/src/coreclr/dlls/clretwrc/CMakeLists.txt +++ b/src/coreclr/dlls/clretwrc/CMakeLists.txt @@ -1,5 +1,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}) +set(CLR_CMAKE_ENABLE_ASAN OFF) + if(CLR_CMAKE_HOST_WIN32) # remove /ltcg from resource-only libraries string(REPLACE "/LTCG" "" CMAKE_SHARED_LINKER_FLAGS_RELEASE ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}) @@ -14,10 +16,6 @@ if(CLR_CMAKE_HOST_WIN32) string(REPLACE "/CETCOMPAT" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) endif (CLR_CMAKE_HOST_ARCH_AMD64) - # remove asan from resource-only libraries - string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}) - string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_CHECKED ${CMAKE_SHARED_LINKER_FLAGS_CHECKED}) - set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NOENTRY") endif(CLR_CMAKE_HOST_WIN32) diff --git a/src/coreclr/dlls/mscorrc/CMakeLists.txt b/src/coreclr/dlls/mscorrc/CMakeLists.txt index f4986925c9c0a..47b8db1fedbd5 100644 --- a/src/coreclr/dlls/mscorrc/CMakeLists.txt +++ b/src/coreclr/dlls/mscorrc/CMakeLists.txt @@ -17,10 +17,6 @@ if(CLR_CMAKE_HOST_WIN32) string(REPLACE "/CETCOMPAT" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}) endif (CLR_CMAKE_HOST_ARCH_AMD64) - # remove asan from resource-only libraries - string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_DEBUG ${CMAKE_SHARED_LINKER_FLAGS_DEBUG}) - string(REPLACE "/INFERASANLIBS" "" CMAKE_SHARED_LINKER_FLAGS_CHECKED ${CMAKE_SHARED_LINKER_FLAGS_CHECKED}) - add_library_clr(mscorrc SHARED include.rc ) From 39784251a0b107fadf40a3ddd5c0f72e24530353 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 13:58:35 -0700 Subject: [PATCH 10/28] Remove TODO --- eng/native/functions.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 0f69589514847..2ba597a8ea8e9 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -604,7 +604,6 @@ endfunction() function(add_sanitizers targetName) if (CLR_CMAKE_ENABLE_ASAN) - # TODO Add ASAN flags here get_target_property(kind "${targetName}" KIND) if ("${kind}" STREQUAL "SHARED" OR "${kind}" STREQUAL "EXECUTABLE") target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.cpp>") From 7f7289a41a1536cbbc0be18730df615f24fb2811 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 17:01:48 -0700 Subject: [PATCH 11/28] ASan is only supported on x86 or x64 of the platforms CoreCLR supports. --- src/coreclr/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index d850fb5278d51..cf147589b425b 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -24,7 +24,12 @@ if(MSVC) set(CMAKE_CXX_STANDARD_LIBRARIES "") # do not link against standard win32 libs i.e. kernel32, uuid, user32, etc. endif (MSVC) -set(CLR_CMAKE_ENABLE_ASAN ON) +# Set up build flags for AddressSanitizer +set (CLR_CMAKE_ENABLE_ASAN OFF) +# ASan is only supported on x86 and x64 today. +if (CLR_CMAKE_HOST_ARCH_AMD64 OR CLR_CMAKE_HOST_ARCH_I386) + set(CLR_CMAKE_ENABLE_ASAN ON) +endif() # Set commonly used directory names set(CLR_DIR ${CMAKE_CURRENT_SOURCE_DIR}) From 6d700476f452f29683cfb0ca69c9862f4562534d Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 24 Jun 2021 19:22:34 -0700 Subject: [PATCH 12/28] Use the same MSVC runtime option Coreclr-build wide, remove extraneous flags, and enable VCASAN. --- eng/native/functions.cmake | 2 -- src/coreclr/debug/dbgutil/CMakeLists.txt | 5 ----- src/coreclr/debug/di/CMakeLists.txt | 3 --- src/coreclr/debug/shim/CMakeLists.txt | 2 -- src/coreclr/dlls/dbgshim/CMakeLists.txt | 2 -- src/coreclr/md/CMakeLists.txt | 4 ---- src/coreclr/tools/GenClrDebugResource/CMakeLists.txt | 1 - src/coreclr/tools/superpmi/mcs/CMakeLists.txt | 7 ++----- .../tools/superpmi/superpmi-shim-collector/CMakeLists.txt | 7 ++----- .../tools/superpmi/superpmi-shim-counter/CMakeLists.txt | 7 ++----- .../tools/superpmi/superpmi-shim-simple/CMakeLists.txt | 7 ++----- src/coreclr/tools/superpmi/superpmi/CMakeLists.txt | 7 ++----- 12 files changed, 10 insertions(+), 44 deletions(-) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 2ba597a8ea8e9..329f62362fe60 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -610,8 +610,6 @@ function(add_sanitizers targetName) endif() if (MSVC) target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) - target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fno-sanitize-address-vcasan-lib>) - target_link_libraries("${targetName}" PRIVATE $<$,$>:-INFERASANLIBS>) else() target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) if (NOT "${kind}" STREQUAL "OBJECT_LIBRARY") diff --git a/src/coreclr/debug/dbgutil/CMakeLists.txt b/src/coreclr/debug/dbgutil/CMakeLists.txt index 91f41404221df..283d0eac44d42 100644 --- a/src/coreclr/debug/dbgutil/CMakeLists.txt +++ b/src/coreclr/debug/dbgutil/CMakeLists.txt @@ -1,10 +1,5 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) -if(CLR_CMAKE_HOST_WIN32) - #use static crt - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) -endif(CLR_CMAKE_HOST_WIN32) - if(CLR_CMAKE_HOST_WIN32 OR CLR_CMAKE_HOST_OSX) include_directories(${CLR_DIR}/inc/llvm) endif(CLR_CMAKE_HOST_WIN32 OR CLR_CMAKE_HOST_OSX) diff --git a/src/coreclr/debug/di/CMakeLists.txt b/src/coreclr/debug/di/CMakeLists.txt index b0272c4d2518d..f501e3d967289 100644 --- a/src/coreclr/debug/di/CMakeLists.txt +++ b/src/coreclr/debug/di/CMakeLists.txt @@ -50,9 +50,6 @@ set(CORDBDI_HEADERS if(CLR_CMAKE_HOST_WIN32) - #use static crt - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) - if (CLR_CMAKE_TARGET_ARCH_AMD64 OR ((CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_ARM) AND NOT DEFINED CLR_CROSS_COMPONENTS_BUILD)) set(CORDBDI_SOURCES_ASM_FILE ${ARCH_SOURCES_DIR}/floatconversion.asm) diff --git a/src/coreclr/debug/shim/CMakeLists.txt b/src/coreclr/debug/shim/CMakeLists.txt index 6e587e082f719..50930c14fdf0b 100644 --- a/src/coreclr/debug/shim/CMakeLists.txt +++ b/src/coreclr/debug/shim/CMakeLists.txt @@ -1,6 +1,4 @@ if(CLR_CMAKE_HOST_WIN32) - #use static crt - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) add_definitions(-DHOST_WINDOWS) endif(CLR_CMAKE_HOST_WIN32) diff --git a/src/coreclr/dlls/dbgshim/CMakeLists.txt b/src/coreclr/dlls/dbgshim/CMakeLists.txt index 6cdd11f3cdab5..4b08428023fb3 100644 --- a/src/coreclr/dlls/dbgshim/CMakeLists.txt +++ b/src/coreclr/dlls/dbgshim/CMakeLists.txt @@ -8,8 +8,6 @@ set(DBGSHIM_SOURCES ) if(CLR_CMAKE_HOST_WIN32) - # Use static crt - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) add_definitions(-DFX_VER_INTERNALNAME_STR=dbgshim.dll) endif(CLR_CMAKE_HOST_WIN32) diff --git a/src/coreclr/md/CMakeLists.txt b/src/coreclr/md/CMakeLists.txt index c820cb19674da..f04170efb12c1 100644 --- a/src/coreclr/md/CMakeLists.txt +++ b/src/coreclr/md/CMakeLists.txt @@ -8,10 +8,6 @@ add_compile_definitions($<$>:FEATURE_METAD add_compile_definitions($<$>:FEATURE_METADATA_RELEASE_MEMORY_ON_REOPEN>) add_compile_definitions($<$>,$>>>:FEATURE_METADATA_VERIFY_LAYOUTS>) -if (CLR_CMAKE_HOST_WIN32) - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$>,$>:Debug>) -endif() - add_subdirectory(compiler) add_subdirectory(runtime) add_subdirectory(enc) diff --git a/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt b/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt index 62e8af333741d..4e414af13b73d 100644 --- a/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt +++ b/src/coreclr/tools/GenClrDebugResource/CMakeLists.txt @@ -1,4 +1,3 @@ -set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) add_executable(GenClrDebugResource GenClrDebugResource.cpp) target_link_libraries(GenClrDebugResource diff --git a/src/coreclr/tools/superpmi/mcs/CMakeLists.txt b/src/coreclr/tools/superpmi/mcs/CMakeLists.txt index 8ce4c19725082..d2ffe6499e943 100644 --- a/src/coreclr/tools/superpmi/mcs/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/mcs/CMakeLists.txt @@ -3,11 +3,6 @@ project(mcs) add_definitions(-DFEATURE_NO_HOST) add_definitions(-DSELF_NO_HOST) -if(CLR_CMAKE_HOST_WIN32) - #use static crt - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) -endif(CLR_CMAKE_HOST_WIN32) - include_directories(.) include_directories(../superpmi-shared) @@ -54,6 +49,7 @@ target_precompile_headers(mcs PRIVATE "$<$:standardpch.h>" if(CLR_CMAKE_HOST_UNIX) target_link_libraries(mcs + PRIVATE utilcodestaticnohost mscorrc coreclrpal @@ -61,6 +57,7 @@ if(CLR_CMAKE_HOST_UNIX) ) else() target_link_libraries(mcs + PRIVATE advapi32.lib ${STATIC_MT_CRT_LIB} ${STATIC_MT_CPP_LIB} diff --git a/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt b/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt index 5090fd411dfc9..b69c3f737591b 100644 --- a/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt +++ b/src/coreclr/tools/superpmi/superpmi-shim-collector/CMakeLists.txt @@ -6,11 +6,6 @@ remove_definitions(-D_UNICODE) add_definitions(-DFEATURE_NO_HOST) add_definitions(-DSELF_NO_HOST) -if(CLR_CMAKE_HOST_WIN32) - #use static crt - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) -endif(CLR_CMAKE_HOST_WIN32) - include_directories(.) include_directories(../superpmi-shared) @@ -48,6 +43,7 @@ target_precompile_headers(superpmi-shim-collector PRIVATE "$<$:standardpc if(CLR_CMAKE_HOST_UNIX) target_link_libraries(superpmi + PRIVATE utilcodestaticnohost mscorrc coreclrpal @@ -57,6 +53,7 @@ if(CLR_CMAKE_HOST_UNIX) ) else() target_link_libraries(superpmi + PRIVATE version.lib advapi32.lib ${STATIC_MT_CRT_LIB} From aef3adeee7b5a83795373efd72bc6fad3de89fa0 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 25 Jun 2021 11:11:04 -0700 Subject: [PATCH 13/28] Remove dac override of CRT flavor. It was causing issues getting VCAsan to link with the Linux DAC. --- eng/native/configurecompiler.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 16fb0aa7d19d0..15a084234e9f4 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -672,7 +672,7 @@ if (MSVC) # For Release builds, we shall dynamically link into uCRT [ucrtbase.dll] (which is pushed down as a Windows Update on downlevel OS) but # wont do the same for debug/checked builds since ucrtbased.dll is not redistributable and Debug/Checked builds are not # production-time scenarios. - set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$,$>,$>>>:Debug>) + set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$,$>:Debug>) add_compile_options($<$:/ZH:SHA_256>) From 212e6ced074297ed7c6e05a695623b9a7410698e Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 25 Jun 2021 18:13:26 -0700 Subject: [PATCH 14/28] Hack together something horrible to correctly fetch the shadow ranges to cooperate with the ASAN AV handler. --- eng/native/functions.cmake | 10 +- src/coreclr/vm/excep.cpp | 9 + src/native/common/asanoptions.cpp | 8 - .../corehost/apphost/static/CMakeLists.txt | 3 + src/native/minipal/asansupport.cpp | 158 ++++++++++++++++++ src/native/minipal/asansupport.h | 10 ++ 6 files changed, 187 insertions(+), 11 deletions(-) delete mode 100644 src/native/common/asanoptions.cpp create mode 100644 src/native/minipal/asansupport.cpp create mode 100644 src/native/minipal/asansupport.h diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 329f62362fe60..66df4026fef4c 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -604,9 +604,13 @@ endfunction() function(add_sanitizers targetName) if (CLR_CMAKE_ENABLE_ASAN) - get_target_property(kind "${targetName}" KIND) - if ("${kind}" STREQUAL "SHARED" OR "${kind}" STREQUAL "EXECUTABLE") - target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asanoptions.cpp>") + get_target_property(kind "${targetName}" TYPE) + message("Kind for target: ${kind} for ${targetName}") + if ("${kind}" STREQUAL "SHARED_LIBRARY" OR "${kind}" STREQUAL "EXECUTABLE") + target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asansupport.cpp>") + if ("${kind}" STREQUAL "EXECUTABLE") + target_compile_definitions(${targetName} PRIVATE ASAN_SUPPORT_EXPOSE_SHADOW) + endif() endif() if (MSVC) target_compile_options("${targetName}" PRIVATE $<$,$,$>>:-fsanitize=address>) diff --git a/src/coreclr/vm/excep.cpp b/src/coreclr/vm/excep.cpp index dd80bf37ec679..374d23093221d 100644 --- a/src/coreclr/vm/excep.cpp +++ b/src/coreclr/vm/excep.cpp @@ -55,6 +55,10 @@ #include "gccover.h" #endif // HAVE_GCCOVER +#ifdef _DEBUG +#include "minipal/asansupport.h" +#endif + #ifndef TARGET_UNIX // Windows uses 64kB as the null-reference area #define NULL_AREA_SIZE (64 * 1024) @@ -7928,6 +7932,11 @@ LONG WINAPI CLRVectoredExceptionHandlerShim(PEXCEPTION_POINTERS pExceptionInfo) } #endif + if (dwCode == STATUS_ACCESS_VIOLATION && isAsanShadowAddress((void*)pExceptionInfo->ExceptionRecord->ExceptionInformation[1])) + { + return EXCEPTION_CONTINUE_SEARCH; + } + if (NtCurrentTeb()->ThreadLocalStoragePointer == NULL) { // Ignore exceptions early during thread startup before the thread is fully initialized by the OS diff --git a/src/native/common/asanoptions.cpp b/src/native/common/asanoptions.cpp deleted file mode 100644 index d4585ba5ae3c4..0000000000000 --- a/src/native/common/asanoptions.cpp +++ /dev/null @@ -1,8 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -// for now, specify alloc_dealloc_mismatch=0 as there are too many error reports that are not an issue. -// Also specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers -const char *__asan_default_options() { - return "symbolize=1:alloc_dealloc_mismatch=0:use_sigaltstack=0"; -} diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index a29ec862e8897..6ceb4e82e9ea8 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -239,3 +239,6 @@ target_link_libraries( ${RUNTIMEINFO_LIB} ${END_WHOLE_ARCHIVE} ) + +target_sources(singlefilehost + PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asansupport.cpp>") diff --git a/src/native/minipal/asansupport.cpp b/src/native/minipal/asansupport.cpp new file mode 100644 index 0000000000000..bd6cff3ecc557 --- /dev/null +++ b/src/native/minipal/asansupport.cpp @@ -0,0 +1,158 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#include "asansupport.h" +#include + +#ifndef HOST_WINDOWS +#include "pal.h" +#else +#include +#endif + + + +#ifdef ASAN_SUPPORT_EXPOSE_SHADOW +// Specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers +extern "C" const char *__asan_default_options() { + return "symbolize=1:use_sigaltstack=0"; +} +namespace __asan +{ + extern uintptr_t kHighMemEnd, kMidMemBeg, kMidMemEnd; +} + +extern "C" __declspec(dllexport) void __cdecl get_asan_shadow_range(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd) +{ + *pHighMemEnd = __asan::kHighMemEnd; + *pMidMemBeg = __asan::kMidMemBeg; + *pMidMemEnd = __asan::kMidMemEnd; +} + +namespace +{ + void initialize_asan_shadow_range() + { + } +} +#else + +namespace __asan +{ + static uintptr_t kHighMemEnd, kMidMemBeg, kMidMemEnd; +} + +namespace +{ + typedef void(*get_asan_shadow_range_ptr)(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd); + void get_asan_shadow_range(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd) + { + static get_asan_shadow_range_ptr get_asan_shadow_range_func = nullptr; + if (!get_asan_shadow_range_func) + { + HMODULE entryHandle = GetModuleHandleW(NULL); + get_asan_shadow_range_func = (get_asan_shadow_range_ptr)GetProcAddress(entryHandle, "get_asan_shadow_range"); + } + get_asan_shadow_range_func(pHighMemEnd, pMidMemBeg, pMidMemEnd); + } + void initialize_asan_shadow_range() + { + static bool initialized = false; + if (initialized) + { + return; + } + get_asan_shadow_range(&__asan::kHighMemEnd, &__asan::kMidMemBeg, &__asan::kMidMemEnd); + initialized = true; + } +} + +#endif + +// Ported from address sanitizer +extern "C" +{ + extern uintptr_t __asan_shadow_memory_dynamic_address; +} + +namespace __asan +{ + const uint32_t SHADOW_SCALE = 3; + +#define SHADOW_OFFSET __asan_shadow_memory_dynamic_address + +#define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) + (SHADOW_OFFSET)) +#define kLowMemBeg 0 +#define kLowMemEnd (SHADOW_OFFSET ? SHADOW_OFFSET - 1 : 0) + +#define kLowShadowBeg SHADOW_OFFSET +#define kLowShadowEnd MEM_TO_SHADOW(kLowMemEnd) + +#define kHighMemBeg (MEM_TO_SHADOW(kHighMemEnd) + 1) + +#define kHighShadowBeg MEM_TO_SHADOW(kHighMemBeg) +#define kHighShadowEnd MEM_TO_SHADOW(kHighMemEnd) + +# define kMidShadowBeg MEM_TO_SHADOW(kMidMemBeg) +# define kMidShadowEnd MEM_TO_SHADOW(kMidMemEnd) + +// With the zero shadow base we can not actually map pages starting from 0. +// This constant is somewhat arbitrary. +#define kZeroBaseShadowStart 0 +#define kZeroBaseMaxShadowStart (1 << 18) + +#define kShadowGapBeg (kLowShadowEnd ? kLowShadowEnd + 1 \ + : kZeroBaseShadowStart) +#define kShadowGapEnd ((kMidMemBeg ? kMidShadowBeg : kHighShadowBeg) - 1) + +#define kShadowGap2Beg (kMidMemBeg ? kMidShadowEnd + 1 : 0) +#define kShadowGap2End (kMidMemBeg ? kMidMemBeg - 1 : 0) + +#define kShadowGap3Beg (kMidMemBeg ? kMidMemEnd + 1 : 0) +#define kShadowGap3End (kMidMemBeg ? kHighShadowBeg - 1 : 0) + + static inline bool AddrIsInLowMem(uintptr_t a) { + return a <= kLowMemEnd; + } + + static inline bool AddrIsInLowShadow(uintptr_t a) { + return a >= kLowShadowBeg && a <= kLowShadowEnd; + } + + static inline bool AddrIsInMidMem(uintptr_t a) { + return kMidMemBeg && a >= kMidMemBeg && a <= kMidMemEnd; + } + + static inline bool AddrIsInMidShadow(uintptr_t a) { + return kMidMemBeg && a >= kMidShadowBeg && a <= kMidShadowEnd; + } + + static inline bool AddrIsInHighMem(uintptr_t a) { + return kHighMemBeg && a >= kHighMemBeg && a <= kHighMemEnd; + } + + static inline bool AddrIsInHighShadow(uintptr_t a) { + return kHighMemBeg && a >= kHighShadowBeg && a <= kHighShadowEnd; + } + + static inline bool AddrIsInShadowGap(uintptr_t a) { + if (kMidMemBeg) { + if (a <= kShadowGapEnd) + return SHADOW_OFFSET == 0 || a >= kShadowGapBeg; + return (a >= kShadowGap2Beg && a <= kShadowGap2End) || + (a >= kShadowGap3Beg && a <= kShadowGap3End); + } + // In zero-based shadow mode we treat addresses near zero as addresses + // in shadow gap as well. + if (SHADOW_OFFSET == 0) + return a <= kShadowGapEnd; + return a >= kShadowGapBeg && a <= kShadowGapEnd; + } +} + +bool WINAPI isAsanShadowAddress(void* address) +{ + initialize_asan_shadow_range(); + uintptr_t ptr = (uintptr_t)address; + return __asan::AddrIsInLowShadow(ptr) || __asan::AddrIsInMidShadow(ptr) || __asan::AddrIsInHighShadow(ptr); +} diff --git a/src/native/minipal/asansupport.h b/src/native/minipal/asansupport.h new file mode 100644 index 0000000000000..1c397a403639b --- /dev/null +++ b/src/native/minipal/asansupport.h @@ -0,0 +1,10 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifdef WIN32 +#include +#else +#include "palrt.h" +#endif + +bool WINAPI isAsanShadowAddress(void* address); From e2addc9c6cabd45077759d2a1ef7c75b084e9529 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 12 Jul 2021 15:01:52 -0700 Subject: [PATCH 15/28] Hook up ASAN support lib on Linux using weak symbols instead of dynamic entry-point loading. --- src/native/minipal/asansupport.cpp | 29 +++++++++++++++++++++++------ src/native/minipal/asansupport.h | 4 +++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/native/minipal/asansupport.cpp b/src/native/minipal/asansupport.cpp index bd6cff3ecc557..21b51148e38f4 100644 --- a/src/native/minipal/asansupport.cpp +++ b/src/native/minipal/asansupport.cpp @@ -1,17 +1,18 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#include "asansupport.h" #include +#include "asansupport.h" #ifndef HOST_WINDOWS -#include "pal.h" +#define HOST_SYMBOL __attribute__((weak)) +#define HOST_SYMBOL_CALLCONV #else #include +#define HOST_SYMBOL __declspec(dllexport) +#define HOST_SYMBOL_CALLCONV __cdecl #endif - - #ifdef ASAN_SUPPORT_EXPOSE_SHADOW // Specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers extern "C" const char *__asan_default_options() { @@ -22,7 +23,7 @@ namespace __asan extern uintptr_t kHighMemEnd, kMidMemBeg, kMidMemEnd; } -extern "C" __declspec(dllexport) void __cdecl get_asan_shadow_range(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd) +extern "C" HOST_SYMBOL void HOST_SYMBOL_CALLCONV get_asan_shadow_range(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd) { *pHighMemEnd = __asan::kHighMemEnd; *pMidMemBeg = __asan::kMidMemBeg; @@ -42,6 +43,7 @@ namespace __asan static uintptr_t kHighMemEnd, kMidMemBeg, kMidMemEnd; } +#ifdef HOST_WINDOWS namespace { typedef void(*get_asan_shadow_range_ptr)(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd); @@ -66,7 +68,22 @@ namespace initialized = true; } } - +#else +extern "C" void __attribute__((weak)) get_asan_shadow_range(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd); +namespace +{ + void initialize_asan_shadow_range() + { + static bool initialized = false; + if (initialized) + { + return; + } + get_asan_shadow_range(&__asan::kHighMemEnd, &__asan::kMidMemBeg, &__asan::kMidMemEnd); + initialized = true; + } +} +#endif #endif // Ported from address sanitizer diff --git a/src/native/minipal/asansupport.h b/src/native/minipal/asansupport.h index 1c397a403639b..3005670d9e5cf 100644 --- a/src/native/minipal/asansupport.h +++ b/src/native/minipal/asansupport.h @@ -4,7 +4,9 @@ #ifdef WIN32 #include #else -#include "palrt.h" +#ifndef WINAPI +#define WINAPI +#endif #endif bool WINAPI isAsanShadowAddress(void* address); From 211f5cd710b6f6cee3472946e47f2a84e851e899 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 12 Jul 2021 16:35:05 -0700 Subject: [PATCH 16/28] Add sanitizers to the single file host. --- src/coreclr/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index cf147589b425b..6024e0e4e02de 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -77,6 +77,7 @@ if(NOT CLR_CROSS_COMPONENTS_BUILD) set(CLR_SINGLE_FILE_HOST_ONLY 1) add_subdirectory(${CLR_SRC_NATIVE_DIR}/corehost/apphost/static Corehost.Static) add_dependencies(runtime singlefilehost) + add_sanitizers(singlefilehost) endif() #------------------------- # Enable C++ EH with SEH From d41d8abe7fc45aeb599de4c4f94eed0781054fd3 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 13 Jul 2021 11:03:25 -0700 Subject: [PATCH 17/28] Try a few fixes to get the Linux build working again. --- src/coreclr/CMakeLists.txt | 1 - src/native/minipal/asansupport.cpp | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/coreclr/CMakeLists.txt b/src/coreclr/CMakeLists.txt index 6024e0e4e02de..cf147589b425b 100644 --- a/src/coreclr/CMakeLists.txt +++ b/src/coreclr/CMakeLists.txt @@ -77,7 +77,6 @@ if(NOT CLR_CROSS_COMPONENTS_BUILD) set(CLR_SINGLE_FILE_HOST_ONLY 1) add_subdirectory(${CLR_SRC_NATIVE_DIR}/corehost/apphost/static Corehost.Static) add_dependencies(runtime singlefilehost) - add_sanitizers(singlefilehost) endif() #------------------------- # Enable C++ EH with SEH diff --git a/src/native/minipal/asansupport.cpp b/src/native/minipal/asansupport.cpp index 21b51148e38f4..adafb39131c78 100644 --- a/src/native/minipal/asansupport.cpp +++ b/src/native/minipal/asansupport.cpp @@ -5,10 +5,12 @@ #include "asansupport.h" #ifndef HOST_WINDOWS +#define WEAK_SYMBOL __attribute__((weak)) #define HOST_SYMBOL __attribute__((weak)) #define HOST_SYMBOL_CALLCONV #else #include +#define WEAK_SYMBOL #define HOST_SYMBOL __declspec(dllexport) #define HOST_SYMBOL_CALLCONV __cdecl #endif @@ -89,7 +91,7 @@ namespace // Ported from address sanitizer extern "C" { - extern uintptr_t __asan_shadow_memory_dynamic_address; + extern WEAK_SYMBOL uintptr_t __asan_shadow_memory_dynamic_address; } namespace __asan From 56da0becaa26ae6f531500ddf68c9ccdaf1d5a19 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 13 Jul 2021 15:37:41 -0700 Subject: [PATCH 18/28] Disable LeakSanitizer for now to get cleaner test logs. --- src/native/minipal/asansupport.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/native/minipal/asansupport.cpp b/src/native/minipal/asansupport.cpp index adafb39131c78..1e38df9aaf4e4 100644 --- a/src/native/minipal/asansupport.cpp +++ b/src/native/minipal/asansupport.cpp @@ -18,7 +18,7 @@ #ifdef ASAN_SUPPORT_EXPOSE_SHADOW // Specify use_sigaltstack=0 as coreclr uses own alternate stack for signal handlers extern "C" const char *__asan_default_options() { - return "symbolize=1:use_sigaltstack=0"; + return "symbolize=1 use_sigaltstack=0 detect_leaks=0"; } namespace __asan { From 40dfadf840ca60eefee800c8696568967271b483 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 11 Oct 2021 14:20:39 -0700 Subject: [PATCH 19/28] Add nosanitizer gcinfo build for arm64/arm32 clrjit. --- src/coreclr/gcinfo/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/coreclr/gcinfo/CMakeLists.txt b/src/coreclr/gcinfo/CMakeLists.txt index b8a281aea69d9..4de9478d1c7ab 100644 --- a/src/coreclr/gcinfo/CMakeLists.txt +++ b/src/coreclr/gcinfo/CMakeLists.txt @@ -84,6 +84,11 @@ endif (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) set(CLR_CMAKE_ENABLE_ASAN OFF) +# For clrjit we need to build an exact targetted gcinfo instead of the universal one +if (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_ARM) + create_gcinfo_lib(TARGET gcinfo_${TARGET_OS_NAME}_${ARCH_TARGET_NAME}_nosanitizer OS ${TARGET_OS_NAME} ARCH ${ARCH_TARGET_NAME}) +endif() + if (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) create_gcinfo_lib(TARGET gcinfo_unix_arm64_nosanitizer OS unix ARCH arm64) create_gcinfo_lib(TARGET gcinfo_unix_x64_nosanitizer OS unix ARCH x64) From 0ea4f851785faaef357f412319daa29d22bdd038 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 11 Oct 2021 15:59:01 -0700 Subject: [PATCH 20/28] Only check if an AV is an asan shadow AV in debug since ASAN is debug-only --- src/coreclr/vm/excep.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/coreclr/vm/excep.cpp b/src/coreclr/vm/excep.cpp index 374d23093221d..bb30e4155f2ba 100644 --- a/src/coreclr/vm/excep.cpp +++ b/src/coreclr/vm/excep.cpp @@ -7932,10 +7932,12 @@ LONG WINAPI CLRVectoredExceptionHandlerShim(PEXCEPTION_POINTERS pExceptionInfo) } #endif +#ifdef DEBUG if (dwCode == STATUS_ACCESS_VIOLATION && isAsanShadowAddress((void*)pExceptionInfo->ExceptionRecord->ExceptionInformation[1])) { return EXCEPTION_CONTINUE_SEARCH; } +#endif if (NtCurrentTeb()->ThreadLocalStoragePointer == NULL) { From df52f7237b40c29e6414e10a1023a507569628a8 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Wed, 20 Oct 2021 09:34:13 -0700 Subject: [PATCH 21/28] Disable ASAN in some methods that poke around in parent frames at return addresses and other areas that ASAN doesn't like. --- src/coreclr/vm/frames.h | 4 ++-- src/coreclr/vm/jithelpers.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/coreclr/vm/frames.h b/src/coreclr/vm/frames.h index 82e57cbbb0401..000e1f5843f8d 100644 --- a/src/coreclr/vm/frames.h +++ b/src/coreclr/vm/frames.h @@ -468,7 +468,7 @@ class Frame : public FrameBase return NULL; } - virtual PCODE GetReturnAddress() + virtual DISABLE_ASAN PCODE GetReturnAddress() { WRAPPER_NO_CONTRACT; TADDR ptr = GetReturnAddressPtr(); @@ -482,7 +482,7 @@ class Frame : public FrameBase return NULL; } - void SetReturnAddress(TADDR val) + void DISABLE_ASAN SetReturnAddress(TADDR val) { WRAPPER_NO_CONTRACT; TADDR ptr = GetReturnAddressPtr(); diff --git a/src/coreclr/vm/jithelpers.cpp b/src/coreclr/vm/jithelpers.cpp index 11b9ff7bf7f5d..221279a4c9a7f 100644 --- a/src/coreclr/vm/jithelpers.cpp +++ b/src/coreclr/vm/jithelpers.cpp @@ -4887,7 +4887,7 @@ HCIMPLEND // Currently, counter is a pointer into the Tier0 method stack // frame so we have exclusive access. -void JIT_Patchpoint(int* counter, int ilOffset) +void DISABLE_ASAN JIT_Patchpoint(int* counter, int ilOffset) { // This method may not return normally STATIC_CONTRACT_GC_NOTRIGGER; From 3f8b19d4a7a35510a18d5412bcea468a42bc24fb Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 1 Nov 2021 13:55:37 -0700 Subject: [PATCH 22/28] Let ASAN know when we won't return so it can do some cleanup on the stack. --- src/coreclr/inc/utilcode.h | 6 ++++++ src/coreclr/vm/excep.cpp | 3 +++ src/coreclr/vm/exceptionhandling.cpp | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/src/coreclr/inc/utilcode.h b/src/coreclr/inc/utilcode.h index c6eca9a037088..696d10f0725c9 100644 --- a/src/coreclr/inc/utilcode.h +++ b/src/coreclr/inc/utilcode.h @@ -197,6 +197,12 @@ typedef LPSTR LPUTF8; # define DISABLE_ASAN #endif +#if defined(HAS_ADDRESS_SANITIZER) +extern "C" void __asan_handle_no_return(); +#else +#define __asan_handle_no_return() +#endif + //=--------------------------------------------------------------------------= // Prefast helpers. // diff --git a/src/coreclr/vm/excep.cpp b/src/coreclr/vm/excep.cpp index bb30e4155f2ba..077d25d41b793 100644 --- a/src/coreclr/vm/excep.cpp +++ b/src/coreclr/vm/excep.cpp @@ -2671,6 +2671,9 @@ HRESULT GetHRFromThrowable(OBJECTREF throwable) VOID DECLSPEC_NORETURN RaiseTheExceptionInternalOnly(OBJECTREF throwable, BOOL rethrow, BOOL fForStackOverflow) { + // Let ASAN that we aren't going to return so it can do some cleanup + __asan_handle_no_return(); + STATIC_CONTRACT_THROWS; STATIC_CONTRACT_GC_TRIGGERS; STATIC_CONTRACT_MODE_COOPERATIVE; diff --git a/src/coreclr/vm/exceptionhandling.cpp b/src/coreclr/vm/exceptionhandling.cpp index 90cac3a5789f6..7dc67709539eb 100644 --- a/src/coreclr/vm/exceptionhandling.cpp +++ b/src/coreclr/vm/exceptionhandling.cpp @@ -4381,6 +4381,9 @@ VOID UnwindManagedExceptionPass2(PAL_SEHException& ex, CONTEXT* unwindStartConte EECodeInfo codeInfo; UINT_PTR establisherFrame = NULL; PVOID handlerData; + + // Let ASAN that we aren't going to return so it can do some cleanup + __asan_handle_no_return(); // Indicate that we are performing second pass. ex.GetExceptionRecord()->ExceptionFlags = EXCEPTION_UNWINDING; @@ -4544,6 +4547,9 @@ VOID DECLSPEC_NORETURN UnwindManagedExceptionPass1(PAL_SEHException& ex, CONTEXT GetThread()->UnhijackThread(); #endif + // Let ASAN that we aren't going to return so it can do some cleanup + __asan_handle_no_return(); + controlPc = GetIP(frameContext); unwindStartContext = *frameContext; @@ -4753,6 +4759,8 @@ VOID DECLSPEC_NORETURN UnwindManagedExceptionPass1(PAL_SEHException& ex, CONTEXT VOID DECLSPEC_NORETURN DispatchManagedException(PAL_SEHException& ex, bool isHardwareException) { + // Let ASAN that we aren't going to return so it can do some cleanup + __asan_handle_no_return(); do { try From f4ca4f9bd9cc9c5a3ad9b63fff56b53802e99e18 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 29 Oct 2021 11:37:25 -0700 Subject: [PATCH 23/28] Add some ASAN suppressions for x86 and clean up some build script changes. --- eng/native/configurecompiler.cmake | 2 -- eng/native/functions.cmake | 1 - src/coreclr/vm/i386/stublinkerx86.cpp | 7 +++++-- src/coreclr/vm/jitinterface.h | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 15a084234e9f4..8af8e294a695a 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -105,8 +105,6 @@ if (MSVC) add_linker_flag(/NODEFAULTLIB:libucrt.lib RELEASE) add_linker_flag(/DEFAULTLIB:ucrt.lib RELEASE) - # Configure ASAN for MSVC - # /RTC1 is added by default by CMake, so remove it. string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}") diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 66df4026fef4c..3f1eb8fe0a38e 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -605,7 +605,6 @@ endfunction() function(add_sanitizers targetName) if (CLR_CMAKE_ENABLE_ASAN) get_target_property(kind "${targetName}" TYPE) - message("Kind for target: ${kind} for ${targetName}") if ("${kind}" STREQUAL "SHARED_LIBRARY" OR "${kind}" STREQUAL "EXECUTABLE") target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asansupport.cpp>") if ("${kind}" STREQUAL "EXECUTABLE") diff --git a/src/coreclr/vm/i386/stublinkerx86.cpp b/src/coreclr/vm/i386/stublinkerx86.cpp index 35750c87f18d2..096ebeb5b208e 100644 --- a/src/coreclr/vm/i386/stublinkerx86.cpp +++ b/src/coreclr/vm/i386/stublinkerx86.cpp @@ -77,7 +77,10 @@ extern "C" VOID __cdecl DebugCheckStubUnwindInfo(); #endif // TARGET_AMD64 #ifdef FEATURE_COMINTEROP -Thread* __stdcall CreateThreadBlockReturnHr(ComMethodFrame *pFrame); +using ThreadPointer = Thread*; +// MSVC doesn't allow a declspec to follow a pointer and be followed by a calling convention, +// so we use a type alias to work around this limitation. +ThreadPointer DISABLE_ASAN __stdcall CreateThreadBlockReturnHr(ComMethodFrame *pFrame); #endif @@ -4904,7 +4907,7 @@ VOID StubLinkerCPU::EmitDebugBreak() // global optimizations. #pragma warning (disable : 4731) #endif // _MSC_VER -Thread* __stdcall CreateThreadBlockReturnHr(ComMethodFrame *pFrame) +ThreadPointer __stdcall CreateThreadBlockReturnHr(ComMethodFrame *pFrame) { WRAPPER_NO_CONTRACT; diff --git a/src/coreclr/vm/jitinterface.h b/src/coreclr/vm/jitinterface.h index 87bab26590f68..a2a486b4fd5db 100644 --- a/src/coreclr/vm/jitinterface.h +++ b/src/coreclr/vm/jitinterface.h @@ -63,7 +63,7 @@ bool SigInfoFlagsAreValid (CORINFO_SIG_INFO *sig) } -void InitJITHelpers1(); +void DISABLE_ASAN InitJITHelpers1(); void InitJITHelpers2(); PCODE UnsafeJitFunction(PrepareCodeConfig* config, From f499ba96f0223941f4ce91e2fc60f904ac2b068e Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 29 Oct 2021 11:51:29 -0700 Subject: [PATCH 24/28] Use a special type alias to handle differences in how uptr is defined across platforms. --- src/native/minipal/asansupport.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/native/minipal/asansupport.cpp b/src/native/minipal/asansupport.cpp index 1e38df9aaf4e4..6bcf235e62d4a 100644 --- a/src/native/minipal/asansupport.cpp +++ b/src/native/minipal/asansupport.cpp @@ -7,10 +7,10 @@ #ifndef HOST_WINDOWS #define WEAK_SYMBOL __attribute__((weak)) #define HOST_SYMBOL __attribute__((weak)) -#define HOST_SYMBOL_CALLCONV +#define HOST_SYMBOL_CALLCONV #else #include -#define WEAK_SYMBOL +#define WEAK_SYMBOL #define HOST_SYMBOL __declspec(dllexport) #define HOST_SYMBOL_CALLCONV __cdecl #endif @@ -20,9 +20,17 @@ extern "C" const char *__asan_default_options() { return "symbolize=1 use_sigaltstack=0 detect_leaks=0"; } + +// We're trying to match C++-mangled names here, so we need to match the exact type that ASAN's uptr type is under the hood to successfully link. +#if defined(HOST_WINDOWS) && defined(HOST_X86) +using asan_uptr = unsigned long; +#else +using asan_uptr = uintptr_t; +#endif + namespace __asan { - extern uintptr_t kHighMemEnd, kMidMemBeg, kMidMemEnd; + extern asan_uptr kHighMemEnd, kMidMemBeg, kMidMemEnd; } extern "C" HOST_SYMBOL void HOST_SYMBOL_CALLCONV get_asan_shadow_range(uintptr_t* pHighMemEnd, uintptr_t* pMidMemBeg, uintptr_t* pMidMemEnd) From d8576aadeaa54a6f8f5cf6bb847f786d94d90a93 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 17 Mar 2022 17:29:34 -0700 Subject: [PATCH 25/28] Change how StressLogAnalyzer builds temporarily to avoid issues with linking against ASAN --- src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt b/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt index 7654f02743551..d781f562ad826 100644 --- a/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt +++ b/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt @@ -1,5 +1,7 @@ set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) -add_executable_clr(StressLogAnalyzer StressLogAnalyzer.cpp StressLogDump.cpp StressLogPlugin.cpp) +# Using add_executable instead of add_executable_clr because changing the MSVC runtime causes issues with ASAN +# TODO: Figure out why it causes problems +add_executable(StressLogAnalyzer StressLogAnalyzer.cpp StressLogDump.cpp StressLogPlugin.cpp) if(CLR_CMAKE_TARGET_WIN32) target_link_libraries(StressLogAnalyzer From ba7837ec54dbf291a78c8e85bf582ab78d1c95b7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 18 Mar 2022 16:32:42 -0700 Subject: [PATCH 26/28] Change install for now for consistency with add_executable. --- src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt b/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt index d781f562ad826..98623dc1e4d61 100644 --- a/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt +++ b/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt @@ -14,4 +14,4 @@ else() ) endif(CLR_CMAKE_TARGET_WIN32) -install_clr(TARGETS StressLogAnalyzer DESTINATIONS . COMPONENT runtime) +install(TARGETS StressLogAnalyzer DESTINATIONS . COMPONENT runtime) From 48e7dfbc5a843beb4937587e46f41b10c5e86f2f Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Mon, 21 Mar 2022 10:26:43 -0700 Subject: [PATCH 27/28] Fix typo --- src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt b/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt index 98623dc1e4d61..1af42d9bb0999 100644 --- a/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt +++ b/src/coreclr/tools/StressLogAnalyzer/CMakeLists.txt @@ -14,4 +14,4 @@ else() ) endif(CLR_CMAKE_TARGET_WIN32) -install(TARGETS StressLogAnalyzer DESTINATIONS . COMPONENT runtime) +install(TARGETS StressLogAnalyzer DESTINATION . COMPONENT runtime) From 2f6079302d552280977388e7df0b54887b9a26d1 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 22 Mar 2022 11:00:39 -0700 Subject: [PATCH 28/28] Fix build from rebase --- eng/native/functions.cmake | 2 +- src/coreclr/gcinfo/CMakeLists.txt | 7 ++----- src/native/corehost/apphost/static/CMakeLists.txt | 2 +- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 3f1eb8fe0a38e..160c1dd12b0ef 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -606,7 +606,7 @@ function(add_sanitizers targetName) if (CLR_CMAKE_ENABLE_ASAN) get_target_property(kind "${targetName}" TYPE) if ("${kind}" STREQUAL "SHARED_LIBRARY" OR "${kind}" STREQUAL "EXECUTABLE") - target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asansupport.cpp>") + target_sources(${targetName} PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/minipal/asansupport.cpp>") if ("${kind}" STREQUAL "EXECUTABLE") target_compile_definitions(${targetName} PRIVATE ASAN_SUPPORT_EXPOSE_SHADOW) endif() diff --git a/src/coreclr/gcinfo/CMakeLists.txt b/src/coreclr/gcinfo/CMakeLists.txt index 4de9478d1c7ab..e262da192cd73 100644 --- a/src/coreclr/gcinfo/CMakeLists.txt +++ b/src/coreclr/gcinfo/CMakeLists.txt @@ -90,15 +90,12 @@ if (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_ARM) endif() if (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) - create_gcinfo_lib(TARGET gcinfo_unix_arm64_nosanitizer OS unix ARCH arm64) + create_gcinfo_lib(TARGET gcinfo_universal_arm64_nosanitizer OS universal ARCH arm64) create_gcinfo_lib(TARGET gcinfo_unix_x64_nosanitizer OS unix ARCH x64) - create_gcinfo_lib(TARGET gcinfo_win_arm64_nosanitizer OS win ARCH arm64) create_gcinfo_lib(TARGET gcinfo_win_x64_nosanitizer OS win ARCH x64) endif (CLR_CMAKE_TARGET_ARCH_ARM64 OR CLR_CMAKE_TARGET_ARCH_AMD64) -create_gcinfo_lib(TARGET gcinfo_unix_armel_nosanitizer OS unix ARCH armel) -create_gcinfo_lib(TARGET gcinfo_unix_arm_nosanitizer OS unix ARCH arm) -create_gcinfo_lib(TARGET gcinfo_win_arm_nosanitizer OS win ARCH arm) +create_gcinfo_lib(TARGET gcinfo_universal_arm_nosanitizer OS universal ARCH arm) create_gcinfo_lib(TARGET gcinfo_win_x86_nosanitizer OS win ARCH x86) if (CLR_CMAKE_TARGET_ARCH_I386 AND CLR_CMAKE_TARGET_UNIX) diff --git a/src/native/corehost/apphost/static/CMakeLists.txt b/src/native/corehost/apphost/static/CMakeLists.txt index 6ceb4e82e9ea8..1d5bf5af4a2dc 100644 --- a/src/native/corehost/apphost/static/CMakeLists.txt +++ b/src/native/corehost/apphost/static/CMakeLists.txt @@ -241,4 +241,4 @@ target_link_libraries( ) target_sources(singlefilehost - PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/common/asansupport.cpp>") + PRIVATE "$<$,$>:${CLR_SRC_NATIVE_DIR}/minipal/asansupport.cpp>")