tsan_external.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void InsertShadowStackFrameForTag(ThreadState *thr, uptr tag) {
  42. FuncEntry(thr, (uptr)&registered_tags[tag]);
  43. }
  44. uptr TagFromShadowStackFrame(uptr pc) {
  45. uptr tag_count = atomic_load(&used_tags, memory_order_relaxed);
  46. void *pc_ptr = (void *)pc;
  47. if (pc_ptr < GetTagData(0) || pc_ptr > GetTagData(tag_count - 1))
  48. return 0;
  49. return (TagData *)pc_ptr - GetTagData(0);
  50. }
  51. #if !SANITIZER_GO
  52. void ExternalAccess(void *addr, uptr caller_pc, void *tag, AccessType typ) {
  53. CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
  54. ThreadState *thr = cur_thread();
  55. if (caller_pc) FuncEntry(thr, caller_pc);
  56. InsertShadowStackFrameForTag(thr, (uptr)tag);
  57. bool in_ignored_lib;
  58. if (!caller_pc || !libignore()->IsIgnored(caller_pc, &in_ignored_lib))
  59. MemoryAccess(thr, CALLERPC, (uptr)addr, 1, typ);
  60. FuncExit(thr);
  61. if (caller_pc) FuncExit(thr);
  62. }
  63. extern "C" {
  64. SANITIZER_INTERFACE_ATTRIBUTE
  65. void *__tsan_external_register_tag(const char *object_type) {
  66. uptr new_tag = atomic_fetch_add(&used_tags, 1, memory_order_relaxed);
  67. CHECK_LT(new_tag, kExternalTagMax);
  68. GetTagData(new_tag)->object_type = internal_strdup(object_type);
  69. char header[127] = {0};
  70. internal_snprintf(header, sizeof(header), "race on %s", object_type);
  71. GetTagData(new_tag)->header = internal_strdup(header);
  72. return (void *)new_tag;
  73. }
  74. SANITIZER_INTERFACE_ATTRIBUTE
  75. void __tsan_external_register_header(void *tag, const char *header) {
  76. CHECK_GE((uptr)tag, kExternalTagFirstUserAvailable);
  77. CHECK_LT((uptr)tag, kExternalTagMax);
  78. atomic_uintptr_t *header_ptr =
  79. (atomic_uintptr_t *)&GetTagData((uptr)tag)->header;
  80. header = internal_strdup(header);
  81. char *old_header =
  82. (char *)atomic_exchange(header_ptr, (uptr)header, memory_order_seq_cst);
  83. Free(old_header);
  84. }
  85. SANITIZER_INTERFACE_ATTRIBUTE
  86. void __tsan_external_assign_tag(void *addr, void *tag) {
  87. CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
  88. Allocator *a = allocator();
  89. MBlock *b = nullptr;
  90. if (a->PointerIsMine((void *)addr)) {
  91. void *block_begin = a->GetBlockBegin((void *)addr);
  92. if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
  93. }
  94. if (b) {
  95. b->tag = (uptr)tag;
  96. }
  97. }
  98. SANITIZER_INTERFACE_ATTRIBUTE
  99. void __tsan_external_read(void *addr, void *caller_pc, void *tag) {
  100. ExternalAccess(addr, STRIP_PAC_PC(caller_pc), tag, kAccessRead);
  101. }
  102. SANITIZER_INTERFACE_ATTRIBUTE
  103. void __tsan_external_write(void *addr, void *caller_pc, void *tag) {
  104. ExternalAccess(addr, STRIP_PAC_PC(caller_pc), tag, kAccessWrite);
  105. }
  106. } // extern "C"
  107. #endif // !SANITIZER_GO
  108. } // namespace __tsan