Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 34d5a20c authored by Dimitry Ivanov's avatar Dimitry Ivanov
Browse files

Fix locking of libnativeloader

This commit fixes race condition introduced in
d047c925

Bug: http://b/27189432
Bug: http://b/22548808
Change-Id: I5d94f130937f18d3443878b3521715a8f87427e0
parent 99fb01e4
Loading
Loading
Loading
Loading
+5 −3
Original line number Original line Diff line number Diff line
@@ -80,8 +80,6 @@ class LibraryNamespaces {
      return nullptr;
      return nullptr;
    }
    }


    std::lock_guard<std::mutex> guard(mutex_);

    android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);
    android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);


    LOG_FATAL_IF(ns != nullptr, "There is already a namespace associated with this classloader");
    LOG_FATAL_IF(ns != nullptr, "There is already a namespace associated with this classloader");
@@ -142,12 +140,12 @@ class LibraryNamespaces {
  }
  }


  bool initialized_;
  bool initialized_;
  std::mutex mutex_;
  std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
  std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;


  DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
  DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
};
};


static std::mutex g_namespaces_mutex;
static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
static LibraryNamespaces* g_namespaces = new LibraryNamespaces;


static bool namespaces_enabled(uint32_t target_sdk_version) {
static bool namespaces_enabled(uint32_t target_sdk_version) {
@@ -157,6 +155,7 @@ static bool namespaces_enabled(uint32_t target_sdk_version) {


void PreloadPublicNativeLibraries() {
void PreloadPublicNativeLibraries() {
#if defined(__ANDROID__)
#if defined(__ANDROID__)
  std::lock_guard<std::mutex> guard(g_namespaces_mutex);
  g_namespaces->PreloadPublicLibraries();
  g_namespaces->PreloadPublicLibraries();
#endif
#endif
}
}
@@ -173,6 +172,7 @@ jstring CreateClassLoaderNamespace(JNIEnv* env,
    return nullptr;
    return nullptr;
  }
  }


  std::lock_guard<std::mutex> guard(g_namespaces_mutex);
  android_namespace_t* ns = g_namespaces->Create(env,
  android_namespace_t* ns = g_namespaces->Create(env,
                                                 class_loader,
                                                 class_loader,
                                                 is_shared,
                                                 is_shared,
@@ -199,6 +199,7 @@ void* OpenNativeLibrary(JNIEnv* env,
    return dlopen(path, RTLD_NOW);
    return dlopen(path, RTLD_NOW);
  }
  }


  std::lock_guard<std::mutex> guard(g_namespaces_mutex);
  android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);
  android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);


  if (ns == nullptr) {
  if (ns == nullptr) {
@@ -223,6 +224,7 @@ void* OpenNativeLibrary(JNIEnv* env,


#if defined(__ANDROID__)
#if defined(__ANDROID__)
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
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);
  return g_namespaces->FindNamespaceByClassLoader(env, class_loader);
}
}
#endif
#endif