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 <backtrace/BacktraceMap.h>
21 #include <gtest/gtest.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         image_location,
177         ArrayRef<const std::string>(Runtime::Current()->GetBootClassPathLocations()),
178         ArrayRef<const std::string>(Runtime::Current()->GetBootClassPath()),
179         kRuntimeISA,
180         &error_msg);
181     ASSERT_EQ(!with_alternate_image, match) << error_msg;
182   }
183 }
184 
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)185 void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
186                                      const std::string& odex_location,
187                                      CompilerFilter::Filter filter,
188                                      const char* compilation_reason,
189                                      const std::vector<std::string>& extra_args) {
190   GenerateOatForTest(dex_location,
191                      odex_location,
192                      filter,
193                      /*with_alternate_image=*/ false,
194                      compilation_reason,
195                      extra_args);
196 }
197 
GenerateOatForTest(const char * dex_location,CompilerFilter::Filter filter,bool with_alternate_image)198 void DexoptTest::GenerateOatForTest(const char* dex_location,
199                                     CompilerFilter::Filter filter,
200                                     bool with_alternate_image) {
201   std::string oat_location;
202   std::string error_msg;
203   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
204         dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
205   GenerateOatForTest(dex_location,
206                      oat_location,
207                      filter,
208                      with_alternate_image);
209 }
210 
GenerateOatForTest(const char * dex_location,CompilerFilter::Filter filter)211 void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
212   GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/ false);
213 }
214 
ReserveImageSpace()215 void DexoptTest::ReserveImageSpace() {
216   MemMap::Init();
217 
218   // Ensure a chunk of memory is reserved for the image space.
219   uint64_t reservation_start = ART_BASE_ADDRESS;
220   uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
221 
222   std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
223   ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
224   for (BacktraceMap::iterator it = map->begin();
225       reservation_start < reservation_end && it != map->end(); ++it) {
226     const backtrace_map_t* entry = *it;
227     ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
228     reservation_start = std::max(reservation_start, entry->end);
229   }
230   ReserveImageSpaceChunk(reservation_start, reservation_end);
231 }
232 
ReserveImageSpaceChunk(uintptr_t start,uintptr_t end)233 void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
234   if (start < end) {
235     std::string error_msg;
236     image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
237                                                       reinterpret_cast<uint8_t*>(start),
238                                                       end - start,
239                                                       PROT_NONE,
240                                                       /*low_4gb=*/ false,
241                                                       /*reuse=*/ false,
242                                                       /*reservation=*/ nullptr,
243                                                       &error_msg));
244     ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
245     LOG(INFO) << "Reserved space for image " <<
246       reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
247       reinterpret_cast<void*>(image_reservation_.back().End());
248   }
249 }
250 
UnreserveImageSpace()251 void DexoptTest::UnreserveImageSpace() {
252   image_reservation_.clear();
253 }
254 
255 }  // namespace art
256