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