Skip to content

SIGSEGV on ARMv7 upon loading dynamic library #14906

Closed

Description

@Palladinium
System information (version)
  • OpenCV => 4.1.0 (master b95e93c)
  • Operating System / Platform => Android 7.0 armeabi-v7a (Samsung Galaxy J5 Pro)
  • Compiler => Android NDK 20
Detailed description

I am using the static libraries to produce a dynamic library which is then used by an Android app.
Upon first calling into my dynamic library, I experience the following crash (some information removed):

2019-06-27 16:12:14.463 10074-10074 A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0xfffffff4 in tid 10074
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG: Revision: '7'
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG: ABI: 'arm'
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG: pid: 10074, tid: 10074
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xfffffff4
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG:     r0 00000000  r1 b8a78d34  r2 00000069  r3 00000001
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG:     r4 ffaf9748  r5 b9aadbb0  r6 b8873c2c  r7 ffaf9738
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG:     r8 00000004  r9 00000069  sl e8553008  fp e96ed8d0
2019-06-27 16:12:14.543 10312-10312/? A/DEBUG:     ip 00000071  sp ffaf9730  lr b81b8901  pc b81b5c1a  cpsr 600d0030

Running in debug mode and running the stack trace through ndk-stack, I get this:

#00 0x000d7c1a /data/app/myapp.debug-1/lib/arm/libmylib.so
std::__ndk1::basic_ostream<char, std::__ndk1::char_traits<char> >::sentry::sentry(std::__ndk1::basic_ostream<char, std::__ndk1::char_traits<char> >&)
/opt/android-ndk/sources/cxx-stl/llvm-libc++/include/ostream:263:9
#01 0x000da8fd /data/app/myapp.debug-1/lib/arm/libmylib.so
std::__ndk1::basic_ostream<char, std::__ndk1::char_traits<char> >& std::__ndk1::__put_character_sequence<char, std::__ndk1::char_traits<char> >(std::__ndk1::basic_ostream<char, std::__ndk1::char_traits<char> >&, char const*, unsigned int)
/opt/android-ndk/sources/cxx-stl/llvm-libc++/include/ostream:722:57
#02 0x00340e07 /data/app/myapp.debug-1/lib/arm/libmylib.so (_ZN2cv5utils7logging8internal15writeLogMessageENS1_8LogLevelEPKc+614)
                                                                  cv::utils::logging::internal::writeLogMessage(cv::utils::logging::LogLevel, char const*)
                                                                  /home/patrick/build/opencv/build/o4a/../../modules/core/src/logger.cpp:213:12
#03 0x00340ff9 /data/app/myapp.debug-1/lib/arm/libmylib.so (_ZN2cv5utils7logging8internal17writeLogMessageExENS1_8LogLevelEPKcS5_iS5_S5_+284)
                                                                  cv::utils::logging::internal::writeLogMessageEx(cv::utils::logging::LogLevel, char const*, char const*, int, char const*, char const*)
                                                                  /home/patrick/build/opencv/build/o4a/../../modules/core/src/logger.cpp:238:5
