Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 1 | /* |
| 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 Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 17 | #define LOG_TAG "nativeloader" |
| 18 | |
Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 19 | #include "public_libraries.h" |
| 20 | |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <memory> |
| 25 | |
Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 26 | #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 Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 32 | #include "utils.h" |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 33 | |
| 34 | namespace android::nativeloader { |
| 35 | |
| 36 | using namespace std::string_literals; |
| 37 | |
| 38 | namespace { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 39 | |
| 40 | constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt"; |
| 41 | constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-"; |
| 42 | constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt"; |
| 43 | constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt"; |
| 44 | constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt"; |
| 45 | constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt"; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 46 | |
| 47 | const std::vector<const std::string> kRuntimePublicLibraries = { |
| 48 | "libicuuc.so", |
| 49 | "libicui18n.so", |
| 50 | }; |
| 51 | |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 52 | constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 53 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame^] | 54 | constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so"; |
| 55 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 56 | // TODO(b/130388701): do we need this? |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 57 | std::string root_dir() { |
| 58 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 59 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 60 | } |
| 61 | |
| 62 | bool debuggable() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 63 | static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 64 | return debuggable; |
| 65 | } |
| 66 | |
| 67 | std::string vndk_version_str() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 68 | static std::string version = android::base::GetProperty("ro.vndk.version", ""); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 69 | if (version != "" && version != "current") { |
| 70 | return "." + version; |
| 71 | } |
| 72 | return ""; |
| 73 | } |
| 74 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 75 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 76 | // variable to add libraries to the list. This is intended for platform tests only. |
| 77 | std::string additional_public_libraries() { |
| 78 | if (debuggable()) { |
| 79 | const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 80 | return val ? val : ""; |
| 81 | } |
| 82 | return ""; |
| 83 | } |
| 84 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 85 | void InsertVndkVersionStr(std::string* file_name) { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 86 | CHECK(file_name != nullptr); |
| 87 | size_t insert_pos = file_name->find_last_of("."); |
| 88 | if (insert_pos == std::string::npos) { |
| 89 | insert_pos = file_name->length(); |
| 90 | } |
| 91 | file_name->insert(insert_pos, vndk_version_str()); |
| 92 | } |
| 93 | |
| 94 | const std::function<bool(const std::string&, std::string*)> always_true = |
| 95 | [](const std::string&, std::string*) { return true; }; |
| 96 | |
| 97 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
| 98 | const std::function<bool(const std::string& /* soname */, |
| 99 | std::string* /* error_msg */)>& check_soname, |
| 100 | std::string* error_msg = nullptr) { |
| 101 | // Read list of public native libraries from the config file. |
| 102 | std::string file_content; |
| 103 | if (!base::ReadFileToString(configFile, &file_content)) { |
| 104 | if (error_msg) *error_msg = strerror(errno); |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 109 | |
| 110 | for (auto& line : lines) { |
| 111 | auto trimmed_line = base::Trim(line); |
| 112 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 113 | continue; |
| 114 | } |
| 115 | size_t space_pos = trimmed_line.rfind(' '); |
| 116 | if (space_pos != std::string::npos) { |
| 117 | std::string type = trimmed_line.substr(space_pos + 1); |
| 118 | if (type != "32" && type != "64") { |
| 119 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 120 | return false; |
| 121 | } |
| 122 | #if defined(__LP64__) |
| 123 | // Skip 32 bit public library. |
| 124 | if (type == "32") { |
| 125 | continue; |
| 126 | } |
| 127 | #else |
| 128 | // Skip 64 bit public library. |
| 129 | if (type == "64") { |
| 130 | continue; |
| 131 | } |
| 132 | #endif |
| 133 | trimmed_line.resize(space_pos); |
| 134 | } |
| 135 | |
| 136 | if (check_soname(trimmed_line, error_msg)) { |
| 137 | sonames->push_back(trimmed_line); |
| 138 | } else { |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 146 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 147 | if (dir != nullptr) { |
| 148 | // Failing to opening the dir is not an error, which can happen in |
| 149 | // webview_zygote. |
| 150 | while (struct dirent* ent = readdir(dir.get())) { |
| 151 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 152 | continue; |
| 153 | } |
| 154 | const std::string filename(ent->d_name); |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 155 | std::string_view fn = filename; |
| 156 | if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) && |
| 157 | android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) { |
| 158 | const std::string company_name(fn); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 159 | const std::string config_file_path = dirname + "/"s + filename; |
| 160 | LOG_ALWAYS_FATAL_IF( |
| 161 | company_name.empty(), |
| 162 | "Error extracting company name from public native library list file path \"%s\"", |
| 163 | config_file_path.c_str()); |
| 164 | |
| 165 | std::string error_msg; |
| 166 | |
| 167 | LOG_ALWAYS_FATAL_IF( |
| 168 | !ReadConfig(config_file_path, sonames, |
| 169 | [&company_name](const std::string& soname, std::string* error_msg) { |
| 170 | if (android::base::StartsWith(soname, "lib") && |
| 171 | android::base::EndsWith(soname, "." + company_name + ".so")) { |
| 172 | return true; |
| 173 | } else { |
| 174 | *error_msg = "Library name \"" + soname + |
| 175 | "\" does not end with the company name: " + company_name + |
| 176 | "."; |
| 177 | return false; |
| 178 | } |
| 179 | }, |
| 180 | &error_msg), |
| 181 | "Error reading public native library list from \"%s\": %s", config_file_path.c_str(), |
| 182 | error_msg.c_str()); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 188 | static std::string InitDefaultPublicLibraries() { |
| 189 | std::string config_file = root_dir() + kDefaultPublicLibrariesFile; |
| 190 | std::vector<std::string> sonames; |
| 191 | std::string error_msg; |
| 192 | LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg), |
| 193 | "Error reading public native library list from \"%s\": %s", |
| 194 | config_file.c_str(), error_msg.c_str()); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 195 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 196 | std::string additional_libs = additional_public_libraries(); |
| 197 | if (!additional_libs.empty()) { |
| 198 | auto vec = base::Split(additional_libs, ":"); |
| 199 | std::copy(vec.begin(), vec.end(), std::back_inserter(sonames)); |
| 200 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 201 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 202 | // Remove the public libs in the runtime namespace. |
| 203 | // These libs are listed in public.android.txt, but we don't want the rest of android |
| 204 | // in default namespace to dlopen the libs. |
| 205 | // For example, libicuuc.so is exposed to classloader namespace from runtime namespace. |
| 206 | // Unfortunately, it does not have stable C symbols, and default namespace should only use |
| 207 | // stable symbols in libandroidicu.so. http://b/120786417 |
| 208 | for (const std::string& lib_name : kRuntimePublicLibraries) { |
| 209 | std::string path(kRuntimeApexLibPath); |
| 210 | path.append("/").append(lib_name); |
| 211 | |
| 212 | struct stat s; |
| 213 | // Do nothing if the path in /apex does not exist. |
| 214 | // Runtime APEX must be mounted since libnativeloader is in the same APEX |
| 215 | if (stat(path.c_str(), &s) != 0) { |
| 216 | continue; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 217 | } |
| 218 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 219 | auto it = std::find(sonames.begin(), sonames.end(), lib_name); |
| 220 | if (it != sonames.end()) { |
| 221 | sonames.erase(it); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 222 | } |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 223 | } |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame^] | 224 | |
| 225 | // Remove the public libs in the nnapi namespace. |
| 226 | auto it = std::find(sonames.begin(), sonames.end(), kNeuralNetworksApexPublicLibrary); |
| 227 | if (it != sonames.end()) { |
| 228 | sonames.erase(it); |
| 229 | } |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 230 | return android::base::Join(sonames, ':'); |
| 231 | } |
| 232 | |
| 233 | static std::string InitRuntimePublicLibraries() { |
| 234 | CHECK(sizeof(kRuntimePublicLibraries) > 0); |
| 235 | std::string list = android::base::Join(kRuntimePublicLibraries, ":"); |
| 236 | |
| 237 | std::string additional_libs = additional_public_libraries(); |
| 238 | if (!additional_libs.empty()) { |
| 239 | list = list + ':' + additional_libs; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 240 | } |
| 241 | return list; |
| 242 | } |
| 243 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 244 | static std::string InitVendorPublicLibraries() { |
| 245 | // This file is optional, quietly ignore if the file does not exist. |
| 246 | std::vector<std::string> sonames; |
| 247 | ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr); |
| 248 | return android::base::Join(sonames, ':'); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 251 | // read /system/etc/public.libraries-<companyname>.txt and |
| 252 | // /product/etc/public.libraries-<companyname>.txt which contain partner defined |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 253 | // system libs that are exposed to apps. The libs in the txt files must be |
| 254 | // named as lib<name>.<companyname>.so. |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 255 | static std::string InitExtendedPublicLibraries() { |
| 256 | std::vector<std::string> sonames; |
| 257 | ReadExtensionLibraries("/system/etc", &sonames); |
| 258 | ReadExtensionLibraries("/product/etc", &sonames); |
| 259 | return android::base::Join(sonames, ':'); |
| 260 | } |
| 261 | |
| 262 | static std::string InitLlndkLibraries() { |
| 263 | std::string config_file = kLlndkLibrariesFile; |
| 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 | static std::string InitVndkspLibraries() { |
| 271 | std::string config_file = kVndkLibrariesFile; |
| 272 | InsertVndkVersionStr(&config_file); |
| 273 | std::vector<std::string> sonames; |
| 274 | ReadConfig(config_file, &sonames, always_true, nullptr); |
| 275 | return android::base::Join(sonames, ':'); |
| 276 | } |
| 277 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame^] | 278 | static std::string InitNeuralNetworksPublicLibraries() { |
| 279 | return kNeuralNetworksApexPublicLibrary; |
| 280 | } |
| 281 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 282 | } // namespace |
| 283 | |
| 284 | const std::string& default_public_libraries() { |
| 285 | static std::string list = InitDefaultPublicLibraries(); |
| 286 | return list; |
| 287 | } |
| 288 | |
| 289 | const std::string& runtime_public_libraries() { |
| 290 | static std::string list = InitRuntimePublicLibraries(); |
| 291 | return list; |
| 292 | } |
| 293 | |
| 294 | const std::string& vendor_public_libraries() { |
| 295 | static std::string list = InitVendorPublicLibraries(); |
| 296 | return list; |
| 297 | } |
| 298 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 299 | const std::string& extended_public_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 300 | static std::string list = InitExtendedPublicLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 301 | return list; |
| 302 | } |
| 303 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame^] | 304 | const std::string& neuralnetworks_public_libraries() { |
| 305 | static std::string list = InitNeuralNetworksPublicLibraries(); |
| 306 | return list; |
| 307 | } |
| 308 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 309 | const std::string& llndk_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 310 | static std::string list = InitLlndkLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 311 | return list; |
| 312 | } |
| 313 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 314 | const std::string& vndksp_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 315 | static std::string list = InitVndkspLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 316 | return list; |
| 317 | } |
| 318 | |
| 319 | } // namespace android::nativeloader |