blob: d1534597c491e4e335a747f5889e57b8e1d81d14 [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"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/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 Sehr312f3b22018-03-19 08:39:26 -070035#include "dex/bytecode_utils.h"
Mathieu Chartier2242ef12018-07-23 18:14:13 -070036#include "dex/class_accessor-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080037#include "dex/code_item_accessors-inl.h"
38#include "dex/dex_file-inl.h"
39#include "dex/dex_file_loader.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070040#include "dex2oat_environment_test.h"
Andreas Gampef7882972017-03-20 16:35:24 -070041#include "dex2oat_return_codes.h"
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -070042#include "intern_table-inl.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070043#include "oat.h"
44#include "oat_file.h"
David Sehr82d046e2018-04-23 08:14:19 -070045#include "profile/profile_compilation_info.h"
Mathieu Chartier792111c2018-02-15 13:02:15 -080046#include "vdex_file.h"
47#include "ziparchive/zip_writer.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070048
Andreas Gampee1459ae2016-06-29 09:36:30 -070049namespace art {
50
Mathieu Chartierea650f32017-05-24 12:04:13 -070051static constexpr size_t kMaxMethodIds = 65535;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070052static constexpr bool kDebugArgs = false;
Mathieu Chartier02129102017-12-22 11:04:01 -080053static const char* kDisableCompactDex = "--compact-dex-level=none";
Mathieu Chartierea650f32017-05-24 12:04:13 -070054
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080055using android::base::StringPrintf;
56
Andreas Gampee1459ae2016-06-29 09:36:30 -070057class Dex2oatTest : public Dex2oatEnvironmentTest {
58 public:
Roland Levillainf73caca2018-08-24 17:19:07 +010059 void TearDown() override {
Andreas Gampee1459ae2016-06-29 09:36:30 -070060 Dex2oatEnvironmentTest::TearDown();
61
62 output_ = "";
63 error_msg_ = "";
64 success_ = false;
65 }
66
67 protected:
Mathieu Chartier9e050df2017-08-09 10:05:47 -070068 int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
Andreas Gampef7882972017-03-20 16:35:24 -070069 const std::string& odex_location,
70 CompilerFilter::Filter filter,
71 std::string* error_msg,
72 const std::vector<std::string>& extra_args = {},
73 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080074 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070075 std::vector<std::string> args;
Mathieu Chartier9e050df2017-08-09 10:05:47 -070076 // Add dex file args.
77 for (const std::string& dex_location : dex_locations) {
78 args.push_back("--dex-file=" + dex_location);
79 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080080 if (use_fd) {
81 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
82 CHECK(oat_file != nullptr) << odex_location;
83 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080084 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080085 } else {
86 args.push_back("--oat-file=" + odex_location);
87 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070088 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
89 args.push_back("--runtime-arg");
90 args.push_back("-Xnorelocate");
91
92 args.insert(args.end(), extra_args.begin(), extra_args.end());
93
Andreas Gampef7882972017-03-20 16:35:24 -070094 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080095 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070096 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080097 }
Andreas Gampef7882972017-03-20 16:35:24 -070098 return status;
99 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700100
Andreas Gampe641a4732017-08-24 13:21:35 -0700101 void GenerateOdexForTest(
102 const std::string& dex_location,
103 const std::string& odex_location,
104 CompilerFilter::Filter filter,
105 const std::vector<std::string>& extra_args = {},
106 bool expect_success = true,
107 bool use_fd = false) {
108 GenerateOdexForTest(dex_location,
109 odex_location,
110 filter,
111 extra_args,
112 expect_success,
113 use_fd,
114 [](const OatFile&) {});
115 }
116
Andreas Gampe80ddf272018-01-11 09:41:00 -0800117 bool test_accepts_odex_file_on_failure = false;
118
Andreas Gampe641a4732017-08-24 13:21:35 -0700119 template <typename T>
120 void GenerateOdexForTest(
121 const std::string& dex_location,
122 const std::string& odex_location,
123 CompilerFilter::Filter filter,
124 const std::vector<std::string>& extra_args,
125 bool expect_success,
126 bool use_fd,
127 T check_oat) {
Andreas Gampef7882972017-03-20 16:35:24 -0700128 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700129 int status = GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -0700130 odex_location,
131 filter,
132 &error_msg,
133 extra_args,
134 use_fd);
Andreas Gampe80ddf272018-01-11 09:41:00 -0800135 bool success = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700136 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800137 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700138
139 // Verify the odex file was generated as expected.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100140 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100141 odex_location.c_str(),
Andreas Gampee1459ae2016-06-29 09:36:30 -0700142 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100143 /*executable=*/ false,
144 /*low_4gb=*/ false,
Andreas Gampee1459ae2016-06-29 09:36:30 -0700145 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100146 /*reservation=*/ nullptr,
Andreas Gampee1459ae2016-06-29 09:36:30 -0700147 &error_msg));
148 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
149
150 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700151 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700152 } else {
153 ASSERT_FALSE(success) << output_;
154
155 error_msg_ = error_msg;
156
Andreas Gampe80ddf272018-01-11 09:41:00 -0800157 if (!test_accepts_odex_file_on_failure) {
158 // Verify there's no loadable odex file.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100159 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100160 odex_location.c_str(),
Andreas Gampe80ddf272018-01-11 09:41:00 -0800161 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100162 /*executable=*/ false,
163 /*low_4gb=*/ false,
Andreas Gampe80ddf272018-01-11 09:41:00 -0800164 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100165 /*reservation=*/ nullptr,
Andreas Gampe80ddf272018-01-11 09:41:00 -0800166 &error_msg));
167 ASSERT_TRUE(odex_file.get() == nullptr);
168 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700169 }
170 }
171
Calin Juravle1ccf6132017-08-02 17:46:53 -0700172 // Check the input compiler filter against the generated oat file's filter. May be overridden
Andreas Gampee1459ae2016-06-29 09:36:30 -0700173 // in subclasses when equality is not expected.
174 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
175 EXPECT_EQ(expected, actual);
176 }
177
Andreas Gampef7882972017-03-20 16:35:24 -0700178 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700179 Runtime* runtime = Runtime::Current();
180
181 const std::vector<gc::space::ImageSpace*>& image_spaces =
182 runtime->GetHeap()->GetBootImageSpaces();
183 if (image_spaces.empty()) {
184 *error_msg = "No image location found for Dex2Oat.";
185 return false;
186 }
187 std::string image_location = image_spaces[0]->GetImageLocation();
188
189 std::vector<std::string> argv;
190 argv.push_back(runtime->GetCompilerExecutable());
Calin Juravle1ccf6132017-08-02 17:46:53 -0700191
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000192 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700193 argv.push_back("--debuggable");
194 }
195 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
196
197 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100198 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700199 }
200
201 if (runtime->MustRelocateIfPossible()) {
202 argv.push_back("--runtime-arg");
203 argv.push_back("-Xrelocate");
204 } else {
205 argv.push_back("--runtime-arg");
206 argv.push_back("-Xnorelocate");
207 }
208
209 if (!kIsTargetBuild) {
210 argv.push_back("--host");
211 }
212
213 argv.push_back("--boot-image=" + image_location);
214
215 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
216 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
217
218 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
219
220 // We must set --android-root.
221 const char* android_root = getenv("ANDROID_ROOT");
222 CHECK(android_root != nullptr);
223 argv.push_back("--android-root=" + std::string(android_root));
224
Mathieu Chartier9e050df2017-08-09 10:05:47 -0700225 if (kDebugArgs) {
226 std::string all_args;
227 for (const std::string& arg : argv) {
228 all_args += arg + " ";
229 }
230 LOG(ERROR) << all_args;
231 }
232
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700233 // We need dex2oat to actually log things.
234 auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:d", 1) == 0; };
235 ForkAndExecResult res = ForkAndExec(argv, post_fork_fn, &output_);
236 if (res.stage != ForkAndExecResult::kFinished) {
237 *error_msg = strerror(errno);
238 return -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100239 }
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700240 success_ = res.StandardSuccess();
241 return res.status_code;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700242 }
243
244 std::string output_ = "";
245 std::string error_msg_ = "";
246 bool success_ = false;
247};
248
249class Dex2oatSwapTest : public Dex2oatTest {
250 protected:
251 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
252 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
253 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
254
Andreas Gampe7adeda82016-07-25 08:27:35 -0700255 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700256
257 std::vector<std::string> copy(extra_args);
258
259 std::unique_ptr<ScratchFile> sf;
260 if (use_fd) {
261 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800262 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700263 } else {
264 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
265 copy.push_back("--swap-file=" + swap_location);
266 }
267 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
268
269 CheckValidity();
270 ASSERT_TRUE(success_);
271 CheckResult(expect_use);
272 }
273
Andreas Gampe7adeda82016-07-25 08:27:35 -0700274 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000275 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700276 }
277
278 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700279 if (kIsTargetBuild) {
280 CheckTargetResult(expect_use);
281 } else {
282 CheckHostResult(expect_use);
283 }
284 }
285
Andreas Gampe7adeda82016-07-25 08:27:35 -0700286 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700287 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
288 // something for variants with file descriptor where we can control the lifetime of
289 // the swap file and thus take a look at it.
290 }
291
Andreas Gampe7adeda82016-07-25 08:27:35 -0700292 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700293 if (!kIsTargetBuild) {
294 if (expect_use) {
295 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
296 << output_;
297 } else {
298 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
299 << output_;
300 }
301 }
302 }
303
304 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700305 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700306 if (kIsTargetBuild) {
307 CheckTargetValidity();
308 } else {
309 CheckHostValidity();
310 }
311 }
312
Andreas Gampe7adeda82016-07-25 08:27:35 -0700313 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700314 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
315 // something for variants with file descriptor where we can control the lifetime of
316 // the swap file and thus take a look at it.
317 }
318
319 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700320 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700321 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
322 }
323};
324
325TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100326 RunTest(/*use_fd=*/ false, /*expect_use=*/ false);
327 RunTest(/*use_fd=*/ true, /*expect_use=*/ false);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700328}
329
330TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100331 RunTest(/*use_fd=*/ false, /*expect_use=*/ false, { "--swap-dex-size-threshold=0" });
332 RunTest(/*use_fd=*/ true, /*expect_use=*/ false, { "--swap-dex-size-threshold=0" });
Andreas Gampee1459ae2016-06-29 09:36:30 -0700333}
334
335TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100336 RunTest(/*use_fd=*/ false, /*expect_use=*/ false, { "--swap-dex-count-threshold=0" });
337 RunTest(/*use_fd=*/ true, /*expect_use=*/ false, { "--swap-dex-count-threshold=0" });
Andreas Gampee1459ae2016-06-29 09:36:30 -0700338}
339
340TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100341 RunTest(/*use_fd=*/ false,
342 /*expect_use=*/ true,
Andreas Gampee1459ae2016-06-29 09:36:30 -0700343 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100344 RunTest(/*use_fd=*/ true,
345 /*expect_use=*/ true,
Andreas Gampee1459ae2016-06-29 09:36:30 -0700346 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
347}
348
Andreas Gampe7adeda82016-07-25 08:27:35 -0700349class Dex2oatSwapUseTest : public Dex2oatSwapTest {
350 protected:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100351 void CheckHostResult(bool expect_use) override {
Andreas Gampe7adeda82016-07-25 08:27:35 -0700352 if (!kIsTargetBuild) {
353 if (expect_use) {
354 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
355 << output_;
356 } else {
357 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
358 << output_;
359 }
360 }
361 }
362
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100363 std::string GetTestDexFileName() override {
Andreas Gampe7adeda82016-07-25 08:27:35 -0700364 // Use Statics as it has a handful of functions.
365 return CommonRuntimeTest::GetTestDexFileName("Statics");
366 }
367
368 void GrabResult1() {
369 if (!kIsTargetBuild) {
370 native_alloc_1_ = ParseNativeAlloc();
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100371 swap_1_ = ParseSwap(/*expected=*/ false);
Andreas Gampe7adeda82016-07-25 08:27:35 -0700372 } else {
373 native_alloc_1_ = std::numeric_limits<size_t>::max();
374 swap_1_ = 0;
375 }
376 }
377
378 void GrabResult2() {
379 if (!kIsTargetBuild) {
380 native_alloc_2_ = ParseNativeAlloc();
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100381 swap_2_ = ParseSwap(/*expected=*/ true);
Andreas Gampe7adeda82016-07-25 08:27:35 -0700382 } else {
383 native_alloc_2_ = 0;
384 swap_2_ = std::numeric_limits<size_t>::max();
385 }
386 }
387
388 private:
389 size_t ParseNativeAlloc() {
390 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
391 std::smatch native_alloc_match;
392 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
393 if (!found) {
394 EXPECT_TRUE(found);
395 return 0;
396 }
397 if (native_alloc_match.size() != 2U) {
398 EXPECT_EQ(native_alloc_match.size(), 2U);
399 return 0;
400 }
401
402 std::istringstream stream(native_alloc_match[1].str());
403 size_t value;
404 stream >> value;
405
406 return value;
407 }
408
409 size_t ParseSwap(bool expected) {
410 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
411 std::smatch swap_match;
412 bool found = std::regex_search(output_, swap_match, swap_regex);
413 if (found != expected) {
414 EXPECT_EQ(expected, found);
415 return 0;
416 }
417
418 if (!found) {
419 return 0;
420 }
421
422 if (swap_match.size() != 2U) {
423 EXPECT_EQ(swap_match.size(), 2U);
424 return 0;
425 }
426
427 std::istringstream stream(swap_match[1].str());
428 size_t value;
429 stream >> value;
430
431 return value;
432 }
433
434 protected:
435 size_t native_alloc_1_;
436 size_t native_alloc_2_;
437
438 size_t swap_1_;
439 size_t swap_2_;
440};
441
442TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Roland Levillain05e34f42018-05-24 13:19:05 +0000443 // Native memory usage isn't correctly tracked when running under ASan.
444 TEST_DISABLED_FOR_MEMORY_TOOL();
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700445
Vladimir Marko57070da2017-02-14 16:16:30 +0000446 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000447 // hold true on some x86 systems; disable this test while we
448 // investigate (b/29259363).
449 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000450
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100451 RunTest(/*use_fd=*/ false,
452 /*expect_use=*/ false);
Andreas Gampe7adeda82016-07-25 08:27:35 -0700453 GrabResult1();
454 std::string output_1 = output_;
455
456 output_ = "";
457
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100458 RunTest(/*use_fd=*/ false,
459 /*expect_use=*/ true,
Andreas Gampe7adeda82016-07-25 08:27:35 -0700460 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
461 GrabResult2();
462 std::string output_2 = output_;
463
464 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
465 EXPECT_LT(native_alloc_2_, native_alloc_1_);
466 EXPECT_LT(swap_1_, swap_2_);
467
468 LOG(ERROR) << output_1;
469 LOG(ERROR) << output_2;
470 }
471}
472
Andreas Gampe67f02822016-06-24 21:05:23 -0700473class Dex2oatVeryLargeTest : public Dex2oatTest {
474 protected:
475 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100476 CompilerFilter::Filter result ATTRIBUTE_UNUSED) override {
Andreas Gampe67f02822016-06-24 21:05:23 -0700477 // Ignore, we'll do our own checks.
478 }
479
480 void RunTest(CompilerFilter::Filter filter,
481 bool expect_large,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700482 bool expect_downgrade,
Andreas Gampe67f02822016-06-24 21:05:23 -0700483 const std::vector<std::string>& extra_args = {}) {
484 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
485 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700486 std::string app_image_file = GetScratchDir() + "/Test.art";
Andreas Gampe67f02822016-06-24 21:05:23 -0700487
488 Copy(GetDexSrc1(), dex_location);
489
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700490 std::vector<std::string> new_args(extra_args);
491 new_args.push_back("--app-image-file=" + app_image_file);
492 GenerateOdexForTest(dex_location, odex_location, filter, new_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700493
494 CheckValidity();
495 ASSERT_TRUE(success_);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700496 CheckResult(dex_location,
497 odex_location,
498 app_image_file,
499 filter,
500 expect_large,
501 expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700502 }
503
504 void CheckResult(const std::string& dex_location,
505 const std::string& odex_location,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700506 const std::string& app_image_file,
Andreas Gampe67f02822016-06-24 21:05:23 -0700507 CompilerFilter::Filter filter,
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700508 bool expect_large,
509 bool expect_downgrade) {
510 if (expect_downgrade) {
511 EXPECT_TRUE(expect_large);
512 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700513 // Host/target independent checks.
514 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100515 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100516 odex_location.c_str(),
Andreas Gampe67f02822016-06-24 21:05:23 -0700517 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100518 /*executable=*/ false,
519 /*low_4gb=*/ false,
Andreas Gampe67f02822016-06-24 21:05:23 -0700520 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100521 /*reservation=*/ nullptr,
Andreas Gampe67f02822016-06-24 21:05:23 -0700522 &error_msg));
523 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700524 EXPECT_GT(app_image_file.length(), 0u);
525 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700526 if (expect_large) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700527 // Note: we cannot check the following
528 // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
Andreas Gampe67f02822016-06-24 21:05:23 -0700529 // The reason is that the filter override currently happens when the dex files are
530 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
531 // store cannot be changed, and the original filter is set in stone.
532
533 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
534 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
535 ASSERT_TRUE(dex_file != nullptr);
536 uint32_t class_def_count = dex_file->NumClassDefs();
537 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
538 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
539 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
540 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
541 }
542 }
543
544 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100545 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700546 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
547 }
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700548
549 // If expect large, make sure the app image isn't generated or is empty.
550 if (file != nullptr) {
551 EXPECT_EQ(file->GetLength(), 0u);
552 }
Andreas Gampe67f02822016-06-24 21:05:23 -0700553 } else {
554 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700555 ASSERT_TRUE(file != nullptr) << app_image_file;
556 EXPECT_GT(file->GetLength(), 0u);
Andreas Gampe67f02822016-06-24 21:05:23 -0700557 }
558
559 // Host/target dependent checks.
560 if (kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700561 CheckTargetResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700562 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700563 CheckHostResult(expect_downgrade);
Andreas Gampe67f02822016-06-24 21:05:23 -0700564 }
565 }
566
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700567 void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700568 // TODO: Ignore for now. May do something for fd things.
569 }
570
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700571 void CheckHostResult(bool expect_downgrade) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700572 if (!kIsTargetBuild) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700573 if (expect_downgrade) {
574 EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700575 } else {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700576 EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
Andreas Gampe67f02822016-06-24 21:05:23 -0700577 }
578 }
579 }
580
581 // Check whether the dex2oat run was really successful.
582 void CheckValidity() {
583 if (kIsTargetBuild) {
584 CheckTargetValidity();
585 } else {
586 CheckHostValidity();
587 }
588 }
589
590 void CheckTargetValidity() {
591 // TODO: Ignore for now.
592 }
593
594 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
595 void CheckHostValidity() {
596 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
597 }
598};
599
600TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700601 RunTest(CompilerFilter::kAssumeVerified, false, false);
602 RunTest(CompilerFilter::kExtract, false, false);
603 RunTest(CompilerFilter::kQuicken, false, false);
604 RunTest(CompilerFilter::kSpeed, false, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700605
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700606 RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
607 RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
608 RunTest(CompilerFilter::kQuicken, false, false, { "--very-large-app-threshold=10000000" });
609 RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700610}
611
612TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700613 RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
614 RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
615 RunTest(CompilerFilter::kQuicken, true, true, { "--very-large-app-threshold=100" });
616 RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700617}
618
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800619// Regressin test for b/35665292.
620TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
621 // Test that dex2oat doesn't crash with speed-profile but no input profile.
Mathieu Chartier8cce65a2017-08-17 00:06:39 -0700622 RunTest(CompilerFilter::kSpeedProfile, false, false);
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800623}
624
Jeff Hao608f2ce2016-10-19 11:17:11 -0700625class Dex2oatLayoutTest : public Dex2oatTest {
626 protected:
627 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100628 CompilerFilter::Filter result ATTRIBUTE_UNUSED) override {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700629 // Ignore, we'll do our own checks.
630 }
631
Jeff Hao41fba6a2016-11-28 11:53:33 -0800632 // Emits a profile with a single dex file with the given location and a single class index of 1.
633 void GenerateProfile(const std::string& test_profile,
634 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800635 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800636 uint32_t checksum) {
637 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
638 CHECK_GE(profile_test_fd, 0);
639
640 ProfileCompilationInfo info;
641 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800642 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700643 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800644 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800645 bool result = info.Save(profile_test_fd);
646 close(profile_test_fd);
647 ASSERT_TRUE(result);
648 }
649
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800650 void CompileProfileOdex(const std::string& dex_location,
651 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800652 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800653 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800654 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000655 const std::vector<std::string>& extra_args = {},
656 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800657 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800658 const char* location = dex_location.c_str();
659 std::string error_msg;
660 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800661 const ArtDexFileLoader dex_file_loader;
662 ASSERT_TRUE(dex_file_loader.Open(
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100663 location, location, /*verify=*/ true, /*verify_checksum=*/ true, &error_msg, &dex_files));
Jeff Hao41fba6a2016-11-28 11:53:33 -0800664 EXPECT_EQ(dex_files.size(), 1U);
665 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800666 GenerateProfile(profile_location,
667 dex_location,
668 num_profile_classes,
669 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800670 std::vector<std::string> copy(extra_args);
671 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800672 std::unique_ptr<File> app_image_file;
673 if (!app_image_file_name.empty()) {
674 if (use_fd) {
675 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
676 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
677 } else {
678 copy.push_back("--app-image-file=" + app_image_file_name);
679 }
680 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800681 GenerateOdexForTest(dex_location,
682 odex_location,
683 CompilerFilter::kSpeedProfile,
684 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000685 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800686 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800687 if (app_image_file != nullptr) {
688 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
689 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800690 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700691
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100692 uint64_t GetImageObjectSectionSize(const std::string& image_file_name) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800693 EXPECT_FALSE(image_file_name.empty());
694 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
695 CHECK(file != nullptr);
696 ImageHeader image_header;
697 const bool success = file->ReadFully(&image_header, sizeof(image_header));
698 CHECK(success);
699 CHECK(image_header.IsValid());
700 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100701 return image_header.GetObjectsSection().Size();
Mathieu Chartier046854b2017-03-01 17:16:22 -0800702 }
703
704 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800705 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
706 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800707 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800708 Copy(GetDexSrc2(), dex_location);
709
Mathieu Chartier046854b2017-03-01 17:16:22 -0800710 uint64_t image_file_empty_profile = 0;
711 if (app_image) {
712 CompileProfileOdex(dex_location,
713 odex_location,
714 app_image_file,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100715 /*use_fd=*/ false,
716 /*num_profile_classes=*/ 0);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800717 CheckValidity();
718 ASSERT_TRUE(success_);
719 // Don't check the result since CheckResult relies on the class being in the profile.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100720 image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800721 EXPECT_GT(image_file_empty_profile, 0u);
722 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700723
Mathieu Chartier046854b2017-03-01 17:16:22 -0800724 // Small profile.
725 CompileProfileOdex(dex_location,
726 odex_location,
727 app_image_file,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100728 /*use_fd=*/ false,
729 /*num_profile_classes=*/ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700730 CheckValidity();
731 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800732 CheckResult(dex_location, odex_location, app_image_file);
733
734 if (app_image) {
735 // Test that the profile made a difference by adding more classes.
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100736 const uint64_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
737 ASSERT_LT(image_file_empty_profile, image_file_small_profile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800738 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700739 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800740
741 void RunTestVDex() {
742 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
743 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
744 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800745 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800746 Copy(GetDexSrc2(), dex_location);
747
748 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
749 CHECK(vdex_file1 != nullptr) << vdex_location;
750 ScratchFile vdex_file2;
751 {
752 std::string input_vdex = "--input-vdex-fd=-1";
753 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
754 CompileProfileOdex(dex_location,
755 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800756 app_image_file_name,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100757 /*use_fd=*/ true,
758 /*num_profile_classes=*/ 1,
Mathieu Chartierf1609832018-01-31 03:09:56 -0800759 { input_vdex, output_vdex });
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800760 EXPECT_GT(vdex_file1->GetLength(), 0u);
761 }
762 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000763 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800764 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
765 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
766 CompileProfileOdex(dex_location,
767 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800768 app_image_file_name,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100769 /*use_fd=*/ true,
770 /*num_profile_classes=*/ 1,
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800771 { input_vdex, output_vdex },
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100772 /*expect_success=*/ true);
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100773 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800774 }
775 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
776 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100777 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800778 }
779
Mathieu Chartier046854b2017-03-01 17:16:22 -0800780 void CheckResult(const std::string& dex_location,
781 const std::string& odex_location,
782 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700783 // Host/target independent checks.
784 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100785 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100786 odex_location.c_str(),
Jeff Hao608f2ce2016-10-19 11:17:11 -0700787 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100788 /*executable=*/ false,
789 /*low_4gb=*/ false,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700790 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100791 /*reservation=*/ nullptr,
Jeff Hao608f2ce2016-10-19 11:17:11 -0700792 &error_msg));
793 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
794
Jeff Hao042e8982016-10-19 11:17:11 -0700795 const char* location = dex_location.c_str();
796 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800797 const ArtDexFileLoader dex_file_loader;
798 ASSERT_TRUE(dex_file_loader.Open(
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100799 location, location, /*verify=*/ true, /*verify_checksum=*/ true, &error_msg, &dex_files));
Jeff Hao042e8982016-10-19 11:17:11 -0700800 EXPECT_EQ(dex_files.size(), 1U);
801 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
802
Jeff Hao608f2ce2016-10-19 11:17:11 -0700803 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700804 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
805 ASSERT_TRUE(new_dex_file != nullptr);
806 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700807 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700808 ASSERT_GE(class_def_count, 2U);
809
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700810 // Make sure the indexes stay the same.
Jeff Hao042e8982016-10-19 11:17:11 -0700811 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
812 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
813 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
814 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
Mathieu Chartier24066ec2017-10-21 16:01:08 -0700815 EXPECT_EQ(old_class0, new_class0);
816 EXPECT_EQ(old_class1, new_class1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700817 }
818
Jeff Haoc155b052017-01-17 17:43:29 -0800819 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800820
821 if (!app_image_file_name.empty()) {
822 // Go peek at the image header to make sure it was large enough to contain the class.
823 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
824 ImageHeader image_header;
825 bool success = file->ReadFully(&image_header, sizeof(image_header));
826 ASSERT_TRUE(success);
827 ASSERT_TRUE(image_header.IsValid());
Vladimir Markocd87c3e2017-09-05 13:11:57 +0100828 EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800829 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700830 }
831
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800832 // Check whether the dex2oat run was really successful.
833 void CheckValidity() {
834 if (kIsTargetBuild) {
835 CheckTargetValidity();
836 } else {
837 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700838 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800839 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700840
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800841 void CheckTargetValidity() {
842 // TODO: Ignore for now.
843 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700844
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800845 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
846 void CheckHostValidity() {
847 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
848 }
849};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700850
851TEST_F(Dex2oatLayoutTest, TestLayout) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100852 RunTest(/*app_image=*/ false);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800853}
854
855TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100856 RunTest(/*app_image=*/ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700857}
858
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800859TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
860 RunTestVDex();
861}
862
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100863class Dex2oatUnquickenTest : public Dex2oatTest {
864 protected:
865 void RunUnquickenMultiDex() {
866 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
867 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
868 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
869 Copy(GetTestDexFileName("MultiDex"), dex_location);
870
871 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
872 CHECK(vdex_file1 != nullptr) << vdex_location;
873 // Quicken the dex file into a vdex file.
874 {
875 std::string input_vdex = "--input-vdex-fd=-1";
876 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
877 GenerateOdexForTest(dex_location,
878 odex_location,
879 CompilerFilter::kQuicken,
Mathieu Chartierf1609832018-01-31 03:09:56 -0800880 { input_vdex, output_vdex },
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100881 /*expect_success=*/ true,
882 /*use_fd=*/ true);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100883 EXPECT_GT(vdex_file1->GetLength(), 0u);
884 }
885 // Unquicken by running the verify compiler filter on the vdex file.
886 {
887 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
888 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
889 GenerateOdexForTest(dex_location,
890 odex_location,
891 CompilerFilter::kVerify,
Mathieu Chartier02129102017-12-22 11:04:01 -0800892 { input_vdex, output_vdex, kDisableCompactDex },
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100893 /*expect_success=*/ true,
894 /*use_fd=*/ true);
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100895 }
896 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
897 CheckResult(dex_location, odex_location);
898 ASSERT_TRUE(success_);
899 }
900
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800901 void RunUnquickenMultiDexCDex() {
902 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
903 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
904 std::string odex_location2 = GetOdexDir() + "/UnquickenMultiDex2.odex";
905 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
906 std::string vdex_location2 = GetOdexDir() + "/UnquickenMultiDex2.vdex";
907 Copy(GetTestDexFileName("MultiDex"), dex_location);
908
909 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
910 std::unique_ptr<File> vdex_file2(OS::CreateEmptyFile(vdex_location2.c_str()));
911 CHECK(vdex_file1 != nullptr) << vdex_location;
912 CHECK(vdex_file2 != nullptr) << vdex_location2;
913
914 // Quicken the dex file into a vdex file.
915 {
916 std::string input_vdex = "--input-vdex-fd=-1";
917 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
918 GenerateOdexForTest(dex_location,
919 odex_location,
920 CompilerFilter::kQuicken,
921 { input_vdex, output_vdex, "--compact-dex-level=fast"},
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100922 /*expect_success=*/ true,
923 /*use_fd=*/ true);
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800924 EXPECT_GT(vdex_file1->GetLength(), 0u);
925 }
926
927 // Unquicken by running the verify compiler filter on the vdex file.
928 {
929 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
930 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2->Fd());
931 GenerateOdexForTest(dex_location,
932 odex_location2,
933 CompilerFilter::kVerify,
934 { input_vdex, output_vdex, "--compact-dex-level=none"},
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100935 /*expect_success=*/ true,
936 /*use_fd=*/ true);
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800937 }
938 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
939 ASSERT_EQ(vdex_file2->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
940 CheckResult(dex_location, odex_location2);
941 ASSERT_TRUE(success_);
942 }
943
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100944 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
945 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100946 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100947 odex_location.c_str(),
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100948 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100949 /*executable=*/ false,
950 /*low_4gb=*/ false,
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100951 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100952 /*reservation=*/ nullptr,
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100953 &error_msg));
954 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
955 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
956
957 // Iterate over the dex files and ensure there is no quickened instruction.
958 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
959 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
Mathieu Chartier2242ef12018-07-23 18:14:13 -0700960 for (ClassAccessor accessor : dex_file->GetClasses()) {
961 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
962 for (const DexInstructionPcPair& inst : method.GetInstructions()) {
963 ASSERT_FALSE(inst->IsQuickened()) << inst->Opcode() << " " << output_;
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100964 }
965 }
966 }
967 }
968 }
969};
970
971TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
972 RunUnquickenMultiDex();
973}
974
Mathieu Chartierd27923c2018-02-08 21:00:03 -0800975TEST_F(Dex2oatUnquickenTest, UnquickenMultiDexCDex) {
976 RunUnquickenMultiDexCDex();
977}
978
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800979class Dex2oatWatchdogTest : public Dex2oatTest {
980 protected:
981 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
982 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
983 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
984
985 Copy(GetTestDexFileName(), dex_location);
986
987 std::vector<std::string> copy(extra_args);
988
989 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
990 copy.push_back("--swap-file=" + swap_location);
Andreas Gampe22fa3762017-10-23 20:58:12 -0700991 copy.push_back("-j512"); // Excessive idle threads just slow down dex2oat.
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800992 GenerateOdexForTest(dex_location,
993 odex_location,
994 CompilerFilter::kSpeed,
995 copy,
996 expect_success);
997 }
998
999 std::string GetTestDexFileName() {
1000 return GetDexSrc1();
1001 }
1002};
1003
1004TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
1005 // Check with default.
1006 RunTest(true);
1007
1008 // Check with ten minutes.
1009 RunTest(true, { "--watchdog-timeout=600000" });
1010}
1011
1012TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
Andreas Gampe80ddf272018-01-11 09:41:00 -08001013 // The watchdog is independent of dex2oat and will not delete intermediates. It is possible
1014 // that the compilation succeeds and the file is completely written by the time the watchdog
1015 // kills dex2oat (but the dex2oat threads must have been scheduled pretty badly).
1016 test_accepts_odex_file_on_failure = true;
1017
Andreas Gampe2e8a2562017-01-18 20:39:02 -08001018 // Check with ten milliseconds.
1019 RunTest(false, { "--watchdog-timeout=10" });
1020}
1021
Andreas Gampef7882972017-03-20 16:35:24 -07001022class Dex2oatReturnCodeTest : public Dex2oatTest {
1023 protected:
1024 int RunTest(const std::vector<std::string>& extra_args = {}) {
1025 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1026 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1027
1028 Copy(GetTestDexFileName(), dex_location);
1029
1030 std::string error_msg;
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001031 return GenerateOdexForTestWithStatus({dex_location},
Andreas Gampef7882972017-03-20 16:35:24 -07001032 odex_location,
1033 CompilerFilter::kSpeed,
1034 &error_msg,
1035 extra_args);
1036 }
1037
1038 std::string GetTestDexFileName() {
1039 return GetDexSrc1();
1040 }
1041};
1042
1043TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -07001044 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -07001045 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
1046 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
1047}
1048
Calin Juravle1ce70852017-06-28 10:59:03 -07001049class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1050 protected:
1051 void RunTest(const char* class_loader_context,
1052 const char* expected_classpath_key,
1053 bool expected_success,
1054 bool use_second_source = false) {
1055 std::string dex_location = GetUsedDexLocation();
1056 std::string odex_location = GetUsedOatLocation();
1057
1058 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1059
1060 std::string error_msg;
1061 std::vector<std::string> extra_args;
1062 if (class_loader_context != nullptr) {
1063 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1064 }
1065 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1066 ASSERT_TRUE(expected_classpath_key != nullptr);
1067 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1068 ASSERT_TRUE(classpath != nullptr);
1069 ASSERT_STREQ(expected_classpath_key, classpath);
1070 };
1071
1072 GenerateOdexForTest(dex_location,
1073 odex_location,
1074 CompilerFilter::kQuicken,
1075 extra_args,
1076 expected_success,
1077 /*use_fd*/ false,
1078 check_oat);
1079 }
1080
1081 std::string GetUsedDexLocation() {
1082 return GetScratchDir() + "/Context.jar";
1083 }
1084
1085 std::string GetUsedOatLocation() {
1086 return GetOdexDir() + "/Context.odex";
1087 }
1088
Calin Juravle7b0648a2017-07-07 18:40:50 -07001089 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001090};
1091
1092TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1093 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1094}
1095
1096TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1097 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1098}
1099
1100TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1101 RunTest(OatFile::kSpecialSharedLibrary,
1102 OatFile::kSpecialSharedLibrary,
1103 /*expected_success*/ true);
1104}
1105
1106TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1107 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1108 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1109}
1110
1111TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1112 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001113
1114 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001115 std::string expected_classpath_key = "PCL[" +
1116 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001117 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1118}
1119
1120TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1121 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1122 Copy(GetStrippedDexSrc1(), stripped_classpath);
1123
1124 std::string context = "PCL[" + stripped_classpath + "]";
1125 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001126 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001127}
1128
1129TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1130 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1131 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1132
1133 Copy(GetDexSrc1(), stripped_classpath);
1134
1135 GenerateOdexForTest(stripped_classpath,
1136 odex_for_classpath,
1137 CompilerFilter::kQuicken,
1138 {},
1139 true);
1140
1141 // Strip the dex file
1142 Copy(GetStrippedDexSrc1(), stripped_classpath);
1143
1144 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001145 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001146 {
1147 // Open the oat file to get the expected classpath.
Nicolas Geoffray29742602017-12-14 10:09:03 +00001148 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false, false);
Calin Juravle1ce70852017-06-28 10:59:03 -07001149 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1150 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1151 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001152 expected_classpath_key = "PCL[";
1153 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1154 if (i > 0) {
1155 expected_classpath_key + ":";
1156 }
1157 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1158 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1159 }
1160 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001161 }
1162
1163 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001164 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001165 /*expected_success*/ true,
1166 /*use_second_source*/ true);
1167}
1168
1169TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1170 std::string context = "PCL[does_not_exists.dex]";
1171 // Expect an empty context because stripped dex files cannot be open.
1172 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1173}
1174
Calin Juravlec79470d2017-07-12 17:37:42 -07001175TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1176 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1177 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1178
1179 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1180 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1181 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1182 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1183
1184 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1185}
1186
Mathieu Chartier9e050df2017-08-09 10:05:47 -07001187class Dex2oatDeterminism : public Dex2oatTest {};
1188
1189TEST_F(Dex2oatDeterminism, UnloadCompile) {
1190 if (!kUseReadBarrier &&
1191 gc::kCollectorTypeDefault != gc::kCollectorTypeCMS &&
1192 gc::kCollectorTypeDefault != gc::kCollectorTypeMS) {
1193 LOG(INFO) << "Test requires determinism support.";
1194 return;
1195 }
1196 Runtime* const runtime = Runtime::Current();
1197 std::string out_dir = GetScratchDir();
1198 const std::string base_oat_name = out_dir + "/base.oat";
1199 const std::string base_vdex_name = out_dir + "/base.vdex";
1200 const std::string unload_oat_name = out_dir + "/unload.oat";
1201 const std::string unload_vdex_name = out_dir + "/unload.vdex";
1202 const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1203 const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1204 const std::string app_image_name = out_dir + "/unload.art";
1205 std::string error_msg;
1206 const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1207 ASSERT_GT(spaces.size(), 0u);
1208 const std::string image_location = spaces[0]->GetImageLocation();
1209 // Without passing in an app image, it will unload in between compilations.
1210 const int res = GenerateOdexForTestWithStatus(
1211 GetLibCoreDexFileNames(),
1212 base_oat_name,
1213 CompilerFilter::Filter::kQuicken,
1214 &error_msg,
1215 {"--force-determinism", "--avoid-storing-invocation"});
1216 EXPECT_EQ(res, 0);
1217 Copy(base_oat_name, unload_oat_name);
1218 Copy(base_vdex_name, unload_vdex_name);
1219 std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1220 std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1221 ASSERT_TRUE(unload_oat != nullptr);
1222 ASSERT_TRUE(unload_vdex != nullptr);
1223 EXPECT_GT(unload_oat->GetLength(), 0u);
1224 EXPECT_GT(unload_vdex->GetLength(), 0u);
1225 // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1226 // the same.
1227 const int res2 = GenerateOdexForTestWithStatus(
1228 GetLibCoreDexFileNames(),
1229 base_oat_name,
1230 CompilerFilter::Filter::kQuicken,
1231 &error_msg,
1232 {"--force-determinism", "--avoid-storing-invocation", "--app-image-file=" + app_image_name});
1233 EXPECT_EQ(res2, 0);
1234 Copy(base_oat_name, no_unload_oat_name);
1235 Copy(base_vdex_name, no_unload_vdex_name);
1236 std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1237 std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1238 ASSERT_TRUE(no_unload_oat != nullptr);
1239 ASSERT_TRUE(no_unload_vdex != nullptr);
1240 EXPECT_GT(no_unload_oat->GetLength(), 0u);
1241 EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1242 // Verify that both of the files are the same (odex and vdex).
1243 EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1244 EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1245 EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1246 << unload_oat_name << " " << no_unload_oat_name;
1247 EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1248 << unload_vdex_name << " " << no_unload_vdex_name;
1249 // App image file.
1250 std::unique_ptr<File> app_image_file(OS::OpenFileForReading(app_image_name.c_str()));
1251 ASSERT_TRUE(app_image_file != nullptr);
1252 EXPECT_GT(app_image_file->GetLength(), 0u);
1253}
1254
Mathieu Chartier120aa282017-08-05 16:03:03 -07001255// Test that dexlayout section info is correctly written to the oat file for profile based
1256// compilation.
1257TEST_F(Dex2oatTest, LayoutSections) {
1258 using Hotness = ProfileCompilationInfo::MethodHotness;
1259 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1260 ScratchFile profile_file;
1261 // We can only layout method indices with code items, figure out which ones have this property
1262 // first.
1263 std::vector<uint16_t> methods;
1264 {
1265 const DexFile::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1266 dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001267 ClassAccessor accessor(*dex, *dex->FindClassDef(type_idx));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001268 std::set<size_t> code_item_offsets;
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001269 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1270 const uint16_t method_idx = method.GetIndex();
1271 const size_t code_item_offset = method.GetCodeItemOffset();
Mathieu Chartier120aa282017-08-05 16:03:03 -07001272 if (code_item_offsets.insert(code_item_offset).second) {
1273 // Unique code item, add the method index.
1274 methods.push_back(method_idx);
1275 }
1276 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001277 }
1278 ASSERT_GE(methods.size(), 8u);
1279 std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1280 std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1281 std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1282 // Here, we build the profile from the method lists.
1283 ProfileCompilationInfo info;
1284 info.AddMethodsForDex(
1285 static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1286 dex.get(),
1287 hot_methods.begin(),
1288 hot_methods.end());
1289 info.AddMethodsForDex(
1290 Hotness::kFlagStartup,
1291 dex.get(),
1292 startup_methods.begin(),
1293 startup_methods.end());
1294 info.AddMethodsForDex(
1295 Hotness::kFlagPostStartup,
1296 dex.get(),
1297 post_methods.begin(),
1298 post_methods.end());
1299 for (uint16_t id : hot_methods) {
1300 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1301 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1302 }
1303 for (uint16_t id : startup_methods) {
1304 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1305 }
1306 for (uint16_t id : post_methods) {
1307 EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1308 }
1309 // Save the profile since we want to use it with dex2oat to produce an oat file.
1310 ASSERT_TRUE(info.Save(profile_file.GetFd()));
1311 // Generate a profile based odex.
1312 const std::string dir = GetScratchDir();
1313 const std::string oat_filename = dir + "/base.oat";
1314 const std::string vdex_filename = dir + "/base.vdex";
1315 std::string error_msg;
1316 const int res = GenerateOdexForTestWithStatus(
1317 {dex->GetLocation()},
1318 oat_filename,
1319 CompilerFilter::Filter::kQuicken,
1320 &error_msg,
1321 {"--profile-file=" + profile_file.GetFilename()});
1322 EXPECT_EQ(res, 0);
1323
1324 // Open our generated oat file.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001325 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001326 oat_filename.c_str(),
Mathieu Chartier120aa282017-08-05 16:03:03 -07001327 oat_filename.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001328 /*executable=*/ false,
1329 /*low_4gb=*/ false,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001330 dex->GetLocation().c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001331 /*reservation=*/ nullptr,
Mathieu Chartier120aa282017-08-05 16:03:03 -07001332 &error_msg));
1333 ASSERT_TRUE(odex_file != nullptr);
1334 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1335 ASSERT_EQ(oat_dex_files.size(), 1u);
1336 // Check that the code sections match what we expect.
1337 for (const OatDexFile* oat_dex : oat_dex_files) {
1338 const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1339 // Testing of logging the sections.
1340 ASSERT_TRUE(sections != nullptr);
1341 LOG(INFO) << *sections;
1342
1343 // Load the sections into temporary variables for convenience.
1344 const DexLayoutSection& code_section =
1345 sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1346 const DexLayoutSection::Subsection& section_hot_code =
1347 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1348 const DexLayoutSection::Subsection& section_sometimes_used =
1349 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1350 const DexLayoutSection::Subsection& section_startup_only =
1351 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1352 const DexLayoutSection::Subsection& section_unused =
1353 code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1354
1355 // All the sections should be non-empty.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001356 EXPECT_GT(section_hot_code.Size(), 0u);
1357 EXPECT_GT(section_sometimes_used.Size(), 0u);
1358 EXPECT_GT(section_startup_only.Size(), 0u);
1359 EXPECT_GT(section_unused.Size(), 0u);
Mathieu Chartier120aa282017-08-05 16:03:03 -07001360
1361 // Open the dex file since we need to peek at the code items to verify the layout matches what
1362 // we expect.
1363 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1364 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1365 const DexFile::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1366 ASSERT_TRUE(type_id != nullptr);
1367 dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1368 const DexFile::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1369 ASSERT_TRUE(class_def != nullptr);
1370
1371 // Count how many code items are for each category, there should be at least one per category.
1372 size_t hot_count = 0;
1373 size_t post_startup_count = 0;
1374 size_t startup_count = 0;
1375 size_t unused_count = 0;
1376 // Visit all of the methdos of the main class and cross reference the method indices to their
1377 // corresponding code item offsets to verify the layout.
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001378 ClassAccessor accessor(*dex_file, *class_def);
1379 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1380 const size_t method_idx = method.GetIndex();
1381 const size_t code_item_offset = method.GetCodeItemOffset();
Mathieu Chartier120aa282017-08-05 16:03:03 -07001382 const bool is_hot = ContainsElement(hot_methods, method_idx);
1383 const bool is_startup = ContainsElement(startup_methods, method_idx);
1384 const bool is_post_startup = ContainsElement(post_methods, method_idx);
1385 if (is_hot) {
1386 // Hot is highest precedence, check that the hot methods are in the hot section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001387 EXPECT_TRUE(section_hot_code.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001388 ++hot_count;
1389 } else if (is_post_startup) {
1390 // Post startup is sometimes used section.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001391 EXPECT_TRUE(section_sometimes_used.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001392 ++post_startup_count;
1393 } else if (is_startup) {
1394 // Startup at this point means not hot or post startup, these must be startup only then.
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001395 EXPECT_TRUE(section_startup_only.Contains(code_item_offset));
Mathieu Chartier120aa282017-08-05 16:03:03 -07001396 ++startup_count;
1397 } else {
Mathieu Chartier3e0c5172017-11-12 12:58:40 -08001398 if (section_unused.Contains(code_item_offset)) {
Alan Leung9595fd32017-10-17 17:08:19 -07001399 // If no flags are set, the method should be unused ...
1400 ++unused_count;
1401 } else {
1402 // or this method is part of the last code item and the end is 4 byte aligned.
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001403 for (const ClassAccessor::Method& method2 : accessor.GetMethods()) {
1404 EXPECT_LE(method2.GetCodeItemOffset(), code_item_offset);
Alan Leung9595fd32017-10-17 17:08:19 -07001405 }
1406 uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx);
1407 EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u);
1408 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001409 }
1410 }
Mathieu Chartier120aa282017-08-05 16:03:03 -07001411 EXPECT_GT(hot_count, 0u);
1412 EXPECT_GT(post_startup_count, 0u);
1413 EXPECT_GT(startup_count, 0u);
1414 EXPECT_GT(unused_count, 0u);
1415 }
1416}
1417
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001418// Test that generating compact dex works.
1419TEST_F(Dex2oatTest, GenerateCompactDex) {
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001420 // 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";
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001424 const std::string dex_location = GetTestDexFileName("MultiDex");
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001425 std::string error_msg;
1426 const int res = GenerateOdexForTestWithStatus(
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001427 { dex_location },
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001428 oat_filename,
1429 CompilerFilter::Filter::kQuicken,
1430 &error_msg,
1431 {"--compact-dex-level=fast"});
1432 EXPECT_EQ(res, 0);
1433 // Open our generated oat file.
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001434 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001435 oat_filename.c_str(),
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001436 oat_filename.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001437 /*executable=*/ false,
1438 /*low_4gb=*/ false,
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001439 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001440 /*reservation=*/ nullptr,
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001441 &error_msg));
1442 ASSERT_TRUE(odex_file != nullptr);
1443 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001444 ASSERT_GT(oat_dex_files.size(), 1u);
1445 // Check that each dex is a compact dex file.
1446 std::vector<std::unique_ptr<const CompactDexFile>> compact_dex_files;
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001447 for (const OatDexFile* oat_dex : oat_dex_files) {
1448 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1449 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1450 ASSERT_TRUE(dex_file->IsCompactDexFile());
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001451 compact_dex_files.push_back(
1452 std::unique_ptr<const CompactDexFile>(dex_file.release()->AsCompactDexFile()));
1453 }
1454 for (const std::unique_ptr<const CompactDexFile>& dex_file : compact_dex_files) {
1455 // Test that every code item is in the owned section.
1456 const CompactDexFile::Header& header = dex_file->GetHeader();
1457 EXPECT_LE(header.OwnedDataBegin(), header.OwnedDataEnd());
1458 EXPECT_LE(header.OwnedDataBegin(), header.data_size_);
1459 EXPECT_LE(header.OwnedDataEnd(), header.data_size_);
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001460 for (ClassAccessor accessor : dex_file->GetClasses()) {
1461 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1462 if (method.GetCodeItemOffset() != 0u) {
1463 ASSERT_GE(method.GetCodeItemOffset(), header.OwnedDataBegin());
1464 ASSERT_LT(method.GetCodeItemOffset(), header.OwnedDataEnd());
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001465 }
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001466 }
Mathieu Chartierc17b7d82018-03-14 14:00:04 -07001467 }
1468 // Test that the owned sections don't overlap.
1469 for (const std::unique_ptr<const CompactDexFile>& other_dex : compact_dex_files) {
1470 if (dex_file != other_dex) {
1471 ASSERT_TRUE(
1472 (dex_file->GetHeader().OwnedDataBegin() >= other_dex->GetHeader().OwnedDataEnd()) ||
1473 (dex_file->GetHeader().OwnedDataEnd() <= other_dex->GetHeader().OwnedDataBegin()));
1474 }
1475 }
Mathieu Chartier603ccab2017-10-20 14:34:28 -07001476 }
1477}
1478
Andreas Gampef39208f2017-10-19 15:06:59 -07001479class Dex2oatVerifierAbort : public Dex2oatTest {};
1480
1481TEST_F(Dex2oatVerifierAbort, HardFail) {
1482 // Use VerifierDeps as it has hard-failing classes.
1483 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps"));
1484 std::string out_dir = GetScratchDir();
1485 const std::string base_oat_name = out_dir + "/base.oat";
1486 std::string error_msg;
1487 const int res_fail = GenerateOdexForTestWithStatus(
1488 {dex->GetLocation()},
1489 base_oat_name,
1490 CompilerFilter::Filter::kQuicken,
1491 &error_msg,
1492 {"--abort-on-hard-verifier-error"});
1493 EXPECT_NE(0, res_fail);
1494
1495 const int res_no_fail = GenerateOdexForTestWithStatus(
1496 {dex->GetLocation()},
1497 base_oat_name,
1498 CompilerFilter::Filter::kQuicken,
1499 &error_msg,
1500 {"--no-abort-on-hard-verifier-error"});
1501 EXPECT_EQ(0, res_no_fail);
1502}
1503
1504TEST_F(Dex2oatVerifierAbort, SoftFail) {
1505 // Use VerifierDepsMulti as it has hard-failing classes.
1506 std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDepsMulti"));
1507 std::string out_dir = GetScratchDir();
1508 const std::string base_oat_name = out_dir + "/base.oat";
1509 std::string error_msg;
1510 const int res_fail = GenerateOdexForTestWithStatus(
1511 {dex->GetLocation()},
1512 base_oat_name,
1513 CompilerFilter::Filter::kQuicken,
1514 &error_msg,
1515 {"--abort-on-soft-verifier-error"});
1516 EXPECT_NE(0, res_fail);
1517
1518 const int res_no_fail = GenerateOdexForTestWithStatus(
1519 {dex->GetLocation()},
1520 base_oat_name,
1521 CompilerFilter::Filter::kQuicken,
1522 &error_msg,
1523 {"--no-abort-on-soft-verifier-error"});
1524 EXPECT_EQ(0, res_no_fail);
1525}
1526
Andreas Gampecac31ad2017-11-06 20:01:17 -08001527class Dex2oatDedupeCode : public Dex2oatTest {};
1528
1529TEST_F(Dex2oatDedupeCode, DedupeTest) {
1530 // Use MyClassNatives. It has lots of native methods that will produce deduplicate-able code.
1531 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MyClassNatives"));
1532 std::string out_dir = GetScratchDir();
1533 const std::string base_oat_name = out_dir + "/base.oat";
1534 size_t no_dedupe_size = 0;
1535 GenerateOdexForTest(dex->GetLocation(),
1536 base_oat_name,
1537 CompilerFilter::Filter::kSpeed,
1538 { "--deduplicate-code=false" },
1539 true, // expect_success
1540 false, // use_fd
1541 [&no_dedupe_size](const OatFile& o) {
1542 no_dedupe_size = o.Size();
1543 });
1544
1545 size_t dedupe_size = 0;
1546 GenerateOdexForTest(dex->GetLocation(),
1547 base_oat_name,
1548 CompilerFilter::Filter::kSpeed,
1549 { "--deduplicate-code=true" },
1550 true, // expect_success
1551 false, // use_fd
1552 [&dedupe_size](const OatFile& o) {
1553 dedupe_size = o.Size();
1554 });
1555
1556 EXPECT_LT(dedupe_size, no_dedupe_size);
1557}
1558
Nicolas Geoffrayf3075272018-01-08 12:41:19 +00001559TEST_F(Dex2oatTest, UncompressedTest) {
1560 std::unique_ptr<const DexFile> dex(OpenTestDexFile("MainUncompressed"));
1561 std::string out_dir = GetScratchDir();
1562 const std::string base_oat_name = out_dir + "/base.oat";
1563 GenerateOdexForTest(dex->GetLocation(),
1564 base_oat_name,
1565 CompilerFilter::Filter::kQuicken,
1566 { },
1567 true, // expect_success
1568 false, // use_fd
1569 [](const OatFile& o) {
1570 CHECK(!o.ContainsDexCode());
1571 });
1572}
1573
Mathieu Chartier700a9852018-02-06 18:27:38 -08001574TEST_F(Dex2oatTest, EmptyUncompressedDexTest) {
1575 std::string out_dir = GetScratchDir();
1576 const std::string base_oat_name = out_dir + "/base.oat";
1577 std::string error_msg;
1578 int status = GenerateOdexForTestWithStatus(
1579 { GetTestDexFileName("MainEmptyUncompressed") },
1580 base_oat_name,
1581 CompilerFilter::Filter::kQuicken,
1582 &error_msg,
1583 { },
1584 /*use_fd*/ false);
1585 // Expect to fail with code 1 and not SIGSEGV or SIGABRT.
1586 ASSERT_TRUE(WIFEXITED(status));
1587 ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg;
1588}
1589
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001590// Dex file that has duplicate methods have different code items and debug info.
1591static const char kDuplicateMethodInputDex[] =
1592 "ZGV4CjAzOQDEy8VPdj4qHpgPYFWtLCtOykfFP4kB8tGYDAAAcAAAAHhWNBIAAAAAAAAAANALAABI"
1593 "AAAAcAAAAA4AAACQAQAABQAAAMgBAAANAAAABAIAABkAAABsAgAABAAAADQDAADgCAAAuAMAADgI"
1594 "AABCCAAASggAAE8IAABcCAAAaggAAHkIAACICAAAlggAAKQIAACyCAAAwAgAAM4IAADcCAAA6ggA"
1595 "APgIAAD7CAAA/wgAABcJAAAuCQAARQkAAFQJAAB4CQAAmAkAALsJAADSCQAA5gkAAPoJAAAVCgAA"
1596 "KQoAADsKAABCCgAASgoAAFIKAABbCgAAZAoAAGwKAAB0CgAAfAoAAIQKAACMCgAAlAoAAJwKAACk"
1597 "CgAArQoAALcKAADACgAAwwoAAMcKAADcCgAA6QoAAPEKAAD3CgAA/QoAAAMLAAAJCwAAEAsAABcL"
1598 "AAAdCwAAIwsAACkLAAAvCwAANQsAADsLAABBCwAARwsAAE0LAABSCwAAWwsAAF4LAABoCwAAbwsA"
1599 "ABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAC4AAAAwAAAA"
1600 "DwAAAAkAAAAAAAAAEAAAAAoAAACoBwAALgAAAAwAAAAAAAAALwAAAAwAAACoBwAALwAAAAwAAACw"
1601 "BwAAAgAJADUAAAACAAkANgAAAAIACQA3AAAAAgAJADgAAAACAAkAOQAAAAIACQA6AAAAAgAJADsA"
1602 "AAACAAkAPAAAAAIACQA9AAAAAgAJAD4AAAACAAkAPwAAAAIACQBAAAAACwAHAEIAAAAAAAIAAQAA"
1603 "AAAAAwAeAAAAAQACAAEAAAABAAMAHgAAAAIAAgAAAAAAAgACAAEAAAADAAIAAQAAAAMAAgAfAAAA"
1604 "AwACACAAAAADAAIAIQAAAAMAAgAiAAAAAwACACMAAAADAAIAJAAAAAMAAgAlAAAAAwACACYAAAAD"
1605 "AAIAJwAAAAMAAgAoAAAAAwACACkAAAADAAIAKgAAAAMABAA0AAAABwADAEMAAAAIAAIAAQAAAAoA"
1606 "AgABAAAACgABADIAAAAKAAAARQAAAAAAAAAAAAAACAAAAAAAAAAdAAAAaAcAALYHAAAAAAAAAQAA"
1607 "AAAAAAAIAAAAAAAAAB0AAAB4BwAAxAcAAAAAAAACAAAAAAAAAAgAAAAAAAAAHQAAAIgHAADSBwAA"
1608 "AAAAAAMAAAAAAAAACAAAAAAAAAAdAAAAmAcAAPoHAAAAAAAAAAAAAAEAAAAAAAAArAYAADEAAAAa"
1609 "AAMAaQAAABoABABpAAEAGgAHAGkABAAaAAgAaQAFABoACQBpAAYAGgAKAGkABwAaAAsAaQAIABoA"
1610 "DABpAAkAGgANAGkACgAaAA4AaQALABoABQBpAAIAGgAGAGkAAwAOAAAAAQABAAEAAACSBgAABAAA"
1611 "AHAQFQAAAA4ABAABAAIAAACWBgAAFwAAAGIADAAiAQoAcBAWAAEAGgICAG4gFwAhAG4gFwAxAG4Q"
1612 "GAABAAwBbiAUABAADgAAAAEAAQABAAAAngYAAAQAAABwEBUAAAAOAAIAAQACAAAAogYAAAYAAABi"
1613 "AAwAbiAUABAADgABAAEAAQAAAKgGAAAEAAAAcBAVAAAADgABAAEAAQAAALsGAAAEAAAAcBAVAAAA"
1614 "DgABAAAAAQAAAL8GAAAGAAAAYgAAAHEQAwAAAA4AAQAAAAEAAADEBgAABgAAAGIAAQBxEAMAAAAO"
1615 "AAEAAAABAAAA8QYAAAYAAABiAAIAcRABAAAADgABAAAAAQAAAPYGAAAGAAAAYgADAHEQAwAAAA4A"
1616 "AQAAAAEAAADJBgAABgAAAGIABABxEAMAAAAOAAEAAAABAAAAzgYAAAYAAABiAAEAcRADAAAADgAB"
1617 "AAAAAQAAANMGAAAGAAAAYgAGAHEQAwAAAA4AAQAAAAEAAADYBgAABgAAAGIABwBxEAMAAAAOAAEA"
1618 "AAABAAAA3QYAAAYAAABiAAgAcRABAAAADgABAAAAAQAAAOIGAAAGAAAAYgAJAHEQAwAAAA4AAQAA"
1619 "AAEAAADnBgAABgAAAGIACgBxEAMAAAAOAAEAAAABAAAA7AYAAAYAAABiAAsAcRABAAAADgABAAEA"
1620 "AAAAAPsGAAAlAAAAcQAHAAAAcQAIAAAAcQALAAAAcQAMAAAAcQANAAAAcQAOAAAAcQAPAAAAcQAQ"
1621 "AAAAcQARAAAAcQASAAAAcQAJAAAAcQAKAAAADgAnAA4AKQFFDgEWDwAhAA4AIwFFDloAEgAOABMA"
1622 "DktLS0tLS0tLS0tLABEADgAuAA5aADIADloANgAOWgA6AA5aAD4ADloAQgAOWgBGAA5aAEoADloA"
1623 "TgAOWgBSAA5aAFYADloAWgAOWgBeATQOPDw8PDw8PDw8PDw8AAIEAUYYAwIFAjEECEEXLAIFAjEE"
1624 "CEEXKwIFAjEECEEXLQIGAUYcAxgAGAEYAgAAAAIAAAAMBwAAEgcAAAIAAAAMBwAAGwcAAAIAAAAM"
1625 "BwAAJAcAAAEAAAAtBwAAPAcAAAAAAAAAAAAAAAAAAEgHAAAAAAAAAAAAAAAAAABUBwAAAAAAAAAA"
1626 "AAAAAAAAYAcAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAQAAAA0AAAACAACAgASsCAEIxAgAAAIAAoCA"
1627 "BIQJAQicCQwAAgAACQEJAQkBCQEJAQkBCQEJAQkBCQEJAQkEiIAEuAcBgIAEuAkAAA4ABoCABNAJ"
1628 "AQnoCQAJhAoACaAKAAm8CgAJ2AoACfQKAAmQCwAJrAsACcgLAAnkCwAJgAwACZwMAAm4DAg8Y2xp"
1629 "bml0PgAGPGluaXQ+AANBQUEAC0hlbGxvIFdvcmxkAAxIZWxsbyBXb3JsZDEADUhlbGxvIFdvcmxk"
1630 "MTAADUhlbGxvIFdvcmxkMTEADEhlbGxvIFdvcmxkMgAMSGVsbG8gV29ybGQzAAxIZWxsbyBXb3Js"
1631 "ZDQADEhlbGxvIFdvcmxkNQAMSGVsbG8gV29ybGQ2AAxIZWxsbyBXb3JsZDcADEhlbGxvIFdvcmxk"
1632 "OAAMSGVsbG8gV29ybGQ5AAFMAAJMTAAWTE1hbnlNZXRob2RzJFByaW50ZXIyOwAVTE1hbnlNZXRo"
1633 "b2RzJFByaW50ZXI7ABVMTWFueU1ldGhvZHMkU3RyaW5nczsADUxNYW55TWV0aG9kczsAIkxkYWx2"
1634 "aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5lckNs"
1635 "YXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABVMamF2YS9pby9QcmludFN0"
1636 "cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABlMamF2YS9sYW5n"
1637 "L1N0cmluZ0J1aWxkZXI7ABJMamF2YS9sYW5nL1N5c3RlbTsAEE1hbnlNZXRob2RzLmphdmEABVBy"
1638 "aW50AAZQcmludDAABlByaW50MQAHUHJpbnQxMAAHUHJpbnQxMQAGUHJpbnQyAAZQcmludDMABlBy"
1639 "aW50NAAGUHJpbnQ1AAZQcmludDYABlByaW50NwAGUHJpbnQ4AAZQcmludDkAB1ByaW50ZXIACFBy"
1640 "aW50ZXIyAAdTdHJpbmdzAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwALYWNjZXNzRmxhZ3MA"
1641 "BmFwcGVuZAAEYXJncwAEbWFpbgAEbXNnMAAEbXNnMQAFbXNnMTAABW1zZzExAARtc2cyAARtc2cz"
1642 "AARtc2c0AARtc2c1AARtc2c2AARtc2c3AARtc2c4AARtc2c5AARuYW1lAANvdXQAB3ByaW50bG4A"
1643 "AXMACHRvU3RyaW5nAAV2YWx1ZQBffn5EOHsibWluLWFwaSI6MTAwMDAsInNoYS0xIjoiZmViODZj"
1644 "MDA2ZWZhY2YxZDc5ODRiODVlMTc5MGZlZjdhNzY3YWViYyIsInZlcnNpb24iOiJ2MS4xLjUtZGV2"
1645 "In0AEAAAAAAAAAABAAAAAAAAAAEAAABIAAAAcAAAAAIAAAAOAAAAkAEAAAMAAAAFAAAAyAEAAAQA"
1646 "AAANAAAABAIAAAUAAAAZAAAAbAIAAAYAAAAEAAAANAMAAAEgAAAUAAAAuAMAAAMgAAAUAAAAkgYA"
1647 "AAQgAAAFAAAADAcAAAMQAAAEAAAAOQcAAAYgAAAEAAAAaAcAAAEQAAACAAAAqAcAAAAgAAAEAAAA"
1648 "tgcAAAIgAABIAAAAOAgAAAAQAAABAAAA0AsAAAAAAAA=";
1649
1650static void WriteBase64ToFile(const char* base64, File* file) {
1651 // Decode base64.
1652 CHECK(base64 != nullptr);
1653 size_t length;
1654 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
1655 CHECK(bytes != nullptr);
1656 if (!file->WriteFully(bytes.get(), length)) {
1657 PLOG(FATAL) << "Failed to write base64 as file";
1658 }
1659}
1660
1661TEST_F(Dex2oatTest, CompactDexGenerationFailure) {
1662 ScratchFile temp_dex;
1663 WriteBase64ToFile(kDuplicateMethodInputDex, temp_dex.GetFile());
1664 std::string out_dir = GetScratchDir();
1665 const std::string oat_filename = out_dir + "/base.oat";
1666 // The dex won't pass the method verifier, only use the verify filter.
1667 GenerateOdexForTest(temp_dex.GetFilename(),
1668 oat_filename,
1669 CompilerFilter::Filter::kVerify,
1670 { },
1671 true, // expect_success
1672 false, // use_fd
1673 [](const OatFile& o) {
1674 CHECK(o.ContainsDexCode());
1675 });
1676 // Open our generated oat file.
1677 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001678 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001679 oat_filename.c_str(),
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001680 oat_filename.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001681 /*executable=*/ false,
1682 /*low_4gb=*/ false,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001683 temp_dex.GetFilename().c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001684 /*reservation=*/ nullptr,
Mathieu Chartier05f90d12018-02-07 13:47:17 -08001685 &error_msg));
1686 ASSERT_TRUE(odex_file != nullptr);
1687 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1688 ASSERT_EQ(oat_dex_files.size(), 1u);
1689 // The dexes should have failed to convert to compact dex.
1690 for (const OatDexFile* oat_dex : oat_dex_files) {
1691 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1692 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1693 ASSERT_TRUE(!dex_file->IsCompactDexFile());
1694 }
1695}
1696
Mathieu Chartiercda83be2018-03-01 23:55:55 -08001697TEST_F(Dex2oatTest, CompactDexGenerationFailureMultiDex) {
1698 // Create a multidex file with only one dex that gets rejected for cdex conversion.
1699 ScratchFile apk_file;
1700 {
Josh Gao7f00f5a2018-08-30 17:26:49 -07001701 FILE* file = fdopen(dup(apk_file.GetFd()), "w+b");
Mathieu Chartiercda83be2018-03-01 23:55:55 -08001702 ZipWriter writer(file);
1703 // Add vdex to zip.
1704 writer.StartEntry("classes.dex", ZipWriter::kCompress);
1705 size_t length = 0u;
1706 std::unique_ptr<uint8_t[]> bytes(DecodeBase64(kDuplicateMethodInputDex, &length));
1707 ASSERT_GE(writer.WriteBytes(&bytes[0], length), 0);
1708 writer.FinishEntry();
1709 writer.StartEntry("classes2.dex", ZipWriter::kCompress);
1710 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1711 ASSERT_GE(writer.WriteBytes(dex->Begin(), dex->Size()), 0);
1712 writer.FinishEntry();
1713 writer.Finish();
1714 ASSERT_EQ(apk_file.GetFile()->Flush(), 0);
1715 }
Andreas Gampebc802de2018-06-20 17:24:11 -07001716 const std::string& dex_location = apk_file.GetFilename();
Mathieu Chartiercda83be2018-03-01 23:55:55 -08001717 const std::string odex_location = GetOdexDir() + "/output.odex";
1718 GenerateOdexForTest(dex_location,
1719 odex_location,
1720 CompilerFilter::kQuicken,
1721 { "--compact-dex-level=fast" },
1722 true);
1723}
1724
Andreas Gampe25419b52018-02-08 21:30:26 -08001725TEST_F(Dex2oatTest, StderrLoggerOutput) {
1726 std::string dex_location = GetScratchDir() + "/Dex2OatStderrLoggerTest.jar";
1727 std::string odex_location = GetOdexDir() + "/Dex2OatStderrLoggerTest.odex";
1728
1729 // Test file doesn't matter.
1730 Copy(GetDexSrc1(), dex_location);
1731
1732 GenerateOdexForTest(dex_location,
1733 odex_location,
1734 CompilerFilter::kQuicken,
1735 { "--runtime-arg", "-Xuse-stderr-logger" },
1736 true);
1737 // Look for some random part of dex2oat logging. With the stderr logger this should be captured,
1738 // even on device.
1739 EXPECT_NE(std::string::npos, output_.find("dex2oat took"));
1740}
1741
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001742TEST_F(Dex2oatTest, VerifyCompilationReason) {
1743 std::string dex_location = GetScratchDir() + "/Dex2OatCompilationReason.jar";
1744 std::string odex_location = GetOdexDir() + "/Dex2OatCompilationReason.odex";
1745
1746 // Test file doesn't matter.
1747 Copy(GetDexSrc1(), dex_location);
1748
1749 GenerateOdexForTest(dex_location,
1750 odex_location,
1751 CompilerFilter::kVerify,
1752 { "--compilation-reason=install" },
1753 true);
1754 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001755 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001756 odex_location.c_str(),
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001757 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001758 /*executable=*/ false,
1759 /*low_4gb=*/ false,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001760 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001761 /*reservation=*/ nullptr,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001762 &error_msg));
1763 ASSERT_TRUE(odex_file != nullptr);
1764 ASSERT_STREQ("install", odex_file->GetCompilationReason());
1765}
1766
1767TEST_F(Dex2oatTest, VerifyNoCompilationReason) {
1768 std::string dex_location = GetScratchDir() + "/Dex2OatNoCompilationReason.jar";
1769 std::string odex_location = GetOdexDir() + "/Dex2OatNoCompilationReason.odex";
1770
1771 // Test file doesn't matter.
1772 Copy(GetDexSrc1(), dex_location);
1773
1774 GenerateOdexForTest(dex_location,
1775 odex_location,
1776 CompilerFilter::kVerify,
1777 {},
1778 true);
1779 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001780 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001781 odex_location.c_str(),
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001782 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001783 /*executable=*/ false,
1784 /*low_4gb=*/ false,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001785 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001786 /*reservation=*/ nullptr,
Calin Juravle0e09dfc2018-02-12 19:01:09 -08001787 &error_msg));
1788 ASSERT_TRUE(odex_file != nullptr);
1789 ASSERT_EQ(nullptr, odex_file->GetCompilationReason());
1790}
1791
Mathieu Chartier792111c2018-02-15 13:02:15 -08001792TEST_F(Dex2oatTest, DontExtract) {
1793 std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1794 std::string error_msg;
1795 const std::string out_dir = GetScratchDir();
1796 const std::string dex_location = dex->GetLocation();
1797 const std::string odex_location = out_dir + "/base.oat";
1798 const std::string vdex_location = out_dir + "/base.vdex";
1799 GenerateOdexForTest(dex_location,
1800 odex_location,
1801 CompilerFilter::Filter::kVerify,
1802 { "--copy-dex-files=false" },
1803 true, // expect_success
1804 false, // use_fd
1805 [](const OatFile&) {
1806 });
1807 {
1808 // Check the vdex doesn't have dex.
1809 std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001810 /*writable=*/ false,
1811 /*low_4gb=*/ false,
1812 /*unquicken=*/ false,
Mathieu Chartier792111c2018-02-15 13:02:15 -08001813 &error_msg));
1814 ASSERT_TRUE(vdex != nullptr);
Nicolas Geoffray3a293552018-03-02 10:52:16 +00001815 EXPECT_FALSE(vdex->HasDexSection()) << output_;
Mathieu Chartier792111c2018-02-15 13:02:15 -08001816 }
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001817 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01001818 odex_location.c_str(),
Mathieu Chartier792111c2018-02-15 13:02:15 -08001819 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001820 /*executable=*/ false,
1821 /*low_4gb=*/ false,
Mathieu Chartier792111c2018-02-15 13:02:15 -08001822 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001823 /*reservation=*/ nullptr,
Mathieu Chartier792111c2018-02-15 13:02:15 -08001824 &error_msg));
1825 ASSERT_TRUE(odex_file != nullptr) << dex_location;
1826 std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1827 ASSERT_EQ(oat_dex_files.size(), 1u);
1828 // Verify that the oat file can still open the dex files.
1829 for (const OatDexFile* oat_dex : oat_dex_files) {
1830 std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1831 ASSERT_TRUE(dex_file != nullptr) << error_msg;
1832 }
1833 // Create a dm file and use it to verify.
1834 // Add produced artifacts to a zip file that doesn't contain the classes.dex.
1835 ScratchFile dm_file;
1836 {
1837 std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_location.c_str()));
1838 ASSERT_TRUE(vdex_file != nullptr);
1839 ASSERT_GT(vdex_file->GetLength(), 0u);
Josh Gao7f00f5a2018-08-30 17:26:49 -07001840 FILE* file = fdopen(dup(dm_file.GetFd()), "w+b");
Mathieu Chartier792111c2018-02-15 13:02:15 -08001841 ZipWriter writer(file);
1842 auto write_all_bytes = [&](File* file) {
1843 std::unique_ptr<uint8_t[]> bytes(new uint8_t[file->GetLength()]);
1844 ASSERT_TRUE(file->ReadFully(&bytes[0], file->GetLength()));
1845 ASSERT_GE(writer.WriteBytes(&bytes[0], file->GetLength()), 0);
1846 };
1847 // Add vdex to zip.
1848 writer.StartEntry(VdexFile::kVdexNameInDmFile, ZipWriter::kCompress);
1849 write_all_bytes(vdex_file.get());
1850 writer.FinishEntry();
1851 writer.Finish();
1852 ASSERT_EQ(dm_file.GetFile()->Flush(), 0);
1853 }
1854
Mathieu Chartier2eabc612018-05-25 14:31:16 -07001855 auto generate_and_check = [&](CompilerFilter::Filter filter) {
1856 GenerateOdexForTest(dex_location,
1857 odex_location,
1858 filter,
1859 { "--dump-timings",
1860 "--dm-file=" + dm_file.GetFilename(),
1861 // Pass -Xuse-stderr-logger have dex2oat output in output_ on target.
1862 "--runtime-arg",
1863 "-Xuse-stderr-logger" },
1864 true, // expect_success
1865 false, // use_fd
1866 [](const OatFile& o) {
1867 CHECK(o.ContainsDexCode());
1868 });
1869 // Check the output for "Fast verify", this is printed from --dump-timings.
1870 std::istringstream iss(output_);
1871 std::string line;
1872 bool found_fast_verify = false;
1873 const std::string kFastVerifyString = "Fast Verify";
1874 while (std::getline(iss, line) && !found_fast_verify) {
1875 found_fast_verify = found_fast_verify || line.find(kFastVerifyString) != std::string::npos;
1876 }
1877 EXPECT_TRUE(found_fast_verify) << "Expected to find " << kFastVerifyString << "\n" << output_;
1878 };
1879
Mathieu Chartier792111c2018-02-15 13:02:15 -08001880 // Generate a quickened dex by using the input dm file to verify.
Mathieu Chartier2eabc612018-05-25 14:31:16 -07001881 generate_and_check(CompilerFilter::Filter::kQuicken);
1882 // Use verify compiler filter to sanity check that FastVerify works for that filter too.
1883 generate_and_check(CompilerFilter::Filter::kVerify);
Mathieu Chartier792111c2018-02-15 13:02:15 -08001884}
1885
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001886// Test that dex files with quickened opcodes aren't dequickened.
1887TEST_F(Dex2oatTest, QuickenedInput) {
1888 std::string error_msg;
1889 ScratchFile temp_dex;
1890 MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("ManyMethods"), [] (DexFile* dex) {
1891 bool mutated_successfully = false;
1892 // Change the dex instructions to make an opcode that spans past the end of the code item.
Mathieu Chartier2242ef12018-07-23 18:14:13 -07001893 for (ClassAccessor accessor : dex->GetClasses()) {
1894 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1895 CodeItemInstructionAccessor instructions = method.GetInstructions();
1896 // Make a quickened instruction that doesn't run past the end of the code item.
1897 if (instructions.InsnsSizeInCodeUnits() > 2) {
1898 const_cast<Instruction&>(instructions.InstructionAt(0)).SetOpcode(
1899 Instruction::IGET_BYTE_QUICK);
1900 mutated_successfully = true;
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001901 }
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001902 }
1903 }
1904 CHECK(mutated_successfully)
1905 << "Failed to find candidate code item with only one code unit in last instruction.";
1906 });
1907
Andreas Gampebc802de2018-06-20 17:24:11 -07001908 const std::string& dex_location = temp_dex.GetFilename();
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001909 std::string odex_location = GetOdexDir() + "/quickened.odex";
1910 std::string vdex_location = GetOdexDir() + "/quickened.vdex";
1911 std::unique_ptr<File> vdex_output(OS::CreateEmptyFile(vdex_location.c_str()));
1912 // Quicken the dex
1913 {
1914 std::string input_vdex = "--input-vdex-fd=-1";
1915 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_output->Fd());
1916 GenerateOdexForTest(dex_location,
1917 odex_location,
1918 CompilerFilter::kQuicken,
1919 // Disable cdex since we want to compare against the original dex file
1920 // after unquickening.
1921 { input_vdex, output_vdex, kDisableCompactDex },
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001922 /*expect_success=*/ true,
1923 /*use_fd=*/ true);
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001924 }
1925 // Unquicken by running the verify compiler filter on the vdex file and verify it matches.
1926 std::string odex_location2 = GetOdexDir() + "/unquickened.odex";
1927 std::string vdex_location2 = GetOdexDir() + "/unquickened.vdex";
1928 std::unique_ptr<File> vdex_unquickened(OS::CreateEmptyFile(vdex_location2.c_str()));
1929 {
1930 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_output->Fd());
1931 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_unquickened->Fd());
1932 GenerateOdexForTest(dex_location,
1933 odex_location2,
1934 CompilerFilter::kVerify,
1935 // Disable cdex to avoid needing to write out the shared section.
1936 { input_vdex, output_vdex, kDisableCompactDex },
Vladimir Markof4efa9e2018-10-17 14:12:45 +01001937 /*expect_success=*/ true,
1938 /*use_fd=*/ true);
Mathieu Chartier2daa1342018-02-20 16:19:28 -08001939 }
1940 ASSERT_EQ(vdex_unquickened->Flush(), 0) << "Could not flush and close vdex file";
1941 ASSERT_TRUE(success_);
1942 {
1943 // Check that hte vdex has one dex and compare it to the original one.
1944 std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_location2.c_str(),
1945 /*writable*/ false,
1946 /*low_4gb*/ false,
1947 /*unquicken*/ false,
1948 &error_msg));
1949 std::vector<std::unique_ptr<const DexFile>> dex_files;
1950 bool result = vdex->OpenAllDexFiles(&dex_files, &error_msg);
1951 ASSERT_TRUE(result) << error_msg;
1952 ASSERT_EQ(dex_files.size(), 1u) << error_msg;
1953 ScratchFile temp;
1954 ASSERT_TRUE(temp.GetFile()->WriteFully(dex_files[0]->Begin(), dex_files[0]->Size()));
1955 ASSERT_EQ(temp.GetFile()->Flush(), 0) << "Could not flush extracted dex";
1956 EXPECT_EQ(temp.GetFile()->Compare(temp_dex.GetFile()), 0);
1957 }
1958 ASSERT_EQ(vdex_output->FlushCloseOrErase(), 0) << "Could not flush and close";
1959 ASSERT_EQ(vdex_unquickened->FlushCloseOrErase(), 0) << "Could not flush and close";
1960}
1961
Mathieu Chartierd45863a2018-03-21 18:16:36 -07001962// Test that compact dex generation with invalid dex files doesn't crash dex2oat. b/75970654
1963TEST_F(Dex2oatTest, CompactDexInvalidSource) {
1964 ScratchFile invalid_dex;
1965 {
Josh Gao7f00f5a2018-08-30 17:26:49 -07001966 FILE* file = fdopen(dup(invalid_dex.GetFd()), "w+b");
Mathieu Chartierd45863a2018-03-21 18:16:36 -07001967 ZipWriter writer(file);
1968 writer.StartEntry("classes.dex", ZipWriter::kAlign32);
1969 DexFile::Header header = {};
1970 StandardDexFile::WriteMagic(header.magic_);
1971 StandardDexFile::WriteCurrentVersion(header.magic_);
1972 header.file_size_ = 4 * KB;
1973 header.data_size_ = 4 * KB;
1974 header.data_off_ = 10 * MB;
1975 header.map_off_ = 10 * MB;
1976 header.class_defs_off_ = 10 * MB;
1977 header.class_defs_size_ = 10000;
1978 ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
1979 writer.FinishEntry();
1980 writer.Finish();
1981 ASSERT_EQ(invalid_dex.GetFile()->Flush(), 0);
1982 }
Andreas Gampebc802de2018-06-20 17:24:11 -07001983 const std::string& dex_location = invalid_dex.GetFilename();
Mathieu Chartierd45863a2018-03-21 18:16:36 -07001984 const std::string odex_location = GetOdexDir() + "/output.odex";
1985 std::string error_msg;
1986 int status = GenerateOdexForTestWithStatus(
1987 {dex_location},
1988 odex_location,
1989 CompilerFilter::kQuicken,
1990 &error_msg,
1991 { "--compact-dex-level=fast" });
1992 ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
1993}
1994
Mathieu Chartier14e7bad2018-03-22 14:33:20 -07001995// Test that dex2oat with a CompactDex file in the APK fails.
1996TEST_F(Dex2oatTest, CompactDexInZip) {
1997 CompactDexFile::Header header = {};
1998 CompactDexFile::WriteMagic(header.magic_);
1999 CompactDexFile::WriteCurrentVersion(header.magic_);
2000 header.file_size_ = sizeof(CompactDexFile::Header);
2001 header.data_off_ = 10 * MB;
2002 header.map_off_ = 10 * MB;
2003 header.class_defs_off_ = 10 * MB;
2004 header.class_defs_size_ = 10000;
2005 // Create a zip containing the invalid dex.
2006 ScratchFile invalid_dex_zip;
2007 {
Josh Gao7f00f5a2018-08-30 17:26:49 -07002008 FILE* file = fdopen(dup(invalid_dex_zip.GetFd()), "w+b");
Mathieu Chartier14e7bad2018-03-22 14:33:20 -07002009 ZipWriter writer(file);
2010 writer.StartEntry("classes.dex", ZipWriter::kCompress);
2011 ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
2012 writer.FinishEntry();
2013 writer.Finish();
2014 ASSERT_EQ(invalid_dex_zip.GetFile()->Flush(), 0);
2015 }
2016 // Create the dex file directly.
2017 ScratchFile invalid_dex;
2018 {
2019 ASSERT_GE(invalid_dex.GetFile()->WriteFully(&header, sizeof(header)), 0);
2020 ASSERT_EQ(invalid_dex.GetFile()->Flush(), 0);
2021 }
2022 std::string error_msg;
2023 int status = 0u;
2024
2025 status = GenerateOdexForTestWithStatus(
2026 { invalid_dex_zip.GetFilename() },
2027 GetOdexDir() + "/output_apk.odex",
2028 CompilerFilter::kQuicken,
2029 &error_msg,
2030 { "--compact-dex-level=fast" });
2031 ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
2032
2033 status = GenerateOdexForTestWithStatus(
2034 { invalid_dex.GetFilename() },
2035 GetOdexDir() + "/output.odex",
2036 CompilerFilter::kQuicken,
2037 &error_msg,
2038 { "--compact-dex-level=fast" });
2039 ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
2040}
2041
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002042TEST_F(Dex2oatTest, AppImageNoProfile) {
2043 ScratchFile app_image_file;
2044 const std::string out_dir = GetScratchDir();
2045 const std::string odex_location = out_dir + "/base.odex";
2046 GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2047 odex_location,
2048 CompilerFilter::Filter::kSpeedProfile,
2049 { "--app-image-fd=" + std::to_string(app_image_file.GetFd()) },
2050 true, // expect_success
2051 false, // use_fd
2052 [](const OatFile&) {});
2053 // Open our generated oat file.
2054 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002055 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +01002056 odex_location.c_str(),
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002057 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002058 /*executable=*/ false,
2059 /*low_4gb=*/ false,
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002060 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002061 /*reservation=*/ nullptr,
Mathieu Chartierf85b3db2018-04-02 18:16:21 -07002062 &error_msg));
2063 ASSERT_TRUE(odex_file != nullptr);
2064 ImageHeader header = {};
2065 ASSERT_TRUE(app_image_file.GetFile()->PreadFully(
2066 reinterpret_cast<void*>(&header),
2067 sizeof(header),
2068 /*offset*/ 0u)) << app_image_file.GetFile()->GetLength();
2069 EXPECT_GT(header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
2070 EXPECT_EQ(header.GetImageSection(ImageHeader::kSectionArtMethods).Size(), 0u);
2071 EXPECT_EQ(header.GetImageSection(ImageHeader::kSectionArtFields).Size(), 0u);
2072}
2073
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002074TEST_F(Dex2oatTest, AppImageResolveStrings) {
Mathieu Chartier966b9332018-10-16 16:11:24 -07002075 if (!ClassLinker::kAppImageMayContainStrings) {
2076 TEST_DISABLED();
2077 }
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002078 using Hotness = ProfileCompilationInfo::MethodHotness;
2079 // Create a profile with the startup method marked.
2080 ScratchFile profile_file;
2081 std::vector<uint16_t> methods;
2082 {
2083 std::unique_ptr<const DexFile> dex(OpenTestDexFile("StringLiterals"));
2084 for (size_t method_idx = 0; method_idx < dex->NumMethodIds(); ++method_idx) {
2085 if (std::string(dex->GetMethodName(dex->GetMethodId(method_idx))) == "startUpMethod") {
2086 methods.push_back(method_idx);
2087 }
2088 }
2089 ASSERT_GT(methods.size(), 0u);
2090 // Here, we build the profile from the method lists.
2091 ProfileCompilationInfo info;
2092 info.AddMethodsForDex(Hotness::kFlagStartup, dex.get(), methods.begin(), methods.end());
2093 // Save the profile since we want to use it with dex2oat to produce an oat file.
2094 ASSERT_TRUE(info.Save(profile_file.GetFd()));
2095 }
2096 const std::string out_dir = GetScratchDir();
2097 const std::string odex_location = out_dir + "/base.odex";
2098 const std::string app_image_location = out_dir + "/base.art";
2099 GenerateOdexForTest(GetTestDexFileName("StringLiterals"),
2100 odex_location,
2101 CompilerFilter::Filter::kSpeedProfile,
2102 { "--app-image-file=" + app_image_location,
2103 "--resolve-startup-const-strings=true",
2104 "--profile-file=" + profile_file.GetFilename()},
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002105 /*expect_success=*/ true,
2106 /*use_fd=*/ false,
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002107 [](const OatFile&) {});
2108 // Open our generated oat file.
2109 std::string error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002110 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002111 odex_location.c_str(),
2112 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002113 /*executable=*/ false,
2114 /*low_4gb=*/ false,
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002115 odex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +01002116 /*reservation=*/ nullptr,
Mathieu Chartiercd0f38f2018-10-15 09:44:35 -07002117 &error_msg));
2118 ASSERT_TRUE(odex_file != nullptr);
2119 // Check the strings in the app image intern table only contain the "startup" strigs.
2120 {
2121 ScopedObjectAccess soa(Thread::Current());
2122 std::unique_ptr<gc::space::ImageSpace> space =
2123 gc::space::ImageSpace::CreateFromAppImage(app_image_location.c_str(),
2124 odex_file.get(),
2125 &error_msg);
2126 ASSERT_TRUE(space != nullptr) << error_msg;
2127 std::set<std::string> seen;
2128 InternTable intern_table;
2129 intern_table.AddImageStringsToTable(space.get(), [&](InternTable::UnorderedSet& interns)
2130 REQUIRES_SHARED(Locks::mutator_lock_) {
2131 for (const GcRoot<mirror::String>& str : interns) {
2132 seen.insert(str.Read()->ToModifiedUtf8());
2133 }
2134 });
2135 EXPECT_TRUE(seen.find("Loading ") != seen.end());
2136 EXPECT_TRUE(seen.find("Starting up") != seen.end());
2137 EXPECT_TRUE(seen.find("abcd.apk") != seen.end());
2138 EXPECT_TRUE(seen.find("Unexpected error") == seen.end());
2139 EXPECT_TRUE(seen.find("Shutting down!") == seen.end());
2140 }
2141}
2142
2143
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002144TEST_F(Dex2oatClassLoaderContextTest, StoredClassLoaderContext) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07002145 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002146 const std::string out_dir = GetScratchDir();
2147 const std::string odex_location = out_dir + "/base.odex";
Mathieu Chartierc4440772018-04-16 14:40:56 -07002148 const std::string valid_context = "PCL[" + dex_files[0]->GetLocation() + "]";
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002149 const std::string stored_context = "PCL[/system/not_real_lib.jar]";
Mathieu Chartierc4440772018-04-16 14:40:56 -07002150 std::string expected_stored_context = "PCL[";
2151 size_t index = 1;
2152 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2153 const bool is_first = index == 1u;
2154 if (!is_first) {
2155 expected_stored_context += ":";
2156 }
2157 expected_stored_context += "/system/not_real_lib.jar";
2158 if (!is_first) {
2159 expected_stored_context += "!classes" + std::to_string(index) + ".dex";
2160 }
2161 expected_stored_context += "*" + std::to_string(dex_file->GetLocationChecksum());
2162 ++index;
2163 }
2164 expected_stored_context += + "]";
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002165 // The class path should not be valid and should fail being stored.
2166 GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2167 odex_location,
2168 CompilerFilter::Filter::kQuicken,
2169 { "--class-loader-context=" + stored_context },
2170 true, // expect_success
2171 false, // use_fd
2172 [&](const OatFile& oat_file) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07002173 EXPECT_NE(oat_file.GetClassLoaderContext(), stored_context) << output_;
2174 EXPECT_NE(oat_file.GetClassLoaderContext(), valid_context) << output_;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002175 });
2176 // The stored context should match what we expect even though it's invalid.
2177 GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2178 odex_location,
2179 CompilerFilter::Filter::kQuicken,
2180 { "--class-loader-context=" + valid_context,
2181 "--stored-class-loader-context=" + stored_context },
2182 true, // expect_success
2183 false, // use_fd
2184 [&](const OatFile& oat_file) {
Mathieu Chartierc4440772018-04-16 14:40:56 -07002185 EXPECT_EQ(oat_file.GetClassLoaderContext(), expected_stored_context) << output_;
Mathieu Chartierf5abfc42018-03-23 21:51:54 -07002186 });
2187}
2188
Andreas Gampee1459ae2016-06-29 09:36:30 -07002189} // namespace art