-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathaccess_enforcement_selection.sil
345 lines (311 loc) · 16.4 KB
/
access_enforcement_selection.sil
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
341
342
343
344
345
// RUN: %target-sil-opt -sil-print-types -access-enforcement-selection -enforce-exclusivity=checked %s | %FileCheck %s
import Builtin
import Swift
sil_stage raw
// Test undef begin_access operands.
// CHECK-LABEL: sil hidden [ossa] @undefStack : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
// CHECK: bb0(%0 : $Builtin.Int64):
// CHECK: bb1:
// CHECK: [[WRITE:%.*]] = begin_access [modify] [static] undef : $*Builtin.Int64
// CHECK: store %{{.*}} to [trivial] [[WRITE]] : $*Builtin.Int64
// CHECK: end_access [[WRITE]] : $*Builtin.Int64
// CHECK: br
// CHECK: bb2:
// CHECK: [[READ:%.*]] = begin_access [read] [static] undef : $*Builtin.Int64
// CHECK: %{{.*}} = load [trivial] [[READ]] : $*Builtin.Int64
// CHECK: end_access [[READ]] : $*Builtin.Int64
// CHECK-LABEL: } // end sil function 'undefStack'
sil hidden [ossa] @undefStack : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
bb0(%0 : $Builtin.Int64):
br bb1
bb1:
%23 = integer_literal $Builtin.Int64, 42
%25 = begin_access [modify] [unknown] undef : $*Builtin.Int64
store %23 to [trivial] %25 : $*Builtin.Int64
end_access %25 : $*Builtin.Int64
br bb2
bb2:
%29 = begin_access [read] [unknown] undef : $*Builtin.Int64
%30 = load [trivial] %29 : $*Builtin.Int64
end_access %29 : $*Builtin.Int64
return %30 : $Builtin.Int64
}
// Test static enforcement selection in the presence of mark_function_escape.
// This really isn't really a special case, but depends on pass pipeline.
//
// CHECK-LABEL: sil hidden [ossa] @markFuncEscape : $@convention(thin) () -> () {
// CHECK: bb0:
// CHECK: [[BOX:%.*]] = alloc_box ${ var Builtin.Int64 }, var, name "x"
// CHECK: [[ADR:%.*]] = project_box [[BOX]] : ${ var Builtin.Int64 }, 0
// CHECK: mark_function_escape [[ADR]] : $*Builtin.Int64
// CHECK: [[READ:%.*]] = begin_access [read] [static] [[ADR]] : $*Builtin.Int64
// CHECK: %{{.*}} = load [trivial] [[READ]] : $*Builtin.Int64
// CHECK: end_access [[READ]] : $*Builtin.Int64
// CHECK: destroy_value [[BOX]] : ${ var Builtin.Int64 }
// CHECK: return %{{.*}} : $()
// CHECK-LABEL:} // end sil function 'markFuncEscape'
sil hidden [ossa] @markFuncEscape : $@convention(thin) () -> () {
%2 = alloc_box ${ var Builtin.Int64 }, var, name "x"
%3 = project_box %2 : ${ var Builtin.Int64 }, 0
mark_function_escape %3 : $*Builtin.Int64
%39 = begin_access [read] [unknown] %3 : $*Builtin.Int64
%40 = load [trivial] %39 : $*Builtin.Int64
end_access %39 : $*Builtin.Int64
destroy_value %2 : ${ var Builtin.Int64 }
%98 = tuple ()
return %98 : $()
}
sil [ossa] @takesInoutAndClosure : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
sil [ossa] @closureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
// Test static enforcement of box addresses that escape via closure
// partial_applys.
// application.
// CHECK-LABEL: sil hidden [ossa] @escapeAsArgumentToPartialApply : $@convention(thin) () -> () {
// CHECK: bb0:
// CHECK: [[BOX:%.*]] = alloc_box ${ var Builtin.Int64 }, var, name "x"
// CHECK: [[ADR:%.*]] = project_box [[BOX]] : ${ var Builtin.Int64 }, 0
// CHECK: [[FUNC:%.*]] = function_ref @takesInoutAndClosure : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
// CHECK: [[CLOSURE:%.*]] = function_ref @closureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
// CHECK: [[PA:%.*]] = partial_apply [callee_guaranteed] [[CLOSURE]]([[ADR]]) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
// CHECK: [[MODIFY:%.*]] = begin_access [modify] [static] [[ADR]] : $*Builtin.Int64
// CHECK: %{{.*}} = apply [[FUNC]]([[MODIFY]], [[PA]]) : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
// CHECK: end_access [[MODIFY]] : $*Builtin.Int64
// CHECK: destroy_value [[PA]]
// CHECK: destroy_value [[BOX]] : ${ var Builtin.Int64 }
// CHECK: return %{{.*}} : $()
// CHECK-LABEL:} // end sil function 'escapeAsArgumentToPartialApply'
sil hidden [ossa] @escapeAsArgumentToPartialApply : $@convention(thin) () -> () {
bb0:
%2 = alloc_box ${ var Builtin.Int64 }, var, name "x"
%3 = project_box %2 : ${ var Builtin.Int64 }, 0
%4 = function_ref @takesInoutAndClosure : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
%5 = function_ref @closureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%6 = partial_apply [callee_guaranteed] %5(%3) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%7 = begin_access [modify] [unknown] %3 : $*Builtin.Int64
%8 = apply %4(%7, %6) : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
end_access %7 : $*Builtin.Int64
destroy_value %6 : $@callee_guaranteed () -> ()
destroy_value %2 : ${ var Builtin.Int64 }
%9 = tuple ()
return %9 : $()
}
sil [dynamically_replacable] [ossa] @closureCapturingByStorageAddress2 : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
// Make sure that we handle dynamic_function_ref.
sil hidden [ossa] @escapeAsArgumentToPartialApplyDynamic : $@convention(thin) () -> () {
bb0:
%2 = alloc_box ${ var Builtin.Int64 }, var, name "x"
%3 = project_box %2 : ${ var Builtin.Int64 }, 0
%4 = function_ref @takesInoutAndClosure : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
%5 = dynamic_function_ref @closureCapturingByStorageAddress2 : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%6 = partial_apply [callee_guaranteed] %5(%3) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%7 = begin_access [modify] [unknown] %3 : $*Builtin.Int64
%8 = apply %4(%7, %6) : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
end_access %7 : $*Builtin.Int64
destroy_value %6 : $@callee_guaranteed () -> ()
destroy_value %2 : ${ var Builtin.Int64 }
%9 = tuple ()
return %9 : $()
}
// Test static enforcement of copied boxes.
// FIXME: Oops... We make this dynamic.
//
// CHECK-LABEL: sil hidden [ossa] @copyBox : $@convention(thin) () -> () {
// CHECK: bb0:
// CHECK: [[BOX:%.*]] = alloc_box ${ var Builtin.Int64 }, var, name "x"
// CHECK: [[ADR1:%.*]] = project_box [[BOX]] : ${ var Builtin.Int64 }, 0
// CHECK: [[CPY:%.*]] = copy_value [[BOX]] : ${ var Builtin.Int64 }
// CHECK: [[ADR2:%.*]] = project_box [[CPY]] : ${ var Builtin.Int64 }, 0
// CHECK: [[READ:%.*]] = begin_access [read] [dynamic] [[ADR2]] : $*Builtin.Int64
// CHECK: %{{.*}} = load [trivial] [[READ]] : $*Builtin.Int64
// CHECK: end_access [[READ]] : $*Builtin.Int64
// CHECK: [[READ:%.*]] = begin_access [read] [dynamic] [[ADR1]] : $*Builtin.Int64
// CHECK: %{{.*}} = load [trivial] [[READ]] : $*Builtin.Int64
// CHECK: end_access [[READ]] : $*Builtin.Int64
// CHECK: destroy_value [[CPY]] : ${ var Builtin.Int64 }
// CHECK: destroy_value [[BOX]] : ${ var Builtin.Int64 }
// CHECK: return %{{.*}} : $()
// CHECK-LABEL: } // end sil function 'copyBox'
sil hidden [ossa] @copyBox : $@convention(thin) () -> () {
%2 = alloc_box ${ var Builtin.Int64 }, var, name "x"
%3 = project_box %2 : ${ var Builtin.Int64 }, 0
%16 = copy_value %2 : ${ var Builtin.Int64 }
%17 = project_box %16 : ${ var Builtin.Int64 }, 0
%18 = begin_access [read] [unknown] %17 : $*Builtin.Int64
%19 = load [trivial] %18 : $*Builtin.Int64
end_access %18 : $*Builtin.Int64
%39 = begin_access [read] [unknown] %3 : $*Builtin.Int64
%40 = load [trivial] %39 : $*Builtin.Int64
end_access %39 : $*Builtin.Int64
destroy_value %16 : ${ var Builtin.Int64 }
destroy_value %2 : ${ var Builtin.Int64 }
%98 = tuple ()
return %98 : $()
}
sil [ossa] @closure : $@convention(thin) (@owned { var Builtin.Int64 }) -> () {
bb0(%0 : @owned ${ var Builtin.Int64 }):
destroy_value %0 : ${ var Builtin.Int64 }
%empty = tuple ()
return %empty : $()
}
// An access that escapes on an unreachable path must be dynamic.
//
// CHECK-LABEL: sil [ossa] @partialUnreachable : $@convention(thin) () -> () {
// CHECK: %[[ACCESS:.*]] = begin_access [modify] [dynamic] %{{.*}} : $*Builtin.Int64
// CHECK: bb1:
// CHECK: end_access %[[ACCESS]] : $*Builtin.Int64
// CHECK: return
// CHECK: bb2:
// CHECK: partial_apply
// CHECK: unreachable
sil [ossa] @partialUnreachable : $@convention(thin) () -> () {
bb0:
%box = alloc_box ${ var Builtin.Int64 }, var, name "x"
%addr = project_box %box : ${ var Builtin.Int64 }, 0
%write = begin_access [modify] [unknown] %addr : $*Builtin.Int64
cond_br undef, bb1, bb2
bb1:
end_access %write : $*Builtin.Int64
destroy_value %box : ${ var Builtin.Int64 }
%empty = tuple ()
return %empty : $()
bb2:
%f = function_ref @closure : $@convention(thin) (@owned { var Builtin.Int64 }) -> ()
%closure = partial_apply %f(%box) : $@convention(thin) (@owned { var Builtin.Int64 }) -> ()
unreachable
}
// An access that refers to mark_uninitialized.
//
// CHECK-LABEL: sil [ossa] @markUninitSource : $@convention(thin) () -> () {
sil [ossa] @markUninitSource : $@convention(thin) () -> () {
bb0:
%stk = alloc_stack $Builtin.Int64, let, name "x"
%var = mark_uninitialized [var] %stk : $*Builtin.Int64
%f = function_ref @closureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%closure = partial_apply %f(%var) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
unreachable
}
// Borrowed closure can be statically checked.
//
// CHECK-LABEL: sil [ossa] @borrowedClosure : $@convention(thin) (@inout_aliasable Builtin.Int64) -> () {
// CHECK: begin_access [read] [static]
// CHECK-LABEL: } // end sil function 'borrowedClosure'
sil [ossa] @borrowedClosure : $@convention(thin) (@inout_aliasable Builtin.Int64) -> () {
bb0(%0 : $*Builtin.Int64):
%access = begin_access [read] [unknown] %0 : $*Builtin.Int64
%val = load [trivial] %access : $*Builtin.Int64
end_access %access : $*Builtin.Int64
%empty = tuple ()
return %empty : $()
}
// Borrow an escaping closure. The closure body will be dynamically checked.
sil [ossa] @borrowClosure : $@convention(thin) () -> () {
%box = alloc_box ${ var Builtin.Int64 }, var, name "x"
%addr = project_box %box : ${ var Builtin.Int64 }, 0
// box escapes.
%copy = copy_value %box : ${ var Builtin.Int64 }
%f = function_ref @borrowedClosure : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%closure = partial_apply [callee_guaranteed] %f(%addr) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
// closure is borrowed.
%borrow = begin_borrow %closure : $@callee_guaranteed () -> ()
%closure_copy = copy_value %borrow : $@callee_guaranteed () -> ()
end_borrow %borrow : $@callee_guaranteed () -> ()
destroy_value %closure_copy : $@callee_guaranteed () -> ()
destroy_value %closure : $@callee_guaranteed () -> ()
destroy_value %copy : ${ var Builtin.Int64 }
destroy_value %box : ${ var Builtin.Int64 }
%empty = tuple ()
return %empty : $()
}
sil [ossa] @dontAssert : $@convention(thin) (Builtin.Int64) -> (@out Builtin.Int64) {
bb0(%0 : $*Builtin.Int64, %1 : $Builtin.Int64):
store %1 to [trivial] %0 : $*Builtin.Int64
%f = function_ref @closureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%closure = partial_apply %f(%0) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
unreachable
}
sil [canonical] [ossa] @serializedClosureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> () {
bb0(%0 : $*Builtin.Int64):
%2 = begin_access [read] [unknown] %0 : $*Builtin.Int64
%3 = load [trivial] %2 : $*Builtin.Int64
end_access %2 : $*Builtin.Int64
%10 = tuple ()
return %10 : $()
}
// A begin_access may not be used by a partial_apply or a nested
// begin_access prior to mandatory inlining. Nonetheless, this does
// occur in deserialized SIL. This SIL is only well-formed because the
// function is marked [canonical].
sil [canonical] [ossa] @accessAroundClosure : $@convention(thin) () -> () {
bb0:
%1 = alloc_box ${ var Builtin.Int64 }, var, name "x"
%2 = copy_value %1 : ${ var Builtin.Int64 }
%3 = project_box %1 : ${ var Builtin.Int64 }, 0
// outer access (presumably its uses were mandatory inlined)
%4 = begin_access [modify] [static] %3 : $*Builtin.Int64
%5 = function_ref @serializedClosureCapturingByStorageAddress : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%6 = partial_apply [callee_guaranteed] %5(%4) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%7 = begin_access [modify] [static] %4 : $*Builtin.Int64
%8 = function_ref @takesInoutAndClosure : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
%9 = apply %8(%7, %6) : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
end_access %7 : $*Builtin.Int64
end_access %4 : $*Builtin.Int64
destroy_value %6 : $@callee_guaranteed () -> ()
destroy_value %2 : ${ var Builtin.Int64 }
destroy_value %1 : ${ var Builtin.Int64 }
%10 = tuple ()
return %10 : $()
}
// -----------------------------------------------------------------------------
// <rdar://problem/43122715> [Exclusivity] failure to diagnose escaping closures
// called within directly applied noescape closures.
//
// The top level function, testDirectApplyNoescapeDynamic, has no
// access markers, but it does define a box that escapes into two
// closures. Since one of those captures is a box, they both need
// dynamic enforcement. The unique thing about this case is that the
// non-escaping closure is directly applied, so has no partial_apply
// to indicate the presence of a captured argument.
sil [noinline] @takeInoutAndPerform : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
// CHECK-LABEL: sil [ossa] @testDirectApplyNoescapeDynamic : $@convention(thin) (Builtin.Int64) -> () {
// CHECK-LABEL: // end sil function 'testDirectApplyNoescapeDynamic'
sil [ossa] @testDirectApplyNoescapeDynamic : $@convention(thin) (Builtin.Int64) -> () {
bb0(%0 : $Builtin.Int64):
%box = alloc_box ${ var Builtin.Int64 }, var, name "localVal"
%boxproj = project_box %box : ${ var Builtin.Int64 }, 0
store %0 to [trivial] %boxproj : $*Builtin.Int64
%closureF = function_ref @directApplyNoescapeDynamicClosure : $@convention(thin) (@guaranteed { var Builtin.Int64 }) -> ()
%boxCopy = copy_value %box : ${ var Builtin.Int64 }
%closure = partial_apply [callee_guaranteed] %closureF(%boxCopy) : $@convention(thin) (@guaranteed { var Builtin.Int64 }) -> ()
%directF = function_ref @directApplyNoescapeDynamicAppliedClosure : $@convention(thin) (@guaranteed @callee_guaranteed () -> (), @inout_aliasable Builtin.Int64) -> ()
%call = apply %directF(%closure, %boxproj) : $@convention(thin) (@guaranteed @callee_guaranteed () -> (), @inout_aliasable Builtin.Int64) -> ()
destroy_value %closure : $@callee_guaranteed () -> ()
destroy_value %box : ${ var Builtin.Int64 }
%v = tuple ()
return %v : $()
}
// CHECK-LABEL: sil private [ossa] @directApplyNoescapeDynamicClosure : $@convention(thin) (@guaranteed { var Builtin.Int64 }) -> () {
// CHECK: [[BOX:%.*]] = project_box %0 : ${ var Builtin.Int64 }, 0
// CHECK: begin_access [modify] [dynamic] [[BOX]] : $*Builtin.Int64
// CHECK-LABEL: // end sil function 'directApplyNoescapeDynamicClosure'
sil private [ossa] @directApplyNoescapeDynamicClosure : $@convention(thin) (@guaranteed { var Builtin.Int64 }) -> () {
bb0(%0 : @guaranteed ${ var Builtin.Int64 }):
%1 = project_box %0 : ${ var Builtin.Int64 }, 0
%4 = integer_literal $Builtin.Int64, 3
%7 = begin_access [modify] [unknown] %1 : $*Builtin.Int64
assign %4 to %7 : $*Builtin.Int64
end_access %7 : $*Builtin.Int64
%10 = tuple ()
return %10 : $()
}
// CHECK-LABEL: sil private [ossa] @directApplyNoescapeDynamicAppliedClosure : $@convention(thin) (@guaranteed @callee_guaranteed () -> (), @inout_aliasable Builtin.Int64) -> () {
// CHECK: begin_access [modify] [dynamic] %1 : $*Builtin.Int64
// CHECK-LABEL: end sil function 'directApplyNoescapeDynamicAppliedClosure'
sil private [ossa] @directApplyNoescapeDynamicAppliedClosure : $@convention(thin) (@guaranteed @callee_guaranteed () -> (), @inout_aliasable Builtin.Int64) -> () {
bb0(%0 : @guaranteed $@callee_guaranteed () -> (), %1 : $*Builtin.Int64):
%4 = begin_access [modify] [unknown] %1 : $*Builtin.Int64
%5 = function_ref @takesInoutAndClosure : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
%6 = apply %5(%4, %0) : $@convention(thin) (@inout Builtin.Int64, @guaranteed @callee_guaranteed () -> ()) -> ()
end_access %4 : $*Builtin.Int64
%8 = tuple ()
return %8 : $()
}