tsan_external.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //===-- tsan_external.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 ThreadSanitizer (TSan), a race detector.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "tsan_rtl.h"
  13. #include "sanitizer_common/sanitizer_ptrauth.h"
  14. #if !SANITIZER_GO
  15. # include "tsan_interceptors.h"
  16. #endif
  17. namespace __tsan {
  18. #define CALLERPC ((uptr)__builtin_return_address(0))
  19. struct TagData {
  20. const char *object_type;
  21. const char *header;
  22. };
  23. static TagData registered_tags[kExternalTagMax] = {
  24. {},
  25. {"Swift variable", "Swift access race"},
  26. };
  27. static atomic_uint32_t used_tags{kExternalTagFirstUserAvailable};
  28. static TagData *GetTagData(uptr tag) {
  29. // Invalid/corrupted tag? Better return NULL and let the caller deal with it.
  30. if (tag >= atomic_load(&used_tags, memory_order_relaxed)) return nullptr;
  31. return &registered_tags[tag];
  32. }
  33. const char *GetObjectTypeFromTag(uptr tag) {
  34. TagData *tag_data = GetTagData(tag);
  35. return tag_data ? tag_data->object_type : nullptr;
  36. }
  37. const char *GetReportHeaderFromTag(uptr tag) {
  38. TagData *tag_data = GetTagData(tag);
  39. return tag_data ? tag_data->header : nullptr;
  40. }
  41. uptr TagFromShadowStackFrame(uptr pc) {
  42. uptr tag_count = atomic_load(&used_tags, memory_order_relaxed);
  43. void *pc_ptr = (void *)pc;
  44. if (pc_ptr < GetTagData(0) || pc_ptr > GetTagData(tag_count - 1))
  45. return 0;
  46. return (TagData *)pc_ptr - GetTagData(0);
  47. }
  48. #if !SANITIZER_GO
  49. // We need to track tags for individual memory accesses, but there is no space
  50. // in the shadow cells for them. Instead we push/pop them onto the thread
  51. // traces and ignore the extra tag frames when printing reports.
  52. static void PushTag(ThreadState *thr, uptr tag) {
  53. FuncEntry(thr, (uptr)&registered_tags[tag]);
  54. }
  55. static void PopTag(ThreadState *thr) { FuncExit(thr); }
  56. static void ExternalAccess(void *addr, uptr caller_pc, uptr tsan_caller_pc,
  57. void *tag, AccessType typ) {
  58. CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
  59. bool in_ignored_lib;
  60. if (caller_pc && libignore()->IsIgnored(caller_pc, &in_ignored_lib))
  61. return;
  62. ThreadState *thr = cur_thread();
  63. if (caller_pc) FuncEntry(thr, caller_pc);
  64. PushTag(thr, (uptr)tag);
  65. MemoryAccess(thr, tsan_caller_pc, (uptr)addr, 1, typ);
  66. PopTag(thr);
  67. if (caller_pc) FuncExit(thr);
  68. }
  69. extern "C" {
  70. SANITIZER_INTERFACE_ATTRIBUTE
  71. void *__tsan_external_register_tag(const char *object_type) {
  72. uptr new_tag = atomic_fetch_add(&used_tags, 1, memory_order_relaxed);
  73. CHECK_LT(new_tag, kExternalTagMax);
  74. GetTagData(new_tag)->object_type = internal_strdup(object_type);
  75. char header[127] = {0};
  76. internal_snprintf(header, sizeof(header), "race on %s", object_type);
  77. GetTagData(new_tag)->header = internal_strdup(header);
  78. return (void *)new_tag;
  79. }
  80. SANITIZER_INTERFACE_ATTRIBUTE
  81. void __tsan_external_register_header(void *tag, const char *header) {
  82. CHECK_GE((uptr)tag, kExternalTagFirstUserAvailable);
  83. CHECK_LT((uptr)tag, kExternalTagMax);
  84. atomic_uintptr_t *header_ptr =
  85. (atomic_uintptr_t *)&GetTagData((uptr)tag)->header;
  86. header = internal_strdup(header);
  87. char *old_header =
  88. (char *)atomic_exchange(header_ptr, (uptr)header, memory_order_seq_cst);
  89. Free(old_header);
  90. }
  91. SANITIZER_INTERFACE_ATTRIBUTE
  92. void __tsan_external_assign_tag(void *addr, void *tag) {
  93. CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
  94. Allocator *a = allocator();
  95. MBlock *b = nullptr;
  96. if (a->PointerIsMine((void *)addr)) {
  97. void *block_begin = a->GetBlockBegin((void *)addr);
  98. if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
  99. }
  100. if (b) {
  101. b->tag = (uptr)tag;
  102. }
  103. }
  104. SANITIZER_INTERFACE_ATTRIBUTE
  105. void __tsan_external_read(void *addr, void *caller_pc, void *tag) {
  106. ExternalAccess(addr, STRIP_PAC_PC(caller_pc), CALLERPC, tag, kAccessRead);
  107. }
  108. SANITIZER_INTERFACE_ATTRIBUTE
  109. void __tsan_external_write(void *addr, void *caller_pc, void *tag) {
  110. ExternalAccess(addr, STRIP_PAC_PC(caller_pc), CALLERPC, tag, kAccessWrite);
  111. }
  112. } // extern "C"
  113. #endif // !SANITIZER_GO
  114. } // namespace __tsan