blob: 9dbc7a127fcc29af3c98701b1f36cde403132216 [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:02 +00001//===-- asan_malloc_linux.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// Linux-specific malloc interception.
13// We simply define functions like malloc, free, realloc, etc.
14// They will replace the corresponding libc functions automagically.
15//===----------------------------------------------------------------------===//
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +000016#ifdef __linux__
Kostya Serebryany019b76f2011-11-30 01:07:02 +000017
18#include "asan_allocator.h"
19#include "asan_interceptors.h"
20#include "asan_internal.h"
21#include "asan_stack.h"
22
23#include <malloc.h>
24
25#define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
26
27#ifdef ANDROID
28struct MallocDebug {
29 void* (*malloc)(size_t bytes);
30 void (*free)(void* mem);
31 void* (*calloc)(size_t n_elements, size_t elem_size);
32 void* (*realloc)(void* oldMem, size_t bytes);
33 void* (*memalign)(size_t alignment, size_t bytes);
34};
35
36const MallocDebug asan_malloc_dispatch __attribute__((aligned(32))) = {
37 malloc, free, calloc, realloc, memalign
38};
39
40extern "C" const MallocDebug* __libc_malloc_dispatch;
41
42namespace __asan {
43void ReplaceSystemMalloc() {
44 __libc_malloc_dispatch = &asan_malloc_dispatch;
45}
46} // namespace __asan
47
48#else // ANDROID
49
50namespace __asan {
51void ReplaceSystemMalloc() {
52}
53} // namespace __asan
54#endif // ANDROID
55
56// ---------------------- Replacement functions ---------------- {{{1
57using namespace __asan; // NOLINT
58
59extern "C" {
60INTERCEPTOR_ATTRIBUTE
61void free(void *ptr) {
62 GET_STACK_TRACE_HERE_FOR_FREE(ptr);
63 asan_free(ptr, &stack);
64}
65
66INTERCEPTOR_ATTRIBUTE
67void cfree(void *ptr) {
68 GET_STACK_TRACE_HERE_FOR_FREE(ptr);
69 asan_free(ptr, &stack);
70}
71
72INTERCEPTOR_ATTRIBUTE
73void *malloc(size_t size) {
74 GET_STACK_TRACE_HERE_FOR_MALLOC;
75 return asan_malloc(size, &stack);
76}
77
78INTERCEPTOR_ATTRIBUTE
79void *calloc(size_t nmemb, size_t size) {
80 if (!asan_inited) {
81 // Hack: dlsym calls calloc before real_calloc is retrieved from dlsym.
82 const size_t kCallocPoolSize = 1024;
83 static uintptr_t calloc_memory_for_dlsym[kCallocPoolSize];
84 static size_t allocated;
85 size_t size_in_words = ((nmemb * size) + kWordSize - 1) / kWordSize;
86 void *mem = (void*)&calloc_memory_for_dlsym[allocated];
87 allocated += size_in_words;
88 CHECK(allocated < kCallocPoolSize);
89 return mem;
90 }
91 GET_STACK_TRACE_HERE_FOR_MALLOC;
92 return asan_calloc(nmemb, size, &stack);
93}
94
95INTERCEPTOR_ATTRIBUTE
96void *realloc(void *ptr, size_t size) {
97 GET_STACK_TRACE_HERE_FOR_MALLOC;
98 return asan_realloc(ptr, size, &stack);
99}
100
101INTERCEPTOR_ATTRIBUTE
102void *memalign(size_t boundary, size_t size) {
103 GET_STACK_TRACE_HERE_FOR_MALLOC;
104 return asan_memalign(boundary, size, &stack);
105}
106
107void* __libc_memalign(size_t align, size_t s)
108 __attribute__((alias("memalign")));
109
110INTERCEPTOR_ATTRIBUTE
111struct mallinfo mallinfo() {
112 struct mallinfo res;
113 real_memset(&res, 0, sizeof(res));
114 return res;
115}
116
117INTERCEPTOR_ATTRIBUTE
118int mallopt(int cmd, int value) {
119 return -1;
120}
121
122INTERCEPTOR_ATTRIBUTE
123int posix_memalign(void **memptr, size_t alignment, size_t size) {
124 GET_STACK_TRACE_HERE_FOR_MALLOC;
125 // Printf("posix_memalign: %lx %ld\n", alignment, size);
126 return asan_posix_memalign(memptr, alignment, size, &stack);
127}
128
129INTERCEPTOR_ATTRIBUTE
130void *valloc(size_t size) {
131 GET_STACK_TRACE_HERE_FOR_MALLOC;
132 return asan_valloc(size, &stack);
133}
134
135INTERCEPTOR_ATTRIBUTE
136void *pvalloc(size_t size) {
137 GET_STACK_TRACE_HERE_FOR_MALLOC;
138 return asan_pvalloc(size, &stack);
139}
140} // extern "C"
Kostya Serebryany5dfa4da2011-12-01 21:40:52 +0000141
142#endif // __linux__