Skip to content

SILVerifier: async functions can be called from async functions only #34526

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 1 commit into from
Nov 2, 2020
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
39 changes: 36 additions & 3 deletions lib/SIL/Verifier/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ static llvm::cl::opt<bool> AbortOnFailure(
"verify-abort-on-failure",
llvm::cl::init(true));

static llvm::cl::opt<bool> ContinueOnFailure("verify-continue-on-failure",
llvm::cl::init(false));

static llvm::cl::opt<bool> VerifyDIHoles(
"verify-di-holes",
llvm::cl::init(true));
Expand Down Expand Up @@ -682,9 +685,18 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
const std::function<void()> &extraContext = nullptr) {
if (condition) return;

llvm::dbgs() << "SIL verification failed: " << complaint << "\n";
StringRef funcName;
if (CurInstruction)
funcName = CurInstruction->getFunction()->getName();
else if (CurArgument)
funcName = CurArgument->getFunction()->getName();
if (ContinueOnFailure) {
llvm::dbgs() << "Begin Error in function " << funcName << "\n";
}

if (extraContext) extraContext();
llvm::dbgs() << "SIL verification failed: " << complaint << "\n";
if (extraContext)
extraContext();

if (CurInstruction) {
llvm::dbgs() << "Verifying instruction:\n";
Expand All @@ -693,6 +705,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
llvm::dbgs() << "Verifying argument:\n";
CurArgument->printInContext(llvm::dbgs());
}
if (ContinueOnFailure) {
llvm::dbgs() << "End Error in function " << funcName << "\n";
return;
}

llvm::dbgs() << "In function:\n";
F.print(llvm::dbgs());
llvm::dbgs() << "In module:\n";
Expand Down Expand Up @@ -1460,6 +1477,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {

require(!calleeConv.funcTy->isCoroutine(),
"cannot call coroutine with normal apply");
require(!calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync(),
"cannot call an async function from a non async function");

// Check that if the apply is of a noreturn callee, make sure that an
// unreachable is the next instruction.
Expand All @@ -1478,6 +1497,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
require(!calleeConv.funcTy->isCoroutine(),
"cannot call coroutine with normal apply");

require(!calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync(),
"cannot call an async function from a non async function");

auto normalBB = AI->getNormalBB();
require(normalBB->args_size() == 1,
"normal destination of try_apply must take one argument");
Expand Down Expand Up @@ -1522,6 +1544,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {

require(calleeConv.funcTy->getCoroutineKind() == SILCoroutineKind::YieldOnce,
"must call yield_once coroutine with begin_apply");
require(!calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync(),
"cannot call an async function from a non async function");
}

void checkAbortApplyInst(AbortApplyInst *AI) {
Expand Down Expand Up @@ -1571,6 +1595,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
verifySILFunctionType(resultInfo);
require(resultInfo->getExtInfo().hasContext(),
"result of closure cannot have a thin function type");
require(!resultInfo->isAsync() || PAI->getFunction()->isAsync(),
"cannot call an async function from a non async function");

checkApplyTypeDependentArguments(PAI);

Expand Down Expand Up @@ -1653,7 +1679,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"function");
}
}

// TODO: Impose additional constraints when partial_apply when the
// -disable-sil-partial-apply flag is enabled. We want to reduce
// partial_apply to being only a means of associating a closure invocation
Expand Down Expand Up @@ -1719,6 +1745,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"result of function_ref");
require(!fnType->getExtInfo().hasContext(),
"function_ref should have a context-free function result");
require(!fnType->isAsync() || FRI->getFunction()->isAsync(),
"cannot call an async function from a non-async function");

// Note: in SingleFunction mode, we relax some of these checks because
// we may not have linked everything yet.
Expand Down Expand Up @@ -2893,6 +2921,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
void checkWitnessMethodInst(WitnessMethodInst *AMI) {
auto methodType = requireObjectType(SILFunctionType, AMI,
"result of witness_method");
require(!methodType->isAsync() || AMI->getFunction()->isAsync(),
"cannot call an async function from a non-async function");

auto *protocol
= dyn_cast<ProtocolDecl>(AMI->getMember().getDecl()->getDeclContext());
Expand Down Expand Up @@ -3048,6 +3078,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"result of class_method");
require(!methodType->getExtInfo().hasContext(),
"result method must be of a context-free function type");
require(!methodType->isAsync() || CMI->getFunction()->isAsync(),
"cannot call an async function from a non-async function");

SILType operandType = CMI->getOperand()->getType();
require(operandType.isClassOrClassMetatype(),
"operand must be of a class type");
Expand Down
156 changes: 156 additions & 0 deletions test/IRGen/async/async_calls_verifier.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// RUN: %target-build-swift -Xfrontend -enable-experimental-concurrency -Xllvm -verify-continue-on-failure=true -parse-sil %s -emit-sil -I %t -L %t -o /dev/null 2>&1 | %FileCheck %s
// REQUIRES: asserts
// REQUIRES: concurrency

import Swift
import Builtin
import _Concurrency

sil @create_error : $@convention(thin) () -> @owned Error

sil [ossa] @asynccallee1 : $@async @convention(thin) () -> () {
bb0:
%result = tuple()
return %result : $()
}

sil [ossa] @asynccallee2 : $@async @convention(thin) () -> ((), @error Error) {
bb0:
cond_br undef, bb1, bb2
bb1:
%result = tuple()
return %result : $()
bb2:
%0 = function_ref @create_error : $@convention(thin) () -> @owned Error
%1 = apply %0() : $@convention(thin) () -> @owned Error
throw %1 : $Error
}

// CHECK-NOT: Function: 'async_apply_caller'
sil [ossa] @async_apply_caller : $@async @convention(thin) () -> () {
bb0:
%call1 = function_ref @asynccallee1 : $@async @convention(thin) () -> ()
%callres1 = apply %call1() : $@async @convention(thin) () -> ()
%result = tuple()
return %result : $()
}

// CHECK-LABEL: Begin Error in function nonasync_apply_caller
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !fnType->isAsync() || FRI->getFunction()->isAsync()
// CHECK: Verifying instruction:
// CHECK: -> // function_ref asynccallee1
// CHECK: %0 = function_ref @asynccallee1 : $@convention(thin) @async () -> () // user: %1
// CHECK: %1 = apply %0() : $@convention(thin) @async () -> ()
// CHECK-LABEL: End Error in function nonasync_apply_caller
sil [ossa] @nonasync_apply_caller : $@convention(thin) () -> () {
bb0:
%call = function_ref @asynccallee1 : $@async @convention(thin) () -> ()
%callres = apply %call() : $@async @convention(thin) () -> ()
%result = tuple()
return %result : $()
}

// CHECK-NOT: Function: 'async_try_apply_caller'
sil [ossa] @async_try_apply_caller : $@async @convention(thin) () -> () {
bb0:
%call1 = function_ref @asynccallee2 : $@async @convention(thin) () -> ((), @error Error)
try_apply %call1() : $@async @convention(thin) () -> ((), @error Error), normal bb1, error bb2
bb1(%result : $()):
return %result : $()
bb2(%result_error : $Error):
unreachable
}

// CHECK-LABEL: Begin Error in function nonasync_try_apply_caller
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !fnType->isAsync() || FRI->getFunction()->isAsync()
// CHECK: Verifying instruction:
// CHECK: -> // function_ref asynccallee2
// CHECK: %0 = function_ref @asynccallee2 : $@convention(thin) @async () -> ((), @error Error) // user: %1
// CHECK: try_apply %0() : $@convention(thin) @async () -> ((), @error Error), normal bb1, error bb2 // id: %1
// CHECK-LABEL: End Error in function nonasync_try_apply_caller
sil [ossa] @nonasync_try_apply_caller : $@convention(thin) () -> () {
bb0:
%call1 = function_ref @asynccallee2 : $@async @convention(thin) () -> ((), @error Error)
try_apply %call1() : $@async @convention(thin) () -> ((), @error Error), normal bb1, error bb2
bb1(%result : $()):
return %result : $()
bb2(%result_error : $Error):
unreachable
}

// CHECK-NOT: Function: 'async_partial_apply_caller'
sil [ossa] @async_partial_apply_caller : $@async @convention(thin) () -> @owned @async @callee_owned () -> () {
bb0:
%1 = function_ref @asynccallee1 : $@async @convention(thin) () -> ()
%2 = partial_apply %1() : $@async @convention(thin) () -> ()
return %2 : $@async @callee_owned () -> ()
}

// CHECK-LABEL: Begin Error in function nonasync_partial_apply_caller
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !fnType->isAsync() || FRI->getFunction()->isAsync()
// CHECK: Verifying instruction:
// CHECK: -> // function_ref asynccallee1
// CHECK: %0 = function_ref @asynccallee1 : $@convention(thin) @async () -> () // user: %1
// CHECK: %1 = partial_apply %0() : $@convention(thin) @async () -> () // user: %2
// CHECK-LABEL: End Error in function nonasync_partial_apply_caller
sil [ossa] @nonasync_partial_apply_caller : $@convention(thin) () -> @owned @async @callee_owned () -> () {
bb0:
%1 = function_ref @asynccallee1 : $@async @convention(thin) () -> ()
%2 = partial_apply %1() : $@async @convention(thin) () -> ()
return %2 : $@async @callee_owned () -> ()
}

protocol P {
func printMe() async -> Int64
}

// CHECK-NOT: Function: 'async_witness_method_caller'
sil hidden @async_witness_method_caller : $@async @convention(thin) <T where T : P> (@in_guaranteed T) -> Int64 {
bb0(%t : $*T):
%method = witness_method $T, #P.printMe : <Self where Self : P> (Self) -> () async -> Int64 : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64
%result = apply %method<T>(%t) : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64
return %result : $Int64
}

// CHECK-LABEL: Begin Error in function nonasync_witness_method_caller
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !methodType->isAsync() || AMI->getFunction()->isAsync()
// CHECK: Verifying instruction:
// CHECK: -> %1 = witness_method $T, #P.printMe : <Self where Self : P> (Self) -> () async -> Int64 : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64 // user: %2
// CHECK: %2 = apply %1<T>(%0) : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64 // user: %3
// CHECK-LABEL: End Error in function nonasync_witness_method_caller
sil hidden @nonasync_witness_method_caller : $@convention(thin) <T where T : P> (@in_guaranteed T) -> Int64 {
bb0(%t : $*T):
%method = witness_method $T, #P.printMe : <Self where Self : P> (Self) -> () async -> Int64 : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64
%result = apply %method<T>(%t) : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64
return %result : $Int64
}

class S {
func method(_ int: Int64) async
deinit
init()
}

// CHECK-NOT: Function: 'async_class_method_caller'
sil hidden @async_class_method_caller : $@async @convention(thin) (Int64, S) -> () {
bb0(%int : $Int64, %instance : $S):
%func = class_method %instance : $S, #S.method : (S) -> (Int64) async -> (), $@convention(method) @async (Int64, @guaranteed S) -> ()
%result = apply %func(%int, %instance) : $@convention(method) @async (Int64, @guaranteed S) -> ()
%res = tuple ()
return %res : $()
}

