-
Notifications
You must be signed in to change notification settings - Fork 265
feat: [SDK-4978] add iOS KMP crash capture and upload #1713
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
Open
fadi-george
wants to merge
7
commits into
main
Choose a base branch
from
fadi/sdk-4978
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9171de
feat(logging): add OSLogCrashHandler for fatal crash capture
fadi-george e760469
fix(logging): harden crash handler and logger lifecycle
fadi-george 3f294a8
fix(logging): address crash pipeline review findings
fadi-george d525893
fix(logging): attribute crashes to the throwing module
fadi-george b44a575
fix(logging): address uploader lifecycle feedback
fadi-george 9fb883c
chore(project): regenerate Xcode file references
fadi-george 0ff5121
fix(logging): use dladdr for crash attribution
fadi-george File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
289 changes: 289 additions & 0 deletions
289
iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/Logging/OSLogCrashHandler.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| /* | ||
| Modified MIT License | ||
|
|
||
| Copyright 2026 OneSignal | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| 1. The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| 2. All copies of substantial portions of the Software may only be used in connection | ||
| with services provided by OneSignal. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| */ | ||
|
|
||
| // Kotlin/Native does not produce a Mac Catalyst framework slice. | ||
| #if !targetEnvironment(macCatalyst) | ||
|
|
||
| import Darwin | ||
| import Foundation | ||
| @_implementationOnly import OneSignalKMP | ||
|
|
||
| private typealias OSExceptionHandler = @convention(c) (NSException) -> Void | ||
|
|
||
| private func osLogUncaughtExceptionHandler(_ exception: NSException) { | ||
| OSLogCrashHandler.handleActive(exception) | ||
| } | ||
|
|
||
| struct OSResolvedStackFrame: Equatable { | ||
| let imagePath: String? | ||
| let symbolName: String? | ||
| } | ||
|
|
||
| final class OSCrashLogger: ILogger { | ||
| func error(message: String) { | ||
| NSLog("[OneSignal crash] ERROR: %@", message) | ||
| } | ||
|
|
||
| func warn(message: String) { | ||
| NSLog("[OneSignal crash] WARN: %@", message) | ||
| } | ||
|
|
||
| func info(message: String) { | ||
| NSLog("[OneSignal crash] INFO: %@", message) | ||
| } | ||
|
|
||
| func debug(message: String) { | ||
| NSLog("[OneSignal crash] DEBUG: %@", message) | ||
| } | ||
| } | ||
|
|
||
| /// Captures uncaught Objective-C exceptions through the synchronous KMP crash | ||
| /// reporter before forwarding to the handler that was previously installed. | ||
| /// | ||
| /// POSIX signals are intentionally not intercepted because Swift, Foundation, | ||
| /// Kotlin/Native, and the durable file store are not async-signal-safe. | ||
| final class OSLogCrashHandler: ILogCrashHandler { | ||
| private static let oneSignalModules: Set<String> = [ | ||
| "OneSignal", | ||
| "OneSignalCore", | ||
| "OneSignalExtension", | ||
| "OneSignalFramework", | ||
| "OneSignalInAppMessages", | ||
| "OneSignalKMP", | ||
| "OneSignalLiveActivities", | ||
| "OneSignalLocation", | ||
| "OneSignalNotifications", | ||
| "OneSignalOSCore", | ||
| "OneSignalOutcomes", | ||
| "OneSignalUser" | ||
| ] | ||
| private static let registryLock = NSLock() | ||
| private static let handlingThreadKey = "com.onesignal.logger.handling-exception" | ||
| private static var active: OSLogCrashHandler? | ||
| private static var inactivePreviousHandler: OSExceptionHandler? | ||
|
|
||
| private let reporter: ILogCrashReporter | ||
| private var previousExceptionHandler: OSExceptionHandler? | ||
| private var isInitialized = false | ||
|
|
||
| init(reporter: ILogCrashReporter) { | ||
| self.reporter = reporter | ||
| } | ||
|
|
||
| func initialize() { | ||
| Self.registryLock.lock() | ||
| defer { Self.registryLock.unlock() } | ||
| guard !isInitialized else { | ||
| return | ||
| } | ||
| guard Self.active == nil else { | ||
| return | ||
| } | ||
|
|
||
| previousExceptionHandler = NSGetUncaughtExceptionHandler() | ||
| Self.inactivePreviousHandler = previousExceptionHandler | ||
| Self.active = self | ||
| NSSetUncaughtExceptionHandler(osLogUncaughtExceptionHandler) | ||
| isInitialized = true | ||
| } | ||
|
|
||
| func unregister() { | ||
| Self.registryLock.lock() | ||
| defer { Self.registryLock.unlock() } | ||
| guard isInitialized else { | ||
| return | ||
| } | ||
|
|
||
| let isCurrentHandler = Self.exceptionHandlerAddress(NSGetUncaughtExceptionHandler()) | ||
| == Self.exceptionHandlerAddress(osLogUncaughtExceptionHandler) | ||
| if Self.active === self, isCurrentHandler { | ||
| NSSetUncaughtExceptionHandler(previousExceptionHandler) | ||
| } | ||
| if Self.active === self { | ||
| Self.active = nil | ||
| } | ||
| isInitialized = false | ||
| } | ||
|
|
||
| func handle(exception: NSException) { | ||
| handle( | ||
| exception: exception, | ||
| stackSymbols: exception.callStackSymbols, | ||
| resolvedFrames: Self.resolveStackFrames(exception.callStackReturnAddresses) | ||
| ) | ||
| } | ||
|
|
||
| func handle( | ||
| exception: NSException, | ||
| stackSymbols: [String], | ||
| resolvedFrames: [OSResolvedStackFrame] | ||
| ) { | ||
| guard Self.isOneSignalAtFault(resolvedFrames) else { | ||
| previousExceptionHandler?(exception) | ||
| return | ||
| } | ||
| capture( | ||
| exceptionType: exception.name.rawValue, | ||
| exceptionMessage: exception.reason ?? exception.description, | ||
| stacktrace: stackSymbols.joined(separator: "\n") | ||
| ) | ||
| previousExceptionHandler?(exception) | ||
| } | ||
|
|
||
| private func capture( | ||
| exceptionType: String, | ||
| exceptionMessage: String, | ||
| stacktrace: String | ||
| ) { | ||
| let crash = CrashData( | ||
| threadName: Self.currentThreadName, | ||
| exceptionType: exceptionType, | ||
| exceptionMessage: exceptionMessage, | ||
| stacktrace: stacktrace | ||
| ) | ||
| do { | ||
| try reporter.saveCrash(crash: crash) | ||
| } catch { | ||
| NSLog("[OneSignal crash] Unable to persist fatal crash: %@", error.localizedDescription) | ||
| } | ||
|
fadi-george marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private static var currentThreadName: String { | ||
| if let name = Thread.current.name, !name.isEmpty { | ||
| return name | ||
| } | ||
| if Thread.isMainThread { | ||
| return "main" | ||
| } | ||
| var name = [CChar](repeating: 0, count: 64) | ||
| guard pthread_getname_np(pthread_self(), &name, name.count) == 0 else { | ||
| return "unknown" | ||
| } | ||
| let threadName = String(cString: name) | ||
| return threadName.isEmpty ? "unknown" : threadName | ||
| } | ||
|
|
||
| private static func exceptionHandlerAddress(_ handler: OSExceptionHandler?) -> UInt { | ||
| handler.map { unsafeBitCast($0, to: UInt.self) } ?? 0 | ||
| } | ||
|
|
||
| static func handleActive(_ exception: NSException) { | ||
| let threadDictionary = Thread.current.threadDictionary | ||
| guard threadDictionary[handlingThreadKey] == nil else { | ||
| return | ||
| } | ||
| threadDictionary[handlingThreadKey] = true | ||
| defer { threadDictionary.removeObject(forKey: handlingThreadKey) } | ||
|
|
||
| registryLock.lock() | ||
| let handler = active | ||
| let previousHandler = inactivePreviousHandler | ||
| registryLock.unlock() | ||
| if let handler { | ||
| handler.handle(exception: exception) | ||
| } else { | ||
| previousHandler?(exception) | ||
| } | ||
| } | ||
|
|
||
| static func isOneSignalAtFault(_ frames: [OSResolvedStackFrame]) -> Bool { | ||
| frames.contains { frame in | ||
| guard let imagePath = frame.imagePath, | ||
| !isSystemImage(imagePath) else { | ||
| return false | ||
| } | ||
| if oneSignalModules.contains(imageName(from: imagePath)) { | ||
| return true | ||
| } | ||
| guard let symbolName = frame.symbolName else { | ||
| return false | ||
| } | ||
| return isOneSignalSymbol(symbolName) | ||
| } | ||
| } | ||
|
|
||
| private static func resolveStackFrames(_ addresses: [NSNumber]) -> [OSResolvedStackFrame] { | ||
| addresses.map { address in | ||
| guard let pointer = UnsafeRawPointer(bitPattern: address.uintValue) else { | ||
| return OSResolvedStackFrame(imagePath: nil, symbolName: nil) | ||
| } | ||
| var info = Dl_info() | ||
| guard dladdr(pointer, &info) != 0 else { | ||
| return OSResolvedStackFrame(imagePath: nil, symbolName: nil) | ||
| } | ||
| return OSResolvedStackFrame( | ||
| imagePath: info.dli_fname.map { String(cString: $0) }, | ||
| symbolName: info.dli_sname.map { String(cString: $0) } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private static func imageName(from path: String) -> String { | ||
| path.split(separator: "/").last.map(String.init) ?? path | ||
| } | ||
|
|
||
| private static func isSystemImage(_ path: String) -> Bool { | ||
| path.contains("/System/Library/") || path.contains("/usr/lib/") | ||
| } | ||
|
|
||
| private static func isOneSignalSymbol(_ symbol: String) -> Bool { | ||
| let symbolWithoutLeadingUnderscores = symbol.drop(while: { $0 == "_" }) | ||
| if symbolWithoutLeadingUnderscores.hasPrefix("-[OneSignal") | ||
| || symbolWithoutLeadingUnderscores.hasPrefix("+[OneSignal") | ||
| || symbolWithoutLeadingUnderscores.hasPrefix("onesignal_") | ||
| || symbolWithoutLeadingUnderscores.contains("kfun:com.onesignal.") { | ||
| return true | ||
| } | ||
| guard let module = swiftModuleName(from: String(symbolWithoutLeadingUnderscores)) else { | ||
| return false | ||
| } | ||
| return oneSignalModules.contains(module) | ||
| } | ||
|
|
||
| private static func swiftModuleName(from symbol: String) -> String? { | ||
| guard symbol.hasPrefix("$s") else { | ||
| return nil | ||
| } | ||
| let moduleLengthStart = symbol.index(symbol.startIndex, offsetBy: 2) | ||
| var moduleNameStart = moduleLengthStart | ||
| while moduleNameStart < symbol.endIndex, symbol[moduleNameStart].isNumber { | ||
| moduleNameStart = symbol.index(after: moduleNameStart) | ||
| } | ||
| guard moduleNameStart > moduleLengthStart, | ||
| let moduleLength = Int(symbol[moduleLengthStart..<moduleNameStart]), | ||
| let moduleNameEnd = symbol.index( | ||
| moduleNameStart, | ||
| offsetBy: moduleLength, | ||
| limitedBy: symbol.endIndex | ||
| ) else { | ||
| return nil | ||
| } | ||
| return String(symbol[moduleNameStart..<moduleNameEnd]) | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.