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