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 | |
| 17 | #include "public_libraries.h" |
| 18 | #define LOG_TAG "nativeloader" |
| 19 | |
| 20 | #include <dirent.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <memory> |
| 24 | |
| 25 | #include "android-base/file.h" |
| 26 | #include "android-base/logging.h" |
| 27 | #include "android-base/properties.h" |
| 28 | #include "android-base/strings.h" |
| 29 | #include "log/log.h" |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame^] | 30 | #include "utils.h" |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 31 | |
| 32 | namespace android::nativeloader { |
| 33 | |
| 34 | using namespace std::string_literals; |
| 35 | |
| 36 | namespace { |
| 37 | // TODO(b/130388701) simplify the names below |
| 38 | constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] = "/etc/public.libraries.txt"; |
| 39 | constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-"; |
| 40 | constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen = |
| 41 | sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1; |
| 42 | constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt"; |
| 43 | constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen = |
| 44 | sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1; |
| 45 | constexpr const char kPublicNativeLibrariesVendorConfig[] = "/vendor/etc/public.libraries.txt"; |
| 46 | constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] = "/etc/llndk.libraries.txt"; |
| 47 | constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] = "/etc/vndksp.libraries.txt"; |
| 48 | |
| 49 | const std::vector<const std::string> kRuntimePublicLibraries = { |
| 50 | "libicuuc.so", |
| 51 | "libicui18n.so", |
| 52 | }; |
| 53 | |
Jiyong Park | f8802e5 | 2019-05-03 16:34:56 +0900 | [diff] [blame^] | 54 | constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB; |
Jiyong Park | 40a6077 | 2019-05-03 16:21:31 +0900 | [diff] [blame] | 55 | |
| 56 | std::string root_dir() { |
| 57 | static const char* android_root_env = getenv("ANDROID_ROOT"); |
| 58 | return android_root_env != nullptr ? android_root_env : "/system"; |
| 59 | } |
| 60 | |
| 61 | bool debuggable() { |
| 62 | bool debuggable = false; |
| 63 | debuggable = android::base::GetBoolProperty("ro.debuggable", false); |
| 64 | return debuggable; |
| 65 | } |
| 66 | |
| 67 | std::string vndk_version_str() { |
| 68 | std::string version = android::base::GetProperty("ro.vndk.version", ""); |
| 69 | if (version != "" && version != "current") { |
| 70 | return "." + version; |
| 71 | } |
| 72 | return ""; |
| 73 | } |
| 74 | |
| 75 | void insert_vndk_version_str(std::string* file_name) { |
| 76 | CHECK(file_name != nullptr); |
| 77 | size_t insert_pos = file_name->find_last_of("."); |
| 78 | if (insert_pos == std::string::npos) { |
| 79 | insert_pos = file_name->length(); |
| 80 | } |
| 81 | file_name->insert(insert_pos, vndk_version_str()); |
| 82 | } |
| 83 | |
| 84 | const std::function<bool(const std::string&, std::string*)> always_true = |
| 85 | [](const std::string&, std::string*) { return true; }; |
| 86 | |
| 87 | bool ReadConfig(const std::string& configFile, std::vector<std::string>* sonames, |
| 88 | const std::function<bool(const std::string& /* soname */, |
| 89 | std::string* /* error_msg */)>& check_soname, |
| 90 | std::string* error_msg = nullptr) { |
| 91 | // Read list of public native libraries from the config file. |
| 92 | std::string file_content; |
| 93 | if (!base::ReadFileToString(configFile, &file_content)) { |
| 94 | if (error_msg) *error_msg = strerror(errno); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | std::vector<std::string> lines = base::Split(file_content, "\n"); |
| 99 | |
| 100 | for (auto& line : lines) { |
| 101 | auto trimmed_line = base::Trim(line); |
| 102 | if (trimmed_line[0] == '#' || trimmed_line.empty()) { |
| 103 | continue; |
| 104 | } |
| 105 | size_t space_pos = trimmed_line.rfind(' '); |
| 106 | if (space_pos != std::string::npos) { |
| 107 | std::string type = trimmed_line.substr(space_pos + 1); |
| 108 | if (type != "32" && type != "64") { |
| 109 | if (error_msg) *error_msg = "Malformed line: " + line; |
| 110 | return false; |
| 111 | } |
| 112 | #if defined(__LP64__) |
| 113 | // Skip 32 bit public library. |
| 114 | if (type == "32") { |
| 115 | continue; |
| 116 | } |
| 117 | #else |
| 118 | // Skip 64 bit public library. |
| 119 | if (type == "64") { |
| 120 | continue; |
| 121 | } |
| 122 | #endif |
| 123 | trimmed_line.resize(space_pos); |
| 124 | } |
| 125 | |
| 126 | if (check_soname(trimmed_line, error_msg)) { |
| 127 | sonames->push_back(trimmed_line); |
| 128 | } else { |
| 129 | return false; |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | void ReadExtensionLibraries(const char* dirname, std::vector<std::string>* sonames) { |
| 136 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(dirname), closedir); |
| 137 | if (dir != nullptr) { |
| 138 | // Failing to opening the dir is not an error, which can happen in |
| 139 | // webview_zygote. |
| 140 | while (struct dirent* ent = readdir(dir.get())) { |
| 141 | if (ent->d_type != DT_REG && ent->d_type != DT_LNK) { |
| 142 | continue; |
| 143 | } |
| 144 | const std::string filename(ent->d_name); |
| 145 | if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) && |
| 146 | android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) { |
| 147 | const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen; |
| 148 | const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen; |
| 149 | const std::string company_name = filename.substr(start, end - start); |
| 150 | const std::string config_file_path = dirname + "/"s + filename; |
| 151 | LOG_ALWAYS_FATAL_IF( |
| 152 | company_name.empty(), |
| 153 | "Error extracting company name from public native library list file path \"%s\"", |
| 154 | config_file_path.c_str()); |
| 155 | |
| 156 | std::string error_msg; |
| 157 | |
| 158 | LOG_ALWAYS_FATAL_IF( |
| 159 | !ReadConfig(config_file_path, sonames, |
| 160 | [&company_name](const std::string& soname, std::string* error_msg) { |
| 161 | if (android::base::StartsWith(soname, "lib") && |
| 162 | android::base::EndsWith(soname, "." + company_name + ".so")) { |
| 163 | return true; |
| 164 | } else { |
| 165 | *error_msg = "Library name \"" + soname + |
| 166 | "\" does not end with the company name: " + company_name + |
| 167 | "."; |
| 168 | return false; |
| 169 | } |
| 170 | }, |
| 171 | &error_msg), |
| 172 | "Error reading public native library list from \"%s\": %s", config_file_path.c_str(), |
| 173 | error_msg.c_str()); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | } // namespace |
| 180 | |
| 181 | const std::string& system_public_libraries() { |
| 182 | static bool cached = false; |
| 183 | static std::string list; |
| 184 | if (!cached) { |
| 185 | std::string config_file = root_dir() + kPublicNativeLibrariesSystemConfigPathFromRoot; |
| 186 | std::vector<std::string> sonames; |
| 187 | std::string error_msg; |
| 188 | LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg), |
| 189 | "Error reading public native library list from \"%s\": %s", |
| 190 | config_file.c_str(), error_msg.c_str()); |
| 191 | |
| 192 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 193 | // variable to add libraries to the list. This is intended for platform tests only. |
| 194 | if (debuggable()) { |
| 195 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 196 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 197 | std::vector<std::string> additional_libs_vector = base::Split(additional_libs, ":"); |
| 198 | std::copy(additional_libs_vector.begin(), additional_libs_vector.end(), |
| 199 | std::back_inserter(sonames)); |
| 200 | } |
| 201 | } |
| 202 | |
| 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; |
| 218 | } |
| 219 | |
| 220 | auto it = std::find(sonames.begin(), sonames.end(), lib_name); |
| 221 | if (it != sonames.end()) { |
| 222 | sonames.erase(it); |
| 223 | } |
| 224 | } |
| 225 | list = android::base::Join(sonames, ':'); |
| 226 | cached = true; |
| 227 | } |
| 228 | return list; |
| 229 | } |
| 230 | |
| 231 | const std::string& runtime_public_libraries() { |
| 232 | static bool cached = false; |
| 233 | static std::string list; |
| 234 | if (!cached) { |
| 235 | list = android::base::Join(kRuntimePublicLibraries, ":"); |
| 236 | // For debuggable platform builds use ANDROID_ADDITIONAL_PUBLIC_LIBRARIES environment |
| 237 | // variable to add libraries to the list. This is intended for platform tests only. |
| 238 | if (debuggable()) { |
| 239 | const char* additional_libs = getenv("ANDROID_ADDITIONAL_PUBLIC_LIBRARIES"); |
| 240 | if (additional_libs != nullptr && additional_libs[0] != '\0') { |
| 241 | list = list + ':' + additional_libs; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | return list; |
| 246 | } |
| 247 | |
| 248 | const std::string& vendor_public_libraries() { |
| 249 | static bool cached = false; |
| 250 | static std::string list; |
| 251 | if (!cached) { |
| 252 | // This file is optional, quietly ignore if the file does not exist. |
| 253 | std::vector<std::string> sonames; |
| 254 | ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr); |
| 255 | list = android::base::Join(sonames, ':'); |
| 256 | cached = true; |
| 257 | } |
| 258 | return list; |
| 259 | } |
| 260 | |
| 261 | // read /system/etc/public.libraries-<companyname>.txt which contain partner defined |
| 262 | // system libs that are exposed to apps. The libs in the txt files must be |
| 263 | // named as lib<name>.<companyname>.so. |
| 264 | const std::string& oem_public_libraries() { |
| 265 | static bool cached = false; |
| 266 | static std::string list; |
| 267 | if (!cached) { |
| 268 | std::vector<std::string> sonames; |
| 269 | ReadExtensionLibraries("/system/etc", &sonames); |
| 270 | list = android::base::Join(sonames, ':'); |
| 271 | cached = true; |
| 272 | } |
| 273 | return list; |
| 274 | } |
| 275 | |
| 276 | // read /product/etc/public.libraries-<companyname>.txt which contain partner defined |
| 277 | // product libs that are exposed to apps. |
| 278 | const std::string& product_public_libraries() { |
| 279 | static bool cached = false; |
| 280 | static std::string list; |
| 281 | if (!cached) { |
| 282 | std::vector<std::string> sonames; |
| 283 | ReadExtensionLibraries("/product/etc", &sonames); |
| 284 | list = android::base::Join(sonames, ':'); |
| 285 | cached = true; |
| 286 | } |
| 287 | return list; |
| 288 | } |
| 289 | |
| 290 | const std::string& system_llndk_libraries() { |
| 291 | static bool cached = false; |
| 292 | static std::string list; |
| 293 | if (!cached) { |
| 294 | std::string config_file = root_dir() + kLlndkNativeLibrariesSystemConfigPathFromRoot; |
| 295 | insert_vndk_version_str(&config_file); |
| 296 | std::vector<std::string> sonames; |
| 297 | ReadConfig(config_file, &sonames, always_true, nullptr); |
| 298 | list = android::base::Join(sonames, ':'); |
| 299 | cached = true; |
| 300 | } |
| 301 | return list; |
| 302 | } |
| 303 | |
| 304 | const std::string& system_vndksp_libraries() { |
| 305 | static bool cached = false; |
| 306 | static std::string list; |
| 307 | if (!cached) { |
| 308 | std::string config_file = root_dir() + kVndkspNativeLibrariesSystemConfigPathFromRoot; |
| 309 | insert_vndk_version_str(&config_file); |
| 310 | std::vector<std::string> sonames; |
| 311 | ReadConfig(config_file, &sonames, always_true, nullptr); |
| 312 | list = android::base::Join(sonames, ':'); |
| 313 | cached = true; |
| 314 | } |
| 315 | return list; |
| 316 | } |
| 317 | |
| 318 | } // namespace android::nativeloader |