Skip to content

Commit 6dc2916

Browse files
committed
[PAC][libunwind] Fix gcc build of libunwind and compiler-rt
This adds guards on the ptrauth feature checks so that they are only performed if __has_feature is actually available.
1 parent 1906c3e commit 6dc2916

File tree

6 files changed

+42
-16
lines changed

6 files changed

+42
-16
lines changed

compiler-rt/lib/builtins/gcc_personality_v0.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, void *, PCONTEXT,
3030
_Unwind_Personality_Fn);
3131
#endif
3232

33-
#if __has_feature(ptrauth_calls)
33+
#if defined(__has_feature)
34+
#define __has_ptrauth_calls __has_feature(ptrauth_calls)
35+
#else
36+
#define __has_ptrauth_calls 0
37+
#endif
38+
39+
#if __has_ptrauth_calls
3440
#include <ptrauth.h>
3541

3642
// `__ptrauth_restricted_intptr` is a feature of apple clang that predates
@@ -48,6 +54,7 @@ EXCEPTION_DISPOSITION _GCC_specific_handler(PEXCEPTION_RECORD, void *, PCONTEXT,
4854
#else
4955
#define __ptrauth_gcc_personality_intptr(...)
5056
#endif
57+
#else
5158

5259
#define __ptrauth_gcc_personality_func_key ptrauth_key_function_pointer
5360

@@ -292,7 +299,7 @@ COMPILER_RT_ABI _Unwind_Reason_Code __gcc_personality_v0(
292299
_Unwind_SetGR(context, __builtin_eh_return_data_regno(1), 0);
293300
size_t __ptrauth_gcc_personality_lpad landingPad =
294301
funcStart + landingPadOffset;
295-
#if __has_feature(ptrauth_calls)
302+
#if __has_ptrauth_calls
296303
uintptr_t stackPointer = _Unwind_GetGR(context, -2);
297304
const uintptr_t existingDiscriminator = ptrauth_blend_discriminator(
298305
&landingPad, __ptrauth_gcc_personality_lpad_disc);

libcxxabi/include/__cxxabi_config.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@
107107
# include <ptrauth.h>
108108
#endif
109109

110-
#if __has_feature(ptrauth_calls)
110+
#if defined(__has_feature)
111+
# define __ptrauth_cxxabi_enabled __has_feature(ptrauth_calls)
112+
#else
113+
# define __ptrauth_cxxabi_enabled 0
114+
#endif
115+
#if __ptrauth_cxxabi_enabled
111116

112117
// ptrauth_string_discriminator("__cxa_exception::actionRecord") == 0xFC91
113118
# define __ptrauth_cxxabi_action_record __ptrauth(ptrauth_key_process_dependent_data, 1, 0xFC91)

libcxxabi/src/cxa_personality.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "cxa_handlers.h"
2222
#include "private_typeinfo.h"
2323

24-
#if __has_feature(ptrauth_calls)
24+
#if __ptrauth_cxxabi_enabled
2525

2626
// CXXABI depends on defintions in libunwind as pointer auth couples the
2727
// definitions
@@ -602,7 +602,7 @@ set_registers(_Unwind_Exception* unwind_exception, _Unwind_Context* context,
602602
reinterpret_cast<uintptr_t>(unwind_exception));
603603
_Unwind_SetGR(context, __builtin_eh_return_data_regno(1),
604604
static_cast<uintptr_t>(results.ttypeIndex));
605-
#if __has_feature(ptrauth_calls)
605+
#if __ptrauth_cxxabi_enabled
606606
auto stackPointer = _Unwind_GetGR(context, UNW_REG_SP);
607607
// We manually re-sign the IP as the __ptrauth qualifiers cannot
608608
// express the required relationship with the destination address
@@ -973,7 +973,7 @@ _UA_CLEANUP_PHASE
973973
using __cxa_catch_temp_type = decltype(__cxa_exception::catchTemp);
974974
static inline void set_landing_pad(scan_results& results,
975975
const __cxa_catch_temp_type& source) {
976-
#if __has_feature(ptrauth_calls)
976+
#if __ptrauth_cxxabi_enabled
977977
const uintptr_t sourceDiscriminator =
978978
ptrauth_blend_discriminator(&source, __ptrauth_cxxabi_catch_temp_disc);
979979
const uintptr_t targetDiscriminator =
@@ -995,7 +995,7 @@ static inline void set_landing_pad(scan_results& results,
995995

996996
static inline void get_landing_pad(__cxa_catch_temp_type &dest,
997997
const scan_results &results) {
998-
#if __has_feature(ptrauth_calls)
998+
#if __ptrauth_cxxabi_enabled
999999
const uintptr_t sourceDiscriminator =
10001000
ptrauth_blend_discriminator(&results.landingPad,
10011001
__ptrauth_scan_results_landingpad_disc);

libunwind/include/__libunwind_config.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,13 @@
212212
# define _LIBUNWIND_HIGHEST_DWARF_REGISTER 287
213213
#endif // _LIBUNWIND_IS_NATIVE_ONLY
214214

215-
#if __has_feature(ptrauth_calls) && __has_feature(ptrauth_returns)
216-
# define _LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING 1
217-
#elif __has_feature(ptrauth_calls) != __has_feature(ptrauth_returns)
218-
# error "Either both or none of ptrauth_calls and ptrauth_returns "\
219-
"is allowed to be enabled"
215+
#if defined(__has_feature)
216+
# if __has_feature(ptrauth_calls) && __has_feature(ptrauth_returns)
217+
# define _LIBUNWIND_TARGET_AARCH64_AUTHENTICATED_UNWINDING 1
218+
# elif __has_feature(ptrauth_calls) != __has_feature(ptrauth_returns)
219+
# error "Either both or none of ptrauth_calls and ptrauth_returns "\
220+
"is allowed to be enabled"
221+
# endif
220222
#endif
221223

222224
#endif // ____LIBUNWIND_CONFIG_H__

libunwind/src/UnwindRegistersRestore.S

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,12 @@ Lnovec:
634634

635635
#elif defined(__aarch64__)
636636

637+
#ifdef __has_feature
638+
#define __has_ptrauth_calls __has_feature(ptrauth_calls)
639+
#else
640+
#define __has_ptrauth_calls 0
641+
#endif
642+
637643
#if defined(__ARM_FEATURE_GCS_DEFAULT)
638644
.arch_extension gcs
639645
#endif
@@ -690,7 +696,7 @@ DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_jumpto)
690696
ldr x16, [x0, #0x0F8] // load sp into scratch
691697
ldr lr, [x0, #0x100] // restore pc into lr
692698

693-
#if __has_feature(ptrauth_calls)
699+
#if __has_ptrauth_calls
694700
// The LR is signed with its address inside the register state. Time
695701
// to resign to be a regular ROP protected signed pointer
696702
add x1, x0, #0x100
@@ -711,7 +717,7 @@ DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_jumpto)
711717
Lnogcs:
712718
#endif
713719

714-
#if __has_feature(ptrauth_calls)
720+
#if __has_ptrauth_calls
715721
retab
716722
#else
717723
ret x30 // jump to pc

libunwind/src/UnwindRegistersSave.S

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,12 @@ LnoR2Fix:
763763

764764
#elif defined(__aarch64__)
765765

766+
#ifdef __has_feature
767+
#define __has_ptrauth_calls __has_feature(ptrauth_calls)
768+
#else
769+
#define __has_ptrauth_calls 0
770+
#endif
771+
766772
//
767773
// extern int __unw_getcontext(unw_context_t* thread_state)
768774
//
@@ -772,7 +778,7 @@ LnoR2Fix:
772778
.p2align 2
773779
DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
774780

775-
#if __has_feature(ptrauth_calls)
781+
#if __has_ptrauth_calls
776782
pacibsp
777783
#endif
778784

@@ -817,7 +823,7 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
817823
#endif
818824
mov x0, #0 // return UNW_ESUCCESS
819825

820-
#if __has_feature(ptrauth_calls)
826+
#if __has_ptrauth_calls
821827
retab
822828
#else
823829
ret

0 commit comments

Comments
 (0)