Skip to content
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

When native code calls a JNI method (Kotlin/Java) and there is a crash in the Kotlin side the crash isn't sent to Sentry #4138

Open
ramib85 opened this issue Feb 4, 2025 · 3 comments

Comments

@ramib85
Copy link

ramib85 commented Feb 4, 2025

Integration

sentry-android

Build System

Gradle

AGP Version

8.7.2

Proguard

Enabled

Version

7.20.0

Steps to Reproduce

  1. You need native code that calls a Java/Kotlin method via JNI.
  2. In the Kotlin/Java method, add something that would crash the app, e.g. throw Throwable("test exception").

A native "abort" crash occurs when this happens and appears in logcat but it isn't sent to Sentry website (or I can't seem to find these crashes).

Example from actual project.

The native code calls this Kotlin function

@Keep
fun handleOnProfileEvent(profileEvent: ProfileEvent) {
    ....
    throw Throwable("ProfileEvent received")
}

Native code in Rust that calls the function above using JNI:

env.call_method(
        *player,
        "handleOnProfileEvent",
        "(Lcom/disneystreaming/nve/player/ProfileEvent;)V",
        &[j_obj.into()]
    )?;

env is defined as env: &'a JNIEnv

Expected Result

A native abort crash is reported in Sentry website.

Actual Result

App crashes but the crash doesn't appear in Sentry website.

@markushi
Copy link
Member

markushi commented Feb 5, 2025

@ramib85 Thanks for bringing this up, we'll have a look at this shortly!

@getsantry getsantry bot removed the status in GitHub Issues with 👀 3 Feb 5, 2025
@markushi markushi self-assigned this Feb 5, 2025
@markushi markushi moved this from Needs Discussion to Needs Investigation in Mobile & Cross Platform SDK Feb 5, 2025
@ramib85
Copy link
Author

ramib85 commented Feb 10, 2025

