swift-llbuild icon indicating copy to clipboard operation
swift-llbuild copied to clipboard

[SR-89] Segfault importing sqlite3.h from system module

Open swift-ci opened this issue 9 years ago • 11 comments

Previous ID SR-89
Radar rdar://problem/26866326
Original Reporter m.buhot (JIRA User)
Type Bug

Attachment: Download

Environment

OSX 10.11.1 (15B42)
swift-2.2-SNAPSHOT-2015-12-01-a-osx

Additional Detail from JIRA
Votes 0
Component/s Compiler, llbuild
Labels Bug
Assignee @benlangmuir
Priority Medium

md5: 6256508fb06b830feeade0377fd99c27

relates to:

  • SR-1567 [SwiftPM] Prevent/solve issue with seeing headers in /usr/include and SDK (was: compiler crash when passing -Xcc -I/usr/include to swiftc)

Issue Description:

Trying to build a trivial application (sources attached) that imports sqlite3.h from a system module causes the segfault output below.

$ swift build -v
Cloning into '/Users/mbuhot/source/app/Packages/CSqlite3'...
remote: Counting objects: 4, done.        
remote: Compressing objects: 100% (3/3), done.        
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0        
Unpacking objects: 100% (4/4), done.
Checking connectivity... done.
/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swiftc -module-name app -incremental -emit-dependencies -emit-module -emit-module-path /Users/mbuhot/source/app/.build/debug/app.swiftmodule -output-file-map /Users/mbuhot/source/app/.build/debug/app.o/app/output-file-map.json -c /Users/mbuhot/source/app/main.swift -I /Users/mbuhot/source/app/.build/debug -j8 -Onone -g -target x86_64-apple-macosx10.10 -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -Xcc -F-module-map=/Users/mbuhot/source/app/Packages/CSqlite3-1.0.0/module.modulemap -I /Users/mbuhot/source/app/Packages/CSqlite3-1.0.0 -I /usr/local/include
0  swift                    0x000000010fbfa47b llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 43
1  swift                    0x000000010fbf9916 llvm::sys::RunSignalHandlers() + 70
2  swift                    0x000000010fbfaaa3 SignalHandler(int) + 243
3  libsystem_platform.dylib 0x00007fff8d25852a _sigtramp + 26
4  libsystem_platform.dylib 000000000000000000 _sigtramp + 1926920944
5  swift                    0x000000010e41502d clang::Preprocessor::MacroState::getModuleInfo(clang::Preprocessor&, clang::IdentifierInfo const*) const + 189
6  swift                    0x000000010e414a76 clang::Preprocessor::getMacroDefinition(clang::IdentifierInfo const*) + 326
7  swift                    0x000000010ed82e50 clang::Preprocessor::HandleIdentifier(clang::Token&) + 320
8  swift                    0x000000010ed2c4f4 clang::Lexer::LexIdentifier(clang::Token&, char const*) + 932
9  swift                    0x000000010ed32312 clang::Lexer::LexTokenInternal(clang::Token&, bool) + 8562
10 swift                    0x000000010ed834c4 clang::Preprocessor::Lex(clang::Token&) + 68
11 swift                    0x000000010ed542b7 clang::Preprocessor::ReadMacroName(clang::Token&, clang::MacroUse, bool*) + 55
12 swift                    0x000000010ed57630 clang::Preprocessor::HandleIfdefDirective(clang::Token&, bool, bool) + 48
13 swift                    0x000000010ed564f4 clang::Preprocessor::HandleDirective(clang::Token&) + 1124
14 swift                    0x000000010ed32966 clang::Lexer::LexTokenInternal(clang::Token&, bool) + 10182
15 swift                    0x000000010ed834c4 clang::Preprocessor::Lex(clang::Token&) + 68
16 swift                    0x000000010e6698bf clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) + 287
17 swift                    0x000000010e5eb875 clang::ParseAST(clang::Sema&, bool, bool) + 501
18 swift                    0x000000010e43eb72 clang::FrontendAction::Execute() + 66
19 swift                    0x000000010e40af43 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 979
20 swift                    0x000000010fb9e98d llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 269
21 swift                    0x000000010fb9ead0 RunSafelyOnThread_Dispatch(void*) + 48
22 swift                    0x000000010fbfbaed ExecuteOnThread_Dispatch(void*) + 13
23 libsystem_pthread.dylib  0x00007fff8ea2a9b1 _pthread_body + 131
24 libsystem_pthread.dylib  0x00007fff8ea2a92e _pthread_body + 0
25 libsystem_pthread.dylib  0x00007fff8ea28385 thread_start + 13
Stack dump:
0.  /usr/include/sqlite3.h:76:2: current parser token 'ifndef'
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
swift-build: exit(1): ["/Library/Developer/Toolchains/swift-2.2-SNAPSHOT-2015-12-01-a.xctoolchain/usr/bin/swift-build-tool", "-v", "-f", "/Users/mbuhot/source/app/.build/debug/app.o/llbuild.yaml"]

