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