blob: 23aca45002fb1425ab0ebdcf98525916fdd20b2a [file] [log] [blame]
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_
18#define ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_
Ian Rogers00f7d0e2012-07-19 15:28:27 -070019
Elliott Hughes1aa246d2012-12-13 09:29:36 -080020#include "base/casts.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070021#include "jni_internal-inl.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070022#include "read_barrier.h"
Ian Rogers693ff612013-02-01 10:56:12 -080023#include "thread-inl.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080024#include "verify_object.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025
26namespace art {
27
28// Scoped change into and out of a particular state. Handles Runnable transitions that require
29// more complicated suspension checking. The subclasses ScopedObjectAccessUnchecked and
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030// ScopedObjectAccess are used to handle the change into Runnable to Get direct access to objects,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031// the unchecked variant doesn't aid annotalysis.
32class ScopedThreadStateChange {
33 public:
34 ScopedThreadStateChange(Thread* self, ThreadState new_thread_state)
Ian Rogers1ffa32f2013-02-05 18:29:08 -080035 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) {
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080037 if (UNLIKELY(self_ == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 // Value chosen arbitrarily and won't be used in the destructor since thread_ == NULL.
39 old_thread_state_ = kTerminated;
Ian Rogers120f1c72012-09-28 17:17:10 -070040 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -070041 CHECK(runtime == NULL || !runtime->IsStarted() || runtime->IsShuttingDown(self_));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 } else {
Ian Rogers22f454c2012-09-08 11:06:29 -070043 DCHECK_EQ(self, Thread::Current());
44 // Read state without locks, ok as state is effectively thread local and we're not interested
45 // in the suspend count (this will be handled in the runnable transitions).
Ian Rogers474b6da2012-09-25 00:20:38 -070046 old_thread_state_ = self->GetState();
Mathieu Chartier92b78892014-04-24 16:14:43 -070047 if (old_thread_state_ != new_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 if (new_thread_state == kRunnable) {
49 self_->TransitionFromSuspendedToRunnable();
Mathieu Chartier92b78892014-04-24 16:14:43 -070050 } else if (old_thread_state_ == kRunnable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 self_->TransitionFromRunnableToSuspended(new_thread_state);
Mathieu Chartier92b78892014-04-24 16:14:43 -070052 } else {
53 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
54 self_->SetState(new_thread_state);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 }
56 }
57 }
58 }
59
Ian Rogers1ffa32f2013-02-05 18:29:08 -080060 ~ScopedThreadStateChange() LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE {
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080061 if (UNLIKELY(self_ == NULL)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062 if (!expected_has_no_thread_) {
Ian Rogers120f1c72012-09-28 17:17:10 -070063 Runtime* runtime = Runtime::Current();
Mathieu Chartier590fee92013-09-13 13:46:47 -070064 bool shutting_down = (runtime == NULL) || runtime->IsShuttingDown(nullptr);
Ian Rogers120f1c72012-09-28 17:17:10 -070065 CHECK(shutting_down);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 }
67 } else {
68 if (old_thread_state_ != thread_state_) {
69 if (old_thread_state_ == kRunnable) {
70 self_->TransitionFromSuspendedToRunnable();
71 } else if (thread_state_ == kRunnable) {
72 self_->TransitionFromRunnableToSuspended(old_thread_state_);
73 } else {
Ian Rogers22f454c2012-09-08 11:06:29 -070074 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
Ian Rogers474b6da2012-09-25 00:20:38 -070075 self_->SetState(old_thread_state_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070076 }
77 }
78 }
79 }
80
81 Thread* Self() const {
82 return self_;
83 }
84
85 protected:
86 // Constructor used by ScopedJniThreadState for an unattached thread that has access to the VM*.
87 ScopedThreadStateChange()
88 : self_(NULL), thread_state_(kTerminated), old_thread_state_(kTerminated),
89 expected_has_no_thread_(true) {}
90
91 Thread* const self_;
92 const ThreadState thread_state_;
93
94 private:
95 ThreadState old_thread_state_;
96 const bool expected_has_no_thread_;
97
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -070098 friend class ScopedObjectAccessUnchecked;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
100};
101
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700102// Assumes we are already runnable.
103class ScopedObjectAccessAlreadyRunnable {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 public:
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700105 Thread* Self() const {
106 return self_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800107 }
108
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700109 JNIEnvExt* Env() const {
110 return env_;
111 }
112
113 JavaVMExt* Vm() const {
114 return vm_;
115 }
116
117 /*
118 * Add a local reference for an object to the indirect reference table associated with the
119 * current stack frame. When the native function returns, the reference will be discarded.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 *
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700121 * We need to allow the same reference to be added multiple times, and cope with NULL.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700122 *
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700123 * This will be called on otherwise unreferenced objects. We cannot do GC allocations here, and
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 * it's best if we don't grab a mutex.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 */
126 template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 T AddLocalReference(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700128 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700129 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 if (obj == NULL) {
131 return NULL;
132 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 DCHECK_NE((reinterpret_cast<uintptr_t>(obj) & 0xffff0000), 0xebad0000);
Ian Rogers987560f2014-04-22 11:42:59 -0700134 return Env()->AddLocalReference<T>(obj);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 }
136
137 template<typename T>
138 T Decode(jobject obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700140 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700141 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 return down_cast<T>(Self()->DecodeJObject(obj));
143 }
144
Brian Carlstromea46f952013-07-30 01:26:50 -0700145 mirror::ArtField* DecodeField(jfieldID fid) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700147 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700148 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700149 CHECK(!kMovingFields);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700150 mirror::ArtField* field = reinterpret_cast<mirror::ArtField*>(fid);
151 return ReadBarrier::BarrierForRoot<mirror::ArtField, kWithReadBarrier>(&field);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 }
153
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 jfieldID EncodeField(mirror::ArtField* field) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700156 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700157 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 CHECK(!kMovingFields);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 return reinterpret_cast<jfieldID>(field);
160 }
161
Brian Carlstromea46f952013-07-30 01:26:50 -0700162 mirror::ArtMethod* DecodeMethod(jmethodID mid) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700164 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700165 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700166 CHECK(!kMovingMethods);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700167 mirror::ArtMethod* method = reinterpret_cast<mirror::ArtMethod*>(mid);
168 return ReadBarrier::BarrierForRoot<mirror::ArtMethod, kWithReadBarrier>(&method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700169 }
170
Brian Carlstromea46f952013-07-30 01:26:50 -0700171 jmethodID EncodeMethod(mirror::ArtMethod* method) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700173 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700174 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700175 CHECK(!kMovingMethods);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700176 return reinterpret_cast<jmethodID>(method);
177 }
178
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700179 bool IsRunnable() const {
180 return self_->GetState() == kRunnable;
181 }
182
183 protected:
184 explicit ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
185 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
186 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {
187 }
188
189 explicit ScopedObjectAccessAlreadyRunnable(Thread* self)
190 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
191 : self_(self), env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
192 vm_(env_ != nullptr ? env_->vm : nullptr) {
193 }
194
195 // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't
196 // change into Runnable or acquire a share on the mutator_lock_.
197 explicit ScopedObjectAccessAlreadyRunnable(JavaVM* vm)
198 : self_(nullptr), env_(nullptr), vm_(down_cast<JavaVMExt*>(vm)) {}
199
200 // Here purely to force inlining.
201 ~ScopedObjectAccessAlreadyRunnable() ALWAYS_INLINE {
202 }
203
204 // Self thread, can be null.
205 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 // The full JNIEnv.
207 JNIEnvExt* const env_;
208 // The full JavaVM.
209 JavaVMExt* const vm_;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700210};
211
212// Entry/exit processing for transitions from Native to Runnable (ie within JNI functions).
213//
214// This class performs the necessary thread state switching to and from Runnable and lets us
215// amortize the cost of working out the current thread. Additionally it lets us check (and repair)
216// apps that are using a JNIEnv on the wrong thread. The class also decodes and encodes Objects
217// into jobjects via methods of this class. Performing this here enforces the Runnable thread state
218// for use of Object, thereby inhibiting the Object being modified by GC whilst native or VM code
219// is also manipulating the Object.
220//
221// The destructor transitions back to the previous thread state, typically Native. In this state
222// GC and thread suspension may occur.
223//
224// For annotalysis the subclass ScopedObjectAccess (below) makes it explicit that a shared of
225// the mutator_lock_ will be acquired on construction.
226class ScopedObjectAccessUnchecked : public ScopedObjectAccessAlreadyRunnable {
227 public:
228 explicit ScopedObjectAccessUnchecked(JNIEnv* env)
229 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
230 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
231 Self()->VerifyStack();
232 Locks::mutator_lock_->AssertSharedHeld(Self());
233 }
234
235 explicit ScopedObjectAccessUnchecked(Thread* self)
236 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
237 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
238 Self()->VerifyStack();
239 Locks::mutator_lock_->AssertSharedHeld(Self());
240 }
241
242 // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't
243 // change into Runnable or acquire a share on the mutator_lock_.
244 explicit ScopedObjectAccessUnchecked(JavaVM* vm) ALWAYS_INLINE
245 : ScopedObjectAccessAlreadyRunnable(vm), tsc_() {}
246
247 private:
248 // The scoped thread state change makes sure that we are runnable and restores the thread state
249 // in the destructor.
250 const ScopedThreadStateChange tsc_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251
252 DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccessUnchecked);
253};
254
255// Annotalysis helping variant of the above.
256class ScopedObjectAccess : public ScopedObjectAccessUnchecked {
257 public:
258 explicit ScopedObjectAccess(JNIEnv* env)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700259 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800260 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700261 : ScopedObjectAccessUnchecked(env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 }
263
264 explicit ScopedObjectAccess(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700266 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 : ScopedObjectAccessUnchecked(self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700268 }
269
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800270 ~ScopedObjectAccess() UNLOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700271 // Base class will release share of lock. Invoked after this destructor.
272 }
273
274 private:
275 // TODO: remove this constructor. It is used by check JNI's ScopedCheck to make it believe that
276 // routines operating with just a VM are sound, they are not, but when you have just a VM
277 // you cannot call the unsound routines.
278 explicit ScopedObjectAccess(JavaVM* vm)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700279 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700280 : ScopedObjectAccessUnchecked(vm) {}
281
282 friend class ScopedCheck;
283 DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccess);
284};
285
286} // namespace art
287
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700288#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_