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 | #include "jni_internal.h" |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 21 | #include "art_method.h" |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 22 | #include "base/dumpable.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 23 | #include "base/mutex.h" |
| 24 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 25 | #include "base/systrace.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 26 | #include "check_jni.h" |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 28 | #include "fault_handler.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 29 | #include "indirect_reference_table-inl.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
| 31 | #include "mirror/class_loader.h" |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 32 | #include "nativebridge/native_bridge.h" |
Dmitriy Ivanov | f5a3099 | 2015-11-11 14:18:55 -0800 | [diff] [blame] | 33 | #include "nativeloader/native_loader.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 34 | #include "java_vm_ext.h" |
| 35 | #include "parsed_options.h" |
Ian Rogers | c0542af | 2014-09-03 16:16:56 -0700 | [diff] [blame] | 36 | #include "runtime-inl.h" |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 37 | #include "runtime_options.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 38 | #include "ScopedLocalRef.h" |
| 39 | #include "scoped_thread_state_change.h" |
| 40 | #include "thread-inl.h" |
| 41 | #include "thread_list.h" |
| 42 | |
| 43 | namespace art { |
| 44 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 45 | static size_t gGlobalsInitial = 512; // Arbitrary. |
| 46 | static size_t gGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.) |
| 47 | |
| 48 | static const size_t kWeakGlobalsInitial = 16; // Arbitrary. |
| 49 | static const size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.) |
| 50 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 51 | bool JavaVMExt::IsBadJniVersion(int version) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 52 | // We don't support JNI_VERSION_1_1. These are the only other valid versions. |
| 53 | return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6; |
| 54 | } |
| 55 | |
| 56 | class SharedLibrary { |
| 57 | public: |
| 58 | SharedLibrary(JNIEnv* env, Thread* self, const std::string& path, void* handle, |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 59 | jobject class_loader, void* class_loader_allocator) |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 60 | : path_(path), |
| 61 | handle_(handle), |
| 62 | needs_native_bridge_(false), |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 63 | class_loader_(env->NewWeakGlobalRef(class_loader)), |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 64 | class_loader_allocator_(class_loader_allocator), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 65 | jni_on_load_lock_("JNI_OnLoad lock"), |
| 66 | jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_), |
| 67 | jni_on_load_thread_id_(self->GetThreadId()), |
| 68 | jni_on_load_result_(kPending) { |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 69 | CHECK(class_loader_allocator_ != nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | ~SharedLibrary() { |
| 73 | Thread* self = Thread::Current(); |
| 74 | if (self != nullptr) { |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 75 | self->GetJniEnv()->DeleteWeakGlobalRef(class_loader_); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 76 | } |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 77 | |
| 78 | if (!needs_native_bridge_) { |
| 79 | android::CloseNativeLibrary(handle_); |
| 80 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 83 | jweak GetClassLoader() const { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 84 | return class_loader_; |
| 85 | } |
| 86 | |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 87 | const void* GetClassLoaderAllocator() const { |
| 88 | return class_loader_allocator_; |
| 89 | } |
| 90 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 91 | const std::string& GetPath() const { |
| 92 | return path_; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Check the result of an earlier call to JNI_OnLoad on this library. |
| 97 | * If the call has not yet finished in another thread, wait for it. |
| 98 | */ |
| 99 | bool CheckOnLoadResult() |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 100 | REQUIRES(!jni_on_load_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 101 | Thread* self = Thread::Current(); |
| 102 | bool okay; |
| 103 | { |
| 104 | MutexLock mu(self, jni_on_load_lock_); |
| 105 | |
| 106 | if (jni_on_load_thread_id_ == self->GetThreadId()) { |
| 107 | // Check this so we don't end up waiting for ourselves. We need to return "true" so the |
| 108 | // caller can continue. |
| 109 | LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\""; |
| 110 | okay = true; |
| 111 | } else { |
| 112 | while (jni_on_load_result_ == kPending) { |
| 113 | VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]"; |
| 114 | jni_on_load_cond_.Wait(self); |
| 115 | } |
| 116 | |
| 117 | okay = (jni_on_load_result_ == kOkay); |
| 118 | VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" " |
| 119 | << (okay ? "succeeded" : "failed") << "]"; |
| 120 | } |
| 121 | } |
| 122 | return okay; |
| 123 | } |
| 124 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 125 | void SetResult(bool result) REQUIRES(!jni_on_load_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 126 | Thread* self = Thread::Current(); |
| 127 | MutexLock mu(self, jni_on_load_lock_); |
| 128 | |
| 129 | jni_on_load_result_ = result ? kOkay : kFailed; |
| 130 | jni_on_load_thread_id_ = 0; |
| 131 | |
| 132 | // Broadcast a wakeup to anybody sleeping on the condition variable. |
| 133 | jni_on_load_cond_.Broadcast(self); |
| 134 | } |
| 135 | |
| 136 | void SetNeedsNativeBridge() { |
| 137 | needs_native_bridge_ = true; |
| 138 | } |
| 139 | |
| 140 | bool NeedsNativeBridge() const { |
| 141 | return needs_native_bridge_; |
| 142 | } |
| 143 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 144 | void* FindSymbol(const std::string& symbol_name, const char* shorty = nullptr) { |
| 145 | return NeedsNativeBridge() |
| 146 | ? FindSymbolWithNativeBridge(symbol_name.c_str(), shorty) |
| 147 | : FindSymbolWithoutNativeBridge(symbol_name.c_str()); |
| 148 | } |
| 149 | |
| 150 | void* FindSymbolWithoutNativeBridge(const std::string& symbol_name) { |
Andreas Gampe | 8fec90b | 2015-06-30 11:23:44 -0700 | [diff] [blame] | 151 | CHECK(!NeedsNativeBridge()); |
| 152 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 153 | return dlsym(handle_, symbol_name.c_str()); |
| 154 | } |
| 155 | |
| 156 | void* FindSymbolWithNativeBridge(const std::string& symbol_name, const char* shorty) { |
| 157 | CHECK(NeedsNativeBridge()); |
| 158 | |
| 159 | uint32_t len = 0; |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 160 | return android::NativeBridgeGetTrampoline(handle_, symbol_name.c_str(), shorty, len); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
| 164 | enum JNI_OnLoadState { |
| 165 | kPending, |
| 166 | kFailed, |
| 167 | kOkay, |
| 168 | }; |
| 169 | |
| 170 | // Path to library "/system/lib/libjni.so". |
| 171 | const std::string path_; |
| 172 | |
| 173 | // The void* returned by dlopen(3). |
| 174 | void* const handle_; |
| 175 | |
| 176 | // True if a native bridge is required. |
| 177 | bool needs_native_bridge_; |
| 178 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 179 | // The ClassLoader this library is associated with, a weak global JNI reference that is |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 180 | // created/deleted with the scope of the library. |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 181 | const jweak class_loader_; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 182 | // Used to do equality check on class loaders so we can avoid decoding the weak root and read |
| 183 | // barriers that mess with class unloading. |
| 184 | const void* class_loader_allocator_; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 185 | |
| 186 | // Guards remaining items. |
| 187 | Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER; |
| 188 | // Wait for JNI_OnLoad in other thread. |
| 189 | ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_); |
| 190 | // Recursive invocation guard. |
| 191 | uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_); |
| 192 | // Result of earlier JNI_OnLoad call. |
| 193 | JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_); |
| 194 | }; |
| 195 | |
| 196 | // This exists mainly to keep implementation details out of the header file. |
| 197 | class Libraries { |
| 198 | public: |
| 199 | Libraries() { |
| 200 | } |
| 201 | |
| 202 | ~Libraries() { |
| 203 | STLDeleteValues(&libraries_); |
| 204 | } |
| 205 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 206 | // NO_THREAD_SAFETY_ANALYSIS since this may be called from Dumpable. Dumpable can't be annotated |
| 207 | // properly due to the template. The caller should be holding the jni_libraries_lock_. |
| 208 | void Dump(std::ostream& os) const NO_THREAD_SAFETY_ANALYSIS { |
| 209 | Locks::jni_libraries_lock_->AssertHeld(Thread::Current()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 210 | bool first = true; |
| 211 | for (const auto& library : libraries_) { |
| 212 | if (!first) { |
| 213 | os << ' '; |
| 214 | } |
| 215 | first = false; |
| 216 | os << library.first; |
| 217 | } |
| 218 | } |
| 219 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 220 | size_t size() const REQUIRES(Locks::jni_libraries_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 221 | return libraries_.size(); |
| 222 | } |
| 223 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 224 | SharedLibrary* Get(const std::string& path) REQUIRES(Locks::jni_libraries_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 225 | auto it = libraries_.find(path); |
| 226 | return (it == libraries_.end()) ? nullptr : it->second; |
| 227 | } |
| 228 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 229 | void Put(const std::string& path, SharedLibrary* library) |
| 230 | REQUIRES(Locks::jni_libraries_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 231 | libraries_.Put(path, library); |
| 232 | } |
| 233 | |
| 234 | // See section 11.3 "Linking Native Methods" of the JNI spec. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 235 | void* FindNativeMethod(ArtMethod* m, std::string& detail) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 236 | REQUIRES(Locks::jni_libraries_lock_) |
| 237 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 238 | std::string jni_short_name(JniShortName(m)); |
| 239 | std::string jni_long_name(JniLongName(m)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 240 | mirror::ClassLoader* const declaring_class_loader = m->GetDeclaringClass()->GetClassLoader(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 241 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 242 | void* const declaring_class_loader_allocator = |
| 243 | Runtime::Current()->GetClassLinker()->GetAllocatorForClassLoader(declaring_class_loader); |
| 244 | CHECK(declaring_class_loader_allocator != nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 245 | for (const auto& lib : libraries_) { |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 246 | SharedLibrary* const library = lib.second; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 247 | // Use the allocator address for class loader equality to avoid unnecessary weak root decode. |
| 248 | if (library->GetClassLoaderAllocator() != declaring_class_loader_allocator) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 249 | // We only search libraries loaded by the appropriate ClassLoader. |
| 250 | continue; |
| 251 | } |
| 252 | // Try the short name then the long name... |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 253 | const char* shorty = library->NeedsNativeBridge() |
| 254 | ? m->GetShorty() |
| 255 | : nullptr; |
| 256 | void* fn = library->FindSymbol(jni_short_name, shorty); |
| 257 | if (fn == nullptr) { |
| 258 | fn = library->FindSymbol(jni_long_name, shorty); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 259 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 260 | if (fn != nullptr) { |
| 261 | VLOG(jni) << "[Found native code for " << PrettyMethod(m) |
| 262 | << " in \"" << library->GetPath() << "\"]"; |
| 263 | return fn; |
| 264 | } |
| 265 | } |
| 266 | detail += "No implementation found for "; |
| 267 | detail += PrettyMethod(m); |
| 268 | detail += " (tried " + jni_short_name + " and " + jni_long_name + ")"; |
| 269 | LOG(ERROR) << detail; |
| 270 | return nullptr; |
| 271 | } |
| 272 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 273 | // Unload native libraries with cleared class loaders. |
| 274 | void UnloadNativeLibraries() |
| 275 | REQUIRES(!Locks::jni_libraries_lock_) |
| 276 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 277 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 278 | std::vector<SharedLibrary*> unload_libraries; |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 279 | { |
| 280 | MutexLock mu(soa.Self(), *Locks::jni_libraries_lock_); |
| 281 | for (auto it = libraries_.begin(); it != libraries_.end(); ) { |
| 282 | SharedLibrary* const library = it->second; |
| 283 | // If class loader is null then it was unloaded, call JNI_OnUnload. |
Mathieu Chartier | cffb747 | 2015-09-28 10:33:00 -0700 | [diff] [blame] | 284 | const jweak class_loader = library->GetClassLoader(); |
| 285 | // If class_loader is a null jobject then it is the boot class loader. We should not unload |
| 286 | // the native libraries of the boot class loader. |
| 287 | if (class_loader != nullptr && |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 288 | soa.Self()->IsJWeakCleared(class_loader)) { |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 289 | unload_libraries.push_back(library); |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 290 | it = libraries_.erase(it); |
| 291 | } else { |
| 292 | ++it; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | // Do this without holding the jni libraries lock to prevent possible deadlocks. |
Alex Light | bc5669e | 2016-06-13 17:22:13 +0000 | [diff] [blame] | 297 | typedef void (*JNI_OnUnloadFn)(JavaVM*, void*); |
| 298 | for (auto library : unload_libraries) { |
| 299 | void* const sym = library->FindSymbol("JNI_OnUnload", nullptr); |
| 300 | if (sym == nullptr) { |
| 301 | VLOG(jni) << "[No JNI_OnUnload found in \"" << library->GetPath() << "\"]"; |
| 302 | } else { |
| 303 | VLOG(jni) << "[JNI_OnUnload found for \"" << library->GetPath() << "\"]: Calling..."; |
| 304 | JNI_OnUnloadFn jni_on_unload = reinterpret_cast<JNI_OnUnloadFn>(sym); |
| 305 | jni_on_unload(soa.Vm(), nullptr); |
| 306 | } |
| 307 | delete library; |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 308 | } |
| 309 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 310 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 311 | private: |
| 312 | AllocationTrackingSafeMap<std::string, SharedLibrary*, kAllocatorTagJNILibraries> libraries_ |
| 313 | GUARDED_BY(Locks::jni_libraries_lock_); |
| 314 | }; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 315 | |
| 316 | class JII { |
| 317 | public: |
| 318 | static jint DestroyJavaVM(JavaVM* vm) { |
| 319 | if (vm == nullptr) { |
| 320 | return JNI_ERR; |
| 321 | } |
| 322 | JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm); |
| 323 | delete raw_vm->GetRuntime(); |
Dimitry Ivanov | 39d68ef | 2016-04-29 16:02:38 -0700 | [diff] [blame] | 324 | android::ResetNativeLoader(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 325 | return JNI_OK; |
| 326 | } |
| 327 | |
| 328 | static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
| 329 | return AttachCurrentThreadInternal(vm, p_env, thr_args, false); |
| 330 | } |
| 331 | |
| 332 | static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) { |
| 333 | return AttachCurrentThreadInternal(vm, p_env, thr_args, true); |
| 334 | } |
| 335 | |
| 336 | static jint DetachCurrentThread(JavaVM* vm) { |
| 337 | if (vm == nullptr || Thread::Current() == nullptr) { |
| 338 | return JNI_ERR; |
| 339 | } |
| 340 | JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm); |
| 341 | Runtime* runtime = raw_vm->GetRuntime(); |
| 342 | runtime->DetachCurrentThread(); |
| 343 | return JNI_OK; |
| 344 | } |
| 345 | |
| 346 | static jint GetEnv(JavaVM* vm, void** env, jint version) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 347 | if (vm == nullptr || env == nullptr) { |
| 348 | return JNI_ERR; |
| 349 | } |
| 350 | Thread* thread = Thread::Current(); |
| 351 | if (thread == nullptr) { |
| 352 | *env = nullptr; |
| 353 | return JNI_EDETACHED; |
| 354 | } |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 355 | JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm); |
| 356 | return raw_vm->HandleGetEnv(env, version); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | private: |
| 360 | static jint AttachCurrentThreadInternal(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) { |
| 361 | if (vm == nullptr || p_env == nullptr) { |
| 362 | return JNI_ERR; |
| 363 | } |
| 364 | |
| 365 | // Return immediately if we're already attached. |
| 366 | Thread* self = Thread::Current(); |
| 367 | if (self != nullptr) { |
| 368 | *p_env = self->GetJniEnv(); |
| 369 | return JNI_OK; |
| 370 | } |
| 371 | |
| 372 | Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->GetRuntime(); |
| 373 | |
| 374 | // No threads allowed in zygote mode. |
| 375 | if (runtime->IsZygote()) { |
| 376 | LOG(ERROR) << "Attempt to attach a thread in the zygote"; |
| 377 | return JNI_ERR; |
| 378 | } |
| 379 | |
| 380 | JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args); |
| 381 | const char* thread_name = nullptr; |
| 382 | jobject thread_group = nullptr; |
| 383 | if (args != nullptr) { |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 384 | if (JavaVMExt::IsBadJniVersion(args->version)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 385 | LOG(ERROR) << "Bad JNI version passed to " |
| 386 | << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": " |
| 387 | << args->version; |
| 388 | return JNI_EVERSION; |
| 389 | } |
| 390 | thread_name = args->name; |
| 391 | thread_group = args->group; |
| 392 | } |
| 393 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 394 | if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group, |
| 395 | !runtime->IsAotCompiler())) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 396 | *p_env = nullptr; |
| 397 | return JNI_ERR; |
| 398 | } else { |
| 399 | *p_env = Thread::Current()->GetJniEnv(); |
| 400 | return JNI_OK; |
| 401 | } |
| 402 | } |
| 403 | }; |
| 404 | |
| 405 | const JNIInvokeInterface gJniInvokeInterface = { |
| 406 | nullptr, // reserved0 |
| 407 | nullptr, // reserved1 |
| 408 | nullptr, // reserved2 |
| 409 | JII::DestroyJavaVM, |
| 410 | JII::AttachCurrentThread, |
| 411 | JII::DetachCurrentThread, |
| 412 | JII::GetEnv, |
| 413 | JII::AttachCurrentThreadAsDaemon |
| 414 | }; |
| 415 | |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 416 | JavaVMExt::JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options) |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 417 | : runtime_(runtime), |
| 418 | check_jni_abort_hook_(nullptr), |
| 419 | check_jni_abort_hook_data_(nullptr), |
| 420 | check_jni_(false), // Initialized properly in the constructor body below. |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 421 | force_copy_(runtime_options.Exists(RuntimeArgumentMap::JniOptsForceCopy)), |
| 422 | tracing_enabled_(runtime_options.Exists(RuntimeArgumentMap::JniTrace) |
| 423 | || VLOG_IS_ON(third_party_jni)), |
| 424 | trace_(runtime_options.GetOrDefault(RuntimeArgumentMap::JniTrace)), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 425 | globals_lock_("JNI global reference table lock"), |
| 426 | globals_(gGlobalsInitial, gGlobalsMax, kGlobal), |
| 427 | libraries_(new Libraries), |
| 428 | unchecked_functions_(&gJniInvokeInterface), |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 429 | weak_globals_lock_("JNI weak global reference table lock", kJniWeakGlobalsLock), |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 430 | weak_globals_(kWeakGlobalsInitial, kWeakGlobalsMax, kWeakGlobal), |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 431 | allow_accessing_weak_globals_(true), |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 432 | weak_globals_add_condition_("weak globals add condition", weak_globals_lock_), |
| 433 | env_hooks_() { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 434 | functions = unchecked_functions_; |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 435 | SetCheckJniEnabled(runtime_options.Exists(RuntimeArgumentMap::CheckJni)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | JavaVMExt::~JavaVMExt() { |
| 439 | } |
| 440 | |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 441 | jint JavaVMExt::HandleGetEnv(/*out*/void** env, jint version) { |
| 442 | for (GetEnvHook hook : env_hooks_) { |
| 443 | jint res = hook(this, env, version); |
| 444 | if (res == JNI_OK) { |
| 445 | return JNI_OK; |
| 446 | } else if (res != JNI_EVERSION) { |
| 447 | LOG(ERROR) << "Error returned from a plugin GetEnv handler! " << res; |
| 448 | return res; |
| 449 | } |
| 450 | } |
| 451 | LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version; |
| 452 | return JNI_EVERSION; |
| 453 | } |
| 454 | |
| 455 | // Add a hook to handle getting environments from the GetEnv call. |
| 456 | void JavaVMExt::AddEnvironmentHook(GetEnvHook hook) { |
| 457 | CHECK(hook != nullptr) << "environment hooks shouldn't be null!"; |
| 458 | env_hooks_.push_back(hook); |
| 459 | } |
| 460 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 461 | void JavaVMExt::JniAbort(const char* jni_function_name, const char* msg) { |
| 462 | Thread* self = Thread::Current(); |
| 463 | ScopedObjectAccess soa(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 464 | ArtMethod* current_method = self->GetCurrentMethod(nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 465 | |
| 466 | std::ostringstream os; |
| 467 | os << "JNI DETECTED ERROR IN APPLICATION: " << msg; |
| 468 | |
| 469 | if (jni_function_name != nullptr) { |
| 470 | os << "\n in call to " << jni_function_name; |
| 471 | } |
| 472 | // TODO: is this useful given that we're about to dump the calling thread's stack? |
| 473 | if (current_method != nullptr) { |
| 474 | os << "\n from " << PrettyMethod(current_method); |
| 475 | } |
| 476 | os << "\n"; |
| 477 | self->Dump(os); |
| 478 | |
| 479 | if (check_jni_abort_hook_ != nullptr) { |
| 480 | check_jni_abort_hook_(check_jni_abort_hook_data_, os.str()); |
| 481 | } else { |
| 482 | // Ensure that we get a native stack trace for this thread. |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 483 | ScopedThreadSuspension sts(self, kNative); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 484 | LOG(FATAL) << os.str(); |
Mathieu Chartier | f1d666e | 2015-09-03 16:13:34 -0700 | [diff] [blame] | 485 | UNREACHABLE(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | void JavaVMExt::JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) { |
| 490 | std::string msg; |
| 491 | StringAppendV(&msg, fmt, ap); |
| 492 | JniAbort(jni_function_name, msg.c_str()); |
| 493 | } |
| 494 | |
| 495 | void JavaVMExt::JniAbortF(const char* jni_function_name, const char* fmt, ...) { |
| 496 | va_list args; |
| 497 | va_start(args, fmt); |
| 498 | JniAbortV(jni_function_name, fmt, args); |
| 499 | va_end(args); |
| 500 | } |
| 501 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 502 | bool JavaVMExt::ShouldTrace(ArtMethod* method) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 503 | // Fast where no tracing is enabled. |
| 504 | if (trace_.empty() && !VLOG_IS_ON(third_party_jni)) { |
| 505 | return false; |
| 506 | } |
| 507 | // Perform checks based on class name. |
| 508 | StringPiece class_name(method->GetDeclaringClassDescriptor()); |
| 509 | if (!trace_.empty() && class_name.find(trace_) != std::string::npos) { |
| 510 | return true; |
| 511 | } |
| 512 | if (!VLOG_IS_ON(third_party_jni)) { |
| 513 | return false; |
| 514 | } |
| 515 | // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look |
| 516 | // like part of Android. |
| 517 | static const char* gBuiltInPrefixes[] = { |
| 518 | "Landroid/", |
| 519 | "Lcom/android/", |
| 520 | "Lcom/google/android/", |
| 521 | "Ldalvik/", |
| 522 | "Ljava/", |
| 523 | "Ljavax/", |
| 524 | "Llibcore/", |
| 525 | "Lorg/apache/harmony/", |
| 526 | }; |
| 527 | for (size_t i = 0; i < arraysize(gBuiltInPrefixes); ++i) { |
| 528 | if (class_name.starts_with(gBuiltInPrefixes[i])) { |
| 529 | return false; |
| 530 | } |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | jobject JavaVMExt::AddGlobalRef(Thread* self, mirror::Object* obj) { |
| 536 | // Check for null after decoding the object to handle cleared weak globals. |
| 537 | if (obj == nullptr) { |
| 538 | return nullptr; |
| 539 | } |
| 540 | WriterMutexLock mu(self, globals_lock_); |
| 541 | IndirectRef ref = globals_.Add(IRT_FIRST_SEGMENT, obj); |
| 542 | return reinterpret_cast<jobject>(ref); |
| 543 | } |
| 544 | |
| 545 | jweak JavaVMExt::AddWeakGlobalRef(Thread* self, mirror::Object* obj) { |
| 546 | if (obj == nullptr) { |
| 547 | return nullptr; |
| 548 | } |
| 549 | MutexLock mu(self, weak_globals_lock_); |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 550 | while (UNLIKELY(!MayAccessWeakGlobals(self))) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 551 | weak_globals_add_condition_.WaitHoldingLocks(self); |
| 552 | } |
| 553 | IndirectRef ref = weak_globals_.Add(IRT_FIRST_SEGMENT, obj); |
| 554 | return reinterpret_cast<jweak>(ref); |
| 555 | } |
| 556 | |
| 557 | void JavaVMExt::DeleteGlobalRef(Thread* self, jobject obj) { |
| 558 | if (obj == nullptr) { |
| 559 | return; |
| 560 | } |
| 561 | WriterMutexLock mu(self, globals_lock_); |
| 562 | if (!globals_.Remove(IRT_FIRST_SEGMENT, obj)) { |
| 563 | LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") " |
| 564 | << "failed to find entry"; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) { |
| 569 | if (obj == nullptr) { |
| 570 | return; |
| 571 | } |
| 572 | MutexLock mu(self, weak_globals_lock_); |
| 573 | if (!weak_globals_.Remove(IRT_FIRST_SEGMENT, obj)) { |
| 574 | LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") " |
| 575 | << "failed to find entry"; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static void ThreadEnableCheckJni(Thread* thread, void* arg) { |
| 580 | bool* check_jni = reinterpret_cast<bool*>(arg); |
| 581 | thread->GetJniEnv()->SetCheckJniEnabled(*check_jni); |
| 582 | } |
| 583 | |
| 584 | bool JavaVMExt::SetCheckJniEnabled(bool enabled) { |
| 585 | bool old_check_jni = check_jni_; |
| 586 | check_jni_ = enabled; |
| 587 | functions = enabled ? GetCheckJniInvokeInterface() : unchecked_functions_; |
| 588 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 589 | runtime_->GetThreadList()->ForEach(ThreadEnableCheckJni, &check_jni_); |
| 590 | return old_check_jni; |
| 591 | } |
| 592 | |
| 593 | void JavaVMExt::DumpForSigQuit(std::ostream& os) { |
| 594 | os << "JNI: CheckJNI is " << (check_jni_ ? "on" : "off"); |
| 595 | if (force_copy_) { |
| 596 | os << " (with forcecopy)"; |
| 597 | } |
| 598 | Thread* self = Thread::Current(); |
| 599 | { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 600 | ReaderMutexLock mu(self, globals_lock_); |
| 601 | os << "; globals=" << globals_.Capacity(); |
| 602 | } |
| 603 | { |
| 604 | MutexLock mu(self, weak_globals_lock_); |
| 605 | if (weak_globals_.Capacity() > 0) { |
| 606 | os << " (plus " << weak_globals_.Capacity() << " weak)"; |
| 607 | } |
| 608 | } |
| 609 | os << '\n'; |
| 610 | |
| 611 | { |
| 612 | MutexLock mu(self, *Locks::jni_libraries_lock_); |
| 613 | os << "Libraries: " << Dumpable<Libraries>(*libraries_) << " (" << libraries_->size() << ")\n"; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | void JavaVMExt::DisallowNewWeakGlobals() { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 618 | CHECK(!kUseReadBarrier); |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 619 | Thread* const self = Thread::Current(); |
| 620 | MutexLock mu(self, weak_globals_lock_); |
| 621 | // DisallowNewWeakGlobals is only called by CMS during the pause. It is required to have the |
| 622 | // mutator lock exclusively held so that we don't have any threads in the middle of |
| 623 | // DecodeWeakGlobal. |
| 624 | Locks::mutator_lock_->AssertExclusiveHeld(self); |
| 625 | allow_accessing_weak_globals_.StoreSequentiallyConsistent(false); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void JavaVMExt::AllowNewWeakGlobals() { |
Hiroshi Yamauchi | fdbd13c | 2015-09-02 16:16:58 -0700 | [diff] [blame] | 629 | CHECK(!kUseReadBarrier); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 630 | Thread* self = Thread::Current(); |
| 631 | MutexLock mu(self, weak_globals_lock_); |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 632 | allow_accessing_weak_globals_.StoreSequentiallyConsistent(true); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 633 | weak_globals_add_condition_.Broadcast(self); |
| 634 | } |
| 635 | |
Hiroshi Yamauchi | 0b71357 | 2015-06-16 18:29:23 -0700 | [diff] [blame] | 636 | void JavaVMExt::BroadcastForNewWeakGlobals() { |
| 637 | CHECK(kUseReadBarrier); |
| 638 | Thread* self = Thread::Current(); |
| 639 | MutexLock mu(self, weak_globals_lock_); |
| 640 | weak_globals_add_condition_.Broadcast(self); |
| 641 | } |
| 642 | |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 643 | mirror::Object* JavaVMExt::DecodeGlobal(IndirectRef ref) { |
| 644 | return globals_.SynchronizedGet(ref); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 647 | void JavaVMExt::UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result) { |
| 648 | WriterMutexLock mu(self, globals_lock_); |
| 649 | globals_.Update(ref, result); |
| 650 | } |
| 651 | |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 652 | inline bool JavaVMExt::MayAccessWeakGlobals(Thread* self) const { |
| 653 | return MayAccessWeakGlobalsUnlocked(self); |
| 654 | } |
| 655 | |
| 656 | inline bool JavaVMExt::MayAccessWeakGlobalsUnlocked(Thread* self) const { |
Hiroshi Yamauchi | 498b160 | 2015-09-16 21:11:44 -0700 | [diff] [blame] | 657 | DCHECK(self != nullptr); |
| 658 | return kUseReadBarrier ? |
| 659 | self->GetWeakRefAccessEnabled() : |
| 660 | allow_accessing_weak_globals_.LoadSequentiallyConsistent(); |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 663 | mirror::Object* JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) { |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 664 | // It is safe to access GetWeakRefAccessEnabled without the lock since CC uses checkpoints to call |
| 665 | // SetWeakRefAccessEnabled, and the other collectors only modify allow_accessing_weak_globals_ |
| 666 | // when the mutators are paused. |
| 667 | // This only applies in the case where MayAccessWeakGlobals goes from false to true. In the other |
| 668 | // case, it may be racy, this is benign since DecodeWeakGlobalLocked does the correct behavior |
| 669 | // if MayAccessWeakGlobals is false. |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 670 | DCHECK_EQ(GetIndirectRefKind(ref), kWeakGlobal); |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 671 | if (LIKELY(MayAccessWeakGlobalsUnlocked(self))) { |
| 672 | return weak_globals_.SynchronizedGet(ref); |
| 673 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 674 | MutexLock mu(self, weak_globals_lock_); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 675 | return DecodeWeakGlobalLocked(self, ref); |
| 676 | } |
| 677 | |
| 678 | mirror::Object* JavaVMExt::DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) { |
| 679 | if (kDebugLocking) { |
| 680 | weak_globals_lock_.AssertHeld(self); |
| 681 | } |
Mathieu Chartier | 30b5e27 | 2015-09-01 11:14:34 -0700 | [diff] [blame] | 682 | while (UNLIKELY(!MayAccessWeakGlobals(self))) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 683 | weak_globals_add_condition_.WaitHoldingLocks(self); |
| 684 | } |
| 685 | return weak_globals_.Get(ref); |
| 686 | } |
| 687 | |
Hiroshi Yamauchi | 498b160 | 2015-09-16 21:11:44 -0700 | [diff] [blame] | 688 | mirror::Object* JavaVMExt::DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) { |
| 689 | DCHECK_EQ(GetIndirectRefKind(ref), kWeakGlobal); |
| 690 | DCHECK(Runtime::Current()->IsShuttingDown(self)); |
| 691 | if (self != nullptr) { |
| 692 | return DecodeWeakGlobal(self, ref); |
| 693 | } |
| 694 | // self can be null during a runtime shutdown. ~Runtime()->~ClassLinker()->DecodeWeakGlobal(). |
| 695 | if (!kUseReadBarrier) { |
| 696 | DCHECK(allow_accessing_weak_globals_.LoadSequentiallyConsistent()); |
| 697 | } |
| 698 | return weak_globals_.SynchronizedGet(ref); |
| 699 | } |
| 700 | |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 701 | bool JavaVMExt::IsWeakGlobalCleared(Thread* self, IndirectRef ref) { |
| 702 | DCHECK_EQ(GetIndirectRefKind(ref), kWeakGlobal); |
| 703 | MutexLock mu(self, weak_globals_lock_); |
| 704 | while (UNLIKELY(!MayAccessWeakGlobals(self))) { |
| 705 | weak_globals_add_condition_.WaitHoldingLocks(self); |
| 706 | } |
| 707 | // When just checking a weak ref has been cleared, avoid triggering the read barrier in decode |
| 708 | // (DecodeWeakGlobal) so that we won't accidentally mark the object alive. Since the cleared |
| 709 | // sentinel is a non-moving object, we can compare the ref to it without the read barrier and |
| 710 | // decide if it's cleared. |
| 711 | return Runtime::Current()->IsClearedJniWeakGlobal(weak_globals_.Get<kWithoutReadBarrier>(ref)); |
| 712 | } |
| 713 | |
Jeff Hao | 83c8195 | 2015-05-27 19:29:29 -0700 | [diff] [blame] | 714 | void JavaVMExt::UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result) { |
| 715 | MutexLock mu(self, weak_globals_lock_); |
| 716 | weak_globals_.Update(ref, result); |
| 717 | } |
| 718 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 719 | void JavaVMExt::DumpReferenceTables(std::ostream& os) { |
| 720 | Thread* self = Thread::Current(); |
| 721 | { |
| 722 | ReaderMutexLock mu(self, globals_lock_); |
| 723 | globals_.Dump(os); |
| 724 | } |
| 725 | { |
| 726 | MutexLock mu(self, weak_globals_lock_); |
| 727 | weak_globals_.Dump(os); |
| 728 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 731 | void JavaVMExt::UnloadNativeLibraries() { |
| 732 | libraries_.get()->UnloadNativeLibraries(); |
| 733 | } |
| 734 | |
Dimitry Ivanov | 942dc298 | 2016-02-24 13:33:33 -0800 | [diff] [blame] | 735 | bool JavaVMExt::LoadNativeLibrary(JNIEnv* env, |
| 736 | const std::string& path, |
| 737 | jobject class_loader, |
| 738 | jstring library_path, |
| 739 | std::string* error_msg) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 740 | error_msg->clear(); |
| 741 | |
| 742 | // See if we've already loaded this library. If we have, and the class loader |
| 743 | // matches, return successfully without doing anything. |
| 744 | // TODO: for better results we should canonicalize the pathname (or even compare |
| 745 | // inodes). This implementation is fine if everybody is using System.loadLibrary. |
| 746 | SharedLibrary* library; |
| 747 | Thread* self = Thread::Current(); |
| 748 | { |
| 749 | // TODO: move the locking (and more of this logic) into Libraries. |
| 750 | MutexLock mu(self, *Locks::jni_libraries_lock_); |
| 751 | library = libraries_->Get(path); |
| 752 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 753 | void* class_loader_allocator = nullptr; |
| 754 | { |
| 755 | ScopedObjectAccess soa(env); |
| 756 | // As the incoming class loader is reachable/alive during the call of this function, |
| 757 | // it's okay to decode it without worrying about unexpectedly marking it alive. |
| 758 | mirror::ClassLoader* loader = soa.Decode<mirror::ClassLoader*>(class_loader); |
Andreas Gampe | 2d48e53 | 2016-06-17 12:46:14 -0700 | [diff] [blame] | 759 | |
| 760 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 761 | if (class_linker->IsBootClassLoader(soa, loader)) { |
| 762 | loader = nullptr; |
| 763 | class_loader = nullptr; |
| 764 | } |
| 765 | |
| 766 | class_loader_allocator = class_linker->GetAllocatorForClassLoader(loader); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 767 | CHECK(class_loader_allocator != nullptr); |
| 768 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 769 | if (library != nullptr) { |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 770 | // Use the allocator pointers for class loader equality to avoid unnecessary weak root decode. |
| 771 | if (library->GetClassLoaderAllocator() != class_loader_allocator) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 772 | // The library will be associated with class_loader. The JNI |
| 773 | // spec says we can't load the same library into more than one |
| 774 | // class loader. |
| 775 | StringAppendF(error_msg, "Shared library \"%s\" already opened by " |
| 776 | "ClassLoader %p; can't open in ClassLoader %p", |
| 777 | path.c_str(), library->GetClassLoader(), class_loader); |
| 778 | LOG(WARNING) << error_msg; |
| 779 | return false; |
| 780 | } |
| 781 | VLOG(jni) << "[Shared library \"" << path << "\" already loaded in " |
| 782 | << " ClassLoader " << class_loader << "]"; |
| 783 | if (!library->CheckOnLoadResult()) { |
| 784 | StringAppendF(error_msg, "JNI_OnLoad failed on a previous attempt " |
| 785 | "to load \"%s\"", path.c_str()); |
| 786 | return false; |
| 787 | } |
| 788 | return true; |
| 789 | } |
| 790 | |
| 791 | // Open the shared library. Because we're using a full path, the system |
| 792 | // doesn't have to search through LD_LIBRARY_PATH. (It may do so to |
| 793 | // resolve this library's dependencies though.) |
| 794 | |
| 795 | // Failures here are expected when java.library.path has several entries |
| 796 | // and we have to hunt for the lib. |
| 797 | |
| 798 | // Below we dlopen but there is no paired dlclose, this would be necessary if we supported |
| 799 | // class unloading. Libraries will only be unloaded when the reference count (incremented by |
| 800 | // dlopen) becomes zero from dlclose. |
| 801 | |
| 802 | Locks::mutator_lock_->AssertNotHeld(self); |
| 803 | const char* path_str = path.empty() ? nullptr : path.c_str(); |
Dimitry Ivanov | 942dc298 | 2016-02-24 13:33:33 -0800 | [diff] [blame] | 804 | void* handle = android::OpenNativeLibrary(env, |
| 805 | runtime_->GetTargetSdkVersion(), |
| 806 | path_str, |
| 807 | class_loader, |
| 808 | library_path); |
| 809 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 810 | bool needs_native_bridge = false; |
| 811 | if (handle == nullptr) { |
Calin Juravle | c842352 | 2014-08-12 20:55:20 +0100 | [diff] [blame] | 812 | if (android::NativeBridgeIsSupported(path_str)) { |
Dmitriy Ivanov | 5305672 | 2015-03-23 13:38:20 -0700 | [diff] [blame] | 813 | handle = android::NativeBridgeLoadLibrary(path_str, RTLD_NOW); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 814 | needs_native_bridge = true; |
| 815 | } |
| 816 | } |
| 817 | |
Dmitriy Ivanov | 5305672 | 2015-03-23 13:38:20 -0700 | [diff] [blame] | 818 | VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_NOW) returned " << handle << "]"; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 819 | |
| 820 | if (handle == nullptr) { |
| 821 | *error_msg = dlerror(); |
Dmitriy Ivanov | 5305672 | 2015-03-23 13:38:20 -0700 | [diff] [blame] | 822 | VLOG(jni) << "dlopen(\"" << path << "\", RTLD_NOW) failed: " << *error_msg; |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 823 | return false; |
| 824 | } |
| 825 | |
| 826 | if (env->ExceptionCheck() == JNI_TRUE) { |
| 827 | LOG(ERROR) << "Unexpected exception:"; |
| 828 | env->ExceptionDescribe(); |
| 829 | env->ExceptionClear(); |
| 830 | } |
| 831 | // Create a new entry. |
| 832 | // TODO: move the locking (and more of this logic) into Libraries. |
| 833 | bool created_library = false; |
| 834 | { |
| 835 | // Create SharedLibrary ahead of taking the libraries lock to maintain lock ordering. |
| 836 | std::unique_ptr<SharedLibrary> new_library( |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 837 | new SharedLibrary(env, self, path, handle, class_loader, class_loader_allocator)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 838 | MutexLock mu(self, *Locks::jni_libraries_lock_); |
| 839 | library = libraries_->Get(path); |
| 840 | if (library == nullptr) { // We won race to get libraries_lock. |
| 841 | library = new_library.release(); |
| 842 | libraries_->Put(path, library); |
| 843 | created_library = true; |
| 844 | } |
| 845 | } |
| 846 | if (!created_library) { |
| 847 | LOG(INFO) << "WOW: we lost a race to add shared library: " |
| 848 | << "\"" << path << "\" ClassLoader=" << class_loader; |
| 849 | return library->CheckOnLoadResult(); |
| 850 | } |
| 851 | VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]"; |
| 852 | |
| 853 | bool was_successful = false; |
| 854 | void* sym; |
| 855 | if (needs_native_bridge) { |
| 856 | library->SetNeedsNativeBridge(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 857 | } |
Mathieu Chartier | 598302a | 2015-09-23 14:52:39 -0700 | [diff] [blame] | 858 | sym = library->FindSymbol("JNI_OnLoad", nullptr); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 859 | if (sym == nullptr) { |
| 860 | VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]"; |
| 861 | was_successful = true; |
| 862 | } else { |
| 863 | // Call JNI_OnLoad. We have to override the current class |
| 864 | // loader, which will always be "null" since the stuff at the |
| 865 | // top of the stack is around Runtime.loadLibrary(). (See |
| 866 | // the comments in the JNI FindClass function.) |
| 867 | ScopedLocalRef<jobject> old_class_loader(env, env->NewLocalRef(self->GetClassLoaderOverride())); |
| 868 | self->SetClassLoaderOverride(class_loader); |
| 869 | |
| 870 | VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]"; |
| 871 | typedef int (*JNI_OnLoadFn)(JavaVM*, void*); |
| 872 | JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym); |
| 873 | int version = (*jni_on_load)(this, nullptr); |
| 874 | |
Mathieu Chartier | d000480 | 2014-10-15 16:59:47 -0700 | [diff] [blame] | 875 | if (runtime_->GetTargetSdkVersion() != 0 && runtime_->GetTargetSdkVersion() <= 21) { |
| 876 | fault_manager.EnsureArtActionInFrontOfSignalChain(); |
| 877 | } |
| 878 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 879 | self->SetClassLoaderOverride(old_class_loader.get()); |
| 880 | |
| 881 | if (version == JNI_ERR) { |
| 882 | StringAppendF(error_msg, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str()); |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 883 | } else if (JavaVMExt::IsBadJniVersion(version)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 884 | StringAppendF(error_msg, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d", |
| 885 | path.c_str(), version); |
| 886 | // It's unwise to call dlclose() here, but we can mark it |
| 887 | // as bad and ensure that future load attempts will fail. |
| 888 | // We don't know how far JNI_OnLoad got, so there could |
| 889 | // be some partially-initialized stuff accessible through |
| 890 | // newly-registered native method calls. We could try to |
| 891 | // unregister them, but that doesn't seem worthwhile. |
| 892 | } else { |
| 893 | was_successful = true; |
| 894 | } |
| 895 | VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure") |
| 896 | << " from JNI_OnLoad in \"" << path << "\"]"; |
| 897 | } |
| 898 | |
| 899 | library->SetResult(was_successful); |
| 900 | return was_successful; |
| 901 | } |
| 902 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 903 | void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 904 | CHECK(m->IsNative()); |
| 905 | mirror::Class* c = m->GetDeclaringClass(); |
| 906 | // If this is a static method, it could be called before the class has been initialized. |
| 907 | CHECK(c->IsInitializing()) << c->GetStatus() << " " << PrettyMethod(m); |
| 908 | std::string detail; |
| 909 | void* native_method; |
| 910 | Thread* self = Thread::Current(); |
| 911 | { |
| 912 | MutexLock mu(self, *Locks::jni_libraries_lock_); |
| 913 | native_method = libraries_->FindNativeMethod(m, detail); |
| 914 | } |
| 915 | // Throwing can cause libraries_lock to be reacquired. |
| 916 | if (native_method == nullptr) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 917 | self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str()); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 918 | } |
| 919 | return native_method; |
| 920 | } |
| 921 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 922 | void JavaVMExt::SweepJniWeakGlobals(IsMarkedVisitor* visitor) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 923 | MutexLock mu(Thread::Current(), weak_globals_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 924 | Runtime* const runtime = Runtime::Current(); |
| 925 | for (auto* entry : weak_globals_) { |
| 926 | // Need to skip null here to distinguish between null entries and cleared weak ref entries. |
| 927 | if (!entry->IsNull()) { |
| 928 | // Since this is called by the GC, we don't need a read barrier. |
| 929 | mirror::Object* obj = entry->Read<kWithoutReadBarrier>(); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 930 | mirror::Object* new_obj = visitor->IsMarked(obj); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 931 | if (new_obj == nullptr) { |
| 932 | new_obj = runtime->GetClearedJniWeakGlobal(); |
| 933 | } |
| 934 | *entry = GcRoot<mirror::Object>(new_obj); |
Hiroshi Yamauchi | 8a74117 | 2014-09-08 13:22:56 -0700 | [diff] [blame] | 935 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
Mathieu Chartier | 91c2f0c | 2014-11-26 11:21:15 -0800 | [diff] [blame] | 939 | void JavaVMExt::TrimGlobals() { |
| 940 | WriterMutexLock mu(Thread::Current(), globals_lock_); |
| 941 | globals_.Trim(); |
| 942 | } |
| 943 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 944 | void JavaVMExt::VisitRoots(RootVisitor* visitor) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 945 | Thread* self = Thread::Current(); |
Mathieu Chartier | e34fa1d | 2015-01-14 14:55:47 -0800 | [diff] [blame] | 946 | ReaderMutexLock mu(self, globals_lock_); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 947 | globals_.VisitRoots(visitor, RootInfo(kRootJNIGlobal)); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 948 | // The weak_globals table is visited by the GC itself (because it mutates the table). |
| 949 | } |
| 950 | |
| 951 | // JNI Invocation interface. |
| 952 | |
| 953 | extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 954 | ScopedTrace trace(__FUNCTION__); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 955 | const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args); |
Alex Light | 185d134 | 2016-08-11 10:48:03 -0700 | [diff] [blame] | 956 | if (JavaVMExt::IsBadJniVersion(args->version)) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 957 | LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version; |
| 958 | return JNI_EVERSION; |
| 959 | } |
| 960 | RuntimeOptions options; |
| 961 | for (int i = 0; i < args->nOptions; ++i) { |
| 962 | JavaVMOption* option = &args->options[i]; |
| 963 | options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo)); |
| 964 | } |
| 965 | bool ignore_unrecognized = args->ignoreUnrecognized; |
| 966 | if (!Runtime::Create(options, ignore_unrecognized)) { |
| 967 | return JNI_ERR; |
| 968 | } |
Dimitry Ivanov | c544f34 | 2016-05-09 16:26:13 -0700 | [diff] [blame] | 969 | |
| 970 | // Initialize native loader. This step makes sure we have |
| 971 | // everything set up before we start using JNI. |
| 972 | android::InitializeNativeLoader(); |
| 973 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 974 | Runtime* runtime = Runtime::Current(); |
| 975 | bool started = runtime->Start(); |
| 976 | if (!started) { |
| 977 | delete Thread::Current()->GetJniEnv(); |
| 978 | delete runtime->GetJavaVM(); |
| 979 | LOG(WARNING) << "CreateJavaVM failed"; |
| 980 | return JNI_ERR; |
| 981 | } |
Dimitry Ivanov | 041169f | 2016-04-21 16:01:24 -0700 | [diff] [blame] | 982 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 983 | *p_env = Thread::Current()->GetJniEnv(); |
| 984 | *p_vm = runtime->GetJavaVM(); |
| 985 | return JNI_OK; |
| 986 | } |
| 987 | |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 988 | extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms_buf, jsize buf_len, jsize* vm_count) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 989 | Runtime* runtime = Runtime::Current(); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 990 | if (runtime == nullptr || buf_len == 0) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 991 | *vm_count = 0; |
| 992 | } else { |
| 993 | *vm_count = 1; |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 994 | vms_buf[0] = runtime->GetJavaVM(); |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 995 | } |
| 996 | return JNI_OK; |
| 997 | } |
| 998 | |
| 999 | // Historically unsupported. |
| 1000 | extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) { |
| 1001 | return JNI_ERR; |
| 1002 | } |
| 1003 | |
| 1004 | } // namespace art |