Skip to content
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

[4.2] SILGen: Fix no escape verification if the closure throws an exc… #17071

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/SILGen/SILGenBridging.cpp
Expand Up @@ -371,7 +371,8 @@ static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
CanAnyFunctionType formalBlockType,
CanSILFunctionType funcTy,
CanSILFunctionType blockTy,
CanSILBlockStorageType blockStorageTy) {
CanSILBlockStorageType blockStorageTy,
bool isUnretainedClosureSafe) {
Scope scope(SGF.Cleanups, CleanupLocation::get(loc));
SILBasicBlock *entry = &*SGF.F.begin();
SILFunctionConventions blockConv(blockTy, SGF.SGM.M);
Expand All @@ -395,7 +396,9 @@ static void buildFuncToBlockInvokeBody(SILGenFunction &SGF,
auto storage = entry->createFunctionArgument(storageAddrTy);
auto capture = SGF.B.createProjectBlockStorage(loc, storage);
auto &funcTL = SGF.getTypeLowering(funcTy);
auto fn = SGF.emitLoad(loc, capture, funcTL, SGFContext(), IsNotTake);
auto fn = isUnretainedClosureSafe
? SGF.emitManagedLoadBorrow(loc, capture)
: SGF.emitLoad(loc, capture, funcTL, SGFContext(), IsNotTake);

// Collect the block arguments, which may have nonstandard conventions.
assert(blockTy->getParameters().size() == funcTy->getParameters().size()
Expand Down Expand Up @@ -590,8 +593,11 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
thunk->setGenericEnvironment(genericEnv);
SILGenFunction thunkSGF(SGM, *thunk);
auto loc = RegularLocation::getAutoGeneratedLocation();
// Not retaining the closure in the reabstraction thunk is safe if we hold
// another reference for the is_escaping sentinel.
buildFuncToBlockInvokeBody(thunkSGF, loc, funcType, blockType,
loweredFuncTy, loweredBlockTy, storageTy);
loweredFuncTy, loweredBlockTy, storageTy,
useWithoutEscapingVerifcation);
}

// Form the block on the stack.
Expand Down
5 changes: 5 additions & 0 deletions test/Interpreter/Inputs/ObjCException.h
@@ -0,0 +1,5 @@
#import <Foundation/Foundation.h>

@interface ExceptionCatcher : NSObject
- (NSException* _Nullable)tryBlock:(__attribute__((noescape)) void(^ _Nonnull)(void))unsafeBlock NS_SWIFT_NAME(tryBlock(_:));
@end
13 changes: 13 additions & 0 deletions test/Interpreter/Inputs/ObjCException.m
@@ -0,0 +1,13 @@
#import "ObjCException.h"

@implementation ExceptionCatcher
- (NSException* _Nullable) tryBlock:(__attribute__((noescape)) void(^_Nonnull)(void))unsafeBlock {
@try {
unsafeBlock();
}
@catch (NSException *exception) {
return exception;
}
return nil;
}
@end
25 changes: 25 additions & 0 deletions test/Interpreter/objc_throw_in_noescape_block.swift
@@ -0,0 +1,25 @@
// RUN: %empty-directory(%t)
//
// RUN: %target-clang -fobjc-arc %S/Inputs/ObjCException.m -c -o %t/ObjCException.o
// RUN: %target-build-swift -import-objc-header %S/Inputs/ObjcException.h -Xlinker %t/ObjCException.o %s -o %t/a.out
// RUN: %target-run %t/a.out

// REQUIRES: executable_test
// REQUIRES: objc_interop

import Foundation
import StdlibUnittest

var ThrowingTestSuite = TestSuite("Throwing")


ThrowingTestSuite.test("noescape verification") {
let catcher = ExceptionCatcher()
let e = catcher.tryBlock {
NSException(name: NSExceptionName(rawValue: "Flames"), reason: "Fire", userInfo: nil).raise()
}
expectEqual(e!.reason, "Fire")
}


runAllTests()
22 changes: 22 additions & 0 deletions test/SILGen/objc_thunks.swift
Expand Up @@ -570,3 +570,25 @@ func registerAnsible() {
// CHECK: function_ref @$S11objc_thunks15registerAnsibleyyFyyycSgcfU_
Ansible.anseAsync({ completion in completion!() })
}
@_silgen_name("noescape")
func noescape(f: @convention(block) () -> ())

// CHECK: sil hidden @$S11objc_thunks21testObjCNoescapeThunkyyF : $@convention(thin) () -> () {
// CHECK: [[REABSTRACT:%.*]] = function_ref @$SIeg_IyB_TR
// CHECK: init_block_storage_header {{.*}} : $*@block_storage @callee_guaranteed () -> (), invoke [[REABSTRACT]]
// CHECK: return
func testObjCNoescapeThunk() {
noescape {
}
}

// Noescape verification relies on there not being a retain/release in order to
// work in the presence of a objective c throwing implementation function.
// CHECK: sil {{.*}} @$SIeg_IyB_TR
// CHECK: bb0([[T0:%.*]] : @trivial $*@block_storage @callee_guaranteed () -> ()):
// CHECK-NEXT: [[T1:%.*]] = project_block_storage [[T0]]
// CHECK-NEXT: [[T2:%.*]] = load_borrow [[T1]]
// CHECK-NEXT: [[T3:%.*]] = apply [[T2]]()
// CHECK-NEXT: [[T4:%.*]] = tuple ()
// CHECK-NEXT: end_borrow [[T2]] from [[T1]]
// CHECK-NEXT: return [[T4]]