-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathArrayTrapsObjC.swift
215 lines (187 loc) · 6.36 KB
/
ArrayTrapsObjC.swift
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
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out_Debug -Onone -swift-version 4.2 && %target-codesign %t/a.out_Debug && %target-run %t/a.out_Debug
// RUN: %target-build-swift %s -o %t/a.out_Release -O -swift-version 4.2 && %target-codesign %t/a.out_Release && %target-run %t/a.out_Release
// REQUIRES: executable_test
// REQUIRES: objc_interop
// Temporarily disable for backdeployment (rdar://89821303)
// UNSUPPORTED: use_os_stdlib
// rdar://121343746
// UNSUPPORTED: OS=xros
import StdlibUnittest
import Foundation
let testSuiteSuffix = _isDebugAssertConfiguration() ? "_debug" : "_release"
var ArrayTraps = TestSuite("ArrayTraps" + testSuiteSuffix)
class Base { }
class Derived : Base { }
class Derived2 : Derived { }
ArrayTraps.test("downcast1")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
let ba: [Base] = [ Derived(), Base() ]
let da = ba as! [Derived]
_ = da[0]
expectCrashLater()
_ = da[1]
}
ArrayTraps.test("downcast2")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
let a: [AnyObject] = ["String" as NSString, 1 as NSNumber]
let sa = a as! [NSString]
_ = sa[0]
expectCrashLater()
_ = sa[1]
}
ArrayTraps.test("downcast3")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
let ba: [Base] = [ Derived2(), Derived(), Base() ]
let d2a = ba as! [Derived2]
_ = d2a[0]
let d1a = d2a as [Derived]
_ = d1a[0]
_ = d1a[1]
expectCrashLater()
_ = d1a[2]
}
@objc protocol ObjCProto { }
class ObjCBase : NSObject, ObjCProto { }
class ObjCDerived : ObjCBase { }
ArrayTraps.test("downcast4")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.code {
let ba: [ObjCProto] = [ ObjCDerived(), ObjCBase() ]
let da = ba as! [ObjCDerived]
_ = da[0]
expectCrashLater()
_ = da[1]
}
ArrayTraps.test("bounds_with_downcast")
.skip(.custom(
{ _isFastAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -Ounchecked"))
.crashOutputMatches(_isDebugAssertConfiguration() ?
"Fatal error: Index out of range" : "")
.code {
let ba: [Base] = [ Derived(), Base() ]
let da = ba as! [Derived]
expectCrashLater()
_ = da[2]
}
func hasBackdeployedConcurrencyRuntime() -> Bool {
// If the stdlib we've loaded predates Swift 5.5, then we're running on a back
// deployed concurrency runtime, which has the side effect of disabling
// regular runtime exclusivity checks.
//
// This makes the two tests below fall back to older, higher-level exclusivity
// checks in the stdlib, which will still trap, but with a different message.
if #available(SwiftStdlib 5.5, *) { return false } // recent enough production stdlib
if #available(SwiftStdlib 9999, *) { return false } // dev stdlib
return true
}
var ArraySemanticOptzns = TestSuite("ArraySemanticOptzns" + testSuiteSuffix)
class BaseClass {
}
class ElementClass : BaseClass {
var val: String
init(_ x: String) {
val = x
}
}
class ViolateInoutSafetySwitchToObjcBuffer {
final var anArray: [ElementClass] = []
let nsArray = NSArray(
objects: ElementClass("a"), ElementClass("b"), ElementClass("c"))
@inline(never)
func accessArrayViaInoutViolation() {
anArray = nsArray as! [ElementClass]
}
@inline(never)
func runLoop(_ A: inout [ElementClass]) {
// Simulate what happens if we hoist array properties out of a loop and the
// loop calls a function that violates inout safety and overrides the array.
let isNativeTypeChecked = A._hoistableIsNativeTypeChecked()
for i in 0..<A.count {
// Note: the compiler is sometimes able to eliminate this
// `_checkSubscript` call when optimizations are enabled, skipping the
// exclusivity check contained within.
let t = A._checkSubscript(
i, wasNativeTypeChecked: isNativeTypeChecked)
_ = A._getElement(
i, wasNativeTypeChecked: isNativeTypeChecked, matchingSubscriptCheck: t)
accessArrayViaInoutViolation()
}
}
@inline(never)
func inoutViolation() {
anArray = [ ElementClass("1"), ElementClass("2"), ElementClass("3") ]
runLoop(&anArray)
}
}
ArraySemanticOptzns.test("inout_rule_violated_isNativeBuffer")
.skip(.custom(
{ !_isDebugAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -O or -Ounchecked"))
.crashOutputMatches(
hasBackdeployedConcurrencyRuntime()
? "inout rules were violated"
: "Fatal access conflict detected."
)
.code {
let v = ViolateInoutSafetySwitchToObjcBuffer()
expectCrashLater()
v.inoutViolation()
}
class ViolateInoutSafetyNeedElementTypeCheck {
final var anArray : [ElementClass] = []
@inline(never)
func accessArrayViaInoutViolation() {
// Overwrite the array with one that needs an element type check.
let ba: [BaseClass] = [ BaseClass(), BaseClass() ]
anArray = ba as! [ElementClass]
}
@inline(never)
func runLoop(_ A: inout [ElementClass]) {
// Simulate what happens if we hoist array properties out of a loop and the
// loop calls a function that violates inout safety and overrides the array.
let isNativeTypeChecked = A._hoistableIsNativeTypeChecked()
for i in 0..<A.count {
// Note: the compiler is sometimes able to eliminate this
// `_checkSubscript` call when optimizations are enabled, skipping the
// exclusivity check contained within.
let t = A._checkSubscript(
i, wasNativeTypeChecked: isNativeTypeChecked)
_ = A._getElement(
i, wasNativeTypeChecked: isNativeTypeChecked, matchingSubscriptCheck: t)
accessArrayViaInoutViolation()
}
}
@inline(never)
func inoutViolation() {
anArray = [ ElementClass("1"), ElementClass("2"), ElementClass("3")]
runLoop(&anArray)
}
}
ArraySemanticOptzns.test("inout_rule_violated_needsElementTypeCheck")
.skip(.custom(
{ !_isDebugAssertConfiguration() },
reason: "this trap is not guaranteed to happen in -O or -Ounchecked"))
.crashOutputMatches(
hasBackdeployedConcurrencyRuntime()
? "inout rules were violated"
: "Fatal access conflict detected."
)
.code {
let v = ViolateInoutSafetyNeedElementTypeCheck()
expectCrashLater()
v.inoutViolation()
}
runAllTests()