| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [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 | |
| 17 | #define LOG_TAG "nativeloader" |
| 18 | |
| 19 | #include "public_libraries.h" |
| 20 | |
| 21 | #include <dirent.h> |
| 22 | |
| 23 | #include <algorithm> |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame^] | 24 | #include <map> |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 25 | #include <memory> |
| 26 | |
| 27 | #include <android-base/file.h> |
| 28 | #include <android-base/logging.h> |
| 29 | #include <android-base/properties.h> |
| 30 | #include <android-base/result.h> |
| 31 | #include <android-base/strings.h> |
| 32 | #include <log/log.h> |
| 33 | |
| Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 34 | #if defined(__ANDROID__) |
| 35 | #include <android/sysprop/VndkProperties.sysprop.h> |
| 36 | #endif |
| 37 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 38 | #include "utils.h" |
| 39 | |
| 40 | namespace android::nativeloader { |
| 41 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 42 | using android::base::ErrnoError; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 43 | using android::base::Result; |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 44 | using internal::ConfigEntry; |
| 45 | using internal::ParseConfig; |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame^] | 46 | using internal::ParseJniConfig; |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 47 | using std::literals::string_literals::operator""s; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 48 | |
| 49 | namespace { |
| 50 | |
| 51 | constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt"; |
| 52 | constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-"; |
| 53 | constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt"; |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame^] | 54 | constexpr const char* kJniConfigFile = "/linkerconfig/jni.config.txt"; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 55 | constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt"; |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 56 | constexpr const char* kLlndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt"; |
| 57 | constexpr const char* kVndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 58 | |
| 59 | const std::vector<const std::string> kArtApexPublicLibraries = { |
| 60 | "libicuuc.so", |
| 61 | "libicui18n.so", |
| 62 | }; |
| 63 | |
| 64 | constexpr const char* kArtApexLibPath = "/apex/com.android.art/" LIB; |
| 65 | |
| 66 | constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so"; |
| Luke Huang | 5c01772 | 2019-12-17 10:54:26 +0800 | [diff] [blame] | 67 | // STOPSHIP(b/146420818): Figure out how to use stub or non-specific lib name for libcronet. |
| 68 | constexpr const char* kCronetApexPublicLibrary = "libcronet.80.0.3986.0.so"; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 69 | |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 70 | constexpr const char* kStatsdApexPublicLibrary = "libstats_jni.so"; |
| 71 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 72 | // TODO(b/130388701): do we need this? |
| 73 | std::string root_dir() { |
| 74 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 75 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 76 | } |
| 77 | |
| 78 | bool debuggable() { |
| 79 | static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
| 80 | return debuggable; |
| 81 | } |
| 82 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 83 | std::string vndk_version_str(bool use_product_vndk) { |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 84 | if (use_product_vndk) { |
| 85 | static std::string product_vndk_version = get_vndk_version(true); |
| 86 | return product_vndk_version; |
| 87 | } else { |
| 88 | static std::string vendor_vndk_version = get_vndk_version(false); |
| 89 | return vendor_vndk_version; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 90 | } |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 94 | // variable to add libraries to the list. This is intended for platform tests only. |
| 95 | std::string additional_public_libraries() { |
| 96 | if (debuggable()) { |
| 97 | const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 98 | return val ? val : ""; |
| 99 | } |
| 100 | return ""; |
| 101 | } |
| 102 | |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 103 | // insert vndk version in every {} placeholder |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 104 | void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 105 | CHECK(file_name != nullptr); |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 106 | auto version = vndk_version_str(use_product_vndk); |
| 107 | size_t pos = file_name->find("{}"); |
| 108 | while (pos != std::string::npos) { |
| 109 | file_name->replace(pos, 2, version); |
| 110 | pos = file_name->find("{}", pos + version.size()); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 111 | } |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | const std::function<Result<bool>(const struct ConfigEntry&)> always_true = |
| 115 | [](const struct ConfigEntry&) -> Result<bool> { return true; }; |
| 116 | |
| 117 | Result<std::vector<std::string>> ReadConfig( |
| 118 | const std::string& configFile, |
| 119 | const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) { |
| 120 | std::string file_content; |
| 121 | if (!base::ReadFileToString(configFile, &file_content)) { |
| 122 | return ErrnoError(); |
| 123 | } |
| 124 | Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 125 | if (!result.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 126 | return Errorf("Cannot parse {}: {}", configFile, result.error().message()); |
| 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | void 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); |
| 141 | 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); |
| 145 | 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 | auto ret = ReadConfig( |
| 152 | config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> { |
| 153 | if (android::base::StartsWith(entry.soname, "lib") && |
| 154 | android::base::EndsWith(entry.soname, "." + company_name + ".so")) { |
| 155 | return true; |
| 156 | } else { |
| 157 | return Errorf("Library name \"{}\" does not end with the company name {}.", |
| 158 | entry.soname, company_name); |
| 159 | } |
| 160 | }); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 161 | if (ret.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 162 | sonames->insert(sonames->end(), ret->begin(), ret->end()); |
| 163 | } else { |
| 164 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 165 | config_file_path.c_str(), ret.error().message().c_str()); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static std::string InitDefaultPublicLibraries(bool for_preload) { |
| 173 | std::string config_file = root_dir() + kDefaultPublicLibrariesFile; |
| 174 | auto sonames = |
| 175 | ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> { |
| 176 | if (for_preload) { |
| 177 | return !entry.nopreload; |
| 178 | } else { |
| 179 | return true; |
| 180 | } |
| 181 | }); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 182 | if (!sonames.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 183 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 184 | config_file.c_str(), sonames.error().message().c_str()); |
| 185 | return ""; |
| 186 | } |
| 187 | |
| 188 | std::string additional_libs = additional_public_libraries(); |
| 189 | if (!additional_libs.empty()) { |
| 190 | auto vec = base::Split(additional_libs, ":"); |
| 191 | std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames)); |
| 192 | } |
| 193 | |
| 194 | // If this is for preloading libs, don't remove the libs from APEXes. |
| 195 | if (for_preload) { |
| 196 | return android::base::Join(*sonames, ':'); |
| 197 | } |
| 198 | |
| 199 | // Remove the public libs in the art namespace. |
| 200 | // These libs are listed in public.android.txt, but we don't want the rest of android |
| 201 | // in default namespace to dlopen the libs. |
| 202 | // For example, libicuuc.so is exposed to classloader namespace from art namespace. |
| 203 | // Unfortunately, it does not have stable C symbols, and default namespace should only use |
| 204 | // stable symbols in libandroidicu.so. http://b/120786417 |
| 205 | for (const std::string& lib_name : kArtApexPublicLibraries) { |
| 206 | std::string path(kArtApexLibPath); |
| 207 | path.append("/").append(lib_name); |
| 208 | |
| 209 | struct stat s; |
| 210 | // Do nothing if the path in /apex does not exist. |
| 211 | // Runtime APEX must be mounted since libnativeloader is in the same APEX |
| 212 | if (stat(path.c_str(), &s) != 0) { |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | auto it = std::find(sonames->begin(), sonames->end(), lib_name); |
| 217 | if (it != sonames->end()) { |
| 218 | sonames->erase(it); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Remove the public libs in the nnapi namespace. |
| 223 | auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary); |
| 224 | if (it != sonames->end()) { |
| 225 | sonames->erase(it); |
| 226 | } |
| 227 | return android::base::Join(*sonames, ':'); |
| 228 | } |
| 229 | |
| 230 | static std::string InitArtPublicLibraries() { |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 231 | CHECK_GT((int)sizeof(kArtApexPublicLibraries), 0); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 232 | std::string list = android::base::Join(kArtApexPublicLibraries, ":"); |
| 233 | |
| 234 | std::string additional_libs = additional_public_libraries(); |
| 235 | if (!additional_libs.empty()) { |
| 236 | list = list + ':' + additional_libs; |
| 237 | } |
| 238 | return list; |
| 239 | } |
| 240 | |
| 241 | static std::string InitVendorPublicLibraries() { |
| 242 | // This file is optional, quietly ignore if the file does not exist. |
| 243 | auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 244 | if (!sonames.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 245 | return ""; |
| 246 | } |
| 247 | return android::base::Join(*sonames, ':'); |
| 248 | } |
| 249 | |
| Justin Yun | 0cc4027 | 2019-12-16 16:47:40 +0900 | [diff] [blame] | 250 | // read /system/etc/public.libraries-<companyname>.txt, |
| 251 | // /system_ext/etc/public.libraries-<companyname>.txt and |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 252 | // /product/etc/public.libraries-<companyname>.txt which contain partner defined |
| 253 | // system libs that are exposed to apps. The libs in the txt files must be |
| 254 | // named as lib<name>.<companyname>.so. |
| 255 | static std::string InitExtendedPublicLibraries() { |
| 256 | std::vector<std::string> sonames; |
| 257 | ReadExtensionLibraries("/system/etc", &sonames); |
| Justin Yun | 0cc4027 | 2019-12-16 16:47:40 +0900 | [diff] [blame] | 258 | ReadExtensionLibraries("/system_ext/etc", &sonames); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 259 | ReadExtensionLibraries("/product/etc", &sonames); |
| 260 | return android::base::Join(sonames, ':'); |
| 261 | } |
| 262 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 263 | static std::string InitLlndkLibrariesVendor() { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 264 | std::string config_file = kLlndkLibrariesFile; |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 265 | InsertVndkVersionStr(&config_file, false); |
| 266 | auto sonames = ReadConfig(config_file, always_true); |
| Bernie Innocenti | c137563 | 2020-02-13 10:37:03 +0900 | [diff] [blame] | 267 | if (!sonames.ok()) { |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 268 | LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str()); |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 269 | return ""; |
| 270 | } |
| 271 | return android::base::Join(*sonames, ':'); |
| 272 | } |
| 273 | |
| 274 | static std::string InitLlndkLibrariesProduct() { |
| 275 | std::string config_file = kLlndkLibrariesFile; |
| 276 | InsertVndkVersionStr(&config_file, true); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 277 | auto sonames = ReadConfig(config_file, always_true); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 278 | if (!sonames.ok()) { |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 279 | LOG_ALWAYS_FATAL("%s: %s", config_file.c_str(), sonames.error().message().c_str()); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 280 | return ""; |
| 281 | } |
| 282 | return android::base::Join(*sonames, ':'); |
| 283 | } |
| 284 | |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 285 | static std::string InitVndkspLibrariesVendor() { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 286 | std::string config_file = kVndkLibrariesFile; |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 287 | InsertVndkVersionStr(&config_file, false); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 288 | auto sonames = ReadConfig(config_file, always_true); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 289 | if (!sonames.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 290 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 291 | return ""; |
| 292 | } |
| 293 | return android::base::Join(*sonames, ':'); |
| 294 | } |
| 295 | |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 296 | static std::string InitVndkspLibrariesProduct() { |
| 297 | std::string config_file = kVndkLibrariesFile; |
| 298 | InsertVndkVersionStr(&config_file, true); |
| 299 | auto sonames = ReadConfig(config_file, always_true); |
| 300 | if (!sonames.ok()) { |
| 301 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 302 | return ""; |
| 303 | } |
| 304 | return android::base::Join(*sonames, ':'); |
| 305 | } |
| 306 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 307 | static std::string InitNeuralNetworksPublicLibraries() { |
| 308 | return kNeuralNetworksApexPublicLibrary; |
| 309 | } |
| 310 | |
| Luke Huang | 5c01772 | 2019-12-17 10:54:26 +0800 | [diff] [blame] | 311 | static std::string InitCronetPublicLibraries() { |
| 312 | return kCronetApexPublicLibrary; |
| 313 | } |
| 314 | |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 315 | static std::string InitStatsdPublicLibraries() { |
| 316 | return kStatsdApexPublicLibrary; |
| 317 | } |
| 318 | |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame^] | 319 | static std::map<std::string, std::string> InitApexJniLibraries() { |
| 320 | std::string file_content; |
| 321 | if (!base::ReadFileToString(kJniConfigFile, &file_content)) { |
| 322 | // jni config is optional |
| 323 | return {}; |
| 324 | } |
| 325 | auto config = ParseJniConfig(file_content); |
| 326 | if (!config.ok()) { |
| 327 | LOG_ALWAYS_FATAL("%s: %s", kJniConfigFile, config.error().message().c_str()); |
| 328 | // not reach here |
| 329 | return {}; |
| 330 | } |
| 331 | return *config; |
| 332 | } |
| 333 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 334 | } // namespace |
| 335 | |
| 336 | const std::string& preloadable_public_libraries() { |
| 337 | static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true); |
| 338 | return list; |
| 339 | } |
| 340 | |
| 341 | const std::string& default_public_libraries() { |
| 342 | static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false); |
| 343 | return list; |
| 344 | } |
| 345 | |
| 346 | const std::string& art_public_libraries() { |
| 347 | static std::string list = InitArtPublicLibraries(); |
| 348 | return list; |
| 349 | } |
| 350 | |
| 351 | const std::string& vendor_public_libraries() { |
| 352 | static std::string list = InitVendorPublicLibraries(); |
| 353 | return list; |
| 354 | } |
| 355 | |
| 356 | const std::string& extended_public_libraries() { |
| 357 | static std::string list = InitExtendedPublicLibraries(); |
| 358 | return list; |
| 359 | } |
| 360 | |
| 361 | const std::string& neuralnetworks_public_libraries() { |
| 362 | static std::string list = InitNeuralNetworksPublicLibraries(); |
| 363 | return list; |
| 364 | } |
| 365 | |
| Luke Huang | 5c01772 | 2019-12-17 10:54:26 +0800 | [diff] [blame] | 366 | const std::string& cronet_public_libraries() { |
| 367 | static std::string list = InitCronetPublicLibraries(); |
| 368 | return list; |
| 369 | } |
| 370 | |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 371 | const std::string& statsd_public_libraries() { |
| 372 | static std::string list = InitStatsdPublicLibraries(); |
| 373 | return list; |
| 374 | } |
| 375 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 376 | const std::string& llndk_libraries_product() { |
| 377 | static std::string list = InitLlndkLibrariesProduct(); |
| 378 | return list; |
| 379 | } |
| 380 | |
| 381 | const std::string& llndk_libraries_vendor() { |
| 382 | static std::string list = InitLlndkLibrariesVendor(); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 383 | return list; |
| 384 | } |
| 385 | |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 386 | const std::string& vndksp_libraries_product() { |
| 387 | static std::string list = InitVndkspLibrariesProduct(); |
| 388 | return list; |
| 389 | } |
| 390 | |
| 391 | const std::string& vndksp_libraries_vendor() { |
| 392 | static std::string list = InitVndkspLibrariesVendor(); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 393 | return list; |
| 394 | } |
| 395 | |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame^] | 396 | const std::string& apex_jni_libraries(const std::string& apex_ns_name) { |
| 397 | static std::map<std::string, std::string> jni_libraries = InitApexJniLibraries(); |
| 398 | return jni_libraries[apex_ns_name]; |
| 399 | } |
| 400 | |
| Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 401 | bool is_product_vndk_version_defined() { |
| 402 | #if defined(__ANDROID__) |
| 403 | return android::sysprop::VndkProperties::product_vndk_version().has_value(); |
| 404 | #else |
| 405 | return false; |
| 406 | #endif |
| 407 | } |
| 408 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 409 | std::string get_vndk_version(bool is_product_vndk) { |
| 410 | #if defined(__ANDROID__) |
| 411 | if (is_product_vndk) { |
| 412 | return android::sysprop::VndkProperties::product_vndk_version().value_or(""); |
| 413 | } |
| 414 | return android::sysprop::VndkProperties::vendor_vndk_version().value_or(""); |
| 415 | #else |
| 416 | if (is_product_vndk) { |
| 417 | return android::base::GetProperty("ro.product.vndk.version", ""); |
| 418 | } |
| 419 | return android::base::GetProperty("ro.vndk.version", ""); |
| 420 | #endif |
| 421 | } |
| 422 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 423 | namespace internal { |
| 424 | // Exported for testing |
| 425 | Result<std::vector<std::string>> ParseConfig( |
| 426 | const std::string& file_content, |
| 427 | const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) { |
| 428 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 429 | |
| 430 | std::vector<std::string> sonames; |
| 431 | for (auto& line : lines) { |
| 432 | auto trimmed_line = base::Trim(line); |
| 433 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 434 | continue; |
| 435 | } |
| 436 | |
| 437 | std::vector<std::string> tokens = android::base::Split(trimmed_line, " "); |
| 438 | if (tokens.size() < 1 || tokens.size() > 3) { |
| 439 | return Errorf("Malformed line \"{}\"", line); |
| 440 | } |
| 441 | struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL}; |
| 442 | size_t i = tokens.size(); |
| 443 | while (i-- > 0) { |
| 444 | if (tokens[i] == "nopreload") { |
| 445 | entry.nopreload = true; |
| 446 | } else if (tokens[i] == "32" || tokens[i] == "64") { |
| 447 | if (entry.bitness != ALL) { |
| 448 | return Errorf("Malformed line \"{}\": bitness can be specified only once", line); |
| 449 | } |
| 450 | entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64; |
| 451 | } else { |
| 452 | if (i != 0) { |
| 453 | return Errorf("Malformed line \"{}\"", line); |
| 454 | } |
| 455 | entry.soname = tokens[i]; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // skip 32-bit lib on 64-bit process and vice versa |
| 460 | #if defined(__LP64__) |
| 461 | if (entry.bitness == ONLY_32) continue; |
| 462 | #else |
| 463 | if (entry.bitness == ONLY_64) continue; |
| 464 | #endif |
| 465 | |
| 466 | Result<bool> ret = filter_fn(entry); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 467 | if (!ret.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 468 | return ret.error(); |
| 469 | } |
| 470 | if (*ret) { |
| 471 | // filter_fn has returned true. |
| 472 | sonames.push_back(entry.soname); |
| 473 | } |
| 474 | } |
| 475 | return sonames; |
| 476 | } |
| 477 | |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame^] | 478 | Result<std::map<std::string, std::string>> ParseJniConfig(const std::string& file_content) { |
| 479 | std::map<std::string, std::string> entries; |
| 480 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 481 | for (auto& line : lines) { |
| 482 | auto trimmed_line = base::Trim(line); |
| 483 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | std::vector<std::string> tokens = base::Split(trimmed_line, " "); |
| 488 | if (tokens.size() < 2) { |
| 489 | return Errorf( "Malformed line \"{}\"", line); |
| 490 | } |
| 491 | entries[tokens[0]] = tokens[1]; |
| 492 | } |
| 493 | return entries; |
| 494 | } |
| 495 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 496 | } // namespace internal |
| 497 | |
| 498 | } // namespace android::nativeloader |