Bhupendra Singh
Bhupendra Singh

Reputation: 11

native crash under ART when calling attachCurrentThread

We have an application where most of my code is written in C++. We are using JNI interface. The event callbacks are passed to java using this JNI interface. For every event call back, we get JNIEnv and then call the AttachCurrentThread for that env. Following is the piece of code which does this:

CJniEnvUtil::CJniEnvUtil(JavaVM *pvm)
: m_fNeedDetach(false)
, mJavaVM(pvm)
, m_pEnv(NULL)
{
    switch (mJavaVM->GetEnv((void**)&m_pEnv, JNI_VERSION_1_6)) { 
        case JNI_OK: break; 
        case JNI_EDETACHED: 
            if (mJavaVM->AttachCurrentThread(&m_pEnv, NULL) != 0) { 
                break;
            } 
            m_fNeedDetach = true;
           break; 
        case JNI_EVERSION: 
            break;
    }
}

CJniEnvUtil::~CJniEnvUtil()
{
    if (m_fNeedDetach && m_pEnv) 
        mJavaVM->DetachCurrentThread(); 
}

So the idea is, for every event callback we create instance of this CJniEnvUtil which in turn gets env for the current thread and attaches current thread. The thread this detached when this CJniUtil object is destructor. On Dalvic, this piece of code works perfectly fine, but on ART (Android Run Time) it is crashing (Inface a sanity fails). Following is the complete stack trace from logcat.

A/art﹕ art/runtime/thread.cc:468] Check failed: &stack_variable > reinterpret_cast<void*>          (stack_end_) (&stack_variable=0x4cd061b0, reinterpret_cast<void*>(stack_end_)=0x50014000)
I/AudioFlinger﹕ BUFFER TIMEOUT: remove(4098) from active list on thread 0xb5e81008
A/art﹕ art/runtime/runtime.cc:203] Runtime aborting...
A/art﹕ art/runtime/runtime.cc:203] Aborting thread:
A/art﹕ art/runtime/runtime.cc:203] "<native thread without managed peer>" prio=5 tid=27 Runnable (still starting up)
A/art﹕ art/runtime/runtime.cc:203]   | group="" sCount=0 dsCount=0 obj=0x00000000 self=0x497e4830
A/art﹕ art/runtime/runtime.cc:203]   | sysTid=10564 nice=0 cgrp=apps sched=0/0 handle=0x4f2e9cd0
A/art﹕ art/runtime/runtime.cc:203]   | state=R schedstat=( 3985475838 2078977182 16206 ) utm=335 stm=63 core=0 HZ=100
A/art﹕ art/runtime/runtime.cc:203]   | stack=0x50010000-0x50014000 stackSize=1016KB
A/art﹕ art/runtime/runtime.cc:203]   native: art::Thread::DumpStack(std::ostream&) const+87         
A/art﹕ art/runtime/runtime.cc:203]   native: art::Runtime::Abort()+79 [0x41634974] (libart.so)
A/art﹕ art/runtime/runtime.cc:203]   native: art::LogMessage::~LogMessage()+505 [0x414e193a] (libart.so)
A/art﹕ art/runtime/runtime.cc:203]   native: art::Thread::InitStackHwm()+849 [0x4163da9a] (libart.so)
A/art﹕ art/runtime/runtime.cc:203]   native: art::Thread::Init(art::ThreadList*,      art::JavaVMExt*)+499 [0x4163dd78] (libart.so)
A/art﹕ art/runtime/runtime.cc:203]   native: art::Thread::Attach(char const*, bool, _jobject*,  bool)+103 [0x41645250] (libart.so)
A/art﹕ art/runtime/runtime.cc:203]   native: art::Runtime::AttachCurrentThread(char const*,  bool, _jobject*, bool)+15 [0x41632b38] (libart.so)

As the first line shows, the sanity check failing.

I would really appreciate if someone can suggest something on this. I have to make application running on Android 5.0, which has ART as default runtime.

Upvotes: 1

Views: 3041

Answers (1)

Zhong Mingliang
Zhong Mingliang

Reputation: 1

Dalvik had separate stacks for native and Java code, with a default Java stack size of 32KB and a default native stack size of 1MB. ART has a unified stack for better locality. Ordinarily, the ART Thread stack size should be approximately the same as for Dalvik. However, if you explicitly set stack sizes, you may need to revisit those values for apps running in ART.

In Java, review calls to the Thread constructor that specify an explicit stack size. For example, you will need to increase the size if StackOverflowError occurs. In C/C++, review use of pthread_attr_setstack() and pthread_attr_setstacksize() for threads that also run Java code via JNI. Here is an example of the error logged when an app attempts to call JNI AttachCurrentThread() when the pthread size is too small: F/art: art/runtime/thread.cc:435] Attempt to attach a thread with a too-small stack (16384 bytes)

https://developer.android.com/guide/practices/verifying-apps-art.html#Stack_Size

Upvotes: 0

Related Questions