Skip to content

Commit

Permalink
This patch is activating the build of Asan on Windows 64-bits.
Browse files Browse the repository at this point in the history
It's fixing compilation errors. The runtime is not yet working.

Missing features:

OverrideFunction for x64
an equiv function for inline asm (atomic_compare_exchange_strong)
shadow memory offset needs to be adjusted
RoundUpToInstrBoundary for x64
They will be implemented by subsequent patches.

Patch by Wei Wang.

Differential revision: http://reviews.llvm.org/D20455

llvm-svn: 271049
  • Loading branch information
bergeret committed May 27, 2016
1 parent 0364f67 commit 00f3f6e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 12 deletions.
6 changes: 2 additions & 4 deletions compiler-rt/cmake/config-ix.cmake
Expand Up @@ -429,15 +429,13 @@ else()
set(COMPILER_RT_HAS_SANITIZER_COMMON FALSE)
endif()

if (COMPILER_RT_HAS_SANITIZER_COMMON AND
(NOT OS_NAME MATCHES "Windows" OR CMAKE_SIZEOF_VOID_P EQUAL 4))
if (COMPILER_RT_HAS_SANITIZER_COMMON)
set(COMPILER_RT_HAS_INTERCEPTION TRUE)
else()
set(COMPILER_RT_HAS_INTERCEPTION FALSE)
endif()

if (COMPILER_RT_HAS_SANITIZER_COMMON AND ASAN_SUPPORTED_ARCH AND
(NOT OS_NAME MATCHES "Windows" OR CMAKE_SIZEOF_VOID_P EQUAL 4))
if (COMPILER_RT_HAS_SANITIZER_COMMON AND ASAN_SUPPORTED_ARCH)
set(COMPILER_RT_HAS_ASAN TRUE)
else()
set(COMPILER_RT_HAS_ASAN FALSE)
Expand Down
17 changes: 17 additions & 0 deletions compiler-rt/lib/asan/asan_win.cc
Expand Up @@ -46,11 +46,20 @@ void __sanitizer_default_free_hook(void *ptr) { }
const char* __asan_default_default_options() { return ""; }
const char* __asan_default_default_suppressions() { return ""; }
void __asan_default_on_error() {}
// 64-bit msvc will not prepend an underscore for symbols.
#ifdef _WIN64
#pragma comment(linker, "/alternatename:__sanitizer_malloc_hook=__sanitizer_default_malloc_hook") // NOLINT
#pragma comment(linker, "/alternatename:__sanitizer_free_hook=__sanitizer_default_free_hook") // NOLINT
#pragma comment(linker, "/alternatename:__asan_default_options=__asan_default_default_options") // NOLINT
#pragma comment(linker, "/alternatename:__asan_default_suppressions=__asan_default_default_suppressions") // NOLINT
#pragma comment(linker, "/alternatename:__asan_on_error=__asan_default_on_error") // NOLINT
#else
#pragma comment(linker, "/alternatename:___sanitizer_malloc_hook=___sanitizer_default_malloc_hook") // NOLINT
#pragma comment(linker, "/alternatename:___sanitizer_free_hook=___sanitizer_default_free_hook") // NOLINT
#pragma comment(linker, "/alternatename:___asan_default_options=___asan_default_default_options") // NOLINT
#pragma comment(linker, "/alternatename:___asan_default_suppressions=___asan_default_default_suppressions") // NOLINT
#pragma comment(linker, "/alternatename:___asan_on_error=___asan_default_on_error") // NOLINT
#endif
// }}}
} // extern "C"

Expand All @@ -61,6 +70,9 @@ INTERCEPTOR_WINAPI(void, RaiseException, void *a, void *b, void *c, void *d) {
REAL(RaiseException)(a, b, c, d);
}

// TODO(wwchrome): Win64 has no _except_handler3/4.
// Need to implement _C_specific_handler instead.
#ifndef _WIN64
INTERCEPTOR(int, _except_handler3, void *a, void *b, void *c, void *d) {
CHECK(REAL(_except_handler3));
__asan_handle_no_return();
Expand All @@ -76,6 +88,7 @@ INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
__asan_handle_no_return();
return REAL(_except_handler4)(a, b, c, d);
}
#endif

