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

OpenCVLoader.initDebug() causes SIGSEGV in libart.so between onPause and onResume in debug sessions (simple repro included) #22515

Open
4 tasks done
andynewman10 opened this issue Sep 14, 2022 · 3 comments
Assignees
Labels
bug needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc) platform: android

Comments

@andynewman10
Copy link

andynewman10 commented Sep 14, 2022

System information (version)
  • OpenCV => 4.6.0
  • Operating System / Platform => Android 7.1.1 (Sony Xperia Z5 Compact)
  • Compiler => Android Studio 2021.2.1 Patch 2 on Windows 11
Detailed description

Calling OpenCVLoader.initDebug() once at the beginning of an application succeeds, but a SIGSEGV will subsequently be raised in libart.so everytime a screen rotation occurs. The SIGSEGV will be raised in between onPause and onResume calls for the fragment.

Steps to reproduce
  • Open Android Studio 2021.2.1 Patch 2

  • File > New > New Project... > Phone and Tablet > select 'Basic Activity'. Make sure the language is set to Java.

  • Download OpenCV 4.6.0 (latest release) from sourceforget.net and unzip the 'sdk' directory next to the settings.gradle file (also next to the 'app' folder of the application) as a folder named 'opencv' (not 'sdk')

  • File > New > Import Module... > source directory : the 'opencv' directory mentionned above

  • Give a name to the opencv module, for instance 'opencv', and finish the import procedure. We now have two projects in the IDE, :app and :opencv

  • Open settings.gradle and reverse the order of these two lines (for some reason, they are in the wrong order when the project is imported):

    project(':opencv').projectDir = new File(rootDir, 'opencv/')
    include 'opencv'
    
  • get rid of the kotlin extension declaration, as the project is written in Java

  • Build the project and launch a debug session on a Sony Xperia Z5 Compact (E5823) device with Android 7.1.1 Nougat. The application is launched successfully

  • The first fragment shows up, now rotate the screen, everything is fine.

  • Exit the application

  • Override FirstFragment's onCreate method by adding this code:

    @Override
    public void onCreate(Bundle b) {
      super.onCreate(b);
      boolean success = OpenCVLoader.initDebug();
    }
    
  • Following this edit, the IDE complains OpenCVLoader is not a recognized class - follow the suggestions to import the '........._main' module and then do 'Import class'. Now the class is recognized and everything compiles fine.

  • Launch a new debug session

  • 'success' will be true, above, as can be seen with the debugger if need be

  • Now rotate the device's screen

  • A SIGSEGV occurs after onPause() has been called., and the debugger is entered

    SIGSEGV (signal SIGSEGV: invalid address (fault address: 0x0))
    libart.so: art_sigsegv_fault
    
    <unknown> 0x0000007f92a99904
    art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*) 0x0000007fb0cba304
    art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::DexFile::CodeItem const*, art::ShadowFrame*, art::JValue*) 0x0000007fb0e6a5b0
    bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e6358c
    bool art::interpreter::DoInvoke<(art::InvokeType)2, false, false>(art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e92fdc
    art::JValue art::interpreter::ExecuteSwitchImpl<false, false>(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame&, art::JValue, bool) 0x0000007fb0e86cb4
    art::interpreter::Execute(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame&, art::JValue, bool) 0x0000007fb0e3c950
    art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame*, art::JValue*) 0x0000007fb0e42648
    bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e63570
    bool art::interpreter::DoInvokeVirtualQuick<false>(art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*) 0x0000007fb0e80b74
    art::JValue art::interpreter::ExecuteSwitchImpl<false, false>(art::Thread*, art::DexFile::CodeItem const*, art::ShadowFrame&, art::JValue, bool) 0x0000007fb0e87938
    ...
    [lots of lines]
    ...
    art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list) 0x0000007fb1004d14
    art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list) 0x0000007fb0f20b00
    <unknown> 0x0000007fb3305278
    android::AndroidRuntime::start(char const*, android::Vector<android::String8> const&, bool) 0x0000007fb3307890
    <unknown> 0x0000005570287264
    __libc_init 0x0000007fb2bbf89c
    
  • Carrying execution with the Continue button works fine (surprisingly ?)

  • onResume() is called and execution resumes normally.

Any subsequent screen rotation will result in a SIGSEGV.

This is very annoying when debugging on the device, which I always prefer.

At the beginning, I thought it was a problem with my code, so I posted a message on stackoverflow:

SIGSEGV libart.so exception only occuring on one computer when debugging, not on the other

Because I managed to build a simple repro, I am posting this issue here.

Surprisingly, there is one computer on which I don't experience the problem, but it is an older Android Studio version (2021.2.1 with no patch).

Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc
@asmorkalov asmorkalov added bug platform: android needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc) labels Sep 15, 2022
@andynewman10
Copy link
Author

andynewman10 commented Sep 15, 2022

I have tried with OpenCV for Android versions 4.5.5 and 4.5.0, and the same problem remains.

Reinstalling Android Studio from scratch doesn't work either.

(in the instructions, make sure target: Android 5.0 Lollipop is specified when creating the application)

EDIT: I do confirm this repro also works with an emulator, with a fresh install of Android Studio.

I tried with OpenCV 4.5.0.
I created a Pixel device, and installed Nougat (API level 25 = Android 7.1 API) on it.

Just launch a debug session and the emulator will also raise a SIGSEGV exception (art_sigsegv_fault) when rotating the device.

@andynewman10
Copy link
Author

I made some progress with the investigation

  • on Android Studio Chipmunk 2021.2.1 Patch 2 and Android Studio Dolphin 2021.3.1 (August 2022), I observe the above mentionned behavior
  • on an older Android Studio Chipmunk 2021.2.1 "non-patched" version, a SIGSEGV is not raised on Android 7.1.1.

This is a bit weird no SIGSEGV is raised with Chipmunk 2021.2.1 "original" version, but given than any newer version seems to exhibit the problem, I prefer to ignore this piece of information.

Now doing all tests on Android Studio Dolphin 2021.3.1, here is a summary for the issue on various Google devices/OS versions (OK means there is no SIGSEGV raised):

  • Nexus 4 with Android 5.0 : OK
  • Nexus 4 with Android 6.0 : OK
  • Nexus 4 with Android 7.0 : SIGSEGV (*)
  • Pixel with Android 7.0 : SIGSEGV (*)
  • Pixel with Android 7.1.1 : SIGSEGV
  • Pixel with Android 8.0 : SIGSEGV (on this version, a SIGSEGV is also raised right at application startup)
  • Pixel with Android 8.1 : SIGSEGV
  • Pixel with Android 9.0 : OK

Conclusion: it seems that when running on Android 7.0, 7.1.1, 8.0 or 8.1, a SIGSEGV is raised, while nothing is raised when running on Android 5.0, 6.0 or 9.0. Weird!
These two lines (*) suggest the problem is not device-specific, just OS-specific.

@andynewman10
Copy link
Author

It is possible to get rid of this problem by placing a breakpoint (red diamond) on the following line in StaticHelper.java:

private static native String getLibraryList();

This is the last line of code for this file. I ignore why but, when you set a breakpoint on this line, the problem goes away! Anybody has a clue as to why this is working?

Note that 'getLibraryList' is shown in red by Android Studio as it (indeed) is an unresolved external, and the IDE detects that. However, it is never called because the following line of code

 System.loadLibrary("opencv_info");

fails. As a result getLibraryList() is never called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs investigation Collect and attach more details (build flags, stacktraces, input dumps, etc) platform: android
Projects
None yet
Development

No branches or pull requests

3 participants