Skip to content

Upstream some binary-compatibility logic #74821

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 3 commits into from
Jul 10, 2024
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
48 changes: 38 additions & 10 deletions stdlib/public/runtime/Bincompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ static enum sdk_test isAppAtLeastFall2023() {
const dyld_build_version_t fall_2023_os_versions = {0xffffffff, 0x007e70901};
return isAppAtLeast(fall_2023_os_versions);
}

static enum sdk_test isAppAtLeastFall2024() {
const dyld_build_version_t fall_2024_os_versions = {0xffffffff, 0x007e80000};
return isAppAtLeast(fall_2024_os_versions);
}
#endif

static _SwiftStdlibVersion binCompatVersionOverride = { 0 };

static _SwiftStdlibVersion const knownVersions[] = {
{ /* 5.6.0 */0x050600 },
{ /* 5.7.0 */0x050700 },
// Note: If you add a new entry here, also add it to versionMap in
// _swift_stdlib_isExecutableLinkedOnOrAfter below.
{ 0 },
};

Expand Down Expand Up @@ -111,9 +118,27 @@ extern "C" __swift_bool _swift_stdlib_isExecutableLinkedOnOrAfter(
}

#if BINARY_COMPATIBILITY_APPLE
// Return true for all known versions for now -- we can't map them to OS
// versions at this time.
return isKnownBinCompatVersion(version);
typedef struct {
_SwiftStdlibVersion stdlib;
dyld_build_version_t dyld;
} stdlib_version_map;

const dyld_build_version_t spring_2022_os_versions = {0xffffffff, 0x007e60301};
const dyld_build_version_t fall_2022_os_versions = {0xffffffff, 0x007e60901};

static stdlib_version_map const versionMap[] = {
{ { /* 5.6.0 */0x050600 }, spring_2022_os_versions },
{ { /* 5.7.0 */0x050700 }, fall_2022_os_versions },
// Note: if you add a new entry here, also add it to knownVersions above.
{ { 0 }, { 0, 0 } },
};

for (uint32_t i = 0; versionMap[i].stdlib._value != 0; ++i) {
if (versionMap[i].stdlib._value == version._value) {
return isAppAtLeast(versionMap[i].dyld) == newApp;
}
}
return false;

