blob: c205eb133894ae13d58bf42fdfb1240a36947ddd [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>
29#include <android-base/strings.h>
30#include <log/log.h>
31
Jiyong Parkf8802e52019-05-03 16:34:56 +090032#include "utils.h"
Jiyong Park40a60772019-05-03 16:21:31 +090033
34namespace android::nativeloader {
35
36using namespace std::string_literals;
37
38namespace {
Jiyong Park5b8b3062019-05-03 18:11:49 +090039
40constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
41constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
42constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
43constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
44constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
45constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
Jiyong Park40a60772019-05-03 16:21:31 +090046
47const std::vector<const std::string> kRuntimePublicLibraries = {
48 "libicuuc.so",
49 "libicui18n.so",
50};
51
Jiyong Parkf8802e52019-05-03 16:34:56 +090052constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB;
Jiyong Park40a60772019-05-03 16:21:31 +090053
Jiyong Park5b8b3062019-05-03 18:11:49 +090054// TODO(b/130388701): do we need this?
Jiyong Park40a60772019-05-03 16:21:31 +090055std::string root_dir() {
56 static const char* android_root_env = getenv("ANDROID_ROOT");
57 return android_root_env != nullptr ? android_root_env : "/system";
58}
59
60bool debuggable() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090061 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park40a60772019-05-03 16:21:31 +090062 return debuggable;
63}
64
65std::string vndk_version_str() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090066 static std::string version = android::base::GetProperty("ro.vndk.version", "");
Jiyong Park40a60772019-05-03 16:21:31 +090067 if (version != "" && version != "current") {
68 return "." + version;
69 }
70 return "";
71}
72
Jiyong Park4b5a37c2019-05-09 17:17:37 +090073// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
74// variable to add libraries to the list. This is intended for platform tests only.
75std::string additional_public_libraries() {
76 if (debuggable()) {
77 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
78 return val ? val : "";
79 }
80 return "";
81}
82
Jiyong Park5b8b3062019-05-03 18:11:49 +090083void InsertVndkVersionStr(std::string* file_name) {
Jiyong Park40a60772019-05-03 16:21:31 +090084 CHECK(file_name != nullptr);
85 size_t insert_pos = file_name->find_last_of(".");
86 if (insert_pos == std::string::npos) {
87 insert_pos = file_name->length();
88 }
89 file_name->insert(insert_pos, vndk_version_str());
90}
91
92const std::function<bool(const std::string&, std::string*)> always_true =
93 [](const std::string&, std::string*) { return true; };
94
95bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
96 const std::function<bool(const std::string& /* soname */,
97 std::string* /* error_msg */)>& check_soname,
98 std::string* error_msg = nullptr) {
99 // Read list of public native libraries from the config file.
100 std::string file_content;
101 if (!base::ReadFileToString(configFile, &file_content)) {
102 if (error_msg) *error_msg = strerror(errno);
103 return false;
104 }
105
106 std::vector<std::string> lines = base::Split(file_content, "\n");
107
108 for (auto& line : lines) {
109 auto trimmed_line = base::Trim(line);
110 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
111 continue;
112 }
113 size_t space_pos = trimmed_line.rfind(' ');
114 if (space_pos != std::string::npos) {
115 std::string type = trimmed_line.substr(space_pos + 1);
116 if (type != "32" && type != "64") {
117 if (error_msg) *error_msg = "Malformed line: " + line;
118 return false;
119 }
120#if defined(__LP64__)
121 // Skip 32 bit public library.
122 if (type == "32") {
123 continue;
124 }
125#else
126 // Skip 64 bit public library.
127 if (type == "64") {
128 continue;
129 }
130#endif
131 trimmed_line.resize(space_pos);
132 }
133
134 if (check_soname(trimmed_line, error_msg)) {
135 sonames->push_back(trimmed_line);
136 } else {
137 return false;
138 }
139 }
140 return true;
141}
142
143void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
144 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
145 if (dir != nullptr) {
146 // Failing to opening the dir is not an error, which can happen in
147 // webview_zygote.
148 while (struct dirent* ent = readdir(dir.get())) {
149 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
150 continue;
151 }
152 const std::string filename(ent->d_name);
Jiyong Park5b8b3062019-05-03 18:11:49 +0900153 std::string_view fn = filename;
154 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
155 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
156 const std::string company_name(fn);
Jiyong Park40a60772019-05-03 16:21:31 +0900157 const std::string config_file_path = dirname + "/"s + filename;
158 LOG_ALWAYS_FATAL_IF(
159 company_name.empty(),
160 "Error extracting company name from public native library list file path \"%s\"",
161 config_file_path.c_str());
162
163 std::string error_msg;
164
165 LOG_ALWAYS_FATAL_IF(
166 !ReadConfig(config_file_path, sonames,
167 [&company_name](const std::string& soname, std::string* error_msg) {
168 if (android::base::StartsWith(soname, "lib") &&
169 android::base::EndsWith(soname, "." + company_name + ".so")) {
170 return true;
171 } else {
172 *error_msg = "Library name \"" + soname +
173 "\" does not end with the company name: " + company_name +
174 ".";
175 return false;
176 }
177 },
178 &error_msg),
179 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
180 error_msg.c_str());
181 }
182 }
183 }
184}
185
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900186static std::string InitDefaultPublicLibraries() {
187 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
188 std::vector<std::string> sonames;
189 std::string error_msg;
190 LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg),
191 "Error reading public native library list from \"%s\": %s",
192 config_file.c_str(), error_msg.c_str());
Jiyong Park40a60772019-05-03 16:21:31 +0900193
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900194 std::string additional_libs = additional_public_libraries();
195 if (!additional_libs.empty()) {
196 auto vec = base::Split(additional_libs, ":");
197 std::copy(vec.begin(), vec.end(), std::back_inserter(sonames));
198 }
Jiyong Park40a60772019-05-03 16:21:31 +0900199
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900200 // Remove the public libs in the runtime namespace.
201 // These libs are listed in public.android.txt, but we don't want the rest of android
202 // in default namespace to dlopen the libs.
203 // For example, libicuuc.so is exposed to classloader namespace from runtime namespace.
204 // Unfortunately, it does not have stable C symbols, and default namespace should only use
205 // stable symbols in libandroidicu.so. http://b/120786417
206 for (const std::string& lib_name : kRuntimePublicLibraries) {
207 std::string path(kRuntimeApexLibPath);
208 path.append("/").append(lib_name);
209
210 struct stat s;
211 // Do nothing if the path in /apex does not exist.
212 // Runtime APEX must be mounted since libnativeloader is in the same APEX
213 if (stat(path.c_str(), &s) != 0) {
214 continue;
Jiyong Park40a60772019-05-03 16:21:31 +0900215 }
216
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900217 auto it = std::find(sonames.begin(), sonames.end(), lib_name);
218 if (it != sonames.end()) {
219 sonames.erase(it);
Jiyong Park40a60772019-05-03 16:21:31 +0900220 }
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900221 }
222 return android::base::Join(sonames, ':');
223}
224
225static std::string InitRuntimePublicLibraries() {
226 CHECK(sizeof(kRuntimePublicLibraries) > 0);
227 std::string list = android::base::Join(kRuntimePublicLibraries, ":");
228
229 std::string additional_libs = additional_public_libraries();
230 if (!additional_libs.empty()) {
231 list = list + ':' + additional_libs;
Jiyong Park40a60772019-05-03 16:21:31 +0900232 }
233 return list;
234}
235
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900236static std::string InitVendorPublicLibraries() {
237 // This file is optional, quietly ignore if the file does not exist.
238 std::vector<std::string> sonames;
239 ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr);
240 return android::base::Join(sonames, ':');
Jiyong Park40a60772019-05-03 16:21:31 +0900241}
242
Jiyong Park5b8b3062019-05-03 18:11:49 +0900243// read /system/etc/public.libraries-<companyname>.txt and
244// /product/etc/public.libraries-<companyname>.txt which contain partner defined
Jiyong Park40a60772019-05-03 16:21:31 +0900245// system libs that are exposed to apps. The libs in the txt files must be
246// named as lib<name>.<companyname>.so.
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900247static std::string InitExtendedPublicLibraries() {
248 std::vector<std::string> sonames;
249 ReadExtensionLibraries("/system/etc", &sonames);
250 ReadExtensionLibraries("/product/etc", &sonames);
251 return android::base::Join(sonames, ':');
252}
253
254static std::string InitLlndkLibraries() {
255 std::string config_file = kLlndkLibrariesFile;
256 InsertVndkVersionStr(&config_file);
257 std::vector<std::string> sonames;
258 ReadConfig(config_file, &sonames, always_true, nullptr);
259 return android::base::Join(sonames, ':');
260}
261
262static std::string InitVndkspLibraries() {
263 std::string config_file = kVndkLibrariesFile;
264 InsertVndkVersionStr(&config_file);
265 std::vector<std::string> sonames;
266 ReadConfig(config_file, &sonames, always_true, nullptr);
267 return android::base::Join(sonames, ':');
268}
269
270} // namespace
271
272const std::string& default_public_libraries() {
273 static std::string list = InitDefaultPublicLibraries();
274 return list;
275}
276
277const std::string& runtime_public_libraries() {
278 static std::string list = InitRuntimePublicLibraries();
279 return list;
280}
281
282const std::string& vendor_public_libraries() {
283 static std::string list = InitVendorPublicLibraries();
284 return list;
285}
286
Jiyong Park5b8b3062019-05-03 18:11:49 +0900287const std::string& extended_public_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900288 static std::string list = InitExtendedPublicLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900289 return list;
290}
291
Jiyong Park5b8b3062019-05-03 18:11:49 +0900292const std::string& llndk_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900293 static std::string list = InitLlndkLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900294 return list;
295}
296
Jiyong Park5b8b3062019-05-03 18:11:49 +0900297const std::string& vndksp_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900298 static std::string list = InitVndkspLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900299 return list;
300}
301
302} // namespace android::nativeloader