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

Commit d047c925 authored by Dimitry Ivanov's avatar Dimitry Ivanov
Browse files

Added function to explicitly initialize a namespace

This change replaces lazy get-or-create logic for
linker namespaces with the explicit one.

ApplicationLoaders.getClassLoader(..) is now resposible for
the namespace initialization for PathClassLoaders.

Bug: http://b/27189432
Bug: http://b/22548808
Change-Id: Ife987c3ca1db33a47c20f363a5ed61512be8b5a7
parent b046e74b
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -29,10 +29,20 @@ __attribute__((visibility("default")))
void PreloadPublicNativeLibraries();

__attribute__((visibility("default")))
void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
                        jobject class_loader, bool is_shared, jstring library_path,
jstring CreateClassLoaderNamespace(JNIEnv* env,
                                   int32_t target_sdk_version,
                                   jobject class_loader,
                                   bool is_shared,
                                   jstring library_path,
                                   jstring permitted_path);

__attribute__((visibility("default")))
void* OpenNativeLibrary(JNIEnv* env,
                        int32_t target_sdk_version,
                        const char* path,
                        jobject class_loader,
                        jstring library_path);

#if defined(__ANDROID__)
// Look up linker namespace by class_loader. Returns nullptr if
// there is no namespace associated with the class_loader.
+56 −19
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#ifdef __ANDROID__
#include <android/dlext.h>
#include "cutils/properties.h"
#include "log/log.h"
#endif

#include <algorithm>
@@ -61,7 +62,8 @@ class LibraryNamespaces {
 public:
  LibraryNamespaces() : initialized_(false) { }

  android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader,
  android_namespace_t* Create(JNIEnv* env,
                              jobject class_loader,
                              bool is_shared,
                              jstring java_library_path,
                              jstring java_permitted_path,
@@ -82,9 +84,7 @@ class LibraryNamespaces {

    android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader);

    if (ns != nullptr) {
      return ns;
    }
    LOG_FATAL_IF(ns != nullptr, "There is already a namespace associated with this classloader");

    uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
    if (is_shared) {
@@ -99,7 +99,9 @@ class LibraryNamespaces {
                                      permitted_path.c_str() :
                                      nullptr);

    if (ns != nullptr) {
      namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
    }

    return ns;
  }
@@ -147,6 +149,10 @@ class LibraryNamespaces {
};

static LibraryNamespaces* g_namespaces = new LibraryNamespaces;

static bool namespaces_enabled(uint32_t target_sdk_version) {
  return target_sdk_version > 0;
}
#endif

void PreloadPublicNativeLibraries() {
@@ -156,21 +162,53 @@ void PreloadPublicNativeLibraries() {
}


void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
                        jobject class_loader, bool is_shared, jstring java_library_path,
                        jstring java_permitted_path) {
jstring CreateClassLoaderNamespace(JNIEnv* env,
                                   int32_t target_sdk_version,
                                   jobject class_loader,
                                   bool is_shared,
                                   jstring library_path,
                                   jstring permitted_path) {
#if defined(__ANDROID__)
  if (target_sdk_version == 0 || class_loader == nullptr) {
  if (!namespaces_enabled(target_sdk_version)) {
    return nullptr;
  }

  android_namespace_t* ns = g_namespaces->Create(env,
                                                 class_loader,
                                                 is_shared,
                                                 library_path,
                                                 permitted_path,
                                                 target_sdk_version);
  if (ns == nullptr) {
    return env->NewStringUTF(dlerror());
  }
#else
  UNUSED(env, target_sdk_version, class_loader, is_shared,
         library_path, permitted_path);
#endif
  return nullptr;
}

void* OpenNativeLibrary(JNIEnv* env,
                        int32_t target_sdk_version,
                        const char* path,
                        jobject class_loader,
                        jstring library_path) {
#if defined(__ANDROID__)
  if (!namespaces_enabled(target_sdk_version) || class_loader == nullptr) {
    return dlopen(path, RTLD_NOW);
  }

  android_namespace_t* ns =
      g_namespaces->GetOrCreate(env, class_loader, is_shared,
                                java_library_path, java_permitted_path, target_sdk_version);
  android_namespace_t* ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader);

  if (ns == nullptr) {
    // This is the case where the classloader was not created by ApplicationLoaders
    // In this case we create an isolated not-shared namespace for it.
    ns = g_namespaces->Create(env, class_loader, false, library_path, nullptr, target_sdk_version);
    if (ns == nullptr) {
      return nullptr;
    }
  }

  android_dlextinfo extinfo;
  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
@@ -178,8 +216,7 @@ void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* pat

  return android_dlopen_ext(path, RTLD_NOW, &extinfo);
#else
  UNUSED(env, target_sdk_version, class_loader, is_shared,
         java_library_path, java_permitted_path);
  UNUSED(env, target_sdk_version, class_loader, library_path);
  return dlopen(path, RTLD_NOW);
#endif
}