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 "dexopt_test.h"
18 
19 #include <gtest/gtest.h>
20 #include <procinfo/process_map.h>
21 
22 #include <string>
23 #include <vector>
24 
25 #include "android-base/stringprintf.h"
26 #include "android-base/strings.h"
27 #include "arch/instruction_set.h"
28 #include "base/file_utils.h"
29 #include "base/mem_map.h"
30 #include "common_runtime_test.h"
31 #include "compiler_callbacks.h"
32 #include "dex/art_dex_file_loader.h"
33 #include "dex/dex_file_loader.h"
34 #include "dex2oat_environment_test.h"
35 #include "gc/space/image_space.h"
36 #include "hidden_api.h"
37 #include "oat/oat.h"
38 #include "oat/oat_file_assistant.h"
39 #include "profile/profile_compilation_info.h"
40 
41 namespace art HIDDEN {
SetUp()42 void DexoptTest::SetUp() {
43   ReserveImageSpace();
44   Dex2oatEnvironmentTest::SetUp();
45 }
46 
PreRuntimeCreate()47 void DexoptTest::PreRuntimeCreate() {
48   std::string error_msg;
49   UnreserveImageSpace();
50 }
51 
PostRuntimeCreate()52 void DexoptTest::PostRuntimeCreate() { ReserveImageSpace(); }
53 
Dex2Oat(const std::vector<std::string> & args,std::string * error_msg)54 bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
55   std::vector<std::string> argv;
56   if (!CommonRuntimeTest::StartDex2OatCommandLine(&argv, error_msg)) {
57     return false;
58   }
59 
60   Runtime* runtime = Runtime::Current();
61   if (runtime->GetHiddenApiEnforcementPolicy() == hiddenapi::EnforcementPolicy::kEnabled) {
62     argv.push_back("--runtime-arg");
63     argv.push_back("-Xhidden-api-policy:enabled");
64   }
65 
66   if (!kIsTargetBuild) {
67     argv.push_back("--host");
68   }
69 
70   argv.insert(argv.end(), args.begin(), args.end());
71 
72   std::string command_line(android::base::Join(argv, ' '));
73   return Exec(argv, error_msg);
74 }
75 
GenerateAlternateImage(const std::string & scratch_dir)76 std::string DexoptTest::GenerateAlternateImage(const std::string& scratch_dir) {
77   std::vector<std::string> libcore_dex_files = GetLibCoreDexFileNames();
78   std::vector<std::string> libcore_dex_locations = GetLibCoreDexLocations();
79 
80   std::string image_dir = scratch_dir + GetInstructionSetString(kRuntimeISA);
81   int mkdir_result = mkdir(image_dir.c_str(), 0700);
82   CHECK_EQ(0, mkdir_result) << image_dir.c_str();
83 
84   std::vector<std::string> extra_args{
85       "--compiler-filter=verify",
86       android::base::StringPrintf("--base=0x%08x", ART_BASE_ADDRESS),
87   };
88   std::string filename_prefix = image_dir + "/boot-interpreter";
89   ArrayRef<const std::string> dex_files(libcore_dex_files);
90   ArrayRef<const std::string> dex_locations(libcore_dex_locations);
91   std::string error_msg;
92   bool ok = CompileBootImage(extra_args, filename_prefix, dex_files, dex_locations, &error_msg);
93   EXPECT_TRUE(ok) << error_msg;
94 
95   return scratch_dir + "boot-interpreter.art";
96 }
97 
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)98 void DexoptTest::GenerateOatForTest(const std::string& dex_location,
99                                     const std::string& oat_location,
100                                     CompilerFilter::Filter filter,
101                                     bool with_alternate_image,
102                                     const char* compilation_reason,
103                                     const std::vector<std::string>& extra_args) {
104   std::vector<std::string> args;
105   args.push_back("--dex-file=" + dex_location);
106   args.push_back("--oat-file=" + oat_location);
107   args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
108   args.push_back("--runtime-arg");
109 
110   // Use -Xnorelocate regardless of the relocate argument.
111   // We control relocation by redirecting the dalvik cache when needed
112   // rather than use this flag.
113   args.push_back("-Xnorelocate");
114 
115   ScratchFile profile_file;
116   if (CompilerFilter::DependsOnProfile(filter)) {
117     // Create a profile with some basic content so that dex2oat
118     // doesn't get an empty profile and changes the filter to verify.
119     std::string error_msg;
120     std::vector<std::unique_ptr<const DexFile>> dex_files;
121     ArtDexFileLoader dex_file_loader(dex_location);
122     ASSERT_TRUE(dex_file_loader.Open(
123         /*verify=*/false, /*verify_checksum=*/false, &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,
157                                                    oat_location,
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     std::unique_ptr<ClassLoaderContext> context = ClassLoaderContext::Create(/*spec=*/"");
167     OatFileAssistant oat_file_assistant(dex_location.c_str(),
168                                         kRuntimeISA,
169                                         context.get(),
170                                         /*load_executable=*/false);
171 
172     bool match = oat_file_assistant.ValidateBootClassPathChecksums(*odex_file);
173     ASSERT_EQ(!with_alternate_image, match) << error_msg;
174   }
175 }
176 
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)177 void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
178                                      const std::string& odex_location,
179                                      CompilerFilter::Filter filter,
180                                      const char* compilation_reason,
181                                      const std::vector<std::string>& extra_args) {
182   GenerateOatForTest(dex_location,
183                      odex_location,
184                      filter,
185                      /*with_alternate_image=*/false,
186                      compilation_reason,
187                      extra_args);
188 }
189 
GenerateOatForTest(const char * dex_location,CompilerFilter::Filter filter,bool with_alternate_image)190 void DexoptTest::GenerateOatForTest(const char* dex_location,
191                                     CompilerFilter::Filter filter,
192                                     bool with_alternate_image) {
193   std::string oat_location;
194   std::string error_msg;
195   ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
196       dex_location, kRuntimeISA, &oat_location, &error_msg))
197       << error_msg;
198   GenerateOatForTest(dex_location, oat_location, filter, with_alternate_image);
199 }
200 
GenerateOatForTest(const char * dex_location,CompilerFilter::Filter filter)201 void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
202   GenerateOatForTest(dex_location, filter, /*with_alternate_image=*/false);
203 }
204 
ReserveImageSpace()205 void DexoptTest::ReserveImageSpace() {
206   MemMap::Init();
207 
208   // Ensure a chunk of memory is reserved for the image space.
209   uint64_t reservation_start = ART_BASE_ADDRESS;
210   uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
211 
212   std::vector<android::procinfo::MapInfo> maps;
213   ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
214   for (const android::procinfo::MapInfo& map_info : maps) {
215     ReserveImageSpaceChunk(reservation_start, std::min(map_info.start, reservation_end));
216     reservation_start = std::max(reservation_start, map_info.end);
217     if (reservation_start >= reservation_end) {
218       break;
219     }
220   }
221   ReserveImageSpaceChunk(reservation_start, reservation_end);
222 }
223 
ReserveImageSpaceChunk(uintptr_t start,uintptr_t end)224 void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
225   if (start < end) {
226     std::string error_msg;
227     image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
228                                                       reinterpret_cast<uint8_t*>(start),
229                                                       end - start,
230                                                       PROT_NONE,
231                                                       /*low_4gb=*/false,
232                                                       /*reuse=*/false,
233                                                       /*reservation=*/nullptr,
234                                                       &error_msg));
235     ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
236     LOG(INFO) << "Reserved space for image "
237               << reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-"
238               << reinterpret_cast<void*>(image_reservation_.back().End());
239   }
240 }
241 
UnreserveImageSpace()242 void DexoptTest::UnreserveImageSpace() { image_reservation_.clear(); }
243 
244 }  // namespace art
245