blob: d0b0d49a39d4fed807788323327cbd2790a794b9 [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 Chartier02129102017-12-22 11:04:01 -0800799 { input_vdex, output_vdex, kDisableCompactDex },
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
929 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
930 std::string error_msg;
931 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
932 odex_location.c_str(),
933 nullptr,
934 nullptr,
935 false,
936 /*low_4gb*/false,
937 dex_location.c_str(),
938 &error_msg));
939 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
940 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
941
942 // Iterate over the dex files and ensure there is no quickened instruction.
943 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
944 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
945 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
946 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
947 const uint8_t* class_data = dex_file->GetClassData(class_def);
948 if (class_data != nullptr) {
949 for (ClassDataItemIterator class_it(*dex_file, class_data);
950 class_it.HasNext();
951 class_it.Next()) {
952 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
Mathieu Chartier0021feb2017-11-07 00:08:52 -0800953 for (const DexInstructionPcPair& inst :
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800954 CodeItemInstructionAccessor(*dex_file, class_it.GetMethodCodeItem())) {
Mathieu Chartier02129102017-12-22 11:04:01 -0800955 ASSERT_FALSE(inst->IsQuickened()) << output_;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100956 }
957 }
958 }
959 }
960 }
961 }
962 }
963};
964
965TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
966 RunUnquickenMultiDex();
967}
968
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800969class Dex2oatWatchdogTest : public Dex2oatTest {
970 protected:
971 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
972 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
973 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
974
975 Copy(GetTestDexFileName(), dex_location);
976
977 std::vector<std::string> copy(extra_args);
978
979 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
980 copy.push_back("--swap-file=" + swap_location);
Andreas Gampe22fa3762017-10-23 20:58:12 -0700981 copy.push_back("-j512"); // Excessive idle threads just slow down dex2oat.
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800982 GenerateOdexForTest(dex_location,
983 odex_location,
984 CompilerFilter::kSpeed,
985 copy,
986 expect_success);
987 }
988
989 std::string GetTestDexFileName() {
990 return GetDexSrc1();
991 }
992};
993
994TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
995 // Check with default.
996 RunTest(true);
997
998 // Check with ten minutes.
999 RunTest(true, { "--watchdog-timeout=600000" });
1000}
1001
1002TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Roland Levillain68db2252017-08-14 12:48:47 +01001003 TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND(); // b/63052624
Andreas Gampe80ddf272018-01-11 09:41:00 -08001004
1005 // The watchdog is independent of dex2oat and will not delete intermediates. It is possible
1006 // that the compilation succeeds and the file is completely written by the time the watchdog
1007 // kills dex2oat (but the dex2oat threads must have been scheduled pretty badly).
1008 test_accepts_odex_file_on_failure = true;
1009
Andreas Gampe2e8a2562017-01-18 20:39:02 -08001010 // Check with ten milliseconds.
1011 RunTest(false, { "--watchdog-timeout=10" });
1012}
1013
Andreas Gampef7882972017-03-20 16:35:24 -07001014class Dex2oatReturnCodeTest : public Dex2oatTest {
1015 protected:
1016 int RunTest(const std::vector<std::string>& extra_args = {}) {
1017 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1018 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1019
1020 Copy(GetTestDexFileName(), dex_location);
1021
1022 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001023 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -07001024 odex_location,
1025 CompilerFilter::kSpeed,
1026 &error_msg,
1027 extra_args);
1028 }
1029
1030 std::string GetTestDexFileName() {
1031 return GetDexSrc1();
1032 }
1033};
1034
1035TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -07001036 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -07001037 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1038 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1039}
1040
Calin Juravle1ce70852017-06-28 10:59:03 -07001041class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1042 protected:
1043 void RunTest(const char* class_loader_context,
1044 const char* expected_classpath_key,
1045 bool expected_success,
1046 bool use_second_source = false) {
1047 std::string dex_location = GetUsedDexLocation();
1048 std::string odex_location = GetUsedOatLocation();
1049
1050 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1051
1052 std::string error_msg;
1053 std::vector<std::string> extra_args;
1054 if (class_loader_context != nullptr) {
1055 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1056 }
1057 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1058 ASSERT_TRUE(expected_classpath_key != nullptr);
1059 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1060 ASSERT_TRUE(classpath != nullptr);
1061 ASSERT_STREQ(expected_classpath_key, classpath);
1062 };
1063
1064 GenerateOdexForTest(dex_location,
1065 odex_location,
1066 CompilerFilter::kQuicken,
1067 extra_args,
1068 expected_success,
1069 /*use_fd*/ false,
1070 check_oat);
1071 }
1072
1073 std::string GetUsedDexLocation() {
1074 return GetScratchDir() + "/Context.jar";
1075 }
1076
1077 std::string GetUsedOatLocation() {
1078 return GetOdexDir() + "/Context.odex";
1079 }
1080
Calin Juravle7b0648a2017-07-07 18:40:50 -07001081 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001082};
1083
1084TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1085 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1086}
1087
1088TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1089 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1090}
1091
1092TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1093 RunTest(OatFile::kSpecialSharedLibrary,
1094 OatFile::kSpecialSharedLibrary,
1095 /*expected_success*/ true);
1096}
1097
1098TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1099 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1100 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1101}
1102
1103TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1104 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001105
1106 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001107 std::string expected_classpath_key = "PCL[" +
1108 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001109 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1110}
1111
1112TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1113 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1114 Copy(GetStrippedDexSrc1(), stripped_classpath);
1115
1116 std::string context = "PCL[" + stripped_classpath + "]";
1117 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001118 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001119}
1120
1121TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1122 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1123 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1124
1125 Copy(GetDexSrc1(), stripped_classpath);
1126
1127 GenerateOdexForTest(stripped_classpath,
1128 odex_for_classpath,
1129 CompilerFilter::kQuicken,
1130 {},
1131 true);
1132
1133 // Strip the dex file
1134 Copy(GetStrippedDexSrc1(), stripped_classpath);
1135
1136 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001137 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001138 {
1139 // Open the oat file to get the expected classpath.
Nicolas Geoffray29742602017-12-14 10:09:03 +00001140 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false, false);
Calin Juravle1ce70852017-06-28 10:59:03 -07001141 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1142 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1143 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001144 expected_classpath_key = "PCL[";
1145 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1146 if (i > 0) {
1147 expected_classpath_key + ":";
1148 }
1149 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1150 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1151 }
1152 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001153 }
1154
1155 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001156 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001157 /*expected_success*/ true,
1158 /*use_second_source*/ true);
1159}
1160
1161TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1162 std::string context = "PCL[does_not_exists.dex]";
1163 // Expect an empty context because stripped dex files cannot be open.
1164 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1165}
1166
Calin Juravlec79470d2017-07-12 17:37:42 -07001167TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1168 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1169 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1170
1171 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1172 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1173 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1174 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1175
1176 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1177}
1178
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001179class Dex2oatDeterminism : public Dex2oatTest {};
1180
1181TEST_F(Dex2oatDeterminism, UnloadCompile) {
1182 if (!kUseReadBarrier &&
1183 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1184 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1185 LOG(INFO) << "Test requires determinism support.";
1186 return;
1187 }
1188 Runtime* const runtime = Runtime::Current();
1189 std::string out_dir = GetScratchDir();
1190 const std::string base_oat_name = out_dir + "/base.oat";
1191 const std::string base_vdex_name = out_dir + "/base.vdex";
1192 const std::string unload_oat_name = out_dir + "/unload.oat";
1193 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1194 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1195 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1196 const std::string app_image_name = out_dir + "/unload.art";
1197 std::string error_msg;
1198 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1199 ASSERT_GT(spaces.size(), 0u);
1200 const std::string image_location = spaces[0]->GetImageLocation();
1201 // Without passing in an app image, it will unload in between compilations.
1202 const int res = GenerateOdexForTestWithStatus(
1203 GetLibCoreDexFileNames(),
1204 base_oat_name,
1205 CompilerFilter::Filter::kQuicken,
1206 &error_msg,
1207 {"--force-determinism", "--avoid-storing-invocation"});
1208 EXPECT_EQ(res, 0);
1209 Copy(base_oat_name, unload_oat_name);
1210 Copy(base_vdex_name, unload_vdex_name);
1211 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1212 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1213 ASSERT_TRUE(unload_oat != nullptr);
1214 ASSERT_TRUE(unload_vdex != nullptr);
1215 EXPECT_GT(unload_oat->GetLength(), 0u);
1216 EXPECT_GT(unload_vdex->GetLength(), 0u);
1217 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1218 // the same.
1219 const int res2 = GenerateOdexForTestWithStatus(
1220 GetLibCoreDexFileNames(),
1221 base_oat_name,
1222 CompilerFilter::Filter::kQuicken,
1223 &error_msg,
1224 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1225 EXPECT_EQ(res2, 0);
1226 Copy(base_oat_name, no_unload_oat_name);
1227 Copy(base_vdex_name, no_unload_vdex_name);
1228 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1229 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1230 ASSERT_TRUE(no_unload_oat != nullptr);
1231 ASSERT_TRUE(no_unload_vdex != nullptr);
1232 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1233 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1234 // Verify that both of the files are the same (odex and vdex).
1235 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1236 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1237 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1238 << unload_oat_name << " " << no_unload_oat_name;
1239 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1240 << unload_vdex_name << " " << no_unload_vdex_name;
1241 // App image file.
1242 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1243 ASSERT_TRUE(app_image_file != nullptr);
1244 EXPECT_GT(app_image_file->GetLength(), 0u);
1245}
1246
Mathieu Chartier120aa282017-08-05 16:03:03 -07001247// Test that dexlayout section info is correctly written to the oat file for profile based
1248// compilation.
1249TEST_F(Dex2oatTest, LayoutSections) {
1250 using Hotness = ProfileCompilationInfo::MethodHotness;
1251 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1252 ScratchFile profile_file;
1253 // We can only layout method indices with code items, figure out which ones have this property
1254 // first.
1255 std::vector<uint16_t> methods;
1256 {
1257 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1258 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
1259 const DexFile::ClassDef* class_def = dex->FindClassDef(type_idx);
1260 ClassDataItemIterator it(*dex, dex->GetClassData(*class_def));
1261 it.SkipAllFields();
1262 std::set<size_t> code_item_offsets;
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001263 for (; it.HasNextMethod(); it.Next()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001264 const uint16_t method_idx = it.GetMemberIndex();
1265 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1266 if (code_item_offsets.insert(code_item_offset).second) {
1267 // Unique code item, add the method index.
1268 methods.push_back(method_idx);
1269 }
1270 }
1271 DCHECK(!it.HasNext());
1272 }
1273 ASSERT_GE(methods.size(), 8u);
1274 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1275 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1276 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1277 // Here, we build the profile from the method lists.
1278 ProfileCompilationInfo info;
1279 info.AddMethodsForDex(
1280 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1281 dex.get(),
1282 hot_methods.begin(),
1283 hot_methods.end());
1284 info.AddMethodsForDex(
1285 Hotness::kFlagStartup,
1286 dex.get(),
1287 startup_methods.begin(),
1288 startup_methods.end());
1289 info.AddMethodsForDex(
1290 Hotness::kFlagPostStartup,
1291 dex.get(),
1292 post_methods.begin(),
1293 post_methods.end());
1294 for (uint16_t id : hot_methods) {
1295 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1296 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1297 }
1298 for (uint16_t id : startup_methods) {
1299 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1300 }
1301 for (uint16_t id : post_methods) {
1302 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1303 }
1304 // Save the profile since we want to use it with dex2oat to produce an oat file.
1305 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1306 // Generate a profile based odex.
1307 const std::string dir = GetScratchDir();
1308 const std::string oat_filename = dir + "/base.oat";
1309 const std::string vdex_filename = dir + "/base.vdex";
1310 std::string error_msg;
1311 const int res = GenerateOdexForTestWithStatus(
1312 {dex->GetLocation()},
1313 oat_filename,
1314 CompilerFilter::Filter::kQuicken,
1315 &error_msg,
1316 {"--profile-file=" + profile_file.GetFilename()});
1317 EXPECT_EQ(res, 0);
1318
1319 // Open our generated oat file.
1320 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1321 oat_filename.c_str(),
1322 nullptr,
1323 nullptr,
1324 false,
1325 /*low_4gb*/false,
1326 dex->GetLocation().c_str(),
1327 &error_msg));
1328 ASSERT_TRUE(odex_file != nullptr);
1329 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1330 ASSERT_EQ(oat_dex_files.size(), 1u);
1331 // Check that the code sections match what we expect.
1332 for (const OatDexFile* oat_dex : oat_dex_files) {
1333 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1334 // Testing of logging the sections.
1335 ASSERT_TRUE(sections != nullptr);
1336 LOG(INFO) << *sections;
1337
1338 // Load the sections into temporary variables for convenience.
1339 const DexLayoutSection& code_section =
1340 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1341 const DexLayoutSection::Subsection& section_hot_code =
1342 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1343 const DexLayoutSection::Subsection& section_sometimes_used =
1344 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1345 const DexLayoutSection::Subsection& section_startup_only =
1346 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1347 const DexLayoutSection::Subsection& section_unused =
1348 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1349
1350 // All the sections should be non-empty.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001351 EXPECT_GT(section_hot_code.Size(), 0u);
1352 EXPECT_GT(section_sometimes_used.Size(), 0u);
1353 EXPECT_GT(section_startup_only.Size(), 0u);
1354 EXPECT_GT(section_unused.Size(), 0u);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001355
1356 // Open the dex file since we need to peek at the code items to verify the layout matches what
1357 // we expect.
1358 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1359 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1360 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1361 ASSERT_TRUE(type_id != nullptr);
1362 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1363 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1364 ASSERT_TRUE(class_def != nullptr);
1365
1366 // Count how many code items are for each category, there should be at least one per category.
1367 size_t hot_count = 0;
1368 size_t post_startup_count = 0;
1369 size_t startup_count = 0;
1370 size_t unused_count = 0;
1371 // Visit all of the methdos of the main class and cross reference the method indices to their
1372 // corresponding code item offsets to verify the layout.
1373 ClassDataItemIterator it(*dex_file, dex_file->GetClassData(*class_def));
1374 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001375 for (; it.HasNextMethod(); it.Next()) {
Mathieu Chartier120aa282017-08-05 16:03:03 -07001376 const size_t method_idx = it.GetMemberIndex();
1377 const size_t code_item_offset = it.GetMethodCodeItemOffset();
1378 const bool is_hot = ContainsElement(hot_methods, method_idx);
1379 const bool is_startup = ContainsElement(startup_methods, method_idx);
1380 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1381 if (is_hot) {
1382 // Hot is highest precedence, check that the hot methods are in the hot section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001383 EXPECT_TRUE(section_hot_code.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001384 ++hot_count;
1385 } else if (is_post_startup) {
1386 // Post startup is sometimes used section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001387 EXPECT_TRUE(section_sometimes_used.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001388 ++post_startup_count;
1389 } else if (is_startup) {
1390 // Startup at this point means not hot or post startup, these must be startup only then.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001391 EXPECT_TRUE(section_startup_only.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001392 ++startup_count;
1393 } else {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001394 if (section_unused.Contains(code_item_offset)) {
Alan Leung9595fd32017-10-17 17:08:19 -07001395 // If no flags are set, the method should be unused ...
1396 ++unused_count;
1397 } else {
1398 // or this method is part of the last code item and the end is 4 byte aligned.
1399 ClassDataItemIterator it2(*dex_file, dex_file->GetClassData(*class_def));
1400 it2.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001401 for (; it2.HasNextMethod(); it2.Next()) {
Alan Leung9595fd32017-10-17 17:08:19 -07001402 EXPECT_LE(it2.GetMethodCodeItemOffset(), code_item_offset);
1403 }
1404 uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx);
1405 EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u);
1406 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001407 }
1408 }
1409 DCHECK(!it.HasNext());
1410 EXPECT_GT(hot_count, 0u);
1411 EXPECT_GT(post_startup_count, 0u);
1412 EXPECT_GT(startup_count, 0u);
1413 EXPECT_GT(unused_count, 0u);
1414 }
1415}
1416
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001417// Test that generating compact dex works.
1418TEST_F(Dex2oatTest, GenerateCompactDex) {
1419 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1420 // Generate a compact dex based odex.
1421 const std::string dir = GetScratchDir();
1422 const std::string oat_filename = dir + "/base.oat";
1423 const std::string vdex_filename = dir + "/base.vdex";
1424 std::string error_msg;
1425 const int res = GenerateOdexForTestWithStatus(
1426 {dex->GetLocation()},
1427 oat_filename,
1428 CompilerFilter::Filter::kQuicken,
1429 &error_msg,
1430 {"--compact-dex-level=fast"});
1431 EXPECT_EQ(res, 0);
1432 // Open our generated oat file.
1433 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1434 oat_filename.c_str(),
1435 nullptr,
1436 nullptr,
1437 false,
1438 /*low_4gb*/false,
1439 dex->GetLocation().c_str(),
1440 &error_msg));
1441 ASSERT_TRUE(odex_file != nullptr);
1442 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1443 ASSERT_EQ(oat_dex_files.size(), 1u);
1444 // Check that each dex is a compact dex.
1445 for (const OatDexFile* oat_dex : oat_dex_files) {
1446 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1447 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1448 ASSERT_TRUE(dex_file->IsCompactDexFile());
1449 }
1450}
1451
Andreas Gampef39208f2017-10-19 15:06:59 -07001452class Dex2oatVerifierAbort : public Dex2oatTest {};
1453
1454TEST_F(Dex2oatVerifierAbort, HardFail) {
1455 // Use VerifierDeps as it has hard-failing classes.
1456 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps"));
1457 std::string out_dir = GetScratchDir();
1458 const std::string base_oat_name = out_dir + "/base.oat";
1459 std::string error_msg;
1460 const int res_fail = GenerateOdexForTestWithStatus(
1461 {dex->GetLocation()},
1462 base_oat_name,
1463 CompilerFilter::Filter::kQuicken,
1464 &error_msg,
1465 {"--abort-on-hard-verifier-error"});
1466 EXPECT_NE(0, res_fail);
1467
1468 const int res_no_fail = GenerateOdexForTestWithStatus(
1469 {dex->GetLocation()},
1470 base_oat_name,
1471 CompilerFilter::Filter::kQuicken,
1472 &error_msg,
1473 {"--no-abort-on-hard-verifier-error"});
1474 EXPECT_EQ(0, res_no_fail);
1475}
1476
1477TEST_F(Dex2oatVerifierAbort, SoftFail) {
1478 // Use VerifierDepsMulti as it has hard-failing classes.
1479 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDepsMulti"));
1480 std::string out_dir = GetScratchDir();
1481 const std::string base_oat_name = out_dir + "/base.oat";
1482 std::string error_msg;
1483 const int res_fail = GenerateOdexForTestWithStatus(
1484 {dex->GetLocation()},
1485 base_oat_name,
1486 CompilerFilter::Filter::kQuicken,
1487 &error_msg,
1488 {"--abort-on-soft-verifier-error"});
1489 EXPECT_NE(0, res_fail);
1490
1491 const int res_no_fail = GenerateOdexForTestWithStatus(
1492 {dex->GetLocation()},
1493 base_oat_name,
1494 CompilerFilter::Filter::kQuicken,
1495 &error_msg,
1496 {"--no-abort-on-soft-verifier-error"});
1497 EXPECT_EQ(0, res_no_fail);
1498}
1499
Andreas Gampecac31ad2017-11-06 20:01:17 -08001500class Dex2oatDedupeCode : public Dex2oatTest {};
1501
1502TEST_F(Dex2oatDedupeCode, DedupeTest) {
1503 // Use MyClassNatives. It has lots of native methods that will produce deduplicate-able code.
1504 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MyClassNatives"));
1505 std::string out_dir = GetScratchDir();
1506 const std::string base_oat_name = out_dir + "/base.oat";
1507 size_t no_dedupe_size = 0;
1508 GenerateOdexForTest(dex->GetLocation(),
1509 base_oat_name,
1510 CompilerFilter::Filter::kSpeed,
1511 { "--deduplicate-code=false" },
1512 true, // expect_success
1513 false, // use_fd
1514 [&no_dedupe_size](const OatFile& o) {
1515 no_dedupe_size = o.Size();
1516 });
1517
1518 size_t dedupe_size = 0;
1519 GenerateOdexForTest(dex->GetLocation(),
1520 base_oat_name,
1521 CompilerFilter::Filter::kSpeed,
1522 { "--deduplicate-code=true" },
1523 true, // expect_success
1524 false, // use_fd
1525 [&dedupe_size](const OatFile& o) {
1526 dedupe_size = o.Size();
1527 });
1528
1529 EXPECT_LT(dedupe_size, no_dedupe_size);
1530}
1531
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001532TEST_F(Dex2oatTest, UncompressedTest) {
1533 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MainUncompressed"));
1534 std::string out_dir = GetScratchDir();
1535 const std::string base_oat_name = out_dir + "/base.oat";
1536 GenerateOdexForTest(dex->GetLocation(),
1537 base_oat_name,
1538 CompilerFilter::Filter::kQuicken,
1539 { },
1540 true, // expect_success
1541 false, // use_fd
1542 [](const OatFile& o) {
1543 CHECK(!o.ContainsDexCode());
1544 });
1545}
1546
Mathieu Chartier700a9852018-02-06 18:27:38 -08001547TEST_F(Dex2oatTest, EmptyUncompressedDexTest) {
1548 std::string out_dir = GetScratchDir();
1549 const std::string base_oat_name = out_dir + "/base.oat";
1550 std::string error_msg;
1551 int status = GenerateOdexForTestWithStatus(
1552 { GetTestDexFileName("MainEmptyUncompressed") },
1553 base_oat_name,
1554 CompilerFilter::Filter::kQuicken,
1555 &error_msg,
1556 { },
1557 /*use_fd*/ false);
1558 // Expect to fail with code 1 and not SIGSEGV or SIGABRT.
1559 ASSERT_TRUE(WIFEXITED(status));
1560 ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg;
1561}
1562
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001563// Dex file that has duplicate methods have different code items and debug info.
1564static const char kDuplicateMethodInputDex[] =
1565 "ZGV4CjAzOQDEy8VPdj4qHpgPYFWtLCtOykfFP4kB8tGYDAAAcAAAAHhWNBIAAAAAAAAAANALAABI"
1566 "AAAAcAAAAA4AAACQAQAABQAAAMgBAAANAAAABAIAABkAAABsAgAABAAAADQDAADgCAAAuAMAADgI"
1567 "AABCCAAASggAAE8IAABcCAAAaggAAHkIAACICAAAlggAAKQIAACyCAAAwAgAAM4IAADcCAAA6ggA"
1568 "APgIAAD7CAAA/wgAABcJAAAuCQAARQkAAFQJAAB4CQAAmAkAALsJAADSCQAA5gkAAPoJAAAVCgAA"
1569 "KQoAADsKAABCCgAASgoAAFIKAABbCgAAZAoAAGwKAAB0CgAAfAoAAIQKAACMCgAAlAoAAJwKAACk"
1570 "CgAArQoAALcKAADACgAAwwoAAMcKAADcCgAA6QoAAPEKAAD3CgAA/QoAAAMLAAAJCwAAEAsAABcL"
1571 "AAAdCwAAIwsAACkLAAAvCwAANQsAADsLAABBCwAARwsAAE0LAABSCwAAWwsAAF4LAABoCwAAbwsA"
1572 "ABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAC4AAAAwAAAA"
1573 "DwAAAAkAAAAAAAAAEAAAAAoAAACoBwAALgAAAAwAAAAAAAAALwAAAAwAAACoBwAALwAAAAwAAACw"
1574 "BwAAAgAJADUAAAACAAkANgAAAAIACQA3AAAAAgAJADgAAAACAAkAOQAAAAIACQA6AAAAAgAJADsA"
1575 "AAACAAkAPAAAAAIACQA9AAAAAgAJAD4AAAACAAkAPwAAAAIACQBAAAAACwAHAEIAAAAAAAIAAQAA"
1576 "AAAAAwAeAAAAAQACAAEAAAABAAMAHgAAAAIAAgAAAAAAAgACAAEAAAADAAIAAQAAAAMAAgAfAAAA"
1577 "AwACACAAAAADAAIAIQAAAAMAAgAiAAAAAwACACMAAAADAAIAJAAAAAMAAgAlAAAAAwACACYAAAAD"
1578 "AAIAJwAAAAMAAgAoAAAAAwACACkAAAADAAIAKgAAAAMABAA0AAAABwADAEMAAAAIAAIAAQAAAAoA"
1579 "AgABAAAACgABADIAAAAKAAAARQAAAAAAAAAAAAAACAAAAAAAAAAdAAAAaAcAALYHAAAAAAAAAQAA"
1580 "AAAAAAAIAAAAAAAAAB0AAAB4BwAAxAcAAAAAAAACAAAAAAAAAAgAAAAAAAAAHQAAAIgHAADSBwAA"
1581 "AAAAAAMAAAAAAAAACAAAAAAAAAAdAAAAmAcAAPoHAAAAAAAAAAAAAAEAAAAAAAAArAYAADEAAAAa"
1582 "AAMAaQAAABoABABpAAEAGgAHAGkABAAaAAgAaQAFABoACQBpAAYAGgAKAGkABwAaAAsAaQAIABoA"
1583 "DABpAAkAGgANAGkACgAaAA4AaQALABoABQBpAAIAGgAGAGkAAwAOAAAAAQABAAEAAACSBgAABAAA"
1584 "AHAQFQAAAA4ABAABAAIAAACWBgAAFwAAAGIADAAiAQoAcBAWAAEAGgICAG4gFwAhAG4gFwAxAG4Q"
1585 "GAABAAwBbiAUABAADgAAAAEAAQABAAAAngYAAAQAAABwEBUAAAAOAAIAAQACAAAAogYAAAYAAABi"
1586 "AAwAbiAUABAADgABAAEAAQAAAKgGAAAEAAAAcBAVAAAADgABAAEAAQAAALsGAAAEAAAAcBAVAAAA"
1587 "DgABAAAAAQAAAL8GAAAGAAAAYgAAAHEQAwAAAA4AAQAAAAEAAADEBgAABgAAAGIAAQBxEAMAAAAO"
1588 "AAEAAAABAAAA8QYAAAYAAABiAAIAcRABAAAADgABAAAAAQAAAPYGAAAGAAAAYgADAHEQAwAAAA4A"
1589 "AQAAAAEAAADJBgAABgAAAGIABABxEAMAAAAOAAEAAAABAAAAzgYAAAYAAABiAAEAcRADAAAADgAB"
1590 "AAAAAQAAANMGAAAGAAAAYgAGAHEQAwAAAA4AAQAAAAEAAADYBgAABgAAAGIABwBxEAMAAAAOAAEA"
1591 "AAABAAAA3QYAAAYAAABiAAgAcRABAAAADgABAAAAAQAAAOIGAAAGAAAAYgAJAHEQAwAAAA4AAQAA"
1592 "AAEAAADnBgAABgAAAGIACgBxEAMAAAAOAAEAAAABAAAA7AYAAAYAAABiAAsAcRABAAAADgABAAEA"
1593 "AAAAAPsGAAAlAAAAcQAHAAAAcQAIAAAAcQALAAAAcQAMAAAAcQANAAAAcQAOAAAAcQAPAAAAcQAQ"
1594 "AAAAcQARAAAAcQASAAAAcQAJAAAAcQAKAAAADgAnAA4AKQFFDgEWDwAhAA4AIwFFDloAEgAOABMA"
1595 "DktLS0tLS0tLS0tLABEADgAuAA5aADIADloANgAOWgA6AA5aAD4ADloAQgAOWgBGAA5aAEoADloA"
1596 "TgAOWgBSAA5aAFYADloAWgAOWgBeATQOPDw8PDw8PDw8PDw8AAIEAUYYAwIFAjEECEEXLAIFAjEE"
1597 "CEEXKwIFAjEECEEXLQIGAUYcAxgAGAEYAgAAAAIAAAAMBwAAEgcAAAIAAAAMBwAAGwcAAAIAAAAM"
1598 "BwAAJAcAAAEAAAAtBwAAPAcAAAAAAAAAAAAAAAAAAEgHAAAAAAAAAAAAAAAAAABUBwAAAAAAAAAA"
1599 "AAAAAAAAYAcAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAQAAAA0AAAACAACAgASsCAEIxAgAAAIAAoCA"
1600 "BIQJAQicCQwAAgAACQEJAQkBCQEJAQkBCQEJAQkBCQEJAQkEiIAEuAcBgIAEuAkAAA4ABoCABNAJ"
1601 "AQnoCQAJhAoACaAKAAm8CgAJ2AoACfQKAAmQCwAJrAsACcgLAAnkCwAJgAwACZwMAAm4DAg8Y2xp"
1602 "bml0PgAGPGluaXQ+AANBQUEAC0hlbGxvIFdvcmxkAAxIZWxsbyBXb3JsZDEADUhlbGxvIFdvcmxk"
1603 "MTAADUhlbGxvIFdvcmxkMTEADEhlbGxvIFdvcmxkMgAMSGVsbG8gV29ybGQzAAxIZWxsbyBXb3Js"
1604 "ZDQADEhlbGxvIFdvcmxkNQAMSGVsbG8gV29ybGQ2AAxIZWxsbyBXb3JsZDcADEhlbGxvIFdvcmxk"
1605 "OAAMSGVsbG8gV29ybGQ5AAFMAAJMTAAWTE1hbnlNZXRob2RzJFByaW50ZXIyOwAVTE1hbnlNZXRo"
1606 "b2RzJFByaW50ZXI7ABVMTWFueU1ldGhvZHMkU3RyaW5nczsADUxNYW55TWV0aG9kczsAIkxkYWx2"
1607 "aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5lckNs"
1608 "YXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABVMamF2YS9pby9QcmludFN0"
1609 "cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABlMamF2YS9sYW5n"
1610 "L1N0cmluZ0J1aWxkZXI7ABJMamF2YS9sYW5nL1N5c3RlbTsAEE1hbnlNZXRob2RzLmphdmEABVBy"
1611 "aW50AAZQcmludDAABlByaW50MQAHUHJpbnQxMAAHUHJpbnQxMQAGUHJpbnQyAAZQcmludDMABlBy"
1612 "aW50NAAGUHJpbnQ1AAZQcmludDYABlByaW50NwAGUHJpbnQ4AAZQcmludDkAB1ByaW50ZXIACFBy"
1613 "aW50ZXIyAAdTdHJpbmdzAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwALYWNjZXNzRmxhZ3MA"
1614 "BmFwcGVuZAAEYXJncwAEbWFpbgAEbXNnMAAEbXNnMQAFbXNnMTAABW1zZzExAARtc2cyAARtc2cz"
1615 "AARtc2c0AARtc2c1AARtc2c2AARtc2c3AARtc2c4AARtc2c5AARuYW1lAANvdXQAB3ByaW50bG4A"
1616 "AXMACHRvU3RyaW5nAAV2YWx1ZQBffn5EOHsibWluLWFwaSI6MTAwMDAsInNoYS0xIjoiZmViODZj"
1617 "MDA2ZWZhY2YxZDc5ODRiODVlMTc5MGZlZjdhNzY3YWViYyIsInZlcnNpb24iOiJ2MS4xLjUtZGV2"
1618 "In0AEAAAAAAAAAABAAAAAAAAAAEAAABIAAAAcAAAAAIAAAAOAAAAkAEAAAMAAAAFAAAAyAEAAAQA"
1619 "AAANAAAABAIAAAUAAAAZAAAAbAIAAAYAAAAEAAAANAMAAAEgAAAUAAAAuAMAAAMgAAAUAAAAkgYA"
1620 "AAQgAAAFAAAADAcAAAMQAAAEAAAAOQcAAAYgAAAEAAAAaAcAAAEQAAACAAAAqAcAAAAgAAAEAAAA"
1621 "tgcAAAIgAABIAAAAOAgAAAAQAAABAAAA0AsAAAAAAAA=";
1622
1623static void WriteBase64ToFile(const char* base64, File* file) {
1624 // Decode base64.
1625 CHECK(base64 != nullptr);
1626 size_t length;
1627 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
1628 CHECK(bytes != nullptr);
1629 if (!file->WriteFully(bytes.get(), length)) {
1630 PLOG(FATAL) << "Failed to write base64 as file";
1631 }
1632}
1633
1634TEST_F(Dex2oatTest, CompactDexGenerationFailure) {
1635 ScratchFile temp_dex;
1636 WriteBase64ToFile(kDuplicateMethodInputDex, temp_dex.GetFile());
1637 std::string out_dir = GetScratchDir();
1638 const std::string oat_filename = out_dir + "/base.oat";
1639 // The dex won't pass the method verifier, only use the verify filter.
1640 GenerateOdexForTest(temp_dex.GetFilename(),
1641 oat_filename,
1642 CompilerFilter::Filter::kVerify,
1643 { },
1644 true, // expect_success
1645 false, // use_fd
1646 [](const OatFile& o) {
1647 CHECK(o.ContainsDexCode());
1648 });
1649 // Open our generated oat file.
1650 std::string error_msg;
1651 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_filename.c_str(),
1652 oat_filename.c_str(),
1653 nullptr,
1654 nullptr,
1655 false,
1656 /*low_4gb*/false,
1657 temp_dex.GetFilename().c_str(),
1658 &error_msg));
1659 ASSERT_TRUE(odex_file != nullptr);
1660 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1661 ASSERT_EQ(oat_dex_files.size(), 1u);
1662 // The dexes should have failed to convert to compact dex.
1663 for (const OatDexFile* oat_dex : oat_dex_files) {
1664 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1665 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1666 ASSERT_TRUE(!dex_file->IsCompactDexFile());
1667 }
1668}
1669
Andreas Gampee1459ae2016-06-29 09:36:30 -07001670} // namespace art