From 6900efe42c23f605235c24cc764acadd21ab0d7a Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Wed, 18 Oct 2023 14:44:18 -0700 Subject: [PATCH] [embedded] Fix an LLVMARCOpts crash by avoiding direct calls to swift_retain/swift_release --- stdlib/public/core/EmbeddedRuntime.swift | 8 ++++++-- test/embedded/arc-crash.swift | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/embedded/arc-crash.swift diff --git a/stdlib/public/core/EmbeddedRuntime.swift b/stdlib/public/core/EmbeddedRuntime.swift index 1526eeb3b36..2be00540c45 100644 --- a/stdlib/public/core/EmbeddedRuntime.swift +++ b/stdlib/public/core/EmbeddedRuntime.swift @@ -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(object) + return swift_retain_n_(object: o, n: 1)._rawValue } // Cannot use UnsafeMutablePointer? directly in the function argument or return value as it causes IRGen crashes @@ -155,7 +157,9 @@ func swift_retain_n_(object: UnsafeMutablePointer, 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(object) + swift_release_n_(object: o, n: 1) } @_silgen_name("swift_release_n") diff --git a/test/embedded/arc-crash.swift b/test/embedded/arc-crash.swift new file mode 100644 index 00000000000..1f4ec45bddc --- /dev/null +++ b/test/embedded/arc-crash.swift @@ -0,0 +1,10 @@ +// 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