[tsan] Adjust setjmp/longjmp handling on Darwin for macOS Mojave
authorKuba Mracek <mracek@apple.com>
Tue, 21 Aug 2018 22:35:52 +0000 (22:35 +0000)
committerKuba Mracek <mracek@apple.com>
Tue, 21 Aug 2018 22:35:52 +0000 (22:35 +0000)
On macOS Mojave, the OS started using the XOR-by-a-secret-key scheme (same as glibc is alread doing) for storing the SP value in setjmp environment. We need to adjust for that to keep supporting setjmp/longjmp on latest Darwin. The patch is basically doing the same what we're already doing for glibc.

rdar://problem/43542596

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

llvm-svn: 340350

compiler-rt/lib/sanitizer_common/sanitizer_mac.cc
compiler-rt/lib/sanitizer_common/sanitizer_mac.h
compiler-rt/lib/tsan/rtl/tsan_interceptors.cc
compiler-rt/lib/tsan/rtl/tsan_platform_mac.cc
compiler-rt/lib/tsan/rtl/tsan_rtl_aarch64.S
compiler-rt/lib/tsan/rtl/tsan_rtl_amd64.S

index 48747bc..9242299 100644 (file)
@@ -511,6 +511,10 @@ MacosVersion GetMacosVersionInternal() {
         case '2': return MACOS_VERSION_MOUNTAIN_LION;
         case '3': return MACOS_VERSION_MAVERICKS;
         case '4': return MACOS_VERSION_YOSEMITE;
+        case '5': return MACOS_VERSION_EL_CAPITAN;
+        case '6': return MACOS_VERSION_SIERRA;
+        case '7': return MACOS_VERSION_HIGH_SIERRA;
+        case '8': return MACOS_VERSION_MOJAVE;
         default:
           if (IsDigit(version[1]))
             return MACOS_VERSION_UNKNOWN_NEWER;
index e022a2c..58423a7 100644 (file)
@@ -40,6 +40,10 @@ enum MacosVersion {
   MACOS_VERSION_MOUNTAIN_LION,
   MACOS_VERSION_MAVERICKS,
   MACOS_VERSION_YOSEMITE,
+  MACOS_VERSION_EL_CAPITAN,
+  MACOS_VERSION_SIERRA,
+  MACOS_VERSION_HIGH_SIERRA,
+  MACOS_VERSION_MOJAVE,
   MACOS_VERSION_UNKNOWN_NEWER
 };
 
index 901997b..5e64d11 100644 (file)
@@ -508,7 +508,8 @@ static void LongJmp(ThreadState *thr, uptr *env) {
   uptr mangled_sp = env[6];
 #elif SANITIZER_MAC
 # ifdef __aarch64__
-    uptr mangled_sp = env[13];
+  uptr mangled_sp =
+      (GetMacosVersion() >= MACOS_VERSION_MOJAVE) ? env[12] : env[13];
 # else
     uptr mangled_sp = env[2];
 # endif
index f8d7324..7e3a473 100644 (file)
@@ -240,6 +240,9 @@ void InitializePlatformEarly() {
 #endif
 }
 
+static const uptr kPthreadSetjmpXorKeySlot = 0x7;
+extern "C" uptr __tsan_darwin_setjmp_xor_key = 0;
+
 void InitializePlatform() {
   DisableCoreDumperIfNecessary();
 #if !SANITIZER_GO
@@ -251,6 +254,11 @@ void InitializePlatform() {
   prev_pthread_introspection_hook =
       pthread_introspection_hook_install(&my_pthread_introspection_hook);
 #endif
+
+  if (GetMacosVersion() >= MACOS_VERSION_MOJAVE) {
+    __tsan_darwin_setjmp_xor_key =
+        (uptr)pthread_getspecific(kPthreadSetjmpXorKeySlot);
+  }
 }
 
 #if !SANITIZER_GO
index 844c2e2..3d02bf2 100644 (file)
@@ -120,8 +120,10 @@ ASM_SYMBOL_INTERCEPTOR(setjmp):
   add     x0, x29, 32
   eor     x1, x2, x0
 #else
+  adrp    x2, ___tsan_darwin_setjmp_xor_key@page
+  ldr     x2, [x2, ___tsan_darwin_setjmp_xor_key@pageoff]
   add     x0, x29, 32
-  mov     x1, x0
+  eor     x1, x2, x0
 #endif
 
   // call tsan interceptor
@@ -178,8 +180,10 @@ ASM_SYMBOL_INTERCEPTOR(_setjmp):
   add     x0, x29, 32
   eor     x1, x2, x0
 #else
+  adrp    x2, ___tsan_darwin_setjmp_xor_key@page
+  ldr     x2, [x2, ___tsan_darwin_setjmp_xor_key@pageoff]
   add     x0, x29, 32
-  mov     x1, x0
+  eor     x1, x2, x0
 #endif
 
   // call tsan interceptor
@@ -238,8 +242,10 @@ ASM_SYMBOL_INTERCEPTOR(sigsetjmp):
   add     x0, x29, 32
   eor     x1, x2, x0
 #else
+  adrp    x2, ___tsan_darwin_setjmp_xor_key@page
+  ldr     x2, [x2, ___tsan_darwin_setjmp_xor_key@pageoff]
   add     x0, x29, 32
-  mov     x1, x0
+  eor     x1, x2, x0
 #endif
 
   // call tsan interceptor
index 8af61bf..34ef51c 100644 (file)
@@ -196,6 +196,7 @@ ASM_SYMBOL_INTERCEPTOR(setjmp):
 #elif defined(__APPLE__)
   lea 16(%rsp), %rdi
   mov %rdi, %rsi
+  xorq ___tsan_darwin_setjmp_xor_key(%rip), %rsi
 #elif defined(__linux__)
   lea 16(%rsp), %rdi
   mov %rdi, %rsi
@@ -244,6 +245,7 @@ ASM_SYMBOL_INTERCEPTOR(_setjmp):
 #elif defined(__APPLE__)
   lea 16(%rsp), %rdi
   mov %rdi, %rsi
+  xorq ___tsan_darwin_setjmp_xor_key(%rip), %rsi
 #elif defined(__linux__)
   lea 16(%rsp), %rdi
   mov %rdi, %rsi
@@ -299,6 +301,7 @@ ASM_SYMBOL_INTERCEPTOR(sigsetjmp):
 #elif defined(__APPLE__)
   lea 32(%rsp), %rdi
   mov %rdi, %rsi
+  xorq ___tsan_darwin_setjmp_xor_key(%rip), %rsi
 #elif defined(__linux__)
   lea 32(%rsp), %rdi
   mov %rdi, %rsi