3

after I upgraded my Android-JNI project to cmake buildsystem I always receive a crash (SIGSEGV) when my c++ backend throws an std:: exception. This only happens on Huawei phones.

I could not rebuild the problem in a minimal example.

Here are the building specs:

  • Android SDK Build-Tools: 25.0.2, 26.0.2
  • Android SDK Platform-Tools: 26.0.1
  • Android SDK Tools: 26.1.1
  • CMake: 3.6.4111459
  • NDK: 15.2.4203891

Gradle: (also tried with '-DANDROID_TOOLCHAIN=gcc')

externalNativeBuild {
    cmake {
        cppFlags "-frtti -fexceptions -pthread -v -std=c++11"
        arguments '-DANDROID_PLATFORM=android-9', '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_shared'
    }
}
ndk {
    abiFilters 'armeabi'
}

The Signal is: SIGSEGV (signal SIGSEGV: invalid address (fault address: 0x7))

With the following stacktrace:

unw_get_reg
_Unwind_VRS_Interpret
__gnu_Unwind_RaiseException
___Unwind_RaiseException
__cxxabiv1::__cxa_throw(void *, std::type_info *, void (*)(void *))
testTryCatch()
Java_de_company_project_wrapper_SystemWrapper_startApplication
art_quick_generic_jni_trampoline
art_quick_invoke_stub_internal
art_quick_invoke_stub

This is literally the first function which gets called in my backend:

#include <exception>
#include <android/log.h>

void testTryCatch() {
    try {
        throw std::exception();
    }catch(std::exception &e){
        __android_log_write(ANDROID_LOG_INFO, "testException", "done");
    }
}

JNIEXPORT void JNICALL
Java_de_company_project_wrapper_SystemWrapper_startApplication(JNIEnv *env, 
    jclass obj)
{
    testTryCatch();
}

This happens in a big project, the c/c++ library results in about 16MB. There are other libraries statically linked in (OpenSSL/FFmpeg/opus/zip).

So my question is how to resolve this problem and why does the library crash on throwing std:: exception only appears on Huawei phones (after upgrade to cmake buildsystem)?

(btw: getting rid of all std:: exceptions is not a good idea)

Da Maex
  • 481
  • 7
  • 27
  • Based on the stack trace, the first function you've shown has nothing to do with it - different func names, just the same exception class. Is this code called, or does it crash before? Maybe it crashes on library initialization? If so, can you keep removing libraries and see which one is causing the error? – hauron Oct 23 '17 at 12:44
  • Changed the stack! The crash appears on the throw inside the testTryCatch() method. I'm doing the library removing one-by-one right now, this takes a lot of time. Will report back. – Da Maex Oct 23 '17 at 12:57
  • I removed all the external libraries but still got the same problem. I'm searching now in the c++ sources if somewhere a memory corruption appears. Still: why only on Huawei?!? – Da Maex Oct 23 '17 at 14:15
  • 1
    At least you now know it's not the extra libraries. I'd check system ones. See here: https://developer.android.com/ndk/guides/cpp-support.html Could be related to: '-DANDROID_STL=gnustl_shared' - mayhaps you need a different implementation? – hauron Oct 23 '17 at 22:09
  • The problem was indeed the gnustl_shared, changed to c++_shared and it works. – Da Maex Oct 25 '17 at 13:21

2 Answers2

3

I found the solution to my problem with the help seen in the comments. It seems that Huawei has problems with the gnustl_shared library when the library itself get's to big in size. So i changed my externalNativeBuild to c++_shared accordingly.

externalNativeBuild {
    cmake {
        cppFlags "-pthread -v -std=c++11"
        arguments '-DANDROID_PLATFORM=android-9', '-DANDROID_CPP_FEATURES=rtti exceptions',
                  '-DANDROID_STL=c++_shared', '-DANDROID_TOOLCHAIN=clang'
    }
}
Da Maex
  • 481
  • 7
  • 27
  • I had exactly the same problem, but when using your solution the screen gets black and the app freezes and even does not crash.Can you suggest any other solution? – user3406222 Feb 18 '19 at 11:53
  • Can you get a little more specific on the versions u use (platform, toolchain, SDK, NDK, CMake) and is it only on Huawei devices? – Da Maex Feb 26 '19 at 11:00
1

I meet same problem, stack is like:

backtrace:
  #00 pc 00056dfe  /apex/com.android.runtime/lib/bionic/libc.so (abort+166) (BuildId: b1803e2c54cf63f48664b8839ccf313b)
  #01 pc 00014daf  /data/app/org.swig.cppexception-TAwOh_fwpqXbW-uy7twUGg==/lib/arm/libnative-lib.so (BuildId: 030a2de7aefd84866dea1b48f592d7fdad70db2d)
  #02 pc 00014851  /data/app/org.swig.cppexception-TAwOh_fwpqXbW-uy7twUGg==/lib/arm/libnative-lib.so (BuildId: 030a2de7aefd84866dea1b48f592d7fdad70db2d)
  #03 pc 000146a9  /data/app/org.swig.cppexception-TAwOh_fwpqXbW-uy7twUGg==/lib/arm/libnative-lib.so (__gxx_personality_v0+100) (BuildId: 030a2de7aefd84866dea1b48f592d7fdad70db2d)
  #04 pc 001bd04c  /data/app/org.swig.cppexception-TAwOh_fwpqXbW-uy7twUGg==/lib/arm/libffmpeg.so (__gnu_Unwind_RaiseException+108)
  #05 pc 001bdb90  /data/app/org.swig.cppexception-TAwOh_fwpqXbW-uy7twUGg==/lib/arm/libffmpeg.so (___Unwind_RaiseException+20)

after search the world, I found it's caused by cpp throw exception. I have catched the cpp exception, but it still crashed.

'-DANDROID_STL=c++_static' -> '-DANDROID_STL=c++_shared'

above modify saved me. the cpp exception can be catched now.
But I still found who caused this?

bluesky
  • 145
  • 7