| 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 |
#include "sanitizer_common/sanitizer_allocator_checks.h" |
| 19 |
#include "sanitizer_common/sanitizer_internal_defs.h" |
| 20 |
|
| 21 |
namespace __sanitizer { |
| 22 |
|
| 23 |
template <typename Details> |
| 24 |
struct DlSymAllocator { |
| 25 |
static bool Use() { |
| 26 |
// Fuchsia doesn't use dlsym-based interceptors. |
| 27 |
return !SANITIZER_FUCHSIA && UNLIKELY(Details::UseImpl()); |
| 28 |
} |
| 29 |
|
| 30 |
static bool PointerIsMine(const void *ptr) { |
| 31 |
// Fuchsia doesn't use dlsym-based interceptors. |
| 32 |
return !SANITIZER_FUCHSIA && |
| 33 |
UNLIKELY(internal_allocator()->FromPrimary(ptr)); |
| 34 |
} |
| 35 |
|
| 36 |
static void *Allocate(uptr size_in_bytes, uptr align = kWordSize) { |
| 37 |
void *ptr = InternalAlloc(size_in_bytes, nullptr, align); |
| 38 |
CHECK(internal_allocator()->FromPrimary(ptr)); |
| 39 |
Details::OnAllocate(ptr, |
| 40 |
internal_allocator()->GetActuallyAllocatedSize(ptr)); |
| 41 |
return ptr; |
| 42 |
} |
| 43 |
|
| 44 |
static void *Callocate(usize nmemb, usize size) { |
| 45 |
void *ptr = InternalCalloc(nmemb, size); |
| 46 |
CHECK(internal_allocator()->FromPrimary(ptr)); |
| 47 |
Details::OnAllocate(ptr, |
| 48 |
internal_allocator()->GetActuallyAllocatedSize(ptr)); |
| 49 |
return ptr; |
| 50 |
} |
| 51 |
|
| 52 |
static void Free(void *ptr) { |
| 53 |
uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr); |
| 54 |
Details::OnFree(ptr, size); |
| 55 |
InternalFree(ptr); |
| 56 |
} |
| 57 |
|
| 58 |
static void *Realloc(void *ptr, uptr new_size) { |
| 59 |
if (!ptr) |
| 60 |
return Allocate(new_size); |
| 61 |
CHECK(internal_allocator()->FromPrimary(ptr)); |
| 62 |
if (!new_size) { |
| 63 |
Free(ptr); |
| 64 |
return nullptr; |
| 65 |
} |
| 66 |
uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr); |
| 67 |
uptr memcpy_size = Min(new_size, size); |
| 68 |
void *new_ptr = Allocate(new_size); |
| 69 |
if (new_ptr) |
| 70 |
internal_memcpy(new_ptr, ptr, memcpy_size); |
| 71 |
Free(ptr); |
| 72 |
return new_ptr; |
| 73 |
} |
| 74 |
|
| 75 |
static void *ReallocArray(void *ptr, uptr count, uptr size) { |
| 76 |
CHECK(!CheckForCallocOverflow(count, size)); |
| 77 |
return Realloc(ptr, count * size); |
| 78 |
} |
| 79 |
|
| 80 |
static void OnAllocate(const void *ptr, uptr size) {} |
| 81 |
static void OnFree(const void *ptr, uptr size) {} |
| 82 |
}; |
| 83 |
|
| 84 |
} // namespace __sanitizer |
| 85 |
|
| 86 |
#endif // SANITIZER_ALLOCATOR_DLSYM_H |