1 //===-- tsan_go.cc --------------------------------------------------------===//
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 *s = SymbolizedStack::New(addr);
58 SymbolizeCodeContext cbctx;
59 internal_memset(&cbctx, 0, sizeof(cbctx));
61 go_runtime_cb(CallbackSymbolizeCode, &cbctx);
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;
73 struct SymbolizeDataContext {
84 ReportLocation *SymbolizeData(uptr addr) {
85 SymbolizeDataContext cbctx;
86 internal_memset(&cbctx, 0, sizeof(cbctx));
88 go_runtime_cb(CallbackSymbolizeData, &cbctx);
92 MBlock *b = ctx->metamap.GetBlock(cbctx.start);
95 ReportLocation *loc = ReportLocation::New(ReportLocationHeap);
96 loc->heap_chunk_start = cbctx.start;
97 loc->heap_chunk_size = b->siz;
99 loc->stack = SymbolizeStackId(b->stk);
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;
112 static ThreadState *main_thr;
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.
123 go_runtime_cb(CallbackGetProc, &proc);
127 Processor *ThreadState::proc() {
128 return get_cur_proc();
133 static ThreadState *AllocGoroutine() {
134 ThreadState *thr = (ThreadState*)internal_alloc(MBlockThreadContex,
135 sizeof(ThreadState));
136 internal_memset(thr, 0, sizeof(*thr));
140 void __tsan_init(ThreadState **thrp, Processor **procp,
141 void (*cb)(uptr cmd, void *cb)) {
143 ThreadState *thr = AllocGoroutine();
144 main_thr = *thrp = thr;
151 // FIXME: Not necessary thread 0.
152 ThreadState *thr = main_thr;
153 int res = Finalize(thr);
157 void __tsan_map_shadow(uptr addr, uptr size) {
158 MapShadow(addr, size);
161 void __tsan_read(ThreadState *thr, void *addr, void *pc) {
162 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
165 void __tsan_read_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
167 FuncEntry(thr, callpc);
168 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
173 void __tsan_write(ThreadState *thr, void *addr, void *pc) {
174 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
177 void __tsan_write_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
179 FuncEntry(thr, callpc);
180 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
185 void __tsan_read_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
186 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, false);
189 void __tsan_write_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
190 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, true);
193 void __tsan_func_enter(ThreadState *thr, void *pc) {
194 FuncEntry(thr, (uptr)pc);
197 void __tsan_func_exit(ThreadState *thr) {
201 void __tsan_malloc(ThreadState *thr, uptr pc, uptr p, uptr sz) {
204 ctx->metamap.AllocBlock(thr, pc, p, sz);
205 MemoryResetRange(0, 0, (uptr)p, sz);
208 void __tsan_free(uptr p, uptr sz) {
209 ctx->metamap.FreeRange(get_cur_proc(), p, sz);
212 void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
213 ThreadState *thr = AllocGoroutine();
215 int goid = ThreadCreate(parent, (uptr)pc, 0, true);
216 ThreadStart(thr, goid, 0, ThreadType::Regular);
219 void __tsan_go_end(ThreadState *thr) {
224 void __tsan_proc_create(Processor **pproc) {
225 *pproc = ProcCreate();
228 void __tsan_proc_destroy(Processor *proc) {
232 void __tsan_acquire(ThreadState *thr, void *addr) {
233 Acquire(thr, 0, (uptr)addr);
236 void __tsan_release(ThreadState *thr, void *addr) {
237 ReleaseStore(thr, 0, (uptr)addr);
240 void __tsan_release_merge(ThreadState *thr, void *addr) {
241 Release(thr, 0, (uptr)addr);
244 void __tsan_finalizer_goroutine(ThreadState *thr) {
245 AcquireGlobal(thr, 0);
248 void __tsan_mutex_before_lock(ThreadState *thr, uptr addr, uptr write) {
250 MutexPreLock(thr, 0, addr);
252 MutexPreReadLock(thr, 0, addr);
255 void __tsan_mutex_after_lock(ThreadState *thr, uptr addr, uptr write) {
257 MutexPostLock(thr, 0, addr);
259 MutexPostReadLock(thr, 0, addr);
262 void __tsan_mutex_before_unlock(ThreadState *thr, uptr addr, uptr write) {
264 MutexUnlock(thr, 0, addr);
266 MutexReadUnlock(thr, 0, addr);
269 void __tsan_go_ignore_sync_begin(ThreadState *thr) {
270 ThreadIgnoreSyncBegin(thr, 0);
273 void __tsan_go_ignore_sync_end(ThreadState *thr) {
274 ThreadIgnoreSyncEnd(thr, 0);
277 void __tsan_report_count(u64 *pn) {
278 Lock lock(&ctx->report_mtx);
279 *pn = ctx->nreported;
283 } // namespace __tsan