| 1 | //===-- copyprof_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 implements Linux-specific malloc and memory allocation |
| 10 | /// interceptors for CopyProf, wrapping application memory management calls. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "copyprof/copyprof_interface_internal.h" |
| 15 | #include "copyprof/copyprof_internal.h" |
| 16 | #include "copyprof_allocator.h" |
| 17 | #include "copyprof_interceptors.h" |
| 18 | #include "interception/interception.h" |
| 19 | #include "sanitizer_common/sanitizer_allocator_dlsym.h" |
| 20 | #include "sanitizer_common/sanitizer_platform_interceptors.h" |
| 21 | |
| 22 | using namespace __copyprof; |
| 23 | using namespace __sanitizer; |
| 24 | |
| 25 | namespace __copyprof { |
| 26 | |
| 27 | // This is needed to make the heap interceptors re-entrancy safe during the |
| 28 | // startup phase as `dlsym` itself might allocate. |
| 29 | struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> { |
| 30 | static bool UseImpl() { return copyprof_init_is_running; } |
| 31 | }; |
| 32 | |
| 33 | } // namespace __copyprof |
| 34 | |
| 35 | #define ENSURE_COPYPROF_INITIALIZED() \ |
| 36 | do { \ |
| 37 | if (UNLIKELY(!copyprof_is_initialized)) { \ |
| 38 | __copyprof_init_once(); \ |
| 39 | } \ |
| 40 | } while (0) |
| 41 | |
| 42 | INTERCEPTOR(void*, malloc, SIZE_T size) { |
| 43 | if (DlsymAlloc::Use()) |
| 44 | return DlsymAlloc::Allocate(size_in_bytes: size); |
| 45 | ENSURE_COPYPROF_INITIALIZED(); |
| 46 | return CopyProfMalloc(size); |
| 47 | } |
| 48 | |
| 49 | INTERCEPTOR(void*, calloc, SIZE_T nmemb, SIZE_T size) { |
| 50 | if (DlsymAlloc::Use()) |
| 51 | return DlsymAlloc::Callocate(nmemb, size); |
| 52 | ENSURE_COPYPROF_INITIALIZED(); |
| 53 | return CopyProfCalloc(num: nmemb, size); |
| 54 | } |
| 55 | |
| 56 | INTERCEPTOR(void*, realloc, void* ptr, SIZE_T size) { |
| 57 | if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr)) |
| 58 | return DlsymAlloc::Realloc(ptr, new_size: size); |
| 59 | ENSURE_COPYPROF_INITIALIZED(); |
| 60 | return CopyProfRealloc(ptr, size); |
| 61 | } |
| 62 | |
| 63 | INTERCEPTOR(void, free, void* ptr) { |
| 64 | if (UNLIKELY(!ptr)) |
| 65 | return; |
| 66 | if (DlsymAlloc::PointerIsMine(ptr)) |
| 67 | return DlsymAlloc::Free(ptr); |
| 68 | ENSURE_COPYPROF_INITIALIZED(); |
| 69 | CopyProfFree(ptr); |
| 70 | } |
| 71 | |
| 72 | // `free` and `realloc` only accept memory from the CopyProf allocator (or |
| 73 | // `DlsymAlloc`), so all other libc heap functions are replaced as well. |
| 74 | |
| 75 | #if SANITIZER_INTERCEPT_CFREE |
| 76 | INTERCEPTOR(void, cfree, void* ptr) { WRAP(free)(ptr); } |
| 77 | #endif |
| 78 | |
| 79 | #if SANITIZER_INTERCEPT_FREE_SIZED |
| 80 | INTERCEPTOR(void, free_sized, void* ptr, SIZE_T size) { WRAP(free)(ptr); } |
| 81 | #endif |
| 82 | |
| 83 | #if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED |
| 84 | INTERCEPTOR(void, free_aligned_sized, void* ptr, SIZE_T alignment, |
| 85 | SIZE_T size) { |
| 86 | WRAP(free)(ptr); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | #if SANITIZER_INTERCEPT_REALLOCARRAY |
| 91 | INTERCEPTOR(void*, reallocarray, void* ptr, SIZE_T nmemb, SIZE_T size) { |
| 92 | ENSURE_COPYPROF_INITIALIZED(); |
| 93 | return CopyProfReallocarray(ptr, num: nmemb, size); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | #if SANITIZER_INTERCEPT_MEMALIGN |
| 98 | INTERCEPTOR(void*, memalign, SIZE_T alignment, SIZE_T size) { |
| 99 | ENSURE_COPYPROF_INITIALIZED(); |
| 100 | return CopyProfAllocate(size, align: alignment); |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | #if SANITIZER_INTERCEPT___LIBC_MEMALIGN |
| 105 | INTERCEPTOR(void*, __libc_memalign, SIZE_T alignment, SIZE_T size) { |
| 106 | ENSURE_COPYPROF_INITIALIZED(); |
| 107 | return CopyProfAllocate(size, align: alignment); |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | #if SANITIZER_INTERCEPT_ALIGNED_ALLOC |
| 112 | // Unlike other sanitizers, `size` need not be a multiple of `alignment`: C17 |
| 113 | // dropped that requirement, and glibc does not enforce it either. |
| 114 | INTERCEPTOR(void*, aligned_alloc, SIZE_T alignment, SIZE_T size) { |
| 115 | ENSURE_COPYPROF_INITIALIZED(); |
| 116 | return CopyProfAllocate(size, align: alignment); |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | INTERCEPTOR(int, posix_memalign, void** memptr, SIZE_T alignment, SIZE_T size) { |
| 121 | ENSURE_COPYPROF_INITIALIZED(); |
| 122 | return CopyProfPosixMemalign(memptr, alignment, size); |
| 123 | } |
| 124 | |
| 125 | INTERCEPTOR(void*, valloc, SIZE_T size) { |
| 126 | ENSURE_COPYPROF_INITIALIZED(); |
| 127 | return CopyProfValloc(size); |
| 128 | } |
| 129 | |
| 130 | #if SANITIZER_INTERCEPT_PVALLOC |
| 131 | INTERCEPTOR(void*, pvalloc, SIZE_T size) { |
| 132 | ENSURE_COPYPROF_INITIALIZED(); |
| 133 | return CopyProfPvalloc(size); |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE |
| 138 | INTERCEPTOR(SIZE_T, malloc_usable_size, void* ptr) { |
| 139 | ENSURE_COPYPROF_INITIALIZED(); |
| 140 | return CopyProfMallocUsableSize(ptr); |
| 141 | } |
| 142 | #endif |
| 143 | |
| 144 | namespace __copyprof { |
| 145 | |
| 146 | void InitializeHeapInterceptors() { |
| 147 | COPYPROF_INTERCEPT_FUNC(malloc); |
| 148 | COPYPROF_INTERCEPT_FUNC(calloc); |
| 149 | COPYPROF_INTERCEPT_FUNC(realloc); |
| 150 | COPYPROF_INTERCEPT_FUNC(free); |
| 151 | #if SANITIZER_INTERCEPT_CFREE |
| 152 | COPYPROF_INTERCEPT_FUNC(cfree); |
| 153 | #endif |
| 154 | #if SANITIZER_INTERCEPT_FREE_SIZED |
| 155 | COPYPROF_INTERCEPT_FUNC(free_sized); |
| 156 | #endif |
| 157 | #if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED |
| 158 | COPYPROF_INTERCEPT_FUNC(free_aligned_sized); |
| 159 | #endif |
| 160 | #if SANITIZER_INTERCEPT_REALLOCARRAY |
| 161 | COPYPROF_INTERCEPT_FUNC(reallocarray); |
| 162 | #endif |
| 163 | #if SANITIZER_INTERCEPT_MEMALIGN |
| 164 | COPYPROF_INTERCEPT_FUNC(memalign); |
| 165 | #endif |
| 166 | #if SANITIZER_INTERCEPT___LIBC_MEMALIGN |
| 167 | COPYPROF_INTERCEPT_FUNC(__libc_memalign); |
| 168 | #endif |
| 169 | #if SANITIZER_INTERCEPT_ALIGNED_ALLOC |
| 170 | COPYPROF_INTERCEPT_FUNC(aligned_alloc); |
| 171 | #endif |
| 172 | COPYPROF_INTERCEPT_FUNC(posix_memalign); |
| 173 | COPYPROF_INTERCEPT_FUNC(valloc); |
| 174 | #if SANITIZER_INTERCEPT_PVALLOC |
| 175 | COPYPROF_INTERCEPT_FUNC(pvalloc); |
| 176 | #endif |
| 177 | #if SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE |
| 178 | COPYPROF_INTERCEPT_FUNC(malloc_usable_size); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | } // namespace __copyprof |
| 183 | |