blob: 10e23fdf10e3e08e8bd59ab0afa6592519563048 [file] [log] [blame]
Jiyong Park40a60772019-05-03 16:21:31 +09001/*
2 * Copyright (C) 2019 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
Jiyong Park40a60772019-05-03 16:21:31 +090017#define LOG_TAG "nativeloader"
18
Jiyong Park16a98962019-05-04 03:10:48 +090019#include "public_libraries.h"
20
Jiyong Park40a60772019-05-03 16:21:31 +090021#include <dirent.h>
22
23#include <algorithm>
24#include <memory>
25
Jiyong Park16a98962019-05-04 03:10:48 +090026#include <android-base/file.h>
27#include <android-base/logging.h>
28#include <android-base/properties.h>
29#include <android-base/strings.h>
30#include <log/log.h>
31
Jiyong Parkf8802e52019-05-03 16:34:56 +090032#include "utils.h"
Jiyong Park40a60772019-05-03 16:21:31 +090033
34namespace android::nativeloader {
35
36using namespace std::string_literals;
37
38namespace {
Jiyong Park5b8b3062019-05-03 18:11:49 +090039
40constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
41constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
42constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
43constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
44constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
45constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
Jiyong Park40a60772019-05-03 16:21:31 +090046
47const std::vector<const std::string> kRuntimePublicLibraries = {
48 "libicuuc.so",
49 "libicui18n.so",
50};
51
Jiyong Parkf8802e52019-05-03 16:34:56 +090052constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB;
Jiyong Park40a60772019-05-03 16:21:31 +090053
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +010054constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so";
55
Jiyong Park5b8b3062019-05-03 18:11:49 +090056// TODO(b/130388701): do we need this?
Jiyong Park40a60772019-05-03 16:21:31 +090057std::string root_dir() {
58 static const char* android_root_env = getenv("ANDROID_ROOT");
59 return android_root_env != nullptr ? android_root_env : "/system";
60}
61
62bool debuggable() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090063 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park40a60772019-05-03 16:21:31 +090064 return debuggable;
65}
66
67std::string vndk_version_str() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090068 static std::string version = android::base::GetProperty("ro.vndk.version", "");
Jiyong Park40a60772019-05-03 16:21:31 +090069 if (version != "" && version != "current") {
70 return "." + version;
71 }
72 return "";
73}
74
Jiyong Park4b5a37c2019-05-09 17:17:37 +090075// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
76// variable to add libraries to the list. This is intended for platform tests only.
77std::string additional_public_libraries() {
78 if (debuggable()) {
79 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
80 return val ? val : "";
81 }
82 return "";
83}
84
Jiyong Park5b8b3062019-05-03 18:11:49 +090085void InsertVndkVersionStr(std::string* file_name) {
Jiyong Park40a60772019-05-03 16:21:31 +090086 CHECK(file_name != nullptr);
87 size_t insert_pos = file_name->find_last_of(".");
88 if (insert_pos == std::string::npos) {
89 insert_pos = file_name->length();
90 }
91 file_name->insert(insert_pos, vndk_version_str());
92}
93
94const std::function<bool(const std::string&, std::string*)> always_true =
95 [](const std::string&, std::string*) { return true; };
96
97bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
98 const std::function<bool(const std::string& /* soname */,
99 std::string* /* error_msg */)>& check_soname,
100 std::string* error_msg = nullptr) {
101 // Read list of public native libraries from the config file.
102 std::string file_content;
103 if (!base::ReadFileToString(configFile, &file_content)) {
104 if (error_msg) *error_msg = strerror(errno);
105 return false;
106 }
107
108 std::vector<std::string> lines = base::Split(file_content, "\n");
109
110 for (auto& line : lines) {
111 auto trimmed_line = base::Trim(line);
112 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
113 continue;
114 }
115 size_t space_pos = trimmed_line.rfind(' ');
116 if (space_pos != std::string::npos) {
117 std::string type = trimmed_line.substr(space_pos + 1);
118 if (type != "32" && type != "64") {
119 if (error_msg) *error_msg = "Malformed line: " + line;
120 return false;
121 }
122#if defined(__LP64__)
123 // Skip 32 bit public library.
124 if (type == "32") {
125 continue;
126 }
127#else
128 // Skip 64 bit public library.
129 if (type == "64") {
130 continue;
131 }
132#endif
133 trimmed_line.resize(space_pos);
134 }
135
136 if (check_soname(trimmed_line, error_msg)) {
137 sonames->push_back(trimmed_line);
138 } else {
139 return false;
140 }
141 }
142 return true;
143}
144
145void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
146 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
147 if (dir != nullptr) {
148 // Failing to opening the dir is not an error, which can happen in
149 // webview_zygote.
150 while (struct dirent* ent = readdir(dir.get())) {
151 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
152 continue;
153 }
154 const std::string filename(ent->d_name);
Jiyong Park5b8b3062019-05-03 18:11:49 +0900155 std::string_view fn = filename;
156 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
157 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
158 const std::string company_name(fn);
Jiyong Park40a60772019-05-03 16:21:31 +0900159 const std::string config_file_path = dirname + "/"s + filename;
160 LOG_ALWAYS_FATAL_IF(
161 company_name.empty(),
162 "Error extracting company name from public native library list file path \"%s\"",
163 config_file_path.c_str());
164
165 std::string error_msg;
166
167 LOG_ALWAYS_FATAL_IF(
168 !ReadConfig(config_file_path, sonames,
169 [&company_name](const std::string& soname, std::string* error_msg) {
170 if (android::base::StartsWith(soname, "lib") &&
171 android::base::EndsWith(soname, "." + company_name + ".so")) {
172 return true;
173 } else {
174 *error_msg = "Library name \"" + soname +
175 "\" does not end with the company name: " + company_name +
176 ".";
177 return false;
178 }
179 },
180 &error_msg),
181 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
182 error_msg.c_str());
183 }
184 }
185 }
186}
187
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900188static std::string InitDefaultPublicLibraries() {
189 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
190 std::vector<std::string> sonames;
191 std::string error_msg;
192 LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg),
193 "Error reading public native library list from \"%s\": %s",
194 config_file.c_str(), error_msg.c_str());
Jiyong Park40a60772019-05-03 16:21:31 +0900195
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900196 std::string additional_libs = additional_public_libraries();
197 if (!additional_libs.empty()) {
198 auto vec = base::Split(additional_libs, ":");
199 std::copy(vec.begin(), vec.end(), std::back_inserter(sonames));
200 }
Jiyong Park40a60772019-05-03 16:21:31 +0900201
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900202 // Remove the public libs in the runtime namespace.
203 // These libs are listed in public.android.txt, but we don't want the rest of android
204 // in default namespace to dlopen the libs.
205 // For example, libicuuc.so is exposed to classloader namespace from runtime namespace.
206 // Unfortunately, it does not have stable C symbols, and default namespace should only use
207 // stable symbols in libandroidicu.so. http://b/120786417
208 for (const std::string& lib_name : kRuntimePublicLibraries) {
209 std::string path(kRuntimeApexLibPath);
210 path.append("/").append(lib_name);
211
212 struct stat s;
213 // Do nothing if the path in /apex does not exist.
214 // Runtime APEX must be mounted since libnativeloader is in the same APEX
215 if (stat(path.c_str(), &s) != 0) {
216 continue;
Jiyong Park40a60772019-05-03 16:21:31 +0900217 }
218
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900219 auto it = std::find(sonames.begin(), sonames.end(), lib_name);
220 if (it != sonames.end()) {
221 sonames.erase(it);
Jiyong Park40a60772019-05-03 16:21:31 +0900222 }
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900223 }
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100224
225 // Remove the public libs in the nnapi namespace.
226 auto it = std::find(sonames.begin(), sonames.end(), kNeuralNetworksApexPublicLibrary);
227 if (it != sonames.end()) {
228 sonames.erase(it);
229 }
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900230 return android::base::Join(sonames, ':');
231}
232
233static std::string InitRuntimePublicLibraries() {
234 CHECK(sizeof(kRuntimePublicLibraries) > 0);
235 std::string list = android::base::Join(kRuntimePublicLibraries, ":");
236
237 std::string additional_libs = additional_public_libraries();
238 if (!additional_libs.empty()) {
239 list = list + ':' + additional_libs;
Jiyong Park40a60772019-05-03 16:21:31 +0900240 }
241 return list;
242}
243
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900244static std::string InitVendorPublicLibraries() {
245 // This file is optional, quietly ignore if the file does not exist.
246 std::vector<std::string> sonames;
247 ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr);
248 return android::base::Join(sonames, ':');
Jiyong Park40a60772019-05-03 16:21:31 +0900249}
250
Jiyong Park5b8b3062019-05-03 18:11:49 +0900251// read /system/etc/public.libraries-<companyname>.txt and
252// /product/etc/public.libraries-<companyname>.txt which contain partner defined
Jiyong Park40a60772019-05-03 16:21:31 +0900253// system libs that are exposed to apps. The libs in the txt files must be
254// named as lib<name>.<companyname>.so.
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900255static std::string InitExtendedPublicLibraries() {
256 std::vector<std::string> sonames;
257 ReadExtensionLibraries("/system/etc", &sonames);
258 ReadExtensionLibraries("/product/etc", &sonames);
259 return android::base::Join(sonames, ':');
260}
261
262static std::string InitLlndkLibraries() {
263 std::string config_file = kLlndkLibrariesFile;
264 InsertVndkVersionStr(&config_file);
265 std::vector<std::string> sonames;
266 ReadConfig(config_file, &sonames, always_true, nullptr);
267 return android::base::Join(sonames, ':');
268}
269
270static std::string InitVndkspLibraries() {
271 std::string config_file = kVndkLibrariesFile;
272 InsertVndkVersionStr(&config_file);
273 std::vector<std::string> sonames;
274 ReadConfig(config_file, &sonames, always_true, nullptr);
275 return android::base::Join(sonames, ':');
276}
277
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100278static std::string InitNeuralNetworksPublicLibraries() {
279 return kNeuralNetworksApexPublicLibrary;
280}
281
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900282} // namespace
283
284const std::string& default_public_libraries() {
285 static std::string list = InitDefaultPublicLibraries();
286 return list;
287}
288
289const std::string& runtime_public_libraries() {
290 static std::string list = InitRuntimePublicLibraries();
291 return list;
292}
293
294const std::string& vendor_public_libraries() {
295 static std::string list = InitVendorPublicLibraries();
296 return list;
297}
298
Jiyong Park5b8b3062019-05-03 18:11:49 +0900299const std::string& extended_public_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900300 static std::string list = InitExtendedPublicLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900301 return list;
302}
303
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100304const std::string& neuralnetworks_public_libraries() {
305 static std::string list = InitNeuralNetworksPublicLibraries();
306 return list;
307}
308
Jiyong Park5b8b3062019-05-03 18:11:49 +0900309const std::string& llndk_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900310 static std::string list = InitLlndkLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900311 return list;
312}
313
Jiyong Park5b8b3062019-05-03 18:11:49 +0900314const std::string& vndksp_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900315 static std::string list = InitVndkspLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900316 return list;
317}
318
319} // namespace android::nativeloader