blob: 6bb67a372785dccd51fa6d60131e77873b3a5b56 [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"
Andreas Gampee03662b2016-10-13 17:12:56 -070018#include "base/casts.h"
Andreas Gampea6822472017-10-11 22:58:53 -070019#include "base/logging.h"
Mathieu Chartier76433272014-09-26 14:32:37 -070020#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampee03662b2016-10-13 17:12:56 -070021#include "indirect_reference_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070023#include "thread-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080024#include "verify_object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025
26namespace art {
27
Andreas Gampee03662b2016-10-13 17:12:56 -070028static_assert(sizeof(IRTSegmentState) == sizeof(uint32_t), "IRTSegmentState size unexpected");
29static_assert(std::is_trivial<IRTSegmentState>::value, "IRTSegmentState not trivial");
30
Andreas Gampea6822472017-10-11 22:58:53 -070031static bool kEnableAnnotationChecks = RegisterRuntimeDebugFlag(&kEnableAnnotationChecks);
32
Igor Murashkinaf1e2992016-10-12 17:44:50 -070033template <bool kDynamicFast>
34static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
35
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070036extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack,
37 Thread* self ATTRIBUTE_UNUSED) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070038 DCHECK(kUseReadBarrier);
39 if (kUseBakerReadBarrier) {
40 DCHECK(handle_on_stack->AsMirrorPtr() != nullptr)
41 << "The class of a static jni call must not be null";
42 // Check the mark bit and return early if it's already marked.
43 if (LIKELY(handle_on_stack->AsMirrorPtr()->GetMarkBit() != 0)) {
44 return;
45 }
46 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070047 // Call the read barrier and update the handle.
48 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
49 handle_on_stack->Assign(to_ref);
50}
51
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070052// Called on entry to fast JNI, push a new local reference table only.
53extern uint32_t JniMethodFastStart(Thread* self) {
54 JNIEnvExt* env = self->GetJniEnv();
55 DCHECK(env != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -070056 uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->local_ref_cookie);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070057 env->local_ref_cookie = env->locals.GetSegmentState();
58
Andreas Gampea6822472017-10-11 22:58:53 -070059 if (kIsDebugBuild && kEnableAnnotationChecks) {
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070060 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
David Sehr709b0702016-10-13 09:12:37 -070061 CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod();
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070062 }
63
64 return saved_local_ref_cookie;
65}
66
Ian Rogers00f7d0e2012-07-19 15:28:27 -070067// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080068extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070070 DCHECK(env != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -070071 uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->local_ref_cookie);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 env->local_ref_cookie = env->locals.GetSegmentState();
Mathieu Chartiere401d142015-04-22 13:56:20 -070073 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070074 if (!native_method->IsFastNative()) {
75 // When not fast JNI we transition out of runnable.
76 self->TransitionFromRunnableToSuspended(kNative);
77 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 return saved_local_ref_cookie;
79}
Elliott Hughesb264f082012-04-06 17:10:10 -070080
Ian Rogers693ff612013-02-01 10:56:12 -080081extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 self->DecodeJObject(to_lock)->MonitorEnter(self);
83 return JniMethodStart(self);
84}
85
Ian Rogers1eb512d2013-10-18 15:42:20 -070086// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
87static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070089 bool is_fast = native_method->IsFastNative();
90 if (!is_fast) {
91 self->TransitionFromSuspendedToRunnable();
Igor Murashkinaf1e2992016-10-12 17:44:50 -070092 } else {
93 GoToRunnableFast</*kDynamicFast*/true>(self);
94 }
95}
96
97// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
98template <bool kDynamicFast>
99ALWAYS_INLINE static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Andreas Gampea6822472017-10-11 22:58:53 -0700100 if (kIsDebugBuild && kEnableAnnotationChecks) {
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700101 // Should only enter here if the method is !Fast JNI or @FastNative.
102 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
103
104 if (kDynamicFast) {
105 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
106 } else {
107 CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod();
108 }
109 }
110
111 // When we are in "fast" JNI or @FastNative, we are already Runnable.
112 // Only do a suspend check on the way out of JNI.
113 if (UNLIKELY(self->TestAllFlags())) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700114 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
115 // is a flag raised.
116 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -0700117 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -0700118 }
119}
120
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700121static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123 JNIEnvExt* env = self->GetJniEnv();
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700124 if (UNLIKELY(env->check_jni)) {
125 env->CheckNoHeldMonitors();
126 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127 env->locals.SetSegmentState(env->local_ref_cookie);
Andreas Gampee03662b2016-10-13 17:12:56 -0700128 env->local_ref_cookie = bit_cast<IRTSegmentState>(saved_local_ref_cookie);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130}
131
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700132// TODO: These should probably be templatized or macro-ized.
133// Otherwise there's just too much repetitive boilerplate.
134
Ian Rogers693ff612013-02-01 10:56:12 -0800135extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700136 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 PopLocalReferences(saved_local_ref_cookie, self);
138}
139
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700140extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700141 GoToRunnableFast</*kDynamicFast*/false>(self);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700142 PopLocalReferences(saved_local_ref_cookie, self);
143}
144
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700145extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,
146 jobject locked,
Ian Rogers693ff612013-02-01 10:56:12 -0800147 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700148 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
150 PopLocalReferences(saved_local_ref_cookie, self);
151}
152
Andreas Gampe48ee3562015-04-10 19:57:29 -0700153// Common result handling for EndWithReference.
154static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
155 uint32_t saved_local_ref_cookie,
156 Thread* self)
157 NO_THREAD_SAFETY_ANALYSIS {
158 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700159 ObjPtr<mirror::Object> o;
160 if (!self->IsExceptionPending()) {
161 o = self->DecodeJObject(result);
162 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700163 PopLocalReferences(saved_local_ref_cookie, self);
164 // Process result.
165 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700166 // CheckReferenceResult can resolve types.
167 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700168 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700169 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700171 VerifyObject(o);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700172 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173}
174
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700175extern mirror::Object* JniMethodFastEndWithReference(jobject result,
176 uint32_t saved_local_ref_cookie,
177 Thread* self) {
178 GoToRunnableFast</*kDynamicFast*/false>(self);
179 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
180}
181
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700182extern mirror::Object* JniMethodEndWithReference(jobject result,
183 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700184 Thread* self) {
185 GoToRunnable(self);
186 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
187}
188
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
190 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700191 jobject locked,
192 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700193 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700194 UnlockJniSynchronizedMethod(locked, self);
195 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700196}
197
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700198extern uint64_t GenericJniMethodEnd(Thread* self,
199 uint32_t saved_local_ref_cookie,
200 jvalue result,
201 uint64_t result_f,
202 ArtMethod* called,
203 HandleScope* handle_scope)
204 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
205 NO_THREAD_SAFETY_ANALYSIS {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700206 bool critical_native = called->IsAnnotatedWithCriticalNative();
207 bool fast_native = called->IsAnnotatedWithFastNative();
208 bool normal_native = !critical_native && !fast_native;
209
210 // @Fast and @CriticalNative do not do a state transition.
211 if (LIKELY(normal_native)) {
212 GoToRunnable(self);
213 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700214 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
215 // locked object.
216 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
217 char return_shorty_char = called->GetShorty()[0];
218 if (return_shorty_char == 'L') {
219 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700220 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700221 UnlockJniSynchronizedMethod(locked, self);
222 }
223 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
224 result.l, saved_local_ref_cookie, self));
225 } else {
226 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700227 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700228 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
229 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700230 if (LIKELY(!critical_native)) {
231 PopLocalReferences(saved_local_ref_cookie, self);
232 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700233 switch (return_shorty_char) {
234 case 'F': {
235 if (kRuntimeISA == kX86) {
236 // Convert back the result to float.
237 double d = bit_cast<double, uint64_t>(result_f);
238 return bit_cast<uint32_t, float>(static_cast<float>(d));
239 } else {
240 return result_f;
241 }
242 }
243 case 'D':
244 return result_f;
245 case 'Z':
246 return result.z;
247 case 'B':
248 return result.b;
249 case 'C':
250 return result.c;
251 case 'S':
252 return result.s;
253 case 'I':
254 return result.i;
255 case 'J':
256 return result.j;
257 case 'V':
258 return 0;
259 default:
260 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
261 return 0;
262 }
263 }
264}
265
Ian Rogers57b86d42012-03-27 16:05:41 -0700266} // namespace art