LLVM crashes in Xcode 11.2 with Build Libraries for Distribution

I'm managing releases for a large open-source Swift project for Apple platforms (AudioKit), and we're trying to build a xcframework for it.

The first step in attempting to do this - simply turning on the Build Libraries for Distribution setting - led to some very strange behavior, namely segmentation faults from LLVM when run in xcodebuild.

Stack dump:
0.	Running pass 'Function Pass Manager' on module '/Users/megastep/src/ak/AudioKit/AudioKit/iOS/build/AudioKit For iOS.build/Release-iphonesimulator/AudioKit.build/Objects-normal/x86_64/AKReverb2.o'.
1.	Running pass 'X86 Assembly Printer' on function '@"$s8AudioKit11AKMIDIEventV15internalPacketsSaySays5UInt8VGGvg"'
0  swift                    0x000000010cd21a13 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x000000010cd211e6 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff657e8b1d _sigtramp + 29
3  libsystem_platform.dylib 0x000000010e372080 _sigtramp + 2830669184
4  swift                    0x000000010cac5740 (anonymous namespace)::MCMachOStreamer::EmitInstToData(llvm::MCInst const&, llvm::MCSubtargetInfo const&) + 192
5  swift                    0x000000010cacce86 llvm::MCObjectStreamer::EmitInstruction(llvm::MCInst const&, llvm::MCSubtargetInfo const&, bool) + 710
6  swift                    0x0000000109c3d623 llvm::X86AsmPrinter::EmitInstruction(llvm::MachineInstr const*) + 21683
7  swift                    0x000000010a0a4ecc llvm::AsmPrinter::EmitFunctionBody() + 5740
8  swift                    0x00000001099eea08 llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) + 296
9  swift                    0x000000010ade6a44 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 308
10 swift                    0x000000010cbfccf6 llvm::FPPassManager::runOnFunction(llvm::Function&) + 1590
11 swift                    0x000000010cbfc4c3 llvm::FPPassManager::runOnModule(llvm::Module&) + 67
12 swift                    0x000000010cc09c27 llvm::legacy::PassManager::run(llvm::Module&) + 1655
13 swift                    0x0000000108b7d23d swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 7549
14 swift                    0x0000000108b81190 (anonymous namespace)::LLVMCodeGenThreads::Thread::run() + 208
15 swift                    0x0000000108b81039 (anonymous namespace)::LLVMCodeGenThreads::runThread(void*) + 9
16 libsystem_pthread.dylib  0x00007fff657f3d36 _pthread_start + 125
17 libsystem_pthread.dylib  0x00007fff657f058f thread_start + 15
error: Segmentation fault: 11 (in target 'AudioKit' from project 'AudioKit For iOS')

I should point out that the codebase is a large mix of mostly Swift with also a lot of Objective-C, C and C++ code. So maybe this has something to do with this crash, but in any case segfaults should never happen in a compiler.

Another weird thing is that when building the project in Xcode this crash doesn't seem to happen - mostly command line builds seem to be affected (without overriding any settings).

Does anybody have any insights on this kind of crash?

Still happening with Xcode 11.3, though it seems to be only happening when compiling the project using the Release configuration, so I'm guessing some particular combination of compiler flags is causing this crash. This seems to mostly happen when building for x86_64 which is problematic for Mac and simulator builds.

It happens early enough in the build cycle to prevent proper build artifacts from being generated and this in turns causes other odd errors like missing Swift header files, etc.

Could you file a bug report on bugs.swift.org with reproduction steps? (Or have you done that already?). You could also try using one of the downloadable toolchains from swift.org to check if this triggers an assertion failure earlier. That would be useful information to have.

Thanks for the helpful suggestions. I have not yet submitted an official bug report, however I just tried several of the downloadable toolchains, and it seems to no longer occur with the 5.2 one as well as the latest development snapshot.

It is however still happening with the Swift 5.1 toolchain and it gives a little more information then (assertion failure).

