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