Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 3 | * |
| 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 Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 17 | #include "art_method-inl.h" |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 18 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "mirror/object-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 20 | #include "thread-inl.h" |
Mathieu Chartier | c645f1d | 2014-03-06 18:11:53 -0800 | [diff] [blame] | 21 | #include "verify_object-inl.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame^] | 25 | template <bool kDynamicFast> |
| 26 | static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
| 27 | |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 28 | extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack, |
| 29 | Thread* self ATTRIBUTE_UNUSED) { |
Hiroshi Yamauchi | 043eb9a | 2016-10-14 11:21:38 -0700 | [diff] [blame] | 30 | 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 Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 39 | // 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 Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 44 | // Called on entry to fast JNI, push a new local reference table only. |
| 45 | extern 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 Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 53 | CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod(); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return saved_local_ref_cookie; |
| 57 | } |
| 58 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | // Called on entry to JNI, transition out of Runnable and release share of mutator_lock_. |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 60 | extern uint32_t JniMethodStart(Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 61 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 62 | DCHECK(env != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | uint32_t saved_local_ref_cookie = env->local_ref_cookie; |
| 64 | env->local_ref_cookie = env->locals.GetSegmentState(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 65 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 66 | if (!native_method->IsFastNative()) { |
| 67 | // When not fast JNI we transition out of runnable. |
| 68 | self->TransitionFromRunnableToSuspended(kNative); |
| 69 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | return saved_local_ref_cookie; |
| 71 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 72 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 73 | extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 74 | self->DecodeJObject(to_lock)->MonitorEnter(self); |
| 75 | return JniMethodStart(self); |
| 76 | } |
| 77 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 78 | // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI. |
| 79 | static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 80 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 81 | bool is_fast = native_method->IsFastNative(); |
| 82 | if (!is_fast) { |
| 83 | self->TransitionFromSuspendedToRunnable(); |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame^] | 84 | } else { |
| 85 | GoToRunnableFast</*kDynamicFast*/true>(self); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI. |
| 90 | template <bool kDynamicFast> |
| 91 | ALWAYS_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 Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 106 | // 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 109 | self->CheckSuspend(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Yevgeny Rouban | 35aef2c | 2014-05-19 16:19:36 +0700 | [diff] [blame] | 113 | static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 114 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 115 | JNIEnvExt* env = self->GetJniEnv(); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 116 | if (UNLIKELY(env->check_jni)) { |
| 117 | env->CheckNoHeldMonitors(); |
| 118 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 119 | env->locals.SetSegmentState(env->local_ref_cookie); |
| 120 | env->local_ref_cookie = saved_local_ref_cookie; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 121 | self->PopHandleScope(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 124 | // TODO: These should probably be templatized or macro-ized. |
| 125 | // Otherwise there's just too much repetitive boilerplate. |
| 126 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 127 | extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 128 | GoToRunnable(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 129 | PopLocalReferences(saved_local_ref_cookie, self); |
| 130 | } |
| 131 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 132 | extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) { |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame^] | 133 | GoToRunnableFast</*kDynamicFast*/false>(self); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 134 | PopLocalReferences(saved_local_ref_cookie, self); |
| 135 | } |
| 136 | |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 137 | extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, |
| 138 | jobject locked, |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 139 | Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 140 | GoToRunnable(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 141 | UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. |
| 142 | PopLocalReferences(saved_local_ref_cookie, self); |
| 143 | } |
| 144 | |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 145 | // Common result handling for EndWithReference. |
| 146 | static 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 Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 151 | ObjPtr<mirror::Object> o; |
| 152 | if (!self->IsExceptionPending()) { |
| 153 | o = self->DecodeJObject(result); |
| 154 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | PopLocalReferences(saved_local_ref_cookie, self); |
| 156 | // Process result. |
| 157 | if (UNLIKELY(self->GetJniEnv()->check_jni)) { |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 158 | // CheckReferenceResult can resolve types. |
| 159 | StackHandleScope<1> hs(self); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 160 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o)); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 161 | CheckReferenceResult(h_obj, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 162 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 163 | VerifyObject(o); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 164 | return o.Ptr(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame^] | 167 | extern 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 Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 174 | extern mirror::Object* JniMethodEndWithReference(jobject result, |
| 175 | uint32_t saved_local_ref_cookie, |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 176 | Thread* self) { |
| 177 | GoToRunnable(self); |
| 178 | return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self); |
| 179 | } |
| 180 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 181 | extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result, |
| 182 | uint32_t saved_local_ref_cookie, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 183 | jobject locked, |
| 184 | Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 185 | GoToRunnable(self); |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 186 | UnlockJniSynchronizedMethod(locked, self); |
| 187 | return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 190 | extern 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 198 | 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 Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 206 | // 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 212 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 213 | 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 219 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 220 | UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. |
| 221 | } |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 222 | if (LIKELY(!critical_native)) { |
| 223 | PopLocalReferences(saved_local_ref_cookie, self); |
| 224 | } |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 225 | 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 Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 258 | } // namespace art |