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 | |
| 37 | using namespace std::string_literals; |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 38 | using android::base::ErrnoError; |
| 39 | using android::base::Errorf; |
| 40 | using android::base::Result; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 41 | |
| 42 | namespace { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 43 | |
| 44 | constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt"; |
| 45 | constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-"; |
| 46 | constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt"; |
| 47 | constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt"; |
| 48 | constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt"; |
| 49 | constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt"; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 50 | |
| 51 | const std::vector<const std::string> kRuntimePublicLibraries = { |
| 52 | "libicuuc.so", |
| 53 | "libicui18n.so", |
| 54 | }; |
| 55 | |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame] | 56 | constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 57 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 58 | constexpr const char* kNeuralNetworksApexPublicLibrary = "libneuralnetworks.so"; |
| 59 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 60 | // TODO(b/130388701): do we need this? |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 61 | std::string root_dir() { |
| 62 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 63 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 64 | } |
| 65 | |
| 66 | bool debuggable() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 67 | static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 68 | return debuggable; |
| 69 | } |
| 70 | |
| 71 | std::string vndk_version_str() { |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 72 | static std::string version = android::base::GetProperty("ro.vndk.version", ""); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 73 | if (version != "" && version != "current") { |
| 74 | return "." + version; |
| 75 | } |
| 76 | return ""; |
| 77 | } |
| 78 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 79 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 80 | // variable to add libraries to the list. This is intended for platform tests only. |
| 81 | std::string additional_public_libraries() { |
| 82 | if (debuggable()) { |
| 83 | const char* val = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 84 | return val ? val : ""; |
| 85 | } |
| 86 | return ""; |
| 87 | } |
| 88 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 89 | void InsertVndkVersionStr(std::string* file_name) { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 90 | CHECK(file_name != nullptr); |
| 91 | size_t insert_pos = file_name->find_last_of("."); |
| 92 | if (insert_pos == std::string::npos) { |
| 93 | insert_pos = file_name->length(); |
| 94 | } |
| 95 | file_name->insert(insert_pos, vndk_version_str()); |
| 96 | } |
| 97 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 98 | const std::function<Result<void>(const std::string&)> always_true = |
| 99 | [](const std::string&) -> Result<void> { return {}; }; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 100 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 101 | Result<std::vector<std::string>> ReadConfig( |
| 102 | const std::string& configFile, |
| 103 | const std::function<Result<void>(const std::string& /* soname */)>& check_soname) { |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 104 | // Read list of public native libraries from the config file. |
| 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 | } |
| 109 | |
| 110 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 111 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 112 | std::vector<std::string> sonames; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 113 | for (auto& line : lines) { |
| 114 | auto trimmed_line = base::Trim(line); |
| 115 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 116 | continue; |
| 117 | } |
| 118 | size_t space_pos = trimmed_line.rfind(' '); |
| 119 | if (space_pos != std::string::npos) { |
| 120 | std::string type = trimmed_line.substr(space_pos + 1); |
| 121 | if (type != "32" && type != "64") { |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 122 | return Errorf("Malformed line: {}", line); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 123 | } |
| 124 | #if defined(__LP64__) |
| 125 | // Skip 32 bit public library. |
| 126 | if (type == "32") { |
| 127 | continue; |
| 128 | } |
| 129 | #else |
| 130 | // Skip 64 bit public library. |
| 131 | if (type == "64") { |
| 132 | continue; |
| 133 | } |
| 134 | #endif |
| 135 | trimmed_line.resize(space_pos); |
| 136 | } |
| 137 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 138 | auto ret = check_soname(trimmed_line); |
| 139 | if (!ret) { |
| 140 | return ret.error(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 141 | } |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 142 | sonames.push_back(trimmed_line); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 143 | } |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 144 | return sonames; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 148 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 149 | if (dir != nullptr) { |
| 150 | // Failing to opening the dir is not an error, which can happen in |
| 151 | // webview_zygote. |
| 152 | while (struct dirent* ent = readdir(dir.get())) { |
| 153 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 154 | continue; |
| 155 | } |
| 156 | const std::string filename(ent->d_name); |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 157 | std::string_view fn = filename; |
| 158 | if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) && |
| 159 | android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) { |
| 160 | const std::string company_name(fn); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 161 | const std::string config_file_path = dirname + "/"s + filename; |
| 162 | LOG_ALWAYS_FATAL_IF( |
| 163 | company_name.empty(), |
| 164 | "Error extracting company name from public native library list file path \"%s\"", |
| 165 | config_file_path.c_str()); |
| 166 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 167 | auto ret = ReadConfig( |
| 168 | config_file_path, [&company_name](const std::string& soname) -> Result<void> { |
| 169 | if (android::base::StartsWith(soname, "lib") && |
| 170 | android::base::EndsWith(soname, "." + company_name + ".so")) { |
| 171 | return {}; |
| 172 | } else { |
| 173 | return Errorf("Library name \"{}\" does not end with the company name {}.", soname, |
| 174 | company_name); |
| 175 | } |
| 176 | }); |
| 177 | if (ret) { |
| 178 | sonames->insert(sonames->end(), ret->begin(), ret->end()); |
| 179 | } else { |
| 180 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 181 | config_file_path.c_str(), ret.error().message().c_str()); |
| 182 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 188 | static std::string InitDefaultPublicLibraries() { |
| 189 | std::string config_file = root_dir() + kDefaultPublicLibrariesFile; |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 190 | auto sonames = ReadConfig(config_file, always_true); |
| 191 | if (!sonames) { |
| 192 | LOG_ALWAYS_FATAL("Error reading public native library list from \"%s\": %s", |
| 193 | config_file.c_str(), sonames.error().message().c_str()); |
| 194 | return ""; |
| 195 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 196 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 197 | std::string additional_libs = additional_public_libraries(); |
| 198 | if (!additional_libs.empty()) { |
| 199 | auto vec = base::Split(additional_libs, ":"); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 200 | std::copy(vec.begin(), vec.end(), std::back_inserter(*sonames)); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 201 | } |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 202 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 203 | // Remove the public libs in the runtime namespace. |
| 204 | // These libs are listed in public.android.txt, but we don't want the rest of android |
| 205 | // in default namespace to dlopen the libs. |
| 206 | // For example, libicuuc.so is exposed to classloader namespace from runtime namespace. |
| 207 | // Unfortunately, it does not have stable C symbols, and default namespace should only use |
| 208 | // stable symbols in libandroidicu.so. http://b/120786417 |
| 209 | for (const std::string& lib_name : kRuntimePublicLibraries) { |
| 210 | std::string path(kRuntimeApexLibPath); |
| 211 | path.append("/").append(lib_name); |
| 212 | |
| 213 | struct stat s; |
| 214 | // Do nothing if the path in /apex does not exist. |
| 215 | // Runtime APEX must be mounted since libnativeloader is in the same APEX |
| 216 | if (stat(path.c_str(), &s) != 0) { |
| 217 | continue; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 218 | } |
| 219 | |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 220 | auto it = std::find(sonames->begin(), sonames->end(), lib_name); |
| 221 | if (it != sonames->end()) { |
| 222 | sonames->erase(it); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 223 | } |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 224 | } |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 225 | |
| 226 | // Remove the public libs in the nnapi namespace. |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 227 | auto it = std::find(sonames->begin(), sonames->end(), kNeuralNetworksApexPublicLibrary); |
| 228 | if (it != sonames->end()) { |
| 229 | sonames->erase(it); |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 230 | } |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 231 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static std::string InitRuntimePublicLibraries() { |
| 235 | CHECK(sizeof(kRuntimePublicLibraries) > 0); |
| 236 | std::string list = android::base::Join(kRuntimePublicLibraries, ":"); |
| 237 | |
| 238 | std::string additional_libs = additional_public_libraries(); |
| 239 | if (!additional_libs.empty()) { |
| 240 | list = list + ':' + additional_libs; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 241 | } |
| 242 | return list; |
| 243 | } |
| 244 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 245 | static std::string InitVendorPublicLibraries() { |
| 246 | // This file is optional, quietly ignore if the file does not exist. |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 247 | auto sonames = ReadConfig(kVendorPublicLibrariesFile, always_true); |
| 248 | if (!sonames) { |
| 249 | return ""; |
| 250 | } |
| 251 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 252 | } |
| 253 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 254 | // read /system/etc/public.libraries-<companyname>.txt and |
| 255 | // /product/etc/public.libraries-<companyname>.txt which contain partner defined |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 256 | // system libs that are exposed to apps. The libs in the txt files must be |
| 257 | // named as lib<name>.<companyname>.so. |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 258 | static std::string InitExtendedPublicLibraries() { |
| 259 | std::vector<std::string> sonames; |
| 260 | ReadExtensionLibraries("/system/etc", &sonames); |
| 261 | ReadExtensionLibraries("/product/etc", &sonames); |
| 262 | return android::base::Join(sonames, ':'); |
| 263 | } |
| 264 | |
| 265 | static std::string InitLlndkLibraries() { |
| 266 | std::string config_file = kLlndkLibrariesFile; |
| 267 | InsertVndkVersionStr(&config_file); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 268 | auto sonames = ReadConfig(config_file, always_true); |
| 269 | if (!sonames) { |
| 270 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 271 | return ""; |
| 272 | } |
| 273 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static std::string InitVndkspLibraries() { |
| 277 | std::string config_file = kVndkLibrariesFile; |
| 278 | InsertVndkVersionStr(&config_file); |
Jiyong Park | 8f4afc8 | 2019-07-22 14:20:40 +0900 | [diff] [blame] | 279 | auto sonames = ReadConfig(config_file, always_true); |
| 280 | if (!sonames) { |
| 281 | LOG_ALWAYS_FATAL("%s", sonames.error().message().c_str()); |
| 282 | return ""; |
| 283 | } |
| 284 | return android::base::Join(*sonames, ':'); |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 285 | } |
| 286 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 287 | static std::string InitNeuralNetworksPublicLibraries() { |
| 288 | return kNeuralNetworksApexPublicLibrary; |
| 289 | } |
| 290 | |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 291 | } // namespace |
| 292 | |
| 293 | const std::string& default_public_libraries() { |
| 294 | static std::string list = InitDefaultPublicLibraries(); |
| 295 | return list; |
| 296 | } |
| 297 | |
| 298 | const std::string& runtime_public_libraries() { |
| 299 | static std::string list = InitRuntimePublicLibraries(); |
| 300 | return list; |
| 301 | } |
| 302 | |
| 303 | const std::string& vendor_public_libraries() { |
| 304 | static std::string list = InitVendorPublicLibraries(); |
| 305 | return list; |
| 306 | } |
| 307 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 308 | const std::string& extended_public_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 309 | static std::string list = InitExtendedPublicLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 310 | return list; |
| 311 | } |
| 312 | |
Przemyslaw Szczepaniak | 0bb871d | 2019-07-10 12:08:57 +0100 | [diff] [blame] | 313 | const std::string& neuralnetworks_public_libraries() { |
| 314 | static std::string list = InitNeuralNetworksPublicLibraries(); |
| 315 | return list; |
| 316 | } |
| 317 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 318 | const std::string& llndk_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 319 | static std::string list = InitLlndkLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 320 | return list; |
| 321 | } |
| 322 | |
Jiyong Park | 5b8b306 | 2019-05-03 18:11:49 +0900 | [diff] [blame] | 323 | const std::string& vndksp_libraries() { |
Jiyong Park | 4b5a37c | 2019-05-09 17:17:37 +0900 | [diff] [blame] | 324 | static std::string list = InitVndkspLibraries(); |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 325 | return list; |
| 326 | } |
| 327 | |
| 328 | } // namespace android::nativeloader |