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

Runtime crash when calling functions with opaque return types, stored as closures #69057

Open
cristik opened this issue Oct 9, 2023 · 4 comments
Assignees
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software function types Feature → types: function types generics Feature: generic declarations and types implicit conversions Feature: implicit conversions opaque result types Feature → types → opaque types: opaque result types opaque types Feature → types: opaque types SILGen Area → compiler: The SIL generation stage swift 6.0 types Feature: types

Comments

@cristik
Copy link

cristik commented Oct 9, 2023

Description
The following code crashes at runtime:

protocol Worker { }

struct DummyWorker: Worker {
    init(_: Int) { }
}

let workerFactory: (Int) -> some Worker = DummyWorker.init

let worker = workerFactory(15)
print(worker)

It crashes when calling workerFactory with

Thread 1: EXC_BAD_ACCESS (code=1, address=0xf)

The address from the crash message is influenced by the value of the parameter passed to workerFactory, for example workerFactory(4) will crash with address 0x4.

If I change the parameter type to [Int] (or any kind of array), the code no longer crashes, however as soon as I add another Int/Double/String/etc parameter it will again crash, but if I add a second array parameter it will not crash.

I think this is a compiler bug, but unsure where the bug is:

  • either the compiler should not allow declaring a type that consist of a function that returns an opaque value (if one tries to write a closure, the compiler won't allow it to use the some keyword)
  • or the bug is when the compiler wraps up the opaque value

Steps to reproduce
Run the above code snippet.

Expected behavior
Either it should not crash, or the compiler should now allow using some in this scenario.

Environment

  • Swift compiler version info:

    swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
    Target: arm64-apple-macosx13.0

  • Xcode version info:

    Xcode 15.0
    Build version 15A240d

  • Deployment target: iOS 17, macOS 13
@cristik cristik added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Oct 9, 2023
@kushal34712
Copy link

assign me

@vanvoorden
Copy link
Contributor

@AnthonyLatsis I am still seeing this same crash from Xcode_16_beta_3. Would you have any ability to help triage or close if we are tracking the issue somewhere else?

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself SILGen Area → compiler: The SIL generation stage opaque types Feature → types: opaque types crash Bug: A crash, i.e., an abnormal termination of software function types Feature → types: function types opaque result types Feature → types → opaque types: opaque result types implicit conversions Feature: implicit conversions types Feature: types assertion failure Bug → crash: An assertion failure swift 6.0 generics Feature: generic declarations and types and removed triage needed This issue needs more specific labels labels Jul 12, 2024
@AnthonyLatsis
Copy link
Collaborator

This crashes at compile time with quite a recent dev compiler. Here is a reduction and a stack trace:

protocol P {}
struct S: P {}

func foo(_: Int) -> S { S() }

let test: (Int) -> some P = foo
Assertion failed: ((!F || opTI->isABICompatibleWith(resTI, *F).isCompatible()) && "Can not convert in between ABI incompatible function types"), function create, file SILInstructions.cpp, line 2783.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /Users/mac/Desktop/swift-project/build/Ninja-RelWithDebInfoAssert+swift-DebugAssert/swift-macosx-x86_64/bin/swift-frontend -frontend -emit-silgen -primary-file /Users/mac/Desktop/test.swift -target x86_64-apple-darwin23.5.0 -enable-objc-interop -color-diagnostics -typo-correction-limit 10 -debug-diagnostic-names -debug-cycles -plugin-path /Users/mac/Desktop/swift-project/build/Ninja-RelWithDebInfoAssert+swift-DebugAssert/swift-macosx-x86_64/lib/swift/host/plugins -plugin-path /Users/mac/Desktop/swift-project/build/Ninja-RelWithDebInfoAssert+swift-DebugAssert/swift-macosx-x86_64/local/lib/swift/host/plugins -dwarf-version=4 -module-name test -o -
1.	Swift version 6.0-dev (LLVM c7c87ee42989d4b, Swift 8493312b06605f1)
2.	Compiling with effective version 5.10
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/Users/mac/Desktop/test.swift")
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x00000001100761b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 40
1  swift-frontend           0x0000000110074605 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x000000011007680e SignalHandler(int) + 270
3  libsystem_platform.dylib 0x00007ff80d870fdd _sigtramp + 29
4  libsystem_platform.dylib 0x00007f8f9a8b8cf8 _sigtramp + 18446743625103867192
5  libsystem_c.dylib        0x00007ff80d767a79 abort + 126
6  libsystem_c.dylib        0x00007ff80d766d68 err + 0
7  swift-frontend           0x00000001099356e7 swift::ConvertFunctionInst::create(swift::SILDebugLocation, swift::SILValue, swift::SILType, swift::SILModule&, swift::SILFunction*, bool, swift::ValueOwnershipKind) + 807
8  swift-frontend           0x000000010840761a swift::SILBuilder::createConvertFunction(swift::SILLocation, swift::SILValue, swift::SILType, bool, swift::ValueOwnershipKind) + 266
9  swift-frontend           0x0000000108426ee6 swift::SILBuilder::createConvertFunction(swift::SILLocation, swift::SILValue, swift::SILType, bool) + 166
10 swift-frontend           0x0000000109841a98 swift::SILBuilder::createUncheckedReinterpretCast(swift::SILLocation, swift::SILValue, swift::SILType) + 824
11 swift-frontend           0x00000001085fa1e9 swift::Lowering::SILGenBuilder::createUncheckedBitCast(swift::SILLocation, swift::Lowering::ManagedValue, swift::SILType) + 153
12 swift-frontend           0x00000001086a8b2b (anonymous namespace)::RValueEmitter::visitUnderlyingToOpaqueExpr(swift::UnderlyingToOpaqueExpr*, swift::Lowering::SGFContext) + 2635
13 swift-frontend           0x000000010868bf95 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 4773
14 swift-frontend           0x000000010867b671 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 209
15 swift-frontend           0x000000010867b475 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 357
16 swift-frontend           0x000000010864624f swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool)::$_0::operator()(swift::Expr*, swift::VarDecl*, bool, std::__1::unique_ptr<swift::Lowering::Initialization, std::__1::default_delete<swift::Lowering::Initialization>>&) const + 671
17 swift-frontend           0x0000000108645c31 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 1489
18 swift-frontend           0x0000000108646326 swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*, bool) + 150
19 swift-frontend           0x000000010865b142 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 1522
20 swift-frontend           0x000000010880eaca swift::Lowering::SILGenTopLevel::visitTopLevelCodeDecl(swift::TopLevelCodeDecl*) + 714
21 swift-frontend           0x000000010880e27f swift::ASTVisitor<swift::Lowering::SILGenTopLevel, void, void, void, void, void, void>::visit(swift::Decl*) + 1391
22 swift-frontend           0x000000010880b9fd swift::Lowering::SILGenTopLevel::visitSourceFile(swift::SourceFile*) + 189
23 swift-frontend           0x000000010880add3 swift::Lowering::SILGenModule::emitEntryPoint(swift::SourceFile*, swift::SILFunction*) + 1763
24 swift-frontend           0x000000010880da38 swift::Lowering::SILGenModule::emitEntryPoint(swift::SourceFile*) + 344
25 swift-frontend           0x0000000108566b4b (anonymous namespace)::SILGenModuleRAII::emitSourceFile(swift::SourceFile*) + 91
26 swift-frontend           0x00000001085667ef swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 879
27 swift-frontend           0x00000001087eb18e std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 142
28 swift-frontend           0x00000001087eb0a8 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 40
29 swift-frontend           0x00000001085829e8 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 264
30 swift-frontend           0x00000001085828c8 swift::ASTLoweringRequest::OutputType swift::Evaluator::operator()<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'(), (void*)0>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 40
31 swift-frontend           0x0000000108567404 swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest) + 36
32 swift-frontend           0x0000000108567740 swift::performASTLowering(swift::FileUnit&, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 256
33 swift-frontend           0x000000010777daeb swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 779
34 swift-frontend           0x00000001077aefd3 performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*)::$_29::operator()(swift::CompilerInstance&) const + 147
35 swift-frontend           0x00000001077aef2d bool llvm::function_ref<bool (swift::CompilerInstance&)>::callback_fn<performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*)::$_29>(long, swift::CompilerInstance&) + 29
36 swift-frontend           0x00000001077ae2f1 llvm::function_ref<bool (swift::CompilerInstance&)>::operator()(swift::CompilerInstance&) const + 33
37 swift-frontend           0x00000001077ad288 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 392
38 swift-frontend           0x00000001077a5645 performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1349
39 swift-frontend           0x0000000107780d86 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 262
40 swift-frontend           0x000000010777f78a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2794
41 swift-frontend           0x000000010743b307 run_driver(llvm::StringRef, llvm::ArrayRef<char const*>, llvm::ArrayRef<char const*>) + 375
42 swift-frontend           0x000000010743a737 swift::mainEntry(int, char const**) + 1543
43 swift-frontend           0x0000000107439e02 main + 34
44 dyld                     0x00007ff80d4b6366 start + 1942
Abort trap: 6

@vanvoorden
Copy link
Contributor

@AnthonyLatsis Ahh… interesting! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertion failure Bug → crash: An assertion failure bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software function types Feature → types: function types generics Feature: generic declarations and types implicit conversions Feature: implicit conversions opaque result types Feature → types → opaque types: opaque result types opaque types Feature → types: opaque types SILGen Area → compiler: The SIL generation stage swift 6.0 types Feature: types
Projects
None yet
Development

No branches or pull requests

5 participants