| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 8b04714 | 2015-12-08 10:38:59 -0800 | [diff] [blame] | 31 | #include "android-base/macros.h" | 
 | 32 | #include "android-base/strings.h" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 33 |  | 
 | 34 | namespace android { | 
 | 35 |  | 
 | 36 | #ifdef __ANDROID__ | 
 | 37 | // TODO(dimitry): move this to system properties. | 
 | 38 | static const char* kPublicNativeLibraries = "libandroid.so:" | 
 | 39 |                                             "libc.so:" | 
 | 40 |                                             "libdl.so:" | 
 | 41 |                                             "libEGL.so:" | 
 | 42 |                                             "libGLESv1_CM.so:" | 
 | 43 |                                             "libGLESv2.so:" | 
 | 44 |                                             "libGLESv3.so:" | 
| Dimitry Ivanov | c24ca89 | 2016-02-02 10:40:08 -0800 | [diff] [blame] | 45 |                                             "libicui18n.so:" | 
 | 46 |                                             "libicuuc.so:" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 47 |                                             "libjnigraphics.so:" | 
 | 48 |                                             "liblog.so:" | 
 | 49 |                                             "libmediandk.so:" | 
 | 50 |                                             "libm.so:" | 
 | 51 |                                             "libOpenMAXAL.so:" | 
 | 52 |                                             "libOpenSLES.so:" | 
| Dimitry Ivanov | cdb6fee | 2016-01-13 15:19:35 -0800 | [diff] [blame] | 53 |                                             "libRS.so:" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 54 |                                             "libstdc++.so:" | 
| Dimitry Ivanov | 90bf68e | 2016-01-08 12:35:38 -0800 | [diff] [blame] | 55 |                                             "libwebviewchromium_plat_support.so:" | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 56 |                                             "libz.so"; | 
 | 57 |  | 
 | 58 | class LibraryNamespaces { | 
 | 59 |  public: | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 60 |   LibraryNamespaces() : initialized_(false) { } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 61 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 62 |   android_namespace_t* GetOrCreate(JNIEnv* env, jobject class_loader, | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 63 |                                    bool is_shared, | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 64 |                                    jstring java_library_path, | 
 | 65 |                                    jstring java_permitted_path) { | 
 | 66 |     ScopedUtfChars library_path(env, java_library_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 67 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 68 |     std::string permitted_path; | 
 | 69 |     if (java_permitted_path != nullptr) { | 
 | 70 |       ScopedUtfChars path(env, java_permitted_path); | 
 | 71 |       permitted_path = path.c_str(); | 
 | 72 |     } | 
 | 73 |  | 
 | 74 |     if (!initialized_ && !InitPublicNamespace(library_path.c_str())) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 75 |       return nullptr; | 
 | 76 |     } | 
 | 77 |  | 
 | 78 |     std::lock_guard<std::mutex> guard(mutex_); | 
 | 79 |  | 
| Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 80 |     android_namespace_t* ns = FindNamespaceByClassLoader(env, class_loader); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 81 |  | 
| Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 82 |     if (ns != nullptr) { | 
 | 83 |       return ns; | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 84 |     } | 
 | 85 |  | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 86 |     uint64_t namespace_type = ANDROID_NAMESPACE_TYPE_ISOLATED; | 
 | 87 |     if (is_shared) { | 
 | 88 |       namespace_type |= ANDROID_NAMESPACE_TYPE_SHARED; | 
 | 89 |     } | 
 | 90 |  | 
| Dimitry Ivanov | 34fa704 | 2016-02-22 13:02:35 -0800 | [diff] [blame] | 91 |     ns = android_create_namespace("classloader-namespace", | 
 | 92 |                                   nullptr, | 
 | 93 |                                   library_path.c_str(), | 
 | 94 |                                   namespace_type, | 
 | 95 |                                   java_permitted_path != nullptr ? | 
 | 96 |                                       permitted_path.c_str() : | 
 | 97 |                                       nullptr); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 98 |  | 
 | 99 |     namespaces_.push_back(std::make_pair(env->NewWeakGlobalRef(class_loader), ns)); | 
 | 100 |  | 
 | 101 |     return ns; | 
 | 102 |   } | 
 | 103 |  | 
| Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 104 |   android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
 | 105 |     auto it = std::find_if(namespaces_.begin(), namespaces_.end(), | 
 | 106 |                 [&](const std::pair<jweak, android_namespace_t*>& value) { | 
 | 107 |                   return env->IsSameObject(value.first, class_loader); | 
 | 108 |                 }); | 
 | 109 |     return it != namespaces_.end() ? it->second : nullptr; | 
 | 110 |   } | 
 | 111 |  | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 112 |   void PreloadPublicLibraries() { | 
 | 113 |     // android_init_namespaces() expects all the public libraries | 
 | 114 |     // to be loaded so that they can be found by soname alone. | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 115 |     std::vector<std::string> sonames = android::base::Split(kPublicNativeLibraries, ":"); | 
 | 116 |     for (const auto& soname : sonames) { | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 117 |       dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 118 |     } | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 119 |   } | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 120 |  | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 121 |  private: | 
| Dimitry Ivanov | 55bbb0d | 2016-02-10 14:09:22 -0800 | [diff] [blame] | 122 |   bool InitPublicNamespace(const char* library_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 123 |     // Some apps call dlopen from generated code unknown to linker in which | 
 | 124 |     // case linker uses anonymous namespace. See b/25844435 for details. | 
 | 125 |     initialized_ = android_init_namespaces(kPublicNativeLibraries, library_path); | 
 | 126 |  | 
 | 127 |     return initialized_; | 
 | 128 |   } | 
 | 129 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 130 |   bool initialized_; | 
 | 131 |   std::mutex mutex_; | 
 | 132 |   std::vector<std::pair<jweak, android_namespace_t*>> namespaces_; | 
 | 133 |  | 
 | 134 |   DISALLOW_COPY_AND_ASSIGN(LibraryNamespaces); | 
 | 135 | }; | 
 | 136 |  | 
 | 137 | static LibraryNamespaces* g_namespaces = new LibraryNamespaces; | 
 | 138 | #endif | 
 | 139 |  | 
