blob: 4e292eee686973889eb016ac66d292827ac2e91c [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>
26
27#include <android-base/file.h>
28#include <android-base/logging.h>
29#include <android-base/properties.h>
30#include <android-base/result.h>
31#include <android-base/strings.h>
32#include <log/log.h>
33
Justin Yun3db26d52019-12-16 14:09:39 +090034#if defined(__ANDROID__)
35#include <android/sysprop/VndkProperties.sysprop.h>
36#endif
37
Orion Hodson9b16e342019-10-09 13:29:16 +010038#include "utils.h"
39
40namespace android::nativeloader {
41
Orion Hodson9b16e342019-10-09 13:29:16 +010042using android::base::ErrnoError;
Orion Hodson9b16e342019-10-09 13:29:16 +010043using android::base::Result;
Jeffrey Huang52575032020-02-11 17:33:45 -080044using internal::ConfigEntry;
45using internal::ParseConfig;
Jooyung Han538f99a2020-03-03 00:46:50 +090046using internal::ParseJniConfig;
Jeffrey Huang52575032020-02-11 17:33:45 -080047using std::literals::string_literals::operator""s;
Orion Hodson9b16e342019-10-09 13:29:16 +010048
49namespace {
50
51constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
52constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
53constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
Jooyung Han538f99a2020-03-03 00:46:50 +090054constexpr const char* kJniConfigFile = "/linkerconfig/jni.config.txt";
Orion Hodson9b16e342019-10-09 13:29:16 +010055constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
Jooyung Han26f7d102020-02-22 23:39:23 +090056constexpr const char* kLlndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt";
57constexpr const char* kVndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt";
Orion Hodson9b16e342019-10-09 13:29:16 +010058
59const std::vector<const std::string> kArtApexPublicLibraries = {
60 "libicuuc.so",
61 "libicui18n.so",
62};
63
64constexpr const char* kArtApexLibPath = "/apex/com.android.art/" LIB;
65
66constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so";
Luke Huang5c017722019-12-17 10:54:26 +080067// STOPSHIP(b/146420818): Figure out how to use stub or non-specific lib name for libcronet.
68constexpr const char* kCronetApexPublicLibrary = "libcronet.80.0.3986.0.so";
Orion Hodson9b16e342019-10-09 13:29:16 +010069
Jeffrey Huang52575032020-02-11 17:33:45 -080070constexpr const char* kStatsdApexPublicLibrary = "libstats_jni.so";
71
Orion Hodson9b16e342019-10-09 13:29:16 +010072// TODO(b/130388701): do we need this?
73std::string root_dir() {
74 static const char* android_root_env = getenv("ANDROID_ROOT");
75 return android_root_env != nullptr ? android_root_env : "/system";
76}
77
78bool debuggable() {
79 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
80 return debuggable;
81}
82
Justin Yun089c1352020-02-06 16:53:08 +090083std::string vndk_version_str(bool use_product_vndk) {
Jooyung Han26f7d102020-02-22 23:39:23 +090084 if (use_product_vndk) {
85 static std::string product_vndk_version = get_vndk_version(true);
86 return product_vndk_version;
87 } else {
88 static std::string vendor_vndk_version = get_vndk_version(false);
89 return vendor_vndk_version;
Orion Hodson9b16e342019-10-09 13:29:16 +010090 }
Orion Hodson9b16e342019-10-09 13:29:16 +010091}
92
93// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
94// variable to add libraries to the list. This is intended for platform tests only.
95std::string additional_public_libraries() {
96 if (debuggable()) {
97 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
98 return val ? val : "";
99 }
100 return "";
101}
102
Jooyung Han26f7d102020-02-22 23:39:23 +0900103// insert vndk version in every {} placeholder
Justin Yun089c1352020-02-06 16:53:08 +0900104void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100105 CHECK(file_name != nullptr);
Jooyung Han26f7d102020-02-22 23:39:23 +0900106 auto version = vndk_version_str(use_product_vndk);
107 size_t pos = file_name->find("{}");
108 while (pos != std::string::npos) {
109 file_name->replace(pos, 2, version);
110 pos = file_name->find("{}", pos + version.size());
Orion Hodson9b16e342019-10-09 13:29:16 +0100111 }
Orion Hodson9b16e342019-10-09 13:29:16 +0100112}
113
114const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
115 [](const struct ConfigEntry&) -> Result<bool> { return true; };
116
117Result<std::vector<std::string>> ReadConfig(
118 const std::string& configFile,
119 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
120 std::string file_content;
121 if (!base::ReadFileToString(configFile, &file_content)) {
122 return ErrnoError();
123 }
124 Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900125 if (!result.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100126 return Errorf("Cannot parse {}: {}", configFile, result.error().message());
127 }
128 return result;
129}
130
131void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
132 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
133 if (dir != nullptr) {
134 // Failing to opening the dir is not an error, which can happen in
135 // webview_zygote.
136 while (struct dirent* ent = readdir(dir.get())) {
137 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
138 continue;
139 }
140 const std::string filename(ent->d_name);
141 std::string_view fn = filename;
142 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
143 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
144 const std::string company_name(fn);
145 const std::string config_file_path = dirname + "/"s + filename;
146 LOG_ALWAYS_FATAL_IF(
147 company_name.empty(),
148 "Error extracting company name from public native library list file path \"%s\"",
149 config_file_path.c_str());
150
151 auto ret = ReadConfig(
152 config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> {
153 if (android::base::StartsWith(entry.soname, "lib") &&
154 android::base::EndsWith(entry.soname, "." + company_name + ".so")) {
155 return true;
156 } else {
157 return Errorf("Library name \"{}\" does not end with the company name {}.",
158 entry.soname, company_name);
159 }
160 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900161 if (ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100162 sonames->insert(sonames->end(), ret->begin(), ret->end());
163 } else {
164 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
165 config_file_path.c_str(), ret.error().message().c_str());
166 }
167 }
168 }
169 }
170}
171
172static std::string InitDefaultPublicLibraries(bool for_preload) {
173 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
174 auto sonames =
175 ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> {
176 if (for_preload) {
177 return !entry.nopreload;
178 } else {
179 return true;
180 }
181 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900182 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100183 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
184 config_file.c_str(), sonames.error().message().c_str());
185 return "";
186 }
187
188 std::string additional_libs = additional_public_libraries();
189 if (!additional_libs.empty()) {
190 auto vec = base::Split(additional_libs, ":");
191 std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames));
192 }
193
194 // If this is for preloading libs, don't remove the libs from APEXes.
195 if (for_preload) {
196 return android::base::Join(*sonames, ':');
197 }
198
199 // Remove the public libs in the art namespace.
200 // These libs are listed in public.android.txt, but we don't want the rest of android
201 // in default namespace to dlopen the libs.
202 // For example, libicuuc.so is exposed to classloader namespace from art namespace.
203 // Unfortunately, it does not have stable C symbols, and default namespace should only use
204 // stable symbols in libandroidicu.so. http://b/120786417
205 for (const std::string& lib_name : kArtApexPublicLibraries) {
206 std::string path(kArtApexLibPath);
207 path.append("/").append(lib_name);
208
209 struct stat s;
210 // Do nothing if the path in /apex does not exist.
211 // Runtime APEX must be mounted since libnativeloader is in the same APEX
212 if (stat(path.c_str(), &s) != 0) {
213 continue;
214 }
215
216 auto it = std::find(sonames->begin(), sonames->end(), lib_name);
217 if (it != sonames->end()) {
218 sonames->erase(it);
219 }
220 }
221
222 // Remove the public libs in the nnapi namespace.
223 auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary);
224 if (it != sonames->end()) {
225 sonames->erase(it);
226 }
227 return android::base::Join(*sonames, ':');
228}
229
230static std::string InitArtPublicLibraries() {
Jeffrey Huang52575032020-02-11 17:33:45 -0800231 CHECK_GT((int)sizeof(kArtApexPublicLibraries), 0);
Orion Hodson9b16e342019-10-09 13:29:16 +0100232 std::string list = android::base::Join(kArtApexPublicLibraries, ":");
233
234 std::string additional_libs = additional_public_libraries();
235 if (!additional_libs.empty()) {
236 list = list + ':' + additional_libs;
237 }
238 return list;
239}
240
241static std::string InitVendorPublicLibraries() {
242 // This file is optional, quietly ignore if the file does not exist.
243 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900244 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100245 return "";
246 }
247 return android::base::Join(*sonames, ':');
248}
249
Justin Yun0cc40272019-12-16 16:47:40 +0900250// read /system/etc/public.libraries-<companyname>.txt,
251// /system_ext/etc/public.libraries-<companyname>.txt and
Orion Hodson9b16e342019-10-09 13:29:16 +0100252// /product/etc/public.libraries-<companyname>.txt which contain partner defined
253// system libs that are exposed to apps. The libs in the txt files must be
254// named as lib<name>.<companyname>.so.
255static std::string InitExtendedPublicLibraries() {
256 std::vector<std::string> sonames;
257 ReadExtensionLibraries("/system/etc", &sonames);
Justin Yun0cc40272019-12-16 16:47:40 +0900258 ReadExtensionLibraries("/system_ext/etc", &sonames);
Orion Hodson9b16e342019-10-09 13:29:16 +0100259 ReadExtensionLibraries("/product/etc", &sonames);
260 return android::base::Join(sonames, ':');
261}
262
Justin Yun089c1352020-02-06 16:53:08 +0900263static std::string InitLlndkLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100264 std::string config_file = kLlndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900265 InsertVndkVersionStr(&config_file, false);
266 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocentic1375632020-02-13 10:37:03 +0900267 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900268 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Justin Yun089c1352020-02-06 16:53:08 +0900269 return "";
270 }
271 return android::base::Join(*sonames, ':');
272}
273
274static std::string InitLlndkLibrariesProduct() {
275 std::string config_file = kLlndkLibrariesFile;
276 InsertVndkVersionStr(&config_file, true);
Orion Hodson9b16e342019-10-09 13:29:16 +0100277 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900278 if (!sonames.ok()) {
Jooyung Han26f7d102020-02-22 23:39:23 +0900279 LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str());
Orion Hodson9b16e342019-10-09 13:29:16 +0100280 return "";
281 }
282 return android::base::Join(*sonames, ':');
283}
284
Justin Yuneb4f08c2020-02-18 11:29:07 +0900285static std::string InitVndkspLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100286 std::string config_file = kVndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900287 InsertVndkVersionStr(&config_file, false);
Orion Hodson9b16e342019-10-09 13:29:16 +0100288 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900289 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100290 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
291 return "";
292 }
293 return android::base::Join(*sonames, ':');
294}
295
Justin Yuneb4f08c2020-02-18 11:29:07 +0900296static std::string InitVndkspLibrariesProduct() {
297 std::string config_file = kVndkLibrariesFile;
298 InsertVndkVersionStr(&config_file, true);
299 auto sonames = ReadConfig(config_file, always_true);
300 if (!sonames.ok()) {
301 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
302 return "";
303 }
304 return android::base::Join(*sonames, ':');
305}
306
Orion Hodson9b16e342019-10-09 13:29:16 +0100307static std::string InitNeuralNetworksPublicLibraries() {
308 return kNeuralNetworksApexPublicLibrary;
309}
310
Luke Huang5c017722019-12-17 10:54:26 +0800311static std::string InitCronetPublicLibraries() {
312 return kCronetApexPublicLibrary;
313}
314
Jeffrey Huang52575032020-02-11 17:33:45 -0800315static std::string InitStatsdPublicLibraries() {
316 return kStatsdApexPublicLibrary;
317}
318
Jooyung Han538f99a2020-03-03 00:46:50 +0900319static std::map<std::string, std::string> InitApexJniLibraries() {
320 std::string file_content;
321 if (!base::ReadFileToString(kJniConfigFile, &file_content)) {
322 // jni config is optional
323 return {};
324 }
325 auto config = ParseJniConfig(file_content);
326 if (!config.ok()) {
327 LOG_ALWAYS_FATAL("%s: %s", kJniConfigFile, config.error().message().c_str());
328 // not reach here
329 return {};
330 }
331 return *config;
332}
333
Orion Hodson9b16e342019-10-09 13:29:16 +0100334} // namespace
335
336const std::string& preloadable_public_libraries() {
337 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true);
338 return list;
339}
340
341const std::string& default_public_libraries() {
342 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false);
343 return list;
344}
345
346const std::string& art_public_libraries() {
347 static std::string list = InitArtPublicLibraries();
348 return list;
349}
350
351const std::string& vendor_public_libraries() {
352 static std::string list = InitVendorPublicLibraries();
353 return list;
354}
355
356const std::string& extended_public_libraries() {
357 static std::string list = InitExtendedPublicLibraries();
358 return list;
359}
360
361const std::string& neuralnetworks_public_libraries() {
362 static std::string list = InitNeuralNetworksPublicLibraries();
363 return list;
364}
365
Luke Huang5c017722019-12-17 10:54:26 +0800366const std::string& cronet_public_libraries() {
367 static std::string list = InitCronetPublicLibraries();
368 return list;
369}
370
Jeffrey Huang52575032020-02-11 17:33:45 -0800371const std::string& statsd_public_libraries() {
372 static std::string list = InitStatsdPublicLibraries();
373 return list;
374}
375
Justin Yun089c1352020-02-06 16:53:08 +0900376const std::string& llndk_libraries_product() {
377 static std::string list = InitLlndkLibrariesProduct();
378 return list;
379}
380
381const std::string& llndk_libraries_vendor() {
382 static std::string list = InitLlndkLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100383 return list;
384}
385
Justin Yuneb4f08c2020-02-18 11:29:07 +0900386const std::string& vndksp_libraries_product() {
387 static std::string list = InitVndkspLibrariesProduct();
388 return list;
389}
390
391const std::string& vndksp_libraries_vendor() {
392 static std::string list = InitVndkspLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100393 return list;
394}
395
Jooyung Han538f99a2020-03-03 00:46:50 +0900396const std::string& apex_jni_libraries(const std::string& apex_ns_name) {
397 static std::map<std::string, std::string> jni_libraries = InitApexJniLibraries();
398 return jni_libraries[apex_ns_name];
399}
400
Justin Yun3db26d52019-12-16 14:09:39 +0900401bool is_product_vndk_version_defined() {
402#if defined(__ANDROID__)
403 return android::sysprop::VndkProperties::product_vndk_version().has_value();
404#else
405 return false;
406#endif
407}
408
Justin Yun089c1352020-02-06 16:53:08 +0900409std::string get_vndk_version(bool is_product_vndk) {
410#if defined(__ANDROID__)
411 if (is_product_vndk) {
412 return android::sysprop::VndkProperties::product_vndk_version().value_or("");
413 }
414 return android::sysprop::VndkProperties::vendor_vndk_version().value_or("");
415#else
416 if (is_product_vndk) {
417 return android::base::GetProperty("ro.product.vndk.version", "");
418 }
419 return android::base::GetProperty("ro.vndk.version", "");
420#endif
421}
422
Orion Hodson9b16e342019-10-09 13:29:16 +0100423namespace internal {
424// Exported for testing
425Result<std::vector<std::string>> ParseConfig(
426 const std::string& file_content,
427 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
428 std::vector<std::string> lines = base::Split(file_content, "\n");
429
430 std::vector<std::string> sonames;
431 for (auto& line : lines) {
432 auto trimmed_line = base::Trim(line);
433 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
434 continue;
435 }
436
437 std::vector<std::string> tokens = android::base::Split(trimmed_line, " ");
438 if (tokens.size() < 1 || tokens.size() > 3) {
439 return Errorf("Malformed line \"{}\"", line);
440 }
441 struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL};
442 size_t i = tokens.size();
443 while (i-- > 0) {
444 if (tokens[i] == "nopreload") {
445 entry.nopreload = true;
446 } else if (tokens[i] == "32" || tokens[i] == "64") {
447 if (entry.bitness != ALL) {
448 return Errorf("Malformed line \"{}\": bitness can be specified only once", line);
449 }
450 entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64;
451 } else {
452 if (i != 0) {
453 return Errorf("Malformed line \"{}\"", line);
454 }
455 entry.soname = tokens[i];
456 }
457 }
458
459 // skip 32-bit lib on 64-bit process and vice versa
460#if defined(__LP64__)
461 if (entry.bitness == ONLY_32) continue;
462#else
463 if (entry.bitness == ONLY_64) continue;
464#endif
465
466 Result<bool> ret = filter_fn(entry);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900467 if (!ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100468 return ret.error();
469 }
470 if (*ret) {
471 // filter_fn has returned true.
472 sonames.push_back(entry.soname);
473 }
474 }
475 return sonames;
476}
477
Jooyung Han538f99a2020-03-03 00:46:50 +0900478Result<std::map<std::string, std::string>> ParseJniConfig(const std::string& file_content) {
479 std::map<std::string, std::string> entries;
480 std::vector<std::string> lines = base::Split(file_content, "\n");
481 for (auto& line : lines) {
482 auto trimmed_line = base::Trim(line);
483 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
484 continue;
485 }
486
487 std::vector<std::string> tokens = base::Split(trimmed_line, " ");
488 if (tokens.size() < 2) {
489 return Errorf( "Malformed line \"{}\"", line);
490 }
491 entries[tokens[0]] = tokens[1];
492 }
493 return entries;
494}
495
Orion Hodson9b16e342019-10-09 13:29:16 +0100496} // namespace internal
497
498} // namespace android::nativeloader