blob: 6420aa87593dfb0f9cfdf5856bd1e32b6f47887f [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"
Andreas Gampef7882972017-03-20 16:35:24 -070033#include "dex2oat_return_codes.h"
Calin Juravle33083d62017-01-18 15:29:12 -080034#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070035#include "oat.h"
36#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070037#include "utils.h"
38
Andreas Gampee1459ae2016-06-29 09:36:30 -070039namespace art {
40
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080041using android::base::StringPrintf;
42
Andreas Gampee1459ae2016-06-29 09:36:30 -070043class Dex2oatTest : public Dex2oatEnvironmentTest {
44 public:
45 virtual void TearDown() OVERRIDE {
46 Dex2oatEnvironmentTest::TearDown();
47
48 output_ = "";
49 error_msg_ = "";
50 success_ = false;
51 }
52
53 protected:
Andreas Gampef7882972017-03-20 16:35:24 -070054 int GenerateOdexForTestWithStatus(const std::string& dex_location,
55 const std::string& odex_location,
56 CompilerFilter::Filter filter,
57 std::string* error_msg,
58 const std::vector<std::string>& extra_args = {},
59 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080060 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070061 std::vector<std::string> args;
62 args.push_back("--dex-file=" + dex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080063 if (use_fd) {
64 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
65 CHECK(oat_file != nullptr) << odex_location;
66 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080067 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080068 } else {
69 args.push_back("--oat-file=" + odex_location);
70 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070071 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
72 args.push_back("--runtime-arg");
73 args.push_back("-Xnorelocate");
74
75 args.insert(args.end(), extra_args.begin(), extra_args.end());
76
Andreas Gampef7882972017-03-20 16:35:24 -070077 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080078 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070079 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080080 }
Andreas Gampef7882972017-03-20 16:35:24 -070081 return status;
82 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070083
Andreas Gampef7882972017-03-20 16:35:24 -070084 void GenerateOdexForTest(const std::string& dex_location,
85 const std::string& odex_location,
86 CompilerFilter::Filter filter,
87 const std::vector<std::string>& extra_args = {},
88 bool expect_success = true,
89 bool use_fd = false) {
90 std::string error_msg;
91 int status = GenerateOdexForTestWithStatus(dex_location,
92 odex_location,
93 filter,
94 &error_msg,
95 extra_args,
96 use_fd);
97 bool success = (status == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -070098 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -080099 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700100
101 // Verify the odex file was generated as expected.
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) << error_msg;
111
112 CheckFilter(filter, odex_file->GetCompilerFilter());
113 } else {
114 ASSERT_FALSE(success) << output_;
115
116 error_msg_ = error_msg;
117
118 // Verify there's no loadable odex file.
119 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
120 odex_location.c_str(),
121 nullptr,
122 nullptr,
123 false,
124 /*low_4gb*/false,
125 dex_location.c_str(),
126 &error_msg));
127 ASSERT_TRUE(odex_file.get() == nullptr);
128 }
129 }
130
131 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
132 // in subclasses when equality is not expected.
133 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
134 EXPECT_EQ(expected, actual);
135 }
136
Andreas Gampef7882972017-03-20 16:35:24 -0700137 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700138 Runtime* runtime = Runtime::Current();
139
140 const std::vector<gc::space::ImageSpace*>& image_spaces =
141 runtime->GetHeap()->GetBootImageSpaces();
142 if (image_spaces.empty()) {
143 *error_msg = "No image location found for Dex2Oat.";
144 return false;
145 }
146 std::string image_location = image_spaces[0]->GetImageLocation();
147
148 std::vector<std::string> argv;
149 argv.push_back(runtime->GetCompilerExecutable());
150 argv.push_back("--runtime-arg");
151 argv.push_back("-classpath");
152 argv.push_back("--runtime-arg");
153 std::string class_path = runtime->GetClassPathString();
154 if (class_path == "") {
155 class_path = OatFile::kSpecialSharedLibrary;
156 }
157 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000158 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700159 argv.push_back("--debuggable");
160 }
161 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
162
163 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100164 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700165 }
166
167 if (runtime->MustRelocateIfPossible()) {
168 argv.push_back("--runtime-arg");
169 argv.push_back("-Xrelocate");
170 } else {
171 argv.push_back("--runtime-arg");
172 argv.push_back("-Xnorelocate");
173 }
174
175 if (!kIsTargetBuild) {
176 argv.push_back("--host");
177 }
178
179 argv.push_back("--boot-image=" + image_location);
180
181 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
182 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
183
184 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
185
186 // We must set --android-root.
187 const char* android_root = getenv("ANDROID_ROOT");
188 CHECK(android_root != nullptr);
189 argv.push_back("--android-root=" + std::string(android_root));
190
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100191 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700192
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100193 if (pipe(link) == -1) {
194 return false;
195 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700196
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100197 pid_t pid = fork();
198 if (pid == -1) {
199 return false;
200 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700201
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100202 if (pid == 0) {
203 // We need dex2oat to actually log things.
204 setenv("ANDROID_LOG_TAGS", "*:d", 1);
205 dup2(link[1], STDERR_FILENO);
206 close(link[0]);
207 close(link[1]);
208 std::vector<const char*> c_args;
209 for (const std::string& str : argv) {
210 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700211 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100212 c_args.push_back(nullptr);
213 execv(c_args[0], const_cast<char* const*>(c_args.data()));
214 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700215 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100216 } else {
217 close(link[1]);
218 char buffer[128];
219 memset(buffer, 0, 128);
220 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700221
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100222 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
223 output_ += std::string(buffer, bytes_read);
224 }
225 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700226 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100227 if (waitpid(pid, &status, 0) != -1) {
228 success_ = (status == 0);
229 }
Andreas Gampef7882972017-03-20 16:35:24 -0700230 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700231 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700232 }
233
234 std::string output_ = "";
235 std::string error_msg_ = "";
236 bool success_ = false;
237};
238
239class Dex2oatSwapTest : public Dex2oatTest {
240 protected:
241 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
242 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
243 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
244
Andreas Gampe7adeda82016-07-25 08:27:35 -0700245 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700246
247 std::vector<std::string> copy(extra_args);
248
249 std::unique_ptr<ScratchFile> sf;
250 if (use_fd) {
251 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800252 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700253 } else {
254 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
255 copy.push_back("--swap-file=" + swap_location);
256 }
257 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
258
259 CheckValidity();
260 ASSERT_TRUE(success_);
261 CheckResult(expect_use);
262 }
263
Andreas Gampe7adeda82016-07-25 08:27:35 -0700264 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000265 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700266 }
267
268 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700269 if (kIsTargetBuild) {
270 CheckTargetResult(expect_use);
271 } else {
272 CheckHostResult(expect_use);
273 }
274 }
275
Andreas Gampe7adeda82016-07-25 08:27:35 -0700276 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700277 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
278 // something for variants with file descriptor where we can control the lifetime of
279 // the swap file and thus take a look at it.
280 }
281
Andreas Gampe7adeda82016-07-25 08:27:35 -0700282 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700283 if (!kIsTargetBuild) {
284 if (expect_use) {
285 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
286 << output_;
287 } else {
288 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
289 << output_;
290 }
291 }
292 }
293
294 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700295 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700296 if (kIsTargetBuild) {
297 CheckTargetValidity();
298 } else {
299 CheckHostValidity();
300 }
301 }
302
Andreas Gampe7adeda82016-07-25 08:27:35 -0700303 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700304 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
305 // something for variants with file descriptor where we can control the lifetime of
306 // the swap file and thus take a look at it.
307 }
308
309 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700310 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700311 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
312 }
313};
314
315TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
316 RunTest(false /* use_fd */, false /* expect_use */);
317 RunTest(true /* use_fd */, false /* expect_use */);
318}
319
320TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
321 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
322 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
323}
324
325TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
326 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
327 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
328}
329
330TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
331 RunTest(false /* use_fd */,
332 true /* expect_use */,
333 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
334 RunTest(true /* use_fd */,
335 true /* expect_use */,
336 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
337}
338
Andreas Gampe7adeda82016-07-25 08:27:35 -0700339class Dex2oatSwapUseTest : public Dex2oatSwapTest {
340 protected:
341 void CheckHostResult(bool expect_use) OVERRIDE {
342 if (!kIsTargetBuild) {
343 if (expect_use) {
344 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
345 << output_;
346 } else {
347 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
348 << output_;
349 }
350 }
351 }
352
353 std::string GetTestDexFileName() OVERRIDE {
354 // Use Statics as it has a handful of functions.
355 return CommonRuntimeTest::GetTestDexFileName("Statics");
356 }
357
358 void GrabResult1() {
359 if (!kIsTargetBuild) {
360 native_alloc_1_ = ParseNativeAlloc();
361 swap_1_ = ParseSwap(false /* expected */);
362 } else {
363 native_alloc_1_ = std::numeric_limits<size_t>::max();
364 swap_1_ = 0;
365 }
366 }
367
368 void GrabResult2() {
369 if (!kIsTargetBuild) {
370 native_alloc_2_ = ParseNativeAlloc();
371 swap_2_ = ParseSwap(true /* expected */);
372 } else {
373 native_alloc_2_ = 0;
374 swap_2_ = std::numeric_limits<size_t>::max();
375 }
376 }
377
378 private:
379 size_t ParseNativeAlloc() {
380 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
381 std::smatch native_alloc_match;
382 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
383 if (!found) {
384 EXPECT_TRUE(found);
385 return 0;
386 }
387 if (native_alloc_match.size() != 2U) {
388 EXPECT_EQ(native_alloc_match.size(), 2U);
389 return 0;
390 }
391
392 std::istringstream stream(native_alloc_match[1].str());
393 size_t value;
394 stream >> value;
395
396 return value;
397 }
398
399 size_t ParseSwap(bool expected) {
400 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
401 std::smatch swap_match;
402 bool found = std::regex_search(output_, swap_match, swap_regex);
403 if (found != expected) {
404 EXPECT_EQ(expected, found);
405 return 0;
406 }
407
408 if (!found) {
409 return 0;
410 }
411
412 if (swap_match.size() != 2U) {
413 EXPECT_EQ(swap_match.size(), 2U);
414 return 0;
415 }
416
417 std::istringstream stream(swap_match[1].str());
418 size_t value;
419 stream >> value;
420
421 return value;
422 }
423
424 protected:
425 size_t native_alloc_1_;
426 size_t native_alloc_2_;
427
428 size_t swap_1_;
429 size_t swap_2_;
430};
431
432TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700433 // Native memory usage isn't correctly tracked under sanitization.
434 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
435
Vladimir Marko57070da2017-02-14 16:16:30 +0000436 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000437 // hold true on some x86 systems; disable this test while we
438 // investigate (b/29259363).
439 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000440
Andreas Gampe7adeda82016-07-25 08:27:35 -0700441 RunTest(false /* use_fd */,
442 false /* expect_use */);
443 GrabResult1();
444 std::string output_1 = output_;
445
446 output_ = "";
447
448 RunTest(false /* use_fd */,
449 true /* expect_use */,
450 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
451 GrabResult2();
452 std::string output_2 = output_;
453
454 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
455 EXPECT_LT(native_alloc_2_, native_alloc_1_);
456 EXPECT_LT(swap_1_, swap_2_);
457
458 LOG(ERROR) << output_1;
459 LOG(ERROR) << output_2;
460 }
461}
462
Andreas Gampe67f02822016-06-24 21:05:23 -0700463class Dex2oatVeryLargeTest : public Dex2oatTest {
464 protected:
465 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
466 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
467 // Ignore, we'll do our own checks.
468 }
469
470 void RunTest(CompilerFilter::Filter filter,
471 bool expect_large,
472 const std::vector<std::string>& extra_args = {}) {
473 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
474 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
475
476 Copy(GetDexSrc1(), dex_location);
477
Andreas Gampeca620d72016-11-08 08:09:33 -0800478 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700479
480 CheckValidity();
481 ASSERT_TRUE(success_);
482 CheckResult(dex_location, odex_location, filter, expect_large);
483 }
484
485 void CheckResult(const std::string& dex_location,
486 const std::string& odex_location,
487 CompilerFilter::Filter filter,
488 bool expect_large) {
489 // Host/target independent checks.
490 std::string error_msg;
491 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
492 odex_location.c_str(),
493 nullptr,
494 nullptr,
495 false,
496 /*low_4gb*/false,
497 dex_location.c_str(),
498 &error_msg));
499 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
500 if (expect_large) {
501 // Note: we cannot check the following:
502 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
503 // odex_file->GetCompilerFilter()));
504 // The reason is that the filter override currently happens when the dex files are
505 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
506 // store cannot be changed, and the original filter is set in stone.
507
508 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
509 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
510 ASSERT_TRUE(dex_file != nullptr);
511 uint32_t class_def_count = dex_file->NumClassDefs();
512 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
513 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
514 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
515 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
516 }
517 }
518
519 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100520 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700521 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
522 }
523 } else {
524 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
525 }
526
527 // Host/target dependent checks.
528 if (kIsTargetBuild) {
529 CheckTargetResult(expect_large);
530 } else {
531 CheckHostResult(expect_large);
532 }
533 }
534
535 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
536 // TODO: Ignore for now. May do something for fd things.
537 }
538
539 void CheckHostResult(bool expect_large) {
540 if (!kIsTargetBuild) {
541 if (expect_large) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100542 EXPECT_NE(output_.find("Very large app, downgrading to extract."),
Andreas Gampe67f02822016-06-24 21:05:23 -0700543 std::string::npos)
544 << output_;
545 } else {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100546 EXPECT_EQ(output_.find("Very large app, downgrading to extract."),
Andreas Gampe67f02822016-06-24 21:05:23 -0700547 std::string::npos)
548 << output_;
549 }
550 }
551 }
552
553 // Check whether the dex2oat run was really successful.
554 void CheckValidity() {
555 if (kIsTargetBuild) {
556 CheckTargetValidity();
557 } else {
558 CheckHostValidity();
559 }
560 }
561
562 void CheckTargetValidity() {
563 // TODO: Ignore for now.
564 }
565
566 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
567 void CheckHostValidity() {
568 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
569 }
570};
571
572TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100573 RunTest(CompilerFilter::kAssumeVerified, false);
574 RunTest(CompilerFilter::kExtract, false);
575 RunTest(CompilerFilter::kQuicken, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700576 RunTest(CompilerFilter::kSpeed, false);
577
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100578 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=1000000" });
579 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=1000000" });
580 RunTest(CompilerFilter::kQuicken, false, { "--very-large-app-threshold=1000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700581 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
582}
583
584TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100585 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=100" });
586 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=100" });
587 RunTest(CompilerFilter::kQuicken, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700588 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
589}
590
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800591// Regressin test for b/35665292.
592TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
593 // Test that dex2oat doesn't crash with speed-profile but no input profile.
594 RunTest(CompilerFilter::kSpeedProfile, false);
595}
596
Jeff Hao608f2ce2016-10-19 11:17:11 -0700597class Dex2oatLayoutTest : public Dex2oatTest {
598 protected:
599 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
600 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
601 // Ignore, we'll do our own checks.
602 }
603
Jeff Hao41fba6a2016-11-28 11:53:33 -0800604 // Emits a profile with a single dex file with the given location and a single class index of 1.
605 void GenerateProfile(const std::string& test_profile,
606 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800607 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800608 uint32_t checksum) {
609 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
610 CHECK_GE(profile_test_fd, 0);
611
612 ProfileCompilationInfo info;
613 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800614 for (size_t i = 0; i < num_classes; ++i) {
615 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i));
616 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800617 bool result = info.Save(profile_test_fd);
618 close(profile_test_fd);
619 ASSERT_TRUE(result);
620 }
621
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800622 void CompileProfileOdex(const std::string& dex_location,
623 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800624 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800625 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800626 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000627 const std::vector<std::string>& extra_args = {},
628 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800629 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800630 const char* location = dex_location.c_str();
631 std::string error_msg;
632 std::vector<std::unique_ptr<const DexFile>> dex_files;
633 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
634 EXPECT_EQ(dex_files.size(), 1U);
635 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800636 GenerateProfile(profile_location,
637 dex_location,
638 num_profile_classes,
639 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800640 std::vector<std::string> copy(extra_args);
641 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800642 std::unique_ptr<File> app_image_file;
643 if (!app_image_file_name.empty()) {
644 if (use_fd) {
645 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
646 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
647 } else {
648 copy.push_back("--app-image-file=" + app_image_file_name);
649 }
650 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800651 GenerateOdexForTest(dex_location,
652 odex_location,
653 CompilerFilter::kSpeedProfile,
654 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000655 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800656 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800657 if (app_image_file != nullptr) {
658 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
659 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800660 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700661
Mathieu Chartier046854b2017-03-01 17:16:22 -0800662 uint64_t GetImageSize(const std::string& image_file_name) {
663 EXPECT_FALSE(image_file_name.empty());
664 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
665 CHECK(file != nullptr);
666 ImageHeader image_header;
667 const bool success = file->ReadFully(&image_header, sizeof(image_header));
668 CHECK(success);
669 CHECK(image_header.IsValid());
670 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
671 return image_header.GetImageSize();
672 }
673
674 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800675 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
676 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800677 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800678 Copy(GetDexSrc2(), dex_location);
679
Mathieu Chartier046854b2017-03-01 17:16:22 -0800680 uint64_t image_file_empty_profile = 0;
681 if (app_image) {
682 CompileProfileOdex(dex_location,
683 odex_location,
684 app_image_file,
685 /* use_fd */ false,
686 /* num_profile_classes */ 0);
687 CheckValidity();
688 ASSERT_TRUE(success_);
689 // Don't check the result since CheckResult relies on the class being in the profile.
690 image_file_empty_profile = GetImageSize(app_image_file);
691 EXPECT_GT(image_file_empty_profile, 0u);
692 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700693
Mathieu Chartier046854b2017-03-01 17:16:22 -0800694 // Small profile.
695 CompileProfileOdex(dex_location,
696 odex_location,
697 app_image_file,
698 /* use_fd */ false,
699 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700700 CheckValidity();
701 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800702 CheckResult(dex_location, odex_location, app_image_file);
703
704 if (app_image) {
705 // Test that the profile made a difference by adding more classes.
706 const uint64_t image_file_small_profile = GetImageSize(app_image_file);
707 CHECK_LT(image_file_empty_profile, image_file_small_profile);
708 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700709 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800710
711 void RunTestVDex() {
712 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
713 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
714 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800715 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800716 Copy(GetDexSrc2(), dex_location);
717
718 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
719 CHECK(vdex_file1 != nullptr) << vdex_location;
720 ScratchFile vdex_file2;
721 {
722 std::string input_vdex = "--input-vdex-fd=-1";
723 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
724 CompileProfileOdex(dex_location,
725 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800726 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800727 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800728 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800729 { input_vdex, output_vdex });
730 EXPECT_GT(vdex_file1->GetLength(), 0u);
731 }
732 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000733 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800734 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
735 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
736 CompileProfileOdex(dex_location,
737 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800738 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800739 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800740 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000741 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100742 /* expect_success */ true);
743 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800744 }
745 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
746 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100747 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800748 }
749
Mathieu Chartier046854b2017-03-01 17:16:22 -0800750 void CheckResult(const std::string& dex_location,
751 const std::string& odex_location,
752 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700753 // Host/target independent checks.
754 std::string error_msg;
755 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
756 odex_location.c_str(),
757 nullptr,
758 nullptr,
759 false,
760 /*low_4gb*/false,
761 dex_location.c_str(),
762 &error_msg));
763 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
764
Jeff Hao042e8982016-10-19 11:17:11 -0700765 const char* location = dex_location.c_str();
766 std::vector<std::unique_ptr<const DexFile>> dex_files;
767 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
768 EXPECT_EQ(dex_files.size(), 1U);
769 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
770
Jeff Hao608f2ce2016-10-19 11:17:11 -0700771 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700772 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
773 ASSERT_TRUE(new_dex_file != nullptr);
774 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700775 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700776 ASSERT_GE(class_def_count, 2U);
777
778 // The new layout swaps the classes at indexes 0 and 1.
779 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
780 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
781 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
782 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
783 EXPECT_EQ(old_class0, new_class1);
784 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700785 }
786
Jeff Haoc155b052017-01-17 17:43:29 -0800787 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800788
789 if (!app_image_file_name.empty()) {
790 // Go peek at the image header to make sure it was large enough to contain the class.
791 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
792 ImageHeader image_header;
793 bool success = file->ReadFully(&image_header, sizeof(image_header));
794 ASSERT_TRUE(success);
795 ASSERT_TRUE(image_header.IsValid());
796 EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
797 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700798 }
799
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800800 // Check whether the dex2oat run was really successful.
801 void CheckValidity() {
802 if (kIsTargetBuild) {
803 CheckTargetValidity();
804 } else {
805 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700806 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800807 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700808
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800809 void CheckTargetValidity() {
810 // TODO: Ignore for now.
811 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700812
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800813 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
814 void CheckHostValidity() {
815 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
816 }
817};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700818
819TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800820 RunTest(/* app-image */ false);
821}
822
823TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
824 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700825}
826
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800827TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
828 RunTestVDex();
829}
830
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800831class Dex2oatWatchdogTest : public Dex2oatTest {
832 protected:
833 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
834 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
835 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
836
837 Copy(GetTestDexFileName(), dex_location);
838
839 std::vector<std::string> copy(extra_args);
840
841 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
842 copy.push_back("--swap-file=" + swap_location);
843 GenerateOdexForTest(dex_location,
844 odex_location,
845 CompilerFilter::kSpeed,
846 copy,
847 expect_success);
848 }
849
850 std::string GetTestDexFileName() {
851 return GetDexSrc1();
852 }
853};
854
855TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
856 // Check with default.
857 RunTest(true);
858
859 // Check with ten minutes.
860 RunTest(true, { "--watchdog-timeout=600000" });
861}
862
863TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
864 // Check with ten milliseconds.
865 RunTest(false, { "--watchdog-timeout=10" });
866}
867
Andreas Gampef7882972017-03-20 16:35:24 -0700868class Dex2oatReturnCodeTest : public Dex2oatTest {
869 protected:
870 int RunTest(const std::vector<std::string>& extra_args = {}) {
871 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
872 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
873
874 Copy(GetTestDexFileName(), dex_location);
875
876 std::string error_msg;
877 return GenerateOdexForTestWithStatus(dex_location,
878 odex_location,
879 CompilerFilter::kSpeed,
880 &error_msg,
881 extra_args);
882 }
883
884 std::string GetTestDexFileName() {
885 return GetDexSrc1();
886 }
887};
888
889TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -0700890 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -0700891 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
892 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
893}
894
Andreas Gampee1459ae2016-06-29 09:36:30 -0700895} // namespace art