1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
; RUN: llc -mtriple=aarch64--linux-gnu -mattr=+sve < %s | FileCheck %s
declare dso_local void @val_fn(<vscale x 4 x float>)
declare dso_local void @ptr_fn(ptr)
; An alloca of a scalable vector shouldn't trigger stack protection.
; CHECK-LABEL: call_value:
; CHECK-NOT: mov x19, sp
; CHECK: addvl sp, sp, #-1
; CHECK-NOT: __stack_chk_guard
; CHECK: str {{z[0-9]+}}, [x29, #-1, mul vl]
define void @call_value() #0 {
entry:
%x = alloca <vscale x 4 x float>, align 16
store <vscale x 4 x float> zeroinitializer, ptr %x, align 16
%0 = load <vscale x 4 x float>, ptr %x, align 16
call void @val_fn(<vscale x 4 x float> %0)
ret void
}
; CHECK-LABEL: call_value_strong:
; CHECK-NOT: mov x19, sp
; CHECK: addvl sp, sp, #-1
; CHECK-NOT: __stack_chk_guard
; CHECK: str {{z[0-9]+}}, [x29, #-1, mul vl]
define void @call_value_strong() #1 {
entry:
%x = alloca <vscale x 4 x float>, align 16
store <vscale x 4 x float> zeroinitializer, ptr %x, align 16
%0 = load <vscale x 4 x float>, ptr %x, align 16
call void @val_fn(<vscale x 4 x float> %0)
ret void
}
; Address-taking of a scalable vector should trigger stack protection only with
; sspstrong, and the scalable vector should be be placed below the stack guard.
; CHECK-LABEL: call_ptr:
; CHECK-NOT: mov x19, sp
; CHECK: addvl sp, sp, #-1
; CHECK-NOT: __stack_chk_guard
; CHECK: addvl x0, x29, #-1
; CHECK: bl ptr_fn
define void @call_ptr() #0 {
entry:
%x = alloca <vscale x 4 x float>, align 16
call void @ptr_fn(ptr %x)
ret void
}
; CHECK-LABEL: call_ptr_strong:
; CHECK: mov x29, sp
; CHECK: addvl sp, sp, #-2
; CHECK-DAG: addvl [[ADDR:x[0-9]+]], x29, #-1
; CHECK-DAG: ldr [[VAL:x[0-9]+]], [{{x[0-9]+}}, :lo12:__stack_chk_guard]
; CHECK-DAG: str [[VAL]], [[[ADDR]]]
; CHECK-DAG: addvl x0, x29, #-2
; CHECK: bl ptr_fn
define void @call_ptr_strong() #1 {
entry:
%x = alloca <vscale x 4 x float>, align 16
call void @ptr_fn(ptr %x)
ret void
}
; Check that both variables are addressed in the same way
; CHECK-LABEL: call_both:
; CHECK: mov x29, sp
; CHECK: addvl sp, sp, #-2
; CHECK-NOT: __stack_chk_guard
; CHECK: str {{z[0-9]+}}, [x29, #-1, mul vl]
; CHECK: bl val_fn
; CHECK: addvl x0, x29, #-2
; CHECK: bl ptr_fn
define void @call_both() #0 {
entry:
%x = alloca <vscale x 4 x float>, align 16
%y = alloca <vscale x 4 x float>, align 16
store <vscale x 4 x float> zeroinitializer, ptr %x, align 16
%0 = load <vscale x 4 x float>, ptr %x, align 16
call void @val_fn(<vscale x 4 x float> %0)
call void @ptr_fn(ptr %y)
ret void
}
; CHECK-LABEL: call_both_strong:
; CHECK: mov x29, sp
; CHECK: addvl sp, sp, #-3
; CHECK-DAG: addvl [[ADDR:x[0-9]+]], x29, #-1
; CHECK-DAG: ldr [[VAL:x[0-9]+]], [{{x[0-9]+}}, :lo12:__stack_chk_guard]
; CHECK-DAG: str [[VAL]], [[[ADDR]]]
; CHECK-DAG: str {{z[0-9]+}}, [x29, #-2, mul vl]
; CHECK: bl val_fn
; CHECK: addvl x0, x29, #-3
; CHECK: bl ptr_fn
define void @call_both_strong() #1 {
entry:
%x = alloca <vscale x 4 x float>, align 16
%y = alloca <vscale x 4 x float>, align 16
store <vscale x 4 x float> zeroinitializer, ptr %x, align 16
%0 = load <vscale x 4 x float>, ptr %x, align 16
call void @val_fn(<vscale x 4 x float> %0)
call void @ptr_fn(ptr %y)
ret void
}
; Pushed callee-saved regs should be above the stack guard
; CHECK-LABEL: callee_save:
; CHECK: mov x29, sp
; CHECK: addvl sp, sp, #-18
; CHECK: str {{z[0-9]+}}, [sp, #{{[0-9]+}}, mul vl]
; CHECK-NOT: mov x29, sp
; CHECK: addvl sp, sp, #-1
; CHECK-NOT: __stack_chk_guard
; CHECK: str {{z[0-9]+}}, [x29, #-19, mul vl]
define void @callee_save(<vscale x 4 x float> %x) #0 {
entry:
%x.addr = alloca <vscale x 4 x float>, align 16
store <vscale x 4 x float> %x, ptr %x.addr, align 16
call void @ptr_fn(ptr %x.addr)
ret void
}
; CHECK-LABEL: callee_save_strong:
; CHECK: mov x29, sp
; CHECK: addvl sp, sp, #-18
; CHECK: str {{z[0-9]+}}, [sp, #{{[0-9]+}}, mul vl]
; CHECK: addvl sp, sp, #-2
; CHECK-DAG: addvl [[ADDR:x[0-9]+]], x29, #-19
; CHECK-DAG: ldr [[VAL:x[0-9]+]], [{{x[0-9]+}}, :lo12:__stack_chk_guard]
; CHECK-DAG: str [[VAL]], [[[ADDR]]]
; CHECK-DAG: str z0, [x29, #-20, mul vl]
define void @callee_save_strong(<vscale x 4 x float> %x) #1 {
entry:
%x.addr = alloca <vscale x 4 x float>, align 16
store <vscale x 4 x float> %x, ptr %x.addr, align 16
call void @ptr_fn(ptr %x.addr)
ret void
}
; Check that local stack allocation works correctly both when we have a stack
; guard but no vulnerable SVE objects, and when we do have such objects.
; CHECK-LABEL: local_stack_alloc:
; CHECK: mov x29, sp
; CHECK: sub sp, sp, #16, lsl #12
; CHECK: sub sp, sp, #16
; CHECK: addvl sp, sp, #-2
; Stack guard is placed below the SVE stack area (and above all fixed-width objects)
; CHECK-DAG: add [[STACK_GUARD_SPILL_PART_LOC:x[0-9]+]], sp, #8, lsl #12
; CHECK-DAG: add [[STACK_GUARD_SPILL_PART_LOC]], [[STACK_GUARD_SPILL_PART_LOC]], #16
; CHECK-DAG: ldr [[STACK_GUARD:x[0-9]+]], [{{x[0-9]+}}, :lo12:__stack_chk_guard]
; CHECK-DAG: str [[STACK_GUARD]], [[[STACK_GUARD_SPILL_PART_LOC]], #32760]
; char_arr is below the stack guard
; CHECK-DAG: add [[CHAR_ARR_LOC:x[0-9]+]], sp, #16, lsl #12
; CHECK-DAG: strb wzr, [[[CHAR_ARR_LOC]]]
; large1 is accessed via a virtual base register
; CHECK-DAG: add [[LARGE1:x[0-9]+]], sp, #8, lsl #12
; CHECK-DAG: stp x0, x0, [[[LARGE1]]]
; large2 is at the bottom of the stack
; CHECK-DAG: stp x0, x0, [sp]
; vec1 and vec2 are in the SVE stack immediately below fp
; CHECK-DAG: addvl x0, x29, #-1
; CHECK-DAG: bl ptr_fn
; CHECK-DAG: addvl x0, x29, #-2
; CHECK-DAG: bl ptr_fn
define void @local_stack_alloc(i64 %val) #0 {
entry:
%char_arr = alloca [8 x i8], align 4
%gep0 = getelementptr [8 x i8], ptr %char_arr, i64 0, i64 0
store i8 0, ptr %gep0, align 8
%large1 = alloca [4096 x i64], align 8
%large2 = alloca [4096 x i64], align 8
%vec_1 = alloca <vscale x 4 x float>, align 16
%vec_2 = alloca <vscale x 4 x float>, align 16
%gep1 = getelementptr [4096 x i64], ptr %large1, i64 0, i64 0
%gep2 = getelementptr [4096 x i64], ptr %large1, i64 0, i64 1
store i64 %val, ptr %gep1, align 8
store i64 %val, ptr %gep2, align 8
%gep3 = getelementptr [4096 x i64], ptr %large2, i64 0, i64 0
%gep4 = getelementptr [4096 x i64], ptr %large2, i64 0, i64 1
store i64 %val, ptr %gep3, align 8
store i64 %val, ptr %gep4, align 8
call void @ptr_fn(ptr %vec_1)
call void @ptr_fn(ptr %vec_2)
ret void
}
; CHECK-LABEL: local_stack_alloc_strong:
; CHECK: mov x29, sp
; CHECK: sub sp, sp, #16, lsl #12
; CHECK: sub sp, sp, #16
; CHECK: addvl sp, sp, #-3
; Stack guard is placed at the top of the SVE stack area
; CHECK-DAG: ldr [[STACK_GUARD:x[0-9]+]], [{{x[0-9]+}}, :lo12:__stack_chk_guard]
; CHECK-DAG: addvl [[STACK_GUARD_POS:x[0-9]+]], x29, #-1
; CHECK-DAG: str [[STACK_GUARD]], [[[STACK_GUARD_POS]]]
; char_arr is below the SVE stack area
; CHECK-DAG: add [[CHAR_ARR:x[0-9]+]], sp, #15, lsl #12 // =61440
; CHECK-DAG: add [[CHAR_ARR]], [[CHAR_ARR]], #9
; CHECK-DAG: strb wzr, [[[CHAR_ARR]], #4095]
; large1 is accessed via a virtual base register
; CHECK-DAG: add [[LARGE1:x[0-9]+]], sp, #8, lsl #12
; CHECK-DAG: stp x0, x0, [[[LARGE1]], #8]
; large2 is at the bottom of the stack
; CHECK-DAG: stp x0, x0, [sp, #8]
; vec1 and vec2 are in the SVE stack area below the stack guard
; CHECK-DAG: addvl x0, x29, #-2
; CHECK-DAG: bl ptr_fn
; CHECK-DAG: addvl x0, x29, #-3
; CHECK-DAG: bl ptr_fn
define void @local_stack_alloc_strong(i64 %val) #1 {
entry:
%char_arr = alloca [8 x i8], align 4
%gep0 = getelementptr [8 x i8], ptr %char_arr, i64 0, i64 0
store i8 0, ptr %gep0, align 8
%large1 = alloca [4096 x i64], align 8
%large2 = alloca [4096 x i64], align 8
%vec_1 = alloca <vscale x 4 x float>, align 16
%vec_2 = alloca <vscale x 4 x float>, align 16
%gep1 = getelementptr [4096 x i64], ptr %large1, i64 0, i64 0
%gep2 = getelementptr [4096 x i64], ptr %large1, i64 0, i64 1
store i64 %val, ptr %gep1, align 8
store i64 %val, ptr %gep2, align 8
%gep3 = getelementptr [4096 x i64], ptr %large2, i64 0, i64 0
%gep4 = getelementptr [4096 x i64], ptr %large2, i64 0, i64 1
store i64 %val, ptr %gep3, align 8
store i64 %val, ptr %gep4, align 8
call void @ptr_fn(ptr %vec_1)
call void @ptr_fn(ptr %vec_2)
ret void
}
; A GEP addressing into a vector of <vscale x 4 x float> is in-bounds for
; offsets up to 3, but out-of-bounds (and so triggers stack protection with
; sspstrong) after that.
; CHECK-LABEL: vector_gep_3:
; CHECK-NOT: __stack_chk_guard
define void @vector_gep_3() #0 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 3
store float 0.0, ptr %gep, align 4
ret void
}
; CHECK-LABEL: vector_gep_4:
; CHECK-NOT: __stack_chk_guard
define void @vector_gep_4() #0 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 4
store float 0.0, ptr %gep, align 4
ret void
}
; CHECK-LABEL: vector_gep_twice:
; CHECK-NOT: __stack_chk_guard
define void @vector_gep_twice() #0 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep1 = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 3
store float 0.0, ptr %gep1, align 4
%gep2 = getelementptr float, ptr %gep1, i64 1
store float 0.0, ptr %gep2, align 4
ret void
}
; CHECK-LABEL: vector_gep_n:
; CHECK-NOT: __stack_chk_guard
define void @vector_gep_n(i64 %n) #0 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 %n
store float 0.0, ptr %gep, align 4
ret void
}
; CHECK-LABEL: vector_gep_3_strong:
; CHECK-NOT: __stack_chk_guard
define void @vector_gep_3_strong() #1 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 3
store float 0.0, ptr %gep, align 4
ret void
}
; CHECK-LABEL: vector_gep_4_strong:
; CHECK: __stack_chk_guard
define void @vector_gep_4_strong(i64 %val) #1 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 4
store float 0.0, ptr %gep, align 4
ret void
}
; CHECK-LABEL: vector_gep_twice_strong:
; CHECK: __stack_chk_guard
define void @vector_gep_twice_strong() #1 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep1 = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 3
store float 0.0, ptr %gep1, align 4
%gep2 = getelementptr float, ptr %gep1, i64 1
store float 0.0, ptr %gep2, align 4
ret void
}
; CHECK-LABEL: vector_gep_n_strong:
; CHECK: __stack_chk_guard
define void @vector_gep_n_strong(i64 %n) #1 {
entry:
%vec = alloca <vscale x 4 x float>, align 16
%gep = getelementptr <vscale x 4 x float>, ptr %vec, i64 0, i64 %n
store float 0.0, ptr %gep, align 4
ret void
}
attributes #0 = { ssp "frame-pointer"="non-leaf" }
attributes #1 = { sspstrong "frame-pointer"="non-leaf" }
!llvm.module.flags = !{!0}
!0 = !{i32 7, !"direct-access-external-data", i32 1}
|