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