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"
|
2012-05-23 23:21:50 +08:00
|
|
|
#include "asan_mapping.h"
|
2012-02-22 17:11:55 +08:00
|
|
|
#include "asan_procmaps.h"
|
2012-01-10 03:18:27 +08:00
|
|
|
#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>
|
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-01-11 10:39:16 +08:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <sys/atomics.h>
|
|
|
|
#endif
|
|
|
|
|
2012-03-10 09:30:01 +08:00
|
|
|
// Should not add dependency on libstdc++,
|
|
|
|
// since most of the stuff here is inlinable.
|
|
|
|
#include <algorithm>
|
|
|
|
|
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 {
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
|
|
|
|
uptr start2, uptr end2) {
|
2012-05-23 23:21:50 +08:00
|
|
|
CHECK(start1 <= end1);
|
|
|
|
CHECK(start2 <= end2);
|
|
|
|
return (end1 < start2) || (end2 < start1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this is thread-unsafe, but should not cause problems most of the time.
|
|
|
|
// When the shadow is mapped only a single thread usually exists (plus maybe
|
|
|
|
// several worker threads on Mac, which aren't expected to map big chunks of
|
|
|
|
// memory).
|
|
|
|
bool AsanShadowRangeIsAvailable() {
|
|
|
|
AsanProcMaps procmaps;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr start, end;
|
|
|
|
uptr shadow_start = kLowShadowBeg;
|
2012-05-23 23:21:50 +08:00
|
|
|
if (kLowShadowBeg > 0) shadow_start -= kMmapGranularity;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr shadow_end = kHighShadowEnd;
|
2012-05-23 23:21:50 +08:00
|
|
|
while (procmaps.Next(&start, &end,
|
2012-05-31 22:35:53 +08:00
|
|
|
/*offset*/0, /*filename*/0, /*filename_size*/0)) {
|
2012-05-23 23:21:50 +08:00
|
|
|
if (!IntervalsAreSeparate(start, end, shadow_start, shadow_end))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 03:18:27 +08:00
|
|
|
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-04-05 18:54:52 +08:00
|
|
|
if (FLAG_use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
|
2012-02-08 21:45:31 +08:00
|
|
|
CHECK(0 == REAL(sigaction)(signum, &sigact, 0));
|
2012-05-30 23:29:11 +08:00
|
|
|
if (FLAG_v >= 1) {
|
|
|
|
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-01-10 07:11:26 +08:00
|
|
|
if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie();
|
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);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-04-05 18:54:52 +08:00
|
|
|
void SetAlternateSignalStack() {
|
|
|
|
stack_t altstack, oldstack;
|
2012-05-31 22:35:53 +08:00
|
|
|
CHECK(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()).
|
|
|
|
void* base = AsanMmapSomewhereOrDie(kAltStackSize, __FUNCTION__);
|
|
|
|
altstack.ss_sp = base;
|
|
|
|
altstack.ss_flags = 0;
|
|
|
|
altstack.ss_size = kAltStackSize;
|
2012-05-31 22:35:53 +08:00
|
|
|
CHECK(0 == sigaltstack(&altstack, 0));
|
2012-04-05 18:54:52 +08:00
|
|
|
if (FLAG_v > 0) {
|
|
|
|
Report("Alternative stack for T%d set: [%p,%p)\n",
|
|
|
|
asanThreadRegistry().GetCurrentTidOrMinusOne(),
|
|
|
|
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;
|
|
|
|
CHECK(0 == sigaltstack(&altstack, &oldstack));
|
|
|
|
AsanUnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
if (FLAG_use_sigaltstack) SetAlternateSignalStack();
|
2012-01-10 03:18:27 +08:00
|
|
|
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-02-22 17:11:55 +08:00
|
|
|
void AsanDumpProcessMap() {
|
|
|
|
AsanProcMaps proc_maps;
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr start, end;
|
2012-05-31 23:02:07 +08:00
|
|
|
const sptr kBufSize = 4095;
|
2012-02-22 17:11:55 +08:00
|
|
|
char filename[kBufSize];
|
|
|
|
Report("Process memory map follows:\n");
|
2012-05-31 22:35:53 +08:00
|
|
|
while (proc_maps.Next(&start, &end, /* file_offset */0,
|
2012-02-22 17:11:55 +08:00
|
|
|
filename, kBufSize)) {
|
|
|
|
Printf("\t%p-%p\t%s\n", (void*)start, (void*)end, filename);
|
|
|
|
}
|
|
|
|
Report("End of process memory map.\n");
|
|
|
|
}
|
|
|
|
|
2012-01-10 07:11:26 +08:00
|
|
|
int GetPid() {
|
|
|
|
return getpid();
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
uptr GetThreadSelf() {
|
|
|
|
return (uptr)pthread_self();
|
2012-01-11 10:21:06 +08:00
|
|
|
}
|
|
|
|
|
2012-02-14 05:24:29 +08:00
|
|
|
void SleepForSeconds(int seconds) {
|
|
|
|
sleep(seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Exit(int exitcode) {
|
2012-03-14 00:29:25 +08:00
|
|
|
_exit(exitcode);
|
2012-02-14 05:24:29 +08:00
|
|
|
}
|
|
|
|
|
2012-04-06 09:27:11 +08:00
|
|
|
void Abort() {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2012-02-22 22:07:06 +08:00
|
|
|
int Atexit(void (*function)(void)) {
|
|
|
|
return atexit(function);
|
|
|
|
}
|
|
|
|
|
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-05-31 23:02:07 +08:00
|
|
|
u16 AtomicExchange(u16 *a, u16 new_val) {
|
2012-04-05 23:55:09 +08:00
|
|
|
return __sync_lock_test_and_set(a, new_val);
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:35:53 +08:00
|
|
|
void SortArray(uptr *array, uptr size) {
|
2012-03-10 09:30:01 +08:00
|
|
|
std::sort(array, array + size);
|
|
|
|
}
|
|
|
|
|
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_
|