swiftlang / swift

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

[SR-9708] Failed assertion with opaque types across module boundaries #52146

Closed dmcyk closed 5 years ago

dmcyk commented 5 years ago
Previous ID SR-9708
Radar None
Original Reporter @dmcyk
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment Swift toolchain **swift-PR-21137-170**, from https://github.com/apple/swift/pull/21137
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | @jckarter | |Priority | Medium | md5: 7aa753d19e307b4d28134845dd2ca3cd

Issue Description:

Trying to use public function from module A, from module B causes the compiler to crash with an assertion.

Module A:

public protocol Foo {}

public struct FooImpl: Foo {
  public init() {}
}

public func anyFoo() -> __opaque Foo {
  return FooImpl()
}

Module B:

import modulea

dump(anyFoo())

Stack dump:

Assertion failed: (hasInterfaceType() && "No interface type was set"), function getInterfaceType, file /Users/buildnode/jenkins/workspace/swift-PR-toolchain-osx/branch-master/swift/lib/AST/Decl.cpp, line 2422.
Stack dump:
0.      Program arguments: /Library/Developer/Toolchains/swift-PR-21137-170.xctoolchain/usr/bin/swift -frontend -c -primary-file /Users/damian/Developer/Swift/Fuzz/Sources/Fuzz/main.swift -emit-module-path /Users/damian/Developer/Swift/Fu
zz/.build/x86_64-apple-macosx/debug/Fuzz.build/main~partial.swiftmodule -emit-module-doc-path /Users/damian/Developer/Swift/Fuzz/.build/x86_64-apple-macosx/debug/Fuzz.build/main~partial.swiftdoc -emit-dependencies-path /Users/damian/Devel
oper/Swift/Fuzz/.build/x86_64-apple-macosx/debug/Fuzz.build/main.d -emit-reference-dependencies-path /Users/damian/Developer/Swift/Fuzz/.build/x86_64-apple-macosx/debug/Fuzz.build/main.swiftdeps -target x86_64-apple-macosx10.10 -enable-ob
jc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I /Users/damian/Developer/Swift/Fuzz/.build/x86_64-apple-macosx/debug -F /Applications/Xcode.app/Contents/Developer/Platf
orms/MacOSX.platform/Developer/Library/Frameworks -enable-testing -g -module-cache-path /Users/damian/Developer/Swift/Fuzz/.build/x86_64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -module-name Fuzz -o
 /Users/damian/Developer/Swift/Fuzz/.build/x86_64-apple-macosx/debug/Fuzz.build/main.swift.o -index-store-path /Users/damian/Developer/Swift/Fuzz/.build/x86_64-apple-macosx/debug/index/store -index-system-modules
0  swift                    0x000000011368f218 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000011368e495 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000011368f822 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff5fc47b3d _sigtramp + 29
4  swift                    0x0000000114974b2d cmark_strbuf__initbuf + 130520
5  libsystem_c.dylib        0x00007fff5fb051c9 abort + 127
6  libsystem_c.dylib        0x00007fff5facd868 basename_r + 0
7  swift                    0x0000000110d89132 swift::ValueDecl::getInterfaceType() const + 50
8  swift                    0x0000000110d5ec35 (anonymous namespace)::Verifier::verifyChecked(swift::ValueDecl*) + 21
9  swift                    0x0000000110d56646 (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) + 326
10 swift                    0x0000000110d60c1c (anonymous namespace)::Traversal::doIt(swift::Decl*) + 252
11 swift                    0x0000000110d60b0b swift::Decl::walk(swift::ASTWalker&) + 27
12 swift                    0x0000000110d4d365 swift::verify(swift::Decl*) + 133
13 swift                    0x00000001109b1a5b swift::ModuleFile::verify() const + 91
14 swift                    0x0000000110a12ba8 swift::SerializedModuleLoaderBase::verifyAllModules() + 40
15 swift                    0x0000000110cdffd9 swift::ASTContext::verifyAllLoadedModules() const + 89
16 swift                    0x0000000110966b08 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsig
ned int) + 1432
17 swift                    0x000000011012f206 swift::CompilerInstance::parseAndTypeCheckMainFileUpTo(swift::SourceFile::ASTStage_t, swift::PersistentParserState&, swift::DelayedParsingCallbacks*, swift::OptionSet<swift::TypeCheckingFlags
, unsigned int>) + 502
18 swift                    0x000000011012db73 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t) + 435
19 swift                    0x000000011012d278 swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 616
20 swift                    0x000000010f909168 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1704
21 swift                    0x000000010f907a52 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2978
22 swift                    0x000000010f8b9a7e main + 686
23 libdyld.dylib            0x00007fff5fa5ced9 start + 1
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)shell returned 1
dmcyk commented 5 years ago

cc @jckarter

jckarter commented 5 years ago

Can you share any more detail about how you built the two modules? I tried this, and couldn't reproduce the crash:

$ cat a.swift 
public protocol Foo {}

public struct FooImpl: Foo {
  public init() {}
}

public func anyFoo() -> __opaque Foo {
  return FooImpl()
}

$ cat b.swift 
import modulea

dump(anyFoo())

