blob: 071997f6b59a28632faed914bbdf7d636515869c [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
Alex Light25396132014-08-27 15:37:23 -070019#include <dirent.h>
Andreas Gampe70be1fb2014-10-31 16:45:19 -070020#include <sys/statvfs.h>
Alex Light25396132014-08-27 15:37:23 -070021#include <sys/types.h>
22
Alex Lighta59dd802014-07-02 16:28:08 -070023#include <random>
24
Ian Rogersc7dd2952014-10-21 23:31:19 -070025#include "base/macros.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070026#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "base/unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010028#include "base/scoped_flock.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070030#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "mirror/class-inl.h"
32#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070033#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "space-inl.h"
36#include "utils.h"
37
38namespace art {
39namespace gc {
40namespace space {
41
Ian Rogersef7d42f2014-01-06 12:55:46 -080042Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070043
Narayan Kamath52f84882014-05-02 10:10:39 +010044ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
45 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap)
46 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
47 kGcRetentionPolicyNeverCollect),
48 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070049 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070050 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070051}
52
Alex Lightcf4bf382014-07-24 11:29:14 -070053static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
54 CHECK_ALIGNED(min_delta, kPageSize);
55 CHECK_ALIGNED(max_delta, kPageSize);
56 CHECK_LT(min_delta, max_delta);
57
58 std::default_random_engine generator;
59 generator.seed(NanoTime() * getpid());
60 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
61 int32_t r = distribution(generator);
62 if (r % 2 == 0) {
63 r = RoundUp(r, kPageSize);
64 } else {
65 r = RoundDown(r, kPageSize);
66 }
67 CHECK_LE(min_delta, r);
68 CHECK_GE(max_delta, r);
69 CHECK_ALIGNED(r, kPageSize);
70 return r;
71}
72
Alex Light25396132014-08-27 15:37:23 -070073// We are relocating or generating the core image. We should get rid of everything. It is all
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080074// out-of-date. We also don't really care if this fails since it is just a convenience.
Alex Light25396132014-08-27 15:37:23 -070075// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
76// Note this should only be used during first boot.
Narayan Kamath28bc9872014-11-07 17:46:28 +000077static void RealPruneDalvikCache(const std::string& cache_dir_path);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080078
Narayan Kamath28bc9872014-11-07 17:46:28 +000079static void PruneDalvikCache(InstructionSet isa) {
Alex Light25396132014-08-27 15:37:23 -070080 CHECK_NE(isa, kNone);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080081 // Prune the base /data/dalvik-cache.
Narayan Kamath28bc9872014-11-07 17:46:28 +000082 RealPruneDalvikCache(GetDalvikCacheOrDie(".", false));
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080083 // Prune /data/dalvik-cache/<isa>.
Narayan Kamath28bc9872014-11-07 17:46:28 +000084 RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
Alex Light25396132014-08-27 15:37:23 -070085}
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080086
Narayan Kamath28bc9872014-11-07 17:46:28 +000087static void RealPruneDalvikCache(const std::string& cache_dir_path) {
Alex Light25396132014-08-27 15:37:23 -070088 if (!OS::DirectoryExists(cache_dir_path.c_str())) {
89 return;
90 }
91 DIR* cache_dir = opendir(cache_dir_path.c_str());
92 if (cache_dir == nullptr) {
93 PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
94 return;
95 }
Alex Light25396132014-08-27 15:37:23 -070096
97 for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
98 const char* name = de->d_name;
99 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
100 continue;
101 }
Andreas Gampe8db9dcd2014-11-09 18:14:30 -0800102 // We only want to delete regular files and symbolic links.
103 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
Alex Light25396132014-08-27 15:37:23 -0700104 if (de->d_type != DT_DIR) {
105 // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
106 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
107 }
108 continue;
109 }
Brian Carlstromdebdda02014-08-28 22:17:13 -0700110 std::string cache_file(cache_dir_path);
111 cache_file += '/';
112 cache_file += name;
113 if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) {
114 PLOG(ERROR) << "Unable to unlink " << cache_file;
Alex Light25396132014-08-27 15:37:23 -0700115 continue;
116 }
117 }
118 CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
119}
120
Narayan Kamath28bc9872014-11-07 17:46:28 +0000121// We write out an empty file to the zygote's ISA specific cache dir at the start of
122// every zygote boot and delete it when the boot completes. If we find a file already
123// present, it usually means the boot didn't complete. We wipe the entire dalvik
124// cache if that's the case.
125static void MarkZygoteStart(const InstructionSet isa) {
126 const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
127 const std::string boot_marker = isa_subdir + "/.booting";
128
129 if (OS::FileExists(boot_marker.c_str())) {
130 LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
131 RealPruneDalvikCache(isa_subdir);
132 }
133
134 VLOG(startup) << "Creating boot start marker: " << boot_marker;
135 std::unique_ptr<File> f(OS::CreateEmptyFile(boot_marker.c_str()));
136 if (f.get() != nullptr) {
137 if (f->FlushCloseOrErase() != 0) {
138 PLOG(WARNING) << "Failed to write boot marker.";
139 }
140 }
141}
142
Alex Light25396132014-08-27 15:37:23 -0700143static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
144 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700145 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
146 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700147 Split(boot_class_path_string, ':', &boot_class_path);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700148 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700149 *error_msg = "Failed to generate image because no boot class path specified";
150 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700151 }
Alex Light25396132014-08-27 15:37:23 -0700152 // We should clean up so we are more likely to have room for the image.
153 if (Runtime::Current()->IsZygote()) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700154 LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000155 PruneDalvikCache(image_isa);
Alex Light25396132014-08-27 15:37:23 -0700156 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700157
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700158 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700159
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700160 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700161 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700162
163 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100164 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700165 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700166
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700167 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700168 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700169 }
170
171 std::string oat_file_option_string("--oat-file=");
Brian Carlstrom2f1e15c2014-10-27 16:27:06 -0700172 oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700173 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700174
Ian Rogers8afeb852014-04-02 14:55:49 -0700175 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700176 CHECK_EQ(image_isa, kRuntimeISA)
177 << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700178
Alex Lightcf4bf382014-07-24 11:29:14 -0700179 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
180 ART_BASE_ADDRESS_MAX_DELTA);
181 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
182 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
183 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700184
Brian Carlstrom57309db2014-07-30 15:13:25 -0700185 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700186 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700187 }
188
Brian Carlstrom6449c622014-02-10 23:48:36 -0800189 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800190 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800191 arg_vector.push_back(compiler_options[i].c_str());
192 }
193
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700194 std::string command_line(Join(arg_vector, ' '));
195 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800196 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700197}
198
Narayan Kamath52f84882014-05-02 10:10:39 +0100199bool ImageSpace::FindImageFilename(const char* image_location,
200 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700201 std::string* system_filename,
202 bool* has_system,
203 std::string* cache_filename,
204 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700205 bool* has_cache,
206 bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700207 *has_system = false;
208 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700209 // image_location = /system/framework/boot.art
210 // system_image_location = /system/framework/<image_isa>/boot.art
211 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
212 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700213 *system_filename = system_image_filename;
214 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700215 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100216
Alex Lighta59dd802014-07-02 16:28:08 -0700217 bool have_android_data = false;
218 *dalvik_cache_exists = false;
219 std::string dalvik_cache;
220 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700221 &have_android_data, dalvik_cache_exists, is_global_cache);
Narayan Kamath52f84882014-05-02 10:10:39 +0100222
Alex Lighta59dd802014-07-02 16:28:08 -0700223 if (have_android_data && *dalvik_cache_exists) {
224 // Always set output location even if it does not exist,
225 // so that the caller knows where to create the image.
226 //
227 // image_location = /system/framework/boot.art
228 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
229 std::string error_msg;
230 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
231 LOG(WARNING) << error_msg;
232 return *has_system;
233 }
234 *has_cache = OS::FileExists(cache_filename->c_str());
235 }
236 return *has_system || *has_cache;
237}
238
239static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
240 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
241 if (image_file.get() == nullptr) {
242 return false;
243 }
244 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
245 if (!success || !image_header->IsValid()) {
246 return false;
247 }
248 return true;
249}
250
Alex Light6e183f22014-07-18 14:57:04 -0700251// Relocate the image at image_location to dest_filename and relocate it by a random amount.
252static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700253 InstructionSet isa, std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700254 // We should clean up so we are more likely to have room for the image.
255 if (Runtime::Current()->IsZygote()) {
256 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000257 PruneDalvikCache(isa);
Alex Light25396132014-08-27 15:37:23 -0700258 }
259
Alex Lighta59dd802014-07-02 16:28:08 -0700260 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
261
262 std::string input_image_location_arg("--input-image-location=");
263 input_image_location_arg += image_location;
264
265 std::string output_image_filename_arg("--output-image-file=");
266 output_image_filename_arg += dest_filename;
267
268 std::string input_oat_location_arg("--input-oat-location=");
269 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
270
271 std::string output_oat_filename_arg("--output-oat-file=");
272 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
273
274 std::string instruction_set_arg("--instruction-set=");
275 instruction_set_arg += GetInstructionSetString(isa);
276
277 std::string base_offset_arg("--base-offset-delta=");
278 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
279 ART_BASE_ADDRESS_MAX_DELTA));
280
281 std::vector<std::string> argv;
282 argv.push_back(patchoat);
283
284 argv.push_back(input_image_location_arg);
285 argv.push_back(output_image_filename_arg);
286
287 argv.push_back(input_oat_location_arg);
288 argv.push_back(output_oat_filename_arg);
289
290 argv.push_back(instruction_set_arg);
291 argv.push_back(base_offset_arg);
292
293 std::string command_line(Join(argv, ' '));
294 LOG(INFO) << "RelocateImage: " << command_line;
295 return Exec(argv, error_msg);
296}
297
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700298static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700299 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
300 if (!ReadSpecificImageHeader(filename, hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700301 *error_msg = StringPrintf("Unable to read image header for %s", filename);
Alex Lighta59dd802014-07-02 16:28:08 -0700302 return nullptr;
303 }
304 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100305}
306
307ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
308 const InstructionSet image_isa) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700309 std::string error_msg;
310 ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
311 if (image_header == nullptr) {
312 LOG(FATAL) << error_msg;
313 }
314 return image_header;
315}
316
317ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
318 const InstructionSet image_isa,
319 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700320 std::string system_filename;
321 bool has_system = false;
322 std::string cache_filename;
323 bool has_cache = false;
324 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700325 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700326 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700327 &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700328 if (Runtime::Current()->ShouldRelocate()) {
329 if (has_system && has_cache) {
330 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
331 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
332 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700333 *error_msg = StringPrintf("Unable to read image header for %s at %s",
334 image_location, system_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700335 return nullptr;
336 }
337 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700338 *error_msg = StringPrintf("Unable to read image header for %s at %s",
339 image_location, cache_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700340 return nullptr;
341 }
342 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700343 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
344 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700345 return nullptr;
346 }
347 return cache_hdr.release();
348 } else if (!has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700349 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
350 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700351 return nullptr;
352 } else if (!has_system && has_cache) {
353 // This can probably just use the cache one.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700354 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700355 }
356 } else {
357 // We don't want to relocate, Just pick the appropriate one if we have it and return.
358 if (has_system && has_cache) {
359 // We want the cache if the checksum matches, otherwise the system.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700360 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
361 error_msg));
362 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
363 error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700364 if (system.get() == nullptr ||
365 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
366 return cache.release();
367 } else {
368 return system.release();
369 }
370 } else if (has_system) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700371 return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700372 } else if (has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700373 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700374 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100375 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100376 }
377
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700378 *error_msg = StringPrintf("Unable to find image file for %s", image_location);
Narayan Kamath52f84882014-05-02 10:10:39 +0100379 return nullptr;
380}
381
Alex Lighta59dd802014-07-02 16:28:08 -0700382static bool ChecksumsMatch(const char* image_a, const char* image_b) {
383 ImageHeader hdr_a;
384 ImageHeader hdr_b;
385 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
386 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
387}
388
Andreas Gampe3c13a792014-09-18 20:56:04 -0700389static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
390 // Anyone can write into a "local" cache.
391 if (!is_global_cache) {
392 return true;
393 }
394
395 // Only the zygote is allowed to create the global boot image.
396 if (Runtime::Current()->IsZygote()) {
397 return true;
398 }
399
400 *error_msg = "Only the zygote can create the global boot image.";
401 return false;
402}
403
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700404static constexpr uint64_t kLowSpaceValue = 50 * MB;
405static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
406
407// Read the free space of the cache partition and make a decision whether to keep the generated
408// image. This is to try to mitigate situations where the system might run out of space later.
409static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
410 // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
411 struct statvfs buf;
412
413 int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
414 if (res != 0) {
415 // Could not stat. Conservatively tell the system to delete the image.
416 *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
417 return false;
418 }
419
420 uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
421 // Zygote is privileged, but other things are not. Use bavail.
422 uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
423
424 // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
425 // environment. We do not want to fail quickening the boot image there, as it is beneficial
426 // for time-to-UI.
427 if (fs_overall_size > kTmpFsSentinelValue) {
428 if (fs_free_size < kLowSpaceValue) {
429 *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image"
430 " generation, need at least %" PRIu64 ".",
431 static_cast<double>(fs_free_size) / MB,
432 kLowSpaceValue / MB);
433 return false;
434 }
435 }
436 return true;
437}
438
Narayan Kamath52f84882014-05-02 10:10:39 +0100439ImageSpace* ImageSpace::Create(const char* image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700440 const InstructionSet image_isa,
441 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700442 std::string system_filename;
443 bool has_system = false;
444 std::string cache_filename;
445 bool has_cache = false;
446 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700447 bool is_global_cache = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700448 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
449 &has_system, &cache_filename, &dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700450 &has_cache, &is_global_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100451
Narayan Kamath28bc9872014-11-07 17:46:28 +0000452 if (Runtime::Current()->IsZygote()) {
453 MarkZygoteStart(image_isa);
454 }
455
Alex Lighta59dd802014-07-02 16:28:08 -0700456 ImageSpace* space;
457 bool relocate = Runtime::Current()->ShouldRelocate();
Alex Light64ad14d2014-08-19 14:23:13 -0700458 bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100459 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700460 const std::string* image_filename;
461 bool is_system = false;
462 bool relocated_version_used = false;
463 if (relocate) {
Alex Light64ad14d2014-08-19 14:23:13 -0700464 if (!dalvik_cache_exists) {
465 *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
466 "any dalvik_cache to find/place it in.",
467 image_location, system_filename.c_str());
468 return nullptr;
469 }
Alex Lighta59dd802014-07-02 16:28:08 -0700470 if (has_system) {
471 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
472 // We already have a relocated version
473 image_filename = &cache_filename;
474 relocated_version_used = true;
475 } else {
476 // We cannot have a relocated version, Relocate the system one and use it.
Andreas Gampe3c13a792014-09-18 20:56:04 -0700477
478 std::string reason;
479 bool success;
480
481 // Check whether we are allowed to relocate.
482 if (!can_compile) {
483 reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
484 success = false;
485 } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
486 // Whether we can write to the cache.
487 success = false;
488 } else {
489 // Try to relocate.
490 success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
491 }
492
493 if (success) {
Alex Lighta59dd802014-07-02 16:28:08 -0700494 relocated_version_used = true;
495 image_filename = &cache_filename;
496 } else {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700497 *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
Alex Light64ad14d2014-08-19 14:23:13 -0700498 image_location, system_filename.c_str(),
499 cache_filename.c_str(), reason.c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700500 // We failed to create files, remove any possibly garbage output.
501 // Since ImageCreationAllowed was true above, we are the zygote
502 // and therefore the only process expected to generate these for
503 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000504 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700505 return nullptr;
506 }
507 }
508 } else {
509 CHECK(has_cache);
510 // We can just use cache's since it should be fine. This might or might not be relocated.
511 image_filename = &cache_filename;
512 }
513 } else {
514 if (has_system && has_cache) {
515 // Check they have the same cksum. If they do use the cache. Otherwise system.
516 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
517 image_filename = &cache_filename;
518 relocated_version_used = true;
519 } else {
520 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700521 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700522 }
523 } else if (has_system) {
524 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700525 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700526 } else {
527 CHECK(has_cache);
528 image_filename = &cache_filename;
529 }
530 }
531 {
532 // Note that we must not use the file descriptor associated with
533 // ScopedFlock::GetFile to Init the image file. We want the file
534 // descriptor (and the associated exclusive lock) to be released when
535 // we leave Create.
536 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700537 image_lock.Init(image_filename->c_str(), error_msg);
Alex Lightb6cabc12014-08-21 09:45:00 -0700538 VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
539 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700540 // If we are in /system we can assume the image is good. We can also
541 // assume this if we are using a relocated image (i.e. image checksum
542 // matches) since this is only different by the offset. We need this to
543 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700544 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700545 !(is_system || relocated_version_used), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700546 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100547 if (space != nullptr) {
548 return space;
549 }
550
Alex Lighta59dd802014-07-02 16:28:08 -0700551 if (relocated_version_used) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700552 // Something is wrong with the relocated copy (even though checksums match). Cleanup.
553 // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
554 // TODO: Check the oat file validity earlier.
555 *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
556 "but image failed to load: %s",
557 image_location, cache_filename.c_str(), system_filename.c_str(),
558 error_msg->c_str());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000559 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700560 return nullptr;
561 } else if (is_system) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700562 // If the /system file exists, it should be up-to-date, don't try to generate it.
Alex Light64ad14d2014-08-19 14:23:13 -0700563 *error_msg = StringPrintf("Failed to load /system image '%s': %s",
564 image_filename->c_str(), error_msg->c_str());
Narayan Kamath52f84882014-05-02 10:10:39 +0100565 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800566 } else {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700567 // Otherwise, log a warning and fall through to GenerateImage.
Alex Light64ad14d2014-08-19 14:23:13 -0700568 LOG(WARNING) << *error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700569 }
570 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100571
Alex Light64ad14d2014-08-19 14:23:13 -0700572 if (!can_compile) {
573 *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
574 return nullptr;
575 } else if (!dalvik_cache_exists) {
576 *error_msg = StringPrintf("No place to put generated image.");
577 return nullptr;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700578 } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
579 return nullptr;
Alex Light25396132014-08-27 15:37:23 -0700580 } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
Alex Light64ad14d2014-08-19 14:23:13 -0700581 *error_msg = StringPrintf("Failed to generate image '%s': %s",
582 cache_filename.c_str(), error_msg->c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700583 // We failed to create files, remove any possibly garbage output.
584 // Since ImageCreationAllowed was true above, we are the zygote
585 // and therefore the only process expected to generate these for
586 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000587 PruneDalvikCache(image_isa);
Alex Light64ad14d2014-08-19 14:23:13 -0700588 return nullptr;
589 } else {
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700590 // Check whether there is enough space left over after we have generated the image.
591 if (!CheckSpace(cache_filename, error_msg)) {
592 // No. Delete the generated image and try to run out of the dex files.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000593 PruneDalvikCache(image_isa);
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700594 return nullptr;
595 }
596
Alex Lighta59dd802014-07-02 16:28:08 -0700597 // Note that we must not use the file descriptor associated with
598 // ScopedFlock::GetFile to Init the image file. We want the file
599 // descriptor (and the associated exclusive lock) to be released when
600 // we leave Create.
601 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700602 image_lock.Init(cache_filename.c_str(), error_msg);
603 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
604 if (space == nullptr) {
605 *error_msg = StringPrintf("Failed to load generated image '%s': %s",
606 cache_filename.c_str(), error_msg->c_str());
607 }
608 return space;
Alex Lighta59dd802014-07-02 16:28:08 -0700609 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700610}
611
Mathieu Chartier31e89252013-08-28 11:29:12 -0700612void ImageSpace::VerifyImageAllocations() {
Ian Rogers13735952014-10-08 12:43:28 -0700613 uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700614 while (current < End()) {
615 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800616 mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700617 CHECK(live_bitmap_->Test(obj));
618 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700619 if (kUseBakerOrBrooksReadBarrier) {
620 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800621 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700622 current += RoundUp(obj->SizeOf(), kObjectAlignment);
623 }
624}
625
Narayan Kamath52f84882014-05-02 10:10:39 +0100626ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
627 bool validate_oat_file, std::string* error_msg) {
628 CHECK(image_filename != nullptr);
629 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700630
631 uint64_t start_time = 0;
632 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
633 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100634 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700635 }
636
Ian Rogers700a4022014-05-19 16:49:03 -0700637 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Ian Rogers1d54e732013-05-02 21:10:01 -0700638 if (file.get() == NULL) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100639 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700640 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700641 }
642 ImageHeader image_header;
643 bool success = file->ReadFully(&image_header, sizeof(image_header));
644 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100645 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700646 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700647 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700648
649 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers700a4022014-05-19 16:49:03 -0700650 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700651 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700652 PROT_READ | PROT_WRITE,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700653 MAP_PRIVATE,
Ian Rogers1d54e732013-05-02 21:10:01 -0700654 file->Fd(),
655 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700656 false,
Narayan Kamath52f84882014-05-02 10:10:39 +0100657 image_filename,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700658 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700659 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700660 DCHECK(!error_msg->empty());
661 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700662 }
663 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
664 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
665
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700666 std::unique_ptr<MemMap> image_map(
667 MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
668 PROT_READ, MAP_PRIVATE,
669 file->Fd(), image_header.GetBitmapOffset(),
670 false,
671 image_filename,
672 error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700673 if (image_map.get() == nullptr) {
674 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
675 return nullptr;
676 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700677 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100678 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700679 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700680 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700681 accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
Ian Rogers13735952014-10-08 12:43:28 -0700682 reinterpret_cast<uint8_t*>(map->Begin()),
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700683 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700684 if (bitmap.get() == nullptr) {
685 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
686 return nullptr;
687 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700688
Ian Rogers700a4022014-05-19 16:49:03 -0700689 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Narayan Kamath52f84882014-05-02 10:10:39 +0100690 map.release(), bitmap.release()));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700691
692 // VerifyImageAllocations() will be called later in Runtime::Init()
693 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
694 // and ArtField::java_lang_reflect_ArtField_, which are used from
695 // Object::SizeOf() which VerifyImageAllocations() calls, are not
696 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700697
Narayan Kamath52f84882014-05-02 10:10:39 +0100698 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700699 if (space->oat_file_.get() == nullptr) {
700 DCHECK(!error_msg->empty());
701 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700702 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700703
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700704 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
705 DCHECK(!error_msg->empty());
706 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700707 }
708
Vladimir Marko7624d252014-05-02 14:40:15 +0100709 Runtime* runtime = Runtime::Current();
710 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
711
712 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
713 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
714 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
715 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700716 mirror::Object* imt_unimplemented_method =
717 image_header.GetImageRoot(ImageHeader::kImtUnimplementedMethod);
718 runtime->SetImtUnimplementedMethod(down_cast<mirror::ArtMethod*>(imt_unimplemented_method));
Vladimir Marko7624d252014-05-02 14:40:15 +0100719 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
720 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
721
722 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700723 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
724 Runtime::kSaveAll);
Vladimir Marko7624d252014-05-02 14:40:15 +0100725 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700726 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
727 Runtime::kRefsOnly);
Vladimir Marko7624d252014-05-02 14:40:15 +0100728 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700729 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
730 Runtime::kRefsAndArgs);
Vladimir Marko7624d252014-05-02 14:40:15 +0100731
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700732 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
733 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
734 << ") " << *space.get();
735 }
736 return space.release();
737}
738
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000739OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700740 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000741 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
742
Igor Murashkin46774762014-10-22 11:37:02 -0700743 CHECK(image_header.GetOatDataBegin() != nullptr);
744
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700745 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Igor Murashkin46774762014-10-22 11:37:02 -0700746 image_header.GetOatFileBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700747 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700748 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700749 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
750 oat_filename.c_str(), GetName(), error_msg->c_str());
751 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700752 }
753 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
754 uint32_t image_oat_checksum = image_header.GetOatChecksum();
755 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700756 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
757 " in image %s", oat_checksum, image_oat_checksum, GetName());
758 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700759 }
Alex Lighta59dd802014-07-02 16:28:08 -0700760 int32_t image_patch_delta = image_header.GetPatchDelta();
761 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
Igor Murashkin46774762014-10-22 11:37:02 -0700762 if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700763 // We should have already relocated by this point. Bail out.
764 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
765 "in image %s", oat_patch_delta, image_patch_delta, GetName());
766 return nullptr;
767 }
768
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700769 return oat_file;
770}
771
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700772bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700773 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700774 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700775 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
776 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700777 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
778 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
779 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700780 return false;
781 }
782 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700783 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
784 "dex file '%s' (0x%x != 0x%x)",
785 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
786 oat_dex_file->GetDexFileLocationChecksum(),
787 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700788 return false;
789 }
790 }
791 return true;
792}
793
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700794const OatFile* ImageSpace::GetOatFile() const {
795 return oat_file_.get();
796}
797
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700798OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700799 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700800 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700801}
802
Ian Rogers1d54e732013-05-02 21:10:01 -0700803void ImageSpace::Dump(std::ostream& os) const {
804 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700805 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700806 << ",end=" << reinterpret_cast<void*>(End())
807 << ",size=" << PrettySize(Size())
808 << ",name=\"" << GetName() << "\"]";
809}
810
811} // namespace space
812} // namespace gc
813} // namespace art