Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Missing mach info for crash reports #4230

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- Skip UI crumbs when target or sender is nil (#4211)
- Guard FramesTracker start and stop (#4224)
- Long-lasting TTID/TTFD spans (#4225). Avoid long TTID spans when the FrameTracker isn't running, which is the case when the app is in the background.
- Missing mach info for crash reports (#4230)


### Improvements

Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/include/SentryInternalCDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ typedef unsigned long long bytes;

#include <TargetConditionals.h>

#ifdef __APPLE__
# define SENTRY_HOST_APPLE 1
#endif

#ifndef TARGET_OS_VISION
# define TARGET_OS_VISION 0
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,50 @@ class SentryCrashReportTests: XCTestCase {
XCTAssertFalse(crashReportContentsAsString.contains("boot_time"), "The crash report must not contain boot_time because Apple forbids sending this information off device see: https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api#4278394.")
}

private func writeCrashReport() {
func testCrashReportContainsMachInfo() throws {
serializeToCrashReport(scope: fixture.scope)

var monitorContext = SentryCrash_MonitorContext()
monitorContext.mach.type = EXC_BAD_ACCESS
monitorContext.mach.code = 1
monitorContext.mach.subcode = 12

let api = sentrycrashcm_system_getAPI()
api?.pointee.addContextualInfoToEvent(&monitorContext)
writeCrashReport(monitorContext: monitorContext)

sentrycrashreport_writeStandardReport(&monitorContext, fixture.reportPath)
let crashReportContents = try XCTUnwrap( FileManager.default.contents(atPath: fixture.reportPath))

let crashReport: CrashReport = try JSONDecoder().decode(CrashReport.self, from: crashReportContents)

let mach = try XCTUnwrap(crashReport.crash.error.mach)
XCTAssertEqual(1, mach.exception)
XCTAssertEqual("EXC_BAD_ACCESS", mach.exception_name)
XCTAssertEqual(1, mach.code)
XCTAssertEqual("KERN_INVALID_ADDRESS", mach.code_name)
XCTAssertEqual(12, mach.subcode)
}

func testCrashReportContainsStandardMachInfo_WhenMachInfoIsEmpty() throws {
serializeToCrashReport(scope: fixture.scope)
writeCrashReport()

let crashReportContents = try XCTUnwrap( FileManager.default.contents(atPath: fixture.reportPath))

let crashReport: CrashReport = try JSONDecoder().decode(CrashReport.self, from: crashReportContents)

let mach = try XCTUnwrap(crashReport.crash.error.mach)
XCTAssertEqual(0, mach.exception)
XCTAssertNil(mach.exception_name)
XCTAssertEqual(0, mach.code)
XCTAssertNil(mach.code_name)
XCTAssertEqual(0, mach.subcode)
}

private func writeCrashReport(monitorContext: SentryCrash_MonitorContext? = nil) {
var localMonitorContext = monitorContext ?? SentryCrash_MonitorContext()

let api = sentrycrashcm_system_getAPI()
api?.pointee.addContextualInfoToEvent(&localMonitorContext)
sentrycrashreport_writeStandardReport(&localMonitorContext, fixture.reportPath)
}

/**
Expand Down Expand Up @@ -154,8 +191,8 @@ class SentryCrashReportTests: XCTestCase {
// We parse JSON so it's fine to disable identifier_name
// swiftlint:disable identifier_name
struct CrashReport: Decodable {
let user: CrashReportUserInfo
let sentry_sdk_scope: CrashReportUserInfo
let user: CrashReportUserInfo?
let sentry_sdk_scope: CrashReportUserInfo?
let crash: Crash
}

Expand All @@ -167,13 +204,22 @@ class SentryCrashReportTests: XCTestCase {
let type: String?
let reason: String?
let nsexception: NSException?
let mach: Mach?
}

struct NSException: Decodable, Equatable {
let name: String?
let userInfo: String?
let reason: String?
}

struct Mach: Decodable, Equatable {
let exception: Int?
let exception_name: String?
let code: Int?
let code_name: String?
let subcode: Int?
}

struct CrashReportUserInfo: Decodable, Equatable {
let user: CrashReportUser?
Expand All @@ -186,7 +232,7 @@ class SentryCrashReportTests: XCTestCase {
let level: String?
let breadcrumbs: [CrashReportCrumb]?
}

struct CrashReportUser: Decodable, Equatable {
let id: String
let email: String
Expand Down
3 changes: 3 additions & 0 deletions Tests/SentryTests/SentryKSCrashReportConverterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ - (void)testConvertReport
stringWithFormat:@"%@", [exception.mechanism.meta.signal valueForKeyPath:@"number"]];
NSString *exc = [NSString
stringWithFormat:@"%@", [exception.mechanism.meta.machException valueForKeyPath:@"name"]];

XCTAssertEqualObjects(code, @"0");
XCTAssertEqualObjects(number, @"10");
XCTAssertEqualObjects(exc, @"EXC_BAD_ACCESS");
XCTAssertEqualObjects(
[exception.mechanism.data valueForKeyPath:@"relevant_address"], @"0x0000000102468000");
XCTAssertNotNil(exception.mechanism.handled);
XCTAssertFalse(exception.mechanism.handled.boolValue);

XCTAssertTrue([NSJSONSerialization isValidJSONObject:[event serialize]]);
XCTAssertNotNil([[event serialize] valueForKeyPath:@"exception.values"]);
Expand Down
Loading