Fix GetFrameNameByAddr hitting stale stack guards.

In the current implementation AsanThread::GetFrameNameByAddr scans the
stack for a magic guard value to locate base address of the stack
frame. This is not reliable, especially on ARM, where the code that
stores this magic value has to construct it in a register from two
small intermediates; this register can then end up stored in a random
stack location in the prologue of another function.

With this change, GetFrameNameByAddr scans the shadow memory for the
signature of a left stack redzone instead. It is now possible to
remove the magic from the instrumentation pass for additional
performance gain. We keep it there for now just to make sure the new
algorithm does not fail in some corner case.

llvm-svn: 156710
This commit is contained in:
Evgeniy Stepanov 2012-05-12 12:33:10 +00:00
parent 95d31bcba5
commit d989be1386
2 changed files with 23 additions and 10 deletions

View File

@ -35,6 +35,7 @@ extern __attribute__((visibility("default"))) uintptr_t __asan_mapping_offset;
#define SHADOW_GRANULARITY (1ULL << SHADOW_SCALE)
#define MEM_TO_SHADOW(mem) (((mem) >> SHADOW_SCALE) | (SHADOW_OFFSET))
#define SHADOW_TO_MEM(shadow) (((shadow) - SHADOW_OFFSET) << SHADOW_SCALE)
#if __WORDSIZE == 64
static const size_t kHighMemEnd = 0x00007fffffffffffUL;

View File

@ -116,17 +116,29 @@ const char *AsanThread::GetFrameNameByAddr(uintptr_t addr, uintptr_t *offset) {
is_fake_stack = true;
}
uintptr_t aligned_addr = addr & ~(__WORDSIZE/8 - 1); // align addr.
uintptr_t *ptr = (uintptr_t*)aligned_addr;
while (ptr >= (uintptr_t*)bottom) {
if (ptr[0] == kCurrentStackFrameMagic ||
(is_fake_stack && ptr[0] == kRetiredStackFrameMagic)) {
*offset = addr - (uintptr_t)ptr;
return (const char*)ptr[1];
}
ptr--;
uint8_t *shadow_ptr = (uint8_t*)MemToShadow(aligned_addr);
uint8_t *shadow_bottom = (uint8_t*)MemToShadow(bottom);
while (shadow_ptr >= shadow_bottom &&
*shadow_ptr != kAsanStackLeftRedzoneMagic) {
shadow_ptr--;
}
*offset = 0;
return "UNKNOWN";
while (shadow_ptr >= shadow_bottom &&
*shadow_ptr == kAsanStackLeftRedzoneMagic) {
shadow_ptr--;
}
if (shadow_ptr < shadow_bottom) {
*offset = 0;
return "UNKNOWN";
}
uintptr_t* ptr = (uintptr_t*)SHADOW_TO_MEM((uintptr_t)(shadow_ptr + 1));
CHECK((ptr[0] == kCurrentStackFrameMagic) ||
(is_fake_stack && ptr[0] == kRetiredStackFrameMagic));
*offset = addr - (uintptr_t)ptr;
return (const char*)ptr[1];
}
} // namespace __asan