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 void *internal_start_thread(void(*func)(void*), void *arg) {
35 void internal_join_thread(void *th) {
38 ReportLocation *SymbolizeData(uptr addr) {
42 void *internal_alloc(MBlockType typ, uptr sz) {
43 return InternalAlloc(sz);
46 void internal_free(void *p) {
50 struct SymbolizeContext {
60 static void (*symbolize_cb)(SymbolizeContext *ctx);
62 SymbolizedStack *SymbolizeCode(uptr addr) {
63 SymbolizedStack *s = SymbolizedStack::New(addr);
65 internal_memset(&ctx, 0, sizeof(ctx));
69 AddressInfo &info = s->info;
70 info.module_offset = ctx.off;
71 info.function = internal_strdup(ctx.func ? ctx.func : "??");
72 info.file = internal_strdup(ctx.file ? ctx.file : "-");
81 static ThreadState *main_thr;
84 static ThreadState *AllocGoroutine() {
85 ThreadState *thr = (ThreadState*)internal_alloc(MBlockThreadContex,
87 internal_memset(thr, 0, sizeof(*thr));
91 void __tsan_init(ThreadState **thrp, void (*cb)(SymbolizeContext *cb)) {
93 ThreadState *thr = AllocGoroutine();
94 main_thr = *thrp = thr;
100 // FIXME: Not necessary thread 0.
101 ThreadState *thr = main_thr;
102 int res = Finalize(thr);
106 void __tsan_map_shadow(uptr addr, uptr size) {
107 MapShadow(addr, size);
110 void __tsan_read(ThreadState *thr, void *addr, void *pc) {
111 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
114 void __tsan_read_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
116 FuncEntry(thr, callpc);
117 MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
122 void __tsan_write(ThreadState *thr, void *addr, void *pc) {
123 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
126 void __tsan_write_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
128 FuncEntry(thr, callpc);
129 MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
134 void __tsan_read_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
135 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, false);
138 void __tsan_write_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
139 MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, true);
142 void __tsan_func_enter(ThreadState *thr, void *pc) {
143 FuncEntry(thr, (uptr)pc);
146 void __tsan_func_exit(ThreadState *thr) {
150 void __tsan_malloc(void *p, uptr sz) {
153 MemoryResetRange(0, 0, (uptr)p, sz);
156 void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
157 ThreadState *thr = AllocGoroutine();
159 int goid = ThreadCreate(parent, (uptr)pc, 0, true);
160 ThreadStart(thr, goid, 0);
163 void __tsan_go_end(ThreadState *thr) {
168 void __tsan_acquire(ThreadState *thr, void *addr) {
169 Acquire(thr, 0, (uptr)addr);
172 void __tsan_release(ThreadState *thr, void *addr) {
173 ReleaseStore(thr, 0, (uptr)addr);
176 void __tsan_release_merge(ThreadState *thr, void *addr) {
177 Release(thr, 0, (uptr)addr);
180 void __tsan_finalizer_goroutine(ThreadState *thr) {
181 AcquireGlobal(thr, 0);
184 void __tsan_mutex_before_lock(ThreadState *thr, uptr addr, uptr write) {
187 void __tsan_mutex_after_lock(ThreadState *thr, uptr addr, uptr write) {
189 MutexLock(thr, 0, addr);
191 MutexReadLock(thr, 0, addr);
194 void __tsan_mutex_before_unlock(ThreadState *thr, uptr addr, uptr write) {
196 MutexUnlock(thr, 0, addr);
198 MutexReadUnlock(thr, 0, addr);
201 void __tsan_go_ignore_sync_begin(ThreadState *thr) {
202 ThreadIgnoreSyncBegin(thr, 0);
205 void __tsan_go_ignore_sync_end(ThreadState *thr) {
206 ThreadIgnoreSyncEnd(thr, 0);
210 } // namespace __tsan
212 namespace __sanitizer {
214 void SymbolizerPrepareForSandboxing() {
215 // Nothing to do here for Go.
218 } // namespace __sanitizer