Debian Codegraph @

clang/test/Sema/ptrauth-qualifier.c

raw
1 // RUN: %clang_cc1 -triple arm64-apple-ios -DIS_DARWIN -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
2 // RUN: %clang_cc1 -triple aarch64-linux-gnu -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
3
4 #if defined(IS_DARWIN) && !__has_extension(ptrauth_qualifier)
5 // This error means that the __ptrauth qualifier availability test says that it
6 // is not available. This error is not expected in the output, if it is seen
7 // there is a feature detection regression.
8 #error __ptrauth qualifier not enabled
9 #endif
10
11 #if defined(IS_DARWIN) && !__has_feature(ptrauth_qualifier)
12 // This error means that the __has_feature test for ptrauth_qualifier has
13 // failed, despite it being expected on darwin.
14 #error __ptrauth qualifier not enabled
15 #elif !defined(IS_DARWIN) && (__has_feature(ptrauth_qualifier) || __has_extension(ptrauth_qualifier))
16 #error ptrauth_qualifier labeled a feature on a non-darwin platform
17 #endif
18
19 #if !defined (__PTRAUTH__)
20 #error __PTRAUTH__ test macro not defined when ptrauth is enabled
21 #endif
22
23 #if __aarch64__
24 #define VALID_CODE_KEY 0
25 #define VALID_DATA_KEY 2
26 #define INVALID_KEY 200
27 #else
28 #error Provide these constants if you port this test
29 #endif
30
31 int * __ptrauth(VALID_DATA_KEY) valid0;
32 int *ptr0;
33
34 typedef int *intp;
35
36 int 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}}
41 int * (__ptrauth(VALID_DATA_KEY) invalid5); // expected-error{{expected identifier or '('}} expected-error{{expected ')'}} expected-note {{to match this '('}}
42 int *__ptrauth(VALID_DATA_KEY) __ptrauth(VALID_DATA_KEY) invalid6; // expected-error{{type 'int *__ptrauth(2,0,0)' is already '__ptrauth'-qualified}}
43 int * __ptrauth(VALID_DATA_KEY, 2) invalid7; // expected-error {{invalid address discrimination flag '2'; '__ptrauth' requires '0' or '1'}}
44 int * __ptrauth(VALID_DATA_KEY, -1) invalid8; // expected-error {{invalid address discrimination flag '-1'; '__ptrauth' requires '0' or '1'}}
45 int * __ptrauth(VALID_DATA_KEY, 1, -1) invalid9; // expected-error {{invalid extra discriminator flag '-1'; '__ptrauth' requires a value between '0' and '65535'}}
46 int * __ptrauth(VALID_DATA_KEY, 1, 100000) invalid10; // expected-error {{invalid extra discriminator flag '100000'; '__ptrauth' requires a value between '0' and '65535'}}
47 int * __ptrauth(VALID_DATA_KEY, 1, nonConstantGlobal) invalid12; // expected-error {{argument to '__ptrauth' must be an integer constant expression}}
48 int * __ptrauth(VALID_DATA_KEY, nonConstantGlobal, 1000) invalid13; // expected-error {{argument to '__ptrauth' must be an integer constant expression}}
49 int * __ptrauth(nonConstantGlobal, 1, 1000) invalid14; // expected-error{{expression is not an integer constant expression}}
50
51 int * __ptrauth(VALID_DATA_KEY) valid0;
52 int * __ptrauth(VALID_DATA_KEY) *valid1;
53 __ptrauth(VALID_DATA_KEY) intp valid2;
54 __ptrauth(VALID_DATA_KEY) intp *valid3;
55 intp __ptrauth(VALID_DATA_KEY) valid4;
56 intp __ptrauth(VALID_DATA_KEY) *valid5;
57 int * __ptrauth(VALID_DATA_KEY, 0) valid6;
58 int * __ptrauth(VALID_DATA_KEY, 1) valid7;
59 int * __ptrauth(VALID_DATA_KEY, (_Bool) 1) valid8;
60 int * __ptrauth(VALID_DATA_KEY, 1, 0) valid9;
61 int * __ptrauth(VALID_DATA_KEY, 1, 65535) valid10;
62
63 int * __ptrauth(VALID_DATA_KEY) array0[10];
64 int (* __ptrauth(VALID_DATA_KEY) array1)[10];
65
66 extern intp redeclaration0; // expected-note {{previous declaration}}
67 extern 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
69 extern intp redeclaration1; // expected-note {{previous declaration}}
70 extern 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
72 intp __ptrauth(VALID_DATA_KEY) redeclaration2; // expected-note {{previous definition}}
73 intp 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
75 intp __ptrauth(VALID_DATA_KEY) redeclaration3; // expected-note {{previous definition}}
76 intp 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
78 void 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)')}}
79 intp __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
81 static_assert(_Generic(typeof(valid0), int * __ptrauth(VALID_DATA_KEY) : 1, int * : 0, default : 0));
82 static_assert(_Generic(typeof(valid0), int * __ptrauth(VALID_CODE_KEY) : 0, default : 1));
83 static_assert(_Generic(typeof_unqual(valid0), int * __ptrauth(VALID_DATA_KEY) : 0, int * : 1, default : 0));
84 static_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
86 static_assert(_Generic(array0, int * __ptrauth(VALID_DATA_KEY) * : 1, default : 0));
87 static_assert(_Generic(*array1, int * : 1, default : 0));
88
89 void 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
105 void 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
114 static_assert(_Generic(typeof(overload_func(&ptr0)), int : 1, default : 0));
115 static_assert(_Generic(typeof(overload_func(&valid0)), float : 1, default : 0));
116
117 void 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
119 struct 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
123 union U0 { // expected-note 4 {{union U0' has subobjects that are non-trivial to copy}}
124 struct S0 s0;
125 };
126
127 struct S1 {
128 intp __ptrauth(1, 0, 50) f0;
129 };
130
131 union U1 {
132 struct S1 s1;
133 };
134
135 union 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
140 struct 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
144 union U3 { // expected-note 2 {{union U3' has subobjects that are non-trivial to copy}}
145 struct S2 s2;
146 };
147
148 struct S4 {
149 union U0 u0;
150 };
151
152 union 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}}
153 union U1 foo1(union U1);
154
155 union 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
157 union 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
159 struct 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}}