2019-08-01 22:30:49 +08:00
|
|
|
//===-- tsan_go.cpp -------------------------------------------------------===//
|
2012-07-06 00:18:28 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-07-06 00:18:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// ThreadSanitizer runtime for Go language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "tsan_rtl.h"
|
|
|
|
#include "tsan_symbolize.h"
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
namespace __tsan {
|
|
|
|
|
|
|
|
void InitializeInterceptors() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeDynamicAnnotations() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsExpectedReport(uptr addr, uptr size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *internal_alloc(MBlockType typ, uptr sz) {
|
|
|
|
return InternalAlloc(sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void internal_free(void *p) {
|
|
|
|
InternalFree(p);
|
|
|
|
}
|
|
|
|
|
2016-03-01 23:38:12 +08:00
|
|
|
// Callback into Go.
|
|
|
|
static void (*go_runtime_cb)(uptr cmd, void *ctx);
|
|
|
|
|
|
|
|
enum {
|
2016-04-27 20:30:48 +08:00
|
|
|
CallbackGetProc = 0,
|
|
|
|
CallbackSymbolizeCode = 1,
|
|
|
|
CallbackSymbolizeData = 2,
|
2016-03-01 23:38:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SymbolizeCodeContext {
|
2014-02-04 18:35:23 +08:00
|
|
|
uptr pc;
|
|
|
|
char *func;
|
|
|
|
char *file;
|
|
|
|
uptr line;
|
|
|
|
uptr off;
|
|
|
|
uptr res;
|
|
|
|
};
|
|
|
|
|
2014-12-03 03:48:40 +08:00
|
|
|
SymbolizedStack *SymbolizeCode(uptr addr) {
|
2019-09-18 17:18:04 +08:00
|
|
|
SymbolizedStack *first = SymbolizedStack::New(addr);
|
|
|
|
SymbolizedStack *s = first;
|
|
|
|
for (;;) {
|
|
|
|
SymbolizeCodeContext cbctx;
|
|
|
|
internal_memset(&cbctx, 0, sizeof(cbctx));
|
|
|
|
cbctx.pc = addr;
|
|
|
|
go_runtime_cb(CallbackSymbolizeCode, &cbctx);
|
|
|
|
if (cbctx.res == 0)
|
|
|
|
break;
|
2014-12-03 03:48:40 +08:00
|
|
|
AddressInfo &info = s->info;
|
2016-03-01 23:38:12 +08:00
|
|
|
info.module_offset = cbctx.off;
|
|
|
|
info.function = internal_strdup(cbctx.func ? cbctx.func : "??");
|
|
|
|
info.file = internal_strdup(cbctx.file ? cbctx.file : "-");
|
|
|
|
info.line = cbctx.line;
|
2014-12-03 03:48:40 +08:00
|
|
|
info.column = 0;
|
2019-09-18 17:18:04 +08:00
|
|
|
|
|
|
|
if (cbctx.pc == addr) // outermost (non-inlined) function
|
|
|
|
break;
|
|
|
|
addr = cbctx.pc;
|
|
|
|
// Allocate a stack entry for the parent of the inlined function.
|
|
|
|
SymbolizedStack *s2 = SymbolizedStack::New(addr);
|
|
|
|
s->next = s2;
|
|
|
|
s = s2;
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
2019-09-18 17:18:04 +08:00
|
|
|
return first;
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2016-03-01 23:38:12 +08:00
|
|
|
struct SymbolizeDataContext {
|
|
|
|
uptr addr;
|
|
|
|
uptr heap;
|
|
|
|
uptr start;
|
|
|
|
uptr size;
|
|
|
|
char *name;
|
|
|
|
char *file;
|
|
|
|
uptr line;
|
|
|
|
uptr res;
|
|
|
|
};
|
|
|
|
|
|
|
|
ReportLocation *SymbolizeData(uptr addr) {
|
|
|
|
SymbolizeDataContext cbctx;
|
|
|
|
internal_memset(&cbctx, 0, sizeof(cbctx));
|
|
|
|
cbctx.addr = addr;
|
|
|
|
go_runtime_cb(CallbackSymbolizeData, &cbctx);
|
|
|
|
if (!cbctx.res)
|
|
|
|
return 0;
|
|
|
|
if (cbctx.heap) {
|
|
|
|
MBlock *b = ctx->metamap.GetBlock(cbctx.start);
|
|
|
|
if (!b)
|
|
|
|
return 0;
|
|
|
|
ReportLocation *loc = ReportLocation::New(ReportLocationHeap);
|
|
|
|
loc->heap_chunk_start = cbctx.start;
|
|
|
|
loc->heap_chunk_size = b->siz;
|
|
|
|
loc->tid = b->tid;
|
|
|
|
loc->stack = SymbolizeStackId(b->stk);
|
|
|
|
return loc;
|
|
|
|
} else {
|
|
|
|
ReportLocation *loc = ReportLocation::New(ReportLocationGlobal);
|
|
|
|
loc->global.name = internal_strdup(cbctx.name ? cbctx.name : "??");
|
|
|
|
loc->global.file = internal_strdup(cbctx.file ? cbctx.file : "??");
|
|
|
|
loc->global.line = cbctx.line;
|
|
|
|
loc->global.start = cbctx.start;
|
|
|
|
loc->global.size = cbctx.size;
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
static ThreadState *main_thr;
|
2014-03-06 21:17:28 +08:00
|
|
|
static bool inited;
|
2013-01-31 15:48:43 +08:00
|
|
|
|
2016-04-27 20:30:48 +08:00
|
|
|
static Processor* get_cur_proc() {
|
|
|
|
if (UNLIKELY(!inited)) {
|
|
|
|
// Running Initialize().
|
|
|
|
// We have not yet returned the Processor to Go, so we cannot ask it back.
|
|
|
|
// Currently, Initialize() does not use the Processor, so return nullptr.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
Processor *proc;
|
|
|
|
go_runtime_cb(CallbackGetProc, &proc);
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Processor *ThreadState::proc() {
|
|
|
|
return get_cur_proc();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
static ThreadState *AllocGoroutine() {
|
2012-08-14 02:44:44 +08:00
|
|
|
ThreadState *thr = (ThreadState*)internal_alloc(MBlockThreadContex,
|
2012-07-07 04:23:59 +08:00
|
|
|
sizeof(ThreadState));
|
2012-08-14 02:44:44 +08:00
|
|
|
internal_memset(thr, 0, sizeof(*thr));
|
2013-01-31 15:48:43 +08:00
|
|
|
return thr;
|
2012-07-07 04:23:59 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:23:02 +08:00
|
|
|
void __tsan_init(ThreadState **thrp, Processor **procp,
|
|
|
|
void (*cb)(uptr cmd, void *cb)) {
|
2016-03-01 23:38:12 +08:00
|
|
|
go_runtime_cb = cb;
|
2013-01-31 15:48:43 +08:00
|
|
|
ThreadState *thr = AllocGoroutine();
|
|
|
|
main_thr = *thrp = thr;
|
2012-07-06 00:18:28 +08:00
|
|
|
Initialize(thr);
|
2016-04-27 20:30:48 +08:00
|
|
|
*procp = thr->proc1;
|
2014-03-06 21:17:28 +08:00
|
|
|
inited = true;
|
2012-07-06 00:18:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __tsan_fini() {
|
|
|
|
// FIXME: Not necessary thread 0.
|
2013-01-31 15:48:43 +08:00
|
|
|
ThreadState *thr = main_thr;
|
2012-07-06 00:18:28 +08:00
|
|
|
int res = Finalize(thr);
|
2012-10-04 21:54:49 +08:00
|
|
|
exit(res);
|
2012-07-06 00:18:28 +08:00
|
|
|
}
|
|
|
|
|
2012-11-07 00:00:16 +08:00
|
|
|
void __tsan_map_shadow(uptr addr, uptr size) {
|
|
|
|
MapShadow(addr, size);
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_read(ThreadState *thr, void *addr, void *pc) {
|
2013-02-01 17:42:06 +08:00
|
|
|
MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 18:35:23 +08:00
|
|
|
void __tsan_read_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
|
|
|
|
if (callpc != 0)
|
|
|
|
FuncEntry(thr, callpc);
|
|
|
|
MemoryRead(thr, (uptr)pc, (uptr)addr, kSizeLog1);
|
|
|
|
if (callpc != 0)
|
|
|
|
FuncExit(thr);
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_write(ThreadState *thr, void *addr, void *pc) {
|
2013-02-01 17:42:06 +08:00
|
|
|
MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 18:35:23 +08:00
|
|
|
void __tsan_write_pc(ThreadState *thr, void *addr, uptr callpc, uptr pc) {
|
|
|
|
if (callpc != 0)
|
|
|
|
FuncEntry(thr, callpc);
|
|
|
|
MemoryWrite(thr, (uptr)pc, (uptr)addr, kSizeLog1);
|
|
|
|
if (callpc != 0)
|
|
|
|
FuncExit(thr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __tsan_read_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
|
2013-06-06 22:31:15 +08:00
|
|
|
MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, false);
|
2012-11-08 21:38:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 18:35:23 +08:00
|
|
|
void __tsan_write_range(ThreadState *thr, void *addr, uptr size, uptr pc) {
|
2013-06-06 22:31:15 +08:00
|
|
|
MemoryAccessRange(thr, (uptr)pc, (uptr)addr, size, true);
|
2012-11-08 21:38:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_func_enter(ThreadState *thr, void *pc) {
|
2012-07-27 22:00:39 +08:00
|
|
|
FuncEntry(thr, (uptr)pc);
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_func_exit(ThreadState *thr) {
|
2012-07-27 22:00:39 +08:00
|
|
|
FuncExit(thr);
|
|
|
|
}
|
|
|
|
|
2016-03-01 23:38:12 +08:00
|
|
|
void __tsan_malloc(ThreadState *thr, uptr pc, uptr p, uptr sz) {
|
|
|
|
CHECK(inited);
|
|
|
|
if (thr && pc)
|
|
|
|
ctx->metamap.AllocBlock(thr, pc, p, sz);
|
2016-02-27 02:26:48 +08:00
|
|
|
MemoryResetRange(0, 0, (uptr)p, sz);
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 20:30:48 +08:00
|
|
|
void __tsan_free(uptr p, uptr sz) {
|
|
|
|
ctx->metamap.FreeRange(get_cur_proc(), p, sz);
|
2016-03-01 23:38:12 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_go_start(ThreadState *parent, ThreadState **pthr, void *pc) {
|
|
|
|
ThreadState *thr = AllocGoroutine();
|
|
|
|
*pthr = thr;
|
|
|
|
int goid = ThreadCreate(parent, (uptr)pc, 0, true);
|
2019-02-07 19:01:22 +08:00
|
|
|
ThreadStart(thr, goid, 0, ThreadType::Regular);
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_go_end(ThreadState *thr) {
|
2012-07-27 22:00:39 +08:00
|
|
|
ThreadFinish(thr);
|
2012-08-14 02:44:44 +08:00
|
|
|
internal_free(thr);
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 16:23:02 +08:00
|
|
|
void __tsan_proc_create(Processor **pproc) {
|
|
|
|
*pproc = ProcCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __tsan_proc_destroy(Processor *proc) {
|
|
|
|
ProcDestroy(proc);
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_acquire(ThreadState *thr, void *addr) {
|
2012-07-27 22:00:39 +08:00
|
|
|
Acquire(thr, 0, (uptr)addr);
|
|
|
|
}
|
|
|
|
|
2020-03-24 17:27:08 +08:00
|
|
|
void __tsan_release_acquire(ThreadState *thr, void *addr) {
|
|
|
|
ReleaseStoreAcquire(thr, 0, (uptr)addr);
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_release(ThreadState *thr, void *addr) {
|
2012-07-28 23:27:41 +08:00
|
|
|
ReleaseStore(thr, 0, (uptr)addr);
|
2012-07-27 22:00:39 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_release_merge(ThreadState *thr, void *addr) {
|
2012-07-27 22:00:39 +08:00
|
|
|
Release(thr, 0, (uptr)addr);
|
2012-07-06 00:18:28 +08:00
|
|
|
}
|
|
|
|
|
2013-01-31 15:48:43 +08:00
|
|
|
void __tsan_finalizer_goroutine(ThreadState *thr) {
|
2012-11-07 23:08:20 +08:00
|
|
|
AcquireGlobal(thr, 0);
|
2012-07-25 21:16:35 +08:00
|
|
|
}
|
|
|
|
|
2014-06-07 02:53:52 +08:00
|
|
|
void __tsan_mutex_before_lock(ThreadState *thr, uptr addr, uptr write) {
|
2017-03-26 23:27:04 +08:00
|
|
|
if (write)
|
|
|
|
MutexPreLock(thr, 0, addr);
|
|
|
|
else
|
|
|
|
MutexPreReadLock(thr, 0, addr);
|
2014-03-19 20:50:47 +08:00
|
|
|
}
|
|
|
|
|
2014-06-07 02:53:52 +08:00
|
|
|
void __tsan_mutex_after_lock(ThreadState *thr, uptr addr, uptr write) {
|
2014-03-19 20:50:47 +08:00
|
|
|
if (write)
|
2017-03-26 23:27:04 +08:00
|
|
|
MutexPostLock(thr, 0, addr);
|
2014-03-19 20:50:47 +08:00
|
|
|
else
|
2017-03-26 23:27:04 +08:00
|
|
|
MutexPostReadLock(thr, 0, addr);
|
2014-03-19 20:50:47 +08:00
|
|
|
}
|
|
|
|
|
2014-06-07 02:53:52 +08:00
|
|
|
void __tsan_mutex_before_unlock(ThreadState *thr, uptr addr, uptr write) {
|
2014-03-19 20:50:47 +08:00
|
|
|
if (write)
|
|
|
|
MutexUnlock(thr, 0, addr);
|
|
|
|
else
|
|
|
|
MutexReadUnlock(thr, 0, addr);
|
|
|
|
}
|
|
|
|
|
2014-07-18 22:54:02 +08:00
|
|
|
void __tsan_go_ignore_sync_begin(ThreadState *thr) {
|
|
|
|
ThreadIgnoreSyncBegin(thr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __tsan_go_ignore_sync_end(ThreadState *thr) {
|
|
|
|
ThreadIgnoreSyncEnd(thr, 0);
|
|
|
|
}
|
|
|
|
|
2016-10-29 05:25:37 +08:00
|
|
|
void __tsan_report_count(u64 *pn) {
|
|
|
|
Lock lock(&ctx->report_mtx);
|
|
|
|
*pn = ctx->nreported;
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:18:28 +08:00
|
|
|
} // extern "C"
|
|
|
|
} // namespace __tsan
|