2015-12-01 22:56:44 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nativeloader/native_loader.h"
|
2017-07-19 01:53:54 +02:00
|
|
|
#include <nativehelper/ScopedUtfChars.h>
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#ifdef __ANDROID__
|
2016-03-22 21:59:59 +01:00
|
|
|
#define LOG_TAG "libnativeloader"
|
2017-01-10 01:04:28 +01:00
|
|
|
#include "nativeloader/dlext_namespaces.h"
|
2016-09-29 00:54:45 +02:00
|
|
|
#include "cutils/properties.h"
|
2017-01-10 22:19:54 +01:00
|
|
|
#include "log/log.h"
|
2015-12-01 22:56:44 +01:00
|
|
|
#endif
|
2017-11-08 10:44:09 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
2016-05-30 05:16:08 +02:00
|
|
|
#include "nativebridge/native_bridge.h"
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2017-11-08 10:44:09 +01:00
|
|
|
#include <memory>
|
2015-12-01 22:56:44 +01:00
|
|
|
#include <mutex>
|
2017-11-08 10:44:09 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-09-29 00:54:45 +02:00
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/macros.h>
|
|
|
|
#include <android-base/strings.h>
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2017-11-27 09:04:14 +01:00
|
|
|
#ifdef __BIONIC__
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
#define CHECK(predicate) LOG_ALWAYS_FATAL_IF(!(predicate),\
|
|
|
|
"%s:%d: %s CHECK '" #predicate "' failed.",\
|
|
|
|
__FILE__, __LINE__, __FUNCTION__)
|
|
|
|
|
2015-12-01 22:56:44 +01:00
|
|
|
namespace android {
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-05-30 05:16:08 +02:00
|
|
|
class NativeLoaderNamespace {
|
|
|
|
public:
|
|
|
|
NativeLoaderNamespace()
|
|
|
|
: android_ns_(nullptr), native_bridge_ns_(nullptr) { }
|
|
|
|
|
|
|
|
explicit NativeLoaderNamespace(android_namespace_t* ns)
|
|
|
|
: android_ns_(ns), native_bridge_ns_(nullptr) { }
|
|
|
|
|
|
|
|
explicit NativeLoaderNamespace(native_bridge_namespace_t* ns)
|
|
|
|
: android_ns_(nullptr), native_bridge_ns_(ns) { }
|
|
|
|
|
|
|
|
NativeLoaderNamespace(NativeLoaderNamespace&& that) = default;
|
|
|
|
NativeLoaderNamespace(const NativeLoaderNamespace& that) = default;
|
|
|
|
|
|
|
|
NativeLoaderNamespace& operator=(const NativeLoaderNamespace& that) = default;
|
|
|
|
|
|
|
|
android_namespace_t* get_android_ns() const {
|
|
|
|
CHECK(native_bridge_ns_ == nullptr);
|
|
|
|
return android_ns_;
|
|
|
|
}
|
|
|
|
|
|
|
|
native_bridge_namespace_t* get_native_bridge_ns() const {
|
|
|
|
CHECK(android_ns_ == nullptr);
|
|
|
|
return native_bridge_ns_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_android_namespace() const {
|
|
|
|
return native_bridge_ns_ == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Only one of them can be not null
|
|
|
|
android_namespace_t* android_ns_;
|
|
|
|
native_bridge_namespace_t* native_bridge_ns_;
|
|
|
|
};
|
|
|
|
|
2017-11-08 10:44:09 +01:00
|
|
|
static constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] =
|
|
|
|
"/etc/public.libraries.txt";
|
|
|
|
static constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-";
|
|
|
|
static constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen =
|
|
|
|
sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1;
|
|
|
|
static constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt";
|
|
|
|
static constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen =
|
|
|
|
sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1;
|
|
|
|
static constexpr const char kPublicNativeLibrariesVendorConfig[] =
|
|
|
|
"/vendor/etc/public.libraries.txt";
|
|
|
|
static constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] =
|
|
|
|
"/etc/llndk.libraries.txt";
|
|
|
|
static constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] =
|
|
|
|
"/etc/vndksp.libraries.txt";
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
// The device may be configured to have the vendor libraries loaded to a separate namespace.
|
|
|
|
// For historical reasons this namespace was named sphal but effectively it is intended
|
|
|
|
// to use to load vendor libraries to separate namespace with controlled interface between
|
|
|
|
// vendor and system namespaces.
|
|
|
|
static constexpr const char* kVendorNamespaceName = "sphal";
|
|
|
|
|
2017-08-22 03:26:10 +02:00
|
|
|
static constexpr const char* kVndkNamespaceName = "vndk";
|
|
|
|
|
|
|
|
static constexpr const char* kClassloaderNamespaceName = "classloader-namespace";
|
|
|
|
static constexpr const char* kVendorClassloaderNamespaceName = "vendor-classloader-namespace";
|
|
|
|
|
2016-05-10 19:39:48 +02:00
|
|
|
// (http://b/27588281) This is a workaround for apps using custom classloaders and calling
|
|
|
|
// System.load() with an absolute path which is outside of the classloader library search path.
|
|
|
|
// This list includes all directories app is allowed to access this way.
|
|
|
|
static constexpr const char* kWhitelistedDirectories = "/data:/mnt/expand";
|
|
|
|
|
2016-05-06 02:30:24 +02:00
|
|
|
static bool is_debuggable() {
|
|
|
|
char debuggable[PROP_VALUE_MAX];
|
|
|
|
property_get("ro.debuggable", debuggable, "0");
|
|
|
|
return std::string(debuggable) == "1";
|
|
|
|
}
|
|
|
|
|
2017-11-27 09:04:14 +01:00
|
|
|
static std::string vndk_version_str() {
|
|
|
|
#ifdef __BIONIC__
|
|
|
|
std::string version = android::base::GetProperty("ro.vndk.version", "");
|
|
|
|
if (version != "" && version != "current") {
|
|
|
|
return "." + version;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void insert_vndk_version_str(std::string* file_name) {
|
|
|
|
CHECK(file_name != nullptr);
|
|
|
|
size_t insert_pos = file_name->find_last_of(".");
|
|
|
|
if (insert_pos == std::string::npos) {
|
|
|
|
insert_pos = file_name->length();
|
|
|
|
}
|
|
|
|
file_name->insert(insert_pos, vndk_version_str());
|
|
|
|
}
|
|
|
|
|
2017-11-08 10:44:09 +01:00
|
|
|
static const std::function<bool(const std::string&, std::string*)> always_true =
|
|
|
|
[](const std::string&, std::string*) { return true; };
|
|
|
|
|
2015-12-01 22:56:44 +01:00
|
|
|
class LibraryNamespaces {
|
|
|
|
public:
|
2016-02-22 20:27:48 +01:00
|
|
|
LibraryNamespaces() : initialized_(false) { }
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool Create(JNIEnv* env,
|
2017-05-09 07:24:24 +02:00
|
|
|
uint32_t target_sdk_version,
|
2016-05-30 05:16:08 +02:00
|
|
|
jobject class_loader,
|
|
|
|
bool is_shared,
|
2017-08-22 03:26:10 +02:00
|
|
|
bool is_for_vendor,
|
2016-05-30 05:16:08 +02:00
|
|
|
jstring java_library_path,
|
|
|
|
jstring java_permitted_path,
|
|
|
|
NativeLoaderNamespace* ns,
|
|
|
|
std::string* error_msg) {
|
2016-05-09 19:55:50 +02:00
|
|
|
std::string library_path; // empty string by default.
|
|
|
|
|
|
|
|
if (java_library_path != nullptr) {
|
|
|
|
ScopedUtfChars library_path_utf_chars(env, java_library_path);
|
|
|
|
library_path = library_path_utf_chars.c_str();
|
|
|
|
}
|
2015-12-08 20:16:56 +01:00
|
|
|
|
2016-05-10 19:39:48 +02:00
|
|
|
// (http://b/27588281) This is a workaround for apps using custom
|
|
|
|
// classloaders and calling System.load() with an absolute path which
|
|
|
|
// is outside of the classloader library search path.
|
|
|
|
//
|
|
|
|
// This part effectively allows such a classloader to access anything
|
|
|
|
// under /data and /mnt/expand
|
|
|
|
std::string permitted_path = kWhitelistedDirectories;
|
|
|
|
|
2015-12-08 20:16:56 +01:00
|
|
|
if (java_permitted_path != nullptr) {
|
|
|
|
ScopedUtfChars path(env, java_permitted_path);
|
2016-05-11 01:21:25 +02:00
|
|
|
if (path.c_str() != nullptr && path.size() > 0) {
|
|
|
|
permitted_path = permitted_path + ":" + path.c_str();
|
|
|
|
}
|
2015-12-08 20:16:56 +01:00
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
if (!initialized_ && !InitPublicNamespace(library_path.c_str(), error_msg)) {
|
|
|
|
return false;
|
2015-12-01 22:56:44 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool found = FindNamespaceByClassLoader(env, class_loader, nullptr);
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
LOG_ALWAYS_FATAL_IF(found,
|
2016-03-15 21:51:26 +01:00
|
|
|
"There is already a namespace associated with this classloader");
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2015-12-15 20:06:57 +01:00
|
|
|
uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
|
|
|
|
if (is_shared) {
|
|
|
|
namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
|
|
|
|
}
|
|
|
|
|
2017-05-09 07:24:24 +02:00
|
|
|
if (target_sdk_version < 24) {
|
|
|
|
namespace_type |= ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED;
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
NativeLoaderNamespace parent_ns;
|
|
|
|
bool found_parent_namespace = FindParentNamespaceByClassLoader(env, class_loader, &parent_ns);
|
2016-05-13 00:34:41 +02:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool is_native_bridge = false;
|
|
|
|
|
|
|
|
if (found_parent_namespace) {
|
|
|
|
is_native_bridge = !parent_ns.is_android_namespace();
|
|
|
|
} else if (!library_path.empty()) {
|
|
|
|
is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
|
|
|
|
}
|
|
|
|
|
2017-08-22 03:26:10 +02:00
|
|
|
std::string system_exposed_libraries = system_public_libraries_;
|
|
|
|
const char* namespace_name = kClassloaderNamespaceName;
|
|
|
|
android_namespace_t* vndk_ns = nullptr;
|
|
|
|
if (is_for_vendor && !is_shared) {
|
|
|
|
LOG_FATAL_IF(is_native_bridge, "Unbundled vendor apk must not use translated architecture");
|
|
|
|
|
|
|
|
// For vendor apks, give access to the vendor lib even though
|
|
|
|
// they are treated as unbundled; the libs and apks are still bundled
|
|
|
|
// together in the vendor partition.
|
|
|
|
#if defined(__LP64__)
|
|
|
|
std::string vendor_lib_path = "/vendor/lib64";
|
|
|
|
#else
|
|
|
|
std::string vendor_lib_path = "/vendor/lib";
|
|
|
|
#endif
|
|
|
|
library_path = library_path + ":" + vendor_lib_path.c_str();
|
|
|
|
permitted_path = permitted_path + ":" + vendor_lib_path.c_str();
|
|
|
|
|
|
|
|
// Also give access to LLNDK libraries since they are available to vendors
|
|
|
|
system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries_.c_str();
|
|
|
|
|
|
|
|
// Give access to VNDK-SP libraries from the 'vndk' namespace.
|
|
|
|
vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
|
|
|
|
LOG_ALWAYS_FATAL_IF(vndk_ns == nullptr,
|
|
|
|
"Cannot find \"%s\" namespace for vendor apks", kVndkNamespaceName);
|
|
|
|
|
|
|
|
// Different name is useful for debugging
|
|
|
|
namespace_name = kVendorClassloaderNamespaceName;
|
|
|
|
ALOGD("classloader namespace configured for unbundled vendor apk. library_path=%s", library_path.c_str());
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
NativeLoaderNamespace native_loader_ns;
|
|
|
|
if (!is_native_bridge) {
|
2017-08-22 03:26:10 +02:00
|
|
|
android_namespace_t* ns = android_create_namespace(namespace_name,
|
2016-05-30 05:16:08 +02:00
|
|
|
nullptr,
|
|
|
|
library_path.c_str(),
|
|
|
|
namespace_type,
|
|
|
|
permitted_path.c_str(),
|
|
|
|
parent_ns.get_android_ns());
|
|
|
|
if (ns == nullptr) {
|
|
|
|
*error_msg = dlerror();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
// Note that when vendor_ns is not configured this function will return nullptr
|
|
|
|
// and it will result in linking vendor_public_libraries_ to the default namespace
|
|
|
|
// which is expected behavior in this case.
|
|
|
|
android_namespace_t* vendor_ns = android_get_exported_namespace(kVendorNamespaceName);
|
|
|
|
|
2017-08-22 03:26:10 +02:00
|
|
|
if (!android_link_namespaces(ns, nullptr, system_exposed_libraries.c_str())) {
|
2017-02-03 23:11:27 +01:00
|
|
|
*error_msg = dlerror();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-22 03:26:10 +02:00
|
|
|
if (vndk_ns != nullptr && !system_vndksp_libraries_.empty()) {
|
|
|
|
// vendor apks are allowed to use VNDK-SP libraries.
|
|
|
|
if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries_.c_str())) {
|
|
|
|
*error_msg = dlerror();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
if (!vendor_public_libraries_.empty()) {
|
|
|
|
if (!android_link_namespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
|
|
|
|
*error_msg = dlerror();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
native_loader_ns = NativeLoaderNamespace(ns);
|
|
|
|
} else {
|
2017-08-22 03:26:10 +02:00
|
|
|
native_bridge_namespace_t* ns = NativeBridgeCreateNamespace(namespace_name,
|
2016-05-30 05:16:08 +02:00
|
|
|
nullptr,
|
|
|
|
library_path.c_str(),
|
|
|
|
namespace_type,
|
|
|
|
permitted_path.c_str(),
|
|
|
|
parent_ns.get_native_bridge_ns());
|
2017-05-02 00:12:49 +02:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
if (ns == nullptr) {
|
|
|
|
*error_msg = NativeBridgeGetError();
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
native_bridge_namespace_t* vendor_ns = NativeBridgeGetVendorNamespace();
|
|
|
|
|
2017-08-22 03:26:10 +02:00
|
|
|
if (!NativeBridgeLinkNamespaces(ns, nullptr, system_exposed_libraries.c_str())) {
|
2017-02-27 03:14:45 +01:00
|
|
|
*error_msg = NativeBridgeGetError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
if (!vendor_public_libraries_.empty()) {
|
|
|
|
if (!NativeBridgeLinkNamespaces(ns, vendor_ns, vendor_public_libraries_.c_str())) {
|
|
|
|
*error_msg = NativeBridgeGetError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
native_loader_ns = NativeLoaderNamespace(ns);
|
2016-02-23 23:23:51 +01:00
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), native_loader_ns));
|
|
|
|
|
|
|
|
*ns = native_loader_ns;
|
|
|
|
return true;
|
2015-12-01 22:56:44 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader, NativeLoaderNamespace* ns) {
|
2016-02-22 22:48:22 +01:00
|
|
|
auto it = std::find_if(namespaces_.begin(), namespaces_.end(),
|
2016-05-30 05:16:08 +02:00
|
|
|
[&](const std::pair<jweak, NativeLoaderNamespace>& value) {
|
2016-02-22 22:48:22 +01:00
|
|
|
return env->IsSameObject(value.first, class_loader);
|
|
|
|
});
|
2016-05-30 05:16:08 +02:00
|
|
|
if (it != namespaces_.end()) {
|
|
|
|
if (ns != nullptr) {
|
|
|
|
*ns = it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-02-22 22:48:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
void Initialize() {
|
2016-05-10 03:12:00 +02:00
|
|
|
// Once public namespace is initialized there is no
|
|
|
|
// point in running this code - it will have no effect
|
|
|
|
// on the current list of public libraries.
|
|
|
|
if (initialized_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
std::vector<std::string> sonames;
|
2016-04-22 01:42:48 +02:00
|
|
|
const char* android_root_env = getenv("ANDROID_ROOT");
|
|
|
|
std::string root_dir = android_root_env != nullptr ? android_root_env : "/system";
|
|
|
|
std::string public_native_libraries_system_config =
|
|
|
|
root_dir + kPublicNativeLibrariesSystemConfigPathFromRoot;
|
2017-08-22 03:26:10 +02:00
|
|
|
std::string llndk_native_libraries_system_config =
|
|
|
|
root_dir + kLlndkNativeLibrariesSystemConfigPathFromRoot;
|
|
|
|
std::string vndksp_native_libraries_system_config =
|
|
|
|
root_dir + kVndkspNativeLibrariesSystemConfigPathFromRoot;
|
2016-04-07 03:24:08 +02:00
|
|
|
|
2016-06-22 01:11:23 +02:00
|
|
|
std::string error_msg;
|
2017-11-08 10:44:09 +01:00
|
|
|
LOG_ALWAYS_FATAL_IF(
|
|
|
|
!ReadConfig(public_native_libraries_system_config, &sonames, always_true, &error_msg),
|
|
|
|
"Error reading public native library list from \"%s\": %s",
|
|
|
|
public_native_libraries_system_config.c_str(), error_msg.c_str());
|
|
|
|
|
|
|
|
// read /system/etc/public.libraries-<companyname>.txt which contain partner defined
|
|
|
|
// system libs that are exposed to apps. The libs in the txt files must be
|
|
|
|
// named as lib<name>.<companyname>.so.
|
|
|
|
std::string dirname = base::Dirname(public_native_libraries_system_config);
|
|
|
|
std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname.c_str()), closedir);
|
|
|
|
if (dir != nullptr) {
|
|
|
|
// Failing to opening the dir is not an error, which can happen in
|
|
|
|
// webview_zygote.
|
|
|
|
struct dirent* ent;
|
|
|
|
while ((ent = readdir(dir.get())) != nullptr) {
|
|
|
|
if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const std::string filename(ent->d_name);
|
|
|
|
if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
|
|
|
|
android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
|
|
|
|
const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
|
|
|
|
const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
|
|
|
|
const std::string company_name = filename.substr(start, end - start);
|
|
|
|
const std::string config_file_path = dirname + "/" + filename;
|
|
|
|
LOG_ALWAYS_FATAL_IF(
|
|
|
|
company_name.empty(),
|
|
|
|
"Error extracting company name from public native library list file path \"%s\"",
|
|
|
|
config_file_path.c_str());
|
|
|
|
LOG_ALWAYS_FATAL_IF(
|
|
|
|
!ReadConfig(
|
|
|
|
config_file_path, &sonames,
|
|
|
|
[&company_name](const std::string& soname, std::string* error_msg) {
|
|
|
|
if (android::base::StartsWith(soname, "lib") &&
|
|
|
|
android::base::EndsWith(soname, ("." + company_name + ".so").c_str())) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
*error_msg = "Library name \"" + soname +
|
|
|
|
"\" does not end with the company name: " + company_name + ".";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
&error_msg),
|
|
|
|
"Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
|
|
|
|
error_msg.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-06 02:30:24 +02:00
|
|
|
|
2017-11-27 09:04:14 +01:00
|
|
|
// Insert VNDK version to llndk and vndksp config file names.
|
|
|
|
insert_vndk_version_str(&llndk_native_libraries_system_config);
|
|
|
|
insert_vndk_version_str(&vndksp_native_libraries_system_config);
|
|
|
|
|
2016-05-06 02:30:24 +02:00
|
|
|
// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
|
|
|
|
// variable to add libraries to the list. This is intended for platform tests only.
|
|
|
|
if (is_debuggable()) {
|
|
|
|
const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
|
|
|
|
if (additional_libs != nullptr && additional_libs[0] != '\0') {
|
|
|
|
std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
|
|
|
|
std::copy(additional_libs_vector.begin(),
|
|
|
|
additional_libs_vector.end(),
|
|
|
|
std::back_inserter(sonames));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
// android_init_namespaces() expects all the public libraries
|
|
|
|
// to be loaded so that they can be found by soname alone.
|
|
|
|
//
|
|
|
|
// TODO(dimitry): this is a bit misleading since we do not know
|
|
|
|
// if the vendor public library is going to be opened from /vendor/lib
|
|
|
|
// we might as well end up loading them from /system/lib
|
|
|
|
// For now we rely on CTS test to catch things like this but
|
|
|
|
// it should probably be addressed in the future.
|
|
|
|
for (const auto& soname : sonames) {
|
2017-02-04 02:09:46 +01:00
|
|
|
LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
|
|
|
|
"Error preloading public library %s: %s",
|
|
|
|
soname.c_str(), dlerror());
|
2016-04-07 03:24:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
system_public_libraries_ = base::Join(sonames, ':');
|
|
|
|
|
2017-08-22 03:26:10 +02:00
|
|
|
sonames.clear();
|
2017-11-08 10:44:09 +01:00
|
|
|
ReadConfig(llndk_native_libraries_system_config, &sonames, always_true);
|
2017-08-22 03:26:10 +02:00
|
|
|
system_llndk_libraries_ = base::Join(sonames, ':');
|
|
|
|
|
|
|
|
sonames.clear();
|
2017-11-08 10:44:09 +01:00
|
|
|
ReadConfig(vndksp_native_libraries_system_config, &sonames, always_true);
|
2017-08-22 03:26:10 +02:00
|
|
|
system_vndksp_libraries_ = base::Join(sonames, ':');
|
|
|
|
|
2017-05-02 00:12:49 +02:00
|
|
|
sonames.clear();
|
|
|
|
// This file is optional, quietly ignore if the file does not exist.
|
2017-11-08 10:44:09 +01:00
|
|
|
ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr);
|
2017-05-02 00:12:49 +02:00
|
|
|
|
|
|
|
vendor_public_libraries_ = base::Join(sonames, ':');
|
2016-04-07 03:24:08 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 19:43:16 +02:00
|
|
|
void Reset() {
|
|
|
|
namespaces_.clear();
|
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
private:
|
2016-06-22 01:11:23 +02:00
|
|
|
bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
|
2017-11-08 10:44:09 +01:00
|
|
|
const std::function<bool(const std::string& /* soname */,
|
|
|
|
std::string* /* error_msg */)>& check_soname,
|
2016-06-22 01:11:23 +02:00
|
|
|
std::string* error_msg = nullptr) {
|
2016-03-15 21:51:26 +01:00
|
|
|
// Read list of public native libraries from the config file.
|
|
|
|
std::string file_content;
|
2016-04-07 03:24:08 +02:00
|
|
|
if(!base::ReadFileToString(configFile, &file_content)) {
|
2016-06-22 01:11:23 +02:00
|
|
|
if (error_msg) *error_msg = strerror(errno);
|
2016-04-07 03:24:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-15 21:51:26 +01:00
|
|
|
|
|
|
|
std::vector<std::string> lines = base::Split(file_content, "\n");
|
|
|
|
|
2016-06-22 01:11:23 +02:00
|
|
|
for (auto& line : lines) {
|
2016-03-15 21:51:26 +01:00
|
|
|
auto trimmed_line = base::Trim(line);
|
|
|
|
if (trimmed_line[0] == '#' || trimmed_line.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-22 01:11:23 +02:00
|
|
|
size_t space_pos = trimmed_line.rfind(' ');
|
|
|
|
if (space_pos != std::string::npos) {
|
|
|
|
std::string type = trimmed_line.substr(space_pos + 1);
|
|
|
|
if (type != "32" && type != "64") {
|
|
|
|
if (error_msg) *error_msg = "Malformed line: " + line;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#if defined(__LP64__)
|
|
|
|
// Skip 32 bit public library.
|
|
|
|
if (type == "32") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// Skip 64 bit public library.
|
|
|
|
if (type == "64") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
trimmed_line.resize(space_pos);
|
|
|
|
}
|
2016-03-15 21:51:26 +01:00
|
|
|
|
2017-11-08 10:44:09 +01:00
|
|
|
if (check_soname(trimmed_line, error_msg)) {
|
|
|
|
sonames->push_back(trimmed_line);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-15 21:51:26 +01:00
|
|
|
}
|
|
|
|
|
2016-04-07 03:24:08 +02:00
|
|
|
return true;
|
2016-02-10 23:09:22 +01:00
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool InitPublicNamespace(const char* library_path, std::string* error_msg) {
|
|
|
|
// Ask native bride if this apps library path should be handled by it
|
|
|
|
bool is_native_bridge = NativeBridgeIsPathSupported(library_path);
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
// (http://b/25844435) - Some apps call dlopen from generated code (mono jited
|
|
|
|
// code is one example) unknown to linker in which case linker uses anonymous
|
|
|
|
// namespace. The second argument specifies the search path for the anonymous
|
|
|
|
// namespace which is the library_path of the classloader.
|
2017-05-02 00:12:49 +02:00
|
|
|
initialized_ = android_init_anonymous_namespace(system_public_libraries_.c_str(),
|
2017-02-03 23:11:27 +01:00
|
|
|
is_native_bridge ? nullptr : library_path);
|
2016-11-03 02:03:10 +01:00
|
|
|
if (!initialized_) {
|
|
|
|
*error_msg = dlerror();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// and now initialize native bridge namespaces if necessary.
|
|
|
|
if (NativeBridgeInitialized()) {
|
2017-05-02 00:12:49 +02:00
|
|
|
initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries_.c_str(),
|
2017-02-27 03:14:45 +01:00
|
|
|
is_native_bridge ? library_path : nullptr);
|
2016-05-30 05:16:08 +02:00
|
|
|
if (!initialized_) {
|
|
|
|
*error_msg = NativeBridgeGetError();
|
|
|
|
}
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
return initialized_;
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:34:41 +02:00
|
|
|
jobject GetParentClassLoader(JNIEnv* env, jobject class_loader) {
|
|
|
|
jclass class_loader_class = env->FindClass("java/lang/ClassLoader");
|
|
|
|
jmethodID get_parent = env->GetMethodID(class_loader_class,
|
|
|
|
"getParent",
|
|
|
|
"()Ljava/lang/ClassLoader;");
|
|
|
|
|
|
|
|
return env->CallObjectMethod(class_loader, get_parent);
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool FindParentNamespaceByClassLoader(JNIEnv* env,
|
|
|
|
jobject class_loader,
|
|
|
|
NativeLoaderNamespace* ns) {
|
2016-05-13 00:34:41 +02:00
|
|
|
jobject parent_class_loader = GetParentClassLoader(env, class_loader);
|
|
|
|
|
|
|
|
while (parent_class_loader != nullptr) {
|
2016-05-30 05:16:08 +02:00
|
|
|
if (FindNamespaceByClassLoader(env, parent_class_loader, ns)) {
|
|
|
|
return true;
|
2016-05-13 00:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent_class_loader = GetParentClassLoader(env, parent_class_loader);
|
|
|
|
}
|
2016-05-30 05:16:08 +02:00
|
|
|
|
|
|
|
return false;
|
2016-05-13 00:34:41 +02:00
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
bool initialized_;
|
2016-05-30 05:16:08 +02:00
|
|
|
std::vector<std::pair<jweak, NativeLoaderNamespace>> namespaces_;
|
2017-05-02 00:12:49 +02:00
|
|
|
std::string system_public_libraries_;
|
|
|
|
std::string vendor_public_libraries_;
|
2017-08-22 03:26:10 +02:00
|
|
|
std::string system_llndk_libraries_;
|
|
|
|
std::string system_vndksp_libraries_;
|
2015-12-01 22:56:44 +01:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
|
|
|
|
};
|
|
|
|
|
2016-02-29 22:21:43 +01:00
|
|
|
static std::mutex g_namespaces_mutex;
|
2015-12-01 22:56:44 +01:00
|
|
|
static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
|
|
|
|
#endif
|
|
|
|
|
2016-03-15 21:51:26 +01:00
|
|
|
void InitializeNativeLoader() {
|
2016-02-22 20:27:48 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-03-15 21:51:26 +01:00
|
|
|
g_namespaces->Initialize();
|
2016-02-22 20:27:48 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-02 19:43:16 +02:00
|
|
|
void ResetNativeLoader() {
|
|
|
|
#if defined(__ANDROID__)
|
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
|
|
|
g_namespaces->Reset();
|
|
|
|
#endif
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-02-23 23:23:51 +01:00
|
|
|
jstring CreateClassLoaderNamespace(JNIEnv* env,
|
|
|
|
int32_t target_sdk_version,
|
|
|
|
jobject class_loader,
|
|
|
|
bool is_shared,
|
2017-08-22 03:26:10 +02:00
|
|
|
bool is_for_vendor,
|
2016-02-23 23:23:51 +01:00
|
|
|
jstring library_path,
|
|
|
|
jstring permitted_path) {
|
2015-12-01 22:56:44 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-05-30 05:16:08 +02:00
|
|
|
|
|
|
|
std::string error_msg;
|
|
|
|
NativeLoaderNamespace ns;
|
|
|
|
bool success = g_namespaces->Create(env,
|
2017-05-09 07:24:24 +02:00
|
|
|
target_sdk_version,
|
2016-05-30 05:16:08 +02:00
|
|
|
class_loader,
|
|
|
|
is_shared,
|
2017-08-22 03:26:10 +02:00
|
|
|
is_for_vendor,
|
2016-05-30 05:16:08 +02:00
|
|
|
library_path,
|
|
|
|
permitted_path,
|
|
|
|
&ns,
|
|
|
|
&error_msg);
|
|
|
|
if (!success) {
|
|
|
|
return env->NewStringUTF(error_msg.c_str());
|
2016-02-23 23:23:51 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-08-22 03:26:10 +02:00
|
|
|
UNUSED(env, target_sdk_version, class_loader, is_shared, is_for_vendor,
|
2016-02-23 23:23:51 +01:00
|
|
|
library_path, permitted_path);
|
|
|
|
#endif
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* OpenNativeLibrary(JNIEnv* env,
|
|
|
|
int32_t target_sdk_version,
|
|
|
|
const char* path,
|
|
|
|
jobject class_loader,
|
2016-05-30 05:16:08 +02:00
|
|
|
jstring library_path,
|
|
|
|
bool* needs_native_bridge,
|
|
|
|
std::string* error_msg) {
|
2016-02-23 23:23:51 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-04-21 01:07:30 +02:00
|
|
|
UNUSED(target_sdk_version);
|
|
|
|
if (class_loader == nullptr) {
|
2016-05-30 05:16:08 +02:00
|
|
|
*needs_native_bridge = false;
|
2015-12-01 22:56:44 +01:00
|
|
|
return dlopen(path, RTLD_NOW);
|
|
|
|
}
|
|
|
|
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-05-30 05:16:08 +02:00
|
|
|
NativeLoaderNamespace ns;
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
if (!g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
|
2016-02-23 23:23:51 +01:00
|
|
|
// This is the case where the classloader was not created by ApplicationLoaders
|
|
|
|
// In this case we create an isolated not-shared namespace for it.
|
2017-05-09 07:24:24 +02:00
|
|
|
if (!g_namespaces->Create(env,
|
|
|
|
target_sdk_version,
|
|
|
|
class_loader,
|
2017-08-22 03:26:10 +02:00
|
|
|
false /* is_shared */,
|
|
|
|
false /* is_for_vendor */,
|
2017-05-09 07:24:24 +02:00
|
|
|
library_path,
|
|
|
|
nullptr,
|
|
|
|
&ns,
|
|
|
|
error_msg)) {
|
2016-02-23 23:23:51 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
if (ns.is_android_namespace()) {
|
|
|
|
android_dlextinfo extinfo;
|
|
|
|
extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
|
|
|
|
extinfo.library_namespace = ns.get_android_ns();
|
2015-12-01 22:56:44 +01:00
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
void* handle = android_dlopen_ext(path, RTLD_NOW, &extinfo);
|
|
|
|
if (handle == nullptr) {
|
|
|
|
*error_msg = dlerror();
|
|
|
|
}
|
|
|
|
*needs_native_bridge = false;
|
|
|
|
return handle;
|
|
|
|
} else {
|
|
|
|
void* handle = NativeBridgeLoadLibraryExt(path, RTLD_NOW, ns.get_native_bridge_ns());
|
|
|
|
if (handle == nullptr) {
|
|
|
|
*error_msg = NativeBridgeGetError();
|
|
|
|
}
|
|
|
|
*needs_native_bridge = true;
|
|
|
|
return handle;
|
|
|
|
}
|
2015-12-01 22:56:44 +01:00
|
|
|
#else
|
2016-02-23 23:23:51 +01:00
|
|
|
UNUSED(env, target_sdk_version, class_loader, library_path);
|
2016-05-30 05:16:08 +02:00
|
|
|
*needs_native_bridge = false;
|
|
|
|
void* handle = dlopen(path, RTLD_NOW);
|
|
|
|
if (handle == nullptr) {
|
|
|
|
if (NativeBridgeIsSupported(path)) {
|
|
|
|
*needs_native_bridge = true;
|
|
|
|
handle = NativeBridgeLoadLibrary(path, RTLD_NOW);
|
|
|
|
if (handle == nullptr) {
|
|
|
|
*error_msg = NativeBridgeGetError();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*needs_native_bridge = false;
|
|
|
|
*error_msg = dlerror();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handle;
|
2015-12-01 22:56:44 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:16:08 +02:00
|
|
|
bool CloseNativeLibrary(void* handle, const bool needs_native_bridge) {
|
|
|
|
return needs_native_bridge ? NativeBridgeUnloadLibrary(handle) :
|
|
|
|
dlclose(handle);
|
2016-05-03 23:55:25 +02:00
|
|
|
}
|
|
|
|
|
2016-02-22 22:48:22 +01:00
|
|
|
#if defined(__ANDROID__)
|
2016-11-01 22:17:00 +01:00
|
|
|
// native_bridge_namespaces are not supported for callers of this function.
|
|
|
|
// This function will return nullptr in the case when application is running
|
|
|
|
// on native bridge.
|
2016-02-22 22:48:22 +01:00
|
|
|
android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
|
2016-02-29 22:21:43 +01:00
|
|
|
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
|
2016-05-30 05:16:08 +02:00
|
|
|
NativeLoaderNamespace ns;
|
|
|
|
if (g_namespaces->FindNamespaceByClassLoader(env, class_loader, &ns)) {
|
2016-11-01 22:17:00 +01:00
|
|
|
return ns.is_android_namespace() ? ns.get_android_ns() : nullptr;
|
2016-05-30 05:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2016-02-22 22:48:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-12-01 22:56:44 +01:00
|
|
|
}; // android namespace
|