Skip to content

Commit 5c61e44

Browse files
authored
Merge pull request #21125 from apple/is-swift-bit-5
[5.0][runtime] Fix some bugs when the stable ABI's is-Swift bit is set.
2 parents 468f5b0 + bf62690 commit 5c61e44

File tree

7 files changed

+40
-26
lines changed

7 files changed

+40
-26
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This file is processed by CMake.
2+
// See https://cmake.org/cmake/help/v3.0/command/configure_file.html.
3+
4+
#ifndef SWIFT_RUNTIME_CMAKECONFIG_H
5+
6+
#cmakedefine01 SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT
7+
8+
#endif

include/swift/Runtime/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
configure_file(CMakeConfig.h.in ${CMAKE_CURRENT_BINARY_DIR}/CMakeConfig.h
2+
ESCAPE_QUOTES @ONLY)

include/swift/Runtime/Config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef SWIFT_RUNTIME_CONFIG_H
1818
#define SWIFT_RUNTIME_CONFIG_H
1919

20+
#include "swift/Runtime/CMakeConfig.h"
21+
2022
/// \macro SWIFT_RUNTIME_GNUC_PREREQ
2123
/// Extend the default __GNUC_PREREQ even if glibc's features.h isn't
2224
/// available.

stdlib/public/runtime/CMakeLists.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
set(swift_runtime_compile_flags ${SWIFT_RUNTIME_CORE_CXX_FLAGS})
22
set(swift_runtime_linker_flags ${SWIFT_RUNTIME_CORE_LINK_FLAGS})
33

4-
if(SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT)
5-
list(APPEND swift_runtime_compile_flags
6-
"-DSWIFT_DARWIN_ENABLE_STABLE_ABI_BIT=1")
7-
endif()
8-
94
if(SWIFT_RUNTIME_CLOBBER_FREED_OBJECTS)
105
list(APPEND swift_runtime_compile_flags
116
"-DSWIFT_RUNTIME_CLOBBER_FREED_OBJECTS=1")

stdlib/public/runtime/Metadata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ static uint32_t getLog2AlignmentFromMask(size_t alignMask) {
21842184
}
21852185

21862186
static inline ClassROData *getROData(ClassMetadata *theClass) {
2187-
return (ClassROData*) (theClass->Data & ~uintptr_t(1));
2187+
return (ClassROData*)(theClass->Data & ~uintptr_t(SWIFT_CLASS_IS_SWIFT_MASK));
21882188
}
21892189

