1 //===-- tsan_new_delete.cc ----------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 // Interceptors for operators new and delete.
13 //===----------------------------------------------------------------------===//
14 #include "interception/interception.h"
15 #include "sanitizer_common/sanitizer_internal_defs.h"
16 #include "tsan_interceptors.h"
17
18 using namespace __tsan; // NOLINT
19
20 namespace std {
21 struct nothrow_t {};
22 } // namespace std
23
24 DECLARE_REAL(void *, malloc, uptr size)
25 DECLARE_REAL(void, free, void *ptr)
26
27 #define OPERATOR_NEW_BODY(mangled_name) \
28 if (cur_thread()->in_symbolizer) \
29 return InternalAlloc(size); \
30 void *p = 0; \
31 { \
32 SCOPED_INTERCEPTOR_RAW(mangled_name, size); \
33 p = user_alloc(thr, pc, size); \
34 } \
35 invoke_malloc_hook(p, size); \
36 return p;
37
38 SANITIZER_INTERFACE_ATTRIBUTE
39 void *operator new(__sanitizer::uptr size);
operator new(__sanitizer::uptr size)40 void *operator new(__sanitizer::uptr size) {
41 OPERATOR_NEW_BODY(_Znwm);
42 }
43
44 SANITIZER_INTERFACE_ATTRIBUTE
45 void *operator new[](__sanitizer::uptr size);
operator new[](__sanitizer::uptr size)46 void *operator new[](__sanitizer::uptr size) {
47 OPERATOR_NEW_BODY(_Znam);
48 }
49
50 SANITIZER_INTERFACE_ATTRIBUTE
51 void *operator new(__sanitizer::uptr size, std::nothrow_t const&);
operator new(__sanitizer::uptr size,std::nothrow_t const &)52 void *operator new(__sanitizer::uptr size, std::nothrow_t const&) {
53 OPERATOR_NEW_BODY(_ZnwmRKSt9nothrow_t);
54 }
55
56 SANITIZER_INTERFACE_ATTRIBUTE
57 void *operator new[](__sanitizer::uptr size, std::nothrow_t const&);
operator new[](__sanitizer::uptr size,std::nothrow_t const &)58 void *operator new[](__sanitizer::uptr size, std::nothrow_t const&) {
59 OPERATOR_NEW_BODY(_ZnamRKSt9nothrow_t);
60 }
61
62 #define OPERATOR_DELETE_BODY(mangled_name) \
63 if (ptr == 0) return; \
64 if (cur_thread()->in_symbolizer) \
65 return InternalFree(ptr); \
66 invoke_free_hook(ptr); \
67 SCOPED_INTERCEPTOR_RAW(mangled_name, ptr); \
68 user_free(thr, pc, ptr);
69
70 SANITIZER_INTERFACE_ATTRIBUTE
71 void operator delete(void *ptr) NOEXCEPT;
operator delete(void * ptr)72 void operator delete(void *ptr) NOEXCEPT {
73 OPERATOR_DELETE_BODY(_ZdlPv);
74 }
75
76 SANITIZER_INTERFACE_ATTRIBUTE
77 void operator delete[](void *ptr) NOEXCEPT;
operator delete[](void * ptr)78 void operator delete[](void *ptr) NOEXCEPT {
79 OPERATOR_DELETE_BODY(_ZdaPv);
80 }
81
82 SANITIZER_INTERFACE_ATTRIBUTE
83 void operator delete(void *ptr, std::nothrow_t const&);
operator delete(void * ptr,std::nothrow_t const &)84 void operator delete(void *ptr, std::nothrow_t const&) {
85 OPERATOR_DELETE_BODY(_ZdlPvRKSt9nothrow_t);
86 }
87
88 SANITIZER_INTERFACE_ATTRIBUTE
89 void operator delete[](void *ptr, std::nothrow_t const&);
operator delete[](void * ptr,std::nothrow_t const &)90 void operator delete[](void *ptr, std::nothrow_t const&) {
91 OPERATOR_DELETE_BODY(_ZdaPvRKSt9nothrow_t);
92 }
93