blob: 127e14e239f9954d0d3db21770cf2fbb26924ce5 [file] [log] [blame]
Calin Juravle36eb3132017-01-13 16:32:38 -08001/*
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
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000023#include "android-base/stringprintf.h"
24#include "android-base/strings.h"
Chris Morin88c6d262018-02-13 15:26:21 -080025#include "base/file_utils.h"
David Sehr79e26072018-04-06 17:58:50 -070026#include "base/mem_map.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080027#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"
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000032#include "hidden_api.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080033
34namespace art {
35void DexoptTest::SetUp() {
36 ReserveImageSpace();
37 Dex2oatEnvironmentTest::SetUp();
38}
39
40void DexoptTest::PreRuntimeCreate() {
41 std::string error_msg;
42 ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg;
43 ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg;
44 UnreserveImageSpace();
45}
46
47void DexoptTest::PostRuntimeCreate() {
48 ReserveImageSpace();
49}
50
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +000051static std::string ImageLocation() {
52 Runtime* runtime = Runtime::Current();
53 const std::vector<gc::space::ImageSpace*>& image_spaces =
54 runtime->GetHeap()->GetBootImageSpaces();
55 if (image_spaces.empty()) {
56 return "";
57 }
58 return image_spaces[0]->GetImageLocation();
59}
60
61bool DexoptTest::Dex2Oat(const std::vector<std::string>& args, std::string* error_msg) {
62 Runtime* runtime = Runtime::Current();
63
64 std::vector<std::string> argv;
65 argv.push_back(runtime->GetCompilerExecutable());
66 if (runtime->IsJavaDebuggable()) {
67 argv.push_back("--debuggable");
68 }
69 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
70
71 if (runtime->GetHiddenApiEnforcementPolicy() != hiddenapi::EnforcementPolicy::kNoChecks) {
72 argv.push_back("--runtime-arg");
73 argv.push_back("-Xhidden-api-checks");
74 }
75
76 if (!kIsTargetBuild) {
77 argv.push_back("--host");
78 }
79
80 argv.push_back("--boot-image=" + ImageLocation());
81
82 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
83 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
84
85 argv.insert(argv.end(), args.begin(), args.end());
86
87 std::string command_line(android::base::Join(argv, ' '));
88 return Exec(argv, error_msg);
89}
90
Calin Juravle36eb3132017-01-13 16:32:38 -080091void DexoptTest::GenerateOatForTest(const std::string& dex_location,
Calin Juravle357c66d2017-05-04 01:57:17 +000092 const std::string& oat_location_in,
93 CompilerFilter::Filter filter,
94 bool relocate,
95 bool pic,
Calin Juravle5f9a8012018-02-12 20:27:46 -080096 bool with_alternate_image,
97 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -080098 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
99 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
Calin Juravle357c66d2017-05-04 01:57:17 +0000100 std::string oat_location = oat_location_in;
Calin Juravle36eb3132017-01-13 16:32:38 -0800101 if (!relocate) {
102 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
103 // relocated image file.
104 ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno);
Calin Juravle357c66d2017-05-04 01:57:17 +0000105 // If the oat location is in dalvik cache, replace the cache path with the temporary one.
106 size_t pos = oat_location.find(dalvik_cache);
107 if (pos != std::string::npos) {
108 oat_location = oat_location.replace(pos, dalvik_cache.length(), dalvik_cache_tmp);
109 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800110 }
111
112 std::vector<std::string> args;
113 args.push_back("--dex-file=" + dex_location);
114 args.push_back("--oat-file=" + oat_location);
115 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
116 args.push_back("--runtime-arg");
117
118 // Use -Xnorelocate regardless of the relocate argument.
119 // We control relocation by redirecting the dalvik cache when needed
120 // rather than use this flag.
121 args.push_back("-Xnorelocate");
122
Mathieu Chartierd0af56c2017-02-17 12:56:25 -0800123 ScratchFile profile_file;
124 if (CompilerFilter::DependsOnProfile(filter)) {
125 args.push_back("--profile-file=" + profile_file.GetFilename());
126 }
127
Calin Juravle36eb3132017-01-13 16:32:38 -0800128 if (pic) {
129 args.push_back("--compile-pic");
130 }
131
132 std::string image_location = GetImageLocation();
133 if (with_alternate_image) {
134 args.push_back("--boot-image=" + GetImageLocation2());
135 }
136
Calin Juravle5f9a8012018-02-12 20:27:46 -0800137 if (compilation_reason != nullptr) {
138 args.push_back("--compilation-reason=" + std::string(compilation_reason));
139 }
140
Calin Juravle36eb3132017-01-13 16:32:38 -0800141 std::string error_msg;
Nicolas Geoffray3d8a78a2018-08-29 21:10:16 +0000142 ASSERT_TRUE(Dex2Oat(args, &error_msg)) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800143
144 if (!relocate) {
145 // Restore the dalvik cache if needed.
146 ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno);
Calin Juravle357c66d2017-05-04 01:57:17 +0000147 oat_location = oat_location_in;
Calin Juravle36eb3132017-01-13 16:32:38 -0800148 }
149
150 // Verify the odex file was generated as expected.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100151 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
152 oat_location.c_str(),
Calin Juravle36eb3132017-01-13 16:32:38 -0800153 oat_location.c_str(),
154 nullptr,
155 nullptr,
156 false,
157 /*low_4gb*/false,
158 dex_location.c_str(),
159 &error_msg));
160 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
161 EXPECT_EQ(pic, odex_file->IsPic());
162 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
163
164 std::unique_ptr<ImageHeader> image_header(
165 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
166 kRuntimeISA,
167 &error_msg));
168 ASSERT_TRUE(image_header != nullptr) << error_msg;
169 const OatHeader& oat_header = odex_file->GetOatHeader();
Richard Uhlerbc26b722017-03-10 14:27:10 +0000170 uint32_t combined_checksum = image_header->GetOatChecksum();
Calin Juravle36eb3132017-01-13 16:32:38 -0800171
172 if (CompilerFilter::DependsOnImageChecksum(filter)) {
173 if (with_alternate_image) {
174 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
175 } else {
176 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
177 }
178 }
179
180 if (!with_alternate_image) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100181 if (CompilerFilter::IsAotCompilationEnabled(filter)) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800182 if (relocate) {
183 EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
184 oat_header.GetImageFileLocationOatDataBegin());
185 EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
186 } else {
187 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
188 oat_header.GetImageFileLocationOatDataBegin());
189 EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
190 }
191 }
192 }
193}
194
195void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
196 const std::string& odex_location,
197 CompilerFilter::Filter filter) {
198 GenerateOatForTest(dex_location,
199 odex_location,
200 filter,
201 /*relocate*/false,
202 /*pic*/false,
203 /*with_alternate_image*/false);
204}
205
206void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location,
207 const std::string& odex_location,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800208 CompilerFilter::Filter filter,
209 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800210 GenerateOatForTest(dex_location,
211 odex_location,
212 filter,
213 /*relocate*/false,
214 /*pic*/true,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800215 /*with_alternate_image*/false,
216 compilation_reason);
Calin Juravle36eb3132017-01-13 16:32:38 -0800217}
218
219void DexoptTest::GenerateOatForTest(const char* dex_location,
220 CompilerFilter::Filter filter,
221 bool relocate,
222 bool pic,
223 bool with_alternate_image) {
224 std::string oat_location;
225 std::string error_msg;
226 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
227 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
228 GenerateOatForTest(dex_location,
229 oat_location,
230 filter,
231 relocate,
232 pic,
233 with_alternate_image);
234}
235
236void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
237 GenerateOatForTest(dex_location,
238 filter,
239 /*relocate*/true,
240 /*pic*/false,
241 /*with_alternate_image*/false);
242}
243
244bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) {
Chris Morin88c6d262018-02-13 15:26:21 -0800245 std::string dalvik_cache;
246 bool have_android_data;
247 bool dalvik_cache_exists;
248 bool is_global_cache;
249 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
250 true,
251 &dalvik_cache,
252 &have_android_data,
253 &dalvik_cache_exists,
254 &is_global_cache);
255 if (!dalvik_cache_exists) {
256 *error_msg = "Failed to create dalvik cache";
Calin Juravle36eb3132017-01-13 16:32:38 -0800257 return false;
258 }
259
260 std::string patchoat = GetAndroidRoot();
261 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
262
263 std::vector<std::string> argv;
264 argv.push_back(patchoat);
265 argv.push_back("--input-image-location=" + image_location);
Chris Morin88c6d262018-02-13 15:26:21 -0800266 argv.push_back("--output-image-directory=" + dalvik_cache);
Calin Juravle36eb3132017-01-13 16:32:38 -0800267 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
268 argv.push_back("--base-offset-delta=0x00008000");
269 return Exec(argv, error_msg);
270}
271
272void DexoptTest::ReserveImageSpace() {
273 MemMap::Init();
274
275 // Ensure a chunk of memory is reserved for the image space.
276 // The reservation_end includes room for the main space that has to come
277 // right after the image in case of the GSS collector.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800278 uint64_t reservation_start = ART_BASE_ADDRESS;
279 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800280
281 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
282 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800283 for (BacktraceMap::iterator it = map->begin();
Calin Juravle36eb3132017-01-13 16:32:38 -0800284 reservation_start < reservation_end && it != map->end(); ++it) {
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800285 const backtrace_map_t* entry = *it;
286 ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
287 reservation_start = std::max(reservation_start, entry->end);
Calin Juravle36eb3132017-01-13 16:32:38 -0800288 }
289 ReserveImageSpaceChunk(reservation_start, reservation_end);
290}
291
292void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
293 if (start < end) {
294 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100295 image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
296 reinterpret_cast<uint8_t*>(start),
297 end - start,
298 PROT_NONE,
299 /* low_4gb*/ false,
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100300 &error_msg));
301 ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800302 LOG(INFO) << "Reserved space for image " <<
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100303 reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
304 reinterpret_cast<void*>(image_reservation_.back().End());
Calin Juravle36eb3132017-01-13 16:32:38 -0800305 }
306}
307
308void DexoptTest::UnreserveImageSpace() {
309 image_reservation_.clear();
310}
311
312} // namespace art