blob: bf897b23d27071ac18084915f06850879392f190 [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>
20#include <sys/types.h>
21
Alex Lighta59dd802014-07-02 16:28:08 -070022#include <random>
23
Brian Carlstrom56d947f2013-07-15 13:14:23 -070024#include "base/stl_util.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070025#include "base/unix_file/fd_file.h"
Narayan Kamathd1c606f2014-06-09 16:50:19 +010026#include "base/scoped_flock.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/space_bitmap-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070029#include "mirror/class-inl.h"
30#include "mirror/object-inl.h"
Brian Carlstrom56d947f2013-07-15 13:14:23 -070031#include "oat_file.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070032#include "os.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "space-inl.h"
34#include "utils.h"
35
36namespace art {
37namespace gc {
38namespace space {
39
Ian Rogersef7d42f2014-01-06 12:55:46 -080040Atomic<uint32_t> ImageSpace::bitmap_index_(0);
Ian Rogers1d54e732013-05-02 21:10:01 -070041
Narayan Kamath52f84882014-05-02 10:10:39 +010042ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
43 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap)
44 : MemMapSpace(image_filename, mem_map, mem_map->Begin(), mem_map->End(), mem_map->End(),
45 kGcRetentionPolicyNeverCollect),
46 image_location_(image_location) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070047 DCHECK(live_bitmap != nullptr);
Mathieu Chartier31e89252013-08-28 11:29:12 -070048 live_bitmap_.reset(live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -070049}
50
Alex Lightcf4bf382014-07-24 11:29:14 -070051static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
52 CHECK_ALIGNED(min_delta, kPageSize);
53 CHECK_ALIGNED(max_delta, kPageSize);
54 CHECK_LT(min_delta, max_delta);
55
56 std::default_random_engine generator;
57 generator.seed(NanoTime() * getpid());
58 std::uniform_int_distribution<int32_t> distribution(min_delta, max_delta);
59 int32_t r = distribution(generator);
60 if (r % 2 == 0) {
61 r = RoundUp(r, kPageSize);
62 } else {
63 r = RoundDown(r, kPageSize);
64 }
65 CHECK_LE(min_delta, r);
66 CHECK_GE(max_delta, r);
67 CHECK_ALIGNED(r, kPageSize);
68 return r;
69}
70
Alex Light25396132014-08-27 15:37:23 -070071// We are relocating or generating the core image. We should get rid of everything. It is all
72// out-of-date. We also don't really care if this fails since it is just a convienence.
73// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
74// Note this should only be used during first boot.
75static void RealPruneDexCache(const std::string& cache_dir_path);
76static void PruneDexCache(InstructionSet isa) {
77 CHECK_NE(isa, kNone);
78 // Prune the base /data/dalvik-cache
79 RealPruneDexCache(GetDalvikCacheOrDie(".", false));
80 // prune /data/dalvik-cache/<isa>
81 RealPruneDexCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
82}
83static void RealPruneDexCache(const std::string& cache_dir_path) {
84 if (!OS::DirectoryExists(cache_dir_path.c_str())) {
85 return;
86 }
87 DIR* cache_dir = opendir(cache_dir_path.c_str());
88 if (cache_dir == nullptr) {
89 PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
90 return;
91 }
92 int dir_fd = dirfd(cache_dir);
93 CHECK_GE(dir_fd, 0);
94
95 for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
96 const char* name = de->d_name;
97 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
98 continue;
99 }
100 // We only want to delete regular files.
101 if (de->d_type != DT_REG) {
102 if (de->d_type != DT_DIR) {
103 // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
104 LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
105 }
106 continue;
107 }
108 if (TEMP_FAILURE_RETRY(unlinkat(dir_fd, name, 0)) != 0) {
109 PLOG(ERROR) << "Unable to unlink " << cache_dir_path << "/" << name;
110 continue;
111 }
112 }
113 CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
114}
115
116static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
117 std::string* error_msg) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700118 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
119 std::vector<std::string> boot_class_path;
120 Split(boot_class_path_string, ':', boot_class_path);
121 if (boot_class_path.empty()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700122 *error_msg = "Failed to generate image because no boot class path specified";
123 return false;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700124 }
Alex Light25396132014-08-27 15:37:23 -0700125 // We should clean up so we are more likely to have room for the image.
126 if (Runtime::Current()->IsZygote()) {
127 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
128 PruneDexCache(image_isa);
129 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700130
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700131 std::vector<std::string> arg_vector;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700132
Tsu Chiang Chuang12e6d742014-05-22 10:22:25 -0700133 std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
Mathieu Chartier08d7d442013-07-31 18:08:51 -0700134 arg_vector.push_back(dex2oat);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700135
136 std::string image_option_string("--image=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100137 image_option_string += image_filename;
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700138 arg_vector.push_back(image_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700139
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700140 for (size_t i = 0; i < boot_class_path.size(); i++) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700141 arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700142 }
143
144 std::string oat_file_option_string("--oat-file=");
Narayan Kamath52f84882014-05-02 10:10:39 +0100145 oat_file_option_string += image_filename;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700146 oat_file_option_string.erase(oat_file_option_string.size() - 3);
147 oat_file_option_string += "oat";
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700148 arg_vector.push_back(oat_file_option_string);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700149
Ian Rogers8afeb852014-04-02 14:55:49 -0700150 Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
Alex Light1291e9b2014-08-28 16:23:48 -0700151 CHECK_EQ(image_isa, kRuntimeISA) << "We should always be generating an image for the current isa.";
Ian Rogers8afeb852014-04-02 14:55:49 -0700152
Alex Lightcf4bf382014-07-24 11:29:14 -0700153 int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
154 ART_BASE_ADDRESS_MAX_DELTA);
155 LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
156 << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
157 arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700158
Brian Carlstrom57309db2014-07-30 15:13:25 -0700159 if (!kIsTargetBuild) {
Mathieu Chartier8bbc8c02013-07-31 16:27:01 -0700160 arg_vector.push_back("--host");
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700161 }
162
Brian Carlstrom6449c622014-02-10 23:48:36 -0800163 const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800164 for (size_t i = 0; i < compiler_options.size(); ++i) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800165 arg_vector.push_back(compiler_options[i].c_str());
166 }
167
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700168 std::string command_line(Join(arg_vector, ' '));
169 LOG(INFO) << "GenerateImage: " << command_line;
Brian Carlstrom6449c622014-02-10 23:48:36 -0800170 return Exec(arg_vector, error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700171}
172
Narayan Kamath52f84882014-05-02 10:10:39 +0100173bool ImageSpace::FindImageFilename(const char* image_location,
174 const InstructionSet image_isa,
Alex Lighta59dd802014-07-02 16:28:08 -0700175 std::string* system_filename,
176 bool* has_system,
177 std::string* cache_filename,
178 bool* dalvik_cache_exists,
179 bool* has_cache) {
180 *has_system = false;
181 *has_cache = false;
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700182 // image_location = /system/framework/boot.art
183 // system_image_location = /system/framework/<image_isa>/boot.art
184 std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
185 if (OS::FileExists(system_image_filename.c_str())) {
Alex Lighta59dd802014-07-02 16:28:08 -0700186 *system_filename = system_image_filename;
187 *has_system = true;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700188 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100189
Alex Lighta59dd802014-07-02 16:28:08 -0700190 bool have_android_data = false;
191 *dalvik_cache_exists = false;
192 std::string dalvik_cache;
193 GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
194 &have_android_data, dalvik_cache_exists);
Narayan Kamath52f84882014-05-02 10:10:39 +0100195
Alex Lighta59dd802014-07-02 16:28:08 -0700196 if (have_android_data && *dalvik_cache_exists) {
197 // Always set output location even if it does not exist,
198 // so that the caller knows where to create the image.
199 //
200 // image_location = /system/framework/boot.art
201 // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
202 std::string error_msg;
203 if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
204 LOG(WARNING) << error_msg;
205 return *has_system;
206 }
207 *has_cache = OS::FileExists(cache_filename->c_str());
208 }
209 return *has_system || *has_cache;
210}
211
212static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
213 std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
214 if (image_file.get() == nullptr) {
215 return false;
216 }
217 const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
218 if (!success || !image_header->IsValid()) {
219 return false;
220 }
221 return true;
222}
223
Alex Light6e183f22014-07-18 14:57:04 -0700224// Relocate the image at image_location to dest_filename and relocate it by a random amount.
225static bool RelocateImage(const char* image_location, const char* dest_filename,
Alex Lighta59dd802014-07-02 16:28:08 -0700226 InstructionSet isa, std::string* error_msg) {
Alex Light25396132014-08-27 15:37:23 -0700227 // We should clean up so we are more likely to have room for the image.
228 if (Runtime::Current()->IsZygote()) {
229 LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
230 PruneDexCache(isa);
231 }
232
Alex Lighta59dd802014-07-02 16:28:08 -0700233 std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
234
235 std::string input_image_location_arg("--input-image-location=");
236 input_image_location_arg += image_location;
237
238 std::string output_image_filename_arg("--output-image-file=");
239 output_image_filename_arg += dest_filename;
240
241 std::string input_oat_location_arg("--input-oat-location=");
242 input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
243
244 std::string output_oat_filename_arg("--output-oat-file=");
245 output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
246
247 std::string instruction_set_arg("--instruction-set=");
248 instruction_set_arg += GetInstructionSetString(isa);
249
250 std::string base_offset_arg("--base-offset-delta=");
251 StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
252 ART_BASE_ADDRESS_MAX_DELTA));
253
254 std::vector<std::string> argv;
255 argv.push_back(patchoat);
256
257 argv.push_back(input_image_location_arg);
258 argv.push_back(output_image_filename_arg);
259
260 argv.push_back(input_oat_location_arg);
261 argv.push_back(output_oat_filename_arg);
262
263 argv.push_back(instruction_set_arg);
264 argv.push_back(base_offset_arg);
265
266 std::string command_line(Join(argv, ' '));
267 LOG(INFO) << "RelocateImage: " << command_line;
268 return Exec(argv, error_msg);
269}
270
271static ImageHeader* ReadSpecificImageHeaderOrDie(const char* filename) {
272 std::unique_ptr<ImageHeader> hdr(new ImageHeader);
273 if (!ReadSpecificImageHeader(filename, hdr.get())) {
274 LOG(FATAL) << "Unable to read image header for " << filename;
275 return nullptr;
276 }
277 return hdr.release();
Narayan Kamath52f84882014-05-02 10:10:39 +0100278}
279
280ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
281 const InstructionSet image_isa) {
Alex Lighta59dd802014-07-02 16:28:08 -0700282 std::string system_filename;
283 bool has_system = false;
284 std::string cache_filename;
285 bool has_cache = false;
286 bool dalvik_cache_exists = false;
287 if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
288 &cache_filename, &dalvik_cache_exists, &has_cache)) {
289 if (Runtime::Current()->ShouldRelocate()) {
290 if (has_system && has_cache) {
291 std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
292 std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
293 if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
294 LOG(FATAL) << "Unable to read image header for " << image_location << " at "
295 << system_filename;
296 return nullptr;
297 }
298 if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
299 LOG(FATAL) << "Unable to read image header for " << image_location << " at "
300 << cache_filename;
301 return nullptr;
302 }
303 if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
304 LOG(FATAL) << "Unable to find a relocated version of image file " << image_location;
305 return nullptr;
306 }
307 return cache_hdr.release();
308 } else if (!has_cache) {
309 LOG(FATAL) << "Unable to find a relocated version of image file " << image_location;
310 return nullptr;
311 } else if (!has_system && has_cache) {
312 // This can probably just use the cache one.
313 return ReadSpecificImageHeaderOrDie(cache_filename.c_str());
314 }
315 } else {
316 // We don't want to relocate, Just pick the appropriate one if we have it and return.
317 if (has_system && has_cache) {
318 // We want the cache if the checksum matches, otherwise the system.
319 std::unique_ptr<ImageHeader> system(ReadSpecificImageHeaderOrDie(system_filename.c_str()));
320 std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeaderOrDie(cache_filename.c_str()));
321 if (system.get() == nullptr ||
322 (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
323 return cache.release();
324 } else {
325 return system.release();
326 }
327 } else if (has_system) {
328 return ReadSpecificImageHeaderOrDie(system_filename.c_str());
329 } else if (has_cache) {
330 return ReadSpecificImageHeaderOrDie(cache_filename.c_str());
331 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100332 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100333 }
334
335 LOG(FATAL) << "Unable to find image file for: " << image_location;
336 return nullptr;
337}
338
Alex Lighta59dd802014-07-02 16:28:08 -0700339static bool ChecksumsMatch(const char* image_a, const char* image_b) {
340 ImageHeader hdr_a;
341 ImageHeader hdr_b;
342 return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
343 && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
344}
345
Narayan Kamath52f84882014-05-02 10:10:39 +0100346ImageSpace* ImageSpace::Create(const char* image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700347 const InstructionSet image_isa,
348 std::string* error_msg) {
Alex Lighta59dd802014-07-02 16:28:08 -0700349 std::string system_filename;
350 bool has_system = false;
351 std::string cache_filename;
352 bool has_cache = false;
353 bool dalvik_cache_exists = false;
354 const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
355 &has_system, &cache_filename, &dalvik_cache_exists,
356 &has_cache);
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100357
Alex Lighta59dd802014-07-02 16:28:08 -0700358 ImageSpace* space;
359 bool relocate = Runtime::Current()->ShouldRelocate();
Alex Light64ad14d2014-08-19 14:23:13 -0700360 bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
Narayan Kamathd1c606f2014-06-09 16:50:19 +0100361 if (found_image) {
Alex Lighta59dd802014-07-02 16:28:08 -0700362 const std::string* image_filename;
363 bool is_system = false;
364 bool relocated_version_used = false;
365 if (relocate) {
Alex Light64ad14d2014-08-19 14:23:13 -0700366 if (!dalvik_cache_exists) {
367 *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
368 "any dalvik_cache to find/place it in.",
369 image_location, system_filename.c_str());
370 return nullptr;
371 }
Alex Lighta59dd802014-07-02 16:28:08 -0700372 if (has_system) {
373 if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
374 // We already have a relocated version
375 image_filename = &cache_filename;
376 relocated_version_used = true;
377 } else {
378 // We cannot have a relocated version, Relocate the system one and use it.
Alex Light64ad14d2014-08-19 14:23:13 -0700379 if (can_compile && RelocateImage(image_location, cache_filename.c_str(), image_isa,
380 error_msg)) {
Alex Lighta59dd802014-07-02 16:28:08 -0700381 relocated_version_used = true;
382 image_filename = &cache_filename;
383 } else {
Alex Light64ad14d2014-08-19 14:23:13 -0700384 std::string reason;
385 if (can_compile) {
386 reason = StringPrintf(": %s", error_msg->c_str());
387 } else {
388 reason = " because image dex2oat is disabled.";
389 }
390 *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s'%s",
391 image_location, system_filename.c_str(),
392 cache_filename.c_str(), reason.c_str());
Alex Lighta59dd802014-07-02 16:28:08 -0700393 return nullptr;
394 }
395 }
396 } else {
397 CHECK(has_cache);
398 // We can just use cache's since it should be fine. This might or might not be relocated.
399 image_filename = &cache_filename;
400 }
401 } else {
402 if (has_system && has_cache) {
403 // Check they have the same cksum. If they do use the cache. Otherwise system.
404 if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
405 image_filename = &cache_filename;
406 relocated_version_used = true;
407 } else {
408 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700409 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700410 }
411 } else if (has_system) {
412 image_filename = &system_filename;
Alex Light1a762132014-07-31 09:32:13 -0700413 is_system = true;
Alex Lighta59dd802014-07-02 16:28:08 -0700414 } else {
415 CHECK(has_cache);
416 image_filename = &cache_filename;
417 }
418 }
419 {
420 // Note that we must not use the file descriptor associated with
421 // ScopedFlock::GetFile to Init the image file. We want the file
422 // descriptor (and the associated exclusive lock) to be released when
423 // we leave Create.
424 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700425 image_lock.Init(image_filename->c_str(), error_msg);
Alex Lightb6cabc12014-08-21 09:45:00 -0700426 VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
427 << image_location;
Alex Lightb93637a2014-07-31 10:48:46 -0700428 // If we are in /system we can assume the image is good. We can also
429 // assume this if we are using a relocated image (i.e. image checksum
430 // matches) since this is only different by the offset. We need this to
431 // make sure that host tests continue to work.
Alex Lighta59dd802014-07-02 16:28:08 -0700432 space = ImageSpace::Init(image_filename->c_str(), image_location,
Alex Light64ad14d2014-08-19 14:23:13 -0700433 !(is_system || relocated_version_used), error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700434 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100435 if (space != nullptr) {
436 return space;
437 }
438
Alex Lighta59dd802014-07-02 16:28:08 -0700439 // If the /system file exists, it should be up-to-date, don't try to generate it. Same if it is
440 // a relocated copy from something in /system (i.e. checksum's match).
441 // Otherwise, log a warning and fall through to GenerateImage.
442 if (relocated_version_used) {
443 LOG(FATAL) << "Attempted to use relocated version of " << image_location << " "
444 << "at " << cache_filename << " generated from " << system_filename << " "
445 << "but image failed to load: " << error_msg;
446 return nullptr;
447 } else if (is_system) {
Alex Light64ad14d2014-08-19 14:23:13 -0700448 *error_msg = StringPrintf("Failed to load /system image '%s': %s",
449 image_filename->c_str(), error_msg->c_str());
Narayan Kamath52f84882014-05-02 10:10:39 +0100450 return nullptr;
Mathieu Chartierc7cb1902014-03-05 14:41:03 -0800451 } else {
Alex Light64ad14d2014-08-19 14:23:13 -0700452 LOG(WARNING) << *error_msg;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700453 }
454 }
Narayan Kamath52f84882014-05-02 10:10:39 +0100455
Alex Light64ad14d2014-08-19 14:23:13 -0700456 if (!can_compile) {
457 *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
458 return nullptr;
459 } else if (!dalvik_cache_exists) {
460 *error_msg = StringPrintf("No place to put generated image.");
461 return nullptr;
Alex Light25396132014-08-27 15:37:23 -0700462 } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
Alex Light64ad14d2014-08-19 14:23:13 -0700463 *error_msg = StringPrintf("Failed to generate image '%s': %s",
464 cache_filename.c_str(), error_msg->c_str());
465 return nullptr;
466 } else {
Alex Lighta59dd802014-07-02 16:28:08 -0700467 // Note that we must not use the file descriptor associated with
468 // ScopedFlock::GetFile to Init the image file. We want the file
469 // descriptor (and the associated exclusive lock) to be released when
470 // we leave Create.
471 ScopedFlock image_lock;
Alex Light64ad14d2014-08-19 14:23:13 -0700472 image_lock.Init(cache_filename.c_str(), error_msg);
473 space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
474 if (space == nullptr) {
475 *error_msg = StringPrintf("Failed to load generated image '%s': %s",
476 cache_filename.c_str(), error_msg->c_str());
477 }
478 return space;
Alex Lighta59dd802014-07-02 16:28:08 -0700479 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700480}
481
Mathieu Chartier31e89252013-08-28 11:29:12 -0700482void ImageSpace::VerifyImageAllocations() {
483 byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
484 while (current < End()) {
485 DCHECK_ALIGNED(current, kObjectAlignment);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800486 mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700487 CHECK(live_bitmap_->Test(obj));
488 CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
Hiroshi Yamauchi624468c2014-03-31 15:14:47 -0700489 if (kUseBakerOrBrooksReadBarrier) {
490 obj->AssertReadBarrierPointer();
Hiroshi Yamauchi9d04a202014-01-31 13:35:49 -0800491 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700492 current += RoundUp(obj->SizeOf(), kObjectAlignment);
493 }
494}
495
Narayan Kamath52f84882014-05-02 10:10:39 +0100496ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
497 bool validate_oat_file, std::string* error_msg) {
498 CHECK(image_filename != nullptr);
499 CHECK(image_location != nullptr);
Ian Rogers1d54e732013-05-02 21:10:01 -0700500
501 uint64_t start_time = 0;
502 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
503 start_time = NanoTime();
Narayan Kamath52f84882014-05-02 10:10:39 +0100504 LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
Ian Rogers1d54e732013-05-02 21:10:01 -0700505 }
506
Ian Rogers700a4022014-05-19 16:49:03 -0700507 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
Ian Rogers1d54e732013-05-02 21:10:01 -0700508 if (file.get() == NULL) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100509 *error_msg = StringPrintf("Failed to open '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700510 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700511 }
512 ImageHeader image_header;
513 bool success = file->ReadFully(&image_header, sizeof(image_header));
514 if (!success || !image_header.IsValid()) {
Narayan Kamath52f84882014-05-02 10:10:39 +0100515 *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700516 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700517 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700518
519 // Note: The image header is part of the image due to mmap page alignment required of offset.
Ian Rogers700a4022014-05-19 16:49:03 -0700520 std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700521 image_header.GetImageSize(),
Ian Rogers1d54e732013-05-02 21:10:01 -0700522 PROT_READ | PROT_WRITE,
Hiroshi Yamauchi4fb5df82014-03-13 15:10:27 -0700523 MAP_PRIVATE,
Ian Rogers1d54e732013-05-02 21:10:01 -0700524 file->Fd(),
525 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526 false,
Narayan Kamath52f84882014-05-02 10:10:39 +0100527 image_filename,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700528 error_msg));
Ian Rogers1d54e732013-05-02 21:10:01 -0700529 if (map.get() == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700530 DCHECK(!error_msg->empty());
531 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700532 }
533 CHECK_EQ(image_header.GetImageBegin(), map->Begin());
534 DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
535
Ian Rogers700a4022014-05-19 16:49:03 -0700536 std::unique_ptr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
Mathieu Chartier31e89252013-08-28 11:29:12 -0700537 PROT_READ, MAP_PRIVATE,
538 file->Fd(), image_header.GetBitmapOffset(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700539 false,
Narayan Kamath52f84882014-05-02 10:10:39 +0100540 image_filename,
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700541 error_msg));
542 if (image_map.get() == nullptr) {
543 *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
544 return nullptr;
545 }
Ian Rogers3e5cf302014-05-20 16:40:37 -0700546 uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
Narayan Kamath52f84882014-05-02 10:10:39 +0100547 std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
Mathieu Chartier31e89252013-08-28 11:29:12 -0700548 bitmap_index));
Ian Rogers700a4022014-05-19 16:49:03 -0700549 std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700550 accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
551 reinterpret_cast<byte*>(map->Begin()),
552 map->Size()));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700553 if (bitmap.get() == nullptr) {
554 *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
555 return nullptr;
556 }
Mathieu Chartier31e89252013-08-28 11:29:12 -0700557
Ian Rogers700a4022014-05-19 16:49:03 -0700558 std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
Narayan Kamath52f84882014-05-02 10:10:39 +0100559 map.release(), bitmap.release()));
Hiroshi Yamauchibd0fb612014-05-20 13:46:00 -0700560
561 // VerifyImageAllocations() will be called later in Runtime::Init()
562 // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
563 // and ArtField::java_lang_reflect_ArtField_, which are used from
564 // Object::SizeOf() which VerifyImageAllocations() calls, are not
565 // set yet at this point.
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700566
Narayan Kamath52f84882014-05-02 10:10:39 +0100567 space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700568 if (space->oat_file_.get() == nullptr) {
569 DCHECK(!error_msg->empty());
570 return nullptr;
Ian Rogers1d54e732013-05-02 21:10:01 -0700571 }
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700572
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700573 if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
574 DCHECK(!error_msg->empty());
575 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700576 }
577
Vladimir Marko7624d252014-05-02 14:40:15 +0100578 Runtime* runtime = Runtime::Current();
579 runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
580
581 mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
582 runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
583 mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
584 runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
585 mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
586 runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
587
588 mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
589 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kSaveAll);
590 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
591 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsOnly);
592 callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
593 runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method), Runtime::kRefsAndArgs);
594
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700595 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
596 LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
597 << ") " << *space.get();
598 }
599 return space.release();
600}
601
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000602OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700603 const ImageHeader& image_header = GetImageHeader();
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000604 std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
605
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700606 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700607 !Runtime::Current()->IsCompiler(), error_msg);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700608 if (oat_file == NULL) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700609 *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
610 oat_filename.c_str(), GetName(), error_msg->c_str());
611 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700612 }
613 uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
614 uint32_t image_oat_checksum = image_header.GetOatChecksum();
615 if (oat_checksum != image_oat_checksum) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700616 *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
617 " in image %s", oat_checksum, image_oat_checksum, GetName());
618 return nullptr;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700619 }
Alex Lighta59dd802014-07-02 16:28:08 -0700620 int32_t image_patch_delta = image_header.GetPatchDelta();
621 int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
622 if (oat_patch_delta != image_patch_delta) {
623 // We should have already relocated by this point. Bail out.
624 *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
625 "in image %s", oat_patch_delta, image_patch_delta, GetName());
626 return nullptr;
627 }
628
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700629 return oat_file;
630}
631
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700632bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700633 CHECK(oat_file_.get() != NULL);
Mathieu Chartier31e89252013-08-28 11:29:12 -0700634 for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700635 const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
636 uint32_t dex_file_location_checksum;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700637 if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
638 *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
639 "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700640 return false;
641 }
642 if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700643 *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
644 "dex file '%s' (0x%x != 0x%x)",
645 oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
646 oat_dex_file->GetDexFileLocationChecksum(),
647 dex_file_location_checksum);
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700648 return false;
649 }
650 }
651 return true;
652}
653
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700654const OatFile* ImageSpace::GetOatFile() const {
655 return oat_file_.get();
656}
657
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700658OatFile* ImageSpace::ReleaseOatFile() {
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700659 CHECK(oat_file_.get() != NULL);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700660 return oat_file_.release();
Ian Rogers1d54e732013-05-02 21:10:01 -0700661}
662
Ian Rogers1d54e732013-05-02 21:10:01 -0700663void ImageSpace::Dump(std::ostream& os) const {
664 os << GetType()
Mathieu Chartier590fee92013-09-13 13:46:47 -0700665 << " begin=" << reinterpret_cast<void*>(Begin())
Ian Rogers1d54e732013-05-02 21:10:01 -0700666 << ",end=" << reinterpret_cast<void*>(End())
667 << ",size=" << PrettySize(Size())
668 << ",name=\"" << GetName() << "\"]";
669}
670
671} // namespace space
672} // namespace gc
673} // namespace art