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