[Sanitizer] move more portability wrappers to common runtime: sleep, _exit, abort, atexit, pthread_self

git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@158493 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/asan/asan_internal.h b/lib/asan/asan_internal.h
index f2663f0..128c83d 100644
--- a/lib/asan/asan_internal.h
+++ b/lib/asan/asan_internal.h
@@ -129,7 +129,6 @@
 void SetAlternateSignalStack();
 void UnsetAlternateSignalStack();
 void InstallSignalHandlers();
-uptr GetThreadSelf();
 int AtomicInc(int *a);
 u16 AtomicExchange(u16 *a, u16 new_val);
 u8 AtomicExchange(u8 *a, u8 new_val);
@@ -200,11 +199,6 @@
 
 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
 
-void SleepForSeconds(int seconds);
-void NORETURN Exit(int exitcode);
-void NORETURN Abort();
-int Atexit(void (*function)(void));
-
 #define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
 
 #if !defined(_WIN32) || defined(__clang__)
diff --git a/lib/asan/asan_posix.cc b/lib/asan/asan_posix.cc
index 429f58a..06161c2 100644
--- a/lib/asan/asan_posix.cc
+++ b/lib/asan/asan_posix.cc
@@ -134,26 +134,6 @@
   MaybeInstallSigaction(SIGBUS, ASAN_OnSIGSEGV);
 }
 
-uptr GetThreadSelf() {
-  return (uptr)pthread_self();
-}
-
-void SleepForSeconds(int seconds) {
-  sleep(seconds);
-}
-
-void Exit(int exitcode) {
-  _exit(exitcode);
-}
-
-void Abort() {
-  abort();
-}
-
-int Atexit(void (*function)(void)) {
-  return atexit(function);
-}
-
 int AtomicInc(int *a) {
 #ifdef ANDROID
   return __atomic_inc(a) + 1;
diff --git a/lib/asan/asan_thread_registry.cc b/lib/asan/asan_thread_registry.cc
index 817f15d..444900d 100644
--- a/lib/asan/asan_thread_registry.cc
+++ b/lib/asan/asan_thread_registry.cc
@@ -16,6 +16,7 @@
 #include "asan_stack.h"
 #include "asan_thread.h"
 #include "asan_thread_registry.h"
+#include "sanitizer_common/sanitizer_common.h"
 
 namespace __asan {
 
diff --git a/lib/asan/asan_win.cc b/lib/asan/asan_win.cc
index b9034ca..83bd0f4 100644
--- a/lib/asan/asan_win.cc
+++ b/lib/asan/asan_win.cc
@@ -192,10 +192,6 @@
   return t;
 }
 
-uptr GetThreadSelf() {
-  return GetCurrentThreadId();
-}
-
 void SetAlternateSignalStack() {
   // FIXME: Decide what to do on Windows.
 }
@@ -208,23 +204,6 @@
   // FIXME: Decide what to do on Windows.
 }
 
-void SleepForSeconds(int seconds) {
-  Sleep(seconds * 1000);
-}
-
-void Exit(int exitcode) {
-  _exit(exitcode);
-}
-
-void Abort() {
-  abort();
-  _exit(-1);  // abort is not NORETURN on Windows.
-}
-
-int Atexit(void (*function)(void)) {
-  return atexit(function);
-}
-
 void SortArray(uptr *array, uptr size) {
   std::sort(array, array + size);
 }
diff --git a/lib/sanitizer_common/sanitizer_common.h b/lib/sanitizer_common/sanitizer_common.h
index 0b40424..2f913a9 100644
--- a/lib/sanitizer_common/sanitizer_common.h
+++ b/lib/sanitizer_common/sanitizer_common.h
@@ -28,6 +28,7 @@
 
 // Threads
 int GetPid();
+uptr GetThreadSelf();
 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
                                 uptr *stack_bottom);
 
@@ -58,6 +59,10 @@
 // Other
 void DisableCoreDumper();
 void DumpProcessMap();
+void SleepForSeconds(int seconds);
+void NORETURN Exit(int exitcode);
+void NORETURN Abort();
+int Atexit(void (*function)(void));
 
 // Bit twiddling.
 inline bool IsPowerOfTwo(uptr x) {
diff --git a/lib/sanitizer_common/sanitizer_posix.cc b/lib/sanitizer_common/sanitizer_posix.cc
index 8d6ec33..a72d3bd 100644
--- a/lib/sanitizer_common/sanitizer_posix.cc
+++ b/lib/sanitizer_common/sanitizer_posix.cc
@@ -17,8 +17,10 @@
 #include "sanitizer_libc.h"
 #include "sanitizer_procmaps.h"
 
+#include <pthread.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/mman.h>
 #include <sys/resource.h>
 #include <sys/time.h>
@@ -33,6 +35,10 @@
   return getpid();
 }
 
+uptr GetThreadSelf() {
+  return (uptr)pthread_self();
+}
+
 void *MmapOrDie(uptr size, const char *mem_type) {
   size = RoundUpTo(size, kPageSize);
   void *res = internal_mmap(0, size,
@@ -90,6 +96,22 @@
   setrlimit(RLIMIT_CORE, &nocore);
 }
 
+void SleepForSeconds(int seconds) {
+  sleep(seconds);
+}
+
+void Exit(int exitcode) {
+  _exit(exitcode);
+}
+
+void Abort() {
+  abort();
+}
+
+int Atexit(void (*function)(void)) {
+  return atexit(function);
+}
+
 // -------------- sanitizer_libc.h
 
 int internal_sscanf(const char *str, const char *format, ...) {
diff --git a/lib/sanitizer_common/sanitizer_win.cc b/lib/sanitizer_common/sanitizer_win.cc
index a8c43ca..465a78d 100644
--- a/lib/sanitizer_common/sanitizer_win.cc
+++ b/lib/sanitizer_common/sanitizer_win.cc
@@ -24,6 +24,10 @@
   return GetProcessId(GetCurrentProcess());
 }
 
+uptr GetThreadSelf() {
+  return GetCurrentThreadId();
+}
+
 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
                                 uptr *stack_bottom) {
   CHECK(stack_top);
@@ -90,6 +94,23 @@
   UNIMPLEMENTED();
 }
 
+void SleepForSeconds(int seconds) {
+  Sleep(seconds * 1000);
+}
+
+void Exit(int exitcode) {
+  _exit(exitcode);
+}
+
+void Abort() {
+  abort();
+  _exit(-1);  // abort is not NORETURN on Windows.
+}
+
+int Atexit(void (*function)(void)) {
+  return atexit(function);
+}
+
 // ------------------ sanitizer_libc.h
 void *internal_mmap(void *addr, uptr length, int prot, int flags,
                     int fd, u64 offset) {