$ ./bin/swiftc -force-single-frontend-invocation -emit-module-path modulea.swiftmodule -c -module-name modulea a.swift
$ ./bin/swiftc -c -I . b.swift 
dmcyk commented 5 years ago

ah sorry, should have written what the included `fuzz.zip` was.
I used SwiftPM (`swift run`), in the archive you can find ready package.

I tried compiling it now the way you did it and I'm getting same assertion.

I was testing with the toolchain you provided few days ago though, is it possible you were testing including the latest commits?

dmcyk commented 5 years ago
$ cat ../Info.plist
...
    <key>Aliases</key>
    <array>
        <string>PR21137</string>
    </array>
    <key>CFBundleIdentifier</key>
    <string>org.swift.pr.21137.170</string>
    ...
    <key>ShortDisplayName</key>
    <string>Swift PR - 21137 (170)</string>
</dict>
</plist>
$ cat a.swift
public protocol Foo {}

public struct FooImpl: Foo {
  public init() {}
}

public func anyFoo() -> __opaque Foo {
  return FooImpl()
}

$ cat b.swift
import modulea

dump(anyFoo())
$ ./bin/swiftc -force-single-frontend-invocation -emit-module-path modulea.swiftmodule -c -module-name modulea a.swift
$ ./bin/swiftc -c -I . b.swift
Assertion failed: (hasInterfaceType() && "No interface type was set"), function getInterfaceType, file /Users/buildnode/jenkins/workspace/swift-PR-toolchain-osx/branch-master/swift/lib/AST/Decl.cpp, line 2422.
Stack dump:
0.  Program arguments: /Users/damian/Downloads/Library/Developer/Toolchains/swift-PR-21137-170.xctoolchain/usr/bin/swift -frontend -c -primary-file b.swift -target x86_64-apple-darwin18.2.0 -enable-objc-interop -I . -color-diagnostics -module-name b -o b.o
0  swift                    0x000000010fed4218 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x000000010fed3495 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010fed4822 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff5fc47b3d _sigtramp + 29
4  libsystem_platform.dylib 0x000000011a384938 _sigtramp + 3128151576
5  libsystem_c.dylib        0x00007fff5fb051c9 abort + 127
6  libsystem_c.dylib        0x00007fff5facd868 basename_r + 0
7  swift                    0x000000010d5ce132 swift::ValueDecl::getInterfaceType() const + 50
8  swift                    0x000000010d5a3c35 (anonymous namespace)::Verifier::verifyChecked(swift::ValueDecl*) + 21
9  swift                    0x000000010d59b646 (anonymous namespace)::Verifier::walkToDeclPost(swift::Decl*) + 326
10 swift                    0x000000010d5a5c1c (anonymous namespace)::Traversal::doIt(swift::Decl*) + 252
11 swift                    0x000000010d5a5b0b swift::Decl::walk(swift::ASTWalker&) + 27
12 swift                    0x000000010d592365 swift::verify(swift::Decl*) + 133
13 swift                    0x000000010d1f6a5b swift::ModuleFile::verify() const + 91
14 swift                    0x000000010d257ba8 swift::SerializedModuleLoaderBase::verifyAllModules() + 40
15 swift                    0x000000010d524fd9 swift::ASTContext::verifyAllLoadedModules() const + 89
16 swift                    0x000000010d1abb08 swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int) + 1432
17 swift                    0x000000010c974206 swift::CompilerInstance::parseAndTypeCheckMainFileUpTo(swift::SourceFile::ASTStage_t, swift::PersistentParserState&, swift::DelayedParsingCallbacks*, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>) + 502
18 swift                    0x000000010c972b73 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t) + 435
19 swift                    0x000000010c972278 swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 616
20 swift                    0x000000010c14e168 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 1704
21 swift                    0x000000010c14ca52 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2978
22 swift                    0x000000010c0fea7e main + 686
23 libdyld.dylib            0x00007fff5fa5ced9 start + 1
24 libdyld.dylib            0x000000000000000f start + 2690265399
fish: './bin/swiftc -c -I . b.swift' terminated by signal SIGABRT (Abort)
jckarter commented 5 years ago

I'll ask swift-ci to build a new toolchain to see whether I incidentally fixed this.

dmcyk commented 5 years ago

hm, that's confusing.
I've tried latest build and I'm getting same assertion.
I'm on latest commit though.

$ ./bin/swiftc -v
Apple Swift version 5.0-dev (LLVM f63b283c71, Clang 41ac4c4262, Swift 551d614228)
Target: x86_64-apple-darwin18.2.0
jckarter commented 5 years ago

OK, thanks for giving it another try. Maybe I can try something to speculatively fix the issue.

jckarter commented 5 years ago

Maybe this will address the crash: https://github.com/apple/swift/pull/21137/commits/68cc0b5a2809f9678e05b0001d01f1cb39b50f7c I started another toolchain build for you to try out.

dmcyk commented 5 years ago

Still the same assertion fails. Could this be something with my environment that you can't reproduce the issue locally? I will try later too on another machine.

dmcyk commented 5 years ago

@jckarter I've tried it again with latest toolchain from the PR and seems like that's fixed

jckarter commented 5 years ago

Great, thanks for following up!