blob: d244f375a6ae04d173c6dcdd4f6b77a00c0021d0 [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>
24#include <memory>
25
26#include <android-base/file.h>
27#include <android-base/logging.h>
28#include <android-base/properties.h>
29#include <android-base/result.h>
30#include <android-base/strings.h>
31#include <log/log.h>
32
Justin Yun3db26d52019-12-16 14:09:39 +090033#if defined(__ANDROID__)
34#include <android/sysprop/VndkProperties.sysprop.h>
35#endif
36
Orion Hodson9b16e342019-10-09 13:29:16 +010037#include "utils.h"
38
39namespace android::nativeloader {
40
Orion Hodson9b16e342019-10-09 13:29:16 +010041using android::base::ErrnoError;
Orion Hodson9b16e342019-10-09 13:29:16 +010042using android::base::Result;
Jeffrey Huang52575032020-02-11 17:33:45 -080043using internal::ConfigEntry;
44using internal::ParseConfig;
45using std::literals::string_literals::operator""s;
Orion Hodson9b16e342019-10-09 13:29:16 +010046
47namespace {
48
49constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
50constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
51constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
52constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
Jooyung Han26f7d102020-02-22 23:39:23 +090053constexpr const char* kLlndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt";
54constexpr const char* kVndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt";
Orion Hodson9b16e342019-10-09 13:29:16 +010055
56const std::vector<const std::string> kArtApexPublicLibraries = {
57 "libicuuc.so",
58 "libicui18n.so",
59};
60
61constexpr const char* kArtApexLibPath = "/apex/com.android.art/" LIB;
62
63constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so";
Luke Huang5c017722019-12-17 10:54:26 +080064// STOPSHIP(b/146420818): Figure out how to use stub or non-specific lib name for libcronet.
65constexpr const char* kCronetApexPublicLibrary = "libcronet.80.0.3986.0.so";
Orion Hodson9b16e342019-10-09 13:29:16 +010066
Jeffrey Huang52575032020-02-11 17:33:45 -080067constexpr const char* kStatsdApexPublicLibrary = "libstats_jni.so";
68
Orion Hodson9b16e342019-10-09 13:29:16 +010069// TODO(b/130388701): do we need this?
70std::string root_dir() {
71 static const char* android_root_env = getenv("ANDROID_ROOT");
72 return android_root_env != nullptr ? android_root_env : "/system";
73}
74
75bool debuggable() {
76 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
77 return debuggable;
78}
79
Justin Yun089c1352020-02-06 16:53:08 +090080std::string vndk_version_str(bool use_product_vndk) {
Jooyung Han26f7d102020-02-22 23:39:23 +090081 if (use_product_vndk) {
82 static std::string product_vndk_version = get_vndk_version(true);
83 return product_vndk_version;
84 } else {
85 static std::string vendor_vndk_version = get_vndk_version(false);
86 return vendor_vndk_version;
Orion Hodson9b16e342019-10-09 13:29:16 +010087 }
Orion Hodson9b16e342019-10-09 13:29:16 +010088}
89
90// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
91// variable to add libraries to the list. This is intended for platform tests only.
92std::string additional_public_libraries() {
93 if (debuggable()) {
94 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
95 return val ? val : "";
96 }
97 return "";
98}
99
Jooyung Han26f7d102020-02-22 23:39:23 +0900100// insert vndk version in every {} placeholder
Justin Yun089c1352020-02-06 16:53:08 +0900101void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100102 CHECK(file_name != nullptr);
Jooyung Han26f7d102020-02-22 23:39:23 +0900103 auto version = vndk_version_str(use_product_vndk);
104 size_t pos = file_name->find("{}");
105 while (pos != std::string::npos) {
106 file_name->replace(pos, 2, version);
107 pos = file_name->find("{}", pos + version.size());
Orion Hodson9b16e342019-10-09 13:29:16 +0100108 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100109}
110
111const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
112 [](const struct ConfigEntry&) -> Result<bool> { return true; };
113
114Result<std::vector<std::string>> ReadConfig(
115 const std::string& configFile,
116 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
117 std::string file_content;
118 if (!base::ReadFileToString(configFile, &file_content)) {
119 return ErrnoError();
120 }
121 Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900122 if (!result.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100123 return Errorf("Cannot parse {}: {}", configFile, result.error().message());
124 }
125 return result;
126}
127
128void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
129 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
130 if (dir != nullptr) {
131 // Failing to opening the dir is not an error, which can happen in
132 // webview_zygote.
133 while (struct dirent* ent = readdir(dir.get())) {
134 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
135 continue;
136 }
137 const std::string filename(ent->d_name);
138 std::string_view fn = filename;
139 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
140 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
141 const std::string company_name(fn);
142 const std::string config_file_path = dirname + "/"s + filename;
143 LOG_ALWAYS_FATAL_IF(
144 company_name.empty(),
145 "Error extracting company name from public native library list file path \"%s\"",
146 config_file_path.c_str());
147
148 auto ret = ReadConfig(
149 config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> {
150 if (android::base::StartsWith(entry.soname, "lib") &&
151 android::base::EndsWith(entry.soname, "." + company_name + ".so")) {
152 return true;
153 } else {
154 return Errorf("Library name \"{}\" does not end with the company name {}.",
155 entry.soname, company_name);
156 }
157 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900158 if (ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100159 sonames->insert(sonames->end(), ret->begin(), ret->end());
160 } else {
161 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
162 config_file_path.c_str(), ret.error().message().c_str());
163 }
164 }
165 }
166 }
167}
168
169static std::string InitDefaultPublicLibraries(bool for_preload) {
170 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
171 auto sonames =
172 ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> {
173 if (for_preload) {
174 return !entry.nopreload;
175 } else {
176 return true;
177 }
178 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900179 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100180 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
181 config_file.c_str(), sonames.error().message().c_str());
182 return "";
183 }
184
185 std::string additional_libs = additional_public_libraries();
186 if (!additional_libs.empty()) {
187 auto vec = base::Split(additional_libs, ":");
188 std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames));
189 }
190
191 // If this is for preloading libs, don't remove the libs from APEXes.
192 if (for_preload) {
193 return android::base::Join(*sonames, ':');
194 }
195
196 // Remove the public libs in the art namespace.
197 // These libs are listed in public.android.txt, but we don't want the rest of android
198 // in default namespace to dlopen the libs.
199 // For example, libicuuc.so is exposed to classloader namespace from art namespace.
200 // Unfortunately, it does not have stable C symbols, and default namespace should only use
201 // stable symbols in libandroidicu.so. http://b/120786417
202 for (const std::string& lib_name : kArtApexPublicLibraries) {
203 std::string path(kArtApexLibPath);
204 path.append("/").append(lib_name);
205
206 struct stat s;
207 // Do nothing if the path in /apex does not exist.
208 // Runtime APEX must be mounted since libnativeloader is in the same APEX
209 if (stat(path.c_str(), &s) != 0) {
210 continue;
211 }
212
213 auto it = std::find(sonames->begin(), sonames->end(), lib_name);
214 if (it != sonames->end()) {
215 sonames->erase(it);
216 }
217 }
218
219 // Remove the public libs in the nnapi namespace.
220 auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary);
221 if (it != sonames->end()) {
222 sonames->erase(it);
223 }
224 return android::base::Join(*sonames, ':');
225}
226
227static std::string InitArtPublicLibraries() {
Jeffrey Huang52575032020-02-11 17:33:45 -0800228 CHECK_GT((int)sizeof(kArtApexPublicLibraries), 0);
Orion Hodson9b16e342019-10-09 13:29:16 +0100229 std::string list = android::base::Join(kArtApexPublicLibraries, ":");
230
231 std::string additional_libs = additional_public_libraries();
232 if (!additional_libs.empty()) {
233 list = list + ':' + additional_libs;
234 }
235 return list;
236}
237
238static std::string InitVendorPublicLibraries() {
239 // This file is optional, quietly ignore if the file does not exist.
240 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900241 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100242 return "";
243 }
244 return android::base::Join(*sonames, ':');
245}
246
Justin Yun0cc40272019-12-16 16:47:40 +0900247// read /system/etc/public.libraries-<companyname>.txt,
248// /system_ext/etc/public.libraries-<companyname>.txt and
Orion Hodson9b16e342019-10-09 13:29:16 +0100249// /product/etc/public.libraries-<companyname>.txt which contain partner defined
250// system libs that are exposed to apps. The libs in the txt files must be
251// named as lib<name>.<companyname>.so.
252static std::string InitExtendedPublicLibraries() {
253 std::vector<std::string> sonames;
254 ReadExtensionLibraries("/system/etc", &sonames);
Justin Yun0cc40272019-12-16 16:47:40 +0900255 ReadExtensionLibraries("/system_ext/etc", &sonames);
Orion Hodson9b16e342019-10-09 13:29:16 +0100256 ReadExtensionLibraries("/product/etc", &sonames);
257 return android::base::Join(sonames, ':');
258}
259
Justin Yun089c1352020-02-06 16:53:08 +0900260static std::string InitLlndkLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100261 std::string config_file = kLlndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900262 InsertVndkVersionStr(&config_file, false);
263 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocentic1375632020-02-13 10:37:03 +0900264 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900265 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Justin Yun089c1352020-02-06 16:53:08 +0900266 return "";
267 }
268 return android::base::Join(*sonames, ':');
269}
270
271static std::string InitLlndkLibrariesProduct() {
272 std::string config_file = kLlndkLibrariesFile;
273 InsertVndkVersionStr(&config_file, true);
Orion Hodson9b16e342019-10-09 13:29:16 +0100274 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900275 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900276 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100277 return "";
278 }
279 return android::base::Join(*sonames, ':');
280}
281
Justin Yuneb4f08c2020-02-18 11:29:07 +0900282static std::string InitVndkspLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100283 std::string config_file = kVndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900284 InsertVndkVersionStr(&config_file, false);
Orion Hodson9b16e342019-10-09 13:29:16 +0100285 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900286 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100287 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
288 return "";
289 }
290 return android::base::Join(*sonames, ':');
291}
292
Justin Yuneb4f08c2020-02-18 11:29:07 +0900293static std::string InitVndkspLibrariesProduct() {
294 std::string config_file = kVndkLibrariesFile;
295 InsertVndkVersionStr(&config_file, true);
296 auto sonames = ReadConfig(config_file, always_true);
297 if (!sonames.ok()) {
298 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
299 return "";
300 }
301 return android::base::Join(*sonames, ':');
302}
303
Orion Hodson9b16e342019-10-09 13:29:16 +0100304static std::string InitNeuralNetworksPublicLibraries() {
305 return kNeuralNetworksApexPublicLibrary;
306}
307
Luke Huang5c017722019-12-17 10:54:26 +0800308static std::string InitCronetPublicLibraries() {
309 return kCronetApexPublicLibrary;
310}
311
Jeffrey Huang52575032020-02-11 17:33:45 -0800312static std::string InitStatsdPublicLibraries() {
313 return kStatsdApexPublicLibrary;
314}
315
Orion Hodson9b16e342019-10-09 13:29:16 +0100316} // namespace
317
318const std::string& preloadable_public_libraries() {
319 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true);
320 return list;
321}
322
323const std::string& default_public_libraries() {
324 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false);
325 return list;
326}
327
328const std::string& art_public_libraries() {
329 static std::string list = InitArtPublicLibraries();
330 return list;
331}
332
333const std::string& vendor_public_libraries() {
334 static std::string list = InitVendorPublicLibraries();
335 return list;
336}
337
338const std::string& extended_public_libraries() {
339 static std::string list = InitExtendedPublicLibraries();
340 return list;
341}
342
343const std::string& neuralnetworks_public_libraries() {
344 static std::string list = InitNeuralNetworksPublicLibraries();
345 return list;
346}
347
Luke Huang5c017722019-12-17 10:54:26 +0800348const std::string& cronet_public_libraries() {
349 static std::string list = InitCronetPublicLibraries();
350 return list;
351}
352
Jeffrey Huang52575032020-02-11 17:33:45 -0800353const std::string& statsd_public_libraries() {
354 static std::string list = InitStatsdPublicLibraries();
355 return list;
356}
357
Justin Yun089c1352020-02-06 16:53:08 +0900358const std::string& llndk_libraries_product() {
359 static std::string list = InitLlndkLibrariesProduct();
360 return list;
361}
362
363const std::string& llndk_libraries_vendor() {
364 static std::string list = InitLlndkLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100365 return list;
366}
367
Justin Yuneb4f08c2020-02-18 11:29:07 +0900368const std::string& vndksp_libraries_product() {
369 static std::string list = InitVndkspLibrariesProduct();
370 return list;
371}
372
373const std::string& vndksp_libraries_vendor() {
374 static std::string list = InitVndkspLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100375 return list;
376}
377
Justin Yun3db26d52019-12-16 14:09:39 +0900378bool is_product_vndk_version_defined() {
379#if defined(__ANDROID__)
380 return android::sysprop::VndkProperties::product_vndk_version().has_value();
381#else
382 return false;
383#endif
384}
385
Justin Yun089c1352020-02-06 16:53:08 +0900386std::string get_vndk_version(bool is_product_vndk) {
387#if defined(__ANDROID__)
388 if (is_product_vndk) {
389 return android::sysprop::VndkProperties::product_vndk_version().value_or("");
390 }
391 return android::sysprop::VndkProperties::vendor_vndk_version().value_or("");
392#else
393 if (is_product_vndk) {
394 return android::base::GetProperty("ro.product.vndk.version", "");
395 }
396 return android::base::GetProperty("ro.vndk.version", "");
397#endif
398}
399
Orion Hodson9b16e342019-10-09 13:29:16 +0100400namespace internal {
401// Exported for testing
402Result<std::vector<std::string>> ParseConfig(
403 const std::string& file_content,
404 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
405 std::vector<std::string> lines = base::Split(file_content, "\n");
406
407 std::vector<std::string> sonames;
408 for (auto& line : lines) {
409 auto trimmed_line = base::Trim(line);
410 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
411 continue;
412 }
413
414 std::vector<std::string> tokens = android::base::Split(trimmed_line, " ");
415 if (tokens.size() < 1 || tokens.size() > 3) {
416 return Errorf("Malformed line \"{}\"", line);
417 }
418 struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL};
419 size_t i = tokens.size();
420 while (i-- > 0) {
421 if (tokens[i] == "nopreload") {
422 entry.nopreload = true;
423 } else if (tokens[i] == "32" || tokens[i] == "64") {
424 if (entry.bitness != ALL) {
425 return Errorf("Malformed line \"{}\": bitness can be specified only once", line);
426 }
427 entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64;
428 } else {
429 if (i != 0) {
430 return Errorf("Malformed line \"{}\"", line);
431 }
432 entry.soname = tokens[i];
433 }
434 }
435
436 // skip 32-bit lib on 64-bit process and vice versa
437#if defined(__LP64__)
438 if (entry.bitness == ONLY_32) continue;
439#else
440 if (entry.bitness == ONLY_64) continue;
441#endif
442
443 Result<bool> ret = filter_fn(entry);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900444 if (!ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100445 return ret.error();
446 }
447 if (*ret) {
448 // filter_fn has returned true.
449 sonames.push_back(entry.soname);
450 }
451 }
452 return sonames;
453}
454
455} // namespace internal
456
457} // namespace android::nativeloader