blob: 99750a16d0942b461f2e17b07a50fdb2744e235b [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 Rogers68d8b422014-07-17 11:09:10 -070021#include "java_vm_ext.h"
22#include "jni_env_ext-inl.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field.h"
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070024#include "read_barrier.h"
Ian Rogers693ff612013-02-01 10:56:12 -080025#include "thread-inl.h"
Mathieu Chartier4e305412014-02-19 10:54:44 -080026#include "verify_object.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027
28namespace art {
29
30// Scoped change into and out of a particular state. Handles Runnable transitions that require
31// more complicated suspension checking. The subclasses ScopedObjectAccessUnchecked and
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032// ScopedObjectAccess are used to handle the change into Runnable to Get direct access to objects,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033// the unchecked variant doesn't aid annotalysis.
34class ScopedThreadStateChange {
35 public:
36 ScopedThreadStateChange(Thread* self, ThreadState new_thread_state)
Ian Rogers1ffa32f2013-02-05 18:29:08 -080037 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 : self_(self), thread_state_(new_thread_state), expected_has_no_thread_(false) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070039 if (UNLIKELY(self_ == nullptr)) {
40 // Value chosen arbitrarily and won't be used in the destructor since thread_ == null.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 old_thread_state_ = kTerminated;
Ian Rogers120f1c72012-09-28 17:17:10 -070042 Runtime* runtime = Runtime::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070043 CHECK(runtime == nullptr || !runtime->IsStarted() || runtime->IsShuttingDown(self_));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070044 } else {
Ian Rogers22f454c2012-09-08 11:06:29 -070045 DCHECK_EQ(self, Thread::Current());
46 // Read state without locks, ok as state is effectively thread local and we're not interested
47 // in the suspend count (this will be handled in the runnable transitions).
Ian Rogers474b6da2012-09-25 00:20:38 -070048 old_thread_state_ = self->GetState();
Mathieu Chartier92b78892014-04-24 16:14:43 -070049 if (old_thread_state_ != new_thread_state) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 if (new_thread_state == kRunnable) {
51 self_->TransitionFromSuspendedToRunnable();
Mathieu Chartier92b78892014-04-24 16:14:43 -070052 } else if (old_thread_state_ == kRunnable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070053 self_->TransitionFromRunnableToSuspended(new_thread_state);
Mathieu Chartier92b78892014-04-24 16:14:43 -070054 } else {
55 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
56 self_->SetState(new_thread_state);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070057 }
58 }
59 }
60 }
61
Ian Rogers1ffa32f2013-02-05 18:29:08 -080062 ~ScopedThreadStateChange() LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070063 if (UNLIKELY(self_ == nullptr)) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 if (!expected_has_no_thread_) {
Ian Rogers120f1c72012-09-28 17:17:10 -070065 Runtime* runtime = Runtime::Current();
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 bool shutting_down = (runtime == nullptr) || runtime->IsShuttingDown(nullptr);
Ian Rogers120f1c72012-09-28 17:17:10 -070067 CHECK(shutting_down);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 }
69 } else {
70 if (old_thread_state_ != thread_state_) {
71 if (old_thread_state_ == kRunnable) {
72 self_->TransitionFromSuspendedToRunnable();
73 } else if (thread_state_ == kRunnable) {
74 self_->TransitionFromRunnableToSuspended(old_thread_state_);
75 } else {
Ian Rogers22f454c2012-09-08 11:06:29 -070076 // A suspended transition to another effectively suspended transition, ok to use Unsafe.
Ian Rogers474b6da2012-09-25 00:20:38 -070077 self_->SetState(old_thread_state_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070078 }
79 }
80 }
81 }
82
83 Thread* Self() const {
84 return self_;
85 }
86
87 protected:
88 // Constructor used by ScopedJniThreadState for an unattached thread that has access to the VM*.
89 ScopedThreadStateChange()
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 : self_(nullptr), thread_state_(kTerminated), old_thread_state_(kTerminated),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 expected_has_no_thread_(true) {}
92
93 Thread* const self_;
94 const ThreadState thread_state_;
95
96 private:
97 ThreadState old_thread_state_;
98 const bool expected_has_no_thread_;
99
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700100 friend class ScopedObjectAccessUnchecked;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange);
102};
103
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700104// Assumes we are already runnable.
105class ScopedObjectAccessAlreadyRunnable {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 public:
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700107 Thread* Self() const {
108 return self_;
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800109 }
110
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 JNIEnvExt* Env() const {
112 return env_;
113 }
114
115 JavaVMExt* Vm() const {
116 return vm_;
117 }
118
Ian Rogers68d8b422014-07-17 11:09:10 -0700119 bool ForceCopy() const {
120 return vm_->ForceCopy();
121 }
122
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123 /*
124 * Add a local reference for an object to the indirect reference table associated with the
125 * current stack frame. When the native function returns, the reference will be discarded.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700126 *
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700127 * We need to allow the same reference to be added multiple times, and cope with nullptr.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 *
Elliott Hughes9dcd45c2013-07-29 14:40:52 -0700129 * This will be called on otherwise unreferenced objects. We cannot do GC allocations here, and
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 * it's best if we don't grab a mutex.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 */
132 template<typename T>
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 T AddLocalReference(mirror::Object* obj) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700134 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700135 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartiere1a01532015-05-04 11:46:25 -0700136 return obj == nullptr ? nullptr : Env()->AddLocalReference<T>(obj);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700137 }
138
139 template<typename T>
140 T Decode(jobject obj) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700142 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700143 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 return down_cast<T>(Self()->DecodeJObject(obj));
145 }
146
Mathieu Chartierc7853442015-03-27 14:35:38 -0700147 ArtField* DecodeField(jfieldID fid) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700148 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700149 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700150 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700151 return reinterpret_cast<ArtField*>(fid);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 }
153
Mathieu Chartierc7853442015-03-27 14:35:38 -0700154 jfieldID EncodeField(ArtField* field) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700155 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700156 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 return reinterpret_cast<jfieldID>(field);
158 }
159
Brian Carlstromea46f952013-07-30 01:26:50 -0700160 mirror::ArtMethod* DecodeMethod(jmethodID mid) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700162 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700163 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700164 CHECK(!kMovingMethods);
Hiroshi Yamauchiea2e1bd2014-06-18 13:47:35 -0700165 mirror::ArtMethod* method = reinterpret_cast<mirror::ArtMethod*>(mid);
166 return ReadBarrier::BarrierForRoot<mirror::ArtMethod, kWithReadBarrier>(&method);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700167 }
168
Brian Carlstromea46f952013-07-30 01:26:50 -0700169 jmethodID EncodeMethod(mirror::ArtMethod* method) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700171 Locks::mutator_lock_->AssertSharedHeld(Self());
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700172 DCHECK(IsRunnable()); // Don't work with raw objects in non-runnable states.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700173 CHECK(!kMovingMethods);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700174 return reinterpret_cast<jmethodID>(method);
175 }
176
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700177 bool IsRunnable() const {
178 return self_->GetState() == kRunnable;
179 }
180
181 protected:
182 explicit ScopedObjectAccessAlreadyRunnable(JNIEnv* env)
183 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
184 : self_(ThreadForEnv(env)), env_(down_cast<JNIEnvExt*>(env)), vm_(env_->vm) {
185 }
186
187 explicit ScopedObjectAccessAlreadyRunnable(Thread* self)
188 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
189 : self_(self), env_(down_cast<JNIEnvExt*>(self->GetJniEnv())),
190 vm_(env_ != nullptr ? env_->vm : nullptr) {
191 }
192
193 // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't
194 // change into Runnable or acquire a share on the mutator_lock_.
195 explicit ScopedObjectAccessAlreadyRunnable(JavaVM* vm)
196 : self_(nullptr), env_(nullptr), vm_(down_cast<JavaVMExt*>(vm)) {}
197
198 // Here purely to force inlining.
199 ~ScopedObjectAccessAlreadyRunnable() ALWAYS_INLINE {
200 }
201
202 // Self thread, can be null.
203 Thread* const self_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700204 // The full JNIEnv.
205 JNIEnvExt* const env_;
206 // The full JavaVM.
207 JavaVMExt* const vm_;
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700208};
209
210// Entry/exit processing for transitions from Native to Runnable (ie within JNI functions).
211//
212// This class performs the necessary thread state switching to and from Runnable and lets us
213// amortize the cost of working out the current thread. Additionally it lets us check (and repair)
214// apps that are using a JNIEnv on the wrong thread. The class also decodes and encodes Objects
215// into jobjects via methods of this class. Performing this here enforces the Runnable thread state
216// for use of Object, thereby inhibiting the Object being modified by GC whilst native or VM code
217// is also manipulating the Object.
218//
219// The destructor transitions back to the previous thread state, typically Native. In this state
220// GC and thread suspension may occur.
221//
222// For annotalysis the subclass ScopedObjectAccess (below) makes it explicit that a shared of
223// the mutator_lock_ will be acquired on construction.
224class ScopedObjectAccessUnchecked : public ScopedObjectAccessAlreadyRunnable {
225 public:
226 explicit ScopedObjectAccessUnchecked(JNIEnv* env)
227 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
228 : ScopedObjectAccessAlreadyRunnable(env), tsc_(Self(), kRunnable) {
229 Self()->VerifyStack();
230 Locks::mutator_lock_->AssertSharedHeld(Self());
231 }
232
233 explicit ScopedObjectAccessUnchecked(Thread* self)
234 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) ALWAYS_INLINE
235 : ScopedObjectAccessAlreadyRunnable(self), tsc_(self, kRunnable) {
236 Self()->VerifyStack();
237 Locks::mutator_lock_->AssertSharedHeld(Self());
238 }
239
240 // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't
241 // change into Runnable or acquire a share on the mutator_lock_.
242 explicit ScopedObjectAccessUnchecked(JavaVM* vm) ALWAYS_INLINE
243 : ScopedObjectAccessAlreadyRunnable(vm), tsc_() {}
244
245 private:
246 // The scoped thread state change makes sure that we are runnable and restores the thread state
247 // in the destructor.
248 const ScopedThreadStateChange tsc_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700249
250 DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccessUnchecked);
251};
252
253// Annotalysis helping variant of the above.
254class ScopedObjectAccess : public ScopedObjectAccessUnchecked {
255 public:
256 explicit ScopedObjectAccess(JNIEnv* env)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800258 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 : ScopedObjectAccessUnchecked(env) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 }
261
262 explicit ScopedObjectAccess(Thread* self)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
Mathieu Chartier2b7c4d12014-05-19 10:52:16 -0700264 SHARED_LOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700265 : ScopedObjectAccessUnchecked(self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700266 }
267
Ian Rogers1ffa32f2013-02-05 18:29:08 -0800268 ~ScopedObjectAccess() UNLOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 // Base class will release share of lock. Invoked after this destructor.
270 }
271
272 private:
273 // TODO: remove this constructor. It is used by check JNI's ScopedCheck to make it believe that
274 // routines operating with just a VM are sound, they are not, but when you have just a VM
275 // you cannot call the unsound routines.
276 explicit ScopedObjectAccess(JavaVM* vm)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700277 SHARED_LOCK_FUNCTION(Locks::mutator_lock_)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700278 : ScopedObjectAccessUnchecked(vm) {}
279
280 friend class ScopedCheck;
281 DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccess);
282};
283
284} // namespace art
285
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700286#endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_