| Dimitry Ivanov | 6f80022 | 2016-02-22 11:27:48 -0800 | [diff] [blame] | 140 | void PreloadPublicNativeLibraries() { | 
 | 141 | #if defined(__ANDROID__) | 
 | 142 |   g_namespaces->PreloadPublicLibraries(); | 
 | 143 | #endif | 
 | 144 | } | 
 | 145 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 146 |  | 
 | 147 | void* OpenNativeLibrary(JNIEnv* env, int32_t target_sdk_version, const char* path, | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 148 |                         jobject class_loader, bool is_shared, jstring java_library_path, | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 149 |                         jstring java_permitted_path) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 150 | #if defined(__ANDROID__) | 
| Dimitry Ivanov | 619ffb4 | 2015-12-15 15:55:50 -0800 | [diff] [blame] | 151 |   if (target_sdk_version == 0 || class_loader == nullptr) { | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 152 |     return dlopen(path, RTLD_NOW); | 
 | 153 |   } | 
 | 154 |  | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 155 |   android_namespace_t* ns = | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 156 |       g_namespaces->GetOrCreate(env, class_loader, is_shared, | 
 | 157 |                                 java_library_path, java_permitted_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 158 |  | 
 | 159 |   if (ns == nullptr) { | 
 | 160 |     return nullptr; | 
 | 161 |   } | 
 | 162 |  | 
 | 163 |   android_dlextinfo extinfo; | 
 | 164 |   extinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; | 
 | 165 |   extinfo.library_namespace = ns; | 
 | 166 |  | 
 | 167 |   return android_dlopen_ext(path, RTLD_NOW, &extinfo); | 
 | 168 | #else | 
| Dimitry Ivanov | d2a6220 | 2015-12-15 11:06:57 -0800 | [diff] [blame] | 169 |   UNUSED(env, target_sdk_version, class_loader, is_shared, | 
| Dimitry Ivanov | 0d6e594 | 2015-12-08 11:16:56 -0800 | [diff] [blame] | 170 |          java_library_path, java_permitted_path); | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 171 |   return dlopen(path, RTLD_NOW); | 
 | 172 | #endif | 
 | 173 | } | 
 | 174 |  | 
| Dimitry Ivanov | 0cd10d8 | 2016-02-22 13:48:22 -0800 | [diff] [blame] | 175 | #if defined(__ANDROID__) | 
 | 176 | android_namespace_t* FindNamespaceByClassLoader(JNIEnv* env, jobject class_loader) { | 
 | 177 |   return g_namespaces->FindNamespaceByClassLoader(env, class_loader); | 
 | 178 | } | 
 | 179 | #endif | 
 | 180 |  | 
| Dimitry Ivanov | ac1b191 | 2015-12-01 13:56:44 -0800 | [diff] [blame] | 181 | }; //  android namespace |