blob: 58f256a1916bd80650a4d7d3b594046c165c48bb [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)
Mathieu Chartier90443472015-07-16 20:32:27 -070066 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 JNIEnvExt* env = self->GetJniEnv();
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070068 if (UNLIKELY(env->check_jni)) {
69 env->CheckNoHeldMonitors();
70 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 env->locals.SetSegmentState(env->local_ref_cookie);
72 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070073 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074}
75
Ian Rogers693ff612013-02-01 10:56:12 -080076extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070077 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 PopLocalReferences(saved_local_ref_cookie, self);
79}
80
Ian Rogers693ff612013-02-01 10:56:12 -080081extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
82 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070083 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070084 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
85 PopLocalReferences(saved_local_ref_cookie, self);
86}
87
Andreas Gampe48ee3562015-04-10 19:57:29 -070088// Common result handling for EndWithReference.
89static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
90 uint32_t saved_local_ref_cookie,
91 Thread* self)
92 NO_THREAD_SAFETY_ANALYSIS {
93 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
94 mirror::Object* o = self->IsExceptionPending() ? nullptr : self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 PopLocalReferences(saved_local_ref_cookie, self);
96 // Process result.
97 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 CheckReferenceResult(o, self);
99 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800100 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 return o;
102}
103
Andreas Gampe48ee3562015-04-10 19:57:29 -0700104extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
105 Thread* self) {
106 GoToRunnable(self);
107 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
108}
109
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
111 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800112 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700113 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700114 UnlockJniSynchronizedMethod(locked, self);
115 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700116}
117
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700118extern uint64_t GenericJniMethodEnd(Thread* self,
119 uint32_t saved_local_ref_cookie,
120 jvalue result,
121 uint64_t result_f,
122 ArtMethod* called,
123 HandleScope* handle_scope)
124 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
125 NO_THREAD_SAFETY_ANALYSIS {
126 GoToRunnable(self);
127 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
128 // locked object.
129 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
130 char return_shorty_char = called->GetShorty()[0];
131 if (return_shorty_char == 'L') {
132 if (locked != nullptr) {
133 UnlockJniSynchronizedMethod(locked, self);
134 }
135 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
136 result.l, saved_local_ref_cookie, self));
137 } else {
138 if (locked != nullptr) {
139 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
140 }
141 PopLocalReferences(saved_local_ref_cookie, self);
142 switch (return_shorty_char) {
143 case 'F': {
144 if (kRuntimeISA == kX86) {
145 // Convert back the result to float.
146 double d = bit_cast<double, uint64_t>(result_f);
147 return bit_cast<uint32_t, float>(static_cast<float>(d));
148 } else {
149 return result_f;
150 }
151 }
152 case 'D':
153 return result_f;
154 case 'Z':
155 return result.z;
156 case 'B':
157 return result.b;
158 case 'C':
159 return result.c;
160 case 'S':
161 return result.s;
162 case 'I':
163 return result.i;
164 case 'J':
165 return result.j;
166 case 'V':
167 return 0;
168 default:
169 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
170 return 0;
171 }
172 }
173}
174
Ian Rogers57b86d42012-03-27 16:05:41 -0700175} // namespace art