static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
AsanThread *t = (AsanThread*)arg;
Expand Down Expand Up @@ -139,8 +152,12 @@ namespace __asan {
void InitializePlatformInterceptors() {
ASAN_INTERCEPT_FUNC(CreateThread);
ASAN_INTERCEPT_FUNC(RaiseException);

// TODO(wwchrome): Win64 uses _C_specific_handler instead.
#ifndef _WIN64
ASAN_INTERCEPT_FUNC(_except_handler3);
ASAN_INTERCEPT_FUNC(_except_handler4);
#endif

// NtWaitForWorkViaWorkerFactory is always linked dynamically.
CHECK(::__interception::OverrideFunction(
Expand Down
17 changes: 14 additions & 3 deletions compiler-rt/lib/interception/interception_win.cc
Expand Up @@ -36,7 +36,7 @@ static void _memcpy(void *dst, void *src, size_t sz) {
}

static void WriteJumpInstruction(char *jmp_from, char *to) {
// jmp XXYYZZWW = E9 WW ZZ YY XX, where XXYYZZWW is an offset fromt jmp_from
// jmp XXYYZZWW = E9 WW ZZ YY XX, where XXYYZZWW is an offset from jmp_from
// to the next instruction to the destination.
ptrdiff_t offset = to - jmp_from - 5;
*jmp_from = '\xE9';
Expand Down Expand Up @@ -68,6 +68,12 @@ static char *GetMemoryForTrampoline(size_t size) {

// Returns 0 on error.
static size_t RoundUpToInstrBoundary(size_t size, char *code) {
#ifdef _WIN64
// TODO(wwchrome): Implement similar logic for x64 instructions.
// Win64 RoundUpToInstrBoundary is not supported yet.
__debugbreak();
return 0;
#else
size_t cursor = 0;
while (cursor < size) {
switch (code[cursor]) {
Expand Down Expand Up @@ -140,12 +146,16 @@ static size_t RoundUpToInstrBoundary(size_t size, char *code) {
}

return cursor;
#endif
}

bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func) {
#ifdef _WIN64
#error OverrideFunction is not yet supported on x64
#endif
// TODO(wwchrome): Implement using x64 jmp.
// OverrideFunction is not yet supported on x64.
__debugbreak();
return false;
#else
// Function overriding works basically like this:
// We write "jmp <new_func>" (5 bytes) at the beginning of the 'old_func'
// to override it.
Expand Down Expand Up @@ -190,6 +200,7 @@ bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func) {
return false; // not clear if this failure bothers us.

return true;
#endif
}

static void **InterestingDLLsAvailable() {
Expand Down
11 changes: 7 additions & 4 deletions compiler-rt/lib/sanitizer_common/sanitizer_atomic_msvc.h
Expand Up @@ -171,12 +171,16 @@ INLINE u32 atomic_exchange(volatile atomic_uint32_t *a,
return (u32)_InterlockedExchange((volatile long*)&a->val_dont_use, v);
}

#ifndef _WIN64

INLINE bool atomic_compare_exchange_strong(volatile atomic_uint8_t *a,
u8 *cmp,
u8 xchgv,
memory_order mo) {
#ifdef _WIN64
// TODO(wwchrome): Implement same functionality without inline asm.
// Inline asm not supported in Win64.
__debugbreak();
return false;
#else
(void)mo;
DCHECK(!((uptr)a % sizeof(*a)));
u8 cmpv = *cmp;
Expand All @@ -192,9 +196,8 @@ INLINE bool atomic_compare_exchange_strong(volatile atomic_uint8_t *a,
return true;
*cmp = prev;
return false;
}

#endif
}

INLINE bool atomic_compare_exchange_strong(volatile atomic_uintptr_t *a,
uptr *cmp,
Expand Down
12 changes: 12 additions & 0 deletions compiler-rt/test/asan/CMakeLists.txt
Expand Up @@ -3,6 +3,12 @@ set(ASAN_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(ASAN_TESTSUITES)
set(ASAN_DYNAMIC_TESTSUITES)

# TODO(wwchrome): Re-enable Win64 asan tests when ready.
# Disable tests for asan Win64 temporarily.
if(NOT OS_NAME MATCHES "Windows" OR CMAKE_SIZEOF_VOID_P EQUAL 8)
set(EXCLUDE_FROM_ALL TRUE)
endif()

macro(get_bits_for_arch arch bits)
if (${arch} MATCHES "i386|i686|arm|mips|mipsel")
set(${bits} 32)
Expand Down Expand Up @@ -102,6 +108,7 @@ set_target_properties(check-asan PROPERTIES FOLDER "ASan tests")
if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
# Add check-dynamic-asan target. It is a part of check-all only on Windows,
# where we want to always test both dynamic and static runtime.

if(NOT OS_NAME MATCHES "Windows")
set(EXCLUDE_FROM_ALL TRUE)
endif()
Expand All @@ -115,3 +122,8 @@ if(COMPILER_RT_ASAN_HAS_STATIC_RUNTIME)
set(EXCLUDE_FROM_ALL FALSE)
endif()
endif()

# TODO(wwchrome): Re-enable the tests for asan Win64 when ready.
if(NOT OS_NAME MATCHES "Windows" OR CMAKE_SIZEOF_VOID_P EQUAL 8)
set(EXCLUDE_FROM_ALL FALSE)
endif()
6 changes: 5 additions & 1 deletion compiler-rt/test/ubsan/CMakeLists.txt
Expand Up @@ -32,7 +32,11 @@ foreach(arch ${UBSAN_TEST_ARCH})
add_ubsan_testsuite("Standalone" ubsan ${arch})

if(COMPILER_RT_HAS_ASAN AND ";${ASAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
add_ubsan_testsuite("AddressSanitizer" asan ${arch})
# TODO(wwchrome): Re-enable ubsan for asan win 64-bit when ready.
# Disable ubsan with AddressSanitizer tests for Windows 64-bit.
if(NOT OS_NAME MATCHES "Windows" OR CMAKE_SIZEOF_VOID_P EQUAL 4)
add_ubsan_testsuite("AddressSanitizer" asan ${arch})
endif()
endif()
if(COMPILER_RT_HAS_MSAN AND ";${MSAN_SUPPORTED_ARCH};" MATCHES ";${arch};")
add_ubsan_testsuite("MemorySanitizer" msan ${arch})
Expand Down

0 comments on commit 00f3f6e

Please sign in to comment.