160 lines · c
1// RUN: %clang_cc1 -triple arm64-apple-ios -DIS_DARWIN -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s2// RUN: %clang_cc1 -triple aarch64-linux-gnu -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s3 4#if defined(IS_DARWIN) && !__has_extension(ptrauth_qualifier)5// This error means that the __ptrauth qualifier availability test says that it6// is not available. This error is not expected in the output, if it is seen7// there is a feature detection regression.8#error __ptrauth qualifier not enabled9#endif10 11#if defined(IS_DARWIN) && !__has_feature(ptrauth_qualifier)12// This error means that the __has_feature test for ptrauth_qualifier has13// failed, despite it being expected on darwin.14#error __ptrauth qualifier not enabled15#elif !defined(IS_DARWIN) && (__has_feature(ptrauth_qualifier) || __has_extension(ptrauth_qualifier))16#error ptrauth_qualifier labeled a feature on a non-darwin platform17#endif18 19#if !defined (__PTRAUTH__)20#error __PTRAUTH__ test macro not defined when ptrauth is enabled21#endif22 23#if __aarch64__24#define VALID_CODE_KEY 025#define VALID_DATA_KEY 226#define INVALID_KEY 20027#else28#error Provide these constants if you port this test29#endif30 31int * __ptrauth(VALID_DATA_KEY) valid0;32int *ptr0;33 34typedef int *intp;35 36int nonConstantGlobal = 5;37 38__ptrauth(INVALID_KEY) int invalid2; // expected-error{{200 does not identify a valid pointer authentication key for the current target}}39__ptrauth(VALID_DATA_KEY) int invalid3; // expected-error {{'__ptrauth' qualifier only applies to pointer or pointer sized integer types; 'int' is invalid}}40__ptrauth(VALID_DATA_KEY) int *invalid4; // expected-error {{'__ptrauth' qualifier only applies to pointer or pointer sized integer types; 'int' is invalid}}41int * (__ptrauth(VALID_DATA_KEY) invalid5); // expected-error{{expected identifier or '('}} expected-error{{expected ')'}} expected-note {{to match this '('}}42int *__ptrauth(VALID_DATA_KEY) __ptrauth(VALID_DATA_KEY) invalid6; // expected-error{{type 'int *__ptrauth(2,0,0)' is already '__ptrauth'-qualified}}43int * __ptrauth(VALID_DATA_KEY, 2) invalid7; // expected-error {{invalid address discrimination flag '2'; '__ptrauth' requires '0' or '1'}}44int * __ptrauth(VALID_DATA_KEY, -1) invalid8; // expected-error {{invalid address discrimination flag '-1'; '__ptrauth' requires '0' or '1'}}45int * __ptrauth(VALID_DATA_KEY, 1, -1) invalid9; // expected-error {{invalid extra discriminator flag '-1'; '__ptrauth' requires a value between '0' and '65535'}}46int * __ptrauth(VALID_DATA_KEY, 1, 100000) invalid10; // expected-error {{invalid extra discriminator flag '100000'; '__ptrauth' requires a value between '0' and '65535'}}47int * __ptrauth(VALID_DATA_KEY, 1, nonConstantGlobal) invalid12; // expected-error {{argument to '__ptrauth' must be an integer constant expression}}48int * __ptrauth(VALID_DATA_KEY, nonConstantGlobal, 1000) invalid13; // expected-error {{argument to '__ptrauth' must be an integer constant expression}}49int * __ptrauth(nonConstantGlobal, 1, 1000) invalid14; // expected-error{{expression is not an integer constant expression}}50 51int * __ptrauth(VALID_DATA_KEY) valid0;52int * __ptrauth(VALID_DATA_KEY) *valid1;53__ptrauth(VALID_DATA_KEY) intp valid2;54__ptrauth(VALID_DATA_KEY) intp *valid3;55intp __ptrauth(VALID_DATA_KEY) valid4;56intp __ptrauth(VALID_DATA_KEY) *valid5;57int * __ptrauth(VALID_DATA_KEY, 0) valid6;58int * __ptrauth(VALID_DATA_KEY, 1) valid7;59int * __ptrauth(VALID_DATA_KEY, (_Bool) 1) valid8;60int * __ptrauth(VALID_DATA_KEY, 1, 0) valid9;61int * __ptrauth(VALID_DATA_KEY, 1, 65535) valid10;62 63int * __ptrauth(VALID_DATA_KEY) array0[10];64int (* __ptrauth(VALID_DATA_KEY) array1)[10];65 66extern intp redeclaration0; // expected-note {{previous declaration}}67extern intp __ptrauth(VALID_DATA_KEY) redeclaration0; // expected-error{{redeclaration of 'redeclaration0' with a different type: '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)') vs 'intp' (aka 'int *')}}68 69extern intp redeclaration1; // expected-note {{previous declaration}}70extern intp __ptrauth(VALID_DATA_KEY) redeclaration1; // expected-error{{redeclaration of 'redeclaration1' with a different type: '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)') vs 'intp' (aka 'int *')}}71 72intp __ptrauth(VALID_DATA_KEY) redeclaration2; // expected-note {{previous definition}}73intp redeclaration2 = 0; // expected-error{{redefinition of 'redeclaration2' with a different type: 'intp' (aka 'int *') vs '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)')}}74 75intp __ptrauth(VALID_DATA_KEY) redeclaration3; // expected-note {{previous definition}}76intp redeclaration3 = 0; // expected-error{{redefinition of 'redeclaration3' with a different type: 'intp' (aka 'int *') vs '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)')}}77 78void illegal0(intp __ptrauth(VALID_DATA_KEY)); // expected-error {{parameter type may not be qualified with '__ptrauth'; type is '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)')}}79intp __ptrauth(VALID_DATA_KEY) illegal1(void); // expected-error {{return type may not be qualified with '__ptrauth'; type is '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)')}}80 81static_assert(_Generic(typeof(valid0), int * __ptrauth(VALID_DATA_KEY) : 1, int * : 0, default : 0));82static_assert(_Generic(typeof(valid0), int * __ptrauth(VALID_CODE_KEY) : 0, default : 1));83static_assert(_Generic(typeof_unqual(valid0), int * __ptrauth(VALID_DATA_KEY) : 0, int * : 1, default : 0));84static_assert(_Generic(valid0, int * __ptrauth(VALID_DATA_KEY) : 0, int * : 1, default : 0)); // expected-warning {{association of type 'int *__ptrauth(2,0,0)' will never be selected}}85 86static_assert(_Generic(array0, int * __ptrauth(VALID_DATA_KEY) * : 1, default : 0));87static_assert(_Generic(*array1, int * : 1, default : 0));88 89void test_code(intp p) {90 p = (intp __ptrauth(VALID_DATA_KEY)) 0; // expected-error {{cannot cast to '__ptrauth'-qualified type '__ptrauth(2,0,0) intp' (aka 'int *__ptrauth(2,0,0)')}}91 92 __ptrauth(VALID_DATA_KEY) intp pSpecial = p;93 pSpecial = p;94 intp pNormal = pSpecial;95 pNormal = pSpecial;96 97 intp __ptrauth(VALID_DATA_KEY) *ppSpecial0 = &pSpecial;98 intp __ptrauth(VALID_DATA_KEY) *ppSpecial1 = &pNormal; // expected-error {{initializing '__ptrauth(2,0,0) intp *' (aka 'int *__ptrauth(2,0,0) *') with an expression of type 'intp *' (aka 'int **') changes pointer authentication of pointee type}}99 intp *ppNormal0 = &pSpecial; // expected-error {{initializing 'intp *' (aka 'int **') with an expression of type '__ptrauth(2,0,0) intp *' (aka 'int *__ptrauth(2,0,0) *') changes pointer authentication of pointee type}}100 intp *ppNormal1 = &pNormal;101 102 intp *pp5 = (p ? &pSpecial : &pNormal); // expected-error {{'__ptrauth' qualification mismatch ('__ptrauth(2,0,0) intp *' (aka 'int *__ptrauth(2,0,0) *') and 'intp *' (aka 'int **'))}}103}104 105void test_array(void) {106 intp __ptrauth(VALID_DATA_KEY) pSpecialArray[10];107 intp __ptrauth(VALID_DATA_KEY) *ppSpecial0 = pSpecialArray;108 intp __ptrauth(VALID_DATA_KEY) *ppSpecial1 = &pSpecialArray[0];109}110 111__attribute__((overloadable)) int overload_func(int **);112__attribute__((overloadable)) float overload_func(int * __ptrauth(VALID_DATA_KEY) *);113 114static_assert(_Generic(typeof(overload_func(&ptr0)), int : 1, default : 0));115static_assert(_Generic(typeof(overload_func(&valid0)), float : 1, default : 0));116 117void func(int array[__ptrauth(VALID_DATA_KEY) 10]); // expected-error {{'__ptrauth' qualifier only applies to pointer or pointer sized integer types; 'int[10]' is invalid}}118 119struct S0 { // expected-note 4 {{struct S0' has subobjects that are non-trivial to copy}}120 intp __ptrauth(1, 1, 50) f0; // expected-note 4 {{f0 has type '__ptrauth(1,1,50) intp' (aka 'int *__ptrauth(1,1,50)') that is non-trivial to copy}}121};122 123union U0 { // expected-note 4 {{union U0' has subobjects that are non-trivial to copy}}124 struct S0 s0;125};126 127struct S1 {128 intp __ptrauth(1, 0, 50) f0;129};130 131union U1 {132 struct S1 s1;133};134 135union U2 { // expected-note 2 {{union U2' has subobjects that are non-trivial to copy}}136 intp __ptrauth(1, 1, 50) f0; // expected-note 2 {{f0 has type '__ptrauth(1,1,50) intp' (aka 'int *__ptrauth(1,1,50)') that is non-trivial to copy}}137 intp __ptrauth(1, 0, 50) f1;138};139 140struct S2 { // expected-note 2 {{struct S2' has subobjects that are non-trivial to copy}}141 intp __ptrauth(1, 1, 50) f0[4]; // expected-note 2 {{f0 has type '__ptrauth(1,1,50) intp' (aka 'int *__ptrauth(1,1,50)') that is non-trivial to copy}}142};143 144union U3 { // expected-note 2 {{union U3' has subobjects that are non-trivial to copy}}145 struct S2 s2;146};147 148struct S4 {149 union U0 u0;150};151 152union U0 foo0(union U0); // expected-error {{cannot use type 'union U0' for function/method return since it is a union that is non-trivial to copy}} expected-error {{cannot use type 'union U0' for a function/method parameter since it is a union that is non-trivial to copy}}153union U1 foo1(union U1);154 155union U2 foo2(union U2); // expected-error {{cannot use type 'union U2' for function/method return since it is a union that is non-trivial to copy}} expected-error {{cannot use type 'union U2' for a function/method parameter since it is a union that is non-trivial to copy}}156 157union U3 foo3(union U3); // expected-error {{cannot use type 'union U3' for function/method return since it is a union that is non-trivial to copy}} expected-error {{cannot use type 'union U3' for a function/method parameter since it is a union that is non-trivial to copy}}158 159struct S4 foo4(struct S4); // expected-error {{cannot use type 'struct S4' for function/method return since it contains a union that is non-trivial to copy}} expected-error {{cannot use type 'struct S4' for a function/method parameter since it contains a union that is non-trivial to copy}}160