Consistently use "rG" constraint for copy instruction in move patterns
[official-gcc.git] / libsanitizer / tsan / tsan_external.cpp
blob19ae174f20a595db3acf3d493569c0577d055561
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.
11 //===----------------------------------------------------------------------===//
12 #include "tsan_rtl.h"
13 #include "sanitizer_common/sanitizer_ptrauth.h"
15 #if !SANITIZER_GO
16 # include "tsan_interceptors.h"
17 #endif
19 namespace __tsan {
21 #define CALLERPC ((uptr)__builtin_return_address(0))
23 struct TagData {
24 const char *object_type;
25 const char *header;
28 static TagData registered_tags[kExternalTagMax] = {
29 {},
30 {"Swift variable", "Swift access race"},
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];
39 const char *GetObjectTypeFromTag(uptr tag) {
40 TagData *tag_data = GetTagData(tag);
41 return tag_data ? tag_data->object_type : nullptr;
44 const char *GetReportHeaderFromTag(uptr tag) {
45 TagData *tag_data = GetTagData(tag);
46 return tag_data ? tag_data->header : nullptr;
49 void InsertShadowStackFrameForTag(ThreadState *thr, uptr tag) {
50 FuncEntry(thr, (uptr)&registered_tags[tag]);
53 uptr TagFromShadowStackFrame(uptr pc) {
54 uptr tag_count = atomic_load(&used_tags, memory_order_relaxed);
55 void *pc_ptr = (void *)pc;
56 if (pc_ptr < GetTagData(0) || pc_ptr > GetTagData(tag_count - 1))
57 return 0;
58 return (TagData *)pc_ptr - GetTagData(0);
61 #if !SANITIZER_GO
63 void ExternalAccess(void *addr, uptr caller_pc, void *tag, AccessType typ) {
64 CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
65 ThreadState *thr = cur_thread();
66 if (caller_pc) FuncEntry(thr, caller_pc);
67 InsertShadowStackFrameForTag(thr, (uptr)tag);
68 bool in_ignored_lib;
69 if (!caller_pc || !libignore()->IsIgnored(caller_pc, &in_ignored_lib))
70 MemoryAccess(thr, CALLERPC, (uptr)addr, 1, typ);
71 FuncExit(thr);
72 if (caller_pc) FuncExit(thr);
75 extern "C" {
76 SANITIZER_INTERFACE_ATTRIBUTE
77 void *__tsan_external_register_tag(const char *object_type) {
78 uptr new_tag = atomic_fetch_add(&used_tags, 1, memory_order_relaxed);
79 CHECK_LT(new_tag, kExternalTagMax);
80 GetTagData(new_tag)->object_type = internal_strdup(object_type);
81 char header[127] = {0};
82 internal_snprintf(header, sizeof(header), "race on %s", object_type);
83 GetTagData(new_tag)->header = internal_strdup(header);
84 return (void *)new_tag;
87 SANITIZER_INTERFACE_ATTRIBUTE
88 void __tsan_external_register_header(void *tag, const char *header) {
89 CHECK_GE((uptr)tag, kExternalTagFirstUserAvailable);
90 CHECK_LT((uptr)tag, kExternalTagMax);
91 atomic_uintptr_t *header_ptr =
92 (atomic_uintptr_t *)&GetTagData((uptr)tag)->header;
93 header = internal_strdup(header);
94 char *old_header =
95 (char *)atomic_exchange(header_ptr, (uptr)header, memory_order_seq_cst);
96 Free(old_header);
99 SANITIZER_INTERFACE_ATTRIBUTE
100 void __tsan_external_assign_tag(void *addr, void *tag) {
101 CHECK_LT(tag, atomic_load(&used_tags, memory_order_relaxed));
102 Allocator *a = allocator();
103 MBlock *b = nullptr;
104 if (a->PointerIsMine((void *)addr)) {
105 void *block_begin = a->GetBlockBegin((void *)addr);
106 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
108 if (b) {
109 b->tag = (uptr)tag;
113 SANITIZER_INTERFACE_ATTRIBUTE
114 void __tsan_external_read(void *addr, void *caller_pc, void *tag) {
115 ExternalAccess(addr, STRIP_PAC_PC(caller_pc), tag, kAccessRead);
118 SANITIZER_INTERFACE_ATTRIBUTE
119 void __tsan_external_write(void *addr, void *caller_pc, void *tag) {
120 ExternalAccess(addr, STRIP_PAC_PC(caller_pc), tag, kAccessWrite);
122 } // extern "C"
124 #endif // !SANITIZER_GO
126 } // namespace __tsan