blob: 55714167e653ad2b3c618383767f70380387ee24 [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";
Justin Yuna21b5842021-08-10 14:05:49 +090058constexpr const char* kProductPublicLibrariesFile = "/product/etc/public.libraries.txt";
Jooyung Han26f7d102020-02-22 23:39:23 +090059constexpr const char* kLlndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt";
60constexpr const char* kVndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt";
Orion Hodson9b16e342019-10-09 13:29:16 +010061
Orion Hodson9b16e342019-10-09 13:29:16 +010062
63// TODO(b/130388701): do we need this?
64std::string root_dir() {
65 static const char* android_root_env = getenv("ANDROID_ROOT");
66 return android_root_env != nullptr ? android_root_env : "/system";
67}
68
Justin Yun089c1352020-02-06 16:53:08 +090069std::string vndk_version_str(bool use_product_vndk) {
Jooyung Han26f7d102020-02-22 23:39:23 +090070 if (use_product_vndk) {
71 static std::string product_vndk_version = get_vndk_version(true);
72 return product_vndk_version;
73 } else {
74 static std::string vendor_vndk_version = get_vndk_version(false);
75 return vendor_vndk_version;
Orion Hodson9b16e342019-10-09 13:29:16 +010076 }
Orion Hodson9b16e342019-10-09 13:29:16 +010077}
78
Jooyung Han26f7d102020-02-22 23:39:23 +090079// insert vndk version in every {} placeholder
Justin Yun089c1352020-02-06 16:53:08 +090080void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) {
Orion Hodson9b16e342019-10-09 13:29:16 +010081 CHECK(file_name != nullptr);
Jooyung Han26f7d102020-02-22 23:39:23 +090082 auto version = vndk_version_str(use_product_vndk);
83 size_t pos = file_name->find("{}");
84 while (pos != std::string::npos) {
85 file_name->replace(pos, 2, version);
86 pos = file_name->find("{}", pos + version.size());
Orion Hodson9b16e342019-10-09 13:29:16 +010087 }
Orion Hodson9b16e342019-10-09 13:29:16 +010088}
89
90const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
91 [](const struct ConfigEntry&) -> Result<bool> { return true; };
92
93Result<std::vector<std::string>> ReadConfig(
94 const std::string& configFile,
95 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
96 std::string file_content;
97 if (!base::ReadFileToString(configFile, &file_content)) {
98 return ErrnoError();
99 }
100 Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900101 if (!result.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100102 return Errorf("Cannot parse {}: {}", configFile, result.error().message());
103 }
104 return result;
105}
106
107void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
108 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
109 if (dir != nullptr) {
110 // Failing to opening the dir is not an error, which can happen in
111 // webview_zygote.
112 while (struct dirent* ent = readdir(dir.get())) {
113 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
114 continue;
115 }
116 const std::string filename(ent->d_name);
117 std::string_view fn = filename;
118 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
119 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
120 const std::string company_name(fn);
121 const std::string config_file_path = dirname + "/"s + filename;
122 LOG_ALWAYS_FATAL_IF(
123 company_name.empty(),
124 "Error extracting company name from public native library list file path \"%s\"",
125 config_file_path.c_str());
126
127 auto ret = ReadConfig(
128 config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> {
129 if (android::base::StartsWith(entry.soname, "lib") &&
130 android::base::EndsWith(entry.soname, "." + company_name + ".so")) {
131 return true;
132 } else {
133 return Errorf("Library name \"{}\" does not end with the company name {}.",
134 entry.soname, company_name);
135 }
136 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900137 if (ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100138 sonames->insert(sonames->end(), ret->begin(), ret->end());
139 } else {
140 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
141 config_file_path.c_str(), ret.error().message().c_str());
142 }
143 }
144 }
145 }
146}
147
148static std::string InitDefaultPublicLibraries(bool for_preload) {
149 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
150 auto sonames =
151 ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> {
152 if (for_preload) {
153 return !entry.nopreload;
154 } else {
155 return true;
156 }
157 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900158 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100159 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
160 config_file.c_str(), sonames.error().message().c_str());
161 return "";
162 }
163
Orion Hodson9b16e342019-10-09 13:29:16 +0100164 // If this is for preloading libs, don't remove the libs from APEXes.
165 if (for_preload) {
166 return android::base::Join(*sonames, ':');
167 }
168
Jooyung Hancd616d02020-09-01 14:53:23 +0900169 // Remove the public libs provided by apexes because these libs are available
170 // from apex namespaces.
171 for (const auto& p : apex_public_libraries()) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900172 auto public_libs = base::Split(p.second, ":");
173 sonames->erase(std::remove_if(sonames->begin(), sonames->end(), [&public_libs](const std::string& v) {
174 return std::find(public_libs.begin(), public_libs.end(), v) != public_libs.end();
175 }), sonames->end());
Orion Hodson9b16e342019-10-09 13:29:16 +0100176 }
177 return android::base::Join(*sonames, ':');
178}
179
Orion Hodson9b16e342019-10-09 13:29:16 +0100180static std::string InitVendorPublicLibraries() {
181 // This file is optional, quietly ignore if the file does not exist.
182 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900183 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100184 return "";
185 }
186 return android::base::Join(*sonames, ':');
187}
188
Justin Yuna21b5842021-08-10 14:05:49 +0900189static std::string InitProductPublicLibraries() {
190 // This file is optional, quietly ignore if the file does not exist.
191 auto sonames = ReadConfig(kProductPublicLibrariesFile, always_true);
192 if (!sonames.ok()) {
193 return "";
194 }
195 return android::base::Join(*sonames, ':');
196}
197
Justin Yun0cc40272019-12-16 16:47:40 +0900198// read /system/etc/public.libraries-<companyname>.txt,
199// /system_ext/etc/public.libraries-<companyname>.txt and
Orion Hodson9b16e342019-10-09 13:29:16 +0100200// /product/etc/public.libraries-<companyname>.txt which contain partner defined
201// system libs that are exposed to apps. The libs in the txt files must be
202// named as lib<name>.<companyname>.so.
203static std::string InitExtendedPublicLibraries() {
204 std::vector<std::string> sonames;
205 ReadExtensionLibraries("/system/etc", &sonames);
Justin Yun0cc40272019-12-16 16:47:40 +0900206 ReadExtensionLibraries("/system_ext/etc", &sonames);
Justin Yuna21b5842021-08-10 14:05:49 +0900207 if (!is_product_vndk_version_defined()) {
208 ReadExtensionLibraries("/product/etc", &sonames);
209 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100210 return android::base::Join(sonames, ':');
211}
212
Justin Yun089c1352020-02-06 16:53:08 +0900213static std::string InitLlndkLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100214 std::string config_file = kLlndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900215 InsertVndkVersionStr(&config_file, false);
216 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocentic1375632020-02-13 10:37:03 +0900217 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900218 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Justin Yun089c1352020-02-06 16:53:08 +0900219 return "";
220 }
221 return android::base::Join(*sonames, ':');
222}
223
224static std::string InitLlndkLibrariesProduct() {
Justin Yun696882f2020-03-24 13:31:19 +0900225 if (!is_product_vndk_version_defined()) {
226 return "";
227 }
Justin Yun089c1352020-02-06 16:53:08 +0900228 std::string config_file = kLlndkLibrariesFile;
229 InsertVndkVersionStr(&config_file, true);
Orion Hodson9b16e342019-10-09 13:29:16 +0100230 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900231 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900232 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100233 return "";
234 }
235 return android::base::Join(*sonames, ':');
236}
237
Justin Yuneb4f08c2020-02-18 11:29:07 +0900238static std::string InitVndkspLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100239 std::string config_file = kVndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900240 InsertVndkVersionStr(&config_file, false);
Orion Hodson9b16e342019-10-09 13:29:16 +0100241 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900242 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100243 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
244 return "";
245 }
246 return android::base::Join(*sonames, ':');
247}
248
Justin Yuneb4f08c2020-02-18 11:29:07 +0900249static std::string InitVndkspLibrariesProduct() {
Justin Yun696882f2020-03-24 13:31:19 +0900250 if (!is_product_vndk_version_defined()) {
251 return "";
252 }
Justin Yuneb4f08c2020-02-18 11:29:07 +0900253 std::string config_file = kVndkLibrariesFile;
254 InsertVndkVersionStr(&config_file, true);
255 auto sonames = ReadConfig(config_file, always_true);
256 if (!sonames.ok()) {
257 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
258 return "";
259 }
260 return android::base::Join(*sonames, ':');
261}
262
Jooyung Hancd616d02020-09-01 14:53:23 +0900263static std::map<std::string, std::string> InitApexLibraries(const std::string& tag) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900264 std::string file_content;
Jooyung Hancd616d02020-09-01 14:53:23 +0900265 if (!base::ReadFileToString(kApexLibrariesConfigFile, &file_content)) {
266 // config is optional
Jooyung Han538f99a2020-03-03 00:46:50 +0900267 return {};
268 }
Jooyung Hancd616d02020-09-01 14:53:23 +0900269 auto config = ParseApexLibrariesConfig(file_content, tag);
Jooyung Han538f99a2020-03-03 00:46:50 +0900270 if (!config.ok()) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900271 LOG_ALWAYS_FATAL("%s: %s", kApexLibrariesConfigFile, config.error().message().c_str());
Jooyung Han538f99a2020-03-03 00:46:50 +0900272 return {};
273 }
274 return *config;
275}
276
Jooyung Hancd616d02020-09-01 14:53:23 +0900277struct ApexLibrariesConfigLine {
278 std::string tag;
279 std::string apex_namespace;
280 std::string library_list;
281};
282
283const std::regex kApexNamespaceRegex("[0-9a-zA-Z_]+");
284const std::regex kLibraryListRegex("[0-9a-zA-Z.:@+_-]+");
285
286Result<ApexLibrariesConfigLine> ParseApexLibrariesConfigLine(const std::string& line) {
287 std::vector<std::string> tokens = base::Split(line, " ");
288 if (tokens.size() != 3) {
289 return Errorf("Malformed line \"{}\"", line);
290 }
291 if (tokens[0] != "jni" && tokens[0] != "public") {
292 return Errorf("Invalid tag \"{}\"", line);
293 }
294 if (!std::regex_match(tokens[1], kApexNamespaceRegex)) {
295 return Errorf("Invalid apex_namespace \"{}\"", line);
296 }
297 if (!std::regex_match(tokens[2], kLibraryListRegex)) {
298 return Errorf("Invalid library_list \"{}\"", line);
299 }
300 return ApexLibrariesConfigLine{std::move(tokens[0]), std::move(tokens[1]), std::move(tokens[2])};
301}
302
Orion Hodson9b16e342019-10-09 13:29:16 +0100303} // namespace
304
305const std::string& preloadable_public_libraries() {
306 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true);
307 return list;
308}
309
310const std::string& default_public_libraries() {
311 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false);
312 return list;
313}
314
Orion Hodson9b16e342019-10-09 13:29:16 +0100315const std::string& vendor_public_libraries() {
316 static std::string list = InitVendorPublicLibraries();
317 return list;
318}
319
Justin Yuna21b5842021-08-10 14:05:49 +0900320const std::string& product_public_libraries() {
321 static std::string list = InitProductPublicLibraries();
322 return list;
323}
324
Orion Hodson9b16e342019-10-09 13:29:16 +0100325const std::string& extended_public_libraries() {
326 static std::string list = InitExtendedPublicLibraries();
327 return list;
328}
329
Justin Yun089c1352020-02-06 16:53:08 +0900330const std::string& llndk_libraries_product() {
331 static std::string list = InitLlndkLibrariesProduct();
332 return list;
333}
334
335const std::string& llndk_libraries_vendor() {
336 static std::string list = InitLlndkLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100337 return list;
338}
339
Justin Yuneb4f08c2020-02-18 11:29:07 +0900340const std::string& vndksp_libraries_product() {
341 static std::string list = InitVndkspLibrariesProduct();
342 return list;
343}
344
345const std::string& vndksp_libraries_vendor() {
346 static std::string list = InitVndkspLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100347 return list;
348}
349
Jooyung Han538f99a2020-03-03 00:46:50 +0900350const std::string& apex_jni_libraries(const std::string& apex_ns_name) {
Jooyung Hancd616d02020-09-01 14:53:23 +0900351 static std::map<std::string, std::string> jni_libraries = InitApexLibraries("jni");
Jooyung Han538f99a2020-03-03 00:46:50 +0900352 return jni_libraries[apex_ns_name];
353}
354
Jooyung Hancd616d02020-09-01 14:53:23 +0900355const std::map<std::string, std::string>& apex_public_libraries() {
356 static std::map<std::string, std::string> public_libraries = InitApexLibraries("public");
357 return public_libraries;
358}
359
Justin Yun3db26d52019-12-16 14:09:39 +0900360bool is_product_vndk_version_defined() {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100361#if defined(ART_TARGET_ANDROID)
Justin Yun3db26d52019-12-16 14:09:39 +0900362 return android::sysprop::VndkProperties::product_vndk_version().has_value();
363#else
364 return false;
365#endif
366}
367
Justin Yun089c1352020-02-06 16:53:08 +0900368std::string get_vndk_version(bool is_product_vndk) {
Nicolas Geoffray7ca8b672020-04-24 15:43:48 +0100369#if defined(ART_TARGET_ANDROID)
Justin Yun089c1352020-02-06 16:53:08 +0900370 if (is_product_vndk) {
371 return android::sysprop::VndkProperties::product_vndk_version().value_or("");
372 }
373 return android::sysprop::VndkProperties::vendor_vndk_version().value_or("");
374#else
375 if (is_product_vndk) {
376 return android::base::GetProperty("ro.product.vndk.version", "");
377 }
378 return android::base::GetProperty("ro.vndk.version", "");
379#endif
380}
381
Orion Hodson9b16e342019-10-09 13:29:16 +0100382namespace internal {
383// Exported for testing
384Result<std::vector<std::string>> ParseConfig(
385 const std::string& file_content,
386 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
387 std::vector<std::string> lines = base::Split(file_content, "\n");
388
389 std::vector<std::string> sonames;
390 for (auto& line : lines) {
391 auto trimmed_line = base::Trim(line);
392 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
393 continue;
394 }
395
396 std::vector<std::string> tokens = android::base::Split(trimmed_line, " ");
397 if (tokens.size() < 1 || tokens.size() > 3) {
398 return Errorf("Malformed line \"{}\"", line);
399 }
400 struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL};
401 size_t i = tokens.size();
402 while (i-- > 0) {
403 if (tokens[i] == "nopreload") {
404 entry.nopreload = true;
405 } else if (tokens[i] == "32" || tokens[i] == "64") {
406 if (entry.bitness != ALL) {
407 return Errorf("Malformed line \"{}\": bitness can be specified only once", line);
408 }
409 entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64;
410 } else {
411 if (i != 0) {
412 return Errorf("Malformed line \"{}\"", line);
413 }
414 entry.soname = tokens[i];
415 }
416 }
417
418 // skip 32-bit lib on 64-bit process and vice versa
419#if defined(__LP64__)
420 if (entry.bitness == ONLY_32) continue;
421#else
422 if (entry.bitness == ONLY_64) continue;
423#endif
424
425 Result<bool> ret = filter_fn(entry);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900426 if (!ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100427 return ret.error();
428 }
429 if (*ret) {
430 // filter_fn has returned true.
431 sonames.push_back(entry.soname);
432 }
433 }
434 return sonames;
435}
436
Jooyung Hancd616d02020-09-01 14:53:23 +0900437// Parses apex.libraries.config.txt file generated by linkerconfig which looks like
438// system/linkerconfig/testdata/golden_output/stages/apex.libraries.config.txt
439// and returns mapping of <apex namespace> to <library list> which matches <tag>.
440//
441// The file is line-based and each line consists of "<tag> <apex namespace> <library list>".
442//
443// <tag> explains what <library list> is. (e.g "jni", "public")
444// <library list> is colon-separated list of library names. (e.g "libfoo.so:libbar.so")
445//
446// If <tag> is "jni", <library list> is the list of JNI libraries exposed by <apex namespace>.
447// If <tag> is "public", <library list> is the list of public libraries exposed by <apex namespace>.
448// Public libraries are the libs listed in /system/etc/public.libraries.txt.
449Result<std::map<std::string, std::string>> ParseApexLibrariesConfig(const std::string& file_content, const std::string& tag) {
Jooyung Han538f99a2020-03-03 00:46:50 +0900450 std::map<std::string, std::string> entries;
451 std::vector<std::string> lines = base::Split(file_content, "\n");
452 for (auto& line : lines) {
453 auto trimmed_line = base::Trim(line);
454 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
455 continue;
456 }
Jooyung Hancd616d02020-09-01 14:53:23 +0900457 auto config_line = ParseApexLibrariesConfigLine(trimmed_line);
458 if (!config_line.ok()) {
459 return config_line.error();
Jooyung Han538f99a2020-03-03 00:46:50 +0900460 }
Jooyung Hancd616d02020-09-01 14:53:23 +0900461 if (config_line->tag != tag) {
462 continue;
463 }
464 entries[config_line->apex_namespace] = config_line->library_list;
465 }
Jooyung Han538f99a2020-03-03 00:46:50 +0900466 return entries;
467}
468
Orion Hodson9b16e342019-10-09 13:29:16 +0100469} // namespace internal
470
471} // namespace android::nativeloader