From 7575968aa4cb3663aae66f95edf8a017487aca2d Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Tue, 28 Aug 2012 14:11:57 +0000 Subject: [PATCH] [asan] actually move StackTrace to sanitizer_common llvm-svn: 162757 --- compiler-rt/lib/asan/asan_internal.h | 14 +- compiler-rt/lib/asan/asan_stack.cc | 206 +----------------- compiler-rt/lib/asan/asan_stack.h | 56 +---- .../lib/sanitizer_common/CMakeLists.txt | 1 + .../sanitizer_internal_defs.h | 11 + 5 files changed, 16 insertions(+), 272 deletions(-) diff --git a/compiler-rt/lib/asan/asan_internal.h b/compiler-rt/lib/asan/asan_internal.h index 1d18026dec03..46741c94bf00 100644 --- a/compiler-rt/lib/asan/asan_internal.h +++ b/compiler-rt/lib/asan/asan_internal.h @@ -17,6 +17,7 @@ #include "asan_flags.h" #include "sanitizer_common/sanitizer_common.h" #include "sanitizer_common/sanitizer_internal_defs.h" +#include "sanitizer_common/sanitizer_stacktrace.h" #include "sanitizer_common/sanitizer_libc.h" #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) @@ -93,7 +94,7 @@ extern "C" void* _ReturnAddress(void); namespace __asan { class AsanThread; -struct StackTrace; +using __sanitizer::StackTrace; // asan_rtl.cc void NORETURN ShowStatsAndAbort(); @@ -145,17 +146,6 @@ extern int asan_inited; extern bool asan_init_is_running; extern void (*death_callback)(void); -#if !defined(_WIN32) || defined(__clang__) -# define GET_CALLER_PC() (uptr)__builtin_return_address(0) -# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) -#else -# define GET_CALLER_PC() (uptr)_ReturnAddress() -// CaptureStackBackTrace doesn't need to know BP on Windows. -// FIXME: This macro is still used when printing error reports though it's not -// clear if the BP value is needed in the ASan reports on Windows. -# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF -#endif - #ifdef _WIN32 bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size); #endif // _WIN32 diff --git a/compiler-rt/lib/asan/asan_stack.cc b/compiler-rt/lib/asan/asan_stack.cc index d8611333ade5..a69accdbc11f 100644 --- a/compiler-rt/lib/asan/asan_stack.cc +++ b/compiler-rt/lib/asan/asan_stack.cc @@ -11,218 +11,14 @@ // // Code for ASan stack trace. //===----------------------------------------------------------------------===// +#include "asan_flags.h" #include "asan_interface.h" #include "asan_stack.h" -#include "sanitizer_common/sanitizer_procmaps.h" -#include "sanitizer_common/sanitizer_symbolizer.h" namespace __asan { static __asan_symbolize_callback symbolize_callback; -static const char *StripPathPrefix(const char *filepath, - const char *strip_file_prefix) { - if (filepath == internal_strstr(filepath, strip_file_prefix)) - return filepath + internal_strlen(strip_file_prefix); - return filepath; -} - -// ----------------------- StackTrace ----------------------------- {{{1 -// PCs in stack traces are actually the return addresses, that is, -// addresses of the next instructions after the call. That's why we -// decrement them. -static uptr patch_pc(uptr pc) { -#ifdef __arm__ - // Cancel Thumb bit. - pc = pc & (~1); -#endif - return pc - 1; -} - -void StackTrace::PrintStack(uptr *addr, uptr size, - bool symbolize, const char *strip_file_prefix, - SymbolizeCallback symbolize_callback ) { - MemoryMappingLayout proc_maps; - uptr frame_num = 0; - for (uptr i = 0; i < size && addr[i]; i++) { - uptr pc = patch_pc(addr[i]); - if (symbolize_callback) { - char buff[4096]; - symbolize_callback((void*)pc, buff, sizeof(buff)); - // We can't know anything about the string returned by external - // symbolizer, but if it starts with filename, try to strip path prefix - // from it. - Printf(" #%zu 0x%zx %s\n", frame_num, pc, - StripPathPrefix(buff, strip_file_prefix)); - frame_num++; - continue; - } - AddressInfo addr_frames[64]; - uptr addr_frames_num = - symbolize ? SymbolizeCode(pc, addr_frames, ARRAY_SIZE(addr_frames)) : 0; - if (addr_frames_num > 0) { - for (uptr j = 0; j < addr_frames_num; j++) { - AddressInfo &info = addr_frames[j]; - Printf(" #%zu 0x%zx", frame_num, pc); - if (info.function) { - Printf(" in %s", info.function); - } - if (info.file) { - Printf(" %s:%d:%d", StripPathPrefix(info.file, strip_file_prefix), - info.line, info.column); - } else if (info.module) { - Printf(" (%s+0x%zx)", StripPathPrefix(info.module, strip_file_prefix), - info.module_offset); - } - Printf("\n"); - info.Clear(); - frame_num++; - } - } else { - uptr offset; - char filename[4096]; - if (proc_maps.GetObjectNameAndOffset(pc, &offset, - filename, sizeof(filename))) { - Printf(" #%zu 0x%zx (%s+0x%zx)\n", frame_num, pc, - StripPathPrefix(filename, strip_file_prefix), offset); - } else { - Printf(" #%zu 0x%zx\n", frame_num, pc); - } - frame_num++; - } - } -} - -uptr StackTrace::GetCurrentPc() { - return GET_CALLER_PC(); -} - -void StackTrace::FastUnwindStack(uptr pc, uptr bp, - uptr stack_top, uptr stack_bottom) { - CHECK(size == 0 && trace[0] == pc); - size = 1; - uptr *frame = (uptr*)bp; - uptr *prev_frame = frame; - while (frame >= prev_frame && - frame < (uptr*)stack_top - 2 && - frame > (uptr*)stack_bottom && - size < max_size) { - uptr pc1 = frame[1]; - if (pc1 != pc) { - trace[size++] = pc1; - } - prev_frame = frame; - frame = (uptr*)frame[0]; - } -} - -// On 32-bits we don't compress stack traces. -// On 64-bits we compress stack traces: if a given pc differes slightly from -// the previous one, we record a 31-bit offset instead of the full pc. -SANITIZER_INTERFACE_ATTRIBUTE -uptr StackTrace::CompressStack(StackTrace *stack, u32 *compressed, uptr size) { -#if __WORDSIZE == 32 - // Don't compress, just copy. - uptr res = 0; - for (uptr i = 0; i < stack->size && i < size; i++) { - compressed[i] = stack->trace[i]; - res++; - } - if (stack->size < size) - compressed[stack->size] = 0; -#else // 64 bits, compress. - uptr prev_pc = 0; - const uptr kMaxOffset = (1ULL << 30) - 1; - uptr c_index = 0; - uptr res = 0; - for (uptr i = 0, n = stack->size; i < n; i++) { - uptr pc = stack->trace[i]; - if (!pc) break; - if ((s64)pc < 0) break; - // Printf("C pc[%zu] %zx\n", i, pc); - if (prev_pc - pc < kMaxOffset || pc - prev_pc < kMaxOffset) { - uptr offset = (s64)(pc - prev_pc); - offset |= (1U << 31); - if (c_index >= size) break; - // Printf("C co[%zu] offset %zx\n", i, offset); - compressed[c_index++] = offset; - } else { - uptr hi = pc >> 32; - uptr lo = (pc << 32) >> 32; - CHECK((hi & (1 << 31)) == 0); - if (c_index + 1 >= size) break; - // Printf("C co[%zu] hi/lo: %zx %zx\n", c_index, hi, lo); - compressed[c_index++] = hi; - compressed[c_index++] = lo; - } - res++; - prev_pc = pc; - } - if (c_index < size) - compressed[c_index] = 0; - if (c_index + 1 < size) - compressed[c_index + 1] = 0; -#endif // __WORDSIZE - - // debug-only code -#if 0 - StackTrace check_stack; - UncompressStack(&check_stack, compressed, size); - if (res < check_stack.size) { - Printf("res %zu check_stack.size %zu; c_size %zu\n", res, - check_stack.size, size); - } - // |res| may be greater than check_stack.size, because - // UncompressStack(CompressStack(stack)) eliminates the 0x0 frames. - CHECK(res >= check_stack.size); - CHECK(0 == REAL(memcmp)(check_stack.trace, stack->trace, - check_stack.size * sizeof(uptr))); -#endif - - return res; -} - -SANITIZER_INTERFACE_ATTRIBUTE -void StackTrace::UncompressStack(StackTrace *stack, - u32 *compressed, uptr size) { -#if __WORDSIZE == 32 - // Don't uncompress, just copy. - stack->size = 0; - for (uptr i = 0; i < size && i < kStackTraceMax; i++) { - if (!compressed[i]) break; - stack->size++; - stack->trace[i] = compressed[i]; - } -#else // 64 bits, uncompress - uptr prev_pc = 0; - stack->size = 0; - for (uptr i = 0; i < size && stack->size < kStackTraceMax; i++) { - u32 x = compressed[i]; - uptr pc = 0; - if (x & (1U << 31)) { - // Printf("U co[%zu] offset: %x\n", i, x); - // this is an offset - s32 offset = x; - offset = (offset << 1) >> 1; // remove the 31-byte and sign-extend. - pc = prev_pc + offset; - CHECK(pc); - } else { - // CHECK(i + 1 < size); - if (i + 1 >= size) break; - uptr hi = x; - uptr lo = compressed[i+1]; - // Printf("U co[%zu] hi/lo: %zx %zx\n", i, hi, lo); - i++; - pc = (hi << 32) | lo; - if (!pc) break; - } - // Printf("U pc[%zu] %zx\n", stack->size, pc); - stack->trace[stack->size++] = pc; - prev_pc = pc; - } -#endif // __WORDSIZE -} - void PrintStack(StackTrace *stack) { stack->PrintStack(stack->trace, stack->size, flags()->symbolize, flags()->strip_path_prefix, symbolize_callback); diff --git a/compiler-rt/lib/asan/asan_stack.h b/compiler-rt/lib/asan/asan_stack.h index 2c72503177c9..99f2cac1485b 100644 --- a/compiler-rt/lib/asan/asan_stack.h +++ b/compiler-rt/lib/asan/asan_stack.h @@ -14,69 +14,15 @@ #ifndef ASAN_STACK_H #define ASAN_STACK_H -#include "asan_internal.h" +#include "sanitizer_common/sanitizer_stacktrace.h" namespace __asan { -static const uptr kStackTraceMax = 64; - -struct StackTrace { - typedef bool (*SymbolizeCallback)(const void *pc, char *out_buffer, - int out_size); - uptr size; - uptr max_size; - uptr trace[kStackTraceMax]; - static void PrintStack(uptr *addr, uptr size, - bool symbolize, const char *strip_file_prefix, - SymbolizeCallback symbolize_callback); - void CopyTo(uptr *dst, uptr dst_size) { - for (uptr i = 0; i < size && i < dst_size; i++) - dst[i] = trace[i]; - for (uptr i = size; i < dst_size; i++) - dst[i] = 0; - } - - void CopyFrom(uptr *src, uptr src_size) { - size = src_size; - if (size > kStackTraceMax) size = kStackTraceMax; - for (uptr i = 0; i < size; i++) { - trace[i] = src[i]; - } - } - - void FastUnwindStack(uptr pc, uptr bp, uptr stack_top, uptr stack_bottom); - - static uptr GetCurrentPc(); - - static uptr CompressStack(StackTrace *stack, - u32 *compressed, uptr size); - static void UncompressStack(StackTrace *stack, - u32 *compressed, uptr size); -}; - void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp); void PrintStack(StackTrace *stack); - - } // namespace __asan -// Use this macro if you want to print stack trace with the caller -// of the current function in the top frame. -#define GET_CALLER_PC_BP_SP \ - uptr bp = GET_CURRENT_FRAME(); \ - uptr pc = GET_CALLER_PC(); \ - uptr local_stack; \ - uptr sp = (uptr)&local_stack - -// Use this macro if you want to print stack trace with the current -// function in the top frame. -#define GET_CURRENT_PC_BP_SP \ - uptr bp = GET_CURRENT_FRAME(); \ - uptr pc = StackTrace::GetCurrentPc(); \ - uptr local_stack; \ - uptr sp = (uptr)&local_stack - // Get the stack trace with the given pc and bp. // The pc will be in the position 0 of the resulting stack trace. // The bp may refer to the current frame or to the caller's frame. diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt index d8ea7c8a74e1..c4f3b4825688 100644 --- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt +++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt @@ -10,6 +10,7 @@ set(SANITIZER_SOURCES sanitizer_mac.cc sanitizer_posix.cc sanitizer_printf.cc + sanitizer_stacktrace.cc sanitizer_symbolizer.cc sanitizer_symbolizer_linux.cc sanitizer_symbolizer_mac.cc diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h index 0c709fed17d7..82bf3fbf1a9b 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h @@ -164,4 +164,15 @@ void NORETURN CheckFailed(const char *file, int line, const char *cond, enum LinkerInitialized { LINKER_INITIALIZED = 0 }; +#if !defined(_WIN32) || defined(__clang__) +# define GET_CALLER_PC() (uptr)__builtin_return_address(0) +# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) +#else +# define GET_CALLER_PC() (uptr)_ReturnAddress() +// CaptureStackBackTrace doesn't need to know BP on Windows. +// FIXME: This macro is still used when printing error reports though it's not +// clear if the BP value is needed in the ASan reports on Windows. +# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF +#endif + #endif // SANITIZER_DEFS_H