[asan] refactoring: move some common linux/mac code to asan_posix.cc

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@147788 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_posix.cc b/lib/asan/asan_posix.cc
new file mode 100644
index 0000000..530db45
--- /dev/null
+++ b/lib/asan/asan_posix.cc
@@ -0,0 +1,68 @@
+//===-- 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"
+
+#include <signal.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+namespace __asan {
+
+static void MaybeInstallSigaction(int signum,
+                                  void (*handler)(int, siginfo_t *, void *)) {
+  if (!AsanInterceptsSignal(signum))
+    return;
+  struct sigaction sigact;
+  real_memset(&sigact, 0, sizeof(sigact));
+  sigact.sa_sigaction = handler;
+  sigact.sa_flags = SA_SIGINFO;
+  CHECK(0 == real_sigaction(signum, &sigact, 0));
+}
+
+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.
+  if (13 != AsanWrite(2, "ASAN:SIGSEGV\n", 13)) ASAN_DIE;
+  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");
+  GET_STACK_TRACE_WITH_PC_AND_BP(kStackTraceMax, false, pc, bp);
+  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);
+}
+
+}  // namespace __asan
+
+#endif  // __linux__ || __APPLE_