blob: c8bc0651b5077788da15a724b3af812b91eb623b [file] [log] [blame]
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +00001//===-- 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 Hines6d186232014-11-26 17:56:19 -080016#include "interception/interception.h"
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000017
Evgeniy Stepanove8650452013-04-15 12:41:52 +000018#if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
19
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000020#include <stddef.h>
21
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000022using namespace __msan; // NOLINT
23
24// Fake std::nothrow_t to avoid including <new>.
25namespace 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 Hines2d1fdb22014-05-28 23:58:16 -070034INTERCEPTOR_ATTRIBUTE
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000035void *operator new(size_t size) { OPERATOR_NEW_BODY; }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070036INTERCEPTOR_ATTRIBUTE
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000037void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070038INTERCEPTOR_ATTRIBUTE
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000039void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070040INTERCEPTOR_ATTRIBUTE
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000041void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
42
43#define OPERATOR_DELETE_BODY \
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +000044 GET_MALLOC_STACK_TRACE; \
45 if (ptr) MsanDeallocate(&stack, ptr)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000046
Stephen Hines2d1fdb22014-05-28 23:58:16 -070047INTERCEPTOR_ATTRIBUTE
48void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
49INTERCEPTOR_ATTRIBUTE
50void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
51INTERCEPTOR_ATTRIBUTE
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000052void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070053INTERCEPTOR_ATTRIBUTE
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000054void operator delete[](void *ptr, std::nothrow_t const&) {
55 OPERATOR_DELETE_BODY;
56}
Evgeniy Stepanove8650452013-04-15 12:41:52 +000057
58#endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE