blob: 568be7dd0d54b74078f7407edea012b61444653d [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"
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010032#include "bytecode_utils.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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "dex_file-inl.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070036#include "dex_file_loader.h"
Calin Juravle33083d62017-01-18 15:29:12 -080037#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070038#include "oat.h"
39#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070040#include "utils.h"
41
Andreas Gampee1459ae2016-06-29 09:36:30 -070042namespace art {
43
Mathieu Chartierea650f32017-05-24 12:04:13 -070044static constexpr size_t kMaxMethodIds = 65535;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070045static constexpr bool kDebugArgs = false;
Mathieu Chartierea650f32017-05-24 12:04:13 -070046
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080047using android::base::StringPrintf;
48
Andreas Gampee1459ae2016-06-29 09:36:30 -070049class Dex2oatTest : public Dex2oatEnvironmentTest {
50 public:
51 virtual void TearDown() OVERRIDE {
52 Dex2oatEnvironmentTest::TearDown();
53
54 output_ = "";
55 error_msg_ = "";
56 success_ = false;
57 }
58
59 protected:
Mathieu Chartier9e050df2017-08-09 10:05:47 -070060 int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
Andreas Gampef7882972017-03-20 16:35:24 -070061 const std::string& odex_location,
62 CompilerFilter::Filter filter,
63 std::string* error_msg,
64 const std::vector<std::string>& extra_args = {},
65 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080066 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070067 std::vector<std::string> args;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070068 // Add dex file args.
69 for (const std::string& dex_location : dex_locations) {
70 args.push_back("--dex-file=" + dex_location);
71 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080072 if (use_fd) {
73 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
74 CHECK(oat_file != nullptr) << odex_location;
75 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080076 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080077 } else {
78 args.push_back("--oat-file=" + odex_location);
79 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070080 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
81 args.push_back("--runtime-arg");
82 args.push_back("-Xnorelocate");
83
84 args.insert(args.end(), extra_args.begin(), extra_args.end());
85
Andreas Gampef7882972017-03-20 16:35:24 -070086 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080087 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070088 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080089 }
Andreas Gampef7882972017-03-20 16:35:24 -070090 return status;
91 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070092
Andreas Gampe641a4732017-08-24 13:21:35 -070093 void GenerateOdexForTest(
94 const std::string& dex_location,
95 const std::string& odex_location,
96 CompilerFilter::Filter filter,
97 const std::vector<std::string>& extra_args = {},
98 bool expect_success = true,
99 bool use_fd = false) {
100 GenerateOdexForTest(dex_location,
101 odex_location,
102 filter,
103 extra_args,
104 expect_success,
105 use_fd,
106 [](const OatFile&) {});
107 }
108
109 template <typename T>
110 void GenerateOdexForTest(
111 const std::string& dex_location,
112 const std::string& odex_location,
113 CompilerFilter::Filter filter,
114 const std::vector<std::string>& extra_args,
115 bool expect_success,
116 bool use_fd,
117 T check_oat) {
Andreas Gampef7882972017-03-20 16:35:24 -0700118 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700119 int status = GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700120 odex_location,
121 filter,
122 &error_msg,
123 extra_args,
124 use_fd);
125 bool success = (status == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700126 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800127 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700128
129 // Verify the odex file was generated as expected.
130 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
131 odex_location.c_str(),
132 nullptr,
133 nullptr,
134 false,
135 /*low_4gb*/false,
136 dex_location.c_str(),
137 &error_msg));
138 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
139
140 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700141 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700142 } else {
143 ASSERT_FALSE(success) << output_;
144
145 error_msg_ = error_msg;
146
147 // Verify there's no loadable odex file.
148 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
149 odex_location.c_str(),
150 nullptr,
151 nullptr,
152 false,
153 /*low_4gb*/false,
154 dex_location.c_str(),
155 &error_msg));
156 ASSERT_TRUE(odex_file.get() == nullptr);
157 }
158 }
159
Calin Juravle1ccf6132017-08-02 17:46:53 -0700160 // Check the input compiler filter against the generated oat file's filter. May be overridden
Andreas Gampee1459ae2016-06-29 09:36:30 -0700161 // in subclasses when equality is not expected.
162 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
163 EXPECT_EQ(expected, actual);
164 }
165
Andreas Gampef7882972017-03-20 16:35:24 -0700166 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700167 Runtime* runtime = Runtime::Current();
168
169 const std::vector<gc::space::ImageSpace*>& image_spaces =
170 runtime->GetHeap()->GetBootImageSpaces();
171 if (image_spaces.empty()) {
172 *error_msg = "No image location found for Dex2Oat.";
173 return false;
174 }
175 std::string image_location = image_spaces[0]->GetImageLocation();
176
177 std::vector<std::string> argv;
178 argv.push_back(runtime->GetCompilerExecutable());
Calin Juravle1ccf6132017-08-02 17:46:53 -0700179
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000180 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700181 argv.push_back("--debuggable");
182 }
183 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
184
185 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100186 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700187 }
188
189 if (runtime->MustRelocateIfPossible()) {
190 argv.push_back("--runtime-arg");
191 argv.push_back("-Xrelocate");
192 } else {
193 argv.push_back("--runtime-arg");
194 argv.push_back("-Xnorelocate");
195 }
196
197 if (!kIsTargetBuild) {
198 argv.push_back("--host");
199 }
200
201 argv.push_back("--boot-image=" + image_location);
202
203 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
204 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
205
206 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
207
208 // We must set --android-root.
209 const char* android_root = getenv("ANDROID_ROOT");
210 CHECK(android_root != nullptr);
211 argv.push_back("--android-root=" + std::string(android_root));
212
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700213 if (kDebugArgs) {
214 std::string all_args;
215 for (const std::string& arg : argv) {
216 all_args += arg + " ";
217 }
218 LOG(ERROR) << all_args;
219 }
220
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100221 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700222
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100223 if (pipe(link) == -1) {
224 return false;
225 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700226
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100227 pid_t pid = fork();
228 if (pid == -1) {
229 return false;
230 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700231
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100232 if (pid == 0) {
233 // We need dex2oat to actually log things.
234 setenv("ANDROID_LOG_TAGS", "*:d", 1);
235 dup2(link[1], STDERR_FILENO);
236 close(link[0]);
237 close(link[1]);
238 std::vector<const char*> c_args;
239 for (const std::string& str : argv) {
240 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700241 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100242 c_args.push_back(nullptr);
243 execv(c_args[0], const_cast<char* const*>(c_args.data()));
244 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700245 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100246 } else {
247 close(link[1]);
248 char buffer[128];
249 memset(buffer, 0, 128);
250 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700251
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100252 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
253 output_ += std::string(buffer, bytes_read);
254 }
255 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700256 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100257 if (waitpid(pid, &status, 0) != -1) {
258 success_ = (status == 0);
259 }
Andreas Gampef7882972017-03-20 16:35:24 -0700260 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700261 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700262 }
263
264 std::string output_ = "";
265 std::string error_msg_ = "";
266 bool success_ = false;
267};
268
269class Dex2oatSwapTest : public Dex2oatTest {
270 protected:
271 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
272 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
273 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
274
Andreas Gampe7adeda82016-07-25 08:27:35 -0700275 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700276
277 std::vector<std::string> copy(extra_args);
278
279 std::unique_ptr<ScratchFile> sf;
280 if (use_fd) {
281 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800282 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700283 } else {
284 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
285 copy.push_back("--swap-file=" + swap_location);
286 }
287 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
288
289 CheckValidity();
290 ASSERT_TRUE(success_);
291 CheckResult(expect_use);
292 }
293
Andreas Gampe7adeda82016-07-25 08:27:35 -0700294 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000295 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700296 }
297
298 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700299 if (kIsTargetBuild) {
300 CheckTargetResult(expect_use);
301 } else {
302 CheckHostResult(expect_use);
303 }
304 }
305
Andreas Gampe7adeda82016-07-25 08:27:35 -0700306 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700307 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
308 // something for variants with file descriptor where we can control the lifetime of
309 // the swap file and thus take a look at it.
310 }
311
Andreas Gampe7adeda82016-07-25 08:27:35 -0700312 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700313 if (!kIsTargetBuild) {
314 if (expect_use) {
315 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
316 << output_;
317 } else {
318 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
319 << output_;
320 }
321 }
322 }
323
324 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700325 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700326 if (kIsTargetBuild) {
327 CheckTargetValidity();
328 } else {
329 CheckHostValidity();
330 }
331 }
332
Andreas Gampe7adeda82016-07-25 08:27:35 -0700333 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700334 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
335 // something for variants with file descriptor where we can control the lifetime of
336 // the swap file and thus take a look at it.
337 }
338
339 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700340 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700341 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
342 }
343};
344
345TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
346 RunTest(false /* use_fd */, false /* expect_use */);
347 RunTest(true /* use_fd */, false /* expect_use */);
348}
349
350TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
351 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
352 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
353}
354
355TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
356 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
357 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
358}
359
360TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
361 RunTest(false /* use_fd */,
362 true /* expect_use */,
363 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
364 RunTest(true /* use_fd */,
365 true /* expect_use */,
366 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
367}
368
Andreas Gampe7adeda82016-07-25 08:27:35 -0700369class Dex2oatSwapUseTest : public Dex2oatSwapTest {
370 protected:
371 void CheckHostResult(bool expect_use) OVERRIDE {
372 if (!kIsTargetBuild) {
373 if (expect_use) {
374 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
375 << output_;
376 } else {
377 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
378 << output_;
379 }
380 }
381 }
382
383 std::string GetTestDexFileName() OVERRIDE {
384 // Use Statics as it has a handful of functions.
385 return CommonRuntimeTest::GetTestDexFileName("Statics");
386 }
387
388 void GrabResult1() {
389 if (!kIsTargetBuild) {
390 native_alloc_1_ = ParseNativeAlloc();
391 swap_1_ = ParseSwap(false /* expected */);
392 } else {
393 native_alloc_1_ = std::numeric_limits<size_t>::max();
394 swap_1_ = 0;
395 }
396 }
397
398 void GrabResult2() {
399 if (!kIsTargetBuild) {
400 native_alloc_2_ = ParseNativeAlloc();
401 swap_2_ = ParseSwap(true /* expected */);
402 } else {
403 native_alloc_2_ = 0;
404 swap_2_ = std::numeric_limits<size_t>::max();
405 }
406 }
407
408 private:
409 size_t ParseNativeAlloc() {
410 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
411 std::smatch native_alloc_match;
412 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
413 if (!found) {
414 EXPECT_TRUE(found);
415 return 0;
416 }
417 if (native_alloc_match.size() != 2U) {
418 EXPECT_EQ(native_alloc_match.size(), 2U);
419 return 0;
420 }
421
422 std::istringstream stream(native_alloc_match[1].str());
423 size_t value;
424 stream >> value;
425
426 return value;
427 }
428
429 size_t ParseSwap(bool expected) {
430 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
431 std::smatch swap_match;
432 bool found = std::regex_search(output_, swap_match, swap_regex);
433 if (found != expected) {
434 EXPECT_EQ(expected, found);
435 return 0;
436 }
437
438 if (!found) {
439 return 0;
440 }
441
442 if (swap_match.size() != 2U) {
443 EXPECT_EQ(swap_match.size(), 2U);
444 return 0;
445 }
446
447 std::istringstream stream(swap_match[1].str());
448 size_t value;
449 stream >> value;
450
451 return value;
452 }
453
454 protected:
455 size_t native_alloc_1_;
456 size_t native_alloc_2_;
457
458 size_t swap_1_;
459 size_t swap_2_;
460};
461
462TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700463 // Native memory usage isn't correctly tracked under sanitization.
464 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
465
Vladimir Marko57070da2017-02-14 16:16:30 +0000466 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000467 // hold true on some x86 systems; disable this test while we
468 // investigate (b/29259363).
469 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000470
Andreas Gampe7adeda82016-07-25 08:27:35 -0700471 RunTest(false /* use_fd */,
472 false /* expect_use */);
473 GrabResult1();
474 std::string output_1 = output_;
475
476 output_ = "";
477
478 RunTest(false /* use_fd */,
479 true /* expect_use */,
480 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
481 GrabResult2();
482 std::string output_2 = output_;
483
484 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
485 EXPECT_LT(native_alloc_2_, native_alloc_1_);
486 EXPECT_LT(swap_1_, swap_2_);
487
488 LOG(ERROR) << output_1;
489 LOG(ERROR) << output_2;
490 }
491}
492
Andreas Gampe67f02822016-06-24 21:05:23 -0700493class Dex2oatVeryLargeTest : public Dex2oatTest {
494 protected:
495 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
496 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
497 // Ignore, we'll do our own checks.
498 }
499
500 void RunTest(CompilerFilter::Filter filter,
501 bool expect_large,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700502 bool expect_downgrade,
Andreas Gampe67f02822016-06-24 21:05:23 -0700503 const std::vector<std::string>& extra_args = {}) {
504 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
505 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700506 std::string app_image_file = GetScratchDir() + "/Test.art";
Andreas Gampe67f02822016-06-24 21:05:23 -0700507
508 Copy(GetDexSrc1(), dex_location);
509
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700510 std::vector<std::string> new_args(extra_args);
511 new_args.push_back("--app-image-file=" + app_image_file);
512 GenerateOdexForTest(dex_location, odex_location, filter, new_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700513
514 CheckValidity();
515 ASSERT_TRUE(success_);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700516 CheckResult(dex_location,
517 odex_location,
518 app_image_file,
519 filter,
520 expect_large,
521 expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700522 }
523
524 void CheckResult(const std::string& dex_location,
525 const std::string& odex_location,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700526 const std::string& app_image_file,
Andreas Gampe67f02822016-06-24 21:05:23 -0700527 CompilerFilter::Filter filter,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700528 bool expect_large,
529 bool expect_downgrade) {
530 if (expect_downgrade) {
531 EXPECT_TRUE(expect_large);
532 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700533 // Host/target independent checks.
534 std::string error_msg;
535 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
536 odex_location.c_str(),
537 nullptr,
538 nullptr,
539 false,
540 /*low_4gb*/false,
541 dex_location.c_str(),
542 &error_msg));
543 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700544 EXPECT_GT(app_image_file.length(), 0u);
545 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700546 if (expect_large) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700547 // Note: we cannot check the following
548 // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700549 // The reason is that the filter override currently happens when the dex files are
550 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
551 // store cannot be changed, and the original filter is set in stone.
552
553 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
554 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
555 ASSERT_TRUE(dex_file != nullptr);
556 uint32_t class_def_count = dex_file->NumClassDefs();
557 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
558 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
559 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
560 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
561 }
562 }
563
564 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100565 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700566 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
567 }
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700568
569 // If expect large, make sure the app image isn't generated or is empty.
570 if (file != nullptr) {
571 EXPECT_EQ(file->GetLength(), 0u);
572 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700573 } else {
574 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700575 ASSERT_TRUE(file != nullptr) << app_image_file;
576 EXPECT_GT(file->GetLength(), 0u);
Andreas Gampe67f02822016-06-24 21:05:23 -0700577 }
578
579 // Host/target dependent checks.
580 if (kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700581 CheckTargetResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700582 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700583 CheckHostResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700584 }
585 }
586
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700587 void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700588 // TODO: Ignore for now. May do something for fd things.
589 }
590
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700591 void CheckHostResult(bool expect_downgrade) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700592 if (!kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700593 if (expect_downgrade) {
594 EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700595 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700596 EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700597 }
598 }
599 }
600
601 // Check whether the dex2oat run was really successful.
602 void CheckValidity() {
603 if (kIsTargetBuild) {
604 CheckTargetValidity();
605 } else {
606 CheckHostValidity();
607 }
608 }
609
610 void CheckTargetValidity() {
611 // TODO: Ignore for now.
612 }
613
614 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
615 void CheckHostValidity() {
616 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
617 }
618};
619
620TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700621 RunTest(CompilerFilter::kAssumeVerified, false, false);
622 RunTest(CompilerFilter::kExtract, false, false);
623 RunTest(CompilerFilter::kQuicken, false, false);
624 RunTest(CompilerFilter::kSpeed, false, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700625
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700626 RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
627 RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
628 RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" });
629 RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700630}
631
632TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700633 RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
634 RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
635 RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" });
636 RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700637}
638
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800639// Regressin test for b/35665292.
640TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
641 // Test that dex2oat doesn't crash with speed-profile but no input profile.
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700642 RunTest(CompilerFilter::kSpeedProfile, false, false);
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800643}
644
Jeff Hao608f2ce2016-10-19 11:17:11 -0700645class Dex2oatLayoutTest : public Dex2oatTest {
646 protected:
647 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
648 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
649 // Ignore, we'll do our own checks.
650 }
651
Jeff Hao41fba6a2016-11-28 11:53:33 -0800652 // Emits a profile with a single dex file with the given location and a single class index of 1.
653 void GenerateProfile(const std::string& test_profile,
654 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800655 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800656 uint32_t checksum) {
657 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
658 CHECK_GE(profile_test_fd, 0);
659
660 ProfileCompilationInfo info;
661 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800662 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700663 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800664 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800665 bool result = info.Save(profile_test_fd);
666 close(profile_test_fd);
667 ASSERT_TRUE(result);
668 }
669
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800670 void CompileProfileOdex(const std::string& dex_location,
671 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800672 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800673 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800674 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000675 const std::vector<std::string>& extra_args = {},
676 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800677 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800678 const char* location = dex_location.c_str();
679 std::string error_msg;
680 std::vector<std::unique_ptr<const DexFile>> dex_files;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700681 ASSERT_TRUE(DexFileLoader::Open(location, location, true, &error_msg, &dex_files));
Jeff Hao41fba6a2016-11-28 11:53:33 -0800682 EXPECT_EQ(dex_files.size(), 1U);
683 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800684 GenerateProfile(profile_location,
685 dex_location,
686 num_profile_classes,
687 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800688 std::vector<std::string> copy(extra_args);
689 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800690 std::unique_ptr<File> app_image_file;
691 if (!app_image_file_name.empty()) {
692 if (use_fd) {
693 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
694 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
695 } else {
696 copy.push_back("--app-image-file=" + app_image_file_name);
697 }
698 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800699 GenerateOdexForTest(dex_location,
700 odex_location,
701 CompilerFilter::kSpeedProfile,
702 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000703 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800704 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800705 if (app_image_file != nullptr) {
706 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
707 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800708 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700709
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100710 uint64_t GetImageObjectSectionSize(const std::string& image_file_name) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800711 EXPECT_FALSE(image_file_name.empty());
712 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
713 CHECK(file != nullptr);
714 ImageHeader image_header;
715 const bool success = file->ReadFully(&image_header, sizeof(image_header));
716 CHECK(success);
717 CHECK(image_header.IsValid());
718 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100719 return image_header.GetObjectsSection().Size();
Mathieu Chartier046854b2017-03-01 17:16:22 -0800720 }
721
722 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800723 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
724 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800725 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800726 Copy(GetDexSrc2(), dex_location);
727
Mathieu Chartier046854b2017-03-01 17:16:22 -0800728 uint64_t image_file_empty_profile = 0;
729 if (app_image) {
730 CompileProfileOdex(dex_location,
731 odex_location,
732 app_image_file,
733 /* use_fd */ false,
734 /* num_profile_classes */ 0);
735 CheckValidity();
736 ASSERT_TRUE(success_);
737 // Don't check the result since CheckResult relies on the class being in the profile.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100738 image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800739 EXPECT_GT(image_file_empty_profile, 0u);
740 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700741
Mathieu Chartier046854b2017-03-01 17:16:22 -0800742 // Small profile.
743 CompileProfileOdex(dex_location,
744 odex_location,
745 app_image_file,
746 /* use_fd */ false,
747 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700748 CheckValidity();
749 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800750 CheckResult(dex_location, odex_location, app_image_file);
751
752 if (app_image) {
753 // Test that the profile made a difference by adding more classes.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100754 const uint64_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
755 ASSERT_LT(image_file_empty_profile, image_file_small_profile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800756 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700757 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800758
759 void RunTestVDex() {
760 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
761 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
762 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800763 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800764 Copy(GetDexSrc2(), dex_location);
765
766 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
767 CHECK(vdex_file1 != nullptr) << vdex_location;
768 ScratchFile vdex_file2;
769 {
770 std::string input_vdex = "--input-vdex-fd=-1";
771 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
772 CompileProfileOdex(dex_location,
773 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800774 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800775 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800776 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800777 { input_vdex, output_vdex });
778 EXPECT_GT(vdex_file1->GetLength(), 0u);
779 }
780 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000781 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800782 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
783 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
784 CompileProfileOdex(dex_location,
785 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800786 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800787 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800788 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000789 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100790 /* expect_success */ true);
791 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800792 }
793 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
794 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100795 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800796 }
797
Mathieu Chartier046854b2017-03-01 17:16:22 -0800798 void CheckResult(const std::string& dex_location,
799 const std::string& odex_location,
800 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700801 // Host/target independent checks.
802 std::string error_msg;
803 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
804 odex_location.c_str(),
805 nullptr,
806 nullptr,
807 false,
808 /*low_4gb*/false,
809 dex_location.c_str(),
810 &error_msg));
811 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
812
Jeff Hao042e8982016-10-19 11:17:11 -0700813 const char* location = dex_location.c_str();
814 std::vector<std::unique_ptr<const DexFile>> dex_files;
Mathieu Chartier79c87da2017-10-10 11:54:29 -0700815 ASSERT_TRUE(DexFileLoader::Open(location, location, true, &error_msg, &dex_files));
Jeff Hao042e8982016-10-19 11:17:11 -0700816 EXPECT_EQ(dex_files.size(), 1U);
817 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
818
Jeff Hao608f2ce2016-10-19 11:17:11 -0700819 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700820 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
821 ASSERT_TRUE(new_dex_file != nullptr);
822 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700823 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700824 ASSERT_GE(class_def_count, 2U);
825
826 // The new layout swaps the classes at indexes 0 and 1.
827 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
828 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
829 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
830 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
831 EXPECT_EQ(old_class0, new_class1);
832 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700833 }
834
Jeff Haoc155b052017-01-17 17:43:29 -0800835 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800836
837 if (!app_image_file_name.empty()) {
838 // Go peek at the image header to make sure it was large enough to contain the class.
839 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
840 ImageHeader image_header;
841 bool success = file->ReadFully(&image_header, sizeof(image_header));
842 ASSERT_TRUE(success);
843 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100844 EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800845 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700846 }
847
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800848 // Check whether the dex2oat run was really successful.
849 void CheckValidity() {
850 if (kIsTargetBuild) {
851 CheckTargetValidity();
852 } else {
853 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700854 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800855 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700856
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800857 void CheckTargetValidity() {
858 // TODO: Ignore for now.
859 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700860
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800861 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
862 void CheckHostValidity() {
863 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
864 }
865};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700866
867TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800868 RunTest(/* app-image */ false);
869}
870
871TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
872 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700873}
874
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800875TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
876 RunTestVDex();
877}
878
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100879class Dex2oatUnquickenTest : public Dex2oatTest {
880 protected:
881 void RunUnquickenMultiDex() {
882 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
883 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
884 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
885 Copy(GetTestDexFileName("MultiDex"), dex_location);
886
887 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
888 CHECK(vdex_file1 != nullptr) << vdex_location;
889 // Quicken the dex file into a vdex file.
890 {
891 std::string input_vdex = "--input-vdex-fd=-1";
892 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
893 GenerateOdexForTest(dex_location,
894 odex_location,
895 CompilerFilter::kQuicken,
896 { input_vdex, output_vdex },
897 /* expect_success */ true,
898 /* use_fd */ true);
899 EXPECT_GT(vdex_file1->GetLength(), 0u);
900 }
901 // Unquicken by running the verify compiler filter on the vdex file.
902 {
903 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
904 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
905 GenerateOdexForTest(dex_location,
906 odex_location,
907 CompilerFilter::kVerify,
908 { input_vdex, output_vdex },
909 /* expect_success */ true,
910 /* use_fd */ true);
911 }
912 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
913 CheckResult(dex_location, odex_location);
914 ASSERT_TRUE(success_);
915 }
916
917 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
918 std::string error_msg;
919 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
920 odex_location.c_str(),
921 nullptr,
922 nullptr,
923 false,
924 /*low_4gb*/false,
925 dex_location.c_str(),
926 &error_msg));
927 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
928 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
929
930 // Iterate over the dex files and ensure there is no quickened instruction.
931 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
932 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
933 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
934 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
935 const uint8_t* class_data = dex_file->GetClassData(class_def);
936 if (class_data != nullptr) {
937 for (ClassDataItemIterator class_it(*dex_file, class_data);
938 class_it.HasNext();
939 class_it.Next()) {
940 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
941 for (CodeItemIterator it(*class_it.GetMethodCodeItem()); !it.Done(); it.Advance()) {
942 Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction());
943 ASSERT_FALSE(inst->IsQuickened());
944 }
945 }
946 }
947 }
948 }
949 }
950 }
951};
952
953TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
954 RunUnquickenMultiDex();
955}
956
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800957class Dex2oatWatchdogTest : public Dex2oatTest {
958 protected:
959 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
960 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
961 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
962
963 Copy(GetTestDexFileName(), dex_location);
964
965 std::vector<std::string> copy(extra_args);
966
967 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
968 copy.push_back("--swap-file=" + swap_location);
969 GenerateOdexForTest(dex_location,
970 odex_location,
971 CompilerFilter::kSpeed,
972 copy,
973 expect_success);
974 }
975
976 std::string GetTestDexFileName() {
977 return GetDexSrc1();
978 }
979};
980
981TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
982 // Check with default.
983 RunTest(true);
984
985 // Check with ten minutes.
986 RunTest(true, { "--watchdog-timeout=600000" });
987}
988
989TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Roland Levillain68db2252017-08-14 12:48:47 +0100990 TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800991 // Check with ten milliseconds.
992 RunTest(false, { "--watchdog-timeout=10" });
993}
994
Andreas Gampef7882972017-03-20 16:35:24 -0700995class Dex2oatReturnCodeTest : public Dex2oatTest {
996 protected:
997 int RunTest(const std::vector<std::string>& extra_args = {}) {
998 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
999 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1000
1001 Copy(GetTestDexFileName(), dex_location);
1002
1003 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001004 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -07001005 odex_location,
1006 CompilerFilter::kSpeed,
1007 &error_msg,
1008 extra_args);
1009 }
1010
1011 std::string GetTestDexFileName() {
1012 return GetDexSrc1();
1013 }
1014};
1015
1016TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -07001017 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -07001018 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1019 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1020}
1021
Calin Juravle1ce70852017-06-28 10:59:03 -07001022class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1023 protected:
1024 void RunTest(const char* class_loader_context,
1025 const char* expected_classpath_key,
1026 bool expected_success,
1027 bool use_second_source = false) {
1028 std::string dex_location = GetUsedDexLocation();
1029 std::string odex_location = GetUsedOatLocation();
1030
1031 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1032
1033 std::string error_msg;
1034 std::vector<std::string> extra_args;
1035 if (class_loader_context != nullptr) {
1036 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1037 }
1038 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1039 ASSERT_TRUE(expected_classpath_key != nullptr);
1040 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1041 ASSERT_TRUE(classpath != nullptr);
1042 ASSERT_STREQ(expected_classpath_key, classpath);
1043 };
1044
1045 GenerateOdexForTest(dex_location,
1046 odex_location,
1047 CompilerFilter::kQuicken,
1048 extra_args,
1049 expected_success,
1050 /*use_fd*/ false,
1051 check_oat);
1052 }
1053
1054 std::string GetUsedDexLocation() {
1055 return GetScratchDir() + "/Context.jar";
1056 }
1057
1058 std::string GetUsedOatLocation() {
1059 return GetOdexDir() + "/Context.odex";
1060 }
1061
Calin Juravle7b0648a2017-07-07 18:40:50 -07001062 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001063};
1064
1065TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1066 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1067}
1068
1069TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1070 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1071}
1072
1073TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1074 RunTest(OatFile::kSpecialSharedLibrary,
1075 OatFile::kSpecialSharedLibrary,
1076 /*expected_success*/ true);
1077}
1078
1079TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1080 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1081 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1082}
1083
1084TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1085 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001086
1087 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001088 std::string expected_classpath_key = "PCL[" +
1089 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001090 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1091}
1092
1093TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1094 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1095 Copy(GetStrippedDexSrc1(), stripped_classpath);
1096
1097 std::string context = "PCL[" + stripped_classpath + "]";
1098 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001099 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001100}
1101
1102TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1103 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1104 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1105
1106 Copy(GetDexSrc1(), stripped_classpath);
1107
1108 GenerateOdexForTest(stripped_classpath,
1109 odex_for_classpath,
1110 CompilerFilter::kQuicken,
1111 {},
1112 true);
1113
1114 // Strip the dex file
1115 Copy(GetStrippedDexSrc1(), stripped_classpath);
1116
1117 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001118 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001119 {
1120 // Open the oat file to get the expected classpath.
1121 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false);
1122 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1123 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1124 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001125 expected_classpath_key = "PCL[";
1126 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1127 if (i > 0) {
1128 expected_classpath_key + ":";
1129 }
1130 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1131 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1132 }
1133 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001134 }
1135
1136 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001137 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001138 /*expected_success*/ true,
1139 /*use_second_source*/ true);
1140}
1141
1142TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1143 std::string context = "PCL[does_not_exists.dex]";
1144 // Expect an empty context because stripped dex files cannot be open.
1145 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1146}
1147
Calin Juravlec79470d2017-07-12 17:37:42 -07001148TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1149 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1150 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1151
1152 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1153 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1154 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1155 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1156
1157 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1158}
1159
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001160class Dex2oatDeterminism : public Dex2oatTest {};
1161
1162TEST_F(Dex2oatDeterminism, UnloadCompile) {
1163 if (!kUseReadBarrier &&
1164 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1165 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1166 LOG(INFO) << "Test requires determinism support.";
1167 return;
1168 }
1169 Runtime* const runtime = Runtime::Current();
1170 std::string out_dir = GetScratchDir();
1171 const std::string base_oat_name = out_dir + "/base.oat";
1172 const std::string base_vdex_name = out_dir + "/base.vdex";
1173 const std::string unload_oat_name = out_dir + "/unload.oat";
1174 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1175 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1176 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1177 const std::string app_image_name = out_dir + "/unload.art";
1178 std::string error_msg;
1179 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1180 ASSERT_GT(spaces.size(), 0u);
1181 const std::string image_location = spaces[0]->GetImageLocation();
1182 // Without passing in an app image, it will unload in between compilations.
1183 const int res = GenerateOdexForTestWithStatus(
1184 GetLibCoreDexFileNames(),
1185 base_oat_name,
1186 CompilerFilter::Filter::kQuicken,
1187 &error_msg,
1188 {"--force-determinism", "--avoid-storing-invocation"});
1189 EXPECT_EQ(res, 0);
1190 Copy(base_oat_name, unload_oat_name);
1191 Copy(base_vdex_name, unload_vdex_name);
1192 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1193 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1194 ASSERT_TRUE(unload_oat != nullptr);
1195 ASSERT_TRUE(unload_vdex != nullptr);
1196 EXPECT_GT(unload_oat->GetLength(), 0u);
1197 EXPECT_GT(unload_vdex->GetLength(), 0u);
1198 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1199 // the same.
1200 const int res2 = GenerateOdexForTestWithStatus(
1201 GetLibCoreDexFileNames(),
1202 base_oat_name,
1203 CompilerFilter::Filter::kQuicken,
1204 &error_msg,
1205 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1206 EXPECT_EQ(res2, 0);
1207 Copy(base_oat_name, no_unload_oat_name);
1208 Copy(base_vdex_name, no_unload_vdex_name);
1209 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1210 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1211 ASSERT_TRUE(no_unload_oat != nullptr);
1212 ASSERT_TRUE(no_unload_vdex != nullptr);
1213 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1214 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1215 // Verify that both of the files are the same (odex and vdex).
1216 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1217 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1218 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1219 << unload_oat_name << " " << no_unload_oat_name;
1220 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1221 << unload_vdex_name << " " << no_unload_vdex_name;
1222 // App image file.
1223 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1224 ASSERT_TRUE(app_image_file != nullptr);
1225 EXPECT_GT(app_image_file->GetLength(), 0u);
1226}
1227
Mathieu Chartier120aa282017-08-05 16:03:03 -07001228// Test that dexlayout section info is correctly written to the oat file for profile based
1229// compilation.
1230TEST_F(Dex2oatTest, LayoutSections) {
1231 using Hotness = ProfileCompilationInfo::MethodHotness;
1232 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1233 ScratchFile profile_file;
1234 // We can only layout method indices with code items, figure out which ones have this property
1235 // first.
1236 std::vector<uint16_t> methods;
1237 {
1238 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1239 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
1240 const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx);
1241 ClassDataItemIterator it(*dex, dex->GetClassData(*class_def));
1242 it.SkipAllFields();
1243 std::set<size_t> code_item_offsets;
1244 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1245 const uint16_t method_idx = it.GetMemberIndex();
1246 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1247 if (code_item_offsets.insert(code_item_offset).second) {
1248 // Unique code item, add the method index.
1249 methods.push_back(method_idx);
1250 }
1251 }
1252 DCHECK(!it.HasNext());
1253 }
1254 ASSERT_GE(methods.size(), 8u);
1255 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1256 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1257 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1258 // Here, we build the profile from the method lists.
1259 ProfileCompilationInfo info;
1260 info.AddMethodsForDex(
1261 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1262 dex.get(),
1263 hot_methods.begin(),
1264 hot_methods.end());
1265 info.AddMethodsForDex(
1266 Hotness::kFlagStartup,
1267 dex.get(),
1268 startup_methods.begin(),
1269 startup_methods.end());
1270 info.AddMethodsForDex(
1271 Hotness::kFlagPostStartup,
1272 dex.get(),
1273 post_methods.begin(),
1274 post_methods.end());
1275 for (uint16_t id : hot_methods) {
1276 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1277 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1278 }
1279 for (uint16_t id : startup_methods) {
1280 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1281 }
1282 for (uint16_t id : post_methods) {
1283 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1284 }
1285 // Save the profile since we want to use it with dex2oat to produce an oat file.
1286 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1287 // Generate a profile based odex.
1288 const std::string dir = GetScratchDir();
1289 const std::string oat_filename = dir + "/base.oat";
1290 const std::string vdex_filename = dir + "/base.vdex";
1291 std::string error_msg;
1292 const int res = GenerateOdexForTestWithStatus(
1293 {dex->GetLocation()},
1294 oat_filename,
1295 CompilerFilter::Filter::kQuicken,
1296 &error_msg,
1297 {"--profile-file=" + profile_file.GetFilename()});
1298 EXPECT_EQ(res, 0);
1299
1300 // Open our generated oat file.
1301 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1302 oat_filename.c_str(),
1303 nullptr,
1304 nullptr,
1305 false,
1306 /*low_4gb*/false,
1307 dex->GetLocation().c_str(),
1308 &error_msg));
1309 ASSERT_TRUE(odex_file != nullptr);
1310 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1311 ASSERT_EQ(oat_dex_files.size(), 1u);
1312 // Check that the code sections match what we expect.
1313 for (const OatDexFile* oat_dex : oat_dex_files) {
1314 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1315 // Testing of logging the sections.
1316 ASSERT_TRUE(sections != nullptr);
1317 LOG(INFO) << *sections;
1318
1319 // Load the sections into temporary variables for convenience.
1320 const DexLayoutSection& code_section =
1321 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1322 const DexLayoutSection::Subsection& section_hot_code =
1323 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1324 const DexLayoutSection::Subsection& section_sometimes_used =
1325 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1326 const DexLayoutSection::Subsection& section_startup_only =
1327 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1328 const DexLayoutSection::Subsection& section_unused =
1329 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1330
1331 // All the sections should be non-empty.
1332 EXPECT_GT(section_hot_code.size_, 0u);
1333 EXPECT_GT(section_sometimes_used.size_, 0u);
1334 EXPECT_GT(section_startup_only.size_, 0u);
1335 EXPECT_GT(section_unused.size_, 0u);
1336
1337 // Open the dex file since we need to peek at the code items to verify the layout matches what
1338 // we expect.
1339 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1340 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1341 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1342 ASSERT_TRUE(type_id != nullptr);
1343 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1344 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1345 ASSERT_TRUE(class_def != nullptr);
1346
1347 // Count how many code items are for each category, there should be at least one per category.
1348 size_t hot_count = 0;
1349 size_t post_startup_count = 0;
1350 size_t startup_count = 0;
1351 size_t unused_count = 0;
1352 // Visit all of the methdos of the main class and cross reference the method indices to their
1353 // corresponding code item offsets to verify the layout.
1354 ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def));
1355 it.SkipAllFields();
1356 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1357 const size_t method_idx = it.GetMemberIndex();
1358 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1359 const bool is_hot = ContainsElement(hot_methods, method_idx);
1360 const bool is_startup = ContainsElement(startup_methods, method_idx);
1361 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1362 if (is_hot) {
1363 // Hot is highest precedence, check that the hot methods are in the hot section.
1364 EXPECT_LT(code_item_offset - section_hot_code.offset_, section_hot_code.size_);
1365 ++hot_count;
1366 } else if (is_post_startup) {
1367 // Post startup is sometimes used section.
1368 EXPECT_LT(code_item_offset - section_sometimes_used.offset_, section_sometimes_used.size_);
1369 ++post_startup_count;
1370 } else if (is_startup) {
1371 // Startup at this point means not hot or post startup, these must be startup only then.
1372 EXPECT_LT(code_item_offset - section_startup_only.offset_, section_startup_only.size_);
1373 ++startup_count;
1374 } else {
Alan Leung9595fd32017-10-17 17:08:19 -07001375 if (code_item_offset - section_unused.offset_ < section_unused.size_) {
1376 // If no flags are set, the method should be unused ...
1377 ++unused_count;
1378 } else {
1379 // or this method is part of the last code item and the end is 4 byte aligned.
1380 ClassDataItemIterator it2(*dex_file, dex_file->GetClassData(*class_def));
1381 it2.SkipAllFields();
1382 for (; it2.HasNextDirectMethod() || it2.HasNextVirtualMethod(); it2.Next()) {
1383 EXPECT_LE(it2.GetMethodCodeItemOffset(), code_item_offset);
1384 }
1385 uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx);
1386 EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u);
1387 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001388 }
1389 }
1390 DCHECK(!it.HasNext());
1391 EXPECT_GT(hot_count, 0u);
1392 EXPECT_GT(post_startup_count, 0u);
1393 EXPECT_GT(startup_count, 0u);
1394 EXPECT_GT(unused_count, 0u);
1395 }
1396}
1397
Andreas Gampef39208f2017-10-19 15:06:59 -07001398class Dex2oatVerifierAbort : public Dex2oatTest {};
1399
1400TEST_F(Dex2oatVerifierAbort, HardFail) {
1401 // Use VerifierDeps as it has hard-failing classes.
1402 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps"));
1403 std::string out_dir = GetScratchDir();
1404 const std::string base_oat_name = out_dir + "/base.oat";
1405 std::string error_msg;
1406 const int res_fail = GenerateOdexForTestWithStatus(
1407 {dex->GetLocation()},
1408 base_oat_name,
1409 CompilerFilter::Filter::kQuicken,
1410 &error_msg,
1411 {"--abort-on-hard-verifier-error"});
1412 EXPECT_NE(0, res_fail);
1413
1414 const int res_no_fail = GenerateOdexForTestWithStatus(
1415 {dex->GetLocation()},
1416 base_oat_name,
1417 CompilerFilter::Filter::kQuicken,
1418 &error_msg,
1419 {"--no-abort-on-hard-verifier-error"});
1420 EXPECT_EQ(0, res_no_fail);
1421}
1422
1423TEST_F(Dex2oatVerifierAbort, SoftFail) {
1424 // Use VerifierDepsMulti as it has hard-failing classes.
1425 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDepsMulti"));
1426 std::string out_dir = GetScratchDir();
1427 const std::string base_oat_name = out_dir + "/base.oat";
1428 std::string error_msg;
1429 const int res_fail = GenerateOdexForTestWithStatus(
1430 {dex->GetLocation()},
1431 base_oat_name,
1432 CompilerFilter::Filter::kQuicken,
1433 &error_msg,
1434 {"--abort-on-soft-verifier-error"});
1435 EXPECT_NE(0, res_fail);
1436
1437 const int res_no_fail = GenerateOdexForTestWithStatus(
1438 {dex->GetLocation()},
1439 base_oat_name,
1440 CompilerFilter::Filter::kQuicken,
1441 &error_msg,
1442 {"--no-abort-on-soft-verifier-error"});
1443 EXPECT_EQ(0, res_no_fail);
1444}
1445
Andreas Gampee1459ae2016-06-29 09:36:30 -07001446} // namespace art