#04 0x00420eb1 /data/app/myapp.debug-1/lib/arm/libmylib.so
cv::HWFeatures::initialize()
/home/patrick/build/opencv/build/o4a/../../modules/core/src/system.cpp:508:9
#05 0x0041e201 /data/app/myapp.debug-1/lib/arm/libmylib.so
cv::HWFeatures::HWFeatures(bool)
/home/patrick/build/opencv/build/o4a/../../modules/core/src/system.cpp:328:13
#06 0x000ba8c3 /data/app/myapp.debug-1/lib/arm/libmylib.so
__cxx_global_var_init.9
/home/patrick/build/opencv/build/o4a/../../modules/core/src/system.cpp:690:20
#07 0x000ba8ed /data/app/myapp.debug-1/lib/arm/libmylib.so
_GLOBAL__sub_I_system.cpp
/home/patrick/build/opencv/build/o4a/../../modules/core/src/system.cpp:0:0
#08 0x00006065 /system/bin/linker (__dl__ZN6soinfo13call_functionEPKcPFvvE+84)
#09 0x00005f85 /system/bin/linker (__dl__ZN6soinfo10call_arrayEPKcPPFvvEjb+184)
#10 0x00004c6f /system/bin/linker (__dl__ZN6soinfo17call_constructorsEv+218)
#11 0x00004a83 /system/bin/linker (__dl__Z9do_dlopenPKciPK17android_dlextinfoPv+406)
#12 0x00002c65 /system/bin/linker (__dl__ZL10dlopen_extPKciPK17android_dlextinfoPv+28)
#13 0x0000328f /system/lib/libnativeloader.so (_ZN7android17OpenNativeLibraryEP7_JNIEnviPKcP8_jobjectP8_jstring+94)
#14 0x0023bd03 /system/lib/libart.so (_ZN3art9JavaVMExt17LoadNativeLibraryEP7_JNIEnvRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEP8_jobjectP8_jstringPS9_+818)
#15 0x0000315f /system/lib/libopenjdkjvm.so (JVM_NativeLoad+178)
#16 0x005e018d /system/framework/arm/boot.oat (offset 0x56f000) (java.lang.Runtime.nativeLoad+144)
#17 0x000a9341 /system/lib/libart.so (art_quick_invoke_stub_internal+64)
#18 0x00407c41 /system/lib/libart.so (art_quick_invoke_static_stub+228)
#19 0x000b065f /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+182)
#20 0x001ed9e3 /system/lib/libart.so (_ZN3art11interpreter34ArtInterpreterToCompiledCodeBridgeEPNS_6ThreadEPNS_9ArtMethodEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+198)
#21 0x001e7f83 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+478)
#22 0x00210bd9 /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE0ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+240)
#23 0x0020554d /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+16372)
#24 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#25 0x001d084f /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
#26 0x001e7f67 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
#27 0x0020fd89 /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE1ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+272)
#28 0x00206911 /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+21432)
#29 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#30 0x001d084f /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
#31 0x001e7f67 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
#32 0x0020e75b /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+430)
#33 0x002047c1 /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
#34 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#35 0x001d084f /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
#36 0x001e7f67 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
#37 0x00210bd9 /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE0ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+240)
#38 0x0020554d /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+16372)
#39 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#40 0x001d07b9 /system/lib/libart.so (_ZN3art11interpreter30EnterInterpreterFromEntryPointEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameE+92)
#41 0x003f6fbd /system/lib/libart.so (artQuickToInterpreterBridge+716)
#42 0x000add93 /system/lib/libart.so (art_quick_to_interpreter_bridge+34)
#43 0x000a9341 /system/lib/libart.so (art_quick_invoke_stub_internal+64)
#44 0x00407c41 /system/lib/libart.so (art_quick_invoke_static_stub+228)
#45 0x000b065f /system/lib/libart.so (_ZN3art9ArtMethod6InvokeEPNS_6ThreadEPjjPNS_6JValueEPKc+182)
#46 0x000eff4d /system/lib/libart.so (_ZN3art11ClassLinker15InitializeClassEPNS_6ThreadENS_6HandleINS_6mirror5ClassEEEbb+3036)
#47 0x000db255 /system/lib/libart.so (_ZN3art11ClassLinker17EnsureInitializedEPNS_6ThreadENS_6HandleINS_6mirror5ClassEEEbb+120)
#48 0x0020c715 /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+45500)
#49 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#50 0x001d084f /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
#51 0x001e7f67 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
#52 0x0020fd89 /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE1ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+272)
#53 0x00206911 /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+21432)
#54 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#55 0x001d084f /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
#56 0x001e7f67 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
#57 0x0020e75b /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+430)
#58 0x002047c1 /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)
#59 0x001cbded /system/lib/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameENS_6JValueEb+368)
#60 0x001d084f /system/lib/libart.so (_ZN3art11interpreter33ArtInterpreterToInterpreterBridgeEPNS_6ThreadEPKNS_7DexFile8CodeItemEPNS_11ShadowFrameEPNS_6JValueE+114)
#61 0x001e7f67 /system/lib/libart.so (_ZN3art11interpreter6DoCallILb0ELb0EEEbPNS_9ArtMethodEPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+450)
#62 0x0020e75b /system/lib/libart.so (_ZN3art11interpreterL8DoInvokeILNS_10InvokeTypeE2ELb0ELb0EEEbPNS_6ThreadERNS_11ShadowFrameEPKNS_11InstructionEtPNS_6JValueE+430)
#63 0x002047c1 /system/lib/libart.so (_ZN3art11interpreter17ExecuteSwitchImplILb0ELb0EEENS_6JValueEPNS_6ThreadEPKNS_7DexFile8CodeItemERNS_11ShadowFrameES2_b+12904)

