Search code examples
androidc++exceptionjava-native-interfacehuawei-mobile-services

std::exception SIGSEGV on Huawei JNI


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)


Solution

  • 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'
        }
    }