| 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> |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 26 | #include <regex> |
| 27 | #include <string> |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 28 | |
| 29 | #include <android-base/file.h> |
| 30 | #include <android-base/logging.h> |
| Nicolas Geoffray | 0bf5b67 | 2021-05-17 11:07:42 +0100 | [diff] [blame] | 31 | #include <android-base/properties.h> |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 32 | #include <android-base/result.h> |
| 33 | #include <android-base/strings.h> |
| 34 | #include <log/log.h> |
| 35 | |
| Nicolas Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 36 | #if defined(ART_TARGET_ANDROID) |
| Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 37 | #include <android/sysprop/VndkProperties.sysprop.h> |
| 38 | #endif |
| 39 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 40 | #include "utils.h" |
| 41 | |
| 42 | namespace android::nativeloader { |
| 43 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 44 | using android::base::ErrnoError; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 45 | using android::base::Result; |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 46 | using internal::ConfigEntry; |
| 47 | using internal::ParseConfig; |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 48 | using internal::ParseApexLibrariesConfig; |
| Jeffrey Huang | 5257503 | 2020-02-11 17:33:45 -0800 | [diff] [blame] | 49 | using std::literals::string_literals::operator""s; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 50 | |
| 51 | namespace { |
| 52 | |
| 53 | constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt"; |
| 54 | constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-"; |
| 55 | constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt"; |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 56 | constexpr const char* kApexLibrariesConfigFile = "/linkerconfig/apex.libraries.config.txt"; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 57 | constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt"; |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 58 | constexpr const char* kLlndkLibrariesFile = "/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt"; |
| 59 | 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] | 60 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 61 | |
| 62 | // TODO(b/130388701): do we need this? |
| 63 | std::string root_dir() { |
| 64 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 65 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 66 | } |
| 67 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 68 | std::string vndk_version_str(bool use_product_vndk) { |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 69 | if (use_product_vndk) { |
| 70 | static std::string product_vndk_version = get_vndk_version(true); |
| 71 | return product_vndk_version; |
| 72 | } else { |
| 73 | static std::string vendor_vndk_version = get_vndk_version(false); |
| 74 | return vendor_vndk_version; |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 75 | } |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 78 | // insert vndk version in every {} placeholder |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 79 | void InsertVndkVersionStr(std::string* file_name, bool use_product_vndk) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 80 | CHECK(file_name != nullptr); |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 81 | auto version = vndk_version_str(use_product_vndk); |
| 82 | size_t pos = file_name->find("{}"); |
| 83 | while (pos != std::string::npos) { |
| 84 | file_name->replace(pos, 2, version); |
| 85 | pos = file_name->find("{}", pos + version.size()); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 86 | } |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | const std::function<Result<bool>(const struct ConfigEntry&)> always_true = |
| 90 | [](const struct ConfigEntry&) -> Result<bool> { return true; }; |
| 91 | |
| 92 | Result<std::vector<std::string>> ReadConfig( |
| 93 | const std::string& configFile, |
| 94 | const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) { |
| 95 | std::string file_content; |
| 96 | if (!base::ReadFileToString(configFile, &file_content)) { |
| 97 | return ErrnoError(); |
| 98 | } |
| 99 | Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 100 | if (!result.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 101 | return Errorf("Cannot parse {}: {}", configFile, result.error().message()); |
| 102 | } |
| 103 | return result; |
| 104 | } |
| 105 | |
| 106 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 107 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 108 | if (dir != nullptr) { |
| 109 | // Failing to opening the dir is not an error, which can happen in |
| 110 | // webview_zygote. |
| 111 | while (struct dirent* ent = readdir(dir.get())) { |
| 112 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 113 | continue; |
| 114 | } |
| 115 | const std::string filename(ent->d_name); |
| 116 | std::string_view fn = filename; |
| 117 | if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) && |
| 118 | android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) { |
| 119 | const std::string company_name(fn); |
| 120 | const std::string config_file_path = dirname + "/"s + filename; |
| 121 | LOG_ALWAYS_FATAL_IF( |
| 122 | company_name.empty(), |
| 123 | "Error extracting company name from public native library list file path \"%s\"", |
| 124 | config_file_path.c_str()); |
| 125 | |
| 126 | auto ret = ReadConfig( |
| 127 | config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> { |
| 128 | if (android::base::StartsWith(entry.soname, "lib") && |
| 129 | android::base::EndsWith(entry.soname, "." + company_name + ".so")) { |
| 130 | return true; |
| 131 | } else { |
| 132 | return Errorf("Library name \"{}\" does not end with the company name {}.", |
| 133 | entry.soname, company_name); |
| 134 | } |
| 135 | }); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 136 | if (ret.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 137 | sonames->insert(sonames->end(), ret->begin(), ret->end()); |
| 138 | } else { |
| 139 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 140 | config_file_path.c_str(), ret.error().message().c_str()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static std::string InitDefaultPublicLibraries(bool for_preload) { |
| 148 | std::string config_file = root_dir() + kDefaultPublicLibrariesFile; |
| 149 | auto sonames = |
| 150 | ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> { |
| 151 | if (for_preload) { |
| 152 | return !entry.nopreload; |
| 153 | } else { |
| 154 | return true; |
| 155 | } |
| 156 | }); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 157 | if (!sonames.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 158 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 159 | config_file.c_str(), sonames.error().message().c_str()); |
| 160 | return ""; |
| 161 | } |
| 162 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 163 | // If this is for preloading libs, don't remove the libs from APEXes. |
| 164 | if (for_preload) { |
| 165 | return android::base::Join(*sonames, ':'); |
| 166 | } |
| 167 | |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 168 | // Remove the public libs provided by apexes because these libs are available |
| 169 | // from apex namespaces. |
| 170 | for (const auto& p : apex_public_libraries()) { |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 171 | auto public_libs = base::Split(p.second, ":"); |
| 172 | sonames->erase(std::remove_if(sonames->begin(), sonames->end(), [&public_libs](const std::string& v) { |
| 173 | return std::find(public_libs.begin(), public_libs.end(), v) != public_libs.end(); |
| 174 | }), sonames->end()); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 175 | } |
| 176 | return android::base::Join(*sonames, ':'); |
| 177 | } |
| 178 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 179 | static std::string InitVendorPublicLibraries() { |
| 180 | // This file is optional, quietly ignore if the file does not exist. |
| 181 | auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true); |
| 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 | return ""; |
| 184 | } |
| 185 | return android::base::Join(*sonames, ':'); |
| 186 | } |
| 187 | |
| Justin Yun | bbd13da | 2021-08-17 19:07:32 +0900 | [diff] [blame^] | 188 | // If ro.product.vndk.version is defined, /product/etc/public.libraries-<companyname>.txt contains |
| 189 | // the product public libraries that are loaded from the product namespace. Otherwise, the file |
| 190 | // contains the extended public libraries that are loaded from the system namespace. |
| Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 191 | static std::string InitProductPublicLibraries() { |
| Justin Yun | bbd13da | 2021-08-17 19:07:32 +0900 | [diff] [blame^] | 192 | std::vector<std::string> sonames; |
| 193 | if (is_product_vndk_version_defined()) { |
| 194 | ReadExtensionLibraries("/product/etc", &sonames); |
| Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 195 | } |
| Justin Yun | bbd13da | 2021-08-17 19:07:32 +0900 | [diff] [blame^] | 196 | return android::base::Join(sonames, ':'); |
| Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 197 | } |
| 198 | |
| Justin Yun | 0cc4027 | 2019-12-16 16:47:40 +0900 | [diff] [blame] | 199 | // read /system/etc/public.libraries-<companyname>.txt, |
| 200 | // /system_ext/etc/public.libraries-<companyname>.txt and |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 201 | // /product/etc/public.libraries-<companyname>.txt which contain partner defined |
| 202 | // system libs that are exposed to apps. The libs in the txt files must be |
| 203 | // named as lib<name>.<companyname>.so. |
| 204 | static std::string InitExtendedPublicLibraries() { |
| 205 | std::vector<std::string> sonames; |
| 206 | ReadExtensionLibraries("/system/etc", &sonames); |
| Justin Yun | 0cc4027 | 2019-12-16 16:47:40 +0900 | [diff] [blame] | 207 | ReadExtensionLibraries("/system_ext/etc", &sonames); |
| Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 208 | if (!is_product_vndk_version_defined()) { |
| 209 | ReadExtensionLibraries("/product/etc", &sonames); |
| 210 | } |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 211 | return android::base::Join(sonames, ':'); |
| 212 | } |
| 213 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 214 | static std::string InitLlndkLibrariesVendor() { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 215 | std::string config_file = kLlndkLibrariesFile; |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 216 | InsertVndkVersionStr(&config_file, false); |
| 217 | auto sonames = ReadConfig(config_file, always_true); |
| Bernie Innocenti | c137563 | 2020-02-13 10:37:03 +0900 | [diff] [blame] | 218 | if (!sonames.ok()) { |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 219 | 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] | 220 | return ""; |
| 221 | } |
| 222 | return android::base::Join(*sonames, ':'); |
| 223 | } |
| 224 | |
| 225 | static std::string InitLlndkLibrariesProduct() { |
| Justin Yun | 696882f | 2020-03-24 13:31:19 +0900 | [diff] [blame] | 226 | if (!is_product_vndk_version_defined()) { |
| 227 | return ""; |
| 228 | } |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 229 | std::string config_file = kLlndkLibrariesFile; |
| 230 | InsertVndkVersionStr(&config_file, true); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 231 | auto sonames = ReadConfig(config_file, always_true); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 232 | if (!sonames.ok()) { |
| Jooyung Han | 26f7d10 | 2020-02-22 23:39:23 +0900 | [diff] [blame] | 233 | 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] | 234 | return ""; |
| 235 | } |
| 236 | return android::base::Join(*sonames, ':'); |
| 237 | } |
| 238 | |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 239 | static std::string InitVndkspLibrariesVendor() { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 240 | std::string config_file = kVndkLibrariesFile; |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 241 | InsertVndkVersionStr(&config_file, false); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 242 | auto sonames = ReadConfig(config_file, always_true); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 243 | if (!sonames.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 244 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 245 | return ""; |
| 246 | } |
| 247 | return android::base::Join(*sonames, ':'); |
| 248 | } |
| 249 | |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 250 | static std::string InitVndkspLibrariesProduct() { |
| Justin Yun | 696882f | 2020-03-24 13:31:19 +0900 | [diff] [blame] | 251 | if (!is_product_vndk_version_defined()) { |
| 252 | return ""; |
| 253 | } |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 254 | std::string config_file = kVndkLibrariesFile; |
| 255 | InsertVndkVersionStr(&config_file, true); |
| 256 | auto sonames = ReadConfig(config_file, always_true); |
| 257 | if (!sonames.ok()) { |
| 258 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 259 | return ""; |
| 260 | } |
| 261 | return android::base::Join(*sonames, ':'); |
| 262 | } |
| 263 | |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 264 | static std::map<std::string, std::string> InitApexLibraries(const std::string& tag) { |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 265 | std::string file_content; |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 266 | if (!base::ReadFileToString(kApexLibrariesConfigFile, &file_content)) { |
| 267 | // config is optional |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 268 | return {}; |
| 269 | } |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 270 | auto config = ParseApexLibrariesConfig(file_content, tag); |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 271 | if (!config.ok()) { |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 272 | LOG_ALWAYS_FATAL("%s: %s", kApexLibrariesConfigFile, config.error().message().c_str()); |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 273 | return {}; |
| 274 | } |
| 275 | return *config; |
| 276 | } |
| 277 | |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 278 | struct ApexLibrariesConfigLine { |
| 279 | std::string tag; |
| 280 | std::string apex_namespace; |
| 281 | std::string library_list; |
| 282 | }; |
| 283 | |
| 284 | const std::regex kApexNamespaceRegex("[0-9a-zA-Z_]+"); |
| 285 | const std::regex kLibraryListRegex("[0-9a-zA-Z.:@+_-]+"); |
| 286 | |
| 287 | Result<ApexLibrariesConfigLine> ParseApexLibrariesConfigLine(const std::string& line) { |
| 288 | std::vector<std::string> tokens = base::Split(line, " "); |
| 289 | if (tokens.size() != 3) { |
| 290 | return Errorf("Malformed line \"{}\"", line); |
| 291 | } |
| 292 | if (tokens[0] != "jni" && tokens[0] != "public") { |
| 293 | return Errorf("Invalid tag \"{}\"", line); |
| 294 | } |
| 295 | if (!std::regex_match(tokens[1], kApexNamespaceRegex)) { |
| 296 | return Errorf("Invalid apex_namespace \"{}\"", line); |
| 297 | } |
| 298 | if (!std::regex_match(tokens[2], kLibraryListRegex)) { |
| 299 | return Errorf("Invalid library_list \"{}\"", line); |
| 300 | } |
| 301 | return ApexLibrariesConfigLine{std::move(tokens[0]), std::move(tokens[1]), std::move(tokens[2])}; |
| 302 | } |
| 303 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 304 | } // namespace |
| 305 | |
| 306 | const std::string& preloadable_public_libraries() { |
| 307 | static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true); |
| 308 | return list; |
| 309 | } |
| 310 | |
| 311 | const std::string& default_public_libraries() { |
| 312 | static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false); |
| 313 | return list; |
| 314 | } |
| 315 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 316 | const std::string& vendor_public_libraries() { |
| 317 | static std::string list = InitVendorPublicLibraries(); |
| 318 | return list; |
| 319 | } |
| 320 | |
| Justin Yun | a21b584 | 2021-08-10 14:05:49 +0900 | [diff] [blame] | 321 | const std::string& product_public_libraries() { |
| 322 | static std::string list = InitProductPublicLibraries(); |
| 323 | return list; |
| 324 | } |
| 325 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 326 | const std::string& extended_public_libraries() { |
| 327 | static std::string list = InitExtendedPublicLibraries(); |
| 328 | return list; |
| 329 | } |
| 330 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 331 | const std::string& llndk_libraries_product() { |
| 332 | static std::string list = InitLlndkLibrariesProduct(); |
| 333 | return list; |
| 334 | } |
| 335 | |
| 336 | const std::string& llndk_libraries_vendor() { |
| 337 | static std::string list = InitLlndkLibrariesVendor(); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 338 | return list; |
| 339 | } |
| 340 | |
| Justin Yun | eb4f08c | 2020-02-18 11:29:07 +0900 | [diff] [blame] | 341 | const std::string& vndksp_libraries_product() { |
| 342 | static std::string list = InitVndkspLibrariesProduct(); |
| 343 | return list; |
| 344 | } |
| 345 | |
| 346 | const std::string& vndksp_libraries_vendor() { |
| 347 | static std::string list = InitVndkspLibrariesVendor(); |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 348 | return list; |
| 349 | } |
| 350 | |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 351 | const std::string& apex_jni_libraries(const std::string& apex_ns_name) { |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 352 | static std::map<std::string, std::string> jni_libraries = InitApexLibraries("jni"); |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 353 | return jni_libraries[apex_ns_name]; |
| 354 | } |
| 355 | |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 356 | const std::map<std::string, std::string>& apex_public_libraries() { |
| 357 | static std::map<std::string, std::string> public_libraries = InitApexLibraries("public"); |
| 358 | return public_libraries; |
| 359 | } |
| 360 | |
| Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 361 | bool is_product_vndk_version_defined() { |
| Nicolas Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 362 | #if defined(ART_TARGET_ANDROID) |
| Justin Yun | 3db26d5 | 2019-12-16 14:09:39 +0900 | [diff] [blame] | 363 | return android::sysprop::VndkProperties::product_vndk_version().has_value(); |
| 364 | #else |
| 365 | return false; |
| 366 | #endif |
| 367 | } |
| 368 | |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 369 | std::string get_vndk_version(bool is_product_vndk) { |
| Nicolas Geoffray | 7ca8b67 | 2020-04-24 15:43:48 +0100 | [diff] [blame] | 370 | #if defined(ART_TARGET_ANDROID) |
| Justin Yun | 089c135 | 2020-02-06 16:53:08 +0900 | [diff] [blame] | 371 | if (is_product_vndk) { |
| 372 | return android::sysprop::VndkProperties::product_vndk_version().value_or(""); |
| 373 | } |
| 374 | return android::sysprop::VndkProperties::vendor_vndk_version().value_or(""); |
| 375 | #else |
| 376 | if (is_product_vndk) { |
| 377 | return android::base::GetProperty("ro.product.vndk.version", ""); |
| 378 | } |
| 379 | return android::base::GetProperty("ro.vndk.version", ""); |
| 380 | #endif |
| 381 | } |
| 382 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 383 | namespace internal { |
| 384 | // Exported for testing |
| 385 | Result<std::vector<std::string>> ParseConfig( |
| 386 | const std::string& file_content, |
| 387 | const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) { |
| 388 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 389 | |
| 390 | std::vector<std::string> sonames; |
| 391 | for (auto& line : lines) { |
| 392 | auto trimmed_line = base::Trim(line); |
| 393 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | std::vector<std::string> tokens = android::base::Split(trimmed_line, " "); |
| 398 | if (tokens.size() < 1 || tokens.size() > 3) { |
| 399 | return Errorf("Malformed line \"{}\"", line); |
| 400 | } |
| 401 | struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL}; |
| 402 | size_t i = tokens.size(); |
| 403 | while (i-- > 0) { |
| 404 | if (tokens[i] == "nopreload") { |
| 405 | entry.nopreload = true; |
| 406 | } else if (tokens[i] == "32" || tokens[i] == "64") { |
| 407 | if (entry.bitness != ALL) { |
| 408 | return Errorf("Malformed line \"{}\": bitness can be specified only once", line); |
| 409 | } |
| 410 | entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64; |
| 411 | } else { |
| 412 | if (i != 0) { |
| 413 | return Errorf("Malformed line \"{}\"", line); |
| 414 | } |
| 415 | entry.soname = tokens[i]; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // skip 32-bit lib on 64-bit process and vice versa |
| 420 | #if defined(__LP64__) |
| 421 | if (entry.bitness == ONLY_32) continue; |
| 422 | #else |
| 423 | if (entry.bitness == ONLY_64) continue; |
| 424 | #endif |
| 425 | |
| 426 | Result<bool> ret = filter_fn(entry); |
| Bernie Innocenti | 4bd5895 | 2020-02-06 15:43:57 +0900 | [diff] [blame] | 427 | if (!ret.ok()) { |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 428 | return ret.error(); |
| 429 | } |
| 430 | if (*ret) { |
| 431 | // filter_fn has returned true. |
| 432 | sonames.push_back(entry.soname); |
| 433 | } |
| 434 | } |
| 435 | return sonames; |
| 436 | } |
| 437 | |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 438 | // Parses apex.libraries.config.txt file generated by linkerconfig which looks like |
| 439 | // system/linkerconfig/testdata/golden_output/stages/apex.libraries.config.txt |
| 440 | // and returns mapping of <apex namespace> to <library list> which matches <tag>. |
| 441 | // |
| 442 | // The file is line-based and each line consists of "<tag> <apex namespace> <library list>". |
| 443 | // |
| 444 | // <tag> explains what <library list> is. (e.g "jni", "public") |
| 445 | // <library list> is colon-separated list of library names. (e.g "libfoo.so:libbar.so") |
| 446 | // |
| 447 | // If <tag> is "jni", <library list> is the list of JNI libraries exposed by <apex namespace>. |
| 448 | // If <tag> is "public", <library list> is the list of public libraries exposed by <apex namespace>. |
| 449 | // Public libraries are the libs listed in /system/etc/public.libraries.txt. |
| 450 | Result<std::map<std::string, std::string>> ParseApexLibrariesConfig(const std::string& file_content, const std::string& tag) { |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 451 | std::map<std::string, std::string> entries; |
| 452 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 453 | for (auto& line : lines) { |
| 454 | auto trimmed_line = base::Trim(line); |
| 455 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 456 | continue; |
| 457 | } |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 458 | auto config_line = ParseApexLibrariesConfigLine(trimmed_line); |
| 459 | if (!config_line.ok()) { |
| 460 | return config_line.error(); |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 461 | } |
| Jooyung Han | cd616d0 | 2020-09-01 14:53:23 +0900 | [diff] [blame] | 462 | if (config_line->tag != tag) { |
| 463 | continue; |
| 464 | } |
| 465 | entries[config_line->apex_namespace] = config_line->library_list; |
| 466 | } |
| Jooyung Han | 538f99a | 2020-03-03 00:46:50 +0900 | [diff] [blame] | 467 | return entries; |
| 468 | } |
| 469 | |
| Orion Hodson | 9b16e34 | 2019-10-09 13:29:16 +0100 | [diff] [blame] | 470 | } // namespace internal |
| 471 | |
| 472 | } // namespace android::nativeloader |