blob: 5bf35139cba55af230f57394ec5b0c380ad7f3af [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"
Calin Juravle33083d62017-01-18 15:29:12 -080036#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070037#include "oat.h"
38#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070039#include "utils.h"
40
Andreas Gampee1459ae2016-06-29 09:36:30 -070041namespace art {
42
Mathieu Chartierea650f32017-05-24 12:04:13 -070043static constexpr size_t kMaxMethodIds = 65535;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070044static constexpr bool kDebugArgs = false;
Mathieu Chartierea650f32017-05-24 12:04:13 -070045
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080046using android::base::StringPrintf;
47
Andreas Gampee1459ae2016-06-29 09:36:30 -070048class Dex2oatTest : public Dex2oatEnvironmentTest {
49 public:
50 virtual void TearDown() OVERRIDE {
51 Dex2oatEnvironmentTest::TearDown();
52
53 output_ = "";
54 error_msg_ = "";
55 success_ = false;
56 }
57
58 protected:
Mathieu Chartier9e050df2017-08-09 10:05:47 -070059 int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
Andreas Gampef7882972017-03-20 16:35:24 -070060 const std::string& odex_location,
61 CompilerFilter::Filter filter,
62 std::string* error_msg,
63 const std::vector<std::string>& extra_args = {},
64 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080065 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070066 std::vector<std::string> args;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070067 // Add dex file args.
68 for (const std::string& dex_location : dex_locations) {
69 args.push_back("--dex-file=" + dex_location);
70 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080071 if (use_fd) {
72 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
73 CHECK(oat_file != nullptr) << odex_location;
74 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080075 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080076 } else {
77 args.push_back("--oat-file=" + odex_location);
78 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070079 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
80 args.push_back("--runtime-arg");
81 args.push_back("-Xnorelocate");
82
83 args.insert(args.end(), extra_args.begin(), extra_args.end());
84
Andreas Gampef7882972017-03-20 16:35:24 -070085 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080086 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070087 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080088 }
Andreas Gampef7882972017-03-20 16:35:24 -070089 return status;
90 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070091
Andreas Gampe641a4732017-08-24 13:21:35 -070092 void GenerateOdexForTest(
93 const std::string& dex_location,
94 const std::string& odex_location,
95 CompilerFilter::Filter filter,
96 const std::vector<std::string>& extra_args = {},
97 bool expect_success = true,
98 bool use_fd = false) {
99 GenerateOdexForTest(dex_location,
100 odex_location,
101 filter,
102 extra_args,
103 expect_success,
104 use_fd,
105 [](const OatFile&) {});
106 }
107
108 template <typename T>
109 void GenerateOdexForTest(
110 const std::string& dex_location,
111 const std::string& odex_location,
112 CompilerFilter::Filter filter,
113 const std::vector<std::string>& extra_args,
114 bool expect_success,
115 bool use_fd,
116 T check_oat) {
Andreas Gampef7882972017-03-20 16:35:24 -0700117 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700118 int status = GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700119 odex_location,
120 filter,
121 &error_msg,
122 extra_args,
123 use_fd);
124 bool success = (status == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700125 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800126 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700127
128 // Verify the odex file was generated as expected.
129 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
130 odex_location.c_str(),
131 nullptr,
132 nullptr,
133 false,
134 /*low_4gb*/false,
135 dex_location.c_str(),
136 &error_msg));
137 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
138
139 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700140 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700141 } else {
142 ASSERT_FALSE(success) << output_;
143
144 error_msg_ = error_msg;
145
146 // Verify there's no loadable odex file.
147 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
148 odex_location.c_str(),
149 nullptr,
150 nullptr,
151 false,
152 /*low_4gb*/false,
153 dex_location.c_str(),
154 &error_msg));
155 ASSERT_TRUE(odex_file.get() == nullptr);
156 }
157 }
158
Calin Juravle1ccf6132017-08-02 17:46:53 -0700159 // Check the input compiler filter against the generated oat file's filter. May be overridden
Andreas Gampee1459ae2016-06-29 09:36:30 -0700160 // in subclasses when equality is not expected.
161 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
162 EXPECT_EQ(expected, actual);
163 }
164
Andreas Gampef7882972017-03-20 16:35:24 -0700165 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700166 Runtime* runtime = Runtime::Current();
167
168 const std::vector<gc::space::ImageSpace*>& image_spaces =
169 runtime->GetHeap()->GetBootImageSpaces();
170 if (image_spaces.empty()) {
171 *error_msg = "No image location found for Dex2Oat.";
172 return false;
173 }
174 std::string image_location = image_spaces[0]->GetImageLocation();
175
176 std::vector<std::string> argv;
177 argv.push_back(runtime->GetCompilerExecutable());
Calin Juravle1ccf6132017-08-02 17:46:53 -0700178
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000179 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700180 argv.push_back("--debuggable");
181 }
182 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
183
184 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100185 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700186 }
187
188 if (runtime->MustRelocateIfPossible()) {
189 argv.push_back("--runtime-arg");
190 argv.push_back("-Xrelocate");
191 } else {
192 argv.push_back("--runtime-arg");
193 argv.push_back("-Xnorelocate");
194 }
195
196 if (!kIsTargetBuild) {
197 argv.push_back("--host");
198 }
199
200 argv.push_back("--boot-image=" + image_location);
201
202 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
203 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
204
205 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
206
207 // We must set --android-root.
208 const char* android_root = getenv("ANDROID_ROOT");
209 CHECK(android_root != nullptr);
210 argv.push_back("--android-root=" + std::string(android_root));
211
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700212 if (kDebugArgs) {
213 std::string all_args;
214 for (const std::string& arg : argv) {
215 all_args += arg + " ";
216 }
217 LOG(ERROR) << all_args;
218 }
219
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100220 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700221
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100222 if (pipe(link) == -1) {
223 return false;
224 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700225
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100226 pid_t pid = fork();
227 if (pid == -1) {
228 return false;
229 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700230
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100231 if (pid == 0) {
232 // We need dex2oat to actually log things.
233 setenv("ANDROID_LOG_TAGS", "*:d", 1);
234 dup2(link[1], STDERR_FILENO);
235 close(link[0]);
236 close(link[1]);
237 std::vector<const char*> c_args;
238 for (const std::string& str : argv) {
239 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700240 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100241 c_args.push_back(nullptr);
242 execv(c_args[0], const_cast<char* const*>(c_args.data()));
243 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700244 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100245 } else {
246 close(link[1]);
247 char buffer[128];
248 memset(buffer, 0, 128);
249 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700250
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100251 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
252 output_ += std::string(buffer, bytes_read);
253 }
254 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700255 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100256 if (waitpid(pid, &status, 0) != -1) {
257 success_ = (status == 0);
258 }
Andreas Gampef7882972017-03-20 16:35:24 -0700259 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700260 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700261 }
262
263 std::string output_ = "";
264 std::string error_msg_ = "";
265 bool success_ = false;
266};
267
268class Dex2oatSwapTest : public Dex2oatTest {
269 protected:
270 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
271 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
272 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
273
Andreas Gampe7adeda82016-07-25 08:27:35 -0700274 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700275
276 std::vector<std::string> copy(extra_args);
277
278 std::unique_ptr<ScratchFile> sf;
279 if (use_fd) {
280 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800281 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700282 } else {
283 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
284 copy.push_back("--swap-file=" + swap_location);
285 }
286 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
287
288 CheckValidity();
289 ASSERT_TRUE(success_);
290 CheckResult(expect_use);
291 }
292
Andreas Gampe7adeda82016-07-25 08:27:35 -0700293 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000294 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700295 }
296
297 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700298 if (kIsTargetBuild) {
299 CheckTargetResult(expect_use);
300 } else {
301 CheckHostResult(expect_use);
302 }
303 }
304
Andreas Gampe7adeda82016-07-25 08:27:35 -0700305 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700306 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
307 // something for variants with file descriptor where we can control the lifetime of
308 // the swap file and thus take a look at it.
309 }
310
Andreas Gampe7adeda82016-07-25 08:27:35 -0700311 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700312 if (!kIsTargetBuild) {
313 if (expect_use) {
314 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
315 << output_;
316 } else {
317 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
318 << output_;
319 }
320 }
321 }
322
323 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700324 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700325 if (kIsTargetBuild) {
326 CheckTargetValidity();
327 } else {
328 CheckHostValidity();
329 }
330 }
331
Andreas Gampe7adeda82016-07-25 08:27:35 -0700332 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700333 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
334 // something for variants with file descriptor where we can control the lifetime of
335 // the swap file and thus take a look at it.
336 }
337
338 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700339 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700340 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
341 }
342};
343
344TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
345 RunTest(false /* use_fd */, false /* expect_use */);
346 RunTest(true /* use_fd */, false /* expect_use */);
347}
348
349TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
350 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
351 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
352}
353
354TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
355 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
356 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
357}
358
359TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
360 RunTest(false /* use_fd */,
361 true /* expect_use */,
362 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
363 RunTest(true /* use_fd */,
364 true /* expect_use */,
365 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
366}
367
Andreas Gampe7adeda82016-07-25 08:27:35 -0700368class Dex2oatSwapUseTest : public Dex2oatSwapTest {
369 protected:
370 void CheckHostResult(bool expect_use) OVERRIDE {
371 if (!kIsTargetBuild) {
372 if (expect_use) {
373 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
374 << output_;
375 } else {
376 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
377 << output_;
378 }
379 }
380 }
381
382 std::string GetTestDexFileName() OVERRIDE {
383 // Use Statics as it has a handful of functions.
384 return CommonRuntimeTest::GetTestDexFileName("Statics");
385 }
386
387 void GrabResult1() {
388 if (!kIsTargetBuild) {
389 native_alloc_1_ = ParseNativeAlloc();
390 swap_1_ = ParseSwap(false /* expected */);
391 } else {
392 native_alloc_1_ = std::numeric_limits<size_t>::max();
393 swap_1_ = 0;
394 }
395 }
396
397 void GrabResult2() {
398 if (!kIsTargetBuild) {
399 native_alloc_2_ = ParseNativeAlloc();
400 swap_2_ = ParseSwap(true /* expected */);
401 } else {
402 native_alloc_2_ = 0;
403 swap_2_ = std::numeric_limits<size_t>::max();
404 }
405 }
406
407 private:
408 size_t ParseNativeAlloc() {
409 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
410 std::smatch native_alloc_match;
411 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
412 if (!found) {
413 EXPECT_TRUE(found);
414 return 0;
415 }
416 if (native_alloc_match.size() != 2U) {
417 EXPECT_EQ(native_alloc_match.size(), 2U);
418 return 0;
419 }
420
421 std::istringstream stream(native_alloc_match[1].str());
422 size_t value;
423 stream >> value;
424
425 return value;
426 }
427
428 size_t ParseSwap(bool expected) {
429 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
430 std::smatch swap_match;
431 bool found = std::regex_search(output_, swap_match, swap_regex);
432 if (found != expected) {
433 EXPECT_EQ(expected, found);
434 return 0;
435 }
436
437 if (!found) {
438 return 0;
439 }
440
441 if (swap_match.size() != 2U) {
442 EXPECT_EQ(swap_match.size(), 2U);
443 return 0;
444 }
445
446 std::istringstream stream(swap_match[1].str());
447 size_t value;
448 stream >> value;
449
450 return value;
451 }
452
453 protected:
454 size_t native_alloc_1_;
455 size_t native_alloc_2_;
456
457 size_t swap_1_;
458 size_t swap_2_;
459};
460
461TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700462 // Native memory usage isn't correctly tracked under sanitization.
463 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
464
Vladimir Marko57070da2017-02-14 16:16:30 +0000465 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000466 // hold true on some x86 systems; disable this test while we
467 // investigate (b/29259363).
468 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000469
Andreas Gampe7adeda82016-07-25 08:27:35 -0700470 RunTest(false /* use_fd */,
471 false /* expect_use */);
472 GrabResult1();
473 std::string output_1 = output_;
474
475 output_ = "";
476
477 RunTest(false /* use_fd */,
478 true /* expect_use */,
479 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
480 GrabResult2();
481 std::string output_2 = output_;
482
483 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
484 EXPECT_LT(native_alloc_2_, native_alloc_1_);
485 EXPECT_LT(swap_1_, swap_2_);
486
487 LOG(ERROR) << output_1;
488 LOG(ERROR) << output_2;
489 }
490}
491
Andreas Gampe67f02822016-06-24 21:05:23 -0700492class Dex2oatVeryLargeTest : public Dex2oatTest {
493 protected:
494 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
495 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
496 // Ignore, we'll do our own checks.
497 }
498
499 void RunTest(CompilerFilter::Filter filter,
500 bool expect_large,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700501 bool expect_downgrade,
Andreas Gampe67f02822016-06-24 21:05:23 -0700502 const std::vector<std::string>& extra_args = {}) {
503 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
504 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700505 std::string app_image_file = GetScratchDir() + "/Test.art";
Andreas Gampe67f02822016-06-24 21:05:23 -0700506
507 Copy(GetDexSrc1(), dex_location);
508
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700509 std::vector<std::string> new_args(extra_args);
510 new_args.push_back("--app-image-file=" + app_image_file);
511 GenerateOdexForTest(dex_location, odex_location, filter, new_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700512
513 CheckValidity();
514 ASSERT_TRUE(success_);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700515 CheckResult(dex_location,
516 odex_location,
517 app_image_file,
518 filter,
519 expect_large,
520 expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700521 }
522
523 void CheckResult(const std::string& dex_location,
524 const std::string& odex_location,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700525 const std::string& app_image_file,
Andreas Gampe67f02822016-06-24 21:05:23 -0700526 CompilerFilter::Filter filter,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700527 bool expect_large,
528 bool expect_downgrade) {
529 if (expect_downgrade) {
530 EXPECT_TRUE(expect_large);
531 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700532 // Host/target independent checks.
533 std::string error_msg;
534 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
535 odex_location.c_str(),
536 nullptr,
537 nullptr,
538 false,
539 /*low_4gb*/false,
540 dex_location.c_str(),
541 &error_msg));
542 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700543 EXPECT_GT(app_image_file.length(), 0u);
544 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700545 if (expect_large) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700546 // Note: we cannot check the following
547 // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700548 // The reason is that the filter override currently happens when the dex files are
549 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
550 // store cannot be changed, and the original filter is set in stone.
551
552 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
553 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
554 ASSERT_TRUE(dex_file != nullptr);
555 uint32_t class_def_count = dex_file->NumClassDefs();
556 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
557 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
558 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
559 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
560 }
561 }
562
563 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100564 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700565 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
566 }
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700567
568 // If expect large, make sure the app image isn't generated or is empty.
569 if (file != nullptr) {
570 EXPECT_EQ(file->GetLength(), 0u);
571 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700572 } else {
573 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700574 ASSERT_TRUE(file != nullptr) << app_image_file;
575 EXPECT_GT(file->GetLength(), 0u);
Andreas Gampe67f02822016-06-24 21:05:23 -0700576 }
577
578 // Host/target dependent checks.
579 if (kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700580 CheckTargetResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700581 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700582 CheckHostResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700583 }
584 }
585
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700586 void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700587 // TODO: Ignore for now. May do something for fd things.
588 }
589
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700590 void CheckHostResult(bool expect_downgrade) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700591 if (!kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700592 if (expect_downgrade) {
593 EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700594 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700595 EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700596 }
597 }
598 }
599
600 // Check whether the dex2oat run was really successful.
601 void CheckValidity() {
602 if (kIsTargetBuild) {
603 CheckTargetValidity();
604 } else {
605 CheckHostValidity();
606 }
607 }
608
609 void CheckTargetValidity() {
610 // TODO: Ignore for now.
611 }
612
613 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
614 void CheckHostValidity() {
615 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
616 }
617};
618
619TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700620 RunTest(CompilerFilter::kAssumeVerified, false, false);
621 RunTest(CompilerFilter::kExtract, false, false);
622 RunTest(CompilerFilter::kQuicken, false, false);
623 RunTest(CompilerFilter::kSpeed, false, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700624
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700625 RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
626 RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
627 RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" });
628 RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700629}
630
631TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700632 RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
633 RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
634 RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" });
635 RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700636}
637
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800638// Regressin test for b/35665292.
639TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
640 // Test that dex2oat doesn't crash with speed-profile but no input profile.
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700641 RunTest(CompilerFilter::kSpeedProfile, false, false);
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800642}
643
Jeff Hao608f2ce2016-10-19 11:17:11 -0700644class Dex2oatLayoutTest : public Dex2oatTest {
645 protected:
646 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
647 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
648 // Ignore, we'll do our own checks.
649 }
650
Jeff Hao41fba6a2016-11-28 11:53:33 -0800651 // Emits a profile with a single dex file with the given location and a single class index of 1.
652 void GenerateProfile(const std::string& test_profile,
653 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800654 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800655 uint32_t checksum) {
656 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
657 CHECK_GE(profile_test_fd, 0);
658
659 ProfileCompilationInfo info;
660 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800661 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700662 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800663 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800664 bool result = info.Save(profile_test_fd);
665 close(profile_test_fd);
666 ASSERT_TRUE(result);
667 }
668
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800669 void CompileProfileOdex(const std::string& dex_location,
670 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800671 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800672 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800673 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000674 const std::vector<std::string>& extra_args = {},
675 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800676 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800677 const char* location = dex_location.c_str();
678 std::string error_msg;
679 std::vector<std::unique_ptr<const DexFile>> dex_files;
680 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
681 EXPECT_EQ(dex_files.size(), 1U);
682 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800683 GenerateProfile(profile_location,
684 dex_location,
685 num_profile_classes,
686 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800687 std::vector<std::string> copy(extra_args);
688 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800689 std::unique_ptr<File> app_image_file;
690 if (!app_image_file_name.empty()) {
691 if (use_fd) {
692 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
693 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
694 } else {
695 copy.push_back("--app-image-file=" + app_image_file_name);
696 }
697 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800698 GenerateOdexForTest(dex_location,
699 odex_location,
700 CompilerFilter::kSpeedProfile,
701 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000702 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800703 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800704 if (app_image_file != nullptr) {
705 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
706 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800707 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700708
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100709 uint64_t GetImageObjectSectionSize(const std::string& image_file_name) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800710 EXPECT_FALSE(image_file_name.empty());
711 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
712 CHECK(file != nullptr);
713 ImageHeader image_header;
714 const bool success = file->ReadFully(&image_header, sizeof(image_header));
715 CHECK(success);
716 CHECK(image_header.IsValid());
717 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100718 return image_header.GetObjectsSection().Size();
Mathieu Chartier046854b2017-03-01 17:16:22 -0800719 }
720
721 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800722 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
723 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800724 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800725 Copy(GetDexSrc2(), dex_location);
726
Mathieu Chartier046854b2017-03-01 17:16:22 -0800727 uint64_t image_file_empty_profile = 0;
728 if (app_image) {
729 CompileProfileOdex(dex_location,
730 odex_location,
731 app_image_file,
732 /* use_fd */ false,
733 /* num_profile_classes */ 0);
734 CheckValidity();
735 ASSERT_TRUE(success_);
736 // Don't check the result since CheckResult relies on the class being in the profile.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100737 image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800738 EXPECT_GT(image_file_empty_profile, 0u);
739 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700740
Mathieu Chartier046854b2017-03-01 17:16:22 -0800741 // Small profile.
742 CompileProfileOdex(dex_location,
743 odex_location,
744 app_image_file,
745 /* use_fd */ false,
746 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700747 CheckValidity();
748 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800749 CheckResult(dex_location, odex_location, app_image_file);
750
751 if (app_image) {
752 // Test that the profile made a difference by adding more classes.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100753 const uint64_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
754 ASSERT_LT(image_file_empty_profile, image_file_small_profile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800755 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700756 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800757
758 void RunTestVDex() {
759 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
760 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
761 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800762 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800763 Copy(GetDexSrc2(), dex_location);
764
765 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
766 CHECK(vdex_file1 != nullptr) << vdex_location;
767 ScratchFile vdex_file2;
768 {
769 std::string input_vdex = "--input-vdex-fd=-1";
770 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
771 CompileProfileOdex(dex_location,
772 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800773 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800774 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800775 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800776 { input_vdex, output_vdex });
777 EXPECT_GT(vdex_file1->GetLength(), 0u);
778 }
779 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000780 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800781 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
782 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
783 CompileProfileOdex(dex_location,
784 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800785 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800786 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800787 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000788 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100789 /* expect_success */ true);
790 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800791 }
792 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
793 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100794 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800795 }
796
Mathieu Chartier046854b2017-03-01 17:16:22 -0800797 void CheckResult(const std::string& dex_location,
798 const std::string& odex_location,
799 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700800 // Host/target independent checks.
801 std::string error_msg;
802 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
803 odex_location.c_str(),
804 nullptr,
805 nullptr,
806 false,
807 /*low_4gb*/false,
808 dex_location.c_str(),
809 &error_msg));
810 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
811
Jeff Hao042e8982016-10-19 11:17:11 -0700812 const char* location = dex_location.c_str();
813 std::vector<std::unique_ptr<const DexFile>> dex_files;
814 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
815 EXPECT_EQ(dex_files.size(), 1U);
816 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
817
Jeff Hao608f2ce2016-10-19 11:17:11 -0700818 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700819 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
820 ASSERT_TRUE(new_dex_file != nullptr);
821 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700822 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700823 ASSERT_GE(class_def_count, 2U);
824
825 // The new layout swaps the classes at indexes 0 and 1.
826 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
827 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
828 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
829 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
830 EXPECT_EQ(old_class0, new_class1);
831 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700832 }
833
Jeff Haoc155b052017-01-17 17:43:29 -0800834 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800835
836 if (!app_image_file_name.empty()) {
837 // Go peek at the image header to make sure it was large enough to contain the class.
838 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
839 ImageHeader image_header;
840 bool success = file->ReadFully(&image_header, sizeof(image_header));
841 ASSERT_TRUE(success);
842 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100843 EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800844 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700845 }
846
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800847 // Check whether the dex2oat run was really successful.
848 void CheckValidity() {
849 if (kIsTargetBuild) {
850 CheckTargetValidity();
851 } else {
852 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700853 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800854 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700855
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800856 void CheckTargetValidity() {
857 // TODO: Ignore for now.
858 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700859
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800860 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
861 void CheckHostValidity() {
862 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
863 }
864};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700865
866TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800867 RunTest(/* app-image */ false);
868}
869
870TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
871 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700872}
873
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800874TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
875 RunTestVDex();
876}
877
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100878class Dex2oatUnquickenTest : public Dex2oatTest {
879 protected:
880 void RunUnquickenMultiDex() {
881 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
882 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
883 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
884 Copy(GetTestDexFileName("MultiDex"), dex_location);
885
886 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
887 CHECK(vdex_file1 != nullptr) << vdex_location;
888 // Quicken the dex file into a vdex file.
889 {
890 std::string input_vdex = "--input-vdex-fd=-1";
891 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
892 GenerateOdexForTest(dex_location,
893 odex_location,
894 CompilerFilter::kQuicken,
895 { input_vdex, output_vdex },
896 /* expect_success */ true,
897 /* use_fd */ true);
898 EXPECT_GT(vdex_file1->GetLength(), 0u);
899 }
900 // Unquicken by running the verify compiler filter on the vdex file.
901 {
902 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
903 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
904 GenerateOdexForTest(dex_location,
905 odex_location,
906 CompilerFilter::kVerify,
907 { input_vdex, output_vdex },
908 /* expect_success */ true,
909 /* use_fd */ true);
910 }
911 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
912 CheckResult(dex_location, odex_location);
913 ASSERT_TRUE(success_);
914 }
915
916 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
917 std::string error_msg;
918 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
919 odex_location.c_str(),
920 nullptr,
921 nullptr,
922 false,
923 /*low_4gb*/false,
924 dex_location.c_str(),
925 &error_msg));
926 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
927 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
928
929 // Iterate over the dex files and ensure there is no quickened instruction.
930 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
931 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
932 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
933 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
934 const uint8_t* class_data = dex_file->GetClassData(class_def);
935 if (class_data != nullptr) {
936 for (ClassDataItemIterator class_it(*dex_file, class_data);
937 class_it.HasNext();
938 class_it.Next()) {
939 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
940 for (CodeItemIterator it(*class_it.GetMethodCodeItem()); !it.Done(); it.Advance()) {
941 Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction());
942 ASSERT_FALSE(inst->IsQuickened());
943 }
944 }
945 }
946 }
947 }
948 }
949 }
950};
951
952TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
953 RunUnquickenMultiDex();
954}
955
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800956class Dex2oatWatchdogTest : public Dex2oatTest {
957 protected:
958 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
959 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
960 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
961
962 Copy(GetTestDexFileName(), dex_location);
963
964 std::vector<std::string> copy(extra_args);
965
966 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
967 copy.push_back("--swap-file=" + swap_location);
968 GenerateOdexForTest(dex_location,
969 odex_location,
970 CompilerFilter::kSpeed,
971 copy,
972 expect_success);
973 }
974
975 std::string GetTestDexFileName() {
976 return GetDexSrc1();
977 }
978};
979
980TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
981 // Check with default.
982 RunTest(true);
983
984 // Check with ten minutes.
985 RunTest(true, { "--watchdog-timeout=600000" });
986}
987
988TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Roland Levillain68db2252017-08-14 12:48:47 +0100989 TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800990 // Check with ten milliseconds.
991 RunTest(false, { "--watchdog-timeout=10" });
992}
993
Andreas Gampef7882972017-03-20 16:35:24 -0700994class Dex2oatReturnCodeTest : public Dex2oatTest {
995 protected:
996 int RunTest(const std::vector<std::string>& extra_args = {}) {
997 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
998 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
999
1000 Copy(GetTestDexFileName(), dex_location);
1001
1002 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001003 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -07001004 odex_location,
1005 CompilerFilter::kSpeed,
1006 &error_msg,
1007 extra_args);
1008 }
1009
1010 std::string GetTestDexFileName() {
1011 return GetDexSrc1();
1012 }
1013};
1014
1015TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -07001016 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -07001017 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1018 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1019}
1020
Calin Juravle1ce70852017-06-28 10:59:03 -07001021class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1022 protected:
1023 void RunTest(const char* class_loader_context,
1024 const char* expected_classpath_key,
1025 bool expected_success,
1026 bool use_second_source = false) {
1027 std::string dex_location = GetUsedDexLocation();
1028 std::string odex_location = GetUsedOatLocation();
1029
1030 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1031
1032 std::string error_msg;
1033 std::vector<std::string> extra_args;
1034 if (class_loader_context != nullptr) {
1035 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1036 }
1037 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1038 ASSERT_TRUE(expected_classpath_key != nullptr);
1039 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1040 ASSERT_TRUE(classpath != nullptr);
1041 ASSERT_STREQ(expected_classpath_key, classpath);
1042 };
1043
1044 GenerateOdexForTest(dex_location,
1045 odex_location,
1046 CompilerFilter::kQuicken,
1047 extra_args,
1048 expected_success,
1049 /*use_fd*/ false,
1050 check_oat);
1051 }
1052
1053 std::string GetUsedDexLocation() {
1054 return GetScratchDir() + "/Context.jar";
1055 }
1056
1057 std::string GetUsedOatLocation() {
1058 return GetOdexDir() + "/Context.odex";
1059 }
1060
Calin Juravle7b0648a2017-07-07 18:40:50 -07001061 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001062};
1063
1064TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1065 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1066}
1067
1068TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1069 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1070}
1071
1072TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1073 RunTest(OatFile::kSpecialSharedLibrary,
1074 OatFile::kSpecialSharedLibrary,
1075 /*expected_success*/ true);
1076}
1077
1078TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1079 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1080 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1081}
1082
1083TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1084 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001085
1086 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001087 std::string expected_classpath_key = "PCL[" +
1088 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001089 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1090}
1091
1092TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1093 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1094 Copy(GetStrippedDexSrc1(), stripped_classpath);
1095
1096 std::string context = "PCL[" + stripped_classpath + "]";
1097 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001098 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001099}
1100
1101TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1102 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1103 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1104
1105 Copy(GetDexSrc1(), stripped_classpath);
1106
1107 GenerateOdexForTest(stripped_classpath,
1108 odex_for_classpath,
1109 CompilerFilter::kQuicken,
1110 {},
1111 true);
1112
1113 // Strip the dex file
1114 Copy(GetStrippedDexSrc1(), stripped_classpath);
1115
1116 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001117 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001118 {
1119 // Open the oat file to get the expected classpath.
1120 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false);
1121 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1122 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1123 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001124 expected_classpath_key = "PCL[";
1125 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1126 if (i > 0) {
1127 expected_classpath_key + ":";
1128 }
1129 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1130 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1131 }
1132 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001133 }
1134
1135 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001136 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001137 /*expected_success*/ true,
1138 /*use_second_source*/ true);
1139}
1140
1141TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1142 std::string context = "PCL[does_not_exists.dex]";
1143 // Expect an empty context because stripped dex files cannot be open.
1144 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1145}
1146
Calin Juravlec79470d2017-07-12 17:37:42 -07001147TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1148 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1149 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1150
1151 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1152 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1153 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1154 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1155
1156 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1157}
1158
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001159class Dex2oatDeterminism : public Dex2oatTest {};
1160
1161TEST_F(Dex2oatDeterminism, UnloadCompile) {
1162 if (!kUseReadBarrier &&
1163 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1164 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1165 LOG(INFO) << "Test requires determinism support.";
1166 return;
1167 }
1168 Runtime* const runtime = Runtime::Current();
1169 std::string out_dir = GetScratchDir();
1170 const std::string base_oat_name = out_dir + "/base.oat";
1171 const std::string base_vdex_name = out_dir + "/base.vdex";
1172 const std::string unload_oat_name = out_dir + "/unload.oat";
1173 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1174 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1175 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1176 const std::string app_image_name = out_dir + "/unload.art";
1177 std::string error_msg;
1178 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1179 ASSERT_GT(spaces.size(), 0u);
1180 const std::string image_location = spaces[0]->GetImageLocation();
1181 // Without passing in an app image, it will unload in between compilations.
1182 const int res = GenerateOdexForTestWithStatus(
1183 GetLibCoreDexFileNames(),
1184 base_oat_name,
1185 CompilerFilter::Filter::kQuicken,
1186 &error_msg,
1187 {"--force-determinism", "--avoid-storing-invocation"});
1188 EXPECT_EQ(res, 0);
1189 Copy(base_oat_name, unload_oat_name);
1190 Copy(base_vdex_name, unload_vdex_name);
1191 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1192 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1193 ASSERT_TRUE(unload_oat != nullptr);
1194 ASSERT_TRUE(unload_vdex != nullptr);
1195 EXPECT_GT(unload_oat->GetLength(), 0u);
1196 EXPECT_GT(unload_vdex->GetLength(), 0u);
1197 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1198 // the same.
1199 const int res2 = GenerateOdexForTestWithStatus(
1200 GetLibCoreDexFileNames(),
1201 base_oat_name,
1202 CompilerFilter::Filter::kQuicken,
1203 &error_msg,
1204 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1205 EXPECT_EQ(res2, 0);
1206 Copy(base_oat_name, no_unload_oat_name);
1207 Copy(base_vdex_name, no_unload_vdex_name);
1208 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1209 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1210 ASSERT_TRUE(no_unload_oat != nullptr);
1211 ASSERT_TRUE(no_unload_vdex != nullptr);
1212 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1213 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1214 // Verify that both of the files are the same (odex and vdex).
1215 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1216 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1217 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1218 << unload_oat_name << " " << no_unload_oat_name;
1219 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1220 << unload_vdex_name << " " << no_unload_vdex_name;
1221 // App image file.
1222 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1223 ASSERT_TRUE(app_image_file != nullptr);
1224 EXPECT_GT(app_image_file->GetLength(), 0u);
1225}
1226
Mathieu Chartier120aa282017-08-05 16:03:03 -07001227// Test that dexlayout section info is correctly written to the oat file for profile based
1228// compilation.
1229TEST_F(Dex2oatTest, LayoutSections) {
1230 using Hotness = ProfileCompilationInfo::MethodHotness;
1231 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1232 ScratchFile profile_file;
1233 // We can only layout method indices with code items, figure out which ones have this property
1234 // first.
1235 std::vector<uint16_t> methods;
1236 {
1237 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1238 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
1239 const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx);
1240 ClassDataItemIterator it(*dex, dex->GetClassData(*class_def));
1241 it.SkipAllFields();
1242 std::set<size_t> code_item_offsets;
1243 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1244 const uint16_t method_idx = it.GetMemberIndex();
1245 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1246 if (code_item_offsets.insert(code_item_offset).second) {
1247 // Unique code item, add the method index.
1248 methods.push_back(method_idx);
1249 }
1250 }
1251 DCHECK(!it.HasNext());
1252 }
1253 ASSERT_GE(methods.size(), 8u);
1254 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1255 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1256 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1257 // Here, we build the profile from the method lists.
1258 ProfileCompilationInfo info;
1259 info.AddMethodsForDex(
1260 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1261 dex.get(),
1262 hot_methods.begin(),
1263 hot_methods.end());
1264 info.AddMethodsForDex(
1265 Hotness::kFlagStartup,
1266 dex.get(),
1267 startup_methods.begin(),
1268 startup_methods.end());
1269 info.AddMethodsForDex(
1270 Hotness::kFlagPostStartup,
1271 dex.get(),
1272 post_methods.begin(),
1273 post_methods.end());
1274 for (uint16_t id : hot_methods) {
1275 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1276 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1277 }
1278 for (uint16_t id : startup_methods) {
1279 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1280 }
1281 for (uint16_t id : post_methods) {
1282 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1283 }
1284 // Save the profile since we want to use it with dex2oat to produce an oat file.
1285 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1286 // Generate a profile based odex.
1287 const std::string dir = GetScratchDir();
1288 const std::string oat_filename = dir + "/base.oat";
1289 const std::string vdex_filename = dir + "/base.vdex";
1290 std::string error_msg;
1291 const int res = GenerateOdexForTestWithStatus(
1292 {dex->GetLocation()},
1293 oat_filename,
1294 CompilerFilter::Filter::kQuicken,
1295 &error_msg,
1296 {"--profile-file=" + profile_file.GetFilename()});
1297 EXPECT_EQ(res, 0);
1298
1299 // Open our generated oat file.
1300 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1301 oat_filename.c_str(),
1302 nullptr,
1303 nullptr,
1304 false,
1305 /*low_4gb*/false,
1306 dex->GetLocation().c_str(),
1307 &error_msg));
1308 ASSERT_TRUE(odex_file != nullptr);
1309 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1310 ASSERT_EQ(oat_dex_files.size(), 1u);
1311 // Check that the code sections match what we expect.
1312 for (const OatDexFile* oat_dex : oat_dex_files) {
1313 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1314 // Testing of logging the sections.
1315 ASSERT_TRUE(sections != nullptr);
1316 LOG(INFO) << *sections;
1317
1318 // Load the sections into temporary variables for convenience.
1319 const DexLayoutSection& code_section =
1320 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1321 const DexLayoutSection::Subsection& section_hot_code =
1322 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1323 const DexLayoutSection::Subsection& section_sometimes_used =
1324 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1325 const DexLayoutSection::Subsection& section_startup_only =
1326 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1327 const DexLayoutSection::Subsection& section_unused =
1328 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1329
1330 // All the sections should be non-empty.
1331 EXPECT_GT(section_hot_code.size_, 0u);
1332 EXPECT_GT(section_sometimes_used.size_, 0u);
1333 EXPECT_GT(section_startup_only.size_, 0u);
1334 EXPECT_GT(section_unused.size_, 0u);
1335
1336 // Open the dex file since we need to peek at the code items to verify the layout matches what
1337 // we expect.
1338 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1339 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1340 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1341 ASSERT_TRUE(type_id != nullptr);
1342 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1343 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1344 ASSERT_TRUE(class_def != nullptr);
1345
1346 // Count how many code items are for each category, there should be at least one per category.
1347 size_t hot_count = 0;
1348 size_t post_startup_count = 0;
1349 size_t startup_count = 0;
1350 size_t unused_count = 0;
1351 // Visit all of the methdos of the main class and cross reference the method indices to their
1352 // corresponding code item offsets to verify the layout.
1353 ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def));
1354 it.SkipAllFields();
1355 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1356 const size_t method_idx = it.GetMemberIndex();
1357 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1358 const bool is_hot = ContainsElement(hot_methods, method_idx);
1359 const bool is_startup = ContainsElement(startup_methods, method_idx);
1360 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1361 if (is_hot) {
1362 // Hot is highest precedence, check that the hot methods are in the hot section.
1363 EXPECT_LT(code_item_offset - section_hot_code.offset_, section_hot_code.size_);
1364 ++hot_count;
1365 } else if (is_post_startup) {
1366 // Post startup is sometimes used section.
1367 EXPECT_LT(code_item_offset - section_sometimes_used.offset_, section_sometimes_used.size_);
1368 ++post_startup_count;
1369 } else if (is_startup) {
1370 // Startup at this point means not hot or post startup, these must be startup only then.
1371 EXPECT_LT(code_item_offset - section_startup_only.offset_, section_startup_only.size_);
1372 ++startup_count;
1373 } else {
1374 // If no flags are set, the method should be unused.
1375 EXPECT_LT(code_item_offset - section_unused.offset_, section_unused.size_);
1376 ++unused_count;
1377 }
1378 }
1379 DCHECK(!it.HasNext());
1380 EXPECT_GT(hot_count, 0u);
1381 EXPECT_GT(post_startup_count, 0u);
1382 EXPECT_GT(startup_count, 0u);
1383 EXPECT_GT(unused_count, 0u);
1384 }
1385}
1386
Andreas Gampee1459ae2016-06-29 09:36:30 -07001387} // namespace art