Skip to content

[Bug] App Check (iOS): crash in TokenChangedMethod because AppCheckToken.expire_time_millis is left uninitialized #1556

Closed

Description

@ccn-suzuki-naritada

Description

On iOS, our app crashes intermittently with Il2CppExceptionWrapper inside FirebaseAppCheck.TokenChangedMethod (the [MonoPInvokeCallback] for App Check token changes). The managed exception cannot propagate through the reverse P/Invoke boundary, so IL2CPP calls std::terminate and the app is killed.

We believe the cause is that AppCheckToken.expire_time_millis is never initialized in the iOS token-change notification path:

  1. app_check/src/ios/app_check_ios.mm (firebase-cpp-sdk, same code in v13.11.0 and v14.1.0), appCheckTokenDidChangeNotification: creates a local firebase::app_check::AppCheckToken cpp_token; and only sets cpp_token.token. The TODO there (b/263138261) notes the notification does not contain an expiration time.
  2. AppCheckToken declares int64_t expire_time_millis; without a default member initializer (app_check/src/include/firebase/app_check.h), so the field holds an indeterminate value.
  3. SwigAppCheckListener::OnAppCheckTokenChanged (app_check/src/swig/app_check.i) copies the token as-is and queues CallbackValue1String1<AppCheckToken> to call into C#.
  4. FirebaseAppCheck.TokenChangedMethod calls AppCheckToken.FromAppCheckTokenInternal, which runs s_unixEpoch.AddMilliseconds((double)tokenInternal.expire_time_millis). When the indeterminate value falls outside the range of DateTime (roughly -6.2e13 to +2.5e14 ms from the Unix epoch), AddMilliseconds throws ArgumentOutOfRangeException. This happens before any user handler is called, and nothing catches it, so the app crashes.

This explains why the crash is intermittent: it depends on whatever value happens to be in that memory.

Note: the crash report does not include the managed exception type or message, so ArgumentOutOfRangeException is our inference from reading the source. We have not reproduced the crash locally.

Expected: a token-change notification should never crash the app. Possible fixes we can think of:

  • Initialize expire_time_millis (for example int64_t expire_time_millis = 0;), or set it explicitly in appCheckTokenDidChangeNotification:.
  • Guard the conversion in FromAppCheckTokenInternal, and/or catch exceptions inside TokenChangedMethod so that nothing escapes the reverse P/Invoke callback.

Because the exception is thrown before the user's TokenChanged handler runs, we cannot work around it in app code other than by not subscribing to TokenChanged on iOS.

Reproducing the issue

We cannot reproduce it deterministically, because it depends on uninitialized memory.

  1. On iOS (IL2CPP), set an App Check provider factory (AppAttestProviderFactory in production) before initializing Firebase.
  2. Get FirebaseAppCheck.DefaultInstance, call SetTokenAutoRefreshEnabled(true), and subscribe to TokenChanged.
  3. Call GetAppCheckTokenAsync(false) at startup and keep the app running so that tokens are refreshed.
  4. Occasionally the app crashes in TokenChangedMethod when a token-change notification is delivered.

Firebase Unity SDK Version

13.15.0

Unity editor version

6000.0.58f2

Installation Method

Unity Package Manager

Problematic Firebase Component(s)

App Check

Other Firebase Component(s) in use

Analytics, Authentication, Crashlytics, Messaging

Additional SDKs you are using

YetAnotherHttpHandler (Rust native library), UniTask

Targeted Platform(s)

Apple Platforms

Unity editor platform

Mac

Scripting Runtime

IL2CPP

Release Distribution Type

Pre-built SDK from https://firebase.google.com/download/unity

Relevant Log Output

