[ASan] move replacements for new/delete to separate file
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@154167 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/Makefile.old b/lib/asan/Makefile.old
index 24d781b..9686838 100644
--- a/lib/asan/Makefile.old
+++ b/lib/asan/Makefile.old
@@ -192,6 +192,7 @@
$(BIN)/asan_mac$(SUFF).o \
$(BIN)/asan_malloc_linux$(SUFF).o \
$(BIN)/asan_malloc_mac$(SUFF).o \
+ $(BIN)/asan_new_delete$(SUFF).o \
$(BIN)/asan_poisoning$(SUFF).o \
$(BIN)/asan_posix$(SUFF).o \
$(BIN)/asan_printf$(SUFF).o \
diff --git a/lib/asan/asan_interceptors.cc b/lib/asan/asan_interceptors.cc
index 0adb699..6d7e63b 100644
--- a/lib/asan/asan_interceptors.cc
+++ b/lib/asan/asan_interceptors.cc
@@ -22,8 +22,6 @@
#include "asan_thread_registry.h"
#include "interception/interception.h"
-#include <new>
-
// Use macro to describe if specific function should be
// intercepted on a given platform.
#if !defined(_WIN32)
@@ -323,33 +321,6 @@
// ---------------------- Wrappers ---------------- {{{1
using namespace __asan; // NOLINT
-#define OPERATOR_NEW_BODY \
- GET_STACK_TRACE_HERE_FOR_MALLOC;\
- return asan_memalign(0, size, &stack);
-
-#ifdef ANDROID
-void *operator new(size_t size) { OPERATOR_NEW_BODY; }
-void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
-#else
-void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
-void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
-void *operator new(size_t size, std::nothrow_t const&) throw()
-{ OPERATOR_NEW_BODY; }
-void *operator new[](size_t size, std::nothrow_t const&) throw()
-{ OPERATOR_NEW_BODY; }
-#endif
-
-#define OPERATOR_DELETE_BODY \
- GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
- asan_free(ptr, &stack);
-
-void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
-void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
-void operator delete(void *ptr, std::nothrow_t const&) throw()
-{ OPERATOR_DELETE_BODY; }
-void operator delete[](void *ptr, std::nothrow_t const&) throw()
-{ OPERATOR_DELETE_BODY;}
-
static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
AsanThread *t = (AsanThread*)arg;
asanThreadRegistry().SetCurrent(t);
diff --git a/lib/asan/asan_internal.h b/lib/asan/asan_internal.h
index 90442c5..3e9faa8 100644
--- a/lib/asan/asan_internal.h
+++ b/lib/asan/asan_internal.h
@@ -155,6 +155,7 @@
// asan_globals.cc
bool DescribeAddrIfGlobal(uintptr_t addr);
+void ReplaceOperatorsNewAndDelete();
// asan_malloc_linux.cc / asan_malloc_mac.cc
void ReplaceSystemMalloc();
diff --git a/lib/asan/asan_new_delete.cc b/lib/asan/asan_new_delete.cc
new file mode 100644
index 0000000..74f8c4e
--- /dev/null
+++ b/lib/asan/asan_new_delete.cc
@@ -0,0 +1,55 @@
+//===-- asan_interceptors.cc ------------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file is a part of AddressSanitizer, an address sanity checker.
+//
+// Interceptors for operators new and delete.
+//===----------------------------------------------------------------------===//
+
+#include "asan_allocator.h"
+#include "asan_internal.h"
+#include "asan_stack.h"
+
+#include <new>
+
+namespace __asan {
+// This function is a no-op. We need it to make sure that object file
+// with our replacements will actually be loaded from static ASan
+// run-time library at link-time.
+void ReplaceOperatorsNewAndDelete() { }
+}
+
+using namespace __asan; // NOLINT
+
+#define OPERATOR_NEW_BODY \
+ GET_STACK_TRACE_HERE_FOR_MALLOC;\
+ return asan_memalign(0, size, &stack);
+
+#ifdef ANDROID
+void *operator new(size_t size) { OPERATOR_NEW_BODY; }
+void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
+#else
+void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
+void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
+void *operator new(size_t size, std::nothrow_t const&) throw()
+{ OPERATOR_NEW_BODY; }
+void *operator new[](size_t size, std::nothrow_t const&) throw()
+{ OPERATOR_NEW_BODY; }
+#endif
+
+#define OPERATOR_DELETE_BODY \
+ GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
+ asan_free(ptr, &stack);
+
+void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
+void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
+void operator delete(void *ptr, std::nothrow_t const&) throw()
+{ OPERATOR_DELETE_BODY; }
+void operator delete[](void *ptr, std::nothrow_t const&) throw()
+{ OPERATOR_DELETE_BODY; }
diff --git a/lib/asan/asan_rtl.cc b/lib/asan/asan_rtl.cc
index c11bb26..d4a1ad6 100644
--- a/lib/asan/asan_rtl.cc
+++ b/lib/asan/asan_rtl.cc
@@ -484,6 +484,7 @@
InitializeAsanInterceptors();
ReplaceSystemMalloc();
+ ReplaceOperatorsNewAndDelete();
if (FLAG_v) {
Printf("|| `[%p, %p]` || HighMem ||\n", kHighMemBeg, kHighMemEnd);