2012-06-04 21:55:19 +08:00
|
|
|
//===-- tsan_mman.cc ------------------------------------------------------===//
|
2012-05-10 21:48:04 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of ThreadSanitizer (TSan), a race detector.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2012-06-07 19:54:08 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2012-08-15 23:35:15 +08:00
|
|
|
#include "sanitizer_common/sanitizer_placement_new.h"
|
2012-05-10 21:48:04 +08:00
|
|
|
#include "tsan_mman.h"
|
|
|
|
#include "tsan_rtl.h"
|
|
|
|
#include "tsan_report.h"
|
|
|
|
#include "tsan_flags.h"
|
|
|
|
|
2012-09-24 21:19:47 +08:00
|
|
|
// May be overriden by front-end.
|
|
|
|
extern "C" void WEAK __tsan_malloc_hook(void *ptr, uptr size) {
|
|
|
|
(void)ptr;
|
|
|
|
(void)size;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void WEAK __tsan_free_hook(void *ptr) {
|
|
|
|
(void)ptr;
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
namespace __tsan {
|
|
|
|
|
2012-08-30 21:02:30 +08:00
|
|
|
static char allocator_placeholder[sizeof(Allocator)] ALIGNED(64);
|
|
|
|
Allocator *allocator() {
|
2012-08-15 23:35:15 +08:00
|
|
|
return reinterpret_cast<Allocator*>(&allocator_placeholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeAllocator() {
|
|
|
|
allocator()->Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlloctorThreadFinish(ThreadState *thr) {
|
|
|
|
allocator()->SwallowCache(&thr->alloc_cache);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
|
|
|
|
if (!thr->in_signal_handler || !flags()->report_signal_unsafe)
|
|
|
|
return;
|
2012-10-05 23:51:32 +08:00
|
|
|
Context *ctx = CTX();
|
2012-05-10 21:48:04 +08:00
|
|
|
StackTrace stack;
|
|
|
|
stack.ObtainCurrent(thr, pc);
|
2013-01-14 18:00:03 +08:00
|
|
|
Lock l(&ctx->thread_mtx);
|
2012-05-10 21:48:04 +08:00
|
|
|
ScopedReport rep(ReportTypeSignalUnsafe);
|
2012-10-05 23:51:32 +08:00
|
|
|
if (!IsFiredSuppression(ctx, rep, stack)) {
|
|
|
|
rep.AddStack(&stack);
|
|
|
|
OutputReport(ctx, rep, rep.GetReport()->stacks[0]);
|
|
|
|
}
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-08-15 23:35:15 +08:00
|
|
|
void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align) {
|
2012-05-10 21:48:04 +08:00
|
|
|
CHECK_GT(thr->in_rtl, 0);
|
2012-08-15 23:35:15 +08:00
|
|
|
void *p = allocator()->Allocate(&thr->alloc_cache, sz, align);
|
|
|
|
if (p == 0)
|
2012-05-21 14:46:27 +08:00
|
|
|
return 0;
|
2012-12-06 20:16:15 +08:00
|
|
|
MBlock *b = new(allocator()->GetMetaData(p)) MBlock;
|
2012-05-10 21:48:04 +08:00
|
|
|
b->size = sz;
|
2012-12-06 20:16:15 +08:00
|
|
|
b->head = 0;
|
2012-08-30 21:02:30 +08:00
|
|
|
b->alloc_tid = thr->unique_id;
|
2012-09-01 01:27:49 +08:00
|
|
|
b->alloc_stack_id = CurrentStackId(thr, pc);
|
2012-05-10 21:48:04 +08:00
|
|
|
if (CTX() && CTX()->initialized) {
|
2012-08-16 01:27:20 +08:00
|
|
|
MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
2012-06-06 21:11:29 +08:00
|
|
|
DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
|
2012-05-10 21:48:04 +08:00
|
|
|
SignalUnsafeCall(thr, pc);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void user_free(ThreadState *thr, uptr pc, void *p) {
|
|
|
|
CHECK_GT(thr->in_rtl, 0);
|
|
|
|
CHECK_NE(p, (void*)0);
|
|
|
|
DPrintf("#%d: free(%p)\n", thr->tid, p);
|
2012-08-15 23:35:15 +08:00
|
|
|
MBlock *b = (MBlock*)allocator()->GetMetaData(p);
|
2012-08-16 01:27:20 +08:00
|
|
|
if (b->head) {
|
|
|
|
Lock l(&b->mtx);
|
|
|
|
for (SyncVar *s = b->head; s;) {
|
|
|
|
SyncVar *res = s;
|
|
|
|
s = s->next;
|
|
|
|
StatInc(thr, StatSyncDestroyed);
|
|
|
|
res->mtx.Lock();
|
|
|
|
res->mtx.Unlock();
|
|
|
|
DestroyAndFree(res);
|
|
|
|
}
|
|
|
|
b->head = 0;
|
|
|
|
}
|
2012-05-10 21:48:04 +08:00
|
|
|
if (CTX() && CTX()->initialized && thr->in_rtl == 1) {
|
|
|
|
MemoryRangeFreed(thr, pc, (uptr)p, b->size);
|
|
|
|
}
|
2012-12-06 20:16:15 +08:00
|
|
|
b->~MBlock();
|
2012-08-15 23:35:15 +08:00
|
|
|
allocator()->Deallocate(&thr->alloc_cache, p);
|
2012-05-10 21:48:04 +08:00
|
|
|
SignalUnsafeCall(thr, pc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
|
|
|
|
CHECK_GT(thr->in_rtl, 0);
|
|
|
|
void *p2 = 0;
|
|
|
|
// FIXME: Handle "shrinking" more efficiently,
|
|
|
|
// it seems that some software actually does this.
|
|
|
|
if (sz) {
|
|
|
|
p2 = user_alloc(thr, pc, sz);
|
2012-05-21 14:46:27 +08:00
|
|
|
if (p2 == 0)
|
|
|
|
return 0;
|
2012-05-10 21:48:04 +08:00
|
|
|
if (p) {
|
|
|
|
MBlock *b = user_mblock(thr, p);
|
|
|
|
internal_memcpy(p2, p, min(b->size, sz));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p) {
|
|
|
|
user_free(thr, pc, p);
|
|
|
|
}
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBlock *user_mblock(ThreadState *thr, void *p) {
|
|
|
|
CHECK_NE(p, (void*)0);
|
2012-12-25 15:45:40 +08:00
|
|
|
Allocator *a = allocator();
|
2013-01-09 19:54:52 +08:00
|
|
|
void *b = a->GetBlockBegin(p);
|
|
|
|
CHECK_NE(b, 0);
|
|
|
|
return (MBlock*)a->GetMetaData(b);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 21:19:47 +08:00
|
|
|
void invoke_malloc_hook(void *ptr, uptr size) {
|
|
|
|
Context *ctx = CTX();
|
|
|
|
ThreadState *thr = cur_thread();
|
|
|
|
if (ctx == 0 || !ctx->initialized || thr->in_rtl)
|
|
|
|
return;
|
|
|
|
__tsan_malloc_hook(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void invoke_free_hook(void *ptr) {
|
|
|
|
Context *ctx = CTX();
|
|
|
|
ThreadState *thr = cur_thread();
|
|
|
|
if (ctx == 0 || !ctx->initialized || thr->in_rtl)
|
|
|
|
return;
|
|
|
|
__tsan_free_hook(ptr);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
void *internal_alloc(MBlockType typ, uptr sz) {
|
|
|
|
ThreadState *thr = cur_thread();
|
|
|
|
CHECK_GT(thr->in_rtl, 0);
|
2012-06-22 19:08:55 +08:00
|
|
|
if (thr->nomalloc) {
|
|
|
|
thr->nomalloc = 0; // CHECK calls internal_malloc().
|
|
|
|
CHECK(0);
|
|
|
|
}
|
2012-06-07 19:54:08 +08:00
|
|
|
return InternalAlloc(sz);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void internal_free(void *p) {
|
|
|
|
ThreadState *thr = cur_thread();
|
|
|
|
CHECK_GT(thr->in_rtl, 0);
|
2012-06-22 19:08:55 +08:00
|
|
|
if (thr->nomalloc) {
|
|
|
|
thr->nomalloc = 0; // CHECK calls internal_malloc().
|
|
|
|
CHECK(0);
|
|
|
|
}
|
2012-06-07 19:54:08 +08:00
|
|
|
InternalFree(p);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __tsan
|