Skip to content

Commit 50b5044

Browse files
committed
[IRGen] Don't emit relative references to Objective-C class references.
Objective-C class references (which show up in the __objc_classrefs section) are always coalesced by the linker. When we relatively address them (which occurs in protocol conformance records), the linker may compute the relative offset *before* coalescing, leading to an incorrect result. The net effect is a protocol conformance record that applies to the wrong Objective-C class, causing all sorts of runtime mayhem. Switch relatively-addressed Objective-C classes over to using the Objective-C runtime name of the class. It's a less efficient encoding (since we need to go through objc_lookUpClass), but it avoids the linker bug. Fixes rdar://problem/46428085 by working around the linker bug.
1 parent 51b9c52 commit 50b5044

File tree

4 files changed

+23
-30
lines changed

4 files changed

+23
-30
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,28 +2565,12 @@ getProtocolDescriptorEntityReference(IRGenModule &IGM, ProtocolDecl *protocol) {
25652565
}
25662566

25672567
static TypeEntityReference
2568-
getObjCClassRefEntityReference(IRGenModule &IGM, ClassDecl *cls) {
2569-
// A reference to an Objective-C class object.
2570-
assert(cls->isObjC() && "Must have an Objective-C class here");
2571-
2572-
auto kind = TypeReferenceKind::IndirectObjCClass;
2573-
auto entity = LinkEntity::forObjCClassRef(cls);
2574-
2575-
auto ref = IGM.getAddrOfLLVMVariableOrGOTEquivalent(entity);
2576-
assert(!ref.isIndirect());
2577-
2578-
return TypeEntityReference(kind, ref.getValue());
2579-
}
2580-
2581-
static TypeEntityReference
2582-
getRuntimeOnlyClassEntityReference(IRGenModule &IGM, ClassDecl *cls) {
2583-
assert(cls->getForeignClassKind() == ClassDecl::ForeignKind::RuntimeOnly);
2584-
2585-
auto namedClangDecl = Mangle::ASTMangler::getClangDeclForMangling(cls);
2586-
assert(namedClangDecl);
2587-
2568+
getObjCClassByNameReference(IRGenModule &IGM, ClassDecl *cls) {
25882569
auto kind = TypeReferenceKind::DirectObjCClassName;
2589-
auto ref = IGM.getAddrOfGlobalString(namedClangDecl->getName());
2570+
SmallString<64> objcRuntimeNameBuffer;
2571+
auto ref = IGM.getAddrOfGlobalString(
2572+
cls->getObjCRuntimeName(objcRuntimeNameBuffer),
2573+
/*willByRelativelyAddressed=*/true);
25902574

25912575
return TypeEntityReference(kind, ref);
25922576
}
@@ -2605,17 +2589,22 @@ IRGenModule::getTypeEntityReference(NominalTypeDecl *decl) {
26052589

26062590
switch (clas->getForeignClassKind()) {
26072591
case ClassDecl::ForeignKind::RuntimeOnly:
2608-
return getRuntimeOnlyClassEntityReference(*this, clas);
2592+
return getObjCClassByNameReference(*this, clas);
26092593

26102594
case ClassDecl::ForeignKind::CFType:
26112595
return getTypeContextDescriptorEntityReference(*this, clas);
26122596

26132597
case ClassDecl::ForeignKind::Normal:
26142598
if (hasKnownSwiftMetadata(*this, clas)) {
26152599
return getTypeContextDescriptorEntityReference(*this, clas);
2616-
} else {
2617-
return getObjCClassRefEntityReference(*this, clas);
26182600
}
2601+
2602+
// Note: we would like to use an Objective-C class reference, but the
2603+
// Darwin linker currently has a bug where it will coalesce these symbols
2604+
// *after* computing a relative offset, causing incorrect relative
2605+
// offsets in the metadata. Therefore, reference Objective-C classes by
2606+
// their runtime names.
2607+
return getObjCClassByNameReference(*this, clas);
26192608
}
26202609
llvm_unreachable("bad foreign type kind");
26212610
}

test/IRGen/objc_bridged_generic_conformance.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
// CHECK-NOT: _TMnCSo
44

5-
// CHECK: @"$sSo6ThingyCyxG32objc_bridged_generic_conformance1PADMc" = hidden constant %swift.protocol_conformance_descriptor {{.*}} @"\01l_OBJC_CLASS_REF_$_Thingy"
5+
// CHECK: @"$sSo6ThingyCyxG32objc_bridged_generic_conformance1PADMc" = hidden constant %swift.protocol_conformance_descriptor {{.*}} @[[THINGY_NAME:[0-9]]]
6+
7+
// CHECK: @[[THINGY_NAME]] = private constant [7 x i8] c"Thingy\00"
68

79
// CHECK-NOT: _TMnCSo
810

test/IRGen/objc_runtime_visible_conformance.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ extension A : YourProtocol {}
1010

1111
// CHECK-LABEL: @"$sSo1AC32objc_runtime_visible_conformance10MyProtocolACMc"
1212
// CHECK-SAME: @"$s32objc_runtime_visible_conformance10MyProtocolMp"
13-
// CHECK-SAME: [2 x i8]* [[STRING_A:@[0-9]+]]
13+
// CHECK-SAME: [[STRING_A:@[0-9]+]]
1414
// CHECK-SAME: @"$sSo1AC32objc_runtime_visible_conformance10MyProtocolACWP"
1515
// DirectObjCClassName
1616
// CHECK-SAME: i32 16
17-
// CHECK: [[STRING_A]] = private unnamed_addr constant [2 x i8] c"A\00"
17+
// CHECK: [[STRING_A]] = private constant [22 x i8] c"MyRuntimeVisibleClass\00"

test/IRGen/protocol_conformance_records_objc.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ extension NSRect: Runcible {
2929
// CHECK-LABEL: @"$sSo5GizmoC33protocol_conformance_records_objc8RuncibleACMc" = constant %swift.protocol_conformance_descriptor {
3030
// -- protocol descriptor
3131
// CHECK-SAME: [[RUNCIBLE]]
32-
// -- class object reference
33-
// CHECK-SAME: @"\01l_OBJC_CLASS_REF_$_Gizmo"
32+
// -- class name reference
33+
// CHECK-SAME: @[[GIZMO_NAME:[0-9]+]]
3434
// -- witness table
3535
// CHECK-SAME: @"$sSo5GizmoC33protocol_conformance_records_objc8RuncibleACWP"
3636
// -- flags
37-
// CHECK-SAME: i32 24
37+
// CHECK-SAME: i32 16
3838
// CHECK-SAME: }
3939
extension Gizmo: Runcible {
4040
public func runce() {}
4141
}
4242

43+
// CHECK: @[[GIZMO_NAME]] = private constant [6 x i8] c"Gizmo\00"
44+
4345
// CHECK-LABEL: @"\01l_protocol_conformances" = private constant [
4446
// CHECK-SAME: @"$sSo6NSRectV33protocol_conformance_records_objc8RuncibleACMc"
4547
// CHECK-SAME: @"$sSo5GizmoC33protocol_conformance_records_objc8RuncibleACMc"

0 commit comments

Comments
 (0)