dfsan_interceptors.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //===-- dfsan_interceptors.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 DataFlowSanitizer.
  10. //
  11. // Interceptors for standard library functions.
  12. //===----------------------------------------------------------------------===//
  13. #include <sys/syscall.h>
  14. #include <unistd.h>
  15. #include "dfsan/dfsan.h"
  16. #include "dfsan/dfsan_thread.h"
  17. #include "interception/interception.h"
  18. #include "sanitizer_common/sanitizer_allocator_dlsym.h"
  19. #include "sanitizer_common/sanitizer_allocator_interface.h"
  20. #include "sanitizer_common/sanitizer_common.h"
  21. #include "sanitizer_common/sanitizer_errno.h"
  22. #include "sanitizer_common/sanitizer_platform_limits_posix.h"
  23. #include "sanitizer_common/sanitizer_posix.h"
  24. #include "sanitizer_common/sanitizer_tls_get_addr.h"
  25. using namespace __sanitizer;
  26. static bool interceptors_initialized;
  27. struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
  28. static bool UseImpl() { return !__dfsan::dfsan_inited; }
  29. };
  30. INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) {
  31. return __dfsan::dfsan_reallocarray(ptr, nmemb, size);
  32. }
  33. INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
  34. void *ptr = __dfsan::dfsan_memalign(alignment, size);
  35. if (ptr)
  36. DTLS_on_libc_memalign(ptr, size);
  37. return ptr;
  38. }
  39. INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
  40. return __dfsan::dfsan_aligned_alloc(alignment, size);
  41. }
  42. INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
  43. if (DlsymAlloc::Use())
  44. return DlsymAlloc::Callocate(nmemb, size);
  45. return __dfsan::dfsan_calloc(nmemb, size);
  46. }
  47. INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
  48. if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
  49. return DlsymAlloc::Realloc(ptr, size);
  50. return __dfsan::dfsan_realloc(ptr, size);
  51. }
  52. INTERCEPTOR(void *, malloc, SIZE_T size) {
  53. if (DlsymAlloc::Use())
  54. return DlsymAlloc::Allocate(size);
  55. return __dfsan::dfsan_malloc(size);
  56. }
  57. INTERCEPTOR(void, free, void *ptr) {
  58. if (!ptr)
  59. return;
  60. if (DlsymAlloc::PointerIsMine(ptr))
  61. return DlsymAlloc::Free(ptr);
  62. return __dfsan::dfsan_deallocate(ptr);
  63. }
  64. INTERCEPTOR(void, cfree, void *ptr) {
  65. if (!ptr)
  66. return;
  67. if (DlsymAlloc::PointerIsMine(ptr))
  68. return DlsymAlloc::Free(ptr);
  69. return __dfsan::dfsan_deallocate(ptr);
  70. }
  71. INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
  72. CHECK_NE(memptr, 0);
  73. int res = __dfsan::dfsan_posix_memalign(memptr, alignment, size);
  74. if (!res)
  75. dfsan_set_label(0, memptr, sizeof(*memptr));
  76. return res;
  77. }
  78. INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
  79. return __dfsan::dfsan_memalign(alignment, size);
  80. }
  81. INTERCEPTOR(void *, valloc, SIZE_T size) { return __dfsan::dfsan_valloc(size); }
  82. INTERCEPTOR(void *, pvalloc, SIZE_T size) {
  83. return __dfsan::dfsan_pvalloc(size);
  84. }
  85. INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
  86. internal_memset(sret, 0, sizeof(*sret));
  87. dfsan_set_label(0, sret, sizeof(*sret));
  88. }
  89. INTERCEPTOR(int, mallopt, int cmd, int value) { return 0; }
  90. INTERCEPTOR(void, malloc_stats, void) {
  91. // FIXME: implement, but don't call REAL(malloc_stats)!
  92. }
  93. INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
  94. return __sanitizer_get_allocated_size(ptr);
  95. }
  96. #define ENSURE_DFSAN_INITED() \
  97. do { \
  98. CHECK(!__dfsan::dfsan_init_is_running); \
  99. if (!__dfsan::dfsan_inited) { \
  100. __dfsan::dfsan_init(); \
  101. } \
  102. } while (0)
  103. #define COMMON_INTERCEPTOR_ENTER(func, ...) \
  104. if (__dfsan::dfsan_init_is_running) \
  105. return REAL(func)(__VA_ARGS__); \
  106. ENSURE_DFSAN_INITED(); \
  107. dfsan_set_label(0, __errno_location(), sizeof(int));
  108. INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
  109. int fd, OFF_T offset) {
  110. if (common_flags()->detect_write_exec)
  111. ReportMmapWriteExec(prot, flags);
  112. if (!__dfsan::dfsan_inited)
  113. return (void *)internal_mmap(addr, length, prot, flags, fd, offset);
  114. COMMON_INTERCEPTOR_ENTER(mmap, addr, length, prot, flags, fd, offset);
  115. void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
  116. if (res != (void *)-1) {
  117. dfsan_set_label(0, res, RoundUpTo(length, GetPageSizeCached()));
  118. }
  119. return res;
  120. }
  121. INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
  122. int fd, OFF64_T offset) {
  123. if (common_flags()->detect_write_exec)
  124. ReportMmapWriteExec(prot, flags);
  125. if (!__dfsan::dfsan_inited)
  126. return (void *)internal_mmap(addr, length, prot, flags, fd, offset);
  127. COMMON_INTERCEPTOR_ENTER(mmap64, addr, length, prot, flags, fd, offset);
  128. void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
  129. if (res != (void *)-1) {
  130. dfsan_set_label(0, res, RoundUpTo(length, GetPageSizeCached()));
  131. }
  132. return res;
  133. }
  134. INTERCEPTOR(int, munmap, void *addr, SIZE_T length) {
  135. if (!__dfsan::dfsan_inited)
  136. return internal_munmap(addr, length);
  137. COMMON_INTERCEPTOR_ENTER(munmap, addr, length);
  138. int res = REAL(munmap)(addr, length);
  139. if (res != -1)
  140. dfsan_set_label(0, addr, RoundUpTo(length, GetPageSizeCached()));
  141. return res;
  142. }
  143. #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
  144. if (__dfsan::DFsanThread *t = __dfsan::GetCurrentThread()) { \
  145. *begin = t->tls_begin(); \
  146. *end = t->tls_end(); \
  147. } else { \
  148. *begin = *end = 0; \
  149. }
  150. #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
  151. dfsan_set_label(0, ptr, size)
  152. INTERCEPTOR(void *, __tls_get_addr, void *arg) {
  153. COMMON_INTERCEPTOR_ENTER(__tls_get_addr, arg);
  154. void *res = REAL(__tls_get_addr)(arg);
  155. uptr tls_begin, tls_end;
  156. COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
  157. DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
  158. if (dtv) {
  159. // New DTLS block has been allocated.
  160. COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
  161. }
  162. return res;
  163. }
  164. namespace __dfsan {
  165. void initialize_interceptors() {
  166. CHECK(!interceptors_initialized);
  167. INTERCEPT_FUNCTION(aligned_alloc);
  168. INTERCEPT_FUNCTION(calloc);
  169. INTERCEPT_FUNCTION(cfree);
  170. INTERCEPT_FUNCTION(free);
  171. INTERCEPT_FUNCTION(mallinfo);
  172. INTERCEPT_FUNCTION(malloc);
  173. INTERCEPT_FUNCTION(malloc_stats);
  174. INTERCEPT_FUNCTION(malloc_usable_size);
  175. INTERCEPT_FUNCTION(mallopt);
  176. INTERCEPT_FUNCTION(memalign);
  177. INTERCEPT_FUNCTION(mmap);
  178. INTERCEPT_FUNCTION(mmap64);
  179. INTERCEPT_FUNCTION(munmap);
  180. INTERCEPT_FUNCTION(posix_memalign);
  181. INTERCEPT_FUNCTION(pvalloc);
  182. INTERCEPT_FUNCTION(realloc);
  183. INTERCEPT_FUNCTION(reallocarray);
  184. INTERCEPT_FUNCTION(valloc);
  185. INTERCEPT_FUNCTION(__tls_get_addr);
  186. INTERCEPT_FUNCTION(__libc_memalign);
  187. interceptors_initialized = true;
  188. }
  189. } // namespace __dfsan