File: over_consume.sil

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (294 lines) | stat: -rw-r--r-- 10,947 bytes parent folder | download
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
// RUN: %target-sil-opt -sil-ownership-verifier-enable-testing -ownership-verifier-textual-error-dumper -enable-sil-verify-all=0 -o /dev/null %s 2>&1 | %FileCheck %s
// REQUIRES: asserts

sil_stage canonical

import Builtin

//////////////////
// Declarations //
//////////////////

sil @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()

class RefWithInt {
  var value: Builtin.Int32

  init()
}

enum Optional<T> {
case some(T)
case none
}

protocol Error {}

struct NativeObjectPair {
  var obj1 : Builtin.NativeObject
  var obj2 : Builtin.NativeObject
}

class SuperKlass {
  func doSomething()
}

class Klass : SuperKlass {
}

typealias AnyObject = Builtin.AnyObject

class ClassProtConformingRef {}
protocol ClassProt : AnyObject {}
extension ClassProtConformingRef : ClassProt {}

///////////
// Tests //
///////////

// This checks if the dataflow verifier asserts when we have two consuming users
// in the same block.
// CHECK-LABEL: Function: 'double_consume_same_bb'
// CHECK: Found over consume?!
// CHECK: Value:   %0 = argument of bb0 : $Builtin.NativeObject
// CHECK: User:   destroy_value %0 : $Builtin.NativeObject
// CHECK: Block: bb0
sil [ossa] @double_consume_same_bb : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  destroy_value %0 : $Builtin.NativeObject
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// This test checks if the dataflow verifier asserts when there are two
// consuming users in chained blocks.
// CHECK-LABEL: Function: 'double_consume_jump_thread_blocks'
// CHECK: Found over consume?!
// CHECK: Value:   %0 = argument of bb0 : $Builtin.NativeObject
// CHECK: User:   destroy_value %0 : $Builtin.NativeObject
// CHECK: Block: bb0
sil [ossa] @double_consume_jump_thread_blocks : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  destroy_value %0 : $Builtin.NativeObject
  br bb1

bb1:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}


// We have a double consume, since we need to copy %0 before we store it.
// CHECK-LABEL: Function: 'double_consume_loop_test'
// CHECK: Found over consume?!
// CHECK: Value:   %0 = argument of bb0 : $Builtin.NativeObject
// CHECK: Block: bb0
sil [ossa] @double_consume_loop_test : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = alloc_stack $Builtin.NativeObject
  store %0 to [init] %1 : $*Builtin.NativeObject
  destroy_addr %1 : $*Builtin.NativeObject
  dealloc_stack %1 : $*Builtin.NativeObject
  br bb1

bb1:
  cond_br undef, bb2, bb5

bb2:
  cond_br undef, bb3, bb4

bb3:
  br bb1

bb4:
  br bb1

bb5:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// We have a consume of a guaranteed argument
// CHECK-LABEL: Function: 'consumed_guaranteed_arg'
// CHECK: Have operand with incompatible ownership?!
// CHECK: Value:   %0 = argument of bb0 : $Builtin.NativeObject
// CHECK: User:   destroy_value %0 : $Builtin.NativeObject
// CHECK: Conv: guaranteed
sil [ossa] @consumed_guaranteed_arg : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// We have a use of a borrowed value after an end_borrow. This is effectively a
// use after consume.
//
// CHECK-LABEL: Function: 'use_after_end_borrow'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject
// CHECK: Non Consuming User:   %4 = apply %2(%1) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
// CHECK: Block: bb0
sil [ossa] @use_after_end_borrow : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  %2 = function_ref @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
  end_borrow %1 : $Builtin.NativeObject
  apply %2(%1) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// We have a destroy value of an owned value before a borrow of the owned value
// has finished.
//
// CHECK-LABEL: Function: 'destroy_before_end_borrow'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %0 = argument of bb0 : $Builtin.NativeObject
// CHECK: Consuming User:   destroy_value %0 : $Builtin.NativeObject
// CHECK: Non Consuming User:   end_borrow %1 : $Builtin.NativeObject
// CHECK: Block: bb0
sil [ossa] @destroy_before_end_borrow : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  %2 = function_ref @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
  destroy_value %0 : $Builtin.NativeObject
  end_borrow %1 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Function: 'ref_element_addr_requires_borrow'
// CHECK: Have operand with incompatible ownership?!
// CHECK: Value:   %0 = argument of bb0 : $RefWithInt
// CHECK: User:   %1 = ref_element_addr %0 : $RefWithInt, #RefWithInt.value
// CHECK: Conv: owned
sil [ossa] @ref_element_addr_requires_borrow : $@convention(thin) (@owned RefWithInt) -> () {
bb0(%0 : @owned $RefWithInt):
  %1 = ref_element_addr %0 : $RefWithInt, #RefWithInt.value
  destroy_value %0 : $RefWithInt
  %9999 = tuple()
  return %9999 : $()
}

