blob: 6a9d979d52a18820127de985bdfee9eb5d178656 [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,
483 const std::vector<std::string>& extra_args = {}) {
484 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
485 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
486
487 Copy(GetDexSrc1(), dex_location);
488
Andreas Gampeca620d72016-11-08 08:09:33 -0800489 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700490
491 CheckValidity();
492 ASSERT_TRUE(success_);
493 CheckResult(dex_location, odex_location, filter, expect_large);
494 }
495
496 void CheckResult(const std::string& dex_location,
497 const std::string& odex_location,
498 CompilerFilter::Filter filter,
499 bool expect_large) {
500 // Host/target independent checks.
501 std::string error_msg;
502 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
503 odex_location.c_str(),
504 nullptr,
505 nullptr,
506 false,
507 /*low_4gb*/false,
508 dex_location.c_str(),
509 &error_msg));
510 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
511 if (expect_large) {
512 // Note: we cannot check the following:
513 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
514 // odex_file->GetCompilerFilter()));
515 // The reason is that the filter override currently happens when the dex files are
516 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
517 // store cannot be changed, and the original filter is set in stone.
518
519 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
520 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
521 ASSERT_TRUE(dex_file != nullptr);
522 uint32_t class_def_count = dex_file->NumClassDefs();
523 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
524 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
525 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
526 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
527 }
528 }
529
530 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100531 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700532 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
533 }
534 } else {
535 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
536 }
537
538 // Host/target dependent checks.
539 if (kIsTargetBuild) {
540 CheckTargetResult(expect_large);
541 } else {
542 CheckHostResult(expect_large);
543 }
544 }
545
546 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
547 // TODO: Ignore for now. May do something for fd things.
548 }
549
550 void CheckHostResult(bool expect_large) {
551 if (!kIsTargetBuild) {
552 if (expect_large) {
Mathieu Chartier010126f2017-07-15 15:44:18 -0700553 EXPECT_NE(output_.find("Very large app, downgrading to"),
Andreas Gampe67f02822016-06-24 21:05:23 -0700554 std::string::npos)
555 << output_;
556 } else {
Mathieu Chartier010126f2017-07-15 15:44:18 -0700557 EXPECT_EQ(output_.find("Very large app, downgrading to"),
Andreas Gampe67f02822016-06-24 21:05:23 -0700558 std::string::npos)
559 << output_;
560 }
561 }
562 }
563
564 // Check whether the dex2oat run was really successful.
565 void CheckValidity() {
566 if (kIsTargetBuild) {
567 CheckTargetValidity();
568 } else {
569 CheckHostValidity();
570 }
571 }
572
573 void CheckTargetValidity() {
574 // TODO: Ignore for now.
575 }
576
577 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
578 void CheckHostValidity() {
579 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
580 }
581};
582
583TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100584 RunTest(CompilerFilter::kAssumeVerified, false);
585 RunTest(CompilerFilter::kExtract, false);
586 RunTest(CompilerFilter::kQuicken, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700587 RunTest(CompilerFilter::kSpeed, false);
588
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100589 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=1000000" });
590 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=1000000" });
591 RunTest(CompilerFilter::kQuicken, false, { "--very-large-app-threshold=1000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700592 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
593}
594
595TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100596 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=100" });
597 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=100" });
598 RunTest(CompilerFilter::kQuicken, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700599 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
600}
601
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800602// Regressin test for b/35665292.
603TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
604 // Test that dex2oat doesn't crash with speed-profile but no input profile.
605 RunTest(CompilerFilter::kSpeedProfile, false);
606}
607
Jeff Hao608f2ce2016-10-19 11:17:11 -0700608class Dex2oatLayoutTest : public Dex2oatTest {
609 protected:
610 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
611 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
612 // Ignore, we'll do our own checks.
613 }
614
Jeff Hao41fba6a2016-11-28 11:53:33 -0800615 // Emits a profile with a single dex file with the given location and a single class index of 1.
616 void GenerateProfile(const std::string& test_profile,
617 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800618 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800619 uint32_t checksum) {
620 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
621 CHECK_GE(profile_test_fd, 0);
622
623 ProfileCompilationInfo info;
624 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800625 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700626 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800627 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800628 bool result = info.Save(profile_test_fd);
629 close(profile_test_fd);
630 ASSERT_TRUE(result);
631 }
632
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800633 void CompileProfileOdex(const std::string& dex_location,
634 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800635 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800636 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800637 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000638 const std::vector<std::string>& extra_args = {},
639 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800640 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800641 const char* location = dex_location.c_str();
642 std::string error_msg;
643 std::vector<std::unique_ptr<const DexFile>> dex_files;
644 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
645 EXPECT_EQ(dex_files.size(), 1U);
646 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800647 GenerateProfile(profile_location,
648 dex_location,
649 num_profile_classes,
650 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800651 std::vector<std::string> copy(extra_args);
652 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800653 std::unique_ptr<File> app_image_file;
654 if (!app_image_file_name.empty()) {
655 if (use_fd) {
656 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
657 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
658 } else {
659 copy.push_back("--app-image-file=" + app_image_file_name);
660 }
661 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800662 GenerateOdexForTest(dex_location,
663 odex_location,
664 CompilerFilter::kSpeedProfile,
665 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000666 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800667 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800668 if (app_image_file != nullptr) {
669 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
670 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800671 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700672
Mathieu Chartier046854b2017-03-01 17:16:22 -0800673 uint64_t GetImageSize(const std::string& image_file_name) {
674 EXPECT_FALSE(image_file_name.empty());
675 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
676 CHECK(file != nullptr);
677 ImageHeader image_header;
678 const bool success = file->ReadFully(&image_header, sizeof(image_header));
679 CHECK(success);
680 CHECK(image_header.IsValid());
681 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
682 return image_header.GetImageSize();
683 }
684
685 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800686 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
687 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800688 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800689 Copy(GetDexSrc2(), dex_location);
690
Mathieu Chartier046854b2017-03-01 17:16:22 -0800691 uint64_t image_file_empty_profile = 0;
692 if (app_image) {
693 CompileProfileOdex(dex_location,
694 odex_location,
695 app_image_file,
696 /* use_fd */ false,
697 /* num_profile_classes */ 0);
698 CheckValidity();
699 ASSERT_TRUE(success_);
700 // Don't check the result since CheckResult relies on the class being in the profile.
701 image_file_empty_profile = GetImageSize(app_image_file);
702 EXPECT_GT(image_file_empty_profile, 0u);
703 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700704
Mathieu Chartier046854b2017-03-01 17:16:22 -0800705 // Small profile.
706 CompileProfileOdex(dex_location,
707 odex_location,
708 app_image_file,
709 /* use_fd */ false,
710 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700711 CheckValidity();
712 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800713 CheckResult(dex_location, odex_location, app_image_file);
714
715 if (app_image) {
716 // Test that the profile made a difference by adding more classes.
717 const uint64_t image_file_small_profile = GetImageSize(app_image_file);
718 CHECK_LT(image_file_empty_profile, image_file_small_profile);
719 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700720 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800721
722 void RunTestVDex() {
723 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
724 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
725 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800726 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800727 Copy(GetDexSrc2(), dex_location);
728
729 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
730 CHECK(vdex_file1 != nullptr) << vdex_location;
731 ScratchFile vdex_file2;
732 {
733 std::string input_vdex = "--input-vdex-fd=-1";
734 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
735 CompileProfileOdex(dex_location,
736 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800737 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800738 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800739 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800740 { input_vdex, output_vdex });
741 EXPECT_GT(vdex_file1->GetLength(), 0u);
742 }
743 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000744 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800745 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
746 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
747 CompileProfileOdex(dex_location,
748 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800749 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800750 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800751 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000752 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100753 /* expect_success */ true);
754 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800755 }
756 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
757 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100758 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800759 }
760
Mathieu Chartier046854b2017-03-01 17:16:22 -0800761 void CheckResult(const std::string& dex_location,
762 const std::string& odex_location,
763 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700764 // Host/target independent checks.
765 std::string error_msg;
766 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
767 odex_location.c_str(),
768 nullptr,
769 nullptr,
770 false,
771 /*low_4gb*/false,
772 dex_location.c_str(),
773 &error_msg));
774 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
775
Jeff Hao042e8982016-10-19 11:17:11 -0700776 const char* location = dex_location.c_str();
777 std::vector<std::unique_ptr<const DexFile>> dex_files;
778 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
779 EXPECT_EQ(dex_files.size(), 1U);
780 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
781
Jeff Hao608f2ce2016-10-19 11:17:11 -0700782 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700783 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
784 ASSERT_TRUE(new_dex_file != nullptr);
785 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700786 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700787 ASSERT_GE(class_def_count, 2U);
788
789 // The new layout swaps the classes at indexes 0 and 1.
790 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
791 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
792 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
793 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
794 EXPECT_EQ(old_class0, new_class1);
795 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700796 }
797
Jeff Haoc155b052017-01-17 17:43:29 -0800798 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800799
800 if (!app_image_file_name.empty()) {
801 // Go peek at the image header to make sure it was large enough to contain the class.
802 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
803 ImageHeader image_header;
804 bool success = file->ReadFully(&image_header, sizeof(image_header));
805 ASSERT_TRUE(success);
806 ASSERT_TRUE(image_header.IsValid());
807 EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
808 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700809 }
810
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800811 // Check whether the dex2oat run was really successful.
812 void CheckValidity() {
813 if (kIsTargetBuild) {
814 CheckTargetValidity();
815 } else {
816 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700817 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800818 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700819
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800820 void CheckTargetValidity() {
821 // TODO: Ignore for now.
822 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700823
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800824 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
825 void CheckHostValidity() {
826 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
827 }
828};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700829
830TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800831 RunTest(/* app-image */ false);
832}
833
834TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
835 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700836}
837
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800838TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
839 RunTestVDex();
840}
841
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100842class Dex2oatUnquickenTest : public Dex2oatTest {
843 protected:
844 void RunUnquickenMultiDex() {
845 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
846 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
847 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
848 Copy(GetTestDexFileName("MultiDex"), dex_location);
849
850 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
851 CHECK(vdex_file1 != nullptr) << vdex_location;
852 // Quicken the dex file into a vdex file.
853 {
854 std::string input_vdex = "--input-vdex-fd=-1";
855 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
856 GenerateOdexForTest(dex_location,
857 odex_location,
858 CompilerFilter::kQuicken,
859 { input_vdex, output_vdex },
860 /* expect_success */ true,
861 /* use_fd */ true);
862 EXPECT_GT(vdex_file1->GetLength(), 0u);
863 }
864 // Unquicken by running the verify compiler filter on the vdex file.
865 {
866 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
867 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
868 GenerateOdexForTest(dex_location,
869 odex_location,
870 CompilerFilter::kVerify,
871 { input_vdex, output_vdex },
872 /* expect_success */ true,
873 /* use_fd */ true);
874 }
875 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
876 CheckResult(dex_location, odex_location);
877 ASSERT_TRUE(success_);
878 }
879
880 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
881 std::string error_msg;
882 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
883 odex_location.c_str(),
884 nullptr,
885 nullptr,
886 false,
887 /*low_4gb*/false,
888 dex_location.c_str(),
889 &error_msg));
890 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
891 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
892
893 // Iterate over the dex files and ensure there is no quickened instruction.
894 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
895 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
896 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
897 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
898 const uint8_t* class_data = dex_file->GetClassData(class_def);
899 if (class_data != nullptr) {
900 for (ClassDataItemIterator class_it(*dex_file, class_data);
901 class_it.HasNext();
902 class_it.Next()) {
903 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
904 for (CodeItemIterator it(*class_it.GetMethodCodeItem()); !it.Done(); it.Advance()) {
905 Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction());
906 ASSERT_FALSE(inst->IsQuickened());
907 }
908 }
909 }
910 }
911 }
912 }
913 }
914};
915
916TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
917 RunUnquickenMultiDex();
918}
919
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800920class Dex2oatWatchdogTest : public Dex2oatTest {
921 protected:
922 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
923 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
924 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
925
926 Copy(GetTestDexFileName(), dex_location);
927
928 std::vector<std::string> copy(extra_args);
929
930 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
931 copy.push_back("--swap-file=" + swap_location);
932 GenerateOdexForTest(dex_location,
933 odex_location,
934 CompilerFilter::kSpeed,
935 copy,
936 expect_success);
937 }
938
939 std::string GetTestDexFileName() {
940 return GetDexSrc1();
941 }
942};
943
944TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
945 // Check with default.
946 RunTest(true);
947
948 // Check with ten minutes.
949 RunTest(true, { "--watchdog-timeout=600000" });
950}
951
952TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
953 // Check with ten milliseconds.
954 RunTest(false, { "--watchdog-timeout=10" });
955}
956
Andreas Gampef7882972017-03-20 16:35:24 -0700957class Dex2oatReturnCodeTest : public Dex2oatTest {
958 protected:
959 int RunTest(const std::vector<std::string>& extra_args = {}) {
960 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
961 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
962
963 Copy(GetTestDexFileName(), dex_location);
964
965 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700966 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700967 odex_location,
968 CompilerFilter::kSpeed,
969 &error_msg,
970 extra_args);
971 }
972
973 std::string GetTestDexFileName() {
974 return GetDexSrc1();
975 }
976};
977
978TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -0700979 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -0700980 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
981 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
982}
983
Calin Juravle1ce70852017-06-28 10:59:03 -0700984class Dex2oatClassLoaderContextTest : public Dex2oatTest {
985 protected:
986 void RunTest(const char* class_loader_context,
987 const char* expected_classpath_key,
988 bool expected_success,
989 bool use_second_source = false) {
990 std::string dex_location = GetUsedDexLocation();
991 std::string odex_location = GetUsedOatLocation();
992
993 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
994
995 std::string error_msg;
996 std::vector<std::string> extra_args;
997 if (class_loader_context != nullptr) {
998 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
999 }
1000 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1001 ASSERT_TRUE(expected_classpath_key != nullptr);
1002 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1003 ASSERT_TRUE(classpath != nullptr);
1004 ASSERT_STREQ(expected_classpath_key, classpath);
1005 };
1006
1007 GenerateOdexForTest(dex_location,
1008 odex_location,
1009 CompilerFilter::kQuicken,
1010 extra_args,
1011 expected_success,
1012 /*use_fd*/ false,
1013 check_oat);
1014 }
1015
1016 std::string GetUsedDexLocation() {
1017 return GetScratchDir() + "/Context.jar";
1018 }
1019
1020 std::string GetUsedOatLocation() {
1021 return GetOdexDir() + "/Context.odex";
1022 }
1023
Calin Juravle7b0648a2017-07-07 18:40:50 -07001024 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001025};
1026
1027TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1028 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1029}
1030
1031TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1032 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1033}
1034
1035TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1036 RunTest(OatFile::kSpecialSharedLibrary,
1037 OatFile::kSpecialSharedLibrary,
1038 /*expected_success*/ true);
1039}
1040
1041TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1042 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1043 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1044}
1045
1046TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1047 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001048
1049 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001050 std::string expected_classpath_key = "PCL[" +
1051 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001052 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1053}
1054
1055TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1056 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1057 Copy(GetStrippedDexSrc1(), stripped_classpath);
1058
1059 std::string context = "PCL[" + stripped_classpath + "]";
1060 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001061 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001062}
1063
1064TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1065 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1066 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1067
1068 Copy(GetDexSrc1(), stripped_classpath);
1069
1070 GenerateOdexForTest(stripped_classpath,
1071 odex_for_classpath,
1072 CompilerFilter::kQuicken,
1073 {},
1074 true);
1075
1076 // Strip the dex file
1077 Copy(GetStrippedDexSrc1(), stripped_classpath);
1078
1079 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001080 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001081 {
1082 // Open the oat file to get the expected classpath.
1083 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false);
1084 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1085 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1086 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001087 expected_classpath_key = "PCL[";
1088 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1089 if (i > 0) {
1090 expected_classpath_key + ":";
1091 }
1092 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1093 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1094 }
1095 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001096 }
1097
1098 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001099 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001100 /*expected_success*/ true,
1101 /*use_second_source*/ true);
1102}
1103
1104TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1105 std::string context = "PCL[does_not_exists.dex]";
1106 // Expect an empty context because stripped dex files cannot be open.
1107 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1108}
1109
Calin Juravlec79470d2017-07-12 17:37:42 -07001110TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1111 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1112 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1113
1114 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1115 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1116 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1117 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1118
1119 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1120}
1121
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001122class Dex2oatDeterminism : public Dex2oatTest {};
1123
1124TEST_F(Dex2oatDeterminism, UnloadCompile) {
1125 if (!kUseReadBarrier &&
1126 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1127 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1128 LOG(INFO) << "Test requires determinism support.";
1129 return;
1130 }
1131 Runtime* const runtime = Runtime::Current();
1132 std::string out_dir = GetScratchDir();
1133 const std::string base_oat_name = out_dir + "/base.oat";
1134 const std::string base_vdex_name = out_dir + "/base.vdex";
1135 const std::string unload_oat_name = out_dir + "/unload.oat";
1136 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1137 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1138 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1139 const std::string app_image_name = out_dir + "/unload.art";
1140 std::string error_msg;
1141 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1142 ASSERT_GT(spaces.size(), 0u);
1143 const std::string image_location = spaces[0]->GetImageLocation();
1144 // Without passing in an app image, it will unload in between compilations.
1145 const int res = GenerateOdexForTestWithStatus(
1146 GetLibCoreDexFileNames(),
1147 base_oat_name,
1148 CompilerFilter::Filter::kQuicken,
1149 &error_msg,
1150 {"--force-determinism", "--avoid-storing-invocation"});
1151 EXPECT_EQ(res, 0);
1152 Copy(base_oat_name, unload_oat_name);
1153 Copy(base_vdex_name, unload_vdex_name);
1154 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1155 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1156 ASSERT_TRUE(unload_oat != nullptr);
1157 ASSERT_TRUE(unload_vdex != nullptr);
1158 EXPECT_GT(unload_oat->GetLength(), 0u);
1159 EXPECT_GT(unload_vdex->GetLength(), 0u);
1160 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1161 // the same.
1162 const int res2 = GenerateOdexForTestWithStatus(
1163 GetLibCoreDexFileNames(),
1164 base_oat_name,
1165 CompilerFilter::Filter::kQuicken,
1166 &error_msg,
1167 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1168 EXPECT_EQ(res2, 0);
1169 Copy(base_oat_name, no_unload_oat_name);
1170 Copy(base_vdex_name, no_unload_vdex_name);
1171 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1172 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1173 ASSERT_TRUE(no_unload_oat != nullptr);
1174 ASSERT_TRUE(no_unload_vdex != nullptr);
1175 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1176 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1177 // Verify that both of the files are the same (odex and vdex).
1178 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1179 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1180 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1181 << unload_oat_name << " " << no_unload_oat_name;
1182 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1183 << unload_vdex_name << " " << no_unload_vdex_name;
1184 // App image file.
1185 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1186 ASSERT_TRUE(app_image_file != nullptr);
1187 EXPECT_GT(app_image_file->GetLength(), 0u);
1188}
1189
Andreas Gampee1459ae2016-06-29 09:36:30 -07001190} // namespace art