blob: 6cee668a12db552fe4e08352ec7a78f8836d1ac2 [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
37using namespace std::string_literals;
Jiyong Park8f4afc82019-07-22 14:20:40 +090038using android::base::ErrnoError;
39using android::base::Errorf;
40using android::base::Result;
Jiyong Park40a60772019-05-03 16:21:31 +090041
42namespace {
Jiyong Park5b8b3062019-05-03 18:11:49 +090043
44constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
45constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
46constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
47constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
48constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
49constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
Jiyong Park40a60772019-05-03 16:21:31 +090050
51const std::vector<const std::string> kRuntimePublicLibraries = {
52 "libicuuc.so",
53 "libicui18n.so",
54};
55
Jiyong Parkf8802e52019-05-03 16:34:56 +090056constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB;
Jiyong Park40a60772019-05-03 16:21:31 +090057
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +010058constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so";
59
Jiyong Park5b8b3062019-05-03 18:11:49 +090060// TODO(b/130388701): do we need this?
Jiyong Park40a60772019-05-03 16:21:31 +090061std::string root_dir() {
62 static const char* android_root_env = getenv("ANDROID_ROOT");
63 return android_root_env != nullptr ? android_root_env : "/system";
64}
65
66bool debuggable() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090067 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park40a60772019-05-03 16:21:31 +090068 return debuggable;
69}
70
71std::string vndk_version_str() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090072 static std::string version = android::base::GetProperty("ro.vndk.version", "");
Jiyong Park40a60772019-05-03 16:21:31 +090073 if (version != "" && version != "current") {
74 return "." + version;
75 }
76 return "";
77}
78
Jiyong Park4b5a37c2019-05-09 17:17:37 +090079// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
80// variable to add libraries to the list. This is intended for platform tests only.
81std::string additional_public_libraries() {
82 if (debuggable()) {
83 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
84 return val ? val : "";
85 }
86 return "";
87}
88
Jiyong Park5b8b3062019-05-03 18:11:49 +090089void InsertVndkVersionStr(std::string* file_name) {
Jiyong Park40a60772019-05-03 16:21:31 +090090 CHECK(file_name != nullptr);
91 size_t insert_pos = file_name->find_last_of(".");
92 if (insert_pos == std::string::npos) {
93 insert_pos = file_name->length();
94 }
95 file_name->insert(insert_pos, vndk_version_str());
96}
97
Jiyong Park8f4afc82019-07-22 14:20:40 +090098const std::function<Result<void>(const std::string&)> always_true =
99 [](const std::string&) -> Result<void> { return {}; };
Jiyong Park40a60772019-05-03 16:21:31 +0900100
Jiyong Park8f4afc82019-07-22 14:20:40 +0900101Result<std::vector<std::string>> ReadConfig(
102 const std::string& configFile,
103 const std::function<Result<void>(const std::string& /* soname */)>& check_soname) {
Jiyong Park40a60772019-05-03 16:21:31 +0900104 // Read list of public native libraries from the config file.
105 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 }
109
110 std::vector<std::string> lines = base::Split(file_content, "\n");
111
Jiyong Park8f4afc82019-07-22 14:20:40 +0900112 std::vector<std::string> sonames;
Jiyong Park40a60772019-05-03 16:21:31 +0900113 for (auto& line : lines) {
114 auto trimmed_line = base::Trim(line);
115 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
116 continue;
117 }
118 size_t space_pos = trimmed_line.rfind(' ');
119 if (space_pos != std::string::npos) {
120 std::string type = trimmed_line.substr(space_pos + 1);
121 if (type != "32" && type != "64") {
Jiyong Park8f4afc82019-07-22 14:20:40 +0900122 return Errorf("Malformed line: {}", line);
Jiyong Park40a60772019-05-03 16:21:31 +0900123 }
124#if defined(__LP64__)
125 // Skip 32 bit public library.
126 if (type == "32") {
127 continue;
128 }
129#else
130 // Skip 64 bit public library.
131 if (type == "64") {
132 continue;
133 }
134#endif
135 trimmed_line.resize(space_pos);
136 }
137
Jiyong Park8f4afc82019-07-22 14:20:40 +0900138 auto ret = check_soname(trimmed_line);
139 if (!ret) {
140 return ret.error();
Jiyong Park40a60772019-05-03 16:21:31 +0900141 }
Jiyong Park8f4afc82019-07-22 14:20:40 +0900142 sonames.push_back(trimmed_line);
Jiyong Park40a60772019-05-03 16:21:31 +0900143 }
Jiyong Park8f4afc82019-07-22 14:20:40 +0900144 return sonames;
Jiyong Park40a60772019-05-03 16:21:31 +0900145}
146
147void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
148 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
149 if (dir != nullptr) {
150 // Failing to opening the dir is not an error, which can happen in
151 // webview_zygote.
152 while (struct dirent* ent = readdir(dir.get())) {
153 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
154 continue;
155 }
156 const std::string filename(ent->d_name);
Jiyong Park5b8b3062019-05-03 18:11:49 +0900157 std::string_view fn = filename;
158 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
159 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
160 const std::string company_name(fn);
Jiyong Park40a60772019-05-03 16:21:31 +0900161 const std::string config_file_path = dirname + "/"s + filename;
162 LOG_ALWAYS_FATAL_IF(
163 company_name.empty(),
164 "Error extracting company name from public native library list file path \"%s\"",
165 config_file_path.c_str());
166
Jiyong Park8f4afc82019-07-22 14:20:40 +0900167 auto ret = ReadConfig(
168 config_file_path, [&company_name](const std::string& soname) -> Result<void> {
169 if (android::base::StartsWith(soname, "lib") &&
170 android::base::EndsWith(soname, "." + company_name + ".so")) {
171 return {};
172 } else {
173 return Errorf("Library name \"{}\" does not end with the company name {}.", soname,
174 company_name);
175 }
176 });
177 if (ret) {
178 sonames->insert(sonames->end(), ret->begin(), ret->end());
179 } else {
180 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
181 config_file_path.c_str(), ret.error().message().c_str());
182 }
Jiyong Park40a60772019-05-03 16:21:31 +0900183 }
184 }
185 }
186}
187
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900188static std::string InitDefaultPublicLibraries() {
189 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
Jiyong Park8f4afc82019-07-22 14:20:40 +0900190 auto sonames = ReadConfig(config_file, always_true);
191 if (!sonames) {
192 LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s",
193 config_file.c_str(), sonames.error().message().c_str());
194 return "";
195 }
Jiyong Park40a60772019-05-03 16:21:31 +0900196
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900197 std::string additional_libs = additional_public_libraries();
198 if (!additional_libs.empty()) {
199 auto vec = base::Split(additional_libs, ":");
Jiyong Park8f4afc82019-07-22 14:20:40 +0900200 std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames));
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900201 }
Jiyong Park40a60772019-05-03 16:21:31 +0900202
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900203 // Remove the public libs in the runtime namespace.
204 // These libs are listed in public.android.txt, but we don't want the rest of android
205 // in default namespace to dlopen the libs.
206 // For example, libicuuc.so is exposed to classloader namespace from runtime namespace.
207 // Unfortunately, it does not have stable C symbols, and default namespace should only use
208 // stable symbols in libandroidicu.so. http://b/120786417
209 for (const std::string& lib_name : kRuntimePublicLibraries) {
210 std::string path(kRuntimeApexLibPath);
211 path.append("/").append(lib_name);
212
213 struct stat s;
214 // Do nothing if the path in /apex does not exist.
215 // Runtime APEX must be mounted since libnativeloader is in the same APEX
216 if (stat(path.c_str(), &s) != 0) {
217 continue;
Jiyong Park40a60772019-05-03 16:21:31 +0900218 }
219
Jiyong Park8f4afc82019-07-22 14:20:40 +0900220 auto it = std::find(sonames->begin(), sonames->end(), lib_name);
221 if (it != sonames->end()) {
222 sonames->erase(it);
Jiyong Park40a60772019-05-03 16:21:31 +0900223 }
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900224 }
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100225
226 // Remove the public libs in the nnapi namespace.
Jiyong Park8f4afc82019-07-22 14:20:40 +0900227 auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary);
228 if (it != sonames->end()) {
229 sonames->erase(it);
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100230 }
Jiyong Park8f4afc82019-07-22 14:20:40 +0900231 return android::base::Join(*sonames, ':');
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900232}
233
234static std::string InitRuntimePublicLibraries() {
235 CHECK(sizeof(kRuntimePublicLibraries) > 0);
236 std::string list = android::base::Join(kRuntimePublicLibraries, ":");
237
238 std::string additional_libs = additional_public_libraries();
239 if (!additional_libs.empty()) {
240 list = list + ':' + additional_libs;
Jiyong Park40a60772019-05-03 16:21:31 +0900241 }
242 return list;
243}
244
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900245static std::string InitVendorPublicLibraries() {
246 // This file is optional, quietly ignore if the file does not exist.
Jiyong Park8f4afc82019-07-22 14:20:40 +0900247 auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true);
248 if (!sonames) {
249 return "";
250 }
251 return android::base::Join(*sonames, ':');
Jiyong Park40a60772019-05-03 16:21:31 +0900252}
253
Jiyong Park5b8b3062019-05-03 18:11:49 +0900254// read /system/etc/public.libraries-<companyname>.txt and
255// /product/etc/public.libraries-<companyname>.txt which contain partner defined
Jiyong Park40a60772019-05-03 16:21:31 +0900256// system libs that are exposed to apps. The libs in the txt files must be
257// named as lib<name>.<companyname>.so.
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900258static std::string InitExtendedPublicLibraries() {
259 std::vector<std::string> sonames;
260 ReadExtensionLibraries("/system/etc", &sonames);
261 ReadExtensionLibraries("/product/etc", &sonames);
262 return android::base::Join(sonames, ':');
263}
264
265static std::string InitLlndkLibraries() {
266 std::string config_file = kLlndkLibrariesFile;
267 InsertVndkVersionStr(&config_file);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900268 auto sonames = ReadConfig(config_file, always_true);
269 if (!sonames) {
270 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
271 return "";
272 }
273 return android::base::Join(*sonames, ':');
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900274}
275
276static std::string InitVndkspLibraries() {
277 std::string config_file = kVndkLibrariesFile;
278 InsertVndkVersionStr(&config_file);
Jiyong Park8f4afc82019-07-22 14:20:40 +0900279 auto sonames = ReadConfig(config_file, always_true);
280 if (!sonames) {
281 LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str());
282 return "";
283 }
284 return android::base::Join(*sonames, ':');
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900285}
286
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100287static std::string InitNeuralNetworksPublicLibraries() {
288 return kNeuralNetworksApexPublicLibrary;
289}
290
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900291} // namespace
292
293const std::string& default_public_libraries() {
294 static std::string list = InitDefaultPublicLibraries();
295 return list;
296}
297
298const std::string& runtime_public_libraries() {
299 static std::string list = InitRuntimePublicLibraries();
300 return list;
301}
302
303const std::string& vendor_public_libraries() {
304 static std::string list = InitVendorPublicLibraries();
305 return list;
306}
307
Jiyong Park5b8b3062019-05-03 18:11:49 +0900308const std::string& extended_public_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900309 static std::string list = InitExtendedPublicLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900310 return list;
311}
312
Przemyslaw Szczepaniak0bb871d2019-07-10 12:08:57 +0100313const std::string& neuralnetworks_public_libraries() {
314 static std::string list = InitNeuralNetworksPublicLibraries();
315 return list;
316}
317
Jiyong Park5b8b3062019-05-03 18:11:49 +0900318const std::string& llndk_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900319 static std::string list = InitLlndkLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900320 return list;
321}
322
Jiyong Park5b8b3062019-05-03 18:11:49 +0900323const std::string& vndksp_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900324 static std::string list = InitVndkspLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900325 return list;
326}
327
328} // namespace android::nativeloader