blob: 7c7e2da74077a0f996a5b54f9655feb6521bca50 [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.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700135 ObjPtr<mirror::Object> o;
136 if (!self->IsExceptionPending()) {
137 o = self->DecodeJObject(result);
138 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 PopLocalReferences(saved_local_ref_cookie, self);
140 // Process result.
141 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700142 // CheckReferenceResult can resolve types.
143 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700144 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700145 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 }
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700147 VerifyObject(o.Ptr());
148 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149}
150
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700151extern mirror::Object* JniMethodEndWithReference(jobject result,
152 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700153 Thread* self) {
154 GoToRunnable(self);
155 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
156}
157
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
159 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700160 jobject locked,
161 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700162 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700163 UnlockJniSynchronizedMethod(locked, self);
164 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700165}
166
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700167extern uint64_t GenericJniMethodEnd(Thread* self,
168 uint32_t saved_local_ref_cookie,
169 jvalue result,
170 uint64_t result_f,
171 ArtMethod* called,
172 HandleScope* handle_scope)
173 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
174 NO_THREAD_SAFETY_ANALYSIS {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700175 bool critical_native = called->IsAnnotatedWithCriticalNative();
176 bool fast_native = called->IsAnnotatedWithFastNative();
177 bool normal_native = !critical_native && !fast_native;
178
179 // @Fast and @CriticalNative do not do a state transition.
180 if (LIKELY(normal_native)) {
181 GoToRunnable(self);
182 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700183 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
184 // locked object.
185 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
186 char return_shorty_char = called->GetShorty()[0];
187 if (return_shorty_char == 'L') {
188 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700189 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700190 UnlockJniSynchronizedMethod(locked, self);
191 }
192 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
193 result.l, saved_local_ref_cookie, self));
194 } else {
195 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700196 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700197 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
198 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700199 if (LIKELY(!critical_native)) {
200 PopLocalReferences(saved_local_ref_cookie, self);
201 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700202 switch (return_shorty_char) {
203 case 'F': {
204 if (kRuntimeISA == kX86) {
205 // Convert back the result to float.
206 double d = bit_cast<double, uint64_t>(result_f);
207 return bit_cast<uint32_t, float>(static_cast<float>(d));
208 } else {
209 return result_f;
210 }
211 }
212 case 'D':
213 return result_f;
214 case 'Z':
215 return result.z;
216 case 'B':
217 return result.b;
218 case 'C':
219 return result.c;
220 case 'S':
221 return result.s;
222 case 'I':
223 return result.i;
224 case 'J':
225 return result.j;
226 case 'V':
227 return 0;
228 default:
229 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
230 return 0;
231 }
232 }
233}
234
Ian Rogers57b86d42012-03-27 16:05:41 -0700235} // namespace art