asan_new_delete.cc revision cfd605e96209d18c937501be870bd0308131e453
1//===-- asan_interceptors.cc ------------------------------------*- C++ -*-===//
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#define OPERATOR_NEW_BODY \
32  GET_STACK_TRACE_HERE_FOR_MALLOC;\
33  return asan_memalign(0, size, &stack);
34
35#ifdef ANDROID
36void *operator new(size_t size) { OPERATOR_NEW_BODY; }
37void *operator new[](size_t size) { OPERATOR_NEW_BODY; }
38#else
39void *operator new(size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
40void *operator new[](size_t size) throw(std::bad_alloc) { OPERATOR_NEW_BODY; }
41void *operator new(size_t size, std::nothrow_t const&) throw()
42{ OPERATOR_NEW_BODY; }
43void *operator new[](size_t size, std::nothrow_t const&) throw()
44{ OPERATOR_NEW_BODY; }
45#endif
46
47#define OPERATOR_DELETE_BODY \
48  GET_STACK_TRACE_HERE_FOR_FREE(ptr);\
49  asan_free(ptr, &stack);
50
51void operator delete(void *ptr) throw() { OPERATOR_DELETE_BODY; }
52void operator delete[](void *ptr) throw() { OPERATOR_DELETE_BODY; }
53void operator delete(void *ptr, std::nothrow_t const&) throw()
54{ OPERATOR_DELETE_BODY; }
55void operator delete[](void *ptr, std::nothrow_t const&) throw()
56{ OPERATOR_DELETE_BODY; }
57