blob: 530db4545c6582a70b214477dc9bf06131a5d061 [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>
24
25namespace __asan {
26
27static void MaybeInstallSigaction(int signum,
28 void (*handler)(int, siginfo_t *, void *)) {
29 if (!AsanInterceptsSignal(signum))
30 return;
31 struct sigaction sigact;
32 real_memset(&sigact, 0, sizeof(sigact));
33 sigact.sa_sigaction = handler;
34 sigact.sa_flags = SA_SIGINFO;
35 CHECK(0 == real_sigaction(signum, &sigact, 0));
36}
37
38static void ASAN_OnSIGSEGV(int, siginfo_t *siginfo, void *context) {
39 uintptr_t addr = (uintptr_t)siginfo->si_addr;
40 // Write the first message using the bullet-proof write.
41 if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) ASAN_DIE;
42 uintptr_t pc, sp, bp;
43 GetPcSpBp(context, &pc, &sp, &bp);
44 Report("ERROR: AddressSanitizer crashed on unknown address %p"
45 " (pc %p sp %p bp %p T%d)\n",
46 addr, pc, sp, bp,
47 asanThreadRegistry().GetCurrentTidOrMinusOne());
48 Printf("AddressSanitizer can not provide additional info. ABORTING\n");
49 GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp);
50 stack.PrintStack();
51 ShowStatsAndAbort();
52}
53
54void InstallSignalHandlers() {
55 MaybeInstallSigaction(SIGSEGV, ASAN_OnSIGSEGV);
56 MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
57}
58
59void AsanDisableCoreDumper() {
60 struct rlimit nocore;
61 nocore.rlim_cur = 0;
62 nocore.rlim_max = 0;
63 setrlimit(RLIMIT_CORE, &nocore);
64}
65
66} // namespace __asan
67
68#endif // __linux__ || __APPLE_