Two bugs in my crash reporter, including one that crashed the app
New in Purrchiver 1.1. If you don't see it, update Purrchiver from the App Store.
The code Purrchiver uses to email me crash reports could itself crash the app, given a long enough diagnostic log with a Japanese file name in the wrong place. And the signature that was supposed to tell me two crash emails were the same bug never matched across builds.
Both live in the network-free crash reporter
I wrote about in June: no SDK, no server, just macOS’s own .ips files, a
scrubber, and an email you choose to send. Both bugs came from working on the
wrong representation of the data. The signature parsed text the code had
rendered itself. The trimmer measured raw bytes when the limit applied to
encoded characters.
A signature that didn’t mean anything
Each crash email carries a short signature: an 8-character hash of the top frame that isn’t in a system library. It’s there so I can tell at a glance that five emails are the same bug. It wasn’t doing that.
The parser read the crashed thread’s top eight frames out of the .ips JSON
and rendered each one as a line of text:
0 Purrchiver closure #1 in foo() + 120Then the signature code took that rendered text and split it back apart on
spaces. It expected the layout of a classic crash log, frame# image address symbol, and threw away the first three tokens to get to the symbol. But the
rendered line has no address column. So it threw away part of the symbol
instead:
| Rendered frame | What got hashed |
|---|---|
0 Purrchiver closure #1 in foo() + 120 | #1 in foo() + 120 |
0 Purrchiver foo() + 120 | + 120 |
Four things were wrong:
- The offset was in the hash. The
+ 120is how far into the function the crash happened. Edit that function, change what gets inlined into it, or just update the compiler, and the offset can move. The same bug got a new signature in every build. - One-word symbols collided. For
foo() + 120, only+ 120was hashed, so two unrelated crashes at the same offset in different functions looked like one bug. - Spaces in image names shifted everything. An image called
My App Helperadds two tokens, so even more of the symbol was thrown away. - System frames weren’t skipped. The filter looked for
/System/in the line, but the rendered line has the image’s name, not its path. AppKit and Foundation frames slipped through and could become the signature.
The fix: keep the structure
The .ips file already has everything as structured data: each frame’s symbol,
its offset, and an index into a list of loaded images with their names and
paths. Rendering it to text and parsing it back was throwing that away.
Now the parser keeps each frame as a small struct (image name, image path, symbol) and computes the signature from that:
struct Frame {
let image: String
let imagePath: String?
let symbol: String?
var isSystem: Bool {
if let imagePath {
return imagePath.hasPrefix("/System/") || imagePath.hasPrefix("/usr/lib/")
}
// No path: fall back to the few names that always mean the system.
let name = image.lowercased()
return name.hasPrefix("libsystem") || name.hasPrefix("libdyld") || name == "corefoundation"
}
}
func signature(for frames: [Frame]) -> String {
guard let frame = frames.first(where: { !$0.isSystem && !($0.symbol ?? "").isEmpty }),
let symbol = frame.symbol else { return "unknown" }
return shortHash(of: frame.image + "\u{0}" + symbol)
}Image plus symbol, no offset. The zero byte between them stops an image and symbol that happen to run together into the same string from colliding. System frames are recognized by the image’s path when the image list has one, and by a short list of names when it doesn’t. As before, the frames are scrubbed before they’re hashed, so nothing about you feeds the signature.
Image plus symbol isn’t permanent. Rename the function and the signature
changes, and adding a closure earlier in a function can turn closure #1 into
closure #2. But it survives the ordinary rebuilds that used to split every
bug, and that’s what grouping needs.
Update, October 23: that holds when the frames have symbols, and the App Store build is stripped, so the app’s own frames arrive without one. Those crashes all got the signature “unknown”. Now a frame with no symbol is shown as its offset in the image, with the image’s build UUID and the app version below the trace, so I can resolve it against the archived debug symbols. The signature falls back to the image, its build UUID and that offset. That groups repeat crashes within one release, not across releases.
The email still shows offsets. They’re useful when I’m reading one crash. They just don’t belong in the thing that groups many.
The tests parse whole .ips fixtures end to end. One crashes in the same
function at offsets 224 and 1,032, expects the same signature both times, and
checks the trace still shows each offset. Another puts an AppKit frame on top
and expects it to be skipped.
The crash reporter that crashed the app
In June I wrote that the email body is capped at about 60,000 bytes before
encoding. That sentence described the bug. The limit exists because some mail
clients quietly truncate a long mailto: URL, and the URL is percent-encoded.
A crash summary never gets near 60,000 bytes. The diagnostic-log export in
Settings uses the same builder, and a log can.
It measured the wrong length. The code compared the raw UTF-8 length against a cap meant for the encoded body. Percent-encoding turns every byte outside the safe set into three characters. A body of accented text that looked like 50 KB could be 150 KB once encoded, two and a half times the cap. The worst case was three times.
It cut in the middle of a character. To find the cut point, the code counted 60,000 UTF-8 bytes in and converted that byte position back to a position in the string:
let cut = body.utf8.index(body.utf8.startIndex, offsetBy: maxBytes)
let prefix = body[..<cut.samePosition(in: body)!]samePosition(in:) returns nil when the byte position falls inside a
character. An é is two bytes, each character of 日本語 is three, and a 🐈
is four, so the 60,000th byte can easily land in the middle of one. The !
turned that into a crash. A diagnostic log full of Japanese file names, long
enough to need trimming, could take the app down at the moment you were trying
to send me information about a problem.
The fix: spend the budget a character at a time
The new version measures what will actually be sent, and only ever stops between characters:
var used = 0
var cut = body.startIndex
for index in body.indices { // whole characters, never bytes
let cost = encodedLength(of: body[index])
if used + cost > budget { break }
used += cost
cut = body.index(after: index)
}encodedLength counts what percent-encoding would produce without building the
string: a byte in the safe set costs one, anything else costs three. The budget
is the cap minus room for the footer at its longest, so the footer can’t push
the body back over. Then it backs off to the last newline, as before.
In Swift, a String is a collection of Characters, and iterating its indices
can’t land between the bytes of one. The bug came from stepping down to bytes
for the cut and then trying to step back up.
A test checks that cheap length model against the real addingPercentEncoding
on mixed samples: a&b+c d, emoji, tabs and a Japanese file name. Others shift
a body of four-byte emoji by zero to three bytes, so at least one alignment
would have put the old cut inside a character, and expect no crash, no
replacement character, and an encoded body within the cap. One uses a body of
é that’s under the cap raw and about two and a half times over it encoded,
and expects it trimmed to whole lines. Another builds a full mailto: URL from
a long body and checks the body in the URL fits.
What to take away
- Don’t parse what you rendered. If the data started out structured, keep it structured until the last moment. Every round trip through text is a chance to lose a field.
- Group crashes by function, not offset. The offset changes with every build. Image plus symbol is stable enough to group by and specific enough not to collide.
- Measure the thing that has the limit. A cap on the encoded URL has to be checked against the encoded length.
- Cut strings by characters, never bytes. And be suspicious of any
!on an index conversion.
Where in your own code is a limit being checked against a different representation than the one that actually has the limit?