1 //===-- hwasan_allocation_functions.cpp -----------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of HWAddressSanitizer.
11 // Definitions for __sanitizer allocation functions.
13 //===----------------------------------------------------------------------===//
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"
21 #if !SANITIZER_FUCHSIA
23 using namespace __hwasan
;
25 struct DlsymAlloc
: public DlSymAllocator
<DlsymAlloc
> {
26 static bool UseImpl() { return !hwasan_inited
; }
31 SANITIZER_INTERFACE_ATTRIBUTE
32 int __sanitizer_posix_memalign(void **memptr
, uptr alignment
, uptr size
) {
33 GET_MALLOC_STACK_TRACE
;
35 int res
= hwasan_posix_memalign(memptr
, alignment
, size
, &stack
);
39 SANITIZER_INTERFACE_ATTRIBUTE
40 void *__sanitizer_memalign(uptr alignment
, uptr size
) {
41 GET_MALLOC_STACK_TRACE
;
42 return hwasan_memalign(alignment
, size
, &stack
);
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
);
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
);
56 DTLS_on_libc_memalign(ptr
, size
);
60 SANITIZER_INTERFACE_ATTRIBUTE
61 void *__sanitizer_valloc(uptr size
) {
62 GET_MALLOC_STACK_TRACE
;
63 return hwasan_valloc(size
, &stack
);
66 SANITIZER_INTERFACE_ATTRIBUTE
67 void *__sanitizer_pvalloc(uptr size
) {
68 GET_MALLOC_STACK_TRACE
;
69 return hwasan_pvalloc(size
, &stack
);
72 SANITIZER_INTERFACE_ATTRIBUTE
73 void __sanitizer_free(void *ptr
) {
76 if (DlsymAlloc::PointerIsMine(ptr
))
77 return DlsymAlloc::Free(ptr
);
78 GET_MALLOC_STACK_TRACE
;
79 hwasan_free(ptr
, &stack
);
82 SANITIZER_INTERFACE_ATTRIBUTE
83 void __sanitizer_cfree(void *ptr
) {
86 if (DlsymAlloc::PointerIsMine(ptr
))
87 return DlsymAlloc::Free(ptr
);
88 GET_MALLOC_STACK_TRACE
;
89 hwasan_free(ptr
, &stack
);
92 SANITIZER_INTERFACE_ATTRIBUTE
93 uptr
__sanitizer_malloc_usable_size(const void *ptr
) {
94 return __sanitizer_get_allocated_size(ptr
);
97 SANITIZER_INTERFACE_ATTRIBUTE
98 struct __sanitizer_struct_mallinfo
__sanitizer_mallinfo() {
99 __sanitizer_struct_mallinfo sret
;
100 internal_memset(&sret
, 0, sizeof(sret
));
104 SANITIZER_INTERFACE_ATTRIBUTE
105 int __sanitizer_mallopt(int cmd
, int value
) { return 0; }
107 SANITIZER_INTERFACE_ATTRIBUTE
108 void __sanitizer_malloc_stats(void) {
109 // FIXME: implement, but don't call REAL(malloc_stats)!
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
);
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
);
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
);
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
);
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)
153 INTERCEPTOR_ALIAS(int, posix_memalign
, void **memptr
, SIZE_T alignment
,
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
);
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);
173 #endif // #if HWASAN_WITH_INTERCEPTORS
175 #endif // SANITIZER_FUCHSIA