sanitizers.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "sanitizers.h"
  2. #include "thread.h"
  3. using namespace NSan;
  4. #if defined(_tsan_enabled_)
  5. #if defined(__clang_major__) && (__clang_major__ >= 9)
  6. extern "C" { // sanitizers API
  7. #if defined(_tsan_enabled_)
  8. void* __tsan_create_fiber(unsigned flags);
  9. void __tsan_set_fiber_name(void* fiber, const char* name);
  10. #endif
  11. } // sanitizers API
  12. #else
  13. namespace {
  14. void* __tsan_create_fiber(unsigned) {
  15. return nullptr;
  16. }
  17. void __tsan_set_fiber_name(void*, const char*) {
  18. }
  19. } // namespace
  20. #endif
  21. #endif
  22. TFiberContext::TFiberContext() noexcept
  23. : Token_(nullptr)
  24. , IsMainFiber_(true)
  25. #if defined(_tsan_enabled_)
  26. , CurrentTSanFiberContext_(__tsan_get_current_fiber())
  27. #endif
  28. {
  29. TCurrentThreadLimits sl;
  30. (void)Token_;
  31. Stack_ = sl.StackBegin;
  32. Len_ = sl.StackLength;
  33. #if defined(_tsan_enabled_)
  34. static constexpr char MainFiberName[] = "main_fiber";
  35. __tsan_set_fiber_name(CurrentTSanFiberContext_, MainFiberName);
  36. #endif
  37. }
  38. TFiberContext::TFiberContext(const void* stack, size_t len, const char* contName) noexcept
  39. : Token_(nullptr)
  40. , Stack_(stack)
  41. , Len_(len)
  42. , IsMainFiber_(false)
  43. #if defined(_tsan_enabled_)
  44. , CurrentTSanFiberContext_(__tsan_create_fiber(/*flags =*/0))
  45. #endif
  46. {
  47. (void)contName;
  48. #if defined(_tsan_enabled_)
  49. __tsan_set_fiber_name(CurrentTSanFiberContext_, contName);
  50. #endif
  51. }
  52. #if defined(_tsan_enabled_)
  53. extern "C" {
  54. // This function should not be directly exposed in headers
  55. // due to signature variations among contrib headers.
  56. void AnnotateBenignRaceSized(const char* file, int line,
  57. const volatile void* address,
  58. size_t size,
  59. const char* description);
  60. }
  61. void NSan::AnnotateBenignRaceSized(const char* file, int line,
  62. const volatile void* address,
  63. size_t size,
  64. const char* description) noexcept {
  65. ::AnnotateBenignRaceSized(file, line, address, size, description);
  66. }
  67. #endif