Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 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_JAVA_VM_EXT_H_ |
| 18 | #define ART_RUNTIME_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 "reference_table.h" |
| 26 | |
| 27 | namespace art { |
| 28 | |
| 29 | namespace mirror { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 30 | class Array; |
| 31 | } // namespace mirror |
| 32 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 33 | class ArtMethod; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 34 | class Libraries; |
| 35 | class ParsedOptions; |
| 36 | class Runtime; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 37 | struct RuntimeArgumentMap; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 38 | |
| 39 | class JavaVMExt : public JavaVM { |
| 40 | public: |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 41 | JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 42 | ~JavaVMExt(); |
| 43 | |
| 44 | bool ForceCopy() const { |
| 45 | return force_copy_; |
| 46 | } |
| 47 | |
| 48 | bool IsCheckJniEnabled() const { |
| 49 | return check_jni_; |
| 50 | } |
| 51 | |
| 52 | bool IsTracingEnabled() const { |
| 53 | return tracing_enabled_; |
| 54 | } |
| 55 | |
| 56 | Runtime* GetRuntime() const { |
| 57 | return runtime_; |
| 58 | } |
| 59 | |
| 60 | void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) { |
| 61 | check_jni_abort_hook_ = hook; |
| 62 | check_jni_abort_hook_data_ = data; |
| 63 | } |
| 64 | |
| 65 | // Aborts execution unless there is an abort handler installed in which case it will return. Its |
| 66 | // therefore important that callers return after aborting as otherwise code following the abort |
| 67 | // will be executed in the abort handler case. |
| 68 | void JniAbort(const char* jni_function_name, const char* msg); |
| 69 | |
| 70 | void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap); |
| 71 | |
| 72 | void JniAbortF(const char* jni_function_name, const char* fmt, ...) |
| 73 | __attribute__((__format__(__printf__, 3, 4))); |
| 74 | |
| 75 | // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages |
| 76 | // when a native method that matches the -Xjnitrace argument calls a JNI function |
| 77 | // such as NewByteArray. |
| 78 | // If -verbose:third-party-jni is on, we want to log any JNI function calls |
| 79 | // made by a third-party native method. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 80 | bool ShouldTrace(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Loads the given shared library. 'path' is an absolute pathname. |
| 84 | * |
Dmitriy Ivanov | f5a3099 | 2015-11-11 14:18:55 -0800 | [diff] [blame] | 85 | * Returns 'true' on success. On failure, sets 'error_msg' to a |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 86 | * human-readable description of the error. |
| 87 | */ |
Dimitry Ivanov | d5bbadf | 2015-12-15 14:08:18 -0800 | [diff] [blame] | 88 | bool LoadNativeLibrary(JNIEnv* env, const std::string& path, jobject class_loader, |
| 89 | bool is_shared_namespace, jstring library_path, jstring permitted_path, |
| 90 | std::string* error_msg); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 91 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 92 | // Unload native libraries with cleared class loaders. |
| 93 | void UnloadNativeLibraries() |
| 94 | REQUIRES(!Locks::jni_libraries_lock_) |
| 95 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 96 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Returns a pointer to the code for the native method 'm', found |
| 99 | * using dlsym(3) on every native library that's been loaded so far. |
| 100 | */ |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 101 | void* FindCodeForNativeMethod(ArtMethod* m) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 102 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 103 | |
| 104 | void DumpForSigQuit(std::ostream& os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 105 | REQUIRES(!Locks::jni_libraries_lock_, !globals_lock_, !weak_globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 106 | |
| 107 | void DumpReferenceTables(std::ostream& os) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 108 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_, !weak_globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 109 | |
| 110 | bool SetCheckJniEnabled(bool enabled); |
| 111 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 112 | void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_) |
| 113 | REQUIRES(!globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 114 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 115 | void DisallowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); |
| 116 | void AllowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 117 | void BroadcastForNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) |
| 118 | REQUIRES(!weak_globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 119 | |
| 120 | jobject AddGlobalRef(Thread* self, mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 121 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 122 | |
| 123 | jweak AddWeakGlobalRef(Thread* self, mirror::Object* obj) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 124 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 125 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 126 | void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 127 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 128 | void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!weak_globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 129 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 130 | void SweepJniWeakGlobals(IsMarkedVisitor* visitor) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 131 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 132 | |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 133 | mirror::Object* DecodeGlobal(IndirectRef ref) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 134 | SHARED_REQUIRES(Locks::mutator_lock_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 135 | |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 136 | void UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 137 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_); |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 138 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 139 | mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref) |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 140 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 141 | REQUIRES(!weak_globals_lock_); |
| 142 | |
| 143 | mirror::Object* DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) |
| 144 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 145 | REQUIRES(weak_globals_lock_); |
| 146 | |
Hiroshi Yamauchi | 498b160 | 2015-09-16 21:11:44 -0700 | [diff] [blame] | 147 | // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be |
| 148 | // null. |
| 149 | mirror::Object* DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) |
| 150 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 151 | REQUIRES(!weak_globals_lock_); |
| 152 | |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 153 | // Checks if the weak global ref has been cleared by the GC without decode (read barrier.) |
| 154 | bool IsWeakGlobalCleared(Thread* self, IndirectRef ref) |
| 155 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 156 | REQUIRES(!weak_globals_lock_); |
| 157 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 158 | Mutex& WeakGlobalsLock() RETURN_CAPABILITY(weak_globals_lock_) { |
| 159 | return weak_globals_lock_; |
| 160 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 161 | |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 162 | void UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 163 | SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_); |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 164 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 165 | const JNIInvokeInterface* GetUncheckedFunctions() const { |
| 166 | return unchecked_functions_; |
| 167 | } |
| 168 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 169 | void TrimGlobals() SHARED_REQUIRES(Locks::mutator_lock_) |
| 170 | REQUIRES(!globals_lock_); |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 171 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 172 | private: |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 173 | // Return true if self can currently access weak globals. |
| 174 | bool MayAccessWeakGlobalsUnlocked(Thread* self) const SHARED_REQUIRES(Locks::mutator_lock_); |
| 175 | bool MayAccessWeakGlobals(Thread* self) const |
| 176 | SHARED_REQUIRES(Locks::mutator_lock_) |
| 177 | REQUIRES(weak_globals_lock_); |
| 178 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 179 | Runtime* const runtime_; |
| 180 | |
| 181 | // Used for testing. By default, we'll LOG(FATAL) the reason. |
| 182 | void (*check_jni_abort_hook_)(void* data, const std::string& reason); |
| 183 | void* check_jni_abort_hook_data_; |
| 184 | |
| 185 | // Extra checking. |
| 186 | bool check_jni_; |
| 187 | bool force_copy_; |
| 188 | const bool tracing_enabled_; |
| 189 | |
| 190 | // Extra diagnostics. |
| 191 | const std::string trace_; |
| 192 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 193 | // JNI global references. |
| 194 | ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 195 | // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject. |
| 196 | IndirectReferenceTable globals_; |
| 197 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 198 | // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the |
| 199 | // jni_libraries_lock_ internally. |
| 200 | std::unique_ptr<Libraries> libraries_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 201 | |
| 202 | // Used by -Xcheck:jni. |
| 203 | const JNIInvokeInterface* const unchecked_functions_; |
| 204 | |
| 205 | // JNI weak global references. |
| 206 | Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 207 | // Since weak_globals_ contain weak roots, be careful not to |
| 208 | // directly access the object references in it. Use Get() with the |
| 209 | // read barrier enabled. |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 210 | // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal. |
| 211 | IndirectReferenceTable weak_globals_; |
| 212 | // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal. |
| 213 | Atomic<bool> allow_accessing_weak_globals_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 214 | ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_); |
| 215 | |
| 216 | DISALLOW_COPY_AND_ASSIGN(JavaVMExt); |
| 217 | }; |
| 218 | |
| 219 | } // namespace art |
| 220 | |
| 221 | #endif // ART_RUNTIME_JAVA_VM_EXT_H_ |