Skip to content

Commit ed105d0

Browse files
authored
Merge pull request swiftlang#69256 from kubamracek/embedded-arc-crash
[embedded] Fix an LLVMARCOpts crash by avoiding direct calls to swift_retain/swift_release
2 parents 60bad0c + 719697d commit ed105d0

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePoint
131131

132132
@_silgen_name("swift_retain")
133133
public func swift_retain(object: Builtin.RawPointer) -> Builtin.RawPointer {
134-
return swift_retain_n(object: object, n: 1)
134+
if Int(Builtin.ptrtoint_Word(object)) == 0 { return object }
135+
let o = UnsafeMutablePointer<HeapObject>(object)
136+
return swift_retain_n_(object: o, n: 1)._rawValue
135137
}
136138

137139
// Cannot use UnsafeMutablePointer<HeapObject>? directly in the function argument or return value as it causes IRGen crashes
@@ -155,7 +157,9 @@ func swift_retain_n_(object: UnsafeMutablePointer<HeapObject>, n: UInt32) -> Uns
155157

156158
@_silgen_name("swift_release")
157159
public func swift_release(object: Builtin.RawPointer) {
158-
swift_release_n(object: object, n: 1)
160+
if Int(Builtin.ptrtoint_Word(object)) == 0 { return }
161+
let o = UnsafeMutablePointer<HeapObject>(object)
162+
swift_release_n_(object: o, n: 1)
159163
}
160164

161165
@_silgen_name("swift_release_n")

test/embedded/ouroboros-bug.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for a "Ouroboros Bug": The ARC optimizer doesn't like the
2+
// presense of a direct call to swift_retain and swift_release in any Swift
3+
// code, but in the embedded Swift's runtime that's somewhat reasonable thing
4+
// to do (but is to be avoided because of this).
5+
6+
// RUN: %target-swift-frontend -target armv7-apple-none-macho -assert-config Debug -Osize -Xcc -D__MACH__ -emit-ir %s -enable-experimental-feature Embedded | %FileCheck %s
7+
// RUN: %target-swift-frontend -target arm64-apple-none-macho -assert-config Debug -Osize -Xcc -D__MACH__ -Xcc -D__arm64__ -Xcc -D__APPLE__ -emit-ir %s -enable-experimental-feature Embedded | %FileCheck %s
8+
9+
// REQUIRES: VENDOR=apple
10+
// REQUIRES: optimized_stdlib
11+
12+
public func test() {}
13+
test()
14+
15+
// CHECK: define {{.*}}i32 @main

0 commit comments

Comments
 (0)