1 /* 2 * Copyright (C) 2011 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 17 #ifndef ART_RUNTIME_JNI_JNI_ENV_EXT_H_ 18 #define ART_RUNTIME_JNI_JNI_ENV_EXT_H_ 19 20 #include <jni.h> 21 22 #include "base/locks.h" 23 #include "base/macros.h" 24 #include "local_reference_table.h" 25 #include "obj_ptr.h" 26 #include "reference_table.h" 27 28 namespace art { 29 30 class ArtMethod; 31 class ArtField; 32 class JavaVMExt; 33 class ScopedObjectAccess; 34 class ScopedObjectAccessAlreadyRunnable; 35 36 namespace mirror { 37 class Object; 38 } // namespace mirror 39 40 class JNIEnvExt : public JNIEnv { 41 public: 42 // Creates a new JNIEnvExt. Returns null on error, in which case error_msg 43 // will contain a description of the error. 44 static JNIEnvExt* Create(Thread* self, JavaVMExt* vm, std::string* error_msg); 45 static MemberOffset SegmentStateOffset(size_t pointer_size); 46 static MemberOffset LocalRefCookieOffset(size_t pointer_size); 47 static MemberOffset SelfOffset(size_t pointer_size); 48 static jint GetEnvHandler(JavaVMExt* vm, /*out*/void** out, jint version); 49 50 ~JNIEnvExt(); 51 52 void DumpReferenceTables(std::ostream& os) 53 REQUIRES_SHARED(Locks::mutator_lock_) 54 REQUIRES(!Locks::alloc_tracker_lock_); 55 56 void SetCheckJniEnabled(bool enabled) REQUIRES(!Locks::jni_function_table_lock_); 57 58 void PushFrame(int capacity) REQUIRES_SHARED(Locks::mutator_lock_); 59 void PopFrame() REQUIRES_SHARED(Locks::mutator_lock_); 60 61 template<typename T> 62 T AddLocalReference(ObjPtr<mirror::Object> obj) 63 REQUIRES_SHARED(Locks::mutator_lock_) 64 REQUIRES(!Locks::alloc_tracker_lock_); 65 66 void UpdateLocal(IndirectRef iref, ObjPtr<mirror::Object> obj) 67 REQUIRES_SHARED(Locks::mutator_lock_); 68 69 jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); 70 void DeleteLocalRef(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); 71 TrimLocals()72 void TrimLocals() REQUIRES_SHARED(Locks::mutator_lock_) { 73 locals_.Trim(); 74 } AssertLocalsEmpty()75 void AssertLocalsEmpty() REQUIRES_SHARED(Locks::mutator_lock_) { 76 locals_.AssertEmpty(); 77 } GetLocalsCapacity()78 size_t GetLocalsCapacity() REQUIRES_SHARED(Locks::mutator_lock_) { 79 return locals_.Capacity(); 80 } 81 GetLocalRefCookie()82 jni::LRTSegmentState GetLocalRefCookie() const { return local_ref_cookie_; } SetLocalRefCookie(jni::LRTSegmentState new_cookie)83 void SetLocalRefCookie(jni::LRTSegmentState new_cookie) { local_ref_cookie_ = new_cookie; } 84 GetLocalsSegmentState()85 jni::LRTSegmentState GetLocalsSegmentState() const REQUIRES_SHARED(Locks::mutator_lock_) { 86 return locals_.GetSegmentState(); 87 } SetLocalSegmentState(jni::LRTSegmentState new_state)88 void SetLocalSegmentState(jni::LRTSegmentState new_state) REQUIRES_SHARED(Locks::mutator_lock_) { 89 locals_.SetSegmentState(new_state); 90 } 91 VisitJniLocalRoots(RootVisitor * visitor,const RootInfo & root_info)92 void VisitJniLocalRoots(RootVisitor* visitor, const RootInfo& root_info) 93 REQUIRES_SHARED(Locks::mutator_lock_) { 94 locals_.VisitRoots(visitor, root_info); 95 } 96 GetSelf()97 Thread* GetSelf() const { return self_; } GetCritical()98 uint32_t GetCritical() const { return critical_; } SetCritical(uint32_t new_critical)99 void SetCritical(uint32_t new_critical) { critical_ = new_critical; } GetCriticalStartUs()100 uint64_t GetCriticalStartUs() const { return critical_start_us_; } SetCriticalStartUs(uint64_t new_critical_start_us)101 void SetCriticalStartUs(uint64_t new_critical_start_us) { 102 critical_start_us_ = new_critical_start_us; 103 } GetUncheckedFunctions()104 const JNINativeInterface* GetUncheckedFunctions() const { 105 return unchecked_functions_; 106 } GetVm()107 JavaVMExt* GetVm() const { return vm_; } 108 SetRuntimeDeleted()109 void SetRuntimeDeleted() { runtime_deleted_.store(true, std::memory_order_relaxed); } IsRuntimeDeleted()110 bool IsRuntimeDeleted() const { return runtime_deleted_.load(std::memory_order_relaxed); } IsCheckJniEnabled()111 bool IsCheckJniEnabled() const { return check_jni_; } 112 113 114 // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking 115 // rules in CheckJNI mode. 116 117 // Record locking of a monitor. 118 void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); 119 120 // Check the release, that is, that the release is performed in the same JNI "segment." 121 void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); 122 123 // Check that no monitors are held that have been acquired in this JNI "segment." 124 void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_); 125 VisitMonitorRoots(RootVisitor * visitor,const RootInfo & root_info)126 void VisitMonitorRoots(RootVisitor* visitor, const RootInfo& root_info) 127 REQUIRES_SHARED(Locks::mutator_lock_) { 128 monitors_.VisitRoots(visitor, root_info); 129 } 130 131 // Set the functions to the runtime shutdown functions. 132 void SetFunctionsToRuntimeShutdownFunctions(); 133 134 // Set the functions to the new JNI functions based on Runtime::GetJniIdType. 135 void UpdateJniFunctionsPointer(); 136 137 // Set the function table override. This will install the override (or original table, if null) 138 // to all threads. 139 // Note: JNI function table overrides are sensitive to the order of operations wrt/ CheckJNI. 140 // After overriding the JNI function table, CheckJNI toggling is ignored. 141 static void SetTableOverride(const JNINativeInterface* table_override) 142 REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_); 143 144 // Return either the regular, or the CheckJNI function table. Will return table_override_ instead 145 // if it is not null. 146 static const JNINativeInterface* GetFunctionTable(bool check_jni) 147 REQUIRES(Locks::jni_function_table_lock_); 148 149 static void ResetFunctionTable() 150 REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_); 151 152 private: 153 // Override of function tables. This applies to both default as well as instrumented (CheckJNI) 154 // function tables. 155 static const JNINativeInterface* table_override_ GUARDED_BY(Locks::jni_function_table_lock_); 156 157 // The constructor should not be called directly. Use `Create()` that initializes 158 // the new `JNIEnvExt` object by calling `Initialize()`. 159 JNIEnvExt(Thread* self, JavaVMExt* vm) 160 REQUIRES(!Locks::jni_function_table_lock_); 161 162 // Initialize the `JNIEnvExt` object. 163 bool Initialize(std::string* error_msg); 164 165 // Link to Thread::Current(). 166 Thread* const self_; 167 168 // The invocation interface JavaVM. 169 JavaVMExt* const vm_; 170 171 // Cookie used when using the local indirect reference table. 172 jni::LRTSegmentState local_ref_cookie_; 173 174 // JNI local references. 175 jni::LocalReferenceTable locals_; 176 177 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls. 178 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return) 179 // to a native method. 180 std::vector<jni::LRTSegmentState> stacked_local_ref_cookies_; 181 182 // Entered JNI monitors, for bulk exit on thread detach. 183 ReferenceTable monitors_; 184 185 // Used by -Xcheck:jni. 186 JNINativeInterface const* unchecked_functions_; 187 188 // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI 189 // to ensure that only monitors locked in this native frame are being unlocked, and that at 190 // the end all are unlocked. 191 std::vector<std::pair<uintptr_t, jobject>> locked_objects_; 192 193 // Start time of "critical" JNI calls to ensure that their use doesn't 194 // excessively block the VM with CheckJNI. 195 uint64_t critical_start_us_; 196 197 // How many nested "critical" JNI calls are we in? Used by CheckJNI to ensure that criticals are 198 uint32_t critical_; 199 200 // Frequently-accessed fields cached from JavaVM. 201 bool check_jni_; 202 203 // If we are a JNI env for a daemon thread with a deleted runtime. 204 std::atomic<bool> runtime_deleted_; 205 206 template<bool kEnableIndexIds> friend class JNI; 207 friend class ScopedJniEnvLocalRefState; 208 friend class Thread; 209 friend IndirectReferenceTable* GetIndirectReferenceTable(ScopedObjectAccess& soa, 210 IndirectRefKind kind); 211 friend jni::LocalReferenceTable* GetLocalReferenceTable(ScopedObjectAccess& soa); 212 friend void ThreadResetFunctionTable(Thread* thread, void* arg); 213 ART_FRIEND_TEST(JniInternalTest, JNIEnvExtOffsets); 214 }; 215 216 // Used to save and restore the JNIEnvExt state when not going through code created by the JNI 217 // compiler. 218 class ScopedJniEnvLocalRefState { 219 public: ScopedJniEnvLocalRefState(JNIEnvExt * env)220 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : 221 env_(env), 222 saved_local_ref_cookie_(env->local_ref_cookie_) { 223 env->local_ref_cookie_ = env->locals_.GetSegmentState(); 224 } 225 ~ScopedJniEnvLocalRefState()226 ~ScopedJniEnvLocalRefState() { 227 env_->locals_.SetSegmentState(env_->local_ref_cookie_); 228 env_->local_ref_cookie_ = saved_local_ref_cookie_; 229 } 230 231 private: 232 JNIEnvExt* const env_; 233 const jni::LRTSegmentState saved_local_ref_cookie_; 234 235 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState); 236 }; 237 238 } // namespace art 239 240 #endif // ART_RUNTIME_JNI_JNI_ENV_EXT_H_ 241