Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 17 | #include <cstdlib> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 18 | #include <fcntl.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 21 | #include <sys/types.h> |
| 22 | #include <sys/stat.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 23 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 24 | #include <android-base/file.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 25 | #include <android-base/logging.h> |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 26 | #include <android-base/properties.h> |
| 27 | #include <android-base/scopeguard.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 28 | #include <android-base/stringprintf.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 30 | #include <binder/Status.h> |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 32 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 33 | #include <gtest/gtest.h> |
| 34 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 35 | #include <selinux/android.h> |
| 36 | #include <selinux/avc.h> |
| 37 | |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 38 | #include "binder_test_utils.h" |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 39 | #include "dexopt.h" |
| 40 | #include "InstalldNativeService.h" |
| 41 | #include "globals.h" |
| 42 | #include "tests/test_utils.h" |
| 43 | #include "utils.h" |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 44 | #include "ziparchive/zip_writer.h" |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 45 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 46 | using android::base::ReadFully; |
| 47 | using android::base::unique_fd; |
| 48 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace installd { |
| 51 | |
| 52 | // TODO(calin): try to dedup this code. |
| 53 | #if defined(__arm__) |
| 54 | static const std::string kRuntimeIsa = "arm"; |
| 55 | #elif defined(__aarch64__) |
| 56 | static const std::string kRuntimeIsa = "arm64"; |
| 57 | #elif defined(__mips__) && !defined(__LP64__) |
| 58 | static const std::string kRuntimeIsa = "mips"; |
| 59 | #elif defined(__mips__) && defined(__LP64__) |
| 60 | static const std::string kRuntimeIsa = "mips64"; |
| 61 | #elif defined(__i386__) |
| 62 | static const std::string kRuntimeIsa = "x86"; |
| 63 | #elif defined(__x86_64__) |
| 64 | static const std::string kRuntimeIsa = "x86_64"; |
| 65 | #else |
| 66 | static const std::string kRuntimeIsa = "none"; |
| 67 | #endif |
| 68 | |
| 69 | int get_property(const char *key, char *value, const char *default_value) { |
| 70 | return property_get(key, value, default_value); |
| 71 | } |
| 72 | |
| 73 | bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path, |
| 74 | const char *instruction_set) { |
| 75 | return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set); |
| 76 | } |
| 77 | |
| 78 | bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path, |
| 79 | const char *instruction_set) { |
| 80 | return calculate_odex_file_path_default(path, apk_path, instruction_set); |
| 81 | } |
| 82 | |
| 83 | bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) { |
| 84 | return create_cache_path_default(path, src, instruction_set); |
| 85 | } |
| 86 | |
| 87 | static void run_cmd(const std::string& cmd) { |
| 88 | system(cmd.c_str()); |
| 89 | } |
| 90 | |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 91 | template <typename Visitor> |
| 92 | static void run_cmd_and_process_output(const std::string& cmd, const Visitor& visitor) { |
| 93 | FILE* file = popen(cmd.c_str(), "r"); |
| 94 | CHECK(file != nullptr) << "Failed to ptrace " << cmd; |
| 95 | char* line = nullptr; |
| 96 | while (true) { |
| 97 | size_t n = 0u; |
| 98 | ssize_t value = getline(&line, &n, file); |
| 99 | if (value == -1) { |
| 100 | break; |
| 101 | } |
| 102 | visitor(line); |
| 103 | } |
| 104 | free(line); |
| 105 | fclose(file); |
| 106 | } |
| 107 | |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 108 | static int mkdir(const std::string& path, uid_t owner, gid_t group, mode_t mode) { |
| 109 | int ret = ::mkdir(path.c_str(), mode); |
| 110 | if (ret != 0) { |
| 111 | return ret; |
| 112 | } |
| 113 | ret = ::chown(path.c_str(), owner, group); |
| 114 | if (ret != 0) { |
| 115 | return ret; |
| 116 | } |
| 117 | return ::chmod(path.c_str(), mode); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 120 | static int log_callback(int type, const char *fmt, ...) { // NOLINT |
| 121 | va_list ap; |
| 122 | int priority; |
| 123 | |
| 124 | switch (type) { |
| 125 | case SELINUX_WARNING: |
| 126 | priority = ANDROID_LOG_WARN; |
| 127 | break; |
| 128 | case SELINUX_INFO: |
| 129 | priority = ANDROID_LOG_INFO; |
| 130 | break; |
| 131 | default: |
| 132 | priority = ANDROID_LOG_ERROR; |
| 133 | break; |
| 134 | } |
| 135 | va_start(ap, fmt); |
| 136 | LOG_PRI_VA(priority, "SELinux", fmt, ap); |
| 137 | va_end(ap); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static bool init_selinux() { |
| 142 | int selinux_enabled = (is_selinux_enabled() > 0); |
| 143 | |
| 144 | union selinux_callback cb; |
| 145 | cb.func_log = log_callback; |
| 146 | selinux_set_callback(SELINUX_CB_LOG, cb); |
| 147 | |
| 148 | if (selinux_enabled && selinux_status_open(true) < 0) { |
| 149 | LOG(ERROR) << "Could not open selinux status; exiting"; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 156 | // Base64 encoding of a simple dex files with 2 methods. |
| 157 | static const char kDexFile[] = |
| 158 | "UEsDBBQAAAAIAOiOYUs9y6BLCgEAABQCAAALABwAY2xhc3Nlcy5kZXhVVAkAA/Ns+lkOHv1ZdXgL" |
| 159 | "AAEEI+UCAASIEwAAS0mt4DIwNmX4qpn7j/2wA7v7N+ZvoQpCJRlVx5SWa4YaiDAxMBQwMDBUhJkI" |
| 160 | "MUBBDyMDAzsDRJwFxAdioBDDHAYEYAbiFUAM1M5wAIhFGCGKDIDYAogdgNgDiH2BOAiI0xghekDm" |
| 161 | "sQIxGxQzM6ACRijNhCbOhCZfyohdPYyuh8szgtVkMkLsLhAAqeCDi+ejibPZZOZlltgxsDnqZSWW" |
| 162 | "JTKwOUFoZh9HayDhZM0g5AMS0M9JzEvX90/KSk0usWZgDAMaws5nAyXBzmpoYGlgAjsAyJoBMp0b" |
| 163 | "zQ8gGhbOTEhhzYwU3qxIYc2GFN6MClC/AhUyKUDMAYU9M1Qc5F8GKBscVgIQM0FxCwBQSwECHgMU" |
| 164 | "AAAACADojmFLPcugSwoBAAAUAgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAA/Ns" |
| 165 | "+ll1eAsAAQQj5QIABIgTAABQSwUGAAAAAAEAAQBRAAAATwEAAAAA"; |
| 166 | |
Andreas Gampe | 1b8700f | 2018-11-01 14:47:00 -0700 | [diff] [blame] | 167 | class DexoptTestEnvTest : public testing::Test { |
| 168 | }; |
| 169 | |
| 170 | TEST_F(DexoptTestEnvTest, CheckSelinux) { |
| 171 | ASSERT_EQ(1, is_selinux_enabled()); |
| 172 | |
| 173 | // Crude cutout for virtual devices. |
| 174 | #if !defined(__i386__) && !defined(__x86_64__) |
| 175 | constexpr bool kIsX86 = false; |
| 176 | #else |
| 177 | constexpr bool kIsX86 = true; |
| 178 | #endif |
Andreas Gampe | 1d1e7c9 | 2018-11-05 09:46:28 -0800 | [diff] [blame] | 179 | ASSERT_TRUE(1 == security_getenforce() || kIsX86 || true /* b/119032200 */); |
Andreas Gampe | 1b8700f | 2018-11-01 14:47:00 -0700 | [diff] [blame] | 180 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 181 | |
| 182 | class DexoptTest : public testing::Test { |
| 183 | protected: |
| 184 | static constexpr bool kDebug = false; |
| 185 | static constexpr uid_t kSystemUid = 1000; |
| 186 | static constexpr uid_t kSystemGid = 1000; |
| 187 | static constexpr int32_t kOSdkVersion = 25; |
| 188 | static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 189 | static constexpr int32_t kTestUserId = 0; |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 190 | static constexpr uid_t kTestAppId = 19999; |
| 191 | |
| 192 | const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId); |
| 193 | const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 194 | |
| 195 | InstalldNativeService* service_; |
| 196 | std::unique_ptr<std::string> volume_uuid_; |
| 197 | std::string package_name_; |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 198 | std::string apk_path_; |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 199 | std::string empty_dm_file_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 200 | std::string app_apk_dir_; |
| 201 | std::string app_private_dir_ce_; |
| 202 | std::string app_private_dir_de_; |
| 203 | std::string se_info_; |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 204 | std::string app_oat_dir_; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 205 | |
| 206 | int64_t ce_data_inode_; |
| 207 | |
| 208 | std::string secondary_dex_ce_; |
| 209 | std::string secondary_dex_ce_link_; |
| 210 | std::string secondary_dex_de_; |
| 211 | |
| 212 | virtual void SetUp() { |
| 213 | setenv("ANDROID_LOG_TAGS", "*:v", 1); |
| 214 | android::base::InitLogging(nullptr); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 215 | // Initialize the globals holding the file system main paths (/data/, /system/ etc..). |
| 216 | // This is needed in order to compute the application and profile paths. |
| 217 | ASSERT_TRUE(init_globals_from_data_and_root()); |
| 218 | // Initialize selinux log callbacks. |
| 219 | // This ensures that selinux is up and running and re-directs the selinux messages |
| 220 | // to logcat (in order to make it easier to investigate test results). |
| 221 | ASSERT_TRUE(init_selinux()); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 222 | service_ = new InstalldNativeService(); |
| 223 | |
| 224 | volume_uuid_ = nullptr; |
| 225 | package_name_ = "com.installd.test.dexopt"; |
| 226 | se_info_ = "default"; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 227 | app_apk_dir_ = android_app_dir + package_name_; |
| 228 | |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 229 | ASSERT_TRUE(create_mock_app()); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | virtual void TearDown() { |
| 233 | if (!kDebug) { |
| 234 | service_->destroyAppData( |
| 235 | volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_); |
| 236 | run_cmd("rm -rf " + app_apk_dir_); |
| 237 | run_cmd("rm -rf " + app_private_dir_ce_); |
| 238 | run_cmd("rm -rf " + app_private_dir_de_); |
| 239 | } |
| 240 | delete service_; |
| 241 | } |
| 242 | |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 243 | ::testing::AssertionResult create_mock_app() { |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 244 | // Create the oat dir. |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 245 | app_oat_dir_ = app_apk_dir_ + "/oat"; |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 246 | // For debug mode, the directory might already exist. Avoid erroring out. |
| 247 | if (mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755) != 0 && !kDebug) { |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 248 | return ::testing::AssertionFailure() << "Could not create app dir " << app_apk_dir_ |
| 249 | << " : " << strerror(errno); |
| 250 | } |
| 251 | binder::Status status = service_->createOatDir(app_oat_dir_, kRuntimeIsa); |
| 252 | if (!status.isOk()) { |
| 253 | return ::testing::AssertionFailure() << "Could not create oat dir: " |
| 254 | << status.toString8().c_str(); |
| 255 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 256 | |
| 257 | // Copy the primary apk. |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 258 | apk_path_ = app_apk_dir_ + "/base.jar"; |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 259 | std::string error_msg; |
| 260 | if (!WriteBase64ToFile(kDexFile, apk_path_, kSystemUid, kSystemGid, 0644, &error_msg)) { |
| 261 | return ::testing::AssertionFailure() << "Could not write base64 file to " << apk_path_ |
| 262 | << " : " << error_msg; |
| 263 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 264 | |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 265 | // Create an empty dm file. |
| 266 | empty_dm_file_ = apk_path_ + ".dm"; |
| 267 | { |
| 268 | int fd = open(empty_dm_file_.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 269 | if (fd < 0) { |
| 270 | return ::testing::AssertionFailure() << "Could not open " << empty_dm_file_; |
| 271 | } |
| 272 | FILE* file = fdopen(fd, "wb"); |
| 273 | if (file == nullptr) { |
| 274 | return ::testing::AssertionFailure() << "Null file for " << empty_dm_file_ |
| 275 | << " fd=" << fd; |
| 276 | } |
| 277 | ZipWriter writer(file); |
| 278 | // Add vdex to zip. |
| 279 | writer.StartEntry("primary.prof", ZipWriter::kCompress); |
| 280 | writer.FinishEntry(); |
| 281 | writer.Finish(); |
| 282 | close(fd); |
| 283 | } |
| 284 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 285 | // Create the app user data. |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 286 | status = service_->createAppData( |
| 287 | volume_uuid_, |
| 288 | package_name_, |
| 289 | kTestUserId, |
| 290 | kAppDataFlags, |
| 291 | kTestAppUid, |
| 292 | se_info_, |
| 293 | kOSdkVersion, |
| 294 | &ce_data_inode_); |
| 295 | if (!status.isOk()) { |
| 296 | return ::testing::AssertionFailure() << "Could not create app data: " |
| 297 | << status.toString8().c_str(); |
| 298 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 299 | |
| 300 | // Create a secondary dex file on CE storage |
| 301 | const char* volume_uuid_cstr = volume_uuid_ == nullptr ? nullptr : volume_uuid_->c_str(); |
| 302 | app_private_dir_ce_ = create_data_user_ce_package_path( |
| 303 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 304 | secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar"; |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 305 | if (!WriteBase64ToFile(kDexFile, |
| 306 | secondary_dex_ce_, |
| 307 | kTestAppUid, |
| 308 | kTestAppGid, |
| 309 | 0600, |
| 310 | &error_msg)) { |
| 311 | return ::testing::AssertionFailure() << "Could not write base64 file to " |
| 312 | << secondary_dex_ce_ << " : " << error_msg; |
| 313 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 314 | std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link( |
| 315 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 316 | secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar"; |
| 317 | |
| 318 | // Create a secondary dex file on DE storage. |
| 319 | app_private_dir_de_ = create_data_user_de_package_path( |
| 320 | volume_uuid_cstr, kTestUserId, package_name_.c_str()); |
| 321 | secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar"; |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 322 | if (!WriteBase64ToFile(kDexFile, |
| 323 | secondary_dex_de_, |
| 324 | kTestAppUid, |
| 325 | kTestAppGid, |
| 326 | 0600, |
| 327 | &error_msg)) { |
| 328 | return ::testing::AssertionFailure() << "Could not write base64 file to " |
| 329 | << secondary_dex_de_ << " : " << error_msg; |
| 330 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 331 | |
| 332 | // Fix app data uid. |
Andreas Gampe | 2ef09a7 | 2018-08-29 14:50:41 -0700 | [diff] [blame] | 333 | status = service_->fixupAppData(volume_uuid_, kTestUserId); |
| 334 | if (!status.isOk()) { |
| 335 | return ::testing::AssertionFailure() << "Could not fixup app data: " |
| 336 | << status.toString8().c_str(); |
| 337 | } |
| 338 | |
| 339 | return ::testing::AssertionSuccess(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 343 | std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) { |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 344 | std::string::size_type end = path.rfind('.'); |
| 345 | std::string::size_type start = path.rfind('/', end); |
| 346 | return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" + |
| 347 | path.substr(start + 1, end - start) + type; |
| 348 | } |
| 349 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 350 | void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 351 | bool should_binder_call_succeed, bool should_dex_be_compiled = true, |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 352 | /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1, |
| 353 | const char* class_loader_context = nullptr) { |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 354 | if (uid == -1) { |
| 355 | uid = kTestAppUid; |
| 356 | } |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 357 | if (class_loader_context == nullptr) { |
| 358 | class_loader_context = "&"; |
| 359 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 360 | std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_)); |
| 361 | int32_t dexopt_needed = 0; // does not matter; |
| 362 | std::unique_ptr<std::string> out_path = nullptr; // does not matter |
| 363 | int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag; |
| 364 | std::string compiler_filter = "speed-profile"; |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 365 | std::unique_ptr<std::string> class_loader_context_ptr( |
| 366 | new std::string(class_loader_context)); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 367 | std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_)); |
| 368 | bool downgrade = false; |
David Brazdil | 570d398 | 2018-01-16 20:15:43 +0000 | [diff] [blame] | 369 | int32_t target_sdk_version = 0; // default |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 370 | std::unique_ptr<std::string> profile_name_ptr = nullptr; |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 371 | std::unique_ptr<std::string> dm_path_ptr = nullptr; |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 372 | std::unique_ptr<std::string> compilation_reason_ptr = nullptr; |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 373 | |
| 374 | binder::Status result = service_->dexopt(path, |
| 375 | uid, |
| 376 | package_name_ptr, |
| 377 | kRuntimeIsa, |
| 378 | dexopt_needed, |
| 379 | out_path, |
| 380 | dex_flags, |
| 381 | compiler_filter, |
| 382 | volume_uuid_, |
| 383 | class_loader_context_ptr, |
| 384 | se_info_ptr, |
David Brazdil | 570d398 | 2018-01-16 20:15:43 +0000 | [diff] [blame] | 385 | downgrade, |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 386 | target_sdk_version, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 387 | profile_name_ptr, |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 388 | dm_path_ptr, |
| 389 | compilation_reason_ptr); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 390 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 391 | int expected_access = should_dex_be_compiled ? 0 : -1; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 392 | std::string odex = GetSecondaryDexArtifact(path, "odex"); |
| 393 | std::string vdex = GetSecondaryDexArtifact(path, "vdex"); |
| 394 | std::string art = GetSecondaryDexArtifact(path, "art"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 395 | ASSERT_EQ(expected_access, access(odex.c_str(), R_OK)); |
| 396 | ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK)); |
| 397 | ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image. |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 398 | if (binder_result != nullptr) { |
| 399 | *binder_result = result; |
| 400 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | void reconcile_secondary_dex(const std::string& path, int32_t storage_flag, |
| 404 | bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted, |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 405 | int32_t uid = -1, std::string* package_override = nullptr) { |
| 406 | if (uid == -1) { |
| 407 | uid = kTestAppUid; |
| 408 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 409 | std::vector<std::string> isas; |
| 410 | isas.push_back(kRuntimeIsa); |
| 411 | bool out_secondary_dex_exists = false; |
| 412 | binder::Status result = service_->reconcileSecondaryDexFile( |
| 413 | path, |
| 414 | package_override == nullptr ? package_name_ : *package_override, |
| 415 | uid, |
| 416 | isas, |
| 417 | volume_uuid_, |
| 418 | storage_flag, |
| 419 | &out_secondary_dex_exists); |
| 420 | |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 421 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 422 | ASSERT_EQ(should_dex_exist, out_secondary_dex_exists); |
| 423 | |
| 424 | int expected_access = should_dex_be_deleted ? -1 : 0; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 425 | std::string odex = GetSecondaryDexArtifact(path, "odex"); |
| 426 | std::string vdex = GetSecondaryDexArtifact(path, "vdex"); |
| 427 | std::string art = GetSecondaryDexArtifact(path, "art"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 428 | ASSERT_EQ(expected_access, access(odex.c_str(), F_OK)); |
| 429 | ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK)); |
| 430 | ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image. |
| 431 | } |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 432 | |
| 433 | void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) { |
| 434 | struct stat st; |
| 435 | ASSERT_EQ(0, stat(file.c_str(), &st)); |
| 436 | ASSERT_EQ(uid, st.st_uid); |
| 437 | ASSERT_EQ(gid, st.st_gid); |
| 438 | ASSERT_EQ(mode, st.st_mode); |
| 439 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 440 | |
| 441 | void CompilePrimaryDexOk(std::string compiler_filter, |
| 442 | int32_t dex_flags, |
| 443 | const char* oat_dir, |
| 444 | int32_t uid, |
| 445 | int32_t dexopt_needed, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 446 | binder::Status* binder_result = nullptr, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 447 | const char* dm_path = nullptr, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 448 | bool downgrade = false) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 449 | CompilePrimaryDex(compiler_filter, |
| 450 | dex_flags, |
| 451 | oat_dir, |
| 452 | uid, |
| 453 | dexopt_needed, |
| 454 | dm_path, |
| 455 | downgrade, |
| 456 | true, |
| 457 | binder_result); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void CompilePrimaryDexFail(std::string compiler_filter, |
| 461 | int32_t dex_flags, |
| 462 | const char* oat_dir, |
| 463 | int32_t uid, |
| 464 | int32_t dexopt_needed, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 465 | binder::Status* binder_result = nullptr, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 466 | const char* dm_path = nullptr, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 467 | bool downgrade = false) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 468 | CompilePrimaryDex(compiler_filter, |
| 469 | dex_flags, |
| 470 | oat_dir, |
| 471 | uid, |
| 472 | dexopt_needed, |
| 473 | dm_path, |
| 474 | downgrade, |
| 475 | false, |
| 476 | binder_result); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void CompilePrimaryDex(std::string compiler_filter, |
| 480 | int32_t dex_flags, |
| 481 | const char* oat_dir, |
| 482 | int32_t uid, |
| 483 | int32_t dexopt_needed, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 484 | const char* dm_path, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 485 | bool downgrade, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 486 | bool should_binder_call_succeed, |
| 487 | /*out */ binder::Status* binder_result) { |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 488 | std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_)); |
| 489 | std::unique_ptr<std::string> out_path( |
| 490 | oat_dir == nullptr ? nullptr : new std::string(oat_dir)); |
| 491 | std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&")); |
| 492 | std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_)); |
| 493 | int32_t target_sdk_version = 0; // default |
| 494 | std::unique_ptr<std::string> profile_name_ptr(new std::string("primary.prof")); |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 495 | std::unique_ptr<std::string> dm_path_ptr = nullptr; |
| 496 | if (dm_path != nullptr) { |
| 497 | dm_path_ptr.reset(new std::string(dm_path)); |
| 498 | } |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 499 | std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason")); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 500 | |
| 501 | bool prof_result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 502 | ASSERT_BINDER_SUCCESS(service_->prepareAppProfile( |
Jeff Sharkey | 1ebe77f | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 503 | package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 504 | dm_path_ptr, &prof_result)); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 505 | ASSERT_TRUE(prof_result); |
| 506 | |
| 507 | binder::Status result = service_->dexopt(apk_path_, |
| 508 | uid, |
| 509 | package_name_ptr, |
| 510 | kRuntimeIsa, |
| 511 | dexopt_needed, |
| 512 | out_path, |
| 513 | dex_flags, |
| 514 | compiler_filter, |
| 515 | volume_uuid_, |
| 516 | class_loader_context_ptr, |
| 517 | se_info_ptr, |
| 518 | downgrade, |
| 519 | target_sdk_version, |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 520 | profile_name_ptr, |
Calin Juravle | 2efc402 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 521 | dm_path_ptr, |
| 522 | compilation_reason_ptr); |
Andreas Gampe | f6a93b1 | 2018-02-27 09:51:15 -0800 | [diff] [blame] | 523 | ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str(); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 524 | |
| 525 | if (!should_binder_call_succeed) { |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 526 | if (binder_result != nullptr) { |
| 527 | *binder_result = result; |
| 528 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | // Check the access to the compiler output. |
| 532 | // - speed-profile artifacts are not world-wide readable. |
| 533 | // - files are owned by the system uid. |
| 534 | std::string odex = GetPrimaryDexArtifact(oat_dir, apk_path_, "odex"); |
| 535 | std::string vdex = GetPrimaryDexArtifact(oat_dir, apk_path_, "vdex"); |
| 536 | std::string art = GetPrimaryDexArtifact(oat_dir, apk_path_, "art"); |
| 537 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 538 | bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0; |
| 539 | mode_t mode = S_IFREG | (is_public ? 0644 : 0640); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 540 | CheckFileAccess(odex, kSystemUid, uid, mode); |
| 541 | CheckFileAccess(vdex, kSystemUid, uid, mode); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 542 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 543 | if (compiler_filter == "speed-profile") { |
| 544 | CheckFileAccess(art, kSystemUid, uid, mode); |
| 545 | } |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 546 | if (binder_result != nullptr) { |
| 547 | *binder_result = result; |
| 548 | } |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | std::string GetPrimaryDexArtifact(const char* oat_dir, |
| 552 | const std::string& dex_path, |
| 553 | const std::string& type) { |
| 554 | if (oat_dir == nullptr) { |
| 555 | std::string path = dex_path; |
| 556 | for (auto it = path.begin() + 1; it < path.end(); ++it) { |
| 557 | if (*it == '/') { |
| 558 | *it = '@'; |
| 559 | } |
| 560 | } |
| 561 | return android_data_dir + DALVIK_CACHE + '/' + kRuntimeIsa + "/" + path |
| 562 | + "@classes.dex"; |
| 563 | } else { |
| 564 | std::string::size_type name_end = dex_path.rfind('.'); |
| 565 | std::string::size_type name_start = dex_path.rfind('/'); |
| 566 | return std::string(oat_dir) + "/" + kRuntimeIsa + "/" + |
| 567 | dex_path.substr(name_start + 1, name_end - name_start) + type; |
| 568 | } |
| 569 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 570 | }; |
| 571 | |
| 572 | |
| 573 | TEST_F(DexoptTest, DexoptSecondaryCe) { |
| 574 | LOG(INFO) << "DexoptSecondaryCe"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 575 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 576 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 577 | } |
| 578 | |
| 579 | TEST_F(DexoptTest, DexoptSecondaryCeLink) { |
| 580 | LOG(INFO) << "DexoptSecondaryCeLink"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 581 | CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 582 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 583 | } |
| 584 | |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 585 | TEST_F(DexoptTest, DexoptSecondaryCeWithContext) { |
| 586 | LOG(INFO) << "DexoptSecondaryCeWithContext"; |
| 587 | std::string class_loader_context = "PCL[" + secondary_dex_ce_ + "]"; |
| 588 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
| 589 | /*binder_ok*/ true, /*compile_ok*/ true, nullptr, -1, class_loader_context.c_str()); |
| 590 | } |
| 591 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 592 | TEST_F(DexoptTest, DexoptSecondaryDe) { |
| 593 | LOG(INFO) << "DexoptSecondaryDe"; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 594 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 595 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 596 | } |
| 597 | |
David Brazdil | 4f6027a | 2019-03-19 11:44:21 +0000 | [diff] [blame] | 598 | TEST_F(DexoptTest, DexoptSecondaryDeWithContext) { |
| 599 | LOG(INFO) << "DexoptSecondaryDeWithContext"; |
| 600 | std::string class_loader_context = "PCL[" + secondary_dex_de_ + "]"; |
| 601 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
| 602 | /*binder_ok*/ true, /*compile_ok*/ true, nullptr, -1, class_loader_context.c_str()); |
| 603 | } |
| 604 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 605 | TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) { |
| 606 | LOG(INFO) << "DexoptSecondaryDoesNotExist"; |
| 607 | // If the file validates but does not exist we do not treat it as an error. |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 608 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 609 | CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 610 | /*binder_ok*/ true, /*compile_ok*/ false, &status); |
| 611 | EXPECT_STREQ(status.toString8().c_str(), "No error"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) { |
| 615 | LOG(INFO) << "DexoptSecondaryStorageValidationError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 616 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 617 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 618 | /*binder_ok*/ false, /*compile_ok*/ false, &status); |
Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 619 | EXPECT_STREQ(status.toString8().c_str(), |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 620 | "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer path validation failed'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) { |
| 624 | LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 625 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 626 | CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 627 | /*binder_ok*/ false, /*compile_ok*/ false, &status); |
Andreas Gampe | 3008bbe | 2018-02-28 20:24:48 -0800 | [diff] [blame] | 628 | EXPECT_STREQ(status.toString8().c_str(), |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 629 | "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer path validation failed'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) { |
| 633 | LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 634 | binder::Status status; |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 635 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 636 | /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid); |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 637 | EXPECT_STREQ(status.toString8().c_str(), |
| 638 | "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer open zip failed'"); |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 641 | TEST_F(DexoptTest, DexoptPrimaryPublic) { |
| 642 | LOG(INFO) << "DexoptPrimaryPublic"; |
| 643 | CompilePrimaryDexOk("verify", |
| 644 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 645 | app_oat_dir_.c_str(), |
| 646 | kTestAppGid, |
| 647 | DEX2OAT_FROM_SCRATCH); |
| 648 | } |
| 649 | |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 650 | TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) { |
| 651 | LOG(INFO) << "DexoptPrimaryFailedInvalidFilter"; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 652 | binder::Status status; |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 653 | CompilePrimaryDexFail("awesome-filter", |
| 654 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC, |
| 655 | app_oat_dir_.c_str(), |
| 656 | kTestAppGid, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 657 | DEX2OAT_FROM_SCRATCH, |
| 658 | &status); |
| 659 | EXPECT_STREQ(status.toString8().c_str(), |
Martijn Coenen | 290de46 | 2019-03-21 13:14:59 +0100 | [diff] [blame] | 660 | "Status(-8, EX_SERVICE_SPECIFIC): \'256: Dex2oat invocation for " |
Andreas Gampe | fa2dadd | 2018-02-28 19:52:47 -0800 | [diff] [blame] | 661 | "/data/app/com.installd.test.dexopt/base.jar failed: unspecified dex2oat error'"); |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 664 | TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) { |
| 665 | LOG(INFO) << "DexoptPrimaryProfileNonPublic"; |
| 666 | CompilePrimaryDexOk("speed-profile", |
Mathieu Chartier | 2d5a1bc | 2018-04-03 14:55:43 -0700 | [diff] [blame] | 667 | DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 668 | app_oat_dir_.c_str(), |
| 669 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 670 | DEX2OAT_FROM_SCRATCH, |
| 671 | /*binder_result=*/nullptr, |
| 672 | empty_dm_file_.c_str()); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | TEST_F(DexoptTest, DexoptPrimaryProfilePublic) { |
| 676 | LOG(INFO) << "DexoptPrimaryProfilePublic"; |
Calin Juravle | 5bd1c72 | 2018-02-01 17:23:54 +0000 | [diff] [blame] | 677 | CompilePrimaryDexOk("speed-profile", |
Mathieu Chartier | 2d5a1bc | 2018-04-03 14:55:43 -0700 | [diff] [blame] | 678 | DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC | |
| 679 | DEXOPT_GENERATE_APP_IMAGE, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 680 | app_oat_dir_.c_str(), |
| 681 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 682 | DEX2OAT_FROM_SCRATCH, |
| 683 | /*binder_result=*/nullptr, |
| 684 | empty_dm_file_.c_str()); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) { |
| 688 | LOG(INFO) << "DexoptPrimaryBackgroundOk"; |
| 689 | CompilePrimaryDexOk("speed-profile", |
Mathieu Chartier | 2d5a1bc | 2018-04-03 14:55:43 -0700 | [diff] [blame] | 690 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 691 | DEXOPT_GENERATE_APP_IMAGE, |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 692 | app_oat_dir_.c_str(), |
| 693 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 694 | DEX2OAT_FROM_SCRATCH, |
| 695 | /*binder_result=*/nullptr, |
| 696 | empty_dm_file_.c_str()); |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 699 | TEST_F(DexoptTest, ResolveStartupConstStrings) { |
| 700 | LOG(INFO) << "DexoptDex2oatResolveStartupStrings"; |
| 701 | const std::string property = "persist.device_config.runtime.dex2oat_resolve_startup_strings"; |
| 702 | const std::string previous_value = android::base::GetProperty(property, ""); |
| 703 | auto restore_property = android::base::make_scope_guard([=]() { |
| 704 | android::base::SetProperty(property, previous_value); |
| 705 | }); |
| 706 | std::string odex = GetPrimaryDexArtifact(app_oat_dir_.c_str(), apk_path_, "odex"); |
| 707 | // Disable the property to start. |
| 708 | bool found_disable = false; |
| 709 | ASSERT_TRUE(android::base::SetProperty(property, "false")) << property; |
| 710 | CompilePrimaryDexOk("speed-profile", |
| 711 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 712 | DEXOPT_GENERATE_APP_IMAGE, |
| 713 | app_oat_dir_.c_str(), |
| 714 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 715 | DEX2OAT_FROM_SCRATCH, |
| 716 | /*binder_result=*/nullptr, |
| 717 | empty_dm_file_.c_str()); |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 718 | run_cmd_and_process_output( |
| 719 | "oatdump --header-only --oat-file=" + odex, |
| 720 | [&](const std::string& line) { |
| 721 | if (line.find("--resolve-startup-const-strings=false") != std::string::npos) { |
| 722 | found_disable = true; |
| 723 | } |
| 724 | }); |
| 725 | EXPECT_TRUE(found_disable); |
| 726 | // Enable the property and inspect that .art artifact is larger. |
| 727 | bool found_enable = false; |
| 728 | ASSERT_TRUE(android::base::SetProperty(property, "true")) << property; |
| 729 | CompilePrimaryDexOk("speed-profile", |
| 730 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED | |
| 731 | DEXOPT_GENERATE_APP_IMAGE, |
| 732 | app_oat_dir_.c_str(), |
| 733 | kTestAppGid, |
liulvping | 6190774 | 2018-08-21 09:36:52 +0800 | [diff] [blame^] | 734 | DEX2OAT_FROM_SCRATCH, |
| 735 | /*binder_result=*/nullptr, |
| 736 | empty_dm_file_.c_str()); |
Mathieu Chartier | d41622c | 2019-01-31 12:59:39 -0800 | [diff] [blame] | 737 | run_cmd_and_process_output( |
| 738 | "oatdump --header-only --oat-file=" + odex, |
| 739 | [&](const std::string& line) { |
| 740 | if (line.find("--resolve-startup-const-strings=true") != std::string::npos) { |
| 741 | found_enable = true; |
| 742 | } |
| 743 | }); |
| 744 | EXPECT_TRUE(found_enable); |
| 745 | } |
| 746 | |
Calin Juravle | c4f6a0b | 2018-02-01 01:27:24 +0000 | [diff] [blame] | 747 | class PrimaryDexReCompilationTest : public DexoptTest { |
| 748 | public: |
| 749 | virtual void SetUp() { |
| 750 | DexoptTest::SetUp(); |
| 751 | CompilePrimaryDexOk("verify", |
| 752 | DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC, |
| 753 | app_oat_dir_.c_str(), |
| 754 | kTestAppGid, |
| 755 | DEX2OAT_FROM_SCRATCH); |
| 756 | std::string odex = GetSecondaryDexArtifact(apk_path_, "odex"); |
| 757 | std::string vdex = GetSecondaryDexArtifact(apk_path_, "vdex"); |
| 758 | |
| 759 | first_compilation_odex_fd_.reset(open(odex.c_str(), O_RDONLY)); |
| 760 | first_compilation_vdex_fd_.reset(open(vdex.c_str(), O_RDONLY)); |
| 761 | } |
| 762 | |
| 763 | virtual void TearDown() { |
| 764 | first_compilation_odex_fd_.reset(-1); |
| 765 | first_compilation_vdex_fd_.reset(-1); |
| 766 | DexoptTest::TearDown(); |
| 767 | } |
| 768 | |
| 769 | protected: |
| 770 | unique_fd first_compilation_odex_fd_; |
| 771 | unique_fd first_compilation_vdex_fd_; |
| 772 | }; |
| 773 | |
| 774 | TEST_F(PrimaryDexReCompilationTest, DexoptPrimaryUpdateInPlaceVdex) { |
| 775 | LOG(INFO) << "DexoptPrimaryUpdateInPlaceVdex"; |
| 776 | |
| 777 | CompilePrimaryDexOk("verify", |
| 778 | DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC, |
| 779 | app_oat_dir_.c_str(), |
| 780 | kTestAppGid, |
| 781 | DEX2OAT_FOR_BOOT_IMAGE); |
| 782 | } |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 783 | |
| 784 | class ReconcileTest : public DexoptTest { |
| 785 | virtual void SetUp() { |
| 786 | DexoptTest::SetUp(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 787 | CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 788 | /*binder_ok*/ true, /*compile_ok*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 789 | CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE, |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 790 | /*binder_ok*/ true, /*compile_ok*/ true); |
| 791 | } |
| 792 | }; |
| 793 | |
| 794 | TEST_F(ReconcileTest, ReconcileSecondaryCeExists) { |
| 795 | LOG(INFO) << "ReconcileSecondaryCeExists"; |
| 796 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 797 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 798 | } |
| 799 | |
| 800 | TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) { |
| 801 | LOG(INFO) << "ReconcileSecondaryCeLinkExists"; |
| 802 | reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE, |
| 803 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 804 | } |
| 805 | |
| 806 | TEST_F(ReconcileTest, ReconcileSecondaryDeExists) { |
| 807 | LOG(INFO) << "ReconcileSecondaryDeExists"; |
| 808 | reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE, |
| 809 | /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false); |
| 810 | } |
| 811 | |
| 812 | TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) { |
| 813 | LOG(INFO) << "ReconcileSecondaryDeDoesNotExist"; |
| 814 | run_cmd("rm -rf " + secondary_dex_de_); |
| 815 | reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE, |
| 816 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true); |
| 817 | } |
| 818 | |
| 819 | TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) { |
| 820 | // Validation errors will not clean the odex/vdex/art files but will mark |
| 821 | // the file as non existent so that the PM knows it should purge it from its |
| 822 | // records. |
| 823 | LOG(INFO) << "ReconcileSecondaryStorageValidationError"; |
| 824 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE, |
| 825 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false); |
| 826 | } |
| 827 | |
| 828 | TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) { |
| 829 | LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError"; |
| 830 | // Attempt to reconcile the dex files of the test app from a different app. |
| 831 | std::string another_app = "another.app"; |
| 832 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 833 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app); |
| 834 | } |
| 835 | |
| 836 | TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) { |
| 837 | LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError"; |
| 838 | reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE, |
| 839 | /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid); |
| 840 | } |
| 841 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 842 | class ProfileTest : public DexoptTest { |
| 843 | protected: |
| 844 | std::string cur_profile_; |
| 845 | std::string ref_profile_; |
| 846 | std::string snap_profile_; |
| 847 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 848 | static constexpr const char* kPrimaryProfile = "primary.prof"; |
| 849 | |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 850 | virtual void SetUp() { |
| 851 | DexoptTest::SetUp(); |
| 852 | cur_profile_ = create_current_profile_path( |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 853 | kTestUserId, package_name_, kPrimaryProfile, /*is_secondary_dex*/ false); |
| 854 | ref_profile_ = create_reference_profile_path(package_name_, kPrimaryProfile, |
| 855 | /*is_secondary_dex*/ false); |
| 856 | snap_profile_ = create_snapshot_profile_path(package_name_, kPrimaryProfile); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 859 | void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode, |
| 860 | int32_t num_dex) { |
| 861 | run_cmd("profman --generate-test-profile-seed=" + std::to_string(num_dex) + |
| 862 | " --generate-test-profile-num-dex=" + std::to_string(num_dex) + |
| 863 | " --generate-test-profile=" + path); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 864 | ::chmod(path.c_str(), mode); |
| 865 | ::chown(path.c_str(), uid, gid); |
| 866 | } |
| 867 | |
| 868 | void SetupProfiles(bool setup_ref) { |
| 869 | SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1); |
| 870 | if (setup_ref) { |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 871 | SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 875 | void createProfileSnapshot(int32_t appid, const std::string& package_name, |
| 876 | bool expected_result) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 877 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 878 | ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot( |
| 879 | appid, package_name, kPrimaryProfile, apk_path_, &result)); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 880 | ASSERT_EQ(expected_result, result); |
| 881 | |
| 882 | if (!expected_result) { |
| 883 | // Do not check the files if we expect to fail. |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | // Check that the snapshot was created witht he expected acess flags. |
| 888 | CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG); |
| 889 | |
| 890 | // The snapshot should be equivalent to the merge of profiles. |
| 891 | std::string expected_profile_content = snap_profile_ + ".expected"; |
| 892 | run_cmd("rm -f " + expected_profile_content); |
| 893 | run_cmd("touch " + expected_profile_content); |
| 894 | run_cmd("profman --profile-file=" + cur_profile_ + |
| 895 | " --profile-file=" + ref_profile_ + |
Calin Juravle | 40b5f46 | 2018-02-07 04:11:52 -0800 | [diff] [blame] | 896 | " --reference-profile-file=" + expected_profile_content + |
| 897 | " --apk=" + apk_path_); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 898 | |
| 899 | ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_)); |
| 900 | |
| 901 | pid_t pid = fork(); |
| 902 | if (pid == 0) { |
| 903 | /* child */ |
| 904 | TransitionToSystemServer(); |
| 905 | |
| 906 | // System server should be able to open the the spanshot. |
| 907 | unique_fd fd(open(snap_profile_.c_str(), O_RDONLY)); |
| 908 | ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno); |
| 909 | _exit(0); |
| 910 | } |
| 911 | /* parent */ |
| 912 | ASSERT_TRUE(WIFEXITED(wait_child(pid))); |
| 913 | } |
| 914 | |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 915 | void mergePackageProfiles(const std::string& package_name, |
| 916 | const std::string& code_path, |
| 917 | bool expected_result) { |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 918 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 919 | ASSERT_BINDER_SUCCESS(service_->mergeProfiles( |
| 920 | kTestAppUid, package_name, code_path, &result)); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 921 | ASSERT_EQ(expected_result, result); |
| 922 | |
| 923 | if (!expected_result) { |
| 924 | // Do not check the files if we expect to fail. |
| 925 | return; |
| 926 | } |
| 927 | |
| 928 | // Check that the snapshot was created witht he expected acess flags. |
| 929 | CheckFileAccess(ref_profile_, kTestAppUid, kTestAppUid, 0600 | S_IFREG); |
| 930 | |
| 931 | // The snapshot should be equivalent to the merge of profiles. |
| 932 | std::string ref_profile_content = ref_profile_ + ".expected"; |
| 933 | run_cmd("rm -f " + ref_profile_content); |
| 934 | run_cmd("touch " + ref_profile_content); |
| 935 | run_cmd("profman --profile-file=" + cur_profile_ + |
| 936 | " --profile-file=" + ref_profile_ + |
| 937 | " --reference-profile-file=" + ref_profile_content); |
| 938 | |
| 939 | ASSERT_TRUE(AreFilesEqual(ref_profile_content, ref_profile_)); |
| 940 | } |
| 941 | |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 942 | // TODO(calin): add dex metadata tests once the ART change is merged. |
| 943 | void preparePackageProfile(const std::string& package_name, const std::string& profile_name, |
| 944 | bool expected_result) { |
| 945 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 946 | ASSERT_BINDER_SUCCESS(service_->prepareAppProfile( |
Jeff Sharkey | 1ebe77f | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 947 | package_name, kTestUserId, kTestAppId, profile_name, apk_path_, |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 948 | /*dex_metadata*/ nullptr, &result)); |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 949 | ASSERT_EQ(expected_result, result); |
| 950 | |
| 951 | if (!expected_result) { |
| 952 | // Do not check the files if we expect to fail. |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | std::string code_path_cur_prof = create_current_profile_path( |
| 957 | kTestUserId, package_name, profile_name, /*is_secondary_dex*/ false); |
| 958 | std::string code_path_ref_profile = create_reference_profile_path(package_name, |
| 959 | profile_name, /*is_secondary_dex*/ false); |
| 960 | |
| 961 | // Check that we created the current profile. |
| 962 | CheckFileAccess(code_path_cur_prof, kTestAppUid, kTestAppUid, 0600 | S_IFREG); |
| 963 | |
| 964 | // Without dex metadata we don't generate a reference profile. |
| 965 | ASSERT_EQ(-1, access(code_path_ref_profile.c_str(), R_OK)); |
| 966 | } |
| 967 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 968 | protected: |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 969 | void TransitionToSystemServer() { |
| 970 | ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid)); |
Andreas Gampe | f04b0b4 | 2019-03-25 11:20:57 -0700 | [diff] [blame] | 971 | int32_t res = selinux_android_setcon("u:r:system_server:s0"); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 972 | ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno); |
| 973 | } |
| 974 | |
| 975 | bool AreFilesEqual(const std::string& file1, const std::string& file2) { |
| 976 | std::vector<uint8_t> content1; |
| 977 | std::vector<uint8_t> content2; |
| 978 | |
| 979 | if (!ReadAll(file1, &content1)) return false; |
| 980 | if (!ReadAll(file2, &content2)) return false; |
| 981 | return content1 == content2; |
| 982 | } |
| 983 | |
| 984 | bool ReadAll(const std::string& file, std::vector<uint8_t>* content) { |
| 985 | unique_fd fd(open(file.c_str(), O_RDONLY)); |
| 986 | if (fd < 0) { |
| 987 | PLOG(ERROR) << "Failed to open " << file; |
| 988 | return false; |
| 989 | } |
| 990 | struct stat st; |
| 991 | if (fstat(fd, &st) != 0) { |
| 992 | PLOG(ERROR) << "Failed to stat " << file; |
| 993 | return false; |
| 994 | } |
| 995 | content->resize(st.st_size); |
| 996 | bool result = ReadFully(fd, content->data(), content->size()); |
| 997 | if (!result) { |
| 998 | PLOG(ERROR) << "Failed to read " << file; |
| 999 | } |
| 1000 | return result; |
| 1001 | } |
| 1002 | }; |
| 1003 | |
| 1004 | TEST_F(ProfileTest, ProfileSnapshotOk) { |
| 1005 | LOG(INFO) << "ProfileSnapshotOk"; |
| 1006 | |
| 1007 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1008 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | // The reference profile is created on the fly. We need to be able to |
| 1012 | // snapshot without one. |
| 1013 | TEST_F(ProfileTest, ProfileSnapshotOkNoReference) { |
| 1014 | LOG(INFO) << "ProfileSnapshotOkNoReference"; |
| 1015 | |
| 1016 | SetupProfiles(/*setup_ref*/ false); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1017 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) { |
| 1021 | LOG(INFO) << "ProfileSnapshotFailWrongPackage"; |
| 1022 | |
| 1023 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1024 | createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) { |
| 1028 | LOG(INFO) << "ProfileSnapshotDestroySnapshot"; |
| 1029 | |
| 1030 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | c3596c3 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 1031 | createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1032 | |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1033 | ASSERT_BINDER_SUCCESS(service_->destroyProfileSnapshot(package_name_, kPrimaryProfile)); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 1034 | struct stat st; |
| 1035 | ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st)); |
| 1036 | ASSERT_EQ(ENOENT, errno); |
| 1037 | } |
| 1038 | |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1039 | TEST_F(ProfileTest, ProfileMergeOk) { |
| 1040 | LOG(INFO) << "ProfileMergeOk"; |
| 1041 | |
| 1042 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1043 | mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | // The reference profile is created on the fly. We need to be able to |
| 1047 | // merge without one. |
| 1048 | TEST_F(ProfileTest, ProfileMergeOkNoReference) { |
| 1049 | LOG(INFO) << "ProfileMergeOkNoReference"; |
| 1050 | |
| 1051 | SetupProfiles(/*setup_ref*/ false); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1052 | mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | TEST_F(ProfileTest, ProfileMergeFailWrongPackage) { |
| 1056 | LOG(INFO) << "ProfileMergeFailWrongPackage"; |
| 1057 | |
| 1058 | SetupProfiles(/*setup_ref*/ true); |
Calin Juravle | 408cd4a | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1059 | mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false); |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 1062 | TEST_F(ProfileTest, ProfileDirOk) { |
| 1063 | LOG(INFO) << "ProfileDirOk"; |
| 1064 | |
| 1065 | std::string cur_profile_dir = create_primary_current_profile_package_dir_path( |
| 1066 | kTestUserId, package_name_); |
| 1067 | std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1068 | kPrimaryProfile, /*is_secondary_dex*/false); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 1069 | std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_); |
| 1070 | |
| 1071 | CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1072 | CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR); |
| 1073 | } |
| 1074 | |
| 1075 | // Verify that the profile directories are fixed up during an upgrade. |
| 1076 | // (The reference profile directory is prepared lazily). |
| 1077 | TEST_F(ProfileTest, ProfileDirOkAfterFixup) { |
| 1078 | LOG(INFO) << "ProfileDirOkAfterFixup"; |
| 1079 | |
| 1080 | std::string cur_profile_dir = create_primary_current_profile_package_dir_path( |
| 1081 | kTestUserId, package_name_); |
| 1082 | std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_, |
Calin Juravle | 824a64d | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 1083 | kPrimaryProfile, /*is_secondary_dex*/false); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1084 | std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_); |
| 1085 | |
| 1086 | // Simulate a pre-P setup by changing the owner to kTestAppGid and permissions to 0700. |
| 1087 | ASSERT_EQ(0, chown(ref_profile_dir.c_str(), kTestAppGid, kTestAppGid)); |
| 1088 | ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700)); |
| 1089 | |
| 1090 | // Run createAppData again which will offer to fix-up the profile directories. |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1091 | ASSERT_BINDER_SUCCESS(service_->createAppData( |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1092 | volume_uuid_, |
| 1093 | package_name_, |
| 1094 | kTestUserId, |
| 1095 | kAppDataFlags, |
| 1096 | kTestAppUid, |
| 1097 | se_info_, |
| 1098 | kOSdkVersion, |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1099 | &ce_data_inode_)); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1100 | |
| 1101 | // Check the file access. |
| 1102 | CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 1103 | CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
Calin Juravle | c3b049e | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 1106 | TEST_F(ProfileTest, ProfilePrepareOk) { |
| 1107 | LOG(INFO) << "ProfilePrepareOk"; |
| 1108 | preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true); |
| 1109 | } |
| 1110 | |
| 1111 | TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) { |
| 1112 | LOG(INFO) << "ProfilePrepareFailInvalidPackage"; |
| 1113 | preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false); |
| 1114 | } |
| 1115 | |
| 1116 | TEST_F(ProfileTest, ProfilePrepareFailProfileChangedUid) { |
| 1117 | LOG(INFO) << "ProfilePrepareFailProfileChangedUid"; |
| 1118 | SetupProfiles(/*setup_ref*/ false); |
| 1119 | // Change the uid on the profile to trigger a failure. |
| 1120 | ::chown(cur_profile_.c_str(), kTestAppUid + 1, kTestAppGid + 1); |
| 1121 | preparePackageProfile(package_name_, "primary.prof", /*expected_result*/ false); |
| 1122 | } |
| 1123 | |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1124 | |
| 1125 | class BootProfileTest : public ProfileTest { |
| 1126 | public: |
| 1127 | virtual void setup() { |
| 1128 | ProfileTest::SetUp(); |
| 1129 | intial_android_profiles_dir = android_profiles_dir; |
| 1130 | } |
| 1131 | |
| 1132 | virtual void TearDown() { |
| 1133 | android_profiles_dir = intial_android_profiles_dir; |
| 1134 | ProfileTest::TearDown(); |
| 1135 | } |
| 1136 | |
| 1137 | void UpdateAndroidProfilesDir(const std::string& profile_dir) { |
| 1138 | android_profiles_dir = profile_dir; |
| 1139 | // We need to create the reference profile directory in the new profile dir. |
| 1140 | run_cmd("mkdir -p " + profile_dir + "/ref"); |
| 1141 | } |
| 1142 | |
| 1143 | void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) { |
| 1144 | bool result; |
Andreas Gampe | f448ced | 2019-01-31 09:22:27 -0800 | [diff] [blame] | 1145 | ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot( |
| 1146 | -1, "android", "android.prof", classpath, &result)); |
Calin Juravle | 0d0a492 | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1147 | ASSERT_EQ(expected_result, result); |
| 1148 | |
| 1149 | if (!expected_result) { |
| 1150 | // Do not check the files if we expect to fail. |
| 1151 | return; |
| 1152 | } |
| 1153 | |
| 1154 | // Check that the snapshot was created with he expected access flags. |
| 1155 | const std::string boot_profile = create_snapshot_profile_path("android", "android.prof"); |
| 1156 | CheckFileAccess(boot_profile, kSystemUid, kSystemGid, 0600 | S_IFREG); |
| 1157 | |
| 1158 | pid_t pid = fork(); |
| 1159 | if (pid == 0) { |
| 1160 | /* child */ |
| 1161 | TransitionToSystemServer(); |
| 1162 | |
| 1163 | // System server should be able to open the snapshot. |
| 1164 | unique_fd fd(open(boot_profile.c_str(), O_RDONLY)); |
| 1165 | ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno); |
| 1166 | _exit(0); |
| 1167 | } |
| 1168 | /* parent */ |
| 1169 | ASSERT_TRUE(WIFEXITED(wait_child(pid))); |
| 1170 | } |
| 1171 | protected: |
| 1172 | std::string intial_android_profiles_dir; |
| 1173 | }; |
| 1174 | |
| 1175 | TEST_F(BootProfileTest, BootProfileSnapshotOk) { |
| 1176 | LOG(INFO) << "BootProfileSnapshotOk"; |
| 1177 | char* boot_classpath = getenv("BOOTCLASSPATH"); |
| 1178 | ASSERT_TRUE(boot_classpath != nullptr); |
| 1179 | createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true); |
| 1180 | } |
| 1181 | |
| 1182 | TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) { |
| 1183 | LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath"; |
| 1184 | |
| 1185 | createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false); |
| 1186 | } |
| 1187 | |
| 1188 | TEST_F(BootProfileTest, BootProfileSnapshotOkNoProfiles) { |
| 1189 | LOG(INFO) << "BootProfileSnapshotOkNoProfiles"; |
| 1190 | char* boot_classpath = getenv("BOOTCLASSPATH"); |
| 1191 | ASSERT_TRUE(boot_classpath != nullptr); |
| 1192 | |
| 1193 | // The app_apk_dir has no profiles. So we shouldn't be able to merge anything. |
| 1194 | // Still, this is not a failure case. |
| 1195 | UpdateAndroidProfilesDir(app_apk_dir_); |
| 1196 | createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true); |
| 1197 | } |
| 1198 | |
| 1199 | // Verify that profile collection. |
| 1200 | TEST_F(BootProfileTest, CollectProfiles) { |
| 1201 | LOG(INFO) << "CollectProfiles"; |
| 1202 | |
| 1203 | // Create some profile directories mimicking the real profile structure. |
| 1204 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/ref"); |
| 1205 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/0/"); |
| 1206 | run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/1/"); |
| 1207 | // Create an empty profile. |
| 1208 | run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/1/primary.prof"); |
| 1209 | // Create a random file. |
| 1210 | run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/0/non.profile.file"); |
| 1211 | |
| 1212 | // Create some non-empty profiles. |
| 1213 | std::string current_prof = app_private_dir_de_ + "/profiles/cur/0/primary.prof"; |
| 1214 | run_cmd("echo 1 > " + current_prof); |
| 1215 | std::string ref_prof = app_private_dir_de_ + "/profiles/ref/primary.prof"; |
| 1216 | run_cmd("echo 1 > " + ref_prof); |
| 1217 | |
| 1218 | UpdateAndroidProfilesDir(app_private_dir_de_ + "/profiles"); |
| 1219 | |
| 1220 | std::vector<std::string> profiles; |
| 1221 | collect_profiles(&profiles); |
| 1222 | |
| 1223 | // Only two profiles should be in the output. |
| 1224 | ASSERT_EQ(2u, profiles.size()); |
| 1225 | ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), current_prof) != profiles.end()); |
| 1226 | ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), ref_prof) != profiles.end()); |
| 1227 | } |
| 1228 | |
Calin Juravle | f74a737 | 2019-02-28 20:29:41 -0800 | [diff] [blame] | 1229 | TEST_F(DexoptTest, select_execution_binary) { |
| 1230 | LOG(INFO) << "DexoptTestselect_execution_binary"; |
| 1231 | |
| 1232 | std::string release_str = app_private_dir_ce_ + "/release"; |
| 1233 | std::string debug_str = app_private_dir_ce_ + "/debug"; |
| 1234 | |
| 1235 | // Setup the binaries. Note that we only need executable files to actually |
| 1236 | // test the execution binary selection |
| 1237 | run_cmd("touch " + release_str); |
| 1238 | run_cmd("touch " + debug_str); |
| 1239 | run_cmd("chmod 777 " + release_str); |
| 1240 | run_cmd("chmod 777 " + debug_str); |
| 1241 | |
| 1242 | const char* release = release_str.c_str(); |
| 1243 | const char* debug = debug_str.c_str(); |
| 1244 | |
| 1245 | ASSERT_STREQ(release, select_execution_binary( |
| 1246 | release, |
| 1247 | debug, |
| 1248 | /*background_job_compile=*/ false, |
| 1249 | /*is_debug_runtime=*/ false, |
| 1250 | /*is_release=*/ false, |
| 1251 | /*is_debuggable_build=*/ false)); |
| 1252 | |
| 1253 | ASSERT_STREQ(release, select_execution_binary( |
| 1254 | release, |
| 1255 | debug, |
| 1256 | /*background_job_compile=*/ true, |
| 1257 | /*is_debug_runtime=*/ false, |
| 1258 | /*is_release=*/ true, |
| 1259 | /*is_debuggable_build=*/ true)); |
| 1260 | |
| 1261 | ASSERT_STREQ(debug, select_execution_binary( |
| 1262 | release, |
| 1263 | debug, |
| 1264 | /*background_job_compile=*/ false, |
| 1265 | /*is_debug_runtime=*/ true, |
| 1266 | /*is_release=*/ false, |
| 1267 | /*is_debuggable_build=*/ false)); |
| 1268 | |
| 1269 | ASSERT_STREQ(debug, select_execution_binary( |
| 1270 | release, |
| 1271 | debug, |
| 1272 | /*background_job_compile=*/ true, |
| 1273 | /*is_debug_runtime=*/ false, |
| 1274 | /*is_release=*/ false, |
| 1275 | /*is_debuggable_build=*/ true)); |
| 1276 | |
| 1277 | |
| 1278 | // Select the release when the debug file is not there. |
| 1279 | ASSERT_STREQ(release, select_execution_binary( |
| 1280 | release, |
| 1281 | "does_not_exist", |
| 1282 | /*background_job_compile=*/ false, |
| 1283 | /*is_debug_runtime=*/ true, |
| 1284 | /*is_release=*/ false, |
| 1285 | /*is_debuggable_build=*/ false)); |
| 1286 | } |
| 1287 | |
Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 1288 | } // namespace installd |
| 1289 | } // namespace android |