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"
|
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 {
|
|
|
|
|
2014-01-31 21:10:07 +08:00
|
|
|
void AsanOnSIGSEGV(int, void *siginfo, void *context) {
|
|
|
|
uptr addr = (uptr)((siginfo_t*)siginfo)->si_addr;
|
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.
|
2012-06-06 15:02:44 +08:00
|
|
|
if (13 != internal_write(2, "ASAN:SIGSEGV\n", 13)) Die();
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr pc, sp, bp;
|
2012-01-10 03:18:27 +08:00
|
|
|
GetPcSpBp(context, &pc, &sp, &bp);
|
2014-02-19 21:40:41 +08:00
|
|
|
|
|
|
|
// Access at a reasonable offset above SP, or slightly below it (to account
|
|
|
|
// for x86_64 redzone, ARM push of multiple registers, etc) is probably a
|
|
|
|
// stack overflow.
|
|
|
|
// 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.
|
|
|
|
if (addr + 128 > sp && addr < sp + 0xFFFF &&
|
|
|
|
(code == si_SEGV_MAPERR || code == si_SEGV_ACCERR))
|
|
|
|
ReportStackOverflow(pc, sp, bp, context, addr);
|
|
|
|
else
|
|
|
|
ReportSIGSEGV(pc, sp, bp, context, addr);
|
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
|