swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.68k stars 10.39k forks source link

Swift segfault in LLVM #61947

Open izik1 opened 2 years ago

izik1 commented 2 years ago

Describe the bug Swift segfaults in LLVM, as reproducible below. I think I saw some glimmer of "ptrtoint cast must have a pointer" at some point while I was minimizing but that text doesn't show up with the minimized code :/

Steps To Reproduce Attempt to build the following snippet: (paste it in a playground, paste it in a swift repl, paste it in a new project, etc)

public struct Foo: ExpressibleByStringLiteral {
    // Any integer type seems to reproduce
    public let bar: UInt8?

    public init!(stringLiteral value: StringLiteralType) {
        // any value works here
        bar = nil
    }
}

public func foo() {
    // I tried a different constructor, doesn't die, tried directly calling `Foo(stringLiteral: "")`, doesn't die.
    let it: Foo = ""
    // The field access is required, but you don't have to _do_ anything with it.
    it.bar
}

Expected behavior The snippet should compile.

Screenshots N/A

Environment (please fill out the following information)

Additional context Output from building the above code:

❯ swift build --verbose
Planning build
Building for debugging...
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -module-name Hedera -incremental -emit-dependencies -emit-module -emit-module-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.swiftmodule -output-file-map /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/output-file-map.json -parse-as-library -c /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift -I /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug -target arm64-apple-macosx10.15 -swift-version 5 -enable-batch-mode -index-store-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/index/store -Onone -enable-testing -g -j10 -DSWIFT_PACKAGE -DDEBUG -module-cache-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/ModuleCache -parseable-output -parse-as-library -emit-objc-header -emit-objc-header-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera-Swift.h -color-diagnostics -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -v
Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50)Target: arm64-apple-macosx10.15/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift -target arm64-apple-macosx10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name Hedera -disable-clang-spi -target-sdk-version 12.3 -emit-module-doc-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.swiftdoc -emit-module-source-info-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.swiftsourceinfo -emit-objc-header-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera-Swift.h -emit-dependencies-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.emit-module.d -parse-as-library -o /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.swiftmodule -emit-abi-descriptor-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.abi.json
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift -emit-dependencies-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.d -emit-reference-dependencies-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.swiftdeps -target arm64-apple-macosx10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name Hedera -disable-clang-spi -target-sdk-version 12.3 -parse-as-library -o /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.swift.o -index-store-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/index/store -index-system-modules
error: compile command failed due to signal 11 (use -v to see invocation)/Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift:15:8: warning: expression of type 'UInt8?' is unused
    it.bar
    ~~~^~~
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project and the crash backtrace.
Stack dump:
0.      Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift -emit-dependencies-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.d -emit-reference-dependencies-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.swiftdeps -target arm64-apple-macosx10.15 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -I /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -module-name Hedera -disable-clang-spi -target-sdk-version 12.3 -parse-as-library -o /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/Hedera.build/Hedera.swift.o -index-store-path /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/.build/arm64-apple-macosx/debug/index/store -index-system-modules
1.      Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50)
2.      Compiling with the current language version
3.      While evaluating request IRGenRequest(IR Generation for file "/Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift")
4.      While emitting IR SIL function "@$s6Hedera3fooyyF".
 for 'foo()' (at /Users/sr/ghq/github.com/hashgraph/hedera-sdk-rust/sdk/swift/Sources/Hedera/Hedera.swift:11:8)
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           0x0000000106dcbe70 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000106dcae74 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000106dcc4f4 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x00000001a88002a4 _sigtramp + 56
4  swift-frontend           0x00000001027b99c4 swift::irgen::projectPhysicalStructMemberFromExplosion(swift::irgen::IRGenFunction&, swift::SILType, swift::irgen::Explosion&, swift::VarDecl*, swift::irgen::Explosion&) + 52
5  swift-frontend           0x0000000102830cb4 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 42048
6  swift-frontend           0x00000001028218e8 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 7860
7  swift-frontend           0x00000001026edfc0 swift::irgen::IRGenerator::emitGlobalTopLevel(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&) + 1552
8  swift-frontend           0x00000001027ef750 swift::IRGenRequest::evaluate(swift::Evaluator&, swift::IRGenDescriptor) const + 2556
9  swift-frontend           0x000000010281f768 swift::SimpleRequest<swift::IRGenRequest, swift::GeneratedModule (swift::IRGenDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::IRGenRequest const&, swift::Evaluator&) + 188
10 swift-frontend           0x00000001027fbc44 llvm::Expected<swift::IRGenRequest::OutputType> swift::Evaluator::getResultUncached<swift::IRGenRequest>(swift::IRGenRequest const&) + 744
11 swift-frontend           0x00000001027f2b38 swift::performIRGeneration(swift::FileUnit*, swift::IRGenOptions const&, swift::TBDGenOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::StringRef, llvm::GlobalVariable**) + 236
12 swift-frontend           0x000000010241d3cc performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2160
13 swift-frontend           0x000000010241f158 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 5932
14 swift-frontend           0x00000001023bfc44 swift::mainEntry(int, char const**) + 3940
15 dyld                     0x00000001a84a7e50 start + 2544
LucianoPAlmeida commented 2 years ago

Reproducible in 5.7, actually fails in SIL verification on main: "SIL verification failed: must struct_extract from struct: sd" See here

tbkka commented 2 years ago

Thank you for taking the time to reduce that!

CC: @meg-gupta @atrick

nate-chandler commented 2 years ago

Synced to rdar://102249283 .