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