[ASan] Use dylib interposition to hook memory allocation in the dynamic runtime.
This CL drastically simplifies the way we're hooking the memory allocation routines in ASan on Mac by using dylib interposition to replace the main malloc_zone_* functions. This allows us to avoid replacing the default CFAllocator and drop the CF dependency at all.

Committing this patch will result in the static runtime being broken. A follow-up CL will switch ASan to use the dynamic runtime library.

llvm-svn: 173134
diff --git a/compiler-rt/lib/asan/lit_tests/Darwin/interface_symbols_darwin.c b/compiler-rt/lib/asan/lit_tests/Darwin/interface_symbols_darwin.c
new file mode 100644
index 0000000..4661f9d
--- /dev/null
+++ b/compiler-rt/lib/asan/lit_tests/Darwin/interface_symbols_darwin.c
@@ -0,0 +1,37 @@
+// Check the presense of interface symbols in the ASan runtime dylib.
+// If you're changing this file, please also change
+// ../Linux/interface_symbols.c
+
+// RUN: %clang -fsanitize=address -dead_strip -O2 %s -o %t.exe
+// RUN: rm -f %t.symbols %t.interface
+
+// RUN: nm `otool -L %t.exe | grep "asan_osx_dynamic.dylib" | \
+// RUN:                       tr -d '\011' | \
+// RUN:                       sed "s/.dylib.*/.dylib/"` \
+// RUN:   | grep " T " | sed "s/.* T //" \
+// RUN:   | grep "__asan_" | sed "s/___asan_/__asan_/" \
+// RUN:   | grep -v "__asan_malloc_hook" \
+// RUN:   | grep -v "__asan_free_hook" \
+// RUN:   | grep -v "__asan_symbolize" \
+// RUN:   | grep -v "__asan_default_options" \
+// RUN:   | grep -v "__asan_on_error" > %t.symbols
+
+// RUN: cat %p/../../../../include/sanitizer/asan_interface.h \
+// RUN:    | sed "s/\/\/.*//" | sed "s/typedef.*//" \
+// RUN:    | grep -v "OPTIONAL" \
+// RUN:    | grep "__asan_.*(" | sed "s/.* __asan_/__asan_/;s/(.*//" \
+// RUN:    > %t.interface
+// RUN: echo __asan_report_load1 >> %t.interface
+// RUN: echo __asan_report_load2 >> %t.interface
+// RUN: echo __asan_report_load4 >> %t.interface
+// RUN: echo __asan_report_load8 >> %t.interface
+// RUN: echo __asan_report_load16 >> %t.interface
+// RUN: echo __asan_report_store1 >> %t.interface
+// RUN: echo __asan_report_store2 >> %t.interface
+// RUN: echo __asan_report_store4 >> %t.interface
+// RUN: echo __asan_report_store8 >> %t.interface
+// RUN: echo __asan_report_store16 >> %t.interface
+
+// RUN: cat %t.interface | sort -u | diff %t.symbols -
+
+int main() { return 0; }
diff --git a/compiler-rt/lib/asan/lit_tests/Darwin/lit.local.cfg b/compiler-rt/lib/asan/lit_tests/Darwin/lit.local.cfg
new file mode 100644
index 0000000..a85dfcd
--- /dev/null
+++ b/compiler-rt/lib/asan/lit_tests/Darwin/lit.local.cfg
@@ -0,0 +1,9 @@
+def getRoot(config):
+  if not config.parent:
+    return config
+  return getRoot(config.parent)
+
+root = getRoot(config)
+
+if root.host_os not in ['Darwin']:
+  config.unsupported = True
diff --git a/compiler-rt/lib/asan/lit_tests/malloc_delete_mismatch.cc b/compiler-rt/lib/asan/lit_tests/Linux/malloc_delete_mismatch.cc
similarity index 100%
rename from compiler-rt/lib/asan/lit_tests/malloc_delete_mismatch.cc
rename to compiler-rt/lib/asan/lit_tests/Linux/malloc_delete_mismatch.cc
diff --git a/compiler-rt/lib/asan/lit_tests/heap-overflow.cc b/compiler-rt/lib/asan/lit_tests/heap-overflow.cc
index 2648ec7..f1d719c 100644
--- a/compiler-rt/lib/asan/lit_tests/heap-overflow.cc
+++ b/compiler-rt/lib/asan/lit_tests/heap-overflow.cc
@@ -29,10 +29,8 @@
   // CHECK-Linux: {{    #0 0x.* in .*malloc}}
   // CHECK-Linux: {{    #1 0x.* in main .*heap-overflow.cc:21}}
 
-  // CHECK-Darwin: {{    #0 0x.* in .*mz_malloc.*}}
-  // CHECK-Darwin: {{    #1 0x.* in malloc_zone_malloc.*}}
-  // CHECK-Darwin: {{    #2 0x.* in malloc.*}}
-  // CHECK-Darwin: {{    #3 0x.* in _?main .*heap-overflow.cc:21}}
+  // CHECK-Darwin: {{    #0 0x.* in _?wrap_malloc.*}}
+  // CHECK-Darwin: {{    #1 0x.* in _?main .*heap-overflow.cc:21}}
   free(x);
   return res;
 }
