1//===-- asan_malloc_linux.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file is a part of AddressSanitizer, an address sanity checker.
10//
11// Linux-specific malloc interception.
12// We simply define functions like malloc, free, realloc, etc.
13// They will replace the corresponding libc functions automagically.
14//===----------------------------------------------------------------------===//
15
16#include "sanitizer_common/sanitizer_platform.h"
17#if SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX || \
18 SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU || SANITIZER_AIX
19
20# include "asan_allocator.h"
21# include "asan_interceptors.h"
22# include "asan_internal.h"
23# include "asan_stack.h"
24# include "lsan/lsan_common.h"
25# include "sanitizer_common/sanitizer_allocator_checks.h"
26# include "sanitizer_common/sanitizer_allocator_dlsym.h"
27# include "sanitizer_common/sanitizer_errno.h"
28
29// ---------------------- Replacement functions ---------------- {{{1
30using namespace __asan;
31
32struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
33 static bool UseImpl() { return !TryAsanInitFromRtl(); }
34 static void OnAllocate(const void *ptr, uptr size) {
35# if CAN_SANITIZE_LEAKS
36 // Suppress leaks from dlerror(). Previously dlsym hack on global array was
37 // used by leak sanitizer as a root region.
38 __lsan_register_root_region(p: ptr, size);
39# endif
40 }
41 static void OnFree(const void *ptr, uptr size) {
42# if CAN_SANITIZE_LEAKS
43 __lsan_unregister_root_region(p: ptr, size);
44# endif
45 }
46};
47
48INTERCEPTOR(void, free, void *ptr) {
49 if (DlsymAlloc::PointerIsMine(ptr))
50 return DlsymAlloc::Free(ptr);
51 GET_STACK_TRACE_FREE;
52 asan_free(ptr, stack: &stack);
53}
54
55#if SANITIZER_INTERCEPT_CFREE
56INTERCEPTOR(void, cfree, void *ptr) {
57 if (DlsymAlloc::PointerIsMine(ptr))
58 return DlsymAlloc::Free(ptr);
59 GET_STACK_TRACE_FREE;
60 asan_free(ptr, stack: &stack);
61}
62#endif // SANITIZER_INTERCEPT_CFREE
63
64# if SANITIZER_INTERCEPT_FREE_SIZED
65INTERCEPTOR(void, free_sized, void* ptr, uptr size) {
66 if (UNLIKELY(!ptr))
67 return;
68 if (DlsymAlloc::PointerIsMine(ptr))
69 return DlsymAlloc::Free(ptr);
70 GET_STACK_TRACE_FREE;
71 asan_free_sized(ptr, size, stack: &stack);
72}
73# endif // SANITIZER_INTERCEPT_FREE_SIZED
74
75# if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
76INTERCEPTOR(void, free_aligned_sized, void* ptr, uptr alignment, uptr size) {
77 if (UNLIKELY(!ptr))
78 return;
79 if (DlsymAlloc::PointerIsMine(ptr))
80 return DlsymAlloc::Free(ptr);
81 GET_STACK_TRACE_FREE;
82 asan_free_aligned_sized(ptr, alignment, size, stack: &stack);
83}
84# endif // SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED
85
86# if SANITIZER_AIX
87// Unlike malloc, vec_malloc must return memory aligned to 16 bytes.
88INTERCEPTOR(void*, vec_malloc, uptr size) {
89 if (DlsymAlloc::Use())
90 return DlsymAlloc::Allocate(size, 16);
91 GET_STACK_TRACE_MALLOC;
92 return asan_vec_malloc(size, &stack);
93}
94
95// Unlike calloc, vec_calloc must return memory aligned to 16 bytes.
96INTERCEPTOR(void*, vec_calloc, uptr nmemb, uptr size) {
97 if (DlsymAlloc::Use())
98 return DlsymAlloc::Callocate(nmemb, size, 16);
99 GET_STACK_TRACE_MALLOC;
100 return asan_vec_calloc(nmemb, size, &stack);
101}
102# endif
103
104// TODO: Fix malloc/calloc interceptors to return 16-byte alignment with AIX on
105// PASE.
106INTERCEPTOR(void*, malloc, uptr size) {
107 if (DlsymAlloc::Use())
108 return DlsymAlloc::Allocate(size_in_bytes: size);
109 GET_STACK_TRACE_MALLOC;
110 return asan_malloc(size, stack: &stack);
111}
112
113INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
114 if (DlsymAlloc::Use())
115 return DlsymAlloc::Callocate(nmemb, size);
116 GET_STACK_TRACE_MALLOC;
117 return asan_calloc(nmemb, size, stack: &stack);
118}
119
120// TODO: AIX needs a method to ensure 16-byte alignment if the incoming
121// pointer was allocated with a 16-byte alignment requirement (or perhaps
122// merely if it happens to have 16-byte alignment).
123INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
124 if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
125 return DlsymAlloc::Realloc(ptr, new_size: size);
126 GET_STACK_TRACE_MALLOC;
127 return asan_realloc(p: ptr, size, stack: &stack);
128}
129
130#if SANITIZER_INTERCEPT_REALLOCARRAY
131INTERCEPTOR(void*, reallocarray, void *ptr, uptr nmemb, uptr size) {
132 AsanInitFromRtl();
133 GET_STACK_TRACE_MALLOC;
134 return asan_reallocarray(p: ptr, nmemb, size, stack: &stack);
135}
136#endif // SANITIZER_INTERCEPT_REALLOCARRAY
137
138#if SANITIZER_INTERCEPT_MEMALIGN
139INTERCEPTOR(void*, memalign, uptr boundary, uptr size) {
140 GET_STACK_TRACE_MALLOC;
141 return asan_memalign(alignment: boundary, size, stack: &stack);
142}
143
144INTERCEPTOR(void*, __libc_memalign, uptr boundary, uptr size) {
145 GET_STACK_TRACE_MALLOC;
146 return asan_memalign(alignment: boundary, size, stack: &stack);
147}
148#endif // SANITIZER_INTERCEPT_MEMALIGN
149
150#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
151INTERCEPTOR(void*, aligned_alloc, uptr boundary, uptr size) {
152 GET_STACK_TRACE_MALLOC;
153 return asan_aligned_alloc(alignment: boundary, size, stack: &stack);
154}
155#endif // SANITIZER_INTERCEPT_ALIGNED_ALLOC
156
157INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
158 GET_CURRENT_PC_BP_SP;
159 (void)sp;
160 return asan_malloc_usable_size(ptr, pc, bp);
161}
162
163#if SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
164// We avoid including malloc.h for portability reasons.
165// man mallinfo says the fields are "long", but the implementation uses int.
166// It doesn't matter much -- we just need to make sure that the libc's mallinfo
167// is not called.
168struct fake_mallinfo {
169 int x[10];
170};
171
172INTERCEPTOR(struct fake_mallinfo, mallinfo, void) {
173 struct fake_mallinfo res;
174 REAL(memset)(&res, 0, sizeof(res));
175 return res;
176}
177
178INTERCEPTOR(int, mallopt, int cmd, int value) {
179 return 0;
180}
181#endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
182
183INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
184 GET_STACK_TRACE_MALLOC;
185 return asan_posix_memalign(memptr, alignment, size, stack: &stack);
186}
187
188INTERCEPTOR(void*, valloc, uptr size) {
189 GET_STACK_TRACE_MALLOC;
190 return asan_valloc(size, stack: &stack);
191}
192
193#if SANITIZER_INTERCEPT_PVALLOC
194INTERCEPTOR(void*, pvalloc, uptr size) {
195 GET_STACK_TRACE_MALLOC;
196 return asan_pvalloc(size, stack: &stack);
197}
198#endif // SANITIZER_INTERCEPT_PVALLOC
199
200INTERCEPTOR(void, malloc_stats, void) {
201 __asan_print_accumulated_stats();
202}
203
204#if SANITIZER_ANDROID
205// Format of __libc_malloc_dispatch has changed in Android L.
206// While we are moving towards a solution that does not depend on bionic
207// internals, here is something to support both K* and L releases.
208struct MallocDebugK {
209 void *(*malloc)(uptr bytes);
210 void (*free)(void *mem);
211 void *(*calloc)(uptr n_elements, uptr elem_size);
212 void *(*realloc)(void *oldMem, uptr bytes);
213 void *(*memalign)(uptr alignment, uptr bytes);
214 uptr (*malloc_usable_size)(void *mem);
215};
216
217struct MallocDebugL {
218 void *(*calloc)(uptr n_elements, uptr elem_size);
219 void (*free)(void *mem);
220 fake_mallinfo (*mallinfo)(void);
221 void *(*malloc)(uptr bytes);
222 uptr (*malloc_usable_size)(void *mem);
223 void *(*memalign)(uptr alignment, uptr bytes);
224 int (*posix_memalign)(void **memptr, uptr alignment, uptr size);
225 void* (*pvalloc)(uptr size);
226 void *(*realloc)(void *oldMem, uptr bytes);
227 void* (*valloc)(uptr size);
228};
229
230alignas(32) const MallocDebugK asan_malloc_dispatch_k = {
231 WRAP(malloc), WRAP(free), WRAP(calloc),
232 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
233
234alignas(32) const MallocDebugL asan_malloc_dispatch_l = {
235 WRAP(calloc), WRAP(free), WRAP(mallinfo),
236 WRAP(malloc), WRAP(malloc_usable_size), WRAP(memalign),
237 WRAP(posix_memalign), WRAP(pvalloc), WRAP(realloc),
238 WRAP(valloc)};
239
240namespace __asan {
241void ReplaceSystemMalloc() {
242 void **__libc_malloc_dispatch_p =
243 (void **)AsanDlSymNext("__libc_malloc_dispatch");
244 if (__libc_malloc_dispatch_p) {
245 // Decide on K vs L dispatch format by the presence of
246 // __libc_malloc_default_dispatch export in libc.
247 void *default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
248 if (default_dispatch_p)
249 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_k;
250 else
251 *__libc_malloc_dispatch_p = (void *)&asan_malloc_dispatch_l;
252 }
253}
254} // namespace __asan
255
256#else // SANITIZER_ANDROID
257
258namespace __asan {
259void ReplaceSystemMalloc() {
260}
261} // namespace __asan
262#endif // SANITIZER_ANDROID
263
264#endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||
265 // SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU
266