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