blob: 64f19afccdbbed0e98c9cb83d615284fca480962 [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
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070032// Called on entry to fast JNI, push a new local reference table only.
33extern uint32_t JniMethodFastStart(Thread* self) {
34 JNIEnvExt* env = self->GetJniEnv();
35 DCHECK(env != nullptr);
36 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
37 env->local_ref_cookie = env->locals.GetSegmentState();
38
39 if (kIsDebugBuild) {
40 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
41 CHECK(native_method->IsAnnotatedWithFastNative()) << PrettyMethod(native_method);
42 }
43
44 return saved_local_ref_cookie;
45}
46
Ian Rogers00f7d0e2012-07-19 15:28:27 -070047// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080048extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070049 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070050 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
52 env->local_ref_cookie = env->locals.GetSegmentState();
Mathieu Chartiere401d142015-04-22 13:56:20 -070053 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070054 if (!native_method->IsFastNative()) {
55 // When not fast JNI we transition out of runnable.
56 self->TransitionFromRunnableToSuspended(kNative);
57 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 return saved_local_ref_cookie;
59}
Elliott Hughesb264f082012-04-06 17:10:10 -070060
Ian Rogers693ff612013-02-01 10:56:12 -080061extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 self->DecodeJObject(to_lock)->MonitorEnter(self);
63 return JniMethodStart(self);
64}
65
Ian Rogers1eb512d2013-10-18 15:42:20 -070066// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
67static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070068 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070069 bool is_fast = native_method->IsFastNative();
70 if (!is_fast) {
71 self->TransitionFromSuspendedToRunnable();
72 } else if (UNLIKELY(self->TestAllFlags())) {
73 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
74 // is a flag raised.
75 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -070076 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -070077 }
78}
79
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070080static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070081 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 JNIEnvExt* env = self->GetJniEnv();
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070083 if (UNLIKELY(env->check_jni)) {
84 env->CheckNoHeldMonitors();
85 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 env->locals.SetSegmentState(env->local_ref_cookie);
87 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070088 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070089}
90
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070091// TODO: These should probably be templatized or macro-ized.
92// Otherwise there's just too much repetitive boilerplate.
93
Ian Rogers693ff612013-02-01 10:56:12 -080094extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070095 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 PopLocalReferences(saved_local_ref_cookie, self);
97}
98
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070099extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) {
100 // inlined fast version of GoToRunnable(self);
101
102 if (kIsDebugBuild) {
103 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
104 CHECK(native_method->IsAnnotatedWithFastNative()) << PrettyMethod(native_method);
105 }
106
107 if (UNLIKELY(self->TestAllFlags())) {
108 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
109 // is a flag raised.
110 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
111 self->CheckSuspend();
112 }
113
114 PopLocalReferences(saved_local_ref_cookie, self);
115}
116
Ian Rogers693ff612013-02-01 10:56:12 -0800117extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
118 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700119 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
121 PopLocalReferences(saved_local_ref_cookie, self);
122}
123
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700124// TODO: JniMethodFastEndWithReference
125// (Probably don't need to have a synchronized variant since
126// it already has to do atomic operations)
127
Andreas Gampe48ee3562015-04-10 19:57:29 -0700128// Common result handling for EndWithReference.
129static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
130 uint32_t saved_local_ref_cookie,
131 Thread* self)
132 NO_THREAD_SAFETY_ANALYSIS {
133 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
134 mirror::Object* o = self->IsExceptionPending() ? nullptr : self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 PopLocalReferences(saved_local_ref_cookie, self);
136 // Process result.
137 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 CheckReferenceResult(o, self);
139 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800140 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 return o;
142}
143
Andreas Gampe48ee3562015-04-10 19:57:29 -0700144extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
145 Thread* self) {
146 GoToRunnable(self);
147 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
148}
149
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
151 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800152 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700153 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700154 UnlockJniSynchronizedMethod(locked, self);
155 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700156}
157
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700158extern uint64_t GenericJniMethodEnd(Thread* self,
159 uint32_t saved_local_ref_cookie,
160 jvalue result,
161 uint64_t result_f,
162 ArtMethod* called,
163 HandleScope* handle_scope)
164 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
165 NO_THREAD_SAFETY_ANALYSIS {
166 GoToRunnable(self);
167 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
168 // locked object.
169 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
170 char return_shorty_char = called->GetShorty()[0];
171 if (return_shorty_char == 'L') {
172 if (locked != nullptr) {
173 UnlockJniSynchronizedMethod(locked, self);
174 }
175 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
176 result.l, saved_local_ref_cookie, self));
177 } else {
178 if (locked != nullptr) {
179 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
180 }
181 PopLocalReferences(saved_local_ref_cookie, self);
182 switch (return_shorty_char) {
183 case 'F': {
184 if (kRuntimeISA == kX86) {
185 // Convert back the result to float.
186 double d = bit_cast<double, uint64_t>(result_f);
187 return bit_cast<uint32_t, float>(static_cast<float>(d));
188 } else {
189 return result_f;
190 }
191 }
192 case 'D':
193 return result_f;
194 case 'Z':
195 return result.z;
196 case 'B':
197 return result.b;
198 case 'C':
199 return result.c;
200 case 'S':
201 return result.s;
202 case 'I':
203 return result.i;
204 case 'J':
205 return result.j;
206 case 'V':
207 return 0;
208 default:
209 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
210 return 0;
211 }
212 }
213}
214
Ian Rogers57b86d42012-03-27 16:05:41 -0700215} // namespace art