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-10470] Segmentation fault in Swift 5 compiler (MACH_O_TYPE staticlib) #52870

Open
swift-ci opened this issue Apr 12, 2019 · 8 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself JIT

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10470
Radar None
Original Reporter freak4pc (JIRA User)
Type Bug
Environment

macOS 10.4.4 (or macOS 10.10 on Travis CI)
Xcode 10.2, Swift 5 compiler

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, JIT
Assignee None
Priority Medium

md5: dc85e87a4ac8578d2653604c9563ff15

Issue Description:

In RxSwift's CI, there is a script that validates the Playground swift code is correct.

It does this by building RxSwift with Release configuration and then concatenating all playground pages into a single swift file and trying to build it using the following command.

swift -v -D NOT_IN_PLAYGROUND -target x86_64-apple-macosx10.10 -F /Users/shai.mishali/Work/OSS/RxSwift/build/Build/Products/Release /Users/shai.mishali/Work/OSS/RxSwift/build/Build/Products/Release/all-playground-pages.swift

We tried switching from a dynamic library to a static library by changing `MACH_O_TYPE` from `mh_dylib` to `staticlib`, which works fine for simply building the project, but fails in this specific case, breaking our CI.

When using the above command with a `staticlib` MACH_O_TYPE, here's the error I'm getting:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret build/Build/Products/Release/all-playground-pages.swift -target x86_64-apple-macosx10.10 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -F build/Build/Products/Release -D NOT_IN_PLAYGROUND -color-diagnostics -module-name main

Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret build/Build/Products/Release/all-playground-pages.swift -target x86_64-apple-macosx10.10 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -F build/Build/Products/Release -D NOT_IN_PLAYGROUND -color-diagnostics -module-name main
0  swift                    0x000000011247cee3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x000000011247c6bc SignalHandler(int) + 348
2  libsystem_platform.dylib 0x00007fff75252b5d _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffee19b4e18 _sigtramp + 1819681496
4  libsystem_platform.dylib 0x0000000117f5101a _sigtramp + 2731533530
5  swift                    0x000000010ed1419d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 365
6  swift                    0x000000010ed1a572 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*) + 1090
7  swift                    0x000000010e2e35d1 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 58913
8  swift                    0x000000010e2d16de swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
9  swift                    0x000000010e26f7be main + 1246
10 libdyld.dylib            0x00007fff7506d3d5 start + 1
11 libdyld.dylib            0x0000000000000010 start + 2331585596
./scripts/validate-playgrounds.sh: line 8: 86001 Segmentation fault: 11  swift -v -D NOT_IN_PLAYGROUND -target x86_64-apple-macosx10.10 -F ${BUILD_DIRECTORY}/Build/Products/${configuration} ${PAGES_PATH}

The easiest way to reproduce this is cloning RxSwift and using the `staticlib` branch, then run `./scripts/validate-playgrounds.sh`: https://github.com/ReactiveX/RxSwift/tree/staticlib

@swift-ci
Copy link
Collaborator Author

Comment by Shai Mishali (JIRA)

P.S. the `NOT_IN_PLAYGROUND` flag is unrelated, it's something internal:
https://github.com/ReactiveX/RxSwift/blob/master/Rx.playground/Sources/SupportCode.swift#L36

@belkadan
Copy link
Contributor

I think this is just "you can't import static libraries into the JIT because dlopen doesn't know how to load them". If you use swiftc instead of swift it'll probably work…and moreover it won't actually run the playground, which depending on the contents of the playground might be a good thing. (Imagine a playground that shows how to modify on-disk content!)

@swift-ci
Copy link
Collaborator Author

Comment by Shai Mishali (JIRA)

I'm not knowledgable enough with JIT or swiftc, but this isn't actually using Playground. What the script does is just merge all Swift files into a single Swift file and compile it as a regular Swift script, not a Playground. The segmentation fault is clearly not the expected outcome here 🙂

Even after disabling this failing script, we're failing with what seems to be a known issue in Swift 5 (47598583):

> Linking against a static Swift library might create a binary with missing type metadata because the object files that define the metadata inside the static archive are mistakenly considered unused.
>
> This can manifest as a Swift runtime error with a message such as: “failed to demangle superclass of MyClass from mangled name ‘<mangled name>’”.
>
> Workaround: If you can rebuild the static library, try building it with whole module optimization enabled. Otherwise, add -all_load to the linker flags in the client binary to ensure all object files are linked into it.

@belkadan
Copy link
Contributor

It's not compiling the file; it's interpreting it. That's the difference between swift and swiftc.

@swift-ci
Copy link
Collaborator Author

Comment by Shai Mishali (JIRA)

And that's something not possible with a Static Library ? We've never had an issue with Dynamic Libraries here.
In any way, I imagine there should be an actual error to work with here, in an optimal world? 🙂

Thanks for your patience and for understanding to me non-compiler brain 😃

@belkadan
Copy link
Contributor

A static library is basically a ball of object files that haven't been turned into a proper library yet by the linker, and dlopen (the underlying APIs the interpreter uses to load libraries) only understands linker outputs. In theory the compiler could do some kind of on-the-fly linking I suppose, but no other interpreter or REPL that I know works like that, so it wouldn't be a priority.

You're right that a diagnostic would be nice; maybe if the interpreter fails to find a dynamic library it can look for a static one just to tell the developer that it's not supported.

@swift-ci
Copy link
Collaborator Author

Comment by Shai Mishali (JIRA)

Is there any way to make that pass though? e.g. using swiftc instead of swift and linking the static libraries ?

@belkadan
Copy link
Contributor

I'm not sure what you mean. swiftc and swift fundamentally do different things: one is a compiler and one is an interpreter. The fact that they share some infrastructure and command-line flags doesn't really change that.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 JIT
Projects
None yet
Development

No branches or pull requests

2 participants