xref: /freebsd/contrib/llvm-project/compiler-rt/lib/hwasan/hwasan_allocation_functions.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===-- hwasan_allocation_functions.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 HWAddressSanitizer.
10 //
11 // Definitions for __sanitizer allocation functions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "hwasan.h"
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_allocator_dlsym.h"
18 #include "sanitizer_common/sanitizer_allocator_interface.h"
19 #include "sanitizer_common/sanitizer_tls_get_addr.h"
20 
21 #if !SANITIZER_FUCHSIA
22 
23 using namespace __hwasan;
24 
25 struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
26   static bool UseImpl() { return !hwasan_inited; }
27 };
28 
29 extern "C" {
30 
31 SANITIZER_INTERFACE_ATTRIBUTE
32 int __sanitizer_posix_memalign(void **memptr, uptr alignment, uptr size) {
33   GET_MALLOC_STACK_TRACE;
34   CHECK_NE(memptr, 0);
35   int res = hwasan_posix_memalign(memptr, alignment, size, &stack);
36   return res;
37 }
38 
39 SANITIZER_INTERFACE_ATTRIBUTE
40 void *__sanitizer_memalign(uptr alignment, uptr size) {
41   GET_MALLOC_STACK_TRACE;
42   return hwasan_memalign(alignment, size, &stack);
43 }
44 
45 SANITIZER_INTERFACE_ATTRIBUTE
46 void *__sanitizer_aligned_alloc(uptr alignment, uptr size) {
47   GET_MALLOC_STACK_TRACE;
48   return hwasan_aligned_alloc(alignment, size, &stack);
49 }
50 
51 SANITIZER_INTERFACE_ATTRIBUTE
52 void *__sanitizer___libc_memalign(uptr alignment, uptr size) {
53   GET_MALLOC_STACK_TRACE;
54   void *ptr = hwasan_memalign(alignment, size, &stack);
55   if (ptr)
56     DTLS_on_libc_memalign(ptr, size);
57   return ptr;
58 }
59 
60 SANITIZER_INTERFACE_ATTRIBUTE
61 void *__sanitizer_valloc(uptr size) {
62   GET_MALLOC_STACK_TRACE;
63   return hwasan_valloc(size, &stack);
64 }
65 
66 SANITIZER_INTERFACE_ATTRIBUTE
67 void *__sanitizer_pvalloc(uptr size) {
68   GET_MALLOC_STACK_TRACE;
69   return hwasan_pvalloc(size, &stack);
70 }
71 
72 SANITIZER_INTERFACE_ATTRIBUTE
73 void __sanitizer_free(void *ptr) {
74   if (!ptr)
75     return;
76   if (DlsymAlloc::PointerIsMine(ptr))
77     return DlsymAlloc::Free(ptr);
78   GET_MALLOC_STACK_TRACE;
79   hwasan_free(ptr, &stack);
80 }
81 
82 SANITIZER_INTERFACE_ATTRIBUTE
83 void __sanitizer_cfree(void *ptr) {
84   if (!ptr)
85     return;
86   if (DlsymAlloc::PointerIsMine(ptr))
87     return DlsymAlloc::Free(ptr);
88   GET_MALLOC_STACK_TRACE;
89   hwasan_free(ptr, &stack);
90 }
91 
92 SANITIZER_INTERFACE_ATTRIBUTE
93 uptr __sanitizer_malloc_usable_size(const void *ptr) {
94   return __sanitizer_get_allocated_size(ptr);
95 }
96 
97 SANITIZER_INTERFACE_ATTRIBUTE
98 struct __sanitizer_struct_mallinfo __sanitizer_mallinfo() {
99   __sanitizer_struct_mallinfo sret;
100   internal_memset(&sret, 0, sizeof(sret));
101   return sret;
102 }
103 
104 SANITIZER_INTERFACE_ATTRIBUTE
105 int __sanitizer_mallopt(int cmd, int value) { return 0; }
106 
107 SANITIZER_INTERFACE_ATTRIBUTE
108 void __sanitizer_malloc_stats(void) {
109   // FIXME: implement, but don't call REAL(malloc_stats)!
110 }
111 
112 SANITIZER_INTERFACE_ATTRIBUTE
113 void *__sanitizer_calloc(uptr nmemb, uptr size) {
114   if (DlsymAlloc::Use())
115     return DlsymAlloc::Callocate(nmemb, size);
116   GET_MALLOC_STACK_TRACE;
117   return hwasan_calloc(nmemb, size, &stack);
118 }
119 
120 SANITIZER_INTERFACE_ATTRIBUTE
121 void *__sanitizer_realloc(void *ptr, uptr size) {
122   if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
123     return DlsymAlloc::Realloc(ptr, size);
124   GET_MALLOC_STACK_TRACE;
125   return hwasan_realloc(ptr, size, &stack);
126 }
127 
128 SANITIZER_INTERFACE_ATTRIBUTE
129 void *__sanitizer_reallocarray(void *ptr, uptr nmemb, uptr size) {
130   GET_MALLOC_STACK_TRACE;
131   return hwasan_reallocarray(ptr, nmemb, size, &stack);
132 }
133 
134 SANITIZER_INTERFACE_ATTRIBUTE
135 void *__sanitizer_malloc(uptr size) {
136   if (UNLIKELY(!hwasan_init_is_running))
137     ENSURE_HWASAN_INITED();
138   if (DlsymAlloc::Use())
139     return DlsymAlloc::Allocate(size);
140   GET_MALLOC_STACK_TRACE;
141   return hwasan_malloc(size, &stack);
142 }
143 
144 }  // extern "C"
145 
146 #if HWASAN_WITH_INTERCEPTORS
147 #  define INTERCEPTOR_ALIAS(RET, FN, ARGS...)                                 \
148     extern "C" SANITIZER_INTERFACE_ATTRIBUTE RET WRAP(FN)(ARGS)               \
149         ALIAS("__sanitizer_" #FN);                                            \
150     extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE RET FN( \
151         ARGS) ALIAS("__sanitizer_" #FN)
152 
153 INTERCEPTOR_ALIAS(int, posix_memalign, void **memptr, SIZE_T alignment,
154                   SIZE_T size);
155 INTERCEPTOR_ALIAS(void *, aligned_alloc, SIZE_T alignment, SIZE_T size);
156 INTERCEPTOR_ALIAS(void *, __libc_memalign, SIZE_T alignment, SIZE_T size);
157 INTERCEPTOR_ALIAS(void *, valloc, SIZE_T size);
158 INTERCEPTOR_ALIAS(void, free, void *ptr);
159 INTERCEPTOR_ALIAS(uptr, malloc_usable_size, const void *ptr);
160 INTERCEPTOR_ALIAS(void *, calloc, SIZE_T nmemb, SIZE_T size);
161 INTERCEPTOR_ALIAS(void *, realloc, void *ptr, SIZE_T size);
162 INTERCEPTOR_ALIAS(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size);
163 INTERCEPTOR_ALIAS(void *, malloc, SIZE_T size);
164 
165 #  if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
166 INTERCEPTOR_ALIAS(void *, memalign, SIZE_T alignment, SIZE_T size);
167 INTERCEPTOR_ALIAS(void *, pvalloc, SIZE_T size);
168 INTERCEPTOR_ALIAS(void, cfree, void *ptr);
169 INTERCEPTOR_ALIAS(__sanitizer_struct_mallinfo, mallinfo);
170 INTERCEPTOR_ALIAS(int, mallopt, int cmd, int value);
171 INTERCEPTOR_ALIAS(void, malloc_stats, void);
172 #  endif
173 #endif  // #if HWASAN_WITH_INTERCEPTORS
174 
175 #endif  // SANITIZER_FUCHSIA
176