blob: 158c1d6348a0f14d61bdeb549d1b49f098746faa [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"
Mathieu Chartier76433272014-09-26 14:32:37 -070019#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampee03662b2016-10-13 17:12:56 -070020#include "indirect_reference_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070022#include "thread-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080023#include "verify_object.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070024
25namespace art {
26
Andreas Gampee03662b2016-10-13 17:12:56 -070027static_assert(sizeof(IRTSegmentState) == sizeof(uint32_t), "IRTSegmentState size unexpected");
28static_assert(std::is_trivial<IRTSegmentState>::value, "IRTSegmentState not trivial");
29
Igor Murashkinaf1e2992016-10-12 17:44:50 -070030template <bool kDynamicFast>
31static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS;
32
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070033extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack,
34 Thread* self ATTRIBUTE_UNUSED) {
Hiroshi Yamauchi043eb9a2016-10-14 11:21:38 -070035 DCHECK(kUseReadBarrier);
36 if (kUseBakerReadBarrier) {
37 DCHECK(handle_on_stack->AsMirrorPtr() != nullptr)
38 << "The class of a static jni call must not be null";
39 // Check the mark bit and return early if it's already marked.
40 if (LIKELY(handle_on_stack->AsMirrorPtr()->GetMarkBit() != 0)) {
41 return;
42 }
43 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -070044 // Call the read barrier and update the handle.
45 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
46 handle_on_stack->Assign(to_ref);
47}
48
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070049// Called on entry to fast JNI, push a new local reference table only.
50extern uint32_t JniMethodFastStart(Thread* self) {
51 JNIEnvExt* env = self->GetJniEnv();
52 DCHECK(env != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -070053 uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->local_ref_cookie);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070054 env->local_ref_cookie = env->locals.GetSegmentState();
55
56 if (kIsDebugBuild) {
57 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
David Sehr709b0702016-10-13 09:12:37 -070058 CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod();
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070059 }
60
61 return saved_local_ref_cookie;
62}
63
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080065extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070067 DCHECK(env != nullptr);
Andreas Gampee03662b2016-10-13 17:12:56 -070068 uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->local_ref_cookie);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069 env->local_ref_cookie = env->locals.GetSegmentState();
Mathieu Chartiere401d142015-04-22 13:56:20 -070070 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070071 if (!native_method->IsFastNative()) {
72 // When not fast JNI we transition out of runnable.
73 self->TransitionFromRunnableToSuspended(kNative);
74 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 return saved_local_ref_cookie;
76}
Elliott Hughesb264f082012-04-06 17:10:10 -070077
Ian Rogers693ff612013-02-01 10:56:12 -080078extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 self->DecodeJObject(to_lock)->MonitorEnter(self);
80 return JniMethodStart(self);
81}
82
Ian Rogers1eb512d2013-10-18 15:42:20 -070083// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
84static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Mathieu Chartiere401d142015-04-22 13:56:20 -070085 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
Ian Rogers1eb512d2013-10-18 15:42:20 -070086 bool is_fast = native_method->IsFastNative();
87 if (!is_fast) {
88 self->TransitionFromSuspendedToRunnable();
Igor Murashkinaf1e2992016-10-12 17:44:50 -070089 } else {
90 GoToRunnableFast</*kDynamicFast*/true>(self);
91 }
92}
93
94// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
95template <bool kDynamicFast>
96ALWAYS_INLINE static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
97 if (kIsDebugBuild) {
98 // Should only enter here if the method is !Fast JNI or @FastNative.
99 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
100
101 if (kDynamicFast) {
102 CHECK(native_method->IsFastNative()) << native_method->PrettyMethod();
103 } else {
104 CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod();
105 }
106 }
107
108 // When we are in "fast" JNI or @FastNative, we are already Runnable.
109 // Only do a suspend check on the way out of JNI.
110 if (UNLIKELY(self->TestAllFlags())) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700111 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
112 // is a flag raised.
113 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -0700114 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -0700115 }
116}
117
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +0700118static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700119 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 JNIEnvExt* env = self->GetJniEnv();
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700121 if (UNLIKELY(env->check_jni)) {
122 env->CheckNoHeldMonitors();
123 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 env->locals.SetSegmentState(env->local_ref_cookie);
Andreas Gampee03662b2016-10-13 17:12:56 -0700125 env->local_ref_cookie = bit_cast<IRTSegmentState>(saved_local_ref_cookie);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700127}
128
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700129// TODO: These should probably be templatized or macro-ized.
130// Otherwise there's just too much repetitive boilerplate.
131
Ian Rogers693ff612013-02-01 10:56:12 -0800132extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700133 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700134 PopLocalReferences(saved_local_ref_cookie, self);
135}
136
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700137extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700138 GoToRunnableFast</*kDynamicFast*/false>(self);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700139 PopLocalReferences(saved_local_ref_cookie, self);
140}
141
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700142extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,
143 jobject locked,
Ian Rogers693ff612013-02-01 10:56:12 -0800144 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700145 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
147 PopLocalReferences(saved_local_ref_cookie, self);
148}
149
Andreas Gampe48ee3562015-04-10 19:57:29 -0700150// Common result handling for EndWithReference.
151static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
152 uint32_t saved_local_ref_cookie,
153 Thread* self)
154 NO_THREAD_SAFETY_ANALYSIS {
155 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700156 ObjPtr<mirror::Object> o;
157 if (!self->IsExceptionPending()) {
158 o = self->DecodeJObject(result);
159 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700160 PopLocalReferences(saved_local_ref_cookie, self);
161 // Process result.
162 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700163 // CheckReferenceResult can resolve types.
164 StackHandleScope<1> hs(self);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700165 HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o));
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700166 CheckReferenceResult(h_obj, self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 }
Mathieu Chartier9d156d52016-10-06 17:44:26 -0700168 VerifyObject(o);
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700169 return o.Ptr();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700170}
171
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700172extern mirror::Object* JniMethodFastEndWithReference(jobject result,
173 uint32_t saved_local_ref_cookie,
174 Thread* self) {
175 GoToRunnableFast</*kDynamicFast*/false>(self);
176 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
177}
178
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700179extern mirror::Object* JniMethodEndWithReference(jobject result,
180 uint32_t saved_local_ref_cookie,
Andreas Gampe48ee3562015-04-10 19:57:29 -0700181 Thread* self) {
182 GoToRunnable(self);
183 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
184}
185
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
187 uint32_t saved_local_ref_cookie,
Mathieu Chartierbe08cf52016-09-13 13:41:24 -0700188 jobject locked,
189 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700190 GoToRunnable(self);
Andreas Gampe48ee3562015-04-10 19:57:29 -0700191 UnlockJniSynchronizedMethod(locked, self);
192 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
Ian Rogers57b86d42012-03-27 16:05:41 -0700193}
194
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700195extern uint64_t GenericJniMethodEnd(Thread* self,
196 uint32_t saved_local_ref_cookie,
197 jvalue result,
198 uint64_t result_f,
199 ArtMethod* called,
200 HandleScope* handle_scope)
201 // TODO: NO_THREAD_SAFETY_ANALYSIS as GoToRunnable() is NO_THREAD_SAFETY_ANALYSIS
202 NO_THREAD_SAFETY_ANALYSIS {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700203 bool critical_native = called->IsAnnotatedWithCriticalNative();
204 bool fast_native = called->IsAnnotatedWithFastNative();
205 bool normal_native = !critical_native && !fast_native;
206
207 // @Fast and @CriticalNative do not do a state transition.
208 if (LIKELY(normal_native)) {
209 GoToRunnable(self);
210 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700211 // We need the mutator lock (i.e., calling GoToRunnable()) before accessing the shorty or the
212 // locked object.
213 jobject locked = called->IsSynchronized() ? handle_scope->GetHandle(0).ToJObject() : nullptr;
214 char return_shorty_char = called->GetShorty()[0];
215 if (return_shorty_char == 'L') {
216 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700217 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700218 UnlockJniSynchronizedMethod(locked, self);
219 }
220 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceHandleResult(
221 result.l, saved_local_ref_cookie, self));
222 } else {
223 if (locked != nullptr) {
Igor Murashkin06a04e02016-09-13 15:57:37 -0700224 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700225 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
226 }
Igor Murashkin06a04e02016-09-13 15:57:37 -0700227 if (LIKELY(!critical_native)) {
228 PopLocalReferences(saved_local_ref_cookie, self);
229 }
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -0700230 switch (return_shorty_char) {
231 case 'F': {
232 if (kRuntimeISA == kX86) {
233 // Convert back the result to float.
234 double d = bit_cast<double, uint64_t>(result_f);
235 return bit_cast<uint32_t, float>(static_cast<float>(d));
236 } else {
237 return result_f;
238 }
239 }
240 case 'D':
241 return result_f;
242 case 'Z':
243 return result.z;
244 case 'B':
245 return result.b;
246 case 'C':
247 return result.c;
248 case 'S':
249 return result.s;
250 case 'I':
251 return result.i;
252 case 'J':
253 return result.j;
254 case 'V':
255 return 0;
256 default:
257 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
258 return 0;
259 }
260 }
261}
262
Ian Rogers57b86d42012-03-27 16:05:41 -0700263} // namespace art