title: native crash under ART when calling attachCurrentThread
body: <p>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 <code>JNIEnv</code> and then call the <code>AttachCurrentThread</code> for that env. Following is the piece of code which does this:</p>
  
  <pre><code>CJniEnvUtil::CJniEnvUtil(JavaVM *pvm)
  : m_fNeedDetach(false)
  , mJavaVM(pvm)
  , m_pEnv(NULL)
  {
      switch (mJavaVM-&gt;GetEnv((void**)&amp;m_pEnv, JNI_VERSION_1_6)) { 
          case JNI_OK: break; 
          case JNI_EDETACHED: 
              if (mJavaVM-&gt;AttachCurrentThread(&amp;m_pEnv, NULL) != 0) { 
                  break;
              } 
              m_fNeedDetach = true;
             break; 
          case JNI_EVERSION: 
              break;
      }
  }
  
  CJniEnvUtil::~CJniEnvUtil()
  {
      if (m_fNeedDetach &amp;&amp; m_pEnv) 
          mJavaVM-&gt;DetachCurrentThread(); 
  }
  </code></pre>
  
  <p>So the idea is, for every event callback we create instance of this <code>CJniEnvUtil</code> which in turn gets env for the current thread and attaches current thread. The thread this detached when this <code>CJniUtil</code> 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.</p>
  
  <pre><code>A/art﹕ art/runtime/thread.cc:468] Check failed: &amp;stack_variable &gt; reinterpret_cast&lt;void*&gt;          (stack_end_) (&amp;stack_variable=0x4cd061b0, reinterpret_cast&lt;void*&gt;(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] "&lt;native thread without managed peer&gt;" 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&amp;) 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)
  </code></pre>
  
  <p>As the first line shows, the sanity check failing.</p>
  
  <p>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.</p>