blob: dbb9dfc86084896d9c578cdd0b38ad045d33bc1e [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 Gampef7882972017-03-20 16:35:24 -070092 void GenerateOdexForTest(const std::string& dex_location,
93 const std::string& odex_location,
94 CompilerFilter::Filter filter,
95 const std::vector<std::string>& extra_args = {},
96 bool expect_success = true,
Calin Juravle1ce70852017-06-28 10:59:03 -070097 bool use_fd = false,
98 std::function<void(const OatFile&)> check_oat = [](const OatFile&) {}) {
Andreas Gampef7882972017-03-20 16:35:24 -070099 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700100 int status = GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700101 odex_location,
102 filter,
103 &error_msg,
104 extra_args,
105 use_fd);
106 bool success = (status == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700107 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800108 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700109
110 // Verify the odex file was generated as expected.
111 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
112 odex_location.c_str(),
113 nullptr,
114 nullptr,
115 false,
116 /*low_4gb*/false,
117 dex_location.c_str(),
118 &error_msg));
119 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
120
121 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700122 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700123 } else {
124 ASSERT_FALSE(success) << output_;
125
126 error_msg_ = error_msg;
127
128 // Verify there's no loadable odex file.
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);
138 }
139 }
140
Calin Juravle1ccf6132017-08-02 17:46:53 -0700141 // Check the input compiler filter against the generated oat file's filter. May be overridden
Andreas Gampee1459ae2016-06-29 09:36:30 -0700142 // in subclasses when equality is not expected.
143 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
144 EXPECT_EQ(expected, actual);
145 }
146
Andreas Gampef7882972017-03-20 16:35:24 -0700147 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700148 Runtime* runtime = Runtime::Current();
149
150 const std::vector<gc::space::ImageSpace*>& image_spaces =
151 runtime->GetHeap()->GetBootImageSpaces();
152 if (image_spaces.empty()) {
153 *error_msg = "No image location found for Dex2Oat.";
154 return false;
155 }
156 std::string image_location = image_spaces[0]->GetImageLocation();
157
158 std::vector<std::string> argv;
159 argv.push_back(runtime->GetCompilerExecutable());
Calin Juravle1ccf6132017-08-02 17:46:53 -0700160
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000161 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700162 argv.push_back("--debuggable");
163 }
164 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
165
166 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100167 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700168 }
169
170 if (runtime->MustRelocateIfPossible()) {
171 argv.push_back("--runtime-arg");
172 argv.push_back("-Xrelocate");
173 } else {
174 argv.push_back("--runtime-arg");
175 argv.push_back("-Xnorelocate");
176 }
177
178 if (!kIsTargetBuild) {
179 argv.push_back("--host");
180 }
181
182 argv.push_back("--boot-image=" + image_location);
183
184 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
185 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
186
187 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
188
189 // We must set --android-root.
190 const char* android_root = getenv("ANDROID_ROOT");
191 CHECK(android_root != nullptr);
192 argv.push_back("--android-root=" + std::string(android_root));
193
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700194 if (kDebugArgs) {
195 std::string all_args;
196 for (const std::string& arg : argv) {
197 all_args += arg + " ";
198 }
199 LOG(ERROR) << all_args;
200 }
201
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100202 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700203
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100204 if (pipe(link) == -1) {
205 return false;
206 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700207
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100208 pid_t pid = fork();
209 if (pid == -1) {
210 return false;
211 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700212
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100213 if (pid == 0) {
214 // We need dex2oat to actually log things.
215 setenv("ANDROID_LOG_TAGS", "*:d", 1);
216 dup2(link[1], STDERR_FILENO);
217 close(link[0]);
218 close(link[1]);
219 std::vector<const char*> c_args;
220 for (const std::string& str : argv) {
221 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700222 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100223 c_args.push_back(nullptr);
224 execv(c_args[0], const_cast<char* const*>(c_args.data()));
225 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700226 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100227 } else {
228 close(link[1]);
229 char buffer[128];
230 memset(buffer, 0, 128);
231 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700232
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100233 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
234 output_ += std::string(buffer, bytes_read);
235 }
236 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700237 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100238 if (waitpid(pid, &status, 0) != -1) {
239 success_ = (status == 0);
240 }
Andreas Gampef7882972017-03-20 16:35:24 -0700241 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700242 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700243 }
244
245 std::string output_ = "";
246 std::string error_msg_ = "";
247 bool success_ = false;
248};
249
250class Dex2oatSwapTest : public Dex2oatTest {
251 protected:
252 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
253 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
254 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
255
Andreas Gampe7adeda82016-07-25 08:27:35 -0700256 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700257
258 std::vector<std::string> copy(extra_args);
259
260 std::unique_ptr<ScratchFile> sf;
261 if (use_fd) {
262 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800263 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700264 } else {
265 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
266 copy.push_back("--swap-file=" + swap_location);
267 }
268 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
269
270 CheckValidity();
271 ASSERT_TRUE(success_);
272 CheckResult(expect_use);
273 }
274
Andreas Gampe7adeda82016-07-25 08:27:35 -0700275 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000276 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700277 }
278
279 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700280 if (kIsTargetBuild) {
281 CheckTargetResult(expect_use);
282 } else {
283 CheckHostResult(expect_use);
284 }
285 }
286
Andreas Gampe7adeda82016-07-25 08:27:35 -0700287 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700288 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
289 // something for variants with file descriptor where we can control the lifetime of
290 // the swap file and thus take a look at it.
291 }
292
Andreas Gampe7adeda82016-07-25 08:27:35 -0700293 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700294 if (!kIsTargetBuild) {
295 if (expect_use) {
296 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
297 << output_;
298 } else {
299 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
300 << output_;
301 }
302 }
303 }
304
305 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700306 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700307 if (kIsTargetBuild) {
308 CheckTargetValidity();
309 } else {
310 CheckHostValidity();
311 }
312 }
313
Andreas Gampe7adeda82016-07-25 08:27:35 -0700314 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700315 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
316 // something for variants with file descriptor where we can control the lifetime of
317 // the swap file and thus take a look at it.
318 }
319
320 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700321 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700322 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
323 }
324};
325
326TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
327 RunTest(false /* use_fd */, false /* expect_use */);
328 RunTest(true /* use_fd */, false /* expect_use */);
329}
330
331TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
332 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
333 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
334}
335
336TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
337 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
338 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
339}
340
341TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
342 RunTest(false /* use_fd */,
343 true /* expect_use */,
344 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
345 RunTest(true /* use_fd */,
346 true /* expect_use */,
347 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
348}
349
Andreas Gampe7adeda82016-07-25 08:27:35 -0700350class Dex2oatSwapUseTest : public Dex2oatSwapTest {
351 protected:
352 void CheckHostResult(bool expect_use) OVERRIDE {
353 if (!kIsTargetBuild) {
354 if (expect_use) {
355 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
356 << output_;
357 } else {
358 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
359 << output_;
360 }
361 }
362 }
363
364 std::string GetTestDexFileName() OVERRIDE {
365 // Use Statics as it has a handful of functions.
366 return CommonRuntimeTest::GetTestDexFileName("Statics");
367 }
368
369 void GrabResult1() {
370 if (!kIsTargetBuild) {
371 native_alloc_1_ = ParseNativeAlloc();
372 swap_1_ = ParseSwap(false /* expected */);
373 } else {
374 native_alloc_1_ = std::numeric_limits<size_t>::max();
375 swap_1_ = 0;
376 }
377 }
378
379 void GrabResult2() {
380 if (!kIsTargetBuild) {
381 native_alloc_2_ = ParseNativeAlloc();
382 swap_2_ = ParseSwap(true /* expected */);
383 } else {
384 native_alloc_2_ = 0;
385 swap_2_ = std::numeric_limits<size_t>::max();
386 }
387 }
388
389 private:
390 size_t ParseNativeAlloc() {
391 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
392 std::smatch native_alloc_match;
393 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
394 if (!found) {
395 EXPECT_TRUE(found);
396 return 0;
397 }
398 if (native_alloc_match.size() != 2U) {
399 EXPECT_EQ(native_alloc_match.size(), 2U);
400 return 0;
401 }
402
403 std::istringstream stream(native_alloc_match[1].str());
404 size_t value;
405 stream >> value;
406
407 return value;
408 }
409
410 size_t ParseSwap(bool expected) {
411 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
412 std::smatch swap_match;
413 bool found = std::regex_search(output_, swap_match, swap_regex);
414 if (found != expected) {
415 EXPECT_EQ(expected, found);
416 return 0;
417 }
418
419 if (!found) {
420 return 0;
421 }
422
423 if (swap_match.size() != 2U) {
424 EXPECT_EQ(swap_match.size(), 2U);
425 return 0;
426 }
427
428 std::istringstream stream(swap_match[1].str());
429 size_t value;
430 stream >> value;
431
432 return value;
433 }
434
435 protected:
436 size_t native_alloc_1_;
437 size_t native_alloc_2_;
438
439 size_t swap_1_;
440 size_t swap_2_;
441};
442
443TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700444 // Native memory usage isn't correctly tracked under sanitization.
445 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
446
Vladimir Marko57070da2017-02-14 16:16:30 +0000447 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000448 // hold true on some x86 systems; disable this test while we
449 // investigate (b/29259363).
450 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000451
Andreas Gampe7adeda82016-07-25 08:27:35 -0700452 RunTest(false /* use_fd */,
453 false /* expect_use */);
454 GrabResult1();
455 std::string output_1 = output_;
456
457 output_ = "";
458
459 RunTest(false /* use_fd */,
460 true /* expect_use */,
461 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
462 GrabResult2();
463 std::string output_2 = output_;
464
465 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
466 EXPECT_LT(native_alloc_2_, native_alloc_1_);
467 EXPECT_LT(swap_1_, swap_2_);
468
469 LOG(ERROR) << output_1;
470 LOG(ERROR) << output_2;
471 }
472}
473
Andreas Gampe67f02822016-06-24 21:05:23 -0700474class Dex2oatVeryLargeTest : public Dex2oatTest {
475 protected:
476 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
477 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
478 // Ignore, we'll do our own checks.
479 }
480
481 void RunTest(CompilerFilter::Filter filter,
482 bool expect_large,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700483 bool expect_downgrade,
Andreas Gampe67f02822016-06-24 21:05:23 -0700484 const std::vector<std::string>& extra_args = {}) {
485 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
486 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700487 std::string app_image_file = GetScratchDir() + "/Test.art";
Andreas Gampe67f02822016-06-24 21:05:23 -0700488
489 Copy(GetDexSrc1(), dex_location);
490
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700491 std::vector<std::string> new_args(extra_args);
492 new_args.push_back("--app-image-file=" + app_image_file);
493 GenerateOdexForTest(dex_location, odex_location, filter, new_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700494
495 CheckValidity();
496 ASSERT_TRUE(success_);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700497 CheckResult(dex_location,
498 odex_location,
499 app_image_file,
500 filter,
501 expect_large,
502 expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700503 }
504
505 void CheckResult(const std::string& dex_location,
506 const std::string& odex_location,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700507 const std::string& app_image_file,
Andreas Gampe67f02822016-06-24 21:05:23 -0700508 CompilerFilter::Filter filter,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700509 bool expect_large,
510 bool expect_downgrade) {
511 if (expect_downgrade) {
512 EXPECT_TRUE(expect_large);
513 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700514 // Host/target independent checks.
515 std::string error_msg;
516 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
517 odex_location.c_str(),
518 nullptr,
519 nullptr,
520 false,
521 /*low_4gb*/false,
522 dex_location.c_str(),
523 &error_msg));
524 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700525 EXPECT_GT(app_image_file.length(), 0u);
526 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700527 if (expect_large) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700528 // Note: we cannot check the following
529 // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700530 // The reason is that the filter override currently happens when the dex files are
531 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
532 // store cannot be changed, and the original filter is set in stone.
533
534 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
535 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
536 ASSERT_TRUE(dex_file != nullptr);
537 uint32_t class_def_count = dex_file->NumClassDefs();
538 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
539 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
540 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
541 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
542 }
543 }
544
545 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100546 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700547 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
548 }
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700549
550 // If expect large, make sure the app image isn't generated or is empty.
551 if (file != nullptr) {
552 EXPECT_EQ(file->GetLength(), 0u);
553 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700554 } else {
555 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700556 ASSERT_TRUE(file != nullptr) << app_image_file;
557 EXPECT_GT(file->GetLength(), 0u);
Andreas Gampe67f02822016-06-24 21:05:23 -0700558 }
559
560 // Host/target dependent checks.
561 if (kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700562 CheckTargetResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700563 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700564 CheckHostResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700565 }
566 }
567
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700568 void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700569 // TODO: Ignore for now. May do something for fd things.
570 }
571
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700572 void CheckHostResult(bool expect_downgrade) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700573 if (!kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700574 if (expect_downgrade) {
575 EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700576 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700577 EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700578 }
579 }
580 }
581
582 // Check whether the dex2oat run was really successful.
583 void CheckValidity() {
584 if (kIsTargetBuild) {
585 CheckTargetValidity();
586 } else {
587 CheckHostValidity();
588 }
589 }
590
591 void CheckTargetValidity() {
592 // TODO: Ignore for now.
593 }
594
595 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
596 void CheckHostValidity() {
597 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
598 }
599};
600
601TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700602 RunTest(CompilerFilter::kAssumeVerified, false, false);
603 RunTest(CompilerFilter::kExtract, false, false);
604 RunTest(CompilerFilter::kQuicken, false, false);
605 RunTest(CompilerFilter::kSpeed, false, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700606
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700607 RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
608 RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
609 RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" });
610 RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700611}
612
613TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700614 RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
615 RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
616 RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" });
617 RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700618}
619
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800620// Regressin test for b/35665292.
621TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
622 // Test that dex2oat doesn't crash with speed-profile but no input profile.
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700623 RunTest(CompilerFilter::kSpeedProfile, false, false);
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800624}
625
Jeff Hao608f2ce2016-10-19 11:17:11 -0700626class Dex2oatLayoutTest : public Dex2oatTest {
627 protected:
628 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
629 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
630 // Ignore, we'll do our own checks.
631 }
632
Jeff Hao41fba6a2016-11-28 11:53:33 -0800633 // Emits a profile with a single dex file with the given location and a single class index of 1.
634 void GenerateProfile(const std::string& test_profile,
635 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800636 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800637 uint32_t checksum) {
638 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
639 CHECK_GE(profile_test_fd, 0);
640
641 ProfileCompilationInfo info;
642 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800643 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700644 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800645 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800646 bool result = info.Save(profile_test_fd);
647 close(profile_test_fd);
648 ASSERT_TRUE(result);
649 }
650
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800651 void CompileProfileOdex(const std::string& dex_location,
652 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800653 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800654 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800655 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000656 const std::vector<std::string>& extra_args = {},
657 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800658 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800659 const char* location = dex_location.c_str();
660 std::string error_msg;
661 std::vector<std::unique_ptr<const DexFile>> dex_files;
662 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
663 EXPECT_EQ(dex_files.size(), 1U);
664 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800665 GenerateProfile(profile_location,
666 dex_location,
667 num_profile_classes,
668 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800669 std::vector<std::string> copy(extra_args);
670 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800671 std::unique_ptr<File> app_image_file;
672 if (!app_image_file_name.empty()) {
673 if (use_fd) {
674 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
675 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
676 } else {
677 copy.push_back("--app-image-file=" + app_image_file_name);
678 }
679 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800680 GenerateOdexForTest(dex_location,
681 odex_location,
682 CompilerFilter::kSpeedProfile,
683 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000684 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800685 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800686 if (app_image_file != nullptr) {
687 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
688 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800689 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700690
Mathieu Chartier046854b2017-03-01 17:16:22 -0800691 uint64_t GetImageSize(const std::string& image_file_name) {
692 EXPECT_FALSE(image_file_name.empty());
693 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
694 CHECK(file != nullptr);
695 ImageHeader image_header;
696 const bool success = file->ReadFully(&image_header, sizeof(image_header));
697 CHECK(success);
698 CHECK(image_header.IsValid());
699 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
700 return image_header.GetImageSize();
701 }
702
703 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800704 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
705 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800706 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800707 Copy(GetDexSrc2(), dex_location);
708
Mathieu Chartier046854b2017-03-01 17:16:22 -0800709 uint64_t image_file_empty_profile = 0;
710 if (app_image) {
711 CompileProfileOdex(dex_location,
712 odex_location,
713 app_image_file,
714 /* use_fd */ false,
715 /* num_profile_classes */ 0);
716 CheckValidity();
717 ASSERT_TRUE(success_);
718 // Don't check the result since CheckResult relies on the class being in the profile.
719 image_file_empty_profile = GetImageSize(app_image_file);
720 EXPECT_GT(image_file_empty_profile, 0u);
721 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700722
Mathieu Chartier046854b2017-03-01 17:16:22 -0800723 // Small profile.
724 CompileProfileOdex(dex_location,
725 odex_location,
726 app_image_file,
727 /* use_fd */ false,
728 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700729 CheckValidity();
730 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800731 CheckResult(dex_location, odex_location, app_image_file);
732
733 if (app_image) {
734 // Test that the profile made a difference by adding more classes.
735 const uint64_t image_file_small_profile = GetImageSize(app_image_file);
736 CHECK_LT(image_file_empty_profile, image_file_small_profile);
737 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700738 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800739
740 void RunTestVDex() {
741 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
742 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
743 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800744 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800745 Copy(GetDexSrc2(), dex_location);
746
747 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
748 CHECK(vdex_file1 != nullptr) << vdex_location;
749 ScratchFile vdex_file2;
750 {
751 std::string input_vdex = "--input-vdex-fd=-1";
752 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
753 CompileProfileOdex(dex_location,
754 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800755 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800756 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800757 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800758 { input_vdex, output_vdex });
759 EXPECT_GT(vdex_file1->GetLength(), 0u);
760 }
761 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000762 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800763 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
764 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
765 CompileProfileOdex(dex_location,
766 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800767 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800768 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800769 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000770 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100771 /* expect_success */ true);
772 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800773 }
774 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
775 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100776 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800777 }
778
Mathieu Chartier046854b2017-03-01 17:16:22 -0800779 void CheckResult(const std::string& dex_location,
780 const std::string& odex_location,
781 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700782 // Host/target independent checks.
783 std::string error_msg;
784 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
785 odex_location.c_str(),
786 nullptr,
787 nullptr,
788 false,
789 /*low_4gb*/false,
790 dex_location.c_str(),
791 &error_msg));
792 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
793
Jeff Hao042e8982016-10-19 11:17:11 -0700794 const char* location = dex_location.c_str();
795 std::vector<std::unique_ptr<const DexFile>> dex_files;
796 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
797 EXPECT_EQ(dex_files.size(), 1U);
798 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
799
Jeff Hao608f2ce2016-10-19 11:17:11 -0700800 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700801 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
802 ASSERT_TRUE(new_dex_file != nullptr);
803 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700804 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700805 ASSERT_GE(class_def_count, 2U);
806
807 // The new layout swaps the classes at indexes 0 and 1.
808 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
809 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
810 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
811 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
812 EXPECT_EQ(old_class0, new_class1);
813 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700814 }
815
Jeff Haoc155b052017-01-17 17:43:29 -0800816 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800817
818 if (!app_image_file_name.empty()) {
819 // Go peek at the image header to make sure it was large enough to contain the class.
820 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
821 ImageHeader image_header;
822 bool success = file->ReadFully(&image_header, sizeof(image_header));
823 ASSERT_TRUE(success);
824 ASSERT_TRUE(image_header.IsValid());
825 EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
826 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700827 }
828
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800829 // Check whether the dex2oat run was really successful.
830 void CheckValidity() {
831 if (kIsTargetBuild) {
832 CheckTargetValidity();
833 } else {
834 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700835 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800836 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700837
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800838 void CheckTargetValidity() {
839 // TODO: Ignore for now.
840 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700841
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800842 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
843 void CheckHostValidity() {
844 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
845 }
846};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700847
848TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800849 RunTest(/* app-image */ false);
850}
851
852TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
853 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700854}
855
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800856TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
857 RunTestVDex();
858}
859
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100860class Dex2oatUnquickenTest : public Dex2oatTest {
861 protected:
862 void RunUnquickenMultiDex() {
863 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
864 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
865 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
866 Copy(GetTestDexFileName("MultiDex"), dex_location);
867
868 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
869 CHECK(vdex_file1 != nullptr) << vdex_location;
870 // Quicken the dex file into a vdex file.
871 {
872 std::string input_vdex = "--input-vdex-fd=-1";
873 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
874 GenerateOdexForTest(dex_location,
875 odex_location,
876 CompilerFilter::kQuicken,
877 { input_vdex, output_vdex },
878 /* expect_success */ true,
879 /* use_fd */ true);
880 EXPECT_GT(vdex_file1->GetLength(), 0u);
881 }
882 // Unquicken by running the verify compiler filter on the vdex file.
883 {
884 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
885 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
886 GenerateOdexForTest(dex_location,
887 odex_location,
888 CompilerFilter::kVerify,
889 { input_vdex, output_vdex },
890 /* expect_success */ true,
891 /* use_fd */ true);
892 }
893 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
894 CheckResult(dex_location, odex_location);
895 ASSERT_TRUE(success_);
896 }
897
898 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
899 std::string error_msg;
900 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
901 odex_location.c_str(),
902 nullptr,
903 nullptr,
904 false,
905 /*low_4gb*/false,
906 dex_location.c_str(),
907 &error_msg));
908 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
909 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
910
911 // Iterate over the dex files and ensure there is no quickened instruction.
912 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
913 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
914 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
915 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
916 const uint8_t* class_data = dex_file->GetClassData(class_def);
917 if (class_data != nullptr) {
918 for (ClassDataItemIterator class_it(*dex_file, class_data);
919 class_it.HasNext();
920 class_it.Next()) {
921 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
922 for (CodeItemIterator it(*class_it.GetMethodCodeItem()); !it.Done(); it.Advance()) {
923 Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction());
924 ASSERT_FALSE(inst->IsQuickened());
925 }
926 }
927 }
928 }
929 }
930 }
931 }
932};
933
934TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
935 RunUnquickenMultiDex();
936}
937
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800938class Dex2oatWatchdogTest : public Dex2oatTest {
939 protected:
940 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
941 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
942 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
943
944 Copy(GetTestDexFileName(), dex_location);
945
946 std::vector<std::string> copy(extra_args);
947
948 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
949 copy.push_back("--swap-file=" + swap_location);
950 GenerateOdexForTest(dex_location,
951 odex_location,
952 CompilerFilter::kSpeed,
953 copy,
954 expect_success);
955 }
956
957 std::string GetTestDexFileName() {
958 return GetDexSrc1();
959 }
960};
961
962TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
963 // Check with default.
964 RunTest(true);
965
966 // Check with ten minutes.
967 RunTest(true, { "--watchdog-timeout=600000" });
968}
969
970TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Roland Levillain68db2252017-08-14 12:48:47 +0100971 TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800972 // Check with ten milliseconds.
973 RunTest(false, { "--watchdog-timeout=10" });
974}
975
Andreas Gampef7882972017-03-20 16:35:24 -0700976class Dex2oatReturnCodeTest : public Dex2oatTest {
977 protected:
978 int RunTest(const std::vector<std::string>& extra_args = {}) {
979 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
980 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
981
982 Copy(GetTestDexFileName(), dex_location);
983
984 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700985 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700986 odex_location,
987 CompilerFilter::kSpeed,
988 &error_msg,
989 extra_args);
990 }
991
992 std::string GetTestDexFileName() {
993 return GetDexSrc1();
994 }
995};
996
997TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -0700998 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -0700999 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1000 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1001}
1002
Calin Juravle1ce70852017-06-28 10:59:03 -07001003class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1004 protected:
1005 void RunTest(const char* class_loader_context,
1006 const char* expected_classpath_key,
1007 bool expected_success,
1008 bool use_second_source = false) {
1009 std::string dex_location = GetUsedDexLocation();
1010 std::string odex_location = GetUsedOatLocation();
1011
1012 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1013
1014 std::string error_msg;
1015 std::vector<std::string> extra_args;
1016 if (class_loader_context != nullptr) {
1017 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1018 }
1019 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1020 ASSERT_TRUE(expected_classpath_key != nullptr);
1021 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1022 ASSERT_TRUE(classpath != nullptr);
1023 ASSERT_STREQ(expected_classpath_key, classpath);
1024 };
1025
1026 GenerateOdexForTest(dex_location,
1027 odex_location,
1028 CompilerFilter::kQuicken,
1029 extra_args,
1030 expected_success,
1031 /*use_fd*/ false,
1032 check_oat);
1033 }
1034
1035 std::string GetUsedDexLocation() {
1036 return GetScratchDir() + "/Context.jar";
1037 }
1038
1039 std::string GetUsedOatLocation() {
1040 return GetOdexDir() + "/Context.odex";
1041 }
1042
Calin Juravle7b0648a2017-07-07 18:40:50 -07001043 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001044};
1045
1046TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1047 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1048}
1049
1050TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1051 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1052}
1053
1054TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1055 RunTest(OatFile::kSpecialSharedLibrary,
1056 OatFile::kSpecialSharedLibrary,
1057 /*expected_success*/ true);
1058}
1059
1060TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1061 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1062 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1063}
1064
1065TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1066 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001067
1068 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001069 std::string expected_classpath_key = "PCL[" +
1070 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001071 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1072}
1073
1074TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1075 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1076 Copy(GetStrippedDexSrc1(), stripped_classpath);
1077
1078 std::string context = "PCL[" + stripped_classpath + "]";
1079 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001080 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001081}
1082
1083TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1084 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1085 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1086
1087 Copy(GetDexSrc1(), stripped_classpath);
1088
1089 GenerateOdexForTest(stripped_classpath,
1090 odex_for_classpath,
1091 CompilerFilter::kQuicken,
1092 {},
1093 true);
1094
1095 // Strip the dex file
1096 Copy(GetStrippedDexSrc1(), stripped_classpath);
1097
1098 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001099 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001100 {
1101 // Open the oat file to get the expected classpath.
1102 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false);
1103 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1104 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1105 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001106 expected_classpath_key = "PCL[";
1107 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1108 if (i > 0) {
1109 expected_classpath_key + ":";
1110 }
1111 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1112 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1113 }
1114 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001115 }
1116
1117 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001118 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001119 /*expected_success*/ true,
1120 /*use_second_source*/ true);
1121}
1122
1123TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1124 std::string context = "PCL[does_not_exists.dex]";
1125 // Expect an empty context because stripped dex files cannot be open.
1126 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1127}
1128
Calin Juravlec79470d2017-07-12 17:37:42 -07001129TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1130 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1131 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1132
1133 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1134 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1135 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1136 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1137
1138 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1139}
1140
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001141class Dex2oatDeterminism : public Dex2oatTest {};
1142
1143TEST_F(Dex2oatDeterminism, UnloadCompile) {
1144 if (!kUseReadBarrier &&
1145 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1146 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1147 LOG(INFO) << "Test requires determinism support.";
1148 return;
1149 }
1150 Runtime* const runtime = Runtime::Current();
1151 std::string out_dir = GetScratchDir();
1152 const std::string base_oat_name = out_dir + "/base.oat";
1153 const std::string base_vdex_name = out_dir + "/base.vdex";
1154 const std::string unload_oat_name = out_dir + "/unload.oat";
1155 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1156 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1157 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1158 const std::string app_image_name = out_dir + "/unload.art";
1159 std::string error_msg;
1160 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1161 ASSERT_GT(spaces.size(), 0u);
1162 const std::string image_location = spaces[0]->GetImageLocation();
1163 // Without passing in an app image, it will unload in between compilations.
1164 const int res = GenerateOdexForTestWithStatus(
1165 GetLibCoreDexFileNames(),
1166 base_oat_name,
1167 CompilerFilter::Filter::kQuicken,
1168 &error_msg,
1169 {"--force-determinism", "--avoid-storing-invocation"});
1170 EXPECT_EQ(res, 0);
1171 Copy(base_oat_name, unload_oat_name);
1172 Copy(base_vdex_name, unload_vdex_name);
1173 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1174 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1175 ASSERT_TRUE(unload_oat != nullptr);
1176 ASSERT_TRUE(unload_vdex != nullptr);
1177 EXPECT_GT(unload_oat->GetLength(), 0u);
1178 EXPECT_GT(unload_vdex->GetLength(), 0u);
1179 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1180 // the same.
1181 const int res2 = GenerateOdexForTestWithStatus(
1182 GetLibCoreDexFileNames(),
1183 base_oat_name,
1184 CompilerFilter::Filter::kQuicken,
1185 &error_msg,
1186 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1187 EXPECT_EQ(res2, 0);
1188 Copy(base_oat_name, no_unload_oat_name);
1189 Copy(base_vdex_name, no_unload_vdex_name);
1190 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1191 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1192 ASSERT_TRUE(no_unload_oat != nullptr);
1193 ASSERT_TRUE(no_unload_vdex != nullptr);
1194 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1195 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1196 // Verify that both of the files are the same (odex and vdex).
1197 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1198 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1199 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1200 << unload_oat_name << " " << no_unload_oat_name;
1201 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1202 << unload_vdex_name << " " << no_unload_vdex_name;
1203 // App image file.
1204 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1205 ASSERT_TRUE(app_image_file != nullptr);
1206 EXPECT_GT(app_image_file->GetLength(), 0u);
1207}
1208
Mathieu Chartier120aa282017-08-05 16:03:03 -07001209// Test that dexlayout section info is correctly written to the oat file for profile based
1210// compilation.
1211TEST_F(Dex2oatTest, LayoutSections) {
1212 using Hotness = ProfileCompilationInfo::MethodHotness;
1213 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1214 ScratchFile profile_file;
1215 // We can only layout method indices with code items, figure out which ones have this property
1216 // first.
1217 std::vector<uint16_t> methods;
1218 {
1219 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1220 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
1221 const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx);
1222 ClassDataItemIterator it(*dex, dex->GetClassData(*class_def));
1223 it.SkipAllFields();
1224 std::set<size_t> code_item_offsets;
1225 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1226 const uint16_t method_idx = it.GetMemberIndex();
1227 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1228 if (code_item_offsets.insert(code_item_offset).second) {
1229 // Unique code item, add the method index.
1230 methods.push_back(method_idx);
1231 }
1232 }
1233 DCHECK(!it.HasNext());
1234 }
1235 ASSERT_GE(methods.size(), 8u);
1236 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1237 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1238 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1239 // Here, we build the profile from the method lists.
1240 ProfileCompilationInfo info;
1241 info.AddMethodsForDex(
1242 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1243 dex.get(),
1244 hot_methods.begin(),
1245 hot_methods.end());
1246 info.AddMethodsForDex(
1247 Hotness::kFlagStartup,
1248 dex.get(),
1249 startup_methods.begin(),
1250 startup_methods.end());
1251 info.AddMethodsForDex(
1252 Hotness::kFlagPostStartup,
1253 dex.get(),
1254 post_methods.begin(),
1255 post_methods.end());
1256 for (uint16_t id : hot_methods) {
1257 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1258 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1259 }
1260 for (uint16_t id : startup_methods) {
1261 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1262 }
1263 for (uint16_t id : post_methods) {
1264 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1265 }
1266 // Save the profile since we want to use it with dex2oat to produce an oat file.
1267 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1268 // Generate a profile based odex.
1269 const std::string dir = GetScratchDir();
1270 const std::string oat_filename = dir + "/base.oat";
1271 const std::string vdex_filename = dir + "/base.vdex";
1272 std::string error_msg;
1273 const int res = GenerateOdexForTestWithStatus(
1274 {dex->GetLocation()},
1275 oat_filename,
1276 CompilerFilter::Filter::kQuicken,
1277 &error_msg,
1278 {"--profile-file=" + profile_file.GetFilename()});
1279 EXPECT_EQ(res, 0);
1280
1281 // Open our generated oat file.
1282 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1283 oat_filename.c_str(),
1284 nullptr,
1285 nullptr,
1286 false,
1287 /*low_4gb*/false,
1288 dex->GetLocation().c_str(),
1289 &error_msg));
1290 ASSERT_TRUE(odex_file != nullptr);
1291 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1292 ASSERT_EQ(oat_dex_files.size(), 1u);
1293 // Check that the code sections match what we expect.
1294 for (const OatDexFile* oat_dex : oat_dex_files) {
1295 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1296 // Testing of logging the sections.
1297 ASSERT_TRUE(sections != nullptr);
1298 LOG(INFO) << *sections;
1299
1300 // Load the sections into temporary variables for convenience.
1301 const DexLayoutSection& code_section =
1302 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1303 const DexLayoutSection::Subsection& section_hot_code =
1304 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1305 const DexLayoutSection::Subsection& section_sometimes_used =
1306 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1307 const DexLayoutSection::Subsection& section_startup_only =
1308 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1309 const DexLayoutSection::Subsection& section_unused =
1310 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1311
1312 // All the sections should be non-empty.
1313 EXPECT_GT(section_hot_code.size_, 0u);
1314 EXPECT_GT(section_sometimes_used.size_, 0u);
1315 EXPECT_GT(section_startup_only.size_, 0u);
1316 EXPECT_GT(section_unused.size_, 0u);
1317
1318 // Open the dex file since we need to peek at the code items to verify the layout matches what
1319 // we expect.
1320 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1321 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1322 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1323 ASSERT_TRUE(type_id != nullptr);
1324 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1325 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1326 ASSERT_TRUE(class_def != nullptr);
1327
1328 // Count how many code items are for each category, there should be at least one per category.
1329 size_t hot_count = 0;
1330 size_t post_startup_count = 0;
1331 size_t startup_count = 0;
1332 size_t unused_count = 0;
1333 // Visit all of the methdos of the main class and cross reference the method indices to their
1334 // corresponding code item offsets to verify the layout.
1335 ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def));
1336 it.SkipAllFields();
1337 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
1338 const size_t method_idx = it.GetMemberIndex();
1339 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1340 const bool is_hot = ContainsElement(hot_methods, method_idx);
1341 const bool is_startup = ContainsElement(startup_methods, method_idx);
1342 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1343 if (is_hot) {
1344 // Hot is highest precedence, check that the hot methods are in the hot section.
1345 EXPECT_LT(code_item_offset - section_hot_code.offset_, section_hot_code.size_);
1346 ++hot_count;
1347 } else if (is_post_startup) {
1348 // Post startup is sometimes used section.
1349 EXPECT_LT(code_item_offset - section_sometimes_used.offset_, section_sometimes_used.size_);
1350 ++post_startup_count;
1351 } else if (is_startup) {
1352 // Startup at this point means not hot or post startup, these must be startup only then.
1353 EXPECT_LT(code_item_offset - section_startup_only.offset_, section_startup_only.size_);
1354 ++startup_count;
1355 } else {
1356 // If no flags are set, the method should be unused.
1357 EXPECT_LT(code_item_offset - section_unused.offset_, section_unused.size_);
1358 ++unused_count;
1359 }
1360 }
1361 DCHECK(!it.HasNext());
1362 EXPECT_GT(hot_count, 0u);
1363 EXPECT_GT(post_startup_count, 0u);
1364 EXPECT_GT(startup_count, 0u);
1365 EXPECT_GT(unused_count, 0u);
1366 }
1367}
1368
Andreas Gampee1459ae2016-06-29 09:36:30 -07001369} // namespace art