1 //===-- tsan_go.cc --------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // ThreadSanitizer runtime for Go language.
12 //===----------------------------------------------------------------------===//
15 #include "tsan_symbolize.h"
16 #include "sanitizer_common/sanitizer_common.h"
21 void InitializeInterceptors() {
24 void InitializeDynamicAnnotations() {
27 bool IsExpectedReport(uptr addr, uptr size) {
31 ReportLocation *SymbolizeData(uptr addr) {
35 void *internal_alloc(MBlockType typ, uptr sz) {
36 return InternalAlloc(sz);
39 void internal_free(void *p) {
43 struct SymbolizeContext {
53 static void (*symbolize_cb)(SymbolizeContext *ctx);
55 SymbolizedStack *SymbolizeCode(uptr addr) {
56 SymbolizedStack *s = SymbolizedStack::New(addr);
58 internal_memset(&ctx, 0, sizeof(ctx));
62 AddressInfo &info = s->info;
63 info.module_offset = ctx.off;
64 info.function = internal_strdup(ctx.func ? ctx.func : "??");
65 info.file = internal_strdup(ctx.file ? ctx.file : "-");
74 static ThreadState *main_thr;
77 static ThreadState *AllocGoroutine() {
78 ThreadState *thr = (ThreadState*)internal_alloc(MBlockThreadContex,
80 internal_memset(thr, 0, sizeof(*thr));
84 void __tsan_init(ThreadState **thrp, void (*cb)(SymbolizeContext *cb)) {
86 ThreadState *thr = AllocGoroutine();
87 main_thr = *thrp = thr;
93 // FIXME: Not necessary thread 0.
94 ThreadState *thr = main_thr;
95 int res = Finalize(thr);
99 void __tsan_map_shadow(uptr addr, uptr size) {
100 MapShadow(addr, size);
103 void __tsan_read(ThreadState *thr, void *addr, void *pc) {
104 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
107 void __tsan_read_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
109 FuncEntry(thr, callpc);
110 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
115 void __tsan_write(ThreadState *thr, void *addr, void *pc) {
116 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
119 void __tsan_write_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
121 FuncEntry(thr, callpc);
122 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
127 void __tsan_read_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
128 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, false);
131 void __tsan_write_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
132 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, true);
135 void __tsan_func_enter(ThreadState *thr, void *pc) {
136 FuncEntry(thr, (uptr)pc);
139 void __tsan_func_exit(ThreadState *thr) {
143 void __tsan_malloc(void *p, uptr sz) {
146 MemoryResetRange(0, 0, (uptr)p, sz);
149 void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
150 ThreadState *thr = AllocGoroutine();
152 int goid = ThreadCreate(parent, (uptr)pc, 0, true);
153 ThreadStart(thr, goid, 0);
156 void __tsan_go_end(ThreadState *thr) {
161 void __tsan_acquire(ThreadState *thr, void *addr) {
162 Acquire(thr, 0, (uptr)addr);
165 void __tsan_release(ThreadState *thr, void *addr) {
166 ReleaseStore(thr, 0, (uptr)addr);
169 void __tsan_release_merge(ThreadState *thr, void *addr) {
170 Release(thr, 0, (uptr)addr);
173 void __tsan_finalizer_goroutine(ThreadState *thr) {
174 AcquireGlobal(thr, 0);
177 void __tsan_mutex_before_lock(ThreadState *thr, uptr addr, uptr write) {
180 void __tsan_mutex_after_lock(ThreadState *thr, uptr addr, uptr write) {
182 MutexLock(thr, 0, addr);
184 MutexReadLock(thr, 0, addr);
187 void __tsan_mutex_before_unlock(ThreadState *thr, uptr addr, uptr write) {
189 MutexUnlock(thr, 0, addr);
191 MutexReadUnlock(thr, 0, addr);
194 void __tsan_go_ignore_sync_begin(ThreadState *thr) {
195 ThreadIgnoreSyncBegin(thr, 0);
198 void __tsan_go_ignore_sync_end(ThreadState *thr) {
199 ThreadIgnoreSyncEnd(thr, 0);
203 } // namespace __tsan
205 namespace __sanitizer {
207 void SymbolizerPrepareForSandboxing() {
208 // Nothing to do here for Go.
211 } // namespace __sanitizer