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