blob: c2a67643383e879a376aeb58296857cd47ab2130 [file] [log] [blame]
Jesse Hall90b25ed2016-12-12 12:56:46 -08001/*
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 Park27c39e12017-05-08 13:00:02 +090019#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080020
Yiwei Zhang64d89212018-11-27 19:58:29 -080021#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 Northrop629ce4e2018-10-15 07:22:09 -060029#include <sys/prctl.h>
30
Jesse Hall53457db2016-12-14 16:54:06 -080031#include <mutex>
32
Jesse Hall57de0ff2017-05-05 16:41:35 -070033// TODO(b/37049319) Get this from a header once one exists
34extern "C" {
Yiwei Zhang64d89212018-11-27 19:58:29 -080035android_namespace_t* android_get_exported_namespace(const char*);
36android_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);
40bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to,
41 const char* shared_libs_sonames);
Jiyong Park9b816a82018-01-02 17:37:37 +090042
Yiwei Zhang64d89212018-11-27 19:58:29 -080043enum {
44 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
45 ANDROID_NAMESPACE_TYPE_SHARED = 2,
46};
Jesse Hall57de0ff2017-05-05 16:41:35 -070047}
48
Jesse Hall90b25ed2016-12-12 12:56:46 -080049namespace android {
50
Yiwei Zhang64d89212018-11-27 19:58:29 -080051enum NativeLibrary {
52 LLNDK = 0,
53 VNDKSP = 1,
54};
55
56static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
57 "/etc/vndksp.libraries.txt"};
58
59static 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
69static 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
78static 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
97static 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 Hall90b25ed2016-12-12 12:56:46 -0800114/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
115 static GraphicsEnv env;
116 return env;
117}
118
Cody Northrop629ce4e2018-10-15 07:22:09 -0600119int 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 Hall90b25ed2016-12-12 12:56:46 -0800127void GraphicsEnv::setDriverPath(const std::string path) {
128 if (!mDriverPath.empty()) {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800129 ALOGV("ignoring attempt to change driver path from '%s' to '%s'", mDriverPath.c_str(),
130 path.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800131 return;
132 }
133 ALOGV("setting driver path to '%s'", path.c_str());
134 mDriverPath = path;
135}
136
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600137void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Cody Northropf0874d32018-10-29 10:59:45 -0600138 bool developerOptIn, const int rulesFd, const long rulesOffset,
Cody Northrop04e70432018-09-06 10:34:58 -0600139 const long rulesLength) {
Cody Northrop1f00e172018-04-02 11:23:31 -0600140 if (!mAnglePath.empty()) {
141 ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(),
142 path.c_str());
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600143 } else {
144 ALOGV("setting ANGLE path to '%s'", path.c_str());
145 mAnglePath = path;
Cody Northrop1f00e172018-04-02 11:23:31 -0600146 }
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600147
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 Northrop04e70432018-09-06 10:34:58 -0600157
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 Northrop1f00e172018-04-02 11:23:31 -0600164}
165
Victor Khimenko4819b522018-07-13 17:24:18 +0200166void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600167 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 Zhang64d89212018-11-27 19:58:29 -0800172 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600173 }
174}
175
Victor Khimenko4819b522018-07-13 17:24:18 +0200176NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600177 return mAppNamespace;
178}
179
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600180const char* GraphicsEnv::getAngleAppName() {
181 if (mAngleAppName.empty()) return nullptr;
182 return mAngleAppName.c_str();
183}
184
185bool GraphicsEnv::getAngleDeveloperOptIn() {
186 return mAngleDeveloperOptIn;
187}
188
Cody Northrop04e70432018-09-06 10:34:58 -0600189int GraphicsEnv::getAngleRulesFd() {
190 return mAngleRulesFd;
191}
192
193long GraphicsEnv::getAngleRulesOffset() {
194 return mAngleRulesOffset;
195}
196
197long GraphicsEnv::getAngleRulesLength() {
198 return mAngleRulesLength;
199}
200
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600201const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600202 return mLayerPaths;
203}
204
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600205const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600206 return mDebugLayers;
207}
208
Cody Northropb9b01b62018-10-23 13:13:10 -0600209const std::string& GraphicsEnv::getDebugLayersGLES() {
210 return mDebugLayersGLES;
211}
212
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600213void GraphicsEnv::setDebugLayers(const std::string layers) {
214 mDebugLayers = layers;
215}
216
Cody Northropb9b01b62018-10-23 13:13:10 -0600217void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
218 mDebugLayersGLES = layers;
219}
220
Jesse Hall53457db2016-12-14 16:54:06 -0800221android_namespace_t* GraphicsEnv::getDriverNamespace() {
222 static std::once_flag once;
223 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800224 if (mDriverPath.empty()) return;
225
226 auto vndkNamespace = android_get_exported_namespace("vndk");
227 if (!vndkNamespace) return;
228
Jesse Hall57de0ff2017-05-05 16:41:35 -0700229 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700230 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700231 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800232 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700233 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800234 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 Hall53457db2016-12-14 16:54:06 -0800257 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800258
Jesse Hall53457db2016-12-14 16:54:06 -0800259 return mDriverNamespace;
260}
261
Cody Northrop1f00e172018-04-02 11:23:31 -0600262android_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 Hall90b25ed2016-12-12 12:56:46 -0800280} // namespace android