[ASan/Win] Add support for sanitizer allocator hooks, __asan_default_options and...
authorTimur Iskhodzhanov <timurrrr@google.com>
Tue, 24 Feb 2015 17:07:22 +0000 (17:07 +0000)
committerTimur Iskhodzhanov <timurrrr@google.com>
Tue, 24 Feb 2015 17:07:22 +0000 (17:07 +0000)
llvm-svn: 230344

compiler-rt/lib/asan/asan_win.cc
compiler-rt/lib/asan/asan_win_dll_thunk.cc
compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
compiler-rt/test/asan/TestCases/Windows/default_options.cc [new file with mode: 0644]
compiler-rt/test/asan/TestCases/Windows/free_hook_realloc.cc [new file with mode: 0644]
compiler-rt/test/asan/TestCases/Windows/on_error_callback.cc [new file with mode: 0644]
compiler-rt/test/asan/TestCases/default_options.cc
compiler-rt/test/asan/TestCases/free_hook_realloc.cc
compiler-rt/test/asan/TestCases/on_error_callback.cc

index 693f0bc..0ffedb2 100644 (file)
 #include "sanitizer_common/sanitizer_mutex.h"
 
 extern "C" {
-  SANITIZER_INTERFACE_ATTRIBUTE
-  int __asan_should_detect_stack_use_after_return() {
-    __asan_init();
-    return __asan_option_detect_stack_use_after_return;
-  }
+SANITIZER_INTERFACE_ATTRIBUTE
+int __asan_should_detect_stack_use_after_return() {
+  __asan_init();
+  return __asan_option_detect_stack_use_after_return;
 }
 
+// We don't have a direct equivalent of weak symbols when using MSVC, but we can
+// use the /alternatename directive to tell the linker to default a specific
+// symbol to a specific value, which works nicely for allocator hooks and
+// __asan_default_options().
+void __sanitizer_default_malloc_hook(void *ptr, uptr size) { }
+void __sanitizer_default_free_hook(void *ptr) { }
+const char* __asan_default_default_options() { return ""; }
+void __asan_default_on_error() {}
+#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_on_error=___asan_default_on_error")                  // NOLINT
+}  // extern "C"
+
 namespace __asan {
 
 // ---------------------- TSD ---------------- {{{1
index 5d39e33..7b94302 100644 (file)
@@ -304,7 +304,6 @@ INTERFACE_FUNCTION(__sanitizer_cov_module_init)
 INTERFACE_FUNCTION(__sanitizer_cov_trace_basic_block)
 INTERFACE_FUNCTION(__sanitizer_cov_trace_func_enter)
 INTERFACE_FUNCTION(__sanitizer_cov_with_check)
-INTERFACE_FUNCTION(__sanitizer_free_hook)
 INTERFACE_FUNCTION(__sanitizer_get_allocated_size)
 INTERFACE_FUNCTION(__sanitizer_get_coverage_guards)
 INTERFACE_FUNCTION(__sanitizer_get_current_allocated_bytes)
@@ -314,7 +313,6 @@ INTERFACE_FUNCTION(__sanitizer_get_heap_size)
 INTERFACE_FUNCTION(__sanitizer_get_ownership)
 INTERFACE_FUNCTION(__sanitizer_get_total_unique_coverage)
 INTERFACE_FUNCTION(__sanitizer_get_unmapped_bytes)
-INTERFACE_FUNCTION(__sanitizer_malloc_hook)
 INTERFACE_FUNCTION(__sanitizer_maybe_open_cov_file)
 INTERFACE_FUNCTION(__sanitizer_print_stack_trace)
 INTERFACE_FUNCTION(__sanitizer_ptr_cmp)
index fc34047..193624c 100644 (file)
@@ -32,7 +32,7 @@
 # define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))
 #endif
 
-#if SANITIZER_LINUX && !defined(SANITIZER_GO)
+#if (SANITIZER_LINUX && !defined(SANITIZER_GO)) || SANITIZER_WINDOWS
 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
 #else
 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
diff --git a/compiler-rt/test/asan/TestCases/Windows/default_options.cc b/compiler-rt/test/asan/TestCases/Windows/default_options.cc
new file mode 100644 (file)
index 0000000..6e0a28f
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clangxx_asan -O2 %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// FIXME: merge this with the common default_options test when we can run common
+// tests on Windows.
+
+const char *kAsanDefaultOptions="verbosity=1 help=1";
+
+extern "C"
+__attribute__((no_sanitize_address))
+const char *__asan_default_options() {
+  // CHECK: Available flags for AddressSanitizer:
+  return kAsanDefaultOptions;
+}
+
+int main() {
+  return 0;
+}
diff --git a/compiler-rt/test/asan/TestCases/Windows/free_hook_realloc.cc b/compiler-rt/test/asan/TestCases/Windows/free_hook_realloc.cc
new file mode 100644 (file)
index 0000000..297218b
--- /dev/null
@@ -0,0 +1,37 @@
+// Check that free hook doesn't conflict with Realloc.
+// RUN: %clangxx_asan -O2 %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+// FIXME: merge this with the common free_hook_realloc test when we can run
+// common tests on Windows.
+
+#include <stdlib.h>
+#include <io.h>
+#include <sanitizer/allocator_interface.h>
+
+static void *glob_ptr;
+
+extern "C" {
+void __sanitizer_free_hook(const volatile void *ptr) {
+  if (ptr == glob_ptr) {
+    *(int*)ptr = 0;
+    write(1, "FreeHook\n", sizeof("FreeHook\n"));
+  }
+}
+}
+
+int main() {
+  int *x = (int*)malloc(100);
+  x[0] = 42;
+  glob_ptr = x;
+  int *y = (int*)realloc(x, 200);
+  // Verify that free hook was called and didn't spoil the memory.
+  if (y[0] != 42) {
+    _exit(1);
+  }
+  write(1, "Passed\n", sizeof("Passed\n"));
+  free(y);
+  // CHECK: FreeHook
+  // CHECK: Passed
+  return 0;
+}
diff --git a/compiler-rt/test/asan/TestCases/Windows/on_error_callback.cc b/compiler-rt/test/asan/TestCases/Windows/on_error_callback.cc
new file mode 100644 (file)
index 0000000..9e690a3
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
+
+// FIXME: merge this with the common on_error_callback test when we can run
+// common tests on Windows.
+
+#include <stdio.h>
+#include <stdlib.h>
+
+extern "C"
+void __asan_on_error() {
+  fprintf(stderr, "__asan_on_error called");
+  fflush(0);
+}
+
+int main() {
+  char *x = (char*)malloc(10 * sizeof(char));
+  free(x);
+  return x[5];
+  // CHECK: __asan_on_error called
+}
index 1614fbe..a3aa663 100644 (file)
@@ -1,9 +1,6 @@
 // RUN: %clangxx_asan -O2 %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-// __asan_default_options() are not supported on Windows.
-// XFAIL: win32
-
 const char *kAsanDefaultOptions="verbosity=1 help=1";
 
 extern "C"
index 4b27532..cbc5d6f 100644 (file)
@@ -2,9 +2,6 @@
 // RUN: %clangxx_asan -O2 %s -o %t
 // RUN: %run %t 2>&1 | FileCheck %s
 
-// Malloc/free hooks are not supported on Windows.
-// XFAIL: win32
-
 #include <stdlib.h>
 #include <unistd.h>
 #include <sanitizer/allocator_interface.h>
index c378c8b..0ad83d5 100644 (file)
@@ -1,8 +1,5 @@
 // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
 
-// FIXME: __asan_on_error() is not supported on Windows yet.
-// XFAIL: win32
-
 #include <stdio.h>
 #include <stdlib.h>