swiftlang / swift

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

[SR-12403] Getting a Segmentation fault: 11 when accessing FileManager.default.currentDirectoryPath #54841

Open swift-ci opened 4 years ago

swift-ci commented 4 years ago
Previous ID SR-12403
Radar rdar://problem/59729722
Original Reporter michal.srutek (JIRA User)
Type Bug

Attachment: Download

Environment MacOS 10.15.3 Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 10 | |Component/s | Compiler | |Labels | Bug, 5.1Regression, CompilerCrash | |Assignee | None | |Priority | Medium | md5: 07e2368c7e176563c97c142ec9826f10

Issue Description:

I've got a custom Swift script that is similar to the following:

#!/usr/bin/env xcrun --sdk macosx swift

import Foundation

let path = FileManager.default.currentDirectoryPath

On Swift 5.1 and lower it runs just fine, but upon updating to Swift 5.2, I get

0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret test.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name test 
1. Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
2. While running user code "test.swift"
0 swift 0x000000010dd2f4ea PrintStackTraceSignalHandler(void*) + 42
1 swift 0x000000010dd2ecc0 SignalHandler(int) + 352
2 libsystem_platform.dylib 0x00007fff6710f42d _sigtramp + 29
3 libsystem_platform.dylib 0x00007ffee6222018 _sigtramp + 2131831816
4 swift 0x0000000109ab29ba llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 458
5 swift 0x0000000109ab9a2b llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, 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&, char const* const*) + 2011
6 swift 0x0000000109a8eaea performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 14362
7 swift 0x0000000109a834a5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 55813
8 swift 0x00000001099f94d3 main + 1283
9 libdyld.dylib 0x00007fff66f167fd start + 1
10 libdyld.dylib 0x000000000000000b start + 2567870479
Segmentation fault: 11

Is this a bug, or am I doing something wrong?

swift-ci commented 4 years ago

Comment by Ondrej Hanslik (JIRA)

It also crashes for me, I have a completely different script, the crash trace is the same. A script with a single `print` works fine but most other commands crash the compiler (e.g. creating `URL` from a string).

swift-ci commented 4 years ago

Comment by Peter Steinberger (JIRA)

We're hitting this as well after updating from Xcode 11.3.1 to Xcode 11.4.

Stack dump:
0.  Program arguments: /Applications/Xcode-11.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret /Users/Adrian/Projects/pspdfkit/pspdfkit/iOS/PSPDFKit/../bin/UpdateLocalizedStringKeys.swift -enable-objc-interop -stack-check -sdk /Applications/Xcode-11.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -module-name UpdateLocalizedStringKeys -- -strings /Users/Adrian/Projects/pspdfkit/pspdfkit/iOS/PSPDFKit/PSPDFKit.bundle/en.lproj/PSPDFKit.strings -stringsdict /Users/Adrian/Projects/pspdfkit/pspdfkit/iOS/PSPDFKit/PSPDFKit.bundle/en.lproj/PSPDFKit.stringsdict -objc /Users/Adrian/Projects/pspdfkit/pspdfkit/iOS/PSPDFKit/Localization/PSPDFLocalizedStringKeys.h 
1.  Apple Swift version 5.2 (swiftlang-1103.0.32.1 clang-1103.0.32.29)
2.  While running user code "/Users/Adrian/Projects/pspdfkit/pspdfkit/iOS/PSPDFKit/../bin/UpdateLocalizedStringKeys.swift"
0  swift                    0x000000010ab224ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x000000010ab21cc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff675e15fd _sigtramp + 29
3  libsystem_platform.dylib 0x000000010c302c00 _sigtramp + 2765231648
4  libsystem_platform.dylib 0x000000010e843020 _sigtramp + 2804292160
5  swift                    0x00000001068a59ba llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 458
6  swift                    0x00000001068aca2b llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, 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&, char const* const*) + 2011
7  swift                    0x0000000106881aea performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, bool, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, bool, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 14362
8  swift                    0x00000001068764a5 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 55813
9  swift                    0x00000001067ec4d3 main + 1283
10 libdyld.dylib            0x00007fff673e8cc9 start + 1
/Users/Adrian/Library/Developer/Xcode/DerivedData/PSPDFKit-hcfwhbqsbrlekyhffzenhcscpmmm/Build/Intermediates.noindex/PSPDFKit.build/Debug-iphonesimulator/UpdateGeneratedSources.build/Script-A57337B223D7B18D00737748.sh: line 2: 13873 Segmentation fault: 11  /usr/bin/env xcrun --sdk macosx swift "${PROJECT_DIR}/../bin/UpdateLocalizedStringKeys.swift" -strings "${SRCROOT}/PSPDFKit.bundle/en.lproj/PSPDFKit.strings" -stringsdict "${SRCROOT}/PSPDFKit.bundle/en.lproj/PSPDFKit.stringsdict" -objc "${SRCROOT}/Localization/PSPDFLocalizedStringKeys.h"
Command PhaseScriptExecution failed with a nonzero exit code
swift-ci commented 4 years ago

