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"
18 #include "sanitizer_common/sanitizer_allocator_checks.h"
19 #include "sanitizer_common/sanitizer_internal_defs.h"
21 namespace __sanitizer
{
23 template <typename Details
>
24 struct DlSymAllocator
{
26 // Fuchsia doesn't use dlsym-based interceptors.
27 return !SANITIZER_FUCHSIA
&& UNLIKELY(Details::UseImpl());
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
));
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
));
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
));
52 static void Free(void *ptr
) {
53 uptr size
= internal_allocator()->GetActuallyAllocatedSize(ptr
);
54 Details::OnFree(ptr
, size
);
58 static void *Realloc(void *ptr
, uptr new_size
) {
60 return Allocate(new_size
);
61 CHECK(internal_allocator()->FromPrimary(ptr
));
66 uptr size
= internal_allocator()->GetActuallyAllocatedSize(ptr
);
67 uptr memcpy_size
= Min(new_size
, size
);
68 void *new_ptr
= Allocate(new_size
);
70 internal_memcpy(new_ptr
, ptr
, memcpy_size
);
75 static void *ReallocArray(void *ptr
, uptr count
, uptr size
) {
76 CHECK(!CheckForCallocOverflow(count
, size
));
77 return Realloc(ptr
, count
* size
);
80 static void OnAllocate(const void *ptr
, uptr size
) {}
81 static void OnFree(const void *ptr
, uptr size
) {}
84 } // namespace __sanitizer
86 #endif // SANITIZER_ALLOCATOR_DLSYM_H