blob: 5c86bbb39ea23b80bdfe60b249bedcc85fbc9313 [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
Andreas Gampe57943812017-12-06 21:39:13 -080017#include <android-base/logging.h>
18
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Andreas Gampee03662b2016-10-13 17:12:56 -070020#include "base/casts.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070021#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampee03662b2016-10-13 17:12:56 -070022#include "indirect_reference_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070024#include "thread-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080025#include "verify_object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070026
27namespace art {
28
Andreas Gampee03662b2016-10-13 17:12:56 -070029static_assert(sizeof(IRTSegmentState) == sizeof(uint32_t), "IRTSegmentState size unexpected");
30static_assert(std::is_trivial<IRTSegmentState>::value, "IRTSegmentState not trivial");
31
Vladimir Markob0a6aee2017-10-27 10:34:04 +010032static inline void GoToRunnableFast(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Igor Murashkinaf1e2992016-10-12 17:44:50 -070033
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070034extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack,
35 Thread* self ATTRIBUTE_UNUSED) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070036 DCHECK(kUseReadBarrier);
37 if (kUseBakerReadBarrier) {
38 DCHECK(handle_on_stack->AsMirrorPtr() != nullptr)
39 << "The class of a static jni call must not be null";
40 // Check the mark bit and return early if it's already marked.
41 if (LIKELY(handle_on_stack->AsMirrorPtr()->GetMarkBit() != 0)) {
42 return;
43 }
44 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070045 // Call the read barrier and update the handle.
46 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
47 handle_on_stack->Assign(to_ref);
48}
49
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070050// Called on entry to fast JNI, push a new local reference table only.
51extern uint32_t JniMethodFastStart(Thread* self) {
52 JNIEnvExt* env = self->GetJniEnv();
53 DCHECK(env != nullptr);
Ian Rogers55256cb2017-12-21 17:07:11 -080054 uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->GetLocalRefCookie());
55 env->SetLocalRefCookie(env->GetLocalsSegmentState());
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070056
Vladimir Markob0a6aee2017-10-27 10:34:04 +010057 if (kIsDebugBuild) {
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070058 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Vladimir Markob0a6aee2017-10-27 10:34:04 +010059 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070060 }
61
62 return saved_local_ref_cookie;
63}
64
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080066extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070068 DCHECK(env != nullptr);
Ian Rogers55256cb2017-12-21 17:07:11 -080069 uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->GetLocalRefCookie());
70 env->SetLocalRefCookie(env->GetLocalsSegmentState());
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Vladimir Markob0a6aee2017-10-27 10:34:04 +010072 // TODO: Introduce special entrypoint for synchronized @FastNative methods?
73 // Or ban synchronized @FastNative outright to avoid the extra check here?
74 DCHECK(!native_method->IsFastNative() || native_method->IsSynchronized());
Ian Rogers1eb512d2013-10-18 15:42:20 -070075 if (!native_method->IsFastNative()) {
76 // When not fast JNI we transition out of runnable.
77 self->TransitionFromRunnableToSuspended(kNative);
78 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 return saved_local_ref_cookie;
80}
Elliott Hughesb264f082012-04-06 17:10:10 -070081
Ian Rogers693ff612013-02-01 10:56:12 -080082extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 self->DecodeJObject(to_lock)->MonitorEnter(self);
84 return JniMethodStart(self);
85}
86
Ian Rogers1eb512d2013-10-18 15:42:20 -070087// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
88static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070090 bool is_fast = native_method->IsFastNative();
91 if (!is_fast) {
92 self->TransitionFromSuspendedToRunnable();
Igor Murashkinaf1e2992016-10-12 17:44:50 -070093 } else {
Vladimir Markob0a6aee2017-10-27 10:34:04 +010094 GoToRunnableFast(self);
Igor Murashkinaf1e2992016-10-12 17:44:50 -070095 }
96}
97
Vladimir Markob0a6aee2017-10-27 10:34:04 +010098ALWAYS_INLINE static inline void GoToRunnableFast(Thread* self) {
99 if (kIsDebugBuild) {
100 // Should only enter here if the method is @FastNative.
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700101 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100102 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700103 }
104
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100105 // When we are in @FastNative, we are already Runnable.
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700106 // Only do a suspend check on the way out of JNI.
107 if (UNLIKELY(self->TestAllFlags())) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700108 // 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));
Ian Rogers7b078e82014-09-10 14:44:24 -0700111 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -0700112 }
113}
114
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700115static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers55256cb2017-12-21 17:07:11 -0800118 if (UNLIKELY(env->IsCheckJniEnabled())) {
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700119 env->CheckNoHeldMonitors();
120 }
Ian Rogers55256cb2017-12-21 17:07:11 -0800121 env->SetLocalSegmentState(env->GetLocalRefCookie());
122 env->SetLocalRefCookie(bit_cast<IRTSegmentState>(saved_local_ref_cookie));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124}
125
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700126// TODO: These should probably be templatized or macro-ized.
127// Otherwise there's just too much repetitive boilerplate.
128
Ian Rogers693ff612013-02-01 10:56:12 -0800129extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700130 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 PopLocalReferences(saved_local_ref_cookie, self);
132}
133
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700134extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100135 GoToRunnableFast(self);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700136 PopLocalReferences(saved_local_ref_cookie, self);
137}
138
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700139extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,
140 jobject locked,
Ian Rogers693ff612013-02-01 10:56:12 -0800141 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700142 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700143 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
144 PopLocalReferences(saved_local_ref_cookie, self);
145}
146
Andreas Gampe48ee3562015-04-10 19:57:29 -0700147// Common result handling for EndWithReference.
148static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
149 uint32_t saved_local_ref_cookie,
150 Thread* self)
151 NO_THREAD_SAFETY_ANALYSIS {
152 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700153 ObjPtr<mirror::Object> o;
154 if (!self->IsExceptionPending()) {
155 o = self->DecodeJObject(result);
156 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 PopLocalReferences(saved_local_ref_cookie, self);
158 // Process result.
Ian Rogers55256cb2017-12-21 17:07:11 -0800159 if (UNLIKELY(self->GetJniEnv()->IsCheckJniEnabled())) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700160 // CheckReferenceResult can resolve types.
161 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700162 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700163 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700164 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700165 VerifyObject(o);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700166 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167}
168
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700169extern mirror::Object* JniMethodFastEndWithReference(jobject result,
170 uint32_t saved_local_ref_cookie,
171 Thread* self) {
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100172 GoToRunnableFast(self);
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700173 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
174}
175
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700176extern mirror::Object* JniMethodEndWithReference(jobject result,
177 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700178 Thread* self) {
179 GoToRunnable(self);
180 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
181}
182
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
184 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700185 jobject locked,
186 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700187 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700188 UnlockJniSynchronizedMethod(locked, self);
189 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700190}
191
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700192extern uint64_t GenericJniMethodEnd(Thread* self,
193 uint32_t saved_local_ref_cookie,
194 jvalue result,
195 uint64_t result_f,
196 ArtMethod* called,
197 HandleScope* handle_scope)
198 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
199 NO_THREAD_SAFETY_ANALYSIS {
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100200 bool critical_native = called->IsCriticalNative();
201 bool fast_native = called->IsFastNative();
Igor Murashkin06a04e02016-09-13 15:57:37 -0700202 bool normal_native = !critical_native && !fast_native;
203
204 // @Fast and @CriticalNative do not do a state transition.
205 if (LIKELY(normal_native)) {
206 GoToRunnable(self);
207 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700208 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
209 // locked object.
210 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
211 char return_shorty_char = called->GetShorty()[0];
212 if (return_shorty_char == 'L') {
213 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700214 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700215 UnlockJniSynchronizedMethod(locked, self);
216 }
217 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
218 result.l, saved_local_ref_cookie, self));
219 } else {
220 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700221 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700222 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
223 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700224 if (LIKELY(!critical_native)) {
225 PopLocalReferences(saved_local_ref_cookie, self);
226 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700227 switch (return_shorty_char) {
228 case 'F': {
Vladimir Marko33bff252017-11-01 14:35:42 +0000229 if (kRuntimeISA == InstructionSet::kX86) {
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700230 // Convert back the result to float.
231 double d = bit_cast<double, uint64_t>(result_f);
232 return bit_cast<uint32_t, float>(static_cast<float>(d));
233 } else {
234 return result_f;
235 }
236 }
237 case 'D':
238 return result_f;
239 case 'Z':
240 return result.z;
241 case 'B':
242 return result.b;
243 case 'C':
244 return result.c;
245 case 'S':
246 return result.s;
247 case 'I':
248 return result.i;
249 case 'J':
250 return result.j;
251 case 'V':
252 return 0;
253 default:
254 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
Elliott Hughesc1896c92018-11-29 11:33:18 -0800255 UNREACHABLE();
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700256 }
257 }
258}
259
Ian Rogers57b86d42012-03-27 16:05:41 -0700260} // namespace art