blob: 4ac8e6a3eb346358cda47d15e21f730910f1436f [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
Igor Murashkin5573c372017-11-16 13:34:30 -080017#include <regex>
Andreas Gampe7adeda82016-07-25 08:27:35 -070018#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
Andreas Gampe57943812017-12-06 21:39:13 -080025#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027
Andreas Gampee1459ae2016-06-29 09:36:30 -070028#include "common_runtime_test.h"
29
Andreas Gampee1459ae2016-06-29 09:36:30 -070030#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"
David Sehr013fd802018-01-11 22:55:24 -080033#include "dex/art_dex_file_loader.h"
Mathieu Chartier05f90d12018-02-07 13:47:17 -080034#include "dex/base64_test_util.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/code_item_accessors-inl.h"
36#include "dex/dex_file-inl.h"
37#include "dex/dex_file_loader.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070038#include "dex2oat_environment_test.h"
Andreas Gampef7882972017-03-20 16:35:24 -070039#include "dex2oat_return_codes.h"
Calin Juravle33083d62017-01-18 15:29:12 -080040#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070041#include "oat.h"
42#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070043#include "utils.h"
44
Andreas Gampee1459ae2016-06-29 09:36:30 -070045namespace art {
46
Mathieu Chartierea650f32017-05-24 12:04:13 -070047static constexpr size_t kMaxMethodIds = 65535;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070048static constexpr bool kDebugArgs = false;
Mathieu Chartier02129102017-12-22 11:04:01 -080049static const char* kDisableCompactDex = "--compact-dex-level=none";
Mathieu Chartierea650f32017-05-24 12:04:13 -070050
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080051using android::base::StringPrintf;
52
Andreas Gampee1459ae2016-06-29 09:36:30 -070053class Dex2oatTest : public Dex2oatEnvironmentTest {
54 public:
55 virtual void TearDown() OVERRIDE {
56 Dex2oatEnvironmentTest::TearDown();
57
58 output_ = "";
59 error_msg_ = "";
60 success_ = false;
61 }
62
63 protected:
Mathieu Chartier9e050df2017-08-09 10:05:47 -070064 int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
Andreas Gampef7882972017-03-20 16:35:24 -070065 const std::string& odex_location,
66 CompilerFilter::Filter filter,
67 std::string* error_msg,
68 const std::vector<std::string>& extra_args = {},
69 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080070 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070071 std::vector<std::string> args;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070072 // Add dex file args.
73 for (const std::string& dex_location : dex_locations) {
74 args.push_back("--dex-file=" + dex_location);
75 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080076 if (use_fd) {
77 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
78 CHECK(oat_file != nullptr) << odex_location;
79 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080080 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080081 } else {
82 args.push_back("--oat-file=" + odex_location);
83 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070084 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
85 args.push_back("--runtime-arg");
86 args.push_back("-Xnorelocate");
87
88 args.insert(args.end(), extra_args.begin(), extra_args.end());
89
Andreas Gampef7882972017-03-20 16:35:24 -070090 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080091 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070092 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080093 }
Andreas Gampef7882972017-03-20 16:35:24 -070094 return status;
95 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070096
Andreas Gampe641a4732017-08-24 13:21:35 -070097 void GenerateOdexForTest(
98 const std::string& dex_location,
99 const std::string& odex_location,
100 CompilerFilter::Filter filter,
101 const std::vector<std::string>& extra_args = {},
102 bool expect_success = true,
103 bool use_fd = false) {
104 GenerateOdexForTest(dex_location,
105 odex_location,
106 filter,
107 extra_args,
108 expect_success,
109 use_fd,
110 [](const OatFile&) {});
111 }
112
Andreas Gampe80ddf272018-01-11 09:41:00 -0800113 bool test_accepts_odex_file_on_failure = false;
114
Andreas Gampe641a4732017-08-24 13:21:35 -0700115 template <typename T>
116 void GenerateOdexForTest(
117 const std::string& dex_location,
118 const std::string& odex_location,
119 CompilerFilter::Filter filter,
120 const std::vector<std::string>& extra_args,
121 bool expect_success,
122 bool use_fd,
123 T check_oat) {
Andreas Gampef7882972017-03-20 16:35:24 -0700124 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700125 int status = GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700126 odex_location,
127 filter,
128 &error_msg,
129 extra_args,
130 use_fd);
Andreas Gampe80ddf272018-01-11 09:41:00 -0800131 bool success = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700132 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800133 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700134
135 // Verify the odex file was generated as expected.
136 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
137 odex_location.c_str(),
138 nullptr,
139 nullptr,
140 false,
141 /*low_4gb*/false,
142 dex_location.c_str(),
143 &error_msg));
144 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
145
146 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700147 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700148 } else {
149 ASSERT_FALSE(success) << output_;
150
151 error_msg_ = error_msg;
152
Andreas Gampe80ddf272018-01-11 09:41:00 -0800153 if (!test_accepts_odex_file_on_failure) {
154 // Verify there's no loadable odex file.
155 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
156 odex_location.c_str(),
157 nullptr,
158 nullptr,
159 false,
160 /*low_4gb*/false,
161 dex_location.c_str(),
162 &error_msg));
163 ASSERT_TRUE(odex_file.get() == nullptr);
164 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700165 }
166 }
167
Calin Juravle1ccf6132017-08-02 17:46:53 -0700168 // Check the input compiler filter against the generated oat file's filter. May be overridden
Andreas Gampee1459ae2016-06-29 09:36:30 -0700169 // in subclasses when equality is not expected.
170 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
171 EXPECT_EQ(expected, actual);
172 }
173
Andreas Gampef7882972017-03-20 16:35:24 -0700174 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700175 Runtime* runtime = Runtime::Current();
176
177 const std::vector<gc::space::ImageSpace*>& image_spaces =
178 runtime->GetHeap()->GetBootImageSpaces();
179 if (image_spaces.empty()) {
180 *error_msg = "No image location found for Dex2Oat.";
181 return false;
182 }
183 std::string image_location = image_spaces[0]->GetImageLocation();
184
185 std::vector<std::string> argv;
186 argv.push_back(runtime->GetCompilerExecutable());
Calin Juravle1ccf6132017-08-02 17:46:53 -0700187
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000188 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700189 argv.push_back("--debuggable");
190 }
191 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
192
193 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100194 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700195 }
196
197 if (runtime->MustRelocateIfPossible()) {
198 argv.push_back("--runtime-arg");
199 argv.push_back("-Xrelocate");
200 } else {
201 argv.push_back("--runtime-arg");
202 argv.push_back("-Xnorelocate");
203 }
204
205 if (!kIsTargetBuild) {
206 argv.push_back("--host");
207 }
208
209 argv.push_back("--boot-image=" + image_location);
210
211 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
212 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
213
214 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
215
216 // We must set --android-root.
217 const char* android_root = getenv("ANDROID_ROOT");
218 CHECK(android_root != nullptr);
219 argv.push_back("--android-root=" + std::string(android_root));
220
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700221 if (kDebugArgs) {
222 std::string all_args;
223 for (const std::string& arg : argv) {
224 all_args += arg + " ";
225 }
226 LOG(ERROR) << all_args;
227 }
228
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100229 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700230
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100231 if (pipe(link) == -1) {
232 return false;
233 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700234
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100235 pid_t pid = fork();
236 if (pid == -1) {
237 return false;
238 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700239
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100240 if (pid == 0) {
241 // We need dex2oat to actually log things.
242 setenv("ANDROID_LOG_TAGS", "*:d", 1);
243 dup2(link[1], STDERR_FILENO);
244 close(link[0]);
245 close(link[1]);
246 std::vector<const char*> c_args;
247 for (const std::string& str : argv) {
248 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700249 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100250 c_args.push_back(nullptr);
251 execv(c_args[0], const_cast<char* const*>(c_args.data()));
252 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700253 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100254 } else {
255 close(link[1]);
256 char buffer[128];
257 memset(buffer, 0, 128);
258 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700259
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100260 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
261 output_ += std::string(buffer, bytes_read);
262 }
263 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700264 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100265 if (waitpid(pid, &status, 0) != -1) {
266 success_ = (status == 0);
267 }
Andreas Gampef7882972017-03-20 16:35:24 -0700268 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700269 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700270 }
271
272 std::string output_ = "";
273 std::string error_msg_ = "";
274 bool success_ = false;
275};
276
277class Dex2oatSwapTest : public Dex2oatTest {
278 protected:
279 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
280 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
281 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
282
Andreas Gampe7adeda82016-07-25 08:27:35 -0700283 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700284
285 std::vector<std::string> copy(extra_args);
286
287 std::unique_ptr<ScratchFile> sf;
288 if (use_fd) {
289 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800290 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700291 } else {
292 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
293 copy.push_back("--swap-file=" + swap_location);
294 }
295 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
296
297 CheckValidity();
298 ASSERT_TRUE(success_);
299 CheckResult(expect_use);
300 }
301
Andreas Gampe7adeda82016-07-25 08:27:35 -0700302 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000303 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700304 }
305
306 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700307 if (kIsTargetBuild) {
308 CheckTargetResult(expect_use);
309 } else {
310 CheckHostResult(expect_use);
311 }
312 }
313
Andreas Gampe7adeda82016-07-25 08:27:35 -0700314 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
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
Andreas Gampe7adeda82016-07-25 08:27:35 -0700320 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700321 if (!kIsTargetBuild) {
322 if (expect_use) {
323 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
324 << output_;
325 } else {
326 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
327 << output_;
328 }
329 }
330 }
331
332 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700333 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700334 if (kIsTargetBuild) {
335 CheckTargetValidity();
336 } else {
337 CheckHostValidity();
338 }
339 }
340
Andreas Gampe7adeda82016-07-25 08:27:35 -0700341 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700342 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
343 // something for variants with file descriptor where we can control the lifetime of
344 // the swap file and thus take a look at it.
345 }
346
347 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700348 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700349 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
350 }
351};
352
353TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
354 RunTest(false /* use_fd */, false /* expect_use */);
355 RunTest(true /* use_fd */, false /* expect_use */);
356}
357
358TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
359 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
360 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
361}
362
363TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
364 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
365 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
366}
367
368TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
369 RunTest(false /* use_fd */,
370 true /* expect_use */,
371 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
372 RunTest(true /* use_fd */,
373 true /* expect_use */,
374 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
375}
376
Andreas Gampe7adeda82016-07-25 08:27:35 -0700377class Dex2oatSwapUseTest : public Dex2oatSwapTest {
378 protected:
379 void CheckHostResult(bool expect_use) OVERRIDE {
380 if (!kIsTargetBuild) {
381 if (expect_use) {
382 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
383 << output_;
384 } else {
385 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
386 << output_;
387 }
388 }
389 }
390
391 std::string GetTestDexFileName() OVERRIDE {
392 // Use Statics as it has a handful of functions.
393 return CommonRuntimeTest::GetTestDexFileName("Statics");
394 }
395
396 void GrabResult1() {
397 if (!kIsTargetBuild) {
398 native_alloc_1_ = ParseNativeAlloc();
399 swap_1_ = ParseSwap(false /* expected */);
400 } else {
401 native_alloc_1_ = std::numeric_limits<size_t>::max();
402 swap_1_ = 0;
403 }
404 }
405
406 void GrabResult2() {
407 if (!kIsTargetBuild) {
408 native_alloc_2_ = ParseNativeAlloc();
409 swap_2_ = ParseSwap(true /* expected */);
410 } else {
411 native_alloc_2_ = 0;
412 swap_2_ = std::numeric_limits<size_t>::max();
413 }
414 }
415
416 private:
417 size_t ParseNativeAlloc() {
418 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
419 std::smatch native_alloc_match;
420 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
421 if (!found) {
422 EXPECT_TRUE(found);
423 return 0;
424 }
425 if (native_alloc_match.size() != 2U) {
426 EXPECT_EQ(native_alloc_match.size(), 2U);
427 return 0;
428 }
429
430 std::istringstream stream(native_alloc_match[1].str());
431 size_t value;
432 stream >> value;
433
434 return value;
435 }
436
437 size_t ParseSwap(bool expected) {
438 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
439 std::smatch swap_match;
440 bool found = std::regex_search(output_, swap_match, swap_regex);
441 if (found != expected) {
442 EXPECT_EQ(expected, found);
443 return 0;
444 }
445
446 if (!found) {
447 return 0;
448 }
449
450 if (swap_match.size() != 2U) {
451 EXPECT_EQ(swap_match.size(), 2U);
452 return 0;
453 }
454
455 std::istringstream stream(swap_match[1].str());
456 size_t value;
457 stream >> value;
458
459 return value;
460 }
461
462 protected:
463 size_t native_alloc_1_;
464 size_t native_alloc_2_;
465
466 size_t swap_1_;
467 size_t swap_2_;
468};
469
470TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700471 // Native memory usage isn't correctly tracked under sanitization.
472 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
473
Vladimir Marko57070da2017-02-14 16:16:30 +0000474 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000475 // hold true on some x86 systems; disable this test while we
476 // investigate (b/29259363).
477 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000478
Andreas Gampe7adeda82016-07-25 08:27:35 -0700479 RunTest(false /* use_fd */,
480 false /* expect_use */);
481 GrabResult1();
482 std::string output_1 = output_;
483
484 output_ = "";
485
486 RunTest(false /* use_fd */,
487 true /* expect_use */,
488 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
489 GrabResult2();
490 std::string output_2 = output_;
491
492 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
493 EXPECT_LT(native_alloc_2_, native_alloc_1_);
494 EXPECT_LT(swap_1_, swap_2_);
495
496 LOG(ERROR) << output_1;
497 LOG(ERROR) << output_2;
498 }
499}
500
Andreas Gampe67f02822016-06-24 21:05:23 -0700501class Dex2oatVeryLargeTest : public Dex2oatTest {
502 protected:
503 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
504 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
505 // Ignore, we'll do our own checks.
506 }
507
508 void RunTest(CompilerFilter::Filter filter,
509 bool expect_large,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700510 bool expect_downgrade,
Andreas Gampe67f02822016-06-24 21:05:23 -0700511 const std::vector<std::string>& extra_args = {}) {
512 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
513 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700514 std::string app_image_file = GetScratchDir() + "/Test.art";
Andreas Gampe67f02822016-06-24 21:05:23 -0700515
516 Copy(GetDexSrc1(), dex_location);
517
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700518 std::vector<std::string> new_args(extra_args);
519 new_args.push_back("--app-image-file=" + app_image_file);
520 GenerateOdexForTest(dex_location, odex_location, filter, new_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700521
522 CheckValidity();
523 ASSERT_TRUE(success_);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700524 CheckResult(dex_location,
525 odex_location,
526 app_image_file,
527 filter,
528 expect_large,
529 expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700530 }
531
532 void CheckResult(const std::string& dex_location,
533 const std::string& odex_location,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700534 const std::string& app_image_file,
Andreas Gampe67f02822016-06-24 21:05:23 -0700535 CompilerFilter::Filter filter,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700536 bool expect_large,
537 bool expect_downgrade) {
538 if (expect_downgrade) {
539 EXPECT_TRUE(expect_large);
540 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700541 // Host/target independent checks.
542 std::string error_msg;
543 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
544 odex_location.c_str(),
545 nullptr,
546 nullptr,
547 false,
548 /*low_4gb*/false,
549 dex_location.c_str(),
550 &error_msg));
551 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700552 EXPECT_GT(app_image_file.length(), 0u);
553 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700554 if (expect_large) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700555 // Note: we cannot check the following
556 // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700557 // The reason is that the filter override currently happens when the dex files are
558 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
559 // store cannot be changed, and the original filter is set in stone.
560
561 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
562 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
563 ASSERT_TRUE(dex_file != nullptr);
564 uint32_t class_def_count = dex_file->NumClassDefs();
565 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
566 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
567 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
568 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
569 }
570 }
571
572 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100573 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700574 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
575 }
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700576
577 // If expect large, make sure the app image isn't generated or is empty.
578 if (file != nullptr) {
579 EXPECT_EQ(file->GetLength(), 0u);
580 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700581 } else {
582 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700583 ASSERT_TRUE(file != nullptr) << app_image_file;
584 EXPECT_GT(file->GetLength(), 0u);
Andreas Gampe67f02822016-06-24 21:05:23 -0700585 }
586
587 // Host/target dependent checks.
588 if (kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700589 CheckTargetResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700590 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700591 CheckHostResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700592 }
593 }
594
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700595 void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700596 // TODO: Ignore for now. May do something for fd things.
597 }
598
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700599 void CheckHostResult(bool expect_downgrade) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700600 if (!kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700601 if (expect_downgrade) {
602 EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700603 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700604 EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700605 }
606 }
607 }
608
609 // Check whether the dex2oat run was really successful.
610 void CheckValidity() {
611 if (kIsTargetBuild) {
612 CheckTargetValidity();
613 } else {
614 CheckHostValidity();
615 }
616 }
617
618 void CheckTargetValidity() {
619 // TODO: Ignore for now.
620 }
621
622 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
623 void CheckHostValidity() {
624 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
625 }
626};
627
628TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700629 RunTest(CompilerFilter::kAssumeVerified, false, false);
630 RunTest(CompilerFilter::kExtract, false, false);
631 RunTest(CompilerFilter::kQuicken, false, false);
632 RunTest(CompilerFilter::kSpeed, false, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700633
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700634 RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
635 RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
636 RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" });
637 RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700638}
639
640TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700641 RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
642 RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
643 RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" });
644 RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700645}
646
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800647// Regressin test for b/35665292.
648TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
649 // Test that dex2oat doesn't crash with speed-profile but no input profile.
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700650 RunTest(CompilerFilter::kSpeedProfile, false, false);
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800651}
652
Jeff Hao608f2ce2016-10-19 11:17:11 -0700653class Dex2oatLayoutTest : public Dex2oatTest {
654 protected:
655 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
656 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
657 // Ignore, we'll do our own checks.
658 }
659
Jeff Hao41fba6a2016-11-28 11:53:33 -0800660 // Emits a profile with a single dex file with the given location and a single class index of 1.
661 void GenerateProfile(const std::string& test_profile,
662 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800663 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800664 uint32_t checksum) {
665 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
666 CHECK_GE(profile_test_fd, 0);
667
668 ProfileCompilationInfo info;
669 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800670 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700671 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800672 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800673 bool result = info.Save(profile_test_fd);
674 close(profile_test_fd);
675 ASSERT_TRUE(result);
676 }
677
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800678 void CompileProfileOdex(const std::string& dex_location,
679 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800680 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800681 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800682 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000683 const std::vector<std::string>& extra_args = {},
684 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800685 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800686 const char* location = dex_location.c_str();
687 std::string error_msg;
688 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800689 const ArtDexFileLoader dex_file_loader;
690 ASSERT_TRUE(dex_file_loader.Open(
Nicolas Geoffray095c6c92017-10-19 13:59:55 +0100691 location, location, /* verify */ true, /* verify_checksum */ true, &error_msg, &dex_files));
Jeff Hao41fba6a2016-11-28 11:53:33 -0800692 EXPECT_EQ(dex_files.size(), 1U);
693 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800694 GenerateProfile(profile_location,
695 dex_location,
696 num_profile_classes,
697 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800698 std::vector<std::string> copy(extra_args);
699 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800700 std::unique_ptr<File> app_image_file;
701 if (!app_image_file_name.empty()) {
702 if (use_fd) {
703 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
704 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
705 } else {
706 copy.push_back("--app-image-file=" + app_image_file_name);
707 }
708 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800709 GenerateOdexForTest(dex_location,
710 odex_location,
711 CompilerFilter::kSpeedProfile,
712 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000713 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800714 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800715 if (app_image_file != nullptr) {
716 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
717 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800718 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700719
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100720 uint64_t GetImageObjectSectionSize(const std::string& image_file_name) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800721 EXPECT_FALSE(image_file_name.empty());
722 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
723 CHECK(file != nullptr);
724 ImageHeader image_header;
725 const bool success = file->ReadFully(&image_header, sizeof(image_header));
726 CHECK(success);
727 CHECK(image_header.IsValid());
728 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100729 return image_header.GetObjectsSection().Size();
Mathieu Chartier046854b2017-03-01 17:16:22 -0800730 }
731
732 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800733 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
734 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800735 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800736 Copy(GetDexSrc2(), dex_location);
737
Mathieu Chartier046854b2017-03-01 17:16:22 -0800738 uint64_t image_file_empty_profile = 0;
739 if (app_image) {
740 CompileProfileOdex(dex_location,
741 odex_location,
742 app_image_file,
743 /* use_fd */ false,
744 /* num_profile_classes */ 0);
745 CheckValidity();
746 ASSERT_TRUE(success_);
747 // Don't check the result since CheckResult relies on the class being in the profile.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100748 image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800749 EXPECT_GT(image_file_empty_profile, 0u);
750 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700751
Mathieu Chartier046854b2017-03-01 17:16:22 -0800752 // Small profile.
753 CompileProfileOdex(dex_location,
754 odex_location,
755 app_image_file,
756 /* use_fd */ false,
757 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700758 CheckValidity();
759 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800760 CheckResult(dex_location, odex_location, app_image_file);
761
762 if (app_image) {
763 // Test that the profile made a difference by adding more classes.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100764 const uint64_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
765 ASSERT_LT(image_file_empty_profile, image_file_small_profile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800766 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700767 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800768
769 void RunTestVDex() {
770 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
771 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
772 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800773 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800774 Copy(GetDexSrc2(), dex_location);
775
776 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
777 CHECK(vdex_file1 != nullptr) << vdex_location;
778 ScratchFile vdex_file2;
779 {
780 std::string input_vdex = "--input-vdex-fd=-1";
781 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
782 CompileProfileOdex(dex_location,
783 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800784 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800785 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800786 /* num_profile_classes */ 1,
Mathieu Chartierf1609832018-01-31 03:09:56 -0800787 { input_vdex, output_vdex });
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800788 EXPECT_GT(vdex_file1->GetLength(), 0u);
789 }
790 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000791 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800792 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
793 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
794 CompileProfileOdex(dex_location,
795 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800796 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800797 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800798 /* num_profile_classes */ 1,
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800799 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100800 /* expect_success */ true);
801 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800802 }
803 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
804 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100805 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800806 }
807
Mathieu Chartier046854b2017-03-01 17:16:22 -0800808 void CheckResult(const std::string& dex_location,
809 const std::string& odex_location,
810 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700811 // Host/target independent checks.
812 std::string error_msg;
813 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
814 odex_location.c_str(),
815 nullptr,
816 nullptr,
817 false,
818 /*low_4gb*/false,
819 dex_location.c_str(),
820 &error_msg));
821 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
822
Jeff Hao042e8982016-10-19 11:17:11 -0700823 const char* location = dex_location.c_str();
824 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800825 const ArtDexFileLoader dex_file_loader;
826 ASSERT_TRUE(dex_file_loader.Open(
Nicolas Geoffray095c6c92017-10-19 13:59:55 +0100827 location, location, /* verify */ true, /* verify_checksum */ true, &error_msg, &dex_files));
Jeff Hao042e8982016-10-19 11:17:11 -0700828 EXPECT_EQ(dex_files.size(), 1U);
829 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
830
Jeff Hao608f2ce2016-10-19 11:17:11 -0700831 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700832 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
833 ASSERT_TRUE(new_dex_file != nullptr);
834 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700835 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700836 ASSERT_GE(class_def_count, 2U);
837
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700838 // Make sure the indexes stay the same.
Jeff Hao042e8982016-10-19 11:17:11 -0700839 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
840 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
841 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
842 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700843 EXPECT_EQ(old_class0, new_class0);
844 EXPECT_EQ(old_class1, new_class1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700845 }
846
Jeff Haoc155b052017-01-17 17:43:29 -0800847 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800848
849 if (!app_image_file_name.empty()) {
850 // Go peek at the image header to make sure it was large enough to contain the class.
851 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
852 ImageHeader image_header;
853 bool success = file->ReadFully(&image_header, sizeof(image_header));
854 ASSERT_TRUE(success);
855 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100856 EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800857 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700858 }
859
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800860 // Check whether the dex2oat run was really successful.
861 void CheckValidity() {
862 if (kIsTargetBuild) {
863 CheckTargetValidity();
864 } else {
865 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700866 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800867 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700868
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800869 void CheckTargetValidity() {
870 // TODO: Ignore for now.
871 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700872
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800873 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
874 void CheckHostValidity() {
875 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
876 }
877};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700878
879TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800880 RunTest(/* app-image */ false);
881}
882
883TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
884 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700885}
886
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800887TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
888 RunTestVDex();
889}
890
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100891class Dex2oatUnquickenTest : public Dex2oatTest {
892 protected:
893 void RunUnquickenMultiDex() {
894 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
895 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
896 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
897 Copy(GetTestDexFileName("MultiDex"), dex_location);
898
899 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
900 CHECK(vdex_file1 != nullptr) << vdex_location;
901 // Quicken the dex file into a vdex file.
902 {
903 std::string input_vdex = "--input-vdex-fd=-1";
904 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
905 GenerateOdexForTest(dex_location,
906 odex_location,
907 CompilerFilter::kQuicken,
Mathieu Chartierf1609832018-01-31 03:09:56 -0800908 { input_vdex, output_vdex },
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100909 /* expect_success */ true,
910 /* use_fd */ true);
911 EXPECT_GT(vdex_file1->GetLength(), 0u);
912 }
913 // Unquicken by running the verify compiler filter on the vdex file.
914 {
915 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
916 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
917 GenerateOdexForTest(dex_location,
918 odex_location,
919 CompilerFilter::kVerify,
Mathieu Chartier02129102017-12-22 11:04:01 -0800920 { input_vdex, output_vdex, kDisableCompactDex },
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100921 /* expect_success */ true,
922 /* use_fd */ true);
923 }
924 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
925 CheckResult(dex_location, odex_location);
926 ASSERT_TRUE(success_);
927 }
928
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800929 void RunUnquickenMultiDexCDex() {
930 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
931 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
932 std::string odex_location2 = GetOdexDir() + "/UnquickenMultiDex2.odex";
933 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
934 std::string vdex_location2 = GetOdexDir() + "/UnquickenMultiDex2.vdex";
935 Copy(GetTestDexFileName("MultiDex"), dex_location);
936
937 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
938 std::unique_ptr<File> vdex_file2(OS::CreateEmptyFile(vdex_location2.c_str()));
939 CHECK(vdex_file1 != nullptr) << vdex_location;
940 CHECK(vdex_file2 != nullptr) << vdex_location2;
941
942 // Quicken the dex file into a vdex file.
943 {
944 std::string input_vdex = "--input-vdex-fd=-1";
945 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
946 GenerateOdexForTest(dex_location,
947 odex_location,
948 CompilerFilter::kQuicken,
949 { input_vdex, output_vdex, "--compact-dex-level=fast"},
950 /* expect_success */ true,
951 /* use_fd */ true);
952 EXPECT_GT(vdex_file1->GetLength(), 0u);
953 }
954
955 // Unquicken by running the verify compiler filter on the vdex file.
956 {
957 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
958 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2->Fd());
959 GenerateOdexForTest(dex_location,
960 odex_location2,
961 CompilerFilter::kVerify,
962 { input_vdex, output_vdex, "--compact-dex-level=none"},
963 /* expect_success */ true,
964 /* use_fd */ true);
965 }
966 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
967 ASSERT_EQ(vdex_file2->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
968 CheckResult(dex_location, odex_location2);
969 ASSERT_TRUE(success_);
970 }
971
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100972 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
973 std::string error_msg;
974 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
975 odex_location.c_str(),
976 nullptr,
977 nullptr,
978 false,
979 /*low_4gb*/false,
980 dex_location.c_str(),
981 &error_msg));
982 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
983 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
984
985 // Iterate over the dex files and ensure there is no quickened instruction.
986 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
987 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
988 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
989 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
990 const uint8_t* class_data = dex_file->GetClassData(class_def);
991 if (class_data != nullptr) {
992 for (ClassDataItemIterator class_it(*dex_file, class_data);
993 class_it.HasNext();
994 class_it.Next()) {
995 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800996 for (const DexInstructionPcPair& inst :
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800997 CodeItemInstructionAccessor(*dex_file, class_it.GetMethodCodeItem())) {
Mathieu Chartier02129102017-12-22 11:04:01 -0800998 ASSERT_FALSE(inst->IsQuickened()) << output_;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100999 }
1000 }
1001 }
1002 }
1003 }
1004 }
1005 }
1006};
1007
1008TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
1009 RunUnquickenMultiDex();
1010}
1011
Mathieu Chartierd27923c2018-02-08 21:00:03 -08001012TEST_F(Dex2oatUnquickenTest, UnquickenMultiDexCDex) {
1013 RunUnquickenMultiDexCDex();
1014}
1015
Andreas Gampe2e8a2562017-01-18 20:39:02 -08001016class Dex2oatWatchdogTest : public Dex2oatTest {
1017 protected:
1018 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
1019 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1020 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1021
1022 Copy(GetTestDexFileName(), dex_location);
1023
1024 std::vector<std::string> copy(extra_args);
1025
1026 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
1027 copy.push_back("--swap-file=" + swap_location);
Andreas Gampe22fa3762017-10-23 20:58:12 -07001028 copy.push_back("-j512"); // Excessive idle threads just slow down dex2oat.
Andreas Gampe2e8a2562017-01-18 20:39:02 -08001029 GenerateOdexForTest(dex_location,
1030 odex_location,
1031 CompilerFilter::kSpeed,
1032 copy,
1033 expect_success);
1034 }
1035
1036 std::string GetTestDexFileName() {
1037 return GetDexSrc1();
1038 }
1039};
1040
1041TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
1042 // Check with default.
1043 RunTest(true);
1044
1045 // Check with ten minutes.
1046 RunTest(true, { "--watchdog-timeout=600000" });
1047}
1048
1049TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Roland Levillain68db2252017-08-14 12:48:47 +01001050 TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624
Andreas Gampe80ddf272018-01-11 09:41:00 -08001051
1052 // The watchdog is independent of dex2oat and will not delete intermediates. It is possible
1053 // that the compilation succeeds and the file is completely written by the time the watchdog
1054 // kills dex2oat (but the dex2oat threads must have been scheduled pretty badly).
1055 test_accepts_odex_file_on_failure = true;
1056
Andreas Gampe2e8a2562017-01-18 20:39:02 -08001057 // Check with ten milliseconds.
1058 RunTest(false, { "--watchdog-timeout=10" });
1059}
1060
Andreas Gampef7882972017-03-20 16:35:24 -07001061class Dex2oatReturnCodeTest : public Dex2oatTest {
1062 protected:
1063 int RunTest(const std::vector<std::string>& extra_args = {}) {
1064 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1065 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1066
1067 Copy(GetTestDexFileName(), dex_location);
1068
1069 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001070 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -07001071 odex_location,
1072 CompilerFilter::kSpeed,
1073 &error_msg,
1074 extra_args);
1075 }
1076
1077 std::string GetTestDexFileName() {
1078 return GetDexSrc1();
1079 }
1080};
1081
1082TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -07001083 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -07001084 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1085 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1086}
1087
Calin Juravle1ce70852017-06-28 10:59:03 -07001088class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1089 protected:
1090 void RunTest(const char* class_loader_context,
1091 const char* expected_classpath_key,
1092 bool expected_success,
1093 bool use_second_source = false) {
1094 std::string dex_location = GetUsedDexLocation();
1095 std::string odex_location = GetUsedOatLocation();
1096
1097 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1098
1099 std::string error_msg;
1100 std::vector<std::string> extra_args;
1101 if (class_loader_context != nullptr) {
1102 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1103 }
1104 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1105 ASSERT_TRUE(expected_classpath_key != nullptr);
1106 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1107 ASSERT_TRUE(classpath != nullptr);
1108 ASSERT_STREQ(expected_classpath_key, classpath);
1109 };
1110
1111 GenerateOdexForTest(dex_location,
1112 odex_location,
1113 CompilerFilter::kQuicken,
1114 extra_args,
1115 expected_success,
1116 /*use_fd*/ false,
1117 check_oat);
1118 }
1119
1120 std::string GetUsedDexLocation() {
1121 return GetScratchDir() + "/Context.jar";
1122 }
1123
1124 std::string GetUsedOatLocation() {
1125 return GetOdexDir() + "/Context.odex";
1126 }
1127
Calin Juravle7b0648a2017-07-07 18:40:50 -07001128 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001129};
1130
1131TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1132 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1133}
1134
1135TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1136 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1137}
1138
1139TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1140 RunTest(OatFile::kSpecialSharedLibrary,
1141 OatFile::kSpecialSharedLibrary,
1142 /*expected_success*/ true);
1143}
1144
1145TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1146 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1147 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1148}
1149
1150TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1151 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001152
1153 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001154 std::string expected_classpath_key = "PCL[" +
1155 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001156 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1157}
1158
1159TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1160 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1161 Copy(GetStrippedDexSrc1(), stripped_classpath);
1162
1163 std::string context = "PCL[" + stripped_classpath + "]";
1164 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001165 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001166}
1167
1168TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1169 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1170 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1171
1172 Copy(GetDexSrc1(), stripped_classpath);
1173
1174 GenerateOdexForTest(stripped_classpath,
1175 odex_for_classpath,
1176 CompilerFilter::kQuicken,
1177 {},
1178 true);
1179
1180 // Strip the dex file
1181 Copy(GetStrippedDexSrc1(), stripped_classpath);
1182
1183 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001184 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001185 {
1186 // Open the oat file to get the expected classpath.
Nicolas Geoffray29742602017-12-14 10:09:03 +00001187 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false, false);
Calin Juravle1ce70852017-06-28 10:59:03 -07001188 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1189 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1190 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001191 expected_classpath_key = "PCL[";
1192 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1193 if (i > 0) {
1194 expected_classpath_key + ":";
1195 }
1196 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1197 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1198 }
1199 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001200 }
1201
1202 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001203 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001204 /*expected_success*/ true,
1205 /*use_second_source*/ true);
1206}
1207
1208TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1209 std::string context = "PCL[does_not_exists.dex]";
1210 // Expect an empty context because stripped dex files cannot be open.
1211 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1212}
1213
Calin Juravlec79470d2017-07-12 17:37:42 -07001214TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1215 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1216 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1217
1218 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1219 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1220 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1221 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1222
1223 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1224}
1225
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001226class Dex2oatDeterminism : public Dex2oatTest {};
1227
1228TEST_F(Dex2oatDeterminism, UnloadCompile) {
1229 if (!kUseReadBarrier &&
1230 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1231 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1232 LOG(INFO) << "Test requires determinism support.";
1233 return;
1234 }
1235 Runtime* const runtime = Runtime::Current();
1236 std::string out_dir = GetScratchDir();
1237 const std::string base_oat_name = out_dir + "/base.oat";
1238 const std::string base_vdex_name = out_dir + "/base.vdex";
1239 const std::string unload_oat_name = out_dir + "/unload.oat";
1240 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1241 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1242 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1243 const std::string app_image_name = out_dir + "/unload.art";
1244 std::string error_msg;
1245 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1246 ASSERT_GT(spaces.size(), 0u);
1247 const std::string image_location = spaces[0]->GetImageLocation();
1248 // Without passing in an app image, it will unload in between compilations.
1249 const int res = GenerateOdexForTestWithStatus(
1250 GetLibCoreDexFileNames(),
1251 base_oat_name,
1252 CompilerFilter::Filter::kQuicken,
1253 &error_msg,
1254 {"--force-determinism", "--avoid-storing-invocation"});
1255 EXPECT_EQ(res, 0);
1256 Copy(base_oat_name, unload_oat_name);
1257 Copy(base_vdex_name, unload_vdex_name);
1258 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1259 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1260 ASSERT_TRUE(unload_oat != nullptr);
1261 ASSERT_TRUE(unload_vdex != nullptr);
1262 EXPECT_GT(unload_oat->GetLength(), 0u);
1263 EXPECT_GT(unload_vdex->GetLength(), 0u);
1264 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1265 // the same.
1266 const int res2 = GenerateOdexForTestWithStatus(
1267 GetLibCoreDexFileNames(),
1268 base_oat_name,
1269 CompilerFilter::Filter::kQuicken,
1270 &error_msg,
1271 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1272 EXPECT_EQ(res2, 0);
1273 Copy(base_oat_name, no_unload_oat_name);
1274 Copy(base_vdex_name, no_unload_vdex_name);
1275 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1276 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1277 ASSERT_TRUE(no_unload_oat != nullptr);
1278 ASSERT_TRUE(no_unload_vdex != nullptr);
1279 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1280 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1281 // Verify that both of the files are the same (odex and vdex).
1282 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1283 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1284 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1285 << unload_oat_name << " " << no_unload_oat_name;
1286 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1287 << unload_vdex_name << " " << no_unload_vdex_name;
1288 // App image file.
1289 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1290 ASSERT_TRUE(app_image_file != nullptr);
1291 EXPECT_GT(app_image_file->GetLength(), 0u);
1292}
1293
Mathieu Chartier120aa282017-08-05 16:03:03 -07001294// Test that dexlayout section info is correctly written to the oat file for profile based
1295// compilation.
1296TEST_F(Dex2oatTest, LayoutSections) {
1297 using Hotness = ProfileCompilationInfo::MethodHotness;
1298 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1299 ScratchFile profile_file;
1300 // We can only layout method indices with code items, figure out which ones have this property
1301 // first.
1302 std::vector<uint16_t> methods;
1303 {
1304 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1305 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
1306 const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx);
1307 ClassDataItemIterator it(*dex, dex->GetClassData(*class_def));
1308 it.SkipAllFields();
1309 std::set<size_t> code_item_offsets;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001310 for (; it.HasNextMethod(); it.Next()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001311 const uint16_t method_idx = it.GetMemberIndex();
1312 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1313 if (code_item_offsets.insert(code_item_offset).second) {
1314 // Unique code item, add the method index.
1315 methods.push_back(method_idx);
1316 }
1317 }
1318 DCHECK(!it.HasNext());
1319 }
1320 ASSERT_GE(methods.size(), 8u);
1321 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1322 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1323 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1324 // Here, we build the profile from the method lists.
1325 ProfileCompilationInfo info;
1326 info.AddMethodsForDex(
1327 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1328 dex.get(),
1329 hot_methods.begin(),
1330 hot_methods.end());
1331 info.AddMethodsForDex(
1332 Hotness::kFlagStartup,
1333 dex.get(),
1334 startup_methods.begin(),
1335 startup_methods.end());
1336 info.AddMethodsForDex(
1337 Hotness::kFlagPostStartup,
1338 dex.get(),
1339 post_methods.begin(),
1340 post_methods.end());
1341 for (uint16_t id : hot_methods) {
1342 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1343 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1344 }
1345 for (uint16_t id : startup_methods) {
1346 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1347 }
1348 for (uint16_t id : post_methods) {
1349 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1350 }
1351 // Save the profile since we want to use it with dex2oat to produce an oat file.
1352 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1353 // Generate a profile based odex.
1354 const std::string dir = GetScratchDir();
1355 const std::string oat_filename = dir + "/base.oat";
1356 const std::string vdex_filename = dir + "/base.vdex";
1357 std::string error_msg;
1358 const int res = GenerateOdexForTestWithStatus(
1359 {dex->GetLocation()},
1360 oat_filename,
1361 CompilerFilter::Filter::kQuicken,
1362 &error_msg,
1363 {"--profile-file=" + profile_file.GetFilename()});
1364 EXPECT_EQ(res, 0);
1365
1366 // Open our generated oat file.
1367 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1368 oat_filename.c_str(),
1369 nullptr,
1370 nullptr,
1371 false,
1372 /*low_4gb*/false,
1373 dex->GetLocation().c_str(),
1374 &error_msg));
1375 ASSERT_TRUE(odex_file != nullptr);
1376 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1377 ASSERT_EQ(oat_dex_files.size(), 1u);
1378 // Check that the code sections match what we expect.
1379 for (const OatDexFile* oat_dex : oat_dex_files) {
1380 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1381 // Testing of logging the sections.
1382 ASSERT_TRUE(sections != nullptr);
1383 LOG(INFO) << *sections;
1384
1385 // Load the sections into temporary variables for convenience.
1386 const DexLayoutSection& code_section =
1387 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1388 const DexLayoutSection::Subsection& section_hot_code =
1389 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1390 const DexLayoutSection::Subsection& section_sometimes_used =
1391 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1392 const DexLayoutSection::Subsection& section_startup_only =
1393 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1394 const DexLayoutSection::Subsection& section_unused =
1395 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1396
1397 // All the sections should be non-empty.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001398 EXPECT_GT(section_hot_code.Size(), 0u);
1399 EXPECT_GT(section_sometimes_used.Size(), 0u);
1400 EXPECT_GT(section_startup_only.Size(), 0u);
1401 EXPECT_GT(section_unused.Size(), 0u);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001402
1403 // Open the dex file since we need to peek at the code items to verify the layout matches what
1404 // we expect.
1405 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1406 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1407 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1408 ASSERT_TRUE(type_id != nullptr);
1409 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1410 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1411 ASSERT_TRUE(class_def != nullptr);
1412
1413 // Count how many code items are for each category, there should be at least one per category.
1414 size_t hot_count = 0;
1415 size_t post_startup_count = 0;
1416 size_t startup_count = 0;
1417 size_t unused_count = 0;
1418 // Visit all of the methdos of the main class and cross reference the method indices to their
1419 // corresponding code item offsets to verify the layout.
1420 ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def));
1421 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001422 for (; it.HasNextMethod(); it.Next()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001423 const size_t method_idx = it.GetMemberIndex();
1424 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1425 const bool is_hot = ContainsElement(hot_methods, method_idx);
1426 const bool is_startup = ContainsElement(startup_methods, method_idx);
1427 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1428 if (is_hot) {
1429 // Hot is highest precedence, check that the hot methods are in the hot section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001430 EXPECT_TRUE(section_hot_code.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001431 ++hot_count;
1432 } else if (is_post_startup) {
1433 // Post startup is sometimes used section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001434 EXPECT_TRUE(section_sometimes_used.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001435 ++post_startup_count;
1436 } else if (is_startup) {
1437 // Startup at this point means not hot or post startup, these must be startup only then.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001438 EXPECT_TRUE(section_startup_only.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001439 ++startup_count;
1440 } else {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001441 if (section_unused.Contains(code_item_offset)) {
Alan Leung9595fd32017-10-17 17:08:19 -07001442 // If no flags are set, the method should be unused ...
1443 ++unused_count;
1444 } else {
1445 // or this method is part of the last code item and the end is 4 byte aligned.
1446 ClassDataItemIterator it2(*dex_file, dex_file->GetClassData(*class_def));
1447 it2.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001448 for (; it2.HasNextMethod(); it2.Next()) {
Alan Leung9595fd32017-10-17 17:08:19 -07001449 EXPECT_LE(it2.GetMethodCodeItemOffset(), code_item_offset);
1450 }
1451 uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx);
1452 EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u);
1453 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001454 }
1455 }
1456 DCHECK(!it.HasNext());
1457 EXPECT_GT(hot_count, 0u);
1458 EXPECT_GT(post_startup_count, 0u);
1459 EXPECT_GT(startup_count, 0u);
1460 EXPECT_GT(unused_count, 0u);
1461 }
1462}
1463
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001464// Test that generating compact dex works.
1465TEST_F(Dex2oatTest, GenerateCompactDex) {
1466 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1467 // Generate a compact dex based odex.
1468 const std::string dir = GetScratchDir();
1469 const std::string oat_filename = dir + "/base.oat";
1470 const std::string vdex_filename = dir + "/base.vdex";
1471 std::string error_msg;
1472 const int res = GenerateOdexForTestWithStatus(
1473 {dex->GetLocation()},
1474 oat_filename,
1475 CompilerFilter::Filter::kQuicken,
1476 &error_msg,
1477 {"--compact-dex-level=fast"});
1478 EXPECT_EQ(res, 0);
1479 // Open our generated oat file.
1480 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1481 oat_filename.c_str(),
1482 nullptr,
1483 nullptr,
1484 false,
1485 /*low_4gb*/false,
1486 dex->GetLocation().c_str(),
1487 &error_msg));
1488 ASSERT_TRUE(odex_file != nullptr);
1489 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1490 ASSERT_EQ(oat_dex_files.size(), 1u);
1491 // Check that each dex is a compact dex.
1492 for (const OatDexFile* oat_dex : oat_dex_files) {
1493 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1494 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1495 ASSERT_TRUE(dex_file->IsCompactDexFile());
1496 }
1497}
1498
Andreas Gampef39208f2017-10-19 15:06:59 -07001499class Dex2oatVerifierAbort : public Dex2oatTest {};
1500
1501TEST_F(Dex2oatVerifierAbort, HardFail) {
1502 // Use VerifierDeps as it has hard-failing classes.
1503 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps"));
1504 std::string out_dir = GetScratchDir();
1505 const std::string base_oat_name = out_dir + "/base.oat";
1506 std::string error_msg;
1507 const int res_fail = GenerateOdexForTestWithStatus(
1508 {dex->GetLocation()},
1509 base_oat_name,
1510 CompilerFilter::Filter::kQuicken,
1511 &error_msg,
1512 {"--abort-on-hard-verifier-error"});
1513 EXPECT_NE(0, res_fail);
1514
1515 const int res_no_fail = GenerateOdexForTestWithStatus(
1516 {dex->GetLocation()},
1517 base_oat_name,
1518 CompilerFilter::Filter::kQuicken,
1519 &error_msg,
1520 {"--no-abort-on-hard-verifier-error"});
1521 EXPECT_EQ(0, res_no_fail);
1522}
1523
1524TEST_F(Dex2oatVerifierAbort, SoftFail) {
1525 // Use VerifierDepsMulti as it has hard-failing classes.
1526 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDepsMulti"));
1527 std::string out_dir = GetScratchDir();
1528 const std::string base_oat_name = out_dir + "/base.oat";
1529 std::string error_msg;
1530 const int res_fail = GenerateOdexForTestWithStatus(
1531 {dex->GetLocation()},
1532 base_oat_name,
1533 CompilerFilter::Filter::kQuicken,
1534 &error_msg,
1535 {"--abort-on-soft-verifier-error"});
1536 EXPECT_NE(0, res_fail);
1537
1538 const int res_no_fail = GenerateOdexForTestWithStatus(
1539 {dex->GetLocation()},
1540 base_oat_name,
1541 CompilerFilter::Filter::kQuicken,
1542 &error_msg,
1543 {"--no-abort-on-soft-verifier-error"});
1544 EXPECT_EQ(0, res_no_fail);
1545}
1546
Andreas Gampecac31ad2017-11-06 20:01:17 -08001547class Dex2oatDedupeCode : public Dex2oatTest {};
1548
1549TEST_F(Dex2oatDedupeCode, DedupeTest) {
1550 // Use MyClassNatives. It has lots of native methods that will produce deduplicate-able code.
1551 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MyClassNatives"));
1552 std::string out_dir = GetScratchDir();
1553 const std::string base_oat_name = out_dir + "/base.oat";
1554 size_t no_dedupe_size = 0;
1555 GenerateOdexForTest(dex->GetLocation(),
1556 base_oat_name,
1557 CompilerFilter::Filter::kSpeed,
1558 { "--deduplicate-code=false" },
1559 true, // expect_success
1560 false, // use_fd
1561 [&no_dedupe_size](const OatFile& o) {
1562 no_dedupe_size = o.Size();
1563 });
1564
1565 size_t dedupe_size = 0;
1566 GenerateOdexForTest(dex->GetLocation(),
1567 base_oat_name,
1568 CompilerFilter::Filter::kSpeed,
1569 { "--deduplicate-code=true" },
1570 true, // expect_success
1571 false, // use_fd
1572 [&dedupe_size](const OatFile& o) {
1573 dedupe_size = o.Size();
1574 });
1575
1576 EXPECT_LT(dedupe_size, no_dedupe_size);
1577}
1578
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001579TEST_F(Dex2oatTest, UncompressedTest) {
1580 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MainUncompressed"));
1581 std::string out_dir = GetScratchDir();
1582 const std::string base_oat_name = out_dir + "/base.oat";
1583 GenerateOdexForTest(dex->GetLocation(),
1584 base_oat_name,
1585 CompilerFilter::Filter::kQuicken,
1586 { },
1587 true, // expect_success
1588 false, // use_fd
1589 [](const OatFile& o) {
1590 CHECK(!o.ContainsDexCode());
1591 });
1592}
1593
Mathieu Chartier700a9852018-02-06 18:27:38 -08001594TEST_F(Dex2oatTest, EmptyUncompressedDexTest) {
1595 std::string out_dir = GetScratchDir();
1596 const std::string base_oat_name = out_dir + "/base.oat";
1597 std::string error_msg;
1598 int status = GenerateOdexForTestWithStatus(
1599 { GetTestDexFileName("MainEmptyUncompressed") },
1600 base_oat_name,
1601 CompilerFilter::Filter::kQuicken,
1602 &error_msg,
1603 { },
1604 /*use_fd*/ false);
1605 // Expect to fail with code 1 and not SIGSEGV or SIGABRT.
1606 ASSERT_TRUE(WIFEXITED(status));
1607 ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg;
1608}
1609
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001610// Dex file that has duplicate methods have different code items and debug info.
1611static const char kDuplicateMethodInputDex[] =
1612 "ZGV4CjAzOQDEy8VPdj4qHpgPYFWtLCtOykfFP4kB8tGYDAAAcAAAAHhWNBIAAAAAAAAAANALAABI"
1613 "AAAAcAAAAA4AAACQAQAABQAAAMgBAAANAAAABAIAABkAAABsAgAABAAAADQDAADgCAAAuAMAADgI"
1614 "AABCCAAASggAAE8IAABcCAAAaggAAHkIAACICAAAlggAAKQIAACyCAAAwAgAAM4IAADcCAAA6ggA"
1615 "APgIAAD7CAAA/wgAABcJAAAuCQAARQkAAFQJAAB4CQAAmAkAALsJAADSCQAA5gkAAPoJAAAVCgAA"
1616 "KQoAADsKAABCCgAASgoAAFIKAABbCgAAZAoAAGwKAAB0CgAAfAoAAIQKAACMCgAAlAoAAJwKAACk"
1617 "CgAArQoAALcKAADACgAAwwoAAMcKAADcCgAA6QoAAPEKAAD3CgAA/QoAAAMLAAAJCwAAEAsAABcL"
1618 "AAAdCwAAIwsAACkLAAAvCwAANQsAADsLAABBCwAARwsAAE0LAABSCwAAWwsAAF4LAABoCwAAbwsA"
1619 "ABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAC4AAAAwAAAA"
1620 "DwAAAAkAAAAAAAAAEAAAAAoAAACoBwAALgAAAAwAAAAAAAAALwAAAAwAAACoBwAALwAAAAwAAACw"
1621 "BwAAAgAJADUAAAACAAkANgAAAAIACQA3AAAAAgAJADgAAAACAAkAOQAAAAIACQA6AAAAAgAJADsA"
1622 "AAACAAkAPAAAAAIACQA9AAAAAgAJAD4AAAACAAkAPwAAAAIACQBAAAAACwAHAEIAAAAAAAIAAQAA"
1623 "AAAAAwAeAAAAAQACAAEAAAABAAMAHgAAAAIAAgAAAAAAAgACAAEAAAADAAIAAQAAAAMAAgAfAAAA"
1624 "AwACACAAAAADAAIAIQAAAAMAAgAiAAAAAwACACMAAAADAAIAJAAAAAMAAgAlAAAAAwACACYAAAAD"
1625 "AAIAJwAAAAMAAgAoAAAAAwACACkAAAADAAIAKgAAAAMABAA0AAAABwADAEMAAAAIAAIAAQAAAAoA"
1626 "AgABAAAACgABADIAAAAKAAAARQAAAAAAAAAAAAAACAAAAAAAAAAdAAAAaAcAALYHAAAAAAAAAQAA"
1627 "AAAAAAAIAAAAAAAAAB0AAAB4BwAAxAcAAAAAAAACAAAAAAAAAAgAAAAAAAAAHQAAAIgHAADSBwAA"
1628 "AAAAAAMAAAAAAAAACAAAAAAAAAAdAAAAmAcAAPoHAAAAAAAAAAAAAAEAAAAAAAAArAYAADEAAAAa"
1629 "AAMAaQAAABoABABpAAEAGgAHAGkABAAaAAgAaQAFABoACQBpAAYAGgAKAGkABwAaAAsAaQAIABoA"
1630 "DABpAAkAGgANAGkACgAaAA4AaQALABoABQBpAAIAGgAGAGkAAwAOAAAAAQABAAEAAACSBgAABAAA"
1631 "AHAQFQAAAA4ABAABAAIAAACWBgAAFwAAAGIADAAiAQoAcBAWAAEAGgICAG4gFwAhAG4gFwAxAG4Q"
1632 "GAABAAwBbiAUABAADgAAAAEAAQABAAAAngYAAAQAAABwEBUAAAAOAAIAAQACAAAAogYAAAYAAABi"
1633 "AAwAbiAUABAADgABAAEAAQAAAKgGAAAEAAAAcBAVAAAADgABAAEAAQAAALsGAAAEAAAAcBAVAAAA"
1634 "DgABAAAAAQAAAL8GAAAGAAAAYgAAAHEQAwAAAA4AAQAAAAEAAADEBgAABgAAAGIAAQBxEAMAAAAO"
1635 "AAEAAAABAAAA8QYAAAYAAABiAAIAcRABAAAADgABAAAAAQAAAPYGAAAGAAAAYgADAHEQAwAAAA4A"
1636 "AQAAAAEAAADJBgAABgAAAGIABABxEAMAAAAOAAEAAAABAAAAzgYAAAYAAABiAAEAcRADAAAADgAB"
1637 "AAAAAQAAANMGAAAGAAAAYgAGAHEQAwAAAA4AAQAAAAEAAADYBgAABgAAAGIABwBxEAMAAAAOAAEA"
1638 "AAABAAAA3QYAAAYAAABiAAgAcRABAAAADgABAAAAAQAAAOIGAAAGAAAAYgAJAHEQAwAAAA4AAQAA"
1639 "AAEAAADnBgAABgAAAGIACgBxEAMAAAAOAAEAAAABAAAA7AYAAAYAAABiAAsAcRABAAAADgABAAEA"
1640 "AAAAAPsGAAAlAAAAcQAHAAAAcQAIAAAAcQALAAAAcQAMAAAAcQANAAAAcQAOAAAAcQAPAAAAcQAQ"
1641 "AAAAcQARAAAAcQASAAAAcQAJAAAAcQAKAAAADgAnAA4AKQFFDgEWDwAhAA4AIwFFDloAEgAOABMA"
1642 "DktLS0tLS0tLS0tLABEADgAuAA5aADIADloANgAOWgA6AA5aAD4ADloAQgAOWgBGAA5aAEoADloA"
1643 "TgAOWgBSAA5aAFYADloAWgAOWgBeATQOPDw8PDw8PDw8PDw8AAIEAUYYAwIFAjEECEEXLAIFAjEE"
1644 "CEEXKwIFAjEECEEXLQIGAUYcAxgAGAEYAgAAAAIAAAAMBwAAEgcAAAIAAAAMBwAAGwcAAAIAAAAM"
1645 "BwAAJAcAAAEAAAAtBwAAPAcAAAAAAAAAAAAAAAAAAEgHAAAAAAAAAAAAAAAAAABUBwAAAAAAAAAA"
1646 "AAAAAAAAYAcAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAQAAAA0AAAACAACAgASsCAEIxAgAAAIAAoCA"
1647 "BIQJAQicCQwAAgAACQEJAQkBCQEJAQkBCQEJAQkBCQEJAQkEiIAEuAcBgIAEuAkAAA4ABoCABNAJ"
1648 "AQnoCQAJhAoACaAKAAm8CgAJ2AoACfQKAAmQCwAJrAsACcgLAAnkCwAJgAwACZwMAAm4DAg8Y2xp"
1649 "bml0PgAGPGluaXQ+AANBQUEAC0hlbGxvIFdvcmxkAAxIZWxsbyBXb3JsZDEADUhlbGxvIFdvcmxk"
1650 "MTAADUhlbGxvIFdvcmxkMTEADEhlbGxvIFdvcmxkMgAMSGVsbG8gV29ybGQzAAxIZWxsbyBXb3Js"
1651 "ZDQADEhlbGxvIFdvcmxkNQAMSGVsbG8gV29ybGQ2AAxIZWxsbyBXb3JsZDcADEhlbGxvIFdvcmxk"
1652 "OAAMSGVsbG8gV29ybGQ5AAFMAAJMTAAWTE1hbnlNZXRob2RzJFByaW50ZXIyOwAVTE1hbnlNZXRo"
1653 "b2RzJFByaW50ZXI7ABVMTWFueU1ldGhvZHMkU3RyaW5nczsADUxNYW55TWV0aG9kczsAIkxkYWx2"
1654 "aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5lckNs"
1655 "YXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABVMamF2YS9pby9QcmludFN0"
1656 "cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABlMamF2YS9sYW5n"
1657 "L1N0cmluZ0J1aWxkZXI7ABJMamF2YS9sYW5nL1N5c3RlbTsAEE1hbnlNZXRob2RzLmphdmEABVBy"
1658 "aW50AAZQcmludDAABlByaW50MQAHUHJpbnQxMAAHUHJpbnQxMQAGUHJpbnQyAAZQcmludDMABlBy"
1659 "aW50NAAGUHJpbnQ1AAZQcmludDYABlByaW50NwAGUHJpbnQ4AAZQcmludDkAB1ByaW50ZXIACFBy"
1660 "aW50ZXIyAAdTdHJpbmdzAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwALYWNjZXNzRmxhZ3MA"
1661 "BmFwcGVuZAAEYXJncwAEbWFpbgAEbXNnMAAEbXNnMQAFbXNnMTAABW1zZzExAARtc2cyAARtc2cz"
1662 "AARtc2c0AARtc2c1AARtc2c2AARtc2c3AARtc2c4AARtc2c5AARuYW1lAANvdXQAB3ByaW50bG4A"
1663 "AXMACHRvU3RyaW5nAAV2YWx1ZQBffn5EOHsibWluLWFwaSI6MTAwMDAsInNoYS0xIjoiZmViODZj"
1664 "MDA2ZWZhY2YxZDc5ODRiODVlMTc5MGZlZjdhNzY3YWViYyIsInZlcnNpb24iOiJ2MS4xLjUtZGV2"
1665 "In0AEAAAAAAAAAABAAAAAAAAAAEAAABIAAAAcAAAAAIAAAAOAAAAkAEAAAMAAAAFAAAAyAEAAAQA"
1666 "AAANAAAABAIAAAUAAAAZAAAAbAIAAAYAAAAEAAAANAMAAAEgAAAUAAAAuAMAAAMgAAAUAAAAkgYA"
1667 "AAQgAAAFAAAADAcAAAMQAAAEAAAAOQcAAAYgAAAEAAAAaAcAAAEQAAACAAAAqAcAAAAgAAAEAAAA"
1668 "tgcAAAIgAABIAAAAOAgAAAAQAAABAAAA0AsAAAAAAAA=";
1669
1670static void WriteBase64ToFile(const char* base64, File* file) {
1671 // Decode base64.
1672 CHECK(base64 != nullptr);
1673 size_t length;
1674 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
1675 CHECK(bytes != nullptr);
1676 if (!file->WriteFully(bytes.get(), length)) {
1677 PLOG(FATAL) << "Failed to write base64 as file";
1678 }
1679}
1680
1681TEST_F(Dex2oatTest, CompactDexGenerationFailure) {
1682 ScratchFile temp_dex;
1683 WriteBase64ToFile(kDuplicateMethodInputDex, temp_dex.GetFile());
1684 std::string out_dir = GetScratchDir();
1685 const std::string oat_filename = out_dir + "/base.oat";
1686 // The dex won't pass the method verifier, only use the verify filter.
1687 GenerateOdexForTest(temp_dex.GetFilename(),
1688 oat_filename,
1689 CompilerFilter::Filter::kVerify,
1690 { },
1691 true, // expect_success
1692 false, // use_fd
1693 [](const OatFile& o) {
1694 CHECK(o.ContainsDexCode());
1695 });
1696 // Open our generated oat file.
1697 std::string error_msg;
1698 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1699 oat_filename.c_str(),
1700 nullptr,
1701 nullptr,
1702 false,
1703 /*low_4gb*/false,
1704 temp_dex.GetFilename().c_str(),
1705 &error_msg));
1706 ASSERT_TRUE(odex_file != nullptr);
1707 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1708 ASSERT_EQ(oat_dex_files.size(), 1u);
1709 // The dexes should have failed to convert to compact dex.
1710 for (const OatDexFile* oat_dex : oat_dex_files) {
1711 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1712 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1713 ASSERT_TRUE(!dex_file->IsCompactDexFile());
1714 }
1715}
1716
Andreas Gampe25419b52018-02-08 21:30:26 -08001717TEST_F(Dex2oatTest, StderrLoggerOutput) {
1718 std::string dex_location = GetScratchDir() + "/Dex2OatStderrLoggerTest.jar";
1719 std::string odex_location = GetOdexDir() + "/Dex2OatStderrLoggerTest.odex";
1720
1721 // Test file doesn't matter.
1722 Copy(GetDexSrc1(), dex_location);
1723
1724 GenerateOdexForTest(dex_location,
1725 odex_location,
1726 CompilerFilter::kQuicken,
1727 { "--runtime-arg", "-Xuse-stderr-logger" },
1728 true);
1729 // Look for some random part of dex2oat logging. With the stderr logger this should be captured,
1730 // even on device.
1731 EXPECT_NE(std::string::npos, output_.find("dex2oat took"));
1732}
1733
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001734TEST_F(Dex2oatTest, VerifyCompilationReason) {
1735 std::string dex_location = GetScratchDir() + "/Dex2OatCompilationReason.jar";
1736 std::string odex_location = GetOdexDir() + "/Dex2OatCompilationReason.odex";
1737
1738 // Test file doesn't matter.
1739 Copy(GetDexSrc1(), dex_location);
1740
1741 GenerateOdexForTest(dex_location,
1742 odex_location,
1743 CompilerFilter::kVerify,
1744 { "--compilation-reason=install" },
1745 true);
1746 std::string error_msg;
1747 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
1748 odex_location.c_str(),
1749 nullptr,
1750 nullptr,
1751 false,
1752 /*low_4gb*/false,
1753 dex_location.c_str(),
1754 &error_msg));
1755 ASSERT_TRUE(odex_file != nullptr);
1756 ASSERT_STREQ("install", odex_file->GetCompilationReason());
1757}
1758
1759TEST_F(Dex2oatTest, VerifyNoCompilationReason) {
1760 std::string dex_location = GetScratchDir() + "/Dex2OatNoCompilationReason.jar";
1761 std::string odex_location = GetOdexDir() + "/Dex2OatNoCompilationReason.odex";
1762
1763 // Test file doesn't matter.
1764 Copy(GetDexSrc1(), dex_location);
1765
1766 GenerateOdexForTest(dex_location,
1767 odex_location,
1768 CompilerFilter::kVerify,
1769 {},
1770 true);
1771 std::string error_msg;
1772 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
1773 odex_location.c_str(),
1774 nullptr,
1775 nullptr,
1776 false,
1777 /*low_4gb*/false,
1778 dex_location.c_str(),
1779 &error_msg));
1780 ASSERT_TRUE(odex_file != nullptr);
1781 ASSERT_EQ(nullptr, odex_file->GetCompilationReason());
1782}
1783
Andreas Gampee1459ae2016-06-29 09:36:30 -07001784} // namespace art