image_space.cc revision 6c050b5f782b61a16083ae35c98931a2d51d0a33
1/*
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
19#include <dirent.h>
20#include <sys/types.h>
21
22#include <random>
23
24#include "base/stl_util.h"
25#include "base/unix_file/fd_file.h"
26#include "base/scoped_flock.h"
27#include "gc/accounting/space_bitmap-inl.h"
28#include "mirror/art_method.h"
29#include "mirror/class-inl.h"
30#include "mirror/object-inl.h"
31#include "oat_file.h"
32#include "os.h"
33#include "space-inl.h"
34#include "utils.h"
35
36namespace art {
37namespace gc {
38namespace space {
39
40Atomic<uint32_t> ImageSpace::bitmap_index_(0);
41
42ImageSpace::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) {
47  DCHECK(live_bitmap != nullptr);
48  live_bitmap_.reset(live_bitmap);
49}
50
51static 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
71// 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) {
118  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()) {
122    *error_msg = "Failed to generate image because no boot class path specified";
123    return false;
124  }
125  // 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  }
130
131  std::vector<std::string> arg_vector;
132
133  std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
134  arg_vector.push_back(dex2oat);
135
136  std::string image_option_string("--image=");
137  image_option_string += image_filename;
138  arg_vector.push_back(image_option_string);
139
140  for (size_t i = 0; i < boot_class_path.size(); i++) {
141    arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
142  }
143
144  std::string oat_file_option_string("--oat-file=");
145  oat_file_option_string += image_filename;
146  oat_file_option_string.erase(oat_file_option_string.size() - 3);
147  oat_file_option_string += "oat";
148  arg_vector.push_back(oat_file_option_string);
149
150  Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
151  CHECK_EQ(image_isa, kRuntimeISA) << "We should always be generating an image for the current isa.";
152
153  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));
158
159  if (!kIsTargetBuild) {
160    arg_vector.push_back("--host");
161  }
162
163  const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
164  for (size_t i = 0; i < compiler_options.size(); ++i) {
165    arg_vector.push_back(compiler_options[i].c_str());
166  }
167
168  std::string command_line(Join(arg_vector, ' '));
169  LOG(INFO) << "GenerateImage: " << command_line;
170  return Exec(arg_vector, error_msg);
171}
172
173bool ImageSpace::FindImageFilename(const char* image_location,
174                                   const InstructionSet image_isa,
175                                   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;
182  // 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())) {
186    *system_filename = system_image_filename;
187    *has_system = true;
188  }
189
190  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);
195
196  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
224// 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,
226                               InstructionSet isa, std::string* error_msg) {
227  // 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
233  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();
278}
279
280ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
281                                              const InstructionSet image_isa) {
282  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      }
332    }
333  }
334
335  LOG(FATAL) << "Unable to find image file for: " << image_location;
336  return nullptr;
337}
338
339static 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
346ImageSpace* ImageSpace::Create(const char* image_location,
347                               const InstructionSet image_isa,
348                               std::string* error_msg) {
349  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);
357
358  ImageSpace* space;
359  bool relocate = Runtime::Current()->ShouldRelocate();
360  bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
361  if (found_image) {
362    const std::string* image_filename;
363    bool is_system = false;
364    bool relocated_version_used = false;
365    if (relocate) {
366      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      }
372      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.
379          if (can_compile && RelocateImage(image_location, cache_filename.c_str(), image_isa,
380                                           error_msg)) {
381            relocated_version_used = true;
382            image_filename = &cache_filename;
383          } else {
384            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());
393            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;
409          is_system = true;
410        }
411      } else if (has_system) {
412        image_filename = &system_filename;
413        is_system = true;
414      } 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;
425      image_lock.Init(image_filename->c_str(), error_msg);
426      VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
427                    << image_location;
428      // 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.
432      space = ImageSpace::Init(image_filename->c_str(), image_location,
433                               !(is_system || relocated_version_used), error_msg);
434    }
435    if (space != nullptr) {
436      return space;
437    }
438
439    // 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) {
448      *error_msg = StringPrintf("Failed to load /system image '%s': %s",
449                                image_filename->c_str(), error_msg->c_str());
450      return nullptr;
451    } else {
452      LOG(WARNING) << *error_msg;
453    }
454  }
455
456  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;
462  } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
463    *error_msg = StringPrintf("Failed to generate image '%s': %s",
464                              cache_filename.c_str(), error_msg->c_str());
465    return nullptr;
466  } else {
467    // 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;
472    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;
479  }
480}
481
482void ImageSpace::VerifyImageAllocations() {
483  byte* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
484  while (current < End()) {
485    DCHECK_ALIGNED(current, kObjectAlignment);
486    mirror::Object* obj = reinterpret_cast<mirror::Object*>(current);
487    CHECK(live_bitmap_->Test(obj));
488    CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
489    if (kUseBakerOrBrooksReadBarrier) {
490      obj->AssertReadBarrierPointer();
491    }
492    current += RoundUp(obj->SizeOf(), kObjectAlignment);
493  }
494}
495
496ImageSpace* 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);
500
501  uint64_t start_time = 0;
502  if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
503    start_time = NanoTime();
504    LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
505  }
506
507  std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
508  if (file.get() == NULL) {
509    *error_msg = StringPrintf("Failed to open '%s'", image_filename);
510    return nullptr;
511  }
512  ImageHeader image_header;
513  bool success = file->ReadFully(&image_header, sizeof(image_header));
514  if (!success || !image_header.IsValid()) {
515    *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
516    return nullptr;
517  }
518
519  // Note: The image header is part of the image due to mmap page alignment required of offset.
520  std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
521                                                 image_header.GetImageSize(),
522                                                 PROT_READ | PROT_WRITE,
523                                                 MAP_PRIVATE,
524                                                 file->Fd(),
525                                                 0,
526                                                 false,
527                                                 image_filename,
528                                                 error_msg));
529  if (map.get() == NULL) {
530    DCHECK(!error_msg->empty());
531    return nullptr;
532  }
533  CHECK_EQ(image_header.GetImageBegin(), map->Begin());
534  DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
535
536  std::unique_ptr<MemMap> image_map(MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
537                                                       PROT_READ, MAP_PRIVATE,
538                                                       file->Fd(), image_header.GetBitmapOffset(),
539                                                       false,
540                                                       image_filename,
541                                                       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  }
546  uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
547  std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
548                                       bitmap_index));
549  std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
550      accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
551                                                          reinterpret_cast<byte*>(map->Begin()),
552                                                          map->Size()));
553  if (bitmap.get() == nullptr) {
554    *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
555    return nullptr;
556  }
557
558  std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
559                                             map.release(), bitmap.release()));
560
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.
566
567  space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
568  if (space->oat_file_.get() == nullptr) {
569    DCHECK(!error_msg->empty());
570    return nullptr;
571  }
572
573  if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
574    DCHECK(!error_msg->empty());
575    return nullptr;
576  }
577
578  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
595  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
602OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
603  const ImageHeader& image_header = GetImageHeader();
604  std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
605
606  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
607                                    !Runtime::Current()->IsCompiler(), error_msg);
608  if (oat_file == NULL) {
609    *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;
612  }
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) {
616    *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;
619  }
620  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
629  return oat_file;
630}
631
632bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
633  CHECK(oat_file_.get() != NULL);
634  for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
635    const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
636    uint32_t dex_file_location_checksum;
637    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());
640      return false;
641    }
642    if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
643      *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);
648      return false;
649    }
650  }
651  return true;
652}
653
654const OatFile* ImageSpace::GetOatFile() const {
655  return oat_file_.get();
656}
657
658OatFile* ImageSpace::ReleaseOatFile() {
659  CHECK(oat_file_.get() != NULL);
660  return oat_file_.release();
661}
662
663void ImageSpace::Dump(std::ostream& os) const {
664  os << GetType()
665      << " begin=" << reinterpret_cast<void*>(Begin())
666      << ",end=" << reinterpret_cast<void*>(End())
667      << ",size=" << PrettySize(Size())
668      << ",name=\"" << GetName() << "\"]";
669}
670
671}  // namespace space
672}  // namespace gc
673}  // namespace art
674