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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2014-07-08 01:39:31 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_interface.h"
|
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.
|
2014-07-08 01:39:31 +08:00
|
|
|
extern "C" void WEAK __sanitizer_malloc_hook(void *ptr, uptr size) {
|
|
|
|
(void)ptr;
|
|
|
|
(void)size;
|
|
|
|
}
|
2012-09-24 21:19:47 +08:00
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
extern "C" void WEAK __sanitizer_free_hook(void *ptr) {
|
|
|
|
(void)ptr;
|
|
|
|
}
|
2012-09-24 21:19:47 +08:00
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
namespace __tsan {
|
|
|
|
|
2013-03-18 18:32:21 +08:00
|
|
|
struct MapUnmapCallback {
|
|
|
|
void OnMap(uptr p, uptr size) const { }
|
|
|
|
void OnUnmap(uptr p, uptr size) const {
|
|
|
|
// We are about to unmap a chunk of user memory.
|
|
|
|
// Mark the corresponding shadow memory as not needed.
|
2013-03-18 23:49:07 +08:00
|
|
|
DontNeedShadowFor(p, size);
|
2013-03-18 18:32:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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() {
|
2014-12-13 04:07:35 +08:00
|
|
|
allocator()->Init(common_flags()->allocator_may_return_null);
|
2012-08-15 23:35:15 +08:00
|
|
|
}
|
|
|
|
|
2013-01-24 17:08:03 +08:00
|
|
|
void AllocatorThreadStart(ThreadState *thr) {
|
|
|
|
allocator()->InitCache(&thr->alloc_cache);
|
2013-05-29 17:15:39 +08:00
|
|
|
internal_allocator()->InitCache(&thr->internal_alloc_cache);
|
2013-01-24 17:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AllocatorThreadFinish(ThreadState *thr) {
|
|
|
|
allocator()->DestroyCache(&thr->alloc_cache);
|
2013-05-29 17:15:39 +08:00
|
|
|
internal_allocator()->DestroyCache(&thr->internal_alloc_cache);
|
2013-01-24 17:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AllocatorPrintStats() {
|
|
|
|
allocator()->PrintStats();
|
2012-08-15 23:35:15 +08:00
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
static void SignalUnsafeCall(ThreadState *thr, uptr pc) {
|
2014-09-02 20:27:45 +08:00
|
|
|
if (atomic_load(&thr->in_signal_handler, memory_order_relaxed) == 0 ||
|
|
|
|
!flags()->report_signal_unsafe)
|
2012-05-10 21:48:04 +08:00
|
|
|
return;
|
2014-11-04 06:23:44 +08:00
|
|
|
VarSizeStackTrace stack;
|
|
|
|
ObtainCurrentStack(thr, pc, &stack);
|
2013-03-15 21:48:44 +08:00
|
|
|
ThreadRegistryLock l(ctx->thread_registry);
|
2012-05-10 21:48:04 +08:00
|
|
|
ScopedReport rep(ReportTypeSignalUnsafe);
|
2012-10-05 23:51:32 +08:00
|
|
|
if (!IsFiredSuppression(ctx, rep, stack)) {
|
2014-11-04 06:23:44 +08:00
|
|
|
rep.AddStack(stack, true);
|
2014-05-29 21:50:54 +08:00
|
|
|
OutputReport(thr, rep);
|
2012-10-05 23:51:32 +08:00
|
|
|
}
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-10-15 16:56:43 +08:00
|
|
|
void *user_alloc(ThreadState *thr, uptr pc, uptr sz, uptr align, bool signal) {
|
2013-03-23 01:06:22 +08:00
|
|
|
if ((sz >= (1ull << 40)) || (align >= (1ull << 40)))
|
2014-12-13 04:07:35 +08:00
|
|
|
return allocator()->ReturnNullOrDie();
|
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;
|
2014-05-29 21:50:54 +08:00
|
|
|
if (ctx && ctx->initialized)
|
|
|
|
OnUserAlloc(thr, pc, (uptr)p, sz, true);
|
2014-10-15 16:56:43 +08:00
|
|
|
if (signal)
|
|
|
|
SignalUnsafeCall(thr, pc);
|
2012-05-10 21:48:04 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-12-13 04:07:35 +08:00
|
|
|
void *user_calloc(ThreadState *thr, uptr pc, uptr size, uptr n) {
|
|
|
|
if (CallocShouldReturnNullDueToOverflow(size, n))
|
|
|
|
return allocator()->ReturnNullOrDie();
|
|
|
|
void *p = user_alloc(thr, pc, n * size);
|
|
|
|
if (p)
|
|
|
|
internal_memset(p, 0, n * size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-10-15 16:56:43 +08:00
|
|
|
void user_free(ThreadState *thr, uptr pc, void *p, bool signal) {
|
2014-05-29 21:50:54 +08:00
|
|
|
if (ctx && ctx->initialized)
|
|
|
|
OnUserFree(thr, pc, (uptr)p, true);
|
2012-08-15 23:35:15 +08:00
|
|
|
allocator()->Deallocate(&thr->alloc_cache, p);
|
2014-10-15 16:56:43 +08:00
|
|
|
if (signal)
|
|
|
|
SignalUnsafeCall(thr, pc);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2014-05-29 21:50:54 +08:00
|
|
|
void OnUserAlloc(ThreadState *thr, uptr pc, uptr p, uptr sz, bool write) {
|
|
|
|
DPrintf("#%d: alloc(%zu) = %p\n", thr->tid, sz, p);
|
|
|
|
ctx->metamap.AllocBlock(thr, pc, p, sz);
|
|
|
|
if (write && thr->ignore_reads_and_writes == 0)
|
|
|
|
MemoryRangeImitateWrite(thr, pc, (uptr)p, sz);
|
|
|
|
else
|
|
|
|
MemoryResetRange(thr, pc, (uptr)p, sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnUserFree(ThreadState *thr, uptr pc, uptr p, bool write) {
|
|
|
|
CHECK_NE(p, (void*)0);
|
|
|
|
uptr sz = ctx->metamap.FreeBlock(thr, pc, p);
|
|
|
|
DPrintf("#%d: free(%p, %zu)\n", thr->tid, p, sz);
|
|
|
|
if (write && thr->ignore_reads_and_writes == 0)
|
|
|
|
MemoryRangeFreed(thr, pc, (uptr)p, sz);
|
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
void *user_realloc(ThreadState *thr, uptr pc, void *p, uptr sz) {
|
|
|
|
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) {
|
2014-07-02 02:01:20 +08:00
|
|
|
uptr oldsz = user_alloc_usable_size(p);
|
2014-05-29 21:50:54 +08:00
|
|
|
internal_memcpy(p2, p, min(oldsz, sz));
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
2013-03-19 03:47:36 +08:00
|
|
|
if (p)
|
2012-05-10 21:48:04 +08:00
|
|
|
user_free(thr, pc, p);
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr user_alloc_usable_size(const void *p) {
|
2013-02-25 16:43:10 +08:00
|
|
|
if (p == 0)
|
|
|
|
return 0;
|
2014-05-29 21:50:54 +08:00
|
|
|
MBlock *b = ctx->metamap.GetBlock((uptr)p);
|
|
|
|
return b ? b->siz : 0;
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
2012-09-24 21:19:47 +08:00
|
|
|
void invoke_malloc_hook(void *ptr, uptr size) {
|
|
|
|
ThreadState *thr = cur_thread();
|
2013-12-24 20:55:56 +08:00
|
|
|
if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
|
2012-09-24 21:19:47 +08:00
|
|
|
return;
|
2014-07-08 01:39:31 +08:00
|
|
|
__sanitizer_malloc_hook(ptr, size);
|
2012-09-24 21:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void invoke_free_hook(void *ptr) {
|
|
|
|
ThreadState *thr = cur_thread();
|
2013-12-24 20:55:56 +08:00
|
|
|
if (ctx == 0 || !ctx->initialized || thr->ignore_interceptors)
|
2012-09-24 21:19:47 +08:00
|
|
|
return;
|
2014-07-08 01:39:31 +08:00
|
|
|
__sanitizer_free_hook(ptr);
|
2012-09-24 21:19:47 +08:00
|
|
|
}
|
|
|
|
|
2012-05-10 21:48:04 +08:00
|
|
|
void *internal_alloc(MBlockType typ, uptr sz) {
|
|
|
|
ThreadState *thr = cur_thread();
|
2012-06-22 19:08:55 +08:00
|
|
|
if (thr->nomalloc) {
|
|
|
|
thr->nomalloc = 0; // CHECK calls internal_malloc().
|
|
|
|
CHECK(0);
|
|
|
|
}
|
2013-05-29 17:15:39 +08:00
|
|
|
return InternalAlloc(sz, &thr->internal_alloc_cache);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void internal_free(void *p) {
|
|
|
|
ThreadState *thr = cur_thread();
|
2012-06-22 19:08:55 +08:00
|
|
|
if (thr->nomalloc) {
|
|
|
|
thr->nomalloc = 0; // CHECK calls internal_malloc().
|
|
|
|
CHECK(0);
|
|
|
|
}
|
2013-05-29 17:15:39 +08:00
|
|
|
InternalFree(p, &thr->internal_alloc_cache);
|
2012-05-10 21:48:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __tsan
|
2013-01-23 20:08:03 +08:00
|
|
|
|
|
|
|
using namespace __tsan;
|
|
|
|
|
|
|
|
extern "C" {
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_current_allocated_bytes() {
|
2014-05-28 23:22:12 +08:00
|
|
|
uptr stats[AllocatorStatCount];
|
2013-01-24 17:08:03 +08:00
|
|
|
allocator()->GetStats(stats);
|
2014-05-28 23:22:12 +08:00
|
|
|
return stats[AllocatorStatAllocated];
|
2013-01-23 20:08:03 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_heap_size() {
|
2014-05-28 23:22:12 +08:00
|
|
|
uptr stats[AllocatorStatCount];
|
2013-01-24 17:08:03 +08:00
|
|
|
allocator()->GetStats(stats);
|
2014-05-28 23:22:12 +08:00
|
|
|
return stats[AllocatorStatMapped];
|
2013-01-23 20:08:03 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_free_bytes() {
|
2013-01-24 22:07:19 +08:00
|
|
|
return 1;
|
2013-01-23 20:08:03 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_unmapped_bytes() {
|
2013-01-24 22:07:19 +08:00
|
|
|
return 1;
|
2013-01-23 20:08:03 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_estimated_allocated_size(uptr size) {
|
2013-01-23 20:08:03 +08:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
int __sanitizer_get_ownership(const void *p) {
|
2013-01-24 17:08:03 +08:00
|
|
|
return allocator()->GetBlockBegin(p) != 0;
|
2013-01-23 20:08:03 +08:00
|
|
|
}
|
|
|
|
|
2014-07-08 01:39:31 +08:00
|
|
|
uptr __sanitizer_get_allocated_size(const void *p) {
|
2014-07-02 02:01:20 +08:00
|
|
|
return user_alloc_usable_size(p);
|
2013-01-23 20:08:03 +08:00
|
|
|
}
|
2013-03-19 01:21:15 +08:00
|
|
|
|
|
|
|
void __tsan_on_thread_idle() {
|
|
|
|
ThreadState *thr = cur_thread();
|
|
|
|
allocator()->SwallowCache(&thr->alloc_cache);
|
2013-05-29 17:15:39 +08:00
|
|
|
internal_allocator()->SwallowCache(&thr->internal_alloc_cache);
|
2014-05-29 21:50:54 +08:00
|
|
|
ctx->metamap.OnThreadIdle(thr);
|
2013-03-19 01:21:15 +08:00
|
|
|
}
|
2013-01-23 20:08:03 +08:00
|
|
|
} // extern "C"
|