arm: Fix ICE on arm_mve.h pragma without MVE types [PR117408]
[official-gcc.git] / libsanitizer / tsan / tsan_external.cpp
blob98abff54e2b2817b37824323d3fe598ec5e85109
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 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);
57 #if !SANITIZER_GO
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]);
65 static void PopTag(ThreadState *thr) { FuncExit(thr); }
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;
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);
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;
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);
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);
115 if (b) {
116 b->tag = (uptr)tag;
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);
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);
129 } // extern "C"
131 #endif // !SANITIZER_GO
133 } // namespace __tsan