1 /*
2 * Copyright (C) 2012 The Android Open Source Project
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
17 #include "art_method-inl.h"
18 #include "entrypoints/entrypoint_utils-inl.h"
19 #include "mirror/object-inl.h"
20 #include "thread-inl.h"
21 #include "verify_object-inl.h"
22
23 namespace art {
24
ReadBarrierJni(mirror::CompressedReference<mirror::Object> * handle_on_stack,Thread * self ATTRIBUTE_UNUSED)25 extern void ReadBarrierJni(mirror::CompressedReference<mirror::Object>* handle_on_stack,
26 Thread* self ATTRIBUTE_UNUSED) {
27 // Call the read barrier and update the handle.
28 mirror::Object* to_ref = ReadBarrier::BarrierForRoot(handle_on_stack);
29 handle_on_stack->Assign(to_ref);
30 }
31
32 // Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
JniMethodStart(Thread * self)33 extern uint32_t JniMethodStart(Thread* self) {
34 JNIEnvExt* env = self->GetJniEnv();
35 DCHECK(env != nullptr);
36 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
37 env->local_ref_cookie = env->locals.GetSegmentState();
38 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
39 if (!native_method->IsFastNative()) {
40 // When not fast JNI we transition out of runnable.
41 self->TransitionFromRunnableToSuspended(kNative);
42 }
43 return saved_local_ref_cookie;
44 }
45
JniMethodStartSynchronized(jobject to_lock,Thread * self)46 extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
47 self->DecodeJObject(to_lock)->MonitorEnter(self);
48 return JniMethodStart(self);
49 }
50
51 // TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
GoToRunnable(Thread * self)52 static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
53 ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
54 bool is_fast = native_method->IsFastNative();
55 if (!is_fast) {
56 self->TransitionFromSuspendedToRunnable();
57 } else if (UNLIKELY(self->TestAllFlags())) {
58 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
59 // is a flag raised.
60 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
61 self->CheckSuspend();
62 }
63 }
64
PopLocalReferences(uint32_t saved_local_ref_cookie,Thread * self)65 static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
67 JNIEnvExt* env = self->GetJniEnv();
68 env->locals.SetSegmentState(env->local_ref_cookie);
69 env->local_ref_cookie = saved_local_ref_cookie;
70 self->PopHandleScope();
71 }
72
JniMethodEnd(uint32_t saved_local_ref_cookie,Thread * self)73 extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
74 GoToRunnable(self);
75 PopLocalReferences(saved_local_ref_cookie, self);
76 }
77
JniMethodEndSynchronized(uint32_t saved_local_ref_cookie,jobject locked,Thread * self)78 extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
79 Thread* self) {
80 GoToRunnable(self);
81 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
82 PopLocalReferences(saved_local_ref_cookie, self);
83 }
84
85 // Common result handling for EndWithReference.
JniMethodEndWithReferenceHandleResult(jobject result,uint32_t saved_local_ref_cookie,Thread * self)86 static mirror::Object* JniMethodEndWithReferenceHandleResult(jobject result,
87 uint32_t saved_local_ref_cookie,
88 Thread* self)
89 NO_THREAD_SAFETY_ANALYSIS {
90 // Must decode before pop. The 'result' may not be valid in case of an exception, though.
91 mirror::Object* o = self->IsExceptionPending() ? nullptr : self->DecodeJObject(result);
92 PopLocalReferences(saved_local_ref_cookie, self);
93 // Process result.
94 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
95 CheckReferenceResult(o, self);
96 }
97 VerifyObject(o);
98 return o;
99 }
100
JniMethodEndWithReference(jobject result,uint32_t saved_local_ref_cookie,Thread * self)101 extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
102 Thread* self) {
103 GoToRunnable(self);
104 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
105 }
106
JniMethodEndWithReferenceSynchronized(jobject result,uint32_t saved_local_ref_cookie,jobject locked,Thread * self)107 extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
108 uint32_t saved_local_ref_cookie,
109 jobject locked, Thread* self) {
110 GoToRunnable(self);
111 UnlockJniSynchronizedMethod(locked, self);
112 return JniMethodEndWithReferenceHandleResult(result, saved_local_ref_cookie, self);
113 }
114
115 } // namespace art
116