native_loader.cpp revision c18ac7cd2bc6214745b91d74e249962ce9ff7d26
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,
59                                   jstring java_library_path,
60                                   jstring java_permitted_path) {
61    ScopedUtfChars library_path(env, java_library_path);
62
63    std::string permitted_path;
64    if (java_permitted_path != nullptr) {
65      ScopedUtfChars path(env, java_permitted_path);
66      permitted_path = path.c_str();
67    }
68
69    if (!initialized_ && !InitPublicNamespace(library_path.c_str())) {
70      return nullptr;
71    }
72
73    std::lock_guard<std::mutex> guard(mutex_);
74
75    auto it = FindNamespaceByClassLoader(env, class_loader);
76
77    if (it != namespaces_.end()) {
78      return it->second;
79    }
80
81    android_namespace_t* ns =
82            android_create_namespace("classloader-namespace",
83                                     nullptr,
84                                     library_path.c_str(),
85                                     true,
86                                     java_permitted_path != nullptr ?
87                                        permitted_path.c_str() :
88                                        nullptr);
89
90    namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns));
91
92    return ns;
93  }
94
95 private:
96  bool InitPublicNamespace(const char* library_path) {
97    // Make sure all the public libraries are loaded
98    std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":");
99    for (const auto& soname : sonames) {
100      if (dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr) {
101        return false;
102      }
103    }
104
105    // Some apps call dlopen from generated code unknown to linker in which
106    // case linker uses anonymous namespace. See b/25844435 for details.
107    initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path);
108
109    return initialized_;
110  }
111
112  std::vector<std::pair<jweak, android_namespace_t*>>::const_iterator
113  FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) {
114    return std::find_if(namespaces_.begin(), namespaces_.end(),
115            [&](const std::pair<jweak, android_namespace_t*>& value) {
116              return env->IsSameObject(value.first, class_loader);
117            });
118  }
119
120  bool initialized_;
121  std::mutex mutex_;
122  std::vector<std::pair<jweak, android_namespace_t*>> namespaces_;
123
124  DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces);
125};
126
127static LibraryNamespaces* g_namespaces = new LibraryNamespaces;
128#endif
129
130
131void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path,
132                        jobject class_loader, jstring java_library_path,
133                        jstring java_permitted_path) {
134#if defined(__ANDROID__)
135  if (target_sdk_version <= INT_MAX || class_loader == nullptr) {
136    return dlopen(path, RTLD_NOW);
137  }
138
139  android_namespace_t* ns =
140      g_namespaces->GetOrCreate(env, class_loader, java_library_path,
141                                java_permitted_path);
142
143  if (ns == nullptr) {
144    return nullptr;
145  }
146
147  android_dlextinfo extinfo;
148  extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
149  extinfo.library_namespace = ns;
150
151  return android_dlopen_ext(path, RTLD_NOW, &extinfo);
152#else
153  UNUSED(env, target_sdk_version, class_loader,
154         java_library_path, java_permitted_path);
155  return dlopen(path, RTLD_NOW);
156#endif
157}
158
159}; //  android namespace
160