summaryrefslogtreecommitdiff
path: root/libnativeloader/native_loader.cpp
diff options
context:
space:
mode:
authorDimitry Ivanov <dimitry@google.com>2016-02-29 13:21:43 -0800
committerDimitry Ivanov <dimitry@google.com>2016-02-29 13:31:26 -0800
commit1dff1aa97c4ee3ea1436e95fea2fb20c7e8cbead (patch)
tree7ef95ff719bff398b4580ff24c4c1787a88b0744 /libnativeloader/native_loader.cpp
parent4cd07681158830ec49f763f746d6644099a73b42 (diff)
Fix locking of libnativeloader
This commit fixes race condition introduced in d047c925af62e1fe28fcd1c1940df4afe18d458a Bug: http://b/27189432 Bug: http://b/22548808 Change-Id: I5d94f130937f18d3443878b3521715a8f87427e0 (cherry picked from commit 34d5a20c8bb57adae8711c7f9d90a77fbd4043c7)
Diffstat (limited to 'libnativeloader/native_loader.cpp')
-rw-r--r--libnativeloader/native_loader.cpp8
1 files changed, 5 insertions, 3 deletions
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index 9a1f98f86..86d9d77f7 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -77,8 +77,6 @@ class LibraryNamespaces {
return nullptr;
}
- std::lock_guard<std::mutex> guard(mutex_);
-
android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
LOG_FATAL_IF(ns != nullptr, "There is already a namespace associated with this classloader");
@@ -130,12 +128,12 @@ class LibraryNamespaces {
}
bool initialized_;
- std::mutex mutex_;
std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
};
+static std::mutex g_namespaces_mutex;
static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
static bool namespaces_enabled(uint32_t target_sdk_version) {
@@ -145,6 +143,7 @@ static bool namespaces_enabled(uint32_t target_sdk_version) {
void PreloadPublicNativeLibraries() {
#if defined(__ANDROID__)
+ std::lock_guard<std::mutex> guard(g_namespaces_mutex);
g_namespaces->PreloadPublicLibraries();
#endif
}
@@ -161,6 +160,7 @@ jstring CreateClassLoaderNamespace(JNIEnv* env,
return nullptr;
}
+ std::lock_guard<std::mutex> guard(g_namespaces_mutex);
android_namespace_t* ns = g_namespaces->Create(env,
class_loader,
is_shared,
@@ -186,6 +186,7 @@ void* OpenNativeLibrary(JNIEnv* env,
return dlopen(path, RTLD_NOW);
}
+ std::lock_guard<std::mutex> guard(g_namespaces_mutex);
android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
if (ns == nullptr) {
@@ -210,6 +211,7 @@ void* OpenNativeLibrary(JNIEnv* env,
#if defined(__ANDROID__)
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
+ std::lock_guard<std::mutex> guard(g_namespaces_mutex);
return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
}
#endif