blob: 58dd04708131f06369ac0273541321db5720822a [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
22#include "common_runtime_test.h"
23
24#include "base/logging.h"
25#include "base/macros.h"
26#include "base/stringprintf.h"
27#include "dex2oat_environment_test.h"
Andreas Gampe67f02822016-06-24 21:05:23 -070028#include "oat.h"
29#include "oat_file.h"
Andreas Gampee1459ae2016-06-29 09:36:30 -070030#include "utils.h"
31
32#include <sys/wait.h>
33#include <unistd.h>
34
35namespace art {
36
37class Dex2oatTest : public Dex2oatEnvironmentTest {
38 public:
39 virtual void TearDown() OVERRIDE {
40 Dex2oatEnvironmentTest::TearDown();
41
42 output_ = "";
43 error_msg_ = "";
44 success_ = false;
45 }
46
47 protected:
48 void GenerateOdexForTest(const std::string& dex_location,
49 const std::string& odex_location,
50 CompilerFilter::Filter filter,
51 const std::vector<std::string>& extra_args = {},
52 bool expect_success = true) {
53 std::vector<std::string> args;
54 args.push_back("--dex-file=" + dex_location);
55 args.push_back("--oat-file=" + odex_location);
56 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
57 args.push_back("--runtime-arg");
58 args.push_back("-Xnorelocate");
59
60 args.insert(args.end(), extra_args.begin(), extra_args.end());
61
62 std::string error_msg;
63 bool success = Dex2Oat(args, &error_msg);
64
65 if (expect_success) {
66 ASSERT_TRUE(success) << error_msg;
67
68 // Verify the odex file was generated as expected.
69 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
70 odex_location.c_str(),
71 nullptr,
72 nullptr,
73 false,
74 /*low_4gb*/false,
75 dex_location.c_str(),
76 &error_msg));
77 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
78
79 CheckFilter(filter, odex_file->GetCompilerFilter());
80 } else {
81 ASSERT_FALSE(success) << output_;
82
83 error_msg_ = error_msg;
84
85 // Verify there's no loadable odex file.
86 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
87 odex_location.c_str(),
88 nullptr,
89 nullptr,
90 false,
91 /*low_4gb*/false,
92 dex_location.c_str(),
93 &error_msg));
94 ASSERT_TRUE(odex_file.get() == nullptr);
95 }
96 }
97
98 // Check the input compiler filter against the generated oat file's filter. Mayb be overridden
99 // in subclasses when equality is not expected.
100 virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
101 EXPECT_EQ(expected, actual);
102 }
103
104 bool Dex2Oat(const std::vector<std::string>& dex2oat_args, std::string* error_msg) {
105 Runtime* runtime = Runtime::Current();
106
107 const std::vector<gc::space::ImageSpace*>& image_spaces =
108 runtime->GetHeap()->GetBootImageSpaces();
109 if (image_spaces.empty()) {
110 *error_msg = "No image location found for Dex2Oat.";
111 return false;
112 }
113 std::string image_location = image_spaces[0]->GetImageLocation();
114
115 std::vector<std::string> argv;
116 argv.push_back(runtime->GetCompilerExecutable());
117 argv.push_back("--runtime-arg");
118 argv.push_back("-classpath");
119 argv.push_back("--runtime-arg");
120 std::string class_path = runtime->GetClassPathString();
121 if (class_path == "") {
122 class_path = OatFile::kSpecialSharedLibrary;
123 }
124 argv.push_back(class_path);
125 if (runtime->IsDebuggable()) {
126 argv.push_back("--debuggable");
127 }
128 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
129
130 if (!runtime->IsVerificationEnabled()) {
131 argv.push_back("--compiler-filter=verify-none");
132 }
133
134 if (runtime->MustRelocateIfPossible()) {
135 argv.push_back("--runtime-arg");
136 argv.push_back("-Xrelocate");
137 } else {
138 argv.push_back("--runtime-arg");
139 argv.push_back("-Xnorelocate");
140 }
141
142 if (!kIsTargetBuild) {
143 argv.push_back("--host");
144 }
145
146 argv.push_back("--boot-image=" + image_location);
147
148 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
149 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
150
151 argv.insert(argv.end(), dex2oat_args.begin(), dex2oat_args.end());
152
153 // We must set --android-root.
154 const char* android_root = getenv("ANDROID_ROOT");
155 CHECK(android_root != nullptr);
156 argv.push_back("--android-root=" + std::string(android_root));
157
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100158 int link[2];
Andreas Gampee1459ae2016-06-29 09:36:30 -0700159
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100160 if (pipe(link) == -1) {
161 return false;
162 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700163
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100164 pid_t pid = fork();
165 if (pid == -1) {
166 return false;
167 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700168
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100169 if (pid == 0) {
170 // We need dex2oat to actually log things.
171 setenv("ANDROID_LOG_TAGS", "*:d", 1);
172 dup2(link[1], STDERR_FILENO);
173 close(link[0]);
174 close(link[1]);
175 std::vector<const char*> c_args;
176 for (const std::string& str : argv) {
177 c_args.push_back(str.c_str());
Andreas Gampee1459ae2016-06-29 09:36:30 -0700178 }
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100179 c_args.push_back(nullptr);
180 execv(c_args[0], const_cast<char* const*>(c_args.data()));
181 exit(1);
182 } else {
183 close(link[1]);
184 char buffer[128];
185 memset(buffer, 0, 128);
186 ssize_t bytes_read = 0;
Andreas Gampee1459ae2016-06-29 09:36:30 -0700187
Nicolas Geoffray56fe0f02016-06-30 15:07:46 +0100188 while (TEMP_FAILURE_RETRY(bytes_read = read(link[0], buffer, 128)) > 0) {
189 output_ += std::string(buffer, bytes_read);
190 }
191 close(link[0]);
192 int status = 0;
193 if (waitpid(pid, &status, 0) != -1) {
194 success_ = (status == 0);
195 }
Andreas Gampee1459ae2016-06-29 09:36:30 -0700196 }
197 return success_;
198 }
199
200 std::string output_ = "";
201 std::string error_msg_ = "";
202 bool success_ = false;
203};
204
205class Dex2oatSwapTest : public Dex2oatTest {
206 protected:
207 void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
208 std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
209 std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
210
Andreas Gampe7adeda82016-07-25 08:27:35 -0700211 Copy(GetTestDexFileName(), dex_location);
Andreas Gampee1459ae2016-06-29 09:36:30 -0700212
213 std::vector<std::string> copy(extra_args);
214
215 std::unique_ptr<ScratchFile> sf;
216 if (use_fd) {
217 sf.reset(new ScratchFile());
218 copy.push_back(StringPrintf("--swap-fd=%d", sf->GetFd()));
219 } else {
220 std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
221 copy.push_back("--swap-file=" + swap_location);
222 }
223 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy);
224
225 CheckValidity();
226 ASSERT_TRUE(success_);
227 CheckResult(expect_use);
228 }
229
Andreas Gampe7adeda82016-07-25 08:27:35 -0700230 virtual std::string GetTestDexFileName() {
231 return GetDexSrc1();
232 }
233
234 virtual void CheckResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700235 if (kIsTargetBuild) {
236 CheckTargetResult(expect_use);
237 } else {
238 CheckHostResult(expect_use);
239 }
240 }
241
Andreas Gampe7adeda82016-07-25 08:27:35 -0700242 virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700243 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
244 // something for variants with file descriptor where we can control the lifetime of
245 // the swap file and thus take a look at it.
246 }
247
Andreas Gampe7adeda82016-07-25 08:27:35 -0700248 virtual void CheckHostResult(bool expect_use) {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700249 if (!kIsTargetBuild) {
250 if (expect_use) {
251 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
252 << output_;
253 } else {
254 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
255 << output_;
256 }
257 }
258 }
259
260 // Check whether the dex2oat run was really successful.
Andreas Gampe7adeda82016-07-25 08:27:35 -0700261 virtual void CheckValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700262 if (kIsTargetBuild) {
263 CheckTargetValidity();
264 } else {
265 CheckHostValidity();
266 }
267 }
268
Andreas Gampe7adeda82016-07-25 08:27:35 -0700269 virtual void CheckTargetValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700270 // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
271 // something for variants with file descriptor where we can control the lifetime of
272 // the swap file and thus take a look at it.
273 }
274
275 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
Andreas Gampe7adeda82016-07-25 08:27:35 -0700276 virtual void CheckHostValidity() {
Andreas Gampee1459ae2016-06-29 09:36:30 -0700277 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
278 }
279};
280
281TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
282 RunTest(false /* use_fd */, false /* expect_use */);
283 RunTest(true /* use_fd */, false /* expect_use */);
284}
285
286TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
287 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
288 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-size-threshold=0" });
289}
290
291TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
292 RunTest(false /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
293 RunTest(true /* use_fd */, false /* expect_use */, { "--swap-dex-count-threshold=0" });
294}
295
296TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
297 RunTest(false /* use_fd */,
298 true /* expect_use */,
299 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
300 RunTest(true /* use_fd */,
301 true /* expect_use */,
302 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
303}
304
Andreas Gampe7adeda82016-07-25 08:27:35 -0700305class Dex2oatSwapUseTest : public Dex2oatSwapTest {
306 protected:
307 void CheckHostResult(bool expect_use) OVERRIDE {
308 if (!kIsTargetBuild) {
309 if (expect_use) {
310 EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
311 << output_;
312 } else {
313 EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
314 << output_;
315 }
316 }
317 }
318
319 std::string GetTestDexFileName() OVERRIDE {
320 // Use Statics as it has a handful of functions.
321 return CommonRuntimeTest::GetTestDexFileName("Statics");
322 }
323
324 void GrabResult1() {
325 if (!kIsTargetBuild) {
326 native_alloc_1_ = ParseNativeAlloc();
327 swap_1_ = ParseSwap(false /* expected */);
328 } else {
329 native_alloc_1_ = std::numeric_limits<size_t>::max();
330 swap_1_ = 0;
331 }
332 }
333
334 void GrabResult2() {
335 if (!kIsTargetBuild) {
336 native_alloc_2_ = ParseNativeAlloc();
337 swap_2_ = ParseSwap(true /* expected */);
338 } else {
339 native_alloc_2_ = 0;
340 swap_2_ = std::numeric_limits<size_t>::max();
341 }
342 }
343
344 private:
345 size_t ParseNativeAlloc() {
346 std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
347 std::smatch native_alloc_match;
348 bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
349 if (!found) {
350 EXPECT_TRUE(found);
351 return 0;
352 }
353 if (native_alloc_match.size() != 2U) {
354 EXPECT_EQ(native_alloc_match.size(), 2U);
355 return 0;
356 }
357
358 std::istringstream stream(native_alloc_match[1].str());
359 size_t value;
360 stream >> value;
361
362 return value;
363 }
364
365 size_t ParseSwap(bool expected) {
366 std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
367 std::smatch swap_match;
368 bool found = std::regex_search(output_, swap_match, swap_regex);
369 if (found != expected) {
370 EXPECT_EQ(expected, found);
371 return 0;
372 }
373
374 if (!found) {
375 return 0;
376 }
377
378 if (swap_match.size() != 2U) {
379 EXPECT_EQ(swap_match.size(), 2U);
380 return 0;
381 }
382
383 std::istringstream stream(swap_match[1].str());
384 size_t value;
385 stream >> value;
386
387 return value;
388 }
389
390 protected:
391 size_t native_alloc_1_;
392 size_t native_alloc_2_;
393
394 size_t swap_1_;
395 size_t swap_2_;
396};
397
398TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
Roland Levillain63b6eb42016-07-28 16:37:28 +0100399 // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
400 // hold true on some x86 systems when read barriers are enabled;
401 // disable this test while we investigate (b/29259363).
402 TEST_DISABLED_FOR_READ_BARRIER_ON_X86();
403
Andreas Gampe7adeda82016-07-25 08:27:35 -0700404 RunTest(false /* use_fd */,
405 false /* expect_use */);
406 GrabResult1();
407 std::string output_1 = output_;
408
409 output_ = "";
410
411 RunTest(false /* use_fd */,
412 true /* expect_use */,
413 { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
414 GrabResult2();
415 std::string output_2 = output_;
416
417 if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
418 EXPECT_LT(native_alloc_2_, native_alloc_1_);
419 EXPECT_LT(swap_1_, swap_2_);
420
421 LOG(ERROR) << output_1;
422 LOG(ERROR) << output_2;
423 }
424}
425
Andreas Gampe67f02822016-06-24 21:05:23 -0700426class Dex2oatVeryLargeTest : public Dex2oatTest {
427 protected:
428 void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
429 CompilerFilter::Filter result ATTRIBUTE_UNUSED) OVERRIDE {
430 // Ignore, we'll do our own checks.
431 }
432
433 void RunTest(CompilerFilter::Filter filter,
434 bool expect_large,
435 const std::vector<std::string>& extra_args = {}) {
436 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
437 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
438
439 Copy(GetDexSrc1(), dex_location);
440
441 std::vector<std::string> copy(extra_args);
442
443 GenerateOdexForTest(dex_location, odex_location, filter, copy);
444
445 CheckValidity();
446 ASSERT_TRUE(success_);
447 CheckResult(dex_location, odex_location, filter, expect_large);
448 }
449
450 void CheckResult(const std::string& dex_location,
451 const std::string& odex_location,
452 CompilerFilter::Filter filter,
453 bool expect_large) {
454 // Host/target independent checks.
455 std::string error_msg;
456 std::unique_ptr<OatFile> odex_file(OatFile::Open(odex_location.c_str(),
457 odex_location.c_str(),
458 nullptr,
459 nullptr,
460 false,
461 /*low_4gb*/false,
462 dex_location.c_str(),
463 &error_msg));
464 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
465 if (expect_large) {
466 // Note: we cannot check the following:
467 // EXPECT_TRUE(CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime,
468 // odex_file->GetCompilerFilter()));
469 // The reason is that the filter override currently happens when the dex files are
470 // loaded in dex2oat, which is after the oat file has been started. Thus, the header
471 // store cannot be changed, and the original filter is set in stone.
472
473 for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
474 std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
475 ASSERT_TRUE(dex_file != nullptr);
476 uint32_t class_def_count = dex_file->NumClassDefs();
477 ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
478 for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
479 OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
480 EXPECT_EQ(oat_class.GetType(), OatClassType::kOatClassNoneCompiled);
481 }
482 }
483
484 // If the input filter was "below," it should have been used.
485 if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kVerifyAtRuntime, filter)) {
486 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
487 }
488 } else {
489 EXPECT_EQ(odex_file->GetCompilerFilter(), filter);
490 }
491
492 // Host/target dependent checks.
493 if (kIsTargetBuild) {
494 CheckTargetResult(expect_large);
495 } else {
496 CheckHostResult(expect_large);
497 }
498 }
499
500 void CheckTargetResult(bool expect_large ATTRIBUTE_UNUSED) {
501 // TODO: Ignore for now. May do something for fd things.
502 }
503
504 void CheckHostResult(bool expect_large) {
505 if (!kIsTargetBuild) {
506 if (expect_large) {
507 EXPECT_NE(output_.find("Very large app, downgrading to verify-at-runtime."),
508 std::string::npos)
509 << output_;
510 } else {
511 EXPECT_EQ(output_.find("Very large app, downgrading to verify-at-runtime."),
512 std::string::npos)
513 << output_;
514 }
515 }
516 }
517
518 // Check whether the dex2oat run was really successful.
519 void CheckValidity() {
520 if (kIsTargetBuild) {
521 CheckTargetValidity();
522 } else {
523 CheckHostValidity();
524 }
525 }
526
527 void CheckTargetValidity() {
528 // TODO: Ignore for now.
529 }
530
531 // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
532 void CheckHostValidity() {
533 EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
534 }
535};
536
537TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
538 RunTest(CompilerFilter::kVerifyNone, false);
539 RunTest(CompilerFilter::kVerifyAtRuntime, false);
540 RunTest(CompilerFilter::kInterpretOnly, false);
541 RunTest(CompilerFilter::kSpeed, false);
542
543 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=1000000" });
544 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=1000000" });
545 RunTest(CompilerFilter::kInterpretOnly, false, { "--very-large-app-threshold=1000000" });
546 RunTest(CompilerFilter::kSpeed, false, { "--very-large-app-threshold=1000000" });
547}
548
549TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
550 RunTest(CompilerFilter::kVerifyNone, false, { "--very-large-app-threshold=100" });
551 RunTest(CompilerFilter::kVerifyAtRuntime, false, { "--very-large-app-threshold=100" });
552 RunTest(CompilerFilter::kInterpretOnly, true, { "--very-large-app-threshold=100" });
553 RunTest(CompilerFilter::kSpeed, true, { "--very-large-app-threshold=100" });
554}
555
Andreas Gampee1459ae2016-06-29 09:36:30 -0700556} // namespace art