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