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
19namespace __tsan {
20
21#define CALLERPC ((uptr)__builtin_return_address(0))
22
23struct TagData {
24 const char *object_type;
25 const char *header;
26};
27
28static TagData registered_tags[kExternalTagMax] = {
29 {},
30 {.object_type: "Swift variable", .header: "Swift access race"},
31};
32static atomic_uint32_t used_tags{.val_dont_use: kExternalTagFirstUserAvailable};
33static TagData *GetTagData(uptr tag) {
34 // Invalid/corrupted tag? Better return NULL and let the caller deal with it.
35 if (tag >= atomic_load(a: &used_tags, mo: memory_order_relaxed)) return nullptr;
36 return &registered_tags[tag];
37}
38
39const char *GetObjectTypeFromTag(uptr tag) {
40 TagData *tag_data = GetTagData(tag);
41 return tag_data ? tag_data->object_type : nullptr;
42}
43
44const char *GetReportHeaderFromTag(uptr tag) {
45 TagData *tag_data = GetTagData(tag);
46 return tag_data ? tag_data->header : nullptr;
47}
48
49uptr TagFromShadowStackFrame(uptr pc) {
50 uptr tag_count = atomic_load(a: &used_tags, mo: memory_order_relaxed);
51 void *pc_ptr = (void *)pc;
52 if (pc_ptr < GetTagData(tag: 0) || pc_ptr > GetTagData(tag: tag_count - 1))
53 return 0;
54 return (TagData *)pc_ptr - GetTagData(tag: 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.
62static void PushTag(ThreadState *thr, uptr tag) {
63 FuncEntry(thr, pc: (uptr)&registered_tags[tag]);
64}
65static void PopTag(ThreadState *thr) { FuncExit(thr); }
66
67static 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(pc: caller_pc, pc_in_ignored_lib: &in_ignored_lib))
72 return;
73
74 ThreadState *thr = cur_thread();
75 if (caller_pc) FuncEntry(thr, pc: caller_pc);
76 PushTag(thr, tag: (uptr)tag);
77 MemoryAccess(thr, pc: tsan_caller_pc, addr: (uptr)addr, size: 1, typ);
78 PopTag(thr);
79 if (caller_pc) FuncExit(thr);
80}
81
82extern "C" {
83SANITIZER_INTERFACE_ATTRIBUTE
84void *__tsan_external_register_tag(const char *object_type) {
85 uptr new_tag = atomic_fetch_add(a: &used_tags, v: 1, mo: memory_order_relaxed);
86 CHECK_LT(new_tag, kExternalTagMax);
87 GetTagData(tag: new_tag)->object_type = internal_strdup(s: object_type);
88 char header[127] = {0};
89 internal_snprintf(buffer: header, length: sizeof(header), format: "race on %s", object_type);
90 GetTagData(tag: new_tag)->header = internal_strdup(s: header);
91 return (void *)new_tag;
92}
93
94SANITIZER_INTERFACE_ATTRIBUTE
95void __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(tag: (uptr)tag)->header;
100 header = internal_strdup(s: header);
101 char *old_header =
102 (char *)atomic_exchange(a: header_ptr, v: (uptr)header, mo: memory_order_seq_cst);
103 Free(p&: old_header);
104}
105
106SANITIZER_INTERFACE_ATTRIBUTE
107void __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(p: (void *)addr)) {
112 void *block_begin = a->GetBlockBegin(p: (void *)addr);
113 if (block_begin) b = ctx->metamap.GetBlock(p: (uptr)block_begin);
114 }
115 if (b) {
116 b->tag = (uptr)tag;
117 }
118}
119
120SANITIZER_INTERFACE_ATTRIBUTE
121void __tsan_external_read(void *addr, void *caller_pc, void *tag) {
122 ExternalAccess(addr, STRIP_PAC_PC(caller_pc), CALLERPC, tag, typ: kAccessRead);
123}
124
125SANITIZER_INTERFACE_ATTRIBUTE
126void __tsan_external_write(void *addr, void *caller_pc, void *tag) {
127 ExternalAccess(addr, STRIP_PAC_PC(caller_pc), CALLERPC, tag, typ: kAccessWrite);
128}
129} // extern "C"
130
131#endif // !SANITIZER_GO
132
133} // namespace __tsan
134

source code of compiler-rt/lib/tsan/rtl/tsan_external.cpp