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