asan_posix.cc revision 276789
1//===-- asan_posix.cc -----------------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12// Posix-specific details. 13//===----------------------------------------------------------------------===// 14 15#include "sanitizer_common/sanitizer_platform.h" 16#if SANITIZER_POSIX 17 18#include "asan_internal.h" 19#include "asan_interceptors.h" 20#include "asan_mapping.h" 21#include "asan_report.h" 22#include "asan_stack.h" 23#include "sanitizer_common/sanitizer_libc.h" 24#include "sanitizer_common/sanitizer_procmaps.h" 25 26#include <pthread.h> 27#include <signal.h> 28#include <stdlib.h> 29#include <sys/time.h> 30#include <sys/resource.h> 31#include <unistd.h> 32 33namespace __asan { 34 35SignalContext SignalContext::Create(void *siginfo, void *context) { 36 uptr addr = (uptr)((siginfo_t*)siginfo)->si_addr; 37 uptr pc, sp, bp; 38 GetPcSpBp(context, &pc, &sp, &bp); 39 return SignalContext(context, addr, pc, sp, bp); 40} 41 42void AsanOnSIGSEGV(int, void *siginfo, void *context) { 43 ScopedDeadlySignal signal_scope(GetCurrentThread()); 44 int code = (int)((siginfo_t*)siginfo)->si_code; 45 // Write the first message using the bullet-proof write. 46 if (13 != internal_write(2, "ASAN:SIGSEGV\n", 13)) Die(); 47 SignalContext sig = SignalContext::Create(siginfo, context); 48 49 // Access at a reasonable offset above SP, or slightly below it (to account 50 // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is 51 // probably a stack overflow. 52 bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF; 53 54#if __powerpc__ 55 // Large stack frames can be allocated with e.g. 56 // lis r0,-10000 57 // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000 58 // If the store faults then sp will not have been updated, so test above 59 // will not work, becase the fault address will be more than just "slightly" 60 // below sp. 61 if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) { 62 u32 inst = *(unsigned *)sig.pc; 63 u32 ra = (inst >> 16) & 0x1F; 64 u32 opcd = inst >> 26; 65 u32 xo = (inst >> 1) & 0x3FF; 66 // Check for store-with-update to sp. The instructions we accept are: 67 // stbu rs,d(ra) stbux rs,ra,rb 68 // sthu rs,d(ra) sthux rs,ra,rb 69 // stwu rs,d(ra) stwux rs,ra,rb 70 // stdu rs,ds(ra) stdux rs,ra,rb 71 // where ra is r1 (the stack pointer). 72 if (ra == 1 && 73 (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 || 74 (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181)))) 75 IsStackAccess = true; 76 } 77#endif // __powerpc__ 78 79 // We also check si_code to filter out SEGV caused by something else other 80 // then hitting the guard page or unmapped memory, like, for example, 81 // unaligned memory access. 82 if (IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR)) 83 ReportStackOverflow(sig); 84 else 85 ReportSIGSEGV("SEGV", sig); 86} 87 88// ---------------------- TSD ---------------- {{{1 89 90static pthread_key_t tsd_key; 91static bool tsd_key_inited = false; 92void AsanTSDInit(void (*destructor)(void *tsd)) { 93 CHECK(!tsd_key_inited); 94 tsd_key_inited = true; 95 CHECK_EQ(0, pthread_key_create(&tsd_key, destructor)); 96} 97 98void *AsanTSDGet() { 99 CHECK(tsd_key_inited); 100 return pthread_getspecific(tsd_key); 101} 102 103void AsanTSDSet(void *tsd) { 104 CHECK(tsd_key_inited); 105 pthread_setspecific(tsd_key, tsd); 106} 107 108void PlatformTSDDtor(void *tsd) { 109 AsanThreadContext *context = (AsanThreadContext*)tsd; 110 if (context->destructor_iterations > 1) { 111 context->destructor_iterations--; 112 CHECK_EQ(0, pthread_setspecific(tsd_key, tsd)); 113 return; 114 } 115 AsanThread::TSDDtor(tsd); 116} 117} // namespace __asan 118 119#endif // SANITIZER_POSIX 120