Skip to content

Commit

Permalink
Merge pull request #563 from DataDog/buranmert/RUMM-1492-fix-isSystem…
Browse files Browse the repository at this point in the history
…Image-v2

RUMM-1492: Fix `isSystemImage` detection
  • Loading branch information
buranmert authored Aug 13, 2021
2 parents 0031ec6 + 12ef7a5 commit 3b43f91
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,12 @@ extension BinaryImageInfo {

self.uuid = imageUUID
self.imageName = URL(string: imagePath)?.lastPathComponent
// NOTE: RUMM-1492 refer to JIRA ticket or `CrashReportTests.swift` to see imagePath examples
self.isSystemImage = !imagePath.contains("/Bundle/Application/") || imagePath.contains("/Contents/Developer/Platforms/")

#if targetEnvironment(simulator)
self.isSystemImage = Self.isPathSystemImageInSimulator(imagePath)
#else
self.isSystemImage = Self.isPathSystemImageInDevice(imagePath)
#endif

if let codeType = imageInfo.codeType {
self.codeType = CodeType(from: codeType)
Expand All @@ -254,6 +258,17 @@ extension BinaryImageInfo {
self.imageBaseAddress = imageInfo.imageBaseAddress
self.imageSize = imageInfo.imageSize
}

static func isPathSystemImageInSimulator(_ path: String) -> Bool {
// in simulator, example system image path: ~/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/...
return path.contains("/Contents/Developer/Platforms/")
}

static func isPathSystemImageInDevice(_ path: String) -> Bool {
// in device, example user image path: .../containers/Bundle/Application/0000/Example.app/Frameworks/...
let isUserImage = path.contains("/Bundle/Application/")
return !isUserImage
}
}

extension BinaryImageInfo.CodeType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,37 @@ class CrashReportTests: XCTestCase {
XCTAssertEqual(threadInfo.stackFrames.count, mockStackFrames.count)
}

private let systemImagePaths_device = [
"/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore",
"/usr/lib/system/libdyld.dylib"
]
private let systemImagePaths_simulator = [
"/Users/john.appleseed/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
]
private let userImagePaths_device = [
"/private/var/containers/Bundle/Application/0000/Example.app/Example",
"/private/var/containers/Bundle/Application/0000/Example.app/Frameworks/DatadogCrashReporting.framework/DatadogCrashReporting"
]
private let userImagePaths_simulator = [
"/Users/john.appleseed/Library/Developer/CoreSimulator/Devices/0000/data/Containers/Bundle/Application/0000/Example.app/Example",
"/Users/john.appleseed/Library/Developer/Xcode/DerivedData/Datadog-abcd/Build/Products/Release-iphonesimulator/DatadogCrashReporting.framework/DatadogCrashReporting"
]

func testItDetectsSystemImages() throws {
for systemImagePath in systemImagePaths_device {
XCTAssertTrue(BinaryImageInfo.isPathSystemImageInDevice(systemImagePath), "\(systemImagePath) is a system image")
}
for systemImagePath in systemImagePaths_simulator {
XCTAssertTrue(BinaryImageInfo.isPathSystemImageInSimulator(systemImagePath), "\(systemImagePath) is a system image")
}
for userImagePath in userImagePaths_device {
XCTAssertFalse(BinaryImageInfo.isPathSystemImageInDevice(userImagePath), "\(userImagePath) is an user image")
}
for userImagePath in userImagePaths_simulator {
XCTAssertFalse(BinaryImageInfo.isPathSystemImageInSimulator(userImagePath), "\(userImagePath) is an user image")
}
}

func testItReadsBinaryImageInfo() throws {
func mock(with imagePath: URL) -> PLCrashReportMock.BinaryImageInfo {
let mock = PLCrashReportMock.BinaryImageInfo()
Expand All @@ -220,20 +251,13 @@ class CrashReportTests: XCTestCase {
}

// Given
let systemImagePathString = [
"/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore",
"/usr/lib/system/libdyld.dylib",
"/Users/john.appleseed/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
].randomElement()!
let systemImagePath = URL(string: systemImagePathString)!

let userImagePathString = [
"/private/var/containers/Bundle/Application/0000/Example.app/Example",
"/private/var/containers/Bundle/Application/0000/Example.app/Frameworks/DatadogCrashReporting.framework/DatadogCrashReporting",
"/Users/john.appleseed/Library/Developer/CoreSimulator/Devices/0000/data/Containers/Bundle/Application/0000/Example.app/Example",
"/Users/john.appleseed/Library/Developer/Xcode/DerivedData/Datadog-abcd/Build/Products/Release-iphonesimulator/DatadogCrashReporting.framework/DatadogCrashReporting"
].randomElement()!
let userImagePath = URL(string: userImagePathString)!
#if targetEnvironment(simulator)
let systemImagePath = URL(string: systemImagePaths_simulator.randomElement()!)!
let userImagePath = URL(string: userImagePaths_simulator.randomElement()!)!
#else
let systemImagePath = URL(string: systemImagePaths_device.randomElement()!)!
let userImagePath = URL(string: userImagePaths_device.randomElement()!)!
#endif

let mockSystemImage = mock(with: systemImagePath)
let mockUserImage = mock(with: userImagePath)
Expand Down

0 comments on commit 3b43f91

Please sign in to comment.