blob: 5d1f23c542e6abdc11ca123a197b62bb24c88234 [file] [log] [blame]
Alexey Samsonov485d3dc2012-06-04 13:50:10 +00001//===-- asan_interceptors.cc ----------------------------------------------===//
Alexey Samsonov6a19d5d2012-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 Serebryany02845c12012-06-01 07:41:04 +000019#include <stddef.h>
Alexey Samsonov6a19d5d2012-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
Evgeniy Stepanovad182872012-10-18 15:22:08 +000030// On Android new() goes through malloc interceptors.
31#if !ASAN_ANDROID
32
Alexey Samsonov37119db2012-11-08 13:31:19 +000033// Fake std::nothrow_t to avoid including <new>.
34namespace std {
35struct nothrow_t {};
36} // namespace std
37
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000038#define OPERATOR_NEW_BODY(type) \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000039 GET_STACK_TRACE_MALLOC;\
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000040 return asan_memalign(0, size, &stack, type);
Alexey Samsonov6a19d5d2012-04-06 08:21:08 +000041
Evgeniy Stepanovad182872012-10-18 15:22:08 +000042INTERCEPTOR_ATTRIBUTE
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000043void *operator new(size_t size) { OPERATOR_NEW_BODY(FROM_NEW); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000044INTERCEPTOR_ATTRIBUTE
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000045void *operator new[](size_t size) { OPERATOR_NEW_BODY(FROM_NEW_BR); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000046INTERCEPTOR_ATTRIBUTE
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000047void *operator new(size_t size, std::nothrow_t const&)
48{ OPERATOR_NEW_BODY(FROM_NEW); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000049INTERCEPTOR_ATTRIBUTE
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000050void *operator new[](size_t size, std::nothrow_t const&)
51{ OPERATOR_NEW_BODY(FROM_NEW_BR); }
Alexey Samsonov6a19d5d2012-04-06 08:21:08 +000052
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000053#define OPERATOR_DELETE_BODY(type) \
Kostya Serebryanybaf583c2012-12-13 09:34:23 +000054 GET_STACK_TRACE_FREE;\
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000055 asan_free(ptr, &stack, type);
Alexey Samsonov6a19d5d2012-04-06 08:21:08 +000056
Evgeniy Stepanovad182872012-10-18 15:22:08 +000057INTERCEPTOR_ATTRIBUTE
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000058void operator delete(void *ptr) { OPERATOR_DELETE_BODY(FROM_NEW); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000059INTERCEPTOR_ATTRIBUTE
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000060void operator delete[](void *ptr) { OPERATOR_DELETE_BODY(FROM_NEW_BR); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000061INTERCEPTOR_ATTRIBUTE
Alexey Samsonov37119db2012-11-08 13:31:19 +000062void operator delete(void *ptr, std::nothrow_t const&)
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000063{ OPERATOR_DELETE_BODY(FROM_NEW); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000064INTERCEPTOR_ATTRIBUTE
Alexey Samsonov37119db2012-11-08 13:31:19 +000065void operator delete[](void *ptr, std::nothrow_t const&)
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000066{ OPERATOR_DELETE_BODY(FROM_NEW_BR); }
Evgeniy Stepanovad182872012-10-18 15:22:08 +000067
68#endif