asan_interceptors_memintrinsics.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 (UNLIKELY(size == 0 || size > sizeof(uptr) * ASAN_SHADOW_GRANULARITY))
  26. return !size;
  27. uptr last = beg + size - 1;
  28. uptr shadow_first = MEM_TO_SHADOW(beg);
  29. uptr shadow_last = MEM_TO_SHADOW(last);
  30. uptr uptr_first = RoundDownTo(shadow_first, sizeof(uptr));
  31. uptr uptr_last = RoundDownTo(shadow_last, sizeof(uptr));
  32. if (LIKELY(((*reinterpret_cast<const uptr *>(uptr_first) |
  33. *reinterpret_cast<const uptr *>(uptr_last)) == 0)))
  34. return true;
  35. u8 shadow = AddressIsPoisoned(last);
  36. for (; shadow_first < shadow_last; ++shadow_first)
  37. shadow |= *((u8 *)shadow_first);
  38. return !shadow;
  39. }
  40. struct AsanInterceptorContext {
  41. const char *interceptor_name;
  42. };
  43. // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
  44. // and ASAN_WRITE_RANGE as macro instead of function so
  45. // that no extra frames are created, and stack trace contains
  46. // relevant information only.
  47. // We check all shadow bytes.
  48. #define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) \
  49. do { \
  50. uptr __offset = (uptr)(offset); \
  51. uptr __size = (uptr)(size); \
  52. uptr __bad = 0; \
  53. if (UNLIKELY(__offset > __offset + __size)) { \
  54. GET_STACK_TRACE_FATAL_HERE; \
  55. ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
  56. } \
  57. if (UNLIKELY(!QuickCheckForUnpoisonedRegion(__offset, __size)) && \
  58. (__bad = __asan_region_is_poisoned(__offset, __size))) { \
  59. AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \
  60. bool suppressed = false; \
  61. if (_ctx) { \
  62. suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \
  63. if (!suppressed && HaveStackTraceBasedSuppressions()) { \
  64. GET_STACK_TRACE_FATAL_HERE; \
  65. suppressed = IsStackTraceSuppressed(&stack); \
  66. } \
  67. } \
  68. if (!suppressed) { \
  69. GET_CURRENT_PC_BP_SP; \
  70. ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false); \
  71. } \
  72. } \
  73. } while (0)
  74. // memcpy is called during __asan_init() from the internals of printf(...).
  75. // We do not treat memcpy with to==from as a bug.
  76. // See http://llvm.org/bugs/show_bug.cgi?id=11763.
  77. #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
  78. do { \
  79. if (LIKELY(replace_intrin_cached)) { \
  80. if (LIKELY(to != from)) { \
  81. CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
  82. } \
  83. ASAN_READ_RANGE(ctx, from, size); \
  84. ASAN_WRITE_RANGE(ctx, to, size); \
  85. } else if (UNLIKELY(!asan_inited)) { \
  86. return internal_memcpy(to, from, 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 (LIKELY(replace_intrin_cached)) { \
  94. ASAN_WRITE_RANGE(ctx, block, size); \
  95. } else if (UNLIKELY(!asan_inited)) { \
  96. return internal_memset(block, c, size); \
  97. } \
  98. return REAL(memset)(block, c, size); \
  99. } while (0)
  100. #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
  101. do { \
  102. if (LIKELY(replace_intrin_cached)) { \
  103. ASAN_READ_RANGE(ctx, from, size); \
  104. ASAN_WRITE_RANGE(ctx, to, size); \
  105. } \
  106. return internal_memmove(to, from, size); \
  107. } while (0)
  108. #define ASAN_READ_RANGE(ctx, offset, size) \
  109. ACCESS_MEMORY_RANGE(ctx, offset, size, false)
  110. #define ASAN_WRITE_RANGE(ctx, offset, size) \
  111. ACCESS_MEMORY_RANGE(ctx, offset, size, true)
  112. // Behavior of functions like "memcpy" or "strcpy" is undefined
  113. // if memory intervals overlap. We report error in this case.
  114. // Macro is used to avoid creation of new frames.
  115. static inline bool RangesOverlap(const char *offset1, uptr length1,
  116. const char *offset2, uptr length2) {
  117. return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
  118. }
  119. #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) \
  120. do { \
  121. const char *offset1 = (const char *)_offset1; \
  122. const char *offset2 = (const char *)_offset2; \
  123. if (UNLIKELY(RangesOverlap(offset1, length1, offset2, length2))) { \
  124. GET_STACK_TRACE_FATAL_HERE; \
  125. bool suppressed = IsInterceptorSuppressed(name); \
  126. if (!suppressed && HaveStackTraceBasedSuppressions()) { \
  127. suppressed = IsStackTraceSuppressed(&stack); \
  128. } \
  129. if (!suppressed) { \
  130. ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
  131. offset2, length2, &stack); \
  132. } \
  133. } \
  134. } while (0)
  135. } // namespace __asan
  136. #endif // ASAN_MEMINTRIN_H