blob: d5eb6eca9321d852be79714c24eb956001644c22 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_interceptors.cc ----------------------------------------------===//
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +00002//
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
Kostya Serebryanycfd605e2012-06-01 07:41:04 +000019#include <stddef.h>
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000020
21namespace __asan {
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 ASan
24// run-time library at link-time.
25void ReplaceOperatorsNewAndDelete() { }
26}
27
28using namespace __asan; // NOLINT
29
Alexander Potapenkoed4c0342013-02-07 15:33:56 +000030// On Android new() goes through malloc interceptors.
Alexander Potapenko6fcbff52013-01-22 11:39:28 +000031// See also https://code.google.com/p/address-sanitizer/issues/detail?id=131.
Evgeniy Stepanov83cb7872013-03-19 13:54:41 +000032#if !SANITIZER_ANDROID
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000033
Alexey Samsonov3aa15c12012-11-08 13:31:19 +000034// Fake std::nothrow_t to avoid including <new>.
35namespace std {
36struct nothrow_t {};
37} // namespace std
38
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000039#define OPERATOR_NEW_BODY(type) \
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000040 GET_STACK_TRACE_MALLOC;\
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000041 return asan_memalign(0, size, &stack, type);
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000042
Alexander Potapenkod4228012013-02-21 16:54:09 +000043// On OS X it's not enough to just provide our own 'operator new' and
44// 'operator delete' implementations, because they're going to be in the
45// runtime dylib, and the main executable will depend on both the runtime
46// dylib and libstdc++, each of those'll have its implementation of new and
47// delete.
48// To make sure that C++ allocation/deallocation operators are overridden on
49// OS X we need to intercept them using their mangled names.
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000050#if !SANITIZER_MAC
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000051INTERCEPTOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000052void *operator new(size_t size) { OPERATOR_NEW_BODY(FROM_NEW); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000053INTERCEPTOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000054void *operator new[](size_t size) { OPERATOR_NEW_BODY(FROM_NEW_BR); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000055INTERCEPTOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000056void *operator new(size_t size, std::nothrow_t const&)
57{ OPERATOR_NEW_BODY(FROM_NEW); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000058INTERCEPTOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000059void *operator new[](size_t size, std::nothrow_t const&)
60{ OPERATOR_NEW_BODY(FROM_NEW_BR); }
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000061
Alexey Samsonov649a2702013-04-03 07:29:53 +000062#else // SANITIZER_MAC
Alexander Potapenkod4228012013-02-21 16:54:09 +000063INTERCEPTOR(void *, _Znwm, size_t size) {
64 OPERATOR_NEW_BODY(FROM_NEW);
65}
66INTERCEPTOR(void *, _Znam, size_t size) {
67 OPERATOR_NEW_BODY(FROM_NEW_BR);
68}
69INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
70 OPERATOR_NEW_BODY(FROM_NEW);
71}
72INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
73 OPERATOR_NEW_BODY(FROM_NEW_BR);
74}
75#endif
76
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000077#define OPERATOR_DELETE_BODY(type) \
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000078 GET_STACK_TRACE_FREE;\
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000079 asan_free(ptr, &stack, type);
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000080
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000081#if !SANITIZER_MAC
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000082INTERCEPTOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000083void operator delete(void *ptr) { OPERATOR_DELETE_BODY(FROM_NEW); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000084INTERCEPTOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000085void operator delete[](void *ptr) { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000086INTERCEPTOR_ATTRIBUTE
Alexey Samsonov3aa15c12012-11-08 13:31:19 +000087void operator delete(void *ptr, std::nothrow_t const&)
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000088{ OPERATOR_DELETE_BODY(FROM_NEW); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000089INTERCEPTOR_ATTRIBUTE
Alexey Samsonov3aa15c12012-11-08 13:31:19 +000090void operator delete[](void *ptr, std::nothrow_t const&)
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000091{ OPERATOR_DELETE_BODY(FROM_NEW_BR); }
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000092
Alexey Samsonov649a2702013-04-03 07:29:53 +000093#else // SANITIZER_MAC
Alexander Potapenkod4228012013-02-21 16:54:09 +000094INTERCEPTOR(void, _ZdlPv, void *ptr) {
95 OPERATOR_DELETE_BODY(FROM_NEW);
96}
97INTERCEPTOR(void, _ZdaPv, void *ptr) {
98 OPERATOR_DELETE_BODY(FROM_NEW_BR);
99}
100INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&) {
101 OPERATOR_DELETE_BODY(FROM_NEW);
102}
103INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&) {
104 OPERATOR_DELETE_BODY(FROM_NEW_BR);
105}
106#endif
107
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +0000108#endif