blob: c19fa82877c81eb1509229788687d6b18a9646d9 [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"
Calin Juravle36eb3132017-01-13 16:32:38 -080024#include "common_runtime_test.h"
25#include "compiler_callbacks.h"
26#include "dex2oat_environment_test.h"
27#include "dexopt_test.h"
28#include "gc/space/image_space.h"
29#include "mem_map.h"
30
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.
108 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(),
109 oat_location.c_str(),
110 nullptr,
111 nullptr,
112 false,
113 /*low_4gb*/false,
114 dex_location.c_str(),
115 &error_msg));
116 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
117 EXPECT_EQ(pic, odex_file->IsPic());
118 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
119
120 std::unique_ptr<ImageHeader> image_header(
121 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
122 kRuntimeISA,
123 &error_msg));
124 ASSERT_TRUE(image_header != nullptr) << error_msg;
125 const OatHeader& oat_header = odex_file->GetOatHeader();
Richard Uhlerbc26b722017-03-10 14:27:10 +0000126 uint32_t combined_checksum = image_header->GetOatChecksum();
Calin Juravle36eb3132017-01-13 16:32:38 -0800127
128 if (CompilerFilter::DependsOnImageChecksum(filter)) {
129 if (with_alternate_image) {
130 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
131 } else {
132 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
133 }
134 }
135
136 if (!with_alternate_image) {
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100137 if (CompilerFilter::IsAotCompilationEnabled(filter)) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800138 if (relocate) {
139 EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
140 oat_header.GetImageFileLocationOatDataBegin());
141 EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
142 } else {
143 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
144 oat_header.GetImageFileLocationOatDataBegin());
145 EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
146 }
147 }
148 }
149}
150
151void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
152 const std::string& odex_location,
153 CompilerFilter::Filter filter) {
154 GenerateOatForTest(dex_location,
155 odex_location,
156 filter,
157 /*relocate*/false,
158 /*pic*/false,
159 /*with_alternate_image*/false);
160}
161
162void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location,
163 const std::string& odex_location,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800164 CompilerFilter::Filter filter,
165 const char* compilation_reason) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800166 GenerateOatForTest(dex_location,
167 odex_location,
168 filter,
169 /*relocate*/false,
170 /*pic*/true,
Calin Juravle5f9a8012018-02-12 20:27:46 -0800171 /*with_alternate_image*/false,
172 compilation_reason);
Calin Juravle36eb3132017-01-13 16:32:38 -0800173}
174
175void DexoptTest::GenerateOatForTest(const char* dex_location,
176 CompilerFilter::Filter filter,
177 bool relocate,
178 bool pic,
179 bool with_alternate_image) {
180 std::string oat_location;
181 std::string error_msg;
182 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
183 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
184 GenerateOatForTest(dex_location,
185 oat_location,
186 filter,
187 relocate,
188 pic,
189 with_alternate_image);
190}
191
192void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
193 GenerateOatForTest(dex_location,
194 filter,
195 /*relocate*/true,
196 /*pic*/false,
197 /*with_alternate_image*/false);
198}
199
200bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) {
Chris Morin88c6d262018-02-13 15:26:21 -0800201 std::string dalvik_cache;
202 bool have_android_data;
203 bool dalvik_cache_exists;
204 bool is_global_cache;
205 GetDalvikCache(GetInstructionSetString(kRuntimeISA),
206 true,
207 &dalvik_cache,
208 &have_android_data,
209 &dalvik_cache_exists,
210 &is_global_cache);
211 if (!dalvik_cache_exists) {
212 *error_msg = "Failed to create dalvik cache";
Calin Juravle36eb3132017-01-13 16:32:38 -0800213 return false;
214 }
215
216 std::string patchoat = GetAndroidRoot();
217 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
218
219 std::vector<std::string> argv;
220 argv.push_back(patchoat);
221 argv.push_back("--input-image-location=" + image_location);
Chris Morin88c6d262018-02-13 15:26:21 -0800222 argv.push_back("--output-image-directory=" + dalvik_cache);
Calin Juravle36eb3132017-01-13 16:32:38 -0800223 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
224 argv.push_back("--base-offset-delta=0x00008000");
225 return Exec(argv, error_msg);
226}
227
228void DexoptTest::ReserveImageSpace() {
229 MemMap::Init();
230
231 // Ensure a chunk of memory is reserved for the image space.
232 // The reservation_end includes room for the main space that has to come
233 // right after the image in case of the GSS collector.
Christopher Ferris77b38df2018-01-18 16:16:49 -0800234 uint64_t reservation_start = ART_BASE_ADDRESS;
235 uint64_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
Calin Juravle36eb3132017-01-13 16:32:38 -0800236
237 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
238 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800239 for (BacktraceMap::iterator it = map->begin();
Calin Juravle36eb3132017-01-13 16:32:38 -0800240 reservation_start < reservation_end && it != map->end(); ++it) {
Christopher Ferris5cf8b532017-12-03 12:46:17 -0800241 const backtrace_map_t* entry = *it;
242 ReserveImageSpaceChunk(reservation_start, std::min(entry->start, reservation_end));
243 reservation_start = std::max(reservation_start, entry->end);
Calin Juravle36eb3132017-01-13 16:32:38 -0800244 }
245 ReserveImageSpaceChunk(reservation_start, reservation_end);
246}
247
248void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
249 if (start < end) {
250 std::string error_msg;
251 image_reservation_.push_back(std::unique_ptr<MemMap>(
252 MemMap::MapAnonymous("image reservation",
253 reinterpret_cast<uint8_t*>(start), end - start,
254 PROT_NONE, false, false, &error_msg)));
255 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
256 LOG(INFO) << "Reserved space for image " <<
257 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
258 reinterpret_cast<void*>(image_reservation_.back()->End());
259 }
260}
261
262void DexoptTest::UnreserveImageSpace() {
263 image_reservation_.clear();
264}
265
266} // namespace art