Skip to content

Fix potential use-after-free in emitFuncToBlock exposed by enabling o… #23218

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
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
7 changes: 5 additions & 2 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
// the block is ultimately destroyed checking that the closure is uniquely
// referenced.
bool useWithoutEscapingVerification = false;
ManagedValue escaping;
ManagedValue escaping;
if (loweredFuncTy->isNoEscape()) {
auto escapingTy = loweredFuncTy->getWithExtInfo(
loweredFuncTy->getExtInfo().withNoEscape(false));
Expand All @@ -525,8 +525,11 @@ ManagedValue SILGenFunction::emitFuncToBlock(SILLocation loc,
auto escapingAnyTy =
funcType.withExtInfo(funcType->getExtInfo().withNoEscape(false));
funcType = escapingAnyTy;
fn = B.createCopyValue(loc, escaping);
fn = escaping.copy(*this, loc);
useWithoutEscapingVerification = true;
} else {
// Since we are going to be storing this into memory, we need fn at +1.
fn = fn.ensurePlusOne(*this, loc);
}

// Build the invoke function signature. The block will capture the original
Expand Down
4 changes: 2 additions & 2 deletions test/PrintAsObjC/blocks.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Please keep this file in alphabetical order!

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t %s -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse-as-library %t/blocks.swiftmodule -typecheck -emit-objc-header-path %t/blocks.h -import-objc-header %S/../Inputs/empty.h -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-sil-ownership -emit-module -o %t %s -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-sil-ownership -parse-as-library %t/blocks.swiftmodule -typecheck -emit-objc-header-path %t/blocks.h -import-objc-header %S/../Inputs/empty.h -disable-objc-attr-requires-foundation-module
// RUN: %FileCheck %s < %t/blocks.h
// RUN: %check-in-clang %t/blocks.h

Expand Down
9 changes: 8 additions & 1 deletion test/SILGen/objc_block_to_func_to_block.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -import-objc-header %S/Inputs/objc_block_to_func_to_block.h -emit-silgen -verify %s
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-sil-ownership -import-objc-header %S/Inputs/objc_block_to_func_to_block.h -emit-silgen -verify %s
// REQUIRES: objc_interop

import Foundation

func bar<A>(x: Foo<A>) {
x.blockInception { f in f { _ = $0 } }
}

typealias MyBlockWithEscapingParam = (@escaping () -> ()) -> Int

// Make sure that we properly create thunks for objc_block_to_func_to_block
@objc class ObjCClass : NSObject {
@objc func blockWithBlockTypealias(_ block: MyBlockWithEscapingParam) {}
}