blob: da4c4b2fa48ae28d09e5217b6207a512aa178499 [file] [log] [blame]
Ian Rogers68d8b422014-07-17 11:09:10 -07001/*
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 Gampe57cf00b2017-06-05 17:15:32 -070017#include "java_vm_ext.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070018
19#include <dlfcn.h>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Andreas Gampec6ea7d02017-02-01 16:46:28 -080023#include "art_method-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070024#include "base/dumpable.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070025#include "base/mutex-inl.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070026#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080027#include "base/systrace.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070028#include "check_jni.h"
David Sehr9e734c72018-01-04 17:56:19 -080029#include "dex/dex_file-inl.h"
Mathieu Chartierd0004802014-10-15 16:59:47 -070030#include "fault_handler.h"
Andreas Gampe1b35b462017-09-29 18:52:15 -070031#include "gc/allocation_record.h"
32#include "gc/heap.h"
Andreas Gamped4901292017-05-30 18:41:34 -070033#include "gc_root-inl.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070034#include "indirect_reference_table-inl.h"
Andreas Gampe57cf00b2017-06-05 17:15:32 -070035#include "jni_internal.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070036#include "mirror/class-inl.h"
37#include "mirror/class_loader.h"
Calin Juravlec8423522014-08-12 20:55:20 +010038#include "nativebridge/native_bridge.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070039#include "nativehelper/scoped_local_ref.h"
40#include "nativehelper/scoped_utf_chars.h"
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080041#include "nativeloader/native_loader.h"
Andreas Gampe57cf00b2017-06-05 17:15:32 -070042#include "object_callbacks.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070043#include "parsed_options.h"
Ian Rogersc0542af2014-09-03 16:16:56 -070044#include "runtime-inl.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080045#include "runtime_options.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070046#include "scoped_thread_state_change-inl.h"
Josh Gao85a78cf2017-03-20 16:26:42 -070047#include "sigchain.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070048#include "thread-inl.h"
49#include "thread_list.h"
Steven Morelande431e272017-07-18 16:53:49 -070050#include "ti/agent.h"
Andreas Gampe473191c2017-12-28 16:55:31 -080051#include "well_known_classes.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070052
53namespace art {
54
Andreas Gampe46ee31b2016-12-14 10:11:49 -080055using android::base::StringAppendF;
56using android::base::StringAppendV;
57
Andreas Gampea8e3b862016-10-17 20:12:52 -070058static constexpr size_t kGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogers68d8b422014-07-17 11:09:10 -070059
Andreas Gampea8e3b862016-10-17 20:12:52 -070060static constexpr size_t kWeakGlobalsMax = 51200; // Arbitrary sanity check. (Must fit in 16 bits.)
Ian Rogers68d8b422014-07-17 11:09:10 -070061
Alex Light185d1342016-08-11 10:48:03 -070062bool JavaVMExt::IsBadJniVersion(int version) {
Ian Rogers68d8b422014-07-17 11:09:10 -070063 // We don't support JNI_VERSION_1_1. These are the only other valid versions.
64 return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
65}
66
67class SharedLibrary {
68 public:
69 SharedLibrary(JNIEnv* env, Thread* self, const std::string& path, void* handle,
Zhenhua WANG8447e6d2016-05-30 11:10:29 +080070 bool needs_native_bridge, jobject class_loader, void* class_loader_allocator)
Ian Rogers68d8b422014-07-17 11:09:10 -070071 : path_(path),
72 handle_(handle),
Zhenhua WANG8447e6d2016-05-30 11:10:29 +080073 needs_native_bridge_(needs_native_bridge),
Mathieu Chartier598302a2015-09-23 14:52:39 -070074 class_loader_(env->NewWeakGlobalRef(class_loader)),
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -080075 class_loader_allocator_(class_loader_allocator),
Ian Rogers68d8b422014-07-17 11:09:10 -070076 jni_on_load_lock_("JNI_OnLoad lock"),
77 jni_on_load_cond_("JNI_OnLoad condition variable", jni_on_load_lock_),
78 jni_on_load_thread_id_(self->GetThreadId()),
79 jni_on_load_result_(kPending) {
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -080080 CHECK(class_loader_allocator_ != nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -070081 }
82
83 ~SharedLibrary() {
84 Thread* self = Thread::Current();
85 if (self != nullptr) {
Mathieu Chartier598302a2015-09-23 14:52:39 -070086 self->GetJniEnv()->DeleteWeakGlobalRef(class_loader_);
Ian Rogers68d8b422014-07-17 11:09:10 -070087 }
Alex Lightbc5669e2016-06-13 17:22:13 +000088
Zhenhua WANG8447e6d2016-05-30 11:10:29 +080089 android::CloseNativeLibrary(handle_, needs_native_bridge_);
Ian Rogers68d8b422014-07-17 11:09:10 -070090 }
91
Mathieu Chartier598302a2015-09-23 14:52:39 -070092 jweak GetClassLoader() const {
Ian Rogers68d8b422014-07-17 11:09:10 -070093 return class_loader_;
94 }
95
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -080096 const void* GetClassLoaderAllocator() const {
97 return class_loader_allocator_;
98 }
99
Ian Rogers68d8b422014-07-17 11:09:10 -0700100 const std::string& GetPath() const {
101 return path_;
102 }
103
104 /*
105 * Check the result of an earlier call to JNI_OnLoad on this library.
106 * If the call has not yet finished in another thread, wait for it.
107 */
108 bool CheckOnLoadResult()
Mathieu Chartier90443472015-07-16 20:32:27 -0700109 REQUIRES(!jni_on_load_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700110 Thread* self = Thread::Current();
111 bool okay;
112 {
113 MutexLock mu(self, jni_on_load_lock_);
114
115 if (jni_on_load_thread_id_ == self->GetThreadId()) {
116 // Check this so we don't end up waiting for ourselves. We need to return "true" so the
117 // caller can continue.
118 LOG(INFO) << *self << " recursive attempt to load library " << "\"" << path_ << "\"";
119 okay = true;
120 } else {
121 while (jni_on_load_result_ == kPending) {
122 VLOG(jni) << "[" << *self << " waiting for \"" << path_ << "\" " << "JNI_OnLoad...]";
123 jni_on_load_cond_.Wait(self);
124 }
125
126 okay = (jni_on_load_result_ == kOkay);
127 VLOG(jni) << "[Earlier JNI_OnLoad for \"" << path_ << "\" "
128 << (okay ? "succeeded" : "failed") << "]";
129 }
130 }
131 return okay;
132 }
133
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 void SetResult(bool result) REQUIRES(!jni_on_load_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700135 Thread* self = Thread::Current();
136 MutexLock mu(self, jni_on_load_lock_);
137
138 jni_on_load_result_ = result ? kOkay : kFailed;
139 jni_on_load_thread_id_ = 0;
140
141 // Broadcast a wakeup to anybody sleeping on the condition variable.
142 jni_on_load_cond_.Broadcast(self);
143 }
144
Zhenhua WANG8447e6d2016-05-30 11:10:29 +0800145 void SetNeedsNativeBridge(bool needs) {
146 needs_native_bridge_ = needs;
Ian Rogers68d8b422014-07-17 11:09:10 -0700147 }
148
149 bool NeedsNativeBridge() const {
150 return needs_native_bridge_;
151 }
152
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700153 // No mutator lock since dlsym may block for a while if another thread is doing dlopen.
154 void* FindSymbol(const std::string& symbol_name, const char* shorty = nullptr)
155 REQUIRES(!Locks::mutator_lock_) {
Mathieu Chartier598302a2015-09-23 14:52:39 -0700156 return NeedsNativeBridge()
157 ? FindSymbolWithNativeBridge(symbol_name.c_str(), shorty)
158 : FindSymbolWithoutNativeBridge(symbol_name.c_str());
159 }
160
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700161 // No mutator lock since dlsym may block for a while if another thread is doing dlopen.
162 void* FindSymbolWithoutNativeBridge(const std::string& symbol_name)
163 REQUIRES(!Locks::mutator_lock_) {
Andreas Gampe8fec90b2015-06-30 11:23:44 -0700164 CHECK(!NeedsNativeBridge());
165
Ian Rogers68d8b422014-07-17 11:09:10 -0700166 return dlsym(handle_, symbol_name.c_str());
167 }
168
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700169 void* FindSymbolWithNativeBridge(const std::string& symbol_name, const char* shorty)
170 REQUIRES(!Locks::mutator_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700171 CHECK(NeedsNativeBridge());
172
173 uint32_t len = 0;
Calin Juravlec8423522014-08-12 20:55:20 +0100174 return android::NativeBridgeGetTrampoline(handle_, symbol_name.c_str(), shorty, len);
Ian Rogers68d8b422014-07-17 11:09:10 -0700175 }
176
177 private:
178 enum JNI_OnLoadState {
179 kPending,
180 kFailed,
181 kOkay,
182 };
183
184 // Path to library "/system/lib/libjni.so".
185 const std::string path_;
186
187 // The void* returned by dlopen(3).
188 void* const handle_;
189
190 // True if a native bridge is required.
191 bool needs_native_bridge_;
192
Mathieu Chartier598302a2015-09-23 14:52:39 -0700193 // The ClassLoader this library is associated with, a weak global JNI reference that is
Ian Rogers68d8b422014-07-17 11:09:10 -0700194 // created/deleted with the scope of the library.
Mathieu Chartier598302a2015-09-23 14:52:39 -0700195 const jweak class_loader_;
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800196 // Used to do equality check on class loaders so we can avoid decoding the weak root and read
197 // barriers that mess with class unloading.
198 const void* class_loader_allocator_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700199
200 // Guards remaining items.
201 Mutex jni_on_load_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
202 // Wait for JNI_OnLoad in other thread.
203 ConditionVariable jni_on_load_cond_ GUARDED_BY(jni_on_load_lock_);
204 // Recursive invocation guard.
205 uint32_t jni_on_load_thread_id_ GUARDED_BY(jni_on_load_lock_);
206 // Result of earlier JNI_OnLoad call.
207 JNI_OnLoadState jni_on_load_result_ GUARDED_BY(jni_on_load_lock_);
208};
209
210// This exists mainly to keep implementation details out of the header file.
211class Libraries {
212 public:
213 Libraries() {
214 }
215
216 ~Libraries() {
217 STLDeleteValues(&libraries_);
218 }
219
Mathieu Chartier598302a2015-09-23 14:52:39 -0700220 // NO_THREAD_SAFETY_ANALYSIS since this may be called from Dumpable. Dumpable can't be annotated
221 // properly due to the template. The caller should be holding the jni_libraries_lock_.
222 void Dump(std::ostream& os) const NO_THREAD_SAFETY_ANALYSIS {
223 Locks::jni_libraries_lock_->AssertHeld(Thread::Current());
Ian Rogers68d8b422014-07-17 11:09:10 -0700224 bool first = true;
225 for (const auto& library : libraries_) {
226 if (!first) {
227 os << ' ';
228 }
229 first = false;
230 os << library.first;
231 }
232 }
233
Mathieu Chartier598302a2015-09-23 14:52:39 -0700234 size_t size() const REQUIRES(Locks::jni_libraries_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700235 return libraries_.size();
236 }
237
Mathieu Chartier598302a2015-09-23 14:52:39 -0700238 SharedLibrary* Get(const std::string& path) REQUIRES(Locks::jni_libraries_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700239 auto it = libraries_.find(path);
240 return (it == libraries_.end()) ? nullptr : it->second;
241 }
242
Mathieu Chartier598302a2015-09-23 14:52:39 -0700243 void Put(const std::string& path, SharedLibrary* library)
244 REQUIRES(Locks::jni_libraries_lock_) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700245 libraries_.Put(path, library);
246 }
247
248 // See section 11.3 "Linking Native Methods" of the JNI spec.
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700249 void* FindNativeMethod(Thread* self, ArtMethod* m, std::string& detail)
250 REQUIRES(!Locks::jni_libraries_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700252 std::string jni_short_name(m->JniShortName());
253 std::string jni_long_name(m->JniLongName());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800254 mirror::ClassLoader* const declaring_class_loader = m->GetDeclaringClass()->GetClassLoader();
Ian Rogers68d8b422014-07-17 11:09:10 -0700255 ScopedObjectAccessUnchecked soa(Thread::Current());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800256 void* const declaring_class_loader_allocator =
257 Runtime::Current()->GetClassLinker()->GetAllocatorForClassLoader(declaring_class_loader);
258 CHECK(declaring_class_loader_allocator != nullptr);
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700259 // TODO: Avoid calling GetShorty here to prevent dirtying dex pages?
260 const char* shorty = m->GetShorty();
261 {
262 // Go to suspended since dlsym may block for a long time if other threads are using dlopen.
263 ScopedThreadSuspension sts(self, kNative);
264 void* native_code = FindNativeMethodInternal(self,
265 declaring_class_loader_allocator,
266 shorty,
267 jni_short_name,
268 jni_long_name);
269 if (native_code != nullptr) {
270 return native_code;
Ian Rogers68d8b422014-07-17 11:09:10 -0700271 }
272 }
273 detail += "No implementation found for ";
David Sehr709b0702016-10-13 09:12:37 -0700274 detail += m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700275 detail += " (tried " + jni_short_name + " and " + jni_long_name + ")";
Ian Rogers68d8b422014-07-17 11:09:10 -0700276 return nullptr;
277 }
278
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700279 void* FindNativeMethodInternal(Thread* self,
280 void* declaring_class_loader_allocator,
281 const char* shorty,
282 const std::string& jni_short_name,
283 const std::string& jni_long_name)
284 REQUIRES(!Locks::jni_libraries_lock_)
285 REQUIRES(!Locks::mutator_lock_) {
286 MutexLock mu(self, *Locks::jni_libraries_lock_);
287 for (const auto& lib : libraries_) {
288 SharedLibrary* const library = lib.second;
289 // Use the allocator address for class loader equality to avoid unnecessary weak root decode.
290 if (library->GetClassLoaderAllocator() != declaring_class_loader_allocator) {
291 // We only search libraries loaded by the appropriate ClassLoader.
292 continue;
293 }
294 // Try the short name then the long name...
295 const char* arg_shorty = library->NeedsNativeBridge() ? shorty : nullptr;
296 void* fn = library->FindSymbol(jni_short_name, arg_shorty);
297 if (fn == nullptr) {
298 fn = library->FindSymbol(jni_long_name, arg_shorty);
299 }
300 if (fn != nullptr) {
301 VLOG(jni) << "[Found native code for " << jni_long_name
302 << " in \"" << library->GetPath() << "\"]";
303 return fn;
304 }
305 }
306 return nullptr;
307 }
308
Mathieu Chartier598302a2015-09-23 14:52:39 -0700309 // Unload native libraries with cleared class loaders.
310 void UnloadNativeLibraries()
311 REQUIRES(!Locks::jni_libraries_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700312 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700313 Thread* const self = Thread::Current();
Alex Lightbc5669e2016-06-13 17:22:13 +0000314 std::vector<SharedLibrary*> unload_libraries;
Mathieu Chartier598302a2015-09-23 14:52:39 -0700315 {
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700316 MutexLock mu(self, *Locks::jni_libraries_lock_);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700317 for (auto it = libraries_.begin(); it != libraries_.end(); ) {
318 SharedLibrary* const library = it->second;
319 // If class loader is null then it was unloaded, call JNI_OnUnload.
Mathieu Chartiercffb7472015-09-28 10:33:00 -0700320 const jweak class_loader = library->GetClassLoader();
321 // If class_loader is a null jobject then it is the boot class loader. We should not unload
322 // the native libraries of the boot class loader.
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700323 if (class_loader != nullptr && self->IsJWeakCleared(class_loader)) {
Alex Lightbc5669e2016-06-13 17:22:13 +0000324 unload_libraries.push_back(library);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700325 it = libraries_.erase(it);
326 } else {
327 ++it;
328 }
329 }
330 }
Mathieu Chartier46e75d02017-06-02 16:03:39 -0700331 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700332 // Do this without holding the jni libraries lock to prevent possible deadlocks.
Alex Lightbc5669e2016-06-13 17:22:13 +0000333 typedef void (*JNI_OnUnloadFn)(JavaVM*, void*);
334 for (auto library : unload_libraries) {
335 void* const sym = library->FindSymbol("JNI_OnUnload", nullptr);
336 if (sym == nullptr) {
337 VLOG(jni) << "[No JNI_OnUnload found in \"" << library->GetPath() << "\"]";
338 } else {
339 VLOG(jni) << "[JNI_OnUnload found for \"" << library->GetPath() << "\"]: Calling...";
340 JNI_OnUnloadFn jni_on_unload = reinterpret_cast<JNI_OnUnloadFn>(sym);
Ian Rogers55256cb2017-12-21 17:07:11 -0800341 jni_on_unload(self->GetJniEnv()->GetVm(), nullptr);
Alex Lightbc5669e2016-06-13 17:22:13 +0000342 }
343 delete library;
Mathieu Chartier598302a2015-09-23 14:52:39 -0700344 }
345 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700346
Mathieu Chartier598302a2015-09-23 14:52:39 -0700347 private:
348 AllocationTrackingSafeMap<std::string, SharedLibrary*, kAllocatorTagJNILibraries> libraries_
349 GUARDED_BY(Locks::jni_libraries_lock_);
350};
Ian Rogers68d8b422014-07-17 11:09:10 -0700351
352class JII {
353 public:
354 static jint DestroyJavaVM(JavaVM* vm) {
355 if (vm == nullptr) {
356 return JNI_ERR;
357 }
358 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
359 delete raw_vm->GetRuntime();
Dimitry Ivanov39d68ef2016-04-29 16:02:38 -0700360 android::ResetNativeLoader();
Ian Rogers68d8b422014-07-17 11:09:10 -0700361 return JNI_OK;
362 }
363
364 static jint AttachCurrentThread(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
365 return AttachCurrentThreadInternal(vm, p_env, thr_args, false);
366 }
367
368 static jint AttachCurrentThreadAsDaemon(JavaVM* vm, JNIEnv** p_env, void* thr_args) {
369 return AttachCurrentThreadInternal(vm, p_env, thr_args, true);
370 }
371
372 static jint DetachCurrentThread(JavaVM* vm) {
373 if (vm == nullptr || Thread::Current() == nullptr) {
374 return JNI_ERR;
375 }
376 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
377 Runtime* runtime = raw_vm->GetRuntime();
378 runtime->DetachCurrentThread();
379 return JNI_OK;
380 }
381
382 static jint GetEnv(JavaVM* vm, void** env, jint version) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700383 if (vm == nullptr || env == nullptr) {
384 return JNI_ERR;
385 }
386 Thread* thread = Thread::Current();
387 if (thread == nullptr) {
388 *env = nullptr;
389 return JNI_EDETACHED;
390 }
Alex Light185d1342016-08-11 10:48:03 -0700391 JavaVMExt* raw_vm = reinterpret_cast<JavaVMExt*>(vm);
392 return raw_vm->HandleGetEnv(env, version);
Ian Rogers68d8b422014-07-17 11:09:10 -0700393 }
394
395 private:
396 static jint AttachCurrentThreadInternal(JavaVM* vm, JNIEnv** p_env, void* raw_args, bool as_daemon) {
397 if (vm == nullptr || p_env == nullptr) {
398 return JNI_ERR;
399 }
400
401 // Return immediately if we're already attached.
402 Thread* self = Thread::Current();
403 if (self != nullptr) {
404 *p_env = self->GetJniEnv();
405 return JNI_OK;
406 }
407
408 Runtime* runtime = reinterpret_cast<JavaVMExt*>(vm)->GetRuntime();
409
410 // No threads allowed in zygote mode.
411 if (runtime->IsZygote()) {
412 LOG(ERROR) << "Attempt to attach a thread in the zygote";
413 return JNI_ERR;
414 }
415
416 JavaVMAttachArgs* args = static_cast<JavaVMAttachArgs*>(raw_args);
417 const char* thread_name = nullptr;
418 jobject thread_group = nullptr;
419 if (args != nullptr) {
Alex Light185d1342016-08-11 10:48:03 -0700420 if (JavaVMExt::IsBadJniVersion(args->version)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700421 LOG(ERROR) << "Bad JNI version passed to "
422 << (as_daemon ? "AttachCurrentThreadAsDaemon" : "AttachCurrentThread") << ": "
423 << args->version;
424 return JNI_EVERSION;
425 }
426 thread_name = args->name;
427 thread_group = args->group;
428 }
429
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800430 if (!runtime->AttachCurrentThread(thread_name, as_daemon, thread_group,
431 !runtime->IsAotCompiler())) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700432 *p_env = nullptr;
433 return JNI_ERR;
434 } else {
435 *p_env = Thread::Current()->GetJniEnv();
436 return JNI_OK;
437 }
438 }
439};
440
441const JNIInvokeInterface gJniInvokeInterface = {
442 nullptr, // reserved0
443 nullptr, // reserved1
444 nullptr, // reserved2
445 JII::DestroyJavaVM,
446 JII::AttachCurrentThread,
447 JII::DetachCurrentThread,
448 JII::GetEnv,
449 JII::AttachCurrentThreadAsDaemon
450};
451
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100452JavaVMExt::JavaVMExt(Runtime* runtime,
453 const RuntimeArgumentMap& runtime_options,
454 std::string* error_msg)
Ian Rogers68d8b422014-07-17 11:09:10 -0700455 : runtime_(runtime),
456 check_jni_abort_hook_(nullptr),
457 check_jni_abort_hook_data_(nullptr),
458 check_jni_(false), // Initialized properly in the constructor body below.
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800459 force_copy_(runtime_options.Exists(RuntimeArgumentMap::JniOptsForceCopy)),
460 tracing_enabled_(runtime_options.Exists(RuntimeArgumentMap::JniTrace)
461 || VLOG_IS_ON(third_party_jni)),
462 trace_(runtime_options.GetOrDefault(RuntimeArgumentMap::JniTrace)),
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700463 globals_(kGlobalsMax, kGlobal, IndirectReferenceTable::ResizableCapacity::kNo, error_msg),
Ian Rogers68d8b422014-07-17 11:09:10 -0700464 libraries_(new Libraries),
465 unchecked_functions_(&gJniInvokeInterface),
Andreas Gampe9d7ef622016-10-24 19:35:19 -0700466 weak_globals_(kWeakGlobalsMax,
467 kWeakGlobal,
468 IndirectReferenceTable::ResizableCapacity::kNo,
469 error_msg),
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700470 allow_accessing_weak_globals_(true),
Andreas Gampe05a364c2016-10-14 13:27:12 -0700471 weak_globals_add_condition_("weak globals add condition",
472 (CHECK(Locks::jni_weak_globals_lock_ != nullptr),
473 *Locks::jni_weak_globals_lock_)),
Andreas Gampe1b35b462017-09-29 18:52:15 -0700474 env_hooks_(),
475 enable_allocation_tracking_delta_(
476 runtime_options.GetOrDefault(RuntimeArgumentMap::GlobalRefAllocStackTraceLimit)),
477 allocation_tracking_enabled_(false),
478 old_allocation_tracking_state_(false) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700479 functions = unchecked_functions_;
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800480 SetCheckJniEnabled(runtime_options.Exists(RuntimeArgumentMap::CheckJni));
Ian Rogers68d8b422014-07-17 11:09:10 -0700481}
482
483JavaVMExt::~JavaVMExt() {
484}
485
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100486// Checking "globals" and "weak_globals" usually requires locks, but we
487// don't need the locks to check for validity when constructing the
488// object. Use NO_THREAD_SAFETY_ANALYSIS for this.
489std::unique_ptr<JavaVMExt> JavaVMExt::Create(Runtime* runtime,
490 const RuntimeArgumentMap& runtime_options,
491 std::string* error_msg) NO_THREAD_SAFETY_ANALYSIS {
492 std::unique_ptr<JavaVMExt> java_vm(new JavaVMExt(runtime, runtime_options, error_msg));
493 if (java_vm && java_vm->globals_.IsValid() && java_vm->weak_globals_.IsValid()) {
494 return java_vm;
495 }
496 return nullptr;
497}
498
Alex Light185d1342016-08-11 10:48:03 -0700499jint JavaVMExt::HandleGetEnv(/*out*/void** env, jint version) {
500 for (GetEnvHook hook : env_hooks_) {
501 jint res = hook(this, env, version);
502 if (res == JNI_OK) {
503 return JNI_OK;
504 } else if (res != JNI_EVERSION) {
505 LOG(ERROR) << "Error returned from a plugin GetEnv handler! " << res;
506 return res;
507 }
508 }
509 LOG(ERROR) << "Bad JNI version passed to GetEnv: " << version;
510 return JNI_EVERSION;
511}
512
513// Add a hook to handle getting environments from the GetEnv call.
514void JavaVMExt::AddEnvironmentHook(GetEnvHook hook) {
515 CHECK(hook != nullptr) << "environment hooks shouldn't be null!";
516 env_hooks_.push_back(hook);
517}
518
Ian Rogers68d8b422014-07-17 11:09:10 -0700519void JavaVMExt::JniAbort(const char* jni_function_name, const char* msg) {
520 Thread* self = Thread::Current();
521 ScopedObjectAccess soa(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 ArtMethod* current_method = self->GetCurrentMethod(nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -0700523
524 std::ostringstream os;
525 os << "JNI DETECTED ERROR IN APPLICATION: " << msg;
526
527 if (jni_function_name != nullptr) {
528 os << "\n in call to " << jni_function_name;
529 }
530 // TODO: is this useful given that we're about to dump the calling thread's stack?
531 if (current_method != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700532 os << "\n from " << current_method->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -0700533 }
534 os << "\n";
535 self->Dump(os);
536
537 if (check_jni_abort_hook_ != nullptr) {
538 check_jni_abort_hook_(check_jni_abort_hook_data_, os.str());
539 } else {
540 // Ensure that we get a native stack trace for this thread.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700541 ScopedThreadSuspension sts(self, kNative);
Ian Rogers68d8b422014-07-17 11:09:10 -0700542 LOG(FATAL) << os.str();
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700543 UNREACHABLE();
Ian Rogers68d8b422014-07-17 11:09:10 -0700544 }
545}
546
547void JavaVMExt::JniAbortV(const char* jni_function_name, const char* fmt, va_list ap) {
548 std::string msg;
549 StringAppendV(&msg, fmt, ap);
550 JniAbort(jni_function_name, msg.c_str());
551}
552
553void JavaVMExt::JniAbortF(const char* jni_function_name, const char* fmt, ...) {
554 va_list args;
555 va_start(args, fmt);
556 JniAbortV(jni_function_name, fmt, args);
557 va_end(args);
558}
559
Mathieu Chartiere401d142015-04-22 13:56:20 -0700560bool JavaVMExt::ShouldTrace(ArtMethod* method) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700561 // Fast where no tracing is enabled.
562 if (trace_.empty() && !VLOG_IS_ON(third_party_jni)) {
563 return false;
564 }
565 // Perform checks based on class name.
566 StringPiece class_name(method->GetDeclaringClassDescriptor());
567 if (!trace_.empty() && class_name.find(trace_) != std::string::npos) {
568 return true;
569 }
570 if (!VLOG_IS_ON(third_party_jni)) {
571 return false;
572 }
573 // Return true if we're trying to log all third-party JNI activity and 'method' doesn't look
574 // like part of Android.
575 static const char* gBuiltInPrefixes[] = {
576 "Landroid/",
577 "Lcom/android/",
578 "Lcom/google/android/",
579 "Ldalvik/",
580 "Ljava/",
581 "Ljavax/",
582 "Llibcore/",
583 "Lorg/apache/harmony/",
584 };
585 for (size_t i = 0; i < arraysize(gBuiltInPrefixes); ++i) {
586 if (class_name.starts_with(gBuiltInPrefixes[i])) {
587 return false;
588 }
589 }
590 return true;
591}
592
Andreas Gampe1b35b462017-09-29 18:52:15 -0700593void JavaVMExt::CheckGlobalRefAllocationTracking() {
594 if (LIKELY(enable_allocation_tracking_delta_ == 0)) {
595 return;
596 }
597 size_t simple_free_capacity = globals_.FreeCapacity();
598 if (UNLIKELY(simple_free_capacity <= enable_allocation_tracking_delta_)) {
599 if (!allocation_tracking_enabled_) {
600 LOG(WARNING) << "Global reference storage appears close to exhaustion, program termination "
601 << "may be imminent. Enabling allocation tracking to improve abort diagnostics. "
602 << "This will result in program slow-down.";
603
604 old_allocation_tracking_state_ = runtime_->GetHeap()->IsAllocTrackingEnabled();
605 if (!old_allocation_tracking_state_) {
606 // Need to be guaranteed suspended.
607 ScopedObjectAccess soa(Thread::Current());
608 ScopedThreadSuspension sts(soa.Self(), ThreadState::kNative);
609 gc::AllocRecordObjectMap::SetAllocTrackingEnabled(true);
610 }
611 allocation_tracking_enabled_ = true;
612 }
613 } else {
614 if (UNLIKELY(allocation_tracking_enabled_)) {
615 if (!old_allocation_tracking_state_) {
616 // Need to be guaranteed suspended.
617 ScopedObjectAccess soa(Thread::Current());
618 ScopedThreadSuspension sts(soa.Self(), ThreadState::kNative);
619 gc::AllocRecordObjectMap::SetAllocTrackingEnabled(false);
620 }
Andreas Gampe45485342017-10-11 12:04:35 -0700621 allocation_tracking_enabled_ = false;
Andreas Gampe1b35b462017-09-29 18:52:15 -0700622 }
623 }
624}
625
Mathieu Chartier0795f232016-09-27 18:43:30 -0700626jobject JavaVMExt::AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700627 // Check for null after decoding the object to handle cleared weak globals.
628 if (obj == nullptr) {
629 return nullptr;
630 }
Andreas Gampe1b35b462017-09-29 18:52:15 -0700631 IndirectRef ref;
Andreas Gampe25651122017-09-25 14:50:23 -0700632 std::string error_msg;
Andreas Gampe1b35b462017-09-29 18:52:15 -0700633 {
634 WriterMutexLock mu(self, *Locks::jni_globals_lock_);
635 ref = globals_.Add(kIRTFirstSegment, obj, &error_msg);
636 }
Andreas Gampe25651122017-09-25 14:50:23 -0700637 if (UNLIKELY(ref == nullptr)) {
638 LOG(FATAL) << error_msg;
639 UNREACHABLE();
640 }
Andreas Gampe1b35b462017-09-29 18:52:15 -0700641 CheckGlobalRefAllocationTracking();
Ian Rogers68d8b422014-07-17 11:09:10 -0700642 return reinterpret_cast<jobject>(ref);
643}
644
Mathieu Chartier0795f232016-09-27 18:43:30 -0700645jweak JavaVMExt::AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700646 if (obj == nullptr) {
647 return nullptr;
648 }
Andreas Gampe05a364c2016-10-14 13:27:12 -0700649 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Hiroshi Yamauchif1c6f872017-01-06 12:23:47 -0800650 // CMS needs this to block for concurrent reference processing because an object allocated during
651 // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak
652 // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant.
653 while (!kUseReadBarrier && UNLIKELY(!MayAccessWeakGlobals(self))) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700654 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
655 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -0800656 self->CheckEmptyCheckpointFromWeakRefAccess(Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700657 weak_globals_add_condition_.WaitHoldingLocks(self);
658 }
Andreas Gampe25651122017-09-25 14:50:23 -0700659 std::string error_msg;
660 IndirectRef ref = weak_globals_.Add(kIRTFirstSegment, obj, &error_msg);
661 if (UNLIKELY(ref == nullptr)) {
662 LOG(FATAL) << error_msg;
663 UNREACHABLE();
664 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700665 return reinterpret_cast<jweak>(ref);
666}
667
668void JavaVMExt::DeleteGlobalRef(Thread* self, jobject obj) {
669 if (obj == nullptr) {
670 return;
671 }
Andreas Gampe1b35b462017-09-29 18:52:15 -0700672 {
673 WriterMutexLock mu(self, *Locks::jni_globals_lock_);
674 if (!globals_.Remove(kIRTFirstSegment, obj)) {
675 LOG(WARNING) << "JNI WARNING: DeleteGlobalRef(" << obj << ") "
676 << "failed to find entry";
677 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700678 }
Andreas Gampe1b35b462017-09-29 18:52:15 -0700679 CheckGlobalRefAllocationTracking();
Ian Rogers68d8b422014-07-17 11:09:10 -0700680}
681
682void JavaVMExt::DeleteWeakGlobalRef(Thread* self, jweak obj) {
683 if (obj == nullptr) {
684 return;
685 }
Andreas Gampe05a364c2016-10-14 13:27:12 -0700686 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Andreas Gampee03662b2016-10-13 17:12:56 -0700687 if (!weak_globals_.Remove(kIRTFirstSegment, obj)) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700688 LOG(WARNING) << "JNI WARNING: DeleteWeakGlobalRef(" << obj << ") "
689 << "failed to find entry";
690 }
691}
692
693static void ThreadEnableCheckJni(Thread* thread, void* arg) {
694 bool* check_jni = reinterpret_cast<bool*>(arg);
695 thread->GetJniEnv()->SetCheckJniEnabled(*check_jni);
696}
697
698bool JavaVMExt::SetCheckJniEnabled(bool enabled) {
699 bool old_check_jni = check_jni_;
700 check_jni_ = enabled;
701 functions = enabled ? GetCheckJniInvokeInterface() : unchecked_functions_;
702 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
703 runtime_->GetThreadList()->ForEach(ThreadEnableCheckJni, &check_jni_);
704 return old_check_jni;
705}
706
707void JavaVMExt::DumpForSigQuit(std::ostream& os) {
708 os << "JNI: CheckJNI is " << (check_jni_ ? "on" : "off");
709 if (force_copy_) {
710 os << " (with forcecopy)";
711 }
712 Thread* self = Thread::Current();
713 {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700714 ReaderMutexLock mu(self, *Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700715 os << "; globals=" << globals_.Capacity();
716 }
717 {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700718 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700719 if (weak_globals_.Capacity() > 0) {
720 os << " (plus " << weak_globals_.Capacity() << " weak)";
721 }
722 }
723 os << '\n';
724
725 {
726 MutexLock mu(self, *Locks::jni_libraries_lock_);
727 os << "Libraries: " << Dumpable<Libraries>(*libraries_) << " (" << libraries_->size() << ")\n";
728 }
729}
730
731void JavaVMExt::DisallowNewWeakGlobals() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700732 CHECK(!kUseReadBarrier);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700733 Thread* const self = Thread::Current();
Andreas Gampe05a364c2016-10-14 13:27:12 -0700734 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700735 // DisallowNewWeakGlobals is only called by CMS during the pause. It is required to have the
736 // mutator lock exclusively held so that we don't have any threads in the middle of
737 // DecodeWeakGlobal.
738 Locks::mutator_lock_->AssertExclusiveHeld(self);
739 allow_accessing_weak_globals_.StoreSequentiallyConsistent(false);
Ian Rogers68d8b422014-07-17 11:09:10 -0700740}
741
742void JavaVMExt::AllowNewWeakGlobals() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -0700743 CHECK(!kUseReadBarrier);
Ian Rogers68d8b422014-07-17 11:09:10 -0700744 Thread* self = Thread::Current();
Andreas Gampe05a364c2016-10-14 13:27:12 -0700745 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700746 allow_accessing_weak_globals_.StoreSequentiallyConsistent(true);
Ian Rogers68d8b422014-07-17 11:09:10 -0700747 weak_globals_add_condition_.Broadcast(self);
748}
749
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700750void JavaVMExt::BroadcastForNewWeakGlobals() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700751 Thread* self = Thread::Current();
Andreas Gampe05a364c2016-10-14 13:27:12 -0700752 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -0700753 weak_globals_add_condition_.Broadcast(self);
754}
755
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700756ObjPtr<mirror::Object> JavaVMExt::DecodeGlobal(IndirectRef ref) {
757 return globals_.SynchronizedGet(ref);
Ian Rogers68d8b422014-07-17 11:09:10 -0700758}
759
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700760void JavaVMExt::UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result) {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700761 WriterMutexLock mu(self, *Locks::jni_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700762 globals_.Update(ref, result);
763}
764
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700765inline bool JavaVMExt::MayAccessWeakGlobals(Thread* self) const {
766 return MayAccessWeakGlobalsUnlocked(self);
767}
768
769inline bool JavaVMExt::MayAccessWeakGlobalsUnlocked(Thread* self) const {
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700770 DCHECK(self != nullptr);
771 return kUseReadBarrier ?
772 self->GetWeakRefAccessEnabled() :
773 allow_accessing_weak_globals_.LoadSequentiallyConsistent();
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700774}
775
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700776ObjPtr<mirror::Object> JavaVMExt::DecodeWeakGlobal(Thread* self, IndirectRef ref) {
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700777 // It is safe to access GetWeakRefAccessEnabled without the lock since CC uses checkpoints to call
778 // SetWeakRefAccessEnabled, and the other collectors only modify allow_accessing_weak_globals_
779 // when the mutators are paused.
780 // This only applies in the case where MayAccessWeakGlobals goes from false to true. In the other
781 // case, it may be racy, this is benign since DecodeWeakGlobalLocked does the correct behavior
782 // if MayAccessWeakGlobals is false.
Andreas Gampedc061d02016-10-24 13:19:37 -0700783 DCHECK_EQ(IndirectReferenceTable::GetIndirectRefKind(ref), kWeakGlobal);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700784 if (LIKELY(MayAccessWeakGlobalsUnlocked(self))) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700785 return weak_globals_.SynchronizedGet(ref);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700786 }
Andreas Gampe05a364c2016-10-14 13:27:12 -0700787 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700788 return DecodeWeakGlobalLocked(self, ref);
789}
790
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700791ObjPtr<mirror::Object> JavaVMExt::DecodeWeakGlobalLocked(Thread* self, IndirectRef ref) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700792 if (kDebugLocking) {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700793 Locks::jni_weak_globals_lock_->AssertHeld(self);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700794 }
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700795 while (UNLIKELY(!MayAccessWeakGlobals(self))) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700796 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
797 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -0800798 self->CheckEmptyCheckpointFromWeakRefAccess(Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700799 weak_globals_add_condition_.WaitHoldingLocks(self);
800 }
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700801 return weak_globals_.Get(ref);
Ian Rogers68d8b422014-07-17 11:09:10 -0700802}
803
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700804ObjPtr<mirror::Object> JavaVMExt::DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref) {
Andreas Gampedc061d02016-10-24 13:19:37 -0700805 DCHECK_EQ(IndirectReferenceTable::GetIndirectRefKind(ref), kWeakGlobal);
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700806 DCHECK(Runtime::Current()->IsShuttingDown(self));
807 if (self != nullptr) {
808 return DecodeWeakGlobal(self, ref);
809 }
810 // self can be null during a runtime shutdown. ~Runtime()->~ClassLinker()->DecodeWeakGlobal().
811 if (!kUseReadBarrier) {
812 DCHECK(allow_accessing_weak_globals_.LoadSequentiallyConsistent());
813 }
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700814 return weak_globals_.SynchronizedGet(ref);
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700815}
816
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800817bool JavaVMExt::IsWeakGlobalCleared(Thread* self, IndirectRef ref) {
Andreas Gampedc061d02016-10-24 13:19:37 -0700818 DCHECK_EQ(IndirectReferenceTable::GetIndirectRefKind(ref), kWeakGlobal);
Andreas Gampe05a364c2016-10-14 13:27:12 -0700819 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800820 while (UNLIKELY(!MayAccessWeakGlobals(self))) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -0700821 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
822 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -0800823 self->CheckEmptyCheckpointFromWeakRefAccess(Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800824 weak_globals_add_condition_.WaitHoldingLocks(self);
825 }
826 // When just checking a weak ref has been cleared, avoid triggering the read barrier in decode
827 // (DecodeWeakGlobal) so that we won't accidentally mark the object alive. Since the cleared
828 // sentinel is a non-moving object, we can compare the ref to it without the read barrier and
829 // decide if it's cleared.
830 return Runtime::Current()->IsClearedJniWeakGlobal(weak_globals_.Get<kWithoutReadBarrier>(ref));
831}
832
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700833void JavaVMExt::UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result) {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700834 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700835 weak_globals_.Update(ref, result);
836}
837
Ian Rogers68d8b422014-07-17 11:09:10 -0700838void JavaVMExt::DumpReferenceTables(std::ostream& os) {
839 Thread* self = Thread::Current();
840 {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700841 ReaderMutexLock mu(self, *Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700842 globals_.Dump(os);
843 }
844 {
Andreas Gampe05a364c2016-10-14 13:27:12 -0700845 MutexLock mu(self, *Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700846 weak_globals_.Dump(os);
847 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700848}
849
Mathieu Chartier598302a2015-09-23 14:52:39 -0700850void JavaVMExt::UnloadNativeLibraries() {
851 libraries_.get()->UnloadNativeLibraries();
852}
853
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800854bool JavaVMExt::LoadNativeLibrary(JNIEnv* env,
855 const std::string& path,
856 jobject class_loader,
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800857 std::string* error_msg) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700858 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 Yamauchi04302db2015-11-11 23:45:34 -0800871 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 Chartier0795f232016-09-27 18:43:30 -0700876 ObjPtr<mirror::ClassLoader> loader = soa.Decode<mirror::ClassLoader>(class_loader);
Andreas Gampe2d48e532016-06-17 12:46:14 -0700877
878 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700879 if (class_linker->IsBootClassLoader(soa, loader.Ptr())) {
Andreas Gampe2d48e532016-06-17 12:46:14 -0700880 loader = nullptr;
881 class_loader = nullptr;
882 }
883
Mathieu Chartier1cc62e42016-10-03 18:01:28 -0700884 class_loader_allocator = class_linker->GetAllocatorForClassLoader(loader.Ptr());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800885 CHECK(class_loader_allocator != nullptr);
886 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700887 if (library != nullptr) {
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800888 // Use the allocator pointers for class loader equality to avoid unnecessary weak root decode.
889 if (library->GetClassLoaderAllocator() != class_loader_allocator) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700890 // 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 Gampefd03f1e2017-09-19 17:10:10 -0700893 //
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 Rogers68d8b422014-07-17 11:09:10 -0700922 StringAppendF(error_msg, "Shared library \"%s\" already opened by "
Andreas Gampefd03f1e2017-09-19 17:10:10 -0700923 "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 Gampe335ee582017-09-19 17:09:52 -0700929 LOG(WARNING) << *error_msg;
Ian Rogers68d8b422014-07-17 11:09:10 -0700930 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
Andreas Gampe473191c2017-12-28 16:55:31 -0800953 // Retrieve the library path from the classloader, if necessary.
954 ScopedLocalRef<jstring> library_path(env, GetLibrarySearchPath(env, class_loader));
955
Ian Rogers68d8b422014-07-17 11:09:10 -0700956 Locks::mutator_lock_->AssertNotHeld(self);
957 const char* path_str = path.empty() ? nullptr : path.c_str();
Zhenhua WANG8447e6d2016-05-30 11:10:29 +0800958 bool needs_native_bridge = false;
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800959 void* handle = android::OpenNativeLibrary(env,
960 runtime_->GetTargetSdkVersion(),
961 path_str,
962 class_loader,
Andreas Gampe473191c2017-12-28 16:55:31 -0800963 library_path.get(),
Zhenhua WANG8447e6d2016-05-30 11:10:29 +0800964 &needs_native_bridge,
965 error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -0700966
Dmitriy Ivanov53056722015-03-23 13:38:20 -0700967 VLOG(jni) << "[Call to dlopen(\"" << path << "\", RTLD_NOW) returned " << handle << "]";
Ian Rogers68d8b422014-07-17 11:09:10 -0700968
969 if (handle == nullptr) {
Dmitriy Ivanov53056722015-03-23 13:38:20 -0700970 VLOG(jni) << "dlopen(\"" << path << "\", RTLD_NOW) failed: " << *error_msg;
Ian Rogers68d8b422014-07-17 11:09:10 -0700971 return false;
972 }
973
974 if (env->ExceptionCheck() == JNI_TRUE) {
975 LOG(ERROR) << "Unexpected exception:";
976 env->ExceptionDescribe();
977 env->ExceptionClear();
978 }
979 // Create a new entry.
980 // TODO: move the locking (and more of this logic) into Libraries.
981 bool created_library = false;
982 {
983 // Create SharedLibrary ahead of taking the libraries lock to maintain lock ordering.
984 std::unique_ptr<SharedLibrary> new_library(
Zhenhua WANG8447e6d2016-05-30 11:10:29 +0800985 new SharedLibrary(env,
986 self,
987 path,
988 handle,
989 needs_native_bridge,
990 class_loader,
991 class_loader_allocator));
992
Ian Rogers68d8b422014-07-17 11:09:10 -0700993 MutexLock mu(self, *Locks::jni_libraries_lock_);
994 library = libraries_->Get(path);
995 if (library == nullptr) { // We won race to get libraries_lock.
996 library = new_library.release();
997 libraries_->Put(path, library);
998 created_library = true;
999 }
1000 }
1001 if (!created_library) {
1002 LOG(INFO) << "WOW: we lost a race to add shared library: "
1003 << "\"" << path << "\" ClassLoader=" << class_loader;
1004 return library->CheckOnLoadResult();
1005 }
1006 VLOG(jni) << "[Added shared library \"" << path << "\" for ClassLoader " << class_loader << "]";
1007
1008 bool was_successful = false;
Zhenhua WANG8447e6d2016-05-30 11:10:29 +08001009 void* sym = library->FindSymbol("JNI_OnLoad", nullptr);
Ian Rogers68d8b422014-07-17 11:09:10 -07001010 if (sym == nullptr) {
1011 VLOG(jni) << "[No JNI_OnLoad found in \"" << path << "\"]";
1012 was_successful = true;
1013 } else {
1014 // Call JNI_OnLoad. We have to override the current class
1015 // loader, which will always be "null" since the stuff at the
1016 // top of the stack is around Runtime.loadLibrary(). (See
1017 // the comments in the JNI FindClass function.)
1018 ScopedLocalRef<jobject> old_class_loader(env, env->NewLocalRef(self->GetClassLoaderOverride()));
1019 self->SetClassLoaderOverride(class_loader);
1020
1021 VLOG(jni) << "[Calling JNI_OnLoad in \"" << path << "\"]";
1022 typedef int (*JNI_OnLoadFn)(JavaVM*, void*);
1023 JNI_OnLoadFn jni_on_load = reinterpret_cast<JNI_OnLoadFn>(sym);
1024 int version = (*jni_on_load)(this, nullptr);
1025
Mathieu Chartierd0004802014-10-15 16:59:47 -07001026 if (runtime_->GetTargetSdkVersion() != 0 && runtime_->GetTargetSdkVersion() <= 21) {
Josh Gao85a78cf2017-03-20 16:26:42 -07001027 // Make sure that sigchain owns SIGSEGV.
1028 EnsureFrontOfChain(SIGSEGV);
Mathieu Chartierd0004802014-10-15 16:59:47 -07001029 }
1030
Ian Rogers68d8b422014-07-17 11:09:10 -07001031 self->SetClassLoaderOverride(old_class_loader.get());
1032
1033 if (version == JNI_ERR) {
1034 StringAppendF(error_msg, "JNI_ERR returned from JNI_OnLoad in \"%s\"", path.c_str());
Alex Light185d1342016-08-11 10:48:03 -07001035 } else if (JavaVMExt::IsBadJniVersion(version)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001036 StringAppendF(error_msg, "Bad JNI version returned from JNI_OnLoad in \"%s\": %d",
1037 path.c_str(), version);
1038 // It's unwise to call dlclose() here, but we can mark it
1039 // as bad and ensure that future load attempts will fail.
1040 // We don't know how far JNI_OnLoad got, so there could
1041 // be some partially-initialized stuff accessible through
1042 // newly-registered native method calls. We could try to
1043 // unregister them, but that doesn't seem worthwhile.
1044 } else {
1045 was_successful = true;
1046 }
1047 VLOG(jni) << "[Returned " << (was_successful ? "successfully" : "failure")
1048 << " from JNI_OnLoad in \"" << path << "\"]";
1049 }
1050
1051 library->SetResult(was_successful);
1052 return was_successful;
1053}
1054
Alex Light65af20b2017-04-20 09:15:08 -07001055static void* FindCodeForNativeMethodInAgents(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
1056 std::string jni_short_name(m->JniShortName());
1057 std::string jni_long_name(m->JniLongName());
Andreas Gampeaadcbc62017-12-28 14:05:42 -08001058 for (const std::unique_ptr<ti::Agent>& agent : Runtime::Current()->GetAgents()) {
1059 void* fn = agent->FindSymbol(jni_short_name);
Alex Light65af20b2017-04-20 09:15:08 -07001060 if (fn != nullptr) {
1061 VLOG(jni) << "Found implementation for " << m->PrettyMethod()
Andreas Gampeaadcbc62017-12-28 14:05:42 -08001062 << " (symbol: " << jni_short_name << ") in " << *agent;
Alex Light65af20b2017-04-20 09:15:08 -07001063 return fn;
1064 }
Andreas Gampeaadcbc62017-12-28 14:05:42 -08001065 fn = agent->FindSymbol(jni_long_name);
Alex Light65af20b2017-04-20 09:15:08 -07001066 if (fn != nullptr) {
1067 VLOG(jni) << "Found implementation for " << m->PrettyMethod()
Andreas Gampeaadcbc62017-12-28 14:05:42 -08001068 << " (symbol: " << jni_long_name << ") in " << *agent;
Alex Light65af20b2017-04-20 09:15:08 -07001069 return fn;
1070 }
1071 }
1072 return nullptr;
1073}
1074
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075void* JavaVMExt::FindCodeForNativeMethod(ArtMethod* m) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001076 CHECK(m->IsNative());
1077 mirror::Class* c = m->GetDeclaringClass();
1078 // If this is a static method, it could be called before the class has been initialized.
David Sehr709b0702016-10-13 09:12:37 -07001079 CHECK(c->IsInitializing()) << c->GetStatus() << " " << m->PrettyMethod();
Ian Rogers68d8b422014-07-17 11:09:10 -07001080 std::string detail;
Mathieu Chartier46e75d02017-06-02 16:03:39 -07001081 Thread* const self = Thread::Current();
1082 void* native_method = libraries_->FindNativeMethod(self, m, detail);
Alex Light65af20b2017-04-20 09:15:08 -07001083 if (native_method == nullptr) {
1084 // Lookup JNI native methods from native TI Agent libraries. See runtime/ti/agent.h for more
1085 // information. Agent libraries are searched for native methods after all jni libraries.
1086 native_method = FindCodeForNativeMethodInAgents(m);
1087 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001088 // Throwing can cause libraries_lock to be reacquired.
1089 if (native_method == nullptr) {
Alex Light65af20b2017-04-20 09:15:08 -07001090 LOG(ERROR) << detail;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +00001091 self->ThrowNewException("Ljava/lang/UnsatisfiedLinkError;", detail.c_str());
Ian Rogers68d8b422014-07-17 11:09:10 -07001092 }
1093 return native_method;
1094}
1095
Mathieu Chartier97509952015-07-13 14:35:43 -07001096void JavaVMExt::SweepJniWeakGlobals(IsMarkedVisitor* visitor) {
Andreas Gampe05a364c2016-10-14 13:27:12 -07001097 MutexLock mu(Thread::Current(), *Locks::jni_weak_globals_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001098 Runtime* const runtime = Runtime::Current();
1099 for (auto* entry : weak_globals_) {
1100 // Need to skip null here to distinguish between null entries and cleared weak ref entries.
1101 if (!entry->IsNull()) {
1102 // Since this is called by the GC, we don't need a read barrier.
1103 mirror::Object* obj = entry->Read<kWithoutReadBarrier>();
Mathieu Chartier97509952015-07-13 14:35:43 -07001104 mirror::Object* new_obj = visitor->IsMarked(obj);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001105 if (new_obj == nullptr) {
1106 new_obj = runtime->GetClearedJniWeakGlobal();
1107 }
1108 *entry = GcRoot<mirror::Object>(new_obj);
Hiroshi Yamauchi8a741172014-09-08 13:22:56 -07001109 }
Ian Rogers68d8b422014-07-17 11:09:10 -07001110 }
1111}
1112
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -08001113void JavaVMExt::TrimGlobals() {
Andreas Gampe05a364c2016-10-14 13:27:12 -07001114 WriterMutexLock mu(Thread::Current(), *Locks::jni_globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -08001115 globals_.Trim();
1116}
1117
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001118void JavaVMExt::VisitRoots(RootVisitor* visitor) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001119 Thread* self = Thread::Current();
Andreas Gampe05a364c2016-10-14 13:27:12 -07001120 ReaderMutexLock mu(self, *Locks::jni_globals_lock_);
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07001121 globals_.VisitRoots(visitor, RootInfo(kRootJNIGlobal));
Ian Rogers68d8b422014-07-17 11:09:10 -07001122 // The weak_globals table is visited by the GC itself (because it mutates the table).
1123}
1124
Andreas Gampe473191c2017-12-28 16:55:31 -08001125jstring JavaVMExt::GetLibrarySearchPath(JNIEnv* env, jobject class_loader) {
1126 if (class_loader == nullptr) {
1127 return nullptr;
1128 }
1129 if (!env->IsInstanceOf(class_loader, WellKnownClasses::dalvik_system_BaseDexClassLoader)) {
1130 return nullptr;
1131 }
1132 return reinterpret_cast<jstring>(env->CallObjectMethod(
1133 class_loader,
1134 WellKnownClasses::dalvik_system_BaseDexClassLoader_getLdLibraryPath));
1135}
1136
Ian Rogers68d8b422014-07-17 11:09:10 -07001137// JNI Invocation interface.
1138
1139extern "C" jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) {
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -08001140 ScopedTrace trace(__FUNCTION__);
Ian Rogers68d8b422014-07-17 11:09:10 -07001141 const JavaVMInitArgs* args = static_cast<JavaVMInitArgs*>(vm_args);
Alex Light185d1342016-08-11 10:48:03 -07001142 if (JavaVMExt::IsBadJniVersion(args->version)) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001143 LOG(ERROR) << "Bad JNI version passed to CreateJavaVM: " << args->version;
1144 return JNI_EVERSION;
1145 }
1146 RuntimeOptions options;
1147 for (int i = 0; i < args->nOptions; ++i) {
1148 JavaVMOption* option = &args->options[i];
1149 options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
1150 }
1151 bool ignore_unrecognized = args->ignoreUnrecognized;
1152 if (!Runtime::Create(options, ignore_unrecognized)) {
1153 return JNI_ERR;
1154 }
Dimitry Ivanovc544f342016-05-09 16:26:13 -07001155
1156 // Initialize native loader. This step makes sure we have
1157 // everything set up before we start using JNI.
1158 android::InitializeNativeLoader();
1159
Ian Rogers68d8b422014-07-17 11:09:10 -07001160 Runtime* runtime = Runtime::Current();
1161 bool started = runtime->Start();
1162 if (!started) {
1163 delete Thread::Current()->GetJniEnv();
1164 delete runtime->GetJavaVM();
1165 LOG(WARNING) << "CreateJavaVM failed";
1166 return JNI_ERR;
1167 }
Dimitry Ivanov041169f2016-04-21 16:01:24 -07001168
Ian Rogers68d8b422014-07-17 11:09:10 -07001169 *p_env = Thread::Current()->GetJniEnv();
1170 *p_vm = runtime->GetJavaVM();
1171 return JNI_OK;
1172}
1173
Ian Rogersf4d4da12014-11-11 16:10:33 -08001174extern "C" jint JNI_GetCreatedJavaVMs(JavaVM** vms_buf, jsize buf_len, jsize* vm_count) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001175 Runtime* runtime = Runtime::Current();
Ian Rogersf4d4da12014-11-11 16:10:33 -08001176 if (runtime == nullptr || buf_len == 0) {
Ian Rogers68d8b422014-07-17 11:09:10 -07001177 *vm_count = 0;
1178 } else {
1179 *vm_count = 1;
Ian Rogersf4d4da12014-11-11 16:10:33 -08001180 vms_buf[0] = runtime->GetJavaVM();
Ian Rogers68d8b422014-07-17 11:09:10 -07001181 }
1182 return JNI_OK;
1183}
1184
1185// Historically unsupported.
1186extern "C" jint JNI_GetDefaultJavaVMInitArgs(void* /*vm_args*/) {
1187 return JNI_ERR;
1188}
1189
1190} // namespace art