2012-12-10 22:19:15 +08:00
|
|
|
//===-- asan_fake_stack.cc ------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// FakeStack is used to detect use-after-return bugs.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-01 08:22:21 +08:00
|
|
|
|
2012-12-10 22:19:15 +08:00
|
|
|
#include "asan_allocator.h"
|
2013-03-28 23:42:43 +08:00
|
|
|
#include "asan_poisoning.h"
|
2012-12-10 22:19:15 +08:00
|
|
|
#include "asan_thread.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2013-09-13 13:57:58 +08:00
|
|
|
static const u64 kMagic1 = kAsanStackAfterReturnMagic;
|
|
|
|
static const u64 kMagic2 = (kMagic1 << 8) | kMagic1;
|
|
|
|
static const u64 kMagic4 = (kMagic2 << 16) | kMagic2;
|
|
|
|
static const u64 kMagic8 = (kMagic4 << 32) | kMagic4;
|
|
|
|
|
2015-05-28 15:49:05 +08:00
|
|
|
static const u64 kAllocaRedzoneSize = 32UL;
|
|
|
|
static const u64 kAllocaRedzoneMask = 31UL;
|
|
|
|
|
2013-09-13 13:57:58 +08:00
|
|
|
// For small size classes inline PoisonShadow for better performance.
|
|
|
|
ALWAYS_INLINE void SetShadow(uptr ptr, uptr size, uptr class_id, u64 magic) {
|
|
|
|
u64 *shadow = reinterpret_cast<u64*>(MemToShadow(ptr));
|
2017-11-17 06:02:58 +08:00
|
|
|
if (SHADOW_SCALE == 3 && class_id <= 6) {
|
|
|
|
// This code expects SHADOW_SCALE=3.
|
2016-06-21 22:53:16 +08:00
|
|
|
for (uptr i = 0; i < (((uptr)1) << class_id); i++) {
|
2013-09-13 13:57:58 +08:00
|
|
|
shadow[i] = magic;
|
2015-10-01 10:15:40 +08:00
|
|
|
// Make sure this does not become memset.
|
|
|
|
SanitizerBreakOptimization(nullptr);
|
2014-11-22 05:25:09 +08:00
|
|
|
}
|
2013-09-13 13:57:58 +08:00
|
|
|
} else {
|
|
|
|
// The size class is too big, it's cheaper to poison only size bytes.
|
2013-09-13 15:20:35 +08:00
|
|
|
PoisonShadow(ptr, size, static_cast<u8>(magic));
|
2013-09-13 13:57:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-27 19:37:23 +08:00
|
|
|
FakeStack *FakeStack::Create(uptr stack_size_log) {
|
|
|
|
static uptr kMinStackSizeLog = 16;
|
|
|
|
static uptr kMaxStackSizeLog = FIRST_32_SECOND_64(24, 28);
|
|
|
|
if (stack_size_log < kMinStackSizeLog)
|
|
|
|
stack_size_log = kMinStackSizeLog;
|
|
|
|
if (stack_size_log > kMaxStackSizeLog)
|
|
|
|
stack_size_log = kMaxStackSizeLog;
|
2013-12-13 23:03:49 +08:00
|
|
|
uptr size = RequiredSize(stack_size_log);
|
2013-09-27 19:37:23 +08:00
|
|
|
FakeStack *res = reinterpret_cast<FakeStack *>(
|
2013-12-13 23:03:49 +08:00
|
|
|
flags()->uar_noreserve ? MmapNoReserveOrDie(size, "FakeStack")
|
|
|
|
: MmapOrDie(size, "FakeStack"));
|
2013-09-27 19:37:23 +08:00
|
|
|
res->stack_size_log_ = stack_size_log;
|
2013-12-05 20:04:51 +08:00
|
|
|
u8 *p = reinterpret_cast<u8 *>(res);
|
2013-12-13 23:03:49 +08:00
|
|
|
VReport(1, "T%d: FakeStack created: %p -- %p stack_size_log: %zd; "
|
2013-12-16 16:42:08 +08:00
|
|
|
"mmapped %zdK, noreserve=%d \n",
|
2013-12-05 20:04:51 +08:00
|
|
|
GetCurrentTidOrInvalid(), p,
|
2013-12-13 23:03:49 +08:00
|
|
|
p + FakeStack::RequiredSize(stack_size_log), stack_size_log,
|
2013-12-16 16:42:08 +08:00
|
|
|
size >> 10, flags()->uar_noreserve);
|
2013-09-27 19:37:23 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2013-12-11 21:54:01 +08:00
|
|
|
void FakeStack::Destroy(int tid) {
|
2013-09-27 19:37:23 +08:00
|
|
|
PoisonAll(0);
|
2015-01-20 21:21:20 +08:00
|
|
|
if (Verbosity() >= 2) {
|
2013-12-11 21:54:01 +08:00
|
|
|
InternalScopedString str(kNumberOfSizeClasses * 50);
|
|
|
|
for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++)
|
|
|
|
str.append("%zd: %zd/%zd; ", class_id, hint_position_[class_id],
|
|
|
|
NumberOfFrames(stack_size_log(), class_id));
|
|
|
|
Report("T%d: FakeStack destroyed: %s\n", tid, str.data());
|
|
|
|
}
|
2013-12-11 22:26:41 +08:00
|
|
|
uptr size = RequiredSize(stack_size_log_);
|
|
|
|
FlushUnneededASanShadowMemory(reinterpret_cast<uptr>(this), size);
|
|
|
|
UnmapOrDie(this, size);
|
2013-09-27 19:37:23 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
void FakeStack::PoisonAll(u8 magic) {
|
|
|
|
PoisonShadow(reinterpret_cast<uptr>(this), RequiredSize(stack_size_log()),
|
|
|
|
magic);
|
2012-12-10 22:19:15 +08:00
|
|
|
}
|
|
|
|
|
2015-08-13 02:09:44 +08:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2013-09-12 22:41:10 +08:00
|
|
|
ALWAYS_INLINE USED
|
2015-08-13 02:09:44 +08:00
|
|
|
#endif
|
2013-09-12 15:11:58 +08:00
|
|
|
FakeFrame *FakeStack::Allocate(uptr stack_size_log, uptr class_id,
|
|
|
|
uptr real_stack) {
|
|
|
|
CHECK_LT(class_id, kNumberOfSizeClasses);
|
2013-09-12 21:25:29 +08:00
|
|
|
if (needs_gc_)
|
|
|
|
GC(real_stack);
|
2013-09-12 15:11:58 +08:00
|
|
|
uptr &hint_position = hint_position_[class_id];
|
|
|
|
const int num_iter = NumberOfFrames(stack_size_log, class_id);
|
|
|
|
u8 *flags = GetFlags(stack_size_log, class_id);
|
|
|
|
for (int i = 0; i < num_iter; i++) {
|
|
|
|
uptr pos = ModuloNumberOfFrames(stack_size_log, class_id, hint_position++);
|
2013-09-13 15:20:35 +08:00
|
|
|
// This part is tricky. On one hand, checking and setting flags[pos]
|
|
|
|
// should be atomic to ensure async-signal safety. But on the other hand,
|
|
|
|
// if the signal arrives between checking and setting flags[pos], the
|
|
|
|
// signal handler's fake stack will start from a different hint_position
|
|
|
|
// and so will not touch this particular byte. So, it is safe to do this
|
2016-08-06 02:49:08 +08:00
|
|
|
// with regular non-atomic load and store (at least I was not able to make
|
2013-09-13 15:20:35 +08:00
|
|
|
// this code crash).
|
2013-09-12 15:11:58 +08:00
|
|
|
if (flags[pos]) continue;
|
2013-09-13 15:20:35 +08:00
|
|
|
flags[pos] = 1;
|
|
|
|
FakeFrame *res = reinterpret_cast<FakeFrame *>(
|
|
|
|
GetFrame(stack_size_log, class_id, pos));
|
|
|
|
res->real_stack = real_stack;
|
2013-09-17 15:42:54 +08:00
|
|
|
*SavedFlagPtr(reinterpret_cast<uptr>(res), class_id) = &flags[pos];
|
2013-09-13 15:20:35 +08:00
|
|
|
return res;
|
2012-12-10 22:19:15 +08:00
|
|
|
}
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr; // We are out of fake stack.
|
2012-12-10 22:19:15 +08:00
|
|
|
}
|
|
|
|
|
2014-02-06 14:56:22 +08:00
|
|
|
uptr FakeStack::AddrIsInFakeStack(uptr ptr, uptr *frame_beg, uptr *frame_end) {
|
2013-09-12 15:11:58 +08:00
|
|
|
uptr stack_size_log = this->stack_size_log();
|
|
|
|
uptr beg = reinterpret_cast<uptr>(GetFrame(stack_size_log, 0, 0));
|
|
|
|
uptr end = reinterpret_cast<uptr>(this) + RequiredSize(stack_size_log);
|
|
|
|
if (ptr < beg || ptr >= end) return 0;
|
|
|
|
uptr class_id = (ptr - beg) >> stack_size_log;
|
|
|
|
uptr base = beg + (class_id << stack_size_log);
|
|
|
|
CHECK_LE(base, ptr);
|
2016-06-21 22:53:16 +08:00
|
|
|
CHECK_LT(ptr, base + (((uptr)1) << stack_size_log));
|
2013-09-12 15:11:58 +08:00
|
|
|
uptr pos = (ptr - base) >> (kMinStackFrameSizeLog + class_id);
|
2014-02-06 14:56:22 +08:00
|
|
|
uptr res = base + pos * BytesInSizeClass(class_id);
|
|
|
|
*frame_end = res + BytesInSizeClass(class_id);
|
|
|
|
*frame_beg = res + sizeof(FakeFrame);
|
|
|
|
return res;
|
2012-12-10 22:19:15 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 21:25:29 +08:00
|
|
|
void FakeStack::HandleNoReturn() {
|
|
|
|
needs_gc_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When throw, longjmp or some such happens we don't call OnFree() and
|
|
|
|
// as the result may leak one or more fake frames, but the good news is that
|
|
|
|
// we are notified about all such events by HandleNoReturn().
|
|
|
|
// If we recently had such no-return event we need to collect garbage frames.
|
|
|
|
// We do it based on their 'real_stack' values -- everything that is lower
|
|
|
|
// than the current real_stack is garbage.
|
|
|
|
NOINLINE void FakeStack::GC(uptr real_stack) {
|
|
|
|
uptr collected = 0;
|
|
|
|
for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++) {
|
|
|
|
u8 *flags = GetFlags(stack_size_log(), class_id);
|
|
|
|
for (uptr i = 0, n = NumberOfFrames(stack_size_log(), class_id); i < n;
|
|
|
|
i++) {
|
|
|
|
if (flags[i] == 0) continue; // not allocated.
|
|
|
|
FakeFrame *ff = reinterpret_cast<FakeFrame *>(
|
|
|
|
GetFrame(stack_size_log(), class_id, i));
|
|
|
|
if (ff->real_stack < real_stack) {
|
|
|
|
flags[i] = 0;
|
|
|
|
collected++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
needs_gc_ = false;
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:04:50 +08:00
|
|
|
void FakeStack::ForEachFakeFrame(RangeIteratorCallback callback, void *arg) {
|
|
|
|
for (uptr class_id = 0; class_id < kNumberOfSizeClasses; class_id++) {
|
|
|
|
u8 *flags = GetFlags(stack_size_log(), class_id);
|
|
|
|
for (uptr i = 0, n = NumberOfFrames(stack_size_log(), class_id); i < n;
|
|
|
|
i++) {
|
|
|
|
if (flags[i] == 0) continue; // not allocated.
|
|
|
|
FakeFrame *ff = reinterpret_cast<FakeFrame *>(
|
|
|
|
GetFrame(stack_size_log(), class_id, i));
|
|
|
|
uptr begin = reinterpret_cast<uptr>(ff);
|
|
|
|
callback(begin, begin + FakeStack::BytesInSizeClass(class_id), arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-09 08:21:45 +08:00
|
|
|
#if (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
|
2013-09-13 14:32:26 +08:00
|
|
|
static THREADLOCAL FakeStack *fake_stack_tls;
|
|
|
|
|
|
|
|
FakeStack *GetTLSFakeStack() {
|
|
|
|
return fake_stack_tls;
|
|
|
|
}
|
|
|
|
void SetTLSFakeStack(FakeStack *fs) {
|
|
|
|
fake_stack_tls = fs;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
FakeStack *GetTLSFakeStack() { return 0; }
|
|
|
|
void SetTLSFakeStack(FakeStack *fs) { }
|
2017-08-09 08:21:45 +08:00
|
|
|
#endif // (SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_FUCHSIA
|
2013-09-13 14:32:26 +08:00
|
|
|
|
2013-09-13 14:04:18 +08:00
|
|
|
static FakeStack *GetFakeStack() {
|
2013-03-20 17:23:28 +08:00
|
|
|
AsanThread *t = GetCurrentThread();
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!t) return nullptr;
|
2013-09-13 14:04:18 +08:00
|
|
|
return t->fake_stack();
|
|
|
|
}
|
|
|
|
|
|
|
|
static FakeStack *GetFakeStackFast() {
|
2013-09-13 14:32:26 +08:00
|
|
|
if (FakeStack *fs = GetTLSFakeStack())
|
|
|
|
return fs;
|
2013-09-18 18:35:12 +08:00
|
|
|
if (!__asan_option_detect_stack_use_after_return)
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
2013-09-13 14:04:18 +08:00
|
|
|
return GetFakeStack();
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:53:19 +08:00
|
|
|
ALWAYS_INLINE uptr OnMalloc(uptr class_id, uptr size) {
|
2013-09-13 14:04:18 +08:00
|
|
|
FakeStack *fs = GetFakeStackFast();
|
2014-12-12 05:53:19 +08:00
|
|
|
if (!fs) return 0;
|
|
|
|
uptr local_stack;
|
|
|
|
uptr real_stack = reinterpret_cast<uptr>(&local_stack);
|
2013-09-12 15:11:58 +08:00
|
|
|
FakeFrame *ff = fs->Allocate(fs->stack_size_log(), class_id, real_stack);
|
2014-12-12 05:53:19 +08:00
|
|
|
if (!ff) return 0; // Out of fake stack.
|
2013-09-12 15:11:58 +08:00
|
|
|
uptr ptr = reinterpret_cast<uptr>(ff);
|
2013-09-13 13:57:58 +08:00
|
|
|
SetShadow(ptr, size, class_id, 0);
|
2012-12-10 22:19:15 +08:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:53:19 +08:00
|
|
|
ALWAYS_INLINE void OnFree(uptr ptr, uptr class_id, uptr size) {
|
2013-09-17 15:42:54 +08:00
|
|
|
FakeStack::Deallocate(ptr, class_id);
|
2013-09-13 13:57:58 +08:00
|
|
|
SetShadow(ptr, size, class_id, kMagic8);
|
2012-12-10 22:19:15 +08:00
|
|
|
}
|
2013-09-10 21:16:26 +08:00
|
|
|
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __asan
|
2013-09-10 21:16:26 +08:00
|
|
|
|
|
|
|
// ---------------------- Interface ---------------- {{{1
|
2014-02-06 14:56:22 +08:00
|
|
|
using namespace __asan;
|
2013-09-10 21:16:26 +08:00
|
|
|
#define DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(class_id) \
|
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr \
|
2014-12-12 05:53:19 +08:00
|
|
|
__asan_stack_malloc_##class_id(uptr size) { \
|
|
|
|
return OnMalloc(class_id, size); \
|
2013-09-10 21:16:26 +08:00
|
|
|
} \
|
|
|
|
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __asan_stack_free_##class_id( \
|
2014-12-12 05:53:19 +08:00
|
|
|
uptr ptr, uptr size) { \
|
|
|
|
OnFree(ptr, class_id, size); \
|
2013-09-10 21:16:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(0)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(1)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(2)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(3)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(4)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(5)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(6)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(7)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(8)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(9)
|
|
|
|
DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID(10)
|
2014-02-06 14:56:22 +08:00
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void *__asan_get_current_fake_stack() { return GetFakeStackFast(); }
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
|
|
|
|
void **end) {
|
|
|
|
FakeStack *fs = reinterpret_cast<FakeStack*>(fake_stack);
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!fs) return nullptr;
|
2014-02-06 14:56:22 +08:00
|
|
|
uptr frame_beg, frame_end;
|
|
|
|
FakeFrame *frame = reinterpret_cast<FakeFrame *>(fs->AddrIsInFakeStack(
|
|
|
|
reinterpret_cast<uptr>(addr), &frame_beg, &frame_end));
|
2015-10-01 08:22:21 +08:00
|
|
|
if (!frame) return nullptr;
|
2014-02-06 14:56:22 +08:00
|
|
|
if (frame->magic != kCurrentStackFrameMagic)
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
2014-02-06 14:56:22 +08:00
|
|
|
if (beg) *beg = reinterpret_cast<void*>(frame_beg);
|
|
|
|
if (end) *end = reinterpret_cast<void*>(frame_end);
|
|
|
|
return reinterpret_cast<void*>(frame->real_stack);
|
|
|
|
}
|
2015-05-28 15:49:05 +08:00
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __asan_alloca_poison(uptr addr, uptr size) {
|
|
|
|
uptr LeftRedzoneAddr = addr - kAllocaRedzoneSize;
|
|
|
|
uptr PartialRzAddr = addr + size;
|
|
|
|
uptr RightRzAddr = (PartialRzAddr + kAllocaRedzoneMask) & ~kAllocaRedzoneMask;
|
|
|
|
uptr PartialRzAligned = PartialRzAddr & ~(SHADOW_GRANULARITY - 1);
|
|
|
|
FastPoisonShadow(LeftRedzoneAddr, kAllocaRedzoneSize, kAsanAllocaLeftMagic);
|
|
|
|
FastPoisonShadowPartialRightRedzone(
|
|
|
|
PartialRzAligned, PartialRzAddr % SHADOW_GRANULARITY,
|
|
|
|
RightRzAddr - PartialRzAligned, kAsanAllocaRightMagic);
|
|
|
|
FastPoisonShadow(RightRzAddr, kAllocaRedzoneSize, kAsanAllocaRightMagic);
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __asan_allocas_unpoison(uptr top, uptr bottom) {
|
|
|
|
if ((!top) || (top > bottom)) return;
|
|
|
|
REAL(memset)(reinterpret_cast<void*>(MemToShadow(top)), 0,
|
|
|
|
(bottom - top) / SHADOW_GRANULARITY);
|
|
|
|
}
|
2015-10-01 08:22:21 +08:00
|
|
|
} // extern "C"
|