| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2017 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 | //#define LOG_NDEBUG 1 | 
|  | 18 | #define LOG_TAG "GraphicsEnv" | 
| Jiyong Park | 27c39e1 | 2017-05-08 13:00:02 +0900 | [diff] [blame] | 19 | #include <graphicsenv/GraphicsEnv.h> | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 20 |  | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 21 | #include <dlfcn.h> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 22 |  | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 23 | #include <android-base/file.h> | 
|  | 24 | #include <android-base/properties.h> | 
|  | 25 | #include <android-base/strings.h> | 
| Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 26 | #include <android/dlext.h> | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 27 | #include <log/log.h> | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 28 | #include <sys/prctl.h> | 
|  | 29 |  | 
|  | 30 | #include <mutex> | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 31 |  | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 32 | // TODO(b/37049319) Get this from a header once one exists | 
|  | 33 | extern "C" { | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 34 | android_namespace_t* android_get_exported_namespace(const char*); | 
|  | 35 | android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path, | 
|  | 36 | const char* default_library_path, uint64_t type, | 
|  | 37 | const char* permitted_when_isolated_path, | 
|  | 38 | android_namespace_t* parent); | 
|  | 39 | bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to, | 
|  | 40 | const char* shared_libs_sonames); | 
| Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 41 |  | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 42 | enum { | 
|  | 43 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, | 
|  | 44 | ANDROID_NAMESPACE_TYPE_SHARED = 2, | 
|  | 45 | }; | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 46 | } | 
|  | 47 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 48 | namespace android { | 
|  | 49 |  | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 50 | enum NativeLibrary { | 
|  | 51 | LLNDK = 0, | 
|  | 52 | VNDKSP = 1, | 
|  | 53 | }; | 
|  | 54 |  | 
|  | 55 | static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt", | 
|  | 56 | "/etc/vndksp.libraries.txt"}; | 
|  | 57 |  | 
|  | 58 | static std::string vndkVersionStr() { | 
|  | 59 | #ifdef __BIONIC__ | 
|  | 60 | std::string version = android::base::GetProperty("ro.vndk.version", ""); | 
|  | 61 | if (version != "" && version != "current") { | 
|  | 62 | return "." + version; | 
|  | 63 | } | 
|  | 64 | #endif | 
|  | 65 | return ""; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | static void insertVndkVersionStr(std::string* fileName) { | 
|  | 69 | LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr"); | 
|  | 70 | size_t insertPos = fileName->find_last_of("."); | 
|  | 71 | if (insertPos == std::string::npos) { | 
|  | 72 | insertPos = fileName->length(); | 
|  | 73 | } | 
|  | 74 | fileName->insert(insertPos, vndkVersionStr()); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) { | 
|  | 78 | // Read list of public native libraries from the config file. | 
|  | 79 | std::string fileContent; | 
|  | 80 | if (!base::ReadFileToString(configFile, &fileContent)) { | 
|  | 81 | return false; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | std::vector<std::string> lines = base::Split(fileContent, "\n"); | 
|  | 85 |  | 
|  | 86 | for (auto& line : lines) { | 
|  | 87 | auto trimmedLine = base::Trim(line); | 
|  | 88 | if (!trimmedLine.empty()) { | 
|  | 89 | soNames->push_back(trimmedLine); | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | return true; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | static const std::string getSystemNativeLibraries(NativeLibrary type) { | 
|  | 97 | static const char* androidRootEnv = getenv("ANDROID_ROOT"); | 
|  | 98 | static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system"; | 
|  | 99 |  | 
|  | 100 | std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type]; | 
|  | 101 |  | 
|  | 102 | insertVndkVersionStr(&nativeLibrariesSystemConfig); | 
|  | 103 |  | 
|  | 104 | std::vector<std::string> soNames; | 
|  | 105 | if (!readConfig(nativeLibrariesSystemConfig, &soNames)) { | 
|  | 106 | ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str()); | 
|  | 107 | return ""; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | return base::Join(soNames, ':'); | 
|  | 111 | } | 
|  | 112 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 113 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { | 
|  | 114 | static GraphicsEnv env; | 
|  | 115 | return env; | 
|  | 116 | } | 
|  | 117 |  | 
| Yiwei Zhang | 49593ea | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 118 | void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path, | 
|  | 119 | const std::string sphalLibraries) { | 
|  | 120 | if (!mDriverPath.empty() || !mSphalLibraries.empty()) { | 
|  | 121 | ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries " | 
|  | 122 | "from '%s' to '%s'", | 
|  | 123 | mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 124 | return; | 
|  | 125 | } | 
| Yiwei Zhang | 49593ea | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 126 | ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(), | 
|  | 127 | sphalLibraries.c_str()); | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 128 | mDriverPath = path; | 
| Yiwei Zhang | 49593ea | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 129 | mSphalLibraries = sphalLibraries; | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Victor Khimenko | bbf7700 | 2018-08-16 22:38:36 +0200 | [diff] [blame] | 132 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 133 | if (mLayerPaths.empty()) { | 
|  | 134 | mLayerPaths = layerPaths; | 
|  | 135 | mAppNamespace = appNamespace; | 
|  | 136 | } else { | 
|  | 137 | ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'", | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 138 | layerPaths.c_str(), appNamespace); | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 139 | } | 
|  | 140 | } | 
|  | 141 |  | 
| Victor Khimenko | bbf7700 | 2018-08-16 22:38:36 +0200 | [diff] [blame] | 142 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 143 | return mAppNamespace; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | const std::string GraphicsEnv::getLayerPaths(){ | 
|  | 147 | return mLayerPaths; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | const std::string GraphicsEnv::getDebugLayers() { | 
|  | 151 | return mDebugLayers; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | void GraphicsEnv::setDebugLayers(const std::string layers) { | 
|  | 155 | mDebugLayers = layers; | 
|  | 156 | } | 
|  | 157 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 158 | android_namespace_t* GraphicsEnv::getDriverNamespace() { | 
|  | 159 | static std::once_flag once; | 
|  | 160 | std::call_once(once, [this]() { | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 161 | if (mDriverPath.empty()) return; | 
|  | 162 |  | 
|  | 163 | auto vndkNamespace = android_get_exported_namespace("vndk"); | 
|  | 164 | if (!vndkNamespace) return; | 
|  | 165 |  | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 166 | mDriverNamespace = android_create_namespace("gfx driver", | 
| Peiyong Lin | a649868 | 2018-10-24 11:18:07 -0700 | [diff] [blame] | 167 | mDriverPath.c_str(), // ld_library_path | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 168 | mDriverPath.c_str(), // default_library_path | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 169 | ANDROID_NAMESPACE_TYPE_ISOLATED, | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 170 | nullptr, // permitted_when_isolated_path | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 171 | nullptr); | 
|  | 172 |  | 
|  | 173 | const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); | 
|  | 174 | if (llndkLibraries.empty()) { | 
|  | 175 | mDriverNamespace = nullptr; | 
|  | 176 | return; | 
|  | 177 | } | 
|  | 178 | if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { | 
|  | 179 | ALOGE("Failed to link default namespace[%s]", dlerror()); | 
|  | 180 | mDriverNamespace = nullptr; | 
|  | 181 | return; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); | 
|  | 185 | if (vndkspLibraries.empty()) { | 
|  | 186 | mDriverNamespace = nullptr; | 
|  | 187 | return; | 
|  | 188 | } | 
|  | 189 | if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { | 
|  | 190 | ALOGE("Failed to link vndk namespace[%s]", dlerror()); | 
|  | 191 | mDriverNamespace = nullptr; | 
|  | 192 | return; | 
|  | 193 | } | 
| Yiwei Zhang | 49593ea | 2019-02-14 12:28:12 -0800 | [diff] [blame] | 194 |  | 
|  | 195 | if (mSphalLibraries.empty()) return; | 
|  | 196 |  | 
|  | 197 | // Make additional libraries in sphal to be accessible | 
|  | 198 | auto sphalNamespace = android_get_exported_namespace("sphal"); | 
|  | 199 | if (!sphalNamespace) { | 
|  | 200 | ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace", | 
|  | 201 | mSphalLibraries.c_str()); | 
|  | 202 | mDriverNamespace = nullptr; | 
|  | 203 | return; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) { | 
|  | 207 | ALOGE("Failed to link sphal namespace[%s]", dlerror()); | 
|  | 208 | mDriverNamespace = nullptr; | 
|  | 209 | return; | 
|  | 210 | } | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 211 | }); | 
| Yiwei Zhang | 9df2a65 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 212 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 213 | return mDriverNamespace; | 
|  | 214 | } | 
|  | 215 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 216 | } // namespace android | 
| Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 217 |  | 
|  | 218 | extern "C" android_namespace_t* android_getDriverNamespace() { | 
|  | 219 | return android::GraphicsEnv::getInstance().getDriverNamespace(); | 
|  | 220 | } |