Fatal Exception: Il2CppExceptionWrapper
Crashed: com.google.firebase.crashlytics.ios.exception
7  UnityFramework   FIRCLSTerminateHandler() + 426 (FIRCLSException.mm:426)
8  libc++abi.dylib  std::__terminate(void (*)()) + 20
9  libc++abi.dylib  __cxa_get_exception_ptr + 34
10 libc++abi.dylib  __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*) + 138
11 UnityFramework   il2cpp::vm::Exception::Rethrow(Il2CppException*) + 116 (Exception.cpp:116)
12 UnityFramework   il2cpp_codegen_get_generic_virtual_method_internal(MethodInfo const*, MethodInfo const*) + 317
13-15 (mis-symbolicated as core::panicking::panic_fmt from a linked Rust library)
16 LivlyIsland      ReversePInvokeWrapper.FirebaseAppCheck_TokenChangedMethod (Firebase.AppCheck.cpp)
17 UnityFramework   firebase::callback::CallbackValue1String1<firebase::app_check::AppCheckToken>::Run() + 3496
18 UnityFramework   firebase::callback::CallbackEntry::Execute() + 1564
19 UnityFramework   firebase::callback::CallbackDispatcher::DispatchCallbacks() + 300
20 UnityFramework   firebase::callback::PollCallbacks() + 48
21-22 (mis-symbolicated as core::panicking::panic_fmt)
23 UnityFramework   il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) + 624
27 UnityFramework   MonoBehaviour::CallUpdateMethod(int) + 598

If using CocoaPods for Apple platforms, the project's Podfile.lock

Not attached. Firebase Unity SDK 13.15.0, which targets Firebase iOS pod 12.17.0 (FIREBASE_IOS_POD_VERSION in cmake/firebase_unity_version.cmake).

