[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 48747bc835936f37a71d1c4a040f9de71e724faf..924229971f294a04ee9763df4bbf6d79adf6137a 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 e022a2c0363cd9903acfced50c30e22e8fe83f96..58423a774926edf797e035ed799136a4414915b2 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 901997b3e85df45b3f2528d1166f615df050b651..5e64d11f342f0c2f7b3cbb7ce1bf5dcb84b540cb 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 f8d7324b763c2bf81925aecb4e662cd9bfe1d266..7e3a47387b6d809c9382f82d0af60071a3822509 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 844c2e23db4ffd2e96b6f54df37c87bd790b20d3..3d02bf22f8a550cc7ccccacb0804b417c12fbe74 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 8af61bf0e8920a90812ed4497e1f8230aabc791c..34ef51c2a725704815630699073263c97aaf8d83 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