<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Feb 24, 2015 at 9:07 AM, Timur Iskhodzhanov <span dir="ltr"><<a href="mailto:timurrrr@google.com" target="_blank">timurrrr@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: timurrrr<br>
Date: Tue Feb 24 11:07:22 2015<br>
New Revision: 230344<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=230344&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=230344&view=rev</a><br>
Log:<br>
[ASan/Win] Add support for sanitizer allocator hooks, __asan_default_options and __asan_on_error<br>
<br>
Added:<br>
    compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc<br>
    compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc<br>
    compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc<br>
Modified:<br>
    compiler-rt/trunk/lib/asan/asan_win.cc<br>
    compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc<br>
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h<br>
    compiler-rt/trunk/test/asan/TestCases/default_options.cc<br>
    compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc<br>
    compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc<br>
<br>
Modified: compiler-rt/trunk/lib/asan/asan_win.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=230344&r1=230343&r2=230344&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=230344&r1=230343&r2=230344&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/asan/asan_win.cc (original)<br>
+++ compiler-rt/trunk/lib/asan/asan_win.cc Tue Feb 24 11:07:22 2015<br>
@@ -27,13 +27,26 @@<br>
 #include "sanitizer_common/sanitizer_mutex.h"<br>
<br>
 extern "C" {<br>
-  SANITIZER_INTERFACE_ATTRIBUTE<br>
-  int __asan_should_detect_stack_use_after_return() {<br>
-    __asan_init();<br>
-    return __asan_option_detect_stack_use_after_return;<br>
-  }<br>
+SANITIZER_INTERFACE_ATTRIBUTE<br>
+int __asan_should_detect_stack_use_after_return() {<br>
+  __asan_init();<br>
+  return __asan_option_detect_stack_use_after_return;<br>
 }<br>
<br>
+// We don't have a direct equivalent of weak symbols when using MSVC, but we can<br>
+// use the /alternatename directive to tell the linker to default a specific<br>
+// symbol to a specific value, which works nicely for allocator hooks and<br>
+// __asan_default_options().<br>
+void __sanitizer_default_malloc_hook(void *ptr, uptr size) { }<br>
+void __sanitizer_default_free_hook(void *ptr) { }<br>
+const char* __asan_default_default_options() { return ""; }<br>
+void __asan_default_on_error() {}<br>
+#pragma comment(linker, "/alternatename:___sanitizer_malloc_hook=___sanitizer_default_malloc_hook")  // NOLINT<br>
+#pragma comment(linker, "/alternatename:___sanitizer_free_hook=___sanitizer_default_free_hook")      // NOLINT<br>
+#pragma comment(linker, "/alternatename:___asan_default_options=___asan_default_default_options")    // NOLINT<br>
+#pragma comment(linker, "/alternatename:___asan_on_error=___asan_default_on_error")                  // NOLINT<br>
+}  // extern "C"<br>
+<br>
 namespace __asan {<br>
<br>
 // ---------------------- TSD ---------------- {{{1<br>
<br>
Modified: compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc?rev=230344&r1=230343&r2=230344&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc?rev=230344&r1=230343&r2=230344&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc (original)<br>
+++ compiler-rt/trunk/lib/asan/asan_win_dll_thunk.cc Tue Feb 24 11:07:22 2015<br>
@@ -304,7 +304,6 @@ INTERFACE_FUNCTION(__sanitizer_cov_modul<br>
 INTERFACE_FUNCTION(__sanitizer_cov_trace_basic_block)<br>
 INTERFACE_FUNCTION(__sanitizer_cov_trace_func_enter)<br>
 INTERFACE_FUNCTION(__sanitizer_cov_with_check)<br>
-INTERFACE_FUNCTION(__sanitizer_free_hook)<br>
 INTERFACE_FUNCTION(__sanitizer_get_allocated_size)<br>
 INTERFACE_FUNCTION(__sanitizer_get_coverage_guards)<br>
 INTERFACE_FUNCTION(__sanitizer_get_current_allocated_bytes)<br>
@@ -314,7 +313,6 @@ INTERFACE_FUNCTION(__sanitizer_get_heap_<br>
 INTERFACE_FUNCTION(__sanitizer_get_ownership)<br>
 INTERFACE_FUNCTION(__sanitizer_get_total_unique_coverage)<br>
 INTERFACE_FUNCTION(__sanitizer_get_unmapped_bytes)<br>
-INTERFACE_FUNCTION(__sanitizer_malloc_hook)<br>
 INTERFACE_FUNCTION(__sanitizer_maybe_open_cov_file)<br>
 INTERFACE_FUNCTION(__sanitizer_print_stack_trace)<br>
 INTERFACE_FUNCTION(__sanitizer_ptr_cmp)<br>
<br>
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=230344&r1=230343&r2=230344&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=230344&r1=230343&r2=230344&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h (original)<br>
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h Tue Feb 24 11:07:22 2015<br>
@@ -32,7 +32,7 @@<br>
 # define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))<br>
 #endif<br>
<br>
-#if SANITIZER_LINUX && !defined(SANITIZER_GO)<br>
+#if (SANITIZER_LINUX && !defined(SANITIZER_GO)) || SANITIZER_WINDOWS<br></blockquote><div><br></div><div>+Dmitry</div><div><br></div><div>This doesn't look right to me. Shouldn't it be</div><div>#if (SANITIZER_LINUX || SANITIZER_WINDOWS) && !defined(SANITIZER_GO)</div><div>?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1<br>
 #else<br>
 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0<br>
<br>
Added: compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc?rev=230344&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc?rev=230344&view=auto</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc (added)<br>
+++ compiler-rt/trunk/test/asan/TestCases/Windows/default_options.cc Tue Feb 24 11:07:22 2015<br>
@@ -0,0 +1,18 @@<br>
+// RUN: %clangxx_asan -O2 %s -o %t<br>
+// RUN: %run %t 2>&1 | FileCheck %s<br>
+<br>
+// FIXME: merge this with the common default_options test when we can run common<br>
+// tests on Windows.<br>
+<br>
+const char *kAsanDefaultOptions="verbosity=1 help=1";<br>
+<br>
+extern "C"<br>
+__attribute__((no_sanitize_address))<br>
+const char *__asan_default_options() {<br>
+  // CHECK: Available flags for AddressSanitizer:<br>
+  return kAsanDefaultOptions;<br>
+}<br>
+<br>
+int main() {<br>
+  return 0;<br>
+}<br>
<br>
Added: compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc?rev=230344&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc?rev=230344&view=auto</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc (added)<br>
+++ compiler-rt/trunk/test/asan/TestCases/Windows/free_hook_realloc.cc Tue Feb 24 11:07:22 2015<br>
@@ -0,0 +1,37 @@<br>
+// Check that free hook doesn't conflict with Realloc.<br>
+// RUN: %clangxx_asan -O2 %s -o %t<br>
+// RUN: %run %t 2>&1 | FileCheck %s<br>
+<br>
+// FIXME: merge this with the common free_hook_realloc test when we can run<br>
+// common tests on Windows.<br>
+<br>
+#include <stdlib.h><br>
+#include <io.h><br>
+#include <sanitizer/allocator_interface.h><br>
+<br>
+static void *glob_ptr;<br>
+<br>
+extern "C" {<br>
+void __sanitizer_free_hook(const volatile void *ptr) {<br>
+  if (ptr == glob_ptr) {<br>
+    *(int*)ptr = 0;<br>
+    write(1, "FreeHook\n", sizeof("FreeHook\n"));<br>
+  }<br>
+}<br>
+}<br>
+<br>
+int main() {<br>
+  int *x = (int*)malloc(100);<br>
+  x[0] = 42;<br>
+  glob_ptr = x;<br>
+  int *y = (int*)realloc(x, 200);<br>
+  // Verify that free hook was called and didn't spoil the memory.<br>
+  if (y[0] != 42) {<br>
+    _exit(1);<br>
+  }<br>
+  write(1, "Passed\n", sizeof("Passed\n"));<br>
+  free(y);<br>
+  // CHECK: FreeHook<br>
+  // CHECK: Passed<br>
+  return 0;<br>
+}<br>
<br>
Added: compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc?rev=230344&view=auto" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc?rev=230344&view=auto</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc (added)<br>
+++ compiler-rt/trunk/test/asan/TestCases/Windows/on_error_callback.cc Tue Feb 24 11:07:22 2015<br>
@@ -0,0 +1,20 @@<br>
+// RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s<br>
+<br>
+// FIXME: merge this with the common on_error_callback test when we can run<br>
+// common tests on Windows.<br>
+<br>
+#include <stdio.h><br>
+#include <stdlib.h><br>
+<br>
+extern "C"<br>
+void __asan_on_error() {<br>
+  fprintf(stderr, "__asan_on_error called");<br>
+  fflush(0);<br>
+}<br>
+<br>
+int main() {<br>
+  char *x = (char*)malloc(10 * sizeof(char));<br>
+  free(x);<br>
+  return x[5];<br>
+  // CHECK: __asan_on_error called<br>
+}<br>
<br>
Modified: compiler-rt/trunk/test/asan/TestCases/default_options.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/default_options.cc?rev=230344&r1=230343&r2=230344&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/default_options.cc?rev=230344&r1=230343&r2=230344&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/default_options.cc (original)<br>
+++ compiler-rt/trunk/test/asan/TestCases/default_options.cc Tue Feb 24 11:07:22 2015<br>
@@ -1,9 +1,6 @@<br>
 // RUN: %clangxx_asan -O2 %s -o %t<br>
 // RUN: %run %t 2>&1 | FileCheck %s<br>
<br>
-// __asan_default_options() are not supported on Windows.<br>
-// XFAIL: win32<br>
-<br>
 const char *kAsanDefaultOptions="verbosity=1 help=1";<br>
<br>
 extern "C"<br>
<br>
Modified: compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc?rev=230344&r1=230343&r2=230344&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc?rev=230344&r1=230343&r2=230344&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc (original)<br>
+++ compiler-rt/trunk/test/asan/TestCases/free_hook_realloc.cc Tue Feb 24 11:07:22 2015<br>
@@ -2,9 +2,6 @@<br>
 // RUN: %clangxx_asan -O2 %s -o %t<br>
 // RUN: %run %t 2>&1 | FileCheck %s<br>
<br>
-// Malloc/free hooks are not supported on Windows.<br>
-// XFAIL: win32<br>
-<br>
 #include <stdlib.h><br>
 #include <unistd.h><br>
 #include <sanitizer/allocator_interface.h><br>
<br>
Modified: compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc?rev=230344&r1=230343&r2=230344&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc?rev=230344&r1=230343&r2=230344&view=diff</a><br>
==============================================================================<br>
--- compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc (original)<br>
+++ compiler-rt/trunk/test/asan/TestCases/on_error_callback.cc Tue Feb 24 11:07:22 2015<br>
@@ -1,8 +1,5 @@<br>
 // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s<br>
<br>
-// FIXME: __asan_on_error() is not supported on Windows yet.<br>
-// XFAIL: win32<br>
-<br>
 #include <stdio.h><br>
 #include <stdlib.h><br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Alexey Samsonov<br><a href="mailto:vonosmas@gmail.com" target="_blank">vonosmas@gmail.com</a></div></div>
</div></div>