asan_new_delete.cc revision dbe60352ef7240327f15f813140c9726854f6b85
1//===-- asan_interceptors.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 AddressSanitizer, an address sanity checker.
11//
12// Interceptors for operators new and delete.
13//===----------------------------------------------------------------------===//
14
15#include "asan_allocator.h"
16#include "asan_internal.h"
17#include "asan_stack.h"
18
19#include <stddef.h>
20#include <new>
21
22namespace __asan {
23// This function is a no-op. We need it to make sure that object file
24// with our replacements will actually be loaded from static ASan
25// run-time library at link-time.
26void ReplaceOperatorsNewAndDelete() { }
27}
28
29using namespace __asan;  // NOLINT
30
31// On Android new() goes through malloc interceptors.
32#if !ASAN_ANDROID
33
34#define OPERATOR_NEW_BODY \
35  GET_STACK_TRACE_HERE_FOR_MALLOC;\
36  return asan_memalign(0, size, &stack);
37
38INTERCEPTOR_ATTRIBUTE
39void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
40INTERCEPTOR_ATTRIBUTE
41void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
42INTERCEPTOR_ATTRIBUTE
43void *operator new(size_t size, std::nothrow_t const&) throw()
44{ OPERATOR_NEW_BODY; }
45INTERCEPTOR_ATTRIBUTE
46void *operator new[](size_t size, std::nothrow_t const&) throw()
47{ OPERATOR_NEW_BODY; }
48
49#define OPERATOR_DELETE_BODY \
50  GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
51  asan_free(ptr, &stack);
52
53INTERCEPTOR_ATTRIBUTE
54void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
55INTERCEPTOR_ATTRIBUTE
56void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
57INTERCEPTOR_ATTRIBUTE
58void operator delete(void *ptr, std::nothrow_t const&) throw()
59{ OPERATOR_DELETE_BODY; }
60INTERCEPTOR_ATTRIBUTE
61void operator delete[](void *ptr, std::nothrow_t const&) throw()
62{ OPERATOR_DELETE_BODY; }
63
64#endif
65