blob: 40ca875fb6f95f653bfb900d15d5d76ce62d0001 [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"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "base/mutex-inl.h"
Jeff Hao608f2ce2016-10-19 11:17:11 -070032#include "dex_file-inl.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070033#include "dex2oat_environment_test.h"
Andreas Gampef7882972017-03-20 16:35:24 -070034#include "dex2oat_return_codes.h"
Calin Juravle33083d62017-01-18 15:29:12 -080035#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070036#include "oat.h"
37#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070038#include "utils.h"
39
Andreas Gampee1459ae2016-06-29 09:36:30 -070040namespace art {
41
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080042using android::base::StringPrintf;
43
Andreas Gampee1459ae2016-06-29 09:36:30 -070044class Dex2oatTest : public Dex2oatEnvironmentTest {
45 public:
46 virtual void TearDown() OVERRIDE {
47 Dex2oatEnvironmentTest::TearDown();
48
49 output_ = "";
50 error_msg_ = "";
51 success_ = false;
52 }
53
54 protected:
Andreas Gampef7882972017-03-20 16:35:24 -070055 int GenerateOdexForTestWithStatus(const std::string& dex_location,
56 const std::string& odex_location,
57 CompilerFilter::Filter filter,
58 std::string* error_msg,
59 const std::vector<std::string>& extra_args = {},
60 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080061 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070062 std::vector<std::string> args;
63 args.push_back("--dex-file=" + dex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080064 if (use_fd) {
65 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
66 CHECK(oat_file != nullptr) << odex_location;
67 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080068 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080069 } else {
70 args.push_back("--oat-file=" + odex_location);
71 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070072 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
73 args.push_back("--runtime-arg");
74 args.push_back("-Xnorelocate");
75
76 args.insert(args.end(), extra_args.begin(), extra_args.end());
77
Andreas Gampef7882972017-03-20 16:35:24 -070078 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080079 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070080 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080081 }
Andreas Gampef7882972017-03-20 16:35:24 -070082 return status;
83 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070084
Andreas Gampef7882972017-03-20 16:35:24 -070085 void GenerateOdexForTest(const std::string& dex_location,
86 const std::string& odex_location,
87 CompilerFilter::Filter filter,
88 const std::vector<std::string>& extra_args = {},
89 bool expect_success = true,
90 bool use_fd = false) {
91 std::string error_msg;
92 int status = GenerateOdexForTestWithStatus(dex_location,
93 odex_location,
94 filter,
95 &error_msg,
96 extra_args,
97 use_fd);
98 bool success = (status == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -070099 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800100 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700101
102 // Verify the odex file was generated as expected.
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) << error_msg;
112
113 CheckFilter(filter, odex_file->GetCompilerFilter());
114 } else {
115 ASSERT_FALSE(success) << output_;
116
117 error_msg_ = error_msg;
118
119 // Verify there's no loadable odex file.
120 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
121 odex_location.c_str(),
122 nullptr,
123 nullptr,
124 false,
125 /*low_4gb*/false,
126 dex_location.c_str(),
127 &error_msg));
128 ASSERT_TRUE(odex_file.get() == nullptr);
129 }
130 }
131
132 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
133 // in subclasses when equality is not expected.
134 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
135 EXPECT_EQ(expected, actual);
136 }
137
Andreas Gampef7882972017-03-20 16:35:24 -0700138 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700139 Runtime* runtime = Runtime::Current();
140
141 const std::vector<gc::space::ImageSpace*>& image_spaces =
142 runtime->GetHeap()->GetBootImageSpaces();
143 if (image_spaces.empty()) {
144 *error_msg = "No image location found for Dex2Oat.";
145 return false;
146 }
147 std::string image_location = image_spaces[0]->GetImageLocation();
148
149 std::vector<std::string> argv;
150 argv.push_back(runtime->GetCompilerExecutable());
151 argv.push_back("--runtime-arg");
152 argv.push_back("-classpath");
153 argv.push_back("--runtime-arg");
154 std::string class_path = runtime->GetClassPathString();
155 if (class_path == "") {
156 class_path = OatFile::kSpecialSharedLibrary;
157 }
158 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000159 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700160 argv.push_back("--debuggable");
161 }
162 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
163
164 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100165 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700166 }
167
168 if (runtime->MustRelocateIfPossible()) {
169 argv.push_back("--runtime-arg");
170 argv.push_back("-Xrelocate");
171 } else {
172 argv.push_back("--runtime-arg");
173 argv.push_back("-Xnorelocate");
174 }
175
176 if (!kIsTargetBuild) {
177 argv.push_back("--host");
178 }
179
180 argv.push_back("--boot-image=" + image_location);
181
182 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
183 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
184
185 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
186
187 // We must set --android-root.
188 const char* android_root = getenv("ANDROID_ROOT");
189 CHECK(android_root != nullptr);
190 argv.push_back("--android-root=" + std::string(android_root));
191
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100192 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700193
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100194 if (pipe(link) == -1) {
195 return false;
196 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700197
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100198 pid_t pid = fork();
199 if (pid == -1) {
200 return false;
201 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700202
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100203 if (pid == 0) {
204 // We need dex2oat to actually log things.
205 setenv("ANDROID_LOG_TAGS", "*:d", 1);
206 dup2(link[1], STDERR_FILENO);
207 close(link[0]);
208 close(link[1]);
209 std::vector<const char*> c_args;
210 for (const std::string& str : argv) {
211 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700212 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100213 c_args.push_back(nullptr);
214 execv(c_args[0], const_cast<char* const*>(c_args.data()));
215 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700216 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100217 } else {
218 close(link[1]);
219 char buffer[128];
220 memset(buffer, 0, 128);
221 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700222
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100223 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
224 output_ += std::string(buffer, bytes_read);
225 }
226 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700227 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100228 if (waitpid(pid, &status, 0) != -1) {
229 success_ = (status == 0);
230 }
Andreas Gampef7882972017-03-20 16:35:24 -0700231 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700232 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700233 }
234
235 std::string output_ = "";
236 std::string error_msg_ = "";
237 bool success_ = false;
238};
239
240class Dex2oatSwapTest : public Dex2oatTest {
241 protected:
242 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
243 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
244 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
245
Andreas Gampe7adeda82016-07-25 08:27:35 -0700246 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700247
248 std::vector<std::string> copy(extra_args);
249
250 std::unique_ptr<ScratchFile> sf;
251 if (use_fd) {
252 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800253 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700254 } else {
255 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
256 copy.push_back("--swap-file=" + swap_location);
257 }
258 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
259
260 CheckValidity();
261 ASSERT_TRUE(success_);
262 CheckResult(expect_use);
263 }
264
Andreas Gampe7adeda82016-07-25 08:27:35 -0700265 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000266 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700267 }
268
269 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700270 if (kIsTargetBuild) {
271 CheckTargetResult(expect_use);
272 } else {
273 CheckHostResult(expect_use);
274 }
275 }
276
Andreas Gampe7adeda82016-07-25 08:27:35 -0700277 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700278 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
279 // something for variants with file descriptor where we can control the lifetime of
280 // the swap file and thus take a look at it.
281 }
282
Andreas Gampe7adeda82016-07-25 08:27:35 -0700283 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700284 if (!kIsTargetBuild) {
285 if (expect_use) {
286 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
287 << output_;
288 } else {
289 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
290 << output_;
291 }
292 }
293 }
294
295 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700296 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700297 if (kIsTargetBuild) {
298 CheckTargetValidity();
299 } else {
300 CheckHostValidity();
301 }
302 }
303
Andreas Gampe7adeda82016-07-25 08:27:35 -0700304 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700305 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
306 // something for variants with file descriptor where we can control the lifetime of
307 // the swap file and thus take a look at it.
308 }
309
310 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700311 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700312 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
313 }
314};
315
316TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
317 RunTest(false /* use_fd */, false /* expect_use */);
318 RunTest(true /* use_fd */, false /* expect_use */);
319}
320
321TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
322 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
323 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
324}
325
326TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
327 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
328 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
329}
330
331TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
332 RunTest(false /* use_fd */,
333 true /* expect_use */,
334 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
335 RunTest(true /* use_fd */,
336 true /* expect_use */,
337 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
338}
339
Andreas Gampe7adeda82016-07-25 08:27:35 -0700340class Dex2oatSwapUseTest : public Dex2oatSwapTest {
341 protected:
342 void CheckHostResult(bool expect_use) OVERRIDE {
343 if (!kIsTargetBuild) {
344 if (expect_use) {
345 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
346 << output_;
347 } else {
348 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
349 << output_;
350 }
351 }
352 }
353
354 std::string GetTestDexFileName() OVERRIDE {
355 // Use Statics as it has a handful of functions.
356 return CommonRuntimeTest::GetTestDexFileName("Statics");
357 }
358
359 void GrabResult1() {
360 if (!kIsTargetBuild) {
361 native_alloc_1_ = ParseNativeAlloc();
362 swap_1_ = ParseSwap(false /* expected */);
363 } else {
364 native_alloc_1_ = std::numeric_limits<size_t>::max();
365 swap_1_ = 0;
366 }
367 }
368
369 void GrabResult2() {
370 if (!kIsTargetBuild) {
371 native_alloc_2_ = ParseNativeAlloc();
372 swap_2_ = ParseSwap(true /* expected */);
373 } else {
374 native_alloc_2_ = 0;
375 swap_2_ = std::numeric_limits<size_t>::max();
376 }
377 }
378
379 private:
380 size_t ParseNativeAlloc() {
381 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
382 std::smatch native_alloc_match;
383 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
384 if (!found) {
385 EXPECT_TRUE(found);
386 return 0;
387 }
388 if (native_alloc_match.size() != 2U) {
389 EXPECT_EQ(native_alloc_match.size(), 2U);
390 return 0;
391 }
392
393 std::istringstream stream(native_alloc_match[1].str());
394 size_t value;
395 stream >> value;
396
397 return value;
398 }
399
400 size_t ParseSwap(bool expected) {
401 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
402 std::smatch swap_match;
403 bool found = std::regex_search(output_, swap_match, swap_regex);
404 if (found != expected) {
405 EXPECT_EQ(expected, found);
406 return 0;
407 }
408
409 if (!found) {
410 return 0;
411 }
412
413 if (swap_match.size() != 2U) {
414 EXPECT_EQ(swap_match.size(), 2U);
415 return 0;
416 }
417
418 std::istringstream stream(swap_match[1].str());
419 size_t value;
420 stream >> value;
421
422 return value;
423 }
424
425 protected:
426 size_t native_alloc_1_;
427 size_t native_alloc_2_;
428
429 size_t swap_1_;
430 size_t swap_2_;
431};
432
433TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700434 // Native memory usage isn't correctly tracked under sanitization.
435 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
436
Vladimir Marko57070da2017-02-14 16:16:30 +0000437 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000438 // hold true on some x86 systems; disable this test while we
439 // investigate (b/29259363).
440 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000441
Andreas Gampe7adeda82016-07-25 08:27:35 -0700442 RunTest(false /* use_fd */,
443 false /* expect_use */);
444 GrabResult1();
445 std::string output_1 = output_;
446
447 output_ = "";
448
449 RunTest(false /* use_fd */,
450 true /* expect_use */,
451 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
452 GrabResult2();
453 std::string output_2 = output_;
454
455 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
456 EXPECT_LT(native_alloc_2_, native_alloc_1_);
457 EXPECT_LT(swap_1_, swap_2_);
458
459 LOG(ERROR) << output_1;
460 LOG(ERROR) << output_2;
461 }
462}
463
Andreas Gampe67f02822016-06-24 21:05:23 -0700464class Dex2oatVeryLargeTest : public Dex2oatTest {
465 protected:
466 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
467 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
468 // Ignore, we'll do our own checks.
469 }
470
471 void RunTest(CompilerFilter::Filter filter,
472 bool expect_large,
473 const std::vector<std::string>& extra_args = {}) {
474 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
475 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
476
477 Copy(GetDexSrc1(), dex_location);
478
Andreas Gampeca620d72016-11-08 08:09:33 -0800479 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700480
481 CheckValidity();
482 ASSERT_TRUE(success_);
483 CheckResult(dex_location, odex_location, filter, expect_large);
484 }
485
486 void CheckResult(const std::string& dex_location,
487 const std::string& odex_location,
488 CompilerFilter::Filter filter,
489 bool expect_large) {
490 // Host/target independent checks.
491 std::string error_msg;
492 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
493 odex_location.c_str(),
494 nullptr,
495 nullptr,
496 false,
497 /*low_4gb*/false,
498 dex_location.c_str(),
499 &error_msg));
500 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
501 if (expect_large) {
502 // Note: we cannot check the following:
503 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
504 // odex_file->GetCompilerFilter()));
505 // The reason is that the filter override currently happens when the dex files are
506 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
507 // store cannot be changed, and the original filter is set in stone.
508
509 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
510 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
511 ASSERT_TRUE(dex_file != nullptr);
512 uint32_t class_def_count = dex_file->NumClassDefs();
513 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
514 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
515 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
516 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
517 }
518 }
519
520 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100521 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700522 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
523 }
524 } else {
525 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
526 }
527
528 // Host/target dependent checks.
529 if (kIsTargetBuild) {
530 CheckTargetResult(expect_large);
531 } else {
532 CheckHostResult(expect_large);
533 }
534 }
535
536 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
537 // TODO: Ignore for now. May do something for fd things.
538 }
539
540 void CheckHostResult(bool expect_large) {
541 if (!kIsTargetBuild) {
542 if (expect_large) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100543 EXPECT_NE(output_.find("Very large app, downgrading to extract."),
Andreas Gampe67f02822016-06-24 21:05:23 -0700544 std::string::npos)
545 << output_;
546 } else {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100547 EXPECT_EQ(output_.find("Very large app, downgrading to extract."),
Andreas Gampe67f02822016-06-24 21:05:23 -0700548 std::string::npos)
549 << output_;
550 }
551 }
552 }
553
554 // Check whether the dex2oat run was really successful.
555 void CheckValidity() {
556 if (kIsTargetBuild) {
557 CheckTargetValidity();
558 } else {
559 CheckHostValidity();
560 }
561 }
562
563 void CheckTargetValidity() {
564 // TODO: Ignore for now.
565 }
566
567 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
568 void CheckHostValidity() {
569 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
570 }
571};
572
573TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100574 RunTest(CompilerFilter::kAssumeVerified, false);
575 RunTest(CompilerFilter::kExtract, false);
576 RunTest(CompilerFilter::kQuicken, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700577 RunTest(CompilerFilter::kSpeed, false);
578
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100579 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=1000000" });
580 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=1000000" });
581 RunTest(CompilerFilter::kQuicken, false, { "--very-large-app-threshold=1000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700582 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
583}
584
585TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100586 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=100" });
587 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=100" });
588 RunTest(CompilerFilter::kQuicken, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700589 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
590}
591
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800592// Regressin test for b/35665292.
593TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
594 // Test that dex2oat doesn't crash with speed-profile but no input profile.
595 RunTest(CompilerFilter::kSpeedProfile, false);
596}
597
Jeff Hao608f2ce2016-10-19 11:17:11 -0700598class Dex2oatLayoutTest : public Dex2oatTest {
599 protected:
600 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
601 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
602 // Ignore, we'll do our own checks.
603 }
604
Jeff Hao41fba6a2016-11-28 11:53:33 -0800605 // Emits a profile with a single dex file with the given location and a single class index of 1.
606 void GenerateProfile(const std::string& test_profile,
607 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800608 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800609 uint32_t checksum) {
610 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
611 CHECK_GE(profile_test_fd, 0);
612
613 ProfileCompilationInfo info;
614 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800615 for (size_t i = 0; i < num_classes; ++i) {
616 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i));
617 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800618 bool result = info.Save(profile_test_fd);
619 close(profile_test_fd);
620 ASSERT_TRUE(result);
621 }
622
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800623 void CompileProfileOdex(const std::string& dex_location,
624 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800625 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800626 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800627 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000628 const std::vector<std::string>& extra_args = {},
629 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800630 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800631 const char* location = dex_location.c_str();
632 std::string error_msg;
633 std::vector<std::unique_ptr<const DexFile>> dex_files;
634 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
635 EXPECT_EQ(dex_files.size(), 1U);
636 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800637 GenerateProfile(profile_location,
638 dex_location,
639 num_profile_classes,
640 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800641 std::vector<std::string> copy(extra_args);
642 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800643 std::unique_ptr<File> app_image_file;
644 if (!app_image_file_name.empty()) {
645 if (use_fd) {
646 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
647 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
648 } else {
649 copy.push_back("--app-image-file=" + app_image_file_name);
650 }
651 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800652 GenerateOdexForTest(dex_location,
653 odex_location,
654 CompilerFilter::kSpeedProfile,
655 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000656 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800657 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800658 if (app_image_file != nullptr) {
659 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
660 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800661 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700662
Mathieu Chartier046854b2017-03-01 17:16:22 -0800663 uint64_t GetImageSize(const std::string& image_file_name) {
664 EXPECT_FALSE(image_file_name.empty());
665 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
666 CHECK(file != nullptr);
667 ImageHeader image_header;
668 const bool success = file->ReadFully(&image_header, sizeof(image_header));
669 CHECK(success);
670 CHECK(image_header.IsValid());
671 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
672 return image_header.GetImageSize();
673 }
674
675 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800676 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
677 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800678 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800679 Copy(GetDexSrc2(), dex_location);
680
Mathieu Chartier046854b2017-03-01 17:16:22 -0800681 uint64_t image_file_empty_profile = 0;
682 if (app_image) {
683 CompileProfileOdex(dex_location,
684 odex_location,
685 app_image_file,
686 /* use_fd */ false,
687 /* num_profile_classes */ 0);
688 CheckValidity();
689 ASSERT_TRUE(success_);
690 // Don't check the result since CheckResult relies on the class being in the profile.
691 image_file_empty_profile = GetImageSize(app_image_file);
692 EXPECT_GT(image_file_empty_profile, 0u);
693 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700694
Mathieu Chartier046854b2017-03-01 17:16:22 -0800695 // Small profile.
696 CompileProfileOdex(dex_location,
697 odex_location,
698 app_image_file,
699 /* use_fd */ false,
700 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700701 CheckValidity();
702 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800703 CheckResult(dex_location, odex_location, app_image_file);
704
705 if (app_image) {
706 // Test that the profile made a difference by adding more classes.
707 const uint64_t image_file_small_profile = GetImageSize(app_image_file);
708 CHECK_LT(image_file_empty_profile, image_file_small_profile);
709 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700710 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800711
712 void RunTestVDex() {
713 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
714 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
715 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800716 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800717 Copy(GetDexSrc2(), dex_location);
718
719 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
720 CHECK(vdex_file1 != nullptr) << vdex_location;
721 ScratchFile vdex_file2;
722 {
723 std::string input_vdex = "--input-vdex-fd=-1";
724 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
725 CompileProfileOdex(dex_location,
726 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800727 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800728 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800729 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800730 { input_vdex, output_vdex });
731 EXPECT_GT(vdex_file1->GetLength(), 0u);
732 }
733 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000734 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800735 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
736 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
737 CompileProfileOdex(dex_location,
738 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800739 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800740 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800741 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000742 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100743 /* expect_success */ true);
744 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800745 }
746 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
747 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100748 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800749 }
750
Mathieu Chartier046854b2017-03-01 17:16:22 -0800751 void CheckResult(const std::string& dex_location,
752 const std::string& odex_location,
753 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700754 // Host/target independent checks.
755 std::string error_msg;
756 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
757 odex_location.c_str(),
758 nullptr,
759 nullptr,
760 false,
761 /*low_4gb*/false,
762 dex_location.c_str(),
763 &error_msg));
764 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
765
Jeff Hao042e8982016-10-19 11:17:11 -0700766 const char* location = dex_location.c_str();
767 std::vector<std::unique_ptr<const DexFile>> dex_files;
768 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
769 EXPECT_EQ(dex_files.size(), 1U);
770 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
771
Jeff Hao608f2ce2016-10-19 11:17:11 -0700772 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700773 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
774 ASSERT_TRUE(new_dex_file != nullptr);
775 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700776 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700777 ASSERT_GE(class_def_count, 2U);
778
779 // The new layout swaps the classes at indexes 0 and 1.
780 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
781 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
782 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
783 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
784 EXPECT_EQ(old_class0, new_class1);
785 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700786 }
787
Jeff Haoc155b052017-01-17 17:43:29 -0800788 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800789
790 if (!app_image_file_name.empty()) {
791 // Go peek at the image header to make sure it was large enough to contain the class.
792 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
793 ImageHeader image_header;
794 bool success = file->ReadFully(&image_header, sizeof(image_header));
795 ASSERT_TRUE(success);
796 ASSERT_TRUE(image_header.IsValid());
797 EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
798 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700799 }
800
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800801 // Check whether the dex2oat run was really successful.
802 void CheckValidity() {
803 if (kIsTargetBuild) {
804 CheckTargetValidity();
805 } else {
806 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700807 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800808 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700809
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800810 void CheckTargetValidity() {
811 // TODO: Ignore for now.
812 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700813
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800814 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
815 void CheckHostValidity() {
816 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
817 }
818};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700819
820TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800821 RunTest(/* app-image */ false);
822}
823
824TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
825 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700826}
827
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800828TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
829 RunTestVDex();
830}
831
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800832class Dex2oatWatchdogTest : public Dex2oatTest {
833 protected:
834 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
835 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
836 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
837
838 Copy(GetTestDexFileName(), dex_location);
839
840 std::vector<std::string> copy(extra_args);
841
842 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
843 copy.push_back("--swap-file=" + swap_location);
844 GenerateOdexForTest(dex_location,
845 odex_location,
846 CompilerFilter::kSpeed,
847 copy,
848 expect_success);
849 }
850
851 std::string GetTestDexFileName() {
852 return GetDexSrc1();
853 }
854};
855
856TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
857 // Check with default.
858 RunTest(true);
859
860 // Check with ten minutes.
861 RunTest(true, { "--watchdog-timeout=600000" });
862}
863
864TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
865 // Check with ten milliseconds.
866 RunTest(false, { "--watchdog-timeout=10" });
867}
868
Andreas Gampef7882972017-03-20 16:35:24 -0700869class Dex2oatReturnCodeTest : public Dex2oatTest {
870 protected:
871 int RunTest(const std::vector<std::string>& extra_args = {}) {
872 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
873 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
874
875 Copy(GetTestDexFileName(), dex_location);
876
877 std::string error_msg;
878 return GenerateOdexForTestWithStatus(dex_location,
879 odex_location,
880 CompilerFilter::kSpeed,
881 &error_msg,
882 extra_args);
883 }
884
885 std::string GetTestDexFileName() {
886 return GetDexSrc1();
887 }
888};
889
890TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -0700891 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -0700892 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
893 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
894}
895
Andreas Gampee1459ae2016-06-29 09:36:30 -0700896} // namespace art