diff --git a/compiler-rt/lib/asan/lit_tests/large_func_test.cc b/compiler-rt/lib/asan/lit_tests/large_func_test.cc
index a748288..ce19422 100644
--- a/compiler-rt/lib/asan/lit_tests/large_func_test.cc
+++ b/compiler-rt/lib/asan/lit_tests/large_func_test.cc
@@ -56,7 +56,12 @@
   // CHECK: {{    #1 0x.* in _?main .*large_func_test.cc:}}[[@LINE-1]]
   // CHECK: {{0x.* is located 44 bytes to the right of 400-byte region}}
   // CHECK: {{allocated by thread T0 here:}}
-  // CHECK: {{    #0 0x.* in operator new.*}}
-  // CHECK: {{    #1 0x.* in _?main .*large_func_test.cc:}}[[@LINE-6]]
+  // CHECK-Linux: {{    #0 0x.* in operator new.*}}
+  // CHECK-Linux: {{    #1 0x.* in _?main .*large_func_test.cc:}}[[@LINE-6]]
+
+  // CHECK-Darwin: {{    #0 0x.* in _?wrap_malloc.*}}
+  // CHECK-Darwin: {{    #1 0x.* in operator new.*}}
+  // CHECK-Darwin: {{    #2 0x.* in operator new\[\].*}}
+  // CHECK-Darwin: {{    #3 0x.* in _?main .*large_func_test.cc:}}[[@LINE-11]]
   delete x;
 }
diff --git a/compiler-rt/lib/asan/lit_tests/strncpy-overflow.cc b/compiler-rt/lib/asan/lit_tests/strncpy-overflow.cc
index 1871184..2d79c7a 100644
--- a/compiler-rt/lib/asan/lit_tests/strncpy-overflow.cc
+++ b/compiler-rt/lib/asan/lit_tests/strncpy-overflow.cc
@@ -32,9 +32,7 @@
   // CHECK-Linux: {{    #0 0x.* in .*malloc}}
   // CHECK-Linux: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-10]]
 
-  // CHECK-Darwin: {{    #0 0x.* in .*mz_malloc.*}}
-  // CHECK-Darwin: {{    #1 0x.* in malloc_zone_malloc.*}}
-  // CHECK-Darwin: {{    #2 0x.* in malloc.*}}
-  // CHECK-Darwin: {{    #3 0x.* in _?main .*strncpy-overflow.cc:}}[[@LINE-15]]
+  // CHECK-Darwin: {{    #0 0x.* in _?wrap_malloc.*}}
+  // CHECK-Darwin: {{    #1 0x.* in _?main .*strncpy-overflow.cc:}}[[@LINE-13]]
   return short_buffer[8];
 }
diff --git a/compiler-rt/lib/asan/lit_tests/use-after-free.cc b/compiler-rt/lib/asan/lit_tests/use-after-free.cc
index 24d5a2a..aee185d 100644
--- a/compiler-rt/lib/asan/lit_tests/use-after-free.cc
+++ b/compiler-rt/lib/asan/lit_tests/use-after-free.cc
@@ -30,19 +30,14 @@
   // CHECK-Linux: {{    #0 0x.* in .*free}}
   // CHECK-Linux: {{    #1 0x.* in main .*use-after-free.cc:21}}
 
-  // CHECK-Darwin: {{    #0 0x.* in .*free_common.*}}
-  // CHECK-Darwin: {{    #1 0x.* in .*mz_free.*}}
-  // We override free() on Darwin, thus no malloc_zone_free
-  // CHECK-Darwin: {{    #2 0x.* in _?wrap_free}}
-  // CHECK-Darwin: {{    #3 0x.* in _?main .*use-after-free.cc:21}}
+  // CHECK-Darwin: {{    #0 0x.* in _?wrap_free}}
+  // CHECK-Darwin: {{    #1 0x.* in _?main .*use-after-free.cc:21}}
 
   // CHECK: {{previously allocated by thread T0 here:}}
 
   // CHECK-Linux: {{    #0 0x.* in .*malloc}}
   // CHECK-Linux: {{    #1 0x.* in main .*use-after-free.cc:20}}
 
-  // CHECK-Darwin: {{    #0 0x.* in .*mz_malloc.*}}
-  // CHECK-Darwin: {{    #1 0x.* in malloc_zone_malloc.*}}
-  // CHECK-Darwin: {{    #2 0x.* in malloc.*}}
-  // CHECK-Darwin: {{    #3 0x.* in _?main .*use-after-free.cc:20}}
+  // CHECK-Darwin: {{    #0 0x.* in _?wrap_malloc.*}}
+  // CHECK-Darwin: {{    #1 0x.* in _?main .*use-after-free.cc:20}}
 }