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