File: borrow_cast_validate.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 (272 lines) | stat: -rw-r--r-- 11,105 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
// RUN: %target-sil-opt -sil-ownership-verifier-enable-testing -ownership-verifier-textual-error-dumper -enable-sil-verify-all=0 %s -o /dev/null 2>&1 | %FileCheck %s
// REQUIRES: asserts

import Builtin
import Swift

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

class Klass {}
struct Pair { var lhs: AnyObject; var rhs: AnyObject }

struct WrapperStruct {
  var cls : Klass
}

struct SmallCodesizeStruct {
  var cls1 : Klass
  var cls2 : Klass
}

sil shared [noinline] @blackhole_spl1 : $@convention(thin) (@guaranteed WrapperStruct) -> () {
bb0(%0 : $WrapperStruct):
  %1 = tuple ()
  return %1 : $()
}

sil shared [noinline] @blackhole_spl1a : $@convention(thin) (@guaranteed Optional<WrapperStruct>) -> () {
bb0(%0 : $Optional<WrapperStruct>):
  %1 = tuple ()
  return %1 : $()
}

sil shared [noinline] @blackhole_spl2a : $@convention(thin) (@guaranteed Klass) -> () {
bb0(%0 : $Klass):
  %1 = tuple ()
  return %1 : $()
}

sil shared [noinline] @blackhole_spl2b : $@convention(thin) (@guaranteed Optional<Klass>) -> () {
bb0(%0 : $Optional<Klass>):
  %1 = tuple ()
  return %1 : $()
}

sil shared [noinline] @blackhole_spl3 : $@convention(thin) (@guaranteed AnyObject) -> () {
bb0(%0 : $AnyObject):
  %1 = tuple ()
  return %1 : $()
}

sil shared [noinline] @blackhole_spl3a : $@convention(thin) (@guaranteed Pair) -> () {
bb0(%0 : $Pair):
  %1 = tuple ()
  return %1 : $()
}

sil shared [noinline] @blackhole_spl3b : $@convention(thin) (AnyObject) -> () {
bb0(%0 : $AnyObject):
  %1 = tuple ()
  return %1 : $()
}

// Borrow of forwarding casts are just like borrow of owned values
// Borrow of the cast dest can be verified like any other owned value
// Borrow of the cast src is only valid if its lifetime ends with end_borrow before the cast and can be verified like any other owned value
// CHECK-NOT: Function: 'forwarding_value_cast_correct'
sil [ossa] @forwarding_value_cast_correct : $@convention(thin) (@guaranteed Optional<WrapperStruct>) -> () {
bb0(%0 : @guaranteed $Optional<WrapperStruct>):
  %1 = copy_value %0 : $Optional<WrapperStruct>
  %cast = unchecked_value_cast %1 : $Optional<WrapperStruct> to $WrapperStruct
  %2 = begin_borrow %cast : $WrapperStruct
  %func = function_ref @blackhole_spl1 : $@convention(thin) (@guaranteed WrapperStruct) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed WrapperStruct) -> ()
  end_borrow %2 : $WrapperStruct
  destroy_value %cast : $WrapperStruct
  %res = tuple ()
  return %res : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'forwarding_value_cast_incorrect'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = copy_value %0 : $Optional<WrapperStruct>   // users: %3, %2
// CHECK: Consuming User:   %3 = unchecked_value_cast %1 : $Optional<WrapperStruct> to $WrapperStruct // user: %7
// CHECK: Non Consuming User:   end_borrow %2 : $Optional<WrapperStruct>        // id: %6
// CHECK: Block: bb0
// CHECK-LABEL: Error#: 0. End Error in Function: 'forwarding_value_cast_incorrect'
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'forwarding_value_cast_incorrect'
sil [ossa] @forwarding_value_cast_incorrect : $@convention(thin) (@guaranteed Optional<WrapperStruct>) -> () {
bb0(%0 : @guaranteed $Optional<WrapperStruct>):
  %1 = copy_value %0 : $Optional<WrapperStruct>
  %2 = begin_borrow %1 : $Optional<WrapperStruct>
  %cast = unchecked_value_cast %1 : $Optional<WrapperStruct> to $WrapperStruct
  %func = function_ref @blackhole_spl1a : $@convention(thin) (@guaranteed Optional<WrapperStruct>) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed Optional<WrapperStruct>) -> ()
  end_borrow %2 : $Optional<WrapperStruct>
  destroy_value %cast : $WrapperStruct
  %res = tuple ()
  return %res : $()
}

