blob: 91b231b4197c5f90d9ab9cbb68bf1017a353dd94 [file] [log] [blame]
Andreas Gampee1459ae2016-06-29 09:36:30 -07001/*
2 * Copyright (C) 2016 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
Igor Murashkin5573c372017-11-16 13:34:30 -080017#include <regex>
Andreas Gampe7adeda82016-07-25 08:27:35 -070018#include <sstream>
Andreas Gampee1459ae2016-06-29 09:36:30 -070019#include <string>
20#include <vector>
Andreas Gampee1459ae2016-06-29 09:36:30 -070021
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include <sys/wait.h>
23#include <unistd.h>
24
Andreas Gampe57943812017-12-06 21:39:13 -080025#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027
Andreas Gampee1459ae2016-06-29 09:36:30 -070028#include "common_runtime_test.h"
29
Andreas Gampee1459ae2016-06-29 09:36:30 -070030#include "base/macros.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "base/mutex-inl.h"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/utils.h"
David Sehr013fd802018-01-11 22:55:24 -080033#include "dex/art_dex_file_loader.h"
Mathieu Chartier05f90d12018-02-07 13:47:17 -080034#include "dex/base64_test_util.h"
David Sehr312f3b22018-03-19 08:39:26 -070035#include "dex/bytecode_utils.h"
Mathieu Chartier2242ef12018-07-23 18:14:13 -070036#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/code_item_accessors-inl.h"
38#include "dex/dex_file-inl.h"
39#include "dex/dex_file_loader.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070040#include "dex2oat_environment_test.h"
Andreas Gampef7882972017-03-20 16:35:24 -070041#include "dex2oat_return_codes.h"
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -070042#include "intern_table-inl.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070043#include "oat.h"
44#include "oat_file.h"
David Sehr82d046e2018-04-23 08:14:19 -070045#include "profile/profile_compilation_info.h"
Mathieu Chartier792111c2018-02-15 13:02:15 -080046#include "vdex_file.h"
47#include "ziparchive/zip_writer.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070048
Andreas Gampee1459ae2016-06-29 09:36:30 -070049namespace art {
50
Mathieu Chartierea650f32017-05-24 12:04:13 -070051static constexpr size_t kMaxMethodIds = 65535;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070052static constexpr bool kDebugArgs = false;
Mathieu Chartier02129102017-12-22 11:04:01 -080053static const char* kDisableCompactDex = "--compact-dex-level=none";
Mathieu Chartierea650f32017-05-24 12:04:13 -070054
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080055using android::base::StringPrintf;
56
Andreas Gampee1459ae2016-06-29 09:36:30 -070057class Dex2oatTest : public Dex2oatEnvironmentTest {
58 public:
Roland Levillainf73caca2018-08-24 17:19:07 +010059 void TearDown() override {
Andreas Gampee1459ae2016-06-29 09:36:30 -070060 Dex2oatEnvironmentTest::TearDown();
61
62 output_ = "";
63 error_msg_ = "";
64 success_ = false;
65 }
66
67 protected:
Mathieu Chartier9e050df2017-08-09 10:05:47 -070068 int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
Andreas Gampef7882972017-03-20 16:35:24 -070069 const std::string& odex_location,
70 CompilerFilter::Filter filter,
71 std::string* error_msg,
72 const std::vector<std::string>& extra_args = {},
73 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080074 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070075 std::vector<std::string> args;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070076 // Add dex file args.
77 for (const std::string& dex_location : dex_locations) {
78 args.push_back("--dex-file=" + dex_location);
79 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080080 if (use_fd) {
81 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
82 CHECK(oat_file != nullptr) << odex_location;
83 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080084 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080085 } else {
86 args.push_back("--oat-file=" + odex_location);
87 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070088 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
89 args.push_back("--runtime-arg");
90 args.push_back("-Xnorelocate");
91
92 args.insert(args.end(), extra_args.begin(), extra_args.end());
93
Andreas Gampef7882972017-03-20 16:35:24 -070094 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080095 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070096 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080097 }
Andreas Gampef7882972017-03-20 16:35:24 -070098 return status;
99 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700100
Andreas Gampe641a4732017-08-24 13:21:35 -0700101 void GenerateOdexForTest(
102 const std::string& dex_location,
103 const std::string& odex_location,
104 CompilerFilter::Filter filter,
105 const std::vector<std::string>& extra_args = {},
106 bool expect_success = true,
107 bool use_fd = false) {
108 GenerateOdexForTest(dex_location,
109 odex_location,
110 filter,
111 extra_args,
112 expect_success,
113 use_fd,
114 [](const OatFile&) {});
115 }
116
Andreas Gampe80ddf272018-01-11 09:41:00 -0800117 bool test_accepts_odex_file_on_failure = false;
118
Andreas Gampe641a4732017-08-24 13:21:35 -0700119 template <typename T>
120 void GenerateOdexForTest(
121 const std::string& dex_location,
122 const std::string& odex_location,
123 CompilerFilter::Filter filter,
124 const std::vector<std::string>& extra_args,
125 bool expect_success,
126 bool use_fd,
127 T check_oat) {
Andreas Gampef7882972017-03-20 16:35:24 -0700128 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700129 int status = GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700130 odex_location,
131 filter,
132 &error_msg,
133 extra_args,
134 use_fd);
Andreas Gampe80ddf272018-01-11 09:41:00 -0800135 bool success = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700136 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800137 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700138
139 // Verify the odex file was generated as expected.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100140 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
141 odex_location.c_str(),
Andreas Gampee1459ae2016-06-29 09:36:30 -0700142 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100143 /* requested_base */ nullptr,
144 /* executable */ false,
145 /* low_4gb */ false,
Andreas Gampee1459ae2016-06-29 09:36:30 -0700146 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100147 /* reservation */ nullptr,
Andreas Gampee1459ae2016-06-29 09:36:30 -0700148 &error_msg));
149 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
150
151 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700152 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700153 } else {
154 ASSERT_FALSE(success) << output_;
155
156 error_msg_ = error_msg;
157
Andreas Gampe80ddf272018-01-11 09:41:00 -0800158 if (!test_accepts_odex_file_on_failure) {
159 // Verify there's no loadable odex file.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100160 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
161 odex_location.c_str(),
Andreas Gampe80ddf272018-01-11 09:41:00 -0800162 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100163 /* requested_base */ nullptr,
164 /* executable */ false,
165 /* low_4gb */ false,
Andreas Gampe80ddf272018-01-11 09:41:00 -0800166 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100167 /* reservation */ nullptr,
Andreas Gampe80ddf272018-01-11 09:41:00 -0800168 &error_msg));
169 ASSERT_TRUE(odex_file.get() == nullptr);
170 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700171 }
172 }
173
Calin Juravle1ccf6132017-08-02 17:46:53 -0700174 // Check the input compiler filter against the generated oat file's filter. May be overridden
Andreas Gampee1459ae2016-06-29 09:36:30 -0700175 // in subclasses when equality is not expected.
176 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
177 EXPECT_EQ(expected, actual);
178 }
179
Andreas Gampef7882972017-03-20 16:35:24 -0700180 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700181 Runtime* runtime = Runtime::Current();
182
183 const std::vector<gc::space::ImageSpace*>& image_spaces =
184 runtime->GetHeap()->GetBootImageSpaces();
185 if (image_spaces.empty()) {
186 *error_msg = "No image location found for Dex2Oat.";
187 return false;
188 }
189 std::string image_location = image_spaces[0]->GetImageLocation();
190
191 std::vector<std::string> argv;
192 argv.push_back(runtime->GetCompilerExecutable());
Calin Juravle1ccf6132017-08-02 17:46:53 -0700193
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000194 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700195 argv.push_back("--debuggable");
196 }
197 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
198
199 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100200 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700201 }
202
203 if (runtime->MustRelocateIfPossible()) {
204 argv.push_back("--runtime-arg");
205 argv.push_back("-Xrelocate");
206 } else {
207 argv.push_back("--runtime-arg");
208 argv.push_back("-Xnorelocate");
209 }
210
211 if (!kIsTargetBuild) {
212 argv.push_back("--host");
213 }
214
215 argv.push_back("--boot-image=" + image_location);
216
217 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
218 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
219
220 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
221
222 // We must set --android-root.
223 const char* android_root = getenv("ANDROID_ROOT");
224 CHECK(android_root != nullptr);
225 argv.push_back("--android-root=" + std::string(android_root));
226
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700227 if (kDebugArgs) {
228 std::string all_args;
229 for (const std::string& arg : argv) {
230 all_args += arg + " ";
231 }
232 LOG(ERROR) << all_args;
233 }
234
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700235 // We need dex2oat to actually log things.
236 auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; };
237 ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, &output_);
238 if (res.stage != ForkAndExecResult::kFinished) {
239 *error_msg = strerror(errno);
240 return -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100241 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700242 success_ = res.StandardSuccess();
243 return res.status_code;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700244 }
245
246 std::string output_ = "";
247 std::string error_msg_ = "";
248 bool success_ = false;
249};
250
251class Dex2oatSwapTest : public Dex2oatTest {
252 protected:
253 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
254 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
255 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
256
Andreas Gampe7adeda82016-07-25 08:27:35 -0700257 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700258
259 std::vector<std::string> copy(extra_args);
260
261 std::unique_ptr<ScratchFile> sf;
262 if (use_fd) {
263 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800264 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700265 } else {
266 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
267 copy.push_back("--swap-file=" + swap_location);
268 }
269 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
270
271 CheckValidity();
272 ASSERT_TRUE(success_);
273 CheckResult(expect_use);
274 }
275
Andreas Gampe7adeda82016-07-25 08:27:35 -0700276 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000277 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700278 }
279
280 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700281 if (kIsTargetBuild) {
282 CheckTargetResult(expect_use);
283 } else {
284 CheckHostResult(expect_use);
285 }
286 }
287
Andreas Gampe7adeda82016-07-25 08:27:35 -0700288 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700289 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
290 // something for variants with file descriptor where we can control the lifetime of
291 // the swap file and thus take a look at it.
292 }
293
Andreas Gampe7adeda82016-07-25 08:27:35 -0700294 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700295 if (!kIsTargetBuild) {
296 if (expect_use) {
297 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
298 << output_;
299 } else {
300 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
301 << output_;
302 }
303 }
304 }
305
306 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700307 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700308 if (kIsTargetBuild) {
309 CheckTargetValidity();
310 } else {
311 CheckHostValidity();
312 }
313 }
314
Andreas Gampe7adeda82016-07-25 08:27:35 -0700315 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700316 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
317 // something for variants with file descriptor where we can control the lifetime of
318 // the swap file and thus take a look at it.
319 }
320
321 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700322 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700323 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
324 }
325};
326
327TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
328 RunTest(false /* use_fd */, false /* expect_use */);
329 RunTest(true /* use_fd */, false /* expect_use */);
330}
331
332TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
333 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
334 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
335}
336
337TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
338 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
339 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
340}
341
342TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
343 RunTest(false /* use_fd */,
344 true /* expect_use */,
345 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
346 RunTest(true /* use_fd */,
347 true /* expect_use */,
348 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
349}
350
Andreas Gampe7adeda82016-07-25 08:27:35 -0700351class Dex2oatSwapUseTest : public Dex2oatSwapTest {
352 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100353 void CheckHostResult(bool expect_use) override {
Andreas Gampe7adeda82016-07-25 08:27:35 -0700354 if (!kIsTargetBuild) {
355 if (expect_use) {
356 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
357 << output_;
358 } else {
359 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
360 << output_;
361 }
362 }
363 }
364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100365 std::string GetTestDexFileName() override {
Andreas Gampe7adeda82016-07-25 08:27:35 -0700366 // Use Statics as it has a handful of functions.
367 return CommonRuntimeTest::GetTestDexFileName("Statics");
368 }
369
370 void GrabResult1() {
371 if (!kIsTargetBuild) {
372 native_alloc_1_ = ParseNativeAlloc();
373 swap_1_ = ParseSwap(false /* expected */);
374 } else {
375 native_alloc_1_ = std::numeric_limits<size_t>::max();
376 swap_1_ = 0;
377 }
378 }
379
380 void GrabResult2() {
381 if (!kIsTargetBuild) {
382 native_alloc_2_ = ParseNativeAlloc();
383 swap_2_ = ParseSwap(true /* expected */);
384 } else {
385 native_alloc_2_ = 0;
386 swap_2_ = std::numeric_limits<size_t>::max();
387 }
388 }
389
390 private:
391 size_t ParseNativeAlloc() {
392 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
393 std::smatch native_alloc_match;
394 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
395 if (!found) {
396 EXPECT_TRUE(found);
397 return 0;
398 }
399 if (native_alloc_match.size() != 2U) {
400 EXPECT_EQ(native_alloc_match.size(), 2U);
401 return 0;
402 }
403
404 std::istringstream stream(native_alloc_match[1].str());
405 size_t value;
406 stream >> value;
407
408 return value;
409 }
410
411 size_t ParseSwap(bool expected) {
412 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
413 std::smatch swap_match;
414 bool found = std::regex_search(output_, swap_match, swap_regex);
415 if (found != expected) {
416 EXPECT_EQ(expected, found);
417 return 0;
418 }
419
420 if (!found) {
421 return 0;
422 }
423
424 if (swap_match.size() != 2U) {
425 EXPECT_EQ(swap_match.size(), 2U);
426 return 0;
427 }
428
429 std::istringstream stream(swap_match[1].str());
430 size_t value;
431 stream >> value;
432
433 return value;
434 }
435
436 protected:
437 size_t native_alloc_1_;
438 size_t native_alloc_2_;
439
440 size_t swap_1_;
441 size_t swap_2_;
442};
443
444TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000445 // Native memory usage isn't correctly tracked when running under ASan.
446 TEST_DISABLED_FOR_MEMORY_TOOL();
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700447
Vladimir Marko57070da2017-02-14 16:16:30 +0000448 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000449 // hold true on some x86 systems; disable this test while we
450 // investigate (b/29259363).
451 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000452
Andreas Gampe7adeda82016-07-25 08:27:35 -0700453 RunTest(false /* use_fd */,
454 false /* expect_use */);
455 GrabResult1();
456 std::string output_1 = output_;
457
458 output_ = "";
459
460 RunTest(false /* use_fd */,
461 true /* expect_use */,
462 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
463 GrabResult2();
464 std::string output_2 = output_;
465
466 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
467 EXPECT_LT(native_alloc_2_, native_alloc_1_);
468 EXPECT_LT(swap_1_, swap_2_);
469
470 LOG(ERROR) << output_1;
471 LOG(ERROR) << output_2;
472 }
473}
474
Andreas Gampe67f02822016-06-24 21:05:23 -0700475class Dex2oatVeryLargeTest : public Dex2oatTest {
476 protected:
477 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100478 CompilerFilter::Filter result ATTRIBUTE_UNUSED) override {
Andreas Gampe67f02822016-06-24 21:05:23 -0700479 // Ignore, we'll do our own checks.
480 }
481
482 void RunTest(CompilerFilter::Filter filter,
483 bool expect_large,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700484 bool expect_downgrade,
Andreas Gampe67f02822016-06-24 21:05:23 -0700485 const std::vector<std::string>& extra_args = {}) {
486 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
487 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700488 std::string app_image_file = GetScratchDir() + "/Test.art";
Andreas Gampe67f02822016-06-24 21:05:23 -0700489
490 Copy(GetDexSrc1(), dex_location);
491
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700492 std::vector<std::string> new_args(extra_args);
493 new_args.push_back("--app-image-file=" + app_image_file);
494 GenerateOdexForTest(dex_location, odex_location, filter, new_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700495
496 CheckValidity();
497 ASSERT_TRUE(success_);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700498 CheckResult(dex_location,
499 odex_location,
500 app_image_file,
501 filter,
502 expect_large,
503 expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700504 }
505
506 void CheckResult(const std::string& dex_location,
507 const std::string& odex_location,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700508 const std::string& app_image_file,
Andreas Gampe67f02822016-06-24 21:05:23 -0700509 CompilerFilter::Filter filter,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700510 bool expect_large,
511 bool expect_downgrade) {
512 if (expect_downgrade) {
513 EXPECT_TRUE(expect_large);
514 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700515 // Host/target independent checks.
516 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100517 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
518 odex_location.c_str(),
Andreas Gampe67f02822016-06-24 21:05:23 -0700519 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100520 /* requested_base */ nullptr,
521 /* executable */ false,
522 /* low_4gb */ false,
Andreas Gampe67f02822016-06-24 21:05:23 -0700523 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100524 /* reservation */ nullptr,
Andreas Gampe67f02822016-06-24 21:05:23 -0700525 &error_msg));
526 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700527 EXPECT_GT(app_image_file.length(), 0u);
528 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700529 if (expect_large) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700530 // Note: we cannot check the following
531 // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700532 // The reason is that the filter override currently happens when the dex files are
533 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
534 // store cannot be changed, and the original filter is set in stone.
535
536 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
537 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
538 ASSERT_TRUE(dex_file != nullptr);
539 uint32_t class_def_count = dex_file->NumClassDefs();
540 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
541 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
542 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
543 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
544 }
545 }
546
547 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100548 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700549 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
550 }
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700551
552 // If expect large, make sure the app image isn't generated or is empty.
553 if (file != nullptr) {
554 EXPECT_EQ(file->GetLength(), 0u);
555 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700556 } else {
557 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700558 ASSERT_TRUE(file != nullptr) << app_image_file;
559 EXPECT_GT(file->GetLength(), 0u);
Andreas Gampe67f02822016-06-24 21:05:23 -0700560 }
561
562 // Host/target dependent checks.
563 if (kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700564 CheckTargetResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700565 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700566 CheckHostResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700567 }
568 }
569
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700570 void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700571 // TODO: Ignore for now. May do something for fd things.
572 }
573
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700574 void CheckHostResult(bool expect_downgrade) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700575 if (!kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700576 if (expect_downgrade) {
577 EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700578 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700579 EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700580 }
581 }
582 }
583
584 // Check whether the dex2oat run was really successful.
585 void CheckValidity() {
586 if (kIsTargetBuild) {
587 CheckTargetValidity();
588 } else {
589 CheckHostValidity();
590 }
591 }
592
593 void CheckTargetValidity() {
594 // TODO: Ignore for now.
595 }
596
597 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
598 void CheckHostValidity() {
599 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
600 }
601};
602
603TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700604 RunTest(CompilerFilter::kAssumeVerified, false, false);
605 RunTest(CompilerFilter::kExtract, false, false);
606 RunTest(CompilerFilter::kQuicken, false, false);
607 RunTest(CompilerFilter::kSpeed, false, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700608
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700609 RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
610 RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
611 RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" });
612 RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700613}
614
615TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700616 RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
617 RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
618 RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" });
619 RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700620}
621
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800622// Regressin test for b/35665292.
623TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
624 // Test that dex2oat doesn't crash with speed-profile but no input profile.
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700625 RunTest(CompilerFilter::kSpeedProfile, false, false);
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800626}
627
Jeff Hao608f2ce2016-10-19 11:17:11 -0700628class Dex2oatLayoutTest : public Dex2oatTest {
629 protected:
630 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100631 CompilerFilter::Filter result ATTRIBUTE_UNUSED) override {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700632 // Ignore, we'll do our own checks.
633 }
634
Jeff Hao41fba6a2016-11-28 11:53:33 -0800635 // Emits a profile with a single dex file with the given location and a single class index of 1.
636 void GenerateProfile(const std::string& test_profile,
637 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800638 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800639 uint32_t checksum) {
640 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
641 CHECK_GE(profile_test_fd, 0);
642
643 ProfileCompilationInfo info;
644 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800645 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700646 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800647 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800648 bool result = info.Save(profile_test_fd);
649 close(profile_test_fd);
650 ASSERT_TRUE(result);
651 }
652
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800653 void CompileProfileOdex(const std::string& dex_location,
654 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800655 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800656 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800657 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000658 const std::vector<std::string>& extra_args = {},
659 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800660 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800661 const char* location = dex_location.c_str();
662 std::string error_msg;
663 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800664 const ArtDexFileLoader dex_file_loader;
665 ASSERT_TRUE(dex_file_loader.Open(
Nicolas Geoffray095c6c92017-10-19 13:59:55 +0100666 location, location, /* verify */ true, /* verify_checksum */ true, &error_msg, &dex_files));
Jeff Hao41fba6a2016-11-28 11:53:33 -0800667 EXPECT_EQ(dex_files.size(), 1U);
668 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800669 GenerateProfile(profile_location,
670 dex_location,
671 num_profile_classes,
672 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800673 std::vector<std::string> copy(extra_args);
674 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800675 std::unique_ptr<File> app_image_file;
676 if (!app_image_file_name.empty()) {
677 if (use_fd) {
678 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
679 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
680 } else {
681 copy.push_back("--app-image-file=" + app_image_file_name);
682 }
683 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800684 GenerateOdexForTest(dex_location,
685 odex_location,
686 CompilerFilter::kSpeedProfile,
687 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000688 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800689 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800690 if (app_image_file != nullptr) {
691 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
692 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800693 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700694
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100695 uint64_t GetImageObjectSectionSize(const std::string& image_file_name) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800696 EXPECT_FALSE(image_file_name.empty());
697 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
698 CHECK(file != nullptr);
699 ImageHeader image_header;
700 const bool success = file->ReadFully(&image_header, sizeof(image_header));
701 CHECK(success);
702 CHECK(image_header.IsValid());
703 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100704 return image_header.GetObjectsSection().Size();
Mathieu Chartier046854b2017-03-01 17:16:22 -0800705 }
706
707 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800708 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
709 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800710 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800711 Copy(GetDexSrc2(), dex_location);
712
Mathieu Chartier046854b2017-03-01 17:16:22 -0800713 uint64_t image_file_empty_profile = 0;
714 if (app_image) {
715 CompileProfileOdex(dex_location,
716 odex_location,
717 app_image_file,
718 /* use_fd */ false,
719 /* num_profile_classes */ 0);
720 CheckValidity();
721 ASSERT_TRUE(success_);
722 // Don't check the result since CheckResult relies on the class being in the profile.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100723 image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800724 EXPECT_GT(image_file_empty_profile, 0u);
725 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700726
Mathieu Chartier046854b2017-03-01 17:16:22 -0800727 // Small profile.
728 CompileProfileOdex(dex_location,
729 odex_location,
730 app_image_file,
731 /* use_fd */ false,
732 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700733 CheckValidity();
734 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800735 CheckResult(dex_location, odex_location, app_image_file);
736
737 if (app_image) {
738 // Test that the profile made a difference by adding more classes.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100739 const uint64_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
740 ASSERT_LT(image_file_empty_profile, image_file_small_profile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800741 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700742 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800743
744 void RunTestVDex() {
745 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
746 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
747 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800748 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800749 Copy(GetDexSrc2(), dex_location);
750
751 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
752 CHECK(vdex_file1 != nullptr) << vdex_location;
753 ScratchFile vdex_file2;
754 {
755 std::string input_vdex = "--input-vdex-fd=-1";
756 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
757 CompileProfileOdex(dex_location,
758 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800759 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800760 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800761 /* num_profile_classes */ 1,
Mathieu Chartierf1609832018-01-31 03:09:56 -0800762 { input_vdex, output_vdex });
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800763 EXPECT_GT(vdex_file1->GetLength(), 0u);
764 }
765 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000766 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800767 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
768 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
769 CompileProfileOdex(dex_location,
770 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800771 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800772 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800773 /* num_profile_classes */ 1,
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800774 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100775 /* expect_success */ true);
776 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800777 }
778 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
779 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100780 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800781 }
782
Mathieu Chartier046854b2017-03-01 17:16:22 -0800783 void CheckResult(const std::string& dex_location,
784 const std::string& odex_location,
785 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700786 // Host/target independent checks.
787 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100788 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
789 odex_location.c_str(),
Jeff Hao608f2ce2016-10-19 11:17:11 -0700790 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100791 /* requested_base */ nullptr,
792 /* executable */ false,
793 /* low_4gb */ false,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700794 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100795 /* reservation */ nullptr,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700796 &error_msg));
797 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
798
Jeff Hao042e8982016-10-19 11:17:11 -0700799 const char* location = dex_location.c_str();
800 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800801 const ArtDexFileLoader dex_file_loader;
802 ASSERT_TRUE(dex_file_loader.Open(
Nicolas Geoffray095c6c92017-10-19 13:59:55 +0100803 location, location, /* verify */ true, /* verify_checksum */ true, &error_msg, &dex_files));
Jeff Hao042e8982016-10-19 11:17:11 -0700804 EXPECT_EQ(dex_files.size(), 1U);
805 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
806
Jeff Hao608f2ce2016-10-19 11:17:11 -0700807 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700808 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
809 ASSERT_TRUE(new_dex_file != nullptr);
810 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700811 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700812 ASSERT_GE(class_def_count, 2U);
813
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700814 // Make sure the indexes stay the same.
Jeff Hao042e8982016-10-19 11:17:11 -0700815 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
816 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
817 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
818 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700819 EXPECT_EQ(old_class0, new_class0);
820 EXPECT_EQ(old_class1, new_class1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700821 }
822
Jeff Haoc155b052017-01-17 17:43:29 -0800823 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800824
825 if (!app_image_file_name.empty()) {
826 // Go peek at the image header to make sure it was large enough to contain the class.
827 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
828 ImageHeader image_header;
829 bool success = file->ReadFully(&image_header, sizeof(image_header));
830 ASSERT_TRUE(success);
831 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100832 EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800833 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700834 }
835
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800836 // Check whether the dex2oat run was really successful.
837 void CheckValidity() {
838 if (kIsTargetBuild) {
839 CheckTargetValidity();
840 } else {
841 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700842 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800843 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700844
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800845 void CheckTargetValidity() {
846 // TODO: Ignore for now.
847 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700848
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800849 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
850 void CheckHostValidity() {
851 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
852 }
853};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700854
855TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800856 RunTest(/* app-image */ false);
857}
858
859TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
860 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700861}
862
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800863TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
864 RunTestVDex();
865}
866
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100867class Dex2oatUnquickenTest : public Dex2oatTest {
868 protected:
869 void RunUnquickenMultiDex() {
870 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
871 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
872 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
873 Copy(GetTestDexFileName("MultiDex"), dex_location);
874
875 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
876 CHECK(vdex_file1 != nullptr) << vdex_location;
877 // Quicken the dex file into a vdex file.
878 {
879 std::string input_vdex = "--input-vdex-fd=-1";
880 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
881 GenerateOdexForTest(dex_location,
882 odex_location,
883 CompilerFilter::kQuicken,
Mathieu Chartierf1609832018-01-31 03:09:56 -0800884 { input_vdex, output_vdex },
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100885 /* expect_success */ true,
886 /* use_fd */ true);
887 EXPECT_GT(vdex_file1->GetLength(), 0u);
888 }
889 // Unquicken by running the verify compiler filter on the vdex file.
890 {
891 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
892 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
893 GenerateOdexForTest(dex_location,
894 odex_location,
895 CompilerFilter::kVerify,
Mathieu Chartier02129102017-12-22 11:04:01 -0800896 { input_vdex, output_vdex, kDisableCompactDex },
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100897 /* expect_success */ true,
898 /* use_fd */ true);
899 }
900 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
901 CheckResult(dex_location, odex_location);
902 ASSERT_TRUE(success_);
903 }
904
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800905 void RunUnquickenMultiDexCDex() {
906 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
907 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
908 std::string odex_location2 = GetOdexDir() + "/UnquickenMultiDex2.odex";
909 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
910 std::string vdex_location2 = GetOdexDir() + "/UnquickenMultiDex2.vdex";
911 Copy(GetTestDexFileName("MultiDex"), dex_location);
912
913 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
914 std::unique_ptr<File> vdex_file2(OS::CreateEmptyFile(vdex_location2.c_str()));
915 CHECK(vdex_file1 != nullptr) << vdex_location;
916 CHECK(vdex_file2 != nullptr) << vdex_location2;
917
918 // Quicken the dex file into a vdex file.
919 {
920 std::string input_vdex = "--input-vdex-fd=-1";
921 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
922 GenerateOdexForTest(dex_location,
923 odex_location,
924 CompilerFilter::kQuicken,
925 { input_vdex, output_vdex, "--compact-dex-level=fast"},
926 /* expect_success */ true,
927 /* use_fd */ true);
928 EXPECT_GT(vdex_file1->GetLength(), 0u);
929 }
930
931 // Unquicken by running the verify compiler filter on the vdex file.
932 {
933 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
934 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2->Fd());
935 GenerateOdexForTest(dex_location,
936 odex_location2,
937 CompilerFilter::kVerify,
938 { input_vdex, output_vdex, "--compact-dex-level=none"},
939 /* expect_success */ true,
940 /* use_fd */ true);
941 }
942 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
943 ASSERT_EQ(vdex_file2->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
944 CheckResult(dex_location, odex_location2);
945 ASSERT_TRUE(success_);
946 }
947
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100948 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
949 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +0100950 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
951 odex_location.c_str(),
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100952 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100953 /* requested_base */ nullptr,
954 /* executable */ false,
955 /* low_4gb */ false,
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100956 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +0100957 /* reservation */ nullptr,
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100958 &error_msg));
959 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
960 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
961
962 // Iterate over the dex files and ensure there is no quickened instruction.
963 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
964 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Mathieu Chartier2242ef12018-07-23 18:14:13 -0700965 for (ClassAccessor accessor : dex_file->GetClasses()) {
966 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
967 for (const DexInstructionPcPair& inst : method.GetInstructions()) {
968 ASSERT_FALSE(inst->IsQuickened()) << inst->Opcode() << " " << output_;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100969 }
970 }
971 }
972 }
973 }
974};
975
976TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
977 RunUnquickenMultiDex();
978}
979
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800980TEST_F(Dex2oatUnquickenTest, UnquickenMultiDexCDex) {
981 RunUnquickenMultiDexCDex();
982}
983
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800984class Dex2oatWatchdogTest : public Dex2oatTest {
985 protected:
986 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
987 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
988 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
989
990 Copy(GetTestDexFileName(), dex_location);
991
992 std::vector<std::string> copy(extra_args);
993
994 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
995 copy.push_back("--swap-file=" + swap_location);
Andreas Gampe22fa3762017-10-23 20:58:12 -0700996 copy.push_back("-j512"); // Excessive idle threads just slow down dex2oat.
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800997 GenerateOdexForTest(dex_location,
998 odex_location,
999 CompilerFilter::kSpeed,
1000 copy,
1001 expect_success);
1002 }
1003
1004 std::string GetTestDexFileName() {
1005 return GetDexSrc1();
1006 }
1007};
1008
1009TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
1010 // Check with default.
1011 RunTest(true);
1012
1013 // Check with ten minutes.
1014 RunTest(true, { "--watchdog-timeout=600000" });
1015}
1016
1017TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Andreas Gampe80ddf272018-01-11 09:41:00 -08001018 // The watchdog is independent of dex2oat and will not delete intermediates. It is possible
1019 // that the compilation succeeds and the file is completely written by the time the watchdog
1020 // kills dex2oat (but the dex2oat threads must have been scheduled pretty badly).
1021 test_accepts_odex_file_on_failure = true;
1022
Andreas Gampe2e8a2562017-01-18 20:39:02 -08001023 // Check with ten milliseconds.
1024 RunTest(false, { "--watchdog-timeout=10" });
1025}
1026
Andreas Gampef7882972017-03-20 16:35:24 -07001027class Dex2oatReturnCodeTest : public Dex2oatTest {
1028 protected:
1029 int RunTest(const std::vector<std::string>& extra_args = {}) {
1030 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1031 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1032
1033 Copy(GetTestDexFileName(), dex_location);
1034
1035 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001036 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -07001037 odex_location,
1038 CompilerFilter::kSpeed,
1039 &error_msg,
1040 extra_args);
1041 }
1042
1043 std::string GetTestDexFileName() {
1044 return GetDexSrc1();
1045 }
1046};
1047
1048TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -07001049 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -07001050 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1051 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1052}
1053
Calin Juravle1ce70852017-06-28 10:59:03 -07001054class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1055 protected:
1056 void RunTest(const char* class_loader_context,
1057 const char* expected_classpath_key,
1058 bool expected_success,
1059 bool use_second_source = false) {
1060 std::string dex_location = GetUsedDexLocation();
1061 std::string odex_location = GetUsedOatLocation();
1062
1063 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1064
1065 std::string error_msg;
1066 std::vector<std::string> extra_args;
1067 if (class_loader_context != nullptr) {
1068 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1069 }
1070 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1071 ASSERT_TRUE(expected_classpath_key != nullptr);
1072 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1073 ASSERT_TRUE(classpath != nullptr);
1074 ASSERT_STREQ(expected_classpath_key, classpath);
1075 };
1076
1077 GenerateOdexForTest(dex_location,
1078 odex_location,
1079 CompilerFilter::kQuicken,
1080 extra_args,
1081 expected_success,
1082 /*use_fd*/ false,
1083 check_oat);
1084 }
1085
1086 std::string GetUsedDexLocation() {
1087 return GetScratchDir() + "/Context.jar";
1088 }
1089
1090 std::string GetUsedOatLocation() {
1091 return GetOdexDir() + "/Context.odex";
1092 }
1093
Calin Juravle7b0648a2017-07-07 18:40:50 -07001094 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001095};
1096
1097TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1098 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1099}
1100
1101TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1102 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1103}
1104
1105TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1106 RunTest(OatFile::kSpecialSharedLibrary,
1107 OatFile::kSpecialSharedLibrary,
1108 /*expected_success*/ true);
1109}
1110
1111TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1112 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1113 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1114}
1115
1116TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1117 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001118
1119 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001120 std::string expected_classpath_key = "PCL[" +
1121 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001122 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1123}
1124
1125TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1126 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1127 Copy(GetStrippedDexSrc1(), stripped_classpath);
1128
1129 std::string context = "PCL[" + stripped_classpath + "]";
1130 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001131 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001132}
1133
1134TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1135 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1136 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1137
1138 Copy(GetDexSrc1(), stripped_classpath);
1139
1140 GenerateOdexForTest(stripped_classpath,
1141 odex_for_classpath,
1142 CompilerFilter::kQuicken,
1143 {},
1144 true);
1145
1146 // Strip the dex file
1147 Copy(GetStrippedDexSrc1(), stripped_classpath);
1148
1149 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001150 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001151 {
1152 // Open the oat file to get the expected classpath.
Nicolas Geoffray29742602017-12-14 10:09:03 +00001153 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false, false);
Calin Juravle1ce70852017-06-28 10:59:03 -07001154 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1155 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1156 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001157 expected_classpath_key = "PCL[";
1158 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1159 if (i > 0) {
1160 expected_classpath_key + ":";
1161 }
1162 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1163 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1164 }
1165 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001166 }
1167
1168 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001169 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001170 /*expected_success*/ true,
1171 /*use_second_source*/ true);
1172}
1173
1174TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1175 std::string context = "PCL[does_not_exists.dex]";
1176 // Expect an empty context because stripped dex files cannot be open.
1177 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1178}
1179
Calin Juravlec79470d2017-07-12 17:37:42 -07001180TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1181 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1182 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1183
1184 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1185 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1186 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1187 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1188
1189 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1190}
1191
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001192class Dex2oatDeterminism : public Dex2oatTest {};
1193
1194TEST_F(Dex2oatDeterminism, UnloadCompile) {
1195 if (!kUseReadBarrier &&
1196 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1197 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1198 LOG(INFO) << "Test requires determinism support.";
1199 return;
1200 }
1201 Runtime* const runtime = Runtime::Current();
1202 std::string out_dir = GetScratchDir();
1203 const std::string base_oat_name = out_dir + "/base.oat";
1204 const std::string base_vdex_name = out_dir + "/base.vdex";
1205 const std::string unload_oat_name = out_dir + "/unload.oat";
1206 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1207 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1208 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1209 const std::string app_image_name = out_dir + "/unload.art";
1210 std::string error_msg;
1211 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1212 ASSERT_GT(spaces.size(), 0u);
1213 const std::string image_location = spaces[0]->GetImageLocation();
1214 // Without passing in an app image, it will unload in between compilations.
1215 const int res = GenerateOdexForTestWithStatus(
1216 GetLibCoreDexFileNames(),
1217 base_oat_name,
1218 CompilerFilter::Filter::kQuicken,
1219 &error_msg,
1220 {"--force-determinism", "--avoid-storing-invocation"});
1221 EXPECT_EQ(res, 0);
1222 Copy(base_oat_name, unload_oat_name);
1223 Copy(base_vdex_name, unload_vdex_name);
1224 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1225 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1226 ASSERT_TRUE(unload_oat != nullptr);
1227 ASSERT_TRUE(unload_vdex != nullptr);
1228 EXPECT_GT(unload_oat->GetLength(), 0u);
1229 EXPECT_GT(unload_vdex->GetLength(), 0u);
1230 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1231 // the same.
1232 const int res2 = GenerateOdexForTestWithStatus(
1233 GetLibCoreDexFileNames(),
1234 base_oat_name,
1235 CompilerFilter::Filter::kQuicken,
1236 &error_msg,
1237 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1238 EXPECT_EQ(res2, 0);
1239 Copy(base_oat_name, no_unload_oat_name);
1240 Copy(base_vdex_name, no_unload_vdex_name);
1241 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1242 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1243 ASSERT_TRUE(no_unload_oat != nullptr);
1244 ASSERT_TRUE(no_unload_vdex != nullptr);
1245 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1246 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1247 // Verify that both of the files are the same (odex and vdex).
1248 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1249 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1250 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1251 << unload_oat_name << " " << no_unload_oat_name;
1252 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1253 << unload_vdex_name << " " << no_unload_vdex_name;
1254 // App image file.
1255 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1256 ASSERT_TRUE(app_image_file != nullptr);
1257 EXPECT_GT(app_image_file->GetLength(), 0u);
1258}
1259
Mathieu Chartier120aa282017-08-05 16:03:03 -07001260// Test that dexlayout section info is correctly written to the oat file for profile based
1261// compilation.
1262TEST_F(Dex2oatTest, LayoutSections) {
1263 using Hotness = ProfileCompilationInfo::MethodHotness;
1264 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1265 ScratchFile profile_file;
1266 // We can only layout method indices with code items, figure out which ones have this property
1267 // first.
1268 std::vector<uint16_t> methods;
1269 {
1270 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1271 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001272 ClassAccessor accessor(*dex, *dex->FindClassDef(type_idx));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001273 std::set<size_t> code_item_offsets;
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001274 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1275 const uint16_t method_idx = method.GetIndex();
1276 const size_t code_item_offset = method.GetCodeItemOffset();
Mathieu Chartier120aa282017-08-05 16:03:03 -07001277 if (code_item_offsets.insert(code_item_offset).second) {
1278 // Unique code item, add the method index.
1279 methods.push_back(method_idx);
1280 }
1281 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001282 }
1283 ASSERT_GE(methods.size(), 8u);
1284 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1285 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1286 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1287 // Here, we build the profile from the method lists.
1288 ProfileCompilationInfo info;
1289 info.AddMethodsForDex(
1290 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1291 dex.get(),
1292 hot_methods.begin(),
1293 hot_methods.end());
1294 info.AddMethodsForDex(
1295 Hotness::kFlagStartup,
1296 dex.get(),
1297 startup_methods.begin(),
1298 startup_methods.end());
1299 info.AddMethodsForDex(
1300 Hotness::kFlagPostStartup,
1301 dex.get(),
1302 post_methods.begin(),
1303 post_methods.end());
1304 for (uint16_t id : hot_methods) {
1305 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1306 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1307 }
1308 for (uint16_t id : startup_methods) {
1309 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1310 }
1311 for (uint16_t id : post_methods) {
1312 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1313 }
1314 // Save the profile since we want to use it with dex2oat to produce an oat file.
1315 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1316 // Generate a profile based odex.
1317 const std::string dir = GetScratchDir();
1318 const std::string oat_filename = dir + "/base.oat";
1319 const std::string vdex_filename = dir + "/base.vdex";
1320 std::string error_msg;
1321 const int res = GenerateOdexForTestWithStatus(
1322 {dex->GetLocation()},
1323 oat_filename,
1324 CompilerFilter::Filter::kQuicken,
1325 &error_msg,
1326 {"--profile-file=" + profile_file.GetFilename()});
1327 EXPECT_EQ(res, 0);
1328
1329 // Open our generated oat file.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001330 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
1331 oat_filename.c_str(),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001332 oat_filename.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001333 /* requested_base */ nullptr,
1334 /* executable */ false,
1335 /* low_4gb */ false,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001336 dex->GetLocation().c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001337 /* reservation */ nullptr,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001338 &error_msg));
1339 ASSERT_TRUE(odex_file != nullptr);
1340 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1341 ASSERT_EQ(oat_dex_files.size(), 1u);
1342 // Check that the code sections match what we expect.
1343 for (const OatDexFile* oat_dex : oat_dex_files) {
1344 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1345 // Testing of logging the sections.
1346 ASSERT_TRUE(sections != nullptr);
1347 LOG(INFO) << *sections;
1348
1349 // Load the sections into temporary variables for convenience.
1350 const DexLayoutSection& code_section =
1351 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1352 const DexLayoutSection::Subsection& section_hot_code =
1353 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1354 const DexLayoutSection::Subsection& section_sometimes_used =
1355 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1356 const DexLayoutSection::Subsection& section_startup_only =
1357 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1358 const DexLayoutSection::Subsection& section_unused =
1359 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1360
1361 // All the sections should be non-empty.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001362 EXPECT_GT(section_hot_code.Size(), 0u);
1363 EXPECT_GT(section_sometimes_used.Size(), 0u);
1364 EXPECT_GT(section_startup_only.Size(), 0u);
1365 EXPECT_GT(section_unused.Size(), 0u);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001366
1367 // Open the dex file since we need to peek at the code items to verify the layout matches what
1368 // we expect.
1369 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1370 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1371 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1372 ASSERT_TRUE(type_id != nullptr);
1373 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1374 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1375 ASSERT_TRUE(class_def != nullptr);
1376
1377 // Count how many code items are for each category, there should be at least one per category.
1378 size_t hot_count = 0;
1379 size_t post_startup_count = 0;
1380 size_t startup_count = 0;
1381 size_t unused_count = 0;
1382 // Visit all of the methdos of the main class and cross reference the method indices to their
1383 // corresponding code item offsets to verify the layout.
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001384 ClassAccessor accessor(*dex_file, *class_def);
1385 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1386 const size_t method_idx = method.GetIndex();
1387 const size_t code_item_offset = method.GetCodeItemOffset();
Mathieu Chartier120aa282017-08-05 16:03:03 -07001388 const bool is_hot = ContainsElement(hot_methods, method_idx);
1389 const bool is_startup = ContainsElement(startup_methods, method_idx);
1390 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1391 if (is_hot) {
1392 // Hot is highest precedence, check that the hot methods are in the hot section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001393 EXPECT_TRUE(section_hot_code.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001394 ++hot_count;
1395 } else if (is_post_startup) {
1396 // Post startup is sometimes used section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001397 EXPECT_TRUE(section_sometimes_used.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001398 ++post_startup_count;
1399 } else if (is_startup) {
1400 // Startup at this point means not hot or post startup, these must be startup only then.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001401 EXPECT_TRUE(section_startup_only.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001402 ++startup_count;
1403 } else {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001404 if (section_unused.Contains(code_item_offset)) {
Alan Leung9595fd32017-10-17 17:08:19 -07001405 // If no flags are set, the method should be unused ...
1406 ++unused_count;
1407 } else {
1408 // or this method is part of the last code item and the end is 4 byte aligned.
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001409 for (const ClassAccessor::Method& method2 : accessor.GetMethods()) {
1410 EXPECT_LE(method2.GetCodeItemOffset(), code_item_offset);
Alan Leung9595fd32017-10-17 17:08:19 -07001411 }
1412 uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx);
1413 EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u);
1414 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001415 }
1416 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001417 EXPECT_GT(hot_count, 0u);
1418 EXPECT_GT(post_startup_count, 0u);
1419 EXPECT_GT(startup_count, 0u);
1420 EXPECT_GT(unused_count, 0u);
1421 }
1422}
1423
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001424// Test that generating compact dex works.
1425TEST_F(Dex2oatTest, GenerateCompactDex) {
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001426 // Generate a compact dex based odex.
1427 const std::string dir = GetScratchDir();
1428 const std::string oat_filename = dir + "/base.oat";
1429 const std::string vdex_filename = dir + "/base.vdex";
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001430 const std::string dex_location = GetTestDexFileName("MultiDex");
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001431 std::string error_msg;
1432 const int res = GenerateOdexForTestWithStatus(
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001433 { dex_location },
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001434 oat_filename,
1435 CompilerFilter::Filter::kQuicken,
1436 &error_msg,
1437 {"--compact-dex-level=fast"});
1438 EXPECT_EQ(res, 0);
1439 // Open our generated oat file.
Nicolas Geoffray30025092018-04-19 14:43:29 +01001440 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
1441 oat_filename.c_str(),
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001442 oat_filename.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001443 /* requested_base */ nullptr,
1444 /* executable */ false,
1445 /* low_4gb */ false,
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001446 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001447 /* reservation */ nullptr,
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001448 &error_msg));
1449 ASSERT_TRUE(odex_file != nullptr);
1450 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001451 ASSERT_GT(oat_dex_files.size(), 1u);
1452 // Check that each dex is a compact dex file.
1453 std::vector<std::unique_ptr<const CompactDexFile>> compact_dex_files;
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001454 for (const OatDexFile* oat_dex : oat_dex_files) {
1455 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1456 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1457 ASSERT_TRUE(dex_file->IsCompactDexFile());
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001458 compact_dex_files.push_back(
1459 std::unique_ptr<const CompactDexFile>(dex_file.release()->AsCompactDexFile()));
1460 }
1461 for (const std::unique_ptr<const CompactDexFile>& dex_file : compact_dex_files) {
1462 // Test that every code item is in the owned section.
1463 const CompactDexFile::Header& header = dex_file->GetHeader();
1464 EXPECT_LE(header.OwnedDataBegin(), header.OwnedDataEnd());
1465 EXPECT_LE(header.OwnedDataBegin(), header.data_size_);
1466 EXPECT_LE(header.OwnedDataEnd(), header.data_size_);
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001467 for (ClassAccessor accessor : dex_file->GetClasses()) {
1468 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1469 if (method.GetCodeItemOffset() != 0u) {
1470 ASSERT_GE(method.GetCodeItemOffset(), header.OwnedDataBegin());
1471 ASSERT_LT(method.GetCodeItemOffset(), header.OwnedDataEnd());
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001472 }
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001473 }
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001474 }
1475 // Test that the owned sections don't overlap.
1476 for (const std::unique_ptr<const CompactDexFile>& other_dex : compact_dex_files) {
1477 if (dex_file != other_dex) {
1478 ASSERT_TRUE(
1479 (dex_file->GetHeader().OwnedDataBegin() >= other_dex->GetHeader().OwnedDataEnd()) ||
1480 (dex_file->GetHeader().OwnedDataEnd() <= other_dex->GetHeader().OwnedDataBegin()));
1481 }
1482 }
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001483 }
1484}
1485
Andreas Gampef39208f2017-10-19 15:06:59 -07001486class Dex2oatVerifierAbort : public Dex2oatTest {};
1487
1488TEST_F(Dex2oatVerifierAbort, HardFail) {
1489 // Use VerifierDeps as it has hard-failing classes.
1490 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps"));
1491 std::string out_dir = GetScratchDir();
1492 const std::string base_oat_name = out_dir + "/base.oat";
1493 std::string error_msg;
1494 const int res_fail = GenerateOdexForTestWithStatus(
1495 {dex->GetLocation()},
1496 base_oat_name,
1497 CompilerFilter::Filter::kQuicken,
1498 &error_msg,
1499 {"--abort-on-hard-verifier-error"});
1500 EXPECT_NE(0, res_fail);
1501
1502 const int res_no_fail = GenerateOdexForTestWithStatus(
1503 {dex->GetLocation()},
1504 base_oat_name,
1505 CompilerFilter::Filter::kQuicken,
1506 &error_msg,
1507 {"--no-abort-on-hard-verifier-error"});
1508 EXPECT_EQ(0, res_no_fail);
1509}
1510
1511TEST_F(Dex2oatVerifierAbort, SoftFail) {
1512 // Use VerifierDepsMulti as it has hard-failing classes.
1513 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDepsMulti"));
1514 std::string out_dir = GetScratchDir();
1515 const std::string base_oat_name = out_dir + "/base.oat";
1516 std::string error_msg;
1517 const int res_fail = GenerateOdexForTestWithStatus(
1518 {dex->GetLocation()},
1519 base_oat_name,
1520 CompilerFilter::Filter::kQuicken,
1521 &error_msg,
1522 {"--abort-on-soft-verifier-error"});
1523 EXPECT_NE(0, res_fail);
1524
1525 const int res_no_fail = GenerateOdexForTestWithStatus(
1526 {dex->GetLocation()},
1527 base_oat_name,
1528 CompilerFilter::Filter::kQuicken,
1529 &error_msg,
1530 {"--no-abort-on-soft-verifier-error"});
1531 EXPECT_EQ(0, res_no_fail);
1532}
1533
Andreas Gampecac31ad2017-11-06 20:01:17 -08001534class Dex2oatDedupeCode : public Dex2oatTest {};
1535
1536TEST_F(Dex2oatDedupeCode, DedupeTest) {
1537 // Use MyClassNatives. It has lots of native methods that will produce deduplicate-able code.
1538 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MyClassNatives"));
1539 std::string out_dir = GetScratchDir();
1540 const std::string base_oat_name = out_dir + "/base.oat";
1541 size_t no_dedupe_size = 0;
1542 GenerateOdexForTest(dex->GetLocation(),
1543 base_oat_name,
1544 CompilerFilter::Filter::kSpeed,
1545 { "--deduplicate-code=false" },
1546 true, // expect_success
1547 false, // use_fd
1548 [&no_dedupe_size](const OatFile& o) {
1549 no_dedupe_size = o.Size();
1550 });
1551
1552 size_t dedupe_size = 0;
1553 GenerateOdexForTest(dex->GetLocation(),
1554 base_oat_name,
1555 CompilerFilter::Filter::kSpeed,
1556 { "--deduplicate-code=true" },
1557 true, // expect_success
1558 false, // use_fd
1559 [&dedupe_size](const OatFile& o) {
1560 dedupe_size = o.Size();
1561 });
1562
1563 EXPECT_LT(dedupe_size, no_dedupe_size);
1564}
1565
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001566TEST_F(Dex2oatTest, UncompressedTest) {
1567 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MainUncompressed"));
1568 std::string out_dir = GetScratchDir();
1569 const std::string base_oat_name = out_dir + "/base.oat";
1570 GenerateOdexForTest(dex->GetLocation(),
1571 base_oat_name,
1572 CompilerFilter::Filter::kQuicken,
1573 { },
1574 true, // expect_success
1575 false, // use_fd
1576 [](const OatFile& o) {
1577 CHECK(!o.ContainsDexCode());
1578 });
1579}
1580
Mathieu Chartier700a9852018-02-06 18:27:38 -08001581TEST_F(Dex2oatTest, EmptyUncompressedDexTest) {
1582 std::string out_dir = GetScratchDir();
1583 const std::string base_oat_name = out_dir + "/base.oat";
1584 std::string error_msg;
1585 int status = GenerateOdexForTestWithStatus(
1586 { GetTestDexFileName("MainEmptyUncompressed") },
1587 base_oat_name,
1588 CompilerFilter::Filter::kQuicken,
1589 &error_msg,
1590 { },
1591 /*use_fd*/ false);
1592 // Expect to fail with code 1 and not SIGSEGV or SIGABRT.
1593 ASSERT_TRUE(WIFEXITED(status));
1594 ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg;
1595}
1596
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001597// Dex file that has duplicate methods have different code items and debug info.
1598static const char kDuplicateMethodInputDex[] =
1599 "ZGV4CjAzOQDEy8VPdj4qHpgPYFWtLCtOykfFP4kB8tGYDAAAcAAAAHhWNBIAAAAAAAAAANALAABI"
1600 "AAAAcAAAAA4AAACQAQAABQAAAMgBAAANAAAABAIAABkAAABsAgAABAAAADQDAADgCAAAuAMAADgI"
1601 "AABCCAAASggAAE8IAABcCAAAaggAAHkIAACICAAAlggAAKQIAACyCAAAwAgAAM4IAADcCAAA6ggA"
1602 "APgIAAD7CAAA/wgAABcJAAAuCQAARQkAAFQJAAB4CQAAmAkAALsJAADSCQAA5gkAAPoJAAAVCgAA"
1603 "KQoAADsKAABCCgAASgoAAFIKAABbCgAAZAoAAGwKAAB0CgAAfAoAAIQKAACMCgAAlAoAAJwKAACk"
1604 "CgAArQoAALcKAADACgAAwwoAAMcKAADcCgAA6QoAAPEKAAD3CgAA/QoAAAMLAAAJCwAAEAsAABcL"
1605 "AAAdCwAAIwsAACkLAAAvCwAANQsAADsLAABBCwAARwsAAE0LAABSCwAAWwsAAF4LAABoCwAAbwsA"
1606 "ABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAC4AAAAwAAAA"
1607 "DwAAAAkAAAAAAAAAEAAAAAoAAACoBwAALgAAAAwAAAAAAAAALwAAAAwAAACoBwAALwAAAAwAAACw"
1608 "BwAAAgAJADUAAAACAAkANgAAAAIACQA3AAAAAgAJADgAAAACAAkAOQAAAAIACQA6AAAAAgAJADsA"
1609 "AAACAAkAPAAAAAIACQA9AAAAAgAJAD4AAAACAAkAPwAAAAIACQBAAAAACwAHAEIAAAAAAAIAAQAA"
1610 "AAAAAwAeAAAAAQACAAEAAAABAAMAHgAAAAIAAgAAAAAAAgACAAEAAAADAAIAAQAAAAMAAgAfAAAA"
1611 "AwACACAAAAADAAIAIQAAAAMAAgAiAAAAAwACACMAAAADAAIAJAAAAAMAAgAlAAAAAwACACYAAAAD"
1612 "AAIAJwAAAAMAAgAoAAAAAwACACkAAAADAAIAKgAAAAMABAA0AAAABwADAEMAAAAIAAIAAQAAAAoA"
1613 "AgABAAAACgABADIAAAAKAAAARQAAAAAAAAAAAAAACAAAAAAAAAAdAAAAaAcAALYHAAAAAAAAAQAA"
1614 "AAAAAAAIAAAAAAAAAB0AAAB4BwAAxAcAAAAAAAACAAAAAAAAAAgAAAAAAAAAHQAAAIgHAADSBwAA"
1615 "AAAAAAMAAAAAAAAACAAAAAAAAAAdAAAAmAcAAPoHAAAAAAAAAAAAAAEAAAAAAAAArAYAADEAAAAa"
1616 "AAMAaQAAABoABABpAAEAGgAHAGkABAAaAAgAaQAFABoACQBpAAYAGgAKAGkABwAaAAsAaQAIABoA"
1617 "DABpAAkAGgANAGkACgAaAA4AaQALABoABQBpAAIAGgAGAGkAAwAOAAAAAQABAAEAAACSBgAABAAA"
1618 "AHAQFQAAAA4ABAABAAIAAACWBgAAFwAAAGIADAAiAQoAcBAWAAEAGgICAG4gFwAhAG4gFwAxAG4Q"
1619 "GAABAAwBbiAUABAADgAAAAEAAQABAAAAngYAAAQAAABwEBUAAAAOAAIAAQACAAAAogYAAAYAAABi"
1620 "AAwAbiAUABAADgABAAEAAQAAAKgGAAAEAAAAcBAVAAAADgABAAEAAQAAALsGAAAEAAAAcBAVAAAA"
1621 "DgABAAAAAQAAAL8GAAAGAAAAYgAAAHEQAwAAAA4AAQAAAAEAAADEBgAABgAAAGIAAQBxEAMAAAAO"
1622 "AAEAAAABAAAA8QYAAAYAAABiAAIAcRABAAAADgABAAAAAQAAAPYGAAAGAAAAYgADAHEQAwAAAA4A"
1623 "AQAAAAEAAADJBgAABgAAAGIABABxEAMAAAAOAAEAAAABAAAAzgYAAAYAAABiAAEAcRADAAAADgAB"
1624 "AAAAAQAAANMGAAAGAAAAYgAGAHEQAwAAAA4AAQAAAAEAAADYBgAABgAAAGIABwBxEAMAAAAOAAEA"
1625 "AAABAAAA3QYAAAYAAABiAAgAcRABAAAADgABAAAAAQAAAOIGAAAGAAAAYgAJAHEQAwAAAA4AAQAA"
1626 "AAEAAADnBgAABgAAAGIACgBxEAMAAAAOAAEAAAABAAAA7AYAAAYAAABiAAsAcRABAAAADgABAAEA"
1627 "AAAAAPsGAAAlAAAAcQAHAAAAcQAIAAAAcQALAAAAcQAMAAAAcQANAAAAcQAOAAAAcQAPAAAAcQAQ"
1628 "AAAAcQARAAAAcQASAAAAcQAJAAAAcQAKAAAADgAnAA4AKQFFDgEWDwAhAA4AIwFFDloAEgAOABMA"
1629 "DktLS0tLS0tLS0tLABEADgAuAA5aADIADloANgAOWgA6AA5aAD4ADloAQgAOWgBGAA5aAEoADloA"
1630 "TgAOWgBSAA5aAFYADloAWgAOWgBeATQOPDw8PDw8PDw8PDw8AAIEAUYYAwIFAjEECEEXLAIFAjEE"
1631 "CEEXKwIFAjEECEEXLQIGAUYcAxgAGAEYAgAAAAIAAAAMBwAAEgcAAAIAAAAMBwAAGwcAAAIAAAAM"
1632 "BwAAJAcAAAEAAAAtBwAAPAcAAAAAAAAAAAAAAAAAAEgHAAAAAAAAAAAAAAAAAABUBwAAAAAAAAAA"
1633 "AAAAAAAAYAcAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAQAAAA0AAAACAACAgASsCAEIxAgAAAIAAoCA"
1634 "BIQJAQicCQwAAgAACQEJAQkBCQEJAQkBCQEJAQkBCQEJAQkEiIAEuAcBgIAEuAkAAA4ABoCABNAJ"
1635 "AQnoCQAJhAoACaAKAAm8CgAJ2AoACfQKAAmQCwAJrAsACcgLAAnkCwAJgAwACZwMAAm4DAg8Y2xp"
1636 "bml0PgAGPGluaXQ+AANBQUEAC0hlbGxvIFdvcmxkAAxIZWxsbyBXb3JsZDEADUhlbGxvIFdvcmxk"
1637 "MTAADUhlbGxvIFdvcmxkMTEADEhlbGxvIFdvcmxkMgAMSGVsbG8gV29ybGQzAAxIZWxsbyBXb3Js"
1638 "ZDQADEhlbGxvIFdvcmxkNQAMSGVsbG8gV29ybGQ2AAxIZWxsbyBXb3JsZDcADEhlbGxvIFdvcmxk"
1639 "OAAMSGVsbG8gV29ybGQ5AAFMAAJMTAAWTE1hbnlNZXRob2RzJFByaW50ZXIyOwAVTE1hbnlNZXRo"
1640 "b2RzJFByaW50ZXI7ABVMTWFueU1ldGhvZHMkU3RyaW5nczsADUxNYW55TWV0aG9kczsAIkxkYWx2"
1641 "aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5lckNs"
1642 "YXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABVMamF2YS9pby9QcmludFN0"
1643 "cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABlMamF2YS9sYW5n"
1644 "L1N0cmluZ0J1aWxkZXI7ABJMamF2YS9sYW5nL1N5c3RlbTsAEE1hbnlNZXRob2RzLmphdmEABVBy"
1645 "aW50AAZQcmludDAABlByaW50MQAHUHJpbnQxMAAHUHJpbnQxMQAGUHJpbnQyAAZQcmludDMABlBy"
1646 "aW50NAAGUHJpbnQ1AAZQcmludDYABlByaW50NwAGUHJpbnQ4AAZQcmludDkAB1ByaW50ZXIACFBy"
1647 "aW50ZXIyAAdTdHJpbmdzAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwALYWNjZXNzRmxhZ3MA"
1648 "BmFwcGVuZAAEYXJncwAEbWFpbgAEbXNnMAAEbXNnMQAFbXNnMTAABW1zZzExAARtc2cyAARtc2cz"
1649 "AARtc2c0AARtc2c1AARtc2c2AARtc2c3AARtc2c4AARtc2c5AARuYW1lAANvdXQAB3ByaW50bG4A"
1650 "AXMACHRvU3RyaW5nAAV2YWx1ZQBffn5EOHsibWluLWFwaSI6MTAwMDAsInNoYS0xIjoiZmViODZj"
1651 "MDA2ZWZhY2YxZDc5ODRiODVlMTc5MGZlZjdhNzY3YWViYyIsInZlcnNpb24iOiJ2MS4xLjUtZGV2"
1652 "In0AEAAAAAAAAAABAAAAAAAAAAEAAABIAAAAcAAAAAIAAAAOAAAAkAEAAAMAAAAFAAAAyAEAAAQA"
1653 "AAANAAAABAIAAAUAAAAZAAAAbAIAAAYAAAAEAAAANAMAAAEgAAAUAAAAuAMAAAMgAAAUAAAAkgYA"
1654 "AAQgAAAFAAAADAcAAAMQAAAEAAAAOQcAAAYgAAAEAAAAaAcAAAEQAAACAAAAqAcAAAAgAAAEAAAA"
1655 "tgcAAAIgAABIAAAAOAgAAAAQAAABAAAA0AsAAAAAAAA=";
1656
1657static void WriteBase64ToFile(const char* base64, File* file) {
1658 // Decode base64.
1659 CHECK(base64 != nullptr);
1660 size_t length;
1661 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
1662 CHECK(bytes != nullptr);
1663 if (!file->WriteFully(bytes.get(), length)) {
1664 PLOG(FATAL) << "Failed to write base64 as file";
1665 }
1666}
1667
1668TEST_F(Dex2oatTest, CompactDexGenerationFailure) {
1669 ScratchFile temp_dex;
1670 WriteBase64ToFile(kDuplicateMethodInputDex, temp_dex.GetFile());
1671 std::string out_dir = GetScratchDir();
1672 const std::string oat_filename = out_dir + "/base.oat";
1673 // The dex won't pass the method verifier, only use the verify filter.
1674 GenerateOdexForTest(temp_dex.GetFilename(),
1675 oat_filename,
1676 CompilerFilter::Filter::kVerify,
1677 { },
1678 true, // expect_success
1679 false, // use_fd
1680 [](const OatFile& o) {
1681 CHECK(o.ContainsDexCode());
1682 });
1683 // Open our generated oat file.
1684 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01001685 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
1686 oat_filename.c_str(),
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001687 oat_filename.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001688 /* requested_base */ nullptr,
1689 /* executable */ false,
1690 /* low_4gb */ false,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001691 temp_dex.GetFilename().c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001692 /* reservation */ nullptr,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001693 &error_msg));
1694 ASSERT_TRUE(odex_file != nullptr);
1695 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1696 ASSERT_EQ(oat_dex_files.size(), 1u);
1697 // The dexes should have failed to convert to compact dex.
1698 for (const OatDexFile* oat_dex : oat_dex_files) {
1699 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1700 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1701 ASSERT_TRUE(!dex_file->IsCompactDexFile());
1702 }
1703}
1704
Mathieu Chartiercda83be2018-03-01 23:55:55 -08001705TEST_F(Dex2oatTest, CompactDexGenerationFailureMultiDex) {
1706 // Create a multidex file with only one dex that gets rejected for cdex conversion.
1707 ScratchFile apk_file;
1708 {
Josh Gao7f00f5a2018-08-30 17:26:49 -07001709 FILE* file = fdopen(dup(apk_file.GetFd()), "w+b");
Mathieu Chartiercda83be2018-03-01 23:55:55 -08001710 ZipWriter writer(file);
1711 // Add vdex to zip.
1712 writer.StartEntry("classes.dex", ZipWriter::kCompress);
1713 size_t length = 0u;
1714 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(kDuplicateMethodInputDex, &length));
1715 ASSERT_GE(writer.WriteBytes(&bytes[0], length), 0);
1716 writer.FinishEntry();
1717 writer.StartEntry("classes2.dex", ZipWriter::kCompress);
1718 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1719 ASSERT_GE(writer.WriteBytes(dex->Begin(), dex->Size()), 0);
1720 writer.FinishEntry();
1721 writer.Finish();
1722 ASSERT_EQ(apk_file.GetFile()->Flush(), 0);
1723 }
Andreas Gampebc802de2018-06-20 17:24:11 -07001724 const std::string& dex_location = apk_file.GetFilename();
Mathieu Chartiercda83be2018-03-01 23:55:55 -08001725 const std::string odex_location = GetOdexDir() + "/output.odex";
1726 GenerateOdexForTest(dex_location,
1727 odex_location,
1728 CompilerFilter::kQuicken,
1729 { "--compact-dex-level=fast" },
1730 true);
1731}
1732
Andreas Gampe25419b52018-02-08 21:30:26 -08001733TEST_F(Dex2oatTest, StderrLoggerOutput) {
1734 std::string dex_location = GetScratchDir() + "/Dex2OatStderrLoggerTest.jar";
1735 std::string odex_location = GetOdexDir() + "/Dex2OatStderrLoggerTest.odex";
1736
1737 // Test file doesn't matter.
1738 Copy(GetDexSrc1(), dex_location);
1739
1740 GenerateOdexForTest(dex_location,
1741 odex_location,
1742 CompilerFilter::kQuicken,
1743 { "--runtime-arg", "-Xuse-stderr-logger" },
1744 true);
1745 // Look for some random part of dex2oat logging. With the stderr logger this should be captured,
1746 // even on device.
1747 EXPECT_NE(std::string::npos, output_.find("dex2oat took"));
1748}
1749
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001750TEST_F(Dex2oatTest, VerifyCompilationReason) {
1751 std::string dex_location = GetScratchDir() + "/Dex2OatCompilationReason.jar";
1752 std::string odex_location = GetOdexDir() + "/Dex2OatCompilationReason.odex";
1753
1754 // Test file doesn't matter.
1755 Copy(GetDexSrc1(), dex_location);
1756
1757 GenerateOdexForTest(dex_location,
1758 odex_location,
1759 CompilerFilter::kVerify,
1760 { "--compilation-reason=install" },
1761 true);
1762 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01001763 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
1764 odex_location.c_str(),
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001765 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001766 /* requested_base */ nullptr,
1767 /* executable */ false,
1768 /* low_4gb */ false,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001769 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001770 /* reservation */ nullptr,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001771 &error_msg));
1772 ASSERT_TRUE(odex_file != nullptr);
1773 ASSERT_STREQ("install", odex_file->GetCompilationReason());
1774}
1775
1776TEST_F(Dex2oatTest, VerifyNoCompilationReason) {
1777 std::string dex_location = GetScratchDir() + "/Dex2OatNoCompilationReason.jar";
1778 std::string odex_location = GetOdexDir() + "/Dex2OatNoCompilationReason.odex";
1779
1780 // Test file doesn't matter.
1781 Copy(GetDexSrc1(), dex_location);
1782
1783 GenerateOdexForTest(dex_location,
1784 odex_location,
1785 CompilerFilter::kVerify,
1786 {},
1787 true);
1788 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01001789 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
1790 odex_location.c_str(),
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001791 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001792 /* requested_base */ nullptr,
1793 /* executable */ false,
1794 /* low_4gb */ false,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001795 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001796 /* reservation */ nullptr,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001797 &error_msg));
1798 ASSERT_TRUE(odex_file != nullptr);
1799 ASSERT_EQ(nullptr, odex_file->GetCompilationReason());
1800}
1801
Mathieu Chartier792111c2018-02-15 13:02:15 -08001802TEST_F(Dex2oatTest, DontExtract) {
1803 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1804 std::string error_msg;
1805 const std::string out_dir = GetScratchDir();
1806 const std::string dex_location = dex->GetLocation();
1807 const std::string odex_location = out_dir + "/base.oat";
1808 const std::string vdex_location = out_dir + "/base.vdex";
1809 GenerateOdexForTest(dex_location,
1810 odex_location,
1811 CompilerFilter::Filter::kVerify,
1812 { "--copy-dex-files=false" },
1813 true, // expect_success
1814 false, // use_fd
1815 [](const OatFile&) {
1816 });
1817 {
1818 // Check the vdex doesn't have dex.
1819 std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_location.c_str(),
1820 /*writable*/ false,
1821 /*low_4gb*/ false,
1822 /*unquicken*/ false,
1823 &error_msg));
1824 ASSERT_TRUE(vdex != nullptr);
Nicolas Geoffray3a293552018-03-02 10:52:16 +00001825 EXPECT_FALSE(vdex->HasDexSection()) << output_;
Mathieu Chartier792111c2018-02-15 13:02:15 -08001826 }
Nicolas Geoffray30025092018-04-19 14:43:29 +01001827 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
1828 odex_location.c_str(),
Mathieu Chartier792111c2018-02-15 13:02:15 -08001829 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001830 /* requested_base */ nullptr,
1831 /* executable */ false,
1832 /* low_4gb */ false,
Mathieu Chartier792111c2018-02-15 13:02:15 -08001833 dex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01001834 /* reservation */ nullptr,
Mathieu Chartier792111c2018-02-15 13:02:15 -08001835 &error_msg));
1836 ASSERT_TRUE(odex_file != nullptr) << dex_location;
1837 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1838 ASSERT_EQ(oat_dex_files.size(), 1u);
1839 // Verify that the oat file can still open the dex files.
1840 for (const OatDexFile* oat_dex : oat_dex_files) {
1841 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1842 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1843 }
1844 // Create a dm file and use it to verify.
1845 // Add produced artifacts to a zip file that doesn't contain the classes.dex.
1846 ScratchFile dm_file;
1847 {
1848 std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_location.c_str()));
1849 ASSERT_TRUE(vdex_file != nullptr);
1850 ASSERT_GT(vdex_file->GetLength(), 0u);
Josh Gao7f00f5a2018-08-30 17:26:49 -07001851 FILE* file = fdopen(dup(dm_file.GetFd()), "w+b");
Mathieu Chartier792111c2018-02-15 13:02:15 -08001852 ZipWriter writer(file);
1853 auto write_all_bytes = [&](File* file) {
1854 std::unique_ptr<uint8_t[]> bytes(new uint8_t[file->GetLength()]);
1855 ASSERT_TRUE(file->ReadFully(&bytes[0], file->GetLength()));
1856 ASSERT_GE(writer.WriteBytes(&bytes[0], file->GetLength()), 0);
1857 };
1858 // Add vdex to zip.
1859 writer.StartEntry(VdexFile::kVdexNameInDmFile, ZipWriter::kCompress);
1860 write_all_bytes(vdex_file.get());
1861 writer.FinishEntry();
1862 writer.Finish();
1863 ASSERT_EQ(dm_file.GetFile()->Flush(), 0);
1864 }
1865
Mathieu Chartier2eabc612018-05-25 14:31:16 -07001866 auto generate_and_check = [&](CompilerFilter::Filter filter) {
1867 GenerateOdexForTest(dex_location,
1868 odex_location,
1869 filter,
1870 { "--dump-timings",
1871 "--dm-file=" + dm_file.GetFilename(),
1872 // Pass -Xuse-stderr-logger have dex2oat output in output_ on target.
1873 "--runtime-arg",
1874 "-Xuse-stderr-logger" },
1875 true, // expect_success
1876 false, // use_fd
1877 [](const OatFile& o) {
1878 CHECK(o.ContainsDexCode());
1879 });
1880 // Check the output for "Fast verify", this is printed from --dump-timings.
1881 std::istringstream iss(output_);
1882 std::string line;
1883 bool found_fast_verify = false;
1884 const std::string kFastVerifyString = "Fast Verify";
1885 while (std::getline(iss, line) && !found_fast_verify) {
1886 found_fast_verify = found_fast_verify || line.find(kFastVerifyString) != std::string::npos;
1887 }
1888 EXPECT_TRUE(found_fast_verify) << "Expected to find " << kFastVerifyString << "\n" << output_;
1889 };
1890
Mathieu Chartier792111c2018-02-15 13:02:15 -08001891 // Generate a quickened dex by using the input dm file to verify.
Mathieu Chartier2eabc612018-05-25 14:31:16 -07001892 generate_and_check(CompilerFilter::Filter::kQuicken);
1893 // Use verify compiler filter to sanity check that FastVerify works for that filter too.
1894 generate_and_check(CompilerFilter::Filter::kVerify);
Mathieu Chartier792111c2018-02-15 13:02:15 -08001895}
1896
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001897// Test that dex files with quickened opcodes aren't dequickened.
1898TEST_F(Dex2oatTest, QuickenedInput) {
1899 std::string error_msg;
1900 ScratchFile temp_dex;
1901 MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("ManyMethods"), [] (DexFile* dex) {
1902 bool mutated_successfully = false;
1903 // Change the dex instructions to make an opcode that spans past the end of the code item.
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001904 for (ClassAccessor accessor : dex->GetClasses()) {
1905 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1906 CodeItemInstructionAccessor instructions = method.GetInstructions();
1907 // Make a quickened instruction that doesn't run past the end of the code item.
1908 if (instructions.InsnsSizeInCodeUnits() > 2) {
1909 const_cast<Instruction&>(instructions.InstructionAt(0)).SetOpcode(
1910 Instruction::IGET_BYTE_QUICK);
1911 mutated_successfully = true;
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001912 }
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001913 }
1914 }
1915 CHECK(mutated_successfully)
1916 << "Failed to find candidate code item with only one code unit in last instruction.";
1917 });
1918
Andreas Gampebc802de2018-06-20 17:24:11 -07001919 const std::string& dex_location = temp_dex.GetFilename();
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001920 std::string odex_location = GetOdexDir() + "/quickened.odex";
1921 std::string vdex_location = GetOdexDir() + "/quickened.vdex";
1922 std::unique_ptr<File> vdex_output(OS::CreateEmptyFile(vdex_location.c_str()));
1923 // Quicken the dex
1924 {
1925 std::string input_vdex = "--input-vdex-fd=-1";
1926 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_output->Fd());
1927 GenerateOdexForTest(dex_location,
1928 odex_location,
1929 CompilerFilter::kQuicken,
1930 // Disable cdex since we want to compare against the original dex file
1931 // after unquickening.
1932 { input_vdex, output_vdex, kDisableCompactDex },
1933 /* expect_success */ true,
1934 /* use_fd */ true);
1935 }
1936 // Unquicken by running the verify compiler filter on the vdex file and verify it matches.
1937 std::string odex_location2 = GetOdexDir() + "/unquickened.odex";
1938 std::string vdex_location2 = GetOdexDir() + "/unquickened.vdex";
1939 std::unique_ptr<File> vdex_unquickened(OS::CreateEmptyFile(vdex_location2.c_str()));
1940 {
1941 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_output->Fd());
1942 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_unquickened->Fd());
1943 GenerateOdexForTest(dex_location,
1944 odex_location2,
1945 CompilerFilter::kVerify,
1946 // Disable cdex to avoid needing to write out the shared section.
1947 { input_vdex, output_vdex, kDisableCompactDex },
1948 /* expect_success */ true,
1949 /* use_fd */ true);
1950 }
1951 ASSERT_EQ(vdex_unquickened->Flush(), 0) << "Could not flush and close vdex file";
1952 ASSERT_TRUE(success_);
1953 {
1954 // Check that hte vdex has one dex and compare it to the original one.
1955 std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_location2.c_str(),
1956 /*writable*/ false,
1957 /*low_4gb*/ false,
1958 /*unquicken*/ false,
1959 &error_msg));
1960 std::vector<std::unique_ptr<const DexFile>> dex_files;
1961 bool result = vdex->OpenAllDexFiles(&dex_files, &error_msg);
1962 ASSERT_TRUE(result) << error_msg;
1963 ASSERT_EQ(dex_files.size(), 1u) << error_msg;
1964 ScratchFile temp;
1965 ASSERT_TRUE(temp.GetFile()->WriteFully(dex_files[0]->Begin(), dex_files[0]->Size()));
1966 ASSERT_EQ(temp.GetFile()->Flush(), 0) << "Could not flush extracted dex";
1967 EXPECT_EQ(temp.GetFile()->Compare(temp_dex.GetFile()), 0);
1968 }
1969 ASSERT_EQ(vdex_output->FlushCloseOrErase(), 0) << "Could not flush and close";
1970 ASSERT_EQ(vdex_unquickened->FlushCloseOrErase(), 0) << "Could not flush and close";
1971}
1972
Mathieu Chartierd45863a2018-03-21 18:16:36 -07001973// Test that compact dex generation with invalid dex files doesn't crash dex2oat. b/75970654
1974TEST_F(Dex2oatTest, CompactDexInvalidSource) {
1975 ScratchFile invalid_dex;
1976 {
Josh Gao7f00f5a2018-08-30 17:26:49 -07001977 FILE* file = fdopen(dup(invalid_dex.GetFd()), "w+b");
Mathieu Chartierd45863a2018-03-21 18:16:36 -07001978 ZipWriter writer(file);
1979 writer.StartEntry("classes.dex", ZipWriter::kAlign32);
1980 DexFile::Header header = {};
1981 StandardDexFile::WriteMagic(header.magic_);
1982 StandardDexFile::WriteCurrentVersion(header.magic_);
1983 header.file_size_ = 4 * KB;
1984 header.data_size_ = 4 * KB;
1985 header.data_off_ = 10 * MB;
1986 header.map_off_ = 10 * MB;
1987 header.class_defs_off_ = 10 * MB;
1988 header.class_defs_size_ = 10000;
1989 ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
1990 writer.FinishEntry();
1991 writer.Finish();
1992 ASSERT_EQ(invalid_dex.GetFile()->Flush(), 0);
1993 }
Andreas Gampebc802de2018-06-20 17:24:11 -07001994 const std::string& dex_location = invalid_dex.GetFilename();
Mathieu Chartierd45863a2018-03-21 18:16:36 -07001995 const std::string odex_location = GetOdexDir() + "/output.odex";
1996 std::string error_msg;
1997 int status = GenerateOdexForTestWithStatus(
1998 {dex_location},
1999 odex_location,
2000 CompilerFilter::kQuicken,
2001 &error_msg,
2002 { "--compact-dex-level=fast" });
2003 ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
2004}
2005
Mathieu Chartier14e7bad2018-03-22 14:33:20 -07002006// Test that dex2oat with a CompactDex file in the APK fails.
2007TEST_F(Dex2oatTest, CompactDexInZip) {
2008 CompactDexFile::Header header = {};
2009 CompactDexFile::WriteMagic(header.magic_);
2010 CompactDexFile::WriteCurrentVersion(header.magic_);
2011 header.file_size_ = sizeof(CompactDexFile::Header);
2012 header.data_off_ = 10 * MB;
2013 header.map_off_ = 10 * MB;
2014 header.class_defs_off_ = 10 * MB;
2015 header.class_defs_size_ = 10000;
2016 // Create a zip containing the invalid dex.
2017 ScratchFile invalid_dex_zip;
2018 {
Josh Gao7f00f5a2018-08-30 17:26:49 -07002019 FILE* file = fdopen(dup(invalid_dex_zip.GetFd()), "w+b");
Mathieu Chartier14e7bad2018-03-22 14:33:20 -07002020 ZipWriter writer(file);
2021 writer.StartEntry("classes.dex", ZipWriter::kCompress);
2022 ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
2023 writer.FinishEntry();
2024 writer.Finish();
2025 ASSERT_EQ(invalid_dex_zip.GetFile()->Flush(), 0);
2026 }
2027 // Create the dex file directly.
2028 ScratchFile invalid_dex;
2029 {
2030 ASSERT_GE(invalid_dex.GetFile()->WriteFully(&header, sizeof(header)), 0);
2031 ASSERT_EQ(invalid_dex.GetFile()->Flush(), 0);
2032 }
2033 std::string error_msg;
2034 int status = 0u;
2035
2036 status = GenerateOdexForTestWithStatus(
2037 { invalid_dex_zip.GetFilename() },
2038 GetOdexDir() + "/output_apk.odex",
2039 CompilerFilter::kQuicken,
2040 &error_msg,
2041 { "--compact-dex-level=fast" });
2042 ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
2043
2044 status = GenerateOdexForTestWithStatus(
2045 { invalid_dex.GetFilename() },
2046 GetOdexDir() + "/output.odex",
2047 CompilerFilter::kQuicken,
2048 &error_msg,
2049 { "--compact-dex-level=fast" });
2050 ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
2051}
2052
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002053TEST_F(Dex2oatTest, AppImageNoProfile) {
2054 ScratchFile app_image_file;
2055 const std::string out_dir = GetScratchDir();
2056 const std::string odex_location = out_dir + "/base.odex";
2057 GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2058 odex_location,
2059 CompilerFilter::Filter::kSpeedProfile,
2060 { "--app-image-fd=" + std::to_string(app_image_file.GetFd()) },
2061 true, // expect_success
2062 false, // use_fd
2063 [](const OatFile&) {});
2064 // Open our generated oat file.
2065 std::string error_msg;
Nicolas Geoffray30025092018-04-19 14:43:29 +01002066 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
2067 odex_location.c_str(),
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002068 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01002069 /* requested_base */ nullptr,
2070 /* executable */ false,
2071 /* low_4gb */ false,
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002072 odex_location.c_str(),
Vladimir Markoc09cd052018-08-23 16:36:36 +01002073 /* reservation */ nullptr,
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002074 &error_msg));
2075 ASSERT_TRUE(odex_file != nullptr);
2076 ImageHeader header = {};
2077 ASSERT_TRUE(app_image_file.GetFile()->PreadFully(
2078 reinterpret_cast<void*>(&header),
2079 sizeof(header),
2080 /*offset*/ 0u)) << app_image_file.GetFile()->GetLength();
2081 EXPECT_GT(header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
2082 EXPECT_EQ(header.GetImageSection(ImageHeader::kSectionArtMethods).Size(), 0u);
2083 EXPECT_EQ(header.GetImageSection(ImageHeader::kSectionArtFields).Size(), 0u);
2084}
2085
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002086TEST_F(Dex2oatTest, AppImageResolveStrings) {
2087 using Hotness = ProfileCompilationInfo::MethodHotness;
2088 // Create a profile with the startup method marked.
2089 ScratchFile profile_file;
2090 std::vector<uint16_t> methods;
2091 {
2092 std::unique_ptr<const DexFile> dex(OpenTestDexFile("StringLiterals"));
2093 for (size_t method_idx = 0; method_idx < dex->NumMethodIds(); ++method_idx) {
2094 if (std::string(dex->GetMethodName(dex->GetMethodId(method_idx))) == "startUpMethod") {
2095 methods.push_back(method_idx);
2096 }
2097 }
2098 ASSERT_GT(methods.size(), 0u);
2099 // Here, we build the profile from the method lists.
2100 ProfileCompilationInfo info;
2101 info.AddMethodsForDex(Hotness::kFlagStartup, dex.get(), methods.begin(), methods.end());
2102 // Save the profile since we want to use it with dex2oat to produce an oat file.
2103 ASSERT_TRUE(info.Save(profile_file.GetFd()));
2104 }
2105 const std::string out_dir = GetScratchDir();
2106 const std::string odex_location = out_dir + "/base.odex";
2107 const std::string app_image_location = out_dir + "/base.art";
2108 GenerateOdexForTest(GetTestDexFileName("StringLiterals"),
2109 odex_location,
2110 CompilerFilter::Filter::kSpeedProfile,
2111 { "--app-image-file=" + app_image_location,
2112 "--resolve-startup-const-strings=true",
2113 "--profile-file=" + profile_file.GetFilename()},
2114 /* expect_success= */ true,
2115 /* use_fd= */ false,
2116 [](const OatFile&) {});
2117 // Open our generated oat file.
2118 std::string error_msg;
2119 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd= */ -1,
2120 odex_location.c_str(),
2121 odex_location.c_str(),
2122 /* requested_base= */ nullptr,
2123 /* executable= */ false,
2124 /* low_4gb= */ false,
2125 odex_location.c_str(),
2126 /* reservation= */ nullptr,
2127 &error_msg));
2128 ASSERT_TRUE(odex_file != nullptr);
2129 // Check the strings in the app image intern table only contain the "startup" strigs.
2130 {
2131 ScopedObjectAccess soa(Thread::Current());
2132 std::unique_ptr<gc::space::ImageSpace> space =
2133 gc::space::ImageSpace::CreateFromAppImage(app_image_location.c_str(),
2134 odex_file.get(),
2135 &error_msg);
2136 ASSERT_TRUE(space != nullptr) << error_msg;
2137 std::set<std::string> seen;
2138 InternTable intern_table;
2139 intern_table.AddImageStringsToTable(space.get(), [&](InternTable::UnorderedSet& interns)
2140 REQUIRES_SHARED(Locks::mutator_lock_) {
2141 for (const GcRoot<mirror::String>& str : interns) {
2142 seen.insert(str.Read()->ToModifiedUtf8());
2143 }
2144 });
2145 EXPECT_TRUE(seen.find("Loading ") != seen.end());
2146 EXPECT_TRUE(seen.find("Starting up") != seen.end());
2147 EXPECT_TRUE(seen.find("abcd.apk") != seen.end());
2148 EXPECT_TRUE(seen.find("Unexpected error") == seen.end());
2149 EXPECT_TRUE(seen.find("Shutting down!") == seen.end());
2150 }
2151}
2152
2153
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002154TEST_F(Dex2oatClassLoaderContextTest, StoredClassLoaderContext) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07002155 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002156 const std::string out_dir = GetScratchDir();
2157 const std::string odex_location = out_dir + "/base.odex";
Mathieu Chartierc4440772018-04-16 14:40:56 -07002158 const std::string valid_context = "PCL[" + dex_files[0]->GetLocation() + "]";
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002159 const std::string stored_context = "PCL[/system/not_real_lib.jar]";
Mathieu Chartierc4440772018-04-16 14:40:56 -07002160 std::string expected_stored_context = "PCL[";
2161 size_t index = 1;
2162 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2163 const bool is_first = index == 1u;
2164 if (!is_first) {
2165 expected_stored_context += ":";
2166 }
2167 expected_stored_context += "/system/not_real_lib.jar";
2168 if (!is_first) {
2169 expected_stored_context += "!classes" + std::to_string(index) + ".dex";
2170 }
2171 expected_stored_context += "*" + std::to_string(dex_file->GetLocationChecksum());
2172 ++index;
2173 }
2174 expected_stored_context += + "]";
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002175 // The class path should not be valid and should fail being stored.
2176 GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2177 odex_location,
2178 CompilerFilter::Filter::kQuicken,
2179 { "--class-loader-context=" + stored_context },
2180 true, // expect_success
2181 false, // use_fd
2182 [&](const OatFile& oat_file) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07002183 EXPECT_NE(oat_file.GetClassLoaderContext(), stored_context) << output_;
2184 EXPECT_NE(oat_file.GetClassLoaderContext(), valid_context) << output_;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002185 });
2186 // The stored context should match what we expect even though it's invalid.
2187 GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2188 odex_location,
2189 CompilerFilter::Filter::kQuicken,
2190 { "--class-loader-context=" + valid_context,
2191 "--stored-class-loader-context=" + stored_context },
2192 true, // expect_success
2193 false, // use_fd
2194 [&](const OatFile& oat_file) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07002195 EXPECT_EQ(oat_file.GetClassLoaderContext(), expected_stored_context) << output_;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002196 });
2197}
2198
Andreas Gampee1459ae2016-06-29 09:36:30 -07002199} // namespace art