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" |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 18 | #include "base/casts.h" |
Andreas Gampe | a682247 | 2017-10-11 22:58:53 -0700 | [diff] [blame] | 19 | #include "base/logging.h" |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 20 | #include "entrypoints/entrypoint_utils-inl.h" |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 21 | #include "indirect_reference_table.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/object-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 23 | #include "thread-inl.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 24 | #include "verify_object.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 28 | static_assert(sizeof(IRTSegmentState) == sizeof(uint32_t), "IRTSegmentState size unexpected"); |
| 29 | static_assert(std::is_trivial<IRTSegmentState>::value, "IRTSegmentState not trivial"); |
| 30 | |
Andreas Gampe | a682247 | 2017-10-11 22:58:53 -0700 | [diff] [blame] | 31 | static bool kEnableAnnotationChecks = RegisterRuntimeDebugFlag(&kEnableAnnotationChecks); |
| 32 | |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 33 | template <bool kDynamicFast> |
| 34 | static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
| 35 | |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 36 | extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack, |
| 37 | Thread* self ATTRIBUTE_UNUSED) { |
Hiroshi Yamauchi | 043eb9a | 2016-10-14 11:21:38 -0700 | [diff] [blame] | 38 | 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 Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 47 | // 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 Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 52 | // Called on entry to fast JNI, push a new local reference table only. |
| 53 | extern uint32_t JniMethodFastStart(Thread* self) { |
| 54 | JNIEnvExt* env = self->GetJniEnv(); |
| 55 | DCHECK(env != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 56 | uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->local_ref_cookie); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 57 | env->local_ref_cookie = env->locals.GetSegmentState(); |
| 58 | |
Andreas Gampe | a682247 | 2017-10-11 22:58:53 -0700 | [diff] [blame] | 59 | if (kIsDebugBuild && kEnableAnnotationChecks) { |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 60 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 61 | CHECK(native_method->IsAnnotatedWithFastNative()) << native_method->PrettyMethod(); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return saved_local_ref_cookie; |
| 65 | } |
| 66 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 67 | // 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] | 68 | extern uint32_t JniMethodStart(Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 69 | JNIEnvExt* env = self->GetJniEnv(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 70 | DCHECK(env != nullptr); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 71 | uint32_t saved_local_ref_cookie = bit_cast<uint32_t>(env->local_ref_cookie); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 72 | env->local_ref_cookie = env->locals.GetSegmentState(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 73 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 74 | if (!native_method->IsFastNative()) { |
| 75 | // When not fast JNI we transition out of runnable. |
| 76 | self->TransitionFromRunnableToSuspended(kNative); |
| 77 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 78 | return saved_local_ref_cookie; |
| 79 | } |
Elliott Hughes | b264f08 | 2012-04-06 17:10:10 -0700 | [diff] [blame] | 80 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 81 | extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 82 | self->DecodeJObject(to_lock)->MonitorEnter(self); |
| 83 | return JniMethodStart(self); |
| 84 | } |
| 85 | |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 86 | // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI. |
| 87 | static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 88 | ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 89 | bool is_fast = native_method->IsFastNative(); |
| 90 | if (!is_fast) { |
| 91 | self->TransitionFromSuspendedToRunnable(); |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 92 | } else { |
| 93 | GoToRunnableFast</*kDynamicFast*/true>(self); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI. |
| 98 | template <bool kDynamicFast> |
| 99 | ALWAYS_INLINE static inline void GoToRunnableFast(Thread* self) NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | a682247 | 2017-10-11 22:58:53 -0700 | [diff] [blame] | 100 | if (kIsDebugBuild && kEnableAnnotationChecks) { |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 101 | // 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 Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 114 | // 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 Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 117 | self->CheckSuspend(); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Yevgeny Rouban | 35aef2c | 2014-05-19 16:19:36 +0700 | [diff] [blame] | 121 | static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 122 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 123 | JNIEnvExt* env = self->GetJniEnv(); |
Andreas Gampe | 5f4a09a | 2015-09-28 13:16:33 -0700 | [diff] [blame] | 124 | if (UNLIKELY(env->check_jni)) { |
| 125 | env->CheckNoHeldMonitors(); |
| 126 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 127 | env->locals.SetSegmentState(env->local_ref_cookie); |
Andreas Gampe | e03662b | 2016-10-13 17:12:56 -0700 | [diff] [blame] | 128 | env->local_ref_cookie = bit_cast<IRTSegmentState>(saved_local_ref_cookie); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 129 | self->PopHandleScope(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 132 | // TODO: These should probably be templatized or macro-ized. |
| 133 | // Otherwise there's just too much repetitive boilerplate. |
| 134 | |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 135 | extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 136 | GoToRunnable(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 137 | PopLocalReferences(saved_local_ref_cookie, self); |
| 138 | } |
| 139 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 140 | extern void JniMethodFastEnd(uint32_t saved_local_ref_cookie, Thread* self) { |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 141 | GoToRunnableFast</*kDynamicFast*/false>(self); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 142 | PopLocalReferences(saved_local_ref_cookie, self); |
| 143 | } |
| 144 | |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 145 | extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, |
| 146 | jobject locked, |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 147 | Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 148 | GoToRunnable(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 149 | UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. |
| 150 | PopLocalReferences(saved_local_ref_cookie, self); |
| 151 | } |
| 152 | |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 153 | // Common result handling for EndWithReference. |
| 154 | static 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 Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 159 | ObjPtr<mirror::Object> o; |
| 160 | if (!self->IsExceptionPending()) { |
| 161 | o = self->DecodeJObject(result); |
| 162 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 163 | PopLocalReferences(saved_local_ref_cookie, self); |
| 164 | // Process result. |
| 165 | if (UNLIKELY(self->GetJniEnv()->check_jni)) { |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 166 | // CheckReferenceResult can resolve types. |
| 167 | StackHandleScope<1> hs(self); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 168 | HandleWrapperObjPtr<mirror::Object> h_obj(hs.NewHandleWrapper(&o)); |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 169 | CheckReferenceResult(h_obj, self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 170 | } |
Mathieu Chartier | 9d156d5 | 2016-10-06 17:44:26 -0700 | [diff] [blame] | 171 | VerifyObject(o); |
Mathieu Chartier | c4f3925 | 2016-10-05 18:32:08 -0700 | [diff] [blame] | 172 | return o.Ptr(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Igor Murashkin | af1e299 | 2016-10-12 17:44:50 -0700 | [diff] [blame] | 175 | extern 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 Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 182 | extern mirror::Object* JniMethodEndWithReference(jobject result, |
| 183 | uint32_t saved_local_ref_cookie, |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 184 | Thread* self) { |
| 185 | GoToRunnable(self); |
| 186 | return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self); |
| 187 | } |
| 188 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 189 | extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result, |
| 190 | uint32_t saved_local_ref_cookie, |
Mathieu Chartier | be08cf5 | 2016-09-13 13:41:24 -0700 | [diff] [blame] | 191 | jobject locked, |
| 192 | Thread* self) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 193 | GoToRunnable(self); |
Andreas Gampe | 48ee356 | 2015-04-10 19:57:29 -0700 | [diff] [blame] | 194 | UnlockJniSynchronizedMethod(locked, self); |
| 195 | return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 198 | extern 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 206 | 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 Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 214 | // 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 220 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 221 | 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 Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 227 | DCHECK(normal_native) << " @FastNative and synchronize is not supported"; |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 228 | UnlockJniSynchronizedMethod(locked, self); // Must decode before pop. |
| 229 | } |
Igor Murashkin | 06a04e0 | 2016-09-13 15:57:37 -0700 | [diff] [blame] | 230 | if (LIKELY(!critical_native)) { |
| 231 | PopLocalReferences(saved_local_ref_cookie, self); |
| 232 | } |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 233 | switch (return_shorty_char) { |
| 234 | case 'F': { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame^] | 235 | if (kRuntimeISA == InstructionSet::kX86) { |
Hiroshi Yamauchi | a23b468 | 2015-09-28 17:47:32 -0700 | [diff] [blame] | 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 Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 266 | } // namespace art |