Sqlite version info from the header is:

#define SQLITE_VERSION        "3.8.10.2"
#define SQLITE_VERSION_NUMBER 3008010
#define SQLITE_SOURCE_ID      "2015-05-20 18:17:19 2ef4f3a5b1d1d0c4338f8243d40a2452cc1f7fe4"

swift-ci avatar Dec 06 '15 10:12 swift-ci

@benlangmuir is this in your area?

ddunbar avatar Dec 06 '15 16:12 ddunbar

Didn't reproduce for me in a debug build of ToT swift. Trying the packaged version.

benlangmuir avatar Dec 07 '15 17:12 benlangmuir

Doesn't reproduce for me with the packaged version either. My sqlite claims to be the same version as yours too (although I had to modify the path because I don't have it in /usr/include). If you want to be really sure, the hash is

$ shasum $sdk/usr/include/sqlite3.h
fdbb76c74b2862301b5a941b77e938df98910c50

benlangmuir avatar Dec 07 '15 18:12 benlangmuir

Comment by Michael Buhot (JIRA)

Interesting, works for me when using the SDK path also 😃 Even though the file has the same contents:

192-168-1-104:app mbuhot$ shasum /usr/include/sqlite3.h 
fdbb76c74b2862301b5a941b77e938df98910c50  /usr/include/sqlite3.h
192-168-1-104:app mbuhot$ shasum /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/sqlite3.h
fdbb76c74b2862301b5a941b77e938df98910c50  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/sqlite3.h

I installed the xcode command line tools to get /usr/include populated. Could it be #include-ing some other problematic file from there?

swift-ci avatar Dec 07 '15 19:12 swift-ci

This is probably related to the module maps that come with the /usr/include directories as part of the command line tools package.

ddunbar avatar Dec 07 '15 20:12 ddunbar

Or one of the other headers. I can reproduce using the command-line-tools. In debug, I get:

Assertion failed: ((ID || !Mod) && "asked for module ID for non-local, non-imported module"), function getSubmoduleID, file /Users/blangmuir/src/s/llvm/tools/clang/lib/Serialization/ASTWriter.cpp, line 2350.
0  swift                    0x0000000114a47dde llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 46
1  swift                    0x0000000114a48229 PrintStackTraceSignalHandler(void*) + 25
2  swift                    0x0000000114a449c9 llvm::sys::RunSignalHandlers() + 425
3  swift                    0x0000000114a48569 SignalHandler(int) + 345
4  libsystem_platform.dylib 0x00007fff8f58352a _sigtramp + 26
5  swift                    0x00000001154ebd3c cmark_strbuf__initbuf + 94235
6  swift                    0x0000000114a4824b raise + 27
7  swift                    0x0000000114a48302 abort + 18
8  swift                    0x0000000114a482e1 __assert_rtn + 129
9  swift                    0x00000001114df73d clang::ASTWriter::getSubmoduleID(clang::Module*) + 125

benlangmuir avatar Dec 07 '15 21:12 benlangmuir

Hmm seems to be related to the combination of:

  • module.modulemap says "/usr/include/sqlite3.h"

  • Build uses the option -sdk /Applications/Xcode.app/.../MacOSX10.11.sdk

If I manually change the SDK path to "/" then it stops crashing.

benlangmuir avatar Dec 07 '15 21:12 benlangmuir

I have the same issue.

More information that may help: the issue only happens when I import SQLite through another package (when I build package A which depends on package B which depends on CSQLite). However, the issue is not there when I build the package B directly.

groue avatar Dec 08 '15 09:12 groue

@groue: the workaround is for the path to sqlite.h in the module.modulemap file to match what's being used as the SDK for your compilation. For example, if you're using -sdk /Applications/Xcode.app/.../MacOSX10.11.sdk, then make sure the module map is looking in /Applications/Xcode.app/.../MacOSX10.11.sdk/usr/include/sqlite.h.

