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