blob: 9e01bcd091bf62192390274a8d7475c33a16e56e [file] [log] [blame]
Sergey Matveeve86e35f2013-10-14 12:01:05 +00001//===-- asan_posix.cc -----------------------------------------------------===//
Kostya Serebryanya7e760a2012-01-09 19:18:27 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Posix-specific details.
13//===----------------------------------------------------------------------===//
Evgeniy Stepanov24e13722013-03-19 14:33:38 +000014
15#include "sanitizer_common/sanitizer_platform.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016#if SANITIZER_POSIX
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000017
18#include "asan_internal.h"
19#include "asan_interceptors.h"
Evgeniy Stepanovc99f7002012-05-23 15:21:50 +000020#include "asan_mapping.h"
Alexey Samsonov73545092012-08-09 07:40:58 +000021#include "asan_report.h"
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000022#include "asan_stack.h"
Alexey Samsonov2221f552012-06-05 08:48:10 +000023#include "sanitizer_common/sanitizer_libc.h"
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070024#include "sanitizer_common/sanitizer_posix.h"
Alexey Samsonov6895adc2012-06-07 06:15:12 +000025#include "sanitizer_common/sanitizer_procmaps.h"
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000026
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000027#include <pthread.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000028#include <signal.h>
Alexey Samsonovb823e3c2012-02-22 14:07:06 +000029#include <stdlib.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000030#include <sys/time.h>
31#include <sys/resource.h>
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000032#include <unistd.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000033
34namespace __asan {
35
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080036void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
Stephen Hines6d186232014-11-26 17:56:19 -080037 ScopedDeadlySignal signal_scope(GetCurrentThread());
Stephen Hines2d1fdb22014-05-28 23:58:16 -070038 int code = (int)((siginfo_t*)siginfo)->si_code;
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000039 // Write the first message using the bullet-proof write.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080040 if (18 != internal_write(2, "ASAN:DEADLYSIGNAL\n", 18)) Die();
Stephen Hines86277eb2015-03-23 12:06:32 -070041 SignalContext sig = SignalContext::Create(siginfo, context);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000042
Stephen Hines2d1fdb22014-05-28 23:58:16 -070043 // Access at a reasonable offset above SP, or slightly below it (to account
Stephen Hines6d186232014-11-26 17:56:19 -080044 // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
45 // probably a stack overflow.
Stephen Hines86277eb2015-03-23 12:06:32 -070046 bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF;
Stephen Hines6d186232014-11-26 17:56:19 -080047
48#if __powerpc__
49 // Large stack frames can be allocated with e.g.
50 // lis r0,-10000
51 // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
52 // If the store faults then sp will not have been updated, so test above
53 // will not work, becase the fault address will be more than just "slightly"
54 // below sp.
Stephen Hines86277eb2015-03-23 12:06:32 -070055 if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) {
56 u32 inst = *(unsigned *)sig.pc;
Stephen Hines6d186232014-11-26 17:56:19 -080057 u32 ra = (inst >> 16) & 0x1F;
58 u32 opcd = inst >> 26;
59 u32 xo = (inst >> 1) & 0x3FF;
60 // Check for store-with-update to sp. The instructions we accept are:
61 // stbu rs,d(ra) stbux rs,ra,rb
62 // sthu rs,d(ra) sthux rs,ra,rb
63 // stwu rs,d(ra) stwux rs,ra,rb
64 // stdu rs,ds(ra) stdux rs,ra,rb
65 // where ra is r1 (the stack pointer).
66 if (ra == 1 &&
67 (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
68 (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
69 IsStackAccess = true;
70 }
71#endif // __powerpc__
72
Stephen Hines2d1fdb22014-05-28 23:58:16 -070073 // We also check si_code to filter out SEGV caused by something else other
74 // then hitting the guard page or unmapped memory, like, for example,
75 // unaligned memory access.
Stephen Hines6d186232014-11-26 17:56:19 -080076 if (IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR))
Stephen Hines86277eb2015-03-23 12:06:32 -070077 ReportStackOverflow(sig);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080078 else if (signo == SIGFPE)
79 ReportDeadlySignal("FPE", sig);
80 else if (signo == SIGILL)
81 ReportDeadlySignal("ILL", sig);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070082 else
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080083 ReportDeadlySignal("SEGV", sig);
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000084}
85
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000086// ---------------------- TSD ---------------- {{{1
87
88static pthread_key_t tsd_key;
89static bool tsd_key_inited = false;
Kostya Serebryanyf58f9982012-02-07 00:27:15 +000090void AsanTSDInit(void (*destructor)(void *tsd)) {
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000091 CHECK(!tsd_key_inited);
92 tsd_key_inited = true;
Kostya Serebryanya27bdf72013-04-05 14:40:25 +000093 CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
Kostya Serebryanycc4e6862012-01-11 02:21:06 +000094}
95
96void *AsanTSDGet() {
97 CHECK(tsd_key_inited);
98 return pthread_getspecific(tsd_key);
99}
100
101void AsanTSDSet(void *tsd) {
102 CHECK(tsd_key_inited);
103 pthread_setspecific(tsd_key, tsd);
104}
105
Sergey Matveeve86e35f2013-10-14 12:01:05 +0000106void PlatformTSDDtor(void *tsd) {
107 AsanThreadContext *context = (AsanThreadContext*)tsd;
108 if (context->destructor_iterations > 1) {
109 context->destructor_iterations--;
110 CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
111 return;
112 }
113 AsanThread::TSDDtor(tsd);
114}
Kostya Serebryanya7e760a2012-01-09 19:18:27 +0000115} // namespace __asan
116
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700117#endif // SANITIZER_POSIX