2011-11-30 09:07:02 +08:00
|
|
|
//===-- asan_allocator.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.
|
|
|
|
//
|
|
|
|
// ASan-private header for asan_allocator.cc.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef ASAN_ALLOCATOR_H
|
|
|
|
#define ASAN_ALLOCATOR_H
|
|
|
|
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kNumberOfSizeClasses = 255;
|
2011-12-01 01:33:13 +08:00
|
|
|
struct AsanChunk;
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
class AsanChunkFifoList {
|
|
|
|
public:
|
|
|
|
explicit AsanChunkFifoList(LinkerInitialized) { }
|
|
|
|
AsanChunkFifoList() { clear(); }
|
|
|
|
void Push(AsanChunk *n);
|
|
|
|
void PushList(AsanChunkFifoList *q);
|
|
|
|
AsanChunk *Pop();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size() { return size_; }
|
2011-11-30 09:07:02 +08:00
|
|
|
void clear() {
|
2012-05-31 22:35:53 +08:00
|
|
|
first_ = last_ = 0;
|
2011-11-30 09:07:02 +08:00
|
|
|
size_ = 0;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
AsanChunk *first_;
|
|
|
|
AsanChunk *last_;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr size_;
|
2011-11-30 09:07:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AsanThreadLocalMallocStorage {
|
|
|
|
explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
|
|
|
|
: quarantine_(x) { }
|
|
|
|
AsanThreadLocalMallocStorage() {
|
2012-02-08 21:45:31 +08:00
|
|
|
CHECK(REAL(memset));
|
|
|
|
REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
|
2011-11-30 09:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AsanChunkFifoList quarantine_;
|
|
|
|
AsanChunk *free_lists_[kNumberOfSizeClasses];
|
|
|
|
void CommitBack();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Fake stack frame contains local variables of one function.
|
|
|
|
// This struct should fit into a stack redzone (32 bytes).
|
|
|
|
struct FakeFrame {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr magic; // Modified by the instrumented code.
|
|
|
|
uptr descr; // Modified by the instrumented code.
|
2011-11-30 09:07:02 +08:00
|
|
|
FakeFrame *next;
|
|
|
|
uint64_t real_stack : 48;
|
|
|
|
uint64_t size_minus_one : 16;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FakeFrameFifo {
|
|
|
|
public:
|
|
|
|
void FifoPush(FakeFrame *node);
|
|
|
|
FakeFrame *FifoPop();
|
|
|
|
private:
|
|
|
|
FakeFrame *first_, *last_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FakeFrameLifo {
|
|
|
|
public:
|
|
|
|
void LifoPush(FakeFrame *node) {
|
|
|
|
node->next = top_;
|
|
|
|
top_ = node;
|
|
|
|
}
|
|
|
|
void LifoPop() {
|
|
|
|
CHECK(top_);
|
|
|
|
top_ = top_->next;
|
|
|
|
}
|
|
|
|
FakeFrame *top() { return top_; }
|
|
|
|
private:
|
|
|
|
FakeFrame *top_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// is not poped but remains there for quite some time until gets used again.
|
|
|
|
// So, we poison the objects on the fake stack when function returns.
|
|
|
|
// It helps us find use-after-return bugs.
|
|
|
|
// We can not rely on __asan_stack_free being called on every function exit,
|
|
|
|
// so we maintain a lifo list of all current fake frames and update it on every
|
|
|
|
// call to __asan_stack_malloc.
|
|
|
|
class FakeStack {
|
|
|
|
public:
|
|
|
|
FakeStack();
|
|
|
|
explicit FakeStack(LinkerInitialized) {}
|
2012-05-31 22:35:53 +08:00
|
|
|
void Init(uptr stack_size);
|
2011-12-09 09:49:31 +08:00
|
|
|
void StopUsingFakeStack() { alive_ = false; }
|
2011-11-30 09:07:02 +08:00
|
|
|
void Cleanup();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr AllocateStack(uptr size, uptr real_stack);
|
|
|
|
static void OnFree(uptr ptr, uptr size, uptr real_stack);
|
2011-11-30 09:07:02 +08:00
|
|
|
// Return the bottom of the maped region.
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr AddrIsInFakeStack(uptr addr);
|
2012-02-21 16:45:41 +08:00
|
|
|
bool StackSize() { return stack_size_; }
|
2011-11-30 09:07:02 +08:00
|
|
|
private:
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kMinStackFrameSizeLog = 9; // Min frame is 512B.
|
|
|
|
static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K.
|
|
|
|
static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
|
|
|
|
static const uptr kNumberOfSizeClasses =
|
2011-11-30 09:07:02 +08:00
|
|
|
kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
bool AddrIsInSizeClass(uptr addr, uptr size_class);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
// Each size class should be large enough to hold all frames.
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr ClassMmapSize(uptr size_class);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr ClassSize(uptr size_class) {
|
2011-11-30 09:07:02 +08:00
|
|
|
return 1UL << (size_class + kMinStackFrameSizeLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeallocateFrame(FakeFrame *fake_frame);
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr ComputeSizeClass(uptr alloc_size);
|
|
|
|
void AllocateOneSizeClass(uptr size_class);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr stack_size_;
|
2011-11-30 09:07:02 +08:00
|
|
|
bool alive_;
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr allocated_size_classes_[kNumberOfSizeClasses];
|
2011-11-30 09:07:02 +08:00
|
|
|
FakeFrameFifo size_classes_[kNumberOfSizeClasses];
|
|
|
|
FakeFrameLifo call_stack_;
|
|
|
|
};
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void *asan_memalign(uptr alignment, uptr size, AsanStackTrace *stack);
|
2011-11-30 09:07:02 +08:00
|
|
|
void asan_free(void *ptr, AsanStackTrace *stack);
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void *asan_malloc(uptr size, AsanStackTrace *stack);
|
|
|
|
void *asan_calloc(uptr nmemb, uptr size, AsanStackTrace *stack);
|
|
|
|
void *asan_realloc(void *p, uptr size, AsanStackTrace *stack);
|
|
|
|
void *asan_valloc(uptr size, AsanStackTrace *stack);
|
|
|
|
void *asan_pvalloc(uptr size, AsanStackTrace *stack);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
|
2011-11-30 09:07:02 +08:00
|
|
|
AsanStackTrace *stack);
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr asan_malloc_usable_size(void *ptr, AsanStackTrace *stack);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr asan_mz_size(const void *ptr);
|
2012-01-17 14:39:10 +08:00
|
|
|
void asan_mz_force_lock();
|
|
|
|
void asan_mz_force_unlock();
|
2012-05-31 22:35:53 +08:00
|
|
|
void DescribeHeapAddress(uptr addr, uptr access_size);
|
2011-11-30 09:07:02 +08:00
|
|
|
|
|
|
|
} // namespace __asan
|
|
|
|
#endif // ASAN_ALLOCATOR_H
|