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 | |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 25 | extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack, |
| 26 | Thread* self ATTRIBUTE_UNUSED) { |
Hiroshi Yamauchi | 043eb9a | 2016-10-14 11:21:38 -0700 | [diff] [blame^] | 27 | 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 Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 36 | // 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 Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 41 | // Called on entry to fast JNI, push a new local reference table only. |
| 42 | extern 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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | // 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] | 57 | extern uint32_t JniMethodStart(Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 58 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 59 | DCHECK(env != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 60 | uint32_t saved_local_ref_cookie = env->local_ref_cookie; |
| 61 | env->local_ref_cookie = env->locals.GetSegmentState(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 62 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 63 | if (!native_method->IsFastNative()) { |
| 64 | // When not fast JNI we transition out of runnable. |
| 65 | self->TransitionFromRunnableToSuspended(kNative); |
| 66 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 67 | return saved_local_ref_cookie; |
| 68 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 69 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 70 | extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | self->DecodeJObject(to_lock)->MonitorEnter(self); |
| 72 | return JniMethodStart(self); |
| 73 | } |
| 74 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 75 | // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI. |
| 76 | static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 77 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 78 | 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 85 | self->CheckSuspend(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Yevgeny Rouban | 35aef2c | 2014-05-19 16:19:36 +0700 | [diff] [blame] | 89 | static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 90 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 91 | JNIEnvExt* env = self->GetJniEnv(); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 92 | if (UNLIKELY(env->check_jni)) { |
| 93 | env->CheckNoHeldMonitors(); |
| 94 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | env->locals.SetSegmentState(env->local_ref_cookie); |
| 96 | env->local_ref_cookie = saved_local_ref_cookie; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 97 | self->PopHandleScope(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 100 | // TODO: These should probably be templatized or macro-ized. |
| 101 | // Otherwise there's just too much repetitive boilerplate. |
| 102 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 103 | extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 104 | GoToRunnable(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 105 | PopLocalReferences(saved_local_ref_cookie, self); |
| 106 | } |
| 107 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 108 | extern 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 Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 126 | extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, |
| 127 | jobject locked, |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 128 | Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 129 | GoToRunnable(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 130 | UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. |
| 131 | PopLocalReferences(saved_local_ref_cookie, self); |
| 132 | } |
| 133 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 134 | // TODO: JniMethodFastEndWithReference |
| 135 | // (Probably don't need to have a synchronized variant since |
| 136 | // it already has to do atomic operations) |
| 137 | |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 138 | // Common result handling for EndWithReference. |
| 139 | static 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 Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 144 | ObjPtr<mirror::Object> o; |
| 145 | if (!self->IsExceptionPending()) { |
| 146 | o = self->DecodeJObject(result); |
| 147 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 148 | PopLocalReferences(saved_local_ref_cookie, self); |
| 149 | // Process result. |
| 150 | if (UNLIKELY(self->GetJniEnv()->check_jni)) { |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 151 | // CheckReferenceResult can resolve types. |
| 152 | StackHandleScope<1> hs(self); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 153 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o)); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 154 | CheckReferenceResult(h_obj, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 156 | VerifyObject(o); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 157 | return o.Ptr(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 160 | extern mirror::Object* JniMethodEndWithReference(jobject result, |
| 161 | uint32_t saved_local_ref_cookie, |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 162 | Thread* self) { |
| 163 | GoToRunnable(self); |
| 164 | return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self); |
| 165 | } |
| 166 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 167 | extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result, |
| 168 | uint32_t saved_local_ref_cookie, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 169 | jobject locked, |
| 170 | Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 171 | GoToRunnable(self); |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 172 | UnlockJniSynchronizedMethod(locked, self); |
| 173 | return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 176 | extern 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 184 | 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 Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 192 | // 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 198 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 199 | 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 205 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 206 | UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. |
| 207 | } |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 208 | if (LIKELY(!critical_native)) { |
| 209 | PopLocalReferences(saved_local_ref_cookie, self); |
| 210 | } |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 211 | 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 Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 244 | } // namespace art |