From b58a59c4e7acfaf4bb91696e25c939654770fc0e Mon Sep 17 00:00:00 2001 From: Timur Iskhodzhanov Date: Thu, 22 May 2014 13:57:22 +0000 Subject: [PATCH] [ASan/Win] Thread more library functions from the DLL thunk to the main module git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@209441 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/asan/asan_dll_thunk.cc | 37 +++++++++++++++++++--- ...intercept_memcpy.cc => dll_intercept_memcpy.cc} | 16 ++++++---- test/asan/TestCases/Windows/intercept_memcpy.cc | 6 ++-- 3 files changed, 45 insertions(+), 14 deletions(-) copy test/asan/TestCases/Windows/{intercept_memcpy.cc => dll_intercept_memcpy.cc} (65%) diff --git a/lib/asan/asan_dll_thunk.cc b/lib/asan/asan_dll_thunk.cc index b7ff6d8aa..48e45d2d7 100644 --- a/lib/asan/asan_dll_thunk.cc +++ b/lib/asan/asan_dll_thunk.cc @@ -80,6 +80,21 @@ struct FunctionInterceptor<0> { // INTERCEPT_HOOKS must be used after the last INTERCEPT_WHEN_POSSIBLE. #define INTERCEPT_HOOKS FunctionInterceptor<__LINE__>::Execute +// We can't define our own version of strlen etc. because that would lead to +// link-time or even type mismatch errors. Instead, we can declare a function +// just to be able to get its address. Me may miss the first few calls to the +// functions since it can be called before __asan_init, but that would lead to +// false negatives in the startup code before user's global initializers, which +// isn't a big deal. +#define INTERCEPT_LIBRARY_FUNCTION(name) \ + extern "C" void name(); \ + INTERCEPT_WHEN_POSSIBLE(WRAPPER_NAME(name), name) + +// Disable compiler warnings that show up if we declare our own version +// of a compiler intrinsic (e.g. strlen). +#pragma warning(disable: 4391) +#pragma warning(disable: 4392) + static void InterceptHooks(); // }}} @@ -284,10 +299,24 @@ WRAP_W_W(_expand_dbg) // TODO(timurrrr): Do we need to add _Crt* stuff here? (see asan_malloc_win.cc). -// strlen is an intrinsic function, so we must specify its exact return and -// parameter types to avoid a compiler error. -extern "C" unsigned strlen(const char *s); -INTERCEPT_WHEN_POSSIBLE(WRAPPER_NAME(strlen), strlen); +INTERCEPT_LIBRARY_FUNCTION(atoi); +INTERCEPT_LIBRARY_FUNCTION(atol); +INTERCEPT_LIBRARY_FUNCTION(memchr); +INTERCEPT_LIBRARY_FUNCTION(memcmp); +INTERCEPT_LIBRARY_FUNCTION(memcpy); +INTERCEPT_LIBRARY_FUNCTION(memmove); +INTERCEPT_LIBRARY_FUNCTION(memset); +INTERCEPT_LIBRARY_FUNCTION(strcat); // NOLINT +INTERCEPT_LIBRARY_FUNCTION(strchr); +INTERCEPT_LIBRARY_FUNCTION(strcmp); +INTERCEPT_LIBRARY_FUNCTION(strcpy); // NOLINT +INTERCEPT_LIBRARY_FUNCTION(strlen); +INTERCEPT_LIBRARY_FUNCTION(strncat); +INTERCEPT_LIBRARY_FUNCTION(strncmp); +INTERCEPT_LIBRARY_FUNCTION(strncpy); +INTERCEPT_LIBRARY_FUNCTION(strnlen); +INTERCEPT_LIBRARY_FUNCTION(strtol); +INTERCEPT_LIBRARY_FUNCTION(wcslen); // Must be at the end of the file due to the way INTERCEPT_HOOKS is defined. void InterceptHooks() { diff --git a/test/asan/TestCases/Windows/intercept_memcpy.cc b/test/asan/TestCases/Windows/dll_intercept_memcpy.cc similarity index 65% copy from test/asan/TestCases/Windows/intercept_memcpy.cc copy to test/asan/TestCases/Windows/dll_intercept_memcpy.cc index 4e52b1a90..ffa3bf9ad 100644 --- a/test/asan/TestCases/Windows/intercept_memcpy.cc +++ b/test/asan/TestCases/Windows/dll_intercept_memcpy.cc @@ -1,6 +1,7 @@ -// RUN: %clangxx_asan -O0 %s -Fe%t +// RUN: %clangxx_asan -O0 %p/dll_host.cc -Fe%t +// RUN: %clangxx_asan -LD -O0 %s -Fe%t.dll // FIXME: 'cat' is needed due to PR19744. -// RUN: not %run %t 2>&1 | cat | FileCheck %s +// RUN: not %run %t %t.dll 2>&1 | cat | FileCheck %s #include #include @@ -10,7 +11,8 @@ void call_memcpy(void* (*f)(void *, const void *, size_t), f(a, b, c); } -int main() { +extern "C" __declspec(dllexport) +int test_function() { char buff1[6] = "Hello", buff2[5]; call_memcpy(&memcpy, buff2, buff1, 5); @@ -23,10 +25,10 @@ int main() { call_memcpy(&memcpy, buff2, buff1, 6); // CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]] // CHECK: WRITE of size 6 at [[ADDR]] thread T0 -// CHECK: __asan_memcpy -// CHECK-NEXT: call_memcpy -// CHECK: main {{.*}}intercept_memcpy.cc:[[@LINE-5]] +// CHECK-NEXT: __asan_memcpy +// CHECK-NEXT: call_memcpy +// CHECK-NEXT: test_function {{.*}}dll_intercept_memcpy.cc:[[@LINE-5]] // CHECK: Address [[ADDR]] is located in stack of thread T0 at offset {{.*}} in frame -// CHECK-NEXT: #0 {{.*}} main +// CHECK-NEXT: test_function {{.*}}dll_intercept_memcpy.cc // CHECK: 'buff2' <== Memory access at offset {{.*}} overflows this variable } diff --git a/test/asan/TestCases/Windows/intercept_memcpy.cc b/test/asan/TestCases/Windows/intercept_memcpy.cc index 4e52b1a90..64f38da6b 100644 --- a/test/asan/TestCases/Windows/intercept_memcpy.cc +++ b/test/asan/TestCases/Windows/intercept_memcpy.cc @@ -23,9 +23,9 @@ int main() { call_memcpy(&memcpy, buff2, buff1, 6); // CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]] // CHECK: WRITE of size 6 at [[ADDR]] thread T0 -// CHECK: __asan_memcpy -// CHECK-NEXT: call_memcpy -// CHECK: main {{.*}}intercept_memcpy.cc:[[@LINE-5]] +// CHECK-NEXT: __asan_memcpy +// CHECK-NEXT: call_memcpy +// CHECK-NEXT: main {{.*}}intercept_memcpy.cc:[[@LINE-5]] // CHECK: Address [[ADDR]] is located in stack of thread T0 at offset {{.*}} in frame // CHECK-NEXT: #0 {{.*}} main // CHECK: 'buff2' <== Memory access at offset {{.*}} overflows this variable -- 2.11.4.GIT