Crashes in protocol witness for Decodable.init(from:)

Some users of my app report that it crashes on launch. In Xcode (13) Organizer I see a lot of crashes titled

Protocol witness for Decodable.init(from:) in conformance Library.State + 20

Library.State is a simple struct with two fields that conforms to Codable (I don't override its methods). Is it a bug on my side or in JSONDecoder?

Here is a sample crash report
Hardware Model:      iPhone12,3
Process:             App [10894]
Path:                /private/var/containers/Bundle/Application/2E147D70-2F05-46B0-BD4D-608F3DA7B7FC/App.app/App
Identifier:          Developer.App
Version:             509 (3)
AppStoreTools:       12E506
AppVariant:          1:iPhone12,3:14
Beta:                YES
Code Type:           ARM-64 (Native)
Role:                unknown
Parent Process:      launchd [1]
Coalition:           Developer.App [2507]

Date/Time:           2021-08-01 00:53:51.8245 +0200
Launch Time:         2021-08-01 00:53:45.2800 +0200
OS Version:          iPhone OS 15.0 (19A5307g)
Release Type:        Beta
Baseband Version:    2.50.05
Report Version:      104

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0


Kernel Triage:
VM - pmap_enter failed with resource shortage
VM - pmap_enter failed with resource shortage
VM - pmap_enter failed with resource shortage
VM - pmap_enter failed with resource shortage
VM - pmap_enter failed with resource shortage


Thread 0 name:
Thread 0 Crashed:
0   libswiftCore.dylib            	0x00000001892029c8 swift::metadataimpl::ValueWitnesses<swift::metadataimpl::ObjCRetainableBox>::destroy(swift::OpaqueValue*, swift::TargetMetadata<swift::InProcess> const*) + 0 (MetadataImpl.h:146)
1   libswiftFoundation.dylib      	0x0000000188ba1df8 _JSONUnkeyedDecodingContainer.decode<A>(_:) + 2276 (JSONEncoder.swift:1990)
2   libswiftFoundation.dylib      	0x0000000188ba3778 protocol witness for UnkeyedDecodingContainer.decode<A>(_:) in conformance _JSONUnkeyedDecodingContainer + 16 (<compiler-generated>:0)
3   libswiftCore.dylib            	0x0000000188eefbc4 Dictionary<>.init(from:) + 4116 (Codable.swift:5622)
4   libswiftCore.dylib            	0x0000000188ef0454 protocol witness for Decodable.init(from:) in conformance <> [A : B] + 40 (<compiler-generated>:0)
5   libswiftCore.dylib            	0x00000001891c33d4 dispatch thunk of Decodable.init(from:) + 32
6   libswiftFoundation.dylib      	0x0000000188b7de88 __JSONDecoder.unbox_(_:as:) + 1268 (JSONEncoder.swift:2546)
7   libswiftFoundation.dylib      	0x0000000188b7ab34 _JSONKeyedDecodingContainer.decode<A>(_:forKey:) + 848 (JSONEncoder.swift:2521)
8   libswiftFoundation.dylib      	0x0000000188b8453c protocol witness for KeyedDecodingContainerProtocol.decode<A>(_:forKey:) in conformance _JSONKeyedDecodingContainer<A> + 56
9   libswiftFoundation.dylib      	0x0000000188b823d4 protocol witness for KeyedDecodingContainerProtocol.decode<A>(_:forKey:) in conformance _JSONKeyedDecodingContainer<A> + 36
10  libswiftCore.dylib            	0x0000000188edc21c _KeyedDecodingContainerBox.decode<A, B>(_:forKey:) + 520 (Codable.swift:4324)
11  libswiftCore.dylib            	0x0000000188ecc5ac KeyedDecodingContainer.decode<A>(_:forKey:) + 68 (Codable.swift:1759)
12  App                    		0x0000000100a54af8 specialized Library.State.init(from:) + 256 (<compiler-generated>:0)
13  App                    		0x0000000100a50334 Library.State.init(from:) + 4 (<compiler-generated>:0)
14  App                    		0x0000000100a50334 protocol witness for Decodable.init(from:) in conformance Library.State + 20
15  libswiftCore.dylib            	0x00000001891c33d4 dispatch thunk of Decodable.init(from:) + 32
16  libswiftFoundation.dylib      	0x0000000188b7de88 __JSONDecoder.unbox_(_:as:) + 1268 (JSONEncoder.swift:2546)
17  libswiftFoundation.dylib      	0x0000000188b697f8 JSONDecoder.decode<A>(_:from:) + 1332 (JSONEncoder.swift:2521)
18  libswiftFoundation.dylib      	0x0000000188ba2948 dispatch thunk of JSONDecoder.decode<A>(_:from:) + 56
19  App                    		0x0000000100a7edd0 0x100a3c000 + 273872
20  SwiftUI                       	0x000000018bfbc418 static App.main() + 112 (App.swift:114)
21  App                    		0x0000000100a4869c 0x100a3c000 + 50844
22  dyld                          	0x0000000100b719e4 start + 520 (dyldMain.cpp:876)

Thread 1:
0   libsystem_pthread.dylib       	0x00000001f3ddfdf4 0x1f3ddf000 + 3572


Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x000000016f3c2ab0   x1: 0x00000001dea5b680   x2: 0x00000001dea59fe0   x3: 0x00000001892029c8
    x4: 0x000000016f3c2958   x5: 0x000000016f3c2950   x6: 0x0000000000000001   x7: 0x0000000000000000
    x8: 0x0000000000000001   x9: 0x0000000189201eec  x10: 0x0000000001000000  x11: 0x000f000282bd0100
   x12: 0x0000000000000007  x13: 0x0000000282bd0160  x14: 0x00000000e6cfe800  x15: 0x00000001dd91c408
   x16: 0x04f80001dea59fe0  x17: 0x00000001dea59fd8  x18: 0x0000000000000000  x19: 0x000000016f3c29d0
   x20: 0x0000000000000000  x21: 0x0000000000000000  x22: 0x00000001dea54e78  x23: 0x0000000000000003
   x24: 0x00000001dea4fc98  x25: 0x00000001dea54e78  x26: 0x000000016f3c29a0  x27: 0x00000001dea59b08
   x28: 0x0000000000000000   fp: 0x000000016f3c2bc0   lr: 0x0000000188ba1df8
    sp: 0x000000016f3c2990   pc: 0x00000001892029c8 cpsr: 0x60000000
   esr: 0x56000080  Address size fault


Binary Images:
0x100a3c000 - 0x100a97fff App arm64  <0412067b611237958e315d06d3fb80c9> /private/var/containers/Bundle/Application/2E147D70-2F05-46B0-BD4D-608F3DA7B7FC/App.app/App
0x100b58000 - 0x100baffff dyld arm64e  <307ca1e7c12f31918ff5dbe126e7126a> /usr/lib/dyld
0x188b5d000 - 0x188e76fff libswiftFoundation.dylib arm64e  <c8327e88a8d43d97be98616e89464fc2> /usr/lib/swift/libswiftFoundation.dylib
0x188e77000 - 0x1892c8fff libswiftCore.dylib arm64e  <fe7d9e7294d53dc9877859b76410590a> /usr/lib/swift/libswiftCore.dylib
0x18be67000 - 0x18ce4afff SwiftUI arm64e  <a25f98b4f2bc331ab68a42fb2ed42ca5> /System/Library/Frameworks/SwiftUI.framework/SwiftUI
0x1f3ddf000 - 0x1f3deafff libsystem_pthread.dylib arm64e  <db2e203d50373d1d9c6d31955d713197> /usr/lib/system/libsystem_pthread.dylib

EOF

Well, maybe Library.State is not so simple for the compiler/JSONDecoder?

enum Library {
  typealias History = [UInt64]
  typealias Snapshot = [UInt64: Item.Snapshot]

  struct State: Codable, Equatable {
    let snapshot: Snapshot
    let history: History
  }
}

extension Item {
  struct Snapshot: Equatable, Codable {
    let pc: Int
    let lp: TimeInterval?
  }
}

would be super handy to see the corresponding json that causes this crash.

Unfortunately, I can't reproduce this on my devices. All I have is the code and crash reports. Also, JSONDecoder().decode() call is used with try? so JSON can be any and still shouldn't crash, isn't it?

I have also run into this kind of crash. But I haven't been able to get a reliable test case to report it in a meaningful way.

In my experience decoding the same object will work the majority of the time and then sometimes it just fails. The only difference I've been able to tell is that the functions on the stack are different. And this is all in Swift/Combine/SwiftUI land not our app directly dropping to unsafe operations.

This looks like a Swift runtime crash, so you can report it to bugs.swift.org without a reproducer. These sorts of crashes happen semi-regularly and there isn't much to do about them except report and hope they get prioritized and investigated. You'll want to include the version of Xcode / Swift you used to compile the crashing version as well.

Terms of Service

Privacy Policy

Cookie Policy