blob: b6b62a80dc2a84b912cf76a7ed71e07a71283630 [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
Andreas Gampe7adeda82016-07-25 08:27:35 -070017#include <regex>
18#include <sstream>
Andreas Gampee1459ae2016-06-29 09:36:30 -070019#include <string>
20#include <vector>
Andreas Gampee1459ae2016-06-29 09:36:30 -070021
22#include "common_runtime_test.h"
23
24#include "base/logging.h"
25#include "base/macros.h"
26#include "base/stringprintf.h"
Jeff Hao608f2ce2016-10-19 11:17:11 -070027#include "dex_file-inl.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070028#include "dex2oat_environment_test.h"
Jeff Hao41fba6a2016-11-28 11:53:33 -080029#include "jit/offline_profiling_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070030#include "oat.h"
31#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070032#include "utils.h"
33
34#include <sys/wait.h>
35#include <unistd.h>
36
37namespace art {
38
39class Dex2oatTest : public Dex2oatEnvironmentTest {
40 public:
41 virtual void TearDown() OVERRIDE {
42 Dex2oatEnvironmentTest::TearDown();
43
44 output_ = "";
45 error_msg_ = "";
46 success_ = false;
47 }
48
49 protected:
50 void GenerateOdexForTest(const std::string& dex_location,
51 const std::string& odex_location,
52 CompilerFilter::Filter filter,
53 const std::vector<std::string>& extra_args = {},
54 bool expect_success = true) {
55 std::vector<std::string> args;
56 args.push_back("--dex-file=" + dex_location);
57 args.push_back("--oat-file=" + odex_location);
58 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
59 args.push_back("--runtime-arg");
60 args.push_back("-Xnorelocate");
61
62 args.insert(args.end(), extra_args.begin(), extra_args.end());
63
64 std::string error_msg;
65 bool success = Dex2Oat(args, &error_msg);
66
67 if (expect_success) {
68 ASSERT_TRUE(success) << error_msg;
69
70 // Verify the odex file was generated as expected.
71 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
72 odex_location.c_str(),
73 nullptr,
74 nullptr,
75 false,
76 /*low_4gb*/false,
77 dex_location.c_str(),
78 &error_msg));
79 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
80
81 CheckFilter(filter, odex_file->GetCompilerFilter());
82 } else {
83 ASSERT_FALSE(success) << output_;
84
85 error_msg_ = error_msg;
86
87 // Verify there's no loadable odex file.
88 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
89 odex_location.c_str(),
90 nullptr,
91 nullptr,
92 false,
93 /*low_4gb*/false,
94 dex_location.c_str(),
95 &error_msg));
96 ASSERT_TRUE(odex_file.get() == nullptr);
97 }
98 }
99
100 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
101 // in subclasses when equality is not expected.
102 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
103 EXPECT_EQ(expected, actual);
104 }
105
106 bool Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
107 Runtime* runtime = Runtime::Current();
108
109 const std::vector<gc::space::ImageSpace*>& image_spaces =
110 runtime->GetHeap()->GetBootImageSpaces();
111 if (image_spaces.empty()) {
112 *error_msg = "No image location found for Dex2Oat.";
113 return false;
114 }
115 std::string image_location = image_spaces[0]->GetImageLocation();
116
117 std::vector<std::string> argv;
118 argv.push_back(runtime->GetCompilerExecutable());
119 argv.push_back("--runtime-arg");
120 argv.push_back("-classpath");
121 argv.push_back("--runtime-arg");
122 std::string class_path = runtime->GetClassPathString();
123 if (class_path == "") {
124 class_path = OatFile::kSpecialSharedLibrary;
125 }
126 argv.push_back(class_path);
127 if (runtime->IsDebuggable()) {
128 argv.push_back("--debuggable");
129 }
130 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
131
132 if (!runtime->IsVerificationEnabled()) {
133 argv.push_back("--compiler-filter=verify-none");
134 }
135
136 if (runtime->MustRelocateIfPossible()) {
137 argv.push_back("--runtime-arg");
138 argv.push_back("-Xrelocate");
139 } else {
140 argv.push_back("--runtime-arg");
141 argv.push_back("-Xnorelocate");
142 }
143
144 if (!kIsTargetBuild) {
145 argv.push_back("--host");
146 }
147
148 argv.push_back("--boot-image=" + image_location);
149
150 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
151 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
152
153 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
154
155 // We must set --android-root.
156 const char* android_root = getenv("ANDROID_ROOT");
157 CHECK(android_root != nullptr);
158 argv.push_back("--android-root=" + std::string(android_root));
159
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100160 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700161
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100162 if (pipe(link) == -1) {
163 return false;
164 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700165
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100166 pid_t pid = fork();
167 if (pid == -1) {
168 return false;
169 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700170
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100171 if (pid == 0) {
172 // We need dex2oat to actually log things.
173 setenv("ANDROID_LOG_TAGS", "*:d", 1);
174 dup2(link[1], STDERR_FILENO);
175 close(link[0]);
176 close(link[1]);
177 std::vector<const char*> c_args;
178 for (const std::string& str : argv) {
179 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700180 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100181 c_args.push_back(nullptr);
182 execv(c_args[0], const_cast<char* const*>(c_args.data()));
183 exit(1);
184 } else {
185 close(link[1]);
186 char buffer[128];
187 memset(buffer, 0, 128);
188 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700189
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100190 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
191 output_ += std::string(buffer, bytes_read);
192 }
193 close(link[0]);
194 int status = 0;
195 if (waitpid(pid, &status, 0) != -1) {
196 success_ = (status == 0);
197 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700198 }
199 return success_;
200 }
201
202 std::string output_ = "";
203 std::string error_msg_ = "";
204 bool success_ = false;
205};
206
207class Dex2oatSwapTest : public Dex2oatTest {
208 protected:
209 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
210 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
211 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
212
Andreas Gampe7adeda82016-07-25 08:27:35 -0700213 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700214
215 std::vector<std::string> copy(extra_args);
216
217 std::unique_ptr<ScratchFile> sf;
218 if (use_fd) {
219 sf.reset(new ScratchFile());
220 copy.push_back(StringPrintf("--swap-fd=%d", sf->GetFd()));
221 } else {
222 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
223 copy.push_back("--swap-file=" + swap_location);
224 }
225 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
226
227 CheckValidity();
228 ASSERT_TRUE(success_);
229 CheckResult(expect_use);
230 }
231
Andreas Gampe7adeda82016-07-25 08:27:35 -0700232 virtual std::string GetTestDexFileName() {
233 return GetDexSrc1();
234 }
235
236 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700237 if (kIsTargetBuild) {
238 CheckTargetResult(expect_use);
239 } else {
240 CheckHostResult(expect_use);
241 }
242 }
243
Andreas Gampe7adeda82016-07-25 08:27:35 -0700244 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700245 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
246 // something for variants with file descriptor where we can control the lifetime of
247 // the swap file and thus take a look at it.
248 }
249
Andreas Gampe7adeda82016-07-25 08:27:35 -0700250 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700251 if (!kIsTargetBuild) {
252 if (expect_use) {
253 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
254 << output_;
255 } else {
256 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
257 << output_;
258 }
259 }
260 }
261
262 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700263 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700264 if (kIsTargetBuild) {
265 CheckTargetValidity();
266 } else {
267 CheckHostValidity();
268 }
269 }
270
Andreas Gampe7adeda82016-07-25 08:27:35 -0700271 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700272 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
273 // something for variants with file descriptor where we can control the lifetime of
274 // the swap file and thus take a look at it.
275 }
276
277 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700278 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700279 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
280 }
281};
282
283TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
284 RunTest(false /* use_fd */, false /* expect_use */);
285 RunTest(true /* use_fd */, false /* expect_use */);
286}
287
288TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
289 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
290 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
291}
292
293TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
294 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
295 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
296}
297
298TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
299 RunTest(false /* use_fd */,
300 true /* expect_use */,
301 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
302 RunTest(true /* use_fd */,
303 true /* expect_use */,
304 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
305}
306
Andreas Gampe7adeda82016-07-25 08:27:35 -0700307class Dex2oatSwapUseTest : public Dex2oatSwapTest {
308 protected:
309 void CheckHostResult(bool expect_use) OVERRIDE {
310 if (!kIsTargetBuild) {
311 if (expect_use) {
312 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
313 << output_;
314 } else {
315 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
316 << output_;
317 }
318 }
319 }
320
321 std::string GetTestDexFileName() OVERRIDE {
322 // Use Statics as it has a handful of functions.
323 return CommonRuntimeTest::GetTestDexFileName("Statics");
324 }
325
326 void GrabResult1() {
327 if (!kIsTargetBuild) {
328 native_alloc_1_ = ParseNativeAlloc();
329 swap_1_ = ParseSwap(false /* expected */);
330 } else {
331 native_alloc_1_ = std::numeric_limits<size_t>::max();
332 swap_1_ = 0;
333 }
334 }
335
336 void GrabResult2() {
337 if (!kIsTargetBuild) {
338 native_alloc_2_ = ParseNativeAlloc();
339 swap_2_ = ParseSwap(true /* expected */);
340 } else {
341 native_alloc_2_ = 0;
342 swap_2_ = std::numeric_limits<size_t>::max();
343 }
344 }
345
346 private:
347 size_t ParseNativeAlloc() {
348 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
349 std::smatch native_alloc_match;
350 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
351 if (!found) {
352 EXPECT_TRUE(found);
353 return 0;
354 }
355 if (native_alloc_match.size() != 2U) {
356 EXPECT_EQ(native_alloc_match.size(), 2U);
357 return 0;
358 }
359
360 std::istringstream stream(native_alloc_match[1].str());
361 size_t value;
362 stream >> value;
363
364 return value;
365 }
366
367 size_t ParseSwap(bool expected) {
368 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
369 std::smatch swap_match;
370 bool found = std::regex_search(output_, swap_match, swap_regex);
371 if (found != expected) {
372 EXPECT_EQ(expected, found);
373 return 0;
374 }
375
376 if (!found) {
377 return 0;
378 }
379
380 if (swap_match.size() != 2U) {
381 EXPECT_EQ(swap_match.size(), 2U);
382 return 0;
383 }
384
385 std::istringstream stream(swap_match[1].str());
386 size_t value;
387 stream >> value;
388
389 return value;
390 }
391
392 protected:
393 size_t native_alloc_1_;
394 size_t native_alloc_2_;
395
396 size_t swap_1_;
397 size_t swap_2_;
398};
399
400TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Roland Levillain63b6eb42016-07-28 16:37:28 +0100401 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
402 // hold true on some x86 systems when read barriers are enabled;
403 // disable this test while we investigate (b/29259363).
404 TEST_DISABLED_FOR_READ_BARRIER_ON_X86();
405
Andreas Gampe7adeda82016-07-25 08:27:35 -0700406 RunTest(false /* use_fd */,
407 false /* expect_use */);
408 GrabResult1();
409 std::string output_1 = output_;
410
411 output_ = "";
412
413 RunTest(false /* use_fd */,
414 true /* expect_use */,
415 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
416 GrabResult2();
417 std::string output_2 = output_;
418
419 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
420 EXPECT_LT(native_alloc_2_, native_alloc_1_);
421 EXPECT_LT(swap_1_, swap_2_);
422
423 LOG(ERROR) << output_1;
424 LOG(ERROR) << output_2;
425 }
426}
427
Andreas Gampe67f02822016-06-24 21:05:23 -0700428class Dex2oatVeryLargeTest : public Dex2oatTest {
429 protected:
430 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
431 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
432 // Ignore, we'll do our own checks.
433 }
434
435 void RunTest(CompilerFilter::Filter filter,
436 bool expect_large,
437 const std::vector<std::string>& extra_args = {}) {
438 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
439 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
440
441 Copy(GetDexSrc1(), dex_location);
442
Andreas Gampeca620d72016-11-08 08:09:33 -0800443 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700444
445 CheckValidity();
446 ASSERT_TRUE(success_);
447 CheckResult(dex_location, odex_location, filter, expect_large);
448 }
449
450 void CheckResult(const std::string& dex_location,
451 const std::string& odex_location,
452 CompilerFilter::Filter filter,
453 bool expect_large) {
454 // Host/target independent checks.
455 std::string error_msg;
456 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
457 odex_location.c_str(),
458 nullptr,
459 nullptr,
460 false,
461 /*low_4gb*/false,
462 dex_location.c_str(),
463 &error_msg));
464 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
465 if (expect_large) {
466 // Note: we cannot check the following:
467 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
468 // odex_file->GetCompilerFilter()));
469 // The reason is that the filter override currently happens when the dex files are
470 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
471 // store cannot be changed, and the original filter is set in stone.
472
473 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
474 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
475 ASSERT_TRUE(dex_file != nullptr);
476 uint32_t class_def_count = dex_file->NumClassDefs();
477 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
478 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
479 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
480 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
481 }
482 }
483
484 // If the input filter was "below," it should have been used.
485 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, filter)) {
486 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
487 }
488 } else {
489 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
490 }
491
492 // Host/target dependent checks.
493 if (kIsTargetBuild) {
494 CheckTargetResult(expect_large);
495 } else {
496 CheckHostResult(expect_large);
497 }
498 }
499
500 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
501 // TODO: Ignore for now. May do something for fd things.
502 }
503
504 void CheckHostResult(bool expect_large) {
505 if (!kIsTargetBuild) {
506 if (expect_large) {
507 EXPECT_NE(output_.find("Very large app, downgrading to verify-at-runtime."),
508 std::string::npos)
509 << output_;
510 } else {
511 EXPECT_EQ(output_.find("Very large app, downgrading to verify-at-runtime."),
512 std::string::npos)
513 << output_;
514 }
515 }
516 }
517
518 // Check whether the dex2oat run was really successful.
519 void CheckValidity() {
520 if (kIsTargetBuild) {
521 CheckTargetValidity();
522 } else {
523 CheckHostValidity();
524 }
525 }
526
527 void CheckTargetValidity() {
528 // TODO: Ignore for now.
529 }
530
531 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
532 void CheckHostValidity() {
533 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
534 }
535};
536
537TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
538 RunTest(CompilerFilter::kVerifyNone, false);
539 RunTest(CompilerFilter::kVerifyAtRuntime, false);
540 RunTest(CompilerFilter::kInterpretOnly, false);
541 RunTest(CompilerFilter::kSpeed, false);
542
543 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=1000000" });
544 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=1000000" });
545 RunTest(CompilerFilter::kInterpretOnly, false, { "--very-large-app-threshold=1000000" });
546 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
547}
548
549TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
550 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=100" });
551 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=100" });
552 RunTest(CompilerFilter::kInterpretOnly, true, { "--very-large-app-threshold=100" });
553 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
554}
555
Jeff Hao608f2ce2016-10-19 11:17:11 -0700556class Dex2oatLayoutTest : public Dex2oatTest {
557 protected:
558 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
559 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
560 // Ignore, we'll do our own checks.
561 }
562
Jeff Hao41fba6a2016-11-28 11:53:33 -0800563 // Emits a profile with a single dex file with the given location and a single class index of 1.
564 void GenerateProfile(const std::string& test_profile,
565 const std::string& dex_location,
566 uint32_t checksum) {
567 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
568 CHECK_GE(profile_test_fd, 0);
569
570 ProfileCompilationInfo info;
571 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
572 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1));
573 bool result = info.Save(profile_test_fd);
574 close(profile_test_fd);
575 ASSERT_TRUE(result);
576 }
577
Jeff Hao608f2ce2016-10-19 11:17:11 -0700578 void RunTest() {
579 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
580 std::string profile_location = GetScratchDir() + "/primary.prof";
581 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
582
583 Copy(GetDexSrc2(), dex_location);
Jeff Hao41fba6a2016-11-28 11:53:33 -0800584 const char* location = dex_location.c_str();
585 std::string error_msg;
586 std::vector<std::unique_ptr<const DexFile>> dex_files;
587 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
588 EXPECT_EQ(dex_files.size(), 1U);
589 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
590 GenerateProfile(profile_location, dex_location, dex_file->GetLocationChecksum());
Jeff Hao608f2ce2016-10-19 11:17:11 -0700591
592 const std::vector<std::string>& extra_args = { "--profile-file=" + profile_location };
593 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kLayoutProfile, extra_args);
594
595 CheckValidity();
596 ASSERT_TRUE(success_);
597 CheckResult(dex_location, odex_location);
598 }
599 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
600 // Host/target independent checks.
601 std::string error_msg;
602 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
603 odex_location.c_str(),
604 nullptr,
605 nullptr,
606 false,
607 /*low_4gb*/false,
608 dex_location.c_str(),
609 &error_msg));
610 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
611
Jeff Hao042e8982016-10-19 11:17:11 -0700612 const char* location = dex_location.c_str();
613 std::vector<std::unique_ptr<const DexFile>> dex_files;
614 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
615 EXPECT_EQ(dex_files.size(), 1U);
616 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
617
Jeff Hao608f2ce2016-10-19 11:17:11 -0700618 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700619 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
620 ASSERT_TRUE(new_dex_file != nullptr);
621 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700622 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700623 ASSERT_GE(class_def_count, 2U);
624
625 // The new layout swaps the classes at indexes 0 and 1.
626 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
627 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
628 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
629 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
630 EXPECT_EQ(old_class0, new_class1);
631 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700632 }
633
634 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kLayoutProfile);
635 }
636
637 // Check whether the dex2oat run was really successful.
638 void CheckValidity() {
639 if (kIsTargetBuild) {
640 CheckTargetValidity();
641 } else {
642 CheckHostValidity();
643 }
644 }
645
646 void CheckTargetValidity() {
647 // TODO: Ignore for now.
648 }
649
650 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
651 void CheckHostValidity() {
652 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
653 }
654 };
655
656TEST_F(Dex2oatLayoutTest, TestLayout) {
657 RunTest();
658}
659
Andreas Gampee1459ae2016-06-29 09:36:30 -0700660} // namespace art