// CHECK-NOT: Function: 'forwarding_ref_cast_correct'
sil [ossa] @forwarding_ref_cast_correct : $@convention(thin) (@guaranteed Optional<Klass>) -> () {
bb0(%0 : @guaranteed $Optional<Klass>):
  %1 = copy_value %0 : $Optional<Klass>
  %cast = unchecked_ref_cast %1 : $Optional<Klass> to $Klass
  %2 = begin_borrow %cast : $Klass
  %func = function_ref @blackhole_spl2a : $@convention(thin) (@guaranteed Klass) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed Klass) -> ()
  end_borrow %2 : $Klass
  destroy_value %cast : $Klass
  %res = tuple ()
  return %res : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'forwarding_ref_cast_incorrect'
// CHECK: Found outside of lifetime use?!
// CHECK: Value:   %1 = copy_value %0 : $Optional<Klass>           // users: %3, %2
// CHECK: Consuming User:   %3 = unchecked_ref_cast %1 : $Optional<Klass> to $Klass // user: %7
// CHECK: Non Consuming User:   end_borrow %2 : $Optional<Klass>                // id: %6
// CHECK: Block: bb0
// CHECK-LABEL: Error#: 0. End Error in Function: 'forwarding_ref_cast_incorrect'
// CHECK-NOT: Error#: {{[0-9][0-9]*}}. End Error in Function: 'forwarding_ref_cast_incorrect'
sil [ossa] @forwarding_ref_cast_incorrect : $@convention(thin) (@guaranteed Optional<Klass>) -> () {
bb0(%0 : @guaranteed $Optional<Klass>):
  %1 = copy_value %0 : $Optional<Klass>
  %2 = begin_borrow %1 : $Optional<Klass>
  %cast = unchecked_ref_cast %1 : $Optional<Klass> to $Klass
  %func = function_ref @blackhole_spl2b : $@convention(thin) (@guaranteed Optional<Klass>) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed Optional<Klass>) -> ()
  end_borrow %2 : $Optional<Klass>
  destroy_value %cast : $Klass
  %res = tuple ()
  return %res : $()
}

// CHECK-LABEL: Error#: 0. Begin Error in Function: 'bitwise_cast_borrow_dest'
// CHECK: Have operand with incompatible ownership?!
// CHECK: Value:   %2 = unchecked_bitwise_cast %1 : $Pair to $AnyObject // user: %3
// CHECK: User:   %3 = begin_borrow %2 : $AnyObject               // users: %6, %5
// CHECK: Conv: unowned
// CHECK-LABEL: Error#: 0. End Error in Function: 'bitwise_cast_borrow_dest'
sil [ossa] @bitwise_cast_borrow_dest : $@convention(thin) (@guaranteed Pair) -> () {
bb0(%0 : @guaranteed $Pair):
  %1 = copy_value %0 : $Pair
  %cast = unchecked_bitwise_cast %1 : $Pair to $AnyObject
  %2 = begin_borrow %cast : $AnyObject
  %func = function_ref @blackhole_spl3 : $@convention(thin) (@guaranteed AnyObject) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed AnyObject) -> ()
  end_borrow %2 : $AnyObject
  destroy_value %1 : $Pair
  %res = tuple ()
  return %res : $()
}

// CHECK-NOT: Function: 'bitwise_cast_borrow_src'
sil [ossa] @bitwise_cast_borrow_src : $@convention(thin) (@guaranteed Pair) -> () {
bb0(%0 : @guaranteed $Pair):
  %1 = copy_value %0 : $Pair
  %2 = begin_borrow %1 : $Pair
  %cast = unchecked_bitwise_cast %1 : $Pair to $AnyObject
  %func = function_ref @blackhole_spl3a : $@convention(thin) (@guaranteed Pair) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed Pair) -> ()
  end_borrow %2 : $Pair
  destroy_value %1 : $Pair
  %res = tuple ()
  return %res : $()
}

// CHECK-NOT: Function: 'bitwise_cast_of_borrowed_val'
sil [ossa] @bitwise_cast_of_borrowed_val : $@convention(thin) (@guaranteed Pair) -> () {
bb0(%0 : @guaranteed $Pair):
  %1 = copy_value %0 : $Pair
  %2 = begin_borrow %1 : $Pair
  %cast = unchecked_bitwise_cast %2 : $Pair to $AnyObject
  %func = function_ref @blackhole_spl3a : $@convention(thin) (@guaranteed Pair) -> ()
  %call1 = apply %func(%2) : $@convention(thin) (@guaranteed Pair) -> ()
  end_borrow %2 : $Pair
  destroy_value %1 : $Pair
  %res = tuple ()
  return %res : $()
}

