blob: dfdda0c2f4119a56ca2a22b61ca1b5812bd4bd81 [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,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700138 const std::string developerOptIn, const int rulesFd,
139 const long rulesOffset, 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
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700156 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 Northrop04e70432018-09-06 10:34:58 -0600163
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 Northrop1f00e172018-04-02 11:23:31 -0600170}
171
Victor Khimenko4819b522018-07-13 17:24:18 +0200172void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600173 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 Zhang64d89212018-11-27 19:58:29 -0800178 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600179 }
180}
181
Victor Khimenko4819b522018-07-13 17:24:18 +0200182NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600183 return mAppNamespace;
184}
185
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600186const char* GraphicsEnv::getAngleAppName() {
187 if (mAngleAppName.empty()) return nullptr;
188 return mAngleAppName.c_str();
189}
190
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700191const char* GraphicsEnv::getAngleDeveloperOptIn() {
192 return mAngleDeveloperOptIn.c_str();
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600193}
194
Cody Northrop04e70432018-09-06 10:34:58 -0600195int GraphicsEnv::getAngleRulesFd() {
196 return mAngleRulesFd;
197}
198
199long GraphicsEnv::getAngleRulesOffset() {
200 return mAngleRulesOffset;
201}
202
203long GraphicsEnv::getAngleRulesLength() {
204 return mAngleRulesLength;
205}
206
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600207const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600208 return mLayerPaths;
209}
210
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600211const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600212 return mDebugLayers;
213}
214
Cody Northropb9b01b62018-10-23 13:13:10 -0600215const std::string& GraphicsEnv::getDebugLayersGLES() {
216 return mDebugLayersGLES;
217}
218
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600219void GraphicsEnv::setDebugLayers(const std::string layers) {
220 mDebugLayers = layers;
221}
222
Cody Northropb9b01b62018-10-23 13:13:10 -0600223void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
224 mDebugLayersGLES = layers;
225}
226
Jesse Hall53457db2016-12-14 16:54:06 -0800227android_namespace_t* GraphicsEnv::getDriverNamespace() {
228 static std::once_flag once;
229 std::call_once(once, [this]() {
Yiwei Zhang64d89212018-11-27 19:58:29 -0800230 if (mDriverPath.empty()) return;
231
232 auto vndkNamespace = android_get_exported_namespace("vndk");
233 if (!vndkNamespace) return;
234
Jesse Hall57de0ff2017-05-05 16:41:35 -0700235 mDriverNamespace = android_create_namespace("gfx driver",
Peiyong Lin2e9c74c2018-10-24 11:18:07 -0700236 mDriverPath.c_str(), // ld_library_path
Jesse Hall57de0ff2017-05-05 16:41:35 -0700237 mDriverPath.c_str(), // default_library_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800238 ANDROID_NAMESPACE_TYPE_ISOLATED,
Jesse Hall57de0ff2017-05-05 16:41:35 -0700239 nullptr, // permitted_when_isolated_path
Yiwei Zhang64d89212018-11-27 19:58:29 -0800240 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 Hall53457db2016-12-14 16:54:06 -0800263 });
Yiwei Zhang64d89212018-11-27 19:58:29 -0800264
Jesse Hall53457db2016-12-14 16:54:06 -0800265 return mDriverNamespace;
266}
267
Cody Northrop1f00e172018-04-02 11:23:31 -0600268android_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 Hall90b25ed2016-12-12 12:56:46 -0800286} // namespace android