Activity

  1. niccolo-hare commented on Oct 30, 2026

    @niccolo-hare

    Hi @ccn-suzuki-naritada, thanks for reaching out. I did some testing, but I'm also unable to reproduce the issue with my sample project. Though, I believe there have been some recent changes with code related to time or timestamps used by the Firebase Unity SDKs, and I'm thinking some of these improvements might be able to resolve the issue. With that, could you try updating your Firebase SDKs to the latest version, then check if that helps mitigate the crash? This will also help us rule out issues related to outdated code.

    Let me know how it goes.

  2. ccn-suzuki-naritada commented on Nov 6, 2026

    @ccn-suzuki-naritada
    Author

    Hi @niccolo-hare, thanks for looking into this.

    As you suggested, we updated to the latest Firebase Unity SDK (14.1.0, together with EDM4U 1.2.190 so that the iOS SDK is resolved via Swift Package Manager) and ran a QA pass on iOS. Unfortunately, the crash still reproduces on 14.1.0, with the same stack trace as before:

    Fatal Exception: Il2CppExceptionWrapper
    Crashed: com.google.firebase.crashlytics.ios.exception
    7  UnityFramework   FIRCLSTerminateHandler() + 426 (FIRCLSException.mm:426)
    8  libc++abi.dylib  std::__terminate(void (*)()) + 20
    11 UnityFramework   il2cpp::vm::Exception::Rethrow(Il2CppException*) + 116 (Exception.cpp:116)
    12 UnityFramework   il2cpp_codegen_get_generic_virtual_method_internal(MethodInfo const*, MethodInfo const*) + 317
    13-15 (mis-symbolicated managed frames)
    16 QA               ReversePInvokeWrapper.FirebaseAppCheck_TokenChangedMethod (Firebase.AppCheck.cpp)
    17 UnityFramework   firebase::callback::CallbackValue1String1<firebase::app_check::AppCheckToken>::Run() + 3704
    18 UnityFramework   firebase::callback::CallbackEntry::Execute() + 1564
    19 UnityFramework   firebase::callback::CallbackDispatcher::DispatchCallbacks() + 300
    20 UnityFramework   firebase::callback::PollCallbacks() + 48
    27 UnityFramework   MonoBehaviour::CallUpdateMethod(int) + 598
    

    This is consistent with the source: the code path from our original report is unchanged in 14.1.0.

    • firebase-cpp-sdk v14.1.0 (the C++ SDK that Unity SDK 14.1.0 is built with), app_check/src/ios/app_check_ios.mm: appCheckTokenDidChangeNotification: still creates firebase::app_check::AppCheckToken cpp_token; and sets only cpp_token.token.
    • app_check/src/include/firebase/app_check.h: int64_t expire_time_millis; still has no initializer.
    • firebase-unity-sdk v14.1.0: AppCheckToken.FromAppCheckTokenInternal still calls s_unixEpoch.AddMilliseconds((double)tokenInternal.expire_time_millis) without a range check, and TokenChangedMethod does not catch exceptions.

    So we don't think the recent time/timestamp changes cover this path.

    Since the crash depends on whatever value is left in uninitialized memory, it may not show up in a small sample project. You should be able to confirm the root cause without a crash by setting a breakpoint after cpp_token.token = ... in appCheckTokenDidChangeNotification: and checking that cpp_token.expire_time_millis holds an arbitrary value.

    This crash is having a serious impact on us. In our production app (still on 13.15.0), more than 3,300 iOS users have experienced it in the last 3 days. Because the exception is thrown inside the SDK before our TokenChanged handler is called, we cannot catch it in app code. The only workaround we have found is to stop subscribing to TokenChanged on iOS, which also means we no longer receive auto-refreshed tokens.

    We would really appreciate it if you could prioritize a fix. Could you let us know:

    1. Whether you can confirm the root cause, and whether a fix is planned (and roughly when it could be released)?
    2. Whether there is a recommended workaround until then? For example, is it safe to stop subscribing to TokenChanged on iOS and call GetAppCheckTokenAsync(false) before each request instead?

    We are happy to provide more information if needed.

  3. niccolo-hare commented on Nov 6, 2026

    @niccolo-hare

    Hi @ccn-suzuki-naritada, thanks for confirming and I'm sorry to hear that the issue persists. Regarding your questions, I think your example workaround would be safe to use for now. But let me raise these details to our engineers for further insights and to help expedite a solution if necessary.

    I'll update you here once I have more information to share, or if I have additional questions.

  4. niccolo-hare commented on Nov 9, 2026

    @niccolo-hare

    Hi @ccn-suzuki-naritada, just wanted to share an update regarding your questions:

    1. I confirmed this with our engineers, and we agree that expire_time_millis seems to be the source of the issue. Since it's uninitialized, it does appear to turn into a garbage value.
    2. The workaround you shared is safe to use, particularly in terms of Firebase related services. Though custom backends might be more likely to encounter issues with this depending on how they're set up. It may be important to note that AppCheck tokens expire after around 1 hour of being issued. So if you are passing information from it to a custom backend, we recommend calling GetAppCheckTokenAsync(false) on each request. This will help ensure you're passing a valid token as it returns a cached one and refreshes it when needed. Doing so shouldn't produce the crash since the call populates expire_time_millis with the correct expiration time.

    Regarding the fix, we're working on an improvement to help mitigate this crash. But I am unable to provide you with a specific timeline or schedule on when it would be released. I would suggest checking out the Firebase Unity SDK release notes for more updates.

    Let me know if you have additional questions or clarifications.

  5. ccn-suzuki-naritada commented on Nov 10, 2026

    @ccn-suzuki-naritada
    Author

    Hi @niccolo-hare, thank you for checking with your engineers and for the detailed update.

    It's very helpful to have the root cause confirmed, and we really appreciate that you're working on a fix for this crash.

    Since we pass the App Check token to our own backend, your note about calling GetAppCheckTokenAsync(false) on each request is especially useful. Based on the information you shared, we'll discuss within our project how to apply this workaround on iOS. We'll also keep an eye on the release notes for the fix.

    Thanks again for your help.

  6. self-assigned this
    on Nov 10, 2026
  7. AustinBenoit commented on Nov 11, 2026

    @AustinBenoit
    Contributor

    Hi all,

    The fix has been merged into the C++ repository. So we should expect this issue to be resolved in the next release. I'll post here again once we have the fixed version published.

    Thanks all,
    --Austin

  8. AustinBenoit commented on Nov 20, 2026

    @AustinBenoit
    Contributor

    Hi @ccn-suzuki-naritada,

    This fix is now published in Firebase Unity SDK v14.2.0 (via Firebase C++ SDK v14.2.0 / firebase/firebase-cpp-sdk#1961). AppCheckToken::expire_time_millis is now default-initialized to 0, and iOS TokenChanged notifications populate it from the JWT exp claim (falling back to 0 if absent or out of range), resolving the ArgumentOutOfRangeException crash in FirebaseAppCheck.TokenChangedMethod.

    Closing this issue now — please upgrade to v14.2.0 and let us know if you still run into any problems!

    Thanks,
    --Austin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions