From a15b12742d71cc87578e671098b3da4ae30c714d Mon Sep 17 00:00:00 2001 From: Mathieu Chartier Date: Tue, 10 May 2016 10:12:00 -0700 Subject: [PATCH] Revert "Revert "Revert "Revert "(dl)Close native libraries on unload"""" This reverts commit 624b68ecbe67dd91cb50f588fbfa2243def47f1e. Bug: 28406866 (cherry picked from commit 33bae7d52debe898879fc034a5524b1b165dbd88) Change-Id: I4d6d6791b286cfeecaeebc388684ee8d3c44a920 --- runtime/java_vm_ext.cc | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/runtime/java_vm_ext.cc b/runtime/java_vm_ext.cc index d983a9fa1..c2164126e 100644 --- a/runtime/java_vm_ext.cc +++ b/runtime/java_vm_ext.cc @@ -74,6 +74,10 @@ class SharedLibrary { if (self != nullptr) { self->GetJniEnv()->DeleteWeakGlobalRef(class_loader_); } + + if (!needs_native_bridge_) { + android::CloseNativeLibrary(handle_); + } } jweak GetClassLoader() const { @@ -271,8 +275,7 @@ class Libraries { REQUIRES(!Locks::jni_libraries_lock_) SHARED_REQUIRES(Locks::mutator_lock_) { ScopedObjectAccessUnchecked soa(Thread::Current()); - typedef void (*JNI_OnUnloadFn)(JavaVM*, void*); - std::vector unload_functions; + std::vector unload_libraries; { MutexLock mu(soa.Self(), *Locks::jni_libraries_lock_); for (auto it = libraries_.begin(); it != libraries_.end(); ) { @@ -283,15 +286,7 @@ class Libraries { // the native libraries of the boot class loader. if (class_loader != nullptr && soa.Self()->IsJWeakCleared(class_loader)) { - void* const sym = library->FindSymbol("JNI_OnUnload", nullptr); - if (sym == nullptr) { - VLOG(jni) << "[No JNI_OnUnload found in \"" << library->GetPath() << "\"]"; - } else { - VLOG(jni) << "[JNI_OnUnload found for \"" << library->GetPath() << "\"]"; - JNI_OnUnloadFn jni_on_unload = reinterpret_cast(sym); - unload_functions.push_back(jni_on_unload); - } - delete library; + unload_libraries.push_back(library); it = libraries_.erase(it); } else { ++it; @@ -299,9 +294,17 @@ class Libraries { } } // Do this without holding the jni libraries lock to prevent possible deadlocks. - for (JNI_OnUnloadFn fn : unload_functions) { - VLOG(jni) << "Calling JNI_OnUnload"; - (*fn)(soa.Vm(), nullptr); + typedef void (*JNI_OnUnloadFn)(JavaVM*, void*); + for (auto library : unload_libraries) { + void* const sym = library->FindSymbol("JNI_OnUnload", nullptr); + if (sym == nullptr) { + VLOG(jni) << "[No JNI_OnUnload found in \"" << library->GetPath() << "\"]"; + } else { + VLOG(jni) << "[JNI_OnUnload found for \"" << library->GetPath() << "\"]: Calling..."; + JNI_OnUnloadFn jni_on_unload = reinterpret_cast(sym); + jni_on_unload(soa.Vm(), nullptr); + } + delete library; } } -- 2.11.0