11f9cb04fSpatrick //===-- sanitizer_ptrauth.h -------------------------------------*- C++ -*-===//
21f9cb04fSpatrick //
31f9cb04fSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41f9cb04fSpatrick // See https://llvm.org/LICENSE.txt for license information.
51f9cb04fSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61f9cb04fSpatrick //
71f9cb04fSpatrick //===----------------------------------------------------------------------===//
81f9cb04fSpatrick
91f9cb04fSpatrick #ifndef SANITIZER_PTRAUTH_H
101f9cb04fSpatrick #define SANITIZER_PTRAUTH_H
111f9cb04fSpatrick
121f9cb04fSpatrick #if __has_feature(ptrauth_calls)
131f9cb04fSpatrick #include <ptrauth.h>
14*d89ec533Spatrick #elif defined(__ARM_FEATURE_PAC_DEFAULT) && !defined(__APPLE__)
ptrauth_strip(void * __value,unsigned int __key)15*d89ec533Spatrick inline unsigned long ptrauth_strip(void* __value, unsigned int __key) {
16*d89ec533Spatrick // On the stack the link register is protected with Pointer
17*d89ec533Spatrick // Authentication Code when compiled with -mbranch-protection.
18*d89ec533Spatrick // Let's stripping the PAC unconditionally because xpaclri is in
19*d89ec533Spatrick // the NOP space so will do nothing when it is not enabled or not available.
20*d89ec533Spatrick unsigned long ret;
21*d89ec533Spatrick asm volatile(
22*d89ec533Spatrick "mov x30, %1\n\t"
23*d89ec533Spatrick "hint #7\n\t" // xpaclri
24*d89ec533Spatrick "mov %0, x30\n\t"
25*d89ec533Spatrick : "=r"(ret)
26*d89ec533Spatrick : "r"(__value)
27*d89ec533Spatrick : "x30");
28*d89ec533Spatrick return ret;
29*d89ec533Spatrick }
30*d89ec533Spatrick #define ptrauth_auth_data(__value, __old_key, __old_data) __value
31*d89ec533Spatrick #define ptrauth_string_discriminator(__string) ((int)0)
321f9cb04fSpatrick #else
331f9cb04fSpatrick // Copied from <ptrauth.h>
341f9cb04fSpatrick #define ptrauth_strip(__value, __key) __value
351f9cb04fSpatrick #define ptrauth_auth_data(__value, __old_key, __old_data) __value
361f9cb04fSpatrick #define ptrauth_string_discriminator(__string) ((int)0)
371f9cb04fSpatrick #endif
381f9cb04fSpatrick
39*d89ec533Spatrick #define STRIP_PAC_PC(pc) ((uptr)ptrauth_strip(pc, 0))
40*d89ec533Spatrick
411f9cb04fSpatrick #endif // SANITIZER_PTRAUTH_H
42