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 | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 21 | #include <dlfcn.h> |
| 22 | |
| 23 | #include <android-base/file.h> |
| 24 | #include <android-base/properties.h> |
| 25 | #include <android-base/strings.h> |
| 26 | #include <android/dlext.h> |
| 27 | #include <cutils/properties.h> |
| 28 | #include <log/log.h> |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 29 | #include <sys/prctl.h> |
| 30 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 31 | #include <mutex> |
| 32 | |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 33 | // TODO(b/37049319) Get this from a header once one exists |
| 34 | extern "C" { |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 35 | android_namespace_t* android_get_exported_namespace(const char*); |
| 36 | android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path, |
| 37 | const char* default_library_path, uint64_t type, |
| 38 | const char* permitted_when_isolated_path, |
| 39 | android_namespace_t* parent); |
| 40 | bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to, |
| 41 | const char* shared_libs_sonames); |
Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 42 | |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 43 | enum { |
| 44 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, |
| 45 | ANDROID_NAMESPACE_TYPE_SHARED = 2, |
| 46 | }; |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 49 | namespace android { |
| 50 | |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 51 | enum NativeLibrary { |
| 52 | LLNDK = 0, |
| 53 | VNDKSP = 1, |
| 54 | }; |
| 55 | |
| 56 | static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt", |
| 57 | "/etc/vndksp.libraries.txt"}; |
| 58 | |
| 59 | static std::string vndkVersionStr() { |
| 60 | #ifdef __BIONIC__ |
| 61 | std::string version = android::base::GetProperty("ro.vndk.version", ""); |
| 62 | if (version != "" && version != "current") { |
| 63 | return "." + version; |
| 64 | } |
| 65 | #endif |
| 66 | return ""; |
| 67 | } |
| 68 | |
| 69 | static void insertVndkVersionStr(std::string* fileName) { |
| 70 | LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr"); |
| 71 | size_t insertPos = fileName->find_last_of("."); |
| 72 | if (insertPos == std::string::npos) { |
| 73 | insertPos = fileName->length(); |
| 74 | } |
| 75 | fileName->insert(insertPos, vndkVersionStr()); |
| 76 | } |
| 77 | |
| 78 | static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) { |
| 79 | // Read list of public native libraries from the config file. |
| 80 | std::string fileContent; |
| 81 | if (!base::ReadFileToString(configFile, &fileContent)) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | std::vector<std::string> lines = base::Split(fileContent, "\n"); |
| 86 | |
| 87 | for (auto& line : lines) { |
| 88 | auto trimmedLine = base::Trim(line); |
| 89 | if (!trimmedLine.empty()) { |
| 90 | soNames->push_back(trimmedLine); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static const std::string getSystemNativeLibraries(NativeLibrary type) { |
| 98 | static const char* androidRootEnv = getenv("ANDROID_ROOT"); |
| 99 | static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system"; |
| 100 | |
| 101 | std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type]; |
| 102 | |
| 103 | insertVndkVersionStr(&nativeLibrariesSystemConfig); |
| 104 | |
| 105 | std::vector<std::string> soNames; |
| 106 | if (!readConfig(nativeLibrariesSystemConfig, &soNames)) { |
| 107 | ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str()); |
| 108 | return ""; |
| 109 | } |
| 110 | |
| 111 | return base::Join(soNames, ':'); |
| 112 | } |
| 113 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 114 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { |
| 115 | static GraphicsEnv env; |
| 116 | return env; |
| 117 | } |
| 118 | |
Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 119 | int GraphicsEnv::getCanLoadSystemLibraries() { |
| 120 | if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 121 | // Return an integer value since this crosses library boundaries |
| 122 | return 1; |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 127 | void GraphicsEnv::setDriverPath(const std::string path) { |
| 128 | if (!mDriverPath.empty()) { |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 129 | ALOGV("ignoring attempt to change driver path from '%s' to '%s'", mDriverPath.c_str(), |
| 130 | path.c_str()); |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | ALOGV("setting driver path to '%s'", path.c_str()); |
| 134 | mDriverPath = path; |
| 135 | } |
| 136 | |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 137 | void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName, |
Cody Northrop | f0874d3 | 2018-10-29 10:59:45 -0600 | [diff] [blame] | 138 | bool developerOptIn, const int rulesFd, const long rulesOffset, |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 139 | const long rulesLength) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 140 | if (!mAnglePath.empty()) { |
| 141 | ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(), |
| 142 | path.c_str()); |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 143 | } else { |
| 144 | ALOGV("setting ANGLE path to '%s'", path.c_str()); |
| 145 | mAnglePath = path; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 146 | } |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 147 | |
| 148 | if (!mAngleAppName.empty()) { |
| 149 | ALOGV("ignoring attempt to change ANGLE app name from '%s' to '%s'", mAngleAppName.c_str(), |
| 150 | appName.c_str()); |
| 151 | } else { |
| 152 | ALOGV("setting ANGLE app name to '%s'", appName.c_str()); |
| 153 | mAngleAppName = appName; |
| 154 | } |
| 155 | |
| 156 | mAngleDeveloperOptIn = developerOptIn; |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 157 | |
| 158 | ALOGV("setting ANGLE rules file descriptor to '%i'", rulesFd); |
| 159 | mAngleRulesFd = rulesFd; |
| 160 | ALOGV("setting ANGLE rules offset to '%li'", rulesOffset); |
| 161 | mAngleRulesOffset = rulesOffset; |
| 162 | ALOGV("setting ANGLE rules length to '%li'", rulesLength); |
| 163 | mAngleRulesLength = rulesLength; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 164 | } |
| 165 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 166 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 167 | if (mLayerPaths.empty()) { |
| 168 | mLayerPaths = layerPaths; |
| 169 | mAppNamespace = appNamespace; |
| 170 | } else { |
| 171 | ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'", |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 172 | layerPaths.c_str(), appNamespace); |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 176 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 177 | return mAppNamespace; |
| 178 | } |
| 179 | |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 180 | const char* GraphicsEnv::getAngleAppName() { |
| 181 | if (mAngleAppName.empty()) return nullptr; |
| 182 | return mAngleAppName.c_str(); |
| 183 | } |
| 184 | |
| 185 | bool GraphicsEnv::getAngleDeveloperOptIn() { |
| 186 | return mAngleDeveloperOptIn; |
| 187 | } |
| 188 | |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 189 | int GraphicsEnv::getAngleRulesFd() { |
| 190 | return mAngleRulesFd; |
| 191 | } |
| 192 | |
| 193 | long GraphicsEnv::getAngleRulesOffset() { |
| 194 | return mAngleRulesOffset; |
| 195 | } |
| 196 | |
| 197 | long GraphicsEnv::getAngleRulesLength() { |
| 198 | return mAngleRulesLength; |
| 199 | } |
| 200 | |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 201 | const std::string& GraphicsEnv::getLayerPaths() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 202 | return mLayerPaths; |
| 203 | } |
| 204 | |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 205 | const std::string& GraphicsEnv::getDebugLayers() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 206 | return mDebugLayers; |
| 207 | } |
| 208 | |
Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 209 | const std::string& GraphicsEnv::getDebugLayersGLES() { |
| 210 | return mDebugLayersGLES; |
| 211 | } |
| 212 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 213 | void GraphicsEnv::setDebugLayers(const std::string layers) { |
| 214 | mDebugLayers = layers; |
| 215 | } |
| 216 | |
Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 217 | void GraphicsEnv::setDebugLayersGLES(const std::string layers) { |
| 218 | mDebugLayersGLES = layers; |
| 219 | } |
| 220 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 221 | android_namespace_t* GraphicsEnv::getDriverNamespace() { |
| 222 | static std::once_flag once; |
| 223 | std::call_once(once, [this]() { |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 224 | if (mDriverPath.empty()) return; |
| 225 | |
| 226 | auto vndkNamespace = android_get_exported_namespace("vndk"); |
| 227 | if (!vndkNamespace) return; |
| 228 | |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 229 | mDriverNamespace = android_create_namespace("gfx driver", |
Peiyong Lin | 2e9c74c | 2018-10-24 11:18:07 -0700 | [diff] [blame] | 230 | mDriverPath.c_str(), // ld_library_path |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 231 | mDriverPath.c_str(), // default_library_path |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 232 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 233 | nullptr, // permitted_when_isolated_path |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 234 | nullptr); |
| 235 | |
| 236 | const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); |
| 237 | if (llndkLibraries.empty()) { |
| 238 | mDriverNamespace = nullptr; |
| 239 | return; |
| 240 | } |
| 241 | if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { |
| 242 | ALOGE("Failed to link default namespace[%s]", dlerror()); |
| 243 | mDriverNamespace = nullptr; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); |
| 248 | if (vndkspLibraries.empty()) { |
| 249 | mDriverNamespace = nullptr; |
| 250 | return; |
| 251 | } |
| 252 | if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { |
| 253 | ALOGE("Failed to link vndk namespace[%s]", dlerror()); |
| 254 | mDriverNamespace = nullptr; |
| 255 | return; |
| 256 | } |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 257 | }); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame^] | 258 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 259 | return mDriverNamespace; |
| 260 | } |
| 261 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 262 | android_namespace_t* GraphicsEnv::getAngleNamespace() { |
| 263 | static std::once_flag once; |
| 264 | std::call_once(once, [this]() { |
| 265 | if (mAnglePath.empty()) return; |
| 266 | |
| 267 | mAngleNamespace = android_create_namespace("ANGLE", |
| 268 | nullptr, // ld_library_path |
| 269 | mAnglePath.c_str(), // default_library_path |
| 270 | ANDROID_NAMESPACE_TYPE_SHARED | |
| 271 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 272 | nullptr, // permitted_when_isolated_path |
| 273 | nullptr); |
| 274 | if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default"); |
| 275 | }); |
| 276 | |
| 277 | return mAngleNamespace; |
| 278 | } |
| 279 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 280 | } // namespace android |