Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1 | /* |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |
| 18 | #define ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 20 | #include "jni.h" |
| 21 | |
| 22 | #include "base/macros.h" |
| 23 | #include "base/mutex.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "base/value_object.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "thread_state.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 29 | class JavaVMExt; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 30 | class JNIEnvExt; |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 31 | template<class MirrorType> class ObjPtr; |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 32 | class Thread; |
| 33 | |
| 34 | namespace mirror { |
| 35 | class Object; |
| 36 | } // namespace mirror |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 37 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | // Scoped change into and out of a particular state. Handles Runnable transitions that require |
| 39 | // more complicated suspension checking. The subclasses ScopedObjectAccessUnchecked and |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 40 | // ScopedObjectAccess are used to handle the change into Runnable to Get direct access to objects, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 41 | // the unchecked variant doesn't aid annotalysis. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 42 | class ScopedThreadStateChange : public ValueObject { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 43 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 44 | ALWAYS_INLINE ScopedThreadStateChange(Thread* self, ThreadState new_thread_state) |
| 45 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 46 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 47 | ALWAYS_INLINE ~ScopedThreadStateChange() REQUIRES(!Locks::thread_suspend_count_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 48 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 49 | ALWAYS_INLINE Thread* Self() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | return self_; |
| 51 | } |
| 52 | |
| 53 | protected: |
| 54 | // Constructor used by ScopedJniThreadState for an unattached thread that has access to the VM*. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 55 | ScopedThreadStateChange() {} |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 57 | Thread* const self_ = nullptr; |
| 58 | const ThreadState thread_state_ = kTerminated; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 61 | ThreadState old_thread_state_ = kTerminated; |
| 62 | const bool expected_has_no_thread_ = true; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 64 | friend class ScopedObjectAccessUnchecked; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 65 | DISALLOW_COPY_AND_ASSIGN(ScopedThreadStateChange); |
| 66 | }; |
| 67 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 68 | // Assumes we are already runnable. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 69 | class ScopedObjectAccessAlreadyRunnable : public ValueObject { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 70 | public: |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 71 | Thread* Self() const { |
| 72 | return self_; |
Ian Rogers | c0fa3ad | 2013-02-05 00:11:55 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | JNIEnvExt* Env() const { |
| 76 | return env_; |
| 77 | } |
| 78 | |
| 79 | JavaVMExt* Vm() const { |
| 80 | return vm_; |
| 81 | } |
| 82 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 83 | bool ForceCopy() const; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 84 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Add a local reference for an object to the indirect reference table associated with the |
| 87 | * current stack frame. When the native function returns, the reference will be discarded. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 88 | * |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 89 | * We need to allow the same reference to be added multiple times, and cope with nullptr. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 90 | * |
Elliott Hughes | 9dcd45c | 2013-07-29 14:40:52 -0700 | [diff] [blame] | 91 | * This will be called on otherwise unreferenced objects. We cannot do GC allocations here, and |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 92 | * it's best if we don't grab a mutex. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 93 | */ |
| 94 | template<typename T> |
Mathieu Chartier | 8778c52 | 2016-10-04 19:06:30 -0700 | [diff] [blame] | 95 | T AddLocalReference(ObjPtr<mirror::Object> obj) const |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 96 | REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 97 | |
Andreas Gampe | c73cb64 | 2017-02-22 10:11:30 -0800 | [diff] [blame] | 98 | template<typename T> |
| 99 | ObjPtr<T> Decode(jobject obj) const REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 100 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 101 | ALWAYS_INLINE bool IsRunnable() const; |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 102 | |
| 103 | protected: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 104 | ALWAYS_INLINE explicit ScopedObjectAccessAlreadyRunnable(JNIEnv* env) |
| 105 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 106 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 107 | ALWAYS_INLINE explicit ScopedObjectAccessAlreadyRunnable(Thread* self) |
| 108 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 109 | |
| 110 | // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't |
| 111 | // change into Runnable or acquire a share on the mutator_lock_. |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 112 | // Note: The reinterpret_cast is backed by a static_assert in the cc file. Avoid a down_cast, |
| 113 | // as it prevents forward declaration of JavaVMExt. |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 114 | explicit ScopedObjectAccessAlreadyRunnable(JavaVM* vm) |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 115 | : self_(nullptr), env_(nullptr), vm_(reinterpret_cast<JavaVMExt*>(vm)) {} |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 116 | |
| 117 | // Here purely to force inlining. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 118 | ALWAYS_INLINE ~ScopedObjectAccessAlreadyRunnable() {} |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 119 | |
Andreas Gampe | c15a2f4 | 2017-04-21 12:09:39 -0700 | [diff] [blame] | 120 | static void DCheckObjIsNotClearedJniWeakGlobal(ObjPtr<mirror::Object> obj) |
| 121 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 122 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 123 | // Self thread, can be null. |
| 124 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | // The full JNIEnv. |
| 126 | JNIEnvExt* const env_; |
| 127 | // The full JavaVM. |
| 128 | JavaVMExt* const vm_; |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // Entry/exit processing for transitions from Native to Runnable (ie within JNI functions). |
| 132 | // |
| 133 | // This class performs the necessary thread state switching to and from Runnable and lets us |
| 134 | // amortize the cost of working out the current thread. Additionally it lets us check (and repair) |
| 135 | // apps that are using a JNIEnv on the wrong thread. The class also decodes and encodes Objects |
| 136 | // into jobjects via methods of this class. Performing this here enforces the Runnable thread state |
| 137 | // for use of Object, thereby inhibiting the Object being modified by GC whilst native or VM code |
| 138 | // is also manipulating the Object. |
| 139 | // |
| 140 | // The destructor transitions back to the previous thread state, typically Native. In this state |
| 141 | // GC and thread suspension may occur. |
| 142 | // |
| 143 | // For annotalysis the subclass ScopedObjectAccess (below) makes it explicit that a shared of |
| 144 | // the mutator_lock_ will be acquired on construction. |
| 145 | class ScopedObjectAccessUnchecked : public ScopedObjectAccessAlreadyRunnable { |
| 146 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 147 | ALWAYS_INLINE explicit ScopedObjectAccessUnchecked(JNIEnv* env) |
| 148 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 149 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 150 | ALWAYS_INLINE explicit ScopedObjectAccessUnchecked(Thread* self) |
| 151 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 152 | |
Mingyao Yang | 01b47b0 | 2017-02-03 12:09:57 -0800 | [diff] [blame] | 153 | ALWAYS_INLINE ~ScopedObjectAccessUnchecked() REQUIRES(!Locks::thread_suspend_count_lock_) {} |
| 154 | |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 155 | // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't |
| 156 | // change into Runnable or acquire a share on the mutator_lock_. |
| 157 | explicit ScopedObjectAccessUnchecked(JavaVM* vm) ALWAYS_INLINE |
| 158 | : ScopedObjectAccessAlreadyRunnable(vm), tsc_() {} |
| 159 | |
| 160 | private: |
| 161 | // The scoped thread state change makes sure that we are runnable and restores the thread state |
| 162 | // in the destructor. |
| 163 | const ScopedThreadStateChange tsc_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccessUnchecked); |
| 166 | }; |
| 167 | |
| 168 | // Annotalysis helping variant of the above. |
| 169 | class ScopedObjectAccess : public ScopedObjectAccessUnchecked { |
| 170 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 171 | ALWAYS_INLINE explicit ScopedObjectAccess(JNIEnv* env) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 172 | REQUIRES(!Locks::thread_suspend_count_lock_) |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 173 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 174 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 175 | ALWAYS_INLINE explicit ScopedObjectAccess(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 176 | REQUIRES(!Locks::thread_suspend_count_lock_) |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 177 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 178 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 179 | // Base class will release share of lock. Invoked after this destructor. |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 180 | ~ScopedObjectAccess() UNLOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 181 | |
| 182 | private: |
| 183 | // TODO: remove this constructor. It is used by check JNI's ScopedCheck to make it believe that |
| 184 | // routines operating with just a VM are sound, they are not, but when you have just a VM |
| 185 | // you cannot call the unsound routines. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 186 | explicit ScopedObjectAccess(JavaVM* vm) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 187 | : ScopedObjectAccessUnchecked(vm) {} |
| 188 | |
| 189 | friend class ScopedCheck; |
| 190 | DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccess); |
| 191 | }; |
| 192 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 193 | // Annotalysis helper for going to a suspended state from runnable. |
| 194 | class ScopedThreadSuspension : public ValueObject { |
| 195 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 196 | ALWAYS_INLINE explicit ScopedThreadSuspension(Thread* self, ThreadState suspended_state) |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 197 | REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_) |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 198 | UNLOCK_FUNCTION(Locks::mutator_lock_); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 199 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 200 | ALWAYS_INLINE ~ScopedThreadSuspension() SHARED_LOCK_FUNCTION(Locks::mutator_lock_); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 201 | |
| 202 | private: |
| 203 | Thread* const self_; |
| 204 | const ThreadState suspended_state_; |
| 205 | DISALLOW_COPY_AND_ASSIGN(ScopedThreadSuspension); |
| 206 | }; |
| 207 | |
| 208 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 209 | } // namespace art |
| 210 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 211 | #endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |