blob: 9c9cca815023b4e610b8a0d283d939f7f1f8517a [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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) {
65 JNIEnvExt* env = self->GetJniEnv();
66 env->locals.SetSegmentState(env->local_ref_cookie);
67 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070068 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069}
70
Ian Rogers693ff612013-02-01 10:56:12 -080071extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070072 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 PopLocalReferences(saved_local_ref_cookie, self);
74}
75
76
Ian Rogers693ff612013-02-01 10:56:12 -080077extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
78 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070079 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
81 PopLocalReferences(saved_local_ref_cookie, self);
82}
83
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080085 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070086 GoToRunnable(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 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 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -080096 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 return o;
98}
99
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
101 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800102 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700103 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 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 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800114 VerifyObject(o);
Elliott Hughesb264f082012-04-06 17:10:10 -0700115 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700116}
117
Ian Rogers57b86d42012-03-27 16:05:41 -0700118} // namespace art