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 HIDDEN { 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 LrtSegmentStateOffset(PointerSize pointer_size); 46 static MemberOffset LrtPreviousStateOffset(PointerSize pointer_size); 47 static MemberOffset SelfOffset(PointerSize 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 EXPORT jobject NewLocalRef(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_); 70 EXPORT 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 PushLocalReferenceFrame()82 jni::LRTSegmentState PushLocalReferenceFrame() { 83 return locals_.PushFrame(); 84 } PopLocalReferenceFrame(jni::LRTSegmentState previous_state)85 void PopLocalReferenceFrame(jni::LRTSegmentState previous_state) { 86 locals_.PopFrame(previous_state); 87 } 88 VisitJniLocalRoots(RootVisitor * visitor,const RootInfo & root_info)89 void VisitJniLocalRoots(RootVisitor* visitor, const RootInfo& root_info) 90 REQUIRES_SHARED(Locks::mutator_lock_) { 91 locals_.VisitRoots(visitor, root_info); 92 } 93 GetSelf()94 Thread* GetSelf() const { return self_; } GetCritical()95 uint32_t GetCritical() const { return critical_; } SetCritical(uint32_t new_critical)96 void SetCritical(uint32_t new_critical) { critical_ = new_critical; } GetCriticalStartUs()97 uint64_t GetCriticalStartUs() const { return critical_start_us_; } SetCriticalStartUs(uint64_t new_critical_start_us)98 void SetCriticalStartUs(uint64_t new_critical_start_us) { 99 critical_start_us_ = new_critical_start_us; 100 } GetUncheckedFunctions()101 const JNINativeInterface* GetUncheckedFunctions() const { 102 return unchecked_functions_; 103 } GetVm()104 JavaVMExt* GetVm() const { return vm_; } 105 SetRuntimeDeleted()106 void SetRuntimeDeleted() { runtime_deleted_.store(true, std::memory_order_relaxed); } IsRuntimeDeleted()107 bool IsRuntimeDeleted() const { return runtime_deleted_.load(std::memory_order_relaxed); } IsCheckJniEnabled()108 bool IsCheckJniEnabled() const { return check_jni_; } 109 110 111 // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking 112 // rules in CheckJNI mode. 113 114 // Record locking of a monitor. 115 void RecordMonitorEnter(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); 116 117 // Check the release, that is, that the release is performed in the same JNI "segment." 118 void CheckMonitorRelease(jobject obj) REQUIRES_SHARED(Locks::mutator_lock_); 119 120 // Check that no monitors are held that have been acquired in this JNI "segment." 121 void CheckNoHeldMonitors() REQUIRES_SHARED(Locks::mutator_lock_); 122 VisitMonitorRoots(RootVisitor * visitor,const RootInfo & root_info)123 void VisitMonitorRoots(RootVisitor* visitor, const RootInfo& root_info) 124 REQUIRES_SHARED(Locks::mutator_lock_) { 125 monitors_.VisitRoots(visitor, root_info); 126 } 127 128 // Set the functions to the runtime shutdown functions. 129 void SetFunctionsToRuntimeShutdownFunctions(); 130 131 // Set the functions to the new JNI functions based on Runtime::GetJniIdType. 132 void UpdateJniFunctionsPointer(); 133 134 // Set the function table override. This will install the override (or original table, if null) 135 // to all threads. 136 // Note: JNI function table overrides are sensitive to the order of operations wrt/ CheckJNI. 137 // After overriding the JNI function table, CheckJNI toggling is ignored. 138 EXPORT static void SetTableOverride(const JNINativeInterface* table_override) 139 REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_); 140 141 // Return either the regular, or the CheckJNI function table. Will return table_override_ instead 142 // if it is not null. 143 EXPORT static const JNINativeInterface* GetFunctionTable(bool check_jni) 144 REQUIRES(Locks::jni_function_table_lock_); 145 146 static void ResetFunctionTable() 147 REQUIRES(!Locks::thread_list_lock_, !Locks::jni_function_table_lock_); 148 149 private: 150 static MemberOffset LocalReferenceTableOffset(PointerSize pointer_size); 151 152 // Override of function tables. This applies to both default as well as instrumented (CheckJNI) 153 // function tables. 154 static const JNINativeInterface* table_override_ GUARDED_BY(Locks::jni_function_table_lock_); 155 156 // The constructor should not be called directly. Use `Create()` that initializes 157 // the new `JNIEnvExt` object by calling `Initialize()`. 158 JNIEnvExt(Thread* self, JavaVMExt* vm) 159 REQUIRES(!Locks::jni_function_table_lock_); 160 161 // Initialize the `JNIEnvExt` object. 162 bool Initialize(std::string* error_msg); 163 164 // Link to Thread::Current(). 165 Thread* const self_; 166 167 // The invocation interface JavaVM. 168 JavaVMExt* const vm_; 169 170 // JNI local references. 171 jni::LocalReferenceTable locals_; 172 173 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls. 174 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return) 175 // to a native method. 176 std::vector<jni::LRTSegmentState> stacked_local_ref_cookies_; 177 178 // Entered JNI monitors, for bulk exit on thread detach. 179 ReferenceTable monitors_; 180 181 // Used by -Xcheck:jni. 182 JNINativeInterface const* unchecked_functions_; 183 184 // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI 185 // to ensure that only monitors locked in this native frame are being unlocked, and that at 186 // the end all are unlocked. 187 std::vector<std::pair<uintptr_t, jobject>> locked_objects_; 188 189 // Start time of "critical" JNI calls to ensure that their use doesn't 190 // excessively block the VM with CheckJNI. 191 uint64_t critical_start_us_; 192 193 // How many nested "critical" JNI calls are we in? Used by CheckJNI to ensure that criticals are 194 uint32_t critical_; 195 196 // Frequently-accessed fields cached from JavaVM. 197 bool check_jni_; 198 199 // If we are a JNI env for a daemon thread with a deleted runtime. 200 std::atomic<bool> runtime_deleted_; 201 202 template<bool kEnableIndexIds> friend class JNI; 203 friend class Thread; 204 friend IndirectReferenceTable* GetIndirectReferenceTable(ScopedObjectAccess& soa, 205 IndirectRefKind kind); 206 friend jni::LocalReferenceTable* GetLocalReferenceTable(ScopedObjectAccess& soa); 207 friend void ThreadResetFunctionTable(Thread* thread, void* arg); 208 ART_FRIEND_TEST(JniInternalTest, JNIEnvExtOffsets); 209 }; 210 211 // Used to save and restore the JNIEnvExt state when not going through code created by the JNI 212 // compiler. 213 class ScopedJniEnvLocalRefState { 214 public: ScopedJniEnvLocalRefState(JNIEnvExt * env)215 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : 216 env_(env), 217 saved_local_ref_cookie_(env->PushLocalReferenceFrame()) {} 218 ~ScopedJniEnvLocalRefState()219 ~ScopedJniEnvLocalRefState() { 220 env_->PopLocalReferenceFrame(saved_local_ref_cookie_); 221 } 222 223 private: 224 JNIEnvExt* const env_; 225 const jni::LRTSegmentState saved_local_ref_cookie_; 226 227 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState); 228 }; 229 230 } // namespace art 231 232 #endif // ART_RUNTIME_JNI_JNI_ENV_EXT_H_ 233