[compiler-rt] r209441 - [ASan/Win] Thread more library functions from the DLL thunk to the main module

Timur Iskhodzhanov timurrrr at google.com
Thu May 22 06:57:22 PDT 2014


Author: timurrrr
Date: Thu May 22 08:57:22 2014
New Revision: 209441

URL: http://llvm.org/viewvc/llvm-project?rev=209441&view=rev
Log:
[ASan/Win] Thread more library functions from the DLL thunk to the main module

Added:
    compiler-rt/trunk/test/asan/TestCases/Windows/dll_intercept_memcpy.cc   (with props)
Modified:
    compiler-rt/trunk/lib/asan/asan_dll_thunk.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/intercept_memcpy.cc

Modified: compiler-rt/trunk/lib/asan/asan_dll_thunk.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_dll_thunk.cc?rev=209441&r1=209440&r2=209441&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_dll_thunk.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_dll_thunk.cc Thu May 22 08:57:22 2014
@@ -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() {

Added: compiler-rt/trunk/test/asan/TestCases/Windows/dll_intercept_memcpy.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/dll_intercept_memcpy.cc?rev=209441&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/dll_intercept_memcpy.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/dll_intercept_memcpy.cc Thu May 22 08:57:22 2014
@@ -0,0 +1,34 @@
+// 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 %t.dll 2>&1 | cat | FileCheck %s
+
+#include <stdio.h>
+#include <string.h>
+
+void call_memcpy(void* (*f)(void *, const void *, size_t),
+                 void *a, const void *b, size_t c) {
+  f(a, b, c);
+}
+
+extern "C" __declspec(dllexport)
+int test_function() {
+  char buff1[6] = "Hello", buff2[5];
+
+  call_memcpy(&memcpy, buff2, buff1, 5);
+  if (buff1[2] != buff2[2])
+    return 2;
+  printf("Initial test OK\n");
+  fflush(0);
+// CHECK: Initial test OK
+
+  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-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:  test_function {{.*}}dll_intercept_memcpy.cc
+// CHECK: 'buff2' <== Memory access at offset {{.*}} overflows this variable
+}

Propchange: compiler-rt/trunk/test/asan/TestCases/Windows/dll_intercept_memcpy.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: compiler-rt/trunk/test/asan/TestCases/Windows/intercept_memcpy.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/intercept_memcpy.cc?rev=209441&r1=209440&r2=209441&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/intercept_memcpy.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/intercept_memcpy.cc Thu May 22 08:57:22 2014
@@ -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





More information about the llvm-commits mailing list