1 //===-- tsan_go.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // ThreadSanitizer runtime for Go language.
11 //===----------------------------------------------------------------------===//
14 #include "tsan_symbolize.h"
15 #include "sanitizer_common/sanitizer_common.h"
20 void InitializeInterceptors() {
23 void InitializeDynamicAnnotations() {
26 bool IsExpectedReport(uptr addr, uptr size) {
30 void *internal_alloc(MBlockType typ, uptr sz) {
31 return InternalAlloc(sz);
34 void internal_free(void *p) {
39 static void (*go_runtime_cb)(uptr cmd, void *ctx);
43 CallbackSymbolizeCode = 1,
44 CallbackSymbolizeData = 2,
47 struct SymbolizeCodeContext {
56 SymbolizedStack *SymbolizeCode(uptr addr) {
57 SymbolizedStack *first = SymbolizedStack::New(addr);
58 SymbolizedStack *s = first;
60 SymbolizeCodeContext cbctx;
61 internal_memset(&cbctx, 0, sizeof(cbctx));
63 go_runtime_cb(CallbackSymbolizeCode, &cbctx);
66 AddressInfo &info = s->info;
67 info.module_offset = cbctx.off;
68 info.function = internal_strdup(cbctx.func ? cbctx.func : "??");
69 info.file = internal_strdup(cbctx.file ? cbctx.file : "-");
70 info.line = cbctx.line;
73 if (cbctx.pc == addr) // outermost (non-inlined) function
76 // Allocate a stack entry for the parent of the inlined function.
77 SymbolizedStack *s2 = SymbolizedStack::New(addr);
84 struct SymbolizeDataContext {
95 ReportLocation *SymbolizeData(uptr addr) {
96 SymbolizeDataContext cbctx;
97 internal_memset(&cbctx, 0, sizeof(cbctx));
99 go_runtime_cb(CallbackSymbolizeData, &cbctx);
103 MBlock *b = ctx->metamap.GetBlock(cbctx.start);
106 ReportLocation *loc = ReportLocation::New(ReportLocationHeap);
107 loc->heap_chunk_start = cbctx.start;
108 loc->heap_chunk_size = b->siz;
110 loc->stack = SymbolizeStackId(b->stk);
113 ReportLocation *loc = ReportLocation::New(ReportLocationGlobal);
114 loc->global.name = internal_strdup(cbctx.name ? cbctx.name : "??");
115 loc->global.file = internal_strdup(cbctx.file ? cbctx.file : "??");
116 loc->global.line = cbctx.line;
117 loc->global.start = cbctx.start;
118 loc->global.size = cbctx.size;
123 static ThreadState *main_thr;
126 static Processor* get_cur_proc() {
127 if (UNLIKELY(!inited)) {
128 // Running Initialize().
129 // We have not yet returned the Processor to Go, so we cannot ask it back.
130 // Currently, Initialize() does not use the Processor, so return nullptr.
134 go_runtime_cb(CallbackGetProc, &proc);
138 Processor *ThreadState::proc() {
139 return get_cur_proc();
144 static ThreadState *AllocGoroutine() {
145 ThreadState *thr = (ThreadState*)internal_alloc(MBlockThreadContex,
146 sizeof(ThreadState));
147 internal_memset(thr, 0, sizeof(*thr));
151 void __tsan_init(ThreadState **thrp, Processor **procp,
152 void (*cb)(uptr cmd, void *cb)) {
154 ThreadState *thr = AllocGoroutine();
155 main_thr = *thrp = thr;
162 // FIXME: Not necessary thread 0.
163 ThreadState *thr = main_thr;
164 int res = Finalize(thr);
168 void __tsan_map_shadow(uptr addr, uptr size) {
169 MapShadow(addr, size);
172 void __tsan_read(ThreadState *thr, void *addr, void *pc) {
173 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
176 void __tsan_read_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
178 FuncEntry(thr, callpc);
179 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
184 void __tsan_write(ThreadState *thr, void *addr, void *pc) {
185 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
188 void __tsan_write_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
190 FuncEntry(thr, callpc);
191 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
196 void __tsan_read_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
197 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, false);
200 void __tsan_write_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
201 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, true);
204 void __tsan_func_enter(ThreadState *thr, void *pc) {
205 FuncEntry(thr, (uptr)pc);
208 void __tsan_func_exit(ThreadState *thr) {
212 void __tsan_malloc(ThreadState *thr, uptr pc, uptr p, uptr sz) {
215 ctx->metamap.AllocBlock(thr, pc, p, sz);
216 MemoryResetRange(0, 0, (uptr)p, sz);
219 void __tsan_free(uptr p, uptr sz) {
220 ctx->metamap.FreeRange(get_cur_proc(), p, sz);
223 void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
224 ThreadState *thr = AllocGoroutine();
226 int goid = ThreadCreate(parent, (uptr)pc, 0, true);
227 ThreadStart(thr, goid, 0, ThreadType::Regular);
230 void __tsan_go_end(ThreadState *thr) {
235 void __tsan_proc_create(Processor **pproc) {
236 *pproc = ProcCreate();
239 void __tsan_proc_destroy(Processor *proc) {
243 void __tsan_acquire(ThreadState *thr, void *addr) {
244 Acquire(thr, 0, (uptr)addr);
247 void __tsan_release(ThreadState *thr, void *addr) {
248 ReleaseStore(thr, 0, (uptr)addr);
251 void __tsan_release_merge(ThreadState *thr, void *addr) {
252 Release(thr, 0, (uptr)addr);
255 void __tsan_finalizer_goroutine(ThreadState *thr) {
256 AcquireGlobal(thr, 0);
259 void __tsan_mutex_before_lock(ThreadState *thr, uptr addr, uptr write) {
261 MutexPreLock(thr, 0, addr);
263 MutexPreReadLock(thr, 0, addr);
266 void __tsan_mutex_after_lock(ThreadState *thr, uptr addr, uptr write) {
268 MutexPostLock(thr, 0, addr);
270 MutexPostReadLock(thr, 0, addr);
273 void __tsan_mutex_before_unlock(ThreadState *thr, uptr addr, uptr write) {
275 MutexUnlock(thr, 0, addr);
277 MutexReadUnlock(thr, 0, addr);
280 void __tsan_go_ignore_sync_begin(ThreadState *thr) {
281 ThreadIgnoreSyncBegin(thr, 0);
284 void __tsan_go_ignore_sync_end(ThreadState *thr) {
285 ThreadIgnoreSyncEnd(thr, 0);
288 void __tsan_report_count(u64 *pn) {
289 Lock lock(&ctx->report_mtx);
290 *pn = ctx->nreported;
294 } // namespace __tsan