Skip to content

[5.1][Runtime] Avoid +class overrides when initializing an ObjC class. #24637

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
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
6 changes: 5 additions & 1 deletion stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,11 @@ id swift_dynamicCastObjCProtocolConditional(id object,
// Used when we have class metadata and we want to ensure a class has been
// initialized by the Objective-C runtime. We need to do this because the
// class "c" might be valid metadata, but it hasn't been initialized yet.
return [c class];
// Send a message that's likely not to be overridden to minimize potential
// side effects. Ignore the return value in case it is overridden to
// return something different. See SR-10463 for an example.
[c self];
return c;
}

static const ClassMetadata *
Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/Inputs/ObjCEvilClassInitialization/EvilClass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>

// A class that overrides +class and +self to return nil.
@interface EvilClass: NSObject
@end
8 changes: 8 additions & 0 deletions test/stdlib/Inputs/ObjCEvilClassInitialization/EvilClass.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#import "EvilClass.h"

@implementation EvilClass

+ (Class)class { return nil; }
+ (id)self { return nil; }

@end
3 changes: 3 additions & 0 deletions test/stdlib/Inputs/ObjCEvilClassInitialization/module.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module EvilClass {
header "EvilClass.h"
}
25 changes: 25 additions & 0 deletions test/stdlib/ObjCEvilClassInitialization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %empty-directory(%t)
// RUN: %target-clang -fobjc-arc %S/Inputs/ObjCEvilClassInitialization/EvilClass.m -c -o %t/EvilClass.o
// RUN: %target-build-swift -I %S/Inputs/ObjCEvilClassInitialization/ %t/EvilClass.o %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out

// REQUIRES: executable_test
// REQUIRES: objc_interop

import EvilClass

import StdlibUnittest

let tests = TestSuite("ObjCEvilClassInitialization")

tests.test("GenericOnEvilClass") {
struct Generic<T> {
var type: T.Type { return T.self }
}
let g = Generic<EvilClass>()
expectEqual("\(type(of: g))", "Generic<EvilClass>")
expectEqual(g.type, EvilClass.self)
}

runAllTests()