Compiler&runtime crash with [String: AnyObject?] type

This code crashes at runtime:

print(["a": 42] as [String: AnyObject?])
Stack trace
Stack dump:
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.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
2.	While running user code "test.swift"
0  swift                    0x000000010b5ac4ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x000000010b5abcc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff6eb905fd _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 2437347872
4  libswiftCore.dylib       0x00007fff6e25c478 outlined init with copy of Any + 24
5  libswiftCore.dylib       0x00007fff6e0c5398 Swift.Optional.debugDescription.getter : Swift.String + 296
6  libswiftCore.dylib       0x00007fff6e04ebb2 Swift._debugPrint_unlocked<A, B where B: Swift.TextOutputStream>(A, inout B) -> () + 306
7  libswiftCore.dylib       0x00007fff6e0cd035 generic specialization <Swift.String> of Swift._debugPrint<A where A: Swift.TextOutputStream>(_: [Any], separator: Swift.String, terminator: Swift.String, to: inout A) -> () + 197
8  libswiftCore.dylib       0x00007fff6e039427 Swift.Dictionary.description.getter : Swift.String + 1143
9  libswiftCore.dylib       0x00007fff6e04e28c Swift._print_unlocked<A, B where B: Swift.TextOutputStream>(A, inout B) -> () + 684
10 libswiftCore.dylib       0x00007fff6e0ce462 merged generic specialization <Swift._Stdout> of Swift._print<A where A: Swift.TextOutputStream>(_: [Any], separator: Swift.String, terminator: Swift.String, to: inout A) -> () + 258
11 libswiftCore.dylib       0x00007fff6e0ccf29 merged Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () + 185
12 libswiftCore.dylib       0x00007fff6e0cc67f Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () + 31
13 libswiftCore.dylib       0x000000010daf3110 Swift.print(_: Any..., separator: Swift.String, terminator: Swift.String) -> () + 2678221488
14 swift                    0x000000010732f48a llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 458
15 swift                    0x00000001073364fb 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
16 swift                    0x000000010730b5ba 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
17 swift                    0x00000001072fff75 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 55813
18 swift                    0x0000000107275f53 main + 1283
19 libdyld.dylib            0x00007fff6e997cc9 start + 1
20 libdyld.dylib            0x000000000000000b start + 2439414595
fish: 'swift test.swift' terminated by signal SIGSEGV (Address boundary error)

Changing the code up a little crashes the compiler instead:

print(["a": nil, "b": 1] as [String: AnyObject?])
produces this trace
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -target x86_64-apple-darwin19.4.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name test -o /var/folders/kn/dv65kcr95js91rgy1vlf6zrm0000gn/T/test-4c142f.o
1.	Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
2.	While emitting IR SIL function "@main".
0  swift                    0x0000000104c734ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x0000000104c72cc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff6eb905fd _sigtramp + 29
3  libsystem_platform.dylib 0x00007fc73e200000 _sigtramp + 3479632416
4  swift                    0x0000000100a35e89 llvm::GetElementPtrInst::getGEPReturnType(llvm::Type*, llvm::Value*, llvm::ArrayRef<llvm::Value*>) + 121
5  swift                    0x0000000100a35c01 llvm::GetElementPtrInst::Create(llvm::Type*, llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine const&, llvm::Instruction*) + 161
6  swift                    0x0000000100a35a58 llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>::CreateConstInBoundsGEP2_32(llvm::Type*, llvm::Value*, unsigned int, unsigned int, llvm::Twine const&) + 504
7  swift                    0x0000000100aca9d7 (anonymous namespace)::PayloadEnumImplStrategyBase::projectExtraTagBits(swift::irgen::IRGenFunction&, swift::irgen::Address) const + 87
8  swift                    0x0000000100ac6d06 (anonymous namespace)::PayloadEnumImplStrategyBase::initialize(swift::irgen::IRGenFunction&, swift::irgen::Explosion&, swift::irgen::Address, bool) const + 118
9  swift                    0x0000000100c06355 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 35301
10 swift                    0x0000000100bfa433 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8835
11 swift                    0x0000000100aadb02 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1410
12 swift                    0x0000000100bd8a29 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1097
13 swift                    0x00000001009cfe3f 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*) + 4255
14 swift                    0x00000001009c529a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48426
15 swift                    0x000000010093cf53 main + 1283
16 libdyld.dylib            0x00007fff6e997cc9 start + 1
17 libdyld.dylib            0x0000000000000010 start + 2439414600
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal 11 (use -v to see invocation)
Changing the nil to a string literal produces yet another stack trace
Stack dump:
0.	Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test.swift -target x86_64-apple-darwin19.4.0 -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -color-diagnostics -module-name test -o /var/folders/kn/dv65kcr95js91rgy1vlf6zrm0000gn/T/test-e8149f.o
1.	Apple Swift version 5.2.2 (swiftlang-1103.0.32.6 clang-1103.0.32.51)
2.	While emitting IR SIL function "@main".
0  swift                    0x00000001055ef4ea PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x00000001055eecc0 SignalHandler(int) + 352
2  libsystem_platform.dylib 0x00007fff6eb905fd _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffeee95fb00 _sigtramp + 2145187104
4  swift                    0x000000010157d942 swift::SILInstructionVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::SILInstruction*) + 16338
5  swift                    0x0000000101576433 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8835
6  swift                    0x0000000101429b02 swift::irgen::IRGenerator::emitGlobalTopLevel() + 1410
7  swift                    0x0000000101554a29 performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::StringRef, swift::PrimarySpecificPaths const&, llvm::LLVMContext&, swift::SourceFile*, llvm::GlobalVariable**) + 1097
8  swift                    0x000000010134be3f 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*) + 4255
9  swift                    0x000000010134129a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 48426
10 swift                    0x00000001012b8f53 main + 1283
11 libdyld.dylib            0x00007fff6e997cc9 start + 1
12 libdyld.dylib            0x0000000000000010 start + 2439414600
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal 11 (use -v to see invocation)

Does anyone know whether we already have a bug for this? If not, I'll file one later.

Interesting. This post looks like it may shed some light into what may be happening:

Int is bridged to NSNumber, so converting to an object type works via the bridge.

So casting 42 as NSNumber avoids the runtime crash. Probably still bug worthy though.

print(["a": 42 as NSNumber] as [String: AnyObject?])

Crashes, whether in the Swift compiler or the Swift runtime, are always worthy of reports. Reports are one of the few ways we can impact Apple's development priorities. Without reports things may not get fixed.

2 Likes

Filed as SR-12792

Terms of Service

Privacy Policy

Cookie Policy