blob: db65e40da55aba2cbadc94549573e48dde09da3e [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
23#include "common_runtime_test.h"
24#include "compiler_callbacks.h"
25#include "dex2oat_environment_test.h"
26#include "dexopt_test.h"
27#include "gc/space/image_space.h"
28#include "mem_map.h"
29
30namespace art {
31void DexoptTest::SetUp() {
32 ReserveImageSpace();
33 Dex2oatEnvironmentTest::SetUp();
34}
35
36void DexoptTest::PreRuntimeCreate() {
37 std::string error_msg;
38 ASSERT_TRUE(PreRelocateImage(GetImageLocation(), &error_msg)) << error_msg;
39 ASSERT_TRUE(PreRelocateImage(GetImageLocation2(), &error_msg)) << error_msg;
40 UnreserveImageSpace();
41}
42
43void DexoptTest::PostRuntimeCreate() {
44 ReserveImageSpace();
45}
46
47void DexoptTest::GenerateOatForTest(const std::string& dex_location,
48 const std::string& oat_location,
49 CompilerFilter::Filter filter,
50 bool relocate,
51 bool pic,
52 bool with_alternate_image) {
53 std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(kRuntimeISA));
54 std::string dalvik_cache_tmp = dalvik_cache + ".redirected";
55
56 if (!relocate) {
57 // Temporarily redirect the dalvik cache so dex2oat doesn't find the
58 // relocated image file.
59 ASSERT_EQ(0, rename(dalvik_cache.c_str(), dalvik_cache_tmp.c_str())) << strerror(errno);
60 }
61
62 std::vector<std::string> args;
63 args.push_back("--dex-file=" + dex_location);
64 args.push_back("--oat-file=" + oat_location);
65 args.push_back("--compiler-filter=" + CompilerFilter::NameOfFilter(filter));
66 args.push_back("--runtime-arg");
67
68 // Use -Xnorelocate regardless of the relocate argument.
69 // We control relocation by redirecting the dalvik cache when needed
70 // rather than use this flag.
71 args.push_back("-Xnorelocate");
72
Mathieu Chartierd0af56c2017-02-17 12:56:25 -080073 ScratchFile profile_file;
74 if (CompilerFilter::DependsOnProfile(filter)) {
75 args.push_back("--profile-file=" + profile_file.GetFilename());
76 }
77
Calin Juravle36eb3132017-01-13 16:32:38 -080078 if (pic) {
79 args.push_back("--compile-pic");
80 }
81
82 std::string image_location = GetImageLocation();
83 if (with_alternate_image) {
84 args.push_back("--boot-image=" + GetImageLocation2());
85 }
86
87 std::string error_msg;
88 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
89
90 if (!relocate) {
91 // Restore the dalvik cache if needed.
92 ASSERT_EQ(0, rename(dalvik_cache_tmp.c_str(), dalvik_cache.c_str())) << strerror(errno);
93 }
94
95 // Verify the odex file was generated as expected.
96 std::unique_ptr<OatFile> odex_file(OatFile::Open(oat_location.c_str(),
97 oat_location.c_str(),
98 nullptr,
99 nullptr,
100 false,
101 /*low_4gb*/false,
102 dex_location.c_str(),
103 &error_msg));
104 ASSERT_TRUE(odex_file.get() != nullptr) << error_msg;
105 EXPECT_EQ(pic, odex_file->IsPic());
106 EXPECT_EQ(filter, odex_file->GetCompilerFilter());
107
108 std::unique_ptr<ImageHeader> image_header(
109 gc::space::ImageSpace::ReadImageHeader(image_location.c_str(),
110 kRuntimeISA,
111 &error_msg));
112 ASSERT_TRUE(image_header != nullptr) << error_msg;
113 const OatHeader& oat_header = odex_file->GetOatHeader();
Richard Uhlerbc26b722017-03-10 14:27:10 +0000114 uint32_t combined_checksum = image_header->GetOatChecksum();
Calin Juravle36eb3132017-01-13 16:32:38 -0800115
116 if (CompilerFilter::DependsOnImageChecksum(filter)) {
117 if (with_alternate_image) {
118 EXPECT_NE(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
119 } else {
120 EXPECT_EQ(combined_checksum, oat_header.GetImageFileLocationOatChecksum());
121 }
122 }
123
124 if (!with_alternate_image) {
125 if (CompilerFilter::IsBytecodeCompilationEnabled(filter)) {
126 if (relocate) {
127 EXPECT_EQ(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
128 oat_header.GetImageFileLocationOatDataBegin());
129 EXPECT_EQ(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
130 } else {
131 EXPECT_NE(reinterpret_cast<uintptr_t>(image_header->GetOatDataBegin()),
132 oat_header.GetImageFileLocationOatDataBegin());
133 EXPECT_NE(image_header->GetPatchDelta(), oat_header.GetImagePatchDelta());
134 }
135 }
136 }
137}
138
139void DexoptTest::GenerateOdexForTest(const std::string& dex_location,
140 const std::string& odex_location,
141 CompilerFilter::Filter filter) {
142 GenerateOatForTest(dex_location,
143 odex_location,
144 filter,
145 /*relocate*/false,
146 /*pic*/false,
147 /*with_alternate_image*/false);
148}
149
150void DexoptTest::GeneratePicOdexForTest(const std::string& dex_location,
151 const std::string& odex_location,
152 CompilerFilter::Filter filter) {
153 GenerateOatForTest(dex_location,
154 odex_location,
155 filter,
156 /*relocate*/false,
157 /*pic*/true,
158 /*with_alternate_image*/false);
159}
160
161void DexoptTest::GenerateOatForTest(const char* dex_location,
162 CompilerFilter::Filter filter,
163 bool relocate,
164 bool pic,
165 bool with_alternate_image) {
166 std::string oat_location;
167 std::string error_msg;
168 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
169 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
170 GenerateOatForTest(dex_location,
171 oat_location,
172 filter,
173 relocate,
174 pic,
175 with_alternate_image);
176}
177
178void DexoptTest::GenerateOatForTest(const char* dex_location, CompilerFilter::Filter filter) {
179 GenerateOatForTest(dex_location,
180 filter,
181 /*relocate*/true,
182 /*pic*/false,
183 /*with_alternate_image*/false);
184}
185
186bool DexoptTest::PreRelocateImage(const std::string& image_location, std::string* error_msg) {
187 std::string image;
188 if (!GetCachedImageFile(image_location, &image, error_msg)) {
189 return false;
190 }
191
192 std::string patchoat = GetAndroidRoot();
193 patchoat += kIsDebugBuild ? "/bin/patchoatd" : "/bin/patchoat";
194
195 std::vector<std::string> argv;
196 argv.push_back(patchoat);
197 argv.push_back("--input-image-location=" + image_location);
198 argv.push_back("--output-image-file=" + image);
199 argv.push_back("--instruction-set=" + std::string(GetInstructionSetString(kRuntimeISA)));
200 argv.push_back("--base-offset-delta=0x00008000");
201 return Exec(argv, error_msg);
202}
203
204void DexoptTest::ReserveImageSpace() {
205 MemMap::Init();
206
207 // Ensure a chunk of memory is reserved for the image space.
208 // The reservation_end includes room for the main space that has to come
209 // right after the image in case of the GSS collector.
210 uintptr_t reservation_start = ART_BASE_ADDRESS;
211 uintptr_t reservation_end = ART_BASE_ADDRESS + 384 * MB;
212
213 std::unique_ptr<BacktraceMap> map(BacktraceMap::Create(getpid(), true));
214 ASSERT_TRUE(map.get() != nullptr) << "Failed to build process map";
215 for (BacktraceMap::const_iterator it = map->begin();
216 reservation_start < reservation_end && it != map->end(); ++it) {
217 ReserveImageSpaceChunk(reservation_start, std::min(it->start, reservation_end));
218 reservation_start = std::max(reservation_start, it->end);
219 }
220 ReserveImageSpaceChunk(reservation_start, reservation_end);
221}
222
223void DexoptTest::ReserveImageSpaceChunk(uintptr_t start, uintptr_t end) {
224 if (start < end) {
225 std::string error_msg;
226 image_reservation_.push_back(std::unique_ptr<MemMap>(
227 MemMap::MapAnonymous("image reservation",
228 reinterpret_cast<uint8_t*>(start), end - start,
229 PROT_NONE, false, false, &error_msg)));
230 ASSERT_TRUE(image_reservation_.back().get() != nullptr) << error_msg;
231 LOG(INFO) << "Reserved space for image " <<
232 reinterpret_cast<void*>(image_reservation_.back()->Begin()) << "-" <<
233 reinterpret_cast<void*>(image_reservation_.back()->End());
234 }
235}
236
237void DexoptTest::UnreserveImageSpace() {
238 image_reservation_.clear();
239}
240
241} // namespace art