• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include <sys/wait.h>
18 #include <unistd.h>
19 
20 #include <algorithm>
21 #include <iterator>
22 #include <regex>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 #include "android-base/logging.h"
28 #include "android-base/macros.h"
29 #include "android-base/stringprintf.h"
30 #include "arch/instruction_set_features.h"
31 #include "base/macros.h"
32 #include "base/mutex-inl.h"
33 #include "base/string_view_cpp20.h"
34 #include "base/utils.h"
35 #include "base/zip_archive.h"
36 #include "common_runtime_test.h"
37 #include "dex/art_dex_file_loader.h"
38 #include "dex/base64_test_util.h"
39 #include "dex/bytecode_utils.h"
40 #include "dex/class_accessor-inl.h"
41 #include "dex/code_item_accessors-inl.h"
42 #include "dex/dex_file-inl.h"
43 #include "dex/dex_file_loader.h"
44 #include "dex2oat_environment_test.h"
45 #include "elf_file.h"
46 #include "elf_file_impl.h"
47 #include "gc_root-inl.h"
48 #include "intern_table-inl.h"
49 #include "oat.h"
50 #include "oat_file.h"
51 #include "profile/profile_compilation_info.h"
52 #include "vdex_file.h"
53 #include "ziparchive/zip_writer.h"
54 
55 namespace art {
56 
57 using android::base::StringPrintf;
58 
59 class Dex2oatTest : public Dex2oatEnvironmentTest {
60  public:
TearDown()61   void TearDown() override {
62     Dex2oatEnvironmentTest::TearDown();
63 
64     output_ = "";
65     error_msg_ = "";
66   }
67 
68  protected:
GenerateOdexForTestWithStatus(const std::vector<std::string> & dex_locations,const std::string & odex_location,CompilerFilter::Filter filter,std::string * error_msg,const std::vector<std::string> & extra_args={},bool use_fd=false)69   int GenerateOdexForTestWithStatus(const std::vector<std::string>& dex_locations,
70                                     const std::string& odex_location,
71                                     CompilerFilter::Filter filter,
72                                     std::string* error_msg,
73                                     const std::vector<std::string>& extra_args = {},
74                                     bool use_fd = false) {
75     std::unique_ptr<File> oat_file;
76     std::vector<std::string> args;
77     // Add dex file args.
78     for (const std::string& dex_location : dex_locations) {
79       args.push_back("--dex-file=" + dex_location);
80     }
81     if (use_fd) {
82       oat_file.reset(OS::CreateEmptyFile(odex_location.c_str()));
83       CHECK(oat_file != nullptr) << odex_location;
84       args.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
85       args.push_back("--oat-location=" + odex_location);
86     } else {
87       args.push_back("--oat-file=" + odex_location);
88     }
89     args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
90     args.push_back("--runtime-arg");
91     args.push_back("-Xnorelocate");
92 
93     // Unless otherwise stated, use a small amount of threads, so that potential aborts are
94     // shorter. This can be overridden with extra_args.
95     args.push_back("-j4");
96 
97     args.insert(args.end(), extra_args.begin(), extra_args.end());
98 
99     int status = Dex2Oat(args, &output_, error_msg);
100     if (oat_file != nullptr) {
101       CHECK_EQ(oat_file->FlushClose(), 0) << "Could not flush and close oat file";
102     }
103     return status;
104   }
105 
GenerateOdexForTest(const std::string & dex_location,const std::string & odex_location,CompilerFilter::Filter filter,const std::vector<std::string> & extra_args={},bool expect_success=true,bool use_fd=false,bool use_zip_fd=false)106   ::testing::AssertionResult GenerateOdexForTest(
107       const std::string& dex_location,
108       const std::string& odex_location,
109       CompilerFilter::Filter filter,
110       const std::vector<std::string>& extra_args = {},
111       bool expect_success = true,
112       bool use_fd = false,
113       bool use_zip_fd = false) WARN_UNUSED {
114     return GenerateOdexForTest(dex_location,
115                                odex_location,
116                                filter,
117                                extra_args,
118                                expect_success,
119                                use_fd,
120                                use_zip_fd,
__anon1b5c20110102(const OatFile&) 121                                [](const OatFile&) {});
122   }
123 
124   bool test_accepts_odex_file_on_failure = false;
125 
126   template <typename T>
GenerateOdexForTest(const std::string & dex_location,const std::string & odex_location,CompilerFilter::Filter filter,const std::vector<std::string> & extra_args,bool expect_success,bool use_fd,bool use_zip_fd,T check_oat)127   ::testing::AssertionResult GenerateOdexForTest(
128       const std::string& dex_location,
129       const std::string& odex_location,
130       CompilerFilter::Filter filter,
131       const std::vector<std::string>& extra_args,
132       bool expect_success,
133       bool use_fd,
134       bool use_zip_fd,
135       T check_oat) WARN_UNUSED {
136     std::vector<std::string> dex_locations;
137     if (use_zip_fd) {
138       std::string loc_arg = "--zip-location=" + dex_location;
139       CHECK(std::any_of(extra_args.begin(),
140                         extra_args.end(),
141                         [&](const std::string& s) { return s == loc_arg; }));
142       CHECK(std::any_of(extra_args.begin(),
143                         extra_args.end(),
144                         [](const std::string& s) { return StartsWith(s, "--zip-fd="); }));
145     } else {
146       dex_locations.push_back(dex_location);
147     }
148     std::string error_msg;
149     int status = GenerateOdexForTestWithStatus(dex_locations,
150                                                odex_location,
151                                                filter,
152                                                &error_msg,
153                                                extra_args,
154                                                use_fd);
155     bool success = (WIFEXITED(status) && WEXITSTATUS(status) == 0);
156     if (expect_success) {
157       if (!success) {
158         return ::testing::AssertionFailure()
159             << "Failed to compile odex: " << error_msg << std::endl << output_;
160       }
161 
162       // Verify the odex file was generated as expected.
163       std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
164                                                        odex_location.c_str(),
165                                                        odex_location.c_str(),
166                                                        /*executable=*/ false,
167                                                        /*low_4gb=*/ false,
168                                                        dex_location,
169                                                        &error_msg));
170       if (odex_file == nullptr) {
171         return ::testing::AssertionFailure() << "Could not open odex file: " << error_msg;
172       }
173 
174       CheckFilter(filter, odex_file->GetCompilerFilter());
175       check_oat(*(odex_file.get()));
176     } else {
177       if (success) {
178         return ::testing::AssertionFailure() << "Succeeded to compile odex: " << output_;
179       }
180 
181       error_msg_ = error_msg;
182 
183       if (!test_accepts_odex_file_on_failure) {
184         // Verify there's no loadable odex file.
185         std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
186                                                          odex_location.c_str(),
187                                                          odex_location.c_str(),
188                                                          /*executable=*/ false,
189                                                          /*low_4gb=*/ false,
190                                                          dex_location,
191                                                          &error_msg));
192         if (odex_file != nullptr) {
193           return ::testing::AssertionFailure() << "Could open odex file: " << error_msg;
194         }
195       }
196     }
197     return ::testing::AssertionSuccess();
198   }
199 
200   // Check the input compiler filter against the generated oat file's filter. May be overridden
201   // in subclasses when equality is not expected.
CheckFilter(CompilerFilter::Filter expected,CompilerFilter::Filter actual)202   virtual void CheckFilter(CompilerFilter::Filter expected, CompilerFilter::Filter actual) {
203     EXPECT_EQ(expected, actual);
204   }
205 
206   std::string output_ = "";
207   std::string error_msg_ = "";
208 };
209 
210 // This test class provides an easy way to validate an expected filter which is different
211 // then the one pass to generate the odex file (compared to adding yet another argument
212 // to what's already huge test methods).
213 class Dex2oatWithExpectedFilterTest : public Dex2oatTest {
214  protected:
CheckFilter(CompilerFilter::Filter expected ATTRIBUTE_UNUSED,CompilerFilter::Filter actual)215   void CheckFilter(
216         CompilerFilter::Filter expected ATTRIBUTE_UNUSED,
217         CompilerFilter::Filter actual) override {
218     EXPECT_EQ(expected_filter_, actual);
219   }
220 
221   CompilerFilter::Filter expected_filter_;
222 };
223 
224 class Dex2oatSwapTest : public Dex2oatTest {
225  protected:
RunTest(bool use_fd,bool expect_use,const std::vector<std::string> & extra_args={})226   void RunTest(bool use_fd, bool expect_use, const std::vector<std::string>& extra_args = {}) {
227     std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
228     std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
229 
230     Copy(GetTestDexFileName(), dex_location);
231 
232     std::vector<std::string> copy(extra_args);
233 
234     std::unique_ptr<ScratchFile> sf;
235     if (use_fd) {
236       sf.reset(new ScratchFile());
237       copy.push_back(android::base::StringPrintf("--swap-fd=%d", sf->GetFd()));
238     } else {
239       std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
240       copy.push_back("--swap-file=" + swap_location);
241     }
242     ASSERT_TRUE(GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed, copy));
243 
244     CheckValidity();
245     CheckResult(expect_use);
246   }
247 
GetTestDexFileName()248   virtual std::string GetTestDexFileName() {
249     return Dex2oatEnvironmentTest::GetTestDexFileName("VerifierDeps");
250   }
251 
CheckResult(bool expect_use)252   virtual void CheckResult(bool expect_use) {
253     if (kIsTargetBuild) {
254       CheckTargetResult(expect_use);
255     } else {
256       CheckHostResult(expect_use);
257     }
258   }
259 
CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED)260   virtual void CheckTargetResult(bool expect_use ATTRIBUTE_UNUSED) {
261     // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
262     //       something for variants with file descriptor where we can control the lifetime of
263     //       the swap file and thus take a look at it.
264   }
265 
CheckHostResult(bool expect_use)266   virtual void CheckHostResult(bool expect_use) {
267     if (!kIsTargetBuild) {
268       if (expect_use) {
269         EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
270             << output_;
271       } else {
272         EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
273             << output_;
274       }
275     }
276   }
277 
278   // Check whether the dex2oat run was really successful.
CheckValidity()279   virtual void CheckValidity() {
280     if (kIsTargetBuild) {
281       CheckTargetValidity();
282     } else {
283       CheckHostValidity();
284     }
285   }
286 
CheckTargetValidity()287   virtual void CheckTargetValidity() {
288     // TODO: Ignore for now, as we won't capture any output (it goes to the logcat). We may do
289     //       something for variants with file descriptor where we can control the lifetime of
290     //       the swap file and thus take a look at it.
291   }
292 
293   // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
CheckHostValidity()294   virtual void CheckHostValidity() {
295     EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
296   }
297 };
298 
TEST_F(Dex2oatSwapTest,DoNotUseSwapDefaultSingleSmall)299 TEST_F(Dex2oatSwapTest, DoNotUseSwapDefaultSingleSmall) {
300   RunTest(/*use_fd=*/ false, /*expect_use=*/ false);
301   RunTest(/*use_fd=*/ true, /*expect_use=*/ false);
302 }
303 
TEST_F(Dex2oatSwapTest,DoNotUseSwapSingle)304 TEST_F(Dex2oatSwapTest, DoNotUseSwapSingle) {
305   RunTest(/*use_fd=*/ false, /*expect_use=*/ false, { "--swap-dex-size-threshold=0" });
306   RunTest(/*use_fd=*/ true, /*expect_use=*/ false, { "--swap-dex-size-threshold=0" });
307 }
308 
TEST_F(Dex2oatSwapTest,DoNotUseSwapSmall)309 TEST_F(Dex2oatSwapTest, DoNotUseSwapSmall) {
310   RunTest(/*use_fd=*/ false, /*expect_use=*/ false, { "--swap-dex-count-threshold=0" });
311   RunTest(/*use_fd=*/ true, /*expect_use=*/ false, { "--swap-dex-count-threshold=0" });
312 }
313 
TEST_F(Dex2oatSwapTest,DoUseSwapSingleSmall)314 TEST_F(Dex2oatSwapTest, DoUseSwapSingleSmall) {
315   RunTest(/*use_fd=*/ false,
316           /*expect_use=*/ true,
317           { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
318   RunTest(/*use_fd=*/ true,
319           /*expect_use=*/ true,
320           { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
321 }
322 
323 class Dex2oatSwapUseTest : public Dex2oatSwapTest {
324  protected:
CheckHostResult(bool expect_use)325   void CheckHostResult(bool expect_use) override {
326     if (!kIsTargetBuild) {
327       if (expect_use) {
328         EXPECT_NE(output_.find("Large app, accepted running with swap."), std::string::npos)
329             << output_;
330       } else {
331         EXPECT_EQ(output_.find("Large app, accepted running with swap."), std::string::npos)
332             << output_;
333       }
334     }
335   }
336 
GetTestDexFileName()337   std::string GetTestDexFileName() override {
338     // Use Statics as it has a handful of functions.
339     return CommonRuntimeTest::GetTestDexFileName("Statics");
340   }
341 
GrabResult1()342   void GrabResult1() {
343     if (!kIsTargetBuild) {
344       native_alloc_1_ = ParseNativeAlloc();
345       swap_1_ = ParseSwap(/*expected=*/ false);
346     } else {
347       native_alloc_1_ = std::numeric_limits<size_t>::max();
348       swap_1_ = 0;
349     }
350   }
351 
GrabResult2()352   void GrabResult2() {
353     if (!kIsTargetBuild) {
354       native_alloc_2_ = ParseNativeAlloc();
355       swap_2_ = ParseSwap(/*expected=*/ true);
356     } else {
357       native_alloc_2_ = 0;
358       swap_2_ = std::numeric_limits<size_t>::max();
359     }
360   }
361 
362  private:
ParseNativeAlloc()363   size_t ParseNativeAlloc() {
364     std::regex native_alloc_regex("dex2oat took.*native alloc=[^ ]+ \\(([0-9]+)B\\)");
365     std::smatch native_alloc_match;
366     bool found = std::regex_search(output_, native_alloc_match, native_alloc_regex);
367     if (!found) {
368       EXPECT_TRUE(found);
369       return 0;
370     }
371     if (native_alloc_match.size() != 2U) {
372       EXPECT_EQ(native_alloc_match.size(), 2U);
373       return 0;
374     }
375 
376     std::istringstream stream(native_alloc_match[1].str());
377     size_t value;
378     stream >> value;
379 
380     return value;
381   }
382 
ParseSwap(bool expected)383   size_t ParseSwap(bool expected) {
384     std::regex swap_regex("dex2oat took[^\\n]+swap=[^ ]+ \\(([0-9]+)B\\)");
385     std::smatch swap_match;
386     bool found = std::regex_search(output_, swap_match, swap_regex);
387     if (found != expected) {
388       EXPECT_EQ(expected, found);
389       return 0;
390     }
391 
392     if (!found) {
393       return 0;
394     }
395 
396     if (swap_match.size() != 2U) {
397       EXPECT_EQ(swap_match.size(), 2U);
398       return 0;
399     }
400 
401     std::istringstream stream(swap_match[1].str());
402     size_t value;
403     stream >> value;
404 
405     return value;
406   }
407 
408  protected:
409   size_t native_alloc_1_;
410   size_t native_alloc_2_;
411 
412   size_t swap_1_;
413   size_t swap_2_;
414 };
415 
TEST_F(Dex2oatSwapUseTest,CheckSwapUsage)416 TEST_F(Dex2oatSwapUseTest, CheckSwapUsage) {
417   // Native memory usage isn't correctly tracked when running under ASan.
418   TEST_DISABLED_FOR_MEMORY_TOOL();
419 
420   // The `native_alloc_2_ >= native_alloc_1_` assertion below may not
421   // hold true on some x86 or x86_64 systems; disable this test while we
422   // investigate (b/29259363).
423   TEST_DISABLED_FOR_X86();
424   TEST_DISABLED_FOR_X86_64();
425 
426   RunTest(/*use_fd=*/ false,
427           /*expect_use=*/ false);
428   GrabResult1();
429   std::string output_1 = output_;
430 
431   output_ = "";
432 
433   RunTest(/*use_fd=*/ false,
434           /*expect_use=*/ true,
435           { "--swap-dex-size-threshold=0", "--swap-dex-count-threshold=0" });
436   GrabResult2();
437   std::string output_2 = output_;
438 
439   if (native_alloc_2_ >= native_alloc_1_ || swap_1_ >= swap_2_) {
440     EXPECT_LT(native_alloc_2_, native_alloc_1_);
441     EXPECT_LT(swap_1_, swap_2_);
442 
443     LOG(ERROR) << output_1;
444     LOG(ERROR) << output_2;
445   }
446 }
447 
448 class Dex2oatVeryLargeTest : public Dex2oatTest {
449  protected:
CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,CompilerFilter::Filter result ATTRIBUTE_UNUSED)450   void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
451                    CompilerFilter::Filter result ATTRIBUTE_UNUSED) override {
452     // Ignore, we'll do our own checks.
453   }
454 
RunTest(CompilerFilter::Filter filter,bool expect_large,bool expect_downgrade,const std::vector<std::string> & extra_args={})455   void RunTest(CompilerFilter::Filter filter,
456                bool expect_large,
457                bool expect_downgrade,
458                const std::vector<std::string>& extra_args = {}) {
459     RunTest(filter, filter, expect_large, expect_downgrade, extra_args);
460   }
461 
RunTest(CompilerFilter::Filter filter,CompilerFilter::Filter expected_filter,bool expect_large,bool expect_downgrade,const std::vector<std::string> & extra_args={})462   void RunTest(CompilerFilter::Filter filter,
463                CompilerFilter::Filter expected_filter,
464                bool expect_large,
465                bool expect_downgrade,
466                const std::vector<std::string>& extra_args = {}) {
467     std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
468     std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
469     std::string app_image_file = GetScratchDir() + "/Test.art";
470 
471     Copy(GetDexSrc1(), dex_location);
472 
473     std::vector<std::string> new_args(extra_args);
474     new_args.push_back("--app-image-file=" + app_image_file);
475     ASSERT_TRUE(GenerateOdexForTest(dex_location, odex_location, filter, new_args));
476 
477     CheckValidity();
478     CheckResult(dex_location,
479                 odex_location,
480                 app_image_file,
481                 filter,
482                 expected_filter,
483                 expect_large,
484                 expect_downgrade);
485   }
486 
CheckResult(const std::string & dex_location,const std::string & odex_location,const std::string & app_image_file,CompilerFilter::Filter filter,CompilerFilter::Filter expected_filter,bool expect_large,bool expect_downgrade)487   void CheckResult(const std::string& dex_location,
488                    const std::string& odex_location,
489                    const std::string& app_image_file,
490                    CompilerFilter::Filter filter,
491                    CompilerFilter::Filter expected_filter,
492                    bool expect_large,
493                    bool expect_downgrade) {
494     if (expect_downgrade) {
495       EXPECT_TRUE(expect_large);
496     }
497     // Host/target independent checks.
498     std::string error_msg;
499     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
500                                                      odex_location.c_str(),
501                                                      odex_location.c_str(),
502                                                      /*executable=*/ false,
503                                                      /*low_4gb=*/ false,
504                                                      dex_location,
505                                                      &error_msg));
506     ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
507     EXPECT_GT(app_image_file.length(), 0u);
508     std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file.c_str()));
509     if (expect_large) {
510       // Note: we cannot check the following
511       // EXPECT_FALSE(CompilerFilter::IsAotCompilationEnabled(odex_file->GetCompilerFilter()));
512       // The reason is that the filter override currently happens when the dex files are
513       // loaded in dex2oat, which is after the oat file has been started. Thus, the header
514       // store cannot be changed, and the original filter is set in stone.
515 
516       for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
517         std::unique_ptr<const DexFile> dex_file = oat_dex_file->OpenDexFile(&error_msg);
518         ASSERT_TRUE(dex_file != nullptr);
519         uint32_t class_def_count = dex_file->NumClassDefs();
520         ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
521         for (uint16_t class_def_index = 0; class_def_index < class_def_count; ++class_def_index) {
522           OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
523           EXPECT_EQ(oat_class.GetType(), OatClassType::kNoneCompiled);
524         }
525       }
526 
527       // If the input filter was "below," it should have been used.
528       if (!CompilerFilter::IsAsGoodAs(CompilerFilter::kExtract, filter)) {
529         EXPECT_EQ(odex_file->GetCompilerFilter(), expected_filter);
530       }
531 
532       // If expect large, make sure the app image isn't generated or is empty.
533       if (file != nullptr) {
534         EXPECT_EQ(file->GetLength(), 0u);
535       }
536     } else {
537       EXPECT_EQ(odex_file->GetCompilerFilter(), expected_filter);
538       ASSERT_TRUE(file != nullptr) << app_image_file;
539       EXPECT_GT(file->GetLength(), 0u);
540     }
541 
542     // Host/target dependent checks.
543     if (kIsTargetBuild) {
544       CheckTargetResult(expect_downgrade);
545     } else {
546       CheckHostResult(expect_downgrade);
547     }
548   }
549 
CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED)550   void CheckTargetResult(bool expect_downgrade ATTRIBUTE_UNUSED) {
551     // TODO: Ignore for now. May do something for fd things.
552   }
553 
CheckHostResult(bool expect_downgrade)554   void CheckHostResult(bool expect_downgrade) {
555     if (!kIsTargetBuild) {
556       if (expect_downgrade) {
557         EXPECT_NE(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
558       } else {
559         EXPECT_EQ(output_.find("Very large app, downgrading to"), std::string::npos) << output_;
560       }
561     }
562   }
563 
564   // Check whether the dex2oat run was really successful.
CheckValidity()565   void CheckValidity() {
566     if (kIsTargetBuild) {
567       CheckTargetValidity();
568     } else {
569       CheckHostValidity();
570     }
571   }
572 
CheckTargetValidity()573   void CheckTargetValidity() {
574     // TODO: Ignore for now.
575   }
576 
577   // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
CheckHostValidity()578   void CheckHostValidity() {
579     EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
580   }
581 };
582 
TEST_F(Dex2oatVeryLargeTest,DontUseVeryLarge)583 TEST_F(Dex2oatVeryLargeTest, DontUseVeryLarge) {
584   RunTest(CompilerFilter::kAssumeVerified, false, false);
585   RunTest(CompilerFilter::kExtract, false, false);
586   RunTest(CompilerFilter::kSpeed, false, false);
587 
588   RunTest(CompilerFilter::kAssumeVerified, false, false, { "--very-large-app-threshold=10000000" });
589   RunTest(CompilerFilter::kExtract, false, false, { "--very-large-app-threshold=10000000" });
590   RunTest(CompilerFilter::kSpeed, false, false, { "--very-large-app-threshold=10000000" });
591 }
592 
TEST_F(Dex2oatVeryLargeTest,UseVeryLarge)593 TEST_F(Dex2oatVeryLargeTest, UseVeryLarge) {
594   RunTest(CompilerFilter::kAssumeVerified, true, false, { "--very-large-app-threshold=100" });
595   RunTest(CompilerFilter::kExtract, true, false, { "--very-large-app-threshold=100" });
596   RunTest(CompilerFilter::kSpeed, true, true, { "--very-large-app-threshold=100" });
597 }
598 
599 // Regressin test for b/35665292.
TEST_F(Dex2oatVeryLargeTest,SpeedProfileNoProfile)600 TEST_F(Dex2oatVeryLargeTest, SpeedProfileNoProfile) {
601   // Test that dex2oat doesn't crash with speed-profile but no input profile.
602   RunTest(CompilerFilter::kSpeedProfile, CompilerFilter::kVerify, false, false);
603 }
604 
605 class Dex2oatLayoutTest : public Dex2oatTest {
606  protected:
CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,CompilerFilter::Filter result ATTRIBUTE_UNUSED)607   void CheckFilter(CompilerFilter::Filter input ATTRIBUTE_UNUSED,
608                    CompilerFilter::Filter result ATTRIBUTE_UNUSED) override {
609     // Ignore, we'll do our own checks.
610   }
611 
612   // Emits a profile with a single dex file with the given location and classes ranging
613   // from `class_offset` to `class_offset + num_classes`.
GenerateProfile(const std::string & test_profile,const std::string & dex_location,size_t num_classes,size_t class_offset=0)614   void GenerateProfile(const std::string& test_profile,
615                        const std::string& dex_location,
616                        size_t num_classes,
617                        size_t class_offset = 0) {
618     const char* location = dex_location.c_str();
619     std::string error_msg;
620     std::vector<std::unique_ptr<const DexFile>> dex_files;
621     const ArtDexFileLoader dex_file_loader;
622     ASSERT_TRUE(dex_file_loader.Open(
623         location, location, /*verify=*/true, /*verify_checksum=*/true, &error_msg, &dex_files));
624     EXPECT_EQ(dex_files.size(), 1U);
625     std::unique_ptr<const DexFile>& dex_file = dex_files[0];
626 
627     int profile_test_fd = open(test_profile.c_str(),
628                                O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
629                                0644);
630     CHECK_GE(profile_test_fd, 0);
631 
632     ProfileCompilationInfo info;
633     std::vector<dex::TypeIndex> classes;;
634     for (size_t i = 0; i < num_classes; ++i) {
635       classes.push_back(dex::TypeIndex(class_offset + 1 + i));
636     }
637     info.AddClassesForDex(dex_file.get(), classes.begin(), classes.end());
638     bool result = info.Save(profile_test_fd);
639     close(profile_test_fd);
640     ASSERT_TRUE(result);
641   }
642 
643   // Compiles a dex file with profiles.
CompileProfileOdex(const std::string & dex_location,const std::string & odex_location,const std::string & app_image_file_name,bool use_fd,const std::vector<std::string> & profile_locations,const std::vector<std::string> & extra_args={},bool expect_success=true)644   void CompileProfileOdex(const std::string& dex_location,
645                           const std::string& odex_location,
646                           const std::string& app_image_file_name,
647                           bool use_fd,
648                           const std::vector<std::string>& profile_locations,
649                           const std::vector<std::string>& extra_args = {},
650                           bool expect_success = true) {
651     std::vector<std::string> copy(extra_args);
652     for (const std::string& profile_location : profile_locations) {
653       copy.push_back("--profile-file=" + profile_location);
654     }
655     std::unique_ptr<File> app_image_file;
656     if (!app_image_file_name.empty()) {
657       if (use_fd) {
658         app_image_file.reset(OS::CreateEmptyFile(app_image_file_name.c_str()));
659         copy.push_back("--app-image-fd=" + std::to_string(app_image_file->Fd()));
660       } else {
661         copy.push_back("--app-image-file=" + app_image_file_name);
662       }
663     }
664     ASSERT_TRUE(GenerateOdexForTest(dex_location,
665                                     odex_location,
666                                     CompilerFilter::kSpeedProfile,
667                                     copy,
668                                     expect_success,
669                                     use_fd));
670     if (app_image_file != nullptr) {
671       ASSERT_EQ(app_image_file->FlushCloseOrErase(), 0) << "Could not flush and close art file";
672     }
673   }
674 
675   // Same as above, but generates the profile internally with classes ranging from 0 to
676   // `num_profile_classes`.
CompileProfileOdex(const std::string & dex_location,const std::string & odex_location,const std::string & app_image_file_name,bool use_fd,size_t num_profile_classes,const std::vector<std::string> & extra_args={},bool expect_success=true)677   void CompileProfileOdex(const std::string& dex_location,
678                           const std::string& odex_location,
679                           const std::string& app_image_file_name,
680                           bool use_fd,
681                           size_t num_profile_classes,
682                           const std::vector<std::string>& extra_args = {},
683                           bool expect_success = true) {
684     const std::string profile_location = GetScratchDir() + "/primary.prof";
685     GenerateProfile(profile_location, dex_location, num_profile_classes);
686     CompileProfileOdex(dex_location,
687                        odex_location,
688                        app_image_file_name,
689                        use_fd,
690                        {profile_location},
691                        extra_args,
692                        expect_success);
693   }
694 
GetImageObjectSectionSize(const std::string & image_file_name)695   uint32_t GetImageObjectSectionSize(const std::string& image_file_name) {
696     EXPECT_FALSE(image_file_name.empty());
697     std::unique_ptr<File> file(OS::OpenFileForReading(image_file_name.c_str()));
698     CHECK(file != nullptr);
699     ImageHeader image_header;
700     const bool success = file->ReadFully(&image_header, sizeof(image_header));
701     CHECK(success);
702     CHECK(image_header.IsValid());
703     ReaderMutexLock mu(Thread::Current(), *Locks::mutator_lock_);
704     return image_header.GetObjectsSection().Size();
705   }
706 
RunTest(bool app_image)707   void RunTest(bool app_image) {
708     std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
709     std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
710     std::string app_image_file = app_image ? (GetOdexDir() + "/DexOdexNoOat.art"): "";
711     Copy(GetDexSrc2(), dex_location);
712 
713     uint32_t image_file_empty_profile = 0;
714     if (app_image) {
715       CompileProfileOdex(dex_location,
716                          odex_location,
717                          app_image_file,
718                          /*use_fd=*/ false,
719                          /*num_profile_classes=*/ 0);
720       CheckValidity();
721       // Don't check the result since CheckResult relies on the class being in the profile.
722       image_file_empty_profile = GetImageObjectSectionSize(app_image_file);
723       EXPECT_GT(image_file_empty_profile, 0u);
724       CheckCompilerFilter(dex_location, odex_location, CompilerFilter::Filter::kVerify);
725     }
726 
727     // Small profile.
728     CompileProfileOdex(dex_location,
729                        odex_location,
730                        app_image_file,
731                        /*use_fd=*/ false,
732                        /*num_profile_classes=*/ 1);
733     CheckValidity();
734     CheckResult(dex_location, odex_location, app_image_file);
735     CheckCompilerFilter(dex_location, odex_location, CompilerFilter::Filter::kSpeedProfile);
736 
737     if (app_image) {
738       // Test that the profile made a difference by adding more classes.
739       const uint32_t image_file_small_profile = GetImageObjectSectionSize(app_image_file);
740       ASSERT_LT(image_file_empty_profile, image_file_small_profile);
741     }
742   }
743 
CheckCompilerFilter(const std::string & dex_location,const std::string & odex_location,CompilerFilter::Filter expected_filter)744   void CheckCompilerFilter(
745       const std::string& dex_location,
746       const std::string& odex_location,
747       CompilerFilter::Filter expected_filter) {
748     std::string error_msg;
749     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
750                                                      odex_location.c_str(),
751                                                      odex_location.c_str(),
752                                                      /*executable=*/ false,
753                                                      /*low_4gb=*/ false,
754                                                      dex_location,
755                                                      &error_msg));
756     EXPECT_EQ(odex_file->GetCompilerFilter(), expected_filter);
757   }
758 
RunTestVDex()759   void RunTestVDex() {
760     std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
761     std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
762     std::string vdex_location = GetOdexDir() + "/DexOdexNoOat.vdex";
763     std::string app_image_file_name = GetOdexDir() + "/DexOdexNoOat.art";
764     Copy(GetDexSrc2(), dex_location);
765 
766     std::unique_ptr<File> vdex_file1(OS::CreateEmptyFile(vdex_location.c_str()));
767     CHECK(vdex_file1 != nullptr) << vdex_location;
768     ScratchFile vdex_file2;
769     {
770       std::string input_vdex = "--input-vdex-fd=-1";
771       std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file1->Fd());
772       CompileProfileOdex(dex_location,
773                          odex_location,
774                          app_image_file_name,
775                          /*use_fd=*/ true,
776                          /*num_profile_classes=*/ 1,
777                          { input_vdex, output_vdex });
778       EXPECT_GT(vdex_file1->GetLength(), 0u);
779     }
780     {
781       // Test that vdex and dexlayout fail gracefully.
782       std::string input_vdex = StringPrintf("--input-vdex-fd=%d", vdex_file1->Fd());
783       std::string output_vdex = StringPrintf("--output-vdex-fd=%d", vdex_file2.GetFd());
784       CompileProfileOdex(dex_location,
785                          odex_location,
786                          app_image_file_name,
787                          /*use_fd=*/ true,
788                          /*num_profile_classes=*/ 1,
789                          { input_vdex, output_vdex },
790                          /*expect_success=*/ true);
791       EXPECT_GT(vdex_file2.GetFile()->GetLength(), 0u);
792     }
793     ASSERT_EQ(vdex_file1->FlushCloseOrErase(), 0) << "Could not flush and close vdex file";
794     CheckValidity();
795   }
796 
CheckResult(const std::string & dex_location,const std::string & odex_location,const std::string & app_image_file_name)797   void CheckResult(const std::string& dex_location,
798                    const std::string& odex_location,
799                    const std::string& app_image_file_name) {
800     // Host/target independent checks.
801     std::string error_msg;
802     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
803                                                      odex_location.c_str(),
804                                                      odex_location.c_str(),
805                                                      /*executable=*/ false,
806                                                      /*low_4gb=*/ false,
807                                                      dex_location,
808                                                      &error_msg));
809     ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
810 
811     const char* location = dex_location.c_str();
812     std::vector<std::unique_ptr<const DexFile>> dex_files;
813     const ArtDexFileLoader dex_file_loader;
814     ASSERT_TRUE(dex_file_loader.Open(
815         location, location, /*verify=*/ true, /*verify_checksum=*/ true, &error_msg, &dex_files));
816     EXPECT_EQ(dex_files.size(), 1U);
817     std::unique_ptr<const DexFile>& old_dex_file = dex_files[0];
818 
819     for (const OatDexFile* oat_dex_file : odex_file->GetOatDexFiles()) {
820       std::unique_ptr<const DexFile> new_dex_file = oat_dex_file->OpenDexFile(&error_msg);
821       ASSERT_TRUE(new_dex_file != nullptr);
822       uint32_t class_def_count = new_dex_file->NumClassDefs();
823       ASSERT_LT(class_def_count, std::numeric_limits<uint16_t>::max());
824       ASSERT_GE(class_def_count, 2U);
825 
826       // Make sure the indexes stay the same.
827       std::string old_class0 = old_dex_file->PrettyType(old_dex_file->GetClassDef(0).class_idx_);
828       std::string old_class1 = old_dex_file->PrettyType(old_dex_file->GetClassDef(1).class_idx_);
829       std::string new_class0 = new_dex_file->PrettyType(new_dex_file->GetClassDef(0).class_idx_);
830       std::string new_class1 = new_dex_file->PrettyType(new_dex_file->GetClassDef(1).class_idx_);
831       EXPECT_EQ(old_class0, new_class0);
832       EXPECT_EQ(old_class1, new_class1);
833     }
834 
835     EXPECT_EQ(odex_file->GetCompilerFilter(), CompilerFilter::kSpeedProfile);
836 
837     if (!app_image_file_name.empty()) {
838       // Go peek at the image header to make sure it was large enough to contain the class.
839       std::unique_ptr<File> file(OS::OpenFileForReading(app_image_file_name.c_str()));
840       ImageHeader image_header;
841       bool success = file->ReadFully(&image_header, sizeof(image_header));
842       ASSERT_TRUE(success);
843       ASSERT_TRUE(image_header.IsValid());
844       EXPECT_GT(image_header.GetObjectsSection().Size(), 0u);
845     }
846   }
847 
848   // Check whether the dex2oat run was really successful.
CheckValidity()849   void CheckValidity() {
850     if (kIsTargetBuild) {
851       CheckTargetValidity();
852     } else {
853       CheckHostValidity();
854     }
855   }
856 
CheckTargetValidity()857   void CheckTargetValidity() {
858     // TODO: Ignore for now.
859   }
860 
861   // On the host, we can get the dex2oat output. Here, look for "dex2oat took."
CheckHostValidity()862   void CheckHostValidity() {
863     EXPECT_NE(output_.find("dex2oat took"), std::string::npos) << output_;
864   }
865 };
866 
TEST_F(Dex2oatLayoutTest,TestLayout)867 TEST_F(Dex2oatLayoutTest, TestLayout) {
868   RunTest(/*app_image=*/ false);
869 }
870 
TEST_F(Dex2oatLayoutTest,TestLayoutAppImage)871 TEST_F(Dex2oatLayoutTest, TestLayoutAppImage) {
872   RunTest(/*app_image=*/ true);
873 }
874 
TEST_F(Dex2oatLayoutTest,TestLayoutAppImageMissingBootImage)875 TEST_F(Dex2oatLayoutTest, TestLayoutAppImageMissingBootImage) {
876   std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
877   std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
878   std::string app_image_file = GetOdexDir() + "/DexOdexNoOat.art";
879   Copy(GetDexSrc2(), dex_location);
880 
881   CompileProfileOdex(dex_location,
882                      odex_location,
883                      app_image_file,
884                      /*use_fd=*/ false,
885                      /*num_profile_classes=*/ 1,
886                      /*extra_args=*/ {"--boot-image=/nonx/boot.art"},
887                      /*expect_success=*/ true);
888 
889   // Verify the odex file does not require an image.
890   std::string error_msg;
891   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
892                                                    odex_location.c_str(),
893                                                    odex_location.c_str(),
894                                                    /*executable=*/ false,
895                                                    /*low_4gb=*/ false,
896                                                    dex_location,
897                                                    &error_msg));
898   ASSERT_TRUE(odex_file != nullptr) << "Could not open odex file: " << error_msg;
899 
900   CheckFilter(CompilerFilter::kSpeedProfile, odex_file->GetCompilerFilter());
901   ASSERT_FALSE(odex_file->GetOatHeader().RequiresImage());
902 }
903 
TEST_F(Dex2oatLayoutTest,TestLayoutMultipleProfiles)904 TEST_F(Dex2oatLayoutTest, TestLayoutMultipleProfiles) {
905   std::string dex_location = GetScratchDir() + "/Dex.jar";
906   std::string odex_location = GetOdexDir() + "/Dex.odex";
907   std::string app_image_file = GetOdexDir() + "/Dex.art";
908   Copy(GetDexSrc2(), dex_location);
909 
910   const std::string profile1_location = GetScratchDir() + "/primary.prof";
911   GenerateProfile(profile1_location, dex_location, /*num_classes=*/1, /*class_offset=*/0);
912   CompileProfileOdex(dex_location,
913                      odex_location,
914                      app_image_file,
915                      /*use_fd=*/false,
916                      {profile1_location});
917   uint32_t image_file_size_profile1 = GetImageObjectSectionSize(app_image_file);
918 
919   const std::string profile2_location = GetScratchDir() + "/secondary.prof";
920   GenerateProfile(profile2_location, dex_location, /*num_classes=*/1, /*class_offset=*/1);
921   CompileProfileOdex(dex_location,
922                      odex_location,
923                      app_image_file,
924                      /*use_fd=*/false,
925                      {profile2_location});
926   uint32_t image_file_size_profile2 = GetImageObjectSectionSize(app_image_file);
927 
928   CompileProfileOdex(dex_location,
929                      odex_location,
930                      app_image_file,
931                      /*use_fd=*/false,
932                      {profile1_location, profile2_location});
933   uint32_t image_file_size_multiple_profiles = GetImageObjectSectionSize(app_image_file);
934 
935   CheckCompilerFilter(dex_location, odex_location, CompilerFilter::Filter::kSpeedProfile);
936 
937   // The image file generated with multiple profiles should be larger than any image file generated
938   // with each profile.
939   ASSERT_GT(image_file_size_multiple_profiles, image_file_size_profile1);
940   ASSERT_GT(image_file_size_multiple_profiles, image_file_size_profile2);
941 }
942 
TEST_F(Dex2oatLayoutTest,TestLayoutMultipleProfilesChecksumMismatch)943 TEST_F(Dex2oatLayoutTest, TestLayoutMultipleProfilesChecksumMismatch) {
944   std::string dex_location = GetScratchDir() + "/Dex.jar";
945 
946   // Create two profiles whose dex locations are the same but checksums are different.
947   Copy(GetDexSrc1(), dex_location);
948   const std::string profile_old = GetScratchDir() + "/profile_old.prof";
949   GenerateProfile(profile_old, dex_location, /*num_classes=*/1, /*class_offset=*/0);
950 
951   Copy(GetDexSrc2(), dex_location);
952   const std::string profile_new = GetScratchDir() + "/profile_new.prof";
953   GenerateProfile(profile_new, dex_location, /*num_classes=*/1, /*class_offset=*/0);
954 
955   // Create an empty profile for reference.
956   const std::string profile_empty = GetScratchDir() + "/profile_empty.prof";
957   GenerateProfile(profile_empty, dex_location, /*num_classes=*/0, /*class_offset=*/0);
958 
959   std::string odex_location = GetOdexDir() + "/Dex.odex";
960   std::string app_image_file = GetOdexDir() + "/Dex.art";
961 
962   // This should produce a normal image because only `profile_new` is used and it has the right
963   // checksum.
964   CompileProfileOdex(dex_location,
965                      odex_location,
966                      app_image_file,
967                      /*use_fd=*/false,
968                      {profile_new, profile_old});
969   uint32_t image_size_right_checksum = GetImageObjectSectionSize(app_image_file);
970 
971   // This should produce an empty image because only `profile_old` is used and it has the wrong
972   // checksum. Note that dex2oat does not abort compilation when the profile verification fails
973   // (b/62602192, b/65260586).
974   CompileProfileOdex(dex_location,
975                      odex_location,
976                      app_image_file,
977                      /*use_fd=*/false,
978                      {profile_old, profile_new});
979   uint32_t image_size_wrong_checksum = GetImageObjectSectionSize(app_image_file);
980 
981   // Create an empty image using an empty profile for reference.
982   CompileProfileOdex(dex_location,
983                      odex_location,
984                      app_image_file,
985                      /*use_fd=*/false,
986                      {profile_empty});
987   uint32_t image_size_empty = GetImageObjectSectionSize(app_image_file);
988 
989   EXPECT_GT(image_size_right_checksum, image_size_empty);
990   EXPECT_EQ(image_size_wrong_checksum, image_size_empty);
991 }
992 
TEST_F(Dex2oatLayoutTest,TestVdexLayout)993 TEST_F(Dex2oatLayoutTest, TestVdexLayout) {
994   RunTestVDex();
995 }
996 
997 class Dex2oatWatchdogTest : public Dex2oatTest {
998  protected:
RunTest(bool expect_success,const std::vector<std::string> & extra_args={})999   void RunTest(bool expect_success, const std::vector<std::string>& extra_args = {}) {
1000     std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1001     std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1002 
1003     Copy(GetTestDexFileName(), dex_location);
1004 
1005     std::vector<std::string> copy(extra_args);
1006 
1007     std::string swap_location = GetOdexDir() + "/Dex2OatSwapTest.odex.swap";
1008     copy.push_back("--swap-file=" + swap_location);
1009     copy.push_back("-j512");  // Excessive idle threads just slow down dex2oat.
1010     ASSERT_TRUE(GenerateOdexForTest(dex_location,
1011                                     odex_location,
1012                                     CompilerFilter::kSpeed,
1013                                     copy,
1014                                     expect_success));
1015   }
1016 
GetTestDexFileName()1017   std::string GetTestDexFileName() {
1018     return GetDexSrc1();
1019   }
1020 };
1021 
TEST_F(Dex2oatWatchdogTest,TestWatchdogOK)1022 TEST_F(Dex2oatWatchdogTest, TestWatchdogOK) {
1023   // Check with default.
1024   RunTest(true);
1025 
1026   // Check with ten minutes.
1027   RunTest(true, { "--watchdog-timeout=600000" });
1028 }
1029 
TEST_F(Dex2oatWatchdogTest,TestWatchdogTrigger)1030 TEST_F(Dex2oatWatchdogTest, TestWatchdogTrigger) {
1031   // This test is frequently interrupted by signal_dumper on host (x86);
1032   // disable it while we investigate (b/121352534).
1033   TEST_DISABLED_FOR_X86();
1034 
1035   // The watchdog is independent of dex2oat and will not delete intermediates. It is possible
1036   // that the compilation succeeds and the file is completely written by the time the watchdog
1037   // kills dex2oat (but the dex2oat threads must have been scheduled pretty badly).
1038   test_accepts_odex_file_on_failure = true;
1039 
1040   // Check with ten milliseconds.
1041   RunTest(false, { "--watchdog-timeout=10" });
1042 }
1043 
1044 class Dex2oatReturnCodeTest : public Dex2oatTest {
1045  protected:
RunTest(const std::vector<std::string> & extra_args={})1046   int RunTest(const std::vector<std::string>& extra_args = {}) {
1047     std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
1048     std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
1049 
1050     Copy(GetTestDexFileName(), dex_location);
1051 
1052     std::string error_msg;
1053     return GenerateOdexForTestWithStatus({dex_location},
1054                                          odex_location,
1055                                          CompilerFilter::kSpeed,
1056                                          &error_msg,
1057                                          extra_args);
1058   }
1059 
GetTestDexFileName()1060   std::string GetTestDexFileName() {
1061     return GetDexSrc1();
1062   }
1063 };
1064 
1065 class Dex2oatClassLoaderContextTest : public Dex2oatTest {
1066  protected:
RunTest(const char * class_loader_context,const char * expected_classpath_key,bool expected_success,bool use_second_source=false,bool generate_image=false)1067   void RunTest(const char* class_loader_context,
1068                const char* expected_classpath_key,
1069                bool expected_success,
1070                bool use_second_source = false,
1071                bool generate_image = false) {
1072     std::string dex_location = GetUsedDexLocation();
1073     std::string odex_location = GetUsedOatLocation();
1074 
1075     Copy(use_second_source ? GetDexSrc2() : GetDexSrc1(), dex_location);
1076 
1077     std::string error_msg;
1078     std::vector<std::string> extra_args;
1079     if (class_loader_context != nullptr) {
1080       extra_args.push_back(std::string("--class-loader-context=") + class_loader_context);
1081     }
1082     if (generate_image) {
1083       extra_args.push_back(std::string("--app-image-file=") + GetUsedImageLocation());
1084     }
1085     auto check_oat = [expected_classpath_key](const OatFile& oat_file) {
1086       ASSERT_TRUE(expected_classpath_key != nullptr);
1087       const char* classpath = oat_file.GetOatHeader().GetStoreValueByKey(OatHeader::kClassPathKey);
1088       ASSERT_TRUE(classpath != nullptr);
1089       ASSERT_STREQ(expected_classpath_key, classpath);
1090     };
1091 
1092     ASSERT_TRUE(GenerateOdexForTest(dex_location,
1093                                     odex_location,
1094                                     CompilerFilter::kVerify,
1095                                     extra_args,
1096                                     expected_success,
1097                                     /*use_fd=*/ false,
1098                                     /*use_zip_fd=*/ false,
1099                                     check_oat));
1100   }
1101 
GetUsedDexLocation()1102   std::string GetUsedDexLocation() {
1103     return GetScratchDir() + "/Context.jar";
1104   }
1105 
GetUsedOatLocation()1106   std::string GetUsedOatLocation() {
1107     return GetOdexDir() + "/Context.odex";
1108   }
1109 
GetUsedImageLocation()1110   std::string GetUsedImageLocation() {
1111     return GetOdexDir() + "/Context.art";
1112   }
1113 
1114   const char* kEmptyClassPathKey = "PCL[]";
1115 };
1116 
TEST_F(Dex2oatClassLoaderContextTest,InvalidContext)1117 TEST_F(Dex2oatClassLoaderContextTest, InvalidContext) {
1118   RunTest("Invalid[]", /*expected_classpath_key*/ nullptr, /*expected_success*/ false);
1119 }
1120 
TEST_F(Dex2oatClassLoaderContextTest,EmptyContext)1121 TEST_F(Dex2oatClassLoaderContextTest, EmptyContext) {
1122   RunTest("PCL[]", kEmptyClassPathKey, /*expected_success*/ true);
1123 }
1124 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithTheSourceDexFiles)1125 TEST_F(Dex2oatClassLoaderContextTest, ContextWithTheSourceDexFiles) {
1126   std::string context = "PCL[" + GetUsedDexLocation() + "]";
1127   RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1128 }
1129 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithOtherDexFiles)1130 TEST_F(Dex2oatClassLoaderContextTest, ContextWithOtherDexFiles) {
1131   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("Nested");
1132 
1133   std::string context = "PCL[" + dex_files[0]->GetLocation() + "]";
1134   std::string expected_classpath_key = "PCL[" +
1135       dex_files[0]->GetLocation() + "*" + std::to_string(dex_files[0]->GetLocationChecksum()) + "]";
1136   RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1137 }
1138 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithResourceOnlyDexFiles)1139 TEST_F(Dex2oatClassLoaderContextTest, ContextWithResourceOnlyDexFiles) {
1140   std::string resource_only_classpath = GetScratchDir() + "/resource_only_classpath.jar";
1141   Copy(GetResourceOnlySrc1(), resource_only_classpath);
1142 
1143   std::string context = "PCL[" + resource_only_classpath + "]";
1144   // Expect an empty context because resource only dex files cannot be open.
1145   RunTest(context.c_str(), kEmptyClassPathKey , /*expected_success*/ true);
1146 }
1147 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithNotExistentDexFiles)1148 TEST_F(Dex2oatClassLoaderContextTest, ContextWithNotExistentDexFiles) {
1149   std::string context = "PCL[does_not_exists.dex]";
1150   // Expect an empty context because stripped dex files cannot be open.
1151   RunTest(context.c_str(), kEmptyClassPathKey, /*expected_success*/ true);
1152 }
1153 
TEST_F(Dex2oatClassLoaderContextTest,ChainContext)1154 TEST_F(Dex2oatClassLoaderContextTest, ChainContext) {
1155   std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1156   std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1157 
1158   std::string context = "PCL[" + GetTestDexFileName("Nested") + "];" +
1159       "DLC[" + GetTestDexFileName("MultiDex") + "]";
1160   std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "];" +
1161       "DLC[" + CreateClassPathWithChecksums(dex_files2) + "]";
1162 
1163   RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1164 }
1165 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithSharedLibrary)1166 TEST_F(Dex2oatClassLoaderContextTest, ContextWithSharedLibrary) {
1167   std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1168   std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1169 
1170   std::string context = "PCL[" + GetTestDexFileName("Nested") + "]" +
1171       "{PCL[" + GetTestDexFileName("MultiDex") + "]}";
1172   std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "]" +
1173       "{PCL[" + CreateClassPathWithChecksums(dex_files2) + "]}";
1174   RunTest(context.c_str(), expected_classpath_key.c_str(), true);
1175 }
1176 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithSharedLibraryAndImage)1177 TEST_F(Dex2oatClassLoaderContextTest, ContextWithSharedLibraryAndImage) {
1178   std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1179   std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1180 
1181   std::string context = "PCL[" + GetTestDexFileName("Nested") + "]" +
1182       "{PCL[" + GetTestDexFileName("MultiDex") + "]}";
1183   std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "]" +
1184       "{PCL[" + CreateClassPathWithChecksums(dex_files2) + "]}";
1185   RunTest(context.c_str(),
1186           expected_classpath_key.c_str(),
1187           /*expected_success=*/ true,
1188           /*use_second_source=*/ false,
1189           /*generate_image=*/ true);
1190 }
1191 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithSameSharedLibrariesAndImage)1192 TEST_F(Dex2oatClassLoaderContextTest, ContextWithSameSharedLibrariesAndImage) {
1193   std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1194   std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1195 
1196   std::string context = "PCL[" + GetTestDexFileName("Nested") + "]" +
1197       "{PCL[" + GetTestDexFileName("MultiDex") + "]" +
1198       "#PCL[" + GetTestDexFileName("MultiDex") + "]}";
1199   std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "]" +
1200       "{PCL[" + CreateClassPathWithChecksums(dex_files2) + "]" +
1201       "#PCL[" + CreateClassPathWithChecksums(dex_files2) + "]}";
1202   RunTest(context.c_str(),
1203           expected_classpath_key.c_str(),
1204           /*expected_success=*/ true,
1205           /*use_second_source=*/ false,
1206           /*generate_image=*/ true);
1207 }
1208 
TEST_F(Dex2oatClassLoaderContextTest,ContextWithSharedLibrariesDependenciesAndImage)1209 TEST_F(Dex2oatClassLoaderContextTest, ContextWithSharedLibrariesDependenciesAndImage) {
1210   std::vector<std::unique_ptr<const DexFile>> dex_files1 = OpenTestDexFiles("Nested");
1211   std::vector<std::unique_ptr<const DexFile>> dex_files2 = OpenTestDexFiles("MultiDex");
1212 
1213   std::string context = "PCL[" + GetTestDexFileName("Nested") + "]" +
1214       "{PCL[" + GetTestDexFileName("MultiDex") + "]" +
1215       "{PCL[" + GetTestDexFileName("Nested") + "]}}";
1216   std::string expected_classpath_key = "PCL[" + CreateClassPathWithChecksums(dex_files1) + "]" +
1217       "{PCL[" + CreateClassPathWithChecksums(dex_files2) + "]" +
1218       "{PCL[" + CreateClassPathWithChecksums(dex_files1) + "]}}";
1219   RunTest(context.c_str(),
1220           expected_classpath_key.c_str(),
1221           /*expected_success=*/ true,
1222           /*use_second_source=*/ false,
1223           /*generate_image=*/ true);
1224 }
1225 
1226 class Dex2oatDeterminism : public Dex2oatTest {};
1227 
TEST_F(Dex2oatDeterminism,UnloadCompile)1228 TEST_F(Dex2oatDeterminism, UnloadCompile) {
1229   Runtime* const runtime = Runtime::Current();
1230   std::string out_dir = GetScratchDir();
1231   const std::string base_oat_name = out_dir + "/base.oat";
1232   const std::string base_vdex_name = out_dir + "/base.vdex";
1233   const std::string unload_oat_name = out_dir + "/unload.oat";
1234   const std::string unload_vdex_name = out_dir + "/unload.vdex";
1235   const std::string no_unload_oat_name = out_dir + "/nounload.oat";
1236   const std::string no_unload_vdex_name = out_dir + "/nounload.vdex";
1237   std::string error_msg;
1238   const std::vector<gc::space::ImageSpace*>& spaces = runtime->GetHeap()->GetBootImageSpaces();
1239   ASSERT_GT(spaces.size(), 0u);
1240   const std::string image_location = spaces[0]->GetImageLocation();
1241   // Without passing in an app image, it will unload in between compilations.
1242   const int res = GenerateOdexForTestWithStatus(
1243       GetLibCoreDexFileNames(),
1244       base_oat_name,
1245       CompilerFilter::Filter::kVerify,
1246       &error_msg,
1247       {"--force-determinism", "--avoid-storing-invocation"});
1248   ASSERT_EQ(res, 0);
1249   Copy(base_oat_name, unload_oat_name);
1250   Copy(base_vdex_name, unload_vdex_name);
1251   std::unique_ptr<File> unload_oat(OS::OpenFileForReading(unload_oat_name.c_str()));
1252   std::unique_ptr<File> unload_vdex(OS::OpenFileForReading(unload_vdex_name.c_str()));
1253   ASSERT_TRUE(unload_oat != nullptr);
1254   ASSERT_TRUE(unload_vdex != nullptr);
1255   EXPECT_GT(unload_oat->GetLength(), 0u);
1256   EXPECT_GT(unload_vdex->GetLength(), 0u);
1257   // Regenerate with an app image to disable the dex2oat unloading and verify that the output is
1258   // the same.
1259   const int res2 = GenerateOdexForTestWithStatus(
1260       GetLibCoreDexFileNames(),
1261       base_oat_name,
1262       CompilerFilter::Filter::kVerify,
1263       &error_msg,
1264       {"--force-determinism", "--avoid-storing-invocation", "--compile-individually"});
1265   ASSERT_EQ(res2, 0);
1266   Copy(base_oat_name, no_unload_oat_name);
1267   Copy(base_vdex_name, no_unload_vdex_name);
1268   std::unique_ptr<File> no_unload_oat(OS::OpenFileForReading(no_unload_oat_name.c_str()));
1269   std::unique_ptr<File> no_unload_vdex(OS::OpenFileForReading(no_unload_vdex_name.c_str()));
1270   ASSERT_TRUE(no_unload_oat != nullptr);
1271   ASSERT_TRUE(no_unload_vdex != nullptr);
1272   EXPECT_GT(no_unload_oat->GetLength(), 0u);
1273   EXPECT_GT(no_unload_vdex->GetLength(), 0u);
1274   // Verify that both of the files are the same (odex and vdex).
1275   EXPECT_EQ(unload_oat->GetLength(), no_unload_oat->GetLength());
1276   EXPECT_EQ(unload_vdex->GetLength(), no_unload_vdex->GetLength());
1277   EXPECT_EQ(unload_oat->Compare(no_unload_oat.get()), 0)
1278       << unload_oat_name << " " << no_unload_oat_name;
1279   EXPECT_EQ(unload_vdex->Compare(no_unload_vdex.get()), 0)
1280       << unload_vdex_name << " " << no_unload_vdex_name;
1281 }
1282 
1283 // Test that dexlayout section info is correctly written to the oat file for profile based
1284 // compilation.
TEST_F(Dex2oatTest,LayoutSections)1285 TEST_F(Dex2oatTest, LayoutSections) {
1286   using Hotness = ProfileCompilationInfo::MethodHotness;
1287   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1288   ScratchFile profile_file;
1289   // We can only layout method indices with code items, figure out which ones have this property
1290   // first.
1291   std::vector<uint16_t> methods;
1292   {
1293     const dex::TypeId* type_id = dex->FindTypeId("LManyMethods;");
1294     dex::TypeIndex type_idx = dex->GetIndexForTypeId(*type_id);
1295     ClassAccessor accessor(*dex, *dex->FindClassDef(type_idx));
1296     std::set<size_t> code_item_offsets;
1297     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1298       const uint16_t method_idx = method.GetIndex();
1299       const size_t code_item_offset = method.GetCodeItemOffset();
1300       if (code_item_offsets.insert(code_item_offset).second) {
1301         // Unique code item, add the method index.
1302         methods.push_back(method_idx);
1303       }
1304     }
1305   }
1306   ASSERT_GE(methods.size(), 8u);
1307   std::vector<uint16_t> hot_methods = {methods[1], methods[3], methods[5]};
1308   std::vector<uint16_t> startup_methods = {methods[1], methods[2], methods[7]};
1309   std::vector<uint16_t> post_methods = {methods[0], methods[2], methods[6]};
1310   // Here, we build the profile from the method lists.
1311   ProfileCompilationInfo info;
1312   info.AddMethodsForDex(
1313       static_cast<Hotness::Flag>(Hotness::kFlagHot | Hotness::kFlagStartup),
1314       dex.get(),
1315       hot_methods.begin(),
1316       hot_methods.end());
1317   info.AddMethodsForDex(
1318       Hotness::kFlagStartup,
1319       dex.get(),
1320       startup_methods.begin(),
1321       startup_methods.end());
1322   info.AddMethodsForDex(
1323       Hotness::kFlagPostStartup,
1324       dex.get(),
1325       post_methods.begin(),
1326       post_methods.end());
1327   for (uint16_t id : hot_methods) {
1328     EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsHot());
1329     EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1330   }
1331   for (uint16_t id : startup_methods) {
1332     EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsStartup());
1333   }
1334   for (uint16_t id : post_methods) {
1335     EXPECT_TRUE(info.GetMethodHotness(MethodReference(dex.get(), id)).IsPostStartup());
1336   }
1337   // Save the profile since we want to use it with dex2oat to produce an oat file.
1338   ASSERT_TRUE(info.Save(profile_file.GetFd()));
1339   // Generate a profile based odex.
1340   const std::string dir = GetScratchDir();
1341   const std::string oat_filename = dir + "/base.oat";
1342   const std::string vdex_filename = dir + "/base.vdex";
1343   std::string error_msg;
1344   const int res = GenerateOdexForTestWithStatus(
1345       {dex->GetLocation()},
1346       oat_filename,
1347       CompilerFilter::Filter::kVerify,
1348       &error_msg,
1349       {"--profile-file=" + profile_file.GetFilename(),
1350        "--compact-dex-level=fast"});
1351   EXPECT_EQ(res, 0);
1352 
1353   // Open our generated oat file.
1354   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
1355                                                    oat_filename.c_str(),
1356                                                    oat_filename.c_str(),
1357                                                    /*executable=*/ false,
1358                                                    /*low_4gb=*/ false,
1359                                                    dex->GetLocation(),
1360                                                    &error_msg));
1361   ASSERT_TRUE(odex_file != nullptr);
1362   std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1363   ASSERT_EQ(oat_dex_files.size(), 1u);
1364   // Check that the code sections match what we expect.
1365   for (const OatDexFile* oat_dex : oat_dex_files) {
1366     const DexLayoutSections* const sections = oat_dex->GetDexLayoutSections();
1367     // Testing of logging the sections.
1368     ASSERT_TRUE(sections != nullptr);
1369     LOG(INFO) << *sections;
1370 
1371     // Load the sections into temporary variables for convenience.
1372     const DexLayoutSection& code_section =
1373         sections->sections_[static_cast<size_t>(DexLayoutSections::SectionType::kSectionTypeCode)];
1374     const DexLayoutSection::Subsection& section_hot_code =
1375         code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeHot)];
1376     const DexLayoutSection::Subsection& section_sometimes_used =
1377         code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeSometimesUsed)];
1378     const DexLayoutSection::Subsection& section_startup_only =
1379         code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeStartupOnly)];
1380     const DexLayoutSection::Subsection& section_unused =
1381         code_section.parts_[static_cast<size_t>(LayoutType::kLayoutTypeUnused)];
1382 
1383     // All the sections should be non-empty.
1384     EXPECT_GT(section_hot_code.Size(), 0u);
1385     EXPECT_GT(section_sometimes_used.Size(), 0u);
1386     EXPECT_GT(section_startup_only.Size(), 0u);
1387     EXPECT_GT(section_unused.Size(), 0u);
1388 
1389     // Open the dex file since we need to peek at the code items to verify the layout matches what
1390     // we expect.
1391     std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1392     ASSERT_TRUE(dex_file != nullptr) << error_msg;
1393     const dex::TypeId* type_id = dex_file->FindTypeId("LManyMethods;");
1394     ASSERT_TRUE(type_id != nullptr);
1395     dex::TypeIndex type_idx = dex_file->GetIndexForTypeId(*type_id);
1396     const dex::ClassDef* class_def = dex_file->FindClassDef(type_idx);
1397     ASSERT_TRUE(class_def != nullptr);
1398 
1399     // Count how many code items are for each category, there should be at least one per category.
1400     size_t hot_count = 0;
1401     size_t post_startup_count = 0;
1402     size_t startup_count = 0;
1403     size_t unused_count = 0;
1404     // Visit all of the methdos of the main class and cross reference the method indices to their
1405     // corresponding code item offsets to verify the layout.
1406     ClassAccessor accessor(*dex_file, *class_def);
1407     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1408       const size_t method_idx = method.GetIndex();
1409       const size_t code_item_offset = method.GetCodeItemOffset();
1410       const bool is_hot = ContainsElement(hot_methods, method_idx);
1411       const bool is_startup = ContainsElement(startup_methods, method_idx);
1412       const bool is_post_startup = ContainsElement(post_methods, method_idx);
1413       if (is_hot) {
1414         // Hot is highest precedence, check that the hot methods are in the hot section.
1415         EXPECT_TRUE(section_hot_code.Contains(code_item_offset));
1416         ++hot_count;
1417       } else if (is_post_startup) {
1418         // Post startup is sometimes used section.
1419         EXPECT_TRUE(section_sometimes_used.Contains(code_item_offset));
1420         ++post_startup_count;
1421       } else if (is_startup) {
1422         // Startup at this point means not hot or post startup, these must be startup only then.
1423         EXPECT_TRUE(section_startup_only.Contains(code_item_offset));
1424         ++startup_count;
1425       } else {
1426         if (section_unused.Contains(code_item_offset)) {
1427           // If no flags are set, the method should be unused ...
1428           ++unused_count;
1429         } else {
1430           // or this method is part of the last code item and the end is 4 byte aligned.
1431           for (const ClassAccessor::Method& method2 : accessor.GetMethods()) {
1432             EXPECT_LE(method2.GetCodeItemOffset(), code_item_offset);
1433           }
1434           uint32_t code_item_size = dex_file->FindCodeItemOffset(*class_def, method_idx);
1435           EXPECT_EQ((code_item_offset + code_item_size) % 4, 0u);
1436         }
1437       }
1438     }
1439     EXPECT_GT(hot_count, 0u);
1440     EXPECT_GT(post_startup_count, 0u);
1441     EXPECT_GT(startup_count, 0u);
1442     EXPECT_GT(unused_count, 0u);
1443   }
1444 }
1445 
1446 // Test that generating compact dex works.
TEST_F(Dex2oatTest,GenerateCompactDex)1447 TEST_F(Dex2oatTest, GenerateCompactDex) {
1448   // Generate a compact dex based odex.
1449   const std::string dir = GetScratchDir();
1450   const std::string oat_filename = dir + "/base.oat";
1451   const std::string vdex_filename = dir + "/base.vdex";
1452   const std::string dex_location = GetTestDexFileName("MultiDex");
1453   std::string error_msg;
1454   const int res = GenerateOdexForTestWithStatus(
1455       { dex_location },
1456       oat_filename,
1457       CompilerFilter::Filter::kVerify,
1458       &error_msg,
1459       {"--compact-dex-level=fast"});
1460   EXPECT_EQ(res, 0);
1461   // Open our generated oat file.
1462   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
1463                                                    oat_filename.c_str(),
1464                                                    oat_filename.c_str(),
1465                                                    /*executable=*/ false,
1466                                                    /*low_4gb=*/ false,
1467                                                    dex_location,
1468                                                    &error_msg));
1469   ASSERT_TRUE(odex_file != nullptr);
1470   std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1471   ASSERT_GT(oat_dex_files.size(), 1u);
1472   // Check that each dex is a compact dex file.
1473   std::vector<std::unique_ptr<const CompactDexFile>> compact_dex_files;
1474   for (const OatDexFile* oat_dex : oat_dex_files) {
1475     std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1476     ASSERT_TRUE(dex_file != nullptr) << error_msg;
1477     ASSERT_TRUE(dex_file->IsCompactDexFile());
1478     compact_dex_files.push_back(
1479         std::unique_ptr<const CompactDexFile>(dex_file.release()->AsCompactDexFile()));
1480   }
1481   for (const std::unique_ptr<const CompactDexFile>& dex_file : compact_dex_files) {
1482     // Test that every code item is in the owned section.
1483     const CompactDexFile::Header& header = dex_file->GetHeader();
1484     EXPECT_LE(header.OwnedDataBegin(), header.OwnedDataEnd());
1485     EXPECT_LE(header.OwnedDataBegin(), header.data_size_);
1486     EXPECT_LE(header.OwnedDataEnd(), header.data_size_);
1487     for (ClassAccessor accessor : dex_file->GetClasses()) {
1488       for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1489         if (method.GetCodeItemOffset() != 0u) {
1490           ASSERT_GE(method.GetCodeItemOffset(), header.OwnedDataBegin());
1491           ASSERT_LT(method.GetCodeItemOffset(), header.OwnedDataEnd());
1492         }
1493       }
1494     }
1495     // Test that the owned sections don't overlap.
1496     for (const std::unique_ptr<const CompactDexFile>& other_dex : compact_dex_files) {
1497       if (dex_file != other_dex) {
1498         ASSERT_TRUE(
1499             (dex_file->GetHeader().OwnedDataBegin() >= other_dex->GetHeader().OwnedDataEnd()) ||
1500             (dex_file->GetHeader().OwnedDataEnd() <= other_dex->GetHeader().OwnedDataBegin()));
1501       }
1502     }
1503   }
1504 }
1505 
1506 class Dex2oatVerifierAbort : public Dex2oatTest {};
1507 
TEST_F(Dex2oatVerifierAbort,HardFail)1508 TEST_F(Dex2oatVerifierAbort, HardFail) {
1509   // Use VerifierDeps as it has hard-failing classes.
1510   std::unique_ptr<const DexFile> dex(OpenTestDexFile("VerifierDeps"));
1511   std::string out_dir = GetScratchDir();
1512   const std::string base_oat_name = out_dir + "/base.oat";
1513   std::string error_msg;
1514   const int res_fail = GenerateOdexForTestWithStatus(
1515         {dex->GetLocation()},
1516         base_oat_name,
1517         CompilerFilter::Filter::kVerify,
1518         &error_msg,
1519         {"--abort-on-hard-verifier-error"});
1520   EXPECT_NE(0, res_fail);
1521 
1522   const int res_no_fail = GenerateOdexForTestWithStatus(
1523         {dex->GetLocation()},
1524         base_oat_name,
1525         CompilerFilter::Filter::kVerify,
1526         &error_msg,
1527         {"--no-abort-on-hard-verifier-error"});
1528   EXPECT_EQ(0, res_no_fail);
1529 }
1530 
1531 class Dex2oatDedupeCode : public Dex2oatTest {};
1532 
TEST_F(Dex2oatDedupeCode,DedupeTest)1533 TEST_F(Dex2oatDedupeCode, DedupeTest) {
1534   // Use MyClassNatives. It has lots of native methods that will produce deduplicate-able code.
1535   std::unique_ptr<const DexFile> dex(OpenTestDexFile("MyClassNatives"));
1536   std::string out_dir = GetScratchDir();
1537   const std::string base_oat_name = out_dir + "/base.oat";
1538   size_t no_dedupe_size = 0;
1539   ASSERT_TRUE(GenerateOdexForTest(dex->GetLocation(),
1540                                   base_oat_name,
1541                                   CompilerFilter::Filter::kSpeed,
1542                                   { "--deduplicate-code=false" },
1543                                   /*expect_success=*/ true,
1544                                   /*use_fd=*/ false,
1545                                   /*use_zip_fd=*/ false,
1546                                   [&no_dedupe_size](const OatFile& o) {
1547                                     no_dedupe_size = o.Size();
1548                                   }));
1549 
1550   size_t dedupe_size = 0;
1551   ASSERT_TRUE(GenerateOdexForTest(dex->GetLocation(),
1552                                   base_oat_name,
1553                                   CompilerFilter::Filter::kSpeed,
1554                                   { "--deduplicate-code=true" },
1555                                   /*expect_success=*/ true,
1556                                   /*use_fd=*/ false,
1557                                   /*use_zip_fd=*/ false,
1558                                   [&dedupe_size](const OatFile& o) {
1559                                     dedupe_size = o.Size();
1560                                   }));
1561 
1562   EXPECT_LT(dedupe_size, no_dedupe_size);
1563 }
1564 
TEST_F(Dex2oatTest,UncompressedTest)1565 TEST_F(Dex2oatTest, UncompressedTest) {
1566   std::unique_ptr<const DexFile> dex(OpenTestDexFile("MainUncompressedAligned"));
1567   std::string out_dir = GetScratchDir();
1568   const std::string base_oat_name = out_dir + "/base.oat";
1569   ASSERT_TRUE(GenerateOdexForTest(dex->GetLocation(),
1570                                   base_oat_name,
1571                                   CompilerFilter::Filter::kVerify,
1572                                   { },
1573                                   /*expect_success=*/ true,
1574                                   /*use_fd=*/ false,
1575                                   /*use_zip_fd=*/ false,
1576                                   [](const OatFile& o) {
1577                                     CHECK(!o.ContainsDexCode());
1578                                   }));
1579 }
1580 
TEST_F(Dex2oatTest,MissingBootImageTest)1581 TEST_F(Dex2oatTest, MissingBootImageTest) {
1582   std::string out_dir = GetScratchDir();
1583   const std::string base_oat_name = out_dir + "/base.oat";
1584   // The compilation should succeed even without the boot image.
1585   ASSERT_TRUE(GenerateOdexForTest(
1586       {GetTestDexFileName("MainUncompressedAligned")},
1587       base_oat_name,
1588       CompilerFilter::Filter::kVerify,
1589       // Note: Extra options go last and the second `--boot-image` option overrides the first.
1590       {"--boot-image=/nonx/boot.art"}));
1591 }
1592 
TEST_F(Dex2oatTest,EmptyUncompressedDexTest)1593 TEST_F(Dex2oatTest, EmptyUncompressedDexTest) {
1594   std::string out_dir = GetScratchDir();
1595   const std::string base_oat_name = out_dir + "/base.oat";
1596   std::string error_msg;
1597   int status = GenerateOdexForTestWithStatus(
1598       { GetTestDexFileName("MainEmptyUncompressed") },
1599       base_oat_name,
1600       CompilerFilter::Filter::kVerify,
1601       &error_msg,
1602       { },
1603       /*use_fd*/ false);
1604   // Expect to fail with code 1 and not SIGSEGV or SIGABRT.
1605   ASSERT_TRUE(WIFEXITED(status));
1606   ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg;
1607 }
1608 
TEST_F(Dex2oatTest,EmptyUncompressedAlignedDexTest)1609 TEST_F(Dex2oatTest, EmptyUncompressedAlignedDexTest) {
1610   std::string out_dir = GetScratchDir();
1611   const std::string base_oat_name = out_dir + "/base.oat";
1612   std::string error_msg;
1613   int status = GenerateOdexForTestWithStatus(
1614       { GetTestDexFileName("MainEmptyUncompressedAligned") },
1615       base_oat_name,
1616       CompilerFilter::Filter::kVerify,
1617       &error_msg,
1618       { },
1619       /*use_fd*/ false);
1620   // Expect to fail with code 1 and not SIGSEGV or SIGABRT.
1621   ASSERT_TRUE(WIFEXITED(status));
1622   ASSERT_EQ(WEXITSTATUS(status), 1) << error_msg;
1623 }
1624 
1625 // Dex file that has duplicate methods have different code items and debug info.
1626 static const char kDuplicateMethodInputDex[] =
1627     "ZGV4CjAzOQDEy8VPdj4qHpgPYFWtLCtOykfFP4kB8tGYDAAAcAAAAHhWNBIAAAAAAAAAANALAABI"
1628     "AAAAcAAAAA4AAACQAQAABQAAAMgBAAANAAAABAIAABkAAABsAgAABAAAADQDAADgCAAAuAMAADgI"
1629     "AABCCAAASggAAE8IAABcCAAAaggAAHkIAACICAAAlggAAKQIAACyCAAAwAgAAM4IAADcCAAA6ggA"
1630     "APgIAAD7CAAA/wgAABcJAAAuCQAARQkAAFQJAAB4CQAAmAkAALsJAADSCQAA5gkAAPoJAAAVCgAA"
1631     "KQoAADsKAABCCgAASgoAAFIKAABbCgAAZAoAAGwKAAB0CgAAfAoAAIQKAACMCgAAlAoAAJwKAACk"
1632     "CgAArQoAALcKAADACgAAwwoAAMcKAADcCgAA6QoAAPEKAAD3CgAA/QoAAAMLAAAJCwAAEAsAABcL"
1633     "AAAdCwAAIwsAACkLAAAvCwAANQsAADsLAABBCwAARwsAAE0LAABSCwAAWwsAAF4LAABoCwAAbwsA"
1634     "ABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAC4AAAAwAAAA"
1635     "DwAAAAkAAAAAAAAAEAAAAAoAAACoBwAALgAAAAwAAAAAAAAALwAAAAwAAACoBwAALwAAAAwAAACw"
1636     "BwAAAgAJADUAAAACAAkANgAAAAIACQA3AAAAAgAJADgAAAACAAkAOQAAAAIACQA6AAAAAgAJADsA"
1637     "AAACAAkAPAAAAAIACQA9AAAAAgAJAD4AAAACAAkAPwAAAAIACQBAAAAACwAHAEIAAAAAAAIAAQAA"
1638     "AAAAAwAeAAAAAQACAAEAAAABAAMAHgAAAAIAAgAAAAAAAgACAAEAAAADAAIAAQAAAAMAAgAfAAAA"
1639     "AwACACAAAAADAAIAIQAAAAMAAgAiAAAAAwACACMAAAADAAIAJAAAAAMAAgAlAAAAAwACACYAAAAD"
1640     "AAIAJwAAAAMAAgAoAAAAAwACACkAAAADAAIAKgAAAAMABAA0AAAABwADAEMAAAAIAAIAAQAAAAoA"
1641     "AgABAAAACgABADIAAAAKAAAARQAAAAAAAAAAAAAACAAAAAAAAAAdAAAAaAcAALYHAAAAAAAAAQAA"
1642     "AAAAAAAIAAAAAAAAAB0AAAB4BwAAxAcAAAAAAAACAAAAAAAAAAgAAAAAAAAAHQAAAIgHAADSBwAA"
1643     "AAAAAAMAAAAAAAAACAAAAAAAAAAdAAAAmAcAAPoHAAAAAAAAAAAAAAEAAAAAAAAArAYAADEAAAAa"
1644     "AAMAaQAAABoABABpAAEAGgAHAGkABAAaAAgAaQAFABoACQBpAAYAGgAKAGkABwAaAAsAaQAIABoA"
1645     "DABpAAkAGgANAGkACgAaAA4AaQALABoABQBpAAIAGgAGAGkAAwAOAAAAAQABAAEAAACSBgAABAAA"
1646     "AHAQFQAAAA4ABAABAAIAAACWBgAAFwAAAGIADAAiAQoAcBAWAAEAGgICAG4gFwAhAG4gFwAxAG4Q"
1647     "GAABAAwBbiAUABAADgAAAAEAAQABAAAAngYAAAQAAABwEBUAAAAOAAIAAQACAAAAogYAAAYAAABi"
1648     "AAwAbiAUABAADgABAAEAAQAAAKgGAAAEAAAAcBAVAAAADgABAAEAAQAAALsGAAAEAAAAcBAVAAAA"
1649     "DgABAAAAAQAAAL8GAAAGAAAAYgAAAHEQAwAAAA4AAQAAAAEAAADEBgAABgAAAGIAAQBxEAMAAAAO"
1650     "AAEAAAABAAAA8QYAAAYAAABiAAIAcRABAAAADgABAAAAAQAAAPYGAAAGAAAAYgADAHEQAwAAAA4A"
1651     "AQAAAAEAAADJBgAABgAAAGIABABxEAMAAAAOAAEAAAABAAAAzgYAAAYAAABiAAEAcRADAAAADgAB"
1652     "AAAAAQAAANMGAAAGAAAAYgAGAHEQAwAAAA4AAQAAAAEAAADYBgAABgAAAGIABwBxEAMAAAAOAAEA"
1653     "AAABAAAA3QYAAAYAAABiAAgAcRABAAAADgABAAAAAQAAAOIGAAAGAAAAYgAJAHEQAwAAAA4AAQAA"
1654     "AAEAAADnBgAABgAAAGIACgBxEAMAAAAOAAEAAAABAAAA7AYAAAYAAABiAAsAcRABAAAADgABAAEA"
1655     "AAAAAPsGAAAlAAAAcQAHAAAAcQAIAAAAcQALAAAAcQAMAAAAcQANAAAAcQAOAAAAcQAPAAAAcQAQ"
1656     "AAAAcQARAAAAcQASAAAAcQAJAAAAcQAKAAAADgAnAA4AKQFFDgEWDwAhAA4AIwFFDloAEgAOABMA"
1657     "DktLS0tLS0tLS0tLABEADgAuAA5aADIADloANgAOWgA6AA5aAD4ADloAQgAOWgBGAA5aAEoADloA"
1658     "TgAOWgBSAA5aAFYADloAWgAOWgBeATQOPDw8PDw8PDw8PDw8AAIEAUYYAwIFAjEECEEXLAIFAjEE"
1659     "CEEXKwIFAjEECEEXLQIGAUYcAxgAGAEYAgAAAAIAAAAMBwAAEgcAAAIAAAAMBwAAGwcAAAIAAAAM"
1660     "BwAAJAcAAAEAAAAtBwAAPAcAAAAAAAAAAAAAAAAAAEgHAAAAAAAAAAAAAAAAAABUBwAAAAAAAAAA"
1661     "AAAAAAAAYAcAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAQAAAA0AAAACAACAgASsCAEIxAgAAAIAAoCA"
1662     "BIQJAQicCQwAAgAACQEJAQkBCQEJAQkBCQEJAQkBCQEJAQkEiIAEuAcBgIAEuAkAAA4ABoCABNAJ"
1663     "AQnoCQAJhAoACaAKAAm8CgAJ2AoACfQKAAmQCwAJrAsACcgLAAnkCwAJgAwACZwMAAm4DAg8Y2xp"
1664     "bml0PgAGPGluaXQ+AANBQUEAC0hlbGxvIFdvcmxkAAxIZWxsbyBXb3JsZDEADUhlbGxvIFdvcmxk"
1665     "MTAADUhlbGxvIFdvcmxkMTEADEhlbGxvIFdvcmxkMgAMSGVsbG8gV29ybGQzAAxIZWxsbyBXb3Js"
1666     "ZDQADEhlbGxvIFdvcmxkNQAMSGVsbG8gV29ybGQ2AAxIZWxsbyBXb3JsZDcADEhlbGxvIFdvcmxk"
1667     "OAAMSGVsbG8gV29ybGQ5AAFMAAJMTAAWTE1hbnlNZXRob2RzJFByaW50ZXIyOwAVTE1hbnlNZXRo"
1668     "b2RzJFByaW50ZXI7ABVMTWFueU1ldGhvZHMkU3RyaW5nczsADUxNYW55TWV0aG9kczsAIkxkYWx2"
1669     "aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2aWsvYW5ub3RhdGlvbi9Jbm5lckNs"
1670     "YXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNzZXM7ABVMamF2YS9pby9QcmludFN0"
1671     "cmVhbTsAEkxqYXZhL2xhbmcvT2JqZWN0OwASTGphdmEvbGFuZy9TdHJpbmc7ABlMamF2YS9sYW5n"
1672     "L1N0cmluZ0J1aWxkZXI7ABJMamF2YS9sYW5nL1N5c3RlbTsAEE1hbnlNZXRob2RzLmphdmEABVBy"
1673     "aW50AAZQcmludDAABlByaW50MQAHUHJpbnQxMAAHUHJpbnQxMQAGUHJpbnQyAAZQcmludDMABlBy"
1674     "aW50NAAGUHJpbnQ1AAZQcmludDYABlByaW50NwAGUHJpbnQ4AAZQcmludDkAB1ByaW50ZXIACFBy"
1675     "aW50ZXIyAAdTdHJpbmdzAAFWAAJWTAATW0xqYXZhL2xhbmcvU3RyaW5nOwALYWNjZXNzRmxhZ3MA"
1676     "BmFwcGVuZAAEYXJncwAEbWFpbgAEbXNnMAAEbXNnMQAFbXNnMTAABW1zZzExAARtc2cyAARtc2cz"
1677     "AARtc2c0AARtc2c1AARtc2c2AARtc2c3AARtc2c4AARtc2c5AARuYW1lAANvdXQAB3ByaW50bG4A"
1678     "AXMACHRvU3RyaW5nAAV2YWx1ZQBffn5EOHsibWluLWFwaSI6MTAwMDAsInNoYS0xIjoiZmViODZj"
1679     "MDA2ZWZhY2YxZDc5ODRiODVlMTc5MGZlZjdhNzY3YWViYyIsInZlcnNpb24iOiJ2MS4xLjUtZGV2"
1680     "In0AEAAAAAAAAAABAAAAAAAAAAEAAABIAAAAcAAAAAIAAAAOAAAAkAEAAAMAAAAFAAAAyAEAAAQA"
1681     "AAANAAAABAIAAAUAAAAZAAAAbAIAAAYAAAAEAAAANAMAAAEgAAAUAAAAuAMAAAMgAAAUAAAAkgYA"
1682     "AAQgAAAFAAAADAcAAAMQAAAEAAAAOQcAAAYgAAAEAAAAaAcAAAEQAAACAAAAqAcAAAAgAAAEAAAA"
1683     "tgcAAAIgAABIAAAAOAgAAAAQAAABAAAA0AsAAAAAAAA=";
1684 
WriteBase64ToFile(const char * base64,File * file)1685 static void WriteBase64ToFile(const char* base64, File* file) {
1686   // Decode base64.
1687   CHECK(base64 != nullptr);
1688   size_t length;
1689   std::unique_ptr<uint8_t[]> bytes(DecodeBase64(base64, &length));
1690   CHECK(bytes != nullptr);
1691   if (!file->WriteFully(bytes.get(), length)) {
1692     PLOG(FATAL) << "Failed to write base64 as file";
1693   }
1694 }
1695 
TEST_F(Dex2oatTest,CompactDexGenerationFailure)1696 TEST_F(Dex2oatTest, CompactDexGenerationFailure) {
1697   ScratchFile temp_dex;
1698   WriteBase64ToFile(kDuplicateMethodInputDex, temp_dex.GetFile());
1699   std::string out_dir = GetScratchDir();
1700   const std::string oat_filename = out_dir + "/base.oat";
1701   // The dex won't pass the method verifier, only use the verify filter.
1702   ASSERT_TRUE(GenerateOdexForTest(temp_dex.GetFilename(),
1703                                   oat_filename,
1704                                   CompilerFilter::Filter::kVerify,
1705                                   { },
1706                                   /*expect_success=*/ true,
1707                                   /*use_fd=*/ false,
1708                                   /*use_zip_fd=*/ false,
1709                                   [](const OatFile& o) {
1710                                     CHECK(o.ContainsDexCode());
1711                                   }));
1712   // Open our generated oat file.
1713   std::string error_msg;
1714   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
1715                                                    oat_filename.c_str(),
1716                                                    oat_filename.c_str(),
1717                                                    /*executable=*/ false,
1718                                                    /*low_4gb=*/ false,
1719                                                    temp_dex.GetFilename(),
1720                                                    &error_msg));
1721   ASSERT_TRUE(odex_file != nullptr);
1722   std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1723   ASSERT_EQ(oat_dex_files.size(), 1u);
1724   // The dexes should have failed to convert to compact dex.
1725   for (const OatDexFile* oat_dex : oat_dex_files) {
1726     std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1727     ASSERT_TRUE(dex_file != nullptr) << error_msg;
1728     ASSERT_TRUE(!dex_file->IsCompactDexFile());
1729   }
1730 }
1731 
TEST_F(Dex2oatTest,CompactDexGenerationFailureMultiDex)1732 TEST_F(Dex2oatTest, CompactDexGenerationFailureMultiDex) {
1733   // Create a multidex file with only one dex that gets rejected for cdex conversion.
1734   ScratchFile apk_file;
1735   {
1736     FILE* file = fdopen(DupCloexec(apk_file.GetFd()), "w+b");
1737     ZipWriter writer(file);
1738     // Add vdex to zip.
1739     writer.StartEntry("classes.dex", ZipWriter::kCompress);
1740     size_t length = 0u;
1741     std::unique_ptr<uint8_t[]> bytes(DecodeBase64(kDuplicateMethodInputDex, &length));
1742     ASSERT_GE(writer.WriteBytes(&bytes[0], length), 0);
1743     writer.FinishEntry();
1744     writer.StartEntry("classes2.dex", ZipWriter::kCompress);
1745     std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1746     ASSERT_GE(writer.WriteBytes(dex->Begin(), dex->Size()), 0);
1747     writer.FinishEntry();
1748     writer.Finish();
1749     ASSERT_EQ(apk_file.GetFile()->Flush(), 0);
1750   }
1751   const std::string& dex_location = apk_file.GetFilename();
1752   const std::string odex_location = GetOdexDir() + "/output.odex";
1753   ASSERT_TRUE(GenerateOdexForTest(dex_location,
1754                                   odex_location,
1755                                   CompilerFilter::kVerify,
1756                                   { "--compact-dex-level=fast" },
1757                                   true));
1758 }
1759 
TEST_F(Dex2oatTest,StderrLoggerOutput)1760 TEST_F(Dex2oatTest, StderrLoggerOutput) {
1761   std::string dex_location = GetScratchDir() + "/Dex2OatStderrLoggerTest.jar";
1762   std::string odex_location = GetOdexDir() + "/Dex2OatStderrLoggerTest.odex";
1763 
1764   // Test file doesn't matter.
1765   Copy(GetDexSrc1(), dex_location);
1766 
1767   ASSERT_TRUE(GenerateOdexForTest(dex_location,
1768                                   odex_location,
1769                                   CompilerFilter::kVerify,
1770                                   { "--runtime-arg", "-Xuse-stderr-logger" },
1771                                   true));
1772   // Look for some random part of dex2oat logging. With the stderr logger this should be captured,
1773   // even on device.
1774   EXPECT_NE(std::string::npos, output_.find("dex2oat took"));
1775 }
1776 
TEST_F(Dex2oatTest,VerifyCompilationReason)1777 TEST_F(Dex2oatTest, VerifyCompilationReason) {
1778   std::string dex_location = GetScratchDir() + "/Dex2OatCompilationReason.jar";
1779   std::string odex_location = GetOdexDir() + "/Dex2OatCompilationReason.odex";
1780 
1781   // Test file doesn't matter.
1782   Copy(GetDexSrc1(), dex_location);
1783 
1784   ASSERT_TRUE(GenerateOdexForTest(dex_location,
1785                                   odex_location,
1786                                   CompilerFilter::kVerify,
1787                                   { "--compilation-reason=install" },
1788                                   true));
1789   std::string error_msg;
1790   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
1791                                                    odex_location.c_str(),
1792                                                    odex_location.c_str(),
1793                                                    /*executable=*/ false,
1794                                                    /*low_4gb=*/ false,
1795                                                    dex_location,
1796                                                    &error_msg));
1797   ASSERT_TRUE(odex_file != nullptr);
1798   ASSERT_STREQ("install", odex_file->GetCompilationReason());
1799 }
1800 
TEST_F(Dex2oatTest,VerifyNoCompilationReason)1801 TEST_F(Dex2oatTest, VerifyNoCompilationReason) {
1802   std::string dex_location = GetScratchDir() + "/Dex2OatNoCompilationReason.jar";
1803   std::string odex_location = GetOdexDir() + "/Dex2OatNoCompilationReason.odex";
1804 
1805   // Test file doesn't matter.
1806   Copy(GetDexSrc1(), dex_location);
1807 
1808   ASSERT_TRUE(GenerateOdexForTest(dex_location,
1809                                   odex_location,
1810                                   CompilerFilter::kVerify,
1811                                   {},
1812                                   true));
1813   std::string error_msg;
1814   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
1815                                                    odex_location.c_str(),
1816                                                    odex_location.c_str(),
1817                                                    /*executable=*/ false,
1818                                                    /*low_4gb=*/ false,
1819                                                    dex_location,
1820                                                    &error_msg));
1821   ASSERT_TRUE(odex_file != nullptr);
1822   ASSERT_EQ(nullptr, odex_file->GetCompilationReason());
1823 }
1824 
TEST_F(Dex2oatTest,DontExtract)1825 TEST_F(Dex2oatTest, DontExtract) {
1826   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
1827   std::string error_msg;
1828   const std::string out_dir = GetScratchDir();
1829   const std::string dex_location = dex->GetLocation();
1830   const std::string odex_location = out_dir + "/base.oat";
1831   const std::string vdex_location = out_dir + "/base.vdex";
1832   ASSERT_TRUE(GenerateOdexForTest(dex_location,
1833                                   odex_location,
1834                                   CompilerFilter::Filter::kVerify,
1835                                   { "--copy-dex-files=false" },
1836                                   /*expect_success=*/ true,
1837                                   /*use_fd=*/ false,
1838                                   /*use_zip_fd=*/ false,
1839                                   [](const OatFile&) {}));
1840   {
1841     // Check the vdex doesn't have dex.
1842     std::unique_ptr<VdexFile> vdex(VdexFile::Open(vdex_location.c_str(),
1843                                                   /*writable=*/ false,
1844                                                   /*low_4gb=*/ false,
1845                                                   &error_msg));
1846     ASSERT_TRUE(vdex != nullptr);
1847     EXPECT_FALSE(vdex->HasDexSection()) << output_;
1848   }
1849   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
1850                                                    odex_location.c_str(),
1851                                                    odex_location.c_str(),
1852                                                    /*executable=*/ false,
1853                                                    /*low_4gb=*/ false,
1854                                                    dex_location,
1855                                                    &error_msg));
1856   ASSERT_TRUE(odex_file != nullptr) << dex_location;
1857   std::vector<const OatDexFile*> oat_dex_files = odex_file->GetOatDexFiles();
1858   ASSERT_EQ(oat_dex_files.size(), 1u);
1859   // Verify that the oat file can still open the dex files.
1860   for (const OatDexFile* oat_dex : oat_dex_files) {
1861     std::unique_ptr<const DexFile> dex_file(oat_dex->OpenDexFile(&error_msg));
1862     ASSERT_TRUE(dex_file != nullptr) << error_msg;
1863   }
1864   // Create a dm file and use it to verify.
1865   // Add produced artifacts to a zip file that doesn't contain the classes.dex.
1866   ScratchFile dm_file;
1867   {
1868     std::unique_ptr<File> vdex_file(OS::OpenFileForReading(vdex_location.c_str()));
1869     ASSERT_TRUE(vdex_file != nullptr);
1870     ASSERT_GT(vdex_file->GetLength(), 0u);
1871     FILE* file = fdopen(DupCloexec(dm_file.GetFd()), "w+b");
1872     ZipWriter writer(file);
1873     auto write_all_bytes = [&](File* file) {
1874       std::unique_ptr<uint8_t[]> bytes(new uint8_t[file->GetLength()]);
1875       ASSERT_TRUE(file->ReadFully(&bytes[0], file->GetLength()));
1876       ASSERT_GE(writer.WriteBytes(&bytes[0], file->GetLength()), 0);
1877     };
1878     // Add vdex to zip.
1879     writer.StartEntry(VdexFile::kVdexNameInDmFile, ZipWriter::kCompress);
1880     write_all_bytes(vdex_file.get());
1881     writer.FinishEntry();
1882     writer.Finish();
1883     ASSERT_EQ(dm_file.GetFile()->Flush(), 0);
1884   }
1885 
1886   auto generate_and_check = [&](CompilerFilter::Filter filter) {
1887     output_.clear();
1888     ASSERT_TRUE(GenerateOdexForTest(dex_location,
1889                                     odex_location,
1890                                     filter,
1891                                     { "--dump-timings",
1892                                       "--dm-file=" + dm_file.GetFilename(),
1893                                       // Pass -Xuse-stderr-logger have dex2oat output in output_ on
1894                                       // target.
1895                                       "--runtime-arg",
1896                                       "-Xuse-stderr-logger" },
1897                                     /*expect_success=*/ true,
1898                                     /*use_fd=*/ false,
1899                                     /*use_zip_fd=*/ false,
1900                                     [](const OatFile& o) {
1901                                       CHECK(o.ContainsDexCode());
1902                                     }));
1903     // Check the output for "Fast verify", this is printed from --dump-timings.
1904     std::istringstream iss(output_);
1905     std::string line;
1906     bool found_fast_verify = false;
1907     const std::string kFastVerifyString = "Fast Verify";
1908     while (std::getline(iss, line) && !found_fast_verify) {
1909       found_fast_verify = found_fast_verify || line.find(kFastVerifyString) != std::string::npos;
1910     }
1911     EXPECT_TRUE(found_fast_verify) << "Expected to find " << kFastVerifyString << "\n" << output_;
1912   };
1913 
1914   // Use verify compiler filter to check that FastVerify works for that filter too.
1915   generate_and_check(CompilerFilter::Filter::kVerify);
1916 }
1917 
1918 // Test that compact dex generation with invalid dex files doesn't crash dex2oat. b/75970654
TEST_F(Dex2oatTest,CompactDexInvalidSource)1919 TEST_F(Dex2oatTest, CompactDexInvalidSource) {
1920   ScratchFile invalid_dex;
1921   {
1922     FILE* file = fdopen(DupCloexec(invalid_dex.GetFd()), "w+b");
1923     ZipWriter writer(file);
1924     writer.StartEntry("classes.dex", ZipWriter::kAlign32);
1925     DexFile::Header header = {};
1926     StandardDexFile::WriteMagic(header.magic_);
1927     StandardDexFile::WriteCurrentVersion(header.magic_);
1928     header.file_size_ = 4 * KB;
1929     header.data_size_ = 4 * KB;
1930     header.data_off_ = 10 * MB;
1931     header.map_off_ = 10 * MB;
1932     header.class_defs_off_ = 10 * MB;
1933     header.class_defs_size_ = 10000;
1934     ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
1935     writer.FinishEntry();
1936     writer.Finish();
1937     ASSERT_EQ(invalid_dex.GetFile()->Flush(), 0);
1938   }
1939   const std::string& dex_location = invalid_dex.GetFilename();
1940   const std::string odex_location = GetOdexDir() + "/output.odex";
1941   std::string error_msg;
1942   int status = GenerateOdexForTestWithStatus(
1943       {dex_location},
1944       odex_location,
1945       CompilerFilter::kVerify,
1946       &error_msg,
1947       { "--compact-dex-level=fast" });
1948   ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
1949 }
1950 
1951 // Test that dex2oat with a CompactDex file in the APK fails.
TEST_F(Dex2oatTest,CompactDexInZip)1952 TEST_F(Dex2oatTest, CompactDexInZip) {
1953   CompactDexFile::Header header = {};
1954   CompactDexFile::WriteMagic(header.magic_);
1955   CompactDexFile::WriteCurrentVersion(header.magic_);
1956   header.file_size_ = sizeof(CompactDexFile::Header);
1957   header.data_off_ = 10 * MB;
1958   header.map_off_ = 10 * MB;
1959   header.class_defs_off_ = 10 * MB;
1960   header.class_defs_size_ = 10000;
1961   // Create a zip containing the invalid dex.
1962   ScratchFile invalid_dex_zip;
1963   {
1964     FILE* file = fdopen(DupCloexec(invalid_dex_zip.GetFd()), "w+b");
1965     ZipWriter writer(file);
1966     writer.StartEntry("classes.dex", ZipWriter::kCompress);
1967     ASSERT_GE(writer.WriteBytes(&header, sizeof(header)), 0);
1968     writer.FinishEntry();
1969     writer.Finish();
1970     ASSERT_EQ(invalid_dex_zip.GetFile()->Flush(), 0);
1971   }
1972   // Create the dex file directly.
1973   ScratchFile invalid_dex;
1974   {
1975     ASSERT_GE(invalid_dex.GetFile()->WriteFully(&header, sizeof(header)), 0);
1976     ASSERT_EQ(invalid_dex.GetFile()->Flush(), 0);
1977   }
1978   std::string error_msg;
1979   int status = 0u;
1980 
1981   status = GenerateOdexForTestWithStatus(
1982       { invalid_dex_zip.GetFilename() },
1983       GetOdexDir() + "/output_apk.odex",
1984       CompilerFilter::kVerify,
1985       &error_msg,
1986       { "--compact-dex-level=fast" });
1987   ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
1988 
1989   status = GenerateOdexForTestWithStatus(
1990       { invalid_dex.GetFilename() },
1991       GetOdexDir() + "/output.odex",
1992       CompilerFilter::kVerify,
1993       &error_msg,
1994       { "--compact-dex-level=fast" });
1995   ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) != 0) << status << " " << output_;
1996 }
1997 
TEST_F(Dex2oatWithExpectedFilterTest,AppImageNoProfile)1998 TEST_F(Dex2oatWithExpectedFilterTest, AppImageNoProfile) {
1999   // Set the expected filter.
2000   expected_filter_ = CompilerFilter::Filter::kVerify;
2001 
2002   ScratchFile app_image_file;
2003   const std::string out_dir = GetScratchDir();
2004   const std::string odex_location = out_dir + "/base.odex";
2005   ASSERT_TRUE(GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2006                                   odex_location,
2007                                   CompilerFilter::Filter::kSpeedProfile,
2008                                   { "--app-image-fd=" + std::to_string(app_image_file.GetFd()) },
2009                                   /*expect_success=*/ true,
2010                                   /*use_fd=*/ false,
2011                                   /*use_zip_fd=*/ false,
2012                                   [](const OatFile&) {}));
2013   // Open our generated oat file.
2014   std::string error_msg;
2015   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
2016                                                    odex_location.c_str(),
2017                                                    odex_location.c_str(),
2018                                                    /*executable=*/ false,
2019                                                    /*low_4gb=*/ false,
2020                                                    &error_msg));
2021   ASSERT_TRUE(odex_file != nullptr);
2022   ImageHeader header = {};
2023   ASSERT_TRUE(app_image_file.GetFile()->PreadFully(
2024       reinterpret_cast<void*>(&header),
2025       sizeof(header),
2026       /*offset*/ 0u)) << app_image_file.GetFile()->GetLength();
2027   EXPECT_GT(header.GetImageSection(ImageHeader::kSectionObjects).Size(), 0u);
2028   EXPECT_EQ(header.GetImageSection(ImageHeader::kSectionArtMethods).Size(), 0u);
2029   EXPECT_EQ(header.GetImageSection(ImageHeader::kSectionArtFields).Size(), 0u);
2030 }
2031 
TEST_F(Dex2oatTest,ZipFd)2032 TEST_F(Dex2oatTest, ZipFd) {
2033   std::string zip_location = GetTestDexFileName("MainUncompressedAligned");
2034   std::unique_ptr<File> dex_file(OS::OpenFileForReading(zip_location.c_str()));
2035   std::vector<std::string> extra_args{
2036       StringPrintf("--zip-fd=%d", dex_file->Fd()),
2037       "--zip-location=" + zip_location,
2038   };
2039   std::string out_dir = GetScratchDir();
2040   const std::string base_oat_name = out_dir + "/base.oat";
2041   ASSERT_TRUE(GenerateOdexForTest(zip_location,
2042                                   base_oat_name,
2043                                   CompilerFilter::Filter::kVerify,
2044                                   extra_args,
2045                                   /*expect_success=*/ true,
2046                                   /*use_fd=*/ false,
2047                                   /*use_zip_fd=*/ true));
2048 }
2049 
TEST_F(Dex2oatWithExpectedFilterTest,AppImageEmptyDex)2050 TEST_F(Dex2oatWithExpectedFilterTest, AppImageEmptyDex) {
2051   // Set the expected filter.
2052   expected_filter_ = CompilerFilter::Filter::kVerify;
2053 
2054   // Create a profile with the startup method marked.
2055   ScratchFile profile_file;
2056   ScratchFile temp_dex;
2057   const std::string& dex_location = temp_dex.GetFilename();
2058   std::vector<uint16_t> methods;
2059   std::vector<dex::TypeIndex> classes;
2060   {
2061     MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("StringLiterals"), [&] (DexFile* dex) {
2062       // Modify the header to make the dex file valid but empty.
2063       DexFile::Header* header = const_cast<DexFile::Header*>(&dex->GetHeader());
2064       header->string_ids_size_ = 0;
2065       header->string_ids_off_ = 0;
2066       header->type_ids_size_ = 0;
2067       header->type_ids_off_ = 0;
2068       header->proto_ids_size_ = 0;
2069       header->proto_ids_off_ = 0;
2070       header->field_ids_size_ = 0;
2071       header->field_ids_off_ = 0;
2072       header->method_ids_size_ = 0;
2073       header->method_ids_off_ = 0;
2074       header->class_defs_size_ = 0;
2075       header->class_defs_off_ = 0;
2076       ASSERT_GT(header->file_size_,
2077                 sizeof(*header) + sizeof(dex::MapList) + sizeof(dex::MapItem) * 2);
2078       // Move map list to be right after the header.
2079       header->map_off_ = sizeof(DexFile::Header);
2080       dex::MapList* map_list = const_cast<dex::MapList*>(dex->GetMapList());
2081       map_list->list_[0].type_ = DexFile::kDexTypeHeaderItem;
2082       map_list->list_[0].size_ = 1u;
2083       map_list->list_[0].offset_ = 0u;
2084       map_list->list_[1].type_ = DexFile::kDexTypeMapList;
2085       map_list->list_[1].size_ = 1u;
2086       map_list->list_[1].offset_ = header->map_off_;
2087       map_list->size_ = 2;
2088       header->data_off_ = header->map_off_;
2089       header->data_size_ = map_list->Size();
2090     });
2091   }
2092   std::unique_ptr<const DexFile> dex_file(OpenDexFile(temp_dex.GetFilename().c_str()));
2093   const std::string out_dir = GetScratchDir();
2094   const std::string odex_location = out_dir + "/base.odex";
2095   const std::string app_image_location = out_dir + "/base.art";
2096   ASSERT_TRUE(GenerateOdexForTest(dex_location,
2097                                   odex_location,
2098                                   CompilerFilter::Filter::kSpeedProfile,
2099                                   { "--app-image-file=" + app_image_location,
2100                                     "--resolve-startup-const-strings=true",
2101                                     "--profile-file=" + profile_file.GetFilename()},
2102                                   /*expect_success=*/ true,
2103                                   /*use_fd=*/ false,
2104                                   /*use_zip_fd=*/ false,
2105                                   [](const OatFile&) {}));
2106   // Open our generated oat file.
2107   std::string error_msg;
2108   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
2109                                                    odex_location.c_str(),
2110                                                    odex_location.c_str(),
2111                                                    /*executable=*/ false,
2112                                                    /*low_4gb=*/ false,
2113                                                    &error_msg));
2114   ASSERT_TRUE(odex_file != nullptr);
2115 }
2116 
TEST_F(Dex2oatTest,DexFileFd)2117 TEST_F(Dex2oatTest, DexFileFd) {
2118   std::string error_msg;
2119   std::string zip_location = GetTestDexFileName("Main");
2120   std::unique_ptr<File> zip_file(OS::OpenFileForReading(zip_location.c_str()));
2121   ASSERT_NE(-1, zip_file->Fd());
2122 
2123   std::unique_ptr<ZipArchive> zip_archive(
2124       ZipArchive::OpenFromFd(zip_file->Release(), zip_location.c_str(), &error_msg));
2125   ASSERT_TRUE(zip_archive != nullptr);
2126 
2127   std::string entry_name = DexFileLoader::GetMultiDexClassesDexName(0);
2128   std::unique_ptr<ZipEntry> entry(zip_archive->Find(entry_name.c_str(), &error_msg));
2129   ASSERT_TRUE(entry != nullptr);
2130 
2131   ScratchFile dex_file;
2132   const std::string& dex_location = dex_file.GetFilename();
2133   const std::string base_oat_name = GetScratchDir() + "/base.oat";
2134 
2135   bool success = entry->ExtractToFile(*(dex_file.GetFile()), &error_msg);
2136   ASSERT_TRUE(success);
2137   ASSERT_EQ(0, lseek(dex_file.GetFd(), 0, SEEK_SET));
2138 
2139   std::vector<std::string> extra_args{
2140       StringPrintf("--zip-fd=%d", dex_file.GetFd()),
2141       "--zip-location=" + dex_location,
2142   };
2143   ASSERT_TRUE(GenerateOdexForTest(dex_location,
2144                                   base_oat_name,
2145                                   CompilerFilter::Filter::kVerify,
2146                                   extra_args,
2147                                   /*expect_success=*/ true,
2148                                   /*use_fd=*/ false,
2149                                   /*use_zip_fd=*/ true));
2150 }
2151 
TEST_F(Dex2oatTest,AppImageResolveStrings)2152 TEST_F(Dex2oatTest, AppImageResolveStrings) {
2153   using Hotness = ProfileCompilationInfo::MethodHotness;
2154   // Create a profile with the startup method marked.
2155   ScratchFile profile_file;
2156   ScratchFile temp_dex;
2157   const std::string& dex_location = temp_dex.GetFilename();
2158   std::vector<uint16_t> methods;
2159   std::vector<dex::TypeIndex> classes;
2160   {
2161     MutateDexFile(temp_dex.GetFile(), GetTestDexFileName("StringLiterals"), [&] (DexFile* dex) {
2162       bool mutated_successfully = false;
2163       // Change the dex instructions to make an opcode that spans past the end of the code item.
2164       for (ClassAccessor accessor : dex->GetClasses()) {
2165         if (accessor.GetDescriptor() == std::string("LStringLiterals$StartupClass;")) {
2166           classes.push_back(accessor.GetClassIdx());
2167         }
2168         for (const ClassAccessor::Method& method : accessor.GetMethods()) {
2169           std::string method_name(dex->GetMethodName(dex->GetMethodId(method.GetIndex())));
2170           CodeItemInstructionAccessor instructions = method.GetInstructions();
2171           if (method_name == "startUpMethod2") {
2172             // Make an instruction that runs past the end of the code item and verify that it
2173             // doesn't cause dex2oat to crash.
2174             ASSERT_TRUE(instructions.begin() != instructions.end());
2175             DexInstructionIterator last_instruction = instructions.begin();
2176             for (auto dex_it = instructions.begin(); dex_it != instructions.end(); ++dex_it) {
2177               last_instruction = dex_it;
2178             }
2179             ASSERT_EQ(last_instruction->SizeInCodeUnits(), 1u);
2180             // Set the opcode to something that will go past the end of the code item.
2181             const_cast<Instruction&>(last_instruction.Inst()).SetOpcode(
2182                 Instruction::CONST_STRING_JUMBO);
2183             mutated_successfully = true;
2184             // Test that the safe iterator doesn't go past the end.
2185             SafeDexInstructionIterator it2(instructions.begin(), instructions.end());
2186             while (!it2.IsErrorState()) {
2187               ++it2;
2188             }
2189             EXPECT_TRUE(it2 == last_instruction);
2190             EXPECT_TRUE(it2 < instructions.end());
2191             methods.push_back(method.GetIndex());
2192             mutated_successfully = true;
2193           } else if (method_name == "startUpMethod") {
2194             methods.push_back(method.GetIndex());
2195           }
2196         }
2197       }
2198       CHECK(mutated_successfully)
2199           << "Failed to find candidate code item with only one code unit in last instruction.";
2200     });
2201   }
2202   std::unique_ptr<const DexFile> dex_file(OpenDexFile(temp_dex.GetFilename().c_str()));
2203   {
2204     ASSERT_GT(classes.size(), 0u);
2205     ASSERT_GT(methods.size(), 0u);
2206     // Here, we build the profile from the method lists.
2207     ProfileCompilationInfo info;
2208     info.AddClassesForDex(dex_file.get(), classes.begin(), classes.end());
2209     info.AddMethodsForDex(Hotness::kFlagStartup, dex_file.get(), methods.begin(), methods.end());
2210     // Save the profile since we want to use it with dex2oat to produce an oat file.
2211     ASSERT_TRUE(info.Save(profile_file.GetFd()));
2212   }
2213   const std::string out_dir = GetScratchDir();
2214   const std::string odex_location = out_dir + "/base.odex";
2215   const std::string app_image_location = out_dir + "/base.art";
2216   ASSERT_TRUE(GenerateOdexForTest(dex_location,
2217                                   odex_location,
2218                                   CompilerFilter::Filter::kSpeedProfile,
2219                                   { "--app-image-file=" + app_image_location,
2220                                     "--resolve-startup-const-strings=true",
2221                                     "--profile-file=" + profile_file.GetFilename()},
2222                                   /*expect_success=*/ true,
2223                                   /*use_fd=*/ false,
2224                                   /*use_zip_fd=*/ false,
2225                                   [](const OatFile&) {}));
2226   // Open our generated oat file.
2227   std::string error_msg;
2228   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
2229                                                    odex_location.c_str(),
2230                                                    odex_location.c_str(),
2231                                                    /*executable=*/ false,
2232                                                    /*low_4gb=*/ false,
2233                                                    &error_msg));
2234   ASSERT_TRUE(odex_file != nullptr);
2235   // Check the strings in the app image intern table only contain the "startup" strigs.
2236   {
2237     ScopedObjectAccess soa(Thread::Current());
2238     std::unique_ptr<gc::space::ImageSpace> space =
2239         gc::space::ImageSpace::CreateFromAppImage(app_image_location.c_str(),
2240                                                   odex_file.get(),
2241                                                   &error_msg);
2242     ASSERT_TRUE(space != nullptr) << error_msg;
2243     std::set<std::string> seen;
2244     InternTable intern_table;
2245     intern_table.AddImageStringsToTable(space.get(), [&](InternTable::UnorderedSet& interns)
2246         REQUIRES_SHARED(Locks::mutator_lock_) {
2247       for (const GcRoot<mirror::String>& str : interns) {
2248         seen.insert(str.Read()->ToModifiedUtf8());
2249       }
2250     });
2251     // Normal methods
2252     EXPECT_TRUE(seen.find("Loading ") != seen.end());
2253     EXPECT_TRUE(seen.find("Starting up") != seen.end());
2254     EXPECT_TRUE(seen.find("abcd.apk") != seen.end());
2255     EXPECT_TRUE(seen.find("Unexpected error") == seen.end());
2256     EXPECT_TRUE(seen.find("Shutting down!") == seen.end());
2257     // Classes initializers
2258     EXPECT_TRUE(seen.find("Startup init") != seen.end());
2259     EXPECT_TRUE(seen.find("Other class init") == seen.end());
2260     // Expect the sets match.
2261     EXPECT_GE(seen.size(), seen.size());
2262 
2263     // Verify what strings are marked as boot image.
2264     std::set<std::string> boot_image_strings;
2265     std::set<std::string> app_image_strings;
2266 
2267     MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
2268     intern_table.VisitInterns([&](const GcRoot<mirror::String>& root)
2269         REQUIRES_SHARED(Locks::mutator_lock_) {
2270       boot_image_strings.insert(root.Read()->ToModifiedUtf8());
2271     }, /*visit_boot_images=*/true, /*visit_non_boot_images=*/false);
2272     intern_table.VisitInterns([&](const GcRoot<mirror::String>& root)
2273         REQUIRES_SHARED(Locks::mutator_lock_) {
2274       app_image_strings.insert(root.Read()->ToModifiedUtf8());
2275     }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true);
2276     EXPECT_EQ(boot_image_strings.size(), 0u);
2277     EXPECT_TRUE(app_image_strings == seen);
2278   }
2279 }
2280 
2281 
TEST_F(Dex2oatClassLoaderContextTest,StoredClassLoaderContext)2282 TEST_F(Dex2oatClassLoaderContextTest, StoredClassLoaderContext) {
2283   std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles("MultiDex");
2284   const std::string out_dir = GetScratchDir();
2285   const std::string odex_location = out_dir + "/base.odex";
2286   const std::string valid_context = "PCL[" + dex_files[0]->GetLocation() + "]";
2287   const std::string stored_context = "PCL[/system/not_real_lib.jar]";
2288   std::string expected_stored_context = "PCL[";
2289   size_t index = 1;
2290   for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
2291     const bool is_first = index == 1u;
2292     if (!is_first) {
2293       expected_stored_context += ":";
2294     }
2295     expected_stored_context += "/system/not_real_lib.jar";
2296     if (!is_first) {
2297       expected_stored_context += "!classes" + std::to_string(index) + ".dex";
2298     }
2299     expected_stored_context += "*" + std::to_string(dex_file->GetLocationChecksum());
2300     ++index;
2301   }
2302   expected_stored_context +=    + "]";
2303   // The class path should not be valid and should fail being stored.
2304   EXPECT_TRUE(GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2305                                   odex_location,
2306                                   CompilerFilter::Filter::kVerify,
2307                                   { "--class-loader-context=" + stored_context },
2308                                   /*expect_success=*/ true,
2309                                   /*use_fd=*/ false,
2310                                   /*use_zip_fd=*/ false,
2311                                   [&](const OatFile& oat_file) {
2312     EXPECT_NE(oat_file.GetClassLoaderContext(), stored_context) << output_;
2313     EXPECT_NE(oat_file.GetClassLoaderContext(), valid_context) << output_;
2314   }));
2315   // The stored context should match what we expect even though it's invalid.
2316   EXPECT_TRUE(GenerateOdexForTest(GetTestDexFileName("ManyMethods"),
2317                                   odex_location,
2318                                   CompilerFilter::Filter::kVerify,
2319                                   { "--class-loader-context=" + valid_context,
2320                                     "--stored-class-loader-context=" + stored_context },
2321                                   /*expect_success=*/ true,
2322                                   /*use_fd=*/ false,
2323                                   /*use_zip_fd=*/ false,
2324                                   [&](const OatFile& oat_file) {
2325     EXPECT_EQ(oat_file.GetClassLoaderContext(), expected_stored_context) << output_;
2326   }));
2327 }
2328 
2329 class Dex2oatISAFeaturesRuntimeDetectionTest : public Dex2oatTest {
2330  protected:
RunTest(const std::vector<std::string> & extra_args={})2331   void RunTest(const std::vector<std::string>& extra_args = {}) {
2332     std::string dex_location = GetScratchDir() + "/Dex2OatSwapTest.jar";
2333     std::string odex_location = GetOdexDir() + "/Dex2OatSwapTest.odex";
2334 
2335     Copy(GetTestDexFileName(), dex_location);
2336 
2337     ASSERT_TRUE(GenerateOdexForTest(dex_location,
2338                                     odex_location,
2339                                     CompilerFilter::kSpeed,
2340                                     extra_args));
2341   }
2342 
GetTestDexFileName()2343   std::string GetTestDexFileName() {
2344     return GetDexSrc1();
2345   }
2346 };
2347 
TEST_F(Dex2oatISAFeaturesRuntimeDetectionTest,TestCurrentRuntimeFeaturesAsDex2OatArguments)2348 TEST_F(Dex2oatISAFeaturesRuntimeDetectionTest, TestCurrentRuntimeFeaturesAsDex2OatArguments) {
2349   std::vector<std::string> argv;
2350   Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
2351   auto option_pos =
2352       std::find(std::begin(argv), std::end(argv), "--instruction-set-features=runtime");
2353   if (InstructionSetFeatures::IsRuntimeDetectionSupported()) {
2354     EXPECT_TRUE(kIsTargetBuild);
2355     EXPECT_NE(option_pos, std::end(argv));
2356   } else {
2357     EXPECT_EQ(option_pos, std::end(argv));
2358   }
2359 
2360   RunTest();
2361 }
2362 
2363 class LinkageTest : public Dex2oatTest {};
2364 
TEST_F(LinkageTest,LinkageEnabled)2365 TEST_F(LinkageTest, LinkageEnabled) {
2366   TEST_DISABLED_FOR_TARGET();
2367   std::unique_ptr<const DexFile> dex(OpenTestDexFile("LinkageTest"));
2368   std::string out_dir = GetScratchDir();
2369   const std::string base_oat_name = out_dir + "/base.oat";
2370   std::string error_msg;
2371   const int res_fail = GenerateOdexForTestWithStatus(
2372         {dex->GetLocation()},
2373         base_oat_name,
2374         CompilerFilter::Filter::kSpeed,
2375         &error_msg,
2376         {"--check-linkage-conditions", "--crash-on-linkage-violation"});
2377   EXPECT_NE(0, res_fail);
2378 
2379   const int res_no_fail = GenerateOdexForTestWithStatus(
2380         {dex->GetLocation()},
2381         base_oat_name,
2382         CompilerFilter::Filter::kSpeed,
2383         &error_msg,
2384         {"--check-linkage-conditions"});
2385   EXPECT_EQ(0, res_no_fail);
2386 }
2387 
2388 // Regression test for bug 179221298.
TEST_F(Dex2oatTest,LoadOutOfDateOatFile)2389 TEST_F(Dex2oatTest, LoadOutOfDateOatFile) {
2390   std::unique_ptr<const DexFile> dex(OpenTestDexFile("ManyMethods"));
2391   std::string out_dir = GetScratchDir();
2392   const std::string base_oat_name = out_dir + "/base.oat";
2393   ASSERT_TRUE(GenerateOdexForTest(dex->GetLocation(),
2394                                   base_oat_name,
2395                                   CompilerFilter::Filter::kSpeed,
2396                                   { "--deduplicate-code=false" },
2397                                   /*expect_success=*/ true,
2398                                   /*use_fd=*/ false,
2399                                   /*use_zip_fd=*/ false));
2400 
2401   // Check that we can open the oat file as executable.
2402   {
2403     std::string error_msg;
2404     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
2405                                                      base_oat_name.c_str(),
2406                                                      base_oat_name.c_str(),
2407                                                      /*executable=*/ true,
2408                                                      /*low_4gb=*/ false,
2409                                                      dex->GetLocation(),
2410                                                      &error_msg));
2411     ASSERT_TRUE(odex_file != nullptr) << error_msg;
2412   }
2413 
2414   // Rewrite the oat file with wrong version and bogus contents.
2415   {
2416     std::unique_ptr<File> file(OS::OpenFileReadWrite(base_oat_name.c_str()));
2417     ASSERT_TRUE(file != nullptr);
2418     // Retrieve the offset and size of the embedded oat file.
2419     size_t oatdata_offset;
2420     size_t oatdata_size;
2421     {
2422       std::string error_msg;
2423       std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file.get(),
2424                                                       /*writable=*/ false,
2425                                                       /*program_header_only=*/ true,
2426                                                       /*low_4gb=*/ false,
2427                                                       &error_msg));
2428       ASSERT_TRUE(elf_file != nullptr) << error_msg;
2429       ASSERT_TRUE(elf_file->Load(file.get(),
2430                                  /*executable=*/ false,
2431                                  /*low_4gb=*/ false,
2432                                  /*reservation=*/ nullptr,
2433                                  &error_msg)) << error_msg;
2434       const uint8_t* base_address = elf_file->Is64Bit()
2435           ? elf_file->GetImpl64()->GetBaseAddress()
2436           : elf_file->GetImpl32()->GetBaseAddress();
2437       const uint8_t* oatdata = elf_file->FindDynamicSymbolAddress("oatdata");
2438       ASSERT_TRUE(oatdata != nullptr);
2439       ASSERT_TRUE(oatdata > base_address);
2440       // Note: We're assuming here that the virtual address offset is the same
2441       // as file offset. This is currently true for all oat files we generate.
2442       oatdata_offset = static_cast<size_t>(oatdata - base_address);
2443       const uint8_t* oatlastword = elf_file->FindDynamicSymbolAddress("oatlastword");
2444       ASSERT_TRUE(oatlastword != nullptr);
2445       ASSERT_TRUE(oatlastword > oatdata);
2446       oatdata_size = oatlastword - oatdata;
2447     }
2448 
2449     // Check that we have the right `oatdata_offset`.
2450     int64_t length = file->GetLength();
2451     ASSERT_GE(length, static_cast<ssize_t>(oatdata_offset + sizeof(OatHeader)));
2452     alignas(OatHeader) uint8_t header_data[sizeof(OatHeader)];
2453     ASSERT_TRUE(file->PreadFully(header_data, sizeof(header_data), oatdata_offset));
2454     const OatHeader& header = reinterpret_cast<const OatHeader&>(header_data);
2455     ASSERT_TRUE(header.IsValid()) << header.GetValidationErrorMessage();
2456 
2457     // Overwrite all oat data from version onwards with bytes with value 4.
2458     // (0x04040404 is not a valid version, we're using three decimal digits and '\0'.)
2459     //
2460     // We previously tried to find the value for key "debuggable" (bug 179221298)
2461     // in the key-value store before checking the oat header. This test tries to
2462     // ensure that such early processing of the key-value store shall crash.
2463     // Reading 0x04040404 as the size of the key-value store yields a bit over
2464     // 64MiB which should hopefully include some unmapped memory beyond the end
2465     // of the loaded oat file. Overwriting the whole embedded oat file ensures
2466     // that we do not match the key within the oat file but we could still
2467     // accidentally match it in the additional sections of the elf file, so this
2468     // approach could fail to catch similar issues. At the time of writing, this
2469     // test crashed when run without the fix on 64-bit host (but not 32-bit).
2470     static constexpr size_t kVersionOffset = sizeof(OatHeader::kOatMagic);
2471     static_assert(kVersionOffset < sizeof(OatHeader));
2472     std::vector<uint8_t> data(oatdata_size - kVersionOffset, 4u);
2473     ASSERT_TRUE(file->PwriteFully(data.data(), data.size(), oatdata_offset + kVersionOffset));
2474     UNUSED(oatdata_size);
2475     CHECK_EQ(file->FlushClose(), 0) << "Could not flush and close oat file";
2476   }
2477 
2478   // Check that we reject the oat file without crashing.
2479   {
2480     std::string error_msg;
2481     std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
2482                                                      base_oat_name.c_str(),
2483                                                      base_oat_name.c_str(),
2484                                                      /*executable=*/ true,
2485                                                      /*low_4gb=*/ false,
2486                                                      dex->GetLocation(),
2487                                                      &error_msg));
2488     ASSERT_FALSE(odex_file != nullptr);
2489   }
2490 }
2491 
2492 }  // namespace art
2493