blob: e56f0dc613e6ec73b340e262e4bf913f2d657a22 [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
2 * Copyright (C) 2011 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 "image_space.h"
18
Mathieu Chartierceb07b32015-12-10 09:33:21 -080019#include <lz4.h>
20#include <random>
Andreas Gampe70be1fb2014-10-31 16:45:19 -070021#include <sys/statvfs.h>
Alex Light25396132014-08-27 15:37:23 -070022#include <sys/types.h>
Narayan Kamath5a2be3f2015-02-16 13:51:51 +000023#include <unistd.h>
Alex Light25396132014-08-27 15:37:23 -070024
Andreas Gampe46ee31b2016-12-14 10:11:49 -080025#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080026#include "android-base/strings.h"
27
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_method.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070029#include "base/enums.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070030#include "base/macros.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070031#include "base/stl_util.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010032#include "base/scoped_flock.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080033#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010034#include "base/time_utils.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/space_bitmap-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080036#include "image-inl.h"
Andreas Gampebec63582015-11-20 19:26:51 -080037#include "image_space_fs.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070038#include "mirror/class-inl.h"
39#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070040#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070041#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070042#include "space-inl.h"
43#include "utils.h"
44
45namespace art {
46namespace gc {
47namespace space {
48
Andreas Gampe46ee31b2016-12-14 10:11:49 -080049using android::base::StringAppendF;
50using android::base::StringPrintf;
51
Ian Rogersef7d42f2014-01-06 12:55:46 -080052Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070053
Jeff Haodcdc85b2015-12-04 14:06:18 -080054ImageSpace::ImageSpace(const std::string& image_filename,
55 const char* image_location,
56 MemMap* mem_map,
57 accounting::ContinuousSpaceBitmap* live_bitmap,
Mathieu Chartier2d124ec2016-01-05 18:03:15 -080058 uint8_t* end)
59 : MemMapSpace(image_filename,
60 mem_map,
61 mem_map->Begin(),
62 end,
63 end,
Narayan Kamath52f84882014-05-02 10:10:39 +010064 kGcRetentionPolicyNeverCollect),
Jeff Haodcdc85b2015-12-04 14:06:18 -080065 oat_file_non_owned_(nullptr),
Mathieu Chartier2d124ec2016-01-05 18:03:15 -080066 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070067 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070068 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070069}
70
Alex Lightcf4bf382014-07-24 11:29:14 -070071static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
72 CHECK_ALIGNED(min_delta, kPageSize);
73 CHECK_ALIGNED(max_delta, kPageSize);
74 CHECK_LT(min_delta, max_delta);
75
Alex Light15324762015-11-19 11:03:10 -080076 int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta);
Alex Lightcf4bf382014-07-24 11:29:14 -070077 if (r % 2 == 0) {
78 r = RoundUp(r, kPageSize);
79 } else {
80 r = RoundDown(r, kPageSize);
81 }
82 CHECK_LE(min_delta, r);
83 CHECK_GE(max_delta, r);
84 CHECK_ALIGNED(r, kPageSize);
85 return r;
86}
87
Andreas Gampea463b6a2016-08-12 21:53:32 -070088static int32_t ChooseRelocationOffsetDelta() {
89 return ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA, ART_BASE_ADDRESS_MAX_DELTA);
90}
91
92static bool GenerateImage(const std::string& image_filename,
93 InstructionSet image_isa,
Alex Light25396132014-08-27 15:37:23 -070094 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070095 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
96 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -070097 Split(boot_class_path_string, ':', &boot_class_path);
Brian Carlstrom56d947f2013-07-15 13:14:23 -070098 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070099 *error_msg = "Failed to generate image because no boot class path specified";
100 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700101 }
Alex Light25396132014-08-27 15:37:23 -0700102 // We should clean up so we are more likely to have room for the image.
103 if (Runtime::Current()->IsZygote()) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700104 LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000105 PruneDalvikCache(image_isa);
Alex Light25396132014-08-27 15:37:23 -0700106 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700107
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700108 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700109
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700110 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700111 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700112
113 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100114 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700115 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700116
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700117 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700118 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700119 }
120
121 std::string oat_file_option_string("--oat-file=");
Brian Carlstrom2f1e15c2014-10-27 16:27:06 -0700122 oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700123 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700124
Sebastien Hertz0de11332015-05-13 12:14:05 +0200125 // Note: we do not generate a fully debuggable boot image so we do not pass the
126 // compiler flag --debuggable here.
127
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700128 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700129 CHECK_EQ(image_isa, kRuntimeISA)
130 << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700131
Andreas Gampea463b6a2016-08-12 21:53:32 -0700132 int32_t base_offset = ChooseRelocationOffsetDelta();
Alex Lightcf4bf382014-07-24 11:29:14 -0700133 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
134 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
135 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700136
Brian Carlstrom57309db2014-07-30 15:13:25 -0700137 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700138 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700139 }
140
Brian Carlstrom6449c622014-02-10 23:48:36 -0800141 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800142 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800143 arg_vector.push_back(compiler_options[i].c_str());
144 }
145
Andreas Gampe9186ced2016-12-12 14:28:21 -0800146 std::string command_line(android::base::Join(arg_vector, ' '));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700147 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800148 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700149}
150
Andreas Gampea463b6a2016-08-12 21:53:32 -0700151static bool FindImageFilenameImpl(const char* image_location,
152 const InstructionSet image_isa,
153 bool* has_system,
154 std::string* system_filename,
155 bool* dalvik_cache_exists,
156 std::string* dalvik_cache,
157 bool* is_global_cache,
158 bool* has_cache,
159 std::string* cache_filename) {
160 DCHECK(dalvik_cache != nullptr);
161
Alex Lighta59dd802014-07-02 16:28:08 -0700162 *has_system = false;
163 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700164 // image_location = /system/framework/boot.art
165 // system_image_location = /system/framework/<image_isa>/boot.art
166 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
167 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700168 *system_filename = system_image_filename;
169 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700170 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100171
Alex Lighta59dd802014-07-02 16:28:08 -0700172 bool have_android_data = false;
173 *dalvik_cache_exists = false;
Andreas Gampea463b6a2016-08-12 21:53:32 -0700174 GetDalvikCache(GetInstructionSetString(image_isa),
175 true,
176 dalvik_cache,
177 &have_android_data,
178 dalvik_cache_exists,
179 is_global_cache);
Narayan Kamath52f84882014-05-02 10:10:39 +0100180
Alex Lighta59dd802014-07-02 16:28:08 -0700181 if (have_android_data && *dalvik_cache_exists) {
182 // Always set output location even if it does not exist,
183 // so that the caller knows where to create the image.
184 //
185 // image_location = /system/framework/boot.art
186 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
187 std::string error_msg;
Andreas Gampea463b6a2016-08-12 21:53:32 -0700188 if (!GetDalvikCacheFilename(image_location,
189 dalvik_cache->c_str(),
190 cache_filename,
191 &error_msg)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700192 LOG(WARNING) << error_msg;
193 return *has_system;
194 }
195 *has_cache = OS::FileExists(cache_filename->c_str());
196 }
197 return *has_system || *has_cache;
198}
199
Andreas Gampea463b6a2016-08-12 21:53:32 -0700200bool ImageSpace::FindImageFilename(const char* image_location,
201 const InstructionSet image_isa,
202 std::string* system_filename,
203 bool* has_system,
204 std::string* cache_filename,
205 bool* dalvik_cache_exists,
206 bool* has_cache,
207 bool* is_global_cache) {
208 std::string dalvik_cache_unused;
209 return FindImageFilenameImpl(image_location,
210 image_isa,
211 has_system,
212 system_filename,
213 dalvik_cache_exists,
214 &dalvik_cache_unused,
215 is_global_cache,
216 has_cache,
217 cache_filename);
218}
219
Alex Lighta59dd802014-07-02 16:28:08 -0700220static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
221 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
222 if (image_file.get() == nullptr) {
223 return false;
224 }
225 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
226 if (!success || !image_header->IsValid()) {
227 return false;
228 }
229 return true;
230}
231
Alex Light6e183f22014-07-18 14:57:04 -0700232// Relocate the image at image_location to dest_filename and relocate it by a random amount.
Andreas Gampea463b6a2016-08-12 21:53:32 -0700233static bool RelocateImage(const char* image_location,
234 const char* dest_filename,
235 InstructionSet isa,
236 std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700237 // We should clean up so we are more likely to have room for the image.
238 if (Runtime::Current()->IsZygote()) {
239 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000240 PruneDalvikCache(isa);
Alex Light25396132014-08-27 15:37:23 -0700241 }
242
Alex Lighta59dd802014-07-02 16:28:08 -0700243 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
244
245 std::string input_image_location_arg("--input-image-location=");
246 input_image_location_arg += image_location;
247
248 std::string output_image_filename_arg("--output-image-file=");
249 output_image_filename_arg += dest_filename;
250
Alex Lighta59dd802014-07-02 16:28:08 -0700251 std::string instruction_set_arg("--instruction-set=");
252 instruction_set_arg += GetInstructionSetString(isa);
253
254 std::string base_offset_arg("--base-offset-delta=");
Andreas Gampea463b6a2016-08-12 21:53:32 -0700255 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700256
257 std::vector<std::string> argv;
258 argv.push_back(patchoat);
259
260 argv.push_back(input_image_location_arg);
261 argv.push_back(output_image_filename_arg);
262
Alex Lighta59dd802014-07-02 16:28:08 -0700263 argv.push_back(instruction_set_arg);
264 argv.push_back(base_offset_arg);
265
Andreas Gampe9186ced2016-12-12 14:28:21 -0800266 std::string command_line(android::base::Join(argv, ' '));
Alex Lighta59dd802014-07-02 16:28:08 -0700267 LOG(INFO) << "RelocateImage: " << command_line;
268 return Exec(argv, error_msg);
269}
270
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700271static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700272 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
273 if (!ReadSpecificImageHeader(filename, hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700274 *error_msg = StringPrintf("Unable to read image header for %s", filename);
Alex Lighta59dd802014-07-02 16:28:08 -0700275 return nullptr;
276 }
277 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100278}
279
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700280ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
281 const InstructionSet image_isa,
282 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700283 std::string system_filename;
284 bool has_system = false;
285 std::string cache_filename;
286 bool has_cache = false;
287 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700288 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700289 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700290 &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700291 if (Runtime::Current()->ShouldRelocate()) {
292 if (has_system && has_cache) {
293 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
294 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
295 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700296 *error_msg = StringPrintf("Unable to read image header for %s at %s",
297 image_location, system_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700298 return nullptr;
299 }
300 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700301 *error_msg = StringPrintf("Unable to read image header for %s at %s",
302 image_location, cache_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700303 return nullptr;
304 }
305 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700306 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
307 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700308 return nullptr;
309 }
310 return cache_hdr.release();
311 } else if (!has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700312 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
313 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700314 return nullptr;
315 } else if (!has_system && has_cache) {
316 // This can probably just use the cache one.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700317 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700318 }
319 } else {
320 // We don't want to relocate, Just pick the appropriate one if we have it and return.
321 if (has_system && has_cache) {
322 // We want the cache if the checksum matches, otherwise the system.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700323 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
324 error_msg));
325 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
326 error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700327 if (system.get() == nullptr ||
328 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
329 return cache.release();
330 } else {
331 return system.release();
332 }
333 } else if (has_system) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700334 return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700335 } else if (has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700336 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700337 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100338 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100339 }
340
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700341 *error_msg = StringPrintf("Unable to find image file for %s", image_location);
Narayan Kamath52f84882014-05-02 10:10:39 +0100342 return nullptr;
343}
344
Andreas Gampea463b6a2016-08-12 21:53:32 -0700345static bool ChecksumsMatch(const char* image_a, const char* image_b, std::string* error_msg) {
346 DCHECK(error_msg != nullptr);
347
Alex Lighta59dd802014-07-02 16:28:08 -0700348 ImageHeader hdr_a;
349 ImageHeader hdr_b;
Andreas Gampea463b6a2016-08-12 21:53:32 -0700350
351 if (!ReadSpecificImageHeader(image_a, &hdr_a)) {
352 *error_msg = StringPrintf("Cannot read header of %s", image_a);
353 return false;
354 }
355 if (!ReadSpecificImageHeader(image_b, &hdr_b)) {
356 *error_msg = StringPrintf("Cannot read header of %s", image_b);
357 return false;
358 }
359
360 if (hdr_a.GetOatChecksum() != hdr_b.GetOatChecksum()) {
361 *error_msg = StringPrintf("Checksum mismatch: %u(%s) vs %u(%s)",
362 hdr_a.GetOatChecksum(),
363 image_a,
364 hdr_b.GetOatChecksum(),
365 image_b);
366 return false;
367 }
368
369 return true;
Alex Lighta59dd802014-07-02 16:28:08 -0700370}
371
Robert Sesekbfa1f8d2016-08-15 15:21:09 -0400372static bool CanWriteToDalvikCache(const InstructionSet isa) {
373 const std::string dalvik_cache = GetDalvikCache(GetInstructionSetString(isa));
374 if (access(dalvik_cache.c_str(), O_RDWR) == 0) {
375 return true;
376 } else if (errno != EACCES) {
377 PLOG(WARNING) << "CanWriteToDalvikCache returned error other than EACCES";
378 }
379 return false;
380}
381
382static bool ImageCreationAllowed(bool is_global_cache,
383 const InstructionSet isa,
384 std::string* error_msg) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700385 // Anyone can write into a "local" cache.
386 if (!is_global_cache) {
387 return true;
388 }
389
Robert Sesekbfa1f8d2016-08-15 15:21:09 -0400390 // Only the zygote running as root is allowed to create the global boot image.
391 // If the zygote is running as non-root (and cannot write to the dalvik-cache),
392 // then image creation is not allowed..
Andreas Gampe3c13a792014-09-18 20:56:04 -0700393 if (Runtime::Current()->IsZygote()) {
Robert Sesekbfa1f8d2016-08-15 15:21:09 -0400394 return CanWriteToDalvikCache(isa);
Andreas Gampe3c13a792014-09-18 20:56:04 -0700395 }
396
397 *error_msg = "Only the zygote can create the global boot image.";
398 return false;
399}
400
Mathieu Chartier31e89252013-08-28 11:29:12 -0700401void ImageSpace::VerifyImageAllocations() {
Ian Rogers13735952014-10-08 12:43:28 -0700402 uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700403 while (current < End()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700404 CHECK_ALIGNED(current, kObjectAlignment);
405 auto* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700406 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
David Sehr709b0702016-10-13 09:12:37 -0700407 CHECK(live_bitmap_->Test(obj)) << obj->PrettyTypeOf();
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -0700408 if (kUseBakerReadBarrier) {
409 obj->AssertReadBarrierState();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800410 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700411 current += RoundUp(obj->SizeOf(), kObjectAlignment);
412 }
413}
414
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800415// Helper class for relocating from one range of memory to another.
416class RelocationRange {
417 public:
418 RelocationRange() = default;
419 RelocationRange(const RelocationRange&) = default;
420 RelocationRange(uintptr_t source, uintptr_t dest, uintptr_t length)
421 : source_(source),
422 dest_(dest),
423 length_(length) {}
424
Mathieu Chartier91edc622016-02-16 17:16:01 -0800425 bool InSource(uintptr_t address) const {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800426 return address - source_ < length_;
427 }
428
Mathieu Chartier91edc622016-02-16 17:16:01 -0800429 bool InDest(uintptr_t address) const {
430 return address - dest_ < length_;
431 }
432
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800433 // Translate a source address to the destination space.
434 uintptr_t ToDest(uintptr_t address) const {
Mathieu Chartier91edc622016-02-16 17:16:01 -0800435 DCHECK(InSource(address));
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800436 return address + Delta();
437 }
438
439 // Returns the delta between the dest from the source.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800440 uintptr_t Delta() const {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800441 return dest_ - source_;
442 }
443
444 uintptr_t Source() const {
445 return source_;
446 }
447
448 uintptr_t Dest() const {
449 return dest_;
450 }
451
452 uintptr_t Length() const {
453 return length_;
454 }
455
456 private:
457 const uintptr_t source_;
458 const uintptr_t dest_;
459 const uintptr_t length_;
460};
461
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800462std::ostream& operator<<(std::ostream& os, const RelocationRange& reloc) {
463 return os << "(" << reinterpret_cast<const void*>(reloc.Source()) << "-"
464 << reinterpret_cast<const void*>(reloc.Source() + reloc.Length()) << ")->("
465 << reinterpret_cast<const void*>(reloc.Dest()) << "-"
466 << reinterpret_cast<const void*>(reloc.Dest() + reloc.Length()) << ")";
467}
468
Andreas Gampea463b6a2016-08-12 21:53:32 -0700469// Helper class encapsulating loading, so we can access private ImageSpace members (this is a
470// friend class), but not declare functions in the header.
471class ImageSpaceLoader {
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800472 public:
Andreas Gampea463b6a2016-08-12 21:53:32 -0700473 static std::unique_ptr<ImageSpace> Load(const char* image_location,
474 const std::string& image_filename,
475 bool is_zygote,
476 bool is_global_cache,
Andreas Gampe44c8ed62016-08-19 16:43:00 -0700477 bool validate_oat_file,
Andreas Gampea463b6a2016-08-12 21:53:32 -0700478 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700479 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700480 // Note that we must not use the file descriptor associated with
481 // ScopedFlock::GetFile to Init the image file. We want the file
482 // descriptor (and the associated exclusive lock) to be released when
483 // we leave Create.
484 ScopedFlock image_lock;
485 // Should this be a RDWR lock? This is only a defensive measure, as at
486 // this point the image should exist.
487 // However, only the zygote can write into the global dalvik-cache, so
488 // restrict to zygote processes, or any process that isn't using
489 // /data/dalvik-cache (which we assume to be allowed to write there).
490 const bool rw_lock = is_zygote || !is_global_cache;
491 image_lock.Init(image_filename.c_str(),
492 rw_lock ? (O_CREAT | O_RDWR) : O_RDONLY /* flags */,
493 true /* block */,
494 error_msg);
495 VLOG(startup) << "Using image file " << image_filename.c_str() << " for image location "
496 << image_location;
497 // If we are in /system we can assume the image is good. We can also
498 // assume this if we are using a relocated image (i.e. image checksum
499 // matches) since this is only different by the offset. We need this to
500 // make sure that host tests continue to work.
501 // Since we are the boot image, pass null since we load the oat file from the boot image oat
502 // file name.
503 return Init(image_filename.c_str(),
504 image_location,
Andreas Gampe44c8ed62016-08-19 16:43:00 -0700505 validate_oat_file,
Andreas Gampea463b6a2016-08-12 21:53:32 -0700506 /* oat_file */nullptr,
507 error_msg);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800508 }
509
Andreas Gampea463b6a2016-08-12 21:53:32 -0700510 static std::unique_ptr<ImageSpace> Init(const char* image_filename,
511 const char* image_location,
512 bool validate_oat_file,
513 const OatFile* oat_file,
514 std::string* error_msg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700515 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700516 CHECK(image_filename != nullptr);
517 CHECK(image_location != nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800518
Andreas Gampea463b6a2016-08-12 21:53:32 -0700519 TimingLogger logger(__PRETTY_FUNCTION__, true, VLOG_IS_ON(image));
520 VLOG(image) << "ImageSpace::Init entering image_filename=" << image_filename;
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800521
Andreas Gampea463b6a2016-08-12 21:53:32 -0700522 std::unique_ptr<File> file;
Mathieu Chartier92ec5942016-04-11 12:03:48 -0700523 {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700524 TimingLogger::ScopedTiming timing("OpenImageFile", &logger);
525 file.reset(OS::OpenFileForReading(image_filename));
526 if (file == nullptr) {
527 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
528 return nullptr;
Mathieu Chartier92ec5942016-04-11 12:03:48 -0700529 }
Andreas Gampea463b6a2016-08-12 21:53:32 -0700530 }
531 ImageHeader temp_image_header;
532 ImageHeader* image_header = &temp_image_header;
533 {
534 TimingLogger::ScopedTiming timing("ReadImageHeader", &logger);
535 bool success = file->ReadFully(image_header, sizeof(*image_header));
536 if (!success || !image_header->IsValid()) {
537 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
538 return nullptr;
539 }
540 }
541 // Check that the file is larger or equal to the header size + data size.
542 const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
543 if (image_file_size < sizeof(ImageHeader) + image_header->GetDataSize()) {
544 *error_msg = StringPrintf("Image file truncated: %" PRIu64 " vs. %" PRIu64 ".",
545 image_file_size,
546 sizeof(ImageHeader) + image_header->GetDataSize());
547 return nullptr;
548 }
549
550 if (oat_file != nullptr) {
551 // If we have an oat file, check the oat file checksum. The oat file is only non-null for the
552 // app image case. Otherwise, we open the oat file after the image and check the checksum there.
553 const uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
554 const uint32_t image_oat_checksum = image_header->GetOatChecksum();
555 if (oat_checksum != image_oat_checksum) {
556 *error_msg = StringPrintf("Oat checksum 0x%x does not match the image one 0x%x in image %s",
557 oat_checksum,
558 image_oat_checksum,
559 image_filename);
560 return nullptr;
Mathieu Chartier92ec5942016-04-11 12:03:48 -0700561 }
562 }
563
Andreas Gampea463b6a2016-08-12 21:53:32 -0700564 if (VLOG_IS_ON(startup)) {
565 LOG(INFO) << "Dumping image sections";
566 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
567 const auto section_idx = static_cast<ImageHeader::ImageSections>(i);
568 auto& section = image_header->GetImageSection(section_idx);
569 LOG(INFO) << section_idx << " start="
570 << reinterpret_cast<void*>(image_header->GetImageBegin() + section.Offset()) << " "
571 << section;
Mathieu Chartier92ec5942016-04-11 12:03:48 -0700572 }
Andreas Gampea463b6a2016-08-12 21:53:32 -0700573 }
574
575 const auto& bitmap_section = image_header->GetImageSection(ImageHeader::kSectionImageBitmap);
576 // The location we want to map from is the first aligned page after the end of the stored
577 // (possibly compressed) data.
578 const size_t image_bitmap_offset = RoundUp(sizeof(ImageHeader) + image_header->GetDataSize(),
579 kPageSize);
580 const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size();
581 if (end_of_bitmap != image_file_size) {
582 *error_msg = StringPrintf(
583 "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size,
584 end_of_bitmap);
585 return nullptr;
586 }
587
588 std::unique_ptr<MemMap> map;
589 // GetImageBegin is the preferred address to map the image. If we manage to map the
590 // image at the image begin, the amount of fixup work required is minimized.
591 map.reset(LoadImageFile(image_filename,
592 image_location,
593 *image_header,
594 image_header->GetImageBegin(),
595 file->Fd(),
596 logger,
597 error_msg));
598 // If the header specifies PIC mode, we can also map at a random low_4gb address since we can
599 // relocate in-place.
600 if (map == nullptr && image_header->IsPic()) {
601 map.reset(LoadImageFile(image_filename,
602 image_location,
603 *image_header,
604 /* address */ nullptr,
605 file->Fd(),
606 logger,
607 error_msg));
608 }
609 // Were we able to load something and continue?
610 if (map == nullptr) {
611 DCHECK(!error_msg->empty());
612 return nullptr;
613 }
614 DCHECK_EQ(0, memcmp(image_header, map->Begin(), sizeof(ImageHeader)));
615
616 std::unique_ptr<MemMap> image_bitmap_map(MemMap::MapFileAtAddress(nullptr,
617 bitmap_section.Size(),
618 PROT_READ, MAP_PRIVATE,
619 file->Fd(),
620 image_bitmap_offset,
621 /*low_4gb*/false,
622 /*reuse*/false,
623 image_filename,
624 error_msg));
625 if (image_bitmap_map == nullptr) {
626 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
627 return nullptr;
628 }
629 // Loaded the map, use the image header from the file now in case we patch it with
630 // RelocateInPlace.
631 image_header = reinterpret_cast<ImageHeader*>(map->Begin());
632 const uint32_t bitmap_index = ImageSpace::bitmap_index_.FetchAndAddSequentiallyConsistent(1);
633 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u",
634 image_filename,
635 bitmap_index));
636 // Bitmap only needs to cover until the end of the mirror objects section.
637 const ImageSection& image_objects = image_header->GetImageSection(ImageHeader::kSectionObjects);
638 // We only want the mirror object, not the ArtFields and ArtMethods.
639 uint8_t* const image_end = map->Begin() + image_objects.End();
640 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap;
641 {
642 TimingLogger::ScopedTiming timing("CreateImageBitmap", &logger);
643 bitmap.reset(
644 accounting::ContinuousSpaceBitmap::CreateFromMemMap(
645 bitmap_name,
646 image_bitmap_map.release(),
647 reinterpret_cast<uint8_t*>(map->Begin()),
648 image_objects.End()));
649 if (bitmap == nullptr) {
650 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
651 return nullptr;
652 }
653 }
654 {
655 TimingLogger::ScopedTiming timing("RelocateImage", &logger);
656 if (!RelocateInPlace(*image_header,
657 map->Begin(),
658 bitmap.get(),
659 oat_file,
660 error_msg)) {
661 return nullptr;
662 }
663 }
664 // We only want the mirror object, not the ArtFields and ArtMethods.
665 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename,
666 image_location,
667 map.release(),
668 bitmap.release(),
669 image_end));
670
671 // VerifyImageAllocations() will be called later in Runtime::Init()
672 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
673 // and ArtField::java_lang_reflect_ArtField_, which are used from
674 // Object::SizeOf() which VerifyImageAllocations() calls, are not
675 // set yet at this point.
676 if (oat_file == nullptr) {
677 TimingLogger::ScopedTiming timing("OpenOatFile", &logger);
678 space->oat_file_ = OpenOatFile(*space, image_filename, error_msg);
679 if (space->oat_file_ == nullptr) {
680 DCHECK(!error_msg->empty());
681 return nullptr;
682 }
683 space->oat_file_non_owned_ = space->oat_file_.get();
684 } else {
685 space->oat_file_non_owned_ = oat_file;
686 }
687
688 if (validate_oat_file) {
689 TimingLogger::ScopedTiming timing("ValidateOatFile", &logger);
690 CHECK(space->oat_file_ != nullptr);
691 if (!ValidateOatFile(*space, *space->oat_file_, error_msg)) {
692 DCHECK(!error_msg->empty());
693 return nullptr;
694 }
695 }
696
697 Runtime* runtime = Runtime::Current();
698
699 // If oat_file is null, then it is the boot image space. Use oat_file_non_owned_ from the space
700 // to set the runtime methods.
701 CHECK_EQ(oat_file != nullptr, image_header->IsAppImage());
702 if (image_header->IsAppImage()) {
703 CHECK_EQ(runtime->GetResolutionMethod(),
704 image_header->GetImageMethod(ImageHeader::kResolutionMethod));
705 CHECK_EQ(runtime->GetImtConflictMethod(),
706 image_header->GetImageMethod(ImageHeader::kImtConflictMethod));
707 CHECK_EQ(runtime->GetImtUnimplementedMethod(),
708 image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod));
709 CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves),
710 image_header->GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod));
711 CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly),
712 image_header->GetImageMethod(ImageHeader::kSaveRefsOnlyMethod));
713 CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs),
714 image_header->GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod));
715 CHECK_EQ(runtime->GetCalleeSaveMethod(Runtime::kSaveEverything),
716 image_header->GetImageMethod(ImageHeader::kSaveEverythingMethod));
717 } else if (!runtime->HasResolutionMethod()) {
718 runtime->SetInstructionSet(space->oat_file_non_owned_->GetOatHeader().GetInstructionSet());
719 runtime->SetResolutionMethod(image_header->GetImageMethod(ImageHeader::kResolutionMethod));
720 runtime->SetImtConflictMethod(image_header->GetImageMethod(ImageHeader::kImtConflictMethod));
721 runtime->SetImtUnimplementedMethod(
722 image_header->GetImageMethod(ImageHeader::kImtUnimplementedMethod));
723 runtime->SetCalleeSaveMethod(
724 image_header->GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod),
725 Runtime::kSaveAllCalleeSaves);
726 runtime->SetCalleeSaveMethod(
727 image_header->GetImageMethod(ImageHeader::kSaveRefsOnlyMethod), Runtime::kSaveRefsOnly);
728 runtime->SetCalleeSaveMethod(
729 image_header->GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod),
730 Runtime::kSaveRefsAndArgs);
731 runtime->SetCalleeSaveMethod(
732 image_header->GetImageMethod(ImageHeader::kSaveEverythingMethod), Runtime::kSaveEverything);
733 }
734
735 VLOG(image) << "ImageSpace::Init exiting " << *space.get();
736 if (VLOG_IS_ON(image)) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700737 logger.Dump(LOG_STREAM(INFO));
Andreas Gampea463b6a2016-08-12 21:53:32 -0700738 }
739 return space;
740 }
741
742 private:
743 static MemMap* LoadImageFile(const char* image_filename,
744 const char* image_location,
745 const ImageHeader& image_header,
746 uint8_t* address,
747 int fd,
748 TimingLogger& logger,
749 std::string* error_msg) {
750 TimingLogger::ScopedTiming timing("MapImageFile", &logger);
751 const ImageHeader::StorageMode storage_mode = image_header.GetStorageMode();
752 if (storage_mode == ImageHeader::kStorageModeUncompressed) {
753 return MemMap::MapFileAtAddress(address,
754 image_header.GetImageSize(),
755 PROT_READ | PROT_WRITE,
756 MAP_PRIVATE,
757 fd,
758 0,
759 /*low_4gb*/true,
760 /*reuse*/false,
761 image_filename,
762 error_msg);
763 }
764
765 if (storage_mode != ImageHeader::kStorageModeLZ4 &&
766 storage_mode != ImageHeader::kStorageModeLZ4HC) {
767 *error_msg = StringPrintf("Invalid storage mode in image header %d",
768 static_cast<int>(storage_mode));
769 return nullptr;
770 }
771
772 // Reserve output and decompress into it.
773 std::unique_ptr<MemMap> map(MemMap::MapAnonymous(image_location,
774 address,
775 image_header.GetImageSize(),
776 PROT_READ | PROT_WRITE,
777 /*low_4gb*/true,
778 /*reuse*/false,
779 error_msg));
780 if (map != nullptr) {
781 const size_t stored_size = image_header.GetDataSize();
782 const size_t decompress_offset = sizeof(ImageHeader); // Skip the header.
783 std::unique_ptr<MemMap> temp_map(MemMap::MapFile(sizeof(ImageHeader) + stored_size,
784 PROT_READ,
785 MAP_PRIVATE,
786 fd,
787 /*offset*/0,
788 /*low_4gb*/false,
789 image_filename,
790 error_msg));
791 if (temp_map == nullptr) {
792 DCHECK(!error_msg->empty());
793 return nullptr;
794 }
795 memcpy(map->Begin(), &image_header, sizeof(ImageHeader));
796 const uint64_t start = NanoTime();
797 // LZ4HC and LZ4 have same internal format, both use LZ4_decompress.
798 TimingLogger::ScopedTiming timing2("LZ4 decompress image", &logger);
799 const size_t decompressed_size = LZ4_decompress_safe(
800 reinterpret_cast<char*>(temp_map->Begin()) + sizeof(ImageHeader),
801 reinterpret_cast<char*>(map->Begin()) + decompress_offset,
802 stored_size,
803 map->Size() - decompress_offset);
804 VLOG(image) << "Decompressing image took " << PrettyDuration(NanoTime() - start);
805 if (decompressed_size + sizeof(ImageHeader) != image_header.GetImageSize()) {
806 *error_msg = StringPrintf(
807 "Decompressed size does not match expected image size %zu vs %zu",
808 decompressed_size + sizeof(ImageHeader),
809 image_header.GetImageSize());
810 return nullptr;
811 }
812 }
813
814 return map.release();
815 }
816
817 class FixupVisitor : public ValueObject {
818 public:
819 FixupVisitor(const RelocationRange& boot_image,
820 const RelocationRange& boot_oat,
821 const RelocationRange& app_image,
822 const RelocationRange& app_oat)
823 : boot_image_(boot_image),
824 boot_oat_(boot_oat),
825 app_image_(app_image),
826 app_oat_(app_oat) {}
827
828 // Return the relocated address of a heap object.
829 template <typename T>
830 ALWAYS_INLINE T* ForwardObject(T* src) const {
831 const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src);
832 if (boot_image_.InSource(uint_src)) {
833 return reinterpret_cast<T*>(boot_image_.ToDest(uint_src));
834 }
835 if (app_image_.InSource(uint_src)) {
836 return reinterpret_cast<T*>(app_image_.ToDest(uint_src));
837 }
838 // Since we are fixing up the app image, there should only be pointers to the app image and
839 // boot image.
840 DCHECK(src == nullptr) << reinterpret_cast<const void*>(src);
841 return src;
842 }
843
844 // Return the relocated address of a code pointer (contained by an oat file).
845 ALWAYS_INLINE const void* ForwardCode(const void* src) const {
846 const uintptr_t uint_src = reinterpret_cast<uintptr_t>(src);
847 if (boot_oat_.InSource(uint_src)) {
848 return reinterpret_cast<const void*>(boot_oat_.ToDest(uint_src));
849 }
850 if (app_oat_.InSource(uint_src)) {
851 return reinterpret_cast<const void*>(app_oat_.ToDest(uint_src));
852 }
853 DCHECK(src == nullptr) << src;
854 return src;
855 }
856
857 // Must be called on pointers that already have been relocated to the destination relocation.
858 ALWAYS_INLINE bool IsInAppImage(mirror::Object* object) const {
859 return app_image_.InDest(reinterpret_cast<uintptr_t>(object));
860 }
861
862 protected:
863 // Source section.
864 const RelocationRange boot_image_;
865 const RelocationRange boot_oat_;
866 const RelocationRange app_image_;
867 const RelocationRange app_oat_;
868 };
869
870 // Adapt for mirror::Class::FixupNativePointers.
871 class FixupObjectAdapter : public FixupVisitor {
872 public:
873 template<typename... Args>
874 explicit FixupObjectAdapter(Args... args) : FixupVisitor(args...) {}
875
876 template <typename T>
877 T* operator()(T* obj) const {
878 return ForwardObject(obj);
879 }
880 };
881
882 class FixupRootVisitor : public FixupVisitor {
883 public:
884 template<typename... Args>
885 explicit FixupRootVisitor(Args... args) : FixupVisitor(args...) {}
886
887 ALWAYS_INLINE void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700888 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700889 if (!root->IsNull()) {
890 VisitRoot(root);
891 }
892 }
893
894 ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700895 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700896 mirror::Object* ref = root->AsMirrorPtr();
897 mirror::Object* new_ref = ForwardObject(ref);
898 if (ref != new_ref) {
899 root->Assign(new_ref);
900 }
901 }
902 };
903
904 class FixupObjectVisitor : public FixupVisitor {
905 public:
906 template<typename... Args>
907 explicit FixupObjectVisitor(gc::accounting::ContinuousSpaceBitmap* visited,
908 const PointerSize pointer_size,
909 Args... args)
910 : FixupVisitor(args...),
911 pointer_size_(pointer_size),
912 visited_(visited) {}
913
914 // Fix up separately since we also need to fix up method entrypoints.
915 ALWAYS_INLINE void VisitRootIfNonNull(
916 mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED) const {}
917
918 ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root ATTRIBUTE_UNUSED)
919 const {}
920
Mathieu Chartier31e88222016-10-14 18:43:19 -0700921 ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> obj,
Andreas Gampea463b6a2016-08-12 21:53:32 -0700922 MemberOffset offset,
923 bool is_static ATTRIBUTE_UNUSED) const
924 NO_THREAD_SAFETY_ANALYSIS {
925 // There could be overlap between ranges, we must avoid visiting the same reference twice.
926 // Avoid the class field since we already fixed it up in FixupClassVisitor.
927 if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) {
928 // Space is not yet added to the heap, don't do a read barrier.
929 mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(
930 offset);
931 // Use SetFieldObjectWithoutWriteBarrier to avoid card marking since we are writing to the
932 // image.
933 obj->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(offset, ForwardObject(ref));
934 }
935 }
936
937 // Visit a pointer array and forward corresponding native data. Ignores pointer arrays in the
938 // boot image. Uses the bitmap to ensure the same array is not visited multiple times.
939 template <typename Visitor>
940 void UpdatePointerArrayContents(mirror::PointerArray* array, const Visitor& visitor) const
941 NO_THREAD_SAFETY_ANALYSIS {
942 DCHECK(array != nullptr);
943 DCHECK(visitor.IsInAppImage(array));
944 // The bit for the array contents is different than the bit for the array. Since we may have
945 // already visited the array as a long / int array from walking the bitmap without knowing it
946 // was a pointer array.
947 static_assert(kObjectAlignment == 8u, "array bit may be in another object");
948 mirror::Object* const contents_bit = reinterpret_cast<mirror::Object*>(
949 reinterpret_cast<uintptr_t>(array) + kObjectAlignment);
950 // If the bit is not set then the contents have not yet been updated.
951 if (!visited_->Test(contents_bit)) {
952 array->Fixup<kVerifyNone, kWithoutReadBarrier>(array, pointer_size_, visitor);
953 visited_->Set(contents_bit);
954 }
955 }
956
957 // java.lang.ref.Reference visitor.
Mathieu Chartier31e88222016-10-14 18:43:19 -0700958 void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED,
959 ObjPtr<mirror::Reference> ref) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700960 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
Andreas Gampea463b6a2016-08-12 21:53:32 -0700961 mirror::Object* obj = ref->GetReferent<kWithoutReadBarrier>();
962 ref->SetFieldObjectWithoutWriteBarrier<false, true, kVerifyNone>(
963 mirror::Reference::ReferentOffset(),
964 ForwardObject(obj));
965 }
966
967 void operator()(mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS {
968 if (visited_->Test(obj)) {
969 // Already visited.
970 return;
971 }
972 visited_->Set(obj);
973
974 // Handle class specially first since we need it to be updated to properly visit the rest of
975 // the instance fields.
976 {
977 mirror::Class* klass = obj->GetClass<kVerifyNone, kWithoutReadBarrier>();
978 DCHECK(klass != nullptr) << "Null class in image";
979 // No AsClass since our fields aren't quite fixed up yet.
980 mirror::Class* new_klass = down_cast<mirror::Class*>(ForwardObject(klass));
981 if (klass != new_klass) {
982 obj->SetClass<kVerifyNone>(new_klass);
983 }
984 if (new_klass != klass && IsInAppImage(new_klass)) {
985 // Make sure the klass contents are fixed up since we depend on it to walk the fields.
986 operator()(new_klass);
987 }
988 }
989
990 obj->VisitReferences</*visit native roots*/false, kVerifyNone, kWithoutReadBarrier>(
991 *this,
992 *this);
993 // Note that this code relies on no circular dependencies.
994 // We want to use our own class loader and not the one in the image.
995 if (obj->IsClass<kVerifyNone, kWithoutReadBarrier>()) {
996 mirror::Class* as_klass = obj->AsClass<kVerifyNone, kWithoutReadBarrier>();
997 FixupObjectAdapter visitor(boot_image_, boot_oat_, app_image_, app_oat_);
998 as_klass->FixupNativePointers<kVerifyNone, kWithoutReadBarrier>(as_klass,
999 pointer_size_,
1000 visitor);
1001 // Deal with the pointer arrays. Use the helper function since multiple classes can reference
1002 // the same arrays.
1003 mirror::PointerArray* const vtable = as_klass->GetVTable<kVerifyNone, kWithoutReadBarrier>();
1004 if (vtable != nullptr && IsInAppImage(vtable)) {
1005 operator()(vtable);
1006 UpdatePointerArrayContents(vtable, visitor);
1007 }
1008 mirror::IfTable* iftable = as_klass->GetIfTable<kVerifyNone, kWithoutReadBarrier>();
1009 // Ensure iftable arrays are fixed up since we need GetMethodArray to return the valid
1010 // contents.
Mathieu Chartier6beced42016-11-15 15:51:31 -08001011 if (IsInAppImage(iftable)) {
Andreas Gampea463b6a2016-08-12 21:53:32 -07001012 operator()(iftable);
1013 for (int32_t i = 0, count = iftable->Count(); i < count; ++i) {
1014 if (iftable->GetMethodArrayCount<kVerifyNone, kWithoutReadBarrier>(i) > 0) {
1015 mirror::PointerArray* methods =
1016 iftable->GetMethodArray<kVerifyNone, kWithoutReadBarrier>(i);
1017 if (visitor.IsInAppImage(methods)) {
1018 operator()(methods);
1019 DCHECK(methods != nullptr);
1020 UpdatePointerArrayContents(methods, visitor);
1021 }
Mathieu Chartier92ec5942016-04-11 12:03:48 -07001022 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001023 }
1024 }
1025 }
1026 }
Mathieu Chartier91edc622016-02-16 17:16:01 -08001027
Andreas Gampea463b6a2016-08-12 21:53:32 -07001028 private:
1029 const PointerSize pointer_size_;
1030 gc::accounting::ContinuousSpaceBitmap* const visited_;
1031 };
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001032
Andreas Gampea463b6a2016-08-12 21:53:32 -07001033 class ForwardObjectAdapter {
1034 public:
1035 ALWAYS_INLINE explicit ForwardObjectAdapter(const FixupVisitor* visitor) : visitor_(visitor) {}
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001036
Andreas Gampea463b6a2016-08-12 21:53:32 -07001037 template <typename T>
1038 ALWAYS_INLINE T* operator()(T* src) const {
1039 return visitor_->ForwardObject(src);
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001040 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001041
Andreas Gampea463b6a2016-08-12 21:53:32 -07001042 private:
1043 const FixupVisitor* const visitor_;
1044 };
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001045
Andreas Gampea463b6a2016-08-12 21:53:32 -07001046 class ForwardCodeAdapter {
1047 public:
1048 ALWAYS_INLINE explicit ForwardCodeAdapter(const FixupVisitor* visitor)
1049 : visitor_(visitor) {}
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001050
Andreas Gampea463b6a2016-08-12 21:53:32 -07001051 template <typename T>
1052 ALWAYS_INLINE T* operator()(T* src) const {
1053 return visitor_->ForwardCode(src);
1054 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001055
Andreas Gampea463b6a2016-08-12 21:53:32 -07001056 private:
1057 const FixupVisitor* const visitor_;
1058 };
1059
1060 class FixupArtMethodVisitor : public FixupVisitor, public ArtMethodVisitor {
1061 public:
1062 template<typename... Args>
1063 explicit FixupArtMethodVisitor(bool fixup_heap_objects, PointerSize pointer_size, Args... args)
1064 : FixupVisitor(args...),
1065 fixup_heap_objects_(fixup_heap_objects),
1066 pointer_size_(pointer_size) {}
1067
1068 virtual void Visit(ArtMethod* method) NO_THREAD_SAFETY_ANALYSIS {
1069 // TODO: Separate visitor for runtime vs normal methods.
1070 if (UNLIKELY(method->IsRuntimeMethod())) {
1071 ImtConflictTable* table = method->GetImtConflictTable(pointer_size_);
1072 if (table != nullptr) {
1073 ImtConflictTable* new_table = ForwardObject(table);
1074 if (table != new_table) {
1075 method->SetImtConflictTable(new_table, pointer_size_);
1076 }
1077 }
1078 const void* old_code = method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size_);
1079 const void* new_code = ForwardCode(old_code);
1080 if (old_code != new_code) {
1081 method->SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size_);
1082 }
1083 } else {
1084 if (fixup_heap_objects_) {
1085 method->UpdateObjectsForImageRelocation(ForwardObjectAdapter(this), pointer_size_);
1086 }
1087 method->UpdateEntrypoints<kWithoutReadBarrier>(ForwardCodeAdapter(this), pointer_size_);
1088 }
1089 }
1090
1091 private:
1092 const bool fixup_heap_objects_;
1093 const PointerSize pointer_size_;
1094 };
1095
1096 class FixupArtFieldVisitor : public FixupVisitor, public ArtFieldVisitor {
1097 public:
1098 template<typename... Args>
1099 explicit FixupArtFieldVisitor(Args... args) : FixupVisitor(args...) {}
1100
1101 virtual void Visit(ArtField* field) NO_THREAD_SAFETY_ANALYSIS {
1102 field->UpdateObjects(ForwardObjectAdapter(this));
1103 }
1104 };
1105
1106 // Relocate an image space mapped at target_base which possibly used to be at a different base
1107 // address. Only needs a single image space, not one for both source and destination.
1108 // In place means modifying a single ImageSpace in place rather than relocating from one ImageSpace
1109 // to another.
1110 static bool RelocateInPlace(ImageHeader& image_header,
1111 uint8_t* target_base,
1112 accounting::ContinuousSpaceBitmap* bitmap,
1113 const OatFile* app_oat_file,
1114 std::string* error_msg) {
1115 DCHECK(error_msg != nullptr);
1116 if (!image_header.IsPic()) {
1117 if (image_header.GetImageBegin() == target_base) {
1118 return true;
1119 }
1120 *error_msg = StringPrintf("Cannot relocate non-pic image for oat file %s",
1121 (app_oat_file != nullptr) ? app_oat_file->GetLocation().c_str() : "");
1122 return false;
1123 }
1124 // Set up sections.
1125 uint32_t boot_image_begin = 0;
1126 uint32_t boot_image_end = 0;
1127 uint32_t boot_oat_begin = 0;
1128 uint32_t boot_oat_end = 0;
1129 const PointerSize pointer_size = image_header.GetPointerSize();
1130 gc::Heap* const heap = Runtime::Current()->GetHeap();
1131 heap->GetBootImagesSize(&boot_image_begin, &boot_image_end, &boot_oat_begin, &boot_oat_end);
1132 if (boot_image_begin == boot_image_end) {
1133 *error_msg = "Can not relocate app image without boot image space";
1134 return false;
1135 }
1136 if (boot_oat_begin == boot_oat_end) {
1137 *error_msg = "Can not relocate app image without boot oat file";
1138 return false;
1139 }
1140 const uint32_t boot_image_size = boot_image_end - boot_image_begin;
1141 const uint32_t boot_oat_size = boot_oat_end - boot_oat_begin;
1142 const uint32_t image_header_boot_image_size = image_header.GetBootImageSize();
1143 const uint32_t image_header_boot_oat_size = image_header.GetBootOatSize();
1144 if (boot_image_size != image_header_boot_image_size) {
1145 *error_msg = StringPrintf("Boot image size %" PRIu64 " does not match expected size %"
1146 PRIu64,
1147 static_cast<uint64_t>(boot_image_size),
1148 static_cast<uint64_t>(image_header_boot_image_size));
1149 return false;
1150 }
1151 if (boot_oat_size != image_header_boot_oat_size) {
1152 *error_msg = StringPrintf("Boot oat size %" PRIu64 " does not match expected size %"
1153 PRIu64,
1154 static_cast<uint64_t>(boot_oat_size),
1155 static_cast<uint64_t>(image_header_boot_oat_size));
1156 return false;
1157 }
1158 TimingLogger logger(__FUNCTION__, true, false);
1159 RelocationRange boot_image(image_header.GetBootImageBegin(),
1160 boot_image_begin,
1161 boot_image_size);
1162 RelocationRange boot_oat(image_header.GetBootOatBegin(),
1163 boot_oat_begin,
1164 boot_oat_size);
1165 RelocationRange app_image(reinterpret_cast<uintptr_t>(image_header.GetImageBegin()),
1166 reinterpret_cast<uintptr_t>(target_base),
1167 image_header.GetImageSize());
1168 // Use the oat data section since this is where the OatFile::Begin is.
1169 RelocationRange app_oat(reinterpret_cast<uintptr_t>(image_header.GetOatDataBegin()),
1170 // Not necessarily in low 4GB.
1171 reinterpret_cast<uintptr_t>(app_oat_file->Begin()),
1172 image_header.GetOatDataEnd() - image_header.GetOatDataBegin());
1173 VLOG(image) << "App image " << app_image;
1174 VLOG(image) << "App oat " << app_oat;
1175 VLOG(image) << "Boot image " << boot_image;
1176 VLOG(image) << "Boot oat " << boot_oat;
1177 // True if we need to fixup any heap pointers, otherwise only code pointers.
1178 const bool fixup_image = boot_image.Delta() != 0 || app_image.Delta() != 0;
1179 const bool fixup_code = boot_oat.Delta() != 0 || app_oat.Delta() != 0;
1180 if (!fixup_image && !fixup_code) {
1181 // Nothing to fix up.
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001182 return true;
1183 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001184 ScopedDebugDisallowReadBarriers sddrb(Thread::Current());
1185 // Need to update the image to be at the target base.
1186 const ImageSection& objects_section = image_header.GetImageSection(ImageHeader::kSectionObjects);
1187 uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset());
1188 uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End());
1189 FixupObjectAdapter fixup_adapter(boot_image, boot_oat, app_image, app_oat);
1190 if (fixup_image) {
1191 // Two pass approach, fix up all classes first, then fix up non class-objects.
1192 // The visited bitmap is used to ensure that pointer arrays are not forwarded twice.
1193 std::unique_ptr<gc::accounting::ContinuousSpaceBitmap> visited_bitmap(
1194 gc::accounting::ContinuousSpaceBitmap::Create("Relocate bitmap",
1195 target_base,
1196 image_header.GetImageSize()));
1197 FixupObjectVisitor fixup_object_visitor(visited_bitmap.get(),
1198 pointer_size,
1199 boot_image,
1200 boot_oat,
1201 app_image,
1202 app_oat);
1203 TimingLogger::ScopedTiming timing("Fixup classes", &logger);
1204 // Fixup objects may read fields in the boot image, use the mutator lock here for sanity. Though
1205 // its probably not required.
1206 ScopedObjectAccess soa(Thread::Current());
1207 timing.NewTiming("Fixup objects");
1208 bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_object_visitor);
1209 // Fixup image roots.
1210 CHECK(app_image.InSource(reinterpret_cast<uintptr_t>(
1211 image_header.GetImageRoots<kWithoutReadBarrier>())));
1212 image_header.RelocateImageObjects(app_image.Delta());
1213 CHECK_EQ(image_header.GetImageBegin(), target_base);
1214 // Fix up dex cache DexFile pointers.
1215 auto* dex_caches = image_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kDexCaches)->
1216 AsObjectArray<mirror::DexCache, kVerifyNone, kWithoutReadBarrier>();
1217 for (int32_t i = 0, count = dex_caches->GetLength(); i < count; ++i) {
1218 mirror::DexCache* dex_cache = dex_caches->Get<kVerifyNone, kWithoutReadBarrier>(i);
1219 // Fix up dex cache pointers.
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07001220 mirror::StringDexCacheType* strings = dex_cache->GetStrings();
Andreas Gampea463b6a2016-08-12 21:53:32 -07001221 if (strings != nullptr) {
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07001222 mirror::StringDexCacheType* new_strings = fixup_adapter.ForwardObject(strings);
Andreas Gampea463b6a2016-08-12 21:53:32 -07001223 if (strings != new_strings) {
1224 dex_cache->SetStrings(new_strings);
1225 }
1226 dex_cache->FixupStrings<kWithoutReadBarrier>(new_strings, fixup_adapter);
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001227 }
Vladimir Markoec786222016-12-20 16:24:13 +00001228 mirror::TypeDexCacheType* types = dex_cache->GetResolvedTypes();
Andreas Gampea463b6a2016-08-12 21:53:32 -07001229 if (types != nullptr) {
Vladimir Markoec786222016-12-20 16:24:13 +00001230 mirror::TypeDexCacheType* new_types = fixup_adapter.ForwardObject(types);
Andreas Gampea463b6a2016-08-12 21:53:32 -07001231 if (types != new_types) {
1232 dex_cache->SetResolvedTypes(new_types);
1233 }
1234 dex_cache->FixupResolvedTypes<kWithoutReadBarrier>(new_types, fixup_adapter);
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001235 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001236 ArtMethod** methods = dex_cache->GetResolvedMethods();
1237 if (methods != nullptr) {
1238 ArtMethod** new_methods = fixup_adapter.ForwardObject(methods);
1239 if (methods != new_methods) {
1240 dex_cache->SetResolvedMethods(new_methods);
1241 }
1242 for (size_t j = 0, num = dex_cache->NumResolvedMethods(); j != num; ++j) {
1243 ArtMethod* orig = mirror::DexCache::GetElementPtrSize(new_methods, j, pointer_size);
1244 ArtMethod* copy = fixup_adapter.ForwardObject(orig);
1245 if (orig != copy) {
1246 mirror::DexCache::SetElementPtrSize(new_methods, j, copy, pointer_size);
1247 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001248 }
1249 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001250 ArtField** fields = dex_cache->GetResolvedFields();
1251 if (fields != nullptr) {
1252 ArtField** new_fields = fixup_adapter.ForwardObject(fields);
1253 if (fields != new_fields) {
1254 dex_cache->SetResolvedFields(new_fields);
1255 }
1256 for (size_t j = 0, num = dex_cache->NumResolvedFields(); j != num; ++j) {
1257 ArtField* orig = mirror::DexCache::GetElementPtrSize(new_fields, j, pointer_size);
1258 ArtField* copy = fixup_adapter.ForwardObject(orig);
1259 if (orig != copy) {
1260 mirror::DexCache::SetElementPtrSize(new_fields, j, copy, pointer_size);
1261 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001262 }
1263 }
Narayan Kamath7fe56582016-10-14 18:49:12 +01001264
1265 mirror::MethodTypeDexCacheType* method_types = dex_cache->GetResolvedMethodTypes();
1266 if (method_types != nullptr) {
1267 mirror::MethodTypeDexCacheType* new_method_types =
1268 fixup_adapter.ForwardObject(method_types);
1269 if (method_types != new_method_types) {
1270 dex_cache->SetResolvedMethodTypes(new_method_types);
1271 }
1272 dex_cache->FixupResolvedMethodTypes<kWithoutReadBarrier>(new_method_types, fixup_adapter);
1273 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001274 }
1275 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001276 {
1277 // Only touches objects in the app image, no need for mutator lock.
Andreas Gampea463b6a2016-08-12 21:53:32 -07001278 TimingLogger::ScopedTiming timing("Fixup methods", &logger);
1279 FixupArtMethodVisitor method_visitor(fixup_image,
1280 pointer_size,
1281 boot_image,
1282 boot_oat,
1283 app_image,
1284 app_oat);
1285 image_header.VisitPackedArtMethods(&method_visitor, target_base, pointer_size);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001286 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001287 if (fixup_image) {
1288 {
1289 // Only touches objects in the app image, no need for mutator lock.
1290 TimingLogger::ScopedTiming timing("Fixup fields", &logger);
1291 FixupArtFieldVisitor field_visitor(boot_image, boot_oat, app_image, app_oat);
1292 image_header.VisitPackedArtFields(&field_visitor, target_base);
1293 }
1294 {
1295 TimingLogger::ScopedTiming timing("Fixup imt", &logger);
1296 image_header.VisitPackedImTables(fixup_adapter, target_base, pointer_size);
1297 }
1298 {
1299 TimingLogger::ScopedTiming timing("Fixup conflict tables", &logger);
1300 image_header.VisitPackedImtConflictTables(fixup_adapter, target_base, pointer_size);
1301 }
1302 // In the app image case, the image methods are actually in the boot image.
1303 image_header.RelocateImageMethods(boot_image.Delta());
1304 const auto& class_table_section = image_header.GetImageSection(ImageHeader::kSectionClassTable);
1305 if (class_table_section.Size() > 0u) {
1306 // Note that we require that ReadFromMemory does not make an internal copy of the elements.
1307 // This also relies on visit roots not doing any verification which could fail after we update
1308 // the roots to be the image addresses.
1309 ScopedObjectAccess soa(Thread::Current());
1310 WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
1311 ClassTable temp_table;
1312 temp_table.ReadFromMemory(target_base + class_table_section.Offset());
1313 FixupRootVisitor root_visitor(boot_image, boot_oat, app_image, app_oat);
1314 temp_table.VisitRoots(root_visitor);
1315 }
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001316 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001317 if (VLOG_IS_ON(image)) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -07001318 logger.Dump(LOG_STREAM(INFO));
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001319 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001320 return true;
Andreas Gampe7fa55782016-06-15 17:45:01 -07001321 }
1322
Andreas Gampea463b6a2016-08-12 21:53:32 -07001323 static std::unique_ptr<OatFile> OpenOatFile(const ImageSpace& image,
1324 const char* image_path,
1325 std::string* error_msg) {
1326 const ImageHeader& image_header = image.GetImageHeader();
1327 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
Andreas Gampe7fa55782016-06-15 17:45:01 -07001328
Andreas Gampea463b6a2016-08-12 21:53:32 -07001329 CHECK(image_header.GetOatDataBegin() != nullptr);
1330
1331 std::unique_ptr<OatFile> oat_file(OatFile::Open(oat_filename,
1332 oat_filename,
1333 image_header.GetOatDataBegin(),
1334 image_header.GetOatFileBegin(),
1335 !Runtime::Current()->IsAotCompiler(),
1336 /*low_4gb*/false,
1337 nullptr,
1338 error_msg));
1339 if (oat_file == nullptr) {
1340 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
1341 oat_filename.c_str(),
1342 image.GetName(),
1343 error_msg->c_str());
Andreas Gampe7fa55782016-06-15 17:45:01 -07001344 return nullptr;
1345 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001346 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
1347 uint32_t image_oat_checksum = image_header.GetOatChecksum();
Mathieu Chartier9ff84602016-01-29 12:22:17 -08001348 if (oat_checksum != image_oat_checksum) {
Andreas Gampea463b6a2016-08-12 21:53:32 -07001349 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
1350 " in image %s",
Mathieu Chartier9ff84602016-01-29 12:22:17 -08001351 oat_checksum,
1352 image_oat_checksum,
Andreas Gampea463b6a2016-08-12 21:53:32 -07001353 image.GetName());
Mathieu Chartier9ff84602016-01-29 12:22:17 -08001354 return nullptr;
1355 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001356 int32_t image_patch_delta = image_header.GetPatchDelta();
1357 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
1358 if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
1359 // We should have already relocated by this point. Bail out.
1360 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
1361 "in image %s",
1362 oat_patch_delta,
1363 image_patch_delta,
1364 image.GetName());
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001365 return nullptr;
1366 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001367
1368 return oat_file;
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001369 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001370
1371 static bool ValidateOatFile(const ImageSpace& space,
1372 const OatFile& oat_file,
1373 std::string* error_msg) {
1374 for (const OatFile::OatDexFile* oat_dex_file : oat_file.GetOatDexFiles()) {
1375 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
1376 uint32_t dex_file_location_checksum;
1377 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
1378 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
1379 "%s",
1380 dex_file_location.c_str(),
1381 space.GetName(),
1382 error_msg->c_str());
1383 return false;
1384 }
1385 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
1386 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
1387 "dex file '%s' (0x%x != 0x%x)",
1388 oat_file.GetLocation().c_str(),
1389 dex_file_location.c_str(),
1390 oat_dex_file->GetDexFileLocationChecksum(),
1391 dex_file_location_checksum);
1392 return false;
1393 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001394 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001395 return true;
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001396 }
Andreas Gampea463b6a2016-08-12 21:53:32 -07001397};
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -07001398
Andreas Gampea463b6a2016-08-12 21:53:32 -07001399static constexpr uint64_t kLowSpaceValue = 50 * MB;
1400static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
1401
1402// Read the free space of the cache partition and make a decision whether to keep the generated
1403// image. This is to try to mitigate situations where the system might run out of space later.
1404static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
1405 // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
1406 struct statvfs buf;
1407
1408 int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
1409 if (res != 0) {
1410 // Could not stat. Conservatively tell the system to delete the image.
1411 *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
1412 return false;
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +00001413 }
Nicolas Geoffray1bc977c2016-01-23 14:15:49 +00001414
Andreas Gampea463b6a2016-08-12 21:53:32 -07001415 uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
1416 // Zygote is privileged, but other things are not. Use bavail.
1417 uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
Brian Carlstrom56d947f2013-07-15 13:14:23 -07001418
Andreas Gampea463b6a2016-08-12 21:53:32 -07001419 // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
1420 // environment. We do not want to fail quickening the boot image there, as it is beneficial
1421 // for time-to-UI.
1422 if (fs_overall_size > kTmpFsSentinelValue) {
1423 if (fs_free_size < kLowSpaceValue) {
1424 *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available, need at "
1425 "least %" PRIu64 ".",
1426 static_cast<double>(fs_free_size) / MB,
1427 kLowSpaceValue / MB);
Brian Carlstrom56d947f2013-07-15 13:14:23 -07001428 return false;
1429 }
1430 }
1431 return true;
1432}
1433
Andreas Gampea463b6a2016-08-12 21:53:32 -07001434std::unique_ptr<ImageSpace> ImageSpace::CreateBootImage(const char* image_location,
1435 const InstructionSet image_isa,
1436 bool secondary_image,
1437 std::string* error_msg) {
1438 ScopedTrace trace(__FUNCTION__);
1439
1440 // Step 0: Extra zygote work.
1441
1442 // Step 0.a: If we're the zygote, mark boot.
1443 const bool is_zygote = Runtime::Current()->IsZygote();
Robert Sesekbfa1f8d2016-08-15 15:21:09 -04001444 if (is_zygote && !secondary_image && CanWriteToDalvikCache(image_isa)) {
Andreas Gampea463b6a2016-08-12 21:53:32 -07001445 MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots());
1446 }
1447
1448 // Step 0.b: If we're the zygote, check for free space, and prune the cache preemptively,
1449 // if necessary. While the runtime may be fine (it is pretty tolerant to
1450 // out-of-disk-space situations), other parts of the platform are not.
1451 //
1452 // The advantage of doing this proactively is that the later steps are simplified,
1453 // i.e., we do not need to code retries.
1454 std::string system_filename;
1455 bool has_system = false;
1456 std::string cache_filename;
1457 bool has_cache = false;
1458 bool dalvik_cache_exists = false;
1459 bool is_global_cache = true;
1460 std::string dalvik_cache;
1461 bool found_image = FindImageFilenameImpl(image_location,
1462 image_isa,
1463 &has_system,
1464 &system_filename,
1465 &dalvik_cache_exists,
1466 &dalvik_cache,
1467 &is_global_cache,
1468 &has_cache,
1469 &cache_filename);
1470
1471 if (is_zygote && dalvik_cache_exists) {
1472 DCHECK(!dalvik_cache.empty());
1473 std::string local_error_msg;
1474 if (!CheckSpace(dalvik_cache, &local_error_msg)) {
1475 LOG(WARNING) << local_error_msg << " Preemptively pruning the dalvik cache.";
1476 PruneDalvikCache(image_isa);
1477
1478 // Re-evaluate the image.
1479 found_image = FindImageFilenameImpl(image_location,
1480 image_isa,
1481 &has_system,
1482 &system_filename,
1483 &dalvik_cache_exists,
1484 &dalvik_cache,
1485 &is_global_cache,
1486 &has_cache,
1487 &cache_filename);
1488 }
1489 }
1490
1491 // Collect all the errors.
1492 std::vector<std::string> error_msgs;
1493
1494 // Step 1: Check if we have an existing and relocated image.
1495
1496 // Step 1.a: Have files in system and cache. Then they need to match.
1497 if (found_image && has_system && has_cache) {
1498 std::string local_error_msg;
1499 // Check that the files are matching.
1500 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str(), &local_error_msg)) {
1501 std::unique_ptr<ImageSpace> relocated_space =
1502 ImageSpaceLoader::Load(image_location,
1503 cache_filename,
1504 is_zygote,
1505 is_global_cache,
Andreas Gampe44c8ed62016-08-19 16:43:00 -07001506 /* validate_oat_file */ false,
Andreas Gampea463b6a2016-08-12 21:53:32 -07001507 &local_error_msg);
1508 if (relocated_space != nullptr) {
1509 return relocated_space;
1510 }
1511 }
1512 error_msgs.push_back(local_error_msg);
1513 }
1514
1515 // Step 1.b: Only have a cache file.
1516 if (found_image && !has_system && has_cache) {
1517 std::string local_error_msg;
1518 std::unique_ptr<ImageSpace> cache_space =
1519 ImageSpaceLoader::Load(image_location,
1520 cache_filename,
1521 is_zygote,
1522 is_global_cache,
Andreas Gampe44c8ed62016-08-19 16:43:00 -07001523 /* validate_oat_file */ true,
Andreas Gampea463b6a2016-08-12 21:53:32 -07001524 &local_error_msg);
1525 if (cache_space != nullptr) {
1526 return cache_space;
1527 }
1528 error_msgs.push_back(local_error_msg);
1529 }
1530
1531 // Step 2: We have an existing image in /system.
1532
1533 // Step 2.a: We are not required to relocate it. Then we can use it directly.
1534 bool relocate = Runtime::Current()->ShouldRelocate();
1535
1536 if (found_image && has_system && !relocate) {
1537 std::string local_error_msg;
1538 std::unique_ptr<ImageSpace> system_space =
1539 ImageSpaceLoader::Load(image_location,
1540 system_filename,
1541 is_zygote,
1542 is_global_cache,
Andreas Gampe44c8ed62016-08-19 16:43:00 -07001543 /* validate_oat_file */ false,
Andreas Gampea463b6a2016-08-12 21:53:32 -07001544 &local_error_msg);
1545 if (system_space != nullptr) {
1546 return system_space;
1547 }
1548 error_msgs.push_back(local_error_msg);
1549 }
1550
1551 // Step 2.b: We require a relocated image. Then we must patch it. This step fails if this is a
1552 // secondary image.
1553 if (found_image && has_system && relocate) {
1554 std::string local_error_msg;
1555 if (!Runtime::Current()->IsImageDex2OatEnabled()) {
1556 local_error_msg = "Patching disabled.";
1557 } else if (secondary_image) {
1558 local_error_msg = "Cannot patch a secondary image.";
Robert Sesekbfa1f8d2016-08-15 15:21:09 -04001559 } else if (ImageCreationAllowed(is_global_cache, image_isa, &local_error_msg)) {
Andreas Gampea463b6a2016-08-12 21:53:32 -07001560 bool patch_success =
1561 RelocateImage(image_location, cache_filename.c_str(), image_isa, &local_error_msg);
1562 if (patch_success) {
1563 std::unique_ptr<ImageSpace> patched_space =
1564 ImageSpaceLoader::Load(image_location,
1565 cache_filename,
1566 is_zygote,
1567 is_global_cache,
Andreas Gampe44c8ed62016-08-19 16:43:00 -07001568 /* validate_oat_file */ false,
Andreas Gampea463b6a2016-08-12 21:53:32 -07001569 &local_error_msg);
1570 if (patched_space != nullptr) {
1571 return patched_space;
1572 }
1573 }
1574 }
1575 error_msgs.push_back(StringPrintf("Cannot relocate image %s to %s: %s",
1576 image_location,
1577 cache_filename.c_str(),
1578 local_error_msg.c_str()));
1579 }
1580
1581 // Step 3: We do not have an existing image in /system, so generate an image into the dalvik
1582 // cache. This step fails if this is a secondary image.
1583 if (!has_system) {
1584 std::string local_error_msg;
1585 if (!Runtime::Current()->IsImageDex2OatEnabled()) {
1586 local_error_msg = "Image compilation disabled.";
1587 } else if (secondary_image) {
1588 local_error_msg = "Cannot compile a secondary image.";
Robert Sesekbfa1f8d2016-08-15 15:21:09 -04001589 } else if (ImageCreationAllowed(is_global_cache, image_isa, &local_error_msg)) {
Andreas Gampea463b6a2016-08-12 21:53:32 -07001590 bool compilation_success = GenerateImage(cache_filename, image_isa, &local_error_msg);
1591 if (compilation_success) {
1592 std::unique_ptr<ImageSpace> compiled_space =
1593 ImageSpaceLoader::Load(image_location,
1594 cache_filename,
1595 is_zygote,
1596 is_global_cache,
Andreas Gampe44c8ed62016-08-19 16:43:00 -07001597 /* validate_oat_file */ false,
Andreas Gampea463b6a2016-08-12 21:53:32 -07001598 &local_error_msg);
1599 if (compiled_space != nullptr) {
1600 return compiled_space;
1601 }
1602 }
1603 }
1604 error_msgs.push_back(StringPrintf("Cannot compile image to %s: %s",
1605 cache_filename.c_str(),
1606 local_error_msg.c_str()));
1607 }
1608
1609 // We failed. Prune the cache the free up space, create a compound error message and return no
1610 // image.
1611 PruneDalvikCache(image_isa);
1612
1613 std::ostringstream oss;
1614 bool first = true;
Andreas Gampe4c481a42016-11-03 08:21:59 -07001615 for (const auto& msg : error_msgs) {
Andreas Gampea463b6a2016-08-12 21:53:32 -07001616 if (!first) {
1617 oss << "\n ";
1618 }
1619 oss << msg;
1620 }
1621 *error_msg = oss.str();
1622
1623 return nullptr;
1624}
1625
Andreas Gampe2bd84282016-12-05 12:37:36 -08001626bool ImageSpace::LoadBootImage(const std::string& image_file_name,
1627 const InstructionSet image_instruction_set,
1628 std::vector<space::ImageSpace*>* boot_image_spaces,
1629 uint8_t** oat_file_end) {
1630 DCHECK(boot_image_spaces != nullptr);
1631 DCHECK(boot_image_spaces->empty());
1632 DCHECK(oat_file_end != nullptr);
1633 DCHECK_NE(image_instruction_set, InstructionSet::kNone);
1634
1635 if (image_file_name.empty()) {
1636 return false;
1637 }
1638
1639 // For code reuse, handle this like a work queue.
1640 std::vector<std::string> image_file_names;
1641 image_file_names.push_back(image_file_name);
1642
1643 bool error = false;
1644 uint8_t* oat_file_end_tmp = *oat_file_end;
1645
1646 for (size_t index = 0; index < image_file_names.size(); ++index) {
1647 std::string& image_name = image_file_names[index];
1648 std::string error_msg;
1649 std::unique_ptr<space::ImageSpace> boot_image_space_uptr = CreateBootImage(
1650 image_name.c_str(),
1651 image_instruction_set,
1652 index > 0,
1653 &error_msg);
1654 if (boot_image_space_uptr != nullptr) {
1655 space::ImageSpace* boot_image_space = boot_image_space_uptr.release();
1656 boot_image_spaces->push_back(boot_image_space);
1657 // Oat files referenced by image files immediately follow them in memory, ensure alloc space
1658 // isn't going to get in the middle
1659 uint8_t* oat_file_end_addr = boot_image_space->GetImageHeader().GetOatFileEnd();
1660 CHECK_GT(oat_file_end_addr, boot_image_space->End());
1661 oat_file_end_tmp = AlignUp(oat_file_end_addr, kPageSize);
1662
1663 if (index == 0) {
1664 // If this was the first space, check whether there are more images to load.
1665 const OatFile* boot_oat_file = boot_image_space->GetOatFile();
1666 if (boot_oat_file == nullptr) {
1667 continue;
1668 }
1669
1670 const OatHeader& boot_oat_header = boot_oat_file->GetOatHeader();
1671 const char* boot_classpath =
1672 boot_oat_header.GetStoreValueByKey(OatHeader::kBootClassPathKey);
1673 if (boot_classpath == nullptr) {
1674 continue;
1675 }
1676
1677 ExtractMultiImageLocations(image_file_name, boot_classpath, &image_file_names);
1678 }
1679 } else {
1680 error = true;
1681 LOG(ERROR) << "Could not create image space with image file '" << image_file_name << "'. "
1682 << "Attempting to fall back to imageless running. Error was: " << error_msg
1683 << "\nAttempted image: " << image_name;
1684 break;
1685 }
1686 }
1687
1688 if (error) {
1689 // Remove already loaded spaces.
1690 for (space::Space* loaded_space : *boot_image_spaces) {
1691 delete loaded_space;
1692 }
1693 boot_image_spaces->clear();
1694 return false;
1695 }
1696
1697 *oat_file_end = oat_file_end_tmp;
1698 return true;
1699}
1700
Andreas Gampea463b6a2016-08-12 21:53:32 -07001701std::unique_ptr<ImageSpace> ImageSpace::CreateFromAppImage(const char* image,
1702 const OatFile* oat_file,
1703 std::string* error_msg) {
1704 return ImageSpaceLoader::Init(image,
1705 image,
1706 /*validate_oat_file*/false,
1707 oat_file,
1708 /*out*/error_msg);
1709}
1710
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001711const OatFile* ImageSpace::GetOatFile() const {
Andreas Gampe88da3b02015-06-12 20:38:49 -07001712 return oat_file_non_owned_;
Andreas Gampe22f8e5c2014-07-09 11:38:21 -07001713}
1714
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001715std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() {
1716 CHECK(oat_file_ != nullptr);
1717 return std::move(oat_file_);
Ian Rogers1d54e732013-05-02 21:10:01 -07001718}
1719
Ian Rogers1d54e732013-05-02 21:10:01 -07001720void ImageSpace::Dump(std::ostream& os) const {
1721 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -07001722 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -07001723 << ",end=" << reinterpret_cast<void*>(End())
1724 << ",size=" << PrettySize(Size())
1725 << ",name=\"" << GetName() << "\"]";
1726}
1727
Mathieu Chartier866d8742016-09-21 15:24:18 -07001728std::string ImageSpace::GetMultiImageBootClassPath(
1729 const std::vector<const char*>& dex_locations,
1730 const std::vector<const char*>& oat_filenames,
1731 const std::vector<const char*>& image_filenames) {
1732 DCHECK_GT(oat_filenames.size(), 1u);
1733 // If the image filename was adapted (e.g., for our tests), we need to change this here,
1734 // too, but need to strip all path components (they will be re-established when loading).
1735 std::ostringstream bootcp_oss;
1736 bool first_bootcp = true;
1737 for (size_t i = 0; i < dex_locations.size(); ++i) {
1738 if (!first_bootcp) {
1739 bootcp_oss << ":";
1740 }
1741
1742 std::string dex_loc = dex_locations[i];
1743 std::string image_filename = image_filenames[i];
1744
1745 // Use the dex_loc path, but the image_filename name (without path elements).
1746 size_t dex_last_slash = dex_loc.rfind('/');
1747
1748 // npos is max(size_t). That makes this a bit ugly.
1749 size_t image_last_slash = image_filename.rfind('/');
1750 size_t image_last_at = image_filename.rfind('@');
1751 size_t image_last_sep = (image_last_slash == std::string::npos)
1752 ? image_last_at
1753 : (image_last_at == std::string::npos)
1754 ? std::string::npos
1755 : std::max(image_last_slash, image_last_at);
1756 // Note: whenever image_last_sep == npos, +1 overflow means using the full string.
1757
1758 if (dex_last_slash == std::string::npos) {
1759 dex_loc = image_filename.substr(image_last_sep + 1);
1760 } else {
1761 dex_loc = dex_loc.substr(0, dex_last_slash + 1) +
1762 image_filename.substr(image_last_sep + 1);
1763 }
1764
1765 // Image filenames already end with .art, no need to replace.
1766
1767 bootcp_oss << dex_loc;
1768 first_bootcp = false;
1769 }
1770 return bootcp_oss.str();
1771}
1772
1773void ImageSpace::ExtractMultiImageLocations(const std::string& input_image_file_name,
1774 const std::string& boot_classpath,
1775 std::vector<std::string>* image_file_names) {
Andreas Gampe8994a042015-12-30 19:03:17 +00001776 DCHECK(image_file_names != nullptr);
1777
1778 std::vector<std::string> images;
1779 Split(boot_classpath, ':', &images);
1780
1781 // Add the rest into the list. We have to adjust locations, possibly:
1782 //
1783 // For example, image_file_name is /a/b/c/d/e.art
1784 // images[0] is f/c/d/e.art
1785 // ----------------------------------------------
1786 // images[1] is g/h/i/j.art -> /a/b/h/i/j.art
Mathieu Chartier8b8f6d62016-03-08 16:50:20 -08001787 const std::string& first_image = images[0];
1788 // Length of common suffix.
1789 size_t common = 0;
1790 while (common < input_image_file_name.size() &&
1791 common < first_image.size() &&
1792 *(input_image_file_name.end() - common - 1) == *(first_image.end() - common - 1)) {
1793 ++common;
Andreas Gampe8994a042015-12-30 19:03:17 +00001794 }
Mathieu Chartier8b8f6d62016-03-08 16:50:20 -08001795 // We want to replace the prefix of the input image with the prefix of the boot class path.
1796 // This handles the case where the image file contains @ separators.
1797 // Example image_file_name is oats/system@framework@boot.art
1798 // images[0] is .../arm/boot.art
1799 // means that the image name prefix will be oats/system@framework@
1800 // so that the other images are openable.
1801 const size_t old_prefix_length = first_image.size() - common;
1802 const std::string new_prefix = input_image_file_name.substr(
1803 0,
1804 input_image_file_name.size() - common);
Andreas Gampe8994a042015-12-30 19:03:17 +00001805
1806 // Apply pattern to images[1] .. images[n].
1807 for (size_t i = 1; i < images.size(); ++i) {
Mathieu Chartier8b8f6d62016-03-08 16:50:20 -08001808 const std::string& image = images[i];
1809 CHECK_GT(image.length(), old_prefix_length);
1810 std::string suffix = image.substr(old_prefix_length);
1811 image_file_names->push_back(new_prefix + suffix);
Andreas Gampe8994a042015-12-30 19:03:17 +00001812 }
1813}
1814
Mathieu Chartierd5f3f322016-03-21 14:05:56 -07001815void ImageSpace::DumpSections(std::ostream& os) const {
1816 const uint8_t* base = Begin();
1817 const ImageHeader& header = GetImageHeader();
1818 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1819 auto section_type = static_cast<ImageHeader::ImageSections>(i);
1820 const ImageSection& section = header.GetImageSection(section_type);
1821 os << section_type << " " << reinterpret_cast<const void*>(base + section.Offset())
1822 << "-" << reinterpret_cast<const void*>(base + section.End()) << "\n";
1823 }
1824}
1825
Ian Rogers1d54e732013-05-02 21:10:01 -07001826} // namespace space
1827} // namespace gc
1828} // namespace art