Skip to content

[embedded] Fix an LLVMARCOpts crash by avoiding direct calls to swift_retain/swift_release #69256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions stdlib/public/core/EmbeddedRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePoint

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

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

@_silgen_name("swift_release")
public func swift_release(object: Builtin.RawPointer) {
swift_release_n(object: object, n: 1)
if Int(Builtin.ptrtoint_Word(object)) == 0 { return }
let o = UnsafeMutablePointer<HeapObject>(object)
swift_release_n_(object: o, n: 1)
}

@_silgen_name("swift_release_n")
Expand Down
15 changes: 15 additions & 0 deletions test/embedded/ouroboros-bug.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Regression test for a "Ouroboros Bug": The ARC optimizer doesn't like the
// presense of a direct call to swift_retain and swift_release in any Swift
// code, but in the embedded Swift's runtime that's somewhat reasonable thing
// to do (but is to be avoided because of this).

// 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
// 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

// REQUIRES: VENDOR=apple
// REQUIRES: optimized_stdlib

public func test() {}
test()

// CHECK: define {{.*}}i32 @main