blob: a6d0eda79129a25fe60b97c40f2da8d5ded7153d [file] [log] [blame]
Kostya Serebryanya7e760a2012-01-09 19:18:27 +00001//===-- asan_linux.cc -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// Posix-specific details.
13//===----------------------------------------------------------------------===//
14#if defined(__linux__) || defined(__APPLE__)
15
16#include "asan_internal.h"
17#include "asan_interceptors.h"
18#include "asan_stack.h"
19#include "asan_thread_registry.h"
20
21#include <signal.h>
22#include <sys/time.h>
23#include <sys/resource.h>
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000024#include <unistd.h>
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000025
26namespace __asan {
27
28static void MaybeInstallSigaction(int signum,
29 void (*handler)(int, siginfo_t *, void *)) {
30 if (!AsanInterceptsSignal(signum))
31 return;
32 struct sigaction sigact;
33 real_memset(&sigact, 0, sizeof(sigact));
34 sigact.sa_sigaction = handler;
35 sigact.sa_flags = SA_SIGINFO;
36 CHECK(0 == real_sigaction(signum, &sigact, 0));
37}
38
39static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
40 uintptr_t addr = (uintptr_t)siginfo->si_addr;
41 // Write the first message using the bullet-proof write.
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000042 if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) AsanDie();
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000043 uintptr_t pc, sp, bp;
44 GetPcSpBp(context, &pc, &sp, &bp);
45 Report("ERROR: AddressSanitizer crashed on unknown address %p"
46 " (pc %p sp %p bp %p T%d)\n",
47 addr, pc, sp, bp,
48 asanThreadRegistry().GetCurrentTidOrMinusOne());
49 Printf("AddressSanitizer can not provide additional info. ABORTING\n");
50 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp);
51 stack.PrintStack();
52 ShowStatsAndAbort();
53}
54
55void InstallSignalHandlers() {
56 MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
57 MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
58}
59
60void AsanDisableCoreDumper() {
61 struct rlimit nocore;
62 nocore.rlim_cur = 0;
63 nocore.rlim_max = 0;
64 setrlimit(RLIMIT_CORE, &nocore);
65}
66
Kostya Serebryany0ecf5eb2012-01-09 23:11:26 +000067void AsanDie() {
68 _exit(FLAG_exitcode);
69}
70
71int GetPid() {
72 return getpid();
73}
74
Kostya Serebryanya7e760a2012-01-09 19:18:27 +000075} // namespace __asan
76
77#endif // __linux__ || __APPLE_