asan_posix.cc revision 296417
1294190Sdes//===-- asan_posix.cc -----------------------------------------------------===// 2238106Sdes// 3238106Sdes// The LLVM Compiler Infrastructure 4238106Sdes// 5238106Sdes// This file is distributed under the University of Illinois Open Source 6238106Sdes// License. See LICENSE.TXT for details. 7238106Sdes// 8238106Sdes//===----------------------------------------------------------------------===// 9238106Sdes// 10238106Sdes// This file is a part of AddressSanitizer, an address sanity checker. 11238106Sdes// 12238106Sdes// Posix-specific details. 13238106Sdes//===----------------------------------------------------------------------===// 14238106Sdes 15238106Sdes#include "sanitizer_common/sanitizer_platform.h" 16238106Sdes#if SANITIZER_POSIX 17238106Sdes 18238106Sdes#include "asan_internal.h" 19238106Sdes#include "asan_interceptors.h" 20238106Sdes#include "asan_mapping.h" 21238106Sdes#include "asan_report.h" 22238106Sdes#include "asan_stack.h" 23238106Sdes#include "sanitizer_common/sanitizer_libc.h" 24238106Sdes#include "sanitizer_common/sanitizer_posix.h" 25238106Sdes#include "sanitizer_common/sanitizer_procmaps.h" 26238106Sdes 27285206Sdes#include <pthread.h> 28238106Sdes#include <signal.h> 29238106Sdes#include <stdlib.h> 30238106Sdes#include <sys/time.h> 31238106Sdes#include <sys/resource.h> 32238106Sdes#include <unistd.h> 33238106Sdes 34285206Sdesnamespace __asan { 35238106Sdes 36238106Sdesvoid AsanOnDeadlySignal(int signo, void *siginfo, void *context) { 37238106Sdes ScopedDeadlySignal signal_scope(GetCurrentThread()); 38238106Sdes int code = (int)((siginfo_t*)siginfo)->si_code; 39238106Sdes // Write the first message using the bullet-proof write. 40238106Sdes if (18 != internal_write(2, "ASAN:DEADLYSIGNAL\n", 18)) Die(); 41238106Sdes SignalContext sig = SignalContext::Create(siginfo, context); 42238106Sdes 43238106Sdes // Access at a reasonable offset above SP, or slightly below it (to account 44238106Sdes // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is 45238106Sdes // probably a stack overflow. 46238106Sdes bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF; 47249141Sdes 48238106Sdes#if __powerpc__ 49238106Sdes // Large stack frames can be allocated with e.g. 50238106Sdes // lis r0,-10000 51238106Sdes // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000 52238106Sdes // If the store faults then sp will not have been updated, so test above 53238106Sdes // will not work, becase the fault address will be more than just "slightly" 54238106Sdes // below sp. 55238106Sdes if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) { 56238106Sdes u32 inst = *(unsigned *)sig.pc; 57238106Sdes u32 ra = (inst >> 16) & 0x1F; 58238106Sdes u32 opcd = inst >> 26; 59238106Sdes u32 xo = (inst >> 1) & 0x3FF; 60238106Sdes // Check for store-with-update to sp. The instructions we accept are: 61238106Sdes // stbu rs,d(ra) stbux rs,ra,rb 62238106Sdes // sthu rs,d(ra) sthux rs,ra,rb 63238106Sdes // stwu rs,d(ra) stwux rs,ra,rb 64238106Sdes // stdu rs,ds(ra) stdux rs,ra,rb 65238106Sdes // where ra is r1 (the stack pointer). 66238106Sdes if (ra == 1 && 67238106Sdes (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 || 68238106Sdes (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181)))) 69238106Sdes IsStackAccess = true; 70238106Sdes } 71238106Sdes#endif // __powerpc__ 72238106Sdes 73238106Sdes // We also check si_code to filter out SEGV caused by something else other 74238106Sdes // then hitting the guard page or unmapped memory, like, for example, 75238106Sdes // unaligned memory access. 76238106Sdes if (IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR)) 77238106Sdes ReportStackOverflow(sig); 78238106Sdes else if (signo == SIGFPE) 79249141Sdes ReportDeadlySignal("FPE", sig); 80249141Sdes else if (signo == SIGILL) 81249141Sdes ReportDeadlySignal("ILL", sig); 82249141Sdes else 83249141Sdes ReportDeadlySignal("SEGV", sig); 84238106Sdes} 85238106Sdes 86238106Sdes// ---------------------- TSD ---------------- {{{1 87238106Sdes 88238106Sdesstatic pthread_key_t tsd_key; 89238106Sdesstatic bool tsd_key_inited = false; 90238106Sdesvoid AsanTSDInit(void (*destructor)(void *tsd)) { 91238106Sdes CHECK(!tsd_key_inited); 92238106Sdes tsd_key_inited = true; 93238106Sdes CHECK_EQ(0, pthread_key_create(&tsd_key, destructor)); 94238106Sdes} 95238106Sdes 96238106Sdesvoid *AsanTSDGet() { 97238106Sdes CHECK(tsd_key_inited); 98238106Sdes return pthread_getspecific(tsd_key); 99238106Sdes} 100238106Sdes 101238106Sdesvoid AsanTSDSet(void *tsd) { 102238106Sdes CHECK(tsd_key_inited); 103238106Sdes pthread_setspecific(tsd_key, tsd); 104238106Sdes} 105238106Sdes 106238106Sdesvoid PlatformTSDDtor(void *tsd) { 107238106Sdes AsanThreadContext *context = (AsanThreadContext*)tsd; 108238106Sdes if (context->destructor_iterations > 1) { 109238106Sdes context->destructor_iterations--; 110238106Sdes CHECK_EQ(0, pthread_setspecific(tsd_key, tsd)); 111238106Sdes return; 112238106Sdes } 113238106Sdes AsanThread::TSDDtor(tsd); 114238106Sdes} 115238106Sdes} // namespace __asan 116238106Sdes 117238106Sdes#endif // SANITIZER_POSIX 118238106Sdes