sanitizers.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #include "defaults.h"
  3. extern "C" { // sanitizers API
  4. #if defined(_asan_enabled_)
  5. void __lsan_ignore_object(const void* p);
  6. #endif
  7. #if defined(_msan_enabled_)
  8. void __msan_unpoison(const volatile void* a, size_t size);
  9. void __msan_poison(const volatile void* a, size_t size);
  10. void __msan_check_mem_is_initialized(const volatile void* x, size_t size);
  11. #endif
  12. #if defined(_tsan_enabled_)
  13. void __tsan_acquire(void* a);
  14. void __tsan_release(void* a);
  15. #endif
  16. } // sanitizers API
  17. namespace NSan {
  18. class TFiberContext {
  19. public:
  20. TFiberContext() noexcept;
  21. TFiberContext(const void* stack, size_t len, const char* contName) noexcept;
  22. ~TFiberContext() noexcept;
  23. void BeforeFinish() noexcept;
  24. void BeforeSwitch(TFiberContext* old) noexcept;
  25. void AfterSwitch() noexcept;
  26. static void AfterStart() noexcept;
  27. private:
  28. void* Token_;
  29. const void* Stack_;
  30. size_t Len_;
  31. const bool IsMainFiber_;
  32. #if defined(_tsan_enabled_)
  33. void* const CurrentTSanFiberContext_;
  34. #endif
  35. };
  36. // Returns plain if no sanitizer enabled or sanitized otherwise
  37. // Ment to be used in test code for constants (timeouts, etc)
  38. template <typename T>
  39. inline constexpr static T PlainOrUnderSanitizer(T plain, T sanitized) noexcept {
  40. #if defined(_tsan_enabled_) || defined(_msan_enabled_) || defined(_asan_enabled_)
  41. Y_UNUSED(plain);
  42. return sanitized;
  43. #else
  44. Y_UNUSED(sanitized);
  45. return plain;
  46. #endif
  47. }
  48. // Determines if asan present
  49. inline constexpr static bool ASanIsOn() noexcept {
  50. #if defined(_asan_enabled_)
  51. return true;
  52. #else
  53. return false;
  54. #endif
  55. }
  56. // Determines if tsan present
  57. inline constexpr static bool TSanIsOn() noexcept {
  58. #if defined(_tsan_enabled_)
  59. return true;
  60. #else
  61. return false;
  62. #endif
  63. }
  64. // Determines if msan present
  65. inline constexpr static bool MSanIsOn() noexcept {
  66. #if defined(_msan_enabled_)
  67. return true;
  68. #else
  69. return false;
  70. #endif
  71. }
  72. // Make memory region fully initialized (without changing its contents).
  73. inline static void Unpoison(const volatile void* a, size_t size) noexcept {
  74. #if defined(_msan_enabled_)
  75. __msan_unpoison(a, size);
  76. #else
  77. Y_UNUSED(a);
  78. Y_UNUSED(size);
  79. #endif
  80. }
  81. // Make memory region fully uninitialized (without changing its contents).
  82. // This is a legacy interface that does not update origin information. Use __msan_allocated_memory() instead.
  83. inline static void Poison(const volatile void* a, size_t size) noexcept {
  84. #if defined(_msan_enabled_)
  85. __msan_poison(a, size);
  86. #else
  87. Y_UNUSED(a);
  88. Y_UNUSED(size);
  89. #endif
  90. }
  91. // Checks that memory range is fully initialized, and reports an error if it is not.
  92. inline static void CheckMemIsInitialized(const volatile void* a, size_t size) noexcept {
  93. #if defined(_msan_enabled_)
  94. __msan_check_mem_is_initialized(a, size);
  95. #else
  96. Y_UNUSED(a);
  97. Y_UNUSED(size);
  98. #endif
  99. }
  100. inline static void MarkAsIntentionallyLeaked(const void* ptr) noexcept {
  101. #if defined(_asan_enabled_)
  102. __lsan_ignore_object(ptr);
  103. #else
  104. Y_UNUSED(ptr);
  105. #endif
  106. }
  107. #if defined(_tsan_enabled_)
  108. // defined in .cpp to avoid exposing problematic C-linkage version of AnnotateBenignRaceSized(...)
  109. void AnnotateBenignRaceSized(const char* file, int line,
  110. const volatile void* address,
  111. size_t size,
  112. const char* description) noexcept;
  113. #else
  114. inline static void AnnotateBenignRaceSized(const char* file, int line,
  115. const volatile void* address,
  116. size_t size,
  117. const char* description) noexcept {
  118. Y_UNUSED(file);
  119. Y_UNUSED(line);
  120. Y_UNUSED(address);
  121. Y_UNUSED(size);
  122. Y_UNUSED(description);
  123. }
  124. #endif
  125. inline static void Acquire(void* a) {
  126. #if defined(_tsan_enabled_)
  127. __tsan_acquire(a);
  128. #else
  129. Y_UNUSED(a);
  130. #endif
  131. }
  132. inline static void Release(void* a) {
  133. #if defined(_tsan_enabled_)
  134. __tsan_release(a);
  135. #else
  136. Y_UNUSED(a);
  137. #endif
  138. }
  139. }