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

[SR-13069] Crash when using autoclosure escaping in property wrappers #55515

Closed
swift-ci opened this issue Jun 24, 2020 · 7 comments
Closed

[SR-13069] Crash when using autoclosure escaping in property wrappers #55515

swift-ci opened this issue Jun 24, 2020 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13069
Radar rdar://problem/65052537
Original Reporter pablobart (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5)
Target: x86_64-apple-darwin19.5.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash
Assignee pablobart (JIRA)
Priority Medium

md5: 5b6c6380de232e4234f38ab9912541e0

is duplicated by:

  • SR-13552 Property wrapper with initializer accepting closure and any additional argument causes segmentation fault

Issue Description:

When declaring a property wrapper with an autoclosure escaping block as the wrapped value like the following:

@propertyWrapper
public class Stored<Value> {
    public var wrappedValue: Value {
        return value()
    }
    
    public var value: () -> Value
    
    public init(wrappedValue: @autoclosure @escaping () -> Value, _ key: String) {
        value = wrappedValue
    }
}


struct ValueHolder {
    @Stored("test")
    var storedProperty: String = ""
} 

the swift compiler included in the Xcode 12 beta 1 (Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5) Target: x86_64-apple-darwin19.5.0) crashes with the following stack trace:

 Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret crash.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -target-sdk-version 10.15.4 -module-name crash 
1.  Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5)
2.  While evaluating request SILGenWholeModuleRequest(SIL Generation for module crash)
3.  While silgen emitPropertyWrapperBackingInitializer SIL function "@$s5crash11ValueHolderV14storedPropertySSvpfP".
 for 'storedProperty' (at crash.swift:16:9)
4.  While emitting reabstraction thunk in SIL function "@$sS2SIegr_TR".
 for <<debugloc at "<compiler-generated>":0:0>>Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret crash.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -target-sdk-version 10.15.4 -module-name crash 
1.  Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5)
2.  While evaluating request SILGenWholeModuleRequest(SIL Generation for module crash)
3.  While silgen emitPropertyWrapperBackingInitializer SIL function "@$s5crash11ValueHolderV14storedPropertySSvpfP".
 for 'storedProperty' (at crash.swift:16:9)
4.  While emitting reabstraction thunk in SIL function "@$sS2SIegr_TR".
 for <<debugloc at "<compiler-generated>":0:0>>0  swift                    0x000000010b9e7f65 0  swift                    0x000000010b9e7f65 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010b9e6f65 llvm::sys::RunSignalHandlers() + 85
1  swift                    0x000000010b9e6f65 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010b9e851f SignalHandler(int) + 111
3  libsystem_platform.dylib 0x00007fff6c7335fd _sigtramp + 29
4  libsystem_platform.dylib 0000000000000000002  swift                    0x000000010b9e851f SignalHandler(int) + 111
3  libsystem_platform.dylib 0x00007fff6c7335fd _sigtramp + 29
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603338696673824
 _sigtramp + 18446603338696673824
5  swift                    0x000000010810725a swift::ApplyInst::create(swift::SILDebugLocation, swift::SILValue, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>, bool, llvm::Optional<swift::SILModuleConventions>, swift::SILFunction&, swift::SILOpenedArchetypesState&, swift::GenericSpecializationInformation const*) + 362
5  swift                    0x000000010810725a swift::ApplyInst::create(swift::SILDebugLocation, swift::SILValue, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>, bool, llvm::Optional<swift::SILModuleConventions>, swift::SILFunction&, swift::SILOpenedArchetypesState&, swift::GenericSpecializationInformation const*) + 362
6  swift                    0x000000010799e53d swift::SILBuilder::createApply(swift::SILLocation, swift::SILValue, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>, bool, swift::GenericSpecializationInformation const*) + 237
6  swift                    0x000000010799e53d swift::SILBuilder::createApply(swift::SILLocation, swift::SILValue, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>, bool, swift::GenericSpecializationInformation const*) + 237
7  swift                    0x0000000107a01145 swift::Lowering::SILGenFunction::emitApplyWithRethrow(swift::SILLocation, swift::SILValue, swift::SILType, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>) + 597
7  swift                    0x0000000107a01145 swift::Lowering::SILGenFunction::emitApplyWithRethrow(swift::SILLocation, swift::SILValue, swift::SILType, swift::SubstitutionMap, llvm::ArrayRef<swift::SILValue>) + 597
8  swift                    0x0000000107ae4dde createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 5454
8  swift                    0x0000000107ae4dde createThunk(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::TypeLowering const&) + 5454
9  swift                    0x0000000107ad7a35 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1589
9  swift                    0x0000000107ad7a35 (anonymous namespace)::Transform::transform(swift::Lowering::ManagedValue, swift::Lowering::AbstractionPattern, swift::CanType, swift::Lowering::AbstractionPattern, swift::CanType, swift::SILType, swift::Lowering::SGFContext) + 1589
10 swift                    0x0000000107a51d93 swift::Lowering::Conversion::emit(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::SGFContext) const + 1315
10 swift                    0x0000000107a51d93 swift::Lowering::Conversion::emit(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, swift::Lowering::SGFContext) const + 1315
11 swift                    0x0000000107a52d53 swift::Lowering::ConvertingInitialization::copyOrInitValueInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, bool) + 131
11 swift                    0x0000000107a52d53 swift::Lowering::ConvertingInitialization::copyOrInitValueInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, bool) + 131
12 swift                    0x0000000107a514ec swift::Lowering::SILGenFunction::manageOpaqueValue(swift::Lowering::ManagedValue, swift::SILLocation, swift::Lowering::SGFContext) + 220
12 swift                    0x0000000107a514ec swift::Lowering::SILGenFunction::manageOpaqueValue(swift::Lowering::ManagedValue, swift::SILLocation, swift::Lowering::SGFContext) + 220
13 swift                    0x0000000107a8ea88 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 696
13 swift                    0x0000000107a8ea88 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 696
14 swift                    0x0000000107a79ad9 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext)14 swift                    0x0000000107a79ad9 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 121
 + 121