21902190
static void initGenericClassObjCName(ClassMetadata *theClass) {

test/IRGen/objc_class_export.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
// CHECK-SAME: %objc_class* @"OBJC_CLASS_$_{{(_TtCs12_)?}}SwiftObject",
5757
// CHECK-SAME: %swift.opaque* @_objc_empty_cache,
5858
// CHECK-SAME: %swift.opaque* null,
59-
// CHECK-SAME: i64 add (i64 ptrtoint ({{.*}}* @_DATA__TtC17objc_class_export3Foo to i64), i64 1),
59+
// CHECK-SAME: i64 add (i64 ptrtoint ({{.*}}* @_DATA__TtC17objc_class_export3Foo to i64), i64 {{1|2}}),
6060
// CHECK-SAME: [[FOO]]* (%swift.type*)* @"$s17objc_class_export3FooC6createACyFZ",
6161
// CHECK-SAME: void (double, double, double, double, [[FOO]]*)* @"$s17objc_class_export3FooC10drawInRect5dirtyySo6NSRectV_tF"
6262
// CHECK-SAME: }>, section "__DATA,__objc_data, regular"

test/stdlib/Inputs/SwiftObjectNSObject/SwiftObjectNSObject.m

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,39 @@
5252

5353
// Add methods to class SwiftObject that can be called by performSelector: et al
5454

55-
#if SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT
56-
// mangled Swift._SwiftObject
57-
#define SwiftObject _TtCs12_SwiftObject
58-
#define SwiftObjectDemangledName "Swift._SwiftObject"
59-
#else
60-
// Pre-stable ABI uses un-mangled name for SwiftObject.
61-
#define SwiftObjectDemangledName "SwiftObject"
62-
#endif
63-
64-
@interface SwiftObject /* trust me, I know what I'm doing */ @end
65-
@implementation SwiftObject (MethodsToPerform)
66-
-(id) perform0 {
55+
static const char *SwiftObjectDemangledName;
56+
57+
static id Perform0(id self, SEL sel) {
6758
return self;
6859
}
6960

70-
-(id) perform1:(id)one {
61+
static id Perform1(id self, SEL sel, id one) {
7162
expectTrue ([one isEqual:@1]);
7263
return self;
7364
}
7465

75-
-(id) perform2:(id)one :(id)two {
66+
static id Perform2(id self, SEL sel, id one, id two) {
7667
expectTrue ([one isEqual:@1]);
7768
expectTrue ([two isEqual:@2]);
7869
return self;
7970
}
80-
@end
71+
72+
static __attribute__((constructor))
73+
void HackSwiftObject()
74+
{
75+
SwiftObjectDemangledName = "Swift._SwiftObject";
76+
Class cls = objc_getClass(SwiftObjectDemangledName);
77+
// FIXME: Remove this fallback after we enable
78+
// SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT everywhere.
79+
if (!cls) {
80+
SwiftObjectDemangledName = "SwiftObject";
81+
cls = objc_getClass(SwiftObjectDemangledName);
82+
}
83+
84+
class_addMethod(cls, @selector(perform0), (IMP)Perform0, "@@:");
85+
class_addMethod(cls, @selector(perform1:), (IMP)Perform1, "@@:@");
86+
class_addMethod(cls, @selector(perform2::), (IMP)Perform2, "@@:@@");
87+
}
8188

8289
void TestSwiftObjectNSObject(id c, id d)
8390
{
@@ -411,10 +418,10 @@ void TestSwiftObjectNSObject(id c, id d)
411418
expectTrue ([[c description] isEqual:@"SwiftObjectNSObject.C"]);
412419
expectTrue ([[D description] isEqual:@"SwiftObjectNSObject.D"]);
413420
expectTrue ([[C description] isEqual:@"SwiftObjectNSObject.C"]);
414-
expectTrue ([[S description] isEqual:@ SwiftObjectDemangledName]);
421+
expectTrue ([[S description] isEqual:@(SwiftObjectDemangledName)]);
415422
expectTrue ([[D_meta description] isEqual:@"SwiftObjectNSObject.D"]);
416423
expectTrue ([[C_meta description] isEqual:@"SwiftObjectNSObject.C"]);
417-
expectTrue ([[S_meta description] isEqual:@ SwiftObjectDemangledName]);
424+
expectTrue ([[S_meta description] isEqual:@(SwiftObjectDemangledName)]);
418425

419426
// NSLog() calls -description and also some private methods.
420427
// This output is checked by FileCheck in SwiftObjectNSObject.swift.
@@ -429,10 +436,10 @@ void TestSwiftObjectNSObject(id c, id d)
429436
expectTrue ([[c debugDescription] isEqual:@"SwiftObjectNSObject.C"]);
430437
expectTrue ([[D debugDescription] isEqual:@"SwiftObjectNSObject.D"]);
431438
expectTrue ([[C debugDescription] isEqual:@"SwiftObjectNSObject.C"]);
432-
expectTrue ([[S debugDescription] isEqual:@ SwiftObjectDemangledName]);
439+
expectTrue ([[S debugDescription] isEqual:@(SwiftObjectDemangledName)]);
433440
expectTrue ([[D_meta debugDescription] isEqual:@"SwiftObjectNSObject.D"]);
434441
expectTrue ([[C_meta debugDescription] isEqual:@"SwiftObjectNSObject.C"]);
435-
expectTrue ([[S_meta debugDescription] isEqual:@ SwiftObjectDemangledName]);
442+
expectTrue ([[S_meta debugDescription] isEqual:@(SwiftObjectDemangledName)]);
436443

437444

438445
printf("NSObjectProtocol.performSelector\n");

0 commit comments

Comments
 (0)