[asan] improve the UAR reporting (try harder to find the correct frame), try to make the test more stable

llvm-svn: 179292
This commit is contained in:
Kostya Serebryany 2013-04-11 15:35:40 +00:00
parent a95f87494a
commit 1c77de3aeb
2 changed files with 21 additions and 20 deletions

View File

@ -30,24 +30,26 @@ bool FakeStack::AddrIsInSizeClass(uptr addr, uptr size_class) {
}
uptr FakeStack::AddrIsInFakeStack(uptr addr) {
for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
if (AddrIsInSizeClass(addr, i)) return allocated_size_classes_[i];
for (uptr size_class = 0; size_class < kNumberOfSizeClasses; size_class++) {
if (!AddrIsInSizeClass(addr, size_class)) continue;
uptr size_class_first_ptr = allocated_size_classes_[size_class];
uptr size = ClassSize(size_class);
CHECK_LE(size_class_first_ptr, addr);
CHECK_GT(size_class_first_ptr + ClassMmapSize(size_class), addr);
return size_class_first_ptr + ((addr - size_class_first_ptr) / size) * size;
}
return 0;
}
// We may want to compute this during compilation.
inline uptr FakeStack::ComputeSizeClass(uptr alloc_size) {
ALWAYS_INLINE uptr FakeStack::ComputeSizeClass(uptr alloc_size) {
uptr rounded_size = RoundUpToPowerOfTwo(alloc_size);
uptr log = Log2(rounded_size);
CHECK(alloc_size <= (1UL << log));
if (!(alloc_size > (1UL << (log-1)))) {
Printf("alloc_size %zu log %zu\n", alloc_size, log);
}
CHECK(alloc_size > (1UL << (log-1)));
CHECK_LE(alloc_size, (1UL << log));
CHECK_GT(alloc_size, (1UL << (log-1)));
uptr res = log < kMinStackFrameSizeLog ? 0 : log - kMinStackFrameSizeLog;
CHECK(res < kNumberOfSizeClasses);
CHECK(ClassSize(res) >= rounded_size);
CHECK_LT(res, kNumberOfSizeClasses);
CHECK_GE(ClassSize(res), rounded_size);
return res;
}
@ -115,7 +117,7 @@ void FakeStack::AllocateOneSizeClass(uptr size_class) {
allocated_size_classes_[size_class] = new_mem;
}
uptr FakeStack::AllocateStack(uptr size, uptr real_stack) {
ALWAYS_INLINE uptr FakeStack::AllocateStack(uptr size, uptr real_stack) {
if (!alive_) return real_stack;
CHECK(size <= kMaxStackMallocSize && size > 1);
uptr size_class = ComputeSizeClass(size);
@ -137,7 +139,7 @@ uptr FakeStack::AllocateStack(uptr size, uptr real_stack) {
return ptr;
}
void FakeStack::DeallocateFrame(FakeFrame *fake_frame) {
ALWAYS_INLINE void FakeStack::DeallocateFrame(FakeFrame *fake_frame) {
CHECK(alive_);
uptr size = fake_frame->size_minus_one + 1;
uptr size_class = ComputeSizeClass(size);
@ -148,7 +150,7 @@ void FakeStack::DeallocateFrame(FakeFrame *fake_frame) {
size_classes_[size_class].FifoPush(fake_frame);
}
void FakeStack::OnFree(uptr ptr, uptr size, uptr real_stack) {
ALWAYS_INLINE void FakeStack::OnFree(uptr ptr, uptr size, uptr real_stack) {
FakeFrame *fake_frame = (FakeFrame*)ptr;
CHECK_EQ(fake_frame->magic, kRetiredStackFrameMagic);
CHECK_NE(fake_frame->descr, 0);

View File

@ -10,7 +10,7 @@
#include <stdlib.h>
__attribute__((noinline))
inline char *pretend_to_do_something(char *x) {
char *pretend_to_do_something(char *x) {
__asm__ __volatile__("" : : "r" (x) : "memory");
return x;
}
@ -26,19 +26,18 @@ __attribute__((noinline))
void RecuriveFunctionWithStackFrame(int depth) {
if (depth <= 0) return;
char x[1024];
memset(x, 0, sizeof(x));
x[0] = depth;
pretend_to_do_something(x);
RecuriveFunctionWithStackFrame(depth - 1);
memset(x, 0, sizeof(x));
}
int main(int argc, char **argv) {
char *stale_stack = LeakStack();
int n_iter = argc >= 2 ? atoi(argv[1]) : 1000;
int depth = argc >= 3 ? atoi(argv[2]) : 1000;
for (int i = 0; i < n_iter; i++) {
int depth = argc >= 3 ? atoi(argv[2]) : 500;
for (int i = 0; i < n_iter; i++)
RecuriveFunctionWithStackFrame(depth);
}
char *stale_stack = LeakStack();
RecuriveFunctionWithStackFrame(10);
stale_stack[100]++;
// CHECK: ERROR: AddressSanitizer: stack-use-after-return on address
// CHECK: is located in stack of thread T0 at offset 132 in frame