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 (and Unix/Unix-like) 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) { return 0; }
179# endif // SANITIZER_INTERCEPT_MALLOPT_AND_MALLINFO
180
181INTERCEPTOR(int, posix_memalign, void** memptr, uptr alignment, uptr size) {
182 GET_STACK_TRACE_MALLOC;
183 return asan_posix_memalign(memptr, alignment, size, stack: &stack);
184}
185
186INTERCEPTOR(void*, valloc, uptr size) {
187 GET_STACK_TRACE_MALLOC;
188 return asan_valloc(size, stack: &stack);
189}
190
191# if SANITIZER_INTERCEPT_PVALLOC
192INTERCEPTOR(void*, pvalloc, uptr size) {
193 GET_STACK_TRACE_MALLOC;
194 return asan_pvalloc(size, stack: &stack);
195}
196# endif // SANITIZER_INTERCEPT_PVALLOC
197
198INTERCEPTOR(void, malloc_stats, void) { __asan_print_accumulated_stats(); }
199
200# if SANITIZER_ANDROID
201// Format of __libc_malloc_dispatch has changed in Android L.
202// While we are moving towards a solution that does not depend on bionic
203// internals, here is something to support both K* and L releases.
204struct MallocDebugK {
205 void* (*malloc)(uptr bytes);
206 void (*free)(void* mem);
207 void* (*calloc)(uptr n_elements, uptr elem_size);
208 void* (*realloc)(void* oldMem, uptr bytes);
209 void* (*memalign)(uptr alignment, uptr bytes);
210 uptr (*malloc_usable_size)(void* mem);
211};
212
213struct MallocDebugL {
214 void* (*calloc)(uptr n_elements, uptr elem_size);
215 void (*free)(void* mem);
216 fake_mallinfo (*mallinfo)(void);
217 void* (*malloc)(uptr bytes);
218 uptr (*malloc_usable_size)(void* mem);
219 void* (*memalign)(uptr alignment, uptr bytes);
220 int (*posix_memalign)(void** memptr, uptr alignment, uptr size);
221 void* (*pvalloc)(uptr size);
222 void* (*realloc)(void* oldMem, uptr bytes);
223 void* (*valloc)(uptr size);
224};
225
226alignas(32) const MallocDebugK asan_malloc_dispatch_k = {
227 WRAP(malloc), WRAP(free), WRAP(calloc),
228 WRAP(realloc), WRAP(memalign), WRAP(malloc_usable_size)};
229
230alignas(32) const MallocDebugL asan_malloc_dispatch_l = {
231 WRAP(calloc),
232 WRAP(free),
233 WRAP(mallinfo),
234 WRAP(malloc),
235 WRAP(malloc_usable_size),
236 WRAP(memalign),
237 WRAP(posix_memalign),
238 WRAP(pvalloc),
239 WRAP(realloc),
240 WRAP(valloc)};
241
242namespace __asan {
243void ReplaceSystemMalloc() {
244 void** __libc_malloc_dispatch_p =
245 (void**)AsanDlSymNext("__libc_malloc_dispatch");
246 if (__libc_malloc_dispatch_p) {
247 // Decide on K vs L dispatch format by the presence of
248 // __libc_malloc_default_dispatch export in libc.
249 void* default_dispatch_p = AsanDlSymNext("__libc_malloc_default_dispatch");
250 if (default_dispatch_p)
251 *__libc_malloc_dispatch_p = (void*)&asan_malloc_dispatch_k;
252 else
253 *__libc_malloc_dispatch_p = (void*)&asan_malloc_dispatch_l;
254 }
255}
256} // namespace __asan
257
258# else // SANITIZER_ANDROID
259
260namespace __asan {
261void ReplaceSystemMalloc() {}
262} // namespace __asan
263# endif // SANITIZER_ANDROID
264
265#endif // SANITIZER_FREEBSD || SANITIZER_FUCHSIA || SANITIZER_LINUX ||
266 // SANITIZER_NETBSD || SANITIZER_SOLARIS || SANITIZER_HAIKU
267