2013-04-11 19:39:19 +08:00
|
|
|
//===-- asan_fake_stack.h ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2013-09-12 15:11:58 +08:00
|
|
|
// ASan-private header for asan_fake_stack.cc, implements FakeStack.
|
2013-04-11 19:39:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef ASAN_FAKE_STACK_H
|
|
|
|
#define ASAN_FAKE_STACK_H
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
|
2013-04-11 19:39:19 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
// Fake stack frame contains local variables of one function.
|
|
|
|
struct FakeFrame {
|
|
|
|
uptr magic; // Modified by the instrumented code.
|
|
|
|
uptr descr; // Modified by the instrumented code.
|
2013-04-11 20:49:38 +08:00
|
|
|
uptr pc; // Modified by the instrumented code.
|
2013-09-13 15:50:44 +08:00
|
|
|
uptr real_stack;
|
2013-04-11 19:39:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// For each thread we create a fake stack and place stack objects on this fake
|
|
|
|
// stack instead of the real stack. The fake stack is not really a stack but
|
|
|
|
// a fast malloc-like allocator so that when a function exits the fake stack
|
2013-09-12 15:11:58 +08:00
|
|
|
// is not popped but remains there for quite some time until gets used again.
|
2013-04-11 19:39:19 +08:00
|
|
|
// So, we poison the objects on the fake stack when function returns.
|
|
|
|
// It helps us find use-after-return bugs.
|
2013-09-12 15:11:58 +08:00
|
|
|
//
|
|
|
|
// The FakeStack objects is allocated by a single mmap call and has no other
|
|
|
|
// pointers. The size of the fake stack depends on the actual thread stack size
|
|
|
|
// and thus can not be a constant.
|
|
|
|
// stack_size is a power of two greater or equal to the thread's stack size;
|
|
|
|
// we store it as its logarithm (stack_size_log).
|
|
|
|
// FakeStack has kNumberOfSizeClasses (11) size classes, each size class
|
|
|
|
// is a power of two, starting from 64 bytes. Each size class occupies
|
|
|
|
// stack_size bytes and thus can allocate
|
|
|
|
// NumberOfFrames=(stack_size/BytesInSizeClass) fake frames (also a power of 2).
|
|
|
|
// For each size class we have NumberOfFrames allocation flags,
|
|
|
|
// each flag indicates whether the given frame is currently allocated.
|
|
|
|
// All flags for size classes 0 .. 10 are stored in a single contiguous region
|
|
|
|
// followed by another contiguous region which contains the actual memory for
|
|
|
|
// size classes. The addresses are computed by GetFlags and GetFrame without
|
|
|
|
// any memory accesses solely based on 'this' and stack_size_log.
|
|
|
|
// Allocate() flips the appropriate allocation flag atomically, thus achieving
|
|
|
|
// async-signal safety.
|
|
|
|
// This allocator does not have quarantine per se, but it tries to allocate the
|
|
|
|
// frames in round robin fasion to maximize the delay between a deallocation
|
|
|
|
// and the next allocation.
|
2013-04-11 19:39:19 +08:00
|
|
|
class FakeStack {
|
2013-09-12 15:11:58 +08:00
|
|
|
static const uptr kMinStackFrameSizeLog = 6; // Min frame is 64B.
|
|
|
|
static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K.
|
|
|
|
|
2013-04-11 19:39:19 +08:00
|
|
|
public:
|
2013-09-12 15:11:58 +08:00
|
|
|
static const uptr kNumberOfSizeClasses =
|
|
|
|
kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
|
|
|
|
|
|
|
|
// CTOR: create the FakeStack as a single mmap-ed object.
|
2013-09-27 19:37:23 +08:00
|
|
|
static FakeStack *Create(uptr stack_size_log);
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-12-11 21:54:01 +08:00
|
|
|
void Destroy(int tid);
|
2013-09-10 21:16:26 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// stack_size_log is at least 15 (stack_size >= 32K).
|
|
|
|
static uptr SizeRequiredForFlags(uptr stack_size_log) {
|
|
|
|
return 1UL << (stack_size_log + 1 - kMinStackFrameSizeLog);
|
2013-09-10 21:16:26 +08:00
|
|
|
}
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// Each size class occupies stack_size bytes.
|
|
|
|
static uptr SizeRequiredForFrames(uptr stack_size_log) {
|
|
|
|
return (1ULL << stack_size_log) * kNumberOfSizeClasses;
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// Number of bytes requires for the whole object.
|
|
|
|
static uptr RequiredSize(uptr stack_size_log) {
|
|
|
|
return kFlagsOffset + SizeRequiredForFlags(stack_size_log) +
|
|
|
|
SizeRequiredForFrames(stack_size_log);
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// Offset of the given flag from the first flag.
|
|
|
|
// The flags for class 0 begin at offset 000000000
|
|
|
|
// The flags for class 1 begin at offset 100000000
|
|
|
|
// ....................2................ 110000000
|
|
|
|
// ....................3................ 111000000
|
|
|
|
// and so on.
|
|
|
|
static uptr FlagsOffset(uptr stack_size_log, uptr class_id) {
|
|
|
|
uptr t = kNumberOfSizeClasses - 1 - class_id;
|
|
|
|
const uptr all_ones = (1 << (kNumberOfSizeClasses - 1)) - 1;
|
|
|
|
return ((all_ones >> t) << t) << (stack_size_log - 15);
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
static uptr NumberOfFrames(uptr stack_size_log, uptr class_id) {
|
|
|
|
return 1UL << (stack_size_log - kMinStackFrameSizeLog - class_id);
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// Divide n by the numbe of frames in size class.
|
|
|
|
static uptr ModuloNumberOfFrames(uptr stack_size_log, uptr class_id, uptr n) {
|
|
|
|
return n & (NumberOfFrames(stack_size_log, class_id) - 1);
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// The the pointer to the flags of the given class_id.
|
|
|
|
u8 *GetFlags(uptr stack_size_log, uptr class_id) {
|
|
|
|
return reinterpret_cast<u8 *>(this) + kFlagsOffset +
|
|
|
|
FlagsOffset(stack_size_log, class_id);
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// Get frame by class_id and pos.
|
|
|
|
u8 *GetFrame(uptr stack_size_log, uptr class_id, uptr pos) {
|
|
|
|
return reinterpret_cast<u8 *>(this) + kFlagsOffset +
|
|
|
|
SizeRequiredForFlags(stack_size_log) +
|
|
|
|
(1 << stack_size_log) * class_id + BytesInSizeClass(class_id) * pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the fake frame.
|
|
|
|
FakeFrame *Allocate(uptr stack_size_log, uptr class_id, uptr real_stack);
|
|
|
|
|
2013-09-17 15:42:54 +08:00
|
|
|
// Deallocate the fake frame: read the saved flag address and write 0 there.
|
|
|
|
static void Deallocate(uptr x, uptr class_id) {
|
|
|
|
**SavedFlagPtr(x, class_id) = 0;
|
|
|
|
}
|
2013-04-11 19:39:19 +08:00
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
// Poison the entire FakeStack's shadow with the magic value.
|
|
|
|
void PoisonAll(u8 magic);
|
|
|
|
|
|
|
|
// Return the beginning of the FakeFrame or 0 if the address is not ours.
|
|
|
|
uptr AddrIsInFakeStack(uptr addr);
|
|
|
|
|
|
|
|
// Number of bytes in a fake frame of this size class.
|
|
|
|
static uptr BytesInSizeClass(uptr class_id) {
|
|
|
|
return 1UL << (class_id + kMinStackFrameSizeLog);
|
|
|
|
}
|
|
|
|
|
2013-09-17 15:42:54 +08:00
|
|
|
// The fake frame is guaranteed to have a right redzone.
|
|
|
|
// We use the last word of that redzone to store the address of the flag
|
|
|
|
// that corresponds to the current frame to make faster deallocation.
|
|
|
|
static u8 **SavedFlagPtr(uptr x, uptr class_id) {
|
|
|
|
return reinterpret_cast<u8 **>(x + BytesInSizeClass(class_id) - sizeof(x));
|
|
|
|
}
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
uptr stack_size_log() const { return stack_size_log_; }
|
|
|
|
|
2013-09-12 21:25:29 +08:00
|
|
|
void HandleNoReturn();
|
|
|
|
void GC(uptr real_stack);
|
|
|
|
|
2013-10-14 22:04:50 +08:00
|
|
|
void ForEachFakeFrame(RangeIteratorCallback callback, void *arg);
|
|
|
|
|
2013-09-12 15:11:58 +08:00
|
|
|
private:
|
|
|
|
FakeStack() { }
|
2013-09-12 21:25:29 +08:00
|
|
|
static const uptr kFlagsOffset = 4096; // This is were the flags begin.
|
2013-09-12 15:11:58 +08:00
|
|
|
// Must match the number of uses of DEFINE_STACK_MALLOC_FREE_WITH_CLASS_ID
|
|
|
|
COMPILER_CHECK(kNumberOfSizeClasses == 11);
|
|
|
|
static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
|
|
|
|
|
|
|
|
uptr hint_position_[kNumberOfSizeClasses];
|
|
|
|
uptr stack_size_log_;
|
2013-09-12 21:25:29 +08:00
|
|
|
// a bit is set if something was allocated from the corresponding size class.
|
|
|
|
bool needs_gc_;
|
2013-09-12 15:11:58 +08:00
|
|
|
};
|
2013-06-26 20:16:05 +08:00
|
|
|
|
2013-09-13 14:32:26 +08:00
|
|
|
FakeStack *GetTLSFakeStack();
|
|
|
|
void SetTLSFakeStack(FakeStack *fs);
|
|
|
|
|
2013-04-11 19:39:19 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
|
|
|
#endif // ASAN_FAKE_STACK_H
|