╭ tsan_external.cpp ────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
.. builtins/ interception/ sanitizer_common/ ubsan/ LICENSE.TXT tsan_debugging.cpp tsan_defs.h tsan_dense_alloc.h tsan_dispatch_defs.h ╭─────────────────────────────────╮ │tsan_external.cpp │ ╰─────────────────────────────────╯ tsan_fd.cpp tsan_fd.h tsan_flags.cpp tsan_flags.h tsan_flags.inc tsan_ignoreset.cpp tsan_ignoreset.h tsan_ilist.h tsan_interceptors.h tsan_interceptors_mac.cpp tsan_interceptors_mach_vm.cpp tsan_interceptors_memintrinsics.c tsan_interceptors_posix.cpp tsan_interface.cpp tsan_interface.h tsan_interface.inc tsan_interface_ann.cpp tsan_interface_ann.h tsan_interface_atomic.cpp tsan_interface_java.cpp tsan_interface_java.h tsan_malloc_mac.cpp tsan_md5.cpp tsan_mman.cpp tsan_mman.h tsan_mutexset.cpp tsan_mutexset.h tsan_new_delete.cpp tsan_platform.h tsan_platform_linux.cpp tsan_platform_mac.cpp tsan_platform_posix.cpp tsan_platform_windows.cpp tsan_ppc_regs.h tsan_preinit.cpp tsan_report.cpp tsan_report.h tsan_rtl.cpp tsan_rtl.h tsan_rtl_aarch64.S tsan_rtl_access.cpp tsan_rtl_amd64.S tsan_rtl_loongarch64.S tsan_rtl_mips64.S tsan_rtl_mutex.cpp tsan_rtl_ppc64.S tsan_rtl_proc.cpp tsan_rtl_report.cpp tsan_rtl_riscv64.S tsan_rtl_s390x.S tsan_rtl_thread.cpp tsan_shadow.h tsan_stack_trace.cpp tsan_stack_trace.h tsan_suppressions.cpp tsan_suppressions.h tsan_symbolize.cpp tsan_symbolize.h tsan_sync.cpp tsan_sync.h tsan_trace.h tsan_vector_clock.cpp tsan_vector_clock.h
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 15 #if !SANITIZER_GO 16 # include "tsan_interceptors.h" 17 #endif 18 19 namespace __tsan { 20 21 #define CALLERPC ((uptr)__builtin_return_address(0)) 22 23 struct TagData { 24 const char *object_type; 25 const char *header; 26 }; 27 28 static TagData registered_tags[kExternalTagMax] = { 29 {}, 30 {"Swift variable", "Swift access race"}, 31 }; 32 static atomic_uint32_t used_tags{kExternalTagFirstUserAvailable}; 33 static TagData *GetTagData(uptr tag) { 34 // Invalid/corrupted tag? Better return NULL and let the caller deal with it. 35 if (tag >= atomic_load(&used_tags, memory_order_relaxed)) return nullptr; 36 return &registered_tags[tag]; 37 } 38 39 const char *GetObjectTypeFromTag(uptr tag) { 40 TagData *tag_data = GetTagData(tag); 41 return tag_data ? tag_data->object_type : nullptr; 42 } 43 44 const char *GetReportHeaderFromTag(uptr tag) { 45 TagData *tag_data = GetTagData(tag); 46 return tag_data ? tag_data->header : nullptr; 47 } 48 49 uptr TagFromShadowStackFrame(uptr pc) { 50 uptr tag_count = atomic_load(&used_tags, memory_order_relaxed); 51 void *pc_ptr = (void *)pc; 52 if (pc_ptr < GetTagData(0) || pc_ptr > GetTagData(tag_count - 1)) 53 return 0; 54 return (TagData *)pc_ptr - GetTagData(0); 55 } 56 57 #if !SANITIZER_GO 58 59 // We need to track tags for individual memory accesses, but there is no space 60 // in the shadow cells for them. Instead we push/pop them onto the thread 61 // traces and ignore the extra tag frames when printing reports. 62 static void PushTag(ThreadState *thr, uptr tag) { 63 FuncEntry(thr, (uptr)&registered_tags[tag]); 64 } 65 static void PopTag(ThreadState *thr) { FuncExit(thr); } 66 67 static void ExternalAccess(void *addr, uptr caller_pc, uptr tsan_caller_pc, 68 void *tag, AccessType typ) { 69 CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed)); 70 bool in_ignored_lib; 71 if (caller_pc && libignore()->IsIgnored(caller_pc, &in_ignored_lib)) 72 return; 73 74 ThreadState *thr = cur_thread(); 75 if (caller_pc) FuncEntry(thr, caller_pc); 76 PushTag(thr, (uptr)tag); 77 MemoryAccess(thr, tsan_caller_pc, (uptr)addr, 1, typ); 78 PopTag(thr); 79 if (caller_pc) FuncExit(thr); 80 } 81 82 extern "C" { 83 SANITIZER_INTERFACE_ATTRIBUTE 84 void *__tsan_external_register_tag(const char *object_type) { 85 uptr new_tag = atomic_fetch_add(&used_tags, 1, memory_order_relaxed); 86 CHECK_LT(new_tag, kExternalTagMax); 87 GetTagData(new_tag)->object_type = internal_strdup(object_type); 88 char header[127] = {0}; 89 internal_snprintf(header, sizeof(header), "race on %s", object_type); 90 GetTagData(new_tag)->header = internal_strdup(header); 91 return (void *)new_tag; 92 } 93 94 SANITIZER_INTERFACE_ATTRIBUTE 95 void __tsan_external_register_header(void *tag, const char *header) { 96 CHECK_GE((uptr)tag, kExternalTagFirstUserAvailable); 97 CHECK_LT((uptr)tag, kExternalTagMax); 98 atomic_uintptr_t *header_ptr = 99 (atomic_uintptr_t *)&GetTagData((uptr)tag)->header; 100 header = internal_strdup(header); 101 char *old_header = 102 (char *)atomic_exchange(header_ptr, (uptr)header, memory_order_seq_cst); 103 Free(old_header); 104 } 105 106 SANITIZER_INTERFACE_ATTRIBUTE 107 void __tsan_external_assign_tag(void *addr, void *tag) { 108 CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed)); 109 Allocator *a = allocator(); 110 MBlock *b = nullptr; 111 if (a->PointerIsMine((void *)addr)) { 112 void *block_begin = a->GetBlockBegin((void *)addr); 113 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin); 114 } 115 if (b) { 116 b->tag = (uptr)tag; 117 } 118 } 119 120 SANITIZER_INTERFACE_ATTRIBUTE 121 void __tsan_external_read(void *addr, void *caller_pc, void *tag) { 122 ExternalAccess(addr, STRIP_PAC_PC(caller_pc), CALLERPC, tag, kAccessRead); 123 } 124 125 SANITIZER_INTERFACE_ATTRIBUTE 126 void __tsan_external_write(void *addr, void *caller_pc, void *tag) { 127 ExternalAccess(addr, STRIP_PAC_PC(caller_pc), CALLERPC, tag, kAccessWrite); 128 } 129 } // extern "C" 130 131 #endif // !SANITIZER_GO 132 133 } // namespace __tsan 134