]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/compiler-rt/lib/tsan/go/tsan_go.cc
Unbreak DRM KMS build by adding the needed compatibility field in the LinuxKPI.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / compiler-rt / lib / tsan / go / tsan_go.cc
1 //===-- tsan_go.cc --------------------------------------------------------===//
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 // ThreadSanitizer runtime for Go language.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "tsan_rtl.h"
14 #include "tsan_symbolize.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include <stdlib.h>
17
18 namespace __tsan {
19
20 void InitializeInterceptors() {
21 }
22
23 void InitializeDynamicAnnotations() {
24 }
25
26 bool IsExpectedReport(uptr addr, uptr size) {
27   return false;
28 }
29
30 void *internal_alloc(MBlockType typ, uptr sz) {
31   return InternalAlloc(sz);
32 }
33
34 void internal_free(void *p) {
35   InternalFree(p);
36 }
37
38 // Callback into Go.
39 static void (*go_runtime_cb)(uptr cmd, void *ctx);
40
41 enum {
42   CallbackGetProc = 0,
43   CallbackSymbolizeCode = 1,
44   CallbackSymbolizeData = 2,
45 };
46
47 struct SymbolizeCodeContext {
48   uptr pc;
49   char *func;
50   char *file;
51   uptr line;
52   uptr off;
53   uptr res;
54 };
55
56 SymbolizedStack *SymbolizeCode(uptr addr) {
57   SymbolizedStack *s = SymbolizedStack::New(addr);
58   SymbolizeCodeContext cbctx;
59   internal_memset(&cbctx, 0, sizeof(cbctx));
60   cbctx.pc = addr;
61   go_runtime_cb(CallbackSymbolizeCode, &cbctx);
62   if (cbctx.res) {
63     AddressInfo &info = s->info;
64     info.module_offset = cbctx.off;
65     info.function = internal_strdup(cbctx.func ? cbctx.func : "??");
66     info.file = internal_strdup(cbctx.file ? cbctx.file : "-");
67     info.line = cbctx.line;
68     info.column = 0;
69   }
70   return s;
71 }
72
73 struct SymbolizeDataContext {
74   uptr addr;
75   uptr heap;
76   uptr start;
77   uptr size;
78   char *name;
79   char *file;
80   uptr line;
81   uptr res;
82 };
83
84 ReportLocation *SymbolizeData(uptr addr) {
85   SymbolizeDataContext cbctx;
86   internal_memset(&cbctx, 0, sizeof(cbctx));
87   cbctx.addr = addr;
88   go_runtime_cb(CallbackSymbolizeData, &cbctx);
89   if (!cbctx.res)
90     return 0;
91   if (cbctx.heap) {
92     MBlock *b = ctx->metamap.GetBlock(cbctx.start);
93     if (!b)
94       return 0;
95     ReportLocation *loc = ReportLocation::New(ReportLocationHeap);
96     loc->heap_chunk_start = cbctx.start;
97     loc->heap_chunk_size = b->siz;
98     loc->tid = b->tid;
99     loc->stack = SymbolizeStackId(b->stk);
100     return loc;
101   } else {
102     ReportLocation *loc = ReportLocation::New(ReportLocationGlobal);
103     loc->global.name = internal_strdup(cbctx.name ? cbctx.name : "??");
104     loc->global.file = internal_strdup(cbctx.file ? cbctx.file : "??");
105     loc->global.line = cbctx.line;
106     loc->global.start = cbctx.start;
107     loc->global.size = cbctx.size;
108     return loc;
109   }
110 }
111
112 static ThreadState *main_thr;
113 static bool inited;
114
115 static Processor* get_cur_proc() {
116   if (UNLIKELY(!inited)) {
117     // Running Initialize().
118     // We have not yet returned the Processor to Go, so we cannot ask it back.
119     // Currently, Initialize() does not use the Processor, so return nullptr.
120     return nullptr;
121   }
122   Processor *proc;
123   go_runtime_cb(CallbackGetProc, &proc);
124   return proc;
125 }
126
127 Processor *ThreadState::proc() {
128   return get_cur_proc();
129 }
130
131 extern "C" {
132
133 static ThreadState *AllocGoroutine() {
134   ThreadState *thr = (ThreadState*)internal_alloc(MBlockThreadContex,
135       sizeof(ThreadState));
136   internal_memset(thr, 0, sizeof(*thr));
137   return thr;
138 }
139
140 void __tsan_init(ThreadState **thrp, Processor **procp,
141                  void (*cb)(uptr cmd, void *cb)) {
142   go_runtime_cb = cb;
143   ThreadState *thr = AllocGoroutine();
144   main_thr = *thrp = thr;
145   Initialize(thr);
146   *procp = thr->proc1;
147   inited = true;
148 }
149
150 void __tsan_fini() {
151   // FIXME: Not necessary thread 0.
152   ThreadState *thr = main_thr;
153   int res = Finalize(thr);
154   exit(res);
155 }
156
157 void __tsan_map_shadow(uptr addr, uptr size) {
158   MapShadow(addr, size);
159 }
160
161 void __tsan_read(ThreadState *thr, void *addr, void *pc) {
162   MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
163 }
164
165 void __tsan_read_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
166   if (callpc != 0)
167     FuncEntry(thr, callpc);
168   MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
169   if (callpc != 0)
170     FuncExit(thr);
171 }
172
173 void __tsan_write(ThreadState *thr, void *addr, void *pc) {
174   MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
175 }
176
177 void __tsan_write_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
178   if (callpc != 0)
179     FuncEntry(thr, callpc);
180   MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
181   if (callpc != 0)
182     FuncExit(thr);
183 }
184
185 void __tsan_read_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
186   MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, false);
187 }
188
189 void __tsan_write_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
190   MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, true);
191 }
192
193 void __tsan_func_enter(ThreadState *thr, void *pc) {
194   FuncEntry(thr, (uptr)pc);
195 }
196
197 void __tsan_func_exit(ThreadState *thr) {
198   FuncExit(thr);
199 }
200
201 void __tsan_malloc(ThreadState *thr, uptr pc, uptr p, uptr sz) {
202   CHECK(inited);
203   if (thr && pc)
204     ctx->metamap.AllocBlock(thr, pc, p, sz);
205   MemoryResetRange(0, 0, (uptr)p, sz);
206 }
207
208 void __tsan_free(uptr p, uptr sz) {
209   ctx->metamap.FreeRange(get_cur_proc(), p, sz);
210 }
211
212 void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
213   ThreadState *thr = AllocGoroutine();
214   *pthr = thr;
215   int goid = ThreadCreate(parent, (uptr)pc, 0, true);
216   ThreadStart(thr, goid, 0, ThreadType::Regular);
217 }
218
219 void __tsan_go_end(ThreadState *thr) {
220   ThreadFinish(thr);
221   internal_free(thr);
222 }
223
224 void __tsan_proc_create(Processor **pproc) {
225   *pproc = ProcCreate();
226 }
227
228 void __tsan_proc_destroy(Processor *proc) {
229   ProcDestroy(proc);
230 }
231
232 void __tsan_acquire(ThreadState *thr, void *addr) {
233   Acquire(thr, 0, (uptr)addr);
234 }
235
236 void __tsan_release(ThreadState *thr, void *addr) {
237   ReleaseStore(thr, 0, (uptr)addr);
238 }
239
240 void __tsan_release_merge(ThreadState *thr, void *addr) {
241   Release(thr, 0, (uptr)addr);
242 }
243
244 void __tsan_finalizer_goroutine(ThreadState *thr) {
245   AcquireGlobal(thr, 0);
246 }
247
248 void __tsan_mutex_before_lock(ThreadState *thr, uptr addr, uptr write) {
249   if (write)
250     MutexPreLock(thr, 0, addr);
251   else
252     MutexPreReadLock(thr, 0, addr);
253 }
254
255 void __tsan_mutex_after_lock(ThreadState *thr, uptr addr, uptr write) {
256   if (write)
257     MutexPostLock(thr, 0, addr);
258   else
259     MutexPostReadLock(thr, 0, addr);
260 }
261
262 void __tsan_mutex_before_unlock(ThreadState *thr, uptr addr, uptr write) {
263   if (write)
264     MutexUnlock(thr, 0, addr);
265   else
266     MutexReadUnlock(thr, 0, addr);
267 }
268
269 void __tsan_go_ignore_sync_begin(ThreadState *thr) {
270   ThreadIgnoreSyncBegin(thr, 0);
271 }
272
273 void __tsan_go_ignore_sync_end(ThreadState *thr) {
274   ThreadIgnoreSyncEnd(thr, 0);
275 }
276
277 void __tsan_report_count(u64 *pn) {
278   Lock lock(&ctx->report_mtx);
279   *pn = ctx->nreported;
280 }
281
282 }  // extern "C"
283 }  // namespace __tsan