15ffd83dbSDimitry Andric //===-- sanitizer_ptrauth.h -------------------------------------*- C++ -*-===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
95ffd83dbSDimitry Andric #ifndef SANITIZER_PTRAUTH_H
105ffd83dbSDimitry Andric #define SANITIZER_PTRAUTH_H
115ffd83dbSDimitry Andric 
125ffd83dbSDimitry Andric #if __has_feature(ptrauth_calls)
135ffd83dbSDimitry Andric #include <ptrauth.h>
14*fe6060f1SDimitry Andric #elif defined(__ARM_FEATURE_PAC_DEFAULT) && !defined(__APPLE__)
ptrauth_strip(void * __value,unsigned int __key)15*fe6060f1SDimitry Andric inline unsigned long ptrauth_strip(void* __value, unsigned int __key) {
16*fe6060f1SDimitry Andric   // On the stack the link register is protected with Pointer
17*fe6060f1SDimitry Andric   // Authentication Code when compiled with -mbranch-protection.
18*fe6060f1SDimitry Andric   // Let's stripping the PAC unconditionally because xpaclri is in
19*fe6060f1SDimitry Andric   // the NOP space so will do nothing when it is not enabled or not available.
20*fe6060f1SDimitry Andric   unsigned long ret;
21*fe6060f1SDimitry Andric   asm volatile(
22*fe6060f1SDimitry Andric       "mov x30, %1\n\t"
23*fe6060f1SDimitry Andric       "hint #7\n\t"  // xpaclri
24*fe6060f1SDimitry Andric       "mov %0, x30\n\t"
25*fe6060f1SDimitry Andric       : "=r"(ret)
26*fe6060f1SDimitry Andric       : "r"(__value)
27*fe6060f1SDimitry Andric       : "x30");
28*fe6060f1SDimitry Andric   return ret;
29*fe6060f1SDimitry Andric }
30*fe6060f1SDimitry Andric #define ptrauth_auth_data(__value, __old_key, __old_data) __value
31*fe6060f1SDimitry Andric #define ptrauth_string_discriminator(__string) ((int)0)
325ffd83dbSDimitry Andric #else
335ffd83dbSDimitry Andric // Copied from <ptrauth.h>
345ffd83dbSDimitry Andric #define ptrauth_strip(__value, __key) __value
355ffd83dbSDimitry Andric #define ptrauth_auth_data(__value, __old_key, __old_data) __value
365ffd83dbSDimitry Andric #define ptrauth_string_discriminator(__string) ((int)0)
375ffd83dbSDimitry Andric #endif
385ffd83dbSDimitry Andric 
39*fe6060f1SDimitry Andric #define STRIP_PAC_PC(pc) ((uptr)ptrauth_strip(pc, 0))
40e8d8bef9SDimitry Andric 
415ffd83dbSDimitry Andric #endif // SANITIZER_PTRAUTH_H
42