Assertion failed: (isReg() && "This is not a register operand!"), function getReg, file /Users/buildnode/jenkins/workspace/oss-swift-5.1-package-osx/llvm/include/llvm/MC/MCInst.h, line 66.
Stack dump:
0.	Running pass 'Function Pass Manager' on module '/Users/megastep/Library/Developer/Xcode/DerivedData/AudioKit_For_iOS-bjgbdowpgngrljgglytxnqbrkwao/Build/Intermediates.noindex/AudioKit For iOS.build/Release-iphonesimulator/AudioKit.build/Objects-normal/x86_64/AKReverb2.o'.
1.	Running pass 'X86 Assembly Printer' on function '@"$s8AudioKit11AKMIDIEventV15internalPacketsSaySays5UInt8VGGvg"'
0  swift                    0x000000010cdccfc5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010cdcc2b5 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x000000010cdcd5a8 SignalHandler(int) + 264
3  libsystem_platform.dylib 0x00007fff6e41b42d _sigtramp + 29
4  libdyld.dylib            0x00007fff6e221345 dyldGlobalLockRelease() + 0
5  libsystem_c.dylib        0x00007fff6e2f0a1c abort + 120
6  libsystem_c.dylib        0x00007fff6e2efcd6 err + 0
7  swift                    0x000000010d2f9b91 (anonymous namespace)::X86MCCodeEmitter::encodeInstruction(llvm::MCInst const&, llvm::raw_ostream&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const (.cold.104) + 33
8  swift                    0x000000010a5d55bd (anonymous namespace)::X86MCCodeEmitter::encodeInstruction(llvm::MCInst const&, llvm::raw_ostream&, llvm::SmallVectorImpl<llvm::MCFixup>&, llvm::MCSubtargetInfo const&) const + 15837
9  swift                    0x000000010cb6f315 (anonymous namespace)::MCMachOStreamer::EmitInstToData(llvm::MCInst const&, llvm::MCSubtargetInfo const&) + 213
10 swift                    0x000000010cb74e91 llvm::MCObjectStreamer::EmitInstruction(llvm::MCInst const&, llvm::MCSubtargetInfo const&, bool) + 49
11 swift                    0x000000010a5695d1 llvm::X86AsmPrinter::EmitInstruction(llvm::MachineInstr const*) + 22081
12 swift                    0x000000010ac4fe51 llvm::AsmPrinter::EmitFunctionBody() + 3393
13 swift                    0x000000010a389b68 llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) + 296
14 swift                    0x000000010b55908a llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 330
15 swift                    0x000000010cc9c043 llvm::FPPassManager::runOnFunction(llvm::Function&) + 947
16 swift                    0x000000010cc9c2f8 llvm::FPPassManager::runOnModule(llvm::Module&) + 72
17 swift                    0x000000010cc9c70e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 798
18 swift                    0x00000001096c9641 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 2417
19 swift                    0x00000001096cf254 (anonymous namespace)::LLVMCodeGenThreads::Thread::run() + 516
20 swift                    0x00000001096cf049 (anonymous namespace)::LLVMCodeGenThreads::runThread(void*) + 9
21 libsystem_pthread.dylib  0x00007fff6e426e65 _pthread_start + 148
22 libsystem_pthread.dylib  0x00007fff6e42283b thread_start + 15
error: Abort trap: 6 (in target 'AudioKit' from project 'AudioKit For iOS')

It's good that this appears to be fixed in 5.2 but I don't know what the timeline is for this to ship in Xcode?

EDIT: I have also narrowed down the Swift method in the code that causes this crash, since it's always the same stack trace. Removing that method allows compilation to complete as expected with the Swift 5.1 toolchain. Here is its code:

public var internalPackets: [[MIDIByte]] {
    var splitData = [[MIDIByte]]()
    let byteLimit = Int(data.count / 256)
    for i in 0...byteLimit {
        let arrayStart = i * 256
        let arrayEnd: Int = min(Int(arrayStart + 256), Int(data.count))
        let tempData = Array(data[arrayStart..<arrayEnd])
        splitData.append(tempData)
    }
    return splitData
}
1 Like

Thanks for digging deeper! I've created a bug report so we can try to reduce it and add a regression test for it. [SR-12005] Add a smaller test case for AudioKit crash in 5.1 · Issue #54440 · apple/swift · GitHub

It's good that this appears to be fixed in 5.2 but I don't know what the timeline is for this to ship in Xcode?

Sorry, I don't know the answer to that.

Thanks. I think I've narrowed the crash down to the line with: Array(data[arrayStart..<arrayEnd]); so I think something is broken in the way arrays of arrays are handled in this case. MIDIByte is basically an alias for UInt8 so this is handling a pretty basic type.

1 Like