blob: 5d36b4c90480397ccb6846cb03d9b112abeca372 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Ian Rogers57b86d42012-03-27 16:05:41 -07003 *
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
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070017#include "dex_file-inl.h"
Ian Rogers7655f292013-07-29 11:07:13 -070018#include "entrypoints/entrypoint_utils.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/object.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070026#include "thread.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080027#include "verify_object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070028
29namespace art {
30
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080032extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070034 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
36 env->local_ref_cookie = env->locals.GetSegmentState();
Ian Rogers1eb512d2013-10-18 15:42:20 -070037 mirror::ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
38 if (!native_method->IsFastNative()) {
39 // When not fast JNI we transition out of runnable.
40 self->TransitionFromRunnableToSuspended(kNative);
41 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 return saved_local_ref_cookie;
43}
Elliott Hughesb264f082012-04-06 17:10:10 -070044
Ian Rogers693ff612013-02-01 10:56:12 -080045extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070046 self->DecodeJObject(to_lock)->MonitorEnter(self);
47 return JniMethodStart(self);
48}
49
Ian Rogers1eb512d2013-10-18 15:42:20 -070050// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
51static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
52 mirror::ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
53 bool is_fast = native_method->IsFastNative();
54 if (!is_fast) {
55 self->TransitionFromSuspendedToRunnable();
56 } else if (UNLIKELY(self->TestAllFlags())) {
57 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
58 // is a flag raised.
59 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
60 CheckSuspend(self);
61 }
62}
63
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070064static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 JNIEnvExt* env = self->GetJniEnv();
67 env->locals.SetSegmentState(env->local_ref_cookie);
68 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070069 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070}
71
Ian Rogers693ff612013-02-01 10:56:12 -080072extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070073 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 PopLocalReferences(saved_local_ref_cookie, self);
75}
76
77
Ian Rogers693ff612013-02-01 10:56:12 -080078extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
79 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070080 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
82 PopLocalReferences(saved_local_ref_cookie, self);
83}
84
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080086 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070087 GoToRunnable(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089 PopLocalReferences(saved_local_ref_cookie, self);
90 // Process result.
91 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
92 if (self->IsExceptionPending()) {
93 return NULL;
94 }
95 CheckReferenceResult(o, self);
96 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -080097 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 return o;
99}
100
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
102 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800103 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700104 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 PopLocalReferences(saved_local_ref_cookie, self);
108 // Process result.
109 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
110 if (self->IsExceptionPending()) {
111 return NULL;
112 }
113 CheckReferenceResult(o, self);
114 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800115 VerifyObject(o);
Elliott Hughesb264f082012-04-06 17:10:10 -0700116 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700117}
118
Ian Rogers57b86d42012-03-27 16:05:41 -0700119} // namespace art