blob: 93df1d05ad597b9bb09711445771f489af5065d3 [file] [log] [blame]
Jiyong Park40a60772019-05-03 16:21:31 +09001/*
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
Jiyong Park40a60772019-05-03 16:21:31 +090017#define LOG_TAG "nativeloader"
18
Jiyong Park16a98962019-05-04 03:10:48 +090019#include "public_libraries.h"
20
Jiyong Park40a60772019-05-03 16:21:31 +090021#include <dirent.h>
22
23#include <algorithm>
24#include <memory>
25
Jiyong Park16a98962019-05-04 03:10:48 +090026#include <android-base/file.h>
27#include <android-base/logging.h>
28#include <android-base/properties.h>
Jiyong Park8f4afc82019-07-22 14:20:40 +090029#include <android-base/result.h>
Jiyong Park16a98962019-05-04 03:10:48 +090030#include <android-base/strings.h>
31#include <log/log.h>
32
Jiyong Parkf8802e52019-05-03 16:34:56 +090033#include "utils.h"
Jiyong Park40a60772019-05-03 16:21:31 +090034
35namespace android::nativeloader {
36
Jiyong Park5db5d192019-07-22 20:29:08 +090037using namespace internal;
38using namespace ::std::string_literals;
Jiyong Park8f4afc82019-07-22 14:20:40 +090039using android::base::ErrnoError;
40using android::base::Errorf;
41using android::base::Result;
Jiyong Park40a60772019-05-03 16:21:31 +090042
43namespace {
Jiyong Park5b8b3062019-05-03 18:11:49 +090044
45constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
46constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
47constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
48constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
49constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
50constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
Jiyong Park40a60772019-05-03 16:21:31 +090051
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +010052const std::vector<const std::string> kArtApexPublicLibraries = {
Jiyong Park40a60772019-05-03 16:21:31 +090053 "libicuuc.so",
54 "libicui18n.so",
55};
56
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +010057constexpr const char* kArtApexLibPath = "/apex/com.android.art/" LIB;
Jiyong Park40a60772019-05-03 16:21:31 +090058
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +010059constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so";
60
Jiyong Park5b8b3062019-05-03 18:11:49 +090061// TODO(b/130388701): do we need this?
Jiyong Park40a60772019-05-03 16:21:31 +090062std::string root_dir() {
63 static const char* android_root_env = getenv("ANDROID_ROOT");
64 return android_root_env != nullptr ? android_root_env : "/system";
65}
66
67bool debuggable() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090068 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park40a60772019-05-03 16:21:31 +090069 return debuggable;
70}
71
72std::string vndk_version_str() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090073 static std::string version = android::base::GetProperty("ro.vndk.version", "");
Jiyong Park40a60772019-05-03 16:21:31 +090074 if (version != "" && version != "current") {
75 return "." + version;
76 }
77 return "";
78}
79
Jiyong Park4b5a37c2019-05-09 17:17:37 +090080// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
81// variable to add libraries to the list. This is intended for platform tests only.
82std::string additional_public_libraries() {
83 if (debuggable()) {
84 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
85 return val ? val : "";
86 }
87 return "";
88}
89
Jiyong Park5b8b3062019-05-03 18:11:49 +090090void InsertVndkVersionStr(std::string* file_name) {
Jiyong Park40a60772019-05-03 16:21:31 +090091 CHECK(file_name != nullptr);
92 size_t insert_pos = file_name->find_last_of(".");
93 if (insert_pos == std::string::npos) {
94 insert_pos = file_name->length();
95 }
96 file_name->insert(insert_pos, vndk_version_str());
97}
98
Jiyong Park5db5d192019-07-22 20:29:08 +090099const std::function<Result<bool>(const struct ConfigEntry&)> always_true =
100 [](const struct ConfigEntry&) -> Result<bool> { return true; };
Jiyong Park40a60772019-05-03 16:21:31 +0900101
Jiyong Park8f4afc82019-07-22 14:20:40 +0900102Result<std::vector<std::string>> ReadConfig(
103 const std::string& configFile,
Jiyong Park5db5d192019-07-22 20:29:08 +0900104 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
Jiyong Park40a60772019-05-03 16:21:31 +0900105 std::string file_content;
106 if (!base::ReadFileToString(configFile, &file_content)) {
Jiyong Park8f4afc82019-07-22 14:20:40 +0900107 return ErrnoError();
Jiyong Park40a60772019-05-03 16:21:31 +0900108 }
Jiyong Park5db5d192019-07-22 20:29:08 +0900109 Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn);
110 if (!result) {
111 return Errorf("Cannot parse {}: {}", configFile, result.error().message());
Jiyong Park40a60772019-05-03 16:21:31 +0900112 }
Jiyong Park5db5d192019-07-22 20:29:08 +0900113 return result;
Jiyong Park40a60772019-05-03 16:21:31 +0900114}
115
116void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
117 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
118 if (dir != nullptr) {
119 // Failing to opening the dir is not an error, which can happen in
120 // webview_zygote.
121 while (struct dirent* ent = readdir(dir.get())) {
122 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
123 continue;
124 }
125 const std::string filename(ent->d_name);
Jiyong Park5b8b3062019-05-03 18:11:49 +0900126 std::string_view fn = filename;
127 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
128 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
129 const std::string company_name(fn);
Jiyong Park40a60772019-05-03 16:21:31 +0900130 const std::string config_file_path = dirname + "/"s + filename;
131 LOG_ALWAYS_FATAL_IF(
132 company_name.empty(),
133 "Error extracting company name from public native library list file path \"%s\"",
134 config_file_path.c_str());
135
Jiyong Park8f4afc82019-07-22 14:20:40 +0900136 auto ret = ReadConfig(
Jiyong Park5db5d192019-07-22 20:29:08 +0900137 config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> {
138 if (android::base::StartsWith(entry.soname, "lib") &&
139 android::base::EndsWith(entry.soname, "." + company_name + ".so")) {
140 return true;
Jiyong Park8f4afc82019-07-22 14:20:40 +0900141 } else {
Jiyong Park5db5d192019-07-22 20:29:08 +0900142 return Errorf("Library name \"{}\" does not end with the company name {}.",
143 entry.soname, company_name);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900144 }
145 });
146 if (ret) {
147 sonames->insert(sonames->end(), ret->begin(), ret->end());
148 } else {
149 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
150 config_file_path.c_str(), ret.error().message().c_str());
151 }
Jiyong Park40a60772019-05-03 16:21:31 +0900152 }
153 }
154 }
155}
156
Jiyong Park5db5d192019-07-22 20:29:08 +0900157static std::string InitDefaultPublicLibraries(bool for_preload) {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900158 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
Jiyong Park5db5d192019-07-22 20:29:08 +0900159 auto sonames =
160 ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> {
161 if (for_preload) {
162 return !entry.nopreload;
163 } else {
164 return true;
165 }
166 });
Jiyong Park8f4afc82019-07-22 14:20:40 +0900167 if (!sonames) {
168 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
169 config_file.c_str(), sonames.error().message().c_str());
170 return "";
171 }
Jiyong Park40a60772019-05-03 16:21:31 +0900172
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900173 std::string additional_libs = additional_public_libraries();
174 if (!additional_libs.empty()) {
175 auto vec = base::Split(additional_libs, ":");
Jiyong Park8f4afc82019-07-22 14:20:40 +0900176 std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames));
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900177 }
Jiyong Park40a60772019-05-03 16:21:31 +0900178
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900179 // Remove the public libs in the runtime namespace.
180 // These libs are listed in public.android.txt, but we don't want the rest of android
181 // in default namespace to dlopen the libs.
182 // For example, libicuuc.so is exposed to classloader namespace from runtime namespace.
183 // Unfortunately, it does not have stable C symbols, and default namespace should only use
184 // stable symbols in libandroidicu.so. http://b/120786417
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100185 for (const std::string& lib_name : kArtApexPublicLibraries) {
186 std::string path(kArtApexLibPath);
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900187 path.append("/").append(lib_name);
188
189 struct stat s;
190 // Do nothing if the path in /apex does not exist.
191 // Runtime APEX must be mounted since libnativeloader is in the same APEX
192 if (stat(path.c_str(), &s) != 0) {
193 continue;
Jiyong Park40a60772019-05-03 16:21:31 +0900194 }
195
Jiyong Park8f4afc82019-07-22 14:20:40 +0900196 auto it = std::find(sonames->begin(), sonames->end(), lib_name);
197 if (it != sonames->end()) {
198 sonames->erase(it);
Jiyong Park40a60772019-05-03 16:21:31 +0900199 }
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900200 }
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100201
202 // Remove the public libs in the nnapi namespace.
Jiyong Park8f4afc82019-07-22 14:20:40 +0900203 auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary);
204 if (it != sonames->end()) {
205 sonames->erase(it);
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100206 }
Jiyong Park8f4afc82019-07-22 14:20:40 +0900207 return android::base::Join(*sonames, ':');
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900208}
209
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100210static std::string InitArtPublicLibraries() {
211 CHECK(sizeof(kArtApexPublicLibraries) > 0);
212 std::string list = android::base::Join(kArtApexPublicLibraries, ":");
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900213
214 std::string additional_libs = additional_public_libraries();
215 if (!additional_libs.empty()) {
216 list = list + ':' + additional_libs;
Jiyong Park40a60772019-05-03 16:21:31 +0900217 }
218 return list;
219}
220
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900221static std::string InitVendorPublicLibraries() {
222 // This file is optional, quietly ignore if the file does not exist.
Jiyong Park8f4afc82019-07-22 14:20:40 +0900223 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
224 if (!sonames) {
225 return "";
226 }
227 return android::base::Join(*sonames, ':');
Jiyong Park40a60772019-05-03 16:21:31 +0900228}
229
Jiyong Park5b8b3062019-05-03 18:11:49 +0900230// read /system/etc/public.libraries-<companyname>.txt and
231// /product/etc/public.libraries-<companyname>.txt which contain partner defined
Jiyong Park40a60772019-05-03 16:21:31 +0900232// system libs that are exposed to apps. The libs in the txt files must be
233// named as lib<name>.<companyname>.so.
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900234static std::string InitExtendedPublicLibraries() {
235 std::vector<std::string> sonames;
236 ReadExtensionLibraries("/system/etc", &sonames);
237 ReadExtensionLibraries("/product/etc", &sonames);
238 return android::base::Join(sonames, ':');
239}
240
241static std::string InitLlndkLibraries() {
242 std::string config_file = kLlndkLibrariesFile;
243 InsertVndkVersionStr(&config_file);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900244 auto sonames = ReadConfig(config_file, always_true);
245 if (!sonames) {
246 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
247 return "";
248 }
249 return android::base::Join(*sonames, ':');
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900250}
251
252static std::string InitVndkspLibraries() {
253 std::string config_file = kVndkLibrariesFile;
254 InsertVndkVersionStr(&config_file);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900255 auto sonames = ReadConfig(config_file, always_true);
256 if (!sonames) {
257 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
258 return "";
259 }
260 return android::base::Join(*sonames, ':');
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900261}
262
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100263static std::string InitNeuralNetworksPublicLibraries() {
264 return kNeuralNetworksApexPublicLibrary;
265}
266
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900267} // namespace
268
Jiyong Park5db5d192019-07-22 20:29:08 +0900269const std::string& preloadable_public_libraries() {
270 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true);
271 return list;
272}
273
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900274const std::string& default_public_libraries() {
Jiyong Park5db5d192019-07-22 20:29:08 +0900275 static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false);
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900276 return list;
277}
278
279const std::string& runtime_public_libraries() {
Martin Stjernholmdf96e1f2019-07-17 22:17:58 +0100280 static std::string list = InitArtPublicLibraries();
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900281 return list;
282}
283
284const std::string& vendor_public_libraries() {
285 static std::string list = InitVendorPublicLibraries();
286 return list;
287}
288
Jiyong Park5b8b3062019-05-03 18:11:49 +0900289const std::string& extended_public_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900290 static std::string list = InitExtendedPublicLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900291 return list;
292}
293
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100294const std::string& neuralnetworks_public_libraries() {
295 static std::string list = InitNeuralNetworksPublicLibraries();
296 return list;
297}
298
Jiyong Park5b8b3062019-05-03 18:11:49 +0900299const std::string& llndk_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900300 static std::string list = InitLlndkLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900301 return list;
302}
303
Jiyong Park5b8b3062019-05-03 18:11:49 +0900304const std::string& vndksp_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900305 static std::string list = InitVndkspLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900306 return list;
307}
308
Jiyong Park5db5d192019-07-22 20:29:08 +0900309namespace internal {
310// Exported for testing
311Result<std::vector<std::string>> ParseConfig(
312 const std::string& file_content,
313 const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) {
314 std::vector<std::string> lines = base::Split(file_content, "\n");
315
316 std::vector<std::string> sonames;
317 for (auto& line : lines) {
318 auto trimmed_line = base::Trim(line);
319 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
320 continue;
321 }
322
323 std::vector<std::string> tokens = android::base::Split(trimmed_line, " ");
324 if (tokens.size() < 1 || tokens.size() > 3) {
325 return Errorf("Malformed line \"{}\"", line);
326 }
327 struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL};
328 size_t i = tokens.size();
329 while (i-- > 0) {
330 if (tokens[i] == "nopreload") {
331 entry.nopreload = true;
332 } else if (tokens[i] == "32" || tokens[i] == "64") {
333 if (entry.bitness != ALL) {
334 return Errorf("Malformed line \"{}\": bitness can be specified only once", line);
335 }
336 entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64;
337 } else {
338 if (i != 0) {
339 return Errorf("Malformed line \"{}\"", line);
340 }
341 entry.soname = tokens[i];
342 }
343 }
344
345 // skip 32-bit lib on 64-bit process and vice versa
346#if defined(__LP64__)
347 if (entry.bitness == ONLY_32) continue;
348#else
349 if (entry.bitness == ONLY_64) continue;
350#endif
351
352 Result<bool> ret = filter_fn(entry);
353 if (!ret) {
354 return ret.error();
355 }
356 if (*ret) {
357 // filter_fn has returned true.
358 sonames.push_back(entry.soname);
359 }
360 }
361 return sonames;
362}
363
364} // namespace internal
365
Jiyong Park40a60772019-05-03 16:21:31 +0900366} // namespace android::nativeloader