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