Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 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 "sanitizer_common/sanitizer_internal_defs.h" |
| 15 | #include "tsan_interceptors.h" |
| 16 | |
| 17 | using namespace __tsan; // NOLINT |
| 18 | |
| 19 | namespace std { |
| 20 | struct nothrow_t {}; |
| 21 | } // namespace std |
| 22 | |
| 23 | #define OPERATOR_NEW_BODY(mangled_name) \ |
| 24 | if (cur_thread()->in_symbolizer) \ |
| 25 | return __libc_malloc(size); \ |
| 26 | void *p = 0; \ |
| 27 | { \ |
| 28 | SCOPED_INTERCEPTOR_RAW(mangled_name, size); \ |
| 29 | p = user_alloc(thr, pc, size); \ |
| 30 | } \ |
| 31 | invoke_malloc_hook(p, size); \ |
| 32 | return p; |
| 33 | |
| 34 | SANITIZER_INTERFACE_ATTRIBUTE |
| 35 | void *operator new(__sanitizer::uptr size); |
| 36 | void *operator new(__sanitizer::uptr size) { |
| 37 | OPERATOR_NEW_BODY(_Znwm); |
| 38 | } |
| 39 | |
| 40 | SANITIZER_INTERFACE_ATTRIBUTE |
| 41 | void *operator new[](__sanitizer::uptr size); |
| 42 | void *operator new[](__sanitizer::uptr size) { |
| 43 | OPERATOR_NEW_BODY(_Znam); |
| 44 | } |
| 45 | |
| 46 | SANITIZER_INTERFACE_ATTRIBUTE |
| 47 | void *operator new(__sanitizer::uptr size, std::nothrow_t const&); |
| 48 | void *operator new(__sanitizer::uptr size, std::nothrow_t const&) { |
| 49 | OPERATOR_NEW_BODY(_ZnwmRKSt9nothrow_t); |
| 50 | } |
| 51 | |
| 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(_ZnamRKSt9nothrow_t); |
| 56 | } |
| 57 | |
| 58 | #define OPERATOR_DELETE_BODY(mangled_name) \ |
| 59 | if (ptr == 0) return; \ |
| 60 | if (cur_thread()->in_symbolizer) \ |
| 61 | return __libc_free(ptr); \ |
| 62 | invoke_free_hook(ptr); \ |
| 63 | SCOPED_INTERCEPTOR_RAW(mangled_name, ptr); \ |
| 64 | user_free(thr, pc, ptr); |
| 65 | |
| 66 | SANITIZER_INTERFACE_ATTRIBUTE |
| 67 | void operator delete(void *ptr) throw(); |
| 68 | void operator delete(void *ptr) throw() { |
| 69 | OPERATOR_DELETE_BODY(_ZdlPv); |
| 70 | } |
| 71 | |
| 72 | SANITIZER_INTERFACE_ATTRIBUTE |
| 73 | void operator delete[](void *ptr) throw(); |
| 74 | void operator delete[](void *ptr) throw() { |
| 75 | OPERATOR_DELETE_BODY(_ZdaPv); |
| 76 | } |
| 77 | |
| 78 | SANITIZER_INTERFACE_ATTRIBUTE |
| 79 | void operator delete(void *ptr, std::nothrow_t const&); |
| 80 | void operator delete(void *ptr, std::nothrow_t const&) { |
| 81 | OPERATOR_DELETE_BODY(_ZdlPvRKSt9nothrow_t); |
| 82 | } |
| 83 | |
| 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(_ZdaPvRKSt9nothrow_t); |
| 88 | } |