asan_interceptors_memintrinsics.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===-- asan_interceptors_memintrinsics.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 AddressSanitizer, an address sanity checker.
  10. //
  11. // ASan versions of memcpy, memmove, and memset.
  12. //===---------------------------------------------------------------------===//
  13. #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS
  14. #include "asan_interceptors_memintrinsics.h"
  15. #include "asan_interceptors.h"
  16. #include "asan_report.h"
  17. #include "asan_stack.h"
  18. #include "asan_suppressions.h"
  19. using namespace __asan;
  20. // memcpy is called during __asan_init() from the internals of printf(...).
  21. // We do not treat memcpy with to==from as a bug.
  22. // See http://llvm.org/bugs/show_bug.cgi?id=11763.
  23. #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
  24. do { \
  25. if (LIKELY(replace_intrin_cached)) { \
  26. if (LIKELY(to != from)) { \
  27. CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
  28. } \
  29. ASAN_READ_RANGE(ctx, from, size); \
  30. ASAN_WRITE_RANGE(ctx, to, size); \
  31. } else if (UNLIKELY(!AsanInited())) { \
  32. return internal_memcpy(to, from, size); \
  33. } \
  34. return REAL(memcpy)(to, from, size); \
  35. } while (0)
  36. // memset is called inside Printf.
  37. #define ASAN_MEMSET_IMPL(ctx, block, c, size) \
  38. do { \
  39. if (LIKELY(replace_intrin_cached)) { \
  40. ASAN_WRITE_RANGE(ctx, block, size); \
  41. } else if (UNLIKELY(!AsanInited())) { \
  42. return internal_memset(block, c, size); \
  43. } \
  44. return REAL(memset)(block, c, size); \
  45. } while (0)
  46. #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
  47. do { \
  48. if (LIKELY(replace_intrin_cached)) { \
  49. ASAN_READ_RANGE(ctx, from, size); \
  50. ASAN_WRITE_RANGE(ctx, to, size); \
  51. } \
  52. return internal_memmove(to, from, size); \
  53. } while (0)
  54. void *__asan_memcpy(void *to, const void *from, uptr size) {
  55. ASAN_MEMCPY_IMPL(nullptr, to, from, size);
  56. }
  57. void *__asan_memset(void *block, int c, uptr size) {
  58. ASAN_MEMSET_IMPL(nullptr, block, c, size);
  59. }
  60. void *__asan_memmove(void *to, const void *from, uptr size) {
  61. ASAN_MEMMOVE_IMPL(nullptr, to, from, size);
  62. }
  63. #if SANITIZER_FUCHSIA
  64. // Fuchsia doesn't use sanitizer_common_interceptors.inc, but
  65. // the only things there it wants are these three. Just define them
  66. // as aliases here rather than repeating the contents.
  67. extern "C" decltype(__asan_memcpy) memcpy[[gnu::alias("__asan_memcpy")]];
  68. extern "C" decltype(__asan_memmove) memmove[[gnu::alias("__asan_memmove")]];
  69. extern "C" decltype(__asan_memset) memset[[gnu::alias("__asan_memset")]];
  70. #else // SANITIZER_FUCHSIA
  71. #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
  72. do { \
  73. ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
  74. ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
  75. } while (false)
  76. #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
  77. do { \
  78. ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
  79. ASAN_MEMCPY_IMPL(ctx, to, from, size); \
  80. } while (false)
  81. #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
  82. do { \
  83. ASAN_INTERCEPTOR_ENTER(ctx, memset); \
  84. ASAN_MEMSET_IMPL(ctx, block, c, size); \
  85. } while (false)
  86. #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
  87. #endif // SANITIZER_FUCHSIA