blob: c473f2cabf70965975d7972a9346637e6fff5005 [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
17#include "public_libraries.h"
18#define LOG_TAG "nativeloader"
19
20#include <dirent.h>
21
22#include <algorithm>
23#include <memory>
24
25#include "android-base/file.h"
26#include "android-base/logging.h"
27#include "android-base/properties.h"
28#include "android-base/strings.h"
29#include "log/log.h"
Jiyong Parkf8802e52019-05-03 16:34:56 +090030#include "utils.h"
Jiyong Park40a60772019-05-03 16:21:31 +090031
32namespace android::nativeloader {
33
34using namespace std::string_literals;
35
36namespace {
Jiyong Park5b8b3062019-05-03 18:11:49 +090037
38constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
39constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
40constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
41constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
42constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
43constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
Jiyong Park40a60772019-05-03 16:21:31 +090044
45const std::vector<const std::string> kRuntimePublicLibraries = {
46 "libicuuc.so",
47 "libicui18n.so",
48};
49
Jiyong Parkf8802e52019-05-03 16:34:56 +090050constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB;
Jiyong Park40a60772019-05-03 16:21:31 +090051
Jiyong Park5b8b3062019-05-03 18:11:49 +090052// TODO(b/130388701): do we need this?
Jiyong Park40a60772019-05-03 16:21:31 +090053std::string root_dir() {
54 static const char* android_root_env = getenv("ANDROID_ROOT");
55 return android_root_env != nullptr ? android_root_env : "/system";
56}
57
58bool debuggable() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090059 static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
Jiyong Park40a60772019-05-03 16:21:31 +090060 return debuggable;
61}
62
63std::string vndk_version_str() {
Jiyong Park5b8b3062019-05-03 18:11:49 +090064 static std::string version = android::base::GetProperty("ro.vndk.version", "");
Jiyong Park40a60772019-05-03 16:21:31 +090065 if (version != "" && version != "current") {
66 return "." + version;
67 }
68 return "";
69}
70
Jiyong Park5b8b3062019-05-03 18:11:49 +090071void InsertVndkVersionStr(std::string* file_name) {
Jiyong Park40a60772019-05-03 16:21:31 +090072 CHECK(file_name != nullptr);
73 size_t insert_pos = file_name->find_last_of(".");
74 if (insert_pos == std::string::npos) {
75 insert_pos = file_name->length();
76 }
77 file_name->insert(insert_pos, vndk_version_str());
78}
79
80const std::function<bool(const std::string&, std::string*)> always_true =
81 [](const std::string&, std::string*) { return true; };
82
83bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
84 const std::function<bool(const std::string& /* soname */,
85 std::string* /* error_msg */)>& check_soname,
86 std::string* error_msg = nullptr) {
87 // Read list of public native libraries from the config file.
88 std::string file_content;
89 if (!base::ReadFileToString(configFile, &file_content)) {
90 if (error_msg) *error_msg = strerror(errno);
91 return false;
92 }
93
94 std::vector<std::string> lines = base::Split(file_content, "\n");
95
96 for (auto& line : lines) {
97 auto trimmed_line = base::Trim(line);
98 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
99 continue;
100 }
101 size_t space_pos = trimmed_line.rfind(' ');
102 if (space_pos != std::string::npos) {
103 std::string type = trimmed_line.substr(space_pos + 1);
104 if (type != "32" && type != "64") {
105 if (error_msg) *error_msg = "Malformed line: " + line;
106 return false;
107 }
108#if defined(__LP64__)
109 // Skip 32 bit public library.
110 if (type == "32") {
111 continue;
112 }
113#else
114 // Skip 64 bit public library.
115 if (type == "64") {
116 continue;
117 }
118#endif
119 trimmed_line.resize(space_pos);
120 }
121
122 if (check_soname(trimmed_line, error_msg)) {
123 sonames->push_back(trimmed_line);
124 } else {
125 return false;
126 }
127 }
128 return true;
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);
Jiyong Park5b8b3062019-05-03 18:11:49 +0900141 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);
Jiyong Park40a60772019-05-03 16:21:31 +0900145 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 std::string error_msg;
152
153 LOG_ALWAYS_FATAL_IF(
154 !ReadConfig(config_file_path, sonames,
155 [&company_name](const std::string& soname, std::string* error_msg) {
156 if (android::base::StartsWith(soname, "lib") &&
157 android::base::EndsWith(soname, "." + company_name + ".so")) {
158 return true;
159 } else {
160 *error_msg = "Library name \"" + soname +
161 "\" does not end with the company name: " + company_name +
162 ".";
163 return false;
164 }
165 },
166 &error_msg),
167 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
168 error_msg.c_str());
169 }
170 }
171 }
172}
173
174} // namespace
175
Jiyong Park5b8b3062019-05-03 18:11:49 +0900176const std::string& default_public_libraries() {
Jiyong Park40a60772019-05-03 16:21:31 +0900177 static bool cached = false;
178 static std::string list;
179 if (!cached) {
Jiyong Park5b8b3062019-05-03 18:11:49 +0900180 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
Jiyong Park40a60772019-05-03 16:21:31 +0900181 std::vector<std::string> sonames;
182 std::string error_msg;
183 LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg),
184 "Error reading public native library list from \"%s\": %s",
185 config_file.c_str(), error_msg.c_str());
186
187 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
188 // variable to add libraries to the list. This is intended for platform tests only.
189 if (debuggable()) {
190 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
191 if (additional_libs != nullptr && additional_libs[0] != '\0') {
192 std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":");
193 std::copy(additional_libs_vector.begin(), additional_libs_vector.end(),
194 std::back_inserter(sonames));
195 }
196 }
197
198 // Remove the public libs in the runtime namespace.
199 // These libs are listed in public.android.txt, but we don't want the rest of android
200 // in default namespace to dlopen the libs.
201 // For example, libicuuc.so is exposed to classloader namespace from runtime namespace.
202 // Unfortunately, it does not have stable C symbols, and default namespace should only use
203 // stable symbols in libandroidicu.so. http://b/120786417
204 for (const std::string& lib_name : kRuntimePublicLibraries) {
205 std::string path(kRuntimeApexLibPath);
206 path.append("/").append(lib_name);
207
208 struct stat s;
209 // Do nothing if the path in /apex does not exist.
210 // Runtime APEX must be mounted since libnativeloader is in the same APEX
211 if (stat(path.c_str(), &s) != 0) {
212 continue;
213 }
214
215 auto it = std::find(sonames.begin(), sonames.end(), lib_name);
216 if (it != sonames.end()) {
217 sonames.erase(it);
218 }
219 }
220 list = android::base::Join(sonames, ':');
221 cached = true;
222 }
223 return list;
224}
225
226const std::string& runtime_public_libraries() {
227 static bool cached = false;
228 static std::string list;
229 if (!cached) {
230 list = android::base::Join(kRuntimePublicLibraries, ":");
231 // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
232 // variable to add libraries to the list. This is intended for platform tests only.
233 if (debuggable()) {
234 const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
235 if (additional_libs != nullptr && additional_libs[0] != '\0') {
236 list = list + ':' + additional_libs;
237 }
238 }
239 }
240 return list;
241}
242
243const std::string& vendor_public_libraries() {
244 static bool cached = false;
245 static std::string list;
246 if (!cached) {
247 // This file is optional, quietly ignore if the file does not exist.
248 std::vector<std::string> sonames;
Jiyong Park5b8b3062019-05-03 18:11:49 +0900249 ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr);
Jiyong Park40a60772019-05-03 16:21:31 +0900250 list = android::base::Join(sonames, ':');
251 cached = true;
252 }
253 return list;
254}
255
Jiyong Park5b8b3062019-05-03 18:11:49 +0900256// read /system/etc/public.libraries-<companyname>.txt and
257// /product/etc/public.libraries-<companyname>.txt which contain partner defined
Jiyong Park40a60772019-05-03 16:21:31 +0900258// system libs that are exposed to apps. The libs in the txt files must be
259// named as lib<name>.<companyname>.so.
Jiyong Park5b8b3062019-05-03 18:11:49 +0900260const std::string& extended_public_libraries() {
Jiyong Park40a60772019-05-03 16:21:31 +0900261 static bool cached = false;
262 static std::string list;
263 if (!cached) {
264 std::vector<std::string> sonames;
265 ReadExtensionLibraries("/system/etc", &sonames);
Jiyong Park40a60772019-05-03 16:21:31 +0900266 ReadExtensionLibraries("/product/etc", &sonames);
267 list = android::base::Join(sonames, ':');
268 cached = true;
269 }
270 return list;
271}
272
Jiyong Park5b8b3062019-05-03 18:11:49 +0900273const std::string& llndk_libraries() {
Jiyong Park40a60772019-05-03 16:21:31 +0900274 static bool cached = false;
275 static std::string list;
276 if (!cached) {
Jiyong Park5b8b3062019-05-03 18:11:49 +0900277 std::string config_file = kLlndkLibrariesFile;
278 InsertVndkVersionStr(&config_file);
Jiyong Park40a60772019-05-03 16:21:31 +0900279 std::vector<std::string> sonames;
280 ReadConfig(config_file, &sonames, always_true, nullptr);
281 list = android::base::Join(sonames, ':');
282 cached = true;
283 }
284 return list;
285}
286
Jiyong Park5b8b3062019-05-03 18:11:49 +0900287const std::string& vndksp_libraries() {
Jiyong Park40a60772019-05-03 16:21:31 +0900288 static bool cached = false;
289 static std::string list;
290 if (!cached) {
Jiyong Park5b8b3062019-05-03 18:11:49 +0900291 std::string config_file = kVndkLibrariesFile;
292 InsertVndkVersionStr(&config_file);
Jiyong Park40a60772019-05-03 16:21:31 +0900293 std::vector<std::string> sonames;
294 ReadConfig(config_file, &sonames, always_true, nullptr);
295 list = android::base::Join(sonames, ':');
296 cached = true;
297 }
298 return list;
299}
300
301} // namespace android::nativeloader