native_loader.cpp revision 8b0471462e5bb9e81652a1a53cc786df9848feef
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "nativeloader/native_loader.h"
18#include "ScopedUtfChars.h"
19
20#include <dlfcn.h>
21#ifdef __ANDROID__
22#include <android/dlext.h>
23#include "cutils/properties.h"
24#endif
25
26#include <algorithm>
27#include <vector>
28#include <string>
29#include <mutex>
30
31#include "android-base/macros.h"
32#include "android-base/strings.h"
33
34namespace android {
35
36#ifdef __ANDROID__
37// TODO(dimitry): move this to system properties.
38static const char* kPublicNativeLibraries = "libandroid.so:"
39                                            "libc.so:"
40                                            "libdl.so:"
41                                            "libEGL.so:"
42                                            "libGLESv1_CM.so:"
43                                            "libGLESv2.so:"
44                                            "libGLESv3.so:"
45                                            "libjnigraphics.so:"
46                                            "liblog.so:"
47                                            "libmediandk.so:"
48                                            "libm.so:"
49                                            "libOpenMAXAL.so:"
50                                            "libOpenSLES.so:"
51                                            "libstdc++.so:"
52                                            "libz.so";
53
54class LibraryNamespaces {
55 public:
56  LibraryNamespaces() : initialized_(false) { }
57
58  android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader, jstring library_path) {
59    ScopedUtfChars libraryPath(env, library_path);
60
61    if (!initialized_ && !InitPublicNamespace(libraryPath.c_str())) {
62      return nullptr;
63    }
64
65    std::lock_guard<std::mutex> guard(mutex_);
66
67    auto it = FindNamespaceByClassLoader(env, class_loader);
68
69    if (it != namespaces_.end()) {
70      return it->second;
71    }
72
73    android_namespace_t* ns =
74            android_create_namespace("classloader-namespace",
75                                     nullptr,
76                                     libraryPath.c_str(),
77                                     true);
78
79    namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
80
81    return ns;
82  }
83
84 private:
85  bool InitPublicNamespace(const char* library_path) {
86    // Make sure all the public libraries are loaded
87    std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":");
88    for (const auto& soname : sonames) {
89      if (dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr) {
90        return false;
91      }
92    }
93
94    // Some apps call dlopen from generated code unknown to linker in which
95    // case linker uses anonymous namespace. See b/25844435 for details.
96    initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path);
97
98    return initialized_;
99  }
100
101  std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator
102  FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
103    return std::find_if(namespaces_.begin(), namespaces_.end(),
104            [&](const std::pair<jweak, android_namespace_t*>& value) {
105              return env->IsSameObject(value.first, class_loader);
106            });
107  }
108
109  bool initialized_;
110  std::mutex mutex_;
111  std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
112
113  DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
114};
115
116static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
117#endif
118
119
120void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
121                        jobject class_loader, jstring library_path) {
122#if defined(__ANDROID__)
123  if (target_sdk_version == 0 || class_loader == nullptr) {
124    return dlopen(path, RTLD_NOW);
125  }
126
127  android_namespace_t* ns = g_namespaces->GetOrCreate(env, class_loader, library_path);
128
129  if (ns == nullptr) {
130    return nullptr;
131  }
132
133  android_dlextinfo extinfo;
134  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
135  extinfo.library_namespace = ns;
136
137  return android_dlopen_ext(path, RTLD_NOW, &extinfo);
138#else
139  UNUSED(env, target_sdk_version, class_loader, library_path);
140  return dlopen(path, RTLD_NOW);
141#endif
142}
143
144}; //  android namespace
145