blob: 35ef4889d0e2e770c4aa4de0b4c56453c0f97307 [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";
53constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
54constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
55
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) {
81 static std::string version = get_vndk_version(use_product_vndk);
Orion Hodson9b16e342019-10-09 13:29:16 +010082 if (version != "" && version != "current") {
83 return "." + version;
84 }
85 return "";
86}
87
88// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
89// variable to add libraries to the list. This is intended for platform tests only.
90std::string additional_public_libraries() {
91 if (debuggable()) {
92 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
93 return val ? val : "";
94 }
95 return "";
96}
97
Justin Yun089c1352020-02-06 16:53:08 +090098void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) {
Orion Hodson9b16e342019-10-09 13:29:16 +010099 CHECK(file_name != nullptr);
100 size_t insert_pos = file_name->find_last_of(".");
101 if (insert_pos == std::string::npos) {
102 insert_pos = file_name->length();
103 }
Justin Yun089c1352020-02-06 16:53:08 +0900104 file_name->insert(insert_pos, vndk_version_str(use_product_vndk));
Orion Hodson9b16e342019-10-09 13:29:16 +0100105}
106
107const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
108 [](const struct ConfigEntry&) -> Result<bool> { return true; };
109
110Result<std::vector<std::string>> ReadConfig(
111 const std::string& configFile,
112 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
113 std::string file_content;
114 if (!base::ReadFileToString(configFile, &file_content)) {
115 return ErrnoError();
116 }
117 Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900118 if (!result.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100119 return Errorf("Cannot parse {}: {}", configFile, result.error().message());
120 }
121 return result;
122}
123
124void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
125 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
126 if (dir != nullptr) {
127 // Failing to opening the dir is not an error, which can happen in
128 // webview_zygote.
129 while (struct dirent* ent = readdir(dir.get())) {
130 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
131 continue;
132 }
133 const std::string filename(ent->d_name);
134 std::string_view fn = filename;
135 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
136 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
137 const std::string company_name(fn);
138 const std::string config_file_path = dirname + "/"s + filename;
139 LOG_ALWAYS_FATAL_IF(
140 company_name.empty(),
141 "Error extracting company name from public native library list file path \"%s\"",
142 config_file_path.c_str());
143
144 auto ret = ReadConfig(
145 config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> {
146 if (android::base::StartsWith(entry.soname, "lib") &&
147 android::base::EndsWith(entry.soname, "." + company_name + ".so")) {
148 return true;
149 } else {
150 return Errorf("Library name \"{}\" does not end with the company name {}.",
151 entry.soname, company_name);
152 }
153 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900154 if (ret.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100155 sonames->insert(sonames->end(), ret->begin(), ret->end());
156 } else {
157 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
158 config_file_path.c_str(), ret.error().message().c_str());
159 }
160 }
161 }
162 }
163}
164
165static std::string InitDefaultPublicLibraries(bool for_preload) {
166 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
167 auto sonames =
168 ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> {
169 if (for_preload) {
170 return !entry.nopreload;
171 } else {
172 return true;
173 }
174 });
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900175 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100176 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
177 config_file.c_str(), sonames.error().message().c_str());
178 return "";
179 }
180
181 std::string additional_libs = additional_public_libraries();
182 if (!additional_libs.empty()) {
183 auto vec = base::Split(additional_libs, ":");
184 std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames));
185 }
186
187 // If this is for preloading libs, don't remove the libs from APEXes.
188 if (for_preload) {
189 return android::base::Join(*sonames, ':');
190 }
191
192 // Remove the public libs in the art namespace.
193 // These libs are listed in public.android.txt, but we don't want the rest of android
194 // in default namespace to dlopen the libs.
195 // For example, libicuuc.so is exposed to classloader namespace from art namespace.
196 // Unfortunately, it does not have stable C symbols, and default namespace should only use
197 // stable symbols in libandroidicu.so. http://b/120786417
198 for (const std::string& lib_name : kArtApexPublicLibraries) {
199 std::string path(kArtApexLibPath);
200 path.append("/").append(lib_name);
201
202 struct stat s;
203 // Do nothing if the path in /apex does not exist.
204 // Runtime APEX must be mounted since libnativeloader is in the same APEX
205 if (stat(path.c_str(), &s) != 0) {
206 continue;
207 }
208
209 auto it = std::find(sonames->begin(), sonames->end(), lib_name);
210 if (it != sonames->end()) {
211 sonames->erase(it);
212 }
213 }
214
215 // Remove the public libs in the nnapi namespace.
216 auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary);
217 if (it != sonames->end()) {
218 sonames->erase(it);
219 }
220 return android::base::Join(*sonames, ':');
221}
222
223static std::string InitArtPublicLibraries() {
Jeffrey Huang52575032020-02-11 17:33:45 -0800224 CHECK_GT((int)sizeof(kArtApexPublicLibraries), 0);
Orion Hodson9b16e342019-10-09 13:29:16 +0100225 std::string list = android::base::Join(kArtApexPublicLibraries, ":");
226
227 std::string additional_libs = additional_public_libraries();
228 if (!additional_libs.empty()) {
229 list = list + ':' + additional_libs;
230 }
231 return list;
232}
233
234static std::string InitVendorPublicLibraries() {
235 // This file is optional, quietly ignore if the file does not exist.
236 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900237 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100238 return "";
239 }
240 return android::base::Join(*sonames, ':');
241}
242
Justin Yun0cc40272019-12-16 16:47:40 +0900243// read /system/etc/public.libraries-<companyname>.txt,
244// /system_ext/etc/public.libraries-<companyname>.txt and
Orion Hodson9b16e342019-10-09 13:29:16 +0100245// /product/etc/public.libraries-<companyname>.txt which contain partner defined
246// system libs that are exposed to apps. The libs in the txt files must be
247// named as lib<name>.<companyname>.so.
248static std::string InitExtendedPublicLibraries() {
249 std::vector<std::string> sonames;
250 ReadExtensionLibraries("/system/etc", &sonames);
Justin Yun0cc40272019-12-16 16:47:40 +0900251 ReadExtensionLibraries("/system_ext/etc", &sonames);
Orion Hodson9b16e342019-10-09 13:29:16 +0100252 ReadExtensionLibraries("/product/etc", &sonames);
253 return android::base::Join(sonames, ':');
254}
255
Justin Yun089c1352020-02-06 16:53:08 +0900256static std::string InitLlndkLibrariesVendor() {
Orion Hodson9b16e342019-10-09 13:29:16 +0100257 std::string config_file = kLlndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900258 InsertVndkVersionStr(&config_file, false);
259 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocentic1375632020-02-13 10:37:03 +0900260 if (!sonames.ok()) {
Justin Yun089c1352020-02-06 16:53:08 +0900261 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
262 return "";
263 }
264 return android::base::Join(*sonames, ':');
265}
266
267static std::string InitLlndkLibrariesProduct() {
268 std::string config_file = kLlndkLibrariesFile;
269 InsertVndkVersionStr(&config_file, true);
Orion Hodson9b16e342019-10-09 13:29:16 +0100270 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900271 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100272 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
273 return "";
274 }
275 return android::base::Join(*sonames, ':');
276}
277
278static std::string InitVndkspLibraries() {
Justin Yun089c1352020-02-06 16:53:08 +0900279 // VNDK-SP is used only for vendor hals which are not available for the
280 // product partition.
Orion Hodson9b16e342019-10-09 13:29:16 +0100281 std::string config_file = kVndkLibrariesFile;
Justin Yun089c1352020-02-06 16:53:08 +0900282 InsertVndkVersionStr(&config_file, false);
Orion Hodson9b16e342019-10-09 13:29:16 +0100283 auto sonames = ReadConfig(config_file, always_true);
Bernie Innocenti4bd58952020-02-06 15:43:57 +0900284 if (!sonames.ok()) {
Orion Hodson9b16e342019-10-09 13:29:16 +0100285 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
286 return "";
287 }
288 return android::base::Join(*sonames, ':');
289}
290
291static std::string InitNeuralNetworksPublicLibraries() {
292 return kNeuralNetworksApexPublicLibrary;
293}
294
Luke Huang5c017722019-12-17 10:54:26 +0800295static std::string InitCronetPublicLibraries() {
296 return kCronetApexPublicLibrary;
297}
298
Jeffrey Huang52575032020-02-11 17:33:45 -0800299static std::string InitStatsdPublicLibraries() {
300 return kStatsdApexPublicLibrary;
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
315const std::string& art_public_libraries() {
316 static std::string list = InitArtPublicLibraries();
317 return list;
318}
319
320const std::string& vendor_public_libraries() {
321 static std::string list = InitVendorPublicLibraries();
322 return list;
323}
324
325const std::string& extended_public_libraries() {
326 static std::string list = InitExtendedPublicLibraries();
327 return list;
328}
329
330const std::string& neuralnetworks_public_libraries() {
331 static std::string list = InitNeuralNetworksPublicLibraries();
332 return list;
333}
334
Luke Huang5c017722019-12-17 10:54:26 +0800335const std::string& cronet_public_libraries() {
336 static std::string list = InitCronetPublicLibraries();
337 return list;
338}
339
Jeffrey Huang52575032020-02-11 17:33:45 -0800340const std::string& statsd_public_libraries() {
341 static std::string list = InitStatsdPublicLibraries();
342 return list;
343}
344
Justin Yun089c1352020-02-06 16:53:08 +0900345const std::string& llndk_libraries_product() {
346 static std::string list = InitLlndkLibrariesProduct();
347 return list;
348}
349
350const std::string& llndk_libraries_vendor() {
351 static std::string list = InitLlndkLibrariesVendor();
Orion Hodson9b16e342019-10-09 13:29:16 +0100352 return list;
353}
354
355const std::string& vndksp_libraries() {
356 static std::string list = InitVndkspLibraries();
357 return list;
358}
359
Justin Yun3db26d52019-12-16 14:09:39 +0900360bool is_product_vndk_version_defined() {
361#if defined(__ANDROID__)
362 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) {
369#if defined(__ANDROID__)
370 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
437} // namespace internal
438
439} // namespace android::nativeloader