asan_interceptors_memintrinsics.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //===-- asan_interceptors_memintrinsics.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. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // ASan-private header for asan_interceptors_memintrinsics.cpp
  12. //===---------------------------------------------------------------------===//
  13. #ifndef ASAN_MEMINTRIN_H
  14. #define ASAN_MEMINTRIN_H
  15. #include "asan_interface_internal.h"
  16. #include "asan_internal.h"
  17. #include "asan_mapping.h"
  18. #include "interception/interception.h"
  19. DECLARE_REAL(void*, memcpy, void *to, const void *from, uptr size)
  20. DECLARE_REAL(void*, memset, void *block, int c, uptr size)
  21. namespace __asan {
  22. // Return true if we can quickly decide that the region is unpoisoned.
  23. // We assume that a redzone is at least 16 bytes.
  24. static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {
  25. if (size == 0) return true;
  26. if (size <= 32)
  27. return !AddressIsPoisoned(beg) &&
  28. !AddressIsPoisoned(beg + size - 1) &&
  29. !AddressIsPoisoned(beg + size / 2);
  30. if (size <= 64)
  31. return !AddressIsPoisoned(beg) &&
  32. !AddressIsPoisoned(beg + size / 4) &&
  33. !AddressIsPoisoned(beg + size - 1) &&
  34. !AddressIsPoisoned(beg + 3 * size / 4) &&
  35. !AddressIsPoisoned(beg + size / 2);
  36. return false;
  37. }
  38. struct AsanInterceptorContext {
  39. const char *interceptor_name;
  40. };
  41. // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
  42. // and ASAN_WRITE_RANGE as macro instead of function so
  43. // that no extra frames are created, and stack trace contains
  44. // relevant information only.
  45. // We check all shadow bytes.
  46. #define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) do { \
  47. uptr __offset = (uptr)(offset); \
  48. uptr __size = (uptr)(size); \
  49. uptr __bad = 0; \
  50. if (__offset > __offset + __size) { \
  51. GET_STACK_TRACE_FATAL_HERE; \
  52. ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
  53. } \
  54. if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
  55. (__bad = __asan_region_is_poisoned(__offset, __size))) { \
  56. AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \
  57. bool suppressed = false; \
  58. if (_ctx) { \
  59. suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \
  60. if (!suppressed && HaveStackTraceBasedSuppressions()) { \
  61. GET_STACK_TRACE_FATAL_HERE; \
  62. suppressed = IsStackTraceSuppressed(&stack); \
  63. } \
  64. } \
  65. if (!suppressed) { \
  66. GET_CURRENT_PC_BP_SP; \
  67. ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false);\
  68. } \
  69. } \
  70. } while (0)
  71. // memcpy is called during __asan_init() from the internals of printf(...).
  72. // We do not treat memcpy with to==from as a bug.
  73. // See http://llvm.org/bugs/show_bug.cgi?id=11763.
  74. #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
  75. do { \
  76. if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size); \
  77. if (asan_init_is_running) { \
  78. return REAL(memcpy)(to, from, size); \
  79. } \
  80. ENSURE_ASAN_INITED(); \
  81. if (flags()->replace_intrin) { \
  82. if (to != from) { \
  83. CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
  84. } \
  85. ASAN_READ_RANGE(ctx, from, size); \
  86. ASAN_WRITE_RANGE(ctx, to, size); \
  87. } \
  88. return REAL(memcpy)(to, from, size); \
  89. } while (0)
  90. // memset is called inside Printf.
  91. #define ASAN_MEMSET_IMPL(ctx, block, c, size) \
  92. do { \
  93. if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size); \
  94. if (asan_init_is_running) { \
  95. return REAL(memset)(block, c, size); \
  96. } \
  97. ENSURE_ASAN_INITED(); \
  98. if (flags()->replace_intrin) { \
  99. ASAN_WRITE_RANGE(ctx, block, size); \
  100. } \
  101. return REAL(memset)(block, c, size); \
  102. } while (0)
  103. #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
  104. do { \
  105. if (UNLIKELY(!asan_inited)) return internal_memmove(to, from, size); \
  106. ENSURE_ASAN_INITED(); \
  107. if (flags()->replace_intrin) { \
  108. ASAN_READ_RANGE(ctx, from, size); \
  109. ASAN_WRITE_RANGE(ctx, to, size); \
  110. } \
  111. return internal_memmove(to, from, size); \
  112. } while (0)
  113. #define ASAN_READ_RANGE(ctx, offset, size) \
  114. ACCESS_MEMORY_RANGE(ctx, offset, size, false)
  115. #define ASAN_WRITE_RANGE(ctx, offset, size) \
  116. ACCESS_MEMORY_RANGE(ctx, offset, size, true)
  117. // Behavior of functions like "memcpy" or "strcpy" is undefined
  118. // if memory intervals overlap. We report error in this case.
  119. // Macro is used to avoid creation of new frames.
  120. static inline bool RangesOverlap(const char *offset1, uptr length1,
  121. const char *offset2, uptr length2) {
  122. return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
  123. }
  124. #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) \
  125. do { \
  126. const char *offset1 = (const char *)_offset1; \
  127. const char *offset2 = (const char *)_offset2; \
  128. if (RangesOverlap(offset1, length1, offset2, length2)) { \
  129. GET_STACK_TRACE_FATAL_HERE; \
  130. bool suppressed = IsInterceptorSuppressed(name); \
  131. if (!suppressed && HaveStackTraceBasedSuppressions()) { \
  132. suppressed = IsStackTraceSuppressed(&stack); \
  133. } \
  134. if (!suppressed) { \
  135. ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
  136. offset2, length2, &stack); \
  137. } \
  138. } \
  139. } while (0)
  140. } // namespace __asan
  141. #endif // ASAN_MEMINTRIN_H