[TSan] Improve handling of stack pointer mangling in {set,long}jmp, pt.7
authorJulian Lettner <jlettner@apple.com>
Tue, 2 Jul 2019 17:32:04 +0000 (17:32 +0000)
committerJulian Lettner <jlettner@apple.com>
Tue, 2 Jul 2019 17:32:04 +0000 (17:32 +0000)
Factor out `ExtractLongJmpSp` helper function and move platform-specific
code to tsan_platform_{linux,mac}.cc.

Reviewed By: dvyukov

Differential Revision: https://reviews.llvm.org/D64050

llvm-svn: 364947

compiler-rt/lib/tsan/rtl/tsan_interceptors.cc
compiler-rt/lib/tsan/rtl/tsan_platform.h
compiler-rt/lib/tsan/rtl/tsan_platform_linux.cc
compiler-rt/lib/tsan/rtl/tsan_platform_mac.cc

index b6c8e64..9a184c7 100644 (file)
@@ -505,29 +505,7 @@ static void SetJmp(ThreadState *thr, uptr sp) {
 }
 
 static void LongJmp(ThreadState *thr, uptr *env) {
-#ifdef __powerpc__
-  uptr mangled_sp = env[0];
-#elif SANITIZER_FREEBSD
-  uptr mangled_sp = env[2];
-#elif SANITIZER_NETBSD
-  uptr mangled_sp = env[6];
-#elif SANITIZER_MAC
-# ifdef __aarch64__
-  uptr mangled_sp =
-      (GetMacosVersion() >= MACOS_VERSION_MOJAVE) ? env[12] : env[13];
-# else
-    uptr mangled_sp = env[2];
-# endif
-#elif SANITIZER_LINUX
-# ifdef __aarch64__
-  uptr mangled_sp = env[13];
-# elif defined(__mips64)
-  uptr mangled_sp = env[1];
-# else
-  uptr mangled_sp = env[6];
-# endif
-#endif
-  uptr sp = UnmangleLongJmpSp(mangled_sp);
+  uptr sp = ExtractLongJmpSp(env);
   // Find the saved buf with matching sp.
   for (uptr i = 0; i < thr->jmp_bufs.Size(); i++) {
     JmpBuf *buf = &thr->jmp_bufs[i];
index 35f3ab9..0d106c4 100644 (file)
@@ -1011,7 +1011,7 @@ void FlushShadowMemory();
 void WriteMemoryProfile(char *buf, uptr buf_size, uptr nthread, uptr nlive);
 int ExtractResolvFDs(void *state, int *fds, int nfd);
 int ExtractRecvmsgFDs(void *msg, int *fds, int nfd);
-uptr UnmangleLongJmpSp(uptr mangled_sp);
+uptr ExtractLongJmpSp(uptr *env);
 void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size);
 
 int call_pthread_cancel_with_cleanup(int(*fn)(void *c, void *m,
index 1d4ef6b..86d732d 100644 (file)
@@ -336,11 +336,11 @@ int ExtractRecvmsgFDs(void *msgp, int *fds, int nfd) {
 }
 
 // Reverse operation of libc stack pointer mangling
-uptr UnmangleLongJmpSp(uptr mangled_sp) {
+static uptr UnmangleLongJmpSp(uptr mangled_sp) {
 #if defined(__x86_64__)
-#if SANITIZER_FREEBSD || SANITIZER_NETBSD
+# if SANITIZER_FREEBSD || SANITIZER_NETBSD
   return mangled_sp;
-#else  // Linux
+# else  // Linux
   // Reverse of:
   //   xor  %fs:0x30, %rsi
   //   rol  $0x11, %rsi
@@ -350,7 +350,7 @@ uptr UnmangleLongJmpSp(uptr mangled_sp) {
       : "=r" (sp)
       : "0" (mangled_sp));
   return sp;
-#endif
+# endif
 #elif defined(__aarch64__)
 # if SANITIZER_LINUX
   return mangled_sp ^ _tsan_pointer_chk_guard;
@@ -371,6 +371,27 @@ uptr UnmangleLongJmpSp(uptr mangled_sp) {
 #endif
 }
 
+#ifdef __powerpc__
+# define LONG_JMP_SP_ENV_SLOT 0
+#elif SANITIZER_FREEBSD
+# define LONG_JMP_SP_ENV_SLOT 2
+#elif SANITIZER_NETBSD
+# define LONG_JMP_SP_ENV_SLOT 6
+#elif SANITIZER_LINUX
+# ifdef __aarch64__
+#  define LONG_JMP_SP_ENV_SLOT 13
+# elif defined(__mips64)
+#  define LONG_JMP_SP_ENV_SLOT 1
+# else
+#  define LONG_JMP_SP_ENV_SLOT 6
+# endif
+#endif
+
+uptr ExtractLongJmpSp(uptr *env) {
+  uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
+  return UnmangleLongJmpSp(mangled_sp);
+}
+
 void ImitateTlsWrite(ThreadState *thr, uptr tls_addr, uptr tls_size) {
   // Check that the thr object is in tls;
   const uptr thr_beg = (uptr)thr;
index 798aee3..e43a9ec 100644 (file)
@@ -259,7 +259,15 @@ void InitializePlatform() {
   }
 }
 
-uptr UnmangleLongJmpSp(uptr mangled_sp) {
+#ifdef __aarch64__
+# define LONG_JMP_SP_ENV_SLOT \
+    ((GetMacosVersion() >= MACOS_VERSION_MOJAVE) ? 12 : 13)
+#else
+# define LONG_JMP_SP_ENV_SLOT 2
+#endif
+
+uptr ExtractLongJmpSp(uptr *env) {
+  uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
   return mangled_sp ^ __tsan_darwin_setjmp_xor_key;
 }