Skip to content

Fix crash when emitting force-unwrap-and-call expression for optional ObjC methods #60399

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 2 commits into from
Aug 10, 2022
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
44 changes: 43 additions & 1 deletion lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,39 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
return std::move(*applyCallee);
}

/// \returns true if the conversion is from an async function to
/// the same type but with a global actor added. For example this:
/// () async -> () ==> @MainActor () async -> ()
/// will return true. In all other cases, returns false.
static bool addsGlobalActorToAsyncFn(FunctionConversionExpr *fce) {
CanType oldTy = fce->getSubExpr()->getType()->getCanonicalType();
CanType newTy = fce->getType()->getCanonicalType();

if (auto oldFnTy = dyn_cast<AnyFunctionType>(oldTy)) {
if (auto newFnTy = dyn_cast<AnyFunctionType>(newTy)) {
// old type MUST be async
if (!oldFnTy->hasEffect(EffectKind::Async))
return false;

// old type MUST NOT have a global actor
if (oldFnTy->hasGlobalActor())
return false;

// new type MUST have a global actor
if (!newFnTy->hasGlobalActor())
return false;

// see if adding the global actor to the old type yields the new type.
auto globalActor = newFnTy->getGlobalActor();
auto addedActor = oldFnTy->getExtInfo().withGlobalActor(globalActor);

return oldFnTy->withExtInfo(addedActor) == newFnTy;
}
}

return false;
}

/// Ignore parentheses and implicit conversions.
static Expr *ignoreParensAndImpConversions(Expr *expr) {
while (true) {
Expand All @@ -1608,7 +1641,16 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
// works given that we check the result for certain forms.
if (auto eval = dyn_cast<OptionalEvaluationExpr>(expr)) {
if (auto inject = dyn_cast<InjectIntoOptionalExpr>(eval->getSubExpr())) {
if (auto bind = dyn_cast<BindOptionalExpr>(inject->getSubExpr())) {

auto nextSubExpr = inject->getSubExpr();

// skip over a specific, known no-op function conversion, if it exists
if (auto funcConv = dyn_cast<FunctionConversionExpr>(nextSubExpr)) {
if (addsGlobalActorToAsyncFn(funcConv))
nextSubExpr = funcConv->getSubExpr();
}

if (auto bind = dyn_cast<BindOptionalExpr>(nextSubExpr)) {
if (bind->getDepth() == 0)
return bind->getSubExpr();
}
Expand Down
8 changes: 8 additions & 0 deletions test/ClangImporter/objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,11 @@ extension SomeWrapper: Sendable where T: Sendable {}
}
}
}

// rdar://97646309 -- lookup and direct call of an optional global-actor constrained method would crash in SILGen
@available(SwiftStdlib 5.5, *)
extension CoffeeDelegate {
@MainActor func test() async -> (NSObject?, NSObject, NSObject) {
return await self.icedMochaServiceGenerateMocha!(NSObject())
}
}
8 changes: 8 additions & 0 deletions test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,12 @@ SENDABLE id StructWithSendableContentsGetSendableComputed(struct StructWithSenda
+ (void)getAsCustomer:(void(^_Nonnull)(NSObject *device))completion;
@end


// rdar://97646309
UI_ACTOR
@protocol CoffeeDelegate <NSObject>
@optional
- (void)icedMochaService:(NSObject *)mochaService generateMochaWithCompletion:(void (^)(NSObject *_Nullable ingredient1, NSObject *ingredient2, NSObject *ingredient3))completionHandler;
@end

#pragma clang assume_nonnull end
29 changes: 27 additions & 2 deletions test/SILGen/dynamic_lookup.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// RUN: %target-swift-emit-silgen -module-name dynamic_lookup -enable-objc-interop -parse-as-library -disable-objc-attr-requires-foundation-module %s | %FileCheck %s
// RUN: %target-swift-emit-silgen -module-name dynamic_lookup -enable-objc-interop -parse-as-library -disable-objc-attr-requires-foundation-module %s | %FileCheck %s --check-prefix=GUARANTEED
// RUN: %target-swift-emit-silgen -module-name dynamic_lookup -enable-objc-interop -parse-as-library -disable-objc-attr-requires-foundation-module -disable-availability-checking %s | %FileCheck %s
// RUN: %target-swift-emit-silgen -module-name dynamic_lookup -enable-objc-interop -parse-as-library -disable-objc-attr-requires-foundation-module -disable-availability-checking %s | %FileCheck %s --check-prefix=GUARANTEED

// REQUIRES: objc_interop
// REQUIRES: concurrency

class X {
@objc func f() { }
Expand Down Expand Up @@ -405,3 +408,25 @@ func testAnyObjectWithDefault(_ x: AnyObject) {
// CHECK: apply [[METHOD]]([[DEFARG]], [[OPENEDX]])
x.hasDefaultParam()
}


// rdar://97646309 -- lookup and direct call of an optional global-actor constrained method would crash in SILGen
@MainActor
@objc protocol OptionalMemberLookups {
@objc optional func generateMaybe() async
}

extension OptionalMemberLookups {
// CHECK-LABEL: sil hidden [ossa] @$s14dynamic_lookup21OptionalMemberLookupsPAAE19testForceDirectCallyyYaF
// CHECK: [[SELF:%[0-9]+]] = copy_value {{.*}} : $Self
// CHECK: [[METH:%[0-9]+]] = objc_method {{.*}} : $Self, #OptionalMemberLookups.generateMaybe!foreign : <Self where Self : OptionalMemberLookups> (Self) -> () async -> (), $@convention(objc_method) (@convention(block) () -> (), Self) -> ()
// CHECK: = function_ref @$sIeyB_yt14dynamic_lookup21OptionalMemberLookupsRzlTz_ : $@convention(c) @pseudogeneric <τ_0_0 where τ_0_0 : OptionalMemberLookups> (@inout_aliasable @block_storage UnsafeContinuation<(), Never>) -> ()
// CHECK: [[BLOCK:%[0-9]+]] = init_block_storage_header {{.*}} : $*@block_storage UnsafeContinuation<(), Never>
// CHECK: = apply [[METH]]([[BLOCK]], [[SELF]]) : $@convention(objc_method) (@convention(block) () -> (), Self) -> ()
// CHECK: await_async_continuation {{.*}} : $Builtin.RawUnsafeContinuation, resume bb1
// CHECK: hop_to_executor {{.*}} : $MainActor
// CHECK: } // end sil function '$s14dynamic_lookup21OptionalMemberLookupsPAAE19testForceDirectCallyyYaF'
func testForceDirectCall() async -> Void {
await self.generateMaybe!()
}
}