A set of trivial changes to support sanitizers on FreeBSD.
[blocksruntime.git] / lib / asan / asan_malloc_linux.cc
blobf676dc2768a05b1a9af2c9b8fed4e2c11a3eb853
1 //===-- asan_malloc_linux.cc ----------------------------------------------===//
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.
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 //===----------------------------------------------------------------------===//
17 #include "sanitizer_common/sanitizer_platform.h"
18 #if SANITIZER_FREEBSD || SANITIZER_LINUX
20 #include "sanitizer_common/sanitizer_tls_get_addr.h"
21 #include "asan_allocator.h"
22 #include "asan_interceptors.h"
23 #include "asan_internal.h"
24 #include "asan_stack.h"
26 #if SANITIZER_ANDROID
27 DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, uptr size)
28 DECLARE_REAL_AND_INTERCEPTOR(void, free, void *ptr)
29 DECLARE_REAL_AND_INTERCEPTOR(void*, calloc, uptr nmemb, uptr size)
30 DECLARE_REAL_AND_INTERCEPTOR(void*, realloc, void *ptr, uptr size)
31 DECLARE_REAL_AND_INTERCEPTOR(void*, memalign, uptr boundary, uptr size)
33 struct MallocDebug {
34 void* (*malloc)(uptr bytes);
35 void (*free)(void* mem);
36 void* (*calloc)(uptr n_elements, uptr elem_size);
37 void* (*realloc)(void* oldMem, uptr bytes);
38 void* (*memalign)(uptr alignment, uptr bytes);
41 const MallocDebug asan_malloc_dispatch ALIGNED(32) = {
42 WRAP(malloc), WRAP(free), WRAP(calloc), WRAP(realloc), WRAP(memalign)
45 extern "C" const MallocDebug* __libc_malloc_dispatch;
47 namespace __asan {
48 void ReplaceSystemMalloc() {
49 __libc_malloc_dispatch = &asan_malloc_dispatch;
51 } // namespace __asan
53 #else // ANDROID
55 namespace __asan {
56 void ReplaceSystemMalloc() {
58 } // namespace __asan
59 #endif // ANDROID
61 // ---------------------- Replacement functions ---------------- {{{1
62 using namespace __asan; // NOLINT
64 INTERCEPTOR(void, free, void *ptr) {
65 GET_STACK_TRACE_FREE;
66 asan_free(ptr, &stack, FROM_MALLOC);
69 INTERCEPTOR(void, cfree, void *ptr) {
70 GET_STACK_TRACE_FREE;
71 asan_free(ptr, &stack, FROM_MALLOC);
74 INTERCEPTOR(void*, malloc, uptr size) {
75 GET_STACK_TRACE_MALLOC;
76 return asan_malloc(size, &stack);
79 INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
80 if (!asan_inited) {
81 // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
82 const uptr kCallocPoolSize = 1024;
83 static uptr calloc_memory_for_dlsym[kCallocPoolSize];
84 static uptr allocated;
85 uptr 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;
91 GET_STACK_TRACE_MALLOC;
92 return asan_calloc(nmemb, size, &stack);
95 INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
96 GET_STACK_TRACE_MALLOC;
97 return asan_realloc(ptr, size, &stack);
100 INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
101 GET_STACK_TRACE_MALLOC;
102 return asan_memalign(boundary, size, &stack, FROM_MALLOC);
105 INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
106 GET_STACK_TRACE_MALLOC;
107 void *res = asan_memalign(boundary, size, &stack, FROM_MALLOC);
108 DTLS_on_libc_memalign(res, size * boundary);
109 return res;
112 INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
113 GET_CURRENT_PC_BP_SP;
114 (void)sp;
115 return asan_malloc_usable_size(ptr, pc, bp);
118 // We avoid including malloc.h for portability reasons.
119 // man mallinfo says the fields are "long", but the implementation uses int.
120 // It doesn't matter much -- we just need to make sure that the libc's mallinfo
121 // is not called.
122 struct fake_mallinfo {
123 int x[10];
126 INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
127 struct fake_mallinfo res;
128 REAL(memset)(&res, 0, sizeof(res));
129 return res;
132 INTERCEPTOR(int, mallopt, int cmd, int value) {
133 return -1;
136 INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
137 GET_STACK_TRACE_MALLOC;
138 // Printf("posix_memalign: %zx %zu\n", alignment, size);
139 return asan_posix_memalign(memptr, alignment, size, &stack);
142 INTERCEPTOR(void*, valloc, uptr size) {
143 GET_STACK_TRACE_MALLOC;
144 return asan_valloc(size, &stack);
147 INTERCEPTOR(void*, pvalloc, uptr size) {
148 GET_STACK_TRACE_MALLOC;
149 return asan_pvalloc(size, &stack);
152 INTERCEPTOR(void, malloc_stats, void) {
153 __asan_print_accumulated_stats();
156 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX