blob: 32877a8cd65ec8c442a0c519a7f491113a710d9b [file] [log] [blame]
Andreas Gampee1459ae2016-06-29 09:36:30 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe7adeda82016-07-25 08:27:35 -070017#include <regex>
18#include <sstream>
Andreas Gampee1459ae2016-06-29 09:36:30 -070019#include <string>
20#include <vector>
Andreas Gampee1459ae2016-06-29 09:36:30 -070021
Andreas Gampe46ee31b2016-12-14 10:11:49 -080022#include <sys/wait.h>
23#include <unistd.h>
24
25#include "android-base/stringprintf.h"
26
Andreas Gampee1459ae2016-06-29 09:36:30 -070027#include "common_runtime_test.h"
28
29#include "base/logging.h"
30#include "base/macros.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "base/mutex-inl.h"
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +010032#include "bytecode_utils.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070033#include "dex2oat_environment_test.h"
Andreas Gampef7882972017-03-20 16:35:24 -070034#include "dex2oat_return_codes.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "dex_file-inl.h"
Calin Juravle33083d62017-01-18 15:29:12 -080036#include "jit/profile_compilation_info.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070037#include "oat.h"
38#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070039#include "utils.h"
40
Andreas Gampee1459ae2016-06-29 09:36:30 -070041namespace art {
42
Mathieu Chartierea650f32017-05-24 12:04:13 -070043static constexpr size_t kMaxMethodIds = 65535;
44
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080045using android::base::StringPrintf;
46
Andreas Gampee1459ae2016-06-29 09:36:30 -070047class Dex2oatTest : public Dex2oatEnvironmentTest {
48 public:
49 virtual void TearDown() OVERRIDE {
50 Dex2oatEnvironmentTest::TearDown();
51
52 output_ = "";
53 error_msg_ = "";
54 success_ = false;
55 }
56
57 protected:
Andreas Gampef7882972017-03-20 16:35:24 -070058 int GenerateOdexForTestWithStatus(const std::string& dex_location,
59 const std::string& odex_location,
60 CompilerFilter::Filter filter,
61 std::string* error_msg,
62 const std::vector<std::string>& extra_args = {},
63 bool use_fd = false) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080064 std::unique_ptr<File> oat_file;
Andreas Gampee1459ae2016-06-29 09:36:30 -070065 std::vector<std::string> args;
66 args.push_back("--dex-file=" + dex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080067 if (use_fd) {
68 oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
69 CHECK(oat_file != nullptr) << odex_location;
70 args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
Mathieu Chartier046854b2017-03-01 17:16:22 -080071 args.push_back("--oat-location=" + odex_location);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080072 } else {
73 args.push_back("--oat-file=" + odex_location);
74 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070075 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
76 args.push_back("--runtime-arg");
77 args.push_back("-Xnorelocate");
78
79 args.insert(args.end(), extra_args.begin(), extra_args.end());
80
Andreas Gampef7882972017-03-20 16:35:24 -070081 int status = Dex2Oat(args, error_msg);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080082 if (oat_file != nullptr) {
Andreas Gampef7882972017-03-20 16:35:24 -070083 CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -080084 }
Andreas Gampef7882972017-03-20 16:35:24 -070085 return status;
86 }
Andreas Gampee1459ae2016-06-29 09:36:30 -070087
Andreas Gampef7882972017-03-20 16:35:24 -070088 void GenerateOdexForTest(const std::string& dex_location,
89 const std::string& odex_location,
90 CompilerFilter::Filter filter,
91 const std::vector<std::string>& extra_args = {},
92 bool expect_success = true,
Calin Juravle1ce70852017-06-28 10:59:03 -070093 bool use_fd = false,
94 std::function<void(const OatFile&)> check_oat = [](const OatFile&) {}) {
Andreas Gampef7882972017-03-20 16:35:24 -070095 std::string error_msg;
96 int status = GenerateOdexForTestWithStatus(dex_location,
97 odex_location,
98 filter,
99 &error_msg,
100 extra_args,
101 use_fd);
102 bool success = (status == 0);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700103 if (expect_success) {
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800104 ASSERT_TRUE(success) << error_msg << std::endl << output_;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700105
106 // Verify the odex file was generated as expected.
107 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
108 odex_location.c_str(),
109 nullptr,
110 nullptr,
111 false,
112 /*low_4gb*/false,
113 dex_location.c_str(),
114 &error_msg));
115 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
116
117 CheckFilter(filter, odex_file->GetCompilerFilter());
Calin Juravle1ce70852017-06-28 10:59:03 -0700118 check_oat(*(odex_file.get()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700119 } else {
120 ASSERT_FALSE(success) << output_;
121
122 error_msg_ = error_msg;
123
124 // Verify there's no loadable odex file.
125 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
126 odex_location.c_str(),
127 nullptr,
128 nullptr,
129 false,
130 /*low_4gb*/false,
131 dex_location.c_str(),
132 &error_msg));
133 ASSERT_TRUE(odex_file.get() == nullptr);
134 }
135 }
136
137 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
138 // in subclasses when equality is not expected.
139 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
140 EXPECT_EQ(expected, actual);
141 }
142
Andreas Gampef7882972017-03-20 16:35:24 -0700143 int Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700144 Runtime* runtime = Runtime::Current();
145
146 const std::vector<gc::space::ImageSpace*>& image_spaces =
147 runtime->GetHeap()->GetBootImageSpaces();
148 if (image_spaces.empty()) {
149 *error_msg = "No image location found for Dex2Oat.";
150 return false;
151 }
152 std::string image_location = image_spaces[0]->GetImageLocation();
153
154 std::vector<std::string> argv;
155 argv.push_back(runtime->GetCompilerExecutable());
156 argv.push_back("--runtime-arg");
157 argv.push_back("-classpath");
158 argv.push_back("--runtime-arg");
159 std::string class_path = runtime->GetClassPathString();
160 if (class_path == "") {
161 class_path = OatFile::kSpecialSharedLibrary;
162 }
163 argv.push_back(class_path);
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000164 if (runtime->IsJavaDebuggable()) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700165 argv.push_back("--debuggable");
166 }
167 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
168
169 if (!runtime->IsVerificationEnabled()) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100170 argv.push_back("--compiler-filter=assume-verified");
Andreas Gampee1459ae2016-06-29 09:36:30 -0700171 }
172
173 if (runtime->MustRelocateIfPossible()) {
174 argv.push_back("--runtime-arg");
175 argv.push_back("-Xrelocate");
176 } else {
177 argv.push_back("--runtime-arg");
178 argv.push_back("-Xnorelocate");
179 }
180
181 if (!kIsTargetBuild) {
182 argv.push_back("--host");
183 }
184
185 argv.push_back("--boot-image=" + image_location);
186
187 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
188 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
189
190 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
191
192 // We must set --android-root.
193 const char* android_root = getenv("ANDROID_ROOT");
194 CHECK(android_root != nullptr);
195 argv.push_back("--android-root=" + std::string(android_root));
196
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100197 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700198
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100199 if (pipe(link) == -1) {
200 return false;
201 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700202
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100203 pid_t pid = fork();
204 if (pid == -1) {
205 return false;
206 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700207
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100208 if (pid == 0) {
209 // We need dex2oat to actually log things.
210 setenv("ANDROID_LOG_TAGS", "*:d", 1);
211 dup2(link[1], STDERR_FILENO);
212 close(link[0]);
213 close(link[1]);
214 std::vector<const char*> c_args;
215 for (const std::string& str : argv) {
216 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700217 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100218 c_args.push_back(nullptr);
219 execv(c_args[0], const_cast<char* const*>(c_args.data()));
220 exit(1);
Andreas Gampef7882972017-03-20 16:35:24 -0700221 UNREACHABLE();
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100222 } else {
223 close(link[1]);
224 char buffer[128];
225 memset(buffer, 0, 128);
226 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700227
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100228 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
229 output_ += std::string(buffer, bytes_read);
230 }
231 close(link[0]);
Andreas Gampef7882972017-03-20 16:35:24 -0700232 int status = -1;
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100233 if (waitpid(pid, &status, 0) != -1) {
234 success_ = (status == 0);
235 }
Andreas Gampef7882972017-03-20 16:35:24 -0700236 return status;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700237 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700238 }
239
240 std::string output_ = "";
241 std::string error_msg_ = "";
242 bool success_ = false;
243};
244
245class Dex2oatSwapTest : public Dex2oatTest {
246 protected:
247 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
248 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
249 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
250
Andreas Gampe7adeda82016-07-25 08:27:35 -0700251 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700252
253 std::vector<std::string> copy(extra_args);
254
255 std::unique_ptr<ScratchFile> sf;
256 if (use_fd) {
257 sf.reset(new ScratchFile());
Andreas Gampe46ee31b2016-12-14 10:11:49 -0800258 copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
Andreas Gampee1459ae2016-06-29 09:36:30 -0700259 } else {
260 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
261 copy.push_back("--swap-file=" + swap_location);
262 }
263 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
264
265 CheckValidity();
266 ASSERT_TRUE(success_);
267 CheckResult(expect_use);
268 }
269
Andreas Gampe7adeda82016-07-25 08:27:35 -0700270 virtual std::string GetTestDexFileName() {
Vladimir Marko15357702017-02-09 10:37:31 +0000271 return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
Andreas Gampe7adeda82016-07-25 08:27:35 -0700272 }
273
274 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700275 if (kIsTargetBuild) {
276 CheckTargetResult(expect_use);
277 } else {
278 CheckHostResult(expect_use);
279 }
280 }
281
Andreas Gampe7adeda82016-07-25 08:27:35 -0700282 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700283 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
284 // something for variants with file descriptor where we can control the lifetime of
285 // the swap file and thus take a look at it.
286 }
287
Andreas Gampe7adeda82016-07-25 08:27:35 -0700288 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700289 if (!kIsTargetBuild) {
290 if (expect_use) {
291 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
292 << output_;
293 } else {
294 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
295 << output_;
296 }
297 }
298 }
299
300 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700301 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700302 if (kIsTargetBuild) {
303 CheckTargetValidity();
304 } else {
305 CheckHostValidity();
306 }
307 }
308
Andreas Gampe7adeda82016-07-25 08:27:35 -0700309 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700310 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
311 // something for variants with file descriptor where we can control the lifetime of
312 // the swap file and thus take a look at it.
313 }
314
315 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700316 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700317 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
318 }
319};
320
321TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
322 RunTest(false /* use_fd */, false /* expect_use */);
323 RunTest(true /* use_fd */, false /* expect_use */);
324}
325
326TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
327 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
328 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
329}
330
331TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
332 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
333 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
334}
335
336TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
337 RunTest(false /* use_fd */,
338 true /* expect_use */,
339 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
340 RunTest(true /* use_fd */,
341 true /* expect_use */,
342 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
343}
344
Andreas Gampe7adeda82016-07-25 08:27:35 -0700345class Dex2oatSwapUseTest : public Dex2oatSwapTest {
346 protected:
347 void CheckHostResult(bool expect_use) OVERRIDE {
348 if (!kIsTargetBuild) {
349 if (expect_use) {
350 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
351 << output_;
352 } else {
353 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
354 << output_;
355 }
356 }
357 }
358
359 std::string GetTestDexFileName() OVERRIDE {
360 // Use Statics as it has a handful of functions.
361 return CommonRuntimeTest::GetTestDexFileName("Statics");
362 }
363
364 void GrabResult1() {
365 if (!kIsTargetBuild) {
366 native_alloc_1_ = ParseNativeAlloc();
367 swap_1_ = ParseSwap(false /* expected */);
368 } else {
369 native_alloc_1_ = std::numeric_limits<size_t>::max();
370 swap_1_ = 0;
371 }
372 }
373
374 void GrabResult2() {
375 if (!kIsTargetBuild) {
376 native_alloc_2_ = ParseNativeAlloc();
377 swap_2_ = ParseSwap(true /* expected */);
378 } else {
379 native_alloc_2_ = 0;
380 swap_2_ = std::numeric_limits<size_t>::max();
381 }
382 }
383
384 private:
385 size_t ParseNativeAlloc() {
386 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
387 std::smatch native_alloc_match;
388 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
389 if (!found) {
390 EXPECT_TRUE(found);
391 return 0;
392 }
393 if (native_alloc_match.size() != 2U) {
394 EXPECT_EQ(native_alloc_match.size(), 2U);
395 return 0;
396 }
397
398 std::istringstream stream(native_alloc_match[1].str());
399 size_t value;
400 stream >> value;
401
402 return value;
403 }
404
405 size_t ParseSwap(bool expected) {
406 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
407 std::smatch swap_match;
408 bool found = std::regex_search(output_, swap_match, swap_regex);
409 if (found != expected) {
410 EXPECT_EQ(expected, found);
411 return 0;
412 }
413
414 if (!found) {
415 return 0;
416 }
417
418 if (swap_match.size() != 2U) {
419 EXPECT_EQ(swap_match.size(), 2U);
420 return 0;
421 }
422
423 std::istringstream stream(swap_match[1].str());
424 size_t value;
425 stream >> value;
426
427 return value;
428 }
429
430 protected:
431 size_t native_alloc_1_;
432 size_t native_alloc_2_;
433
434 size_t swap_1_;
435 size_t swap_2_;
436};
437
438TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Andreas Gampef4a67fd2017-05-04 09:55:36 -0700439 // Native memory usage isn't correctly tracked under sanitization.
440 TEST_DISABLED_FOR_MEMORY_TOOL_ASAN();
441
Vladimir Marko57070da2017-02-14 16:16:30 +0000442 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
Roland Levillain19772bf2017-02-16 11:28:10 +0000443 // hold true on some x86 systems; disable this test while we
444 // investigate (b/29259363).
445 TEST_DISABLED_FOR_X86();
Vladimir Marko57070da2017-02-14 16:16:30 +0000446
Andreas Gampe7adeda82016-07-25 08:27:35 -0700447 RunTest(false /* use_fd */,
448 false /* expect_use */);
449 GrabResult1();
450 std::string output_1 = output_;
451
452 output_ = "";
453
454 RunTest(false /* use_fd */,
455 true /* expect_use */,
456 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
457 GrabResult2();
458 std::string output_2 = output_;
459
460 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
461 EXPECT_LT(native_alloc_2_, native_alloc_1_);
462 EXPECT_LT(swap_1_, swap_2_);
463
464 LOG(ERROR) << output_1;
465 LOG(ERROR) << output_2;
466 }
467}
468
Andreas Gampe67f02822016-06-24 21:05:23 -0700469class Dex2oatVeryLargeTest : public Dex2oatTest {
470 protected:
471 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
472 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
473 // Ignore, we'll do our own checks.
474 }
475
476 void RunTest(CompilerFilter::Filter filter,
477 bool expect_large,
478 const std::vector<std::string>& extra_args = {}) {
479 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
480 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
481
482 Copy(GetDexSrc1(), dex_location);
483
Andreas Gampeca620d72016-11-08 08:09:33 -0800484 GenerateOdexForTest(dex_location, odex_location, filter, extra_args);
Andreas Gampe67f02822016-06-24 21:05:23 -0700485
486 CheckValidity();
487 ASSERT_TRUE(success_);
488 CheckResult(dex_location, odex_location, filter, expect_large);
489 }
490
491 void CheckResult(const std::string& dex_location,
492 const std::string& odex_location,
493 CompilerFilter::Filter filter,
494 bool expect_large) {
495 // Host/target independent checks.
496 std::string error_msg;
497 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
498 odex_location.c_str(),
499 nullptr,
500 nullptr,
501 false,
502 /*low_4gb*/false,
503 dex_location.c_str(),
504 &error_msg));
505 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
506 if (expect_large) {
507 // Note: we cannot check the following:
508 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
509 // odex_file->GetCompilerFilter()));
510 // The reason is that the filter override currently happens when the dex files are
511 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
512 // store cannot be changed, and the original filter is set in stone.
513
514 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
515 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
516 ASSERT_TRUE(dex_file != nullptr);
517 uint32_t class_def_count = dex_file->NumClassDefs();
518 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
519 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
520 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
521 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
522 }
523 }
524
525 // If the input filter was "below," it should have been used.
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100526 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
Andreas Gampe67f02822016-06-24 21:05:23 -0700527 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
528 }
529 } else {
530 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
531 }
532
533 // Host/target dependent checks.
534 if (kIsTargetBuild) {
535 CheckTargetResult(expect_large);
536 } else {
537 CheckHostResult(expect_large);
538 }
539 }
540
541 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
542 // TODO: Ignore for now. May do something for fd things.
543 }
544
545 void CheckHostResult(bool expect_large) {
546 if (!kIsTargetBuild) {
547 if (expect_large) {
Mathieu Chartier010126f2017-07-15 15:44:18 -0700548 EXPECT_NE(output_.find("Very large app, downgrading to"),
Andreas Gampe67f02822016-06-24 21:05:23 -0700549 std::string::npos)
550 << output_;
551 } else {
Mathieu Chartier010126f2017-07-15 15:44:18 -0700552 EXPECT_EQ(output_.find("Very large app, downgrading to"),
Andreas Gampe67f02822016-06-24 21:05:23 -0700553 std::string::npos)
554 << output_;
555 }
556 }
557 }
558
559 // Check whether the dex2oat run was really successful.
560 void CheckValidity() {
561 if (kIsTargetBuild) {
562 CheckTargetValidity();
563 } else {
564 CheckHostValidity();
565 }
566 }
567
568 void CheckTargetValidity() {
569 // TODO: Ignore for now.
570 }
571
572 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
573 void CheckHostValidity() {
574 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
575 }
576};
577
578TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100579 RunTest(CompilerFilter::kAssumeVerified, false);
580 RunTest(CompilerFilter::kExtract, false);
581 RunTest(CompilerFilter::kQuicken, false);
Andreas Gampe67f02822016-06-24 21:05:23 -0700582 RunTest(CompilerFilter::kSpeed, false);
583
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100584 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=1000000" });
585 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=1000000" });
586 RunTest(CompilerFilter::kQuicken, false, { "--very-large-app-threshold=1000000" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700587 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
588}
589
590TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100591 RunTest(CompilerFilter::kAssumeVerified, false, { "--very-large-app-threshold=100" });
592 RunTest(CompilerFilter::kExtract, false, { "--very-large-app-threshold=100" });
593 RunTest(CompilerFilter::kQuicken, true, { "--very-large-app-threshold=100" });
Andreas Gampe67f02822016-06-24 21:05:23 -0700594 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
595}
596
Mathieu Chartier97ab5e32017-02-22 13:35:44 -0800597// Regressin test for b/35665292.
598TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
599 // Test that dex2oat doesn't crash with speed-profile but no input profile.
600 RunTest(CompilerFilter::kSpeedProfile, false);
601}
602
Jeff Hao608f2ce2016-10-19 11:17:11 -0700603class Dex2oatLayoutTest : public Dex2oatTest {
604 protected:
605 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
606 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
607 // Ignore, we'll do our own checks.
608 }
609
Jeff Hao41fba6a2016-11-28 11:53:33 -0800610 // Emits a profile with a single dex file with the given location and a single class index of 1.
611 void GenerateProfile(const std::string& test_profile,
612 const std::string& dex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800613 size_t num_classes,
Jeff Hao41fba6a2016-11-28 11:53:33 -0800614 uint32_t checksum) {
615 int profile_test_fd = open(test_profile.c_str(), O_CREAT | O_TRUNC | O_WRONLY, 0644);
616 CHECK_GE(profile_test_fd, 0);
617
618 ProfileCompilationInfo info;
619 std::string profile_key = ProfileCompilationInfo::GetProfileDexFileKey(dex_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800620 for (size_t i = 0; i < num_classes; ++i) {
Mathieu Chartierea650f32017-05-24 12:04:13 -0700621 info.AddClassIndex(profile_key, checksum, dex::TypeIndex(1 + i), kMaxMethodIds);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800622 }
Jeff Hao41fba6a2016-11-28 11:53:33 -0800623 bool result = info.Save(profile_test_fd);
624 close(profile_test_fd);
625 ASSERT_TRUE(result);
626 }
627
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800628 void CompileProfileOdex(const std::string& dex_location,
629 const std::string& odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800630 const std::string& app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800631 bool use_fd,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800632 size_t num_profile_classes,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000633 const std::vector<std::string>& extra_args = {},
634 bool expect_success = true) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800635 const std::string profile_location = GetScratchDir() + "/primary.prof";
Jeff Hao41fba6a2016-11-28 11:53:33 -0800636 const char* location = dex_location.c_str();
637 std::string error_msg;
638 std::vector<std::unique_ptr<const DexFile>> dex_files;
639 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
640 EXPECT_EQ(dex_files.size(), 1U);
641 std::unique_ptr<const DexFile>& dex_file = dex_files[0];
Mathieu Chartier046854b2017-03-01 17:16:22 -0800642 GenerateProfile(profile_location,
643 dex_location,
644 num_profile_classes,
645 dex_file->GetLocationChecksum());
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800646 std::vector<std::string> copy(extra_args);
647 copy.push_back("--profile-file=" + profile_location);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800648 std::unique_ptr<File> app_image_file;
649 if (!app_image_file_name.empty()) {
650 if (use_fd) {
651 app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
652 copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
653 } else {
654 copy.push_back("--app-image-file=" + app_image_file_name);
655 }
656 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800657 GenerateOdexForTest(dex_location,
658 odex_location,
659 CompilerFilter::kSpeedProfile,
660 copy,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000661 expect_success,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800662 use_fd);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800663 if (app_image_file != nullptr) {
664 ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
665 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800666 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700667
Mathieu Chartier046854b2017-03-01 17:16:22 -0800668 uint64_t GetImageSize(const std::string& image_file_name) {
669 EXPECT_FALSE(image_file_name.empty());
670 std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
671 CHECK(file != nullptr);
672 ImageHeader image_header;
673 const bool success = file->ReadFully(&image_header, sizeof(image_header));
674 CHECK(success);
675 CHECK(image_header.IsValid());
676 ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
677 return image_header.GetImageSize();
678 }
679
680 void RunTest(bool app_image) {
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800681 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
682 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800683 std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800684 Copy(GetDexSrc2(), dex_location);
685
Mathieu Chartier046854b2017-03-01 17:16:22 -0800686 uint64_t image_file_empty_profile = 0;
687 if (app_image) {
688 CompileProfileOdex(dex_location,
689 odex_location,
690 app_image_file,
691 /* use_fd */ false,
692 /* num_profile_classes */ 0);
693 CheckValidity();
694 ASSERT_TRUE(success_);
695 // Don't check the result since CheckResult relies on the class being in the profile.
696 image_file_empty_profile = GetImageSize(app_image_file);
697 EXPECT_GT(image_file_empty_profile, 0u);
698 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700699
Mathieu Chartier046854b2017-03-01 17:16:22 -0800700 // Small profile.
701 CompileProfileOdex(dex_location,
702 odex_location,
703 app_image_file,
704 /* use_fd */ false,
705 /* num_profile_classes */ 1);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700706 CheckValidity();
707 ASSERT_TRUE(success_);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800708 CheckResult(dex_location, odex_location, app_image_file);
709
710 if (app_image) {
711 // Test that the profile made a difference by adding more classes.
712 const uint64_t image_file_small_profile = GetImageSize(app_image_file);
713 CHECK_LT(image_file_empty_profile, image_file_small_profile);
714 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700715 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800716
717 void RunTestVDex() {
718 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
719 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
720 std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
Mathieu Chartier046854b2017-03-01 17:16:22 -0800721 std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800722 Copy(GetDexSrc2(), dex_location);
723
724 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
725 CHECK(vdex_file1 != nullptr) << vdex_location;
726 ScratchFile vdex_file2;
727 {
728 std::string input_vdex = "--input-vdex-fd=-1";
729 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
730 CompileProfileOdex(dex_location,
731 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800732 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800733 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800734 /* num_profile_classes */ 1,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800735 { input_vdex, output_vdex });
736 EXPECT_GT(vdex_file1->GetLength(), 0u);
737 }
738 {
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000739 // Test that vdex and dexlayout fail gracefully.
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800740 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
741 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
742 CompileProfileOdex(dex_location,
743 odex_location,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800744 app_image_file_name,
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800745 /* use_fd */ true,
Mathieu Chartier046854b2017-03-01 17:16:22 -0800746 /* num_profile_classes */ 1,
Nicolas Geoffray97fa9922017-03-09 13:13:25 +0000747 { input_vdex, output_vdex },
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100748 /* expect_success */ true);
749 EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800750 }
751 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
752 CheckValidity();
Nicolas Geoffray4e868fa2017-04-21 17:16:44 +0100753 ASSERT_TRUE(success_);
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800754 }
755
Mathieu Chartier046854b2017-03-01 17:16:22 -0800756 void CheckResult(const std::string& dex_location,
757 const std::string& odex_location,
758 const std::string& app_image_file_name) {
Jeff Hao608f2ce2016-10-19 11:17:11 -0700759 // Host/target independent checks.
760 std::string error_msg;
761 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
762 odex_location.c_str(),
763 nullptr,
764 nullptr,
765 false,
766 /*low_4gb*/false,
767 dex_location.c_str(),
768 &error_msg));
769 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
770
Jeff Hao042e8982016-10-19 11:17:11 -0700771 const char* location = dex_location.c_str();
772 std::vector<std::unique_ptr<const DexFile>> dex_files;
773 ASSERT_TRUE(DexFile::Open(location, location, true, &error_msg, &dex_files));
774 EXPECT_EQ(dex_files.size(), 1U);
775 std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
776
Jeff Hao608f2ce2016-10-19 11:17:11 -0700777 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
Jeff Hao042e8982016-10-19 11:17:11 -0700778 std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
779 ASSERT_TRUE(new_dex_file != nullptr);
780 uint32_t class_def_count = new_dex_file->NumClassDefs();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700781 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
Jeff Hao042e8982016-10-19 11:17:11 -0700782 ASSERT_GE(class_def_count, 2U);
783
784 // The new layout swaps the classes at indexes 0 and 1.
785 std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
786 std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
787 std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
788 std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
789 EXPECT_EQ(old_class0, new_class1);
790 EXPECT_EQ(old_class1, new_class0);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700791 }
792
Jeff Haoc155b052017-01-17 17:43:29 -0800793 EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
Mathieu Chartier046854b2017-03-01 17:16:22 -0800794
795 if (!app_image_file_name.empty()) {
796 // Go peek at the image header to make sure it was large enough to contain the class.
797 std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
798 ImageHeader image_header;
799 bool success = file->ReadFully(&image_header, sizeof(image_header));
800 ASSERT_TRUE(success);
801 ASSERT_TRUE(image_header.IsValid());
802 EXPECT_GT(image_header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
803 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700804 }
805
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800806 // Check whether the dex2oat run was really successful.
807 void CheckValidity() {
808 if (kIsTargetBuild) {
809 CheckTargetValidity();
810 } else {
811 CheckHostValidity();
Jeff Hao608f2ce2016-10-19 11:17:11 -0700812 }
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800813 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700814
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800815 void CheckTargetValidity() {
816 // TODO: Ignore for now.
817 }
Jeff Hao608f2ce2016-10-19 11:17:11 -0700818
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800819 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
820 void CheckHostValidity() {
821 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
822 }
823};
Jeff Hao608f2ce2016-10-19 11:17:11 -0700824
825TEST_F(Dex2oatLayoutTest, TestLayout) {
Mathieu Chartier046854b2017-03-01 17:16:22 -0800826 RunTest(/* app-image */ false);
827}
828
829TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
830 RunTest(/* app-image */ true);
Jeff Hao608f2ce2016-10-19 11:17:11 -0700831}
832
Mathieu Chartier8bc343b2017-03-01 15:20:30 -0800833TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
834 RunTestVDex();
835}
836
Nicolas Geoffrayb02ba932017-07-13 15:53:54 +0100837class Dex2oatUnquickenTest : public Dex2oatTest {
838 protected:
839 void RunUnquickenMultiDex() {
840 std::string dex_location = GetScratchDir() + "/UnquickenMultiDex.jar";
841 std::string odex_location = GetOdexDir() + "/UnquickenMultiDex.odex";
842 std::string vdex_location = GetOdexDir() + "/UnquickenMultiDex.vdex";
843 Copy(GetTestDexFileName("MultiDex"), dex_location);
844
845 std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
846 CHECK(vdex_file1 != nullptr) << vdex_location;
847 // Quicken the dex file into a vdex file.
848 {
849 std::string input_vdex = "--input-vdex-fd=-1";
850 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
851 GenerateOdexForTest(dex_location,
852 odex_location,
853 CompilerFilter::kQuicken,
854 { input_vdex, output_vdex },
855 /* expect_success */ true,
856 /* use_fd */ true);
857 EXPECT_GT(vdex_file1->GetLength(), 0u);
858 }
859 // Unquicken by running the verify compiler filter on the vdex file.
860 {
861 std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
862 std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
863 GenerateOdexForTest(dex_location,
864 odex_location,
865 CompilerFilter::kVerify,
866 { input_vdex, output_vdex },
867 /* expect_success */ true,
868 /* use_fd */ true);
869 }
870 ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
871 CheckResult(dex_location, odex_location);
872 ASSERT_TRUE(success_);
873 }
874
875 void CheckResult(const std::string& dex_location, const std::string& odex_location) {
876 std::string error_msg;
877 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
878 odex_location.c_str(),
879 nullptr,
880 nullptr,
881 false,
882 /*low_4gb*/false,
883 dex_location.c_str(),
884 &error_msg));
885 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
886 ASSERT_GE(odex_file->GetOatDexFiles().size(), 1u);
887
888 // Iterate over the dex files and ensure there is no quickened instruction.
889 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
890 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
891 for (uint32_t i = 0; i < dex_file->NumClassDefs(); ++i) {
892 const DexFile::ClassDef& class_def = dex_file->GetClassDef(i);
893 const uint8_t* class_data = dex_file->GetClassData(class_def);
894 if (class_data != nullptr) {
895 for (ClassDataItemIterator class_it(*dex_file, class_data);
896 class_it.HasNext();
897 class_it.Next()) {
898 if (class_it.IsAtMethod() && class_it.GetMethodCodeItem() != nullptr) {
899 for (CodeItemIterator it(*class_it.GetMethodCodeItem()); !it.Done(); it.Advance()) {
900 Instruction* inst = const_cast<Instruction*>(&it.CurrentInstruction());
901 ASSERT_FALSE(inst->IsQuickened());
902 }
903 }
904 }
905 }
906 }
907 }
908 }
909};
910
911TEST_F(Dex2oatUnquickenTest, UnquickenMultiDex) {
912 RunUnquickenMultiDex();
913}
914
Andreas Gampe2e8a2562017-01-18 20:39:02 -0800915class Dex2oatWatchdogTest : public Dex2oatTest {
916 protected:
917 void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
918 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
919 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
920
921 Copy(GetTestDexFileName(), dex_location);
922
923 std::vector<std::string> copy(extra_args);
924
925 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
926 copy.push_back("--swap-file=" + swap_location);
927 GenerateOdexForTest(dex_location,
928 odex_location,
929 CompilerFilter::kSpeed,
930 copy,
931 expect_success);
932 }
933
934 std::string GetTestDexFileName() {
935 return GetDexSrc1();
936 }
937};
938
939TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
940 // Check with default.
941 RunTest(true);
942
943 // Check with ten minutes.
944 RunTest(true, { "--watchdog-timeout=600000" });
945}
946
947TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
948 // Check with ten milliseconds.
949 RunTest(false, { "--watchdog-timeout=10" });
950}
951
Andreas Gampef7882972017-03-20 16:35:24 -0700952class Dex2oatReturnCodeTest : public Dex2oatTest {
953 protected:
954 int RunTest(const std::vector<std::string>& extra_args = {}) {
955 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
956 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
957
958 Copy(GetTestDexFileName(), dex_location);
959
960 std::string error_msg;
961 return GenerateOdexForTestWithStatus(dex_location,
962 odex_location,
963 CompilerFilter::kSpeed,
964 &error_msg,
965 extra_args);
966 }
967
968 std::string GetTestDexFileName() {
969 return GetDexSrc1();
970 }
971};
972
973TEST_F(Dex2oatReturnCodeTest, TestCreateRuntime) {
Andreas Gampefd80b172017-04-26 22:25:31 -0700974 TEST_DISABLED_FOR_MEMORY_TOOL(); // b/19100793
Andreas Gampef7882972017-03-20 16:35:24 -0700975 int status = RunTest({ "--boot-image=/this/does/not/exist/yolo.oat" });
976 EXPECT_EQ(static_cast<int>(dex2oat::ReturnCode::kCreateRuntime), WEXITSTATUS(status)) << output_;
977}
978
Calin Juravle1ce70852017-06-28 10:59:03 -0700979class Dex2oatClassLoaderContextTest : public Dex2oatTest {
980 protected:
981 void RunTest(const char* class_loader_context,
982 const char* expected_classpath_key,
983 bool expected_success,
984 bool use_second_source = false) {
985 std::string dex_location = GetUsedDexLocation();
986 std::string odex_location = GetUsedOatLocation();
987
988 Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
989
990 std::string error_msg;
991 std::vector<std::string> extra_args;
992 if (class_loader_context != nullptr) {
993 extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
994 }
995 auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
996 ASSERT_TRUE(expected_classpath_key != nullptr);
997 const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
998 ASSERT_TRUE(classpath != nullptr);
999 ASSERT_STREQ(expected_classpath_key, classpath);
1000 };
1001
1002 GenerateOdexForTest(dex_location,
1003 odex_location,
1004 CompilerFilter::kQuicken,
1005 extra_args,
1006 expected_success,
1007 /*use_fd*/ false,
1008 check_oat);
1009 }
1010
1011 std::string GetUsedDexLocation() {
1012 return GetScratchDir() + "/Context.jar";
1013 }
1014
1015 std::string GetUsedOatLocation() {
1016 return GetOdexDir() + "/Context.odex";
1017 }
1018
Calin Juravle7b0648a2017-07-07 18:40:50 -07001019 const char* kEmptyClassPathKey = "PCL[]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001020};
1021
1022TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1023 RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1024}
1025
1026TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1027 RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1028}
1029
1030TEST_F(Dex2oatClassLoaderContextTest, SpecialContext) {
1031 RunTest(OatFile::kSpecialSharedLibrary,
1032 OatFile::kSpecialSharedLibrary,
1033 /*expected_success*/ true);
1034}
1035
1036TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1037 std::string context = "PCL[" + GetUsedDexLocation() + "]";
1038 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1039}
1040
1041TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1042 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
Calin Juravle1ce70852017-06-28 10:59:03 -07001043
1044 std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001045 std::string expected_classpath_key = "PCL[" +
1046 dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001047 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1048}
1049
1050TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFiles) {
1051 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1052 Copy(GetStrippedDexSrc1(), stripped_classpath);
1053
1054 std::string context = "PCL[" + stripped_classpath + "]";
1055 // Expect an empty context because stripped dex files cannot be open.
Calin Juravle7b0648a2017-07-07 18:40:50 -07001056 RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
Calin Juravle1ce70852017-06-28 10:59:03 -07001057}
1058
1059TEST_F(Dex2oatClassLoaderContextTest, ContextWithStrippedDexFilesBackedByOdex) {
1060 std::string stripped_classpath = GetScratchDir() + "/stripped_classpath.jar";
1061 std::string odex_for_classpath = GetOdexDir() + "/stripped_classpath.odex";
1062
1063 Copy(GetDexSrc1(), stripped_classpath);
1064
1065 GenerateOdexForTest(stripped_classpath,
1066 odex_for_classpath,
1067 CompilerFilter::kQuicken,
1068 {},
1069 true);
1070
1071 // Strip the dex file
1072 Copy(GetStrippedDexSrc1(), stripped_classpath);
1073
1074 std::string context = "PCL[" + stripped_classpath + "]";
Calin Juravle7b0648a2017-07-07 18:40:50 -07001075 std::string expected_classpath_key;
Calin Juravle1ce70852017-06-28 10:59:03 -07001076 {
1077 // Open the oat file to get the expected classpath.
1078 OatFileAssistant oat_file_assistant(stripped_classpath.c_str(), kRuntimeISA, false);
1079 std::unique_ptr<OatFile> oat_file(oat_file_assistant.GetBestOatFile());
1080 std::vector<std::unique_ptr<const DexFile>> oat_dex_files =
1081 OatFileAssistant::LoadDexFiles(*oat_file, stripped_classpath.c_str());
Calin Juravle7b0648a2017-07-07 18:40:50 -07001082 expected_classpath_key = "PCL[";
1083 for (size_t i = 0; i < oat_dex_files.size(); i++) {
1084 if (i > 0) {
1085 expected_classpath_key + ":";
1086 }
1087 expected_classpath_key += oat_dex_files[i]->GetLocation() + "*" +
1088 std::to_string(oat_dex_files[i]->GetLocationChecksum());
1089 }
1090 expected_classpath_key += "]";
Calin Juravle1ce70852017-06-28 10:59:03 -07001091 }
1092
1093 RunTest(context.c_str(),
Calin Juravle7b0648a2017-07-07 18:40:50 -07001094 expected_classpath_key.c_str(),
Calin Juravle1ce70852017-06-28 10:59:03 -07001095 /*expected_success*/ true,
1096 /*use_second_source*/ true);
1097}
1098
1099TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1100 std::string context = "PCL[does_not_exists.dex]";
1101 // Expect an empty context because stripped dex files cannot be open.
1102 RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1103}
1104
Calin Juravlec79470d2017-07-12 17:37:42 -07001105TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1106 std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1107 std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1108
1109 std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1110 "DLC[" + GetTestDexFileName("MultiDex") + "]";
1111 std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1112 "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1113
1114 RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1115}
1116
Andreas Gampee1459ae2016-06-29 09:36:30 -07001117} // namespace art