The app works fine on other architectures.

Steps to reproduce

Load a dynamic library (.so) that links against the OpenCV static libraries for armeabi-v7a.

Activity

  1. alalek commented on Jun 27, 2019

    @alalek
    Member

    libmylib.so

    Could you try to rebuild both OpenCV and your app with:

    -DANDROID_STL=c++_shared

    Details: https://developer.android.com/ndk/guides/cpp-support#important_considerations


    Config should look like that:

    # ndk-20-shared.config.py
    ABIs = [
        ABI("2", "armeabi-v7a", None, 21, cmake_vars=dict(ANDROID_ABI='armeabi-v7a with NEON', 'ANDROID_STL'='c++_shared'))
    ]
    

    Build with parameter:

    build_sdk --config ndk-20-shared.config.py
    
  2. kinchungwong commented on Jun 29, 2019

    @kinchungwong
    Contributor

    @Palladinium

    The stack trace involves both existing code (cv::HWFeatures::HWFeatures(bool)) and code that was added in a recent release of OpenCV in PR #13909 that I contributed (cv::utils::logging::internal::writeLogMessageEx(cv::utils::logging::LogLevel, char const*, char const*, int, char const*, char const*)).

    The fault address, 0xfffffff4, as seen in
    Fatal signal 11 (SIGSEGV), code 1, fault addr 0xfffffff4 in tid 10074
    indicates a negative-offset low-address null pointer dereference. However, more information is needed to determine how that null pointer got into the call chain.

    The relevant code is near this location:

    void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message)

    Without further information, these are some educated guesses:

    • One or more of the input arguments to writeLogMessageEx were invalid (not exactly null, but a "low address null pointer dereference", such 0x000000?? or 0xffffff??.
    • The input argument const char* message was null. The current code does not explicitly check the nullness of this argument.
    • Any (zero, one, or more) of the C++ I/O Stream library objects (such as std::ostringstream, std::cout, std::cerr) might have been not fully initialized yet, because the program is still in the process of static initialization. This is corroborated by the crash being reported from std::basic_ostream::sentry, which calls the basic_ostream::good() method as part of its status check.
    • Bugs inside writeLogMessage or writeLogMessageEx.
    • The issue raised by @alalek, namely compatibility issues caused by static linking of C++ runtime library, as mentioned in the link https://developer.android.com/ndk/guides/cpp-support#important_considerations

    Currently I am fully occupied due to some family emergency issue and unable to spend time on this issue. I hope the above information helps.

  3. Palladinium commented on Jul 4, 2019

    @Palladinium
    Author

    @alalek I originally tried rebuilding with ANDROID_STL=c++_shared and it didn't help, but upon trying again it seems to have worked - I must have gotten something wrong on my first attempt.

  4. michael-destiny commented on Jun 10, 2020

    @michael-destiny

    @alalek this issue also exist in opencv 4.3.0, will this be merged to 4.3.0 or future versions?

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions