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