Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 17 | #define LOG_TAG "nativeloader" |
| 18 | |
Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 19 | #include "public_libraries.h" |
| 20 | |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | |
| 23 | #include <algorithm> |
| 24 | #include <memory> |
| 25 | |
Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 26 | #include <android-base/file.h> |
| 27 | #include <android-base/logging.h> |
| 28 | #include <android-base/properties.h> |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 29 | #include <android-base/result.h> |
Jiyong Park | 16a9896 | 2019-05-04 03:10:48 +0900 | [diff] [blame] | 30 | #include <android-base/strings.h> |
| 31 | #include <log/log.h> |
| 32 | |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 33 | #include "utils.h" |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 34 | |
| 35 | namespace android::nativeloader { |
| 36 | |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 37 | using namespace internal; |
| 38 | using namespace ::std::string_literals; |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 39 | using android::base::ErrnoError; |
| 40 | using android::base::Errorf; |
| 41 | using android::base::Result; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 42 | |
| 43 | namespace { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 44 | |
| 45 | constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt"; |
| 46 | constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-"; |
| 47 | constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt"; |
| 48 | constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt"; |
| 49 | constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt"; |
| 50 | constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt"; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 51 | |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 52 | const std::vector<const std::string> kArtApexPublicLibraries = { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 53 | "libicuuc.so", |
| 54 | "libicui18n.so", |
| 55 | }; |
| 56 | |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 57 | constexpr const char* kArtApexLibPath = "/apex/com.android.art/" LIB; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 58 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 59 | constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so"; |
| 60 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 61 | // TODO(b/130388701): do we need this? |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 62 | std::string root_dir() { |
| 63 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 64 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 65 | } |
| 66 | |
| 67 | bool debuggable() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 68 | static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 69 | return debuggable; |
| 70 | } |
| 71 | |
| 72 | std::string vndk_version_str() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 73 | static std::string version = android::base::GetProperty("ro.vndk.version", ""); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 74 | if (version != "" && version != "current") { |
| 75 | return "." + version; |
| 76 | } |
| 77 | return ""; |
| 78 | } |
| 79 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 80 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 81 | // variable to add libraries to the list. This is intended for platform tests only. |
| 82 | std::string additional_public_libraries() { |
| 83 | if (debuggable()) { |
| 84 | const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 85 | return val ? val : ""; |
| 86 | } |
| 87 | return ""; |
| 88 | } |
| 89 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 90 | void InsertVndkVersionStr(std::string* file_name) { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 91 | CHECK(file_name != nullptr); |
| 92 | size_t insert_pos = file_name->find_last_of("."); |
| 93 | if (insert_pos == std::string::npos) { |
| 94 | insert_pos = file_name->length(); |
| 95 | } |
| 96 | file_name->insert(insert_pos, vndk_version_str()); |
| 97 | } |
| 98 | |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 99 | const std::function<Result<bool>(const struct ConfigEntry&)> always_true = |
| 100 | [](const struct ConfigEntry&) -> Result<bool> { return true; }; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 101 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 102 | Result<std::vector<std::string>> ReadConfig( |
| 103 | const std::string& configFile, |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 104 | const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 105 | std::string file_content; |
| 106 | if (!base::ReadFileToString(configFile, &file_content)) { |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 107 | return ErrnoError(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 108 | } |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 109 | Result<std::vector<std::string>> result = ParseConfig(file_content, filter_fn); |
| 110 | if (!result) { |
| 111 | return Errorf("Cannot parse {}: {}", configFile, result.error().message()); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 112 | } |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 113 | return result; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 117 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 118 | if (dir != nullptr) { |
| 119 | // Failing to opening the dir is not an error, which can happen in |
| 120 | // webview_zygote. |
| 121 | while (struct dirent* ent = readdir(dir.get())) { |
| 122 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 123 | continue; |
| 124 | } |
| 125 | const std::string filename(ent->d_name); |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 126 | std::string_view fn = filename; |
| 127 | if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) && |
| 128 | android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) { |
| 129 | const std::string company_name(fn); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 130 | const std::string config_file_path = dirname + "/"s + filename; |
| 131 | LOG_ALWAYS_FATAL_IF( |
| 132 | company_name.empty(), |
| 133 | "Error extracting company name from public native library list file path \"%s\"", |
| 134 | config_file_path.c_str()); |
| 135 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 136 | auto ret = ReadConfig( |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 137 | config_file_path, [&company_name](const struct ConfigEntry& entry) -> Result<bool> { |
| 138 | if (android::base::StartsWith(entry.soname, "lib") && |
| 139 | android::base::EndsWith(entry.soname, "." + company_name + ".so")) { |
| 140 | return true; |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 141 | } else { |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 142 | return Errorf("Library name \"{}\" does not end with the company name {}.", |
| 143 | entry.soname, company_name); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 144 | } |
| 145 | }); |
| 146 | if (ret) { |
| 147 | sonames->insert(sonames->end(), ret->begin(), ret->end()); |
| 148 | } else { |
| 149 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 150 | config_file_path.c_str(), ret.error().message().c_str()); |
| 151 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 157 | static std::string InitDefaultPublicLibraries(bool for_preload) { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 158 | std::string config_file = root_dir() + kDefaultPublicLibrariesFile; |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 159 | auto sonames = |
| 160 | ReadConfig(config_file, [&for_preload](const struct ConfigEntry& entry) -> Result<bool> { |
| 161 | if (for_preload) { |
| 162 | return !entry.nopreload; |
| 163 | } else { |
| 164 | return true; |
| 165 | } |
| 166 | }); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 167 | if (!sonames) { |
| 168 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 169 | config_file.c_str(), sonames.error().message().c_str()); |
| 170 | return ""; |
| 171 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 172 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 173 | std::string additional_libs = additional_public_libraries(); |
| 174 | if (!additional_libs.empty()) { |
| 175 | auto vec = base::Split(additional_libs, ":"); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 176 | std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames)); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 177 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 178 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 179 | // Remove the public libs in the runtime namespace. |
| 180 | // These libs are listed in public.android.txt, but we don't want the rest of android |
| 181 | // in default namespace to dlopen the libs. |
| 182 | // For example, libicuuc.so is exposed to classloader namespace from runtime namespace. |
| 183 | // Unfortunately, it does not have stable C symbols, and default namespace should only use |
| 184 | // stable symbols in libandroidicu.so. http://b/120786417 |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 185 | for (const std::string& lib_name : kArtApexPublicLibraries) { |
| 186 | std::string path(kArtApexLibPath); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 187 | path.append("/").append(lib_name); |
| 188 | |
| 189 | struct stat s; |
| 190 | // Do nothing if the path in /apex does not exist. |
| 191 | // Runtime APEX must be mounted since libnativeloader is in the same APEX |
| 192 | if (stat(path.c_str(), &s) != 0) { |
| 193 | continue; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 194 | } |
| 195 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 196 | auto it = std::find(sonames->begin(), sonames->end(), lib_name); |
| 197 | if (it != sonames->end()) { |
| 198 | sonames->erase(it); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 199 | } |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 200 | } |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 201 | |
| 202 | // Remove the public libs in the nnapi namespace. |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 203 | auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary); |
| 204 | if (it != sonames->end()) { |
| 205 | sonames->erase(it); |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 206 | } |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 207 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 210 | static std::string InitArtPublicLibraries() { |
| 211 | CHECK(sizeof(kArtApexPublicLibraries) > 0); |
| 212 | std::string list = android::base::Join(kArtApexPublicLibraries, ":"); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 213 | |
| 214 | std::string additional_libs = additional_public_libraries(); |
| 215 | if (!additional_libs.empty()) { |
| 216 | list = list + ':' + additional_libs; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 217 | } |
| 218 | return list; |
| 219 | } |
| 220 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 221 | static std::string InitVendorPublicLibraries() { |
| 222 | // This file is optional, quietly ignore if the file does not exist. |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 223 | auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true); |
| 224 | if (!sonames) { |
| 225 | return ""; |
| 226 | } |
| 227 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 228 | } |
| 229 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 230 | // read /system/etc/public.libraries-<companyname>.txt and |
| 231 | // /product/etc/public.libraries-<companyname>.txt which contain partner defined |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 232 | // system libs that are exposed to apps. The libs in the txt files must be |
| 233 | // named as lib<name>.<companyname>.so. |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 234 | static std::string InitExtendedPublicLibraries() { |
| 235 | std::vector<std::string> sonames; |
| 236 | ReadExtensionLibraries("/system/etc", &sonames); |
| 237 | ReadExtensionLibraries("/product/etc", &sonames); |
| 238 | return android::base::Join(sonames, ':'); |
| 239 | } |
| 240 | |
| 241 | static std::string InitLlndkLibraries() { |
| 242 | std::string config_file = kLlndkLibrariesFile; |
| 243 | InsertVndkVersionStr(&config_file); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 244 | auto sonames = ReadConfig(config_file, always_true); |
| 245 | if (!sonames) { |
| 246 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 247 | return ""; |
| 248 | } |
| 249 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static std::string InitVndkspLibraries() { |
| 253 | std::string config_file = kVndkLibrariesFile; |
| 254 | InsertVndkVersionStr(&config_file); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 255 | auto sonames = ReadConfig(config_file, always_true); |
| 256 | if (!sonames) { |
| 257 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 258 | return ""; |
| 259 | } |
| 260 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 263 | static std::string InitNeuralNetworksPublicLibraries() { |
| 264 | return kNeuralNetworksApexPublicLibrary; |
| 265 | } |
| 266 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 267 | } // namespace |
| 268 | |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 269 | const std::string& preloadable_public_libraries() { |
| 270 | static std::string list = InitDefaultPublicLibraries(/*for_preload*/ true); |
| 271 | return list; |
| 272 | } |
| 273 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 274 | const std::string& default_public_libraries() { |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 275 | static std::string list = InitDefaultPublicLibraries(/*for_preload*/ false); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 276 | return list; |
| 277 | } |
| 278 | |
| 279 | const std::string& runtime_public_libraries() { |
Martin Stjernholm | df96e1f | 2019-07-17 22:17:58 +0100 | [diff] [blame] | 280 | static std::string list = InitArtPublicLibraries(); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 281 | return list; |
| 282 | } |
| 283 | |
| 284 | const std::string& vendor_public_libraries() { |
| 285 | static std::string list = InitVendorPublicLibraries(); |
| 286 | return list; |
| 287 | } |
| 288 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 289 | const std::string& extended_public_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 290 | static std::string list = InitExtendedPublicLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 291 | return list; |
| 292 | } |
| 293 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 294 | const std::string& neuralnetworks_public_libraries() { |
| 295 | static std::string list = InitNeuralNetworksPublicLibraries(); |
| 296 | return list; |
| 297 | } |
| 298 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 299 | const std::string& llndk_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 300 | static std::string list = InitLlndkLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 301 | return list; |
| 302 | } |
| 303 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 304 | const std::string& vndksp_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 305 | static std::string list = InitVndkspLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 306 | return list; |
| 307 | } |
| 308 | |
Jiyong Park | 5db5d19 | 2019-07-22 20:29:08 +0900 | [diff] [blame] | 309 | namespace internal { |
| 310 | // Exported for testing |
| 311 | Result<std::vector<std::string>> ParseConfig( |
| 312 | const std::string& file_content, |
| 313 | const std::function<Result<bool>(const ConfigEntry& /* entry */)>& filter_fn) { |
| 314 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 315 | |
| 316 | std::vector<std::string> sonames; |
| 317 | for (auto& line : lines) { |
| 318 | auto trimmed_line = base::Trim(line); |
| 319 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 320 | continue; |
| 321 | } |
| 322 | |
| 323 | std::vector<std::string> tokens = android::base::Split(trimmed_line, " "); |
| 324 | if (tokens.size() < 1 || tokens.size() > 3) { |
| 325 | return Errorf("Malformed line \"{}\"", line); |
| 326 | } |
| 327 | struct ConfigEntry entry = {.soname = "", .nopreload = false, .bitness = ALL}; |
| 328 | size_t i = tokens.size(); |
| 329 | while (i-- > 0) { |
| 330 | if (tokens[i] == "nopreload") { |
| 331 | entry.nopreload = true; |
| 332 | } else if (tokens[i] == "32" || tokens[i] == "64") { |
| 333 | if (entry.bitness != ALL) { |
| 334 | return Errorf("Malformed line \"{}\": bitness can be specified only once", line); |
| 335 | } |
| 336 | entry.bitness = tokens[i] == "32" ? ONLY_32 : ONLY_64; |
| 337 | } else { |
| 338 | if (i != 0) { |
| 339 | return Errorf("Malformed line \"{}\"", line); |
| 340 | } |
| 341 | entry.soname = tokens[i]; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | // skip 32-bit lib on 64-bit process and vice versa |
| 346 | #if defined(__LP64__) |
| 347 | if (entry.bitness == ONLY_32) continue; |
| 348 | #else |
| 349 | if (entry.bitness == ONLY_64) continue; |
| 350 | #endif |
| 351 | |
| 352 | Result<bool> ret = filter_fn(entry); |
| 353 | if (!ret) { |
| 354 | return ret.error(); |
| 355 | } |
| 356 | if (*ret) { |
| 357 | // filter_fn has returned true. |
| 358 | sonames.push_back(entry.soname); |
| 359 | } |
| 360 | } |
| 361 | return sonames; |
| 362 | } |
| 363 | |
| 364 | } // namespace internal |
| 365 | |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 366 | } // namespace android::nativeloader |