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