an example of how such crash looks like in logcat:

Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 17206 (Thread-36), pid 16888 (sney.disneyplus)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  Build fingerprint: 'google/sdk_gphone64_arm64/emu64a:15/AE3A.240806.036/12592187:user/release-keys'
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  Revision: '0'
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  ABI: 'arm64'
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  Timestamp: 2025-02-04 09:26:23.037673105-0500
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  Process uptime: 36s
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  Cmdline: com.disney.disneyplus
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  pid: 16888, tid: 17206, name: Thread-36  >>> com.disney.disneyplus <<<
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  uid: 10207
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  pac_enabled_keys: 000000000000000f (PR_PAC_APIAKEY, PR_PAC_APIBKEY, PR_PAC_APDAKEY, PR_PAC_APDBKEY)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  Abort message: 'JNI DETECTED ERROR IN APPLICATION: JNI GetStaticMethodID called with pending exception com.disneystreaming.nve.player.TestException: ProfileEvent received
                                                                                                      at void com.disneystreaming.nve.player.MediaXPlayer.handleOnProfileEvent(com.disneystreaming.nve.player.ProfileEvent) (MediaXPlayer.kt:1213)
                                                                                                      at boolean com.disneystreaming.nve.player.NativePlayerImpl.nEventProcess(java.lang.Object, java.lang.Object, long) (NativePlayerImpl.kt:-2)
                                                                                                      at void com.disneystreaming.nve.player.ServiceThread.run() (ServiceThread.kt:40)
                                                                                                    
                                                                                                        in call to GetStaticMethodID
                                                                                                        from boolean com.disneystreaming.nve.player.NativePlayerImpl.nEventProcess(java.lang.Object, java.lang.Object, long)'
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x0  0000000000000000  x1  0000000000004336  x2  0000000000000006  x3  0000006c3cbf6270
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x4  fefefefefefefeff  x5  fefefefefefefeff  x6  fefefefefefefeff  x7  7f7f7f7f7f7f7f7f
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x8  00000000000000f0  x9  000000709429c468  x10 ffffff80fffffb9f  x11 0000000000000001
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x12 0000006c3cbf4fb0  x13 0000000000000316  x14 0000006c3cbf5060  x15 000002c738614202
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x16 000000709434bff8  x17 00000070943361c0  x18 0000006c364c8000  x19 00000000000041f8
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x20 0000000000004336  x21 00000000ffffffff  x22 0000000000000000  x23 0000006c3cbf8a80
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x24 00000000000002af  x25 0000006c3cbf8a80  x26 0000000000000000  x27 b400006f7433eed0
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      x28 0000006df1c920a4  x29 0000006c3cbf62f0
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A      lr  00000070942d46a4  sp  0000006c3cbf6250  pc  00000070942d46d4  pst 0000000000001000
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  32 total frames
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A  backtrace:
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #00 pc 000000000005b6d4  /apex/com.android.runtime/lib64/bionic/libc.so (abort+168) (BuildId: 1b9fecf834d610f77e641f026ca7269b)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #01 pc 000000000092f3fc  /apex/com.android.art/lib64/libart.so (art::Runtime::Abort(char const*)+344) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #02 pc 00000000000160fc  /apex/com.android.art/lib64/libbase.so (android::base::SetAborter(std::__1::function<void (char const*)>&&)::$_0::__invoke(char const*)+80) (BuildId: 42d41ca7c77853791d096606e7186547)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #03 pc 00000000000156d0  /apex/com.android.art/lib64/libbase.so (android::base::LogMessage::~LogMessage()+516) (BuildId: 42d41ca7c77853791d096606e7186547)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #04 pc 00000000003b7e88  /apex/com.android.art/lib64/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+1696) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #05 pc 000000000088a304  /apex/com.android.art/lib64/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, std::__va_list)+108) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #06 pc 00000000005a590c  /apex/com.android.art/lib64/libart.so (art::(anonymous namespace)::ScopedCheck::AbortF(char const*, ...) (.__uniq.99033978352804627313491551960229047428)+140) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #07 pc 00000000005a07b0  /apex/com.android.art/lib64/libart.so (art::(anonymous namespace)::ScopedCheck::Check(art::ScopedObjectAccess&, bool, char const*, art::(anonymous namespace)::JniValueType*) (.__uniq.99033978352804627313491551960229047428)+5440) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #08 pc 000000000059acac  /apex/com.android.art/lib64/libart.so (art::(anonymous namespace)::CheckJNI::GetMethodIDInternal(char const*, _JNIEnv*, _jclass*, char const*, char const*, bool) (.__uniq.99033978352804627313491551960229047428)+212) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #09 pc 00000000007be09c  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libnve_ffi.so (log_message+340) (BuildId: 1428fa52ede2475a17202c937f7ccd82cd0197d3)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #10 pc 000000000041d604  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libnve_ffi.so (send_log_to_adk(int, char const*, ...)+272) (BuildId: 1428fa52ede2475a17202c937f7ccd82cd0197d3)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #11 pc 000000000041c3fc  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libnve_ffi.so (output(LogItem*)+200) (BuildId: 1428fa52ede2475a17202c937f7ccd82cd0197d3)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #12 pc 000000000041be18  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libnve_ffi.so (nve_log+200) (BuildId: 1428fa52ede2475a17202c937f7ccd82cd0197d3)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #13 pc 00000000003e75dc  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libnve_ffi.so (nve_ffi_nve_log+544) (BuildId: 1428fa52ede2475a17202c937f7ccd82cd0197d3)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #14 pc 00000000003a7b5c  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libjni_mediax_player.so
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #15 pc 00000000002ed648  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libjni_mediax_player.so
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #16 pc 00000000001f04a4  /data/app/~~SYBmNRRoBqKJB_c9mw03kA==/com.disney.disneyplus-BIKGABLcLs6bL2F4jOvr0g==/lib/arm64/libjni_mediax_player.so (Java_com_disneystreaming_nve_player_NativePlayerImpl_nEventProcess+5232)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #17 pc 0000000000378f70  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+144) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #18 pc 0000000000362774  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+612) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #19 pc 000000000035bd1c  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+1928) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #20 pc 000000000076da48  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+12208) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #21 pc 000000000037b5d8  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #22 pc 000000000009fcb0  <anonymous:6d623df000> (com.disneystreaming.nve.player.ServiceThread.run+0)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #23 pc 000000000034d5a8  /apex/com.android.art/lib64/libart.so (artQuickToInterpreterBridge+1932) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #24 pc 0000000000379098  /apex/com.android.art/lib64/libart.so (art_quick_to_interpreter_bridge+88) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #25 pc 0000000000362774  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+612) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #26 pc 000000000034def0  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+132) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #27 pc 00000000009430d8  /apex/com.android.art/lib64/libart.so (art::detail::ShortyTraits<(char)86>::Type art::ArtMethod::InvokeInstance<(char)86>(art::Thread*, art::ObjPtr<art::mirror::Object>, art::detail::ShortyTraits<>::Type...)+60) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #28 pc 000000000063e694  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback(void*)+1344) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #29 pc 000000000063e144  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallbackWithUffdGc(void*)+8) (BuildId: dcb9fe2b5c99aa3f1a682a6008427d08)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #30 pc 000000000006c354  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start(void*)+196) (BuildId: 1b9fecf834d610f77e641f026ca7269b)
2025-02-04 09:26:23.390 17300-17300 DEBUG                   crash_dump64                         A        #31 pc 000000000005efc4  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+64) (BuildId: 1b9fecf834d610f77e641f026ca7269b)        

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Feb 10, 2025
@markushi markushi moved this from Needs Investigation to In Progress in Mobile & Cross Platform SDK Feb 25, 2025
@markushi
Copy link
Member

markushi commented Feb 26, 2025

Hey everyone, thanks for all the details! I finally had some time to look into this and it seems that our SDK is behaving as expected. Having said that, a few notes:

If the following code gets executed, and the javaCrash method throws an error, it will be reported as a Java crash after your C++ code finished executing.

JNIEXPORT void JNICALL
Java_io_sentry_samples_android_example(JNIEnv *env, jclass cls) {
    jmethodID javaCrashMethod = env->GetStaticMethodID(cls, "javaCrash", "()V");
    env->CallStaticVoidMethod(cls, javaCrashMethod); // Java exception is marked as pending
} // Java's UncaughtExceptionHandler will be called after execution 

However if your C++ code performs other Java calls after a Java error was thrown previously, you'll get a native C++ crash, as you're not supposed to make certain JNI calls if your Java layer has a pending exception

JNIEXPORT void JNICALL
Java_io_sentry_samples_android_example(JNIEnv *env, jclass cls) {
    jmethodID javaCrashMethod = env->GetStaticMethodID(cls, "javaCrash", "()V");
    env->CallStaticVoidMethod(cls, javaCrashMethod); // Java exception is marked as pending

    // Causes native ABORT crash, as it's not allowed to call GetStaticMethodID while an exception is pending
    env->GetStaticMethodID(cls, "javaCrash", "()V");
}

It's worth noting that native crashes will be reported to sentry.io on the next app start only, for Java crashes the Sentry SDK will try to report them immediatelly.

There are different ways around this, see the Android docs for more details.

@getsantry getsantry bot moved this to Waiting for: Community in GitHub Issues with 👀 3 Feb 26, 2025
@kahest kahest moved this from In Progress to Needs More Information in Mobile & Cross Platform SDK Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for: Community
Status: Needs More Information
Development

No branches or pull requests

3 participants