firebase / firebase-ios-sdk

Firebase SDK for Apple App Development
https://firebase.google.com
Apache License 2.0
5.47k stars 1.43k forks source link

App crashes in Release mode while logging App start session event #11403

Closed sanju-naik closed 4 months ago

sanju-naik commented 1 year ago

Description

App works fine in Debug mode but crashes in Release mode upon App launch while logging App start session event.

Screenshot 2023-06-06 at 4 56 23 PM

Reproducing the issue

We are observing this issue when using Bazel(https://bazel.build/) build system hence the Demo project is also built using Bazel.

To reproduce.

Clone this repo - https://github.com/sanju-naik/bazel-demo-github , and checkout firebase-sessions-crash-in-opt branch.

Firebase SDK Version

10.7.0

Xcode Version

14.3

Installation Method

CocoaPods

Firebase Product(s)

Analytics, Crashlytics, Performance

Targeted Platforms

iOS

Relevant Log Output

No response

If using Swift Package Manager, the project's Package.resolved

Expand Package.resolved snippet
```json Replace this line with the contents of your Package.resolved. ```

If using CocoaPods, the project's Podfile.lock

Expand Podfile.lock snippet
```yml PODS: - Alamofire (5.6.4) - Firebase (10.7.0): - Firebase/Core (= 10.7.0) - Firebase/Core (10.7.0): - Firebase/CoreOnly - FirebaseAnalytics (~> 10.7.0) - Firebase/CoreOnly (10.7.0): - FirebaseCore (= 10.7.0) - FirebaseABTesting (10.10.0): - FirebaseCore (~> 10.0) - FirebaseAnalytics (10.7.0): - FirebaseAnalytics/AdIdSupport (= 10.7.0) - FirebaseCore (~> 10.0) - FirebaseInstallations (~> 10.0) - GoogleUtilities/AppDelegateSwizzler (~> 7.8) - GoogleUtilities/MethodSwizzler (~> 7.8) - GoogleUtilities/Network (~> 7.8) - "GoogleUtilities/NSData+zlib (~> 7.8)" - nanopb (< 2.30910.0, >= 2.30908.0) - FirebaseAnalytics/AdIdSupport (10.7.0): - FirebaseCore (~> 10.0) - FirebaseInstallations (~> 10.0) - GoogleAppMeasurement (= 10.7.0) - GoogleUtilities/AppDelegateSwizzler (~> 7.8) - GoogleUtilities/MethodSwizzler (~> 7.8) - GoogleUtilities/Network (~> 7.8) - "GoogleUtilities/NSData+zlib (~> 7.8)" - nanopb (< 2.30910.0, >= 2.30908.0) - FirebaseCore (10.7.0): - FirebaseCoreInternal (~> 10.0) - GoogleUtilities/Environment (~> 7.8) - GoogleUtilities/Logger (~> 7.8) - FirebaseCoreExtension (10.10.0): - FirebaseCore (~> 10.0) - FirebaseCoreInternal (10.10.0): - "GoogleUtilities/NSData+zlib (~> 7.8)" - FirebaseCrashlytics (10.7.0): - FirebaseCore (~> 10.5) - FirebaseInstallations (~> 10.0) - FirebaseSessions (~> 10.5) - GoogleDataTransport (~> 9.2) - GoogleUtilities/Environment (~> 7.8) - nanopb (< 2.30910.0, >= 2.30908.0) - PromisesObjC (~> 2.1) - FirebaseDynamicLinks (10.7.0): - FirebaseCore (~> 10.0) - FirebaseInstallations (10.10.0): - FirebaseCore (~> 10.0) - GoogleUtilities/Environment (~> 7.8) - GoogleUtilities/UserDefaults (~> 7.8) - PromisesObjC (~> 2.1) - FirebasePerformance (10.7.0): - FirebaseCore (~> 10.5) - FirebaseInstallations (~> 10.0) - FirebaseRemoteConfig (~> 10.0) - FirebaseSessions (~> 10.5) - GoogleDataTransport (~> 9.2) - GoogleUtilities/Environment (~> 7.8) - GoogleUtilities/ISASwizzler (~> 7.8) - GoogleUtilities/MethodSwizzler (~> 7.8) - nanopb (< 2.30910.0, >= 2.30908.0) - FirebaseRemoteConfig (10.7.0): - FirebaseABTesting (~> 10.0) - FirebaseCore (~> 10.0) - FirebaseInstallations (~> 10.0) - GoogleUtilities/Environment (~> 7.8) - "GoogleUtilities/NSData+zlib (~> 7.8)" - FirebaseSessions (10.10.0): - FirebaseCore (~> 10.5) - FirebaseCoreExtension (~> 10.0) - FirebaseInstallations (~> 10.0) - GoogleDataTransport (~> 9.2) - GoogleUtilities/Environment (~> 7.10) - nanopb (< 2.30910.0, >= 2.30908.0) - PromisesSwift (~> 2.1) - GoogleAppMeasurement (10.7.0): - GoogleAppMeasurement/AdIdSupport (= 10.7.0) - GoogleUtilities/AppDelegateSwizzler (~> 7.8) - GoogleUtilities/MethodSwizzler (~> 7.8) - GoogleUtilities/Network (~> 7.8) - "GoogleUtilities/NSData+zlib (~> 7.8)" - nanopb (< 2.30910.0, >= 2.30908.0) - GoogleAppMeasurement/AdIdSupport (10.7.0): - GoogleAppMeasurement/WithoutAdIdSupport (= 10.7.0) - GoogleUtilities/AppDelegateSwizzler (~> 7.8) - GoogleUtilities/MethodSwizzler (~> 7.8) - GoogleUtilities/Network (~> 7.8) - "GoogleUtilities/NSData+zlib (~> 7.8)" - nanopb (< 2.30910.0, >= 2.30908.0) - GoogleAppMeasurement/WithoutAdIdSupport (10.7.0): - GoogleUtilities/AppDelegateSwizzler (~> 7.8) - GoogleUtilities/MethodSwizzler (~> 7.8) - GoogleUtilities/Network (~> 7.8) - "GoogleUtilities/NSData+zlib (~> 7.8)" - nanopb (< 2.30910.0, >= 2.30908.0) - GoogleDataTransport (9.2.3): - GoogleUtilities/Environment (~> 7.7) - nanopb (< 2.30910.0, >= 2.30908.0) - PromisesObjC (< 3.0, >= 1.2) - GoogleUtilities/AppDelegateSwizzler (7.11.1): - GoogleUtilities/Environment - GoogleUtilities/Logger - GoogleUtilities/Network - GoogleUtilities/Environment (7.11.1): - PromisesObjC (< 3.0, >= 1.2) - GoogleUtilities/ISASwizzler (7.11.1) - GoogleUtilities/Logger (7.11.1): - GoogleUtilities/Environment - GoogleUtilities/MethodSwizzler (7.11.1): - GoogleUtilities/Logger - GoogleUtilities/Network (7.11.1): - GoogleUtilities/Logger - "GoogleUtilities/NSData+zlib" - GoogleUtilities/Reachability - "GoogleUtilities/NSData+zlib (7.11.1)" - GoogleUtilities/Reachability (7.11.1): - GoogleUtilities/Logger - GoogleUtilities/UserDefaults (7.11.1): - GoogleUtilities/Logger - nanopb (2.30909.0): - nanopb/decode (= 2.30909.0) - nanopb/encode (= 2.30909.0) - nanopb/decode (2.30909.0) - nanopb/encode (2.30909.0) - PromisesObjC (2.2.0) - PromisesSwift (2.2.0): - PromisesObjC (= 2.2.0) DEPENDENCIES: - Alamofire - Firebase (= 10.7.0) - FirebaseCrashlytics (= 10.7.0) - FirebaseDynamicLinks (= 10.7.0) - FirebasePerformance (= 10.7.0) - FirebaseRemoteConfig (= 10.7.0) SPEC REPOS: trunk: - Alamofire - Firebase - FirebaseABTesting - FirebaseAnalytics - FirebaseCore - FirebaseCoreExtension - FirebaseCoreInternal - FirebaseCrashlytics - FirebaseDynamicLinks - FirebaseInstallations - FirebasePerformance - FirebaseRemoteConfig - FirebaseSessions - GoogleAppMeasurement - GoogleDataTransport - GoogleUtilities - nanopb - PromisesObjC - PromisesSwift SPEC CHECKSUMS: Alamofire: 4e95d97098eacb88856099c4fc79b526a299e48c Firebase: 0219acf760880eeec8ce479895bd7767466d9f81 FirebaseABTesting: b2a87808d90f02766fcffb3957d3e397e7accc15 FirebaseAnalytics: f8133442ee6f8512e28ff19e62ce15398bfaeace FirebaseCore: e317665b9d744727a97e623edbbed009320afdd7 FirebaseCoreExtension: 8d93ebbf6838a874b4ed82a564c9d6705f8365dd FirebaseCoreInternal: 971029061d326000d65bfdc21f5502c75c8b0893 FirebaseCrashlytics: 35fdd1a433b31e28adcf5c8933f4c526691a1e0b FirebaseDynamicLinks: 16a8fae3697fba66fed7a1d646fe59f30d42aa31 FirebaseInstallations: 52153982b057d3afcb4e1fbb3eb0b6d00611e681 FirebasePerformance: 8281bbaf08aad194001018b932115b7d58a6f00b FirebaseRemoteConfig: d5de62211e2eaa2152d8ee85a23c301b70887a74 FirebaseSessions: 5f9e62cd4096e24d2011cbd845b0efceffaee1ec GoogleAppMeasurement: fe17c92a32207dd5cdd4e8d742767f2da74857f6 GoogleDataTransport: f0308f5905a745f94fb91fea9c6cbaf3831cb1bd GoogleUtilities: 9aa0ad5a7bc171f8bae016300bfcfa3fb8425749 nanopb: b552cce312b6c8484180ef47159bc0f65a1f0431 PromisesObjC: 09985d6d70fbe7878040aa746d78236e6946d2ef PromisesSwift: cf9eb58666a43bbe007302226e510b16c1e10959 PODFILE CHECKSUM: be1abb165a8c7f88af2b139d4ea107cea4f424ac COCOAPODS: 1.12.1 ```
google-oss-bot commented 1 year ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

paulb777 commented 1 year ago

This may be the result of building the nanopb library without specifying the right options. For example see https://github.com/firebase/firebase-ios-sdk/blob/master/FirebaseCrashlytics.podspec#L101

'PB_FIELD_32BIT=1 PB_NO_PACKED_STRUCTS=1 PB_ENABLE_MALLOC=1'

sanju-naik commented 1 year ago

Above mentioned flags are being passed, From the example I shared, I am passing the flags here - https://github.com/sanju-naik/bazel-demo-github/blob/firebase-sessions-crash-in-opt/BazelDemo/Pods/nanopb/BUILD.bazel#L24 . But still the app crashes.

paulb777 commented 1 year ago

Those flags also need to be passed to the clients of nanopb since the clients need to have the right -D options when they use the nanopb headers.

sanju-naik commented 1 year ago

Ok, let me examine the swiftc invocations and check if these flags are present.

Those flags also need to be passed to the clients of nanopb

By Clients here you mean Targets that depend on nanopb? i.e the Firebase Pods or Is it the iOS Application Targer?

Also in the same setup, it runs fine in Debug mode so just curious if those options are passed by default in Debug mode?

paulb777 commented 1 year ago

Any Firebase product that sets the options in the podspec like https://github.com/firebase/firebase-ios-sdk/blob/master/FirebaseCrashlytics.podspec#L101 needs to be built with those options.

Crashes from alignment issues can be randomish since they depend on how linking occurs and whether or not data happens to fall on a required alignment boundary or not.

sanju-naik commented 1 year ago

All the flags mentioned in the Podspec file are passed in all of the Firebase Pods as well as nanopb pod but it still crashes in Release mode. Also I tried comparing different compiler options between debug & release mode and add the debug mode ones to Release to get it working but still no luck 😞 .

Would it be possible to Debug this using the example I provided above and provide more insights onto which flag is causing this issue? 🙏

samedson commented 8 months ago

@sanju-naik I don't seem to have access to the repository anymore. Were you able to get this working?

google-oss-bot commented 8 months ago

Hey @sanju-naik. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

sanju-naik commented 8 months ago

We solved this issue by issue by using xcframework of nanopb. But the issue still exists on source code based Cocoapod. If you can take a look at that, it would be great 🙏 Let me update and share the correct url for the Sample project.

sergiocampama commented 4 months ago

I'm seeing this issue as well on Firebase 10.21.0 from cocoapods. It happens on startup everytime

paulb777 commented 4 months ago

As discussed above, nanopb crashes are almost always related to a problem with option setting/propagation in the build system.

We'll need a reproducible example to help here.

sergiocampama commented 4 months ago

I was able to reproduce with the following project https://github.com/sergiocampama/SessionsTestApp on Xcode 15.3 beta 3, wasn't able to repro on 15.2, both attempts on a 17.2 iPhone simulator. It looks to be related to Xcode beta then. The project is configured to run on the simulator using the Release configuration. It does not reproduce on the Debug configuration. I believe you should be able to just download that project, pod install and then run on a simulator using xcode 15.3 beta 3 to reproduce.

Screenshot 2024-02-24 at 7 23 07 PM Screenshot 2024-02-24 at 7 23 16 PM
sergiocampama commented 4 months ago

We solved this issue by issue by using xcframework of nanopb. But the issue still exists on source code based Cocoapod. If you can take a look at that, it would be great 🙏 Let me update and share the correct url for the Sample project.

@sanju-naik did you replace nanopb with an xcframework on a cocoapods project? could you share how you did it?

paulb777 commented 4 months ago

@sergiocampama Thanks for the repro, but I'm not able to reproduce the crash on Xcode 15.3 beta 3. Maybe the crash is specific to Arm Macs, and I have an Intel Mac?

sergiocampama commented 4 months ago

You should be able to connect an iPhone as well, a bit more cumbersome since you'll need to add your account to Xcode, change the bundle ID of the app and set your development team on the signing settings, but it should also reproduce there

sergiocampama commented 4 months ago

Could also reproduce on a different Apple Silicon machine, same config as the above, could be that x86 simulators are unaffected.

paulb777 commented 4 months ago

Thanks. On an iPhone 13, I don't see a crash, but I see several log messages like:

10.21.0 - [FirebaseSessions][I-SES000000] Session Event failed to encode as proto with error: Optional(Error Domain=FIRSESEncodeError Code=-1 "(null)" UserInfo={NSLocalizedDescriptionKey=Error in nanopb encoding to get size: invalid data_size})
10.21.0 - [FirebaseSessions][I-SES000000] Session Event generated nil transportBytes. Returning empty data.

that doesn't occur when built in Debug mode.

And on closer inspection, the log failure DOES reproduce on the simulator when built in Release mode.

paulb777 commented 4 months ago

Looks like the issue could be related to type conversions between Swift UnsafeMutablePointers and C types. Note the difference between fields[1] and its corresponding entry in the original array.

The random? memory values in the entries after array element 0 would also explain sometimes crashing and sometimes logging an error message.

Screenshot 2024-02-25 at 10 12 29 AM

sergiocampama commented 4 months ago

I tested the linked PR on a locally modified copy of Firebase, and the crash does not occur any more on my repro cases 🚀

thanks for looking into it over the weekend! wish I could send you a peer bonus :)

paulb777 commented 4 months ago

@sergiocampama Thanks for isolating it to a reproducible example and testing the proposed fix! I'll ask the team to review it this week and target it for our next release.

sanju-naik commented 4 months ago

did you replace nanopb with an xcframework on a cocoapods project? could you share how you did it?

@sergiocampama For a release we download all of xcframeworks from a firebase using their source code zip linkhttps://github.com/firebase/firebase-ios-sdk/releases/download/10.21.0/Firebase.zip , and then we are hosting these xcframeworks in our internal artifactory with our own forked podspecs.

This way we replaced all of our Firebase Pods from source code to xcframeworks . Since the crash was happening when we compile using Bazel, after moving to pre-built xcframework crash was resolved.

sanju-naik commented 4 months ago

And glad to see now the crash is fixed on source code based Pod as well ❤️

SPHthabib commented 4 months ago

@paulb777 Is there any way to prevent this occurrence?

Would utilizing an earlier version of the Firebase SDK potentially address the issue, until a fix is released?

paulb777 commented 4 months ago

The problematic source code was introduced in Firebase 10.6.0 so it may work to use an earlier version.

It might be easier to stay on Xcode 15.2 instead though until the fix releases in 10.22.0.

ShepSims commented 4 months ago

Has this fix also been added to the react-native-firebase 19.0.1 release? I was seeing this issue as well after the upgrade to 17.4 and want to release a fix as soon as possible

ShepSims commented 4 months ago

just upgraded and it did fix the issue^