// CHECK-LABEL: Begin Error in function nonasync_class_method_caller
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !methodType->isAsync() || CMI->getFunction()->isAsync()
// CHECK: Verifying instruction:
// CHECK: %1 = argument of bb0 : $S // users: %3, %2
// CHECK: -> %2 = class_method %1 : $S, #S.method : (S) -> (Int64) async -> (), $@convention(method) @async (Int64, @guaranteed S) -> () // user: %3
// CHECK: %3 = apply %2(%0, %1) : $@convention(method) @async (Int64, @guaranteed S) -> ()
// CHECK-LABEL: End Error in function nonasync_class_method_caller
sil hidden @nonasync_class_method_caller : $@convention(thin) (Int64, S) -> () {
bb0(%int : $Int64, %instance : $S):
%func = class_method %instance : $S, #S.method : (S) -> (Int64) async -> (), $@convention(method) @async (Int64, @guaranteed S) -> ()
%result = apply %func(%int, %instance) : $@convention(method) @async (Int64, @guaranteed S) -> ()
%res = tuple ()
return %res : $()
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sil_witness_table ObserverImpl : Observer module main {
}

// CHECK-LL: define internal swiftcc void @"$sTA"(%swift.task* {{%[0-9]+}}, %swift.executor* {{%[0-9]+}}, %swift.context* {{%[0-9]+}}, %swift.refcounted* swiftself {{%[0-9]*}}) {{#[0-9]*}} {
sil hidden @witness_method : $@convention(thin) <S where S : Observable><O where O : Observer, S.Result == O.Result> (@in S) -> @owned @async @callee_owned (@in O) -> () {
sil hidden @witness_method : $@async @convention(thin) <S where S : Observable><O where O : Observer, S.Result == O.Result> (@in S) -> @owned @async @callee_owned (@in O) -> () {
bb0(%0 : $*S):
%1 = witness_method $S, #Observable.subscribe : $@async @convention(witness_method: Observable) <τ_0_0 where τ_0_0 : Observable><τ_1_0 where τ_1_0 : Observer, τ_0_0.Result == τ_1_0.Result> (@in τ_1_0, @in_guaranteed τ_0_0) -> ()
%2 = partial_apply %1<S, O>(%0) : $@async @convention(witness_method: Observable) <τ_0_0 where τ_0_0 : Observable><τ_1_0 where τ_1_0 : Observer, τ_0_0.Result == τ_1_0.Result> (@in τ_1_0, @in_guaranteed τ_0_0) -> ()
Expand All @@ -71,8 +71,8 @@ bb0(%argc : $Int32, %argv : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<
strong_retain %observableImpl : $ObservableImpl
%observableImpl_addr = alloc_stack $ObservableImpl
store %observableImpl to %observableImpl_addr : $*ObservableImpl
%witness_method = function_ref @witness_method : $@convention(thin) <S where S : Observable><O where O : Observer, S.Result == O.Result> (@in S) -> @owned @async @callee_owned (@in O) -> ()
%partiallyApplied = apply %witness_method<ObservableImpl, ObserverImpl>(%observableImpl_addr) : $@convention(thin) <S where S : Observable><O where O : Observer, S.Result == O.Result> (@in S) -> @owned @async @callee_owned (@in O) -> ()
%witness_method = function_ref @witness_method : $@async @convention(thin) <S where S : Observable><O where O : Observer, S.Result == O.Result> (@in S) -> @owned @async @callee_owned (@in O) -> ()
%partiallyApplied = apply %witness_method<ObservableImpl, ObserverImpl>(%observableImpl_addr) : $@async @convention(thin) <S where S : Observable><O where O : Observer, S.Result == O.Result> (@in S) -> @owned @async @callee_owned (@in O) -> ()
%observerImpl = alloc_ref $ObserverImpl
%observerImpl_addr = alloc_stack $ObserverImpl
store %observerImpl to %observerImpl_addr : $*ObserverImpl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import _Concurrency
sil public_external @printGeneric : $@convention(thin) <T> (@in_guaranteed T) -> ()
sil public_external @printInt32 : $@convention(thin) (Int32) -> ()

sil @partial_apply_dynamic_with_out_param : $@convention(thin) <T> (Int32, @owned @async @callee_owned (Int32) -> @out T) -> @async @callee_owned () -> @out T {
sil @partial_apply_dynamic_with_out_param : $@async @convention(thin) <T> (Int32, @owned @async @callee_owned (Int32) -> @out T) -> @async @callee_owned () -> @out T {
bb0(%x : $Int32, %f : $@async @callee_owned (Int32) -> @out T):
%p = partial_apply %f(%x) : $@async @callee_owned (Int32) -> @out T
return %p : $@async @callee_owned () -> @out T
Expand All @@ -45,10 +45,10 @@ bb0(%argc : $Int32, %argv : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<
%first_addr = alloc_stack $Int32
store %first to %first_addr : $*Int32
%callee1 = partial_apply %callee<Int32>(%first_addr) : $@async @convention(thin) <T> (Int32, @in_guaranteed T) -> @out T
%partialApplier = function_ref @partial_apply_dynamic_with_out_param : $@convention(thin) <T> (Int32, @owned @async @callee_owned (Int32) -> @out T) -> @async @callee_owned () -> @out T
%partialApplier = function_ref @partial_apply_dynamic_with_out_param : $@async @convention(thin) <T> (Int32, @owned @async @callee_owned (Int32) -> @out T) -> @async @callee_owned () -> @out T
%second_literal = integer_literal $Builtin.Int32, 6789
%second = struct $Int32 (%second_literal : $Builtin.Int32)
%callee2 = apply %partialApplier<Int32>(%second, %callee1) : $@convention(thin) <T> (Int32, @owned @async @callee_owned (Int32) -> @out T) -> @async @callee_owned () -> @out T
%callee2 = apply %partialApplier<Int32>(%second, %callee1) : $@async @convention(thin) <T> (Int32, @owned @async @callee_owned (Int32) -> @out T) -> @async @callee_owned () -> @out T

%result_addr = alloc_stack $Int32
%result = apply %callee2(%result_addr) : $@async @callee_owned () -> @out Int32
Expand Down
2 changes: 1 addition & 1 deletion test/SIL/Serialization/async.sil
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Builtin
import Swift
import _Concurrency

sil [serialized] @aaa : $@convention(thin) () -> () {
sil [serialized] @aaa : $@async @convention(thin) () -> () {
entry:
%a = function_ref @async_continuation : $@convention(thin) @async () -> ()
%b = function_ref @async_continuation_throws : $@convention(thin) @async () -> ()
Expand Down