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