1 //===-- sanitizer_allocator_dlsym.h -----------------------------*- C++ -*-===//
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 // Hack: Sanitizer initializer calls dlsym which may need to allocate and call
10 // back into uninitialized sanitizer.
12 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_ALLOCATOR_DLSYM_H
15 #define SANITIZER_ALLOCATOR_DLSYM_H
17 #include "sanitizer_allocator_internal.h"
19 namespace __sanitizer
{
21 template <typename Details
>
22 struct DlSymAllocator
{
24 // Fuchsia doesn't use dlsym-based interceptors.
25 return !SANITIZER_FUCHSIA
&& UNLIKELY(Details::UseImpl());
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
));
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
));
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
));
50 static void Free(void *ptr
) {
51 uptr size
= internal_allocator()->GetActuallyAllocatedSize(ptr
);
52 Details::OnFree(ptr
, size
);
56 static void *Realloc(void *ptr
, uptr new_size
) {
58 return Allocate(new_size
);
59 CHECK(internal_allocator()->FromPrimary(ptr
));
64 uptr size
= internal_allocator()->GetActuallyAllocatedSize(ptr
);
65 uptr memcpy_size
= Min(new_size
, size
);
66 void *new_ptr
= Allocate(new_size
);
68 internal_memcpy(new_ptr
, ptr
, memcpy_size
);
73 static void OnAllocate(const void *ptr
, uptr size
) {}
74 static void OnFree(const void *ptr
, uptr size
) {}
77 } // namespace __sanitizer
79 #endif // SANITIZER_ALLOCATOR_DLSYM_H