d: Merge upstream dmd, druntime 2bbf64907c, phobos b64bfbf91
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_allocator_dlsym.h
blob92b1373ef84d1aa400aca267655e5274a8aed5be
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.
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 {
23 static bool Use() {
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));
39 return 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));
47 return ptr;
50 static void Free(void *ptr) {
51 uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
52 Details::OnFree(ptr, size);
53 InternalFree(ptr);
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;
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;
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