blob: b0eef005510e3c407fc49846cf057fd90fc7b99a [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
Chris Morin88c6d262018-02-13 15:26:21 -080023#include "base/file_utils.h"
David Sehr79e26072018-04-06 17:58:50 -070024#include "base/mem_map.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080025#include "common_runtime_test.h"
26#include "compiler_callbacks.h"
27#include "dex2oat_environment_test.h"
28#include "dexopt_test.h"
29#include "gc/space/image_space.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080030
31namespace art {
32void DexoptTest::SetUp() {
33 ReserveImageSpace();
34 Dex2oatEnvironmentTest::SetUp();
35}
36
37void DexoptTest::PreRuntimeCreate() {
38 std::string error_msg;
39 ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg;
40 ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg;
41 UnreserveImageSpace();
42}
43
44void DexoptTest::PostRuntimeCreate() {
45 ReserveImageSpace();
46}
47
48void DexoptTest::GenerateOatForTest(const std::string& dex_location,
Calin Juravle357c66d2017-05-04 01:57:17 +000049 const std::string& oat_location_in,
50 CompilerFilter::Filter filter,
51 bool relocate,
52 bool pic,
Calin Juravle5f9a8012018-02-12 20:27:46 -080053 bool with_alternate_image,
54 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -080055 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
56 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
Calin Juravle357c66d2017-05-04 01:57:17 +000057 std::string oat_location = oat_location_in;
Calin Juravle36eb3132017-01-13 16:32:38 -080058 if (!relocate) {
59 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
60 // relocated image file.
61 ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno);
Calin Juravle357c66d2017-05-04 01:57:17 +000062 // If the oat location is in dalvik cache, replace the cache path with the temporary one.
63 size_t pos = oat_location.find(dalvik_cache);
64 if (pos != std::string::npos) {
65 oat_location = oat_location.replace(pos, dalvik_cache.length(), dalvik_cache_tmp);
66 }
Calin Juravle36eb3132017-01-13 16:32:38 -080067 }
68
69 std::vector<std::string> args;
70 args.push_back("--dex-file=" + dex_location);
71 args.push_back("--oat-file=" + oat_location);
72 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
73 args.push_back("--runtime-arg");
74
75 // Use -Xnorelocate regardless of the relocate argument.
76 // We control relocation by redirecting the dalvik cache when needed
77 // rather than use this flag.
78 args.push_back("-Xnorelocate");
79
Mathieu Chartierd0af56c2017-02-17 12:56:25 -080080 ScratchFile profile_file;
81 if (CompilerFilter::DependsOnProfile(filter)) {
82 args.push_back("--profile-file=" + profile_file.GetFilename());
83 }
84
Calin Juravle36eb3132017-01-13 16:32:38 -080085 if (pic) {
86 args.push_back("--compile-pic");
87 }
88
89 std::string image_location = GetImageLocation();
90 if (with_alternate_image) {
91 args.push_back("--boot-image=" + GetImageLocation2());
92 }
93
Calin Juravle5f9a8012018-02-12 20:27:46 -080094 if (compilation_reason != nullptr) {
95 args.push_back("--compilation-reason=" + std::string(compilation_reason));
96 }
97
Calin Juravle36eb3132017-01-13 16:32:38 -080098 std::string error_msg;
99 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
100
101 if (!relocate) {
102 // Restore the dalvik cache if needed.
103 ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno);
Calin Juravle357c66d2017-05-04 01:57:17 +0000104 oat_location = oat_location_in;
Calin Juravle36eb3132017-01-13 16:32:38 -0800105 }
106
107 // Verify the odex file was generated as expected.
Nicolas Geoffray30025092018-04-19 14:43:29 +0100108 std::unique_ptr<OatFile> odex_file(OatFile::Open(/* zip_fd */ -1,
109 oat_location.c_str(),
Calin Juravle36eb3132017-01-13 16:32:38 -0800110 oat_location.c_str(),
111 nullptr,
112 nullptr,
113 false,
114 /*low_4gb*/false,
115 dex_location.c_str(),
116 &error_msg));
117 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
118 EXPECT_EQ(pic, odex_file->IsPic());
119 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
120
121 std::unique_ptr<ImageHeader> image_header(
122 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
123 kRuntimeISA,
124 &error_msg));
125 ASSERT_TRUE(image_header != nullptr) << error_msg;
126 const OatHeader& oat_header = odex_file->GetOatHeader();
Richard Uhlerbc26b722017-03-10 14:27:10 +0000127 uint32_t combined_checksum = image_header->GetOatChecksum();
Calin Juravle36eb3132017-01-13 16:32:38 -0800128
129 if (CompilerFilter::DependsOnImageChecksum(filter)) {
130 if (with_alternate_image) {
131 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
132 } else {
133 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
134 }
135 }
136
137 if (!with_alternate_image) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100138 if (CompilerFilter::IsAotCompilationEnabled(filter)) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800139 if (relocate) {
140 EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
141 oat_header.GetImageFileLocationOatDataBegin());
142 EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
143 } else {
144 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
145 oat_header.GetImageFileLocationOatDataBegin());
146 EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
147 }
148 }
149 }
150}
151
152void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
153 const std::string& odex_location,
154 CompilerFilter::Filter filter) {
155 GenerateOatForTest(dex_location,
156 odex_location,
157 filter,
158 /*relocate*/false,
159 /*pic*/false,
160 /*with_alternate_image*/false);
161}
162
163void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location,
164 const std::string& odex_location,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800165 CompilerFilter::Filter filter,
166 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800167 GenerateOatForTest(dex_location,
168 odex_location,
169 filter,
170 /*relocate*/false,
171 /*pic*/true,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800172 /*with_alternate_image*/false,
173 compilation_reason);
Calin Juravle36eb3132017-01-13 16:32:38 -0800174}
175
176void DexoptTest::GenerateOatForTest(const char* dex_location,
177 CompilerFilter::Filter filter,
178 bool relocate,
179 bool pic,
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 relocate,
189 pic,
190 with_alternate_image);
191}
192
193void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
194 GenerateOatForTest(dex_location,
195 filter,
196 /*relocate*/true,
197 /*pic*/false,
198 /*with_alternate_image*/false);
199}
200
201bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) {
Chris Morin88c6d262018-02-13 15:26:21 -0800202 std::string dalvik_cache;
203 bool have_android_data;
204 bool dalvik_cache_exists;
205 bool is_global_cache;
206 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
207 true,
208 &dalvik_cache,
209 &have_android_data,
210 &dalvik_cache_exists,
211 &is_global_cache);
212 if (!dalvik_cache_exists) {
213 *error_msg = "Failed to create dalvik cache";
Calin Juravle36eb3132017-01-13 16:32:38 -0800214 return false;
215 }
216
217 std::string patchoat = GetAndroidRoot();
218 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
219
220 std::vector<std::string> argv;
221 argv.push_back(patchoat);
222 argv.push_back("--input-image-location=" + image_location);
Chris Morin88c6d262018-02-13 15:26:21 -0800223 argv.push_back("--output-image-directory=" + dalvik_cache);
Calin Juravle36eb3132017-01-13 16:32:38 -0800224 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
225 argv.push_back("--base-offset-delta=0x00008000");
226 return Exec(argv, error_msg);
227}
228
229void DexoptTest::ReserveImageSpace() {
230 MemMap::Init();
231
232 // Ensure a chunk of memory is reserved for the image space.
233 // The reservation_end includes room for the main space that has to come
234 // right after the image in case of the GSS collector.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800235 uint64_t reservation_start = ART_BASE_ADDRESS;
236 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800237
238 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
239 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800240 for (BacktraceMap::iterator it = map->begin();
Calin Juravle36eb3132017-01-13 16:32:38 -0800241 reservation_start < reservation_end && it != map->end(); ++it) {
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800242 const backtrace_map_t* entry = *it;
243 ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
244 reservation_start = std::max(reservation_start, entry->end);
Calin Juravle36eb3132017-01-13 16:32:38 -0800245 }
246 ReserveImageSpaceChunk(reservation_start, reservation_end);
247}
248
249void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
250 if (start < end) {
251 std::string error_msg;
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100252 image_reservation_.push_back(MemMap::MapAnonymous("image reservation",
253 reinterpret_cast<uint8_t*>(start),
254 end - start,
255 PROT_NONE,
256 /* low_4gb*/ false,
257 /* reuse */ false,
258 &error_msg));
259 ASSERT_TRUE(image_reservation_.back().IsValid()) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800260 LOG(INFO) << "Reserved space for image " <<
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100261 reinterpret_cast<void*>(image_reservation_.back().Begin()) << "-" <<
262 reinterpret_cast<void*>(image_reservation_.back().End());
Calin Juravle36eb3132017-01-13 16:32:38 -0800263 }
264}
265
266void DexoptTest::UnreserveImageSpace() {
267 image_reservation_.clear();
268}
269
270} // namespace art