Skip to content

Commit cb8f571

Browse files
authored
Merge pull request #9697 from atrick/access
2 parents 63540b4 + 2898f6b commit cb8f571

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

test/SILOptimizer/access_enforcement_noescape.swift

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ func nestedNoEscape(f: inout Frob) {
6363
// Allow aliased noescape reads.
6464
func readRead() {
6565
var x = 3
66-
// Around the call: [read] [dynamic]
66+
// Around the call: [read] [static]
6767
// Inside each closure: [read] [static]
6868
doTwo({ _ = x }, { _ = x })
6969
x = 42
7070
}
7171
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape8readReadyyF : $@convention(thin) () -> () {
7272
// CHECK: [[ALLOC:%.*]] = alloc_stack $Int, var, name "x"
73-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[ALLOC]] : $*Int
73+
// CHECK-NOT: begin_access [read] [dynamic]
74+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [static] [[ALLOC]] : $*Int
75+
// CHECK-NOT: begin_access [read] [dynamic]
7476
// CHECK: apply
7577
// FIXME-CHECK: end_access [[ACCESS]]
7678
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape8readReadyyF'
@@ -121,18 +123,15 @@ func inoutReadRead(x: inout Int) {
121123
func readBoxRead() {
122124
var x = 3
123125
let c = { _ = x }
124-
// Around the call: [read] [dynamic]
125126
// Inside may-escape closure `c`: [read] [dynamic]
126-
// Inside never-escape closure: [read] [static]
127+
// Inside never-escape closure: [read] [dynamic]
127128
doTwo(c, { _ = x })
128129
x = 42
129130
}
130131
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape11readBoxReadyyF : $@convention(thin) () -> () {
131132
// CHECK: [[PA1:%.*]] = partial_apply
132133
// CHECK: [[PA2:%.*]] = partial_apply
133-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] %0 : $*Int
134134
// CHECK: apply %{{.*}}([[PA1]], [[PA2]])
135-
// FIXME-CHECK: end_access [[ACCESS]]
136135
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape11readBoxReadyyF'
137136

138137
// closure #1 in readBoxRead()
@@ -144,8 +143,9 @@ func readBoxRead() {
144143

145144
// closure #2 in readBoxRead()
146145
// CHECK-LABEL: sil private @_T027access_enforcement_noescape11readBoxReadyyFyycfU0_ : $@convention(thin) (@inout_aliasable Int) -> () {
147-
// CHECK-NOT: [[ACCESS:%.*]] = begin_access [read] [dynamic]
148-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0 : $*Int
146+
// FIXME-CHECK-LABEL: sil private @_T027access_enforcement_noescape11readBoxReadyyFyycfU0_ : $@convention(thin) (@owned { var Int }) -> () {
147+
// FIXME-CHECK: [[ADDR:%.*]] = project_box %0 : ${ var Int }, 0
148+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[ADDR]] : $*Int
149149
// FIXME-CHECK: end_access [[ACCESS]]
150150
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape11readBoxReadyyFyycfU0_'
151151

@@ -159,15 +159,15 @@ func readBoxRead() {
159159
// Allow aliased noescape read + write.
160160
func readWrite() {
161161
var x = 3
162-
// Around the call: [modify] [dynamic]
162+
// Around the call: [modify] [static]
163163
// Inside closure 1: [read] [static]
164164
// Inside closure 2: [modify] [static]
165165
doTwo({ _ = x }, { x = 42 })
166166
}
167167
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape9readWriteyyF : $@convention(thin) () -> () {
168168
// CHECK: [[PA1:%.*]] = partial_apply
169169
// CHECK: [[PA2:%.*]] = partial_apply
170-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %0 : $*Int
170+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0 : $*Int
171171
// CHECK: apply %{{.*}}([[PA1]], [[PA2]])
172172
// FIXME-CHECK: end_access [[ACCESS]]
173173
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape9readWriteyyF'
@@ -227,17 +227,15 @@ func inoutReadWrite(x: inout Int) {
227227
func readBoxWrite() {
228228
var x = 3
229229
let c = { _ = x }
230-
// Around the call: [modify] [dynamic]
231230
// Inside may-escape closure `c`: [read] [dynamic]
232-
// Inside never-escape closure: [modify] [static]
231+
// Inside never-escape closure: [modify] [dynamic]
233232
doTwo(c, { x = 42 })
234233
}
235234
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape12readBoxWriteyyF : $@convention(thin) () -> () {
236235
// CHECK: [[PA1:%.*]] = partial_apply
237236
// CHECK: [[PA2:%.*]] = partial_apply
238-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %0 : $*Int
237+
// CHECK-NOT: begin_access
239238
// CHECK: apply %{{.*}}([[PA1]], [[PA2]])
240-
// FIXME-CHECK: end_access [[ACCESS]]
241239
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape12readBoxWriteyyF'
242240

243241
// closure #1 in readBoxWrite()
@@ -249,8 +247,9 @@ func readBoxWrite() {
249247

250248
// closure #2 in readBoxWrite()
251249
// CHECK-LABEL: sil private @_T027access_enforcement_noescape12readBoxWriteyyFyycfU0_ : $@convention(thin) (@inout_aliasable Int) -> () {
252-
// CHECK-NOT: [[ACCESS:%.*]] = begin_access [modify] [dynamic]
253-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0 : $*Int
250+
// FIXME-CHECK-LABEL: sil private @_T027access_enforcement_noescape12readBoxWriteyyFyycfU0_ : $@convention(thin) (@owned { var Int }) -> () {
251+
// FIXME-CHECK: [[ADDR:%.*]] = project_box %0 : ${ var Int }, 0
252+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[ADDR]] : $*Int
254253
// FIXME-CHECK: end_access [[ACCESS]]
255254
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape12readBoxWriteyyFyycfU0_'
256255

@@ -266,18 +265,16 @@ func readBoxWrite() {
266265
func readWriteBox() {
267266
var x = 3
268267
let c = { x = 42 }
269-
// Around the call: [read] [dynamic]
270268
// Inside may-escape closure `c`: [modify] [dynamic]
271-
// Inside never-escape closure: [read] [static]
269+
// Inside never-escape closure: [read] [dynamic]
272270
doTwo({ _ = x }, c)
273271
}
274272

275273
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape12readWriteBoxyyF : $@convention(thin) () -> () {
276274
// CHECK: [[PA1:%.*]] = partial_apply
277275
// CHECK: [[PA2:%.*]] = partial_apply
278-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] %0 : $*Int
276+
// CHECK-NOT: begin_access
279277
// CHECK: apply %{{.*}}([[PA2]], [[PA1]])
280-
// FIXME-CHECK: end_access [[ACCESS]]
281278
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape12readWriteBoxyyF'
282279

283280
// closure #1 in readWriteBox()
@@ -289,8 +286,9 @@ func readWriteBox() {
289286

290287
// closure #2 in readWriteBox()
291288
// CHECK-LABEL: sil private @_T027access_enforcement_noescape12readWriteBoxyyFyycfU0_ : $@convention(thin) (@inout_aliasable Int) -> () {
292-
// CHECK-NOT: [[ACCESS:%.*]] = begin_access [read] [dynamic]
293-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0 : $*Int
289+
// FIXME-CHECK-LABEL: sil private @_T027access_enforcement_noescape12readWriteBoxyyFyycfU0_ : $@convention(thin) ((@owned { var Int }) -> () {
290+
// FIXME-CHECK: [[ADDR:%.*]] = project_box %0 : ${ var Int }, 0
291+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[ADDR]] : $*Int
294292
// FIXME-CHECK: end_access [[ACCESS]]
295293
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape12readWriteBoxyyFyycfU0_'
296294

@@ -303,15 +301,15 @@ func readWriteBox() {
303301
// Error: noescape read + write inout.
304302
func readWriteInout() {
305303
var x = 3
306-
// Around the call: [read] [dynamic]
304+
// Around the call: [read] [static]
307305
// Around the call: [modify] [static] // Error
308306
// Inside closure: [modify] [static]
309307
doOneInout({ _ = x }, &x)
310308
}
311309

312310
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape14readWriteInoutyyF : $@convention(thin) () -> () {
313311
// CHECK: [[PA1:%.*]] = partial_apply
314-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] %0 : $*Int
312+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [read] [static] %0 : $*Int
315313
// FIXME-CHECK: [[ACCESS2:%.*]] = begin_access [modify] [static] %0 : $*Int
316314
// FIXME-CHECK: apply %{{.*}}([[PA1]], [[ACCESS2]])
317315
// FIXME-CHECK: end_access [[ACCESS2]]
@@ -350,6 +348,7 @@ func inoutReadWriteInout(x: inout Int) {
350348
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape19inoutReadWriteInoutySiz1x_tFyycfU_'
351349

352350
// Trap on boxed read + write inout.
351+
// FIXME: Passing a captured var as inout needs dynamic enforcement.
353352
func readBoxWriteInout() {
354353
var x = 3
355354
let c = { _ = x }
@@ -381,7 +380,7 @@ func readBoxWriteInout() {
381380
// Allow aliased noescape write + write.
382381
func writeWrite() {
383382
var x = 3
384-
// Around the call: [modify] [dynamic]
383+
// Around the call: [modify] [static]
385384
// Inside closure 1: [modify] [static]
386385
// Inside closure 2: [modify] [static]
387386
doTwo({ x = 42 }, { x = 87 })
@@ -391,7 +390,7 @@ func writeWrite() {
391390
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape10writeWriteyyF : $@convention(thin) () -> () {
392391
// CHECK: [[PA1:%.*]] = partial_apply
393392
// CHECK: [[PA2:%.*]] = partial_apply
394-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %0 : $*Int
393+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0 : $*Int
395394
// FIXME-CHECK: apply %{{.*}}([[PA1]], [[PA2]])
396395
// FIXME-CHECK: end_access [[ACCESS]]
397396
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape10writeWriteyyF'
@@ -447,19 +446,17 @@ func inoutWriteWrite(x: inout Int) {
447446
func writeWriteBox() {
448447
var x = 3
449448
let c = { x = 87 }
450-
// Around the call: [modify] [dynamic]
451449
// Inside may-escape closure `c`: [modify] [dynamic]
452-
// Inside never-escape closure: [modify] [static]
450+
// Inside never-escape closure: [modify] [dynamic]
453451
doTwo({ x = 42 }, c)
454452
_ = x
455453
}
456454

457455
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape13writeWriteBoxyyF : $@convention(thin) () -> () {
458456
// CHECK: [[PA1:%.*]] = partial_apply
459457
// CHECK: [[PA2:%.*]] = partial_apply
460-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %0 : $*Int
458+
// FIXME-CHECK-NOT: begin_access
461459
// FIXME-CHECK: apply %{{.*}}([[PA1]], [[PA2]])
462-
// FIXME-CHECK: end_access [[ACCESS]]
463460
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape13writeWriteBoxyyF'
464461

465462
// closure #1 in writeWriteBox()
@@ -471,8 +468,8 @@ func writeWriteBox() {
471468

472469
// closure #2 in writeWriteBox()
473470
// CHECK-LABEL: sil private @_T027access_enforcement_noescape13writeWriteBoxyyFyycfU0_ : $@convention(thin) (@inout_aliasable Int) -> () {
474-
// CHECK-NOT: [[ACCESS:%.*]] = begin_access [modify] [dynamic]
475-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [static] %0 : $*Int
471+
// FIXME-CHECK: [[ADDR:%.*]] = project_box %0 : ${ var Int }, 0
472+
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] [[ADDR]] : $*Int
476473
// FIXME-CHECK: end_access [[ACCESS]]
477474
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape13writeWriteBoxyyFyycfU0_'
478475

@@ -482,21 +479,22 @@ func writeWriteBox() {
482479
// doTwo({ x = 42 }, c)
483480
// }
484481

485-
/// Error: on noescape write + write inout.
482+
// Error: on noescape write + write inout.
486483
func writeWriteInout() {
487484
var x = 3
488-
// Around the call: [modify] [dynamic]
485+
// Around the call: [modify] [static]
489486
// Around the call: [modify] [static] // Error
490487
// Inside closure: [modify] [static]
491488
doOneInout({ x = 42 }, &x)
492489
}
493490

494491
// CHECK-LABEL: sil hidden @_T027access_enforcement_noescape15writeWriteInoutyyF : $@convention(thin) () -> () {
495492
// CHECK: [[PA1:%.*]] = partial_apply
496-
// FIXME-CHECK: [[ACCESS:%.*]] = begin_access [modify] [dynamic] %0 : $*Int
493+
// FIXME-CHECK: [[ACCESS1:%.*]] = begin_access [modify] [static] %0 : $*Int
497494
// FIXME-CHECK: [[ACCESS2:%.*]] = begin_access [modify] [static] %0 : $*Int
498495
// FIXME-CHECK: apply %{{.*}}([[PA1]], [[ACCESS2]])
499-
// FIXME-CHECK: end_access [[ACCESS]]
496+
// FIXME-CHECK: end_access [[ACCESS2]]
497+
// FIXME-CHECK: end_access [[ACCESS1]]
500498
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape15writeWriteInoutyyF'
501499

502500
// closure #1 in writeWriteInout()
@@ -531,6 +529,7 @@ func inoutWriteWriteInout(x: inout Int) {
531529
// CHECK-LABEL: } // end sil function '_T027access_enforcement_noescape010inoutWriteE5InoutySiz1x_tFyycfU_'
532530

533531
// Trap on boxed write + write inout.
532+
// FIXME: Passing a captured var as inout needs dynamic enforcement.
534533
func writeBoxWriteInout() {
535534
var x = 3
536535
let c = { x = 42 }

test/SILOptimizer/access_enforcement_selection.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public func captureStackWithInoutInProgress() -> Int {
6565
// CHECK: Static Access: %{{.*}} = begin_access [read] [static] %{{.*}} : $*Int
6666

6767
// Generate an alloc_box that escapes into a closure.
68+
// FIXME: `x` is eventually promoted to an alloc_stack even though it has dynamic enforcement.
69+
// We should ensure that alloc_stack variables are statically enforced.
6870
public func captureBox() -> Int {
6971
var x = 3
7072
takeEscapingClosure { x = 4; return x }

test/SILOptimizer/exclusivity_static_diagnostics.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,21 @@ func violationWithGenericType<T>(_ p: T) {
8888
// expected-note@+1{{conflicting access is here}}
8989
takesTwoInouts(&local, &local)
9090
}
91+
92+
// Helper.
93+
struct StructWithTwoStoredProp {
94+
var f1: Int
95+
var f2: Int
96+
}
97+
98+
// Take an unsafe pointer to a stored property while accessing another stored property.
99+
func violationWithUnsafePointer(_ s: inout StructWithTwoStoredProp) {
100+
// FIXME: This needs to be statically enforced.
101+
withUnsafePointer(to: &s.f1) { (ptr) in
102+
_ = s.f1
103+
}
104+
// FIXME: We may want to allow this case for known-layout stored properties.
105+
withUnsafePointer(to: &s.f1) { (ptr) in
106+
_ = s.f2
107+
}
108+
}

0 commit comments

Comments
 (0)