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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
|
|
|
|
|
|
#include "asan_internal.h"
|
|
|
|
#include "asan_interceptors.h"
|
|
|
|
#include "asan_stack.h"
|
|
|
|
#include "asan_thread_registry.h"
|
|
|
|
|
2012-01-11 10:21:06 +08:00
|
|
|
#include <pthread.h>
|
2012-01-10 03:18:27 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#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-01-11 10:39:16 +08:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <sys/atomics.h>
|
|
|
|
#endif
|
|
|
|
|
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-02-08 21:45:31 +08:00
|
|
|
CHECK(0 == REAL(sigaction)(signum, &sigact, 0));
|
2012-01-10 03:18:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
|
|
|
|
uintptr_t addr = (uintptr_t)siginfo->si_addr;
|
|
|
|
// Write the first message using the bullet-proof write.
|
2012-01-10 07:11:26 +08:00
|
|
|
if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie();
|
2012-01-10 03:18:27 +08:00
|
|
|
uintptr_t pc, sp, bp;
|
|
|
|
GetPcSpBp(context, &pc, &sp, &bp);
|
|
|
|
Report("ERROR: AddressSanitizer crashed on unknown address %p"
|
|
|
|
" (pc %p sp %p bp %p T%d)\n",
|
|
|
|
addr, pc, sp, bp,
|
|
|
|
asanThreadRegistry().GetCurrentTidOrMinusOne());
|
|
|
|
Printf("AddressSanitizer can not provide additional info. ABORTING\n");
|
2012-01-19 19:34:18 +08:00
|
|
|
GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, pc, bp);
|
2012-01-10 03:18:27 +08:00
|
|
|
stack.PrintStack();
|
|
|
|
ShowStatsAndAbort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InstallSignalHandlers() {
|
|
|
|
MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
|
|
|
|
MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsanDisableCoreDumper() {
|
|
|
|
struct rlimit nocore;
|
|
|
|
nocore.rlim_cur = 0;
|
|
|
|
nocore.rlim_max = 0;
|
|
|
|
setrlimit(RLIMIT_CORE, &nocore);
|
|
|
|
}
|
|
|
|
|
2012-01-10 07:11:26 +08:00
|
|
|
int GetPid() {
|
|
|
|
return getpid();
|
|
|
|
}
|
|
|
|
|
2012-01-11 10:21:06 +08:00
|
|
|
uintptr_t GetThreadSelf() {
|
|
|
|
return (uintptr_t)pthread_self();
|
|
|
|
}
|
|
|
|
|
2012-02-14 05:24:29 +08:00
|
|
|
void SleepForSeconds(int seconds) {
|
|
|
|
sleep(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Exit(int exitcode) {
|
|
|
|
return _exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2012-01-11 10:39:16 +08:00
|
|
|
int AtomicInc(int *a) {
|
|
|
|
#ifdef ANDROID
|
|
|
|
return __atomic_inc(a) + 1;
|
|
|
|
#else
|
|
|
|
return __sync_add_and_fetch(a, 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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;
|
2012-02-07 08:27:15 +08:00
|
|
|
CHECK(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
|
|
|
|
|
|
|
|
#endif // __linux__ || __APPLE_
|