facebook / facebook-ios-sdk

Used to integrate the Facebook Platform with your iOS & tvOS apps.
https://developers.facebook.com/docs/ios
Other
7.8k stars 3.56k forks source link

Crash due to EXC_BAD_ACCESS in SensitiveParamsManager.processParameters #2418

Closed 5anniversary closed 6 months ago

5anniversary commented 6 months ago

Checklist before submitting a bug report

Xcode version

15.3.0

Facebook iOS SDK version

17.0.1

Dependency Manager

SPM

SDK Framework

Core

Goals

I aim to ensure that the SensitiveParamsManager.processParameters function in the Facebook SDK processes event parameters reliably and filters sensitive information without causing any crashes due to memory access violations.

Expected results

I expect that all event parameters passed to the function are correctly filtered to remove sensitive information, and the process should handle all inputs without resulting in any memory access errors or application crashes.

Actual results

When processing some event parameters, the application crashes with an EXC_BAD_ACCESS (SIGSEGV) error within the SensitiveParamsManager.processParameters function. This suggests an attempt to access an invalid or unallocated memory address during the parameter processing stage.

Steps to reproduce

No response

Code samples & details

we call in this code


    func setEvent(
        _ eventName: String,
        parameters: [String: Any]?
    ) {
        let facebookEventName: AppEvents.Name = AppEvents.Name(eventName)
        let facebookEventParams: [AppEvents.ParameterName: Any] = transformLogging(parameters)

        AppEvents.shared.logEvent(
            facebookEventName,
            valueToSum: 0,
            parameters: facebookEventParams
        )
    }

    private func transformLogging(
        _ logging: [String: Any]?
    ) -> [AppEvents.ParameterName: Any] {
        var appEventsParams: [AppEvents.ParameterName: Any] = [:]

        guard
            let parameters = logging
        else {
            return appEventsParams
        }

        parameters.forEach { key, value in
            appEventsParams.updateValue(
                value,
                forKey: AppEvents.ParameterName(key)
            )
        }
        return appEventsParams
    }

and we encounter this crash log

OS Version: iOS 17.4.1 (21E236)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: SEGV_NOOP
Crashed Thread: 3

Application Specific Information:
Exception 1, Code 1, Subcode 9223372036854775824 >
KERN_INVALID_ADDRESS at 0x8000000000000010.

Thread 3 Crashed:
0   FBSDKCoreKit                    0x1094a4a50         [inlined] Dictionary.subscript.getter
1   FBSDKCoreKit                    0x1094a4a50         SensitiveParamsManager.processParameters (SensitiveParamsManager.swift:54)
2   FBSDKCoreKit                    0x1094a4a48         SensitiveParamsManager.processParameters (SensitiveParamsManager.swift:54)
3   FBSDKCoreKit                    0x1094a4c88         SensitiveParamsManager.processParameters
4   FBSDKCoreKit                    0x1093fee34         -[FBSDKAppEvents logEvent:valueToSum:parameters:isImplicitlyLogged:accessToken:] (FBSDKAppEvents.m:1168)
5   FBSDKCoreKit                    0x1093fb4d8         -[FBSDKAppEvents logEvent:valueToSum:parameters:accessToken:] (FBSDKAppEvents.m:236)
6   FBSDKCoreKit                    0x1093fb418         -[FBSDKAppEvents logEvent:valueToSum:parameters:] (FBSDKAppEvents.m:225)
7   ably                            0x2056cefac         [inlined] FacebookService.setEvent (FacebookService.swift:23)
8   ably                            0x2056cefac         FacebookEventTracker.logEvent (FacebookEventTracker.swift:31)
5anniversary commented 6 months ago

Solution The issue was identified as a concurrent access problem. The function -[FBSDKAppEvents flushOnMainQueue:forReason:] was being called, causing a crash due to the dictionary being accessed simultaneously by the main thread and the thread where the crash occurred. Ensuring that the function is called on the main thread resolved the issue.

            DispatchQueue.main.async {
                AppEvents.shared.logEvent(
                    facebookEventName,
                    parameters: facebookEventParams
                )
            }

By making sure that the flushOnMainQueue function is executed on the main thread, we avoided the concurrent access issue and resolved the crash.

Closing the Issue The issue has been resolved by addressing the concurrent access problem. Ensuring that the function flushOnMainQueue is executed on the main thread prevented the crash. Thus, I am closing this issue.