blob: d50c486c134d8a3107c35624015ce89e7989edfb [file] [log] [blame]
Calin Juravle7d765462017-09-04 15:57:10 -07001/*
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 Juravle0d0a4922018-01-23 19:54:11 -080017#include <cstdlib>
Calin Juravle29591732017-11-20 17:46:19 -080018#include <fcntl.h>
Calin Juravle7d765462017-09-04 15:57:10 -070019#include <stdlib.h>
20#include <string.h>
Calin Juravle29591732017-11-20 17:46:19 -080021#include <sys/types.h>
22#include <sys/stat.h>
Calin Juravle7d765462017-09-04 15:57:10 -070023
Calin Juravle29591732017-11-20 17:46:19 -080024#include <android-base/file.h>
Calin Juravle7d765462017-09-04 15:57:10 -070025#include <android-base/logging.h>
Mathieu Chartierd41622c2019-01-31 12:59:39 -080026#include <android-base/properties.h>
27#include <android-base/scopeguard.h>
Calin Juravle7d765462017-09-04 15:57:10 -070028#include <android-base/stringprintf.h>
Calin Juravle29591732017-11-20 17:46:19 -080029#include <android-base/unique_fd.h>
Andreas Gampef448ced2019-01-31 09:22:27 -080030#include <binder/Status.h>
Calin Juravle7d765462017-09-04 15:57:10 -070031#include <cutils/properties.h>
Calin Juravle29591732017-11-20 17:46:19 -080032
Calin Juravle7d765462017-09-04 15:57:10 -070033#include <gtest/gtest.h>
34
Calin Juravle29591732017-11-20 17:46:19 -080035#include <selinux/android.h>
36#include <selinux/avc.h>
37
Andreas Gampef448ced2019-01-31 09:22:27 -080038#include "binder_test_utils.h"
Calin Juravle7d765462017-09-04 15:57:10 -070039#include "dexopt.h"
40#include "InstalldNativeService.h"
41#include "globals.h"
42#include "tests/test_utils.h"
43#include "utils.h"
44
Calin Juravle29591732017-11-20 17:46:19 -080045using android::base::ReadFully;
46using android::base::unique_fd;
47
Calin Juravle7d765462017-09-04 15:57:10 -070048namespace android {
49namespace installd {
50
51// TODO(calin): try to dedup this code.
52#if defined(__arm__)
53static const std::string kRuntimeIsa = "arm";
54#elif defined(__aarch64__)
55static const std::string kRuntimeIsa = "arm64";
56#elif defined(__mips__) && !defined(__LP64__)
57static const std::string kRuntimeIsa = "mips";
58#elif defined(__mips__) && defined(__LP64__)
59static const std::string kRuntimeIsa = "mips64";
60#elif defined(__i386__)
61static const std::string kRuntimeIsa = "x86";
62#elif defined(__x86_64__)
63static const std::string kRuntimeIsa = "x86_64";
64#else
65static const std::string kRuntimeIsa = "none";
66#endif
67
68int get_property(const char *key, char *value, const char *default_value) {
69 return property_get(key, value, default_value);
70}
71
72bool calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
73 const char *instruction_set) {
74 return calculate_oat_file_path_default(path, oat_dir, apk_path, instruction_set);
75}
76
77bool calculate_odex_file_path(char path[PKG_PATH_MAX], const char *apk_path,
78 const char *instruction_set) {
79 return calculate_odex_file_path_default(path, apk_path, instruction_set);
80}
81
82bool create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set) {
83 return create_cache_path_default(path, src, instruction_set);
84}
85
86static void run_cmd(const std::string& cmd) {
87 system(cmd.c_str());
88}
89
Mathieu Chartierd41622c2019-01-31 12:59:39 -080090template <typename Visitor>
91static void run_cmd_and_process_output(const std::string& cmd, const Visitor& visitor) {
92 FILE* file = popen(cmd.c_str(), "r");
93 CHECK(file != nullptr) << "Failed to ptrace " << cmd;
94 char* line = nullptr;
95 while (true) {
96 size_t n = 0u;
97 ssize_t value = getline(&line, &n, file);
98 if (value == -1) {
99 break;
100 }
101 visitor(line);
102 }
103 free(line);
104 fclose(file);
105}
106
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700107static int mkdir(const std::string& path, uid_t owner, gid_t group, mode_t mode) {
108 int ret = ::mkdir(path.c_str(), mode);
109 if (ret != 0) {
110 return ret;
111 }
112 ret = ::chown(path.c_str(), owner, group);
113 if (ret != 0) {
114 return ret;
115 }
116 return ::chmod(path.c_str(), mode);
Calin Juravle7d765462017-09-04 15:57:10 -0700117}
118
Calin Juravle29591732017-11-20 17:46:19 -0800119static int log_callback(int type, const char *fmt, ...) { // NOLINT
120 va_list ap;
121 int priority;
122
123 switch (type) {
124 case SELINUX_WARNING:
125 priority = ANDROID_LOG_WARN;
126 break;
127 case SELINUX_INFO:
128 priority = ANDROID_LOG_INFO;
129 break;
130 default:
131 priority = ANDROID_LOG_ERROR;
132 break;
133 }
134 va_start(ap, fmt);
135 LOG_PRI_VA(priority, "SELinux", fmt, ap);
136 va_end(ap);
137 return 0;
138}
139
140static bool init_selinux() {
141 int selinux_enabled = (is_selinux_enabled() > 0);
142
143 union selinux_callback cb;
144 cb.func_log = log_callback;
145 selinux_set_callback(SELINUX_CB_LOG, cb);
146
147 if (selinux_enabled && selinux_status_open(true) < 0) {
148 LOG(ERROR) << "Could not open selinux status; exiting";
149 return false;
150 }
151
152 return true;
153}
154
Calin Juravle7d765462017-09-04 15:57:10 -0700155// Base64 encoding of a simple dex files with 2 methods.
156static const char kDexFile[] =
157 "UEsDBBQAAAAIAOiOYUs9y6BLCgEAABQCAAALABwAY2xhc3Nlcy5kZXhVVAkAA/Ns+lkOHv1ZdXgL"
158 "AAEEI+UCAASIEwAAS0mt4DIwNmX4qpn7j/2wA7v7N+ZvoQpCJRlVx5SWa4YaiDAxMBQwMDBUhJkI"
159 "MUBBDyMDAzsDRJwFxAdioBDDHAYEYAbiFUAM1M5wAIhFGCGKDIDYAogdgNgDiH2BOAiI0xghekDm"
160 "sQIxGxQzM6ACRijNhCbOhCZfyohdPYyuh8szgtVkMkLsLhAAqeCDi+ejibPZZOZlltgxsDnqZSWW"
161 "JTKwOUFoZh9HayDhZM0g5AMS0M9JzEvX90/KSk0usWZgDAMaws5nAyXBzmpoYGlgAjsAyJoBMp0b"
162 "zQ8gGhbOTEhhzYwU3qxIYc2GFN6MClC/AhUyKUDMAYU9M1Qc5F8GKBscVgIQM0FxCwBQSwECHgMU"
163 "AAAACADojmFLPcugSwoBAAAUAgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAA/Ns"
164 "+ll1eAsAAQQj5QIABIgTAABQSwUGAAAAAAEAAQBRAAAATwEAAAAA";
165
Andreas Gampe1b8700f2018-11-01 14:47:00 -0700166class DexoptTestEnvTest : public testing::Test {
167};
168
169TEST_F(DexoptTestEnvTest, CheckSelinux) {
170 ASSERT_EQ(1, is_selinux_enabled());
171
172 // Crude cutout for virtual devices.
173#if !defined(__i386__) && !defined(__x86_64__)
174 constexpr bool kIsX86 = false;
175#else
176 constexpr bool kIsX86 = true;
177#endif
Andreas Gampe1d1e7c92018-11-05 09:46:28 -0800178 ASSERT_TRUE(1 == security_getenforce() || kIsX86 || true /* b/119032200 */);
Andreas Gampe1b8700f2018-11-01 14:47:00 -0700179}
Calin Juravle7d765462017-09-04 15:57:10 -0700180
181class DexoptTest : public testing::Test {
182protected:
183 static constexpr bool kDebug = false;
184 static constexpr uid_t kSystemUid = 1000;
185 static constexpr uid_t kSystemGid = 1000;
186 static constexpr int32_t kOSdkVersion = 25;
187 static constexpr int32_t kAppDataFlags = FLAG_STORAGE_CE | FLAG_STORAGE_DE;
Calin Juravle7d765462017-09-04 15:57:10 -0700188 static constexpr int32_t kTestUserId = 0;
Calin Juravled2affb82017-11-28 17:41:43 -0800189 static constexpr uid_t kTestAppId = 19999;
190
191 const gid_t kTestAppUid = multiuser_get_uid(kTestUserId, kTestAppId);
192 const uid_t kTestAppGid = multiuser_get_shared_gid(kTestUserId, kTestAppId);
Calin Juravle7d765462017-09-04 15:57:10 -0700193
194 InstalldNativeService* service_;
195 std::unique_ptr<std::string> volume_uuid_;
196 std::string package_name_;
Calin Juravle0d0a4922018-01-23 19:54:11 -0800197 std::string apk_path_;
Calin Juravle7d765462017-09-04 15:57:10 -0700198 std::string app_apk_dir_;
199 std::string app_private_dir_ce_;
200 std::string app_private_dir_de_;
201 std::string se_info_;
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000202 std::string app_oat_dir_;
Calin Juravle7d765462017-09-04 15:57:10 -0700203
204 int64_t ce_data_inode_;
205
206 std::string secondary_dex_ce_;
207 std::string secondary_dex_ce_link_;
208 std::string secondary_dex_de_;
209
210 virtual void SetUp() {
211 setenv("ANDROID_LOG_TAGS", "*:v", 1);
212 android::base::InitLogging(nullptr);
Calin Juravle29591732017-11-20 17:46:19 -0800213 // Initialize the globals holding the file system main paths (/data/, /system/ etc..).
214 // This is needed in order to compute the application and profile paths.
215 ASSERT_TRUE(init_globals_from_data_and_root());
216 // Initialize selinux log callbacks.
217 // This ensures that selinux is up and running and re-directs the selinux messages
218 // to logcat (in order to make it easier to investigate test results).
219 ASSERT_TRUE(init_selinux());
Calin Juravle7d765462017-09-04 15:57:10 -0700220 service_ = new InstalldNativeService();
221
222 volume_uuid_ = nullptr;
223 package_name_ = "com.installd.test.dexopt";
224 se_info_ = "default";
Calin Juravle7d765462017-09-04 15:57:10 -0700225 app_apk_dir_ = android_app_dir + package_name_;
226
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700227 ASSERT_TRUE(create_mock_app());
Calin Juravle7d765462017-09-04 15:57:10 -0700228 }
229
230 virtual void TearDown() {
231 if (!kDebug) {
232 service_->destroyAppData(
233 volume_uuid_, package_name_, kTestUserId, kAppDataFlags, ce_data_inode_);
234 run_cmd("rm -rf " + app_apk_dir_);
235 run_cmd("rm -rf " + app_private_dir_ce_);
236 run_cmd("rm -rf " + app_private_dir_de_);
237 }
238 delete service_;
239 }
240
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700241 ::testing::AssertionResult create_mock_app() {
Calin Juravle7d765462017-09-04 15:57:10 -0700242 // Create the oat dir.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000243 app_oat_dir_ = app_apk_dir_ + "/oat";
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800244 // For debug mode, the directory might already exist. Avoid erroring out.
245 if (mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755) != 0 && !kDebug) {
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700246 return ::testing::AssertionFailure() << "Could not create app dir " << app_apk_dir_
247 << " : " << strerror(errno);
248 }
249 binder::Status status = service_->createOatDir(app_oat_dir_, kRuntimeIsa);
250 if (!status.isOk()) {
251 return ::testing::AssertionFailure() << "Could not create oat dir: "
252 << status.toString8().c_str();
253 }
Calin Juravle7d765462017-09-04 15:57:10 -0700254
255 // Copy the primary apk.
Calin Juravle0d0a4922018-01-23 19:54:11 -0800256 apk_path_ = app_apk_dir_ + "/base.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700257 std::string error_msg;
258 if (!WriteBase64ToFile(kDexFile, apk_path_, kSystemUid, kSystemGid, 0644, &error_msg)) {
259 return ::testing::AssertionFailure() << "Could not write base64 file to " << apk_path_
260 << " : " << error_msg;
261 }
Calin Juravle7d765462017-09-04 15:57:10 -0700262
263 // Create the app user data.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700264 status = service_->createAppData(
265 volume_uuid_,
266 package_name_,
267 kTestUserId,
268 kAppDataFlags,
269 kTestAppUid,
270 se_info_,
271 kOSdkVersion,
272 &ce_data_inode_);
273 if (!status.isOk()) {
274 return ::testing::AssertionFailure() << "Could not create app data: "
275 << status.toString8().c_str();
276 }
Calin Juravle7d765462017-09-04 15:57:10 -0700277
278 // Create a secondary dex file on CE storage
279 const char* volume_uuid_cstr = volume_uuid_ == nullptr ? nullptr : volume_uuid_->c_str();
280 app_private_dir_ce_ = create_data_user_ce_package_path(
281 volume_uuid_cstr, kTestUserId, package_name_.c_str());
282 secondary_dex_ce_ = app_private_dir_ce_ + "/secondary_ce.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700283 if (!WriteBase64ToFile(kDexFile,
284 secondary_dex_ce_,
285 kTestAppUid,
286 kTestAppGid,
287 0600,
288 &error_msg)) {
289 return ::testing::AssertionFailure() << "Could not write base64 file to "
290 << secondary_dex_ce_ << " : " << error_msg;
291 }
Calin Juravle7d765462017-09-04 15:57:10 -0700292 std::string app_private_dir_ce_link = create_data_user_ce_package_path_as_user_link(
293 volume_uuid_cstr, kTestUserId, package_name_.c_str());
294 secondary_dex_ce_link_ = app_private_dir_ce_link + "/secondary_ce.jar";
295
296 // Create a secondary dex file on DE storage.
297 app_private_dir_de_ = create_data_user_de_package_path(
298 volume_uuid_cstr, kTestUserId, package_name_.c_str());
299 secondary_dex_de_ = app_private_dir_de_ + "/secondary_de.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700300 if (!WriteBase64ToFile(kDexFile,
301 secondary_dex_de_,
302 kTestAppUid,
303 kTestAppGid,
304 0600,
305 &error_msg)) {
306 return ::testing::AssertionFailure() << "Could not write base64 file to "
307 << secondary_dex_de_ << " : " << error_msg;
308 }
Calin Juravle7d765462017-09-04 15:57:10 -0700309
310 // Fix app data uid.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700311 status = service_->fixupAppData(volume_uuid_, kTestUserId);
312 if (!status.isOk()) {
313 return ::testing::AssertionFailure() << "Could not fixup app data: "
314 << status.toString8().c_str();
315 }
316
317 return ::testing::AssertionSuccess();
Calin Juravle7d765462017-09-04 15:57:10 -0700318 }
319
320
Calin Juravle29591732017-11-20 17:46:19 -0800321 std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) {
Calin Juravle7d765462017-09-04 15:57:10 -0700322 std::string::size_type end = path.rfind('.');
323 std::string::size_type start = path.rfind('/', end);
324 return path.substr(0, start) + "/oat/" + kRuntimeIsa + "/" +
325 path.substr(start + 1, end - start) + type;
326 }
327
Calin Juravle29591732017-11-20 17:46:19 -0800328 void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
Calin Juravle7d765462017-09-04 15:57:10 -0700329 bool should_binder_call_succeed, bool should_dex_be_compiled = true,
David Brazdil4f6027a2019-03-19 11:44:21 +0000330 /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1,
331 const char* class_loader_context = nullptr) {
Calin Juravled2affb82017-11-28 17:41:43 -0800332 if (uid == -1) {
333 uid = kTestAppUid;
334 }
David Brazdil4f6027a2019-03-19 11:44:21 +0000335 if (class_loader_context == nullptr) {
336 class_loader_context = "&";
337 }
Calin Juravle7d765462017-09-04 15:57:10 -0700338 std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
339 int32_t dexopt_needed = 0; // does not matter;
340 std::unique_ptr<std::string> out_path = nullptr; // does not matter
341 int32_t dex_flags = DEXOPT_SECONDARY_DEX | dex_storage_flag;
342 std::string compiler_filter = "speed-profile";
David Brazdil4f6027a2019-03-19 11:44:21 +0000343 std::unique_ptr<std::string> class_loader_context_ptr(
344 new std::string(class_loader_context));
Calin Juravle7d765462017-09-04 15:57:10 -0700345 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
346 bool downgrade = false;
David Brazdil570d3982018-01-16 20:15:43 +0000347 int32_t target_sdk_version = 0; // default
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000348 std::unique_ptr<std::string> profile_name_ptr = nullptr;
Calin Juravle62c5a372018-02-01 17:03:23 +0000349 std::unique_ptr<std::string> dm_path_ptr = nullptr;
Calin Juravle2efc4022018-02-13 18:31:32 -0800350 std::unique_ptr<std::string> compilation_reason_ptr = nullptr;
Calin Juravle7d765462017-09-04 15:57:10 -0700351
352 binder::Status result = service_->dexopt(path,
353 uid,
354 package_name_ptr,
355 kRuntimeIsa,
356 dexopt_needed,
357 out_path,
358 dex_flags,
359 compiler_filter,
360 volume_uuid_,
361 class_loader_context_ptr,
362 se_info_ptr,
David Brazdil570d3982018-01-16 20:15:43 +0000363 downgrade,
Calin Juravle408cd4a2018-01-20 23:34:18 -0800364 target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000365 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800366 dm_path_ptr,
367 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800368 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700369 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800370 std::string odex = GetSecondaryDexArtifact(path, "odex");
371 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
372 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700373 ASSERT_EQ(expected_access, access(odex.c_str(), R_OK));
374 ASSERT_EQ(expected_access, access(vdex.c_str(), R_OK));
375 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
Andreas Gampe023b2242018-02-28 16:03:25 -0800376 if (binder_result != nullptr) {
377 *binder_result = result;
378 }
Calin Juravle7d765462017-09-04 15:57:10 -0700379 }
380
381 void reconcile_secondary_dex(const std::string& path, int32_t storage_flag,
382 bool should_binder_call_succeed, bool should_dex_exist, bool should_dex_be_deleted,
Calin Juravled2affb82017-11-28 17:41:43 -0800383 int32_t uid = -1, std::string* package_override = nullptr) {
384 if (uid == -1) {
385 uid = kTestAppUid;
386 }
Calin Juravle7d765462017-09-04 15:57:10 -0700387 std::vector<std::string> isas;
388 isas.push_back(kRuntimeIsa);
389 bool out_secondary_dex_exists = false;
390 binder::Status result = service_->reconcileSecondaryDexFile(
391 path,
392 package_override == nullptr ? package_name_ : *package_override,
393 uid,
394 isas,
395 volume_uuid_,
396 storage_flag,
397 &out_secondary_dex_exists);
398
Andreas Gampef6a93b12018-02-27 09:51:15 -0800399 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700400 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
401
402 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800403 std::string odex = GetSecondaryDexArtifact(path, "odex");
404 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
405 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700406 ASSERT_EQ(expected_access, access(odex.c_str(), F_OK));
407 ASSERT_EQ(expected_access, access(vdex.c_str(), F_OK));
408 ASSERT_EQ(-1, access(art.c_str(), R_OK)); // empty profiles do not generate an image.
409 }
Calin Juravle29591732017-11-20 17:46:19 -0800410
411 void CheckFileAccess(const std::string& file, uid_t uid, gid_t gid, mode_t mode) {
412 struct stat st;
413 ASSERT_EQ(0, stat(file.c_str(), &st));
414 ASSERT_EQ(uid, st.st_uid);
415 ASSERT_EQ(gid, st.st_gid);
416 ASSERT_EQ(mode, st.st_mode);
417 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000418
419 void CompilePrimaryDexOk(std::string compiler_filter,
420 int32_t dex_flags,
421 const char* oat_dir,
422 int32_t uid,
423 int32_t dexopt_needed,
Andreas Gampe023b2242018-02-28 16:03:25 -0800424 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000425 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000426 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800427 CompilePrimaryDex(compiler_filter,
428 dex_flags,
429 oat_dir,
430 uid,
431 dexopt_needed,
432 dm_path,
433 downgrade,
434 true,
435 binder_result);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000436 }
437
438 void CompilePrimaryDexFail(std::string compiler_filter,
439 int32_t dex_flags,
440 const char* oat_dir,
441 int32_t uid,
442 int32_t dexopt_needed,
Andreas Gampe023b2242018-02-28 16:03:25 -0800443 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000444 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000445 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800446 CompilePrimaryDex(compiler_filter,
447 dex_flags,
448 oat_dir,
449 uid,
450 dexopt_needed,
451 dm_path,
452 downgrade,
453 false,
454 binder_result);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000455 }
456
457 void CompilePrimaryDex(std::string compiler_filter,
458 int32_t dex_flags,
459 const char* oat_dir,
460 int32_t uid,
461 int32_t dexopt_needed,
Calin Juravle62c5a372018-02-01 17:03:23 +0000462 const char* dm_path,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000463 bool downgrade,
Andreas Gampe023b2242018-02-28 16:03:25 -0800464 bool should_binder_call_succeed,
465 /*out */ binder::Status* binder_result) {
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000466 std::unique_ptr<std::string> package_name_ptr(new std::string(package_name_));
467 std::unique_ptr<std::string> out_path(
468 oat_dir == nullptr ? nullptr : new std::string(oat_dir));
469 std::unique_ptr<std::string> class_loader_context_ptr(new std::string("&"));
470 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
471 int32_t target_sdk_version = 0; // default
472 std::unique_ptr<std::string> profile_name_ptr(new std::string("primary.prof"));
Calin Juravle62c5a372018-02-01 17:03:23 +0000473 std::unique_ptr<std::string> dm_path_ptr = nullptr;
474 if (dm_path != nullptr) {
475 dm_path_ptr.reset(new std::string(dm_path));
476 }
Calin Juravle2efc4022018-02-13 18:31:32 -0800477 std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason"));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000478
479 bool prof_result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800480 ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700481 package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_,
Andreas Gampef448ced2019-01-31 09:22:27 -0800482 /*dex_metadata*/ nullptr, &prof_result));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000483 ASSERT_TRUE(prof_result);
484
485 binder::Status result = service_->dexopt(apk_path_,
486 uid,
487 package_name_ptr,
488 kRuntimeIsa,
489 dexopt_needed,
490 out_path,
491 dex_flags,
492 compiler_filter,
493 volume_uuid_,
494 class_loader_context_ptr,
495 se_info_ptr,
496 downgrade,
497 target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000498 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800499 dm_path_ptr,
500 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800501 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000502
503 if (!should_binder_call_succeed) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800504 if (binder_result != nullptr) {
505 *binder_result = result;
506 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000507 return;
508 }
509 // Check the access to the compiler output.
510 // - speed-profile artifacts are not world-wide readable.
511 // - files are owned by the system uid.
512 std::string odex = GetPrimaryDexArtifact(oat_dir, apk_path_, "odex");
513 std::string vdex = GetPrimaryDexArtifact(oat_dir, apk_path_, "vdex");
514 std::string art = GetPrimaryDexArtifact(oat_dir, apk_path_, "art");
515
Calin Juravle5bd1c722018-02-01 17:23:54 +0000516 bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0;
517 mode_t mode = S_IFREG | (is_public ? 0644 : 0640);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000518 CheckFileAccess(odex, kSystemUid, uid, mode);
519 CheckFileAccess(vdex, kSystemUid, uid, mode);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000520
Calin Juravle5bd1c722018-02-01 17:23:54 +0000521 if (compiler_filter == "speed-profile") {
522 CheckFileAccess(art, kSystemUid, uid, mode);
523 }
Andreas Gampe023b2242018-02-28 16:03:25 -0800524 if (binder_result != nullptr) {
525 *binder_result = result;
526 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000527 }
528
529 std::string GetPrimaryDexArtifact(const char* oat_dir,
530 const std::string& dex_path,
531 const std::string& type) {
532 if (oat_dir == nullptr) {
533 std::string path = dex_path;
534 for (auto it = path.begin() + 1; it < path.end(); ++it) {
535 if (*it == '/') {
536 *it = '@';
537 }
538 }
539 return android_data_dir + DALVIK_CACHE + '/' + kRuntimeIsa + "/" + path
540 + "@classes.dex";
541 } else {
542 std::string::size_type name_end = dex_path.rfind('.');
543 std::string::size_type name_start = dex_path.rfind('/');
544 return std::string(oat_dir) + "/" + kRuntimeIsa + "/" +
545 dex_path.substr(name_start + 1, name_end - name_start) + type;
546 }
547 }
Calin Juravle7d765462017-09-04 15:57:10 -0700548};
549
550
551TEST_F(DexoptTest, DexoptSecondaryCe) {
552 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800553 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700554 /*binder_ok*/ true, /*compile_ok*/ true);
555}
556
557TEST_F(DexoptTest, DexoptSecondaryCeLink) {
558 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800559 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700560 /*binder_ok*/ true, /*compile_ok*/ true);
561}
562
David Brazdil4f6027a2019-03-19 11:44:21 +0000563TEST_F(DexoptTest, DexoptSecondaryCeWithContext) {
564 LOG(INFO) << "DexoptSecondaryCeWithContext";
565 std::string class_loader_context = "PCL[" + secondary_dex_ce_ + "]";
566 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
567 /*binder_ok*/ true, /*compile_ok*/ true, nullptr, -1, class_loader_context.c_str());
568}
569
Calin Juravle7d765462017-09-04 15:57:10 -0700570TEST_F(DexoptTest, DexoptSecondaryDe) {
571 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800572 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700573 /*binder_ok*/ true, /*compile_ok*/ true);
574}
575
David Brazdil4f6027a2019-03-19 11:44:21 +0000576TEST_F(DexoptTest, DexoptSecondaryDeWithContext) {
577 LOG(INFO) << "DexoptSecondaryDeWithContext";
578 std::string class_loader_context = "PCL[" + secondary_dex_de_ + "]";
579 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
580 /*binder_ok*/ true, /*compile_ok*/ true, nullptr, -1, class_loader_context.c_str());
581}
582
Calin Juravle7d765462017-09-04 15:57:10 -0700583TEST_F(DexoptTest, DexoptSecondaryDoesNotExist) {
584 LOG(INFO) << "DexoptSecondaryDoesNotExist";
585 // If the file validates but does not exist we do not treat it as an error.
Andreas Gampe023b2242018-02-28 16:03:25 -0800586 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800587 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800588 /*binder_ok*/ true, /*compile_ok*/ false, &status);
589 EXPECT_STREQ(status.toString8().c_str(), "No error");
Calin Juravle7d765462017-09-04 15:57:10 -0700590}
591
592TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
593 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800594 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800595 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800596 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800597 EXPECT_STREQ(status.toString8().c_str(),
598 "Status(-8): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700599}
600
601TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
602 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800603 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800604 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800605 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800606 EXPECT_STREQ(status.toString8().c_str(),
607 "Status(-8): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700608}
609
610TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
611 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800612 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800613 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800614 /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800615 EXPECT_STREQ(status.toString8().c_str(), "Status(-8): '-1: Dexoptanalyzer open zip failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700616}
617
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000618TEST_F(DexoptTest, DexoptPrimaryPublic) {
619 LOG(INFO) << "DexoptPrimaryPublic";
620 CompilePrimaryDexOk("verify",
621 DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC,
622 app_oat_dir_.c_str(),
623 kTestAppGid,
624 DEX2OAT_FROM_SCRATCH);
625}
626
Calin Juravle5bd1c722018-02-01 17:23:54 +0000627TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) {
628 LOG(INFO) << "DexoptPrimaryFailedInvalidFilter";
Andreas Gampe023b2242018-02-28 16:03:25 -0800629 binder::Status status;
Calin Juravle5bd1c722018-02-01 17:23:54 +0000630 CompilePrimaryDexFail("awesome-filter",
631 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC,
632 app_oat_dir_.c_str(),
633 kTestAppGid,
Andreas Gampe023b2242018-02-28 16:03:25 -0800634 DEX2OAT_FROM_SCRATCH,
635 &status);
636 EXPECT_STREQ(status.toString8().c_str(),
637 "Status(-8): \'256: Dex2oat invocation for "
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800638 "/data/app/com.installd.test.dexopt/base.jar failed: unspecified dex2oat error'");
Calin Juravle5bd1c722018-02-01 17:23:54 +0000639}
640
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000641TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) {
642 LOG(INFO) << "DexoptPrimaryProfileNonPublic";
643 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700644 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000645 app_oat_dir_.c_str(),
646 kTestAppGid,
647 DEX2OAT_FROM_SCRATCH);
648}
649
650TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
651 LOG(INFO) << "DexoptPrimaryProfilePublic";
Calin Juravle5bd1c722018-02-01 17:23:54 +0000652 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700653 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC |
654 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000655 app_oat_dir_.c_str(),
656 kTestAppGid,
657 DEX2OAT_FROM_SCRATCH);
658}
659
660TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
661 LOG(INFO) << "DexoptPrimaryBackgroundOk";
662 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700663 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED |
664 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000665 app_oat_dir_.c_str(),
666 kTestAppGid,
667 DEX2OAT_FROM_SCRATCH);
668}
669
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800670TEST_F(DexoptTest, ResolveStartupConstStrings) {
671 LOG(INFO) << "DexoptDex2oatResolveStartupStrings";
672 const std::string property = "persist.device_config.runtime.dex2oat_resolve_startup_strings";
673 const std::string previous_value = android::base::GetProperty(property, "");
674 auto restore_property = android::base::make_scope_guard([=]() {
675 android::base::SetProperty(property, previous_value);
676 });
677 std::string odex = GetPrimaryDexArtifact(app_oat_dir_.c_str(), apk_path_, "odex");
678 // Disable the property to start.
679 bool found_disable = false;
680 ASSERT_TRUE(android::base::SetProperty(property, "false")) << property;
681 CompilePrimaryDexOk("speed-profile",
682 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED |
683 DEXOPT_GENERATE_APP_IMAGE,
684 app_oat_dir_.c_str(),
685 kTestAppGid,
686 DEX2OAT_FROM_SCRATCH);
687 run_cmd_and_process_output(
688 "oatdump --header-only --oat-file=" + odex,
689 [&](const std::string& line) {
690 if (line.find("--resolve-startup-const-strings=false") != std::string::npos) {
691 found_disable = true;
692 }
693 });
694 EXPECT_TRUE(found_disable);
695 // Enable the property and inspect that .art artifact is larger.
696 bool found_enable = false;
697 ASSERT_TRUE(android::base::SetProperty(property, "true")) << property;
698 CompilePrimaryDexOk("speed-profile",
699 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED |
700 DEXOPT_GENERATE_APP_IMAGE,
701 app_oat_dir_.c_str(),
702 kTestAppGid,
703 DEX2OAT_FROM_SCRATCH);
704 run_cmd_and_process_output(
705 "oatdump --header-only --oat-file=" + odex,
706 [&](const std::string& line) {
707 if (line.find("--resolve-startup-const-strings=true") != std::string::npos) {
708 found_enable = true;
709 }
710 });
711 EXPECT_TRUE(found_enable);
712}
713
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000714class PrimaryDexReCompilationTest : public DexoptTest {
715 public:
716 virtual void SetUp() {
717 DexoptTest::SetUp();
718 CompilePrimaryDexOk("verify",
719 DEXOPT_BOOTCOMPLETE | DEXOPT_PUBLIC,
720 app_oat_dir_.c_str(),
721 kTestAppGid,
722 DEX2OAT_FROM_SCRATCH);
723 std::string odex = GetSecondaryDexArtifact(apk_path_, "odex");
724 std::string vdex = GetSecondaryDexArtifact(apk_path_, "vdex");
725
726 first_compilation_odex_fd_.reset(open(odex.c_str(), O_RDONLY));
727 first_compilation_vdex_fd_.reset(open(vdex.c_str(), O_RDONLY));
728 }
729
730 virtual void TearDown() {
731 first_compilation_odex_fd_.reset(-1);
732 first_compilation_vdex_fd_.reset(-1);
733 DexoptTest::TearDown();
734 }
735
736 protected:
737 unique_fd first_compilation_odex_fd_;
738 unique_fd first_compilation_vdex_fd_;
739};
740
741TEST_F(PrimaryDexReCompilationTest, DexoptPrimaryUpdateInPlaceVdex) {
742 LOG(INFO) << "DexoptPrimaryUpdateInPlaceVdex";
743
744 CompilePrimaryDexOk("verify",
745 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC,
746 app_oat_dir_.c_str(),
747 kTestAppGid,
748 DEX2OAT_FOR_BOOT_IMAGE);
749}
Calin Juravle7d765462017-09-04 15:57:10 -0700750
751class ReconcileTest : public DexoptTest {
752 virtual void SetUp() {
753 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800754 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700755 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800756 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700757 /*binder_ok*/ true, /*compile_ok*/ true);
758 }
759};
760
761TEST_F(ReconcileTest, ReconcileSecondaryCeExists) {
762 LOG(INFO) << "ReconcileSecondaryCeExists";
763 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
764 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
765}
766
767TEST_F(ReconcileTest, ReconcileSecondaryCeLinkExists) {
768 LOG(INFO) << "ReconcileSecondaryCeLinkExists";
769 reconcile_secondary_dex(secondary_dex_ce_link_, FLAG_STORAGE_CE,
770 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
771}
772
773TEST_F(ReconcileTest, ReconcileSecondaryDeExists) {
774 LOG(INFO) << "ReconcileSecondaryDeExists";
775 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
776 /*binder_ok*/ true, /*dex_ok */ true, /*odex_deleted*/ false);
777}
778
779TEST_F(ReconcileTest, ReconcileSecondaryDeDoesNotExist) {
780 LOG(INFO) << "ReconcileSecondaryDeDoesNotExist";
781 run_cmd("rm -rf " + secondary_dex_de_);
782 reconcile_secondary_dex(secondary_dex_de_, FLAG_STORAGE_DE,
783 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ true);
784}
785
786TEST_F(ReconcileTest, ReconcileSecondaryStorageValidationError) {
787 // Validation errors will not clean the odex/vdex/art files but will mark
788 // the file as non existent so that the PM knows it should purge it from its
789 // records.
790 LOG(INFO) << "ReconcileSecondaryStorageValidationError";
791 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_DE,
792 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false);
793}
794
795TEST_F(ReconcileTest, ReconcileSecondaryAppOwnershipValidationError) {
796 LOG(INFO) << "ReconcileSecondaryAppOwnershipValidationError";
797 // Attempt to reconcile the dex files of the test app from a different app.
798 std::string another_app = "another.app";
799 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
800 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid, &another_app);
801}
802
803TEST_F(ReconcileTest, ReconcileSecondaryAcessViaDifferentUidError) {
804 LOG(INFO) << "ReconcileSecondaryAcessViaDifferentUidError";
805 reconcile_secondary_dex(secondary_dex_ce_, FLAG_STORAGE_CE,
806 /*binder_ok*/ true, /*dex_ok */ false, /*odex_deleted*/ false, kSystemUid);
807}
808
Calin Juravle29591732017-11-20 17:46:19 -0800809class ProfileTest : public DexoptTest {
810 protected:
811 std::string cur_profile_;
812 std::string ref_profile_;
813 std::string snap_profile_;
814
Calin Juravle824a64d2018-01-18 20:23:17 -0800815 static constexpr const char* kPrimaryProfile = "primary.prof";
816
Calin Juravle29591732017-11-20 17:46:19 -0800817 virtual void SetUp() {
818 DexoptTest::SetUp();
819 cur_profile_ = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -0800820 kTestUserId, package_name_, kPrimaryProfile, /*is_secondary_dex*/ false);
821 ref_profile_ = create_reference_profile_path(package_name_, kPrimaryProfile,
822 /*is_secondary_dex*/ false);
823 snap_profile_ = create_snapshot_profile_path(package_name_, kPrimaryProfile);
Calin Juravle29591732017-11-20 17:46:19 -0800824 }
825
Calin Juravle824a64d2018-01-18 20:23:17 -0800826 void SetupProfile(const std::string& path, uid_t uid, gid_t gid, mode_t mode,
827 int32_t num_dex) {
828 run_cmd("profman --generate-test-profile-seed=" + std::to_string(num_dex) +
829 " --generate-test-profile-num-dex=" + std::to_string(num_dex) +
830 " --generate-test-profile=" + path);
Calin Juravle29591732017-11-20 17:46:19 -0800831 ::chmod(path.c_str(), mode);
832 ::chown(path.c_str(), uid, gid);
833 }
834
835 void SetupProfiles(bool setup_ref) {
836 SetupProfile(cur_profile_, kTestAppUid, kTestAppGid, 0600, 1);
837 if (setup_ref) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800838 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2);
Calin Juravle29591732017-11-20 17:46:19 -0800839 }
840 }
841
Calin Juravlec3596c32017-12-05 12:29:15 -0800842 void createProfileSnapshot(int32_t appid, const std::string& package_name,
843 bool expected_result) {
Calin Juravle29591732017-11-20 17:46:19 -0800844 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800845 ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
846 appid, package_name, kPrimaryProfile, apk_path_, &result));
Calin Juravle29591732017-11-20 17:46:19 -0800847 ASSERT_EQ(expected_result, result);
848
849 if (!expected_result) {
850 // Do not check the files if we expect to fail.
851 return;
852 }
853
854 // Check that the snapshot was created witht he expected acess flags.
855 CheckFileAccess(snap_profile_, kSystemUid, kSystemGid, 0600 | S_IFREG);
856
857 // The snapshot should be equivalent to the merge of profiles.
858 std::string expected_profile_content = snap_profile_ + ".expected";
859 run_cmd("rm -f " + expected_profile_content);
860 run_cmd("touch " + expected_profile_content);
861 run_cmd("profman --profile-file=" + cur_profile_ +
862 " --profile-file=" + ref_profile_ +
Calin Juravle40b5f462018-02-07 04:11:52 -0800863 " --reference-profile-file=" + expected_profile_content +
864 " --apk=" + apk_path_);
Calin Juravle29591732017-11-20 17:46:19 -0800865
866 ASSERT_TRUE(AreFilesEqual(expected_profile_content, snap_profile_));
867
868 pid_t pid = fork();
869 if (pid == 0) {
870 /* child */
871 TransitionToSystemServer();
872
873 // System server should be able to open the the spanshot.
874 unique_fd fd(open(snap_profile_.c_str(), O_RDONLY));
875 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
876 _exit(0);
877 }
878 /* parent */
879 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
880 }
881
Calin Juravle408cd4a2018-01-20 23:34:18 -0800882 void mergePackageProfiles(const std::string& package_name,
883 const std::string& code_path,
884 bool expected_result) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800885 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800886 ASSERT_BINDER_SUCCESS(service_->mergeProfiles(
887 kTestAppUid, package_name, code_path, &result));
Calin Juravle824a64d2018-01-18 20:23:17 -0800888 ASSERT_EQ(expected_result, result);
889
890 if (!expected_result) {
891 // Do not check the files if we expect to fail.
892 return;
893 }
894
895 // Check that the snapshot was created witht he expected acess flags.
896 CheckFileAccess(ref_profile_, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
897
898 // The snapshot should be equivalent to the merge of profiles.
899 std::string ref_profile_content = ref_profile_ + ".expected";
900 run_cmd("rm -f " + ref_profile_content);
901 run_cmd("touch " + ref_profile_content);
902 run_cmd("profman --profile-file=" + cur_profile_ +
903 " --profile-file=" + ref_profile_ +
904 " --reference-profile-file=" + ref_profile_content);
905
906 ASSERT_TRUE(AreFilesEqual(ref_profile_content, ref_profile_));
907 }
908
Calin Juravlec3b049e2018-01-18 22:32:58 -0800909 // TODO(calin): add dex metadata tests once the ART change is merged.
910 void preparePackageProfile(const std::string& package_name, const std::string& profile_name,
911 bool expected_result) {
912 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800913 ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700914 package_name, kTestUserId, kTestAppId, profile_name, apk_path_,
Andreas Gampef448ced2019-01-31 09:22:27 -0800915 /*dex_metadata*/ nullptr, &result));
Calin Juravlec3b049e2018-01-18 22:32:58 -0800916 ASSERT_EQ(expected_result, result);
917
918 if (!expected_result) {
919 // Do not check the files if we expect to fail.
920 return;
921 }
922
923 std::string code_path_cur_prof = create_current_profile_path(
924 kTestUserId, package_name, profile_name, /*is_secondary_dex*/ false);
925 std::string code_path_ref_profile = create_reference_profile_path(package_name,
926 profile_name, /*is_secondary_dex*/ false);
927
928 // Check that we created the current profile.
929 CheckFileAccess(code_path_cur_prof, kTestAppUid, kTestAppUid, 0600 | S_IFREG);
930
931 // Without dex metadata we don't generate a reference profile.
932 ASSERT_EQ(-1, access(code_path_ref_profile.c_str(), R_OK));
933 }
934
Calin Juravle0d0a4922018-01-23 19:54:11 -0800935 protected:
Calin Juravle29591732017-11-20 17:46:19 -0800936 void TransitionToSystemServer() {
937 ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid));
938 int32_t res = selinux_android_setcontext(
939 kSystemUid, true, se_info_.c_str(), "system_server");
940 ASSERT_EQ(0, res) << "Failed to setcon " << strerror(errno);
941 }
942
943 bool AreFilesEqual(const std::string& file1, const std::string& file2) {
944 std::vector<uint8_t> content1;
945 std::vector<uint8_t> content2;
946
947 if (!ReadAll(file1, &content1)) return false;
948 if (!ReadAll(file2, &content2)) return false;
949 return content1 == content2;
950 }
951
952 bool ReadAll(const std::string& file, std::vector<uint8_t>* content) {
953 unique_fd fd(open(file.c_str(), O_RDONLY));
954 if (fd < 0) {
955 PLOG(ERROR) << "Failed to open " << file;
956 return false;
957 }
958 struct stat st;
959 if (fstat(fd, &st) != 0) {
960 PLOG(ERROR) << "Failed to stat " << file;
961 return false;
962 }
963 content->resize(st.st_size);
964 bool result = ReadFully(fd, content->data(), content->size());
965 if (!result) {
966 PLOG(ERROR) << "Failed to read " << file;
967 }
968 return result;
969 }
970};
971
972TEST_F(ProfileTest, ProfileSnapshotOk) {
973 LOG(INFO) << "ProfileSnapshotOk";
974
975 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800976 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800977}
978
979// The reference profile is created on the fly. We need to be able to
980// snapshot without one.
981TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
982 LOG(INFO) << "ProfileSnapshotOkNoReference";
983
984 SetupProfiles(/*setup_ref*/ false);
Calin Juravlec3596c32017-12-05 12:29:15 -0800985 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800986}
987
988TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
989 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
990
991 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800992 createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false);
Calin Juravle29591732017-11-20 17:46:19 -0800993}
994
995TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
996 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
997
998 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -0800999 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -08001000
Andreas Gampef448ced2019-01-31 09:22:27 -08001001 ASSERT_BINDER_SUCCESS(service_->destroyProfileSnapshot(package_name_, kPrimaryProfile));
Calin Juravle29591732017-11-20 17:46:19 -08001002 struct stat st;
1003 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
1004 ASSERT_EQ(ENOENT, errno);
1005}
1006
Calin Juravle824a64d2018-01-18 20:23:17 -08001007TEST_F(ProfileTest, ProfileMergeOk) {
1008 LOG(INFO) << "ProfileMergeOk";
1009
1010 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -08001011 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -08001012}
1013
1014// The reference profile is created on the fly. We need to be able to
1015// merge without one.
1016TEST_F(ProfileTest, ProfileMergeOkNoReference) {
1017 LOG(INFO) << "ProfileMergeOkNoReference";
1018
1019 SetupProfiles(/*setup_ref*/ false);
Calin Juravle408cd4a2018-01-20 23:34:18 -08001020 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -08001021}
1022
1023TEST_F(ProfileTest, ProfileMergeFailWrongPackage) {
1024 LOG(INFO) << "ProfileMergeFailWrongPackage";
1025
1026 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -08001027 mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false);
Calin Juravle824a64d2018-01-18 20:23:17 -08001028}
1029
Calin Juravled2affb82017-11-28 17:41:43 -08001030TEST_F(ProfileTest, ProfileDirOk) {
1031 LOG(INFO) << "ProfileDirOk";
1032
1033 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
1034 kTestUserId, package_name_);
1035 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
Calin Juravle824a64d2018-01-18 20:23:17 -08001036 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravled2affb82017-11-28 17:41:43 -08001037 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
1038
1039 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -08001040 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
1041}
1042
1043// Verify that the profile directories are fixed up during an upgrade.
1044// (The reference profile directory is prepared lazily).
1045TEST_F(ProfileTest, ProfileDirOkAfterFixup) {
1046 LOG(INFO) << "ProfileDirOkAfterFixup";
1047
1048 std::string cur_profile_dir = create_primary_current_profile_package_dir_path(
1049 kTestUserId, package_name_);
1050 std::string cur_profile_file = create_current_profile_path(kTestUserId, package_name_,
Calin Juravle824a64d2018-01-18 20:23:17 -08001051 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravle6f06eb62017-11-28 18:44:53 -08001052 std::string ref_profile_dir = create_primary_reference_profile_package_dir_path(package_name_);
1053
1054 // Simulate a pre-P setup by changing the owner to kTestAppGid and permissions to 0700.
1055 ASSERT_EQ(0, chown(ref_profile_dir.c_str(), kTestAppGid, kTestAppGid));
1056 ASSERT_EQ(0, chmod(ref_profile_dir.c_str(), 0700));
1057
1058 // Run createAppData again which will offer to fix-up the profile directories.
Andreas Gampef448ced2019-01-31 09:22:27 -08001059 ASSERT_BINDER_SUCCESS(service_->createAppData(
Calin Juravle6f06eb62017-11-28 18:44:53 -08001060 volume_uuid_,
1061 package_name_,
1062 kTestUserId,
1063 kAppDataFlags,
1064 kTestAppUid,
1065 se_info_,
1066 kOSdkVersion,
Andreas Gampef448ced2019-01-31 09:22:27 -08001067 &ce_data_inode_));
Calin Juravle6f06eb62017-11-28 18:44:53 -08001068
1069 // Check the file access.
1070 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -08001071 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
Calin Juravled2affb82017-11-28 17:41:43 -08001072}
1073
Calin Juravlec3b049e2018-01-18 22:32:58 -08001074TEST_F(ProfileTest, ProfilePrepareOk) {
1075 LOG(INFO) << "ProfilePrepareOk";
1076 preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true);
1077}
1078
1079TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) {
1080 LOG(INFO) << "ProfilePrepareFailInvalidPackage";
1081 preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false);
1082}
1083
1084TEST_F(ProfileTest, ProfilePrepareFailProfileChangedUid) {
1085 LOG(INFO) << "ProfilePrepareFailProfileChangedUid";
1086 SetupProfiles(/*setup_ref*/ false);
1087 // Change the uid on the profile to trigger a failure.
1088 ::chown(cur_profile_.c_str(), kTestAppUid + 1, kTestAppGid + 1);
1089 preparePackageProfile(package_name_, "primary.prof", /*expected_result*/ false);
1090}
1091
Calin Juravle0d0a4922018-01-23 19:54:11 -08001092
1093class BootProfileTest : public ProfileTest {
1094 public:
1095 virtual void setup() {
1096 ProfileTest::SetUp();
1097 intial_android_profiles_dir = android_profiles_dir;
1098 }
1099
1100 virtual void TearDown() {
1101 android_profiles_dir = intial_android_profiles_dir;
1102 ProfileTest::TearDown();
1103 }
1104
1105 void UpdateAndroidProfilesDir(const std::string& profile_dir) {
1106 android_profiles_dir = profile_dir;
1107 // We need to create the reference profile directory in the new profile dir.
1108 run_cmd("mkdir -p " + profile_dir + "/ref");
1109 }
1110
1111 void createBootImageProfileSnapshot(const std::string& classpath, bool expected_result) {
1112 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -08001113 ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
1114 -1, "android", "android.prof", classpath, &result));
Calin Juravle0d0a4922018-01-23 19:54:11 -08001115 ASSERT_EQ(expected_result, result);
1116
1117 if (!expected_result) {
1118 // Do not check the files if we expect to fail.
1119 return;
1120 }
1121
1122 // Check that the snapshot was created with he expected access flags.
1123 const std::string boot_profile = create_snapshot_profile_path("android", "android.prof");
1124 CheckFileAccess(boot_profile, kSystemUid, kSystemGid, 0600 | S_IFREG);
1125
1126 pid_t pid = fork();
1127 if (pid == 0) {
1128 /* child */
1129 TransitionToSystemServer();
1130
1131 // System server should be able to open the snapshot.
1132 unique_fd fd(open(boot_profile.c_str(), O_RDONLY));
1133 ASSERT_TRUE(fd > -1) << "Failed to open profile as kSystemUid: " << strerror(errno);
1134 _exit(0);
1135 }
1136 /* parent */
1137 ASSERT_TRUE(WIFEXITED(wait_child(pid)));
1138 }
1139 protected:
1140 std::string intial_android_profiles_dir;
1141};
1142
1143TEST_F(BootProfileTest, BootProfileSnapshotOk) {
1144 LOG(INFO) << "BootProfileSnapshotOk";
1145 char* boot_classpath = getenv("BOOTCLASSPATH");
1146 ASSERT_TRUE(boot_classpath != nullptr);
1147 createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true);
1148}
1149
1150TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) {
1151 LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath";
1152
1153 createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false);
1154}
1155
1156TEST_F(BootProfileTest, BootProfileSnapshotOkNoProfiles) {
1157 LOG(INFO) << "BootProfileSnapshotOkNoProfiles";
1158 char* boot_classpath = getenv("BOOTCLASSPATH");
1159 ASSERT_TRUE(boot_classpath != nullptr);
1160
1161 // The app_apk_dir has no profiles. So we shouldn't be able to merge anything.
1162 // Still, this is not a failure case.
1163 UpdateAndroidProfilesDir(app_apk_dir_);
1164 createBootImageProfileSnapshot(boot_classpath, /*expected_result*/ true);
1165}
1166
1167// Verify that profile collection.
1168TEST_F(BootProfileTest, CollectProfiles) {
1169 LOG(INFO) << "CollectProfiles";
1170
1171 // Create some profile directories mimicking the real profile structure.
1172 run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/ref");
1173 run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/0/");
1174 run_cmd("mkdir -p " + app_private_dir_de_ + "/profiles/cur/1/");
1175 // Create an empty profile.
1176 run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/1/primary.prof");
1177 // Create a random file.
1178 run_cmd("touch " + app_private_dir_de_ + "/profiles/cur/0/non.profile.file");
1179
1180 // Create some non-empty profiles.
1181 std::string current_prof = app_private_dir_de_ + "/profiles/cur/0/primary.prof";
1182 run_cmd("echo 1 > " + current_prof);
1183 std::string ref_prof = app_private_dir_de_ + "/profiles/ref/primary.prof";
1184 run_cmd("echo 1 > " + ref_prof);
1185
1186 UpdateAndroidProfilesDir(app_private_dir_de_ + "/profiles");
1187
1188 std::vector<std::string> profiles;
1189 collect_profiles(&profiles);
1190
1191 // Only two profiles should be in the output.
1192 ASSERT_EQ(2u, profiles.size());
1193 ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), current_prof) != profiles.end());
1194 ASSERT_TRUE(std::find(profiles.begin(), profiles.end(), ref_prof) != profiles.end());
1195}
1196
Calin Juravlef74a7372019-02-28 20:29:41 -08001197TEST_F(DexoptTest, select_execution_binary) {
1198 LOG(INFO) << "DexoptTestselect_execution_binary";
1199
1200 std::string release_str = app_private_dir_ce_ + "/release";
1201 std::string debug_str = app_private_dir_ce_ + "/debug";
1202
1203 // Setup the binaries. Note that we only need executable files to actually
1204 // test the execution binary selection
1205 run_cmd("touch " + release_str);
1206 run_cmd("touch " + debug_str);
1207 run_cmd("chmod 777 " + release_str);
1208 run_cmd("chmod 777 " + debug_str);
1209
1210 const char* release = release_str.c_str();
1211 const char* debug = debug_str.c_str();
1212
1213 ASSERT_STREQ(release, select_execution_binary(
1214 release,
1215 debug,
1216 /*background_job_compile=*/ false,
1217 /*is_debug_runtime=*/ false,
1218 /*is_release=*/ false,
1219 /*is_debuggable_build=*/ false));
1220
1221 ASSERT_STREQ(release, select_execution_binary(
1222 release,
1223 debug,
1224 /*background_job_compile=*/ true,
1225 /*is_debug_runtime=*/ false,
1226 /*is_release=*/ true,
1227 /*is_debuggable_build=*/ true));
1228
1229 ASSERT_STREQ(debug, select_execution_binary(
1230 release,
1231 debug,
1232 /*background_job_compile=*/ false,
1233 /*is_debug_runtime=*/ true,
1234 /*is_release=*/ false,
1235 /*is_debuggable_build=*/ false));
1236
1237 ASSERT_STREQ(debug, select_execution_binary(
1238 release,
1239 debug,
1240 /*background_job_compile=*/ true,
1241 /*is_debug_runtime=*/ false,
1242 /*is_release=*/ false,
1243 /*is_debuggable_build=*/ true));
1244
1245
1246 // Select the release when the debug file is not there.
1247 ASSERT_STREQ(release, select_execution_binary(
1248 release,
1249 "does_not_exist",
1250 /*background_job_compile=*/ false,
1251 /*is_debug_runtime=*/ true,
1252 /*is_release=*/ false,
1253 /*is_debuggable_build=*/ false));
1254}
1255
Calin Juravle7d765462017-09-04 15:57:10 -07001256} // namespace installd
1257} // namespace android