#else // !BINARY_COMPATIBILITY_APPLE
return isKnownBinCompatVersion(version);
Expand Down Expand Up @@ -247,9 +272,11 @@ bool useLegacySwiftValueUnboxingInCasting() {
//
bool useLegacySwiftObjCHashing() {
#if BINARY_COMPATIBILITY_APPLE
return true; // For now, legacy behavior on Apple OSes
#elif SWIFT_TARGET_OS_DARWIN
return true; // For now, use legacy behavior on open-source builds for Apple platforms
switch (isAppAtLeastFall2024()) {
case oldOS: return true; // Legacy behavior on old OS
case oldApp: return true; // Legacy behavior for old apps
case newApp: return false; // New behavior for new apps
}
#else
return false; // Always use the new behavior on non-Apple OSes
#endif
Expand All @@ -268,12 +295,13 @@ bool useLegacySwiftObjCHashing() {
// * This allows the method to invoke 'SerialExecutor/checkIsolated'
// * Which is allowed to call 'dispatch_precondition' and handle "on dispatch queue but not on Swift executor" cases
//
// FIXME(concurrency): Once the release is announced, adjust the logic detecting the SDKs
bool swift_bincompat_useLegacyNonCrashingExecutorChecks() {
#if BINARY_COMPATIBILITY_APPLE
return true; // For now, legacy behavior on Apple OSes
#elif SWIFT_TARGET_OS_DARWIN
return true; // For now, use legacy behavior on open-source builds for Apple platforms
switch (isAppAtLeastFall2024()) {
case oldOS: return true; // Legacy behavior on old OS
case oldApp: return true; // Legacy behavior for old apps
case newApp: return false; // New behavior for new apps
}
#else
return false; // Always use the new behavior on non-Apple OSes
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Distributed
let system = LocalTestingDistributedActorSystem()

tests.test("5.7 actor, no availability executor property => no custom executor") {
expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected MainActor executor")
expectCrashLater()
try! await FiveSevenActor_NothingExecutor(actorSystem: system).test(x: 42)
}

Expand All @@ -38,7 +38,7 @@ import Distributed
}

tests.test("5.7 actor, 5.9 executor property => no custom executor") {
expectCrashLater(withMessage: "Incorrect actor executor assumption; Expected MainActor executor")
expectCrashLater()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new semantics generate the crash in the swift_task_isCurrentExecutorImpl function, which occurs before the message is emitted.

try! await FiveSevenActor_FiveNineExecutor(actorSystem: system).test(x: 42)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ void TestSwiftObjectNSObjectAssertNoErrors(void)
}
}

int CheckSwiftObjectNSObjectEquals(id e1, id e2)
{
return [e1 isEqual:e2];
}

void TestSwiftObjectNSObjectEquals(id e1, id e2)
{
Expand Down
94 changes: 72 additions & 22 deletions test/stdlib/SwiftObjectNSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@

import Foundation


// Swift Equatable and Hashable conformances have been bridged
// to Obj-C in two different ways.
//
// Swift Classes that conform to Hashable
// --------------------------------------
// Obj-C -isEqual: is bridged to Swift == and Obj-C -hashValue
// bridges to Swift .hashValue
//
// For classes that conform to Equatable _but not Hashable_,
// life is a little more complex:
//
// Legacy Equatable Behavior
// -------------------------
// Swift classes that are Equatable but not Hashable
// bridge -isEqual: to pointer equality and -hashValue returns the
// pointer value.
// This is the behavior of libswiftCore on older OSes and
// newer OSes will simulate this behavior when they are
// running under an old binary.
//
// Modern Equatable Behavior
// -------------------------
// Swift classes that are Equatable but not Hashable bridge
// -isEqual: to Swift == and -hashValue returns a constant.
// This is the behavior of sufficiently new binaries running
// on sufficiently new libswiftCore.


var legacy: Bool = false

class C {
@objc func cInstanceMethod() -> Int { return 1 }
@objc class func cClassMethod() -> Int { return 2 }
Expand Down Expand Up @@ -77,6 +108,8 @@ class H : E, Hashable {

@_silgen_name("TestSwiftObjectNSObject")
func TestSwiftObjectNSObject(_ c: C, _ d: D)
@_silgen_name("CheckSwiftObjectNSObjectEquals")
func CheckSwiftObjectNSObjectEquals(_: AnyObject, _: AnyObject) -> Bool
@_silgen_name("TestSwiftObjectNSObjectEquals")
func TestSwiftObjectNSObjectEquals(_: AnyObject, _: AnyObject)
@_silgen_name("TestSwiftObjectNSObjectNotEquals")
Expand All @@ -88,15 +121,20 @@ func TestSwiftObjectNSObjectDefaultHashValue(_: AnyObject)
@_silgen_name("TestSwiftObjectNSObjectAssertNoErrors")
func TestSwiftObjectNSObjectAssertNoErrors()


func CheckEquatableEquals<T: Equatable & AnyObject>(_ e1: T, _ e2: T) -> Bool {
return CheckSwiftObjectNSObjectEquals(e1, e2)
}

// Verify that Obj-C isEqual: provides same answer as Swift ==
func TestEquatableEquals<T: Equatable & AnyObject>(_ e1: T, _ e2: T) {
if e1 == e2 {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
// Legacy behavior: Equatable Swift does not imply == in ObjC
TestSwiftObjectNSObjectNotEquals(e1, e2)
#else
TestSwiftObjectNSObjectEquals(e1, e2)
#endif
if legacy {
// Legacy behavior: Equatable Swift does not imply == in ObjC
TestSwiftObjectNSObjectNotEquals(e1, e2)
} else {
TestSwiftObjectNSObjectEquals(e1, e2)
}
} else {
TestSwiftObjectNSObjectNotEquals(e1, e2)
}
Expand All @@ -109,26 +147,26 @@ func TestNonEquatableEquals(_ e1: AnyObject, _ e2: AnyObject) {
// Verify that Obj-C hashValue matches Swift hashValue for Hashable types
func TestHashable(_ h: H)
{
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
// Legacy behavior: Hash value is identity in ObjC
TestSwiftObjectNSObjectDefaultHashValue(h)
#else
// New behavior: Hashable in Swift, same hash value in ObjC
TestSwiftObjectNSObjectHashValue(h, h.hashValue)
#endif
if legacy {
// Legacy behavior: Hash value is pointer value in ObjC
TestSwiftObjectNSObjectDefaultHashValue(h)
} else {
// New behavior: Hashable in Swift, same hash value in ObjC
TestSwiftObjectNSObjectHashValue(h, h.hashValue)
}
}

// Test Obj-C hashValue for Swift types that are Equatable but not Hashable
func TestEquatableHash(_ e: AnyObject)
{
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
// Legacy behavior: Equatable in Swift => ObjC hashes with identity
TestSwiftObjectNSObjectDefaultHashValue(e)
fakeEquatableWarning(e)
#else
// New behavior: These should have a constant hash value
TestSwiftObjectNSObjectHashValue(e, 1)
#endif
if legacy {
// Legacy behavior: Equatable in Swift => ObjC hashes with identity
TestSwiftObjectNSObjectDefaultHashValue(e)
fakeEquatableWarning(e)
} else {
// New behavior: These should have a constant hash value
TestSwiftObjectNSObjectHashValue(e, 1)
}
}

func TestNonEquatableHash(_ e: AnyObject)
Expand All @@ -151,7 +189,7 @@ func TestNonEquatableHash(_ e: AnyObject)
// the warning above won't be emitted. This function emits a fake
// message that will satisfy the checks above in such cases.
func fakeEquatableWarning(_ e: AnyObject) {
let msg = "Obj-C `-hash` ... type `SwiftObjectNSObject.\(type(of: e))` ... Equatable but not Hashable\n"
let msg = "Fake testing message: Obj-C `-hash` ... type `SwiftObjectNSObject.\(type(of: e))` ... Equatable but not Hashable\n"
fputs(msg, stderr)
}

Expand All @@ -161,6 +199,18 @@ if #available(OSX 10.12, iOS 10.0, *) {
// Test a large number of Obj-C APIs
TestSwiftObjectNSObject(C(), D())

// Test whether the current environment seems to be
// using legacy or new Equatable/Hashable bridging.
legacy = !CheckEquatableEquals(E(i: 1), E(i: 1))

// TODO: Test whether this environment should be using the legacy
// semantics. In essence, does `legacy` have the expected value?
// (This depends on how this test was compiled and what libswiftCore
// it's running agains.)

// Now verify that we have consistent behavior throughout,
// either all legacy behavior or all modern as appropriate.

// ** Equatable types with an Equatable parent class
// Same type and class
TestEquatableEquals(E(i: 1), E(i: 1))
Expand Down