blob: 64fedaead7e9871355302dab06b88b95770524d9 [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 Park4b5a37c2019-05-09 17:17:37 +090071// For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment
72// variable to add libraries to the list. This is intended for platform tests only.
73std::string additional_public_libraries() {
74 if (debuggable()) {
75 const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES");
76 return val ? val : "";
77 }
78 return "";
79}
80
Jiyong Park5b8b3062019-05-03 18:11:49 +090081void InsertVndkVersionStr(std::string* file_name) {
Jiyong Park40a60772019-05-03 16:21:31 +090082 CHECK(file_name != nullptr);
83 size_t insert_pos = file_name->find_last_of(".");
84 if (insert_pos == std::string::npos) {
85 insert_pos = file_name->length();
86 }
87 file_name->insert(insert_pos, vndk_version_str());
88}
89
90const std::function<bool(const std::string&, std::string*)> always_true =
91 [](const std::string&, std::string*) { return true; };
92
93bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames,
94 const std::function<bool(const std::string& /* soname */,
95 std::string* /* error_msg */)>& check_soname,
96 std::string* error_msg = nullptr) {
97 // Read list of public native libraries from the config file.
98 std::string file_content;
99 if (!base::ReadFileToString(configFile, &file_content)) {
100 if (error_msg) *error_msg = strerror(errno);
101 return false;
102 }
103
104 std::vector<std::string> lines = base::Split(file_content, "\n");
105
106 for (auto& line : lines) {
107 auto trimmed_line = base::Trim(line);
108 if (trimmed_line[0] == '#' || trimmed_line.empty()) {
109 continue;
110 }
111 size_t space_pos = trimmed_line.rfind(' ');
112 if (space_pos != std::string::npos) {
113 std::string type = trimmed_line.substr(space_pos + 1);
114 if (type != "32" && type != "64") {
115 if (error_msg) *error_msg = "Malformed line: " + line;
116 return false;
117 }
118#if defined(__LP64__)
119 // Skip 32 bit public library.
120 if (type == "32") {
121 continue;
122 }
123#else
124 // Skip 64 bit public library.
125 if (type == "64") {
126 continue;
127 }
128#endif
129 trimmed_line.resize(space_pos);
130 }
131
132 if (check_soname(trimmed_line, error_msg)) {
133 sonames->push_back(trimmed_line);
134 } else {
135 return false;
136 }
137 }
138 return true;
139}
140
141void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) {
142 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir);
143 if (dir != nullptr) {
144 // Failing to opening the dir is not an error, which can happen in
145 // webview_zygote.
146 while (struct dirent* ent = readdir(dir.get())) {
147 if (ent->d_type != DT_REG && ent->d_type != DT_LNK) {
148 continue;
149 }
150 const std::string filename(ent->d_name);
Jiyong Park5b8b3062019-05-03 18:11:49 +0900151 std::string_view fn = filename;
152 if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
153 android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
154 const std::string company_name(fn);
Jiyong Park40a60772019-05-03 16:21:31 +0900155 const std::string config_file_path = dirname + "/"s + filename;
156 LOG_ALWAYS_FATAL_IF(
157 company_name.empty(),
158 "Error extracting company name from public native library list file path \"%s\"",
159 config_file_path.c_str());
160
161 std::string error_msg;
162
163 LOG_ALWAYS_FATAL_IF(
164 !ReadConfig(config_file_path, sonames,
165 [&company_name](const std::string& soname, std::string* error_msg) {
166 if (android::base::StartsWith(soname, "lib") &&
167 android::base::EndsWith(soname, "." + company_name + ".so")) {
168 return true;
169 } else {
170 *error_msg = "Library name \"" + soname +
171 "\" does not end with the company name: " + company_name +
172 ".";
173 return false;
174 }
175 },
176 &error_msg),
177 "Error reading public native library list from \"%s\": %s", config_file_path.c_str(),
178 error_msg.c_str());
179 }
180 }
181 }
182}
183
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900184static std::string InitDefaultPublicLibraries() {
185 std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
186 std::vector<std::string> sonames;
187 std::string error_msg;
188 LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg),
189 "Error reading public native library list from \"%s\": %s",
190 config_file.c_str(), error_msg.c_str());
Jiyong Park40a60772019-05-03 16:21:31 +0900191
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900192 std::string additional_libs = additional_public_libraries();
193 if (!additional_libs.empty()) {
194 auto vec = base::Split(additional_libs, ":");
195 std::copy(vec.begin(), vec.end(), std::back_inserter(sonames));
196 }
Jiyong Park40a60772019-05-03 16:21:31 +0900197
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900198 // 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;
Jiyong Park40a60772019-05-03 16:21:31 +0900213 }
214
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900215 auto it = std::find(sonames.begin(), sonames.end(), lib_name);
216 if (it != sonames.end()) {
217 sonames.erase(it);
Jiyong Park40a60772019-05-03 16:21:31 +0900218 }
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900219 }
220 return android::base::Join(sonames, ':');
221}
222
223static std::string InitRuntimePublicLibraries() {
224 CHECK(sizeof(kRuntimePublicLibraries) > 0);
225 std::string list = android::base::Join(kRuntimePublicLibraries, ":");
226
227 std::string additional_libs = additional_public_libraries();
228 if (!additional_libs.empty()) {
229 list = list + ':' + additional_libs;
Jiyong Park40a60772019-05-03 16:21:31 +0900230 }
231 return list;
232}
233
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900234static std::string InitVendorPublicLibraries() {
235 // This file is optional, quietly ignore if the file does not exist.
236 std::vector<std::string> sonames;
237 ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr);
238 return android::base::Join(sonames, ':');
Jiyong Park40a60772019-05-03 16:21:31 +0900239}
240
Jiyong Park5b8b3062019-05-03 18:11:49 +0900241// read /system/etc/public.libraries-<companyname>.txt and
242// /product/etc/public.libraries-<companyname>.txt which contain partner defined
Jiyong Park40a60772019-05-03 16:21:31 +0900243// system libs that are exposed to apps. The libs in the txt files must be
244// named as lib<name>.<companyname>.so.
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900245static std::string InitExtendedPublicLibraries() {
246 std::vector<std::string> sonames;
247 ReadExtensionLibraries("/system/etc", &sonames);
248 ReadExtensionLibraries("/product/etc", &sonames);
249 return android::base::Join(sonames, ':');
250}
251
252static std::string InitLlndkLibraries() {
253 std::string config_file = kLlndkLibrariesFile;
254 InsertVndkVersionStr(&config_file);
255 std::vector<std::string> sonames;
256 ReadConfig(config_file, &sonames, always_true, nullptr);
257 return android::base::Join(sonames, ':');
258}
259
260static std::string InitVndkspLibraries() {
261 std::string config_file = kVndkLibrariesFile;
262 InsertVndkVersionStr(&config_file);
263 std::vector<std::string> sonames;
264 ReadConfig(config_file, &sonames, always_true, nullptr);
265 return android::base::Join(sonames, ':');
266}
267
268} // namespace
269
270const std::string& default_public_libraries() {
271 static std::string list = InitDefaultPublicLibraries();
272 return list;
273}
274
275const std::string& runtime_public_libraries() {
276 static std::string list = InitRuntimePublicLibraries();
277 return list;
278}
279
280const std::string& vendor_public_libraries() {
281 static std::string list = InitVendorPublicLibraries();
282 return list;
283}
284
Jiyong Park5b8b3062019-05-03 18:11:49 +0900285const std::string& extended_public_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900286 static std::string list = InitExtendedPublicLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900287 return list;
288}
289
Jiyong Park5b8b3062019-05-03 18:11:49 +0900290const std::string& llndk_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900291 static std::string list = InitLlndkLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900292 return list;
293}
294
Jiyong Park5b8b3062019-05-03 18:11:49 +0900295const std::string& vndksp_libraries() {
Jiyong Park4b5a37c2019-05-09 17:17:37 +0900296 static std::string list = InitVndkspLibraries();
Jiyong Park40a60772019-05-03 16:21:31 +0900297 return list;
298}
299
300} // namespace android::nativeloader