1 //===-- asan_interceptors.cc ----------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // Interceptors for operators new and delete.
11 //===----------------------------------------------------------------------===//
13 #include "asan_allocator.h"
14 #include "asan_internal.h"
15 #include "asan_stack.h"
20 // This function is a no-op. We need it to make sure that object file
21 // with our replacements will actually be loaded from static ASan
22 // run-time library at link-time.
23 void ReplaceOperatorsNewAndDelete() { }
26 using namespace __asan
; // NOLINT
28 // On Android new() goes through malloc interceptors.
29 // See also https://code.google.com/p/address-sanitizer/issues/detail?id=131.
30 #if !SANITIZER_ANDROID
32 // Fake std::nothrow_t to avoid including <new>.
37 #define OPERATOR_NEW_BODY(type) \
38 GET_STACK_TRACE_MALLOC;\
39 return asan_memalign(0, size, &stack, type);
41 // On OS X it's not enough to just provide our own 'operator new' and
42 // 'operator delete' implementations, because they're going to be in the
43 // runtime dylib, and the main executable will depend on both the runtime
44 // dylib and libstdc++, each of those'll have its implementation of new and
46 // To make sure that C++ allocation/deallocation operators are overridden on
47 // OS X we need to intercept them using their mangled names.
50 void *operator new(size_t size
) { OPERATOR_NEW_BODY(FROM_NEW
); }
52 void *operator new[](size_t size
) { OPERATOR_NEW_BODY(FROM_NEW_BR
); }
54 void *operator new(size_t size
, std::nothrow_t
const&)
55 { OPERATOR_NEW_BODY(FROM_NEW
); }
57 void *operator new[](size_t size
, std::nothrow_t
const&)
58 { OPERATOR_NEW_BODY(FROM_NEW_BR
); }
60 #else // SANITIZER_MAC
61 INTERCEPTOR(void *, _Znwm
, size_t size
) {
62 OPERATOR_NEW_BODY(FROM_NEW
);
64 INTERCEPTOR(void *, _Znam
, size_t size
) {
65 OPERATOR_NEW_BODY(FROM_NEW_BR
);
67 INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t
, size_t size
, std::nothrow_t
const&) {
68 OPERATOR_NEW_BODY(FROM_NEW
);
70 INTERCEPTOR(void *, _ZnamRKSt9nothrow_t
, size_t size
, std::nothrow_t
const&) {
71 OPERATOR_NEW_BODY(FROM_NEW_BR
);
75 #define OPERATOR_DELETE_BODY(type) \
76 GET_STACK_TRACE_FREE;\
77 asan_free(ptr, &stack, type);
81 void operator delete(void *ptr
) { OPERATOR_DELETE_BODY(FROM_NEW
); }
83 void operator delete[](void *ptr
) { OPERATOR_DELETE_BODY(FROM_NEW_BR
); }
85 void operator delete(void *ptr
, std::nothrow_t
const&)
86 { OPERATOR_DELETE_BODY(FROM_NEW
); }
88 void operator delete[](void *ptr
, std::nothrow_t
const&)
89 { OPERATOR_DELETE_BODY(FROM_NEW_BR
); }
91 #else // SANITIZER_MAC
92 INTERCEPTOR(void, _ZdlPv
, void *ptr
) {
93 OPERATOR_DELETE_BODY(FROM_NEW
);
95 INTERCEPTOR(void, _ZdaPv
, void *ptr
) {
96 OPERATOR_DELETE_BODY(FROM_NEW_BR
);
98 INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t
, void *ptr
, std::nothrow_t
const&) {
99 OPERATOR_DELETE_BODY(FROM_NEW
);
101 INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t
, void *ptr
, std::nothrow_t
const&) {
102 OPERATOR_DELETE_BODY(FROM_NEW_BR
);