blob: e7277bceae822c092b27b24d1a634b1aa35ac9b8 [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()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080066 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080067 } else {
68 args.push_back("--oat-file=" + odex_location);
69 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070070 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
71 args.push_back("--runtime-arg");
72 args.push_back("-Xnorelocate");
73
74 args.insert(args.end(), extra_args.begin(), extra_args.end());
75
76 std::string error_msg;
77 bool success = Dex2Oat(args, &error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080078 if (oat_file != nullptr) {
79 ASSERT_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
80 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070081
82 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -080083 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -070084
85 // Verify the odex file was generated as expected.
86 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
87 odex_location.c_str(),
88 nullptr,
89 nullptr,
90 false,
91 /*low_4gb*/false,
92 dex_location.c_str(),
93 &error_msg));
94 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
95
96 CheckFilter(filter, odex_file->GetCompilerFilter());
97 } else {
98 ASSERT_FALSE(success) << output_;
99
100 error_msg_ = error_msg;
101
102 // Verify there's no loadable odex file.
103 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
104 odex_location.c_str(),
105 nullptr,
106 nullptr,
107 false,
108 /*low_4gb*/false,
109 dex_location.c_str(),
110 &error_msg));
111 ASSERT_TRUE(odex_file.get() == nullptr);
112 }
113 }
114
115 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
116 // in subclasses when equality is not expected.
117 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
118 EXPECT_EQ(expected, actual);
119 }
120
121 bool Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
122 Runtime* runtime = Runtime::Current();
123
124 const std::vector<gc::space::ImageSpace*>& image_spaces =
125 runtime->GetHeap()->GetBootImageSpaces();
126 if (image_spaces.empty()) {
127 *error_msg = "No image location found for Dex2Oat.";
128 return false;
129 }
130 std::string image_location = image_spaces[0]->GetImageLocation();
131
132 std::vector<std::string> argv;
133 argv.push_back(runtime->GetCompilerExecutable());
134 argv.push_back("--runtime-arg");
135 argv.push_back("-classpath");
136 argv.push_back("--runtime-arg");
137 std::string class_path = runtime->GetClassPathString();
138 if (class_path == "") {
139 class_path = OatFile::kSpecialSharedLibrary;
140 }
141 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000142 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700143 argv.push_back("--debuggable");
144 }
145 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
146
147 if (!runtime->IsVerificationEnabled()) {
148 argv.push_back("--compiler-filter=verify-none");
149 }
150
151 if (runtime->MustRelocateIfPossible()) {
152 argv.push_back("--runtime-arg");
153 argv.push_back("-Xrelocate");
154 } else {
155 argv.push_back("--runtime-arg");
156 argv.push_back("-Xnorelocate");
157 }
158
159 if (!kIsTargetBuild) {
160 argv.push_back("--host");
161 }
162
163 argv.push_back("--boot-image=" + image_location);
164
165 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
166 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
167
168 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
169
170 // We must set --android-root.
171 const char* android_root = getenv("ANDROID_ROOT");
172 CHECK(android_root != nullptr);
173 argv.push_back("--android-root=" + std::string(android_root));
174
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100175 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700176
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100177 if (pipe(link) == -1) {
178 return false;
179 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700180
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100181 pid_t pid = fork();
182 if (pid == -1) {
183 return false;
184 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700185
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100186 if (pid == 0) {
187 // We need dex2oat to actually log things.
188 setenv("ANDROID_LOG_TAGS", "*:d", 1);
189 dup2(link[1], STDERR_FILENO);
190 close(link[0]);
191 close(link[1]);
192 std::vector<const char*> c_args;
193 for (const std::string& str : argv) {
194 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700195 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100196 c_args.push_back(nullptr);
197 execv(c_args[0], const_cast<char* const*>(c_args.data()));
198 exit(1);
199 } else {
200 close(link[1]);
201 char buffer[128];
202 memset(buffer, 0, 128);
203 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700204
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100205 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
206 output_ += std::string(buffer, bytes_read);
207 }
208 close(link[0]);
209 int status = 0;
210 if (waitpid(pid, &status, 0) != -1) {
211 success_ = (status == 0);
212 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700213 }
214 return success_;
215 }
216
217 std::string output_ = "";
218 std::string error_msg_ = "";
219 bool success_ = false;
220};
221
222class Dex2oatSwapTest : public Dex2oatTest {
223 protected:
224 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
225 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
226 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
227
Andreas Gampe7adeda82016-07-25 08:27:35 -0700228 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700229
230 std::vector<std::string> copy(extra_args);
231
232 std::unique_ptr<ScratchFile> sf;
233 if (use_fd) {
234 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800235 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700236 } else {
237 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
238 copy.push_back("--swap-file=" + swap_location);
239 }
240 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
241
242 CheckValidity();
243 ASSERT_TRUE(success_);
244 CheckResult(expect_use);
245 }
246
Andreas Gampe7adeda82016-07-25 08:27:35 -0700247 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000248 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700249 }
250
251 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700252 if (kIsTargetBuild) {
253 CheckTargetResult(expect_use);
254 } else {
255 CheckHostResult(expect_use);
256 }
257 }
258
Andreas Gampe7adeda82016-07-25 08:27:35 -0700259 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700260 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
261 // something for variants with file descriptor where we can control the lifetime of
262 // the swap file and thus take a look at it.
263 }
264
Andreas Gampe7adeda82016-07-25 08:27:35 -0700265 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700266 if (!kIsTargetBuild) {
267 if (expect_use) {
268 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
269 << output_;
270 } else {
271 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
272 << output_;
273 }
274 }
275 }
276
277 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700278 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700279 if (kIsTargetBuild) {
280 CheckTargetValidity();
281 } else {
282 CheckHostValidity();
283 }
284 }
285
Andreas Gampe7adeda82016-07-25 08:27:35 -0700286 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700287 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
288 // something for variants with file descriptor where we can control the lifetime of
289 // the swap file and thus take a look at it.
290 }
291
292 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700293 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700294 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
295 }
296};
297
298TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
299 RunTest(false /* use_fd */, false /* expect_use */);
300 RunTest(true /* use_fd */, false /* expect_use */);
301}
302
303TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
304 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
305 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
306}
307
308TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
309 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
310 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
311}
312
313TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
314 RunTest(false /* use_fd */,
315 true /* expect_use */,
316 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
317 RunTest(true /* use_fd */,
318 true /* expect_use */,
319 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
320}
321
Andreas Gampe7adeda82016-07-25 08:27:35 -0700322class Dex2oatSwapUseTest : public Dex2oatSwapTest {
323 protected:
324 void CheckHostResult(bool expect_use) OVERRIDE {
325 if (!kIsTargetBuild) {
326 if (expect_use) {
327 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
328 << output_;
329 } else {
330 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
331 << output_;
332 }
333 }
334 }
335
336 std::string GetTestDexFileName() OVERRIDE {
337 // Use Statics as it has a handful of functions.
338 return CommonRuntimeTest::GetTestDexFileName("Statics");
339 }
340
341 void GrabResult1() {
342 if (!kIsTargetBuild) {
343 native_alloc_1_ = ParseNativeAlloc();
344 swap_1_ = ParseSwap(false /* expected */);
345 } else {
346 native_alloc_1_ = std::numeric_limits<size_t>::max();
347 swap_1_ = 0;
348 }
349 }
350
351 void GrabResult2() {
352 if (!kIsTargetBuild) {
353 native_alloc_2_ = ParseNativeAlloc();
354 swap_2_ = ParseSwap(true /* expected */);
355 } else {
356 native_alloc_2_ = 0;
357 swap_2_ = std::numeric_limits<size_t>::max();
358 }
359 }
360
361 private:
362 size_t ParseNativeAlloc() {
363 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
364 std::smatch native_alloc_match;
365 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
366 if (!found) {
367 EXPECT_TRUE(found);
368 return 0;
369 }
370 if (native_alloc_match.size() != 2U) {
371 EXPECT_EQ(native_alloc_match.size(), 2U);
372 return 0;
373 }
374
375 std::istringstream stream(native_alloc_match[1].str());
376 size_t value;
377 stream >> value;
378
379 return value;
380 }
381
382 size_t ParseSwap(bool expected) {
383 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
384 std::smatch swap_match;
385 bool found = std::regex_search(output_, swap_match, swap_regex);
386 if (found != expected) {
387 EXPECT_EQ(expected, found);
388 return 0;
389 }
390
391 if (!found) {
392 return 0;
393 }
394
395 if (swap_match.size() != 2U) {
396 EXPECT_EQ(swap_match.size(), 2U);
397 return 0;
398 }
399
400 std::istringstream stream(swap_match[1].str());
401 size_t value;
402 stream >> value;
403
404 return value;
405 }
406
407 protected:
408 size_t native_alloc_1_;
409 size_t native_alloc_2_;
410
411 size_t swap_1_;
412 size_t swap_2_;
413};
414
415TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Vladimir Marko57070da2017-02-14 16:16:30 +0000416 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000417 // hold true on some x86 systems; disable this test while we
418 // investigate (b/29259363).
419 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000420
Andreas Gampe7adeda82016-07-25 08:27:35 -0700421 RunTest(false /* use_fd */,
422 false /* expect_use */);
423 GrabResult1();
424 std::string output_1 = output_;
425
426 output_ = "";
427
428 RunTest(false /* use_fd */,
429 true /* expect_use */,
430 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
431 GrabResult2();
432 std::string output_2 = output_;
433
434 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
435 EXPECT_LT(native_alloc_2_, native_alloc_1_);
436 EXPECT_LT(swap_1_, swap_2_);
437
438 LOG(ERROR) << output_1;
439 LOG(ERROR) << output_2;
440 }
441}
442
Andreas Gampe67f02822016-06-24 21:05:23 -0700443class Dex2oatVeryLargeTest : public Dex2oatTest {
444 protected:
445 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
446 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
447 // Ignore, we'll do our own checks.
448 }
449
450 void RunTest(CompilerFilter::Filter filter,
451 bool expect_large,
452 const std::vector<std::string>& extra_args = {}) {
453 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
454 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
455
456 Copy(GetDexSrc1(), dex_location);
457
Andreas Gampeca620d72016-11-08 08:09:33 -0800458 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700459
460 CheckValidity();
461 ASSERT_TRUE(success_);
462 CheckResult(dex_location, odex_location, filter, expect_large);
463 }
464
465 void CheckResult(const std::string& dex_location,
466 const std::string& odex_location,
467 CompilerFilter::Filter filter,
468 bool expect_large) {
469 // Host/target independent checks.
470 std::string error_msg;
471 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
472 odex_location.c_str(),
473 nullptr,
474 nullptr,
475 false,
476 /*low_4gb*/false,
477 dex_location.c_str(),
478 &error_msg));
479 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
480 if (expect_large) {
481 // Note: we cannot check the following:
482 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
483 // odex_file->GetCompilerFilter()));
484 // The reason is that the filter override currently happens when the dex files are
485 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
486 // store cannot be changed, and the original filter is set in stone.
487
488 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
489 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
490 ASSERT_TRUE(dex_file != nullptr);
491 uint32_t class_def_count = dex_file->NumClassDefs();
492 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
493 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
494 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
495 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
496 }
497 }
498
499 // If the input filter was "below," it should have been used.
500 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, filter)) {
501 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
502 }
503 } else {
504 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
505 }
506
507 // Host/target dependent checks.
508 if (kIsTargetBuild) {
509 CheckTargetResult(expect_large);
510 } else {
511 CheckHostResult(expect_large);
512 }
513 }
514
515 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
516 // TODO: Ignore for now. May do something for fd things.
517 }
518
519 void CheckHostResult(bool expect_large) {
520 if (!kIsTargetBuild) {
521 if (expect_large) {
522 EXPECT_NE(output_.find("Very large app, downgrading to verify-at-runtime."),
523 std::string::npos)
524 << output_;
525 } else {
526 EXPECT_EQ(output_.find("Very large app, downgrading to verify-at-runtime."),
527 std::string::npos)
528 << output_;
529 }
530 }
531 }
532
533 // Check whether the dex2oat run was really successful.
534 void CheckValidity() {
535 if (kIsTargetBuild) {
536 CheckTargetValidity();
537 } else {
538 CheckHostValidity();
539 }
540 }
541
542 void CheckTargetValidity() {
543 // TODO: Ignore for now.
544 }
545
546 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
547 void CheckHostValidity() {
548 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
549 }
550};
551
552TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
553 RunTest(CompilerFilter::kVerifyNone, false);
554 RunTest(CompilerFilter::kVerifyAtRuntime, false);
555 RunTest(CompilerFilter::kInterpretOnly, false);
556 RunTest(CompilerFilter::kSpeed, false);
557
558 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=1000000" });
559 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=1000000" });
560 RunTest(CompilerFilter::kInterpretOnly, false, { "--very-large-app-threshold=1000000" });
561 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
562}
563
564TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
565 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=100" });
566 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=100" });
567 RunTest(CompilerFilter::kInterpretOnly, true, { "--very-large-app-threshold=100" });
568 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
569}
570
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800571// Regressin test for b/35665292.
572TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
573 // Test that dex2oat doesn't crash with speed-profile but no input profile.
574 RunTest(CompilerFilter::kSpeedProfile, false);
575}
576
Jeff Hao608f2ce2016-10-19 11:17:11 -0700577class Dex2oatLayoutTest : public Dex2oatTest {
578 protected:
579 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
580 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
581 // Ignore, we'll do our own checks.
582 }
583
Jeff Hao41fba6a2016-11-28 11:53:33 -0800584 // Emits a profile with a single dex file with the given location and a single class index of 1.
585 void GenerateProfile(const std::string& test_profile,
586 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800587 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800588 uint32_t checksum) {
589 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
590 CHECK_GE(profile_test_fd, 0);
591
592 ProfileCompilationInfo info;
593 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800594 for (size_t i = 0; i < num_classes; ++i) {
595 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i));
596 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800597 bool result = info.Save(profile_test_fd);
598 close(profile_test_fd);
599 ASSERT_TRUE(result);
600 }
601
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800602 void CompileProfileOdex(const std::string& dex_location,
603 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800604 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800605 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800606 size_t num_profile_classes,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800607 const std::vector<std::string>& extra_args = {}) {
608 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800609 const char* location = dex_location.c_str();
610 std::string error_msg;
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>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800615 GenerateProfile(profile_location,
616 dex_location,
617 num_profile_classes,
618 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800619 std::vector<std::string> copy(extra_args);
620 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800621 std::unique_ptr<File> app_image_file;
622 if (!app_image_file_name.empty()) {
623 if (use_fd) {
624 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
625 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
626 } else {
627 copy.push_back("--app-image-file=" + app_image_file_name);
628 }
629 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800630 GenerateOdexForTest(dex_location,
631 odex_location,
632 CompilerFilter::kSpeedProfile,
633 copy,
634 /* expect_success */ true,
635 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800636 if (app_image_file != nullptr) {
637 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
638 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800639 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700640
Mathieu Chartier046854b2017-03-01 17:16:22 -0800641 uint64_t GetImageSize(const std::string& image_file_name) {
642 EXPECT_FALSE(image_file_name.empty());
643 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
644 CHECK(file != nullptr);
645 ImageHeader image_header;
646 const bool success = file->ReadFully(&image_header, sizeof(image_header));
647 CHECK(success);
648 CHECK(image_header.IsValid());
649 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
650 return image_header.GetImageSize();
651 }
652
653 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800654 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
655 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800656 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800657 Copy(GetDexSrc2(), dex_location);
658
Mathieu Chartier046854b2017-03-01 17:16:22 -0800659 uint64_t image_file_empty_profile = 0;
660 if (app_image) {
661 CompileProfileOdex(dex_location,
662 odex_location,
663 app_image_file,
664 /* use_fd */ false,
665 /* num_profile_classes */ 0);
666 CheckValidity();
667 ASSERT_TRUE(success_);
668 // Don't check the result since CheckResult relies on the class being in the profile.
669 image_file_empty_profile = GetImageSize(app_image_file);
670 EXPECT_GT(image_file_empty_profile, 0u);
671 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700672
Mathieu Chartier046854b2017-03-01 17:16:22 -0800673 // Small profile.
674 CompileProfileOdex(dex_location,
675 odex_location,
676 app_image_file,
677 /* use_fd */ false,
678 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700679 CheckValidity();
680 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800681 CheckResult(dex_location, odex_location, app_image_file);
682
683 if (app_image) {
684 // Test that the profile made a difference by adding more classes.
685 const uint64_t image_file_small_profile = GetImageSize(app_image_file);
686 CHECK_LT(image_file_empty_profile, image_file_small_profile);
687 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700688 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800689
690 void RunTestVDex() {
691 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
692 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
693 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800694 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800695 Copy(GetDexSrc2(), dex_location);
696
697 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
698 CHECK(vdex_file1 != nullptr) << vdex_location;
699 ScratchFile vdex_file2;
700 {
701 std::string input_vdex = "--input-vdex-fd=-1";
702 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
703 CompileProfileOdex(dex_location,
704 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800705 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800706 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800707 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800708 { input_vdex, output_vdex });
709 EXPECT_GT(vdex_file1->GetLength(), 0u);
710 }
711 {
712 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
713 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
714 CompileProfileOdex(dex_location,
715 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800716 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800717 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800718 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800719 { input_vdex, output_vdex });
720 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
721 }
722 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
723 CheckValidity();
724 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800725 CheckResult(dex_location, odex_location, app_image_file_name);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800726 }
727
Mathieu Chartier046854b2017-03-01 17:16:22 -0800728 void CheckResult(const std::string& dex_location,
729 const std::string& odex_location,
730 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700731 // Host/target independent checks.
732 std::string error_msg;
733 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
734 odex_location.c_str(),
735 nullptr,
736 nullptr,
737 false,
738 /*low_4gb*/false,
739 dex_location.c_str(),
740 &error_msg));
741 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
742
Jeff Hao042e8982016-10-19 11:17:11 -0700743 const char* location = dex_location.c_str();
744 std::vector<std::unique_ptr<const DexFile>> dex_files;
745 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
746 EXPECT_EQ(dex_files.size(), 1U);
747 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
748
Jeff Hao608f2ce2016-10-19 11:17:11 -0700749 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700750 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
751 ASSERT_TRUE(new_dex_file != nullptr);
752 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700753 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700754 ASSERT_GE(class_def_count, 2U);
755
756 // The new layout swaps the classes at indexes 0 and 1.
757 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
758 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
759 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
760 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
761 EXPECT_EQ(old_class0, new_class1);
762 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700763 }
764
Jeff Haoc155b052017-01-17 17:43:29 -0800765 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800766
767 if (!app_image_file_name.empty()) {
768 // Go peek at the image header to make sure it was large enough to contain the class.
769 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
770 ImageHeader image_header;
771 bool success = file->ReadFully(&image_header, sizeof(image_header));
772 ASSERT_TRUE(success);
773 ASSERT_TRUE(image_header.IsValid());
774 EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
775 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700776 }
777
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800778 // Check whether the dex2oat run was really successful.
779 void CheckValidity() {
780 if (kIsTargetBuild) {
781 CheckTargetValidity();
782 } else {
783 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700784 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800785 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700786
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800787 void CheckTargetValidity() {
788 // TODO: Ignore for now.
789 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700790
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800791 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
792 void CheckHostValidity() {
793 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
794 }
795};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700796
797TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800798 RunTest(/* app-image */ false);
799}
800
801TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
802 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700803}
804
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800805TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
806 RunTestVDex();
807}
808
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800809class Dex2oatWatchdogTest : public Dex2oatTest {
810 protected:
811 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
812 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
813 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
814
815 Copy(GetTestDexFileName(), dex_location);
816
817 std::vector<std::string> copy(extra_args);
818
819 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
820 copy.push_back("--swap-file=" + swap_location);
821 GenerateOdexForTest(dex_location,
822 odex_location,
823 CompilerFilter::kSpeed,
824 copy,
825 expect_success);
826 }
827
828 std::string GetTestDexFileName() {
829 return GetDexSrc1();
830 }
831};
832
833TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
834 // Check with default.
835 RunTest(true);
836
837 // Check with ten minutes.
838 RunTest(true, { "--watchdog-timeout=600000" });
839}
840
841TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
842 // Check with ten milliseconds.
843 RunTest(false, { "--watchdog-timeout=10" });
844}
845
Andreas Gampee1459ae2016-06-29 09:36:30 -0700846} // namespace art