[asan] get rid of some of the uses of kPageSize. The intent is to get rid of it completely to support platforms with multiple possible page sizes.

llvm-svn: 168517
This commit is contained in:
Kostya Serebryany 2012-11-23 15:38:49 +00:00
parent 59189597de
commit f22c697f58
11 changed files with 61 additions and 29 deletions

View File

@ -132,7 +132,7 @@ static void PoisonHeapPartialRightRedzone(uptr mem, uptr size) {
} }
static u8 *MmapNewPagesAndPoisonShadow(uptr size) { static u8 *MmapNewPagesAndPoisonShadow(uptr size) {
CHECK(IsAligned(size, kPageSize)); CHECK(IsAligned(size, GetPageSizeCached()));
u8 *res = (u8*)MmapOrDie(size, __FUNCTION__); u8 *res = (u8*)MmapOrDie(size, __FUNCTION__);
PoisonShadow((uptr)res, size, kAsanHeapLeftRedzoneMagic); PoisonShadow((uptr)res, size, kAsanHeapLeftRedzoneMagic);
if (flags()->debug) { if (flags()->debug) {
@ -534,12 +534,13 @@ class MallocInfo {
uptr mmap_size = Max(size, kMinMmapSize); uptr mmap_size = Max(size, kMinMmapSize);
uptr n_chunks = mmap_size / size; uptr n_chunks = mmap_size / size;
CHECK(n_chunks * size == mmap_size); CHECK(n_chunks * size == mmap_size);
if (size < kPageSize) { uptr PageSize = GetPageSizeCached();
if (size < PageSize) {
// Size is small, just poison the last chunk. // Size is small, just poison the last chunk.
n_chunks--; n_chunks--;
} else { } else {
// Size is large, allocate an extra page at right and poison it. // Size is large, allocate an extra page at right and poison it.
mmap_size += kPageSize; mmap_size += PageSize;
} }
CHECK(n_chunks > 0); CHECK(n_chunks > 0);
u8 *mem = MmapNewPagesAndPoisonShadow(mmap_size); u8 *mem = MmapNewPagesAndPoisonShadow(mmap_size);
@ -813,18 +814,19 @@ void *asan_realloc(void *p, uptr size, StackTrace *stack) {
} }
void *asan_valloc(uptr size, StackTrace *stack) { void *asan_valloc(uptr size, StackTrace *stack) {
void *ptr = (void*)Allocate(kPageSize, size, stack); void *ptr = (void*)Allocate(GetPageSizeCached(), size, stack);
__asan_malloc_hook(ptr, size); __asan_malloc_hook(ptr, size);
return ptr; return ptr;
} }
void *asan_pvalloc(uptr size, StackTrace *stack) { void *asan_pvalloc(uptr size, StackTrace *stack) {
size = RoundUpTo(size, kPageSize); uptr PageSize = GetPageSizeCached();
size = RoundUpTo(size, PageSize);
if (size == 0) { if (size == 0) {
// pvalloc(0) should allocate one page. // pvalloc(0) should allocate one page.
size = kPageSize; size = PageSize;
} }
void *ptr = (void*)Allocate(kPageSize, size, stack); void *ptr = (void*)Allocate(PageSize, size, stack);
__asan_malloc_hook(ptr, size); __asan_malloc_hook(ptr, size);
return ptr; return ptr;
} }
@ -943,7 +945,7 @@ uptr FakeStack::ClassMmapSize(uptr size_class) {
} }
void FakeStack::AllocateOneSizeClass(uptr size_class) { void FakeStack::AllocateOneSizeClass(uptr size_class) {
CHECK(ClassMmapSize(size_class) >= kPageSize); CHECK(ClassMmapSize(size_class) >= GetPageSizeCached());
uptr new_mem = (uptr)MmapOrDie( uptr new_mem = (uptr)MmapOrDie(
ClassMmapSize(size_class), __FUNCTION__); ClassMmapSize(size_class), __FUNCTION__);
// Printf("T%d new_mem[%zu]: %p-%p mmap %zu\n", // Printf("T%d new_mem[%zu]: %p-%p mmap %zu\n",

View File

@ -176,9 +176,10 @@ void ClearShadowMemoryForContext(void *context) {
uptr sp = (uptr)ucp->uc_stack.ss_sp; uptr sp = (uptr)ucp->uc_stack.ss_sp;
uptr size = ucp->uc_stack.ss_size; uptr size = ucp->uc_stack.ss_size;
// Align to page size. // Align to page size.
uptr bottom = sp & ~(kPageSize - 1); uptr PageSize = GetPageSizeCached();
uptr bottom = sp & ~(PageSize - 1);
size += sp - bottom; size += sp - bottom;
size = RoundUpTo(size, kPageSize); size = RoundUpTo(size, PageSize);
PoisonShadow(bottom, size, 0); PoisonShadow(bottom, size, 0);
} }
#else #else

View File

@ -165,7 +165,7 @@ void *mz_valloc(malloc_zone_t *zone, size_t size) {
return malloc_zone_valloc(system_malloc_zone, size); return malloc_zone_valloc(system_malloc_zone, size);
} }
GET_STACK_TRACE_HERE_FOR_MALLOC; GET_STACK_TRACE_HERE_FOR_MALLOC;
return asan_memalign(kPageSize, size, &stack); return asan_memalign(GetPageSizeCached(), size, &stack);
} }
#define GET_ZONE_FOR_PTR(ptr) \ #define GET_ZONE_FOR_PTR(ptr) \

View File

@ -165,8 +165,8 @@ void ShowStatsAndAbort() {
// ---------------------- mmap -------------------- {{{1 // ---------------------- mmap -------------------- {{{1
// Reserve memory range [beg, end]. // Reserve memory range [beg, end].
static void ReserveShadowMemoryRange(uptr beg, uptr end) { static void ReserveShadowMemoryRange(uptr beg, uptr end) {
CHECK((beg % kPageSize) == 0); CHECK((beg % GetPageSizeCached()) == 0);
CHECK(((end + 1) % kPageSize) == 0); CHECK(((end + 1) % GetPageSizeCached()) == 0);
uptr size = end - beg + 1; uptr size = end - beg + 1;
void *res = MmapFixedNoReserve(beg, size); void *res = MmapFixedNoReserve(beg, size);
if (res != (void*)beg) { if (res != (void*)beg) {
@ -271,8 +271,9 @@ void NOINLINE __asan_handle_no_return() {
int local_stack; int local_stack;
AsanThread *curr_thread = asanThreadRegistry().GetCurrent(); AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
CHECK(curr_thread); CHECK(curr_thread);
uptr PageSize = GetPageSizeCached();
uptr top = curr_thread->stack_top(); uptr top = curr_thread->stack_top();
uptr bottom = ((uptr)&local_stack - kPageSize) & ~(kPageSize-1); uptr bottom = ((uptr)&local_stack - PageSize) & ~(PageSize-1);
PoisonShadow(bottom, top - bottom, 0); PoisonShadow(bottom, top - bottom, 0);
} }

View File

@ -43,7 +43,7 @@ void AsanStats::Print() {
Printf("Stats: %zuM really freed by %zu calls\n", Printf("Stats: %zuM really freed by %zu calls\n",
really_freed>>20, real_frees); really_freed>>20, real_frees);
Printf("Stats: %zuM (%zu full pages) mmaped in %zu calls\n", Printf("Stats: %zuM (%zu full pages) mmaped in %zu calls\n",
mmaped>>20, mmaped / kPageSize, mmaps); mmaped>>20, mmaped / GetPageSizeCached(), mmaps);
PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size); PrintMallocStatsArray(" mmaps by size class: ", mmaped_by_size);
PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size); PrintMallocStatsArray(" mallocs by size class: ", malloced_by_size);

View File

@ -28,15 +28,16 @@ AsanThread::AsanThread(LinkerInitialized x)
AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine, AsanThread *AsanThread::Create(u32 parent_tid, thread_callback_t start_routine,
void *arg, StackTrace *stack) { void *arg, StackTrace *stack) {
uptr size = RoundUpTo(sizeof(AsanThread), kPageSize); uptr PageSize = GetPageSizeCached();
uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__); AsanThread *thread = (AsanThread*)MmapOrDie(size, __FUNCTION__);
thread->start_routine_ = start_routine; thread->start_routine_ = start_routine;
thread->arg_ = arg; thread->arg_ = arg;
const uptr kSummaryAllocSize = kPageSize; const uptr kSummaryAllocSize = PageSize;
CHECK_LE(sizeof(AsanThreadSummary), kSummaryAllocSize); CHECK_LE(sizeof(AsanThreadSummary), kSummaryAllocSize);
AsanThreadSummary *summary = AsanThreadSummary *summary =
(AsanThreadSummary*)MmapOrDie(kPageSize, "AsanThreadSummary"); (AsanThreadSummary*)MmapOrDie(PageSize, "AsanThreadSummary");
summary->Init(parent_tid, stack); summary->Init(parent_tid, stack);
summary->set_thread(thread); summary->set_thread(thread);
thread->set_summary(summary); thread->set_summary(summary);
@ -66,7 +67,7 @@ void AsanThread::Destroy() {
// and we don't want it to have any poisoned stack. // and we don't want it to have any poisoned stack.
ClearShadowForThreadStack(); ClearShadowForThreadStack();
fake_stack().Cleanup(); fake_stack().Cleanup();
uptr size = RoundUpTo(sizeof(AsanThread), kPageSize); uptr size = RoundUpTo(sizeof(AsanThread), GetPageSizeCached());
UnmapOrDie(this, size); UnmapOrDie(this, size);
} }

View File

@ -16,6 +16,13 @@
namespace __sanitizer { namespace __sanitizer {
uptr GetPageSizeCached() {
static uptr PageSize;
if (!PageSize)
PageSize = GetPageSize();
return PageSize;
}
// By default, dump to stderr. If report_fd is kInvalidFd, try to obtain file // By default, dump to stderr. If report_fd is kInvalidFd, try to obtain file
// descriptor by opening file in report_path. // descriptor by opening file in report_path.
static fd_t report_fd = kStderrFd; static fd_t report_fd = kStderrFd;
@ -77,7 +84,8 @@ void RawWrite(const char *buffer) {
uptr ReadFileToBuffer(const char *file_name, char **buff, uptr ReadFileToBuffer(const char *file_name, char **buff,
uptr *buff_size, uptr max_len) { uptr *buff_size, uptr max_len) {
const uptr kMinFileLen = kPageSize; uptr PageSize = GetPageSizeCached();
uptr kMinFileLen = PageSize;
uptr read_len = 0; uptr read_len = 0;
*buff = 0; *buff = 0;
*buff_size = 0; *buff_size = 0;
@ -91,8 +99,8 @@ uptr ReadFileToBuffer(const char *file_name, char **buff,
// Read up to one page at a time. // Read up to one page at a time.
read_len = 0; read_len = 0;
bool reached_eof = false; bool reached_eof = false;
while (read_len + kPageSize <= size) { while (read_len + PageSize <= size) {
uptr just_read = internal_read(fd, *buff + read_len, kPageSize); uptr just_read = internal_read(fd, *buff + read_len, PageSize);
if (just_read == 0) { if (just_read == 0) {
reached_eof = true; reached_eof = true;
break; break;

View File

@ -42,6 +42,9 @@ const uptr kCacheLineSize = 64;
const uptr kMmapGranularity = 1UL << 16; const uptr kMmapGranularity = 1UL << 16;
#endif #endif
uptr GetPageSize();
uptr GetPageSizeCached();
uptr GetMmapGranularity();
// Threads // Threads
int GetPid(); int GetPid();
uptr GetTid(); uptr GetTid();

View File

@ -32,6 +32,13 @@
namespace __sanitizer { namespace __sanitizer {
// ------------- sanitizer_common.h // ------------- sanitizer_common.h
uptr GetPageSize() {
return sysconf(_SC_PAGESIZE);
}
uptr GetMmapGranularity() {
return GetPageSize();
}
int GetPid() { int GetPid() {
return getpid(); return getpid();
@ -42,7 +49,7 @@ uptr GetThreadSelf() {
} }
void *MmapOrDie(uptr size, const char *mem_type) { void *MmapOrDie(uptr size, const char *mem_type) {
size = RoundUpTo(size, kPageSize); size = RoundUpTo(size, GetPageSizeCached());
void *res = internal_mmap(0, size, void *res = internal_mmap(0, size,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0); MAP_PRIVATE | MAP_ANON, -1, 0);
@ -74,8 +81,9 @@ void UnmapOrDie(void *addr, uptr size) {
} }
void *MmapFixedNoReserve(uptr fixed_addr, uptr size) { void *MmapFixedNoReserve(uptr fixed_addr, uptr size) {
void *p = internal_mmap((void*)(fixed_addr & ~(kPageSize - 1)), uptr PageSize = GetPageSizeCached();
RoundUpTo(size, kPageSize), void *p = internal_mmap((void*)(fixed_addr & ~(PageSize - 1)),
RoundUpTo(size, PageSize),
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE, MAP_PRIVATE | MAP_ANON | MAP_FIXED | MAP_NORESERVE,
-1, 0); -1, 0);
@ -98,7 +106,7 @@ void *MapFileToMemory(const char *file_name, uptr *buff_size) {
uptr fsize = internal_filesize(fd); uptr fsize = internal_filesize(fd);
CHECK_NE(fsize, (uptr)-1); CHECK_NE(fsize, (uptr)-1);
CHECK_GT(fsize, 0); CHECK_GT(fsize, 0);
*buff_size = RoundUpTo(fsize, kPageSize); *buff_size = RoundUpTo(fsize, GetPageSizeCached());
void *map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0); void *map = internal_mmap(0, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
return (map == MAP_FAILED) ? 0 : map; return (map == MAP_FAILED) ? 0 : map;
} }

View File

@ -23,6 +23,14 @@
namespace __sanitizer { namespace __sanitizer {
// --------------------- sanitizer_common.h // --------------------- sanitizer_common.h
uptr GetPageSize() {
return 1U << 14; // FIXME: is this configurable?
}
uptr GetMmapGranularity() {
return 1U << 16; // FIXME: is this configurable?
}
bool FileExists(const char *filename) { bool FileExists(const char *filename) {
UNIMPLEMENTED(); UNIMPLEMENTED();
} }

View File

@ -566,13 +566,13 @@ TSAN_INTERCEPTOR(void*, memalign, uptr align, uptr sz) {
TSAN_INTERCEPTOR(void*, valloc, uptr sz) { TSAN_INTERCEPTOR(void*, valloc, uptr sz) {
SCOPED_TSAN_INTERCEPTOR(valloc, sz); SCOPED_TSAN_INTERCEPTOR(valloc, sz);
return user_alloc(thr, pc, sz, kPageSize); return user_alloc(thr, pc, sz, GetPageSizeCached());
} }
TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) { TSAN_INTERCEPTOR(void*, pvalloc, uptr sz) {
SCOPED_TSAN_INTERCEPTOR(pvalloc, sz); SCOPED_TSAN_INTERCEPTOR(pvalloc, sz);
sz = RoundUp(sz, kPageSize); sz = RoundUp(sz, GetPageSizeCached());
return user_alloc(thr, pc, sz, kPageSize); return user_alloc(thr, pc, sz, GetPageSizeCached());
} }
TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) { TSAN_INTERCEPTOR(int, posix_memalign, void **memptr, uptr align, uptr sz) {