1 //===-- asan_posix.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // Posix-specific details.
13 //===----------------------------------------------------------------------===//
15 #include "sanitizer_common/sanitizer_platform.h"
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_posix.h"
25 #include "sanitizer_common/sanitizer_procmaps.h"
31 #include <sys/resource.h>
36 void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
37 ScopedDeadlySignal signal_scope(GetCurrentThread());
38 int code = (int)((siginfo_t*)siginfo)->si_code;
39 // Write the first message using fd=2, just in case.
40 // It may actually fail to write in case stderr is closed.
41 internal_write(2, "ASAN:DEADLYSIGNAL\n", 18);
42 SignalContext sig = SignalContext::Create(siginfo, context);
44 // Access at a reasonable offset above SP, or slightly below it (to account
45 // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
46 // probably a stack overflow.
48 // On s390, the fault address in siginfo points to start of the page, not
49 // to the precise word that was accessed. Mask off the low bits of sp to
50 // take it into account.
51 bool IsStackAccess = sig.addr >= (sig.sp & ~0xFFF) &&
52 sig.addr < sig.sp + 0xFFFF;
54 bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF;
58 // Large stack frames can be allocated with e.g.
60 // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
61 // If the store faults then sp will not have been updated, so test above
62 // will not work, becase the fault address will be more than just "slightly"
64 if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) {
65 u32 inst = *(unsigned *)sig.pc;
66 u32 ra = (inst >> 16) & 0x1F;
67 u32 opcd = inst >> 26;
68 u32 xo = (inst >> 1) & 0x3FF;
69 // Check for store-with-update to sp. The instructions we accept are:
70 // stbu rs,d(ra) stbux rs,ra,rb
71 // sthu rs,d(ra) sthux rs,ra,rb
72 // stwu rs,d(ra) stwux rs,ra,rb
73 // stdu rs,ds(ra) stdux rs,ra,rb
74 // where ra is r1 (the stack pointer).
76 (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
77 (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
82 // We also check si_code to filter out SEGV caused by something else other
83 // then hitting the guard page or unmapped memory, like, for example,
84 // unaligned memory access.
85 if (IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR))
86 ReportStackOverflow(sig);
87 else if (signo == SIGFPE)
88 ReportDeadlySignal("FPE", sig);
89 else if (signo == SIGILL)
90 ReportDeadlySignal("ILL", sig);
92 ReportDeadlySignal("SEGV", sig);
95 // ---------------------- TSD ---------------- {{{1
97 static pthread_key_t tsd_key;
98 static bool tsd_key_inited = false;
99 void AsanTSDInit(void (*destructor)(void *tsd)) {
100 CHECK(!tsd_key_inited);
101 tsd_key_inited = true;
102 CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
106 CHECK(tsd_key_inited);
107 return pthread_getspecific(tsd_key);
110 void AsanTSDSet(void *tsd) {
111 CHECK(tsd_key_inited);
112 pthread_setspecific(tsd_key, tsd);
115 void PlatformTSDDtor(void *tsd) {
116 AsanThreadContext *context = (AsanThreadContext*)tsd;
117 if (context->destructor_iterations > 1) {
118 context->destructor_iterations--;
119 CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
122 AsanThread::TSDDtor(tsd);
124 } // namespace __asan
126 #endif // SANITIZER_POSIX