[compiler-rt] r230344 - [ASan/Win] Add support for sanitizer allocator hooks, __asan_default_options and __asan_on_error
Timur Iskhodzhanov
timurrrr at google.com
Tue Feb 24 09:07:23 PST 2015
Author: timurrrr
Date: Tue Feb 24 11:07:22 2015
New Revision: 230344
URL: http://llvm.org/viewvc/llvm-project?rev=230344&view=rev
Log:
[ASan/Win] Add support for sanitizer allocator hooks, __asan_default_options and __asan_on_error
Added:
compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc
compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc
compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc
Modified:
compiler-rt/trunk/lib/asan/asan_win.cc
compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
compiler-rt/trunk/test/asan/TestCases/default_options.cc
compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc
compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc
Modified: compiler-rt/trunk/lib/asan/asan_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=230344&r1=230343&r2=230344&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win.cc Tue Feb 24 11:07:22 2015
@@ -27,13 +27,26 @@
#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
Modified: compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc?rev=230344&r1=230343&r2=230344&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc Tue Feb 24 11:07:22 2015
@@ -304,7 +304,6 @@ INTERFACE_FUNCTION(__sanitizer_cov_modul
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_
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)
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=230344&r1=230343&r2=230344&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h Tue Feb 24 11:07:22 2015
@@ -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
Added: compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc?rev=230344&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc Tue Feb 24 11:07:22 2015
@@ -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;
+}
Added: compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc?rev=230344&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc Tue Feb 24 11:07:22 2015
@@ -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;
+}
Added: compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc?rev=230344&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc Tue Feb 24 11:07:22 2015
@@ -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
+}
Modified: compiler-rt/trunk/test/asan/TestCases/default_options.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/default_options.cc?rev=230344&r1=230343&r2=230344&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/default_options.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/default_options.cc Tue Feb 24 11:07:22 2015
@@ -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"
Modified: compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc?rev=230344&r1=230343&r2=230344&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc Tue Feb 24 11:07:22 2015
@@ -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>
Modified: compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc?rev=230344&r1=230343&r2=230344&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc Tue Feb 24 11:07:22 2015
@@ -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>
More information about the llvm-commits
mailing list