blob: 88d4364f6562b21fc7baebf19401e8eebf1cbf25 [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"
16
Evgeniy Stepanove8650452013-04-15 12:41:52 +000017#if MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
18
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000019#include <stddef.h>
20
21namespace __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.
25void ReplaceOperatorsNewAndDelete() { }
26}
27
28using namespace __msan; // NOLINT
29
30// Fake std::nothrow_t to avoid including <new>.
31namespace 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
40void *operator new(size_t size) { OPERATOR_NEW_BODY; }
41void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
42void *operator new(size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
43void *operator new[](size_t size, std::nothrow_t const&) { OPERATOR_NEW_BODY; }
44
45#define OPERATOR_DELETE_BODY \
46 if (ptr) MsanDeallocate(ptr)
47
48void operator delete(void *ptr) { OPERATOR_DELETE_BODY; }
49void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; }
50void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
51void operator delete[](void *ptr, std::nothrow_t const&) {
52 OPERATOR_DELETE_BODY;
53}
Evgeniy Stepanove8650452013-04-15 12:41:52 +000054
55#endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE