blob: de225ad8e856e464732722b7f1f078f6ff6fce10 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#include "art_method-inl.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070018#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070020#include "thread-inl.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080021#include "verify_object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022
23namespace art {
24
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070025extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack,
26 Thread* self ATTRIBUTE_UNUSED) {
27 // Call the read barrier and update the handle.
28 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
29 handle_on_stack->Assign(to_ref);
30}
31
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080033extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070035 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
37 env->local_ref_cookie = env->locals.GetSegmentState();
Mathieu Chartiere401d142015-04-22 13:56:20 -070038 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070039 if (!native_method->IsFastNative()) {
40 // When not fast JNI we transition out of runnable.
41 self->TransitionFromRunnableToSuspended(kNative);
42 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070043 return saved_local_ref_cookie;
44}
Elliott Hughesb264f082012-04-06 17:10:10 -070045
Ian Rogers693ff612013-02-01 10:56:12 -080046extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047 self->DecodeJObject(to_lock)->MonitorEnter(self);
48 return JniMethodStart(self);
49}
50
Ian Rogers1eb512d2013-10-18 15:42:20 -070051// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
52static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070053 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070054 bool is_fast = native_method->IsFastNative();
55 if (!is_fast) {
56 self->TransitionFromSuspendedToRunnable();
57 } else if (UNLIKELY(self->TestAllFlags())) {
58 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
59 // is a flag raised.
60 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -070061 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -070062 }
63}
64
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070065static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 JNIEnvExt* env = self->GetJniEnv();
68 env->locals.SetSegmentState(env->local_ref_cookie);
69 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070070 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071}
72
Ian Rogers693ff612013-02-01 10:56:12 -080073extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070074 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 PopLocalReferences(saved_local_ref_cookie, self);
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
Andreas Gampe48ee3562015-04-10 19:57:29 -070085// Common result handling for EndWithReference.
86static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
87 uint32_t saved_local_ref_cookie,
88 Thread* self)
89 NO_THREAD_SAFETY_ANALYSIS {
90 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
91 mirror::Object* o = self->IsExceptionPending() ? nullptr : self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 PopLocalReferences(saved_local_ref_cookie, self);
93 // Process result.
94 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 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
Andreas Gampe48ee3562015-04-10 19:57:29 -0700101extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
102 Thread* self) {
103 GoToRunnable(self);
104 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
105}
106
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
108 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800109 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700110 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700111 UnlockJniSynchronizedMethod(locked, self);
112 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700113}
114
Ian Rogers57b86d42012-03-27 16:05:41 -0700115} // namespace art