Skip to content

Fix building the sanitizer instrumentation and hooking it into the hosts #116166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eng/native/configurecompiler.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ if (MSVC)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:CLR_EH_OPTION>>)
add_link_options($<$<BOOL:$<TARGET_PROPERTY:CLR_CONTROL_FLOW_GUARD>>:/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()

Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/nativeaot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ endif()

add_subdirectory(Bootstrap)
add_subdirectory(Runtime)

if (NOT "${ASAN_RUNTIME}" STREQUAL "")
install(FILES ${ASAN_RUNTIME} DESTINATION . COMPONENT nativeaot)
endif()
1 change: 1 addition & 0 deletions src/native/corehost/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/native/minipal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 6 additions & 0 deletions src/native/minipal/sanitizer_exports.def
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions src/native/minipal/sansupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,3 +24,7 @@ charptr_t SANITIZER_CALLBACK_CALLCONV __asan_default_options(void) {

void SANITIZER_CALLBACK_CALLCONV __asan_on_error(void) {
}

#ifdef __cplusplus
}
#endif
9 changes: 2 additions & 7 deletions src/native/minipal/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
2 changes: 1 addition & 1 deletion src/tests/nativeaot/CustomMain/CustomMainNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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.
Expand Down
Loading