Debian Codegraph @

clang/test/CodeGen/memtag-globals-asm.cpp

raw
1 // REQUIRES: aarch64-registered-target
2
3 // RUN: %clang_cc1 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \
4 // RUN: -fsanitize=memtag-globals -o %t.out %s
5 // RUN: FileCheck %s --input-file=%t.out
6 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-A
7 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-B
8 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-C
9 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-D
10 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-E
11 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-F
12 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-G
13 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-H
14 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-I
15 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-J
16 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-K
17 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-L
18 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-M
19 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-N
20 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-O
21 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P
22 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q
23 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R
24 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S
25
26 // RUN: %clang_cc1 -O3 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \
27 // RUN: -fsanitize=memtag-globals -o %t.out %s
28 // RUN: FileCheck %s --input-file=%t.out
29 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-A
30 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-B
31 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-C
32 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-D
33 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-E
34 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-F
35 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-G
36 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-H
37 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-I
38 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-J
39 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-K
40 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-L
41 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-M
42 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-N
43 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-O
44 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P
45 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q
46 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R
47 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S
48
49 /// Ensure that emulated TLS also doesn't get sanitized.
50 // RUN: %clang_cc1 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \
51 // RUN: -fsanitize=memtag-globals -o - %s | FileCheck %s
52
53 // CHECK-A: .memtag global_int
54 // CHECK-A: .globl global_int
55 // CHECK-A: .p2align 4, 0x0
56 // CHECK-A: .zero 16
57 // CHECK-A: .size global_int, 16
58 int global_int;
59 // CHECK-B: .memtag _ZL9local_int
60 // CHECK-B: .local _ZL9local_int
61 // CHECK-B: .comm _ZL9local_int,16,16
62 static int local_int;
63
64 // CHECK-C: .memtag _ZL12local_buffer
65 // CHECK-C: .local _ZL12local_buffer
66 // CHECK-C: .comm _ZL12local_buffer,16,16
67 static char local_buffer[16];
68 // CHECK-D: .memtag _ZL22local_buffer_local_end
69 // CHECK-D: .p2align 4, 0x0
70 // CHECK-D: _ZL22local_buffer_local_end:
71 // CHECK-D: .xword _ZL12local_buffer+16
72 // CHECK-D: .zero 8
73 // CHECK-D: .size _ZL22local_buffer_local_end, 16
74 static char* local_buffer_local_end = &local_buffer[16];
75 // CHECK-E: .memtag local_buffer_global_end
76 // CHECK-E: .globl local_buffer_global_end
77 // CHECK-E .p2align 4, 0x0
78 // CHECK-E: local_buffer_global_end:
79 // CHECK-E: .xword _ZL12local_buffer+16
80 // CHECK-E: .zero 8
81 // CHECK-E: .size local_buffer_global_end, 16
82 char* local_buffer_global_end = &local_buffer[16];
83
84 // CHECK-F: .memtag global_buffer
85 // CHECK-F: .globl global_buffer
86 // CHECK-F: .p2align 4, 0x0
87 // CHECK-F: .zero 16
88 // CHECK-F: .size global_buffer, 16
89 char global_buffer[16];
90 // CHECK-G: .memtag _ZL23global_buffer_local_end
91 // CHECK-G: .p2align 4, 0x0
92 // CHECK-G: _ZL23global_buffer_local_end:
93 // CHECK-G: .xword global_buffer+16
94 // CHECK-G: .zero 8
95 // CHECK-G: .size _ZL23global_buffer_local_end, 16
96 static char* global_buffer_local_end = &global_buffer[16];
97 // CHECK-H: .memtag global_buffer_global_end
98 // CHECK-H: .p2align 4, 0x0
99 // CHECK-H: global_buffer_global_end:
100 // CHECK-H: .xword global_buffer+16
101 // CHECK-H: .size global_buffer_global_end, 16
102 char* global_buffer_global_end = &global_buffer[16];
103
104 // CHECK-S-NOT: .memtag zero_sized
105 struct empty {};
106 char zero_sized[0];
107
108 class MyClass {
109 public:
110 virtual ~MyClass() {}
111 static int my_class_int;
112 static const int my_class_const_int;
113 virtual int virtual_func() { return 1; }
114 };
115 // CHECK-I: .memtag _ZN7MyClass12my_class_intE
116 // CHECK-I: .globl _ZN7MyClass12my_class_intE
117 // CHECK-I: .p2align 4, 0x0
118 // CHECK-I: .zero 16
119 // CHECK-I: .size _ZN7MyClass12my_class_intE, 16
120 int MyClass::my_class_int;
121 // CHECK-NOT: .memtag _ZN7MyClass18my_class_const_intE
122 const int MyClass::my_class_const_int = 1;
123
124 // CHECK-J: .memtag global_my_class
125 // CHECK-J: .globl global_my_class
126 // CHECK-J: .p2align 4, 0x0
127 // CHECK-J: .zero 8
128 // CHECK-J: .size global_my_class, 16
129 MyClass global_my_class;
130 // CHECK-K: .memtag _ZL14local_my_class
131 // CHECK-K: .p2align 4, 0x0
132 // CHECK-K: .zero 8
133 // CHECK-K: .size _ZL14local_my_class, 16
134 static MyClass local_my_class;
135
136 // CHECK-NOT: .memtag _ZL18local_const_string
137 static const char local_const_string[] = "this is a local string";
138 // CHECK-L: .memtag _ZL12local_string
139 // CHECK-L: .p2align 4, 0x0
140 // CHECK-L: .zero 9
141 // CHECK-L: .size _ZL12local_string, 32
142 static char local_string[] = "this is a local string";
143
144 // CHECK-M: .memtag global_atomic_int
145 // CHECK-M: .globl global_atomic_int
146 // CHECK-M: .p2align 4, 0x0
147 // CHECK-M: .zero 16
148 // CHECK-M: .size global_atomic_int, 16
149 _Atomic(int) global_atomic_int;
150 // CHECK-N: .memtag _ZL16local_atomic_int
151 // CHECK-N: .local _ZL16local_atomic_int
152 // CHECK-N: .comm _ZL16local_atomic_int,16,16
153 static _Atomic(int) local_atomic_int;
154
155 union MyUnion {
156 int i;
157 char c;
158 };
159
160 // CHECK-O: .memtag global_union
161 // CHECK-O: .globl global_union
162 // CHECK-O: .p2align 4, 0x0
163 // CHECK-O: .zero 16
164 // CHECK-O: .size global_union, 16
165 MyUnion global_union;
166 // CHECK-P: .memtag _ZL11local_union
167 // CHECK-P: .local _ZL11local_union
168 // CHECK-P: .comm _ZL11local_union,16,16
169 static MyUnion local_union;
170
171 // CHECK-NOT: .memtag {{.*}}global_tls
172 thread_local int global_tls;
173 // CHECK-NOT: .memtag {{.*}}local_tls
174 static thread_local int local_tls;
175
176 /// Prevent the compiler from realising that non-const local variables are not
177 /// modified, and constant inlining into f().
178 const void* export_pointers(int c) {
179 switch (c) {
180 case 0: return &local_int;
181 case 1: return &local_buffer;
182 case 2: return &local_buffer_local_end;
183 case 3: return &global_buffer_local_end;
184 case 4: return &MyClass::my_class_int;
185 case 6: return &local_my_class;
186 case 8: return &local_string;
187 case 9: return &local_atomic_int;
188 case 10: return &local_union;
189 case 11: return &local_tls;
190 }
191 return nullptr;
192 }
193
194 /// Ensure that all tagged globals are loaded/referenced via. the GOT.
195 // CHECK-NOT: .memtag _Z1fi
196 // CHECK-Q: _Z1fi:
197 int f(int x) {
198 // CHECK-R: .memtag _ZZ1fiE12function_int
199 // CHECK-R: .local _ZZ1fiE12function_int
200 // CHECK-R: .comm _ZZ1fiE12function_int,16,16
201 static int function_int = 0;
202 /// Prevent non-const `f` from being promoted to a constant and inlined.
203 function_int += x;
204
205 return
206 // CHECK-Q-DAG: adrp [[REG_A:x[0-9]+]], :got:global_int
207 // CHECK-Q-DAG: ldr [[REG_A2:x[0-9]+]], [[[REG_A]], :got_lo12:global_int]
208 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_A2]]]
209 global_int +
210 // CHECK-Q-DAG: adrp [[REG_B:x[0-9]+]], :got:_ZL9local_int
211 // CHECK-Q-DAG: ldr [[REG_B2:x[0-9]+]], [[[REG_B]], :got_lo12:_ZL9local_int]
212 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_B2]]]
213 local_int +
214 // CHECK-Q-DAG: adrp [[REG_C:x[0-9]+]], :got:_ZL12local_buffer
215 // CHECK-Q-DAG: ldr [[REG_C2:x[0-9]+]], [[[REG_C]], :got_lo12:_ZL12local_buffer]
216 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_C2]]]
217 local_buffer[0] +
218 // CHECK-Q-DAG: adrp [[REG_D:x[0-9]+]], :got:_ZL22local_buffer_local_end
219 // CHECK-Q-DAG: ldr [[REG_D2:x[0-9]+]], [[[REG_D]], :got_lo12:_ZL22local_buffer_local_end]
220 // CHECK-Q-DAG: ldr [[REG_D3:x[0-9]+]], [[[REG_D2]]]
221 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_D3]], #-16]
222 local_buffer_local_end[-16] +
223 // CHECK-Q-DAG: adrp [[REG_E:x[0-9]+]], :got:local_buffer_global_end
224 // CHECK-Q-DAG: ldr [[REG_E2:x[0-9]+]], [[[REG_E]], :got_lo12:local_buffer_global_end]
225 // CHECK-Q-DAG: ldr [[REG_E3:x[0-9]+]], [[[REG_E2]]]
226 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_E3]], #-16]
227 local_buffer_global_end[-16] +
228 // CHECK-Q-DAG: adrp [[REG_F:x[0-9]+]], :got:global_buffer{{$}}
229 // CHECK-Q-DAG: ldr [[REG_F2:x[0-9]+]], [[[REG_F]], :got_lo12:global_buffer]
230 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_F2]]]
231 global_buffer[0] +
232 // CHECK-Q-DAG: adrp [[REG_G:x[0-9]+]], :got:_ZL23global_buffer_local_end
233 // CHECK-Q-DAG: ldr [[REG_G2:x[0-9]+]], [[[REG_G]], :got_lo12:_ZL23global_buffer_local_end]
234 // CHECK-Q-DAG: ldr [[REG_G3:x[0-9]+]], [[[REG_G2]]]
235 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_G3]], #-16]
236 global_buffer_local_end[-16] +
237 // CHECK-Q-DAG: adrp [[REG_H:x[0-9]+]], :got:global_buffer_global_end
238 // CHECK-Q-DAG: ldr [[REG_H2:x[0-9]+]], [[[REG_H]], :got_lo12:global_buffer_global_end]
239 // CHECK-Q-DAG: ldr [[REG_H3:x[0-9]+]], [[[REG_H2]]]
240 // CHECK-Q-DAG: ldursb {{.*}}, [[[REG_H3]], #-16]
241 global_buffer_global_end[-16] +
242 // CHECK-Q-DAG: adrp [[REG_I:x[0-9]+]], :got:_ZN7MyClass12my_class_intE
243 // CHECK-Q-DAG: ldr [[REG_I2:x[0-9]+]], [[[REG_I]], :got_lo12:_ZN7MyClass12my_class_intE]
244 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_I2]]]
245 MyClass::my_class_int +
246 /// Constant values - ignore.
247 MyClass::my_class_const_int +
248 global_my_class.virtual_func() +
249 local_my_class.virtual_func() +
250 local_const_string[0] +
251 // CHECK-Q-DAG: adrp [[REG_J:x[0-9]+]], :got:_ZL12local_string
252 // CHECK-Q-DAG: ldr [[REG_J2:x[0-9]+]], [[[REG_J]], :got_lo12:_ZL12local_string]
253 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_J2]]]
254 local_string[0] +
255 // CHECK-Q-DAG: adrp [[REG_K:x[0-9]+]], :got:_ZL16local_atomic_int
256 // CHECK-Q-DAG: ldr [[REG_K2:x[0-9]+]], [[[REG_K]], :got_lo12:_ZL16local_atomic_int]
257 // CHECK-Q-DAG: ldar {{.*}}, [[[REG_K2]]]
258 local_atomic_int +
259 // CHECK-Q-DAG: adrp [[REG_L:x[0-9]+]], :got:global_atomic_int
260 // CHECK-Q-DAG: ldr [[REG_L2:x[0-9]+]], [[[REG_L]], :got_lo12:global_atomic_int]
261 // CHECK-Q-DAG: ldar {{.*}}, [[[REG_L2]]]
262 global_atomic_int +
263 // CHECK-Q-DAG: adrp [[REG_M:x[0-9]+]], :got:global_union
264 // CHECK-Q-DAG: ldr [[REG_M2:x[0-9]+]], [[[REG_M]], :got_lo12:global_union]
265 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_M2]]]
266 global_union.i +
267 // CHECK-Q-DAG: adrp [[REG_N:x[0-9]+]], :got:_ZL11local_union
268 // CHECK-Q-DAG: ldr [[REG_N2:x[0-9]+]], [[[REG_N]], :got_lo12:_ZL11local_union]
269 // CHECK-Q-DAG: ldrsb {{.*}}, [[[REG_N2]]]
270 local_union.c +
271 /// Global variables - ignore.
272 global_tls +
273 local_tls +
274 // CHECK-Q-DAG: adrp [[REG_O:x[0-9]+]], :got:_ZZ1fiE12function_int
275 // CHECK-Q-DAG: ldr [[REG_O2:x[0-9]+]], [[[REG_O]], :got_lo12:_ZZ1fiE12function_int]
276 // CHECK-Q-DAG: ldr {{.*}}, [[[REG_O2]]]
277 function_int;
278 }
279
280 typedef void (*func_t)(void);
281 #define CONSTRUCTOR(section_name) \
282 __attribute__((used)) __attribute__((section(section_name)))
283
284 __attribute__((constructor(0))) void func_constructor() {}
285 CONSTRUCTOR(".init") func_t func_init = func_constructor;
286 CONSTRUCTOR(".fini") func_t func_fini = func_constructor;
287 CONSTRUCTOR(".ctors") func_t func_ctors = func_constructor;
288 CONSTRUCTOR(".dtors") func_t func_dtors = func_constructor;
289 CONSTRUCTOR(".init_array") func_t func_init_array = func_constructor;
290 CONSTRUCTOR(".fini_array") func_t func_fini_array = func_constructor;
291 CONSTRUCTOR(".preinit_array") func_t preinit_array = func_constructor;
292 CONSTRUCTOR("array_of_globals") int global1;
293 CONSTRUCTOR("array_of_globals") int global2;
294 CONSTRUCTOR("array_of_globals") int global_string;
295
296 // CHECK-NOT: .memtag func_constructor
297 // CHECK-NOT: .memtag func_init
298 // CHECK-NOT: .memtag func_fini
299 // CHECK-NOT: .memtag func_ctors
300 // CHECK-NOT: .memtag func_dtors
301 // CHECK-NOT: .memtag func_init_array
302 // CHECK-NOT: .memtag func_fini_array
303 // CHECK-NOT: .memtag preinit_array
304 // CHECK-NOT: .memtag global1
305 // CHECK-NOT: .memtag global2
306 // CHECK-NOT: .memtag global_string