sanitizer_allocator_dlsym.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef SANITIZER_ALLOCATOR_DLSYM_H
  14. #define SANITIZER_ALLOCATOR_DLSYM_H
  15. #include "sanitizer_allocator_internal.h"
  16. namespace __sanitizer {
  17. template <typename Details>
  18. struct DlSymAllocator {
  19. static bool Use() {
  20. // Fuchsia doesn't use dlsym-based interceptors.
  21. return !SANITIZER_FUCHSIA && UNLIKELY(Details::UseImpl());
  22. }
  23. static bool PointerIsMine(const void *ptr) {
  24. // Fuchsia doesn't use dlsym-based interceptors.
  25. return !SANITIZER_FUCHSIA &&
  26. UNLIKELY(internal_allocator()->FromPrimary(ptr));
  27. }
  28. static void *Allocate(uptr size_in_bytes) {
  29. void *ptr = InternalAlloc(size_in_bytes, nullptr, kWordSize);
  30. CHECK(internal_allocator()->FromPrimary(ptr));
  31. Details::OnAllocate(ptr,
  32. internal_allocator()->GetActuallyAllocatedSize(ptr));
  33. return ptr;
  34. }
  35. static void *Callocate(SIZE_T nmemb, SIZE_T size) {
  36. void *ptr = InternalCalloc(nmemb, size);
  37. CHECK(internal_allocator()->FromPrimary(ptr));
  38. Details::OnAllocate(ptr,
  39. internal_allocator()->GetActuallyAllocatedSize(ptr));
  40. return ptr;
  41. }
  42. static void Free(void *ptr) {
  43. uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
  44. Details::OnFree(ptr, size);
  45. InternalFree(ptr);
  46. }
  47. static void *Realloc(void *ptr, uptr new_size) {
  48. if (!ptr)
  49. return Allocate(new_size);
  50. CHECK(internal_allocator()->FromPrimary(ptr));
  51. if (!new_size) {
  52. Free(ptr);
  53. return nullptr;
  54. }
  55. uptr size = internal_allocator()->GetActuallyAllocatedSize(ptr);
  56. uptr memcpy_size = Min(new_size, size);
  57. void *new_ptr = Allocate(new_size);
  58. if (new_ptr)
  59. internal_memcpy(new_ptr, ptr, memcpy_size);
  60. Free(ptr);
  61. return new_ptr;
  62. }
  63. static void OnAllocate(const void *ptr, uptr size) {}
  64. static void OnFree(const void *ptr, uptr size) {}
  65. };
  66. } // namespace __sanitizer
  67. #endif // SANITIZER_ALLOCATOR_DLSYM_H