Skip to content

Commit bba38de

Browse files
author
Julian Lettner
committed
[compile-rt] Reduce #ifdef noise for ptrauth
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
1 parent e6615d7 commit bba38de

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

compiler-rt/lib/sanitizer_common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ set(SANITIZER_IMPL_HEADERS
164164
sanitizer_platform_limits_solaris.h
165165
sanitizer_posix.h
166166
sanitizer_procmaps.h
167+
sanitizer_ptrauth.h
167168
sanitizer_quarantine.h
168169
sanitizer_report_decorator.h
169170
sanitizer_ring_buffer.h

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "sanitizer_placement_new.h"
3131
#include "sanitizer_platform_limits_posix.h"
3232
#include "sanitizer_procmaps.h"
33+
#include "sanitizer_ptrauth.h"
3334

3435
#if !SANITIZER_IOS
3536
#include <crt_externs.h> // for _NSGetEnviron
@@ -765,12 +766,6 @@ bool SignalContext::IsTrueFaultingAddress() const {
765766
return si->si_signo == SIGSEGV && si->si_code != 0;
766767
}
767768

768-
#if __has_feature(ptrauth_calls)
769-
# include <ptrauth.h>
770-
#else
771-
# define ptrauth_strip(value, key) (value)
772-
#endif
773-
774769
#if defined(__aarch64__) && defined(arm_thread_state64_get_sp)
775770
#define AARCH64_GET_REG(r) \
776771
(uptr)ptrauth_strip( \
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- sanitizer_ptrauth.h -------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef SANITIZER_PTRAUTH_H
10+
#define SANITIZER_PTRAUTH_H
11+
12+
#if __has_feature(ptrauth_calls)
13+
#include <ptrauth.h>
14+
#else
15+
// Copied from <ptrauth.h>
16+
#define ptrauth_strip(__value, __key) __value
17+
#define ptrauth_auth_data(__value, __old_key, __old_data) __value
18+
#define ptrauth_string_discriminator(__string) ((int)0)
19+
#endif
20+
21+
#endif // SANITIZER_PTRAUTH_H

compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "sanitizer_common/sanitizer_libc.h"
2020
#include "sanitizer_common/sanitizer_posix.h"
2121
#include "sanitizer_common/sanitizer_procmaps.h"
22+
#include "sanitizer_common/sanitizer_ptrauth.h"
2223
#include "sanitizer_common/sanitizer_stackdepot.h"
2324
#include "tsan_platform.h"
2425
#include "tsan_rtl.h"
@@ -41,10 +42,6 @@
4142
#include <errno.h>
4243
#include <sched.h>
4344

44-
#if __has_feature(ptrauth_calls)
45-
#include <ptrauth.h>
46-
#endif
47-
4845
namespace __tsan {
4946

5047
#if !SANITIZER_GO
@@ -278,10 +275,8 @@ void InitializePlatform() {
278275
uptr ExtractLongJmpSp(uptr *env) {
279276
uptr mangled_sp = env[LONG_JMP_SP_ENV_SLOT];
280277
uptr sp = mangled_sp ^ longjmp_xor_key;
281-
#if __has_feature(ptrauth_calls)
282278
sp = (uptr)ptrauth_auth_data((void *)sp, ptrauth_key_asdb,
283279
ptrauth_string_discriminator("sp"));
284-
#endif
285280
return sp;
286281
}
287282

compiler-rt/lib/ubsan/ubsan_type_hash_itanium.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
#include "ubsan_type_hash.h"
1717

1818
#include "sanitizer_common/sanitizer_common.h"
19-
20-
#if __has_feature(ptrauth_calls)
21-
#include <ptrauth.h>
22-
#endif
19+
#include "sanitizer_common/sanitizer_ptrauth.h"
2320

2421
// The following are intended to be binary compatible with the definitions
2522
// given in the Itanium ABI. We make no attempt to be ODR-compatible with
@@ -198,9 +195,7 @@ struct VtablePrefix {
198195
std::type_info *TypeInfo;
199196
};
200197
VtablePrefix *getVtablePrefix(void *Vtable) {
201-
#if __has_feature(ptrauth_calls)
202198
Vtable = ptrauth_auth_data(Vtable, ptrauth_key_cxx_vtable_pointer, 0);
203-
#endif
204199
VtablePrefix *Vptr = reinterpret_cast<VtablePrefix*>(Vtable);
205200
VtablePrefix *Prefix = Vptr - 1;
206201
if (!IsAccessibleMemoryRange((uptr)Prefix, sizeof(VtablePrefix)))

0 commit comments

Comments
 (0)