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_JAVA_VM_EXT_H_ 18 #define ART_RUNTIME_JNI_JAVA_VM_EXT_H_ 19 20 #include "jni.h" 21 22 #include "base/macros.h" 23 #include "base/mutex.h" 24 #include "indirect_reference_table.h" 25 #include "obj_ptr.h" 26 #include "reference_table.h" 27 28 namespace art HIDDEN { 29 30 namespace linker { 31 class ImageWriter; 32 } // namespace linker 33 34 namespace mirror { 35 class Array; 36 } // namespace mirror 37 38 class ArtMethod; 39 class IsMarkedVisitor; 40 class Libraries; 41 class ParsedOptions; 42 class Runtime; 43 struct RuntimeArgumentMap; 44 class ScopedObjectAccess; 45 46 class JavaVMExt; 47 // Hook definition for runtime plugins. 48 using GetEnvHook = jint (*)(JavaVMExt* vm, /*out*/void** new_env, jint version); 49 50 class JavaVMExt : public JavaVM { 51 public: 52 // Creates a new JavaVMExt object. 53 // Returns nullptr on error, in which case error_msg is set to a message 54 // describing the error. 55 static std::unique_ptr<JavaVMExt> Create(Runtime* runtime, 56 const RuntimeArgumentMap& runtime_options, 57 std::string* error_msg); 58 59 60 ~JavaVMExt(); 61 ForceCopy()62 bool ForceCopy() const { 63 return force_copy_; 64 } 65 IsCheckJniEnabled()66 bool IsCheckJniEnabled() const { 67 return check_jni_; 68 } 69 IsTracingEnabled()70 bool IsTracingEnabled() const { 71 return tracing_enabled_; 72 } 73 GetRuntime()74 Runtime* GetRuntime() const { 75 return runtime_; 76 } 77 SetCheckJniAbortHook(void (* hook)(void *,const std::string &),void * data)78 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) { 79 check_jni_abort_hook_ = hook; 80 check_jni_abort_hook_data_ = data; 81 } 82 83 // Aborts execution unless there is an abort handler installed in which case it will return. Its 84 // therefore important that callers return after aborting as otherwise code following the abort 85 // will be executed in the abort handler case. 86 void JniAbort(const char* jni_function_name, const char* msg); 87 88 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap); 89 90 void JniAbortF(const char* jni_function_name, const char* fmt, ...) 91 __attribute__((__format__(__printf__, 3, 4))); 92 93 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages 94 // when a native method that matches the -Xjnitrace argument calls a JNI function 95 // such as NewByteArray. 96 // If -verbose:third-party-jni is on, we want to log any JNI function calls 97 // made by a third-party native method. 98 bool ShouldTrace(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_); 99 100 /** 101 * Loads the given shared library. 'path' is an absolute pathname. 102 * 103 * Returns 'true' on success. On failure, sets 'error_msg' to a 104 * human-readable description of the error. 105 */ 106 EXPORT bool LoadNativeLibrary(JNIEnv* env, 107 const std::string& path, 108 jobject class_loader, 109 jclass caller_class, 110 std::string* error_msg); 111 112 // Unload native libraries with cleared class loaders. 113 void UnloadNativeLibraries() 114 REQUIRES(!Locks::jni_libraries_lock_) 115 REQUIRES_SHARED(Locks::mutator_lock_); 116 117 // Unload all boot classpath native libraries. 118 void UnloadBootNativeLibraries() 119 REQUIRES(!Locks::jni_libraries_lock_) 120 REQUIRES_SHARED(Locks::mutator_lock_); 121 122 /** 123 * Returns a pointer to the code for the native method 'm', found 124 * using dlsym(3) on every native library that's been loaded so far. 125 */ 126 void* FindCodeForNativeMethod(ArtMethod* m, std::string* error_msg, bool can_suspend) 127 REQUIRES_SHARED(Locks::mutator_lock_); 128 129 void DumpForSigQuit(std::ostream& os) 130 REQUIRES(!Locks::jni_libraries_lock_, 131 !Locks::jni_globals_lock_, 132 !Locks::jni_weak_globals_lock_); 133 134 void DumpReferenceTables(std::ostream& os) 135 REQUIRES_SHARED(Locks::mutator_lock_) 136 REQUIRES(!Locks::jni_globals_lock_, 137 !Locks::jni_weak_globals_lock_, 138 !Locks::alloc_tracker_lock_); 139 140 bool SetCheckJniEnabled(bool enabled); 141 142 EXPORT void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) 143 REQUIRES(!Locks::jni_globals_lock_); 144 145 void DisallowNewWeakGlobals() 146 REQUIRES_SHARED(Locks::mutator_lock_) 147 REQUIRES(!Locks::jni_weak_globals_lock_); 148 void AllowNewWeakGlobals() 149 REQUIRES_SHARED(Locks::mutator_lock_) 150 REQUIRES(!Locks::jni_weak_globals_lock_); 151 void BroadcastForNewWeakGlobals() 152 REQUIRES(!Locks::jni_weak_globals_lock_); 153 154 EXPORT jobject AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj) 155 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::jni_globals_lock_); 156 157 EXPORT jweak AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj) 158 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Locks::jni_weak_globals_lock_); 159 160 EXPORT void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!Locks::jni_globals_lock_); 161 162 EXPORT void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!Locks::jni_weak_globals_lock_); 163 SweepJniWeakGlobals(IsMarkedVisitor * visitor)164 void SweepJniWeakGlobals(IsMarkedVisitor* visitor) 165 REQUIRES_SHARED(Locks::mutator_lock_) 166 REQUIRES(!Locks::jni_weak_globals_lock_) { 167 weak_globals_.SweepJniWeakGlobals(visitor); 168 } 169 170 ObjPtr<mirror::Object> DecodeGlobal(IndirectRef ref) 171 REQUIRES_SHARED(Locks::mutator_lock_); 172 173 void UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result) 174 REQUIRES_SHARED(Locks::mutator_lock_) 175 REQUIRES(!Locks::jni_globals_lock_); 176 177 ObjPtr<mirror::Object> DecodeWeakGlobal(Thread* self, IndirectRef ref) 178 REQUIRES_SHARED(Locks::mutator_lock_) 179 REQUIRES(!Locks::jni_weak_globals_lock_); 180 181 ObjPtr<mirror::Object> DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) 182 REQUIRES_SHARED(Locks::mutator_lock_) 183 REQUIRES(Locks::jni_weak_globals_lock_); 184 185 // Decode weak global as strong. Use only if the target object is known to be alive. 186 ObjPtr<mirror::Object> DecodeWeakGlobalAsStrong(IndirectRef ref) 187 REQUIRES_SHARED(Locks::mutator_lock_) 188 REQUIRES(!Locks::jni_weak_globals_lock_); 189 190 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be 191 // null. 192 ObjPtr<mirror::Object> DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) 193 REQUIRES_SHARED(Locks::mutator_lock_) 194 REQUIRES(!Locks::jni_weak_globals_lock_); 195 196 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.) 197 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref) 198 REQUIRES_SHARED(Locks::mutator_lock_) 199 REQUIRES(!Locks::jni_weak_globals_lock_); 200 201 void UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result) 202 REQUIRES_SHARED(Locks::mutator_lock_) 203 REQUIRES(!Locks::jni_weak_globals_lock_); 204 GetUncheckedFunctions()205 const JNIInvokeInterface* GetUncheckedFunctions() const { 206 return unchecked_functions_; 207 } 208 209 void TrimGlobals() REQUIRES_SHARED(Locks::mutator_lock_) 210 REQUIRES(!Locks::jni_globals_lock_); 211 212 jint HandleGetEnv(/*out*/void** env, jint version) 213 REQUIRES(!env_hooks_lock_); 214 215 EXPORT void AddEnvironmentHook(GetEnvHook hook) REQUIRES(!env_hooks_lock_); 216 217 static bool IsBadJniVersion(int version); 218 219 // Return the library search path for the given classloader, if the classloader is of a 220 // well-known type. The jobject will be a local reference and is expected to be managed by the 221 // caller. 222 static jstring GetLibrarySearchPath(JNIEnv* env, jobject class_loader); 223 224 private: 225 // The constructor should not be called directly. Use `Create()` that initializes 226 // the new `JavaVMExt` object by calling `Initialize()`. 227 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options); 228 229 // Initialize the `JavaVMExt` object. 230 bool Initialize(std::string* error_msg); 231 232 // Return true if self can currently access weak globals. 233 bool MayAccessWeakGlobals(Thread* self) const REQUIRES_SHARED(Locks::mutator_lock_); 234 235 void WaitForWeakGlobalsAccess(Thread* self) 236 REQUIRES_SHARED(Locks::mutator_lock_) 237 REQUIRES(Locks::jni_weak_globals_lock_); 238 239 void CheckGlobalRefAllocationTracking(); 240 241 inline void MaybeTraceGlobals() REQUIRES(Locks::jni_globals_lock_); 242 inline void MaybeTraceWeakGlobals() REQUIRES(Locks::jni_weak_globals_lock_); 243 244 Runtime* const runtime_; 245 246 // Used for testing. By default, we'll LOG(FATAL) the reason. 247 void (*check_jni_abort_hook_)(void* data, const std::string& reason); 248 void* check_jni_abort_hook_data_; 249 250 // Extra checking. 251 bool check_jni_; 252 const bool force_copy_; 253 const bool tracing_enabled_; 254 255 // Extra diagnostics. 256 const std::string trace_; 257 258 IndirectReferenceTable globals_; 259 260 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the 261 // jni_libraries_lock_ internally. 262 std::unique_ptr<Libraries> libraries_; 263 264 // Used by -Xcheck:jni. 265 const JNIInvokeInterface* const unchecked_functions_; 266 267 // Since weak_globals_ contain weak roots, be careful not to 268 // directly access the object references in it. Use Get() with the 269 // read barrier enabled or disabled based on the use case. 270 IndirectReferenceTable weak_globals_; 271 Atomic<bool> allow_accessing_weak_globals_; 272 ConditionVariable weak_globals_add_condition_ GUARDED_BY(Locks::jni_weak_globals_lock_); 273 274 // TODO Maybe move this to Runtime. 275 ReaderWriterMutex env_hooks_lock_ BOTTOM_MUTEX_ACQUIRED_AFTER; 276 std::vector<GetEnvHook> env_hooks_ GUARDED_BY(env_hooks_lock_); 277 278 size_t enable_allocation_tracking_delta_; 279 std::atomic<bool> allocation_tracking_enabled_; 280 std::atomic<bool> old_allocation_tracking_state_; 281 282 // We report the number of global references after every kGlobalRefReportInterval changes. 283 static constexpr uint32_t kGlobalRefReportInterval = 17; 284 uint32_t weak_global_ref_report_counter_ GUARDED_BY(Locks::jni_weak_globals_lock_) 285 = kGlobalRefReportInterval; 286 uint32_t global_ref_report_counter_ GUARDED_BY(Locks::jni_globals_lock_) 287 = kGlobalRefReportInterval; 288 289 friend class linker::ImageWriter; // Uses `globals_` and `weak_globals_` without read barrier. 290 friend IndirectReferenceTable* GetIndirectReferenceTable(ScopedObjectAccess& soa, 291 IndirectRefKind kind); 292 293 DISALLOW_COPY_AND_ASSIGN(JavaVMExt); 294 }; 295 296 } // namespace art 297 298 #endif // ART_RUNTIME_JNI_JAVA_VM_EXT_H_ 299