Comment by Igor Kulman (JIRA)

Same problem in my script, but I found a workaround: compiling the script and running the binary works. So instead of `swift script.swift` I do `swiftc script.swift && ./script && rm script`

jckarter commented 4 years ago

What dylibs are loaded into the crashed swift process? You could look at the generated crash logs to find out. @xymus noticed that in some previous reproductions of this issue, there were Brew projects that had installed their own versions of the Swift overlays into /usr/local/Cellar, which will cause all sorts of havoc.

swift-ci commented 4 years ago

Comment by Ondrej Hanslik (JIRA)

Added a crash log.

jckarter commented 4 years ago

Thanks Ondrej! In the crash log, I see:

       0x115515000 -        0x115524ff7 +libswiftCoreGraphics.dylib (4.2.1 - 1000.11.42) <579D172D-0873-33EB-A529-742D5D70BD74> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftCoreGraphics.dylib
       0x115549000 -        0x11554cfff +libswiftIOKit.dylib (4.2.1 - 1000.11.42) <575A9454-3EB8-3314-B67B-9D0C88C03D5F> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftIOKit.dylib
       0x115554000 -        0x115557fff +libswiftCoreFoundation.dylib (4.2.1 - 1000.11.42) <801E1416-6C23-3C48-BF46-2349B9F36C9E> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftCoreFoundation.dylib
       0x11555e000 -        0x115566fff +libswiftDarwin.dylib (4.2.1 - 1000.11.42) <70D5D45B-0327-352C-BDCE-F792699DAB88> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftDarwin.dylib
       0x117350000 -        0x1174b8fff +libswiftFoundation.dylib (4.2.1 - 1000.11.42) <2252DB9E-6B26-31AD-A869-9D92FA2BE196> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftFoundation.dylib
       0x117654000 -        0x1179e6ff7 +libswiftCore.dylib (4.2.1 - 1000.11.42) <D0C86227-BFC8-30D7-93B8-9F228E0F15BA> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftCore.dylib
       0x117cba000 -        0x117cd3ff7 +libswiftDispatch.dylib (4.2.1 - 1000.11.42) <E8DBE923-0513-3FE8-8E3A-5778014FD810> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftDispatch.dylib
       0x117d12000 -        0x117d16fff +libswiftObjectiveC.dylib (4.2.1 - 1000.11.42) <A521AAA2-3B1F-38AF-BFA0-BB02A94ECA8B> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftObjectiveC.dylib
       0x117d21000 -        0x117d24fff +libswiftXPC.dylib (4.2.1 - 1000.11.42) <3EC739CE-A1AC-3199-8461-9CA872E2B9EF> /usr/local/Cellar/swiftgen/6.1.0/lib/libswiftXPC.dylib

Does the problem go away if you remove these rogue overlay dylibs?

swift-ci commented 4 years ago

Comment by Nikolaj (JIRA)

I have the same issue, and removing the dylibs from SwiftGen does fix the issue, but that breaks SwiftGen

swift-ci commented 4 years ago

Comment by Nikolaj (JIRA)

Reinstalling SwiftGen from Homebrew with "--build-from-source" and removing the dylibs from the "lib" folder seems to work

swift-ci commented 4 years ago

Comment by Michal Srutek (JIRA)

I can confirm that Tørring (JIRA User) 's solution does seem to work for me.

swift-ci commented 4 years ago

Comment by Ondrej Hanslik (JIRA)

I cannot confirm since I had to downgrade my Xcode but the script works on my second laptop where SwiftGen was never installed.

However, I am still baffled why `swift script` works differently than `swiftc script && ./script`.