Skip to content

[SILGen] Fix a crash when a closure is converted to a pointer to a function returning a non-trivial C++ type #73561

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 4 commits into from
Jun 18, 2024
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
56 changes: 34 additions & 22 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,8 @@ static SILFunctionType *
emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
SmallVectorImpl<SILValue> &args,
SILValue &foreignErrorSlot, SILValue &foreignAsyncSlot,
std::optional<ForeignErrorConvention> &foreignError,
std::optional<ForeignAsyncConvention> &foreignAsync,
std::optional<ForeignErrorConvention> foreignError,
std::optional<ForeignAsyncConvention> foreignAsync,
CanType &nativeFormalResultTy,
CanType &bridgedFormalResultTy) {
SILDeclRef native = thunk.asForeign(false);
Expand All @@ -1355,18 +1355,6 @@ emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
SmallVector<ManagedValue, 8> bridgedArgs;
bridgedArgs.reserve(objcFnTy->getParameters().size());

// Find the foreign error and async conventions if we have one.
if (thunk.hasDecl()) {
if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl())) {
foreignError = func->getForeignErrorConvention();
foreignAsync = func->getForeignAsyncConvention();
}
}

// We don't know what to do with indirect results from the Objective-C side.
assert(objcFnTy->getNumIndirectFormalResults() == 0
&& "Objective-C methods cannot have indirect results");

auto bridgedFormalTypes = getParameterTypes(objcFormalFnTy.getParams());
bridgedFormalResultTy = objcFormalFnTy.getResult();

Expand Down Expand Up @@ -1616,16 +1604,40 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
}
}

std::optional<ForeignErrorConvention> foreignError;
std::optional<ForeignAsyncConvention> foreignAsync;

// Find the foreign error and async conventions if we have one.
if (thunk.hasDecl()) {
if (auto func = dyn_cast<AbstractFunctionDecl>(thunk.getDecl())) {
foreignError = func->getForeignErrorConvention();
foreignAsync = func->getForeignAsyncConvention();
}
}

// If we are bridging a Swift method with Any return value(s), create a
// stack allocation to hold the result(s), since Any is address-only.
SmallVector<SILValue, 4> args;
SILFunctionConventions funcConv = F.getConventions();
bool needsBridging = true;
if (substConv.hasIndirectSILResults()) {
for (auto result : substConv.getResults()) {
if (!substConv.isSILIndirect(result)) {
continue;
}

if (!foreignAsync && funcConv.hasIndirectSILResults()) {
auto resultTy =
funcConv.getSingleSILResultType(getTypeExpansionContext());
assert(substConv.getSingleSILResultType(getTypeExpansionContext()) ==
resultTy);
args.push_back(F.begin()->createFunctionArgument(resultTy));
needsBridging = false;
break;
}

args.push_back(emitTemporaryAllocation(
loc, substConv.getSILType(result, getTypeExpansionContext())));
loc, substConv.getSILType(result, getTypeExpansionContext())));
}
}

Expand All @@ -1635,8 +1647,6 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
Scope argScope(Cleanups, CleanupLocation(loc));

// Bridge the arguments.
std::optional<ForeignErrorConvention> foreignError;
std::optional<ForeignAsyncConvention> foreignAsync;
SILValue foreignErrorSlot;
SILValue foreignAsyncSlot;
CanType nativeFormalResultType, bridgedFormalResultType;
Expand Down Expand Up @@ -1863,12 +1873,14 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
if (foreignAsync) {
result = passResultToCompletionHandler(result);
} else {
if (substConv.hasIndirectSILResults()) {
assert(substTy->getNumResults() == 1);
result = args[0];
if (needsBridging) {
if (substConv.hasIndirectSILResults()) {
assert(substTy->getNumResults() == 1);
result = args[0];
}
result = emitBridgeReturnValue(*this, loc, result, nativeFormalResultType,
bridgedFormalResultType, objcResultTy);
}
result = emitBridgeReturnValue(*this, loc, result, nativeFormalResultType,
bridgedFormalResultType, objcResultTy);
}
} else {
SILBasicBlock *contBB = createBasicBlock();
Expand Down
1 change: 1 addition & 0 deletions test/Interop/Cxx/class/Inputs/closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ void cfuncARCStrong(void (*_Nonnull)(ARCStrong));
#endif

void cfuncReturnNonTrivial(NonTrivial (^_Nonnull)()) noexcept;
void cfuncReturnNonTrivial2(NonTrivial (*_Nonnull)()) noexcept;

#endif // __CLOSURE__
13 changes: 13 additions & 0 deletions test/Interop/Cxx/class/closure-thunk-irgen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %target-swiftxx-frontend -I %S/Inputs -emit-irgen %s | %FileCheck %s

// REQUIRES: OS=macosx || OS=linux-android

import Closure

// CHECK: define internal void @"$s4main36testClosureToFuncPtrReturnNonTrivialyyFSo0hI0VycfU_To"(ptr noalias sret(%{{.*}}) %[[V0:.*]])
// CHECK: call swiftcc void @"$s4main36testClosureToFuncPtrReturnNonTrivialyyFSo0hI0VycfU_"(ptr noalias sret(%{{.*}}) %[[V0]])
// CHECK: ret void

public func testClosureToFuncPtrReturnNonTrivial() {
cfuncReturnNonTrivial2({() -> NonTrivial in return NonTrivial()});
}
10 changes: 10 additions & 0 deletions test/Interop/Cxx/class/closure-thunk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ import Closure
public func testClosureToFuncPtr() {
cfunc2({N in})
}

// CHECK: sil private [thunk] [ossa] @$s4main36testClosureToFuncPtrReturnNonTrivialyyFSo0hI0VycfU_To : $@convention(c) () -> @out NonTrivial {
// CHECK: bb0(%[[V0:.*]] : $*NonTrivial):
// CHECK: %[[V1:.*]] = function_ref @$s4main36testClosureToFuncPtrReturnNonTrivialyyFSo0hI0VycfU_ : $@convention(thin) () -> @out NonTrivial
// CHECK: %[[V2:.*]] = apply %[[V1]](%[[V0]]) : $@convention(thin) () -> @out NonTrivial
// CHECK: return %[[V2]] : $()

public func testClosureToFuncPtrReturnNonTrivial() {
cfuncReturnNonTrivial2({() -> NonTrivial in return NonTrivial()});
}