native_loader.cpp revision 6082622b1805e925412100c736fb98143d9390d7
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                                            // TODO (dimitry): This is a workaround for http://b/26436837
40                                            // will be removed before the release.
41                                            "libart.so:"
42                                            // END OF WORKAROUND
43                                            "libc.so:"
44                                            "libdl.so:"
45                                            "libEGL.so:"
46                                            "libGLESv1_CM.so:"
47                                            "libGLESv2.so:"
48                                            "libGLESv3.so:"
49                                            "libjnigraphics.so:"
50                                            "liblog.so:"
51                                            "libmediandk.so:"
52                                            "libm.so:"
53                                            "libOpenMAXAL.so:"
54                                            "libOpenSLES.so:"
55                                            "libstdc++.so:"
56                                            "libwebviewchromium_plat_support.so:"
57                                            "libz.so";
58
59class LibraryNamespaces {
60 public:
61  LibraryNamespaces() : initialized_(false) { }
62
63  android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader,
64                                   bool is_shared,
65                                   jstring java_library_path,
66                                   jstring java_permitted_path) {
67    ScopedUtfChars library_path(env, java_library_path);
68
69    std::string permitted_path;
70    if (java_permitted_path != nullptr) {
71      ScopedUtfChars path(env, java_permitted_path);
72      permitted_path = path.c_str();
73    }
74
75    if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
76      return nullptr;
77    }
78
79    std::lock_guard<std::mutex> guard(mutex_);
80
81    auto it = FindNamespaceByClassLoader(env, class_loader);
82
83    if (it != namespaces_.end()) {
84      return it->second;
85    }
86
87    uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED;
88    if (is_shared) {
89      namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED;
90    }
91
92    android_namespace_t* ns =
93            android_create_namespace("classloader-namespace",
94                                     nullptr,
95                                     library_path.c_str(),
96                                     namespace_type,
97                                     java_permitted_path != nullptr ?
98                                        permitted_path.c_str() :
99                                        nullptr);
100
101    namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
102
103    return ns;
104  }
105
106 private:
107  bool InitPublicNamespace(const char* library_path) {
108    // Make sure all the public libraries are loaded
109    std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":");
110    for (const auto& soname : sonames) {
111      if (dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr) {
112        return false;
113      }
114    }
115
116    // Some apps call dlopen from generated code unknown to linker in which
117    // case linker uses anonymous namespace. See b/25844435 for details.
118    initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path);
119
120    return initialized_;
121  }
122
123  std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator
124  FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
125    return std::find_if(namespaces_.begin(), namespaces_.end(),
126            [&](const std::pair<jweak, android_namespace_t*>& value) {
127              return env->IsSameObject(value.first, class_loader);
128            });
129  }
130
131  bool initialized_;
132  std::mutex mutex_;
133  std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
134
135  DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
136};
137
138static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
139#endif
140
141
142void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
143                        jobject class_loader, bool is_shared, jstring java_library_path,
144                        jstring java_permitted_path) {
145#if defined(__ANDROID__)
146  if (target_sdk_version == 0 || class_loader == nullptr) {
147    return dlopen(path, RTLD_NOW);
148  }
149
150  android_namespace_t* ns =
151      g_namespaces->GetOrCreate(env, class_loader, is_shared,
152                                java_library_path, java_permitted_path);
153
154  if (ns == nullptr) {
155    return nullptr;
156  }
157
158  android_dlextinfo extinfo;
159  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
160  extinfo.library_namespace = ns;
161
162  return android_dlopen_ext(path, RTLD_NOW, &extinfo);
163#else
164  UNUSED(env, target_sdk_version, class_loader, is_shared,
165         java_library_path, java_permitted_path);
166  return dlopen(path, RTLD_NOW);
167#endif
168}
169
170}; //  android namespace
171