asan_interceptors_memintrinsics.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #define ASAN_READ_RANGE(ctx, offset, size) \
  75. ACCESS_MEMORY_RANGE(ctx, offset, size, false)
  76. #define ASAN_WRITE_RANGE(ctx, offset, size) \
  77. ACCESS_MEMORY_RANGE(ctx, offset, size, true)
  78. // Behavior of functions like "memcpy" or "strcpy" is undefined
  79. // if memory intervals overlap. We report error in this case.
  80. // Macro is used to avoid creation of new frames.
  81. static inline bool RangesOverlap(const char *offset1, uptr length1,
  82. const char *offset2, uptr length2) {
  83. return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));
  84. }
  85. #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) \
  86. do { \
  87. const char *offset1 = (const char *)_offset1; \
  88. const char *offset2 = (const char *)_offset2; \
  89. if (UNLIKELY(RangesOverlap(offset1, length1, offset2, length2))) { \
  90. GET_STACK_TRACE_FATAL_HERE; \
  91. bool suppressed = IsInterceptorSuppressed(name); \
  92. if (!suppressed && HaveStackTraceBasedSuppressions()) { \
  93. suppressed = IsStackTraceSuppressed(&stack); \
  94. } \
  95. if (!suppressed) { \
  96. ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
  97. offset2, length2, &stack); \
  98. } \
  99. } \
  100. } while (0)
  101. } // namespace __asan
  102. #endif // ASAN_MEMINTRIN_H