Skip to content
Open
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
8 changes: 8 additions & 0 deletions iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@
3C14E3B32FAE54C006ED053 /* OSLoggerPlatformProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C14E3AC2FAE54C006ED053 /* OSLoggerPlatformProvider.swift */; };
3C14E3B42FAE54C006ED053 /* KotlinByteArray+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C14E3AD2FAE54C006ED053 /* KotlinByteArray+Data.swift */; };
3C14E3B52FAE54C006ED053 /* OSLoggerAdaptersTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C14E3AE2FAE54C006ED053 /* OSLoggerAdaptersTests.swift */; };
C781A33FED62B4B54221A09A /* OSLogCrashHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B6A59620B83538CEFF77269 /* OSLogCrashHandlerTests.swift */; };
B96A3B6BA8CC49EE4796D9BF /* OSRemoteLoggingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A72F938F8A3808AC1FF7F3C /* OSRemoteLoggingController.swift */; };
25898119922BDCDA7AF0B9CC /* OSRemoteLoggingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A72F938F8A3808AC1FF7F3C /* OSRemoteLoggingController.swift */; };
9EAF92032D0429FA35E04417 /* OSRemoteLoggingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A72F938F8A3808AC1FF7F3C /* OSRemoteLoggingController.swift */; };
ACE2175908241BB46C9F1829 /* OSRemoteLoggingControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 658E6E9E6BC6BBF702BCBD33 /* OSRemoteLoggingControllerTests.swift */; };
7732574D325D34CC7C498199 /* OSRemoteLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6972EE491A57C79EFE56D4C8 /* OSRemoteLogger.swift */; };
698F58A488FCE503DFD5247F /* OSLogCrashHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACD3284210A7DF2597594778 /* OSLogCrashHandler.swift */; };
3C19C6322E919F0C00D6731E /* OSRequestLiveActivityClicked.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C19C6312E919F0C00D6731E /* OSRequestLiveActivityClicked.swift */; };
3C23A21B2FCE0A52001D32E3 /* OneSignalIdentifiersFallbackTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C23A21A2FCE0A52001D32E3 /* OneSignalIdentifiersFallbackTests.swift */; };
3C23A21D2FCE0A83001D32E3 /* OSModelStoreRefreshTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C23A21C2FCE0A83001D32E3 /* OSModelStoreRefreshTests.swift */; };
Expand Down Expand Up @@ -1796,10 +1798,12 @@
3C14E3AC2FAE54C006ED053 /* OSLoggerPlatformProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLoggerPlatformProvider.swift; sourceTree = "<group>"; };
3C14E3AD2FAE54C006ED053 /* KotlinByteArray+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "KotlinByteArray+Data.swift"; sourceTree = "<group>"; };
3C14E3AE2FAE54C006ED053 /* OSLoggerAdaptersTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLoggerAdaptersTests.swift; sourceTree = "<group>"; };
3B6A59620B83538CEFF77269 /* OSLogCrashHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLogCrashHandlerTests.swift; sourceTree = "<group>"; };
8A72F938F8A3808AC1FF7F3C /* OSRemoteLoggingController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSRemoteLoggingController.swift; sourceTree = "<group>"; };
C0462F96E1AADF655F3B3765 /* OSRemoteLoggingController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSRemoteLoggingController.h; sourceTree = "<group>"; };
658E6E9E6BC6BBF702BCBD33 /* OSRemoteLoggingControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSRemoteLoggingControllerTests.swift; sourceTree = "<group>"; };
6972EE491A57C79EFE56D4C8 /* OSRemoteLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSRemoteLogger.swift; sourceTree = "<group>"; };
ACD3284210A7DF2597594778 /* OSLogCrashHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSLogCrashHandler.swift; sourceTree = "<group>"; };
DEF5CCF12539321A0003E9CC /* UnitTestApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = UnitTestApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
DEF5CCF32539321A0003E9CC /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
DEF5CCF42539321A0003E9CC /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2295,6 +2299,7 @@
3C14E3AB2FAE54C006ED053 /* FileLogStore.swift */,
3C14E3AA2FAE54C006ED053 /* OneSignalLogHttpSender.swift */,
3C14E3A92FAE54C006ED053 /* IOSLogger.swift */,
ACD3284210A7DF2597594778 /* OSLogCrashHandler.swift */,
3C14E3AC2FAE54C006ED053 /* OSLoggerPlatformProvider.swift */,
6972EE491A57C79EFE56D4C8 /* OSRemoteLogger.swift */,
);
Expand Down Expand Up @@ -2569,6 +2574,7 @@
5BC1DE672C90C23E00CA8807 /* OSConsistencyManagerTests.swift */,
3C427AC8301BB28A0059B8B7 /* OSOperationRepoFlushTests.swift */,
3C14E3AE2FAE54C006ED053 /* OSLoggerAdaptersTests.swift */,
3B6A59620B83538CEFF77269 /* OSLogCrashHandlerTests.swift */,
3C23A21A2FCE0A52001D32E3 /* OneSignalIdentifiersFallbackTests.swift */,
3C23A21E2FCE0AA1001D32E3 /* OSResilientStorageTests.swift */,
3C23A21C2FCE0A83001D32E3 /* OSModelStoreRefreshTests.swift */,
Expand Down Expand Up @@ -4442,6 +4448,7 @@
3C14E3B12FAE54C006ED053 /* OneSignalLogHttpSender.swift in Sources */,
3C4F9E4428A4466C009F453A /* OSOperationRepo.swift in Sources */,
3C14E3B02FAE54C006ED053 /* IOSLogger.swift in Sources */,
698F58A488FCE503DFD5247F /* OSLogCrashHandler.swift in Sources */,
7732574D325D34CC7C498199 /* OSRemoteLogger.swift in Sources */,
3C11518B289ADEEB00565C41 /* OSEventProducer.swift in Sources */,
3C115165289A259500565C41 /* OneSignalOSCore.docc in Sources */,
Expand Down Expand Up @@ -4604,6 +4611,7 @@
3C23A21D2FCE0A83001D32E3 /* OSModelStoreRefreshTests.swift in Sources */,
3C427AC9301BB28A0059B8B7 /* OSOperationRepoFlushTests.swift in Sources */,
3C14E3B52FAE54C006ED053 /* OSLoggerAdaptersTests.swift in Sources */,
C781A33FED62B4B54221A09A /* OSLogCrashHandlerTests.swift in Sources */,
3C23A21B2FCE0A52001D32E3 /* OneSignalIdentifiersFallbackTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
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"
Comment thread
fadi-george marked this conversation as resolved.
]
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)
}
Comment thread
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
Loading
Loading