blob: 13fd0677e59002e00a07486e89ea499ace5e0260 [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"
liulvping61907742018-08-21 09:36:52 +080044#include "ziparchive/zip_writer.h"
Calin Juravle7d765462017-09-04 15:57:10 -070045
Calin Juravle29591732017-11-20 17:46:19 -080046using android::base::ReadFully;
47using android::base::unique_fd;
48
Calin Juravle7d765462017-09-04 15:57:10 -070049namespace android {
50namespace installd {
51
52// TODO(calin): try to dedup this code.
53#if defined(__arm__)
54static const std::string kRuntimeIsa = "arm";
55#elif defined(__aarch64__)
56static const std::string kRuntimeIsa = "arm64";
57#elif defined(__mips__) && !defined(__LP64__)
58static const std::string kRuntimeIsa = "mips";
59#elif defined(__mips__) && defined(__LP64__)
60static const std::string kRuntimeIsa = "mips64";
61#elif defined(__i386__)
62static const std::string kRuntimeIsa = "x86";
63#elif defined(__x86_64__)
64static const std::string kRuntimeIsa = "x86_64";
65#else
66static const std::string kRuntimeIsa = "none";
67#endif
68
69int get_property(const char *key, char *value, const char *default_value) {
70 return property_get(key, value, default_value);
71}
72
73bool 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
78bool 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
83bool 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
87static void run_cmd(const std::string& cmd) {
88 system(cmd.c_str());
89}
90
Mathieu Chartierd41622c2019-01-31 12:59:39 -080091template <typename Visitor>
92static 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 Gampe2ef09a72018-08-29 14:50:41 -0700108static 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 Juravle7d765462017-09-04 15:57:10 -0700118}
119
Calin Juravle29591732017-11-20 17:46:19 -0800120static 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
141static 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 Juravle7d765462017-09-04 15:57:10 -0700156// Base64 encoding of a simple dex files with 2 methods.
157static 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 Gampe1b8700f2018-11-01 14:47:00 -0700167class DexoptTestEnvTest : public testing::Test {
168};
169
170TEST_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 Gampe1d1e7c92018-11-05 09:46:28 -0800179 ASSERT_TRUE(1 == security_getenforce() || kIsX86 || true /* b/119032200 */);
Andreas Gampe1b8700f2018-11-01 14:47:00 -0700180}
Calin Juravle7d765462017-09-04 15:57:10 -0700181
182class DexoptTest : public testing::Test {
183protected:
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 Juravle7d765462017-09-04 15:57:10 -0700189 static constexpr int32_t kTestUserId = 0;
Calin Juravled2affb82017-11-28 17:41:43 -0800190 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 Juravle7d765462017-09-04 15:57:10 -0700194
195 InstalldNativeService* service_;
196 std::unique_ptr<std::string> volume_uuid_;
197 std::string package_name_;
Calin Juravle0d0a4922018-01-23 19:54:11 -0800198 std::string apk_path_;
liulvping61907742018-08-21 09:36:52 +0800199 std::string empty_dm_file_;
Calin Juravle7d765462017-09-04 15:57:10 -0700200 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000204 std::string app_oat_dir_;
Calin Juravle7d765462017-09-04 15:57:10 -0700205
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 Juravle29591732017-11-20 17:46:19 -0800215 // 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 Juravle7d765462017-09-04 15:57:10 -0700222 service_ = new InstalldNativeService();
223
224 volume_uuid_ = nullptr;
225 package_name_ = "com.installd.test.dexopt";
226 se_info_ = "default";
Calin Juravle7d765462017-09-04 15:57:10 -0700227 app_apk_dir_ = android_app_dir + package_name_;
228
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700229 ASSERT_TRUE(create_mock_app());
Calin Juravle7d765462017-09-04 15:57:10 -0700230 }
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 Gampe2ef09a72018-08-29 14:50:41 -0700243 ::testing::AssertionResult create_mock_app() {
Calin Juravle7d765462017-09-04 15:57:10 -0700244 // Create the oat dir.
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000245 app_oat_dir_ = app_apk_dir_ + "/oat";
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800246 // For debug mode, the directory might already exist. Avoid erroring out.
247 if (mkdir(app_apk_dir_, kSystemUid, kSystemGid, 0755) != 0 && !kDebug) {
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700248 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 Juravle7d765462017-09-04 15:57:10 -0700256
257 // Copy the primary apk.
Calin Juravle0d0a4922018-01-23 19:54:11 -0800258 apk_path_ = app_apk_dir_ + "/base.jar";
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700259 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 Juravle7d765462017-09-04 15:57:10 -0700264
liulvping61907742018-08-21 09:36:52 +0800265 // 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 Juravle7d765462017-09-04 15:57:10 -0700285 // Create the app user data.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700286 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 Juravle7d765462017-09-04 15:57:10 -0700299
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 Gampe2ef09a72018-08-29 14:50:41 -0700305 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 Juravle7d765462017-09-04 15:57:10 -0700314 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 Gampe2ef09a72018-08-29 14:50:41 -0700322 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 Juravle7d765462017-09-04 15:57:10 -0700331
332 // Fix app data uid.
Andreas Gampe2ef09a72018-08-29 14:50:41 -0700333 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 Juravle7d765462017-09-04 15:57:10 -0700340 }
341
342
Calin Juravle29591732017-11-20 17:46:19 -0800343 std::string GetSecondaryDexArtifact(const std::string& path, const std::string& type) {
Calin Juravle7d765462017-09-04 15:57:10 -0700344 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 Juravle29591732017-11-20 17:46:19 -0800350 void CompileSecondaryDex(const std::string& path, int32_t dex_storage_flag,
Calin Juravle7d765462017-09-04 15:57:10 -0700351 bool should_binder_call_succeed, bool should_dex_be_compiled = true,
David Brazdil4f6027a2019-03-19 11:44:21 +0000352 /*out */ binder::Status* binder_result = nullptr, int32_t uid = -1,
353 const char* class_loader_context = nullptr) {
Calin Juravled2affb82017-11-28 17:41:43 -0800354 if (uid == -1) {
355 uid = kTestAppUid;
356 }
David Brazdil4f6027a2019-03-19 11:44:21 +0000357 if (class_loader_context == nullptr) {
358 class_loader_context = "&";
359 }
Calin Juravle7d765462017-09-04 15:57:10 -0700360 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 Brazdil4f6027a2019-03-19 11:44:21 +0000365 std::unique_ptr<std::string> class_loader_context_ptr(
366 new std::string(class_loader_context));
Calin Juravle7d765462017-09-04 15:57:10 -0700367 std::unique_ptr<std::string> se_info_ptr(new std::string(se_info_));
368 bool downgrade = false;
David Brazdil570d3982018-01-16 20:15:43 +0000369 int32_t target_sdk_version = 0; // default
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000370 std::unique_ptr<std::string> profile_name_ptr = nullptr;
Calin Juravle62c5a372018-02-01 17:03:23 +0000371 std::unique_ptr<std::string> dm_path_ptr = nullptr;
Calin Juravle2efc4022018-02-13 18:31:32 -0800372 std::unique_ptr<std::string> compilation_reason_ptr = nullptr;
Calin Juravle7d765462017-09-04 15:57:10 -0700373
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 Brazdil570d3982018-01-16 20:15:43 +0000385 downgrade,
Calin Juravle408cd4a2018-01-20 23:34:18 -0800386 target_sdk_version,
Calin Juravle62c5a372018-02-01 17:03:23 +0000387 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800388 dm_path_ptr,
389 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800390 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700391 int expected_access = should_dex_be_compiled ? 0 : -1;
Calin Juravle29591732017-11-20 17:46:19 -0800392 std::string odex = GetSecondaryDexArtifact(path, "odex");
393 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
394 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700395 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 Gampe023b2242018-02-28 16:03:25 -0800398 if (binder_result != nullptr) {
399 *binder_result = result;
400 }
Calin Juravle7d765462017-09-04 15:57:10 -0700401 }
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 Juravled2affb82017-11-28 17:41:43 -0800405 int32_t uid = -1, std::string* package_override = nullptr) {
406 if (uid == -1) {
407 uid = kTestAppUid;
408 }
Calin Juravle7d765462017-09-04 15:57:10 -0700409 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 Gampef6a93b12018-02-27 09:51:15 -0800421 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravle7d765462017-09-04 15:57:10 -0700422 ASSERT_EQ(should_dex_exist, out_secondary_dex_exists);
423
424 int expected_access = should_dex_be_deleted ? -1 : 0;
Calin Juravle29591732017-11-20 17:46:19 -0800425 std::string odex = GetSecondaryDexArtifact(path, "odex");
426 std::string vdex = GetSecondaryDexArtifact(path, "vdex");
427 std::string art = GetSecondaryDexArtifact(path, "art");
Calin Juravle7d765462017-09-04 15:57:10 -0700428 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 Juravle29591732017-11-20 17:46:19 -0800432
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 Juravlec4f6a0b2018-02-01 01:27:24 +0000440
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 Gampe023b2242018-02-28 16:03:25 -0800446 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000447 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000448 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800449 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000458 }
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 Gampe023b2242018-02-28 16:03:25 -0800465 binder::Status* binder_result = nullptr,
Calin Juravle62c5a372018-02-01 17:03:23 +0000466 const char* dm_path = nullptr,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000467 bool downgrade = false) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800468 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000477 }
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 Juravle62c5a372018-02-01 17:03:23 +0000484 const char* dm_path,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000485 bool downgrade,
Andreas Gampe023b2242018-02-28 16:03:25 -0800486 bool should_binder_call_succeed,
487 /*out */ binder::Status* binder_result) {
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000488 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 Juravle62c5a372018-02-01 17:03:23 +0000495 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 Juravle2efc4022018-02-13 18:31:32 -0800499 std::unique_ptr<std::string> compilation_reason_ptr(new std::string("test-reason"));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000500
501 bool prof_result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800502 ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700503 package_name_, kTestUserId, kTestAppId, *profile_name_ptr, apk_path_,
liulvping61907742018-08-21 09:36:52 +0800504 dm_path_ptr, &prof_result));
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000505 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 Juravle62c5a372018-02-01 17:03:23 +0000520 profile_name_ptr,
Calin Juravle2efc4022018-02-13 18:31:32 -0800521 dm_path_ptr,
522 compilation_reason_ptr);
Andreas Gampef6a93b12018-02-27 09:51:15 -0800523 ASSERT_EQ(should_binder_call_succeed, result.isOk()) << result.toString8().c_str();
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000524
525 if (!should_binder_call_succeed) {
Andreas Gampe023b2242018-02-28 16:03:25 -0800526 if (binder_result != nullptr) {
527 *binder_result = result;
528 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000529 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 Juravle5bd1c722018-02-01 17:23:54 +0000538 bool is_public = (dex_flags & DEXOPT_PUBLIC) != 0;
539 mode_t mode = S_IFREG | (is_public ? 0644 : 0640);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000540 CheckFileAccess(odex, kSystemUid, uid, mode);
541 CheckFileAccess(vdex, kSystemUid, uid, mode);
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000542
Calin Juravle5bd1c722018-02-01 17:23:54 +0000543 if (compiler_filter == "speed-profile") {
544 CheckFileAccess(art, kSystemUid, uid, mode);
545 }
Andreas Gampe023b2242018-02-28 16:03:25 -0800546 if (binder_result != nullptr) {
547 *binder_result = result;
548 }
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000549 }
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 Juravle7d765462017-09-04 15:57:10 -0700570};
571
572
573TEST_F(DexoptTest, DexoptSecondaryCe) {
574 LOG(INFO) << "DexoptSecondaryCe";
Calin Juravle29591732017-11-20 17:46:19 -0800575 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700576 /*binder_ok*/ true, /*compile_ok*/ true);
577}
578
579TEST_F(DexoptTest, DexoptSecondaryCeLink) {
580 LOG(INFO) << "DexoptSecondaryCeLink";
Calin Juravle29591732017-11-20 17:46:19 -0800581 CompileSecondaryDex(secondary_dex_ce_link_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700582 /*binder_ok*/ true, /*compile_ok*/ true);
583}
584
David Brazdil4f6027a2019-03-19 11:44:21 +0000585TEST_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 Juravle7d765462017-09-04 15:57:10 -0700592TEST_F(DexoptTest, DexoptSecondaryDe) {
593 LOG(INFO) << "DexoptSecondaryDe";
Calin Juravle29591732017-11-20 17:46:19 -0800594 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700595 /*binder_ok*/ true, /*compile_ok*/ true);
596}
597
David Brazdil4f6027a2019-03-19 11:44:21 +0000598TEST_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 Juravle7d765462017-09-04 15:57:10 -0700605TEST_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 Gampe023b2242018-02-28 16:03:25 -0800608 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800609 CompileSecondaryDex(secondary_dex_ce_ + "not.there", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800610 /*binder_ok*/ true, /*compile_ok*/ false, &status);
611 EXPECT_STREQ(status.toString8().c_str(), "No error");
Calin Juravle7d765462017-09-04 15:57:10 -0700612}
613
614TEST_F(DexoptTest, DexoptSecondaryStorageValidationError) {
615 LOG(INFO) << "DexoptSecondaryStorageValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800616 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800617 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_DE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800618 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800619 EXPECT_STREQ(status.toString8().c_str(),
Martijn Coenen290de462019-03-21 13:14:59 +0100620 "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700621}
622
623TEST_F(DexoptTest, DexoptSecondaryAppOwnershipValidationError) {
624 LOG(INFO) << "DexoptSecondaryAppOwnershipValidationError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800625 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800626 CompileSecondaryDex("/data/data/random.app/secondary.jar", DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800627 /*binder_ok*/ false, /*compile_ok*/ false, &status);
Andreas Gampe3008bbe2018-02-28 20:24:48 -0800628 EXPECT_STREQ(status.toString8().c_str(),
Martijn Coenen290de462019-03-21 13:14:59 +0100629 "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer path validation failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700630}
631
632TEST_F(DexoptTest, DexoptSecondaryAcessViaDifferentUidError) {
633 LOG(INFO) << "DexoptSecondaryAcessViaDifferentUidError";
Andreas Gampe023b2242018-02-28 16:03:25 -0800634 binder::Status status;
Calin Juravle29591732017-11-20 17:46:19 -0800635 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Andreas Gampe023b2242018-02-28 16:03:25 -0800636 /*binder_ok*/ false, /*compile_ok*/ false, &status, kSystemUid);
Martijn Coenen290de462019-03-21 13:14:59 +0100637 EXPECT_STREQ(status.toString8().c_str(),
638 "Status(-8, EX_SERVICE_SPECIFIC): '-1: Dexoptanalyzer open zip failed'");
Calin Juravle7d765462017-09-04 15:57:10 -0700639}
640
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000641TEST_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 Juravle5bd1c722018-02-01 17:23:54 +0000650TEST_F(DexoptTest, DexoptPrimaryFailedInvalidFilter) {
651 LOG(INFO) << "DexoptPrimaryFailedInvalidFilter";
Andreas Gampe023b2242018-02-28 16:03:25 -0800652 binder::Status status;
Calin Juravle5bd1c722018-02-01 17:23:54 +0000653 CompilePrimaryDexFail("awesome-filter",
654 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PUBLIC,
655 app_oat_dir_.c_str(),
656 kTestAppGid,
Andreas Gampe023b2242018-02-28 16:03:25 -0800657 DEX2OAT_FROM_SCRATCH,
658 &status);
659 EXPECT_STREQ(status.toString8().c_str(),
Martijn Coenen290de462019-03-21 13:14:59 +0100660 "Status(-8, EX_SERVICE_SPECIFIC): \'256: Dex2oat invocation for "
Andreas Gampefa2dadd2018-02-28 19:52:47 -0800661 "/data/app/com.installd.test.dexopt/base.jar failed: unspecified dex2oat error'");
Calin Juravle5bd1c722018-02-01 17:23:54 +0000662}
663
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000664TEST_F(DexoptTest, DexoptPrimaryProfileNonPublic) {
665 LOG(INFO) << "DexoptPrimaryProfileNonPublic";
666 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700667 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000668 app_oat_dir_.c_str(),
669 kTestAppGid,
liulvping61907742018-08-21 09:36:52 +0800670 DEX2OAT_FROM_SCRATCH,
671 /*binder_result=*/nullptr,
672 empty_dm_file_.c_str());
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000673}
674
675TEST_F(DexoptTest, DexoptPrimaryProfilePublic) {
676 LOG(INFO) << "DexoptPrimaryProfilePublic";
Calin Juravle5bd1c722018-02-01 17:23:54 +0000677 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700678 DEXOPT_BOOTCOMPLETE | DEXOPT_PROFILE_GUIDED | DEXOPT_PUBLIC |
679 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000680 app_oat_dir_.c_str(),
681 kTestAppGid,
liulvping61907742018-08-21 09:36:52 +0800682 DEX2OAT_FROM_SCRATCH,
683 /*binder_result=*/nullptr,
684 empty_dm_file_.c_str());
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000685}
686
687TEST_F(DexoptTest, DexoptPrimaryBackgroundOk) {
688 LOG(INFO) << "DexoptPrimaryBackgroundOk";
689 CompilePrimaryDexOk("speed-profile",
Mathieu Chartier2d5a1bc2018-04-03 14:55:43 -0700690 DEXOPT_IDLE_BACKGROUND_JOB | DEXOPT_PROFILE_GUIDED |
691 DEXOPT_GENERATE_APP_IMAGE,
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000692 app_oat_dir_.c_str(),
693 kTestAppGid,
liulvping61907742018-08-21 09:36:52 +0800694 DEX2OAT_FROM_SCRATCH,
695 /*binder_result=*/nullptr,
696 empty_dm_file_.c_str());
Calin Juravlec4f6a0b2018-02-01 01:27:24 +0000697}
698
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800699TEST_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,
liulvping61907742018-08-21 09:36:52 +0800715 DEX2OAT_FROM_SCRATCH,
716 /*binder_result=*/nullptr,
717 empty_dm_file_.c_str());
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800718 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,
liulvping61907742018-08-21 09:36:52 +0800734 DEX2OAT_FROM_SCRATCH,
735 /*binder_result=*/nullptr,
736 empty_dm_file_.c_str());
Mathieu Chartierd41622c2019-01-31 12:59:39 -0800737 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 Juravlec4f6a0b2018-02-01 01:27:24 +0000747class 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
774TEST_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 Juravle7d765462017-09-04 15:57:10 -0700783
784class ReconcileTest : public DexoptTest {
785 virtual void SetUp() {
786 DexoptTest::SetUp();
Calin Juravle29591732017-11-20 17:46:19 -0800787 CompileSecondaryDex(secondary_dex_ce_, DEXOPT_STORAGE_CE,
Calin Juravle7d765462017-09-04 15:57:10 -0700788 /*binder_ok*/ true, /*compile_ok*/ true);
Calin Juravle29591732017-11-20 17:46:19 -0800789 CompileSecondaryDex(secondary_dex_de_, DEXOPT_STORAGE_DE,
Calin Juravle7d765462017-09-04 15:57:10 -0700790 /*binder_ok*/ true, /*compile_ok*/ true);
791 }
792};
793
794TEST_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
800TEST_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
806TEST_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
812TEST_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
819TEST_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
828TEST_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
836TEST_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 Juravle29591732017-11-20 17:46:19 -0800842class ProfileTest : public DexoptTest {
843 protected:
844 std::string cur_profile_;
845 std::string ref_profile_;
846 std::string snap_profile_;
847
Calin Juravle824a64d2018-01-18 20:23:17 -0800848 static constexpr const char* kPrimaryProfile = "primary.prof";
849
Calin Juravle29591732017-11-20 17:46:19 -0800850 virtual void SetUp() {
851 DexoptTest::SetUp();
852 cur_profile_ = create_current_profile_path(
Calin Juravle824a64d2018-01-18 20:23:17 -0800853 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 Juravle29591732017-11-20 17:46:19 -0800857 }
858
Calin Juravle824a64d2018-01-18 20:23:17 -0800859 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 Juravle29591732017-11-20 17:46:19 -0800864 ::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 Juravle824a64d2018-01-18 20:23:17 -0800871 SetupProfile(ref_profile_, kTestAppUid, kTestAppGid, 0600, 2);
Calin Juravle29591732017-11-20 17:46:19 -0800872 }
873 }
874
Calin Juravlec3596c32017-12-05 12:29:15 -0800875 void createProfileSnapshot(int32_t appid, const std::string& package_name,
876 bool expected_result) {
Calin Juravle29591732017-11-20 17:46:19 -0800877 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800878 ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
879 appid, package_name, kPrimaryProfile, apk_path_, &result));
Calin Juravle29591732017-11-20 17:46:19 -0800880 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 Juravle40b5f462018-02-07 04:11:52 -0800896 " --reference-profile-file=" + expected_profile_content +
897 " --apk=" + apk_path_);
Calin Juravle29591732017-11-20 17:46:19 -0800898
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 Juravle408cd4a2018-01-20 23:34:18 -0800915 void mergePackageProfiles(const std::string& package_name,
916 const std::string& code_path,
917 bool expected_result) {
Calin Juravle824a64d2018-01-18 20:23:17 -0800918 bool result;
Andreas Gampef448ced2019-01-31 09:22:27 -0800919 ASSERT_BINDER_SUCCESS(service_->mergeProfiles(
920 kTestAppUid, package_name, code_path, &result));
Calin Juravle824a64d2018-01-18 20:23:17 -0800921 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 Juravlec3b049e2018-01-18 22:32:58 -0800942 // 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 Gampef448ced2019-01-31 09:22:27 -0800946 ASSERT_BINDER_SUCCESS(service_->prepareAppProfile(
Jeff Sharkey1ebe77f2018-02-27 11:40:45 -0700947 package_name, kTestUserId, kTestAppId, profile_name, apk_path_,
Andreas Gampef448ced2019-01-31 09:22:27 -0800948 /*dex_metadata*/ nullptr, &result));
Calin Juravlec3b049e2018-01-18 22:32:58 -0800949 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 Juravle0d0a4922018-01-23 19:54:11 -0800968 protected:
Calin Juravle29591732017-11-20 17:46:19 -0800969 void TransitionToSystemServer() {
970 ASSERT_TRUE(DropCapabilities(kSystemUid, kSystemGid));
Andreas Gampef04b0b42019-03-25 11:20:57 -0700971 int32_t res = selinux_android_setcon("u:r:system_server:s0");
Calin Juravle29591732017-11-20 17:46:19 -0800972 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
1004TEST_F(ProfileTest, ProfileSnapshotOk) {
1005 LOG(INFO) << "ProfileSnapshotOk";
1006
1007 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -08001008 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -08001009}
1010
1011// The reference profile is created on the fly. We need to be able to
1012// snapshot without one.
1013TEST_F(ProfileTest, ProfileSnapshotOkNoReference) {
1014 LOG(INFO) << "ProfileSnapshotOkNoReference";
1015
1016 SetupProfiles(/*setup_ref*/ false);
Calin Juravlec3596c32017-12-05 12:29:15 -08001017 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -08001018}
1019
1020TEST_F(ProfileTest, ProfileSnapshotFailWrongPackage) {
1021 LOG(INFO) << "ProfileSnapshotFailWrongPackage";
1022
1023 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -08001024 createProfileSnapshot(kTestAppId, "not.there", /*expected_result*/ false);
Calin Juravle29591732017-11-20 17:46:19 -08001025}
1026
1027TEST_F(ProfileTest, ProfileSnapshotDestroySnapshot) {
1028 LOG(INFO) << "ProfileSnapshotDestroySnapshot";
1029
1030 SetupProfiles(/*setup_ref*/ true);
Calin Juravlec3596c32017-12-05 12:29:15 -08001031 createProfileSnapshot(kTestAppId, package_name_, /*expected_result*/ true);
Calin Juravle29591732017-11-20 17:46:19 -08001032
Andreas Gampef448ced2019-01-31 09:22:27 -08001033 ASSERT_BINDER_SUCCESS(service_->destroyProfileSnapshot(package_name_, kPrimaryProfile));
Calin Juravle29591732017-11-20 17:46:19 -08001034 struct stat st;
1035 ASSERT_EQ(-1, stat(snap_profile_.c_str(), &st));
1036 ASSERT_EQ(ENOENT, errno);
1037}
1038
Calin Juravle824a64d2018-01-18 20:23:17 -08001039TEST_F(ProfileTest, ProfileMergeOk) {
1040 LOG(INFO) << "ProfileMergeOk";
1041
1042 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -08001043 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -08001044}
1045
1046// The reference profile is created on the fly. We need to be able to
1047// merge without one.
1048TEST_F(ProfileTest, ProfileMergeOkNoReference) {
1049 LOG(INFO) << "ProfileMergeOkNoReference";
1050
1051 SetupProfiles(/*setup_ref*/ false);
Calin Juravle408cd4a2018-01-20 23:34:18 -08001052 mergePackageProfiles(package_name_, "primary.prof", /*expected_result*/ true);
Calin Juravle824a64d2018-01-18 20:23:17 -08001053}
1054
1055TEST_F(ProfileTest, ProfileMergeFailWrongPackage) {
1056 LOG(INFO) << "ProfileMergeFailWrongPackage";
1057
1058 SetupProfiles(/*setup_ref*/ true);
Calin Juravle408cd4a2018-01-20 23:34:18 -08001059 mergePackageProfiles("not.there", "primary.prof", /*expected_result*/ false);
Calin Juravle824a64d2018-01-18 20:23:17 -08001060}
1061
Calin Juravled2affb82017-11-28 17:41:43 -08001062TEST_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 Juravle824a64d2018-01-18 20:23:17 -08001068 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravled2affb82017-11-28 17:41:43 -08001069 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 Juravle6f06eb62017-11-28 18:44:53 -08001072 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).
1077TEST_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 Juravle824a64d2018-01-18 20:23:17 -08001083 kPrimaryProfile, /*is_secondary_dex*/false);
Calin Juravle6f06eb62017-11-28 18:44:53 -08001084 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 Gampef448ced2019-01-31 09:22:27 -08001091 ASSERT_BINDER_SUCCESS(service_->createAppData(
Calin Juravle6f06eb62017-11-28 18:44:53 -08001092 volume_uuid_,
1093 package_name_,
1094 kTestUserId,
1095 kAppDataFlags,
1096 kTestAppUid,
1097 se_info_,
1098 kOSdkVersion,
Andreas Gampef448ced2019-01-31 09:22:27 -08001099 &ce_data_inode_));
Calin Juravle6f06eb62017-11-28 18:44:53 -08001100
1101 // Check the file access.
1102 CheckFileAccess(cur_profile_dir, kTestAppUid, kTestAppUid, 0700 | S_IFDIR);
Calin Juravle6f06eb62017-11-28 18:44:53 -08001103 CheckFileAccess(ref_profile_dir, kSystemUid, kTestAppGid, 0770 | S_IFDIR);
Calin Juravled2affb82017-11-28 17:41:43 -08001104}
1105
Calin Juravlec3b049e2018-01-18 22:32:58 -08001106TEST_F(ProfileTest, ProfilePrepareOk) {
1107 LOG(INFO) << "ProfilePrepareOk";
1108 preparePackageProfile(package_name_, "split.prof", /*expected_result*/ true);
1109}
1110
1111TEST_F(ProfileTest, ProfilePrepareFailInvalidPackage) {
1112 LOG(INFO) << "ProfilePrepareFailInvalidPackage";
1113 preparePackageProfile("not.there.package", "split.prof", /*expected_result*/ false);
1114}
1115
1116TEST_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 Juravle0d0a4922018-01-23 19:54:11 -08001124
1125class 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 Gampef448ced2019-01-31 09:22:27 -08001145 ASSERT_BINDER_SUCCESS(service_->createProfileSnapshot(
1146 -1, "android", "android.prof", classpath, &result));
Calin Juravle0d0a4922018-01-23 19:54:11 -08001147 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
1175TEST_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
1182TEST_F(BootProfileTest, BootProfileSnapshotFailEmptyClasspath) {
1183 LOG(INFO) << "BootProfileSnapshotFailEmptyClasspath";
1184
1185 createBootImageProfileSnapshot(/*boot_classpath*/ "", /*expected_result*/ false);
1186}
1187
1188TEST_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.
1200TEST_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 Juravlef74a7372019-02-28 20:29:41 -08001229TEST_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 Juravle7d765462017-09-04 15:57:10 -07001288} // namespace installd
1289} // namespace android