benlangmuir avatar Mar 22 '16 17:03 benlangmuir

@benlangmuir Thanks for the tip. It does indeed works, but with hard consequences :-)

Here is a real modulemap, and it does not even include support for tvOS, watchOS, or linux: https://github.com/groue/GRDB.swift/blob/v0.57.0/Support/module.modulemap

framework module GRDB {
    umbrella header "GRDB.h"

    export *
    module * { export * }
}

module SQLiteMacOSX {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/sqlite3.h"
    export *
}

module SQLiteiPhoneOS {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/sqlite3.h"
    export *
}

module SQLiteiPhoneSimulator {
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/sqlite3.h"
    export *
}

And in the source code, it's also difficult, because one has to pick up the right module depending on the platform (https://github.com/groue/GRDB.swift/blob/v0.57.0/GRDB/Core/Database.swift):

#if os(OSX)
    import SQLiteMacOSX
#elseif os(iOS)
    #if (arch(i386) || arch(x86_64))
        import SQLiteiPhoneSimulator
    #else
        import SQLiteiPhoneOS
    #endif
#endif

Not practical, but it works...

groue avatar Apr 10 '16 14:04 groue

Comment by Stefan Urbanek (JIRA)

Still having this issue when importing `Csqlite3` with the following module map:

module Csqlite3 [system] {
  header "/usr/include/sqlite3.h"
  link "sqlite3"
  export *
}

It fails on `import Csqlite3` with:

Assertion failed: ((ID || !Mod) && "asked for module ID for non-local, non-imported module"), function getSubmoduleID, file /Users/buildnode/jenkins/workspace/oss-swift-package-osx/llvm/tools/clang/lib/Serialization/ASTWriter.cpp, line 2377.
0  swift                    0x000000010351e54b llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 43
1  swift                    0x000000010351d7d6 llvm::sys::RunSignalHandlers() + 70
2  swift                    0x000000010351ec1f SignalHandler(int) + 383
3  libsystem_platform.dylib 0x00007fff9140a52a _sigtramp + 26
4  swift                    0x00000001040b03f0 clang::vfs::getNextVirtualUniqueID()::UID + 71992
5  swift                    0x000000010351e9f6 abort + 22
6  swift                    0x000000010351e9d1 __assert_rtn + 81
7  swift                    0x000000010198b402 clang::ASTWriter::WritePreprocessor(clang::Preprocessor const&, bool) + 6210
8  swift                    0x00000001019a2150 clang::ASTWriter::WriteASTCore(clang::Sema&, llvm::StringRef, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, clang::Module*) + 9168
9  swift                    0x000000010199fd32 clang::ASTWriter::WriteAST(clang::Sema&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, clang::Module*, llvm::StringRef, bool) + 642
10 swift                    0x00000001019dc700 clang::PCHGenerator::HandleTranslationUnit(clang::ASTContext&) + 112
11 swift                    0x00000001018075bc clang::MultiplexConsumer::HandleTranslationUnit(clang::ASTContext&) + 44
12 swift                    0x00000001019e9ca5 clang::ParseAST(clang::Sema&, bool, bool) + 581
13 swift                    0x00000001017d27a9 clang::FrontendAction::Execute() + 73
14 swift                    0x000000010179f361 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 1073
15 swift                    0x00000001034b096b llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 283
16 swift                    0x00000001034b0b30 RunSafelyOnThread_Dispatch(void*) + 48
17 swift                    0x000000010351fc6d ExecuteOnThread_Dispatch(void*) + 13
18 libsystem_pthread.dylib  0x00007fff88df099d _pthread_body + 131
19 libsystem_pthread.dylib  0x00007fff88df091a _pthread_body + 0
20 libsystem_pthread.dylib  0x00007fff88dee351 thread_start + 13
Stack dump:
0.      <eof> parser at end of file
<unknown>:0: error: unable to execute command: Illegal instruction: 4
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)
<unknown>:0: error: build had 1 command failures
error: exit(1): /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2016-05-09-a.xctoolchain/usr/bin/swift-build-tool -f /Users/stefan/Developer/Projects/AbstractSQL/.build/debug.yaml -v

Swift version:

$ swift --version
Apple Swift version 3.0-dev (LLVM dffa09ffd8, Clang 9f0d189820, Swift 1c720b8f84)
Target: x86_64-apple-macosx10.9

swift-ci avatar May 13 '16 07:05 swift-ci