diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index e8ad8615f6d4bd..a923d5f0811783 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -70,9 +70,9 @@ if (MSVC) add_compile_options($<$:$>) add_link_options($<$>:/guard:cf>) - if (NOT CLR_CMAKE_PGO_INSTRUMENT) + if (NOT CLR_CMAKE_PGO_INSTRUMENT AND NOT CLR_CMAKE_ENABLE_SANITIZERS) # Load all imported DLLs from the System32 directory. - # Don't do this when instrumenting for PGO as a local DLL dependency is introduced by the instrumentation + # Don't do this when instrumenting for PGO and not when a sanitizer is enabled as a local DLL dependency is introduced by the instrumentation add_linker_flag(/DEPENDENTLOADFLAG:0x800) endif() diff --git a/src/coreclr/nativeaot/CMakeLists.txt b/src/coreclr/nativeaot/CMakeLists.txt index e1c43480500970..1eb0ead03da5b1 100644 --- a/src/coreclr/nativeaot/CMakeLists.txt +++ b/src/coreclr/nativeaot/CMakeLists.txt @@ -34,3 +34,7 @@ endif() add_subdirectory(Bootstrap) add_subdirectory(Runtime) + +if (NOT "${ASAN_RUNTIME}" STREQUAL "") + install(FILES ${ASAN_RUNTIME} DESTINATION . COMPONENT nativeaot) +endif() \ No newline at end of file diff --git a/src/native/corehost/CMakeLists.txt b/src/native/corehost/CMakeLists.txt index 5ce72395e49f7f..998836366d4806 100644 --- a/src/native/corehost/CMakeLists.txt +++ b/src/native/corehost/CMakeLists.txt @@ -87,6 +87,7 @@ if ((NOT DEFINED CLR_CMAKE_USE_SYSTEM_RAPIDJSON) OR (NOT CLR_CMAKE_USE_SYSTEM_RA include_directories(${CLR_SRC_NATIVE_DIR}/external/) endif() +add_subdirectory(${CLR_SRC_NATIVE_DIR}/minipal/ minipal) add_subdirectory(hostcommon) add_subdirectory(hostmisc) add_subdirectory(fxr) diff --git a/src/native/minipal/CMakeLists.txt b/src/native/minipal/CMakeLists.txt index 78011d4779694a..ae5ea667ea1ae0 100644 --- a/src/native/minipal/CMakeLists.txt +++ b/src/native/minipal/CMakeLists.txt @@ -42,8 +42,16 @@ if(CLR_CMAKE_HOST_ANDROID) target_link_libraries(minipal PRIVATE log) endif(CLR_CMAKE_HOST_ANDROID) +set(SANITIZER_EXPORTS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/sanitizer_exports.def) + add_library(minipal_sanitizer_support OBJECT sansupport.c) + +set_source_files_properties(sansupport.c PROPERTIES OBJECT_DEPENDS ${SANITIZER_EXPORTS_FILE}) + +if (MSVC) + target_link_options(minipal_sanitizer_support INTERFACE /DEF:${SANITIZER_EXPORTS_FILE}) +endif() # Exclude this target from the default build as we may not have sanitzer headers available # in a non-sanitized build. set_target_properties(minipal_sanitizer_support PROPERTIES EXCLUDE_FROM_ALL ON) diff --git a/src/native/minipal/sanitizer_exports.def b/src/native/minipal/sanitizer_exports.def new file mode 100644 index 00000000000000..c68857ce3f396e --- /dev/null +++ b/src/native/minipal/sanitizer_exports.def @@ -0,0 +1,6 @@ +; Licensed to the .NET Foundation under one or more agreements. +; The .NET Foundation licenses this file to you under the MIT license. + +EXPORTS + __asan_default_options + __asan_on_error \ No newline at end of file diff --git a/src/native/minipal/sansupport.c b/src/native/minipal/sansupport.c index d6474e21791a3b..5ae8a4ce55bad7 100644 --- a/src/native/minipal/sansupport.c +++ b/src/native/minipal/sansupport.c @@ -6,6 +6,10 @@ // Use a typedef here as __declspec + pointer return type causes a parse error in MSVC typedef const char* charptr_t; +#ifdef __cplusplus +extern "C" +{ +#endif charptr_t SANITIZER_CALLBACK_CALLCONV __asan_default_options(void) { // symbolize=1 to get symbolized stack traces @@ -20,3 +24,7 @@ charptr_t SANITIZER_CALLBACK_CALLCONV __asan_default_options(void) { void SANITIZER_CALLBACK_CALLCONV __asan_on_error(void) { } + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/src/native/minipal/utils.h b/src/native/minipal/utils.h index de25c457341e4f..928e461255e624 100644 --- a/src/native/minipal/utils.h +++ b/src/native/minipal/utils.h @@ -63,13 +63,8 @@ #endif #if defined(_MSC_VER) -# ifdef SANITIZER_SHARED_RUNTIME -# define SANITIZER_CALLBACK_CALLCONV __declspec(dllexport no_sanitize_address) __cdecl -# define SANITIZER_INTERFACE_CALLCONV __declspec(dllimport) __cdecl -# else -# define SANITIZER_CALLBACK_CALLCONV __declspec(no_sanitize_address) __cdecl -# define SANITIZER_INTERFACE_CALLCONV __cdecl -# endif +# define SANITIZER_CALLBACK_CALLCONV __declspec(no_sanitize_address) __cdecl +# define SANITIZER_INTERFACE_CALLCONV __cdecl #else # ifdef SANITIZER_SHARED_RUNTIME # define SANITIZER_CALLBACK_CALLCONV __attribute__((no_address_safety_analysis)) __attribute__((visibility("default"))) diff --git a/src/tests/nativeaot/CustomMain/CustomMainNative.cpp b/src/tests/nativeaot/CustomMain/CustomMainNative.cpp index e9bb1c25b9d460..2ab3b6bed8f95e 100644 --- a/src/tests/nativeaot/CustomMain/CustomMainNative.cpp +++ b/src/tests/nativeaot/CustomMain/CustomMainNative.cpp @@ -6,6 +6,7 @@ #ifndef TARGET_WINDOWS #define __stdcall +#define __cdecl #endif #if defined(_WIN32) @@ -27,7 +28,7 @@ int main(int argc, char* argv[]) return __managed__Main(argc, argv); } -extern "C" const char* __stdcall __asan_default_options() +extern "C" const char* __cdecl __asan_default_options() { // NativeAOT is not designed to be unloadable, so we'll leak a few allocations from the shared library. // Disable leak detection as we don't care about these leaks as of now. diff --git a/src/tests/nativeaot/CustomMainWithStubExe/CustomMainWithStubExeNative.cpp b/src/tests/nativeaot/CustomMainWithStubExe/CustomMainWithStubExeNative.cpp index c883eacbcb9fa3..293dd5a623aa24 100644 --- a/src/tests/nativeaot/CustomMainWithStubExe/CustomMainWithStubExeNative.cpp +++ b/src/tests/nativeaot/CustomMainWithStubExe/CustomMainWithStubExeNative.cpp @@ -12,6 +12,7 @@ #ifndef TARGET_WINDOWS #define __stdcall +#define __cdecl #endif // typedef for shared lib exported methods @@ -52,7 +53,7 @@ int main(int argc, char* argv[]) return __managed__MainFunc(argc, argv); } -extern "C" const char* __stdcall __asan_default_options() +extern "C" const char* __cdecl __asan_default_options() { // NativeAOT is not designed to be unloadable, so we'll leak a few allocations from the shared library. // Disable leak detection as we don't care about these leaks as of now. diff --git a/src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.cpp b/src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.cpp index c76b3535b03023..cc03865016a4f9 100644 --- a/src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.cpp +++ b/src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.cpp @@ -11,6 +11,7 @@ #ifndef TARGET_WINDOWS #define __stdcall +#define __cdecl #endif // typedef for shared lib exported methods @@ -83,7 +84,7 @@ int main(int argc, char* argv[]) return 100; } -extern "C" const char* __stdcall __asan_default_options() +extern "C" const char* __cdecl __asan_default_options() { // NativeAOT is not designed to be unloadable, so we'll leak a few allocations from the shared library. // Disable leak detection as we don't care about these leaks as of now.