blob: 9d7124fc26b10f0e3232951fc158bc20b89ea5af [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
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
17#define LOG_TAG "nativeloader"
18
19#include "public_libraries.h"
20
21#include <dirent.h>
22
23#include <algorithm>
Jooyung Han538f99a2020-03-03 00:46:50 +090024#include <map>
Orion Hodson9b16e342019-10-09 13:29:16 +010025#include <memory>
Jooyung Hancd616d02020-09-01 14:53:23 +090026#include <regex>
27#include <string>
Orion Hodson9b16e342019-10-09 13:29:16 +010028
29#include <android-base/file.h>
30#include <android-base/logging.h>
Nicolas Geoffray0bf5b672021-05-17 11:07:42 +010031#include <android-base/properties.h>
Orion Hodson9b16e342019-10-09 13:29:16 +010032#include <android-base/result.h>
33#include <android-base/strings.h>
34#include <log/log.h>
35
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +010036#if defined(ART_TARGET_ANDROID)
Justin Yun3db26d52019-12-16 14:09:39 +090037#include <android/sysprop/VndkProperties.sysprop.h>
38#endif
39
Orion Hodson9b16e342019-10-09 13:29:16 +010040#include "utils.h"
41
42namespace android::nativeloader {
43
Orion Hodson9b16e342019-10-09 13:29:16 +010044using android::base::ErrnoError;
Orion Hodson9b16e342019-10-09 13:29:16 +010045using android::base::Result;
Jeffrey Huang52575032020-02-11 17:33:45 -080046using internal::ConfigEntry;
47using internal::ParseConfig;
Jooyung Hancd616d02020-09-01 14:53:23 +090048using internal::ParseApexLibrariesConfig;
Jeffrey Huang52575032020-02-11 17:33:45 -080049using std::literals::string_literals::operator""s;
Orion Hodson9b16e342019-10-09 13:29:16 +010050
51namespace {
52
53constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
54constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
55constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
Jooyung Hancd616d02020-09-01 14:53:23 +090056constexpr const char* kApexLibrariesConfigFile = "/linkerconfig/apex.libraries.config.txt";
Orion Hodson9b16e342019-10-09 13:29:16 +010057constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
Jooyung Han26f7d102020-02-22 23:39:23 +090058constexpr const char* kLlndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt";
59constexpr const char* kVndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt";
Orion Hodson9b16e342019-10-09 13:29:16 +010060
Orion Hodson9b16e342019-10-09 13:29:16 +010061
62// TODO(b/130388701): do we need this?
63std::string root_dir() {
64 static const char* android_root_env = getenv("ANDROID_ROOT");
65 return android_root_env != nullptr ? android_root_env : "/system";
66}
67
Justin Yun089c1352020-02-06 16:53:08 +090068std::string vndk_version_str(bool use_product_vndk) {
Jooyung Han26f7d102020-02-22 23:39:23 +090069 if (use_product_vndk) {
70 static std::string product_vndk_version = get_vndk_version(true);
71 return product_vndk_version;
72 } else {
73 static std::string vendor_vndk_version = get_vndk_version(false);
74 return vendor_vndk_version;
Orion Hodson9b16e342019-10-09 13:29:16 +010075 }
Orion Hodson9b16e342019-10-09 13:29:16 +010076}
77
Jooyung Han26f7d102020-02-22 23:39:23 +090078// insert vndk version in every {} placeholder
Justin Yun089c1352020-02-06 16:53:08 +090079void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) {
Orion Hodson9b16e342019-10-09 13:29:16 +010080 CHECK(file_name != nullptr);
Jooyung Han26f7d102020-02-22 23:39:23 +090081 auto version = vndk_version_str(use_product_vndk);
82 size_t pos = file_name->find("{}");
83 while (pos != std::string::npos) {
84 file_name->replace(pos, 2, version);
85 pos = file_name->find("{}", pos + version.size());
Orion Hodson9b16e342019-10-09 13:29:16 +010086 }
Orion Hodson9b16e342019-10-09 13:29:16 +010087}
88
89const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
90 [](const struct ConfigEntry&) -> Result<bool> { return true; };
91
92Result<std::vector<std::string>> ReadConfig(
93 const std::string& configFile,
94 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
95 std::string file_content;
96 if (!base::ReadFileToString(configFile, &file_content)) {
97 return ErrnoError();
98 }
99 Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900100 if (!result.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100101 return Errorf("Cannot parse {}: {}", configFile, result.error().message());
102 }
103 return result;
104}
105
106void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
107 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
108 if (dir != nullptr) {
109 // Failing to opening the dir is not an error, which can happen in
110 // webview_zygote.
111 while (struct dirent* ent = readdir(dir.get())) {
112 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
113 continue;
114 }
115 const std::string filename(ent->d_name);
116 std::string_view fn = filename;
117 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
118 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
119 const std::string company_name(fn);
120 const std::string config_file_path = dirname + "/"s + filename;
121 LOG_ALWAYS_FATAL_IF(
122 company_name.empty(),
123 "Error extracting company name from public native library list file path \"%s\"",
124 config_file_path.c_str());
125
126 auto ret = ReadConfig(
127 config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> {
128 if (android::base::StartsWith(entry.soname, "lib") &&
129 android::base::EndsWith(entry.soname, "." + company_name + ".so")) {
130 return true;
131 } else {
132 return Errorf("Library name \"{}\" does not end with the company name {}.",
133 entry.soname, company_name);
134 }
135 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900136 if (ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100137 sonames->insert(sonames->end(), ret->begin(), ret->end());
138 } else {
139 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
140 config_file_path.c_str(), ret.error().message().c_str());
141 }
142 }
143 }
144 }
145}
146
147static std::string InitDefaultPublicLibraries(bool for_preload) {
148 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
149 auto sonames =
150 ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> {
151 if (for_preload) {
152 return !entry.nopreload;
153 } else {
154 return true;
155 }
156 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900157 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100158 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
159 config_file.c_str(), sonames.error().message().c_str());
160 return "";
161 }
162
Orion Hodson9b16e342019-10-09 13:29:16 +0100163 // If this is for preloading libs, don't remove the libs from APEXes.
164 if (for_preload) {
165 return android::base::Join(*sonames, ':');
166 }
167
Jooyung Hancd616d02020-09-01 14:53:23 +0900168 // Remove the public libs provided by apexes because these libs are available
169 // from apex namespaces.
170 for (const auto& p : apex_public_libraries()) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900171 auto public_libs = base::Split(p.second, ":");
172 sonames->erase(std::remove_if(sonames->begin(), sonames->end(), [&public_libs](const std::string& v) {
173 return std::find(public_libs.begin(), public_libs.end(), v) != public_libs.end();
174 }), sonames->end());
Orion Hodson9b16e342019-10-09 13:29:16 +0100175 }
176 return android::base::Join(*sonames, ':');
177}
178
Orion Hodson9b16e342019-10-09 13:29:16 +0100179static std::string InitVendorPublicLibraries() {
180 // This file is optional, quietly ignore if the file does not exist.
181 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900182 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100183 return "";
184 }
185 return android::base::Join(*sonames, ':');
186}
187
Justin Yun0cc40272019-12-16 16:47:40 +0900188// read /system/etc/public.libraries-<companyname>.txt,
189// /system_ext/etc/public.libraries-<companyname>.txt and
Orion Hodson9b16e342019-10-09 13:29:16 +0100190// /product/etc/public.libraries-<companyname>.txt which contain partner defined
191// system libs that are exposed to apps. The libs in the txt files must be
192// named as lib<name>.<companyname>.so.
193static std::string InitExtendedPublicLibraries() {
194 std::vector<std::string> sonames;
195 ReadExtensionLibraries("/system/etc", &sonames);
Justin Yun0cc40272019-12-16 16:47:40 +0900196 ReadExtensionLibraries("/system_ext/etc", &sonames);
Orion Hodson9b16e342019-10-09 13:29:16 +0100197 ReadExtensionLibraries("/product/etc", &sonames);
198 return android::base::Join(sonames, ':');
199}
200
Justin Yun089c1352020-02-06 16:53:08 +0900201static std::string InitLlndkLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100202 std::string config_file = kLlndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900203 InsertVndkVersionStr(&config_file, false);
204 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocentic1375632020-02-13 10:37:03 +0900205 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900206 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Justin Yun089c1352020-02-06 16:53:08 +0900207 return "";
208 }
209 return android::base::Join(*sonames, ':');
210}
211
212static std::string InitLlndkLibrariesProduct() {
Justin Yun696882f2020-03-24 13:31:19 +0900213 if (!is_product_vndk_version_defined()) {
214 return "";
215 }
Justin Yun089c1352020-02-06 16:53:08 +0900216 std::string config_file = kLlndkLibrariesFile;
217 InsertVndkVersionStr(&config_file, true);
Orion Hodson9b16e342019-10-09 13:29:16 +0100218 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900219 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900220 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100221 return "";
222 }
223 return android::base::Join(*sonames, ':');
224}
225
Justin Yuneb4f08c2020-02-18 11:29:07 +0900226static std::string InitVndkspLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100227 std::string config_file = kVndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900228 InsertVndkVersionStr(&config_file, false);
Orion Hodson9b16e342019-10-09 13:29:16 +0100229 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900230 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100231 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
232 return "";
233 }
234 return android::base::Join(*sonames, ':');
235}
236
Justin Yuneb4f08c2020-02-18 11:29:07 +0900237static std::string InitVndkspLibrariesProduct() {
Justin Yun696882f2020-03-24 13:31:19 +0900238 if (!is_product_vndk_version_defined()) {
239 return "";
240 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900241 std::string config_file = kVndkLibrariesFile;
242 InsertVndkVersionStr(&config_file, true);
243 auto sonames = ReadConfig(config_file, always_true);
244 if (!sonames.ok()) {
245 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
246 return "";
247 }
248 return android::base::Join(*sonames, ':');
249}
250
Jooyung Hancd616d02020-09-01 14:53:23 +0900251static std::map<std::string, std::string> InitApexLibraries(const std::string& tag) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900252 std::string file_content;
Jooyung Hancd616d02020-09-01 14:53:23 +0900253 if (!base::ReadFileToString(kApexLibrariesConfigFile, &file_content)) {
254 // config is optional
Jooyung Han538f99a2020-03-03 00:46:50 +0900255 return {};
256 }
Jooyung Hancd616d02020-09-01 14:53:23 +0900257 auto config = ParseApexLibrariesConfig(file_content, tag);
Jooyung Han538f99a2020-03-03 00:46:50 +0900258 if (!config.ok()) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900259 LOG_ALWAYS_FATAL("%s: %s", kApexLibrariesConfigFile, config.error().message().c_str());
Jooyung Han538f99a2020-03-03 00:46:50 +0900260 return {};
261 }
262 return *config;
263}
264
Jooyung Hancd616d02020-09-01 14:53:23 +0900265struct ApexLibrariesConfigLine {
266 std::string tag;
267 std::string apex_namespace;
268 std::string library_list;
269};
270
271const std::regex kApexNamespaceRegex("[0-9a-zA-Z_]+");
272const std::regex kLibraryListRegex("[0-9a-zA-Z.:@+_-]+");
273
274Result<ApexLibrariesConfigLine> ParseApexLibrariesConfigLine(const std::string& line) {
275 std::vector<std::string> tokens = base::Split(line, " ");
276 if (tokens.size() != 3) {
277 return Errorf("Malformed line \"{}\"", line);
278 }
279 if (tokens[0] != "jni" && tokens[0] != "public") {
280 return Errorf("Invalid tag \"{}\"", line);
281 }
282 if (!std::regex_match(tokens[1], kApexNamespaceRegex)) {
283 return Errorf("Invalid apex_namespace \"{}\"", line);
284 }
285 if (!std::regex_match(tokens[2], kLibraryListRegex)) {
286 return Errorf("Invalid library_list \"{}\"", line);
287 }
288 return ApexLibrariesConfigLine{std::move(tokens[0]), std::move(tokens[1]), std::move(tokens[2])};
289}
290
Orion Hodson9b16e342019-10-09 13:29:16 +0100291} // namespace
292
293const std::string& preloadable_public_libraries() {
294 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true);
295 return list;
296}
297
298const std::string& default_public_libraries() {
299 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false);
300 return list;
301}
302
Orion Hodson9b16e342019-10-09 13:29:16 +0100303const std::string& vendor_public_libraries() {
304 static std::string list = InitVendorPublicLibraries();
305 return list;
306}
307
308const std::string& extended_public_libraries() {
309 static std::string list = InitExtendedPublicLibraries();
310 return list;
311}
312
Justin Yun089c1352020-02-06 16:53:08 +0900313const std::string& llndk_libraries_product() {
314 static std::string list = InitLlndkLibrariesProduct();
315 return list;
316}
317
318const std::string& llndk_libraries_vendor() {
319 static std::string list = InitLlndkLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100320 return list;
321}
322
Justin Yuneb4f08c2020-02-18 11:29:07 +0900323const std::string& vndksp_libraries_product() {
324 static std::string list = InitVndkspLibrariesProduct();
325 return list;
326}
327
328const std::string& vndksp_libraries_vendor() {
329 static std::string list = InitVndkspLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100330 return list;
331}
332
Jooyung Han538f99a2020-03-03 00:46:50 +0900333const std::string& apex_jni_libraries(const std::string& apex_ns_name) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900334 static std::map<std::string, std::string> jni_libraries = InitApexLibraries("jni");
Jooyung Han538f99a2020-03-03 00:46:50 +0900335 return jni_libraries[apex_ns_name];
336}
337
Jooyung Hancd616d02020-09-01 14:53:23 +0900338const std::map<std::string, std::string>& apex_public_libraries() {
339 static std::map<std::string, std::string> public_libraries = InitApexLibraries("public");
340 return public_libraries;
341}
342
Justin Yun3db26d52019-12-16 14:09:39 +0900343bool is_product_vndk_version_defined() {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100344#if defined(ART_TARGET_ANDROID)
Justin Yun3db26d52019-12-16 14:09:39 +0900345 return android::sysprop::VndkProperties::product_vndk_version().has_value();
346#else
347 return false;
348#endif
349}
350
Justin Yun089c1352020-02-06 16:53:08 +0900351std::string get_vndk_version(bool is_product_vndk) {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100352#if defined(ART_TARGET_ANDROID)
Justin Yun089c1352020-02-06 16:53:08 +0900353 if (is_product_vndk) {
354 return android::sysprop::VndkProperties::product_vndk_version().value_or("");
355 }
356 return android::sysprop::VndkProperties::vendor_vndk_version().value_or("");
357#else
358 if (is_product_vndk) {
359 return android::base::GetProperty("ro.product.vndk.version", "");
360 }
361 return android::base::GetProperty("ro.vndk.version", "");
362#endif
363}
364
Orion Hodson9b16e342019-10-09 13:29:16 +0100365namespace internal {
366// Exported for testing
367Result<std::vector<std::string>> ParseConfig(
368 const std::string& file_content,
369 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
370 std::vector<std::string> lines = base::Split(file_content, "\n");
371
372 std::vector<std::string> sonames;
373 for (auto& line : lines) {
374 auto trimmed_line = base::Trim(line);
375 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
376 continue;
377 }
378
379 std::vector<std::string> tokens = android::base::Split(trimmed_line, " ");
380 if (tokens.size() < 1 || tokens.size() > 3) {
381 return Errorf("Malformed line \"{}\"", line);
382 }
383 struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL};
384 size_t i = tokens.size();
385 while (i-- > 0) {
386 if (tokens[i] == "nopreload") {
387 entry.nopreload = true;
388 } else if (tokens[i] == "32" || tokens[i] == "64") {
389 if (entry.bitness != ALL) {
390 return Errorf("Malformed line \"{}\": bitness can be specified only once", line);
391 }
392 entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64;
393 } else {
394 if (i != 0) {
395 return Errorf("Malformed line \"{}\"", line);
396 }
397 entry.soname = tokens[i];
398 }
399 }
400
401 // skip 32-bit lib on 64-bit process and vice versa
402#if defined(__LP64__)
403 if (entry.bitness == ONLY_32) continue;
404#else
405 if (entry.bitness == ONLY_64) continue;
406#endif
407
408 Result<bool> ret = filter_fn(entry);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900409 if (!ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100410 return ret.error();
411 }
412 if (*ret) {
413 // filter_fn has returned true.
414 sonames.push_back(entry.soname);
415 }
416 }
417 return sonames;
418}
419
Jooyung Hancd616d02020-09-01 14:53:23 +0900420// Parses apex.libraries.config.txt file generated by linkerconfig which looks like
421// system/linkerconfig/testdata/golden_output/stages/apex.libraries.config.txt
422// and returns mapping of <apex namespace> to <library list> which matches <tag>.
423//
424// The file is line-based and each line consists of "<tag> <apex namespace> <library list>".
425//
426// <tag> explains what <library list> is. (e.g "jni", "public")
427// <library list> is colon-separated list of library names. (e.g "libfoo.so:libbar.so")
428//
429// If <tag> is "jni", <library list> is the list of JNI libraries exposed by <apex namespace>.
430// If <tag> is "public", <library list> is the list of public libraries exposed by <apex namespace>.
431// Public libraries are the libs listed in /system/etc/public.libraries.txt.
432Result<std::map<std::string, std::string>> ParseApexLibrariesConfig(const std::string& file_content, const std::string& tag) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900433 std::map<std::string, std::string> entries;
434 std::vector<std::string> lines = base::Split(file_content, "\n");
435 for (auto& line : lines) {
436 auto trimmed_line = base::Trim(line);
437 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
438 continue;
439 }
Jooyung Hancd616d02020-09-01 14:53:23 +0900440 auto config_line = ParseApexLibrariesConfigLine(trimmed_line);
441 if (!config_line.ok()) {
442 return config_line.error();
Jooyung Han538f99a2020-03-03 00:46:50 +0900443 }
Jooyung Hancd616d02020-09-01 14:53:23 +0900444 if (config_line->tag != tag) {
445 continue;
446 }
447 entries[config_line->apex_namespace] = config_line->library_list;
448 }
Jooyung Han538f99a2020-03-03 00:46:50 +0900449 return entries;
450}
451
Orion Hodson9b16e342019-10-09 13:29:16 +0100452} // namespace internal
453
454} // namespace android::nativeloader