Debugging a crash on Linux

I'm getting this crash on Ubuntu 22.04. I've isolated a few lines in my project that seem to cause it. Commenting out the lines prevents the crash, but the code doesn't do anything memory or concurrency related, it's mostly nested resultBuilder code. I haven't been able to reproduce the crash in a sample project. Any ideas?

Thread 0:

0  0x00007f89b130982d <unknown> in libc.so.6

Thread 1:

0  0x00007f89b127f117 <unknown> in libc.so.6

Thread 2 crashed:

0  0x00007f89b2643388 swift_retain + 56 in libswiftCore.so


Registers:

rax 0x00000000801f0fff  2149519359
rdx 0x00007f89ab499900  a0 fe 6f 94 44 56 00 00 00 00 00 00 00 00 00 00   þo·DV··········
rcx 0x0000564493243978  ff 0f 1f 80 00 00 00 00 55 48 89 e5 48 8b 05 75  ÿ·······UH·åH··u
rbx 0x0000000200000000  8589934592
rsi 0x0000564493243970  49 8b 7d 10 e9 57 f1 ff ff 0f 1f 80 00 00 00 00  I·}·éWñÿÿ·······
rdi 0x00000002801f0fff  10739453951
rbp 0x0000000000000000  0
rsp 0x00007f89ab4995a8  27 ff 23 93 44 56 00 00 00 00 00 00 02 00 00 00  'ÿ#·DV··········
 r8 0x0000000000000000  0
 r9 0x80000000ffffffff  9223372041149743103
r10 0x0000000100000000  4294967296
r11 0x6378b1b0fbd84080  7167674180657758336
r12 0x0000000000000000  0
r13 0x0000000000000000  0
r14 0x0000000000000000  0
r15 0x0000000000000000  0
rip 0x00007f89b2643388  f0 48 0f b1 31 75 f1 48 89 f8 c3 83 f8 ff 74 f7  ðH·±1uñH·ø÷øÿt÷

rflags 0x0000000000010206  PF

cs 0x0033  fs 0x0000  gs 0x0000


Images (70 omitted):

0x00007f89b11ee000–0x00007f89b13aa541 a43bfc8428df6623cd498c9c0caeb91aec9be4f9 libc.so.6       /usr/lib/x86_64-linux-gnu/libc.so.6
0x00007f89b224a000–0x00007f89b2787d68 <no build ID>                            libswiftCore.so /opt/swift-5.9/usr/lib/swift/linux/libswiftCore.so

Can you provide a very small code sample that demonstrates the issue? I'm not using x86 Linux but who knows, maybe it would also happen on my aarch64 Linux system. BTW which version of swiftc are you using?

I wasn't able yet to reproduce the issue outside of my project unfortunately.

You could try linking your code against some of the LLVM sanitizers and see if that turns up anything.

1 Like

I found the issue, it seems to be a miscompile related to result builders and structs memberwise initializers.

Changing this code

struct MyType {
    let object: MyObject
    @MyBuilder
    let value: MyValue
}

to this

struct MyType {
    let object: MyObject
    @MyBuilder
    let value: MyValue

    init(object: MyObject, @MyBuilder value: () -> MyValue) {
        self.object = object
        self.value = value
    }
}

fixes the crash.

There is a related compiler crash. For another type this crash went away after writing out the initializer:

IntToPtr source must be an integral
  %5 = inttoptr i8* %0 to %swift.bridge*, !dbg !256
IntToPtr source must be an integral
  %6 = inttoptr i8* %2 to %swift.bridge*, !dbg !256
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
1.	Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
2.	Compiling with the current language version
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x000000010554714c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x00000001080c5f2c llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000103e0710c SignalHandler(int) + 352
3  libsystem_platform.dylib 0x00000001a4dfaa24 _sigtramp + 56
4  libsystem_pthread.dylib  0x00000001a4dcbc28 pthread_kill + 288
5  libsystem_c.dylib        0x00000001a4cd9ae8 abort + 180
6  swift-frontend           0x000000010806f374 llvm::vfs::recursive_directory_iterator::increment(std::__1::error_code&) + 0
7  swift-frontend           0x00000001054f7890 llvm::report_fatal_error(llvm::Twine const&, bool) + 280
8  swift-frontend           0x00000001054f7778 llvm::report_fatal_error(llvm::Twine const&, bool) + 0
9  swift-frontend           0x00000001070da5a4 std::__1::__function::__func<llvm::CombinerHelper::matchCombineFAddFpExtFMulToFMadOrFMAAggressive(llvm::MachineInstr&, std::__1::function<void (llvm::MachineIRBuilder&)>&)::$_52, std::__1::allocator<llvm::CombinerHelper::matchCombineFAddFpExtFMulToFMadOrFMAAggressive(llvm::MachineInstr&, std::__1::function<void (llvm::MachineIRBuilder&)>&)::$_52>, void (llvm::MachineIRBuilder&)>::operator()(llvm::MachineIRBuilder&) + 0
10 swift-frontend           0x0000000107d76490 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 224
11 swift-frontend           0x0000000106ee0990 swift::performLLVMOptimizations(swift::IRGenOptions const&, llvm::Module*, llvm::TargetMachine*, llvm::raw_pwrite_stream*) + 4252
12 swift-frontend           0x0000000103a0e5bc swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, llvm::StringRef, swift::UnifiedStatsReporter*) + 2736
13 swift-frontend           0x0000000108078c74 generateCode(swift::CompilerInstance&, llvm::StringRef, llvm::Module*, llvm::GlobalVariable*) + 252
14 swift-frontend           0x000000010795b618 performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>>, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 1708
15 swift-frontend           0x000000010807d91c performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1748
16 swift-frontend           0x0000000108081474 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 4568
17 swift-frontend           0x00000001079c74e0 swift::mainEntry(int, char const**) + 4116
18 dyld                     0x00000001a4a73f28 start + 2236

Have you found a corresponding issue on github.com/apple/swift/issue, or would you mind filing one a self-contained snippet and repro steps otherwise?

I filed a bug here: Compiler and runtime crashes when combining result builders with memberwise initializer · Issue #69272 · apple/swift · GitHub

If I'm able to reproduce the issue in a snippet, I'll attach it to the ticket.

The compiler crash can be reproduced easily:

@resultBuilder
enum MyBuilder {
    static func buildBlock(_ components: String...) -> [String] {
        components
    }
}

struct MyType {
    @MyBuilder
    let first: [String]
    @MyBuilder
    let second: [String]
}
1 Like