blob: e48bdaf03dd322e5aaacf2d952b1f3fc2de2725b [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
Stephen Hines6d186232014-11-26 17:56:19 -080019#include "interception/interception.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070020
Kostya Serebryanycfd605e2012-06-01 07:41:04 +000021#include <stddef.h>
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000022
Stephen Hines2d1fdb22014-05-28 23:58:16 -070023// C++ operators can't have visibility attributes on Windows.
24#if SANITIZER_WINDOWS
25# define CXX_OPERATOR_ATTRIBUTE
26#else
27# define CXX_OPERATOR_ATTRIBUTE INTERCEPTOR_ATTRIBUTE
28#endif
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000029
30using namespace __asan; // NOLINT
31
Stephen Hines2d1fdb22014-05-28 23:58:16 -070032// This code has issues on OSX.
33// See https://code.google.com/p/address-sanitizer/issues/detail?id=131.
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +000034
Alexey Samsonov3aa15c12012-11-08 13:31:19 +000035// Fake std::nothrow_t to avoid including <new>.
36namespace std {
37struct nothrow_t {};
38} // namespace std
39
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000040#define OPERATOR_NEW_BODY(type) \
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000041 GET_STACK_TRACE_MALLOC;\
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000042 return asan_memalign(0, size, &stack, type);
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000043
Alexander Potapenkod4228012013-02-21 16:54:09 +000044// On OS X it's not enough to just provide our own 'operator new' and
45// 'operator delete' implementations, because they're going to be in the
46// runtime dylib, and the main executable will depend on both the runtime
47// dylib and libstdc++, each of those'll have its implementation of new and
48// delete.
49// To make sure that C++ allocation/deallocation operators are overridden on
50// OS X we need to intercept them using their mangled names.
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000051#if !SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -070052// FreeBSD prior v9.2 have wrong definition of 'size_t'.
53// http://svnweb.freebsd.org/base?view=revision&revision=232261
54#if SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 32
55#include <sys/param.h>
56#if __FreeBSD_version <= 902001 // v9.2
57#define size_t unsigned
58#endif // __FreeBSD_version
59#endif // SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 32
60
61CXX_OPERATOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000062void *operator new(size_t size) { OPERATOR_NEW_BODY(FROM_NEW); }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070063CXX_OPERATOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000064void *operator new[](size_t size) { OPERATOR_NEW_BODY(FROM_NEW_BR); }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070065CXX_OPERATOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000066void *operator new(size_t size, std::nothrow_t const&)
67{ OPERATOR_NEW_BODY(FROM_NEW); }
Stephen Hines2d1fdb22014-05-28 23:58:16 -070068CXX_OPERATOR_ATTRIBUTE
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000069void *operator new[](size_t size, std::nothrow_t const&)
70{ OPERATOR_NEW_BODY(FROM_NEW_BR); }
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000071
Alexey Samsonov649a2702013-04-03 07:29:53 +000072#else // SANITIZER_MAC
Alexander Potapenkod4228012013-02-21 16:54:09 +000073INTERCEPTOR(void *, _Znwm, size_t size) {
74 OPERATOR_NEW_BODY(FROM_NEW);
75}
76INTERCEPTOR(void *, _Znam, size_t size) {
77 OPERATOR_NEW_BODY(FROM_NEW_BR);
78}
79INTERCEPTOR(void *, _ZnwmRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
80 OPERATOR_NEW_BODY(FROM_NEW);
81}
82INTERCEPTOR(void *, _ZnamRKSt9nothrow_t, size_t size, std::nothrow_t const&) {
83 OPERATOR_NEW_BODY(FROM_NEW_BR);
84}
85#endif
86
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000087#define OPERATOR_DELETE_BODY(type) \
Kostya Serebryanya30c8f92012-12-13 09:34:23 +000088 GET_STACK_TRACE_FREE;\
Kostya Serebryanyfe6d9162012-12-21 08:53:59 +000089 asan_free(ptr, &stack, type);
Alexey Samsonov4d5f98d2012-04-06 08:21:08 +000090
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000091#if !SANITIZER_MAC
Stephen Hines2d1fdb22014-05-28 23:58:16 -070092CXX_OPERATOR_ATTRIBUTE
93void operator delete(void *ptr) throw() {
94 OPERATOR_DELETE_BODY(FROM_NEW);
95}
96CXX_OPERATOR_ATTRIBUTE
97void operator delete[](void *ptr) throw() {
98 OPERATOR_DELETE_BODY(FROM_NEW_BR);
99}
100CXX_OPERATOR_ATTRIBUTE
101void operator delete(void *ptr, std::nothrow_t const&) {
102 OPERATOR_DELETE_BODY(FROM_NEW);
103}
104CXX_OPERATOR_ATTRIBUTE
105void operator delete[](void *ptr, std::nothrow_t const&) {
106 OPERATOR_DELETE_BODY(FROM_NEW_BR);
107}
Stephen Hines6d186232014-11-26 17:56:19 -0800108CXX_OPERATOR_ATTRIBUTE
109void operator delete(void *ptr, size_t size) throw() {
110 GET_STACK_TRACE_FREE;
111 asan_sized_free(ptr, size, &stack, FROM_NEW);
112}
113CXX_OPERATOR_ATTRIBUTE
114void operator delete[](void *ptr, size_t size) throw() {
115 GET_STACK_TRACE_FREE;
116 asan_sized_free(ptr, size, &stack, FROM_NEW_BR);
117}
Evgeniy Stepanovdbe60352012-10-18 15:22:08 +0000118
Alexey Samsonov649a2702013-04-03 07:29:53 +0000119#else // SANITIZER_MAC
Alexander Potapenkod4228012013-02-21 16:54:09 +0000120INTERCEPTOR(void, _ZdlPv, void *ptr) {
121 OPERATOR_DELETE_BODY(FROM_NEW);
122}
123INTERCEPTOR(void, _ZdaPv, void *ptr) {
124 OPERATOR_DELETE_BODY(FROM_NEW_BR);
125}
126INTERCEPTOR(void, _ZdlPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&) {
127 OPERATOR_DELETE_BODY(FROM_NEW);
128}
129INTERCEPTOR(void, _ZdaPvRKSt9nothrow_t, void *ptr, std::nothrow_t const&) {
130 OPERATOR_DELETE_BODY(FROM_NEW_BR);
131}
132#endif