blob: 330c742354cd191008bc4f0016b25384df12a856 [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
Igor Murashkinaf1e2992016-10-12 17:44:50 -070025template <bool kDynamicFast>
26static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
27
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070028extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack,
29 Thread* self ATTRIBUTE_UNUSED) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070030 DCHECK(kUseReadBarrier);
31 if (kUseBakerReadBarrier) {
32 DCHECK(handle_on_stack->AsMirrorPtr() != nullptr)
33 << "The class of a static jni call must not be null";
34 // Check the mark bit and return early if it's already marked.
35 if (LIKELY(handle_on_stack->AsMirrorPtr()->GetMarkBit() != 0)) {
36 return;
37 }
38 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070039 // Call the read barrier and update the handle.
40 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
41 handle_on_stack->Assign(to_ref);
42}
43
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070044// Called on entry to fast JNI, push a new local reference table only.
45extern uint32_t JniMethodFastStart(Thread* self) {
46 JNIEnvExt* env = self->GetJniEnv();
47 DCHECK(env != nullptr);
48 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
49 env->local_ref_cookie = env->locals.GetSegmentState();
50
51 if (kIsDebugBuild) {
52 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
David Sehr709b0702016-10-13 09:12:37 -070053 CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod();
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070054 }
55
56 return saved_local_ref_cookie;
57}
58
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080060extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070062 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
64 env->local_ref_cookie = env->locals.GetSegmentState();
Mathieu Chartiere401d142015-04-22 13:56:20 -070065 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070066 if (!native_method->IsFastNative()) {
67 // When not fast JNI we transition out of runnable.
68 self->TransitionFromRunnableToSuspended(kNative);
69 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070070 return saved_local_ref_cookie;
71}
Elliott Hughesb264f082012-04-06 17:10:10 -070072
Ian Rogers693ff612013-02-01 10:56:12 -080073extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070074 self->DecodeJObject(to_lock)->MonitorEnter(self);
75 return JniMethodStart(self);
76}
77
Ian Rogers1eb512d2013-10-18 15:42:20 -070078// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
79static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070080 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070081 bool is_fast = native_method->IsFastNative();
82 if (!is_fast) {
83 self->TransitionFromSuspendedToRunnable();
Igor Murashkinaf1e2992016-10-12 17:44:50 -070084 } else {
85 GoToRunnableFast</*kDynamicFast*/true>(self);
86 }
87}
88
89// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
90template <bool kDynamicFast>
91ALWAYS_INLINE static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
92 if (kIsDebugBuild) {
93 // Should only enter here if the method is !Fast JNI or @FastNative.
94 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
95
96 if (kDynamicFast) {
97 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
98 } else {
99 CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod();
100 }
101 }
102
103 // When we are in "fast" JNI or @FastNative, we are already Runnable.
104 // Only do a suspend check on the way out of JNI.
105 if (UNLIKELY(self->TestAllFlags())) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700106 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
107 // is a flag raised.
108 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -0700109 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -0700110 }
111}
112
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700113static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700114 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 JNIEnvExt* env = self->GetJniEnv();
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700116 if (UNLIKELY(env->check_jni)) {
117 env->CheckNoHeldMonitors();
118 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 env->locals.SetSegmentState(env->local_ref_cookie);
120 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122}
123
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700124// TODO: These should probably be templatized or macro-ized.
125// Otherwise there's just too much repetitive boilerplate.
126
Ian Rogers693ff612013-02-01 10:56:12 -0800127extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700128 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 PopLocalReferences(saved_local_ref_cookie, self);
130}
131
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700132extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700133 GoToRunnableFast</*kDynamicFast*/false>(self);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700134 PopLocalReferences(saved_local_ref_cookie, self);
135}
136
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700137extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,
138 jobject locked,
Ian Rogers693ff612013-02-01 10:56:12 -0800139 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700140 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
142 PopLocalReferences(saved_local_ref_cookie, self);
143}
144
Andreas Gampe48ee3562015-04-10 19:57:29 -0700145// Common result handling for EndWithReference.
146static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
147 uint32_t saved_local_ref_cookie,
148 Thread* self)
149 NO_THREAD_SAFETY_ANALYSIS {
150 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700151 ObjPtr<mirror::Object> o;
152 if (!self->IsExceptionPending()) {
153 o = self->DecodeJObject(result);
154 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 PopLocalReferences(saved_local_ref_cookie, self);
156 // Process result.
157 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700158 // CheckReferenceResult can resolve types.
159 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700160 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700161 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700162 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700163 VerifyObject(o);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700164 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165}
166
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700167extern mirror::Object* JniMethodFastEndWithReference(jobject result,
168 uint32_t saved_local_ref_cookie,
169 Thread* self) {
170 GoToRunnableFast</*kDynamicFast*/false>(self);
171 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
172}
173
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700174extern mirror::Object* JniMethodEndWithReference(jobject result,
175 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700176 Thread* self) {
177 GoToRunnable(self);
178 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
179}
180
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
182 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700183 jobject locked,
184 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700185 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700186 UnlockJniSynchronizedMethod(locked, self);
187 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700188}
189
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700190extern uint64_t GenericJniMethodEnd(Thread* self,
191 uint32_t saved_local_ref_cookie,
192 jvalue result,
193 uint64_t result_f,
194 ArtMethod* called,
195 HandleScope* handle_scope)
196 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
197 NO_THREAD_SAFETY_ANALYSIS {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700198 bool critical_native = called->IsAnnotatedWithCriticalNative();
199 bool fast_native = called->IsAnnotatedWithFastNative();
200 bool normal_native = !critical_native && !fast_native;
201
202 // @Fast and @CriticalNative do not do a state transition.
203 if (LIKELY(normal_native)) {
204 GoToRunnable(self);
205 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700206 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
207 // locked object.
208 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
209 char return_shorty_char = called->GetShorty()[0];
210 if (return_shorty_char == 'L') {
211 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700212 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700213 UnlockJniSynchronizedMethod(locked, self);
214 }
215 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
216 result.l, saved_local_ref_cookie, self));
217 } else {
218 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700219 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700220 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
221 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700222 if (LIKELY(!critical_native)) {
223 PopLocalReferences(saved_local_ref_cookie, self);
224 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700225 switch (return_shorty_char) {
226 case 'F': {
227 if (kRuntimeISA == kX86) {
228 // Convert back the result to float.
229 double d = bit_cast<double, uint64_t>(result_f);
230 return bit_cast<uint32_t, float>(static_cast<float>(d));
231 } else {
232 return result_f;
233 }
234 }
235 case 'D':
236 return result_f;
237 case 'Z':
238 return result.z;
239 case 'B':
240 return result.b;
241 case 'C':
242 return result.c;
243 case 'S':
244 return result.s;
245 case 'I':
246 return result.i;
247 case 'J':
248 return result.j;
249 case 'V':
250 return 0;
251 default:
252 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
253 return 0;
254 }
255 }
256}
257
Ian Rogers57b86d42012-03-27 16:05:41 -0700258} // namespace art