hwasan_allocation_functions.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //===-- hwasan_allocation_functions.cpp -----------------------------------===//
  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. // This file is a part of HWAddressSanitizer.
  10. //
  11. // Definitions for __sanitizer allocation functions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "hwasan.h"
  15. #include "interception/interception.h"
  16. #include "sanitizer_common/sanitizer_allocator_dlsym.h"
  17. #include "sanitizer_common/sanitizer_allocator_interface.h"
  18. #include "sanitizer_common/sanitizer_tls_get_addr.h"
  19. #if !SANITIZER_FUCHSIA
  20. using namespace __hwasan;
  21. struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
  22. static bool UseImpl() { return !hwasan_inited; }
  23. };
  24. extern "C" {
  25. SANITIZER_INTERFACE_ATTRIBUTE
  26. int __sanitizer_posix_memalign(void **memptr, uptr alignment, uptr size) {
  27. GET_MALLOC_STACK_TRACE;
  28. CHECK_NE(memptr, 0);
  29. int res = hwasan_posix_memalign(memptr, alignment, size, &stack);
  30. return res;
  31. }
  32. SANITIZER_INTERFACE_ATTRIBUTE
  33. void *__sanitizer_memalign(uptr alignment, uptr size) {
  34. GET_MALLOC_STACK_TRACE;
  35. return hwasan_memalign(alignment, size, &stack);
  36. }
  37. SANITIZER_INTERFACE_ATTRIBUTE
  38. void *__sanitizer_aligned_alloc(uptr alignment, uptr size) {
  39. GET_MALLOC_STACK_TRACE;
  40. return hwasan_aligned_alloc(alignment, size, &stack);
  41. }
  42. SANITIZER_INTERFACE_ATTRIBUTE
  43. void *__sanitizer___libc_memalign(uptr alignment, uptr size) {
  44. GET_MALLOC_STACK_TRACE;
  45. void *ptr = hwasan_memalign(alignment, size, &stack);
  46. if (ptr)
  47. DTLS_on_libc_memalign(ptr, size);
  48. return ptr;
  49. }
  50. SANITIZER_INTERFACE_ATTRIBUTE
  51. void *__sanitizer_valloc(uptr size) {
  52. GET_MALLOC_STACK_TRACE;
  53. return hwasan_valloc(size, &stack);
  54. }
  55. SANITIZER_INTERFACE_ATTRIBUTE
  56. void *__sanitizer_pvalloc(uptr size) {
  57. GET_MALLOC_STACK_TRACE;
  58. return hwasan_pvalloc(size, &stack);
  59. }
  60. SANITIZER_INTERFACE_ATTRIBUTE
  61. void __sanitizer_free(void *ptr) {
  62. if (!ptr)
  63. return;
  64. if (DlsymAlloc::PointerIsMine(ptr))
  65. return DlsymAlloc::Free(ptr);
  66. GET_MALLOC_STACK_TRACE;
  67. hwasan_free(ptr, &stack);
  68. }
  69. SANITIZER_INTERFACE_ATTRIBUTE
  70. void __sanitizer_cfree(void *ptr) {
  71. if (!ptr)
  72. return;
  73. if (DlsymAlloc::PointerIsMine(ptr))
  74. return DlsymAlloc::Free(ptr);
  75. GET_MALLOC_STACK_TRACE;
  76. hwasan_free(ptr, &stack);
  77. }
  78. SANITIZER_INTERFACE_ATTRIBUTE
  79. uptr __sanitizer_malloc_usable_size(const void *ptr) {
  80. return __sanitizer_get_allocated_size(ptr);
  81. }
  82. SANITIZER_INTERFACE_ATTRIBUTE
  83. struct __sanitizer_struct_mallinfo __sanitizer_mallinfo() {
  84. __sanitizer_struct_mallinfo sret;
  85. internal_memset(&sret, 0, sizeof(sret));
  86. return sret;
  87. }
  88. SANITIZER_INTERFACE_ATTRIBUTE
  89. int __sanitizer_mallopt(int cmd, int value) { return 0; }
  90. SANITIZER_INTERFACE_ATTRIBUTE
  91. void __sanitizer_malloc_stats(void) {
  92. // FIXME: implement, but don't call REAL(malloc_stats)!
  93. }
  94. SANITIZER_INTERFACE_ATTRIBUTE
  95. void *__sanitizer_calloc(uptr nmemb, uptr size) {
  96. if (DlsymAlloc::Use())
  97. return DlsymAlloc::Callocate(nmemb, size);
  98. GET_MALLOC_STACK_TRACE;
  99. return hwasan_calloc(nmemb, size, &stack);
  100. }
  101. SANITIZER_INTERFACE_ATTRIBUTE
  102. void *__sanitizer_realloc(void *ptr, uptr size) {
  103. if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
  104. return DlsymAlloc::Realloc(ptr, size);
  105. GET_MALLOC_STACK_TRACE;
  106. return hwasan_realloc(ptr, size, &stack);
  107. }
  108. SANITIZER_INTERFACE_ATTRIBUTE
  109. void *__sanitizer_reallocarray(void *ptr, uptr nmemb, uptr size) {
  110. GET_MALLOC_STACK_TRACE;
  111. return hwasan_reallocarray(ptr, nmemb, size, &stack);
  112. }
  113. SANITIZER_INTERFACE_ATTRIBUTE
  114. void *__sanitizer_malloc(uptr size) {
  115. if (UNLIKELY(!hwasan_init_is_running))
  116. ENSURE_HWASAN_INITED();
  117. if (DlsymAlloc::Use())
  118. return DlsymAlloc::Allocate(size);
  119. GET_MALLOC_STACK_TRACE;
  120. return hwasan_malloc(size, &stack);
  121. }
  122. } // extern "C"
  123. #if HWASAN_WITH_INTERCEPTORS
  124. # define INTERCEPTOR_ALIAS(RET, FN, ARGS...) \
  125. extern "C" SANITIZER_INTERFACE_ATTRIBUTE RET WRAP(FN)(ARGS) \
  126. ALIAS("__sanitizer_" #FN); \
  127. extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE RET FN( \
  128. ARGS) ALIAS("__sanitizer_" #FN)
  129. INTERCEPTOR_ALIAS(int, posix_memalign, void **memptr, SIZE_T alignment,
  130. SIZE_T size);
  131. INTERCEPTOR_ALIAS(void *, aligned_alloc, SIZE_T alignment, SIZE_T size);
  132. INTERCEPTOR_ALIAS(void *, __libc_memalign, SIZE_T alignment, SIZE_T size);
  133. INTERCEPTOR_ALIAS(void *, valloc, SIZE_T size);
  134. INTERCEPTOR_ALIAS(void, free, void *ptr);
  135. INTERCEPTOR_ALIAS(uptr, malloc_usable_size, const void *ptr);
  136. INTERCEPTOR_ALIAS(void *, calloc, SIZE_T nmemb, SIZE_T size);
  137. INTERCEPTOR_ALIAS(void *, realloc, void *ptr, SIZE_T size);
  138. INTERCEPTOR_ALIAS(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size);
  139. INTERCEPTOR_ALIAS(void *, malloc, SIZE_T size);
  140. # if !SANITIZER_FREEBSD && !SANITIZER_NETBSD
  141. INTERCEPTOR_ALIAS(void *, memalign, SIZE_T alignment, SIZE_T size);
  142. INTERCEPTOR_ALIAS(void *, pvalloc, SIZE_T size);
  143. INTERCEPTOR_ALIAS(void, cfree, void *ptr);
  144. INTERCEPTOR_ALIAS(__sanitizer_struct_mallinfo, mallinfo);
  145. INTERCEPTOR_ALIAS(int, mallopt, int cmd, int value);
  146. INTERCEPTOR_ALIAS(void, malloc_stats, void);
  147. # endif
  148. #endif // #if HWASAN_WITH_INTERCEPTORS
  149. #endif // SANITIZER_FUCHSIA