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