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