1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "dex_file-inl.h" 18 #include "entrypoints/entrypoint_utils-inl.h" 19 #include "mirror/art_method-inl.h" 20 #include "mirror/class-inl.h" 21 #include "mirror/object.h" 22 #include "mirror/object-inl.h" 23 #include "mirror/object_array-inl.h" 24 #include "scoped_thread_state_change.h" 25 #include "thread.h" 26 #include "verify_object-inl.h" 27 28 namespace art { 29 30 // Called on entry to JNI, transition out of Runnable and release share of mutator_lock_. 31 extern uint32_t JniMethodStart(Thread* self) { 32 JNIEnvExt* env = self->GetJniEnv(); 33 DCHECK(env != nullptr); 34 uint32_t saved_local_ref_cookie = env->local_ref_cookie; 35 env->local_ref_cookie = env->locals.GetSegmentState(); 36 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr(); 37 if (!native_method->IsFastNative()) { 38 // When not fast JNI we transition out of runnable. 39 self->TransitionFromRunnableToSuspended(kNative); 40 } 41 return saved_local_ref_cookie; 42 } 43 44 extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) { 45 self->DecodeJObject(to_lock)->MonitorEnter(self); 46 return JniMethodStart(self); 47 } 48 49 // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI. 50 static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS { 51 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr(); 52 bool is_fast = native_method->IsFastNative(); 53 if (!is_fast) { 54 self->TransitionFromSuspendedToRunnable(); 55 } else if (UNLIKELY(self->TestAllFlags())) { 56 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there 57 // is a flag raised. 58 DCHECK(Locks::mutator_lock_->IsSharedHeld(self)); 59 CheckSuspend(self); 60 } 61 } 62 63 static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) 64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 65 JNIEnvExt* env = self->GetJniEnv(); 66 env->locals.SetSegmentState(env->local_ref_cookie); 67 env->local_ref_cookie = saved_local_ref_cookie; 68 self->PopHandleScope(); 69 } 70 71 extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) { 72 GoToRunnable(self); 73 PopLocalReferences(saved_local_ref_cookie, self); 74 } 75 76 77 extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked, 78 Thread* self) { 79 GoToRunnable(self); 80 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. 81 PopLocalReferences(saved_local_ref_cookie, self); 82 } 83 84 extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie, 85 Thread* self) { 86 GoToRunnable(self); 87 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop. 88 PopLocalReferences(saved_local_ref_cookie, self); 89 // Process result. 90 if (UNLIKELY(self->GetJniEnv()->check_jni)) { 91 if (self->IsExceptionPending()) { 92 return NULL; 93 } 94 CheckReferenceResult(o, self); 95 } 96 VerifyObject(o); 97 return o; 98 } 99 100 extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result, 101 uint32_t saved_local_ref_cookie, 102 jobject locked, Thread* self) { 103 GoToRunnable(self); 104 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. 105 mirror::Object* o = self->DecodeJObject(result); 106 PopLocalReferences(saved_local_ref_cookie, self); 107 // Process result. 108 if (UNLIKELY(self->GetJniEnv()->check_jni)) { 109 if (self->IsExceptionPending()) { 110 return NULL; 111 } 112 CheckReferenceResult(o, self); 113 } 114 VerifyObject(o); 115 return o; 116 } 117 118 } // namespace art 119