// Make sure that we catch that in the case where unchecked_enum_data is
// propagating forward @owned ownership, that we catch a double consumed.
//
// CHECK-LABEL: Function: 'unchecked_enum_data_propagates_ownership'
// CHECK: Found over consume?!
// CHECK: Value: %0 = argument of bb0 : $Optional<Builtin.NativeObject>
// CHECK: User:   %1 = unchecked_enum_data %0 : $Optional<Builtin.NativeObject>, #Optional.some!enumelt
// CHECK: Block: bb0
sil [ossa] @unchecked_enum_data_propagates_ownership : $@convention(thin) (@owned Optional<Builtin.NativeObject>) -> () {
bb0(%0 : @owned $Optional<Builtin.NativeObject>):
  %1 = unchecked_enum_data %0 : $Optional<Builtin.NativeObject>, #Optional.some!enumelt
  destroy_value %0 : $Optional<Builtin.NativeObject>
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Function: 'switch_enum_guaranteed_arg_outlives_original_value'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Optional<Builtin.NativeObject>
// CHECK: Consuming User:   end_borrow %1 : $Optional<Builtin.NativeObject>
// CHECK: Non Consuming User:   %5 = unchecked_ref_cast %3 : $Builtin.NativeObject to $Builtin.NativeObject
// CHECK: Block: bb1
sil [ossa] @switch_enum_guaranteed_arg_outlives_original_value : $@convention(thin) (@owned Optional<Builtin.NativeObject>) -> () {
bb0(%0 : @owned $Optional<Builtin.NativeObject>):
  %1 = begin_borrow %0 : $Optional<Builtin.NativeObject>
  switch_enum %1 : $Optional<Builtin.NativeObject>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%2 : @guaranteed $Builtin.NativeObject):
  end_borrow %1 : $Optional<Builtin.NativeObject>
  %2a = unchecked_ref_cast %2 : $Builtin.NativeObject to $Builtin.NativeObject
  br bb3

bb2:
  end_borrow %1 : $Optional<Builtin.NativeObject>
  br bb3

bb3:
  destroy_value %0 : $Optional<Builtin.NativeObject>
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Function: 'checked_cast_br_guaranteed_arg_outlives_original_value'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = begin_borrow %0 : $Builtin.NativeObject
// CHECK: Consuming User:   end_borrow %1 : $Builtin.NativeObject
// CHECK: Non Consuming User:   %9 = unchecked_ref_cast %7 : $Builtin.NativeObject to $Builtin.NativeObject
// CHECK: Block: bb2
sil [ossa] @checked_cast_br_guaranteed_arg_outlives_original_value : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = begin_borrow %0 : $Builtin.NativeObject
  checked_cast_br Builtin.NativeObject in %1 : $Builtin.NativeObject to SuperKlass, bb1, bb2

bb1(%2 : @guaranteed $SuperKlass):
  end_borrow %1 : $Builtin.NativeObject
  %2a = unchecked_ref_cast %2 : $SuperKlass to $SuperKlass
  br bb3

bb2(%3 : @guaranteed $Builtin.NativeObject):
  end_borrow %1 : $Builtin.NativeObject
  %3a = unchecked_ref_cast %3 : $Builtin.NativeObject to $Builtin.NativeObject
  br bb3

bb3:
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Function: 'consume_with_classmethod'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %2 = upcast %0 : $Klass to $SuperKlass
// CHECK: Consuming User:   store %2 to [init] %1 : $*SuperKlass
// CHECK: Non Consuming User:   %4 = class_method %2 : $SuperKlass, #SuperKlass.doSomething : (SuperKlass) -> () -> (), $@convention(method) (@guaranteed SuperKlass) -> ()
// CHECK: Block: bb0
sil [ossa] @consume_with_classmethod : $@convention(thin) (@owned Klass) -> () {
bb0(%0 : @owned $Klass):
  %1 = alloc_stack $SuperKlass
  %2 = upcast %0 : $Klass to $SuperKlass
  store %2 to [init] %1 : $*SuperKlass
  %3 = class_method %2 : $SuperKlass, #SuperKlass.doSomething : (SuperKlass) -> () -> (), $@convention(method) (@guaranteed SuperKlass) -> ()
  destroy_addr %1 : $*SuperKlass
  dealloc_stack %1 : $*SuperKlass
  %9999 = tuple()
  return %9999 : $()
}

sil [ossa] @eliminate_copy_try_apple_callee : $@convention(thin) (@owned Builtin.NativeObject) -> @error Error {
entry(%0 : @owned $Builtin.NativeObject):
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: Function: 'use_after_free_consume_in_same_block'
// CHECK: Found non consuming use outside of the lifetime being verified.
// CHECK: Value:   %3 = copy_value %2 : $Builtin.NativeObject
// CHECK: User:   %10 = apply %7(%3) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
sil [ossa] @use_after_free_consume_in_same_block : $@convention(thin) (@owned NativeObjectPair) -> @error Error {
bb0(%0 : @owned $NativeObjectPair):
  %1 = begin_borrow %0 : $NativeObjectPair
  %2 = struct_extract %1 : $NativeObjectPair, #NativeObjectPair.obj1
  %3 = copy_value %2 : $Builtin.NativeObject
  end_borrow %1 : $NativeObjectPair
  destroy_value %0 : $NativeObjectPair
  %4 = function_ref @eliminate_copy_try_apple_callee : $@convention(thin) (@owned Builtin.NativeObject) -> @error Error
  %5 = function_ref @guaranteed_user : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
  try_apply %4(%3) : $@convention(thin) (@owned Builtin.NativeObject) -> @error Error, normal bb1, error bb2

bb1(%errorEmptyTup: $()):
  apply %5(%3) : $@convention(thin) (@guaranteed Builtin.NativeObject) -> ()
  %9999 = tuple()
  return %9999 : $()

bb2(%error : @owned $Error):
  throw %error : $Error
}