blob: e28e8d7771d5b764b47b8c3f83e7264ebdec8f26 [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>
Narayan Kamath5a2be3f2015-02-16 13:51:51 +000022#include <unistd.h>
Alex Light25396132014-08-27 15:37:23 -070023
Alex Lighta59dd802014-07-02 16:28:08 -070024#include <random>
25
Ian Rogersc7dd2952014-10-21 23:31:19 -070026#include "base/macros.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070027#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "base/unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010029#include "base/scoped_flock.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070031#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070034#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070036#include "space-inl.h"
37#include "utils.h"
38
39namespace art {
40namespace gc {
41namespace space {
42
Ian Rogersef7d42f2014-01-06 12:55:46 -080043Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070044
Narayan Kamath52f84882014-05-02 10:10:39 +010045ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
Mathieu Chartierc7853442015-03-27 14:35:38 -070046 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap,
47 uint8_t* end)
48 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), end, end,
Narayan Kamath52f84882014-05-02 10:10:39 +010049 kGcRetentionPolicyNeverCollect),
50 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070051 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070052 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070053}
54
Alex Lightcf4bf382014-07-24 11:29:14 -070055static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
56 CHECK_ALIGNED(min_delta, kPageSize);
57 CHECK_ALIGNED(max_delta, kPageSize);
58 CHECK_LT(min_delta, max_delta);
59
60 std::default_random_engine generator;
61 generator.seed(NanoTime() * getpid());
62 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
63 int32_t r = distribution(generator);
64 if (r % 2 == 0) {
65 r = RoundUp(r, kPageSize);
66 } else {
67 r = RoundDown(r, kPageSize);
68 }
69 CHECK_LE(min_delta, r);
70 CHECK_GE(max_delta, r);
71 CHECK_ALIGNED(r, kPageSize);
72 return r;
73}
74
Alex Light25396132014-08-27 15:37:23 -070075// We are relocating or generating the core image. We should get rid of everything. It is all
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080076// 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 -070077// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
78// Note this should only be used during first boot.
Narayan Kamath28bc9872014-11-07 17:46:28 +000079static void RealPruneDalvikCache(const std::string& cache_dir_path);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080080
Narayan Kamath28bc9872014-11-07 17:46:28 +000081static void PruneDalvikCache(InstructionSet isa) {
Alex Light25396132014-08-27 15:37:23 -070082 CHECK_NE(isa, kNone);
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080083 // Prune the base /data/dalvik-cache.
Narayan Kamath28bc9872014-11-07 17:46:28 +000084 RealPruneDalvikCache(GetDalvikCacheOrDie(".", false));
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080085 // Prune /data/dalvik-cache/<isa>.
Narayan Kamath28bc9872014-11-07 17:46:28 +000086 RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
Alex Light25396132014-08-27 15:37:23 -070087}
Andreas Gampe8db9dcd2014-11-09 18:14:30 -080088
Narayan Kamath28bc9872014-11-07 17:46:28 +000089static void RealPruneDalvikCache(const std::string& cache_dir_path) {
Alex Light25396132014-08-27 15:37:23 -070090 if (!OS::DirectoryExists(cache_dir_path.c_str())) {
91 return;
92 }
93 DIR* cache_dir = opendir(cache_dir_path.c_str());
94 if (cache_dir == nullptr) {
95 PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
96 return;
97 }
Alex Light25396132014-08-27 15:37:23 -070098
99 for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
100 const char* name = de->d_name;
101 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
102 continue;
103 }
Andreas Gampe8db9dcd2014-11-09 18:14:30 -0800104 // We only want to delete regular files and symbolic links.
105 if (de->d_type != DT_REG && de->d_type != DT_LNK) {
Alex Light25396132014-08-27 15:37:23 -0700106 if (de->d_type != DT_DIR) {
107 // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
108 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
109 }
110 continue;
111 }
Brian Carlstromdebdda02014-08-28 22:17:13 -0700112 std::string cache_file(cache_dir_path);
113 cache_file += '/';
114 cache_file += name;
115 if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) {
116 PLOG(ERROR) << "Unable to unlink " << cache_file;
Alex Light25396132014-08-27 15:37:23 -0700117 continue;
118 }
119 }
120 CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
121}
122
Narayan Kamath28bc9872014-11-07 17:46:28 +0000123// We write out an empty file to the zygote's ISA specific cache dir at the start of
124// every zygote boot and delete it when the boot completes. If we find a file already
125// present, it usually means the boot didn't complete. We wipe the entire dalvik
126// cache if that's the case.
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000127static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
Narayan Kamath28bc9872014-11-07 17:46:28 +0000128 const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
129 const std::string boot_marker = isa_subdir + "/.booting";
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000130 const char* file_name = boot_marker.c_str();
Narayan Kamath28bc9872014-11-07 17:46:28 +0000131
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000132 uint32_t num_failed_boots = 0;
133 std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
134 if (file.get() == nullptr) {
135 file.reset(OS::CreateEmptyFile(file_name));
136
137 if (file.get() == nullptr) {
138 PLOG(WARNING) << "Failed to create boot marker.";
139 return;
140 }
141 } else {
142 if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
143 PLOG(WARNING) << "Failed to read boot marker.";
144 file->Erase();
145 return;
146 }
147 }
148
149 if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
Narayan Kamath28bc9872014-11-07 17:46:28 +0000150 LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
151 RealPruneDalvikCache(isa_subdir);
152 }
153
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000154 ++num_failed_boots;
155 VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
156
157 if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
158 PLOG(WARNING) << "Failed to write boot marker.";
159 file->Erase();
160 return;
161 }
162
163 if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
164 PLOG(WARNING) << "Failed to write boot marker.";
165 file->Erase();
166 return;
167 }
168
169 if (file->FlushCloseOrErase() != 0) {
170 PLOG(WARNING) << "Failed to flush boot marker.";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000171 }
172}
173
Alex Light25396132014-08-27 15:37:23 -0700174static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
175 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700176 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
177 std::vector<std::string> boot_class_path;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700178 Split(boot_class_path_string, ':', &boot_class_path);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700179 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700180 *error_msg = "Failed to generate image because no boot class path specified";
181 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700182 }
Alex Light25396132014-08-27 15:37:23 -0700183 // We should clean up so we are more likely to have room for the image.
184 if (Runtime::Current()->IsZygote()) {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700185 LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000186 PruneDalvikCache(image_isa);
Alex Light25396132014-08-27 15:37:23 -0700187 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700188
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700189 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700190
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700191 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700192 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700193
194 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100195 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700196 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700197
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700198 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700199 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700200 }
201
202 std::string oat_file_option_string("--oat-file=");
Brian Carlstrom2f1e15c2014-10-27 16:27:06 -0700203 oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700204 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700205
Ian Rogers8afeb852014-04-02 14:55:49 -0700206 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700207 CHECK_EQ(image_isa, kRuntimeISA)
208 << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700209
Alex Lightcf4bf382014-07-24 11:29:14 -0700210 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
211 ART_BASE_ADDRESS_MAX_DELTA);
212 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
213 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
214 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700215
Brian Carlstrom57309db2014-07-30 15:13:25 -0700216 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700217 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700218 }
219
Brian Carlstrom6449c622014-02-10 23:48:36 -0800220 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800221 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800222 arg_vector.push_back(compiler_options[i].c_str());
223 }
224
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700225 std::string command_line(Join(arg_vector, ' '));
226 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800227 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700228}
229
Narayan Kamath52f84882014-05-02 10:10:39 +0100230bool ImageSpace::FindImageFilename(const char* image_location,
231 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700232 std::string* system_filename,
233 bool* has_system,
234 std::string* cache_filename,
235 bool* dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700236 bool* has_cache,
237 bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700238 *has_system = false;
239 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700240 // image_location = /system/framework/boot.art
241 // system_image_location = /system/framework/<image_isa>/boot.art
242 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
243 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700244 *system_filename = system_image_filename;
245 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700246 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100247
Alex Lighta59dd802014-07-02 16:28:08 -0700248 bool have_android_data = false;
249 *dalvik_cache_exists = false;
250 std::string dalvik_cache;
251 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700252 &have_android_data, dalvik_cache_exists, is_global_cache);
Narayan Kamath52f84882014-05-02 10:10:39 +0100253
Alex Lighta59dd802014-07-02 16:28:08 -0700254 if (have_android_data && *dalvik_cache_exists) {
255 // Always set output location even if it does not exist,
256 // so that the caller knows where to create the image.
257 //
258 // image_location = /system/framework/boot.art
259 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
260 std::string error_msg;
261 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
262 LOG(WARNING) << error_msg;
263 return *has_system;
264 }
265 *has_cache = OS::FileExists(cache_filename->c_str());
266 }
267 return *has_system || *has_cache;
268}
269
270static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
271 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
272 if (image_file.get() == nullptr) {
273 return false;
274 }
275 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
276 if (!success || !image_header->IsValid()) {
277 return false;
278 }
279 return true;
280}
281
Alex Light6e183f22014-07-18 14:57:04 -0700282// Relocate the image at image_location to dest_filename and relocate it by a random amount.
283static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700284 InstructionSet isa, std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700285 // We should clean up so we are more likely to have room for the image.
286 if (Runtime::Current()->IsZygote()) {
287 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
Narayan Kamath28bc9872014-11-07 17:46:28 +0000288 PruneDalvikCache(isa);
Alex Light25396132014-08-27 15:37:23 -0700289 }
290
Alex Lighta59dd802014-07-02 16:28:08 -0700291 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
292
293 std::string input_image_location_arg("--input-image-location=");
294 input_image_location_arg += image_location;
295
296 std::string output_image_filename_arg("--output-image-file=");
297 output_image_filename_arg += dest_filename;
298
299 std::string input_oat_location_arg("--input-oat-location=");
300 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
301
302 std::string output_oat_filename_arg("--output-oat-file=");
303 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
304
305 std::string instruction_set_arg("--instruction-set=");
306 instruction_set_arg += GetInstructionSetString(isa);
307
308 std::string base_offset_arg("--base-offset-delta=");
309 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
310 ART_BASE_ADDRESS_MAX_DELTA));
311
312 std::vector<std::string> argv;
313 argv.push_back(patchoat);
314
315 argv.push_back(input_image_location_arg);
316 argv.push_back(output_image_filename_arg);
317
318 argv.push_back(input_oat_location_arg);
319 argv.push_back(output_oat_filename_arg);
320
321 argv.push_back(instruction_set_arg);
322 argv.push_back(base_offset_arg);
323
324 std::string command_line(Join(argv, ' '));
325 LOG(INFO) << "RelocateImage: " << command_line;
326 return Exec(argv, error_msg);
327}
328
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700329static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700330 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
331 if (!ReadSpecificImageHeader(filename, hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700332 *error_msg = StringPrintf("Unable to read image header for %s", filename);
Alex Lighta59dd802014-07-02 16:28:08 -0700333 return nullptr;
334 }
335 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100336}
337
338ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
339 const InstructionSet image_isa) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700340 std::string error_msg;
341 ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
342 if (image_header == nullptr) {
343 LOG(FATAL) << error_msg;
344 }
345 return image_header;
346}
347
348ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
349 const InstructionSet image_isa,
350 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700351 std::string system_filename;
352 bool has_system = false;
353 std::string cache_filename;
354 bool has_cache = false;
355 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700356 bool is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700357 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700358 &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700359 if (Runtime::Current()->ShouldRelocate()) {
360 if (has_system && has_cache) {
361 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
362 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
363 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700364 *error_msg = StringPrintf("Unable to read image header for %s at %s",
365 image_location, system_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700366 return nullptr;
367 }
368 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700369 *error_msg = StringPrintf("Unable to read image header for %s at %s",
370 image_location, cache_filename.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700371 return nullptr;
372 }
373 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700374 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
375 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700376 return nullptr;
377 }
378 return cache_hdr.release();
379 } else if (!has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700380 *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
381 image_location);
Alex Lighta59dd802014-07-02 16:28:08 -0700382 return nullptr;
383 } else if (!has_system && has_cache) {
384 // This can probably just use the cache one.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700385 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700386 }
387 } else {
388 // We don't want to relocate, Just pick the appropriate one if we have it and return.
389 if (has_system && has_cache) {
390 // We want the cache if the checksum matches, otherwise the system.
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700391 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
392 error_msg));
393 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
394 error_msg));
Alex Lighta59dd802014-07-02 16:28:08 -0700395 if (system.get() == nullptr ||
396 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
397 return cache.release();
398 } else {
399 return system.release();
400 }
401 } else if (has_system) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700402 return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700403 } else if (has_cache) {
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700404 return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700405 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100406 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100407 }
408
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700409 *error_msg = StringPrintf("Unable to find image file for %s", image_location);
Narayan Kamath52f84882014-05-02 10:10:39 +0100410 return nullptr;
411}
412
Alex Lighta59dd802014-07-02 16:28:08 -0700413static bool ChecksumsMatch(const char* image_a, const char* image_b) {
414 ImageHeader hdr_a;
415 ImageHeader hdr_b;
416 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
417 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
418}
419
Andreas Gampe3c13a792014-09-18 20:56:04 -0700420static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
421 // Anyone can write into a "local" cache.
422 if (!is_global_cache) {
423 return true;
424 }
425
426 // Only the zygote is allowed to create the global boot image.
427 if (Runtime::Current()->IsZygote()) {
428 return true;
429 }
430
431 *error_msg = "Only the zygote can create the global boot image.";
432 return false;
433}
434
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700435static constexpr uint64_t kLowSpaceValue = 50 * MB;
436static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
437
438// Read the free space of the cache partition and make a decision whether to keep the generated
439// image. This is to try to mitigate situations where the system might run out of space later.
440static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
441 // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
442 struct statvfs buf;
443
444 int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
445 if (res != 0) {
446 // Could not stat. Conservatively tell the system to delete the image.
447 *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
448 return false;
449 }
450
451 uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
452 // Zygote is privileged, but other things are not. Use bavail.
453 uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
454
455 // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
456 // environment. We do not want to fail quickening the boot image there, as it is beneficial
457 // for time-to-UI.
458 if (fs_overall_size > kTmpFsSentinelValue) {
459 if (fs_free_size < kLowSpaceValue) {
460 *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image"
461 " generation, need at least %" PRIu64 ".",
462 static_cast<double>(fs_free_size) / MB,
463 kLowSpaceValue / MB);
464 return false;
465 }
466 }
467 return true;
468}
469
Narayan Kamath52f84882014-05-02 10:10:39 +0100470ImageSpace* ImageSpace::Create(const char* image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700471 const InstructionSet image_isa,
472 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700473 std::string system_filename;
474 bool has_system = false;
475 std::string cache_filename;
476 bool has_cache = false;
477 bool dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700478 bool is_global_cache = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700479 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
480 &has_system, &cache_filename, &dalvik_cache_exists,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700481 &has_cache, &is_global_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100482
Narayan Kamath28bc9872014-11-07 17:46:28 +0000483 if (Runtime::Current()->IsZygote()) {
Narayan Kamath5a2be3f2015-02-16 13:51:51 +0000484 MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000485 }
486
Alex Lighta59dd802014-07-02 16:28:08 -0700487 ImageSpace* space;
488 bool relocate = Runtime::Current()->ShouldRelocate();
Alex Light64ad14d2014-08-19 14:23:13 -0700489 bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100490 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700491 const std::string* image_filename;
492 bool is_system = false;
493 bool relocated_version_used = false;
494 if (relocate) {
Alex Light64ad14d2014-08-19 14:23:13 -0700495 if (!dalvik_cache_exists) {
496 *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
497 "any dalvik_cache to find/place it in.",
498 image_location, system_filename.c_str());
499 return nullptr;
500 }
Alex Lighta59dd802014-07-02 16:28:08 -0700501 if (has_system) {
502 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
503 // We already have a relocated version
504 image_filename = &cache_filename;
505 relocated_version_used = true;
506 } else {
507 // We cannot have a relocated version, Relocate the system one and use it.
Andreas Gampe3c13a792014-09-18 20:56:04 -0700508
509 std::string reason;
510 bool success;
511
512 // Check whether we are allowed to relocate.
513 if (!can_compile) {
514 reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
515 success = false;
516 } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
517 // Whether we can write to the cache.
518 success = false;
519 } else {
520 // Try to relocate.
521 success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
522 }
523
524 if (success) {
Alex Lighta59dd802014-07-02 16:28:08 -0700525 relocated_version_used = true;
526 image_filename = &cache_filename;
527 } else {
Andreas Gampe3c13a792014-09-18 20:56:04 -0700528 *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
Alex Light64ad14d2014-08-19 14:23:13 -0700529 image_location, system_filename.c_str(),
530 cache_filename.c_str(), reason.c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700531 // We failed to create files, remove any possibly garbage output.
532 // Since ImageCreationAllowed was true above, we are the zygote
533 // and therefore the only process expected to generate these for
534 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000535 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700536 return nullptr;
537 }
538 }
539 } else {
540 CHECK(has_cache);
541 // We can just use cache's since it should be fine. This might or might not be relocated.
542 image_filename = &cache_filename;
543 }
544 } else {
545 if (has_system && has_cache) {
546 // Check they have the same cksum. If they do use the cache. Otherwise system.
547 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
548 image_filename = &cache_filename;
549 relocated_version_used = true;
550 } else {
551 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700552 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700553 }
554 } else if (has_system) {
555 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700556 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700557 } else {
558 CHECK(has_cache);
559 image_filename = &cache_filename;
560 }
561 }
562 {
563 // Note that we must not use the file descriptor associated with
564 // ScopedFlock::GetFile to Init the image file. We want the file
565 // descriptor (and the associated exclusive lock) to be released when
566 // we leave Create.
567 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700568 image_lock.Init(image_filename->c_str(), error_msg);
Alex Lightb6cabc12014-08-21 09:45:00 -0700569 VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
570 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700571 // If we are in /system we can assume the image is good. We can also
572 // assume this if we are using a relocated image (i.e. image checksum
573 // matches) since this is only different by the offset. We need this to
574 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700575 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700576 !(is_system || relocated_version_used), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700577 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100578 if (space != nullptr) {
579 return space;
580 }
581
Alex Lighta59dd802014-07-02 16:28:08 -0700582 if (relocated_version_used) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700583 // Something is wrong with the relocated copy (even though checksums match). Cleanup.
584 // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
585 // TODO: Check the oat file validity earlier.
586 *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
587 "but image failed to load: %s",
588 image_location, cache_filename.c_str(), system_filename.c_str(),
589 error_msg->c_str());
Narayan Kamath28bc9872014-11-07 17:46:28 +0000590 PruneDalvikCache(image_isa);
Alex Lighta59dd802014-07-02 16:28:08 -0700591 return nullptr;
592 } else if (is_system) {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700593 // If the /system file exists, it should be up-to-date, don't try to generate it.
Alex Light64ad14d2014-08-19 14:23:13 -0700594 *error_msg = StringPrintf("Failed to load /system image '%s': %s",
595 image_filename->c_str(), error_msg->c_str());
Narayan Kamath52f84882014-05-02 10:10:39 +0100596 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800597 } else {
Brian Carlstrome9105f72014-10-28 15:53:43 -0700598 // Otherwise, log a warning and fall through to GenerateImage.
Alex Light64ad14d2014-08-19 14:23:13 -0700599 LOG(WARNING) << *error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700600 }
601 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100602
Alex Light64ad14d2014-08-19 14:23:13 -0700603 if (!can_compile) {
604 *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
605 return nullptr;
606 } else if (!dalvik_cache_exists) {
607 *error_msg = StringPrintf("No place to put generated image.");
608 return nullptr;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700609 } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
610 return nullptr;
Alex Light25396132014-08-27 15:37:23 -0700611 } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
Alex Light64ad14d2014-08-19 14:23:13 -0700612 *error_msg = StringPrintf("Failed to generate image '%s': %s",
613 cache_filename.c_str(), error_msg->c_str());
Brian Carlstrome9105f72014-10-28 15:53:43 -0700614 // We failed to create files, remove any possibly garbage output.
615 // Since ImageCreationAllowed was true above, we are the zygote
616 // and therefore the only process expected to generate these for
617 // the device.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000618 PruneDalvikCache(image_isa);
Alex Light64ad14d2014-08-19 14:23:13 -0700619 return nullptr;
620 } else {
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700621 // Check whether there is enough space left over after we have generated the image.
622 if (!CheckSpace(cache_filename, error_msg)) {
623 // No. Delete the generated image and try to run out of the dex files.
Narayan Kamath28bc9872014-11-07 17:46:28 +0000624 PruneDalvikCache(image_isa);
Andreas Gampe70be1fb2014-10-31 16:45:19 -0700625 return nullptr;
626 }
627
Alex Lighta59dd802014-07-02 16:28:08 -0700628 // Note that we must not use the file descriptor associated with
629 // ScopedFlock::GetFile to Init the image file. We want the file
630 // descriptor (and the associated exclusive lock) to be released when
631 // we leave Create.
632 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700633 image_lock.Init(cache_filename.c_str(), error_msg);
634 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
635 if (space == nullptr) {
636 *error_msg = StringPrintf("Failed to load generated image '%s': %s",
637 cache_filename.c_str(), error_msg->c_str());
638 }
639 return space;
Alex Lighta59dd802014-07-02 16:28:08 -0700640 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700641}
642
Mathieu Chartier31e89252013-08-28 11:29:12 -0700643void ImageSpace::VerifyImageAllocations() {
Ian Rogers13735952014-10-08 12:43:28 -0700644 uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700645 while (current < End()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700646 CHECK_ALIGNED(current, kObjectAlignment);
647 auto* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700648 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Mathieu Chartierc7853442015-03-27 14:35:38 -0700649 CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj);
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700650 if (kUseBakerOrBrooksReadBarrier) {
651 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800652 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700653 current += RoundUp(obj->SizeOf(), kObjectAlignment);
654 }
655}
656
Narayan Kamath52f84882014-05-02 10:10:39 +0100657ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
658 bool validate_oat_file, std::string* error_msg) {
659 CHECK(image_filename != nullptr);
660 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700661
662 uint64_t start_time = 0;
663 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
664 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100665 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700666 }
667
Ian Rogers700a4022014-05-19 16:49:03 -0700668 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Ian Rogers1d54e732013-05-02 21:10:01 -0700669 if (file.get() == NULL) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100670 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700671 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700672 }
673 ImageHeader image_header;
674 bool success = file->ReadFully(&image_header, sizeof(image_header));
675 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100676 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700677 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700678 }
Andreas Gampe6c8b49f2015-02-19 11:42:36 -0800679 // Check that the file is large enough.
680 uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
681 if (image_header.GetImageSize() > image_file_size) {
682 *error_msg = StringPrintf("Image file too small for image heap: %" PRIu64 " vs. %zu.",
683 image_file_size, image_header.GetImageSize());
684 return nullptr;
685 }
Mathieu Chartierc7853442015-03-27 14:35:38 -0700686 auto end_of_bitmap = image_header.GetImageBitmapOffset() + image_header.GetImageBitmapSize();
687 if (end_of_bitmap != image_file_size) {
688 *error_msg = StringPrintf(
689 "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size,
690 end_of_bitmap);
Andreas Gampe6c8b49f2015-02-19 11:42:36 -0800691 return nullptr;
692 }
693
Mathieu Chartier31e89252013-08-28 11:29:12 -0700694 // Note: The image header is part of the image due to mmap page alignment required of offset.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700695 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(
696 image_header.GetImageBegin(), image_header.GetImageSize() + image_header.GetArtFieldsSize(),
697 PROT_READ | PROT_WRITE, MAP_PRIVATE, file->Fd(), 0, false, image_filename, error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700698 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700699 DCHECK(!error_msg->empty());
700 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700701 }
702 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
703 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
704
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700705 std::unique_ptr<MemMap> image_map(
706 MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
707 PROT_READ, MAP_PRIVATE,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700708 file->Fd(), image_header.GetImageBitmapOffset(),
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700709 false,
710 image_filename,
711 error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700712 if (image_map.get() == nullptr) {
713 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
714 return nullptr;
715 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700716 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100717 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700718 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700719 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700720 accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
Ian Rogers13735952014-10-08 12:43:28 -0700721 reinterpret_cast<uint8_t*>(map->Begin()),
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700722 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700723 if (bitmap.get() == nullptr) {
724 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
725 return nullptr;
726 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700727
Mathieu Chartierc7853442015-03-27 14:35:38 -0700728 uint8_t* const image_end = map->Begin() + image_header.GetImageSize();
Ian Rogers700a4022014-05-19 16:49:03 -0700729 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700730 map.release(), bitmap.release(), image_end));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700731
732 // VerifyImageAllocations() will be called later in Runtime::Init()
733 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
734 // and ArtField::java_lang_reflect_ArtField_, which are used from
735 // Object::SizeOf() which VerifyImageAllocations() calls, are not
736 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700737
Narayan Kamath52f84882014-05-02 10:10:39 +0100738 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700739 if (space->oat_file_.get() == nullptr) {
740 DCHECK(!error_msg->empty());
741 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700742 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700743
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700744 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
745 DCHECK(!error_msg->empty());
746 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700747 }
748
Vladimir Marko7624d252014-05-02 14:40:15 +0100749 Runtime* runtime = Runtime::Current();
750 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
751
752 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
753 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
754 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
755 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700756 mirror::Object* imt_unimplemented_method =
757 image_header.GetImageRoot(ImageHeader::kImtUnimplementedMethod);
758 runtime->SetImtUnimplementedMethod(down_cast<mirror::ArtMethod*>(imt_unimplemented_method));
Vladimir Marko7624d252014-05-02 14:40:15 +0100759 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
760 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
761
762 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700763 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
764 Runtime::kSaveAll);
Vladimir Marko7624d252014-05-02 14:40:15 +0100765 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700766 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
767 Runtime::kRefsOnly);
Vladimir Marko7624d252014-05-02 14:40:15 +0100768 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
Brian Carlstrom31d8f522014-09-29 11:22:54 -0700769 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
770 Runtime::kRefsAndArgs);
Vladimir Marko7624d252014-05-02 14:40:15 +0100771
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700772 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
773 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
774 << ") " << *space.get();
775 }
776 return space.release();
777}
778
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000779OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700780 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000781 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
782
Igor Murashkin46774762014-10-22 11:37:02 -0700783 CHECK(image_header.GetOatDataBegin() != nullptr);
784
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700785 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Igor Murashkin46774762014-10-22 11:37:02 -0700786 image_header.GetOatFileBegin(),
Richard Uhlere5fed032015-03-18 08:21:11 -0700787 !Runtime::Current()->IsAotCompiler(),
788 nullptr, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700789 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700790 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
791 oat_filename.c_str(), GetName(), error_msg->c_str());
792 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700793 }
794 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
795 uint32_t image_oat_checksum = image_header.GetOatChecksum();
796 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700797 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
798 " in image %s", oat_checksum, image_oat_checksum, GetName());
799 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700800 }
Alex Lighta59dd802014-07-02 16:28:08 -0700801 int32_t image_patch_delta = image_header.GetPatchDelta();
802 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
Igor Murashkin46774762014-10-22 11:37:02 -0700803 if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
Alex Lighta59dd802014-07-02 16:28:08 -0700804 // We should have already relocated by this point. Bail out.
805 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
806 "in image %s", oat_patch_delta, image_patch_delta, GetName());
807 return nullptr;
808 }
809
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700810 return oat_file;
811}
812
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700813bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700814 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700815 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700816 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
817 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700818 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
819 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
820 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700821 return false;
822 }
823 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700824 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
825 "dex file '%s' (0x%x != 0x%x)",
826 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
827 oat_dex_file->GetDexFileLocationChecksum(),
828 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700829 return false;
830 }
831 }
832 return true;
833}
834
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700835const OatFile* ImageSpace::GetOatFile() const {
836 return oat_file_.get();
837}
838
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700839OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700840 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700841 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700842}
843
Ian Rogers1d54e732013-05-02 21:10:01 -0700844void ImageSpace::Dump(std::ostream& os) const {
845 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700846 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700847 << ",end=" << reinterpret_cast<void*>(End())
848 << ",size=" << PrettySize(Size())
849 << ",name=\"" << GetName() << "\"]";
850}
851
852} // namespace space
853} // namespace gc
854} // namespace art