blob: b79050e9d0027c12c4d6b6d365a3eba1b12746e3 [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include <sys/wait.h>
23#include <unistd.h>
24
25#include "android-base/stringprintf.h"
26
Andreas Gampee1459ae2016-06-29 09:36:30 -070027#include "common_runtime_test.h"
28
29#include "base/logging.h"
30#include "base/macros.h"
Jeff Hao608f2ce2016-10-19 11:17:11 -070031#include "dex_file-inl.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070032#include "dex2oat_environment_test.h"
Calin Juravle33083d62017-01-18 15:29:12 -080033#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070034#include "oat.h"
35#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070036#include "utils.h"
37
Andreas Gampee1459ae2016-06-29 09:36:30 -070038namespace art {
39
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080040using android::base::StringPrintf;
41
Andreas Gampee1459ae2016-06-29 09:36:30 -070042class Dex2oatTest : public Dex2oatEnvironmentTest {
43 public:
44 virtual void TearDown() OVERRIDE {
45 Dex2oatEnvironmentTest::TearDown();
46
47 output_ = "";
48 error_msg_ = "";
49 success_ = false;
50 }
51
52 protected:
53 void GenerateOdexForTest(const std::string& dex_location,
54 const std::string& odex_location,
55 CompilerFilter::Filter filter,
56 const std::vector<std::string>& extra_args = {},
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080057 bool expect_success = true,
58 bool use_fd = false) {
59 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070060 std::vector<std::string> args;
61 args.push_back("--dex-file=" + dex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080062 if (use_fd) {
63 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
64 CHECK(oat_file != nullptr) << odex_location;
65 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
66 } else {
67 args.push_back("--oat-file=" + odex_location);
68 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070069 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
70 args.push_back("--runtime-arg");
71 args.push_back("-Xnorelocate");
72
73 args.insert(args.end(), extra_args.begin(), extra_args.end());
74
75 std::string error_msg;
76 bool success = Dex2Oat(args, &error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080077 if (oat_file != nullptr) {
78 ASSERT_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
79 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070080
81 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -080082 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -070083
84 // Verify the odex file was generated as expected.
85 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
86 odex_location.c_str(),
87 nullptr,
88 nullptr,
89 false,
90 /*low_4gb*/false,
91 dex_location.c_str(),
92 &error_msg));
93 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
94
95 CheckFilter(filter, odex_file->GetCompilerFilter());
96 } else {
97 ASSERT_FALSE(success) << output_;
98
99 error_msg_ = error_msg;
100
101 // Verify there's no loadable odex file.
102 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
103 odex_location.c_str(),
104 nullptr,
105 nullptr,
106 false,
107 /*low_4gb*/false,
108 dex_location.c_str(),
109 &error_msg));
110 ASSERT_TRUE(odex_file.get() == nullptr);
111 }
112 }
113
114 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
115 // in subclasses when equality is not expected.
116 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
117 EXPECT_EQ(expected, actual);
118 }
119
120 bool Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
121 Runtime* runtime = Runtime::Current();
122
123 const std::vector<gc::space::ImageSpace*>& image_spaces =
124 runtime->GetHeap()->GetBootImageSpaces();
125 if (image_spaces.empty()) {
126 *error_msg = "No image location found for Dex2Oat.";
127 return false;
128 }
129 std::string image_location = image_spaces[0]->GetImageLocation();
130
131 std::vector<std::string> argv;
132 argv.push_back(runtime->GetCompilerExecutable());
133 argv.push_back("--runtime-arg");
134 argv.push_back("-classpath");
135 argv.push_back("--runtime-arg");
136 std::string class_path = runtime->GetClassPathString();
137 if (class_path == "") {
138 class_path = OatFile::kSpecialSharedLibrary;
139 }
140 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000141 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700142 argv.push_back("--debuggable");
143 }
144 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
145
146 if (!runtime->IsVerificationEnabled()) {
147 argv.push_back("--compiler-filter=verify-none");
148 }
149
150 if (runtime->MustRelocateIfPossible()) {
151 argv.push_back("--runtime-arg");
152 argv.push_back("-Xrelocate");
153 } else {
154 argv.push_back("--runtime-arg");
155 argv.push_back("-Xnorelocate");
156 }
157
158 if (!kIsTargetBuild) {
159 argv.push_back("--host");
160 }
161
162 argv.push_back("--boot-image=" + image_location);
163
164 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
165 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
166
167 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
168
169 // We must set --android-root.
170 const char* android_root = getenv("ANDROID_ROOT");
171 CHECK(android_root != nullptr);
172 argv.push_back("--android-root=" + std::string(android_root));
173
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100174 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700175
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100176 if (pipe(link) == -1) {
177 return false;
178 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700179
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100180 pid_t pid = fork();
181 if (pid == -1) {
182 return false;
183 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700184
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100185 if (pid == 0) {
186 // We need dex2oat to actually log things.
187 setenv("ANDROID_LOG_TAGS", "*:d", 1);
188 dup2(link[1], STDERR_FILENO);
189 close(link[0]);
190 close(link[1]);
191 std::vector<const char*> c_args;
192 for (const std::string& str : argv) {
193 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700194 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100195 c_args.push_back(nullptr);
196 execv(c_args[0], const_cast<char* const*>(c_args.data()));
197 exit(1);
198 } else {
199 close(link[1]);
200 char buffer[128];
201 memset(buffer, 0, 128);
202 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700203
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100204 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
205 output_ += std::string(buffer, bytes_read);
206 }
207 close(link[0]);
208 int status = 0;
209 if (waitpid(pid, &status, 0) != -1) {
210 success_ = (status == 0);
211 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700212 }
213 return success_;
214 }
215
216 std::string output_ = "";
217 std::string error_msg_ = "";
218 bool success_ = false;
219};
220
221class Dex2oatSwapTest : public Dex2oatTest {
222 protected:
223 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
224 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
225 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
226
Andreas Gampe7adeda82016-07-25 08:27:35 -0700227 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700228
229 std::vector<std::string> copy(extra_args);
230
231 std::unique_ptr<ScratchFile> sf;
232 if (use_fd) {
233 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800234 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700235 } else {
236 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
237 copy.push_back("--swap-file=" + swap_location);
238 }
239 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
240
241 CheckValidity();
242 ASSERT_TRUE(success_);
243 CheckResult(expect_use);
244 }
245
Andreas Gampe7adeda82016-07-25 08:27:35 -0700246 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000247 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700248 }
249
250 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700251 if (kIsTargetBuild) {
252 CheckTargetResult(expect_use);
253 } else {
254 CheckHostResult(expect_use);
255 }
256 }
257
Andreas Gampe7adeda82016-07-25 08:27:35 -0700258 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700259 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
260 // something for variants with file descriptor where we can control the lifetime of
261 // the swap file and thus take a look at it.
262 }
263
Andreas Gampe7adeda82016-07-25 08:27:35 -0700264 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700265 if (!kIsTargetBuild) {
266 if (expect_use) {
267 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
268 << output_;
269 } else {
270 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
271 << output_;
272 }
273 }
274 }
275
276 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700277 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700278 if (kIsTargetBuild) {
279 CheckTargetValidity();
280 } else {
281 CheckHostValidity();
282 }
283 }
284
Andreas Gampe7adeda82016-07-25 08:27:35 -0700285 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700286 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
287 // something for variants with file descriptor where we can control the lifetime of
288 // the swap file and thus take a look at it.
289 }
290
291 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700292 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700293 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
294 }
295};
296
297TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
298 RunTest(false /* use_fd */, false /* expect_use */);
299 RunTest(true /* use_fd */, false /* expect_use */);
300}
301
302TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
303 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
304 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
305}
306
307TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
308 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
309 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
310}
311
312TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
313 RunTest(false /* use_fd */,
314 true /* expect_use */,
315 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
316 RunTest(true /* use_fd */,
317 true /* expect_use */,
318 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
319}
320
Andreas Gampe7adeda82016-07-25 08:27:35 -0700321class Dex2oatSwapUseTest : public Dex2oatSwapTest {
322 protected:
323 void CheckHostResult(bool expect_use) OVERRIDE {
324 if (!kIsTargetBuild) {
325 if (expect_use) {
326 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
327 << output_;
328 } else {
329 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
330 << output_;
331 }
332 }
333 }
334
335 std::string GetTestDexFileName() OVERRIDE {
336 // Use Statics as it has a handful of functions.
337 return CommonRuntimeTest::GetTestDexFileName("Statics");
338 }
339
340 void GrabResult1() {
341 if (!kIsTargetBuild) {
342 native_alloc_1_ = ParseNativeAlloc();
343 swap_1_ = ParseSwap(false /* expected */);
344 } else {
345 native_alloc_1_ = std::numeric_limits<size_t>::max();
346 swap_1_ = 0;
347 }
348 }
349
350 void GrabResult2() {
351 if (!kIsTargetBuild) {
352 native_alloc_2_ = ParseNativeAlloc();
353 swap_2_ = ParseSwap(true /* expected */);
354 } else {
355 native_alloc_2_ = 0;
356 swap_2_ = std::numeric_limits<size_t>::max();
357 }
358 }
359
360 private:
361 size_t ParseNativeAlloc() {
362 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
363 std::smatch native_alloc_match;
364 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
365 if (!found) {
366 EXPECT_TRUE(found);
367 return 0;
368 }
369 if (native_alloc_match.size() != 2U) {
370 EXPECT_EQ(native_alloc_match.size(), 2U);
371 return 0;
372 }
373
374 std::istringstream stream(native_alloc_match[1].str());
375 size_t value;
376 stream >> value;
377
378 return value;
379 }
380
381 size_t ParseSwap(bool expected) {
382 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
383 std::smatch swap_match;
384 bool found = std::regex_search(output_, swap_match, swap_regex);
385 if (found != expected) {
386 EXPECT_EQ(expected, found);
387 return 0;
388 }
389
390 if (!found) {
391 return 0;
392 }
393
394 if (swap_match.size() != 2U) {
395 EXPECT_EQ(swap_match.size(), 2U);
396 return 0;
397 }
398
399 std::istringstream stream(swap_match[1].str());
400 size_t value;
401 stream >> value;
402
403 return value;
404 }
405
406 protected:
407 size_t native_alloc_1_;
408 size_t native_alloc_2_;
409
410 size_t swap_1_;
411 size_t swap_2_;
412};
413
414TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Vladimir Marko57070da2017-02-14 16:16:30 +0000415 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000416 // hold true on some x86 systems; disable this test while we
417 // investigate (b/29259363).
418 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000419
Andreas Gampe7adeda82016-07-25 08:27:35 -0700420 RunTest(false /* use_fd */,
421 false /* expect_use */);
422 GrabResult1();
423 std::string output_1 = output_;
424
425 output_ = "";
426
427 RunTest(false /* use_fd */,
428 true /* expect_use */,
429 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
430 GrabResult2();
431 std::string output_2 = output_;
432
433 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
434 EXPECT_LT(native_alloc_2_, native_alloc_1_);
435 EXPECT_LT(swap_1_, swap_2_);
436
437 LOG(ERROR) << output_1;
438 LOG(ERROR) << output_2;
439 }
440}
441
Andreas Gampe67f02822016-06-24 21:05:23 -0700442class Dex2oatVeryLargeTest : public Dex2oatTest {
443 protected:
444 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
445 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
446 // Ignore, we'll do our own checks.
447 }
448
449 void RunTest(CompilerFilter::Filter filter,
450 bool expect_large,
451 const std::vector<std::string>& extra_args = {}) {
452 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
453 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
454
455 Copy(GetDexSrc1(), dex_location);
456
Andreas Gampeca620d72016-11-08 08:09:33 -0800457 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700458
459 CheckValidity();
460 ASSERT_TRUE(success_);
461 CheckResult(dex_location, odex_location, filter, expect_large);
462 }
463
464 void CheckResult(const std::string& dex_location,
465 const std::string& odex_location,
466 CompilerFilter::Filter filter,
467 bool expect_large) {
468 // Host/target independent checks.
469 std::string error_msg;
470 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
471 odex_location.c_str(),
472 nullptr,
473 nullptr,
474 false,
475 /*low_4gb*/false,
476 dex_location.c_str(),
477 &error_msg));
478 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
479 if (expect_large) {
480 // Note: we cannot check the following:
481 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
482 // odex_file->GetCompilerFilter()));
483 // The reason is that the filter override currently happens when the dex files are
484 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
485 // store cannot be changed, and the original filter is set in stone.
486
487 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
488 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
489 ASSERT_TRUE(dex_file != nullptr);
490 uint32_t class_def_count = dex_file->NumClassDefs();
491 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
492 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
493 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
494 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
495 }
496 }
497
498 // If the input filter was "below," it should have been used.
499 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, filter)) {
500 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
501 }
502 } else {
503 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
504 }
505
506 // Host/target dependent checks.
507 if (kIsTargetBuild) {
508 CheckTargetResult(expect_large);
509 } else {
510 CheckHostResult(expect_large);
511 }
512 }
513
514 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
515 // TODO: Ignore for now. May do something for fd things.
516 }
517
518 void CheckHostResult(bool expect_large) {
519 if (!kIsTargetBuild) {
520 if (expect_large) {
521 EXPECT_NE(output_.find("Very large app, downgrading to verify-at-runtime."),
522 std::string::npos)
523 << output_;
524 } else {
525 EXPECT_EQ(output_.find("Very large app, downgrading to verify-at-runtime."),
526 std::string::npos)
527 << output_;
528 }
529 }
530 }
531
532 // Check whether the dex2oat run was really successful.
533 void CheckValidity() {
534 if (kIsTargetBuild) {
535 CheckTargetValidity();
536 } else {
537 CheckHostValidity();
538 }
539 }
540
541 void CheckTargetValidity() {
542 // TODO: Ignore for now.
543 }
544
545 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
546 void CheckHostValidity() {
547 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
548 }
549};
550
551TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
552 RunTest(CompilerFilter::kVerifyNone, false);
553 RunTest(CompilerFilter::kVerifyAtRuntime, false);
554 RunTest(CompilerFilter::kInterpretOnly, false);
555 RunTest(CompilerFilter::kSpeed, false);
556
557 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=1000000" });
558 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=1000000" });
559 RunTest(CompilerFilter::kInterpretOnly, false, { "--very-large-app-threshold=1000000" });
560 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
561}
562
563TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
564 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=100" });
565 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=100" });
566 RunTest(CompilerFilter::kInterpretOnly, true, { "--very-large-app-threshold=100" });
567 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
568}
569
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800570// Regressin test for b/35665292.
571TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
572 // Test that dex2oat doesn't crash with speed-profile but no input profile.
573 RunTest(CompilerFilter::kSpeedProfile, false);
574}
575
Jeff Hao608f2ce2016-10-19 11:17:11 -0700576class Dex2oatLayoutTest : public Dex2oatTest {
577 protected:
578 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
579 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
580 // Ignore, we'll do our own checks.
581 }
582
Jeff Hao41fba6a2016-11-28 11:53:33 -0800583 // Emits a profile with a single dex file with the given location and a single class index of 1.
584 void GenerateProfile(const std::string& test_profile,
585 const std::string& dex_location,
586 uint32_t checksum) {
587 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
588 CHECK_GE(profile_test_fd, 0);
589
590 ProfileCompilationInfo info;
591 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
592 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1));
593 bool result = info.Save(profile_test_fd);
594 close(profile_test_fd);
595 ASSERT_TRUE(result);
596 }
597
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800598 void CompileProfileOdex(const std::string& dex_location,
599 const std::string& odex_location,
600 bool use_fd,
601 const std::vector<std::string>& extra_args = {}) {
602 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800603 const char* location = dex_location.c_str();
604 std::string error_msg;
605 std::vector<std::unique_ptr<const DexFile>> dex_files;
606 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
607 EXPECT_EQ(dex_files.size(), 1U);
608 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
609 GenerateProfile(profile_location, dex_location, dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800610 std::vector<std::string> copy(extra_args);
611 copy.push_back("--profile-file=" + profile_location);
612 GenerateOdexForTest(dex_location,
613 odex_location,
614 CompilerFilter::kSpeedProfile,
615 copy,
616 /* expect_success */ true,
617 use_fd);
618 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700619
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800620 void RunTest() {
621 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
622 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
623 Copy(GetDexSrc2(), dex_location);
624
625 CompileProfileOdex(dex_location, odex_location, /* use_fd */ false);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700626
627 CheckValidity();
628 ASSERT_TRUE(success_);
629 CheckResult(dex_location, odex_location);
630 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800631
632 void RunTestVDex() {
633 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
634 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
635 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
636 Copy(GetDexSrc2(), dex_location);
637
638 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
639 CHECK(vdex_file1 != nullptr) << vdex_location;
640 ScratchFile vdex_file2;
641 {
642 std::string input_vdex = "--input-vdex-fd=-1";
643 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
644 CompileProfileOdex(dex_location,
645 odex_location,
646 /* use_fd */ true,
647 { input_vdex, output_vdex });
648 EXPECT_GT(vdex_file1->GetLength(), 0u);
649 }
650 {
651 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
652 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
653 CompileProfileOdex(dex_location,
654 odex_location,
655 /* use_fd */ true,
656 { input_vdex, output_vdex });
657 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
658 }
659 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
660 CheckValidity();
661 ASSERT_TRUE(success_);
662 CheckResult(dex_location, odex_location);
663 }
664
Jeff Hao608f2ce2016-10-19 11:17:11 -0700665 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
666 // Host/target independent checks.
667 std::string error_msg;
668 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
669 odex_location.c_str(),
670 nullptr,
671 nullptr,
672 false,
673 /*low_4gb*/false,
674 dex_location.c_str(),
675 &error_msg));
676 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
677
Jeff Hao042e8982016-10-19 11:17:11 -0700678 const char* location = dex_location.c_str();
679 std::vector<std::unique_ptr<const DexFile>> dex_files;
680 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
681 EXPECT_EQ(dex_files.size(), 1U);
682 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
683
Jeff Hao608f2ce2016-10-19 11:17:11 -0700684 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700685 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
686 ASSERT_TRUE(new_dex_file != nullptr);
687 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700688 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700689 ASSERT_GE(class_def_count, 2U);
690
691 // The new layout swaps the classes at indexes 0 and 1.
692 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
693 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
694 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
695 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
696 EXPECT_EQ(old_class0, new_class1);
697 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700698 }
699
Jeff Haoc155b052017-01-17 17:43:29 -0800700 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700701 }
702
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800703 // Check whether the dex2oat run was really successful.
704 void CheckValidity() {
705 if (kIsTargetBuild) {
706 CheckTargetValidity();
707 } else {
708 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700709 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800710 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700711
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800712 void CheckTargetValidity() {
713 // TODO: Ignore for now.
714 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700715
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800716 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
717 void CheckHostValidity() {
718 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
719 }
720};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700721
722TEST_F(Dex2oatLayoutTest, TestLayout) {
723 RunTest();
724}
725
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800726TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
727 RunTestVDex();
728}
729
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800730class Dex2oatWatchdogTest : public Dex2oatTest {
731 protected:
732 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
733 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
734 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
735
736 Copy(GetTestDexFileName(), dex_location);
737
738 std::vector<std::string> copy(extra_args);
739
740 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
741 copy.push_back("--swap-file=" + swap_location);
742 GenerateOdexForTest(dex_location,
743 odex_location,
744 CompilerFilter::kSpeed,
745 copy,
746 expect_success);
747 }
748
749 std::string GetTestDexFileName() {
750 return GetDexSrc1();
751 }
752};
753
754TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
755 // Check with default.
756 RunTest(true);
757
758 // Check with ten minutes.
759 RunTest(true, { "--watchdog-timeout=600000" });
760}
761
762TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
763 // Check with ten milliseconds.
764 RunTest(false, { "--watchdog-timeout=10" });
765}
766
Andreas Gampee1459ae2016-06-29 09:36:30 -0700767} // namespace art