• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 <string>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 #include <procinfo/process_map.h>
22 
23 #include "android-base/stringprintf.h"
24 #include "android-base/strings.h"
25 #include "base/file_utils.h"
26 #include "base/mem_map.h"
27 #include "common_runtime_test.h"
28 #include "compiler_callbacks.h"
29 #include "dex/art_dex_file_loader.h"
30 #include "dex/dex_file_loader.h"
31 #include "dex2oat_environment_test.h"
32 #include "dexopt_test.h"
33 #include "gc/space/image_space.h"
34 #include "hidden_api.h"
35 #include "oat.h"
36 #include "profile/profile_compilation_info.h"
37 
38 namespace art {
SetUp()39 void DexoptTest::SetUp() {
40   ReserveImageSpace();
41   Dex2oatEnvironmentTest::SetUp();
42 }
43 
PreRuntimeCreate()44 void DexoptTest::PreRuntimeCreate() {
45   std::string error_msg;
46   UnreserveImageSpace();
47 }
48 
PostRuntimeCreate()49 void DexoptTest::PostRuntimeCreate() {
50   ReserveImageSpace();
51 }
52 
Dex2Oat(const std::vector<std::string> & args,std::string * error_msg)53 bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
54   std::vector<std::string> argv;
55   if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) {
56     return false;
57   }
58 
59   Runtime* runtime = Runtime::Current();
60   if (runtime->GetHiddenApiEnforcementPolicy() == hiddenapi::EnforcementPolicy::kEnabled) {
61     argv.push_back("--runtime-arg");
62     argv.push_back("-Xhidden-api-policy:enabled");
63   }
64 
65   if (!kIsTargetBuild) {
66     argv.push_back("--host");
67   }
68 
69   argv.insert(argv.end(), args.begin(), args.end());
70 
71   std::string command_line(android::base::Join(argv, ' '));
72   return Exec(argv, error_msg);
73 }
74 
GenerateAlternateImage(const std::string & scratch_dir)75 std::string DexoptTest::GenerateAlternateImage(const std::string& scratch_dir) {
76   std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
77   std::vector<std::string> libcore_dex_locations = GetLibCoreDexLocations();
78 
79   std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
80   int mkdir_result = mkdir(image_dir.c_str(), 0700);
81   CHECK_EQ(0, mkdir_result) << image_dir.c_str();
82 
83   std::vector<std::string> extra_args {
84     "--compiler-filter=verify",
85     android::base::StringPrintf("--base=0x%08x", ART_BASE_ADDRESS),
86   };
87   std::string filename_prefix = image_dir + "/boot-interpreter";
88   ArrayRef<const std::string> dex_files(libcore_dex_files);
89   ArrayRef<const std::string> dex_locations(libcore_dex_locations);
90   std::string error_msg;
91   bool ok = CompileBootImage(extra_args, filename_prefix, dex_files, dex_locations, &error_msg);
92   EXPECT_TRUE(ok) << error_msg;
93 
94   return scratch_dir + "boot-interpreter.art";
95 }
96 
GenerateOatForTest(const std::string & dex_location,const std::string & oat_location,CompilerFilter::Filter filter,bool with_alternate_image,const char * compilation_reason,const std::vector<std::string> & extra_args)97 void DexoptTest::GenerateOatForTest(const std::string& dex_location,
98                                     const std::string& oat_location,
99                                     CompilerFilter::Filter filter,
100                                     bool with_alternate_image,
101                                     const char* compilation_reason,
102                                     const std::vector<std::string>& extra_args) {
103   std::vector<std::string> args;
104   args.push_back("--dex-file=" + dex_location);
105   args.push_back("--oat-file=" + oat_location);
106   args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
107   args.push_back("--runtime-arg");
108 
109   // Use -Xnorelocate regardless of the relocate argument.
110   // We control relocation by redirecting the dalvik cache when needed
111   // rather than use this flag.
112   args.push_back("-Xnorelocate");
113 
114   ScratchFile profile_file;
115   if (CompilerFilter::DependsOnProfile(filter)) {
116     // Create a profile with some basic content so that dex2oat
117     // doesn't get an empty profile and changes the filter to verify.
118     std::string error_msg;
119     std::vector<std::unique_ptr<const DexFile>> dex_files;
120     const ArtDexFileLoader dex_file_loader;
121     ASSERT_TRUE(dex_file_loader.Open(
122         dex_location.c_str(), dex_location.c_str(), /*verify=*/ false, /*verify_checksum=*/ false,
123         &error_msg, &dex_files));
124     EXPECT_GE(dex_files.size(), 1U);
125     std::unique_ptr<const DexFile>& dex_file = dex_files[0];
126     ProfileCompilationInfo info;
127 
128     info.AddClass(*dex_file, dex::TypeIndex(0));
129 
130     ASSERT_TRUE(info.Save(profile_file.GetFd()));
131     ASSERT_EQ(0, profile_file.GetFile()->Flush());
132 
133     // Set the argument
134     args.push_back("--profile-file=" + profile_file.GetFilename());
135   }
136 
137   std::string image_location = GetImageLocation();
138   std::optional<ScratchDir> scratch;
139   if (with_alternate_image) {
140     scratch.emplace();  // Create the scratch directory for the generated boot image.
141     std::string alternate_image_location = GenerateAlternateImage(scratch->GetPath());
142     args.push_back("--boot-image=" + alternate_image_location);
143   }
144 
145   if (compilation_reason != nullptr) {
146     args.push_back("--compilation-reason=" + std::string(compilation_reason));
147   }
148 
149   args.insert(args.end(), extra_args.begin(), extra_args.end());
150 
151   std::string error_msg;
152   ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
153 
154   // Verify the odex file was generated as expected.
155   std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
156                                                    oat_location.c_str(),
157                                                    oat_location.c_str(),
158                                                    /*executable=*/ false,
159                                                    /*low_4gb=*/ false,
160                                                    dex_location,
161                                                    &error_msg));
162   ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
163   EXPECT_EQ(filter, odex_file->GetCompilerFilter());
164 
165   if (CompilerFilter::DependsOnImageChecksum(filter)) {
166     const OatHeader& oat_header = odex_file->GetOatHeader();
167     const char* oat_bcp = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey);
168     ASSERT_TRUE(oat_bcp != nullptr);
169     ASSERT_EQ(oat_bcp, android::base::Join(Runtime::Current()->GetBootClassPathLocations(), ':'));
170     const char* checksums = oat_header.GetStoreValueByKey(OatHeader::kBootClassPathChecksumsKey);
171     ASSERT_TRUE(checksums != nullptr);
172 
173     bool match = gc::space::ImageSpace::VerifyBootClassPathChecksums(
174         checksums,
175         oat_bcp,
176         ArrayRef<const std::string>(&image_location, 1),
177         ArrayRef<const std::string>(Runtime::Current()->GetBootClassPathLocations()),
178         ArrayRef<const std::string>(Runtime::Current()->GetBootClassPath()),
179         ArrayRef<const int>(Runtime::Current()->GetBootClassPathFds()),
180         kRuntimeISA,
181         &error_msg);
182     ASSERT_EQ(!with_alternate_image, match) << error_msg;
183   }
184 }
185 
GenerateOdexForTest(const std::string & dex_location,const std::string & odex_location,CompilerFilter::Filter filter,const char * compilation_reason,const std::vector<std::string> & extra_args)186 void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
187                                      const std::string& odex_location,
188                                      CompilerFilter::Filter filter,
189                                      const char* compilation_reason,
190                                      const std::vector<std::string>& extra_args) {
191   GenerateOatForTest(dex_location,
192                      odex_location,
193                      filter,
194                      /*with_alternate_image=*/ false,
195                      compilation_reason,
196                      extra_args);
197 }
198 
GenerateOatForTest(const char * dex_location,CompilerFilter::Filter filter,bool with_alternate_image)199 void DexoptTest::GenerateOatForTest(const char* dex_location,
200                                     CompilerFilter::Filter filter,
201                                     bool with_alternate_image) {
202   std::string oat_location;
203   std::string error_msg;
204   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
205         dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
206   GenerateOatForTest(dex_location,
207                      oat_location,
208                      filter,
209                      with_alternate_image);
210 }
211 
GenerateOatForTest(const char * dex_location,CompilerFilter::Filter filter)212 void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
213   GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false);
214 }
215 
ReserveImageSpace()216 void DexoptTest::ReserveImageSpace() {
217   MemMap::Init();
218 
219   // Ensure a chunk of memory is reserved for the image space.
220   uint64_t reservation_start = ART_BASE_ADDRESS;
221   uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
222 
223   std::vector<android::procinfo::MapInfo> maps;
224   ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
225   for (const android::procinfo::MapInfo& map_info : maps) {
226     ReserveImageSpaceChunk(reservation_start, std::min(map_info.start, reservation_end));
227     reservation_start = std::max(reservation_start, map_info.end);
228     if (reservation_start >= reservation_end) {
229       break;
230     }
231   }
232   ReserveImageSpaceChunk(reservation_start, reservation_end);
233 }
234 
ReserveImageSpaceChunk(uintptr_t start,uintptr_t end)235 void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
236   if (start < end) {
237     std::string error_msg;
238     image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
239                                                       reinterpret_cast<uint8_t*>(start),
240                                                       end - start,
241                                                       PROT_NONE,
242                                                       /*low_4gb=*/ false,
243                                                       /*reuse=*/ false,
244                                                       /*reservation=*/ nullptr,
245                                                       &error_msg));
246     ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
247     LOG(INFO) << "Reserved space for image " <<
248       reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
249       reinterpret_cast<void*>(image_reservation_.back().End());
250   }
251 }
252 
UnreserveImageSpace()253 void DexoptTest::UnreserveImageSpace() {
254   image_reservation_.clear();
255 }
256 
257 }  // namespace art
258