blob: 446e3431a9fe71fa98fbb6b948a2d26d48d1c3ca [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
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700117extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,
118 jobject locked,
Ian Rogers693ff612013-02-01 10:56:12 -0800119 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700120 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
122 PopLocalReferences(saved_local_ref_cookie, self);
123}
124
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700125// TODO: JniMethodFastEndWithReference
126// (Probably don't need to have a synchronized variant since
127// it already has to do atomic operations)
128
Andreas Gampe48ee3562015-04-10 19:57:29 -0700129// Common result handling for EndWithReference.
130static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
131 uint32_t saved_local_ref_cookie,
132 Thread* self)
133 NO_THREAD_SAFETY_ANALYSIS {
134 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
135 mirror::Object* o = self->IsExceptionPending() ? nullptr : self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 PopLocalReferences(saved_local_ref_cookie, self);
137 // Process result.
138 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700139 // CheckReferenceResult can resolve types.
140 StackHandleScope<1> hs(self);
141 HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
142 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800144 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700145 return o;
146}
147
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700148extern mirror::Object* JniMethodEndWithReference(jobject result,
149 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700150 Thread* self) {
151 GoToRunnable(self);
152 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
153}
154
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
156 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700157 jobject locked,
158 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700159 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700160 UnlockJniSynchronizedMethod(locked, self);
161 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700162}
163
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700164extern uint64_t GenericJniMethodEnd(Thread* self,
165 uint32_t saved_local_ref_cookie,
166 jvalue result,
167 uint64_t result_f,
168 ArtMethod* called,
169 HandleScope* handle_scope)
170 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
171 NO_THREAD_SAFETY_ANALYSIS {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700172 bool critical_native = called->IsAnnotatedWithCriticalNative();
173 bool fast_native = called->IsAnnotatedWithFastNative();
174 bool normal_native = !critical_native && !fast_native;
175
176 // @Fast and @CriticalNative do not do a state transition.
177 if (LIKELY(normal_native)) {
178 GoToRunnable(self);
179 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700180 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
181 // locked object.
182 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
183 char return_shorty_char = called->GetShorty()[0];
184 if (return_shorty_char == 'L') {
185 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700186 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700187 UnlockJniSynchronizedMethod(locked, self);
188 }
189 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
190 result.l, saved_local_ref_cookie, self));
191 } else {
192 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700193 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700194 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
195 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700196 if (LIKELY(!critical_native)) {
197 PopLocalReferences(saved_local_ref_cookie, self);
198 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700199 switch (return_shorty_char) {
200 case 'F': {
201 if (kRuntimeISA == kX86) {
202 // Convert back the result to float.
203 double d = bit_cast<double, uint64_t>(result_f);
204 return bit_cast<uint32_t, float>(static_cast<float>(d));
205 } else {
206 return result_f;
207 }
208 }
209 case 'D':
210 return result_f;
211 case 'Z':
212 return result.z;
213 case 'B':
214 return result.b;
215 case 'C':
216 return result.c;
217 case 'S':
218 return result.s;
219 case 'I':
220 return result.i;
221 case 'J':
222 return result.j;
223 case 'V':
224 return 0;
225 default:
226 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
227 return 0;
228 }
229 }
230}
231
Ian Rogers57b86d42012-03-27 16:05:41 -0700232} // namespace art