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 | ArtField* DecodeField(jfieldID fid) const REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 98 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 99 | jfieldID EncodeField(ArtField* field) 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 | ArtMethod* DecodeMethod(jmethodID mid) 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 | jmethodID EncodeMethod(ArtMethod* method) const REQUIRES_SHARED(Locks::mutator_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 104 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 105 | ALWAYS_INLINE bool IsRunnable() const; |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 106 | |
| 107 | protected: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 108 | ALWAYS_INLINE explicit ScopedObjectAccessAlreadyRunnable(JNIEnv* env) |
| 109 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 110 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 111 | ALWAYS_INLINE explicit ScopedObjectAccessAlreadyRunnable(Thread* self) |
| 112 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 113 | |
| 114 | // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't |
| 115 | // change into Runnable or acquire a share on the mutator_lock_. |
| 116 | explicit ScopedObjectAccessAlreadyRunnable(JavaVM* vm) |
| 117 | : self_(nullptr), env_(nullptr), vm_(down_cast<JavaVMExt*>(vm)) {} |
| 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 | |
| 122 | // Self thread, can be null. |
| 123 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | // The full JNIEnv. |
| 125 | JNIEnvExt* const env_; |
| 126 | // The full JavaVM. |
| 127 | JavaVMExt* const vm_; |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | // Entry/exit processing for transitions from Native to Runnable (ie within JNI functions). |
| 131 | // |
| 132 | // This class performs the necessary thread state switching to and from Runnable and lets us |
| 133 | // amortize the cost of working out the current thread. Additionally it lets us check (and repair) |
| 134 | // apps that are using a JNIEnv on the wrong thread. The class also decodes and encodes Objects |
| 135 | // into jobjects via methods of this class. Performing this here enforces the Runnable thread state |
| 136 | // for use of Object, thereby inhibiting the Object being modified by GC whilst native or VM code |
| 137 | // is also manipulating the Object. |
| 138 | // |
| 139 | // The destructor transitions back to the previous thread state, typically Native. In this state |
| 140 | // GC and thread suspension may occur. |
| 141 | // |
| 142 | // For annotalysis the subclass ScopedObjectAccess (below) makes it explicit that a shared of |
| 143 | // the mutator_lock_ will be acquired on construction. |
| 144 | class ScopedObjectAccessUnchecked : public ScopedObjectAccessAlreadyRunnable { |
| 145 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 146 | ALWAYS_INLINE explicit ScopedObjectAccessUnchecked(JNIEnv* env) |
| 147 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 148 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 149 | ALWAYS_INLINE explicit ScopedObjectAccessUnchecked(Thread* self) |
| 150 | REQUIRES(!Locks::thread_suspend_count_lock_); |
Mathieu Chartier | 2b7c4d1 | 2014-05-19 10:52:16 -0700 | [diff] [blame] | 151 | |
| 152 | // Used when we want a scoped JNI thread state but have no thread/JNIEnv. Consequently doesn't |
| 153 | // change into Runnable or acquire a share on the mutator_lock_. |
| 154 | explicit ScopedObjectAccessUnchecked(JavaVM* vm) ALWAYS_INLINE |
| 155 | : ScopedObjectAccessAlreadyRunnable(vm), tsc_() {} |
| 156 | |
| 157 | private: |
| 158 | // The scoped thread state change makes sure that we are runnable and restores the thread state |
| 159 | // in the destructor. |
| 160 | const ScopedThreadStateChange tsc_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 161 | |
| 162 | DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccessUnchecked); |
| 163 | }; |
| 164 | |
| 165 | // Annotalysis helping variant of the above. |
| 166 | class ScopedObjectAccess : public ScopedObjectAccessUnchecked { |
| 167 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 168 | ALWAYS_INLINE explicit ScopedObjectAccess(JNIEnv* env) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 169 | REQUIRES(!Locks::thread_suspend_count_lock_) |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 170 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
| 171 | : ScopedObjectAccessUnchecked(env) {} |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 172 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 173 | ALWAYS_INLINE explicit ScopedObjectAccess(Thread* self) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 174 | REQUIRES(!Locks::thread_suspend_count_lock_) |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 175 | SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
| 176 | : ScopedObjectAccessUnchecked(self) {} |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 177 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 178 | // Base class will release share of lock. Invoked after this destructor. |
| 179 | ~ScopedObjectAccess() UNLOCK_FUNCTION(Locks::mutator_lock_) ALWAYS_INLINE {} |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 180 | |
| 181 | private: |
| 182 | // TODO: remove this constructor. It is used by check JNI's ScopedCheck to make it believe that |
| 183 | // routines operating with just a VM are sound, they are not, but when you have just a VM |
| 184 | // you cannot call the unsound routines. |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 185 | explicit ScopedObjectAccess(JavaVM* vm) SHARED_LOCK_FUNCTION(Locks::mutator_lock_) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 186 | : ScopedObjectAccessUnchecked(vm) {} |
| 187 | |
| 188 | friend class ScopedCheck; |
| 189 | DISALLOW_COPY_AND_ASSIGN(ScopedObjectAccess); |
| 190 | }; |
| 191 | |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 192 | // Annotalysis helper for going to a suspended state from runnable. |
| 193 | class ScopedThreadSuspension : public ValueObject { |
| 194 | public: |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 195 | ALWAYS_INLINE explicit ScopedThreadSuspension(Thread* self, ThreadState suspended_state) |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 196 | REQUIRES(!Locks::thread_suspend_count_lock_, !Roles::uninterruptible_) |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 197 | UNLOCK_FUNCTION(Locks::mutator_lock_); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 198 | |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 199 | ALWAYS_INLINE ~ScopedThreadSuspension() SHARED_LOCK_FUNCTION(Locks::mutator_lock_); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 200 | |
| 201 | private: |
| 202 | Thread* const self_; |
| 203 | const ThreadState suspended_state_; |
| 204 | DISALLOW_COPY_AND_ASSIGN(ScopedThreadSuspension); |
| 205 | }; |
| 206 | |
| 207 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 208 | } // namespace art |
| 209 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 210 | #endif // ART_RUNTIME_SCOPED_THREAD_STATE_CHANGE_H_ |