[compile-rt] Reduce #ifdef noise for ptrauth
authorJulian Lettner <julian.lettner@apple.com>
Thu, 7 May 2020 02:05:31 +0000 (19:05 -0700)
committerJulian Lettner <julian.lettner@apple.com>
Mon, 11 May 2020 16:47:21 +0000 (09:47 -0700)
Create a sanitizer_ptrauth.h header that #includes <ptrauth> when
available and defines just the required macros as "no ops" otherwise.
This should avoid the need for excessive #ifdef'ing.

Follow-up to and discussed in: https://reviews.llvm.org/D79132

Reviewed By: delcypher

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

compiler-rt/lib/sanitizer_common/CMakeLists.txt
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
compiler-rt/lib/sanitizer_common/sanitizer_ptrauth.h [new file with mode: 0644]
compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp

index 235c5d7..97e6b1a 100644 (file)
@@ -164,6 +164,7 @@ set(SANITIZER_IMPL_HEADERS
   sanitizer_platform_limits_solaris.h
   sanitizer_posix.h
   sanitizer_procmaps.h
+  sanitizer_ptrauth.h
   sanitizer_quarantine.h
   sanitizer_report_decorator.h
   sanitizer_ring_buffer.h
index eff970d..1d767f6 100644 (file)
@@ -30,6 +30,7 @@
 #include "sanitizer_placement_new.h"
 #include "sanitizer_platform_limits_posix.h"
 #include "sanitizer_procmaps.h"
+#include "sanitizer_ptrauth.h"
 
 #if !SANITIZER_IOS
 #include <crt_externs.h>  // for _NSGetEnviron
@@ -765,12 +766,6 @@ bool SignalContext::IsTrueFaultingAddress() const {
   return si->si_signo == SIGSEGV && si->si_code != 0;
 }
 
-#if __has_feature(ptrauth_calls)
-# include <ptrauth.h>
-#else
-# define ptrauth_strip(value, key) (value)
-#endif
-
 #if defined(__aarch64__) && defined(arm_thread_state64_get_sp)
   #define AARCH64_GET_REG(r) \
     (uptr)ptrauth_strip(     \
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_ptrauth.h b/compiler-rt/lib/sanitizer_common/sanitizer_ptrauth.h
new file mode 100644 (file)
index 0000000..4d0d96a
--- /dev/null
@@ -0,0 +1,21 @@
+//===-- sanitizer_ptrauth.h -------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SANITIZER_PTRAUTH_H
+#define SANITIZER_PTRAUTH_H
+
+#if __has_feature(ptrauth_calls)
+#include <ptrauth.h>
+#else
+// Copied from <ptrauth.h>
+#define ptrauth_strip(__value, __key) __value
+#define ptrauth_auth_data(__value, __old_key, __old_data) __value
+#define ptrauth_string_discriminator(__string) ((int)0)
+#endif
+
+#endif // SANITIZER_PTRAUTH_H
index fdda701..f92ecc5 100644 (file)
@@ -19,6 +19,7 @@
 #include "sanitizer_common/sanitizer_libc.h"
 #include "sanitizer_common/sanitizer_posix.h"
 #include "sanitizer_common/sanitizer_procmaps.h"
+#include "sanitizer_common/sanitizer_ptrauth.h"
 #include "sanitizer_common/sanitizer_stackdepot.h"
 #include "tsan_platform.h"
 #include "tsan_rtl.h"
 #include <errno.h>
 #include <sched.h>
 
-#if __has_feature(ptrauth_calls)
-#include <ptrauth.h>
-#endif
-
 namespace __tsan {
 
 #if !SANITIZER_GO
@@ -278,10 +275,8 @@ void InitializePlatform() {
 uptr ExtractLongJmpSp(uptr *env) {
   uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
   uptr sp = mangled_sp ^ longjmp_xor_key;
-#if __has_feature(ptrauth_calls)
   sp = (uptr)ptrauth_auth_data((void *)sp, ptrauth_key_asdb,
                                ptrauth_string_discriminator("sp"));
-#endif
   return sp;
 }
 
index 465aa64..4f1708b 100644 (file)
 #include "ubsan_type_hash.h"
 
 #include "sanitizer_common/sanitizer_common.h"
-
-#if __has_feature(ptrauth_calls)
-#include <ptrauth.h>
-#endif
+#include "sanitizer_common/sanitizer_ptrauth.h"
 
 // The following are intended to be binary compatible with the definitions
 // given in the Itanium ABI. We make no attempt to be ODR-compatible with
@@ -198,9 +195,7 @@ struct VtablePrefix {
   std::type_info *TypeInfo;
 };
 VtablePrefix *getVtablePrefix(void *Vtable) {
-#if __has_feature(ptrauth_calls)
   Vtable = ptrauth_auth_data(Vtable, ptrauth_key_cxx_vtable_pointer, 0);
-#endif
   VtablePrefix *Vptr = reinterpret_cast<VtablePrefix*>(Vtable);
   VtablePrefix *Prefix = Vptr - 1;
   if (!IsAccessibleMemoryRange((uptr)Prefix, sizeof(VtablePrefix)))