2013-10-14 20:01:05 +08:00
|
|
|
//===-- asan_posix.cc -----------------------------------------------------===//
|
2012-01-10 03:18:27 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Posix-specific details.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-03-19 22:33:38 +08:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_platform.h"
|
2014-03-04 16:55:41 +08:00
|
|
|
#if SANITIZER_POSIX
|
2012-01-10 03:18:27 +08:00
|
|
|
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_interceptors.h"
|
2012-05-23 23:21:50 +08:00
|
|
|
#include "asan_mapping.h"
|
2012-08-09 15:40:58 +08:00
|
|
|
#include "asan_report.h"
|
2012-01-10 03:18:27 +08:00
|
|
|
#include "asan_stack.h"
|
2012-06-05 16:48:10 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
2015-04-09 22:11:25 +08:00
|
|
|
#include "sanitizer_common/sanitizer_posix.h"
|
2012-06-07 14:15:12 +08:00
|
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
2012-01-10 03:18:27 +08:00
|
|
|
|
2012-01-11 10:21:06 +08:00
|
|
|
#include <pthread.h>
|
2012-01-10 03:18:27 +08:00
|
|
|
#include <signal.h>
|
2012-02-22 22:07:06 +08:00
|
|
|
#include <stdlib.h>
|
2012-01-10 03:18:27 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2012-01-10 07:11:26 +08:00
|
|
|
#include <unistd.h>
|
2012-01-10 03:18:27 +08:00
|
|
|
|
|
|
|
namespace __asan {
|
|
|
|
|
2015-08-07 01:52:54 +08:00
|
|
|
void AsanOnDeadlySignal(int signo, void *siginfo, void *context) {
|
2014-11-10 23:22:04 +08:00
|
|
|
ScopedDeadlySignal signal_scope(GetCurrentThread());
|
2014-02-19 21:40:41 +08:00
|
|
|
int code = (int)((siginfo_t*)siginfo)->si_code;
|
2012-01-10 03:18:27 +08:00
|
|
|
// Write the first message using the bullet-proof write.
|
2015-08-07 01:52:54 +08:00
|
|
|
if (18 != internal_write(2, "ASAN:DEADLYSIGNAL\n", 18)) Die();
|
2014-11-25 21:00:21 +08:00
|
|
|
SignalContext sig = SignalContext::Create(siginfo, context);
|
2014-02-19 21:40:41 +08:00
|
|
|
|
|
|
|
// Access at a reasonable offset above SP, or slightly below it (to account
|
2014-11-08 17:51:45 +08:00
|
|
|
// for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
|
|
|
|
// probably a stack overflow.
|
2014-11-25 21:00:21 +08:00
|
|
|
bool IsStackAccess = sig.addr + 512 > sig.sp && sig.addr < sig.sp + 0xFFFF;
|
2014-11-14 23:30:39 +08:00
|
|
|
|
2014-11-15 21:52:10 +08:00
|
|
|
#if __powerpc__
|
2014-11-14 23:30:39 +08:00
|
|
|
// Large stack frames can be allocated with e.g.
|
|
|
|
// lis r0,-10000
|
|
|
|
// stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
|
|
|
|
// If the store faults then sp will not have been updated, so test above
|
|
|
|
// will not work, becase the fault address will be more than just "slightly"
|
|
|
|
// below sp.
|
2014-11-25 21:00:21 +08:00
|
|
|
if (!IsStackAccess && IsAccessibleMemoryRange(sig.pc, 4)) {
|
|
|
|
u32 inst = *(unsigned *)sig.pc;
|
2014-11-14 23:30:39 +08:00
|
|
|
u32 ra = (inst >> 16) & 0x1F;
|
|
|
|
u32 opcd = inst >> 26;
|
|
|
|
u32 xo = (inst >> 1) & 0x3FF;
|
|
|
|
// Check for store-with-update to sp. The instructions we accept are:
|
|
|
|
// stbu rs,d(ra) stbux rs,ra,rb
|
|
|
|
// sthu rs,d(ra) sthux rs,ra,rb
|
|
|
|
// stwu rs,d(ra) stwux rs,ra,rb
|
|
|
|
// stdu rs,ds(ra) stdux rs,ra,rb
|
|
|
|
// where ra is r1 (the stack pointer).
|
|
|
|
if (ra == 1 &&
|
|
|
|
(opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 ||
|
|
|
|
(opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181))))
|
|
|
|
IsStackAccess = true;
|
|
|
|
}
|
2014-11-15 21:52:10 +08:00
|
|
|
#endif // __powerpc__
|
2014-11-14 23:30:39 +08:00
|
|
|
|
2014-02-19 21:40:41 +08:00
|
|
|
// We also check si_code to filter out SEGV caused by something else other
|
|
|
|
// then hitting the guard page or unmapped memory, like, for example,
|
|
|
|
// unaligned memory access.
|
2014-11-14 23:30:39 +08:00
|
|
|
if (IsStackAccess && (code == si_SEGV_MAPERR || code == si_SEGV_ACCERR))
|
2014-11-25 21:00:21 +08:00
|
|
|
ReportStackOverflow(sig);
|
2015-08-07 01:52:54 +08:00
|
|
|
else if (signo == SIGFPE)
|
|
|
|
ReportDeadlySignal("FPE", sig);
|
2014-02-19 21:40:41 +08:00
|
|
|
else
|
2015-08-07 01:52:54 +08:00
|
|
|
ReportDeadlySignal("SEGV", sig);
|
2012-01-10 03:18:27 +08:00
|
|
|
}
|
|
|
|
|
2012-01-11 10:21:06 +08:00
|
|
|
// ---------------------- TSD ---------------- {{{1
|
|
|
|
|
|
|
|
static pthread_key_t tsd_key;
|
|
|
|
static bool tsd_key_inited = false;
|
2012-02-07 08:27:15 +08:00
|
|
|
void AsanTSDInit(void (*destructor)(void *tsd)) {
|
2012-01-11 10:21:06 +08:00
|
|
|
CHECK(!tsd_key_inited);
|
|
|
|
tsd_key_inited = true;
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK_EQ(0, pthread_key_create(&tsd_key, destructor));
|
2012-01-11 10:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *AsanTSDGet() {
|
|
|
|
CHECK(tsd_key_inited);
|
|
|
|
return pthread_getspecific(tsd_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanTSDSet(void *tsd) {
|
|
|
|
CHECK(tsd_key_inited);
|
|
|
|
pthread_setspecific(tsd_key, tsd);
|
|
|
|
}
|
|
|
|
|
2013-10-14 20:01:05 +08:00
|
|
|
void PlatformTSDDtor(void *tsd) {
|
|
|
|
AsanThreadContext *context = (AsanThreadContext*)tsd;
|
|
|
|
if (context->destructor_iterations > 1) {
|
|
|
|
context->destructor_iterations--;
|
|
|
|
CHECK_EQ(0, pthread_setspecific(tsd_key, tsd));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AsanThread::TSDDtor(tsd);
|
|
|
|
}
|
2012-01-10 03:18:27 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
2014-03-04 16:55:41 +08:00
|
|
|
#endif // SANITIZER_POSIX
|