2012-01-10 03:18:27 +08:00
|
|
|
//===-- asan_linux.cc -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#if SANITIZER_LINUX || SANITIZER_MAC
|
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
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough.
|
2012-04-05 18:54:52 +08:00
|
|
|
|
2012-01-10 03:18:27 +08:00
|
|
|
namespace __asan {
|
|
|
|
|
|
|
|
static void MaybeInstallSigaction(int signum,
|
|
|
|
void (*handler)(int, siginfo_t *, void *)) {
|
|
|
|
if (!AsanInterceptsSignal(signum))
|
|
|
|
return;
|
|
|
|
struct sigaction sigact;
|
2012-02-08 21:45:31 +08:00
|
|
|
REAL(memset)(&sigact, 0, sizeof(sigact));
|
2012-01-10 03:18:27 +08:00
|
|
|
sigact.sa_sigaction = handler;
|
|
|
|
sigact.sa_flags = SA_SIGINFO;
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK_EQ(0, REAL(sigaction)(signum, &sigact, 0));
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->verbosity >= 1) {
|
2012-05-30 23:29:11 +08:00
|
|
|
Report("Installed the sigaction for signal %d\n", signum);
|
|
|
|
}
|
2012-01-10 03:18:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr addr = (uptr)siginfo->si_addr;
|
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);
|
2012-08-09 15:40:58 +08:00
|
|
|
ReportSIGSEGV(pc, sp, bp, addr);
|
2012-01-10 03:18:27 +08:00
|
|
|
}
|
|
|
|
|
2012-04-05 18:54:52 +08:00
|
|
|
void SetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK_EQ(0, sigaltstack(0, &oldstack));
|
2012-04-05 18:54:52 +08:00
|
|
|
// If the alternate stack is already in place, do nothing.
|
|
|
|
if ((oldstack.ss_flags & SS_DISABLE) == 0) return;
|
|
|
|
// TODO(glider): the mapped stack should have the MAP_STACK flag in the
|
|
|
|
// future. It is not required by man 2 sigaltstack now (they're using
|
|
|
|
// malloc()).
|
2012-06-07 00:15:07 +08:00
|
|
|
void* base = MmapOrDie(kAltStackSize, __FUNCTION__);
|
2012-04-05 18:54:52 +08:00
|
|
|
altstack.ss_sp = base;
|
|
|
|
altstack.ss_flags = 0;
|
|
|
|
altstack.ss_size = kAltStackSize;
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK_EQ(0, sigaltstack(&altstack, 0));
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->verbosity > 0) {
|
2012-04-05 18:54:52 +08:00
|
|
|
Report("Alternative stack for T%d set: [%p,%p)\n",
|
2013-03-20 17:23:28 +08:00
|
|
|
GetCurrentTidOrInvalid(),
|
2012-04-05 18:54:52 +08:00
|
|
|
altstack.ss_sp, (char*)altstack.ss_sp + altstack.ss_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnsetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2012-05-31 22:35:53 +08:00
|
|
|
altstack.ss_sp = 0;
|
2012-04-05 18:54:52 +08:00
|
|
|
altstack.ss_flags = SS_DISABLE;
|
|
|
|
altstack.ss_size = 0;
|
2013-04-05 22:40:25 +08:00
|
|
|
CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
|
2012-06-07 00:15:07 +08:00
|
|
|
UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
|
2012-04-05 18:54:52 +08:00
|
|
|
}
|
|
|
|
|
2012-01-10 03:18:27 +08:00
|
|
|
void InstallSignalHandlers() {
|
2012-04-05 18:54:52 +08:00
|
|
|
// Set the alternate signal stack for the main thread.
|
|
|
|
// This will cause SetAlternateSignalStack to be called twice, but the stack
|
|
|
|
// will be actually set only once.
|
2012-07-09 22:36:04 +08:00
|
|
|
if (flags()->use_sigaltstack) SetAlternateSignalStack();
|
2012-01-10 03:18:27 +08:00
|
|
|
MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
|
|
|
|
MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-01-10 03:18:27 +08:00
|
|
|
} // namespace __asan
|
|
|
|
|
2013-04-03 15:29:53 +08:00
|
|
|
#endif // SANITIZER_LINUX || SANITIZER_MAC
|