swiftlang / swift

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

[SR-11039] Interpreter crash with some types and Codable #53428

Open swift-ci opened 5 years ago

swift-ci commented 5 years ago
Previous ID SR-11039
Radar rdar://problem/49639321
Original Reporter jeff.huff (JIRA User)
Type Bug
Environment $ swift --version Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5) Target: x86_64-apple-darwin18.6.0
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, RunTimeCrash | |Assignee | None | |Priority | Medium | md5: c4e62080ddb495e4319ea2ab65f82714

Issue Description:

I had a more complicated set of classes, but finally narrowed it down to a type of Date? and trying to use JSONEncoder to encode a class with one property.

$ cat test-crash.swift 
import Foundation

public class TestClass: Codable {
    var startDate: Date?
}

let test = TestClass()

if let jsonData = try? JSONEncoder().encode(test), let json = String(data: jsonData, encoding: .utf8) {
    print(json)
}

$ swift test-crash.swift 
Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret test-crash.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name main 
0  swift                    0x0000000114237ee3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x00000001142376bc SignalHandler(int) + 348
2  libsystem_platform.dylib 0x00007fff5fc08b5d _sigtramp + 29
3  libsystem_platform.dylib 0x00007ffedfbfa0d0 _sigtramp + 2147423632
4  libobjc.A.dylib          0x00007fff5e25362f initializeAndMaybeRelock(objc_class*, objc_object*, mutex_tt<false>&, bool) + 187
5  libobjc.A.dylib          0x00007fff5e242690 lookUpImpOrForward + 228
6  libobjc.A.dylib          0x00007fff5e242114 _objc_msgSend_uncached + 68
7  libswiftFoundation.dylib 0x00007fff5f69490c $s10Foundation21__CharacterSetStorage33_45BFD3D387700B862E3A7353B97EF7EDLLCMaTm + 28
8  libswiftFoundation.dylib 0x00007fff5f660b57 $s10Foundation11JSONEncoderCMa + 23
9  libswiftFoundation.dylib 0x000000011990e08a $s10Foundation11JSONEncoderCMa + 3123369290
10 swift                    0x0000000110acf38d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 365
11 swift                    0x0000000110ad5762 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
12 swift                    0x000000011009e831 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 58929
13 swift                    0x000000011008c92e swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
14 swift                    0x000000011002a9ce main + 1246
15 libdyld.dylib            0x00007fff5fa1d3d5 start + 1
Segmentation fault: 11

If you compile it with swiftc and run it, it works normally.

If you change the type of startDate to String? then it works normally running it with swift.

belkadan commented 5 years ago

Still crashes in master with

Assertion failed: (dependentIvars->Count == numFields), function initObjCClass, file /Volumes/Data/swift-public/swift/stdlib/public/runtime/Metadata.cpp, line 2445.

@mikeash, we have a Radar for this already, right?

mikeash commented 5 years ago

Yes, it's rdar://problem/49639321. I added it to this issue.