blob: 383cdd256dc1e415ea6524f0e434bba85de3a1b5 [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) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070027 DCHECK(kUseReadBarrier);
28 if (kUseBakerReadBarrier) {
29 DCHECK(handle_on_stack->AsMirrorPtr() != nullptr)
30 << "The class of a static jni call must not be null";
31 // Check the mark bit and return early if it's already marked.
32 if (LIKELY(handle_on_stack->AsMirrorPtr()->GetMarkBit() != 0)) {
33 return;
34 }
35 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070036 // Call the read barrier and update the handle.
37 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
38 handle_on_stack->Assign(to_ref);
39}
40
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070041// Called on entry to fast JNI, push a new local reference table only.
42extern uint32_t JniMethodFastStart(Thread* self) {
43 JNIEnvExt* env = self->GetJniEnv();
44 DCHECK(env != nullptr);
45 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
46 env->local_ref_cookie = env->locals.GetSegmentState();
47
48 if (kIsDebugBuild) {
49 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
50 CHECK(native_method->IsAnnotatedWithFastNative()) << PrettyMethod(native_method);
51 }
52
53 return saved_local_ref_cookie;
54}
55
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080057extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070059 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
61 env->local_ref_cookie = env->locals.GetSegmentState();
Mathieu Chartiere401d142015-04-22 13:56:20 -070062 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070063 if (!native_method->IsFastNative()) {
64 // When not fast JNI we transition out of runnable.
65 self->TransitionFromRunnableToSuspended(kNative);
66 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 return saved_local_ref_cookie;
68}
Elliott Hughesb264f082012-04-06 17:10:10 -070069
Ian Rogers693ff612013-02-01 10:56:12 -080070extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 self->DecodeJObject(to_lock)->MonitorEnter(self);
72 return JniMethodStart(self);
73}
74
Ian Rogers1eb512d2013-10-18 15:42:20 -070075// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
76static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070077 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070078 bool is_fast = native_method->IsFastNative();
79 if (!is_fast) {
80 self->TransitionFromSuspendedToRunnable();
81 } else if (UNLIKELY(self->TestAllFlags())) {
82 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
83 // is a flag raised.
84 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -070085 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -070086 }
87}
88
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070089static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 JNIEnvExt* env = self->GetJniEnv();
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070092 if (UNLIKELY(env->check_jni)) {
93 env->CheckNoHeldMonitors();
94 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 env->locals.SetSegmentState(env->local_ref_cookie);
96 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070097 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098}
99
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700100// TODO: These should probably be templatized or macro-ized.
101// Otherwise there's just too much repetitive boilerplate.
102
Ian Rogers693ff612013-02-01 10:56:12 -0800103extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700104 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 PopLocalReferences(saved_local_ref_cookie, self);
106}
107
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700108extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) {
109 // inlined fast version of GoToRunnable(self);
110
111 if (kIsDebugBuild) {
112 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
113 CHECK(native_method->IsAnnotatedWithFastNative()) << PrettyMethod(native_method);
114 }
115
116 if (UNLIKELY(self->TestAllFlags())) {
117 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
118 // is a flag raised.
119 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
120 self->CheckSuspend();
121 }
122
123 PopLocalReferences(saved_local_ref_cookie, self);
124}
125
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700126extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,
127 jobject locked,
Ian Rogers693ff612013-02-01 10:56:12 -0800128 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700129 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
131 PopLocalReferences(saved_local_ref_cookie, self);
132}
133
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700134// TODO: JniMethodFastEndWithReference
135// (Probably don't need to have a synchronized variant since
136// it already has to do atomic operations)
137
Andreas Gampe48ee3562015-04-10 19:57:29 -0700138// Common result handling for EndWithReference.
139static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
140 uint32_t saved_local_ref_cookie,
141 Thread* self)
142 NO_THREAD_SAFETY_ANALYSIS {
143 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700144 ObjPtr<mirror::Object> o;
145 if (!self->IsExceptionPending()) {
146 o = self->DecodeJObject(result);
147 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700148 PopLocalReferences(saved_local_ref_cookie, self);
149 // Process result.
150 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700151 // CheckReferenceResult can resolve types.
152 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700153 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700154 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700155 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700156 VerifyObject(o);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700157 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158}
159
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700160extern mirror::Object* JniMethodEndWithReference(jobject result,
161 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700162 Thread* self) {
163 GoToRunnable(self);
164 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
165}
166
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
168 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700169 jobject locked,
170 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700171 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700172 UnlockJniSynchronizedMethod(locked, self);
173 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700174}
175
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700176extern uint64_t GenericJniMethodEnd(Thread* self,
177 uint32_t saved_local_ref_cookie,
178 jvalue result,
179 uint64_t result_f,
180 ArtMethod* called,
181 HandleScope* handle_scope)
182 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
183 NO_THREAD_SAFETY_ANALYSIS {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700184 bool critical_native = called->IsAnnotatedWithCriticalNative();
185 bool fast_native = called->IsAnnotatedWithFastNative();
186 bool normal_native = !critical_native && !fast_native;
187
188 // @Fast and @CriticalNative do not do a state transition.
189 if (LIKELY(normal_native)) {
190 GoToRunnable(self);
191 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700192 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
193 // locked object.
194 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
195 char return_shorty_char = called->GetShorty()[0];
196 if (return_shorty_char == 'L') {
197 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700198 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700199 UnlockJniSynchronizedMethod(locked, self);
200 }
201 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
202 result.l, saved_local_ref_cookie, self));
203 } else {
204 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700205 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700206 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
207 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700208 if (LIKELY(!critical_native)) {
209 PopLocalReferences(saved_local_ref_cookie, self);
210 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700211 switch (return_shorty_char) {
212 case 'F': {
213 if (kRuntimeISA == kX86) {
214 // Convert back the result to float.
215 double d = bit_cast<double, uint64_t>(result_f);
216 return bit_cast<uint32_t, float>(static_cast<float>(d));
217 } else {
218 return result_f;
219 }
220 }
221 case 'D':
222 return result_f;
223 case 'Z':
224 return result.z;
225 case 'B':
226 return result.b;
227 case 'C':
228 return result.c;
229 case 'S':
230 return result.s;
231 case 'I':
232 return result.i;
233 case 'J':
234 return result.j;
235 case 'V':
236 return 0;
237 default:
238 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
239 return 0;
240 }
241 }
242}
243
Ian Rogers57b86d42012-03-27 16:05:41 -0700244} // namespace art