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