15 swift                    0x00000001079da9f0 swift::Lowering::ArgumentSource::getAsSingleValue(swift::Lowering::SILGenFunction&, swift::Lowering::SGFContext) && + 928
15 swift                    0x00000001079da9f0 swift::Lowering::ArgumentSource::getAsSingleValue(swift::Lowering::SILGenFunction&, swift::Lowering::SGFContext) && + 928
16 swift                    0x0000000107a517cc swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 492
16 swift                    0x0000000107a517cc swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 492
17 swift                    0x0000000107a1a86c (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 4124
17 swift                    0x0000000107a1a86c (anonymous namespace)::ArgEmitter::emit(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 4124
18 swift                    0x0000000107a01f9d (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 173
18 swift                    0x0000000107a01f9d (anonymous namespace)::ArgEmitter::emitSingleArg(swift::Lowering::ArgumentSource&&, swift::Lowering::AbstractionPattern) + 173
19 swift                    0x0000000107a1967e (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 238
19 swift                    0x0000000107a1967e (anonymous namespace)::ArgEmitter::emitPreparedArgs(swift::Lowering::PreparedArguments&&, swift::Lowering::AbstractionPattern) + 238
20 swift                    0x0000000107a22825 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 949
20 swift                    0x0000000107a22825 (anonymous namespace)::CallEmission::emitArgumentsForNormalApply(swift::Lowering::AbstractionPattern, swift::CanTypeWrapper<swift::SILFunctionType>, llvm::Optional<swift::ForeignErrorConvention> const&, swift::ImportAsMemberStatus, llvm::SmallVectorImpl<swift::Lowering::ManagedValue>&, llvm::Optional<swift::SILLocation>&) + 949
21 swift                    0x0000000107a0856f (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3935
21 swift                    0x0000000107a0856f (anonymous namespace)::CallEmission::apply(swift::Lowering::SGFContext) + 3935
22 swift                    0x0000000107a0572c swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2652
22 swift                    0x0000000107a0572c swift::Lowering::SILGenFunction::emitApplyExpr(swift::ApplyExpr*, swift::Lowering::SGFContext) + 2652
23 swift                    0x0000000107a79aa9 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 73
23 swift                    0x0000000107a79aa9 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 73
24 swift                    0x0000000107af966f swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 447
24 swift                    0x0000000107af966f swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 447
25 swift                    0x0000000107a9db2d swift::Lowering::SILGenFunction::emitGeneratorFunction(swift::SILDeclRef, swift::Expr*, bool) + 2029
25 swift                    0x0000000107a9db2d swift::Lowering::SILGenFunction::emitGeneratorFunction(swift::SILDeclRef, swift::Expr*, bool) + 2029
26 swift                    0x00000001079fc2fc swift::Lowering::SILGenModule::emitPropertyWrapperBackingInitializer(swift::VarDecl*)::$_10::operator()(swift::SILFunction*) const + 636
26 swift                    0x00000001079fc2fc swift::Lowering::SILGenModule::emitPropertyWrapperBackingInitializer(swift::VarDecl*)::$_10::operator()(swift::SILFunction*) const + 636
27 swift                    0x00000001079f465e swift::Lowering::SILGenModule::emitPropertyWrapperBackingInitializer(swift::VarDecl*) + 398
27 swift                    0x00000001079f465e swift::Lowering::SILGenModule::emitPropertyWrapperBackingInitializer(swift::VarDecl*) + 398
28 swift                    0x0000000107b04f6e (anonymous namespace)::SILGenType::visitVarDecl(swift::VarDecl*) + 302
28 swift                    0x0000000107b04f6e (anonymous namespace)::SILGenType::visitVarDecl(swift::VarDecl*) + 302
29 swift                    0x0000000107affb0b (anonymous namespace)::SILGenType::emitType() + 427
29 swift                    0x0000000107affb0b (anonymous namespace)::SILGenType::emitType() + 427
30 swift                    0x00000001079f92a2 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 82
30 swift                    0x00000001079f92a2 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 82
31 swift                    0x00000001079f75c9 (anonymous namespace)::SILGenModuleRAII::emitSourceFile(swift::SourceFile*) + 1417
31 swift                    0x00000001079f75c9 (anonymous namespace)::SILGenModuleRAII::emitSourceFile(swift::SourceFile*) + 1417
32 swift                    0x00000001079f8c89 swift::SILGenWholeModuleRequest::evaluate(swift::Evaluator&, swift::SILGenDescriptor) const + 185
32 swift                    0x00000001079f8c89 swift::SILGenWholeModuleRequest::evaluate(swift::Evaluator&, swift::SILGenDescriptor) const + 185
33 swift                    0x0000000107af1c71 33 swift                    0x0000000107af1c71 swift::SimpleRequest<swift::SILGenWholeModuleRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::SILGenDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::SILGenWholeModuleRequest const&, swift::Evaluator&) + 33
swift::SimpleRequest<swift::SILGenWholeModuleRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> > (swift::SILGenDescriptor), (swift::RequestFlags)1>::evaluateRequest(swift::SILGenWholeModuleRequest const&, swift::Evaluator&) + 33
34 swift                    0x00000001079fcd0d llvm::Expected<swift::SILGenWholeModuleRequest::OutputType> swift::Evaluator::getResultUncached<swift::SILGenWholeModuleRequest>(swift::SILGenWholeModuleRequest const&) + 973
34 swift                    0x00000001079fcd0d llvm::Expected<swift::SILGenWholeModuleRequest::OutputType> swift::Evaluator::getResultUncached<swift::SILGenWholeModuleRequest>(swift::SILGenWholeModuleRequest const&) + 973
35 swift                    0x000000010756b6e2 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 21186
35 swift                    0x000000010756b6e2 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 21186
36 swift                    0x00000001074eca97 main + 1255
37 libdyld.dylib            0x00007fff6c53acc936 swift                    0x00000001074eca97 main + 1255
37 libdyld.dylib            0x00007fff6c53acc9 start + 1
38 libdyld.dylib            0x000000000000000c start + 18446603338698740548
 start + 1
38 libdyld.dylib            0x000000000000000c start + 18446603338698740548

The compiler doesn't crash if the autoclosure is not escaping, or if the second parameter in the init is removed.

Attached:

  • The sample swift file

  • Stack dump when running `swift crash.swift`

  • A "reproducer" while running "swift" interactively directly from the command line

@hborla
Copy link
Member

hborla commented Jul 2, 2020

@swift-ci create

@hborla
Copy link
Member

hborla commented Jul 9, 2020

2 possible workarounds:

1. Provide a default argument for the second initializer parameter:

    public init(wrappedValue: @autoclosure @escaping () -> Value, _ key: String = "") {
        value = wrappedValue
    }

2. Provide both property wrapper initializer arguments in the wrapper attribute:

    @Stored(wrappedValue: "", "test")
    var storedProperty: String

@swift-ci
Copy link
Collaborator Author

Comment by John Estropia (JIRA)

Commenting here as this also broke CoreStore on Swift 5.3 (Xcode 12 beta 4)
https://github.com/JohnEstropia/CoreStore/blob/4a97d5a8dc023f8e4b0d94bf4dbfd2da4286532c/Sources/Field.Stored.swift#L71

@hborla
Copy link
Member

hborla commented Aug 18, 2020

This is fixed by #32866

Could you please verify using the latest master snapshot from https://swift.org/download/#snapshots ? Thank you!

@swift-ci
Copy link
Collaborator Author

Comment by John Estropia (JIRA)

@hborla
Thanks, I verified it working with the master snapshot (Aug 11), but it looks like it's not fixed in the latest Swift 5.3 snapshot (Aug 18) yet. Will it be included in the final Swift 5.3 release?

@hborla
Copy link
Member

hborla commented Aug 19, 2020

johnestropia (JIRA User) Unfortunately no, the fix couldn't be cherry-picked to Swift 5.3. However, there are two workarounds that I mentioned above if either of those work for your case

@swift-ci
Copy link
Collaborator Author

Comment by John Estropia (JIRA)

That's unfortunate, but thanks. Since I need it for an open-source library, what I'd do for now is something like

public init(
    wrappedValue: @autoclosure @escaping () -> Value, 
    _ key: String = { fatalError("key is required. This is a workaround for SR-13069") }()) {
        ...
}

Though unfortunately it's a runtime check, and might be overlooked during compilation.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

3 participants