// Gap in verification. Verify unchecked_bitwise_cast correctly rdar://70558176
// CHECK-NOT: Function: 'bitwise_cast_use_after_free'
sil [ossa] @bitwise_cast_use_after_free : $@convention(thin) (@guaranteed Pair) -> () {
bb0(%0 : @guaranteed $Pair):
  %1 = copy_value %0 : $Pair
  %cast = unchecked_bitwise_cast %1 : $Pair to $AnyObject
  %func = function_ref @blackhole_spl3b : $@convention(thin) (AnyObject) -> ()
  destroy_value %1 : $Pair
  %call1 = apply %func(%cast) : $@convention(thin) (AnyObject) -> ()
  %res = tuple ()
  return %res : $()
}

// CHECK-NOT: Function: 'borrowed_cast_not_dominate_correct'
sil [ossa] @borrowed_cast_not_dominate_correct : $@convention(thin) (@guaranteed Optional<WrapperStruct>, @guaranteed Optional<WrapperStruct>) -> () {
bb0(%0 : @guaranteed $Optional<WrapperStruct>, %1 : @guaranteed $Optional<WrapperStruct>):
  cond_br undef, bb1, bb2

bb1:
  %copy0 = copy_value %0 : $Optional<WrapperStruct>
  %cast0 = unchecked_value_cast %copy0 : $Optional<WrapperStruct> to $WrapperStruct
  %borrow0 = begin_borrow %cast0 : $WrapperStruct
  br bb3(%borrow0 : $WrapperStruct, %cast0 : $WrapperStruct)

bb2:
  %copy1 = copy_value %1 : $Optional<WrapperStruct>
  %cast1 = unchecked_value_cast %copy1 : $Optional<WrapperStruct> to $WrapperStruct
  %borrow1 = begin_borrow %cast1 : $WrapperStruct
  br bb3(%borrow1 : $WrapperStruct, %cast1 : $WrapperStruct)

bb3(%newborrow : @guaranteed $WrapperStruct, %newowned : @owned $WrapperStruct):
  end_borrow %newborrow : $WrapperStruct
  destroy_value %newowned : $WrapperStruct
  %ret = tuple ()
  return %ret : $()
}

// CHECK-NOT: Function: 'borrowed_cast_dominates_one_path'
sil [ossa] @borrowed_cast_dominates_one_path : $@convention(thin) (@guaranteed Optional<WrapperStruct>, @guaranteed Optional<WrapperStruct>) -> () {
bb0(%0 : @guaranteed $Optional<WrapperStruct>, %1 : @guaranteed $Optional<WrapperStruct>):
  %copy1 = copy_value %1 : $Optional<WrapperStruct>
  cond_br undef, bb1, bb2

bb1:
  %copy0 = copy_value %0 : $Optional<WrapperStruct>
  %cast0 = unchecked_value_cast %copy0 : $Optional<WrapperStruct> to $WrapperStruct
  %borrow0 = begin_borrow %cast0 : $WrapperStruct
  br bb3(%borrow0 : $WrapperStruct, %cast0 : $WrapperStruct)

bb2:
  %copy2 = copy_value %1 : $Optional<WrapperStruct>
  %cast2 = unchecked_value_cast %copy2 : $Optional<WrapperStruct> to $WrapperStruct
  %borrow1 = begin_borrow %cast2 : $WrapperStruct
  br bb3(%borrow1 : $WrapperStruct, %cast2 : $WrapperStruct)

bb3(%newborrow : @guaranteed $WrapperStruct, %newowned : @owned $WrapperStruct):
  end_borrow %newborrow : $WrapperStruct
  destroy_value %newowned : $WrapperStruct
  destroy_value %copy1 : $Optional<WrapperStruct>
  %ret = tuple ()
  return %ret : $()
}

// CHECK-NOT: Function: 'borrowed_cast_dominates_one_path_loop'
sil [ossa] @borrowed_cast_dominates_one_path_loop : $@convention(thin) (@guaranteed Optional<WrapperStruct>) -> () {
bb0(%0 : @guaranteed $Optional<WrapperStruct>):
  %1b = copy_value %0 : $Optional<WrapperStruct>
  %cast0 = unchecked_value_cast %1b : $Optional<WrapperStruct> to $WrapperStruct
  %1a = begin_borrow %cast0 : $WrapperStruct
  br bb1(%1a : $WrapperStruct, %cast0 : $WrapperStruct)

bb1(%2a : @guaranteed $WrapperStruct, %2b : @owned $WrapperStruct):
  cond_br undef, bb2, bb3

bb2:
  %3 = copy_value %2a : $WrapperStruct
  %3a = begin_borrow %3 : $WrapperStruct
  end_borrow %2a : $WrapperStruct
  destroy_value %2b : $WrapperStruct
  br bb1(%3a : $WrapperStruct, %3 : $WrapperStruct)

bb3:
  end_borrow %2a : $WrapperStruct
  destroy_value %2b : $WrapperStruct
  %9999 = tuple()
  return %9999 : $()
}