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" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 16 | #include "interception/interception.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 17 | |
Evgeniy Stepanov | e865045 | 2013-04-15 12:41:52 +0000 | [diff] [blame] | 18 | #if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE |
| 19 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 22 | using namespace __msan; // NOLINT |
| 23 | |
| 24 | // Fake std::nothrow_t to avoid including <new>. |
| 25 | namespace std { |
| 26 | struct nothrow_t {}; |
| 27 | } // namespace std |
| 28 | |
| 29 | |
| 30 | #define OPERATOR_NEW_BODY \ |
| 31 | GET_MALLOC_STACK_TRACE; \ |
| 32 | return MsanReallocate(&stack, 0, size, sizeof(u64), false) |
| 33 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 34 | INTERCEPTOR_ATTRIBUTE |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 35 | void *operator new(size_t size) { OPERATOR_NEW_BODY; } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 36 | INTERCEPTOR_ATTRIBUTE |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 37 | void *operator new[](size_t size) { OPERATOR_NEW_BODY; } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 38 | INTERCEPTOR_ATTRIBUTE |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 39 | void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 40 | INTERCEPTOR_ATTRIBUTE |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 41 | void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; } |
| 42 | |
| 43 | #define OPERATOR_DELETE_BODY \ |
Evgeniy Stepanov | effdc7e | 2013-09-16 11:03:31 +0000 | [diff] [blame] | 44 | GET_MALLOC_STACK_TRACE; \ |
| 45 | if (ptr) MsanDeallocate(&stack, ptr) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 46 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 47 | INTERCEPTOR_ATTRIBUTE |
| 48 | void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; } |
| 49 | INTERCEPTOR_ATTRIBUTE |
| 50 | void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; } |
| 51 | INTERCEPTOR_ATTRIBUTE |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 52 | void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 53 | INTERCEPTOR_ATTRIBUTE |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 54 | void operator delete[](void *ptr, std::nothrow_t const&) { |
| 55 | OPERATOR_DELETE_BODY; |
| 56 | } |
Evgeniy Stepanov | e865045 | 2013-04-15 12:41:52 +0000 | [diff] [blame] | 57 | |
| 58 | #endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE |