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 "tsan_interceptors.h"
14 #include "sanitizer_common/sanitizer_ptrauth.h"
15 
16 namespace __tsan {
17 
18 #define CALLERPC ((uptr)__builtin_return_address(0))
19 
20 struct TagData {
21   const char *object_type;
22   const char *header;
23 };
24 
25 static TagData registered_tags[kExternalTagMax] = {
26   {},
27   {"Swift variable", "Swift access race"},
28 };
29 static atomic_uint32_t used_tags{kExternalTagFirstUserAvailable};
GetTagData(uptr tag)30 static TagData *GetTagData(uptr tag) {
31   // Invalid/corrupted tag?  Better return NULL and let the caller deal with it.
32   if (tag >= atomic_load(&used_tags, memory_order_relaxed)) return nullptr;
33   return &registered_tags[tag];
34 }
35 
GetObjectTypeFromTag(uptr tag)36 const char *GetObjectTypeFromTag(uptr tag) {
37   TagData *tag_data = GetTagData(tag);
38   return tag_data ? tag_data->object_type : nullptr;
39 }
40 
GetReportHeaderFromTag(uptr tag)41 const char *GetReportHeaderFromTag(uptr tag) {
42   TagData *tag_data = GetTagData(tag);
43   return tag_data ? tag_data->header : nullptr;
44 }
45 
InsertShadowStackFrameForTag(ThreadState * thr,uptr tag)46 void InsertShadowStackFrameForTag(ThreadState *thr, uptr tag) {
47   FuncEntry(thr, (uptr)&registered_tags[tag]);
48 }
49 
TagFromShadowStackFrame(uptr pc)50 uptr TagFromShadowStackFrame(uptr pc) {
51   uptr tag_count = atomic_load(&used_tags, memory_order_relaxed);
52   void *pc_ptr = (void *)pc;
53   if (pc_ptr < GetTagData(0) || pc_ptr > GetTagData(tag_count - 1))
54     return 0;
55   return (TagData *)pc_ptr - GetTagData(0);
56 }
57 
58 #if !SANITIZER_GO
59 
60 typedef void(*AccessFunc)(ThreadState *, uptr, uptr, int);
ExternalAccess(void * addr,uptr caller_pc,void * tag,AccessFunc access)61 void ExternalAccess(void *addr, uptr caller_pc, void *tag, AccessFunc access) {
62   CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
63   ThreadState *thr = cur_thread();
64   if (caller_pc) FuncEntry(thr, caller_pc);
65   InsertShadowStackFrameForTag(thr, (uptr)tag);
66   bool in_ignored_lib;
67   if (!caller_pc || !libignore()->IsIgnored(caller_pc, &in_ignored_lib)) {
68     access(thr, CALLERPC, (uptr)addr, kSizeLog1);
69   }
70   FuncExit(thr);
71   if (caller_pc) FuncExit(thr);
72 }
73 
74 extern "C" {
75 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_external_register_tag(const char * object_type)76 void *__tsan_external_register_tag(const char *object_type) {
77   uptr new_tag = atomic_fetch_add(&used_tags, 1, memory_order_relaxed);
78   CHECK_LT(new_tag, kExternalTagMax);
79   GetTagData(new_tag)->object_type = internal_strdup(object_type);
80   char header[127] = {0};
81   internal_snprintf(header, sizeof(header), "race on %s", object_type);
82   GetTagData(new_tag)->header = internal_strdup(header);
83   return (void *)new_tag;
84 }
85 
86 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_external_register_header(void * tag,const char * header)87 void __tsan_external_register_header(void *tag, const char *header) {
88   CHECK_GE((uptr)tag, kExternalTagFirstUserAvailable);
89   CHECK_LT((uptr)tag, kExternalTagMax);
90   atomic_uintptr_t *header_ptr =
91       (atomic_uintptr_t *)&GetTagData((uptr)tag)->header;
92   header = internal_strdup(header);
93   char *old_header =
94       (char *)atomic_exchange(header_ptr, (uptr)header, memory_order_seq_cst);
95   if (old_header) internal_free(old_header);
96 }
97 
98 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_external_assign_tag(void * addr,void * tag)99 void __tsan_external_assign_tag(void *addr, void *tag) {
100   CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
101   Allocator *a = allocator();
102   MBlock *b = nullptr;
103   if (a->PointerIsMine((void *)addr)) {
104     void *block_begin = a->GetBlockBegin((void *)addr);
105     if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
106   }
107   if (b) {
108     b->tag = (uptr)tag;
109   }
110 }
111 
112 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_external_read(void * addr,void * caller_pc,void * tag)113 void __tsan_external_read(void *addr, void *caller_pc, void *tag) {
114   ExternalAccess(addr, STRIP_PC(caller_pc), tag, MemoryRead);
115 }
116 
117 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_external_write(void * addr,void * caller_pc,void * tag)118 void __tsan_external_write(void *addr, void *caller_pc, void *tag) {
119   ExternalAccess(addr, STRIP_PC(caller_pc), tag, MemoryWrite);
120 }
121 }  // extern "C"
122 
123 #endif  // !SANITIZER_GO
124 
125 }  // namespace __tsan
126