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
|
// RUN: %empty-directory(%t)
//
// RUN: %target-clang -x c %S/Inputs/retain_release_wrappers.c -c -o %t/retain_release_wrappers.o
// RUN: %target-build-swift -enable-experimental-feature Extern %t/retain_release_wrappers.o %s -o %t/refcount_overflow
// RUN: %target-codesign %t/refcount_overflow
// RUN: %target-run %t/refcount_overflow
// REQUIRES: executable_test
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
import StdlibUnittest
// Declarations of runtime ABI refcounting functions.
@_extern(c)
func wrapper_swift_retain_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32) -> UnsafeMutableRawPointer
@_extern(c)
func wrapper_swift_release_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32)
@_extern(c)
func wrapper_swift_nonatomic_retain_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32) -> UnsafeMutableRawPointer
@_extern(c)
func wrapper_swift_nonatomic_release_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32)
@_extern(c)
func wrapper_swift_unownedRetain_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32) -> UnsafeMutableRawPointer
@_extern(c)
func wrapper_swift_unownedRelease_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32)
@_extern(c)
func wrapper_swift_nonatomic_unownedRetain_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32) -> UnsafeMutableRawPointer
@_extern(c)
func wrapper_swift_nonatomic_unownedRelease_n(_ obj: UnsafeMutableRawPointer, _ n: UInt32)
// Maximum legal retain count.
// 30 bits of extra retain count, plus 1 for the implicit retain.
let maxRC = (1 as UInt32) << 30
// Maximum retain count that fits inline on 32-bit.
let maxInlineRC32 = (1 as UInt32) << 22
// Maximum unowned count that fits inline on 32-bit.
let maxInlineURC32 = (1 as UInt32) << 7
var didDeinit = false
class C {
deinit {
didDeinit = true
}
}
// Get a pointer to an object with a single strong retain count. This prevents
// ARC from doing different things to us depending on optimization level.
func getTestObject() -> UnsafeMutableRawPointer {
let obj = C()
let ptr = unsafeBitCast(obj, to: UnsafeMutableRawPointer.self)
_ = wrapper_swift_retain_n(ptr, 1)
return ptr
}
// Balance the retain from getTestObject.
func releaseTestObject(_ obj: UnsafeMutableRawPointer) {
wrapper_swift_release_n(obj, 1)
}
let RefcountOverflowTests = TestSuite("RefcountOverflow")
RefcountOverflowTests.test("retain") {
do {
let obj = getTestObject()
_ = wrapper_swift_retain_n(obj, maxRC - 1)
wrapper_swift_release_n(obj, maxRC - 1)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("retain2") {
do {
let obj = getTestObject()
_ = wrapper_swift_retain_n(obj, maxRC - 1)
wrapper_swift_release_n(obj, maxRC - 1)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("nonatomic_retain") {
do {
let obj = getTestObject()
_ = wrapper_swift_nonatomic_retain_n(obj, maxRC - 1)
wrapper_swift_nonatomic_release_n(obj, maxRC - 1)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("retain one by one") {
do {
// Retain to near maxInlineRC32, then +1 over the limit.
let obj = getTestObject()
_ = wrapper_swift_retain_n(obj, maxInlineRC32 - 100)
for _ in 0..<200 {
_ = wrapper_swift_retain_n(obj, 1)
}
for _ in 0..<200 {
wrapper_swift_release_n(obj, 1)
}
wrapper_swift_release_n(obj, maxInlineRC32 - 100)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("nonatomic_retain one by one") {
do {
// Retain to near maxInlineRC32, then +1 over the limit.
let obj = getTestObject()
_ = wrapper_swift_nonatomic_retain_n(obj, maxInlineRC32 - 100)
for _ in 0..<200 {
_ = wrapper_swift_nonatomic_retain_n(obj, 1)
}
for _ in 0..<200 {
wrapper_swift_nonatomic_release_n(obj, 1)
}
wrapper_swift_nonatomic_release_n(obj, maxInlineRC32 - 100)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("unownedRetain") {
do {
let obj = getTestObject()
_ = wrapper_swift_unownedRetain_n(obj, maxRC - 1)
wrapper_swift_unownedRelease_n(obj, maxRC - 1)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("nonatomic_unownedRetain") {
do {
let obj = getTestObject()
_ = wrapper_swift_nonatomic_unownedRetain_n(obj, maxRC - 1)
wrapper_swift_nonatomic_unownedRelease_n(obj, maxRC - 1)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("unownedRetain one by one") {
do {
let obj = getTestObject()
let n = maxInlineURC32 * 2
for _ in 0..<n {
_ = wrapper_swift_unownedRetain_n(obj, 1)
}
for _ in 0..<n {
wrapper_swift_unownedRelease_n(obj, 1)
}
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("nonatomic_unownedRetain one by one") {
do {
let obj = getTestObject()
let n = maxInlineURC32 * 2
for _ in 0..<n {
_ = wrapper_swift_nonatomic_unownedRetain_n(obj, 1)
}
for _ in 0..<n {
wrapper_swift_nonatomic_unownedRelease_n(obj, 1)
}
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("unownedRetain moderate increment") {
do {
let obj = getTestObject()
let n = maxInlineURC32 * 3 / 4
_ = wrapper_swift_unownedRetain_n(obj, n)
_ = wrapper_swift_unownedRetain_n(obj, n)
wrapper_swift_unownedRelease_n(obj, n)
wrapper_swift_unownedRelease_n(obj, n)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
RefcountOverflowTests.test("nonatomic_unownedRetain moderate increment") {
do {
let obj = getTestObject()
let n = maxInlineURC32 * 3 / 4
_ = wrapper_swift_nonatomic_unownedRetain_n(obj, n)
_ = wrapper_swift_nonatomic_unownedRetain_n(obj, n)
wrapper_swift_nonatomic_unownedRelease_n(obj, n)
wrapper_swift_nonatomic_unownedRelease_n(obj, n)
releaseTestObject(obj)
}
expectTrue(didDeinit)
didDeinit = false
}
runAllTests()
|