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