blob: 17687ddfc213067e62d82482a8debc2f8c75fd09 [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 \
Evgeniy Stepanoveffdc7e2013-09-16 11:03:31 +000046 GET_MALLOC_STACK_TRACE; \
47 if (ptr) MsanDeallocate(&stack, ptr)
Evgeniy Stepanov78c56c32012-12-11 12:27:27 +000048
49void operator delete(void *ptr) { OPERATOR_DELETE_BODY; }
50void operator delete[](void *ptr) { OPERATOR_DELETE_BODY; }
51void operator delete(void *ptr, std::nothrow_t const&) { OPERATOR_DELETE_BODY; }
52void operator delete[](void *ptr, std::nothrow_t const&) {
53 OPERATOR_DELETE_BODY;
54}
Evgeniy Stepanove8650452013-04-15 12:41:52 +000055
56#endif // MSAN_REPLACE_OPERATORS_NEW_AND_DELETE