Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 1 | //===-- msan_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 MemorySanitizer. |
| 11 | // |
| 12 | // Interceptors for operators new and delete. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "msan.h" |
| 16 | |
Evgeniy Stepanov | e865045 | 2013-04-15 12:41:52 +0000 | [diff] [blame] | 17 | #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE |
| 18 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | |
| 21 | namespace __msan { |
| 22 | // This function is a no-op. We need it to make sure that object file |
| 23 | // with our replacements will actually be loaded from static MSan |
| 24 | // run-time library at link-time. |
| 25 | void ReplaceOperatorsNewAndDelete() { } |
| 26 | } |
| 27 | |
| 28 | using namespace __msan; // NOLINT |
| 29 | |
| 30 | // Fake std::nothrow_t to avoid including <new>. |
| 31 | namespace std { |
| 32 | struct nothrow_t {}; |
| 33 | } // namespace std |
| 34 | |
| 35 | |
| 36 | #define OPERATOR_NEW_BODY \ |
| 37 | GET_MALLOC_STACK_TRACE; \ |
| 38 | return MsanReallocate(&stack, 0, size, sizeof(u64), false) |
| 39 | |
| 40 | void *operator new(size_t size) { OPERATOR_NEW_BODY; } |
| 41 | void *operator new[](size_t size) { OPERATOR_NEW_BODY; } |
| 42 | void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; } |
| 43 | void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; } |
| 44 | |
| 45 | #define OPERATOR_DELETE_BODY \ |
Evgeniy Stepanov | effdc7e | 2013-09-16 11:03:31 +0000 | [diff] [blame] | 46 | GET_MALLOC_STACK_TRACE; \ |
| 47 | if (ptr) MsanDeallocate(&stack, ptr) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 48 | |
| 49 | void operator delete(void *ptr) { OPERATOR_DELETE_BODY; } |
| 50 | void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; } |
| 51 | void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; } |
| 52 | void operator delete[](void *ptr, std::nothrow_t const&) { |
| 53 | OPERATOR_DELETE_BODY; |
| 54 | } |
Evgeniy Stepanov | e865045 | 2013-04-15 12:41:52 +0000 | [diff] [blame] | 55 | |
| 56 | #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE |