Vitaly Buka | cb0e14c | 2021-11-10 18:17:20 -0800 | [diff] [blame] | 1 | //===-- sanitizer_allocator_dlsym.h -----------------------------*- C++ -*-===// |
| 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 | // Hack: Sanitizer initializer calls dlsym which may need to allocate and call |
| 10 | // back into uninitialized sanitizer. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef SANITIZER_ALLOCATOR_DLSYM_H |
| 15 | #define SANITIZER_ALLOCATOR_DLSYM_H |
| 16 | |
| 17 | #include "sanitizer_allocator_internal.h" |
| 18 | |
| 19 | namespace __sanitizer { |
| 20 | |
| 21 | template <typename Details> |
| 22 | struct DlSymAllocator { |
| 23 | static bool Use() { |
| 24 | // Fuchsia doesn't use dlsym-based interceptors. |
| 25 | return !SANITIZER_FUCHSIA && UNLIKELY(Details::UseImpl()); |
| 26 | } |
| 27 | |
| 28 | static bool PointerIsMine(const void *ptr) { |
| 29 | // Fuchsia doesn't use dlsym-based interceptors. |
| 30 | return !SANITIZER_FUCHSIA && |
| 31 | UNLIKELY(internal_allocator()->FromPrimary(ptr)); |
| 32 | } |
| 33 | |
| 34 | static void *Allocate(uptr size_in_bytes) { |
| 35 | void *ptr = InternalAlloc(size_in_bytes, nullptr, kWordSize); |
| 36 | CHECK(internal_allocator()->FromPrimary(ptr)); |
| 37 | Details::OnAllocate(ptr, |
| 38 | internal_allocator()->GetActuallyAllocatedSize(ptr)); |
| 39 | return ptr; |
| 40 | } |
| 41 | |
| 42 | static void *Callocate(SIZE_T nmemb, SIZE_T size) { |
| 43 | void *ptr = InternalCalloc(nmemb, size); |
| 44 | CHECK(internal_allocator()->FromPrimary(ptr)); |
| 45 | Details::OnAllocate(ptr, |
| 46 | internal_allocator()->GetActuallyAllocatedSize(ptr)); |
| 47 | return ptr; |
| 48 | } |
| 49 | |
| 50 | static void Free(void *ptr) { |
| 51 | uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr); |
| 52 | Details::OnFree(ptr, size); |
| 53 | InternalFree(ptr); |
| 54 | } |
| 55 | |
| 56 | static void *Realloc(void *ptr, uptr new_size) { |
| 57 | if (!ptr) |
| 58 | return Allocate(new_size); |
| 59 | CHECK(internal_allocator()->FromPrimary(ptr)); |
| 60 | if (!new_size) { |
| 61 | Free(ptr); |
| 62 | return nullptr; |
| 63 | } |
| 64 | uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr); |
| 65 | uptr memcpy_size = Min(new_size, size); |
| 66 | void *new_ptr = Allocate(new_size); |
| 67 | if (new_ptr) |
| 68 | internal_memcpy(new_ptr, ptr, memcpy_size); |
| 69 | Free(ptr); |
| 70 | return new_ptr; |
| 71 | } |
| 72 | |
| 73 | static void OnAllocate(const void *ptr, uptr size) {} |
| 74 | static void OnFree(const void *ptr, uptr size) {} |
| 75 | }; |
| 76 | |
| 77 | } // namespace __sanitizer |
| 78 | |
| 79 | #endif // SANITIZER_ALLOCATOR_DLSYM_H |