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