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, |
Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame^] | 138 | const std::string developerOptIn, const int rulesFd, |
| 139 | const long rulesOffset, 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 | |
Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame^] | 156 | if (!mAngleDeveloperOptIn.empty()) { |
| 157 | ALOGV("ignoring attempt to change ANGLE application opt-in from '%s' to '%s'", |
| 158 | mAngleDeveloperOptIn.c_str(), developerOptIn.c_str()); |
| 159 | } else { |
| 160 | ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str()); |
| 161 | mAngleDeveloperOptIn = developerOptIn; |
| 162 | } |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 163 | |
| 164 | ALOGV("setting ANGLE rules file descriptor to '%i'", rulesFd); |
| 165 | mAngleRulesFd = rulesFd; |
| 166 | ALOGV("setting ANGLE rules offset to '%li'", rulesOffset); |
| 167 | mAngleRulesOffset = rulesOffset; |
| 168 | ALOGV("setting ANGLE rules length to '%li'", rulesLength); |
| 169 | mAngleRulesLength = rulesLength; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 172 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 173 | if (mLayerPaths.empty()) { |
| 174 | mLayerPaths = layerPaths; |
| 175 | mAppNamespace = appNamespace; |
| 176 | } else { |
| 177 | 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] | 178 | layerPaths.c_str(), appNamespace); |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 182 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 183 | return mAppNamespace; |
| 184 | } |
| 185 | |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 186 | const char* GraphicsEnv::getAngleAppName() { |
| 187 | if (mAngleAppName.empty()) return nullptr; |
| 188 | return mAngleAppName.c_str(); |
| 189 | } |
| 190 | |
Tim Van Patten | a2a60a0 | 2018-11-09 16:51:15 -0700 | [diff] [blame^] | 191 | const char* GraphicsEnv::getAngleDeveloperOptIn() { |
| 192 | return mAngleDeveloperOptIn.c_str(); |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 193 | } |
| 194 | |
Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 195 | int GraphicsEnv::getAngleRulesFd() { |
| 196 | return mAngleRulesFd; |
| 197 | } |
| 198 | |
| 199 | long GraphicsEnv::getAngleRulesOffset() { |
| 200 | return mAngleRulesOffset; |
| 201 | } |
| 202 | |
| 203 | long GraphicsEnv::getAngleRulesLength() { |
| 204 | return mAngleRulesLength; |
| 205 | } |
| 206 | |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 207 | const std::string& GraphicsEnv::getLayerPaths() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 208 | return mLayerPaths; |
| 209 | } |
| 210 | |
Courtney Goeltzenleuchter | 30ad2ab | 2018-10-30 08:20:44 -0600 | [diff] [blame] | 211 | const std::string& GraphicsEnv::getDebugLayers() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 212 | return mDebugLayers; |
| 213 | } |
| 214 | |
Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 215 | const std::string& GraphicsEnv::getDebugLayersGLES() { |
| 216 | return mDebugLayersGLES; |
| 217 | } |
| 218 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 219 | void GraphicsEnv::setDebugLayers(const std::string layers) { |
| 220 | mDebugLayers = layers; |
| 221 | } |
| 222 | |
Cody Northrop | b9b01b6 | 2018-10-23 13:13:10 -0600 | [diff] [blame] | 223 | void GraphicsEnv::setDebugLayersGLES(const std::string layers) { |
| 224 | mDebugLayersGLES = layers; |
| 225 | } |
| 226 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 227 | android_namespace_t* GraphicsEnv::getDriverNamespace() { |
| 228 | static std::once_flag once; |
| 229 | std::call_once(once, [this]() { |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 230 | if (mDriverPath.empty()) return; |
| 231 | |
| 232 | auto vndkNamespace = android_get_exported_namespace("vndk"); |
| 233 | if (!vndkNamespace) return; |
| 234 | |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 235 | mDriverNamespace = android_create_namespace("gfx driver", |
Peiyong Lin | 2e9c74c | 2018-10-24 11:18:07 -0700 | [diff] [blame] | 236 | mDriverPath.c_str(), // ld_library_path |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 237 | mDriverPath.c_str(), // default_library_path |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 238 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 239 | nullptr, // permitted_when_isolated_path |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 240 | nullptr); |
| 241 | |
| 242 | const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK); |
| 243 | if (llndkLibraries.empty()) { |
| 244 | mDriverNamespace = nullptr; |
| 245 | return; |
| 246 | } |
| 247 | if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) { |
| 248 | ALOGE("Failed to link default namespace[%s]", dlerror()); |
| 249 | mDriverNamespace = nullptr; |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP); |
| 254 | if (vndkspLibraries.empty()) { |
| 255 | mDriverNamespace = nullptr; |
| 256 | return; |
| 257 | } |
| 258 | if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) { |
| 259 | ALOGE("Failed to link vndk namespace[%s]", dlerror()); |
| 260 | mDriverNamespace = nullptr; |
| 261 | return; |
| 262 | } |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 263 | }); |
Yiwei Zhang | 64d8921 | 2018-11-27 19:58:29 -0800 | [diff] [blame] | 264 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 265 | return mDriverNamespace; |
| 266 | } |
| 267 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 268 | android_namespace_t* GraphicsEnv::getAngleNamespace() { |
| 269 | static std::once_flag once; |
| 270 | std::call_once(once, [this]() { |
| 271 | if (mAnglePath.empty()) return; |
| 272 | |
| 273 | mAngleNamespace = android_create_namespace("ANGLE", |
| 274 | nullptr, // ld_library_path |
| 275 | mAnglePath.c_str(), // default_library_path |
| 276 | ANDROID_NAMESPACE_TYPE_SHARED | |
| 277 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 278 | nullptr, // permitted_when_isolated_path |
| 279 | nullptr); |
| 280 | if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default"); |
| 281 | }); |
| 282 | |
| 283 | return mAngleNamespace; |
| 284 | } |
| 285 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 286 | } // namespace android |