image_space.cc revision 094ff2c0c1f736521c634d5f606e274cb6c55345
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/statvfs.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include <random>
25
26#include "base/macros.h"
27#include "base/stl_util.h"
28#include "base/unix_file/fd_file.h"
29#include "base/scoped_flock.h"
30#include "gc/accounting/space_bitmap-inl.h"
31#include "mirror/art_method.h"
32#include "mirror/class-inl.h"
33#include "mirror/object-inl.h"
34#include "oat_file.h"
35#include "os.h"
36#include "space-inl.h"
37#include "utils.h"
38
39namespace art {
40namespace gc {
41namespace space {
42
43Atomic<uint32_t> ImageSpace::bitmap_index_(0);
44
45ImageSpace::ImageSpace(const std::string& image_filename, const char* image_location,
46                       MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap,
47                       uint8_t* end)
48    : MemMapSpace(image_filename, mem_map, mem_map->Begin(), end, end,
49                  kGcRetentionPolicyNeverCollect),
50      image_location_(image_location) {
51  DCHECK(live_bitmap != nullptr);
52  live_bitmap_.reset(live_bitmap);
53}
54
55static 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
75// We are relocating or generating the core image. We should get rid of everything. It is all
76// out-of-date. We also don't really care if this fails since it is just a convenience.
77// 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.
79static void RealPruneDalvikCache(const std::string& cache_dir_path);
80
81static void PruneDalvikCache(InstructionSet isa) {
82  CHECK_NE(isa, kNone);
83  // Prune the base /data/dalvik-cache.
84  RealPruneDalvikCache(GetDalvikCacheOrDie(".", false));
85  // Prune /data/dalvik-cache/<isa>.
86  RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
87}
88
89static void RealPruneDalvikCache(const std::string& cache_dir_path) {
90  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  }
98
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    }
104    // We only want to delete regular files and symbolic links.
105    if (de->d_type != DT_REG && de->d_type != DT_LNK) {
106      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    }
112    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;
117      continue;
118    }
119  }
120  CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
121}
122
123// 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.
127static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
128  const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
129  const std::string boot_marker = isa_subdir + "/.booting";
130  const char* file_name = boot_marker.c_str();
131
132  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) {
150    LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
151    RealPruneDalvikCache(isa_subdir);
152  }
153
154  ++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.";
171  }
172}
173
174static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
175                          std::string* error_msg) {
176  const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
177  std::vector<std::string> boot_class_path;
178  Split(boot_class_path_string, ':', &boot_class_path);
179  if (boot_class_path.empty()) {
180    *error_msg = "Failed to generate image because no boot class path specified";
181    return false;
182  }
183  // We should clean up so we are more likely to have room for the image.
184  if (Runtime::Current()->IsZygote()) {
185    LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
186    PruneDalvikCache(image_isa);
187  }
188
189  std::vector<std::string> arg_vector;
190
191  std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
192  arg_vector.push_back(dex2oat);
193
194  std::string image_option_string("--image=");
195  image_option_string += image_filename;
196  arg_vector.push_back(image_option_string);
197
198  for (size_t i = 0; i < boot_class_path.size(); i++) {
199    arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
200  }
201
202  std::string oat_file_option_string("--oat-file=");
203  oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
204  arg_vector.push_back(oat_file_option_string);
205
206  // Note: we do not generate a fully debuggable boot image so we do not pass the
207  // compiler flag --debuggable here.
208
209  Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
210  CHECK_EQ(image_isa, kRuntimeISA)
211      << "We should always be generating an image for the current isa.";
212
213  int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
214                                                    ART_BASE_ADDRESS_MAX_DELTA);
215  LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
216            << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
217  arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
218
219  if (!kIsTargetBuild) {
220    arg_vector.push_back("--host");
221  }
222
223  const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
224  for (size_t i = 0; i < compiler_options.size(); ++i) {
225    arg_vector.push_back(compiler_options[i].c_str());
226  }
227
228  std::string command_line(Join(arg_vector, ' '));
229  LOG(INFO) << "GenerateImage: " << command_line;
230  return Exec(arg_vector, error_msg);
231}
232
233bool ImageSpace::FindImageFilename(const char* image_location,
234                                   const InstructionSet image_isa,
235                                   std::string* system_filename,
236                                   bool* has_system,
237                                   std::string* cache_filename,
238                                   bool* dalvik_cache_exists,
239                                   bool* has_cache,
240                                   bool* is_global_cache) {
241  *has_system = false;
242  *has_cache = false;
243  // image_location = /system/framework/boot.art
244  // system_image_location = /system/framework/<image_isa>/boot.art
245  std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
246  if (OS::FileExists(system_image_filename.c_str())) {
247    *system_filename = system_image_filename;
248    *has_system = true;
249  }
250
251  bool have_android_data = false;
252  *dalvik_cache_exists = false;
253  std::string dalvik_cache;
254  GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
255                 &have_android_data, dalvik_cache_exists, is_global_cache);
256
257  if (have_android_data && *dalvik_cache_exists) {
258    // Always set output location even if it does not exist,
259    // so that the caller knows where to create the image.
260    //
261    // image_location = /system/framework/boot.art
262    // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
263    std::string error_msg;
264    if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
265      LOG(WARNING) << error_msg;
266      return *has_system;
267    }
268    *has_cache = OS::FileExists(cache_filename->c_str());
269  }
270  return *has_system || *has_cache;
271}
272
273static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
274    std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
275    if (image_file.get() == nullptr) {
276      return false;
277    }
278    const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
279    if (!success || !image_header->IsValid()) {
280      return false;
281    }
282    return true;
283}
284
285// Relocate the image at image_location to dest_filename and relocate it by a random amount.
286static bool RelocateImage(const char* image_location, const char* dest_filename,
287                               InstructionSet isa, std::string* error_msg) {
288  // We should clean up so we are more likely to have room for the image.
289  if (Runtime::Current()->IsZygote()) {
290    LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
291    PruneDalvikCache(isa);
292  }
293
294  std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
295
296  std::string input_image_location_arg("--input-image-location=");
297  input_image_location_arg += image_location;
298
299  std::string output_image_filename_arg("--output-image-file=");
300  output_image_filename_arg += dest_filename;
301
302  std::string input_oat_location_arg("--input-oat-location=");
303  input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
304
305  std::string output_oat_filename_arg("--output-oat-file=");
306  output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
307
308  std::string instruction_set_arg("--instruction-set=");
309  instruction_set_arg += GetInstructionSetString(isa);
310
311  std::string base_offset_arg("--base-offset-delta=");
312  StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
313                                                                    ART_BASE_ADDRESS_MAX_DELTA));
314
315  std::vector<std::string> argv;
316  argv.push_back(patchoat);
317
318  argv.push_back(input_image_location_arg);
319  argv.push_back(output_image_filename_arg);
320
321  argv.push_back(input_oat_location_arg);
322  argv.push_back(output_oat_filename_arg);
323
324  argv.push_back(instruction_set_arg);
325  argv.push_back(base_offset_arg);
326
327  std::string command_line(Join(argv, ' '));
328  LOG(INFO) << "RelocateImage: " << command_line;
329  return Exec(argv, error_msg);
330}
331
332static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
333  std::unique_ptr<ImageHeader> hdr(new ImageHeader);
334  if (!ReadSpecificImageHeader(filename, hdr.get())) {
335    *error_msg = StringPrintf("Unable to read image header for %s", filename);
336    return nullptr;
337  }
338  return hdr.release();
339}
340
341ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
342                                              const InstructionSet image_isa) {
343  std::string error_msg;
344  ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
345  if (image_header == nullptr) {
346    LOG(FATAL) << error_msg;
347  }
348  return image_header;
349}
350
351ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
352                                         const InstructionSet image_isa,
353                                         std::string* error_msg) {
354  std::string system_filename;
355  bool has_system = false;
356  std::string cache_filename;
357  bool has_cache = false;
358  bool dalvik_cache_exists = false;
359  bool is_global_cache = false;
360  if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
361                        &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
362    if (Runtime::Current()->ShouldRelocate()) {
363      if (has_system && has_cache) {
364        std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
365        std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
366        if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
367          *error_msg = StringPrintf("Unable to read image header for %s at %s",
368                                    image_location, system_filename.c_str());
369          return nullptr;
370        }
371        if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
372          *error_msg = StringPrintf("Unable to read image header for %s at %s",
373                                    image_location, cache_filename.c_str());
374          return nullptr;
375        }
376        if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
377          *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
378                                    image_location);
379          return nullptr;
380        }
381        return cache_hdr.release();
382      } else if (!has_cache) {
383        *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
384                                  image_location);
385        return nullptr;
386      } else if (!has_system && has_cache) {
387        // This can probably just use the cache one.
388        return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
389      }
390    } else {
391      // We don't want to relocate, Just pick the appropriate one if we have it and return.
392      if (has_system && has_cache) {
393        // We want the cache if the checksum matches, otherwise the system.
394        std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
395                                                                    error_msg));
396        std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
397                                                                   error_msg));
398        if (system.get() == nullptr ||
399            (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
400          return cache.release();
401        } else {
402          return system.release();
403        }
404      } else if (has_system) {
405        return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
406      } else if (has_cache) {
407        return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
408      }
409    }
410  }
411
412  *error_msg = StringPrintf("Unable to find image file for %s", image_location);
413  return nullptr;
414}
415
416static bool ChecksumsMatch(const char* image_a, const char* image_b) {
417  ImageHeader hdr_a;
418  ImageHeader hdr_b;
419  return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
420      && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
421}
422
423static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
424  // Anyone can write into a "local" cache.
425  if (!is_global_cache) {
426    return true;
427  }
428
429  // Only the zygote is allowed to create the global boot image.
430  if (Runtime::Current()->IsZygote()) {
431    return true;
432  }
433
434  *error_msg = "Only the zygote can create the global boot image.";
435  return false;
436}
437
438static constexpr uint64_t kLowSpaceValue = 50 * MB;
439static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
440
441// Read the free space of the cache partition and make a decision whether to keep the generated
442// image. This is to try to mitigate situations where the system might run out of space later.
443static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
444  // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
445  struct statvfs buf;
446
447  int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
448  if (res != 0) {
449    // Could not stat. Conservatively tell the system to delete the image.
450    *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
451    return false;
452  }
453
454  uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
455  // Zygote is privileged, but other things are not. Use bavail.
456  uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
457
458  // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
459  // environment. We do not want to fail quickening the boot image there, as it is beneficial
460  // for time-to-UI.
461  if (fs_overall_size > kTmpFsSentinelValue) {
462    if (fs_free_size < kLowSpaceValue) {
463      *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image"
464                                " generation, need at least %" PRIu64 ".",
465                                static_cast<double>(fs_free_size) / MB,
466                                kLowSpaceValue / MB);
467      return false;
468    }
469  }
470  return true;
471}
472
473ImageSpace* ImageSpace::Create(const char* image_location,
474                               const InstructionSet image_isa,
475                               std::string* error_msg) {
476  std::string system_filename;
477  bool has_system = false;
478  std::string cache_filename;
479  bool has_cache = false;
480  bool dalvik_cache_exists = false;
481  bool is_global_cache = true;
482  const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
483                                             &has_system, &cache_filename, &dalvik_cache_exists,
484                                             &has_cache, &is_global_cache);
485
486  if (Runtime::Current()->IsZygote()) {
487    MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots());
488  }
489
490  ImageSpace* space;
491  bool relocate = Runtime::Current()->ShouldRelocate();
492  bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
493  if (found_image) {
494    const std::string* image_filename;
495    bool is_system = false;
496    bool relocated_version_used = false;
497    if (relocate) {
498      if (!dalvik_cache_exists) {
499        *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
500                                  "any dalvik_cache to find/place it in.",
501                                  image_location, system_filename.c_str());
502        return nullptr;
503      }
504      if (has_system) {
505        if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
506          // We already have a relocated version
507          image_filename = &cache_filename;
508          relocated_version_used = true;
509        } else {
510          // We cannot have a relocated version, Relocate the system one and use it.
511
512          std::string reason;
513          bool success;
514
515          // Check whether we are allowed to relocate.
516          if (!can_compile) {
517            reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
518            success = false;
519          } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
520            // Whether we can write to the cache.
521            success = false;
522          } else {
523            // Try to relocate.
524            success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
525          }
526
527          if (success) {
528            relocated_version_used = true;
529            image_filename = &cache_filename;
530          } else {
531            *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
532                                      image_location, system_filename.c_str(),
533                                      cache_filename.c_str(), reason.c_str());
534            // We failed to create files, remove any possibly garbage output.
535            // Since ImageCreationAllowed was true above, we are the zygote
536            // and therefore the only process expected to generate these for
537            // the device.
538            PruneDalvikCache(image_isa);
539            return nullptr;
540          }
541        }
542      } else {
543        CHECK(has_cache);
544        // We can just use cache's since it should be fine. This might or might not be relocated.
545        image_filename = &cache_filename;
546      }
547    } else {
548      if (has_system && has_cache) {
549        // Check they have the same cksum. If they do use the cache. Otherwise system.
550        if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
551          image_filename = &cache_filename;
552          relocated_version_used = true;
553        } else {
554          image_filename = &system_filename;
555          is_system = true;
556        }
557      } else if (has_system) {
558        image_filename = &system_filename;
559        is_system = true;
560      } else {
561        CHECK(has_cache);
562        image_filename = &cache_filename;
563      }
564    }
565    {
566      // Note that we must not use the file descriptor associated with
567      // ScopedFlock::GetFile to Init the image file. We want the file
568      // descriptor (and the associated exclusive lock) to be released when
569      // we leave Create.
570      ScopedFlock image_lock;
571      image_lock.Init(image_filename->c_str(), error_msg);
572      VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
573                    << image_location;
574      // If we are in /system we can assume the image is good. We can also
575      // assume this if we are using a relocated image (i.e. image checksum
576      // matches) since this is only different by the offset. We need this to
577      // make sure that host tests continue to work.
578      space = ImageSpace::Init(image_filename->c_str(), image_location,
579                               !(is_system || relocated_version_used), error_msg);
580    }
581    if (space != nullptr) {
582      return space;
583    }
584
585    if (relocated_version_used) {
586      // Something is wrong with the relocated copy (even though checksums match). Cleanup.
587      // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
588      // TODO: Check the oat file validity earlier.
589      *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
590                                "but image failed to load: %s",
591                                image_location, cache_filename.c_str(), system_filename.c_str(),
592                                error_msg->c_str());
593      PruneDalvikCache(image_isa);
594      return nullptr;
595    } else if (is_system) {
596      // If the /system file exists, it should be up-to-date, don't try to generate it.
597      *error_msg = StringPrintf("Failed to load /system image '%s': %s",
598                                image_filename->c_str(), error_msg->c_str());
599      return nullptr;
600    } else {
601      // Otherwise, log a warning and fall through to GenerateImage.
602      LOG(WARNING) << *error_msg;
603    }
604  }
605
606  if (!can_compile) {
607    *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
608    return nullptr;
609  } else if (!dalvik_cache_exists) {
610    *error_msg = StringPrintf("No place to put generated image.");
611    return nullptr;
612  } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
613    return nullptr;
614  } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
615    *error_msg = StringPrintf("Failed to generate image '%s': %s",
616                              cache_filename.c_str(), error_msg->c_str());
617    // We failed to create files, remove any possibly garbage output.
618    // Since ImageCreationAllowed was true above, we are the zygote
619    // and therefore the only process expected to generate these for
620    // the device.
621    PruneDalvikCache(image_isa);
622    return nullptr;
623  } else {
624    // Check whether there is enough space left over after we have generated the image.
625    if (!CheckSpace(cache_filename, error_msg)) {
626      // No. Delete the generated image and try to run out of the dex files.
627      PruneDalvikCache(image_isa);
628      return nullptr;
629    }
630
631    // Note that we must not use the file descriptor associated with
632    // ScopedFlock::GetFile to Init the image file. We want the file
633    // descriptor (and the associated exclusive lock) to be released when
634    // we leave Create.
635    ScopedFlock image_lock;
636    image_lock.Init(cache_filename.c_str(), error_msg);
637    space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
638    if (space == nullptr) {
639      *error_msg = StringPrintf("Failed to load generated image '%s': %s",
640                                cache_filename.c_str(), error_msg->c_str());
641    }
642    return space;
643  }
644}
645
646void ImageSpace::VerifyImageAllocations() {
647  uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
648  while (current < End()) {
649    CHECK_ALIGNED(current, kObjectAlignment);
650    auto* obj = reinterpret_cast<mirror::Object*>(current);
651    CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
652    CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj);
653    if (kUseBakerOrBrooksReadBarrier) {
654      obj->AssertReadBarrierPointer();
655    }
656    current += RoundUp(obj->SizeOf(), kObjectAlignment);
657  }
658}
659
660ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
661                             bool validate_oat_file, std::string* error_msg) {
662  CHECK(image_filename != nullptr);
663  CHECK(image_location != nullptr);
664
665  uint64_t start_time = 0;
666  if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
667    start_time = NanoTime();
668    LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
669  }
670
671  std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
672  if (file.get() == nullptr) {
673    *error_msg = StringPrintf("Failed to open '%s'", image_filename);
674    return nullptr;
675  }
676  ImageHeader image_header;
677  bool success = file->ReadFully(&image_header, sizeof(image_header));
678  if (!success || !image_header.IsValid()) {
679    *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
680    return nullptr;
681  }
682  // Check that the file is large enough.
683  uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
684  if (image_header.GetImageSize() > image_file_size) {
685    *error_msg = StringPrintf("Image file too small for image heap: %" PRIu64 " vs. %zu.",
686                              image_file_size, image_header.GetImageSize());
687    return nullptr;
688  }
689  auto end_of_bitmap = image_header.GetImageBitmapOffset() + image_header.GetImageBitmapSize();
690  if (end_of_bitmap != image_file_size) {
691    *error_msg = StringPrintf(
692        "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size,
693        end_of_bitmap);
694    return nullptr;
695  }
696
697  // Note: The image header is part of the image due to mmap page alignment required of offset.
698  std::unique_ptr<MemMap> map(MemMap::MapFileAtAddress(
699      image_header.GetImageBegin(), image_header.GetImageSize() + image_header.GetArtFieldsSize(),
700      PROT_READ | PROT_WRITE, MAP_PRIVATE, file->Fd(), 0, false, image_filename, error_msg));
701  if (map.get() == nullptr) {
702    DCHECK(!error_msg->empty());
703    return nullptr;
704  }
705  CHECK_EQ(image_header.GetImageBegin(), map->Begin());
706  DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
707
708  std::unique_ptr<MemMap> image_map(
709      MemMap::MapFileAtAddress(nullptr, image_header.GetImageBitmapSize(),
710                               PROT_READ, MAP_PRIVATE,
711                               file->Fd(), image_header.GetImageBitmapOffset(),
712                               false,
713                               image_filename,
714                               error_msg));
715  if (image_map.get() == nullptr) {
716    *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
717    return nullptr;
718  }
719  uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
720  std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
721                                       bitmap_index));
722  std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
723      accounting::ContinuousSpaceBitmap::CreateFromMemMap(bitmap_name, image_map.release(),
724                                                          reinterpret_cast<uint8_t*>(map->Begin()),
725                                                          map->Size()));
726  if (bitmap.get() == nullptr) {
727    *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
728    return nullptr;
729  }
730
731  uint8_t* const image_end = map->Begin() + image_header.GetImageSize();
732  std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename, image_location,
733                                                   map.release(), bitmap.release(), image_end));
734
735  // VerifyImageAllocations() will be called later in Runtime::Init()
736  // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
737  // and ArtField::java_lang_reflect_ArtField_, which are used from
738  // Object::SizeOf() which VerifyImageAllocations() calls, are not
739  // set yet at this point.
740
741  space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
742  if (space->oat_file_.get() == nullptr) {
743    DCHECK(!error_msg->empty());
744    return nullptr;
745  }
746
747  if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
748    DCHECK(!error_msg->empty());
749    return nullptr;
750  }
751
752  Runtime* runtime = Runtime::Current();
753  runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
754
755  mirror::Object* resolution_method = image_header.GetImageRoot(ImageHeader::kResolutionMethod);
756  runtime->SetResolutionMethod(down_cast<mirror::ArtMethod*>(resolution_method));
757  mirror::Object* imt_conflict_method = image_header.GetImageRoot(ImageHeader::kImtConflictMethod);
758  runtime->SetImtConflictMethod(down_cast<mirror::ArtMethod*>(imt_conflict_method));
759  mirror::Object* imt_unimplemented_method =
760      image_header.GetImageRoot(ImageHeader::kImtUnimplementedMethod);
761  runtime->SetImtUnimplementedMethod(down_cast<mirror::ArtMethod*>(imt_unimplemented_method));
762  mirror::Object* default_imt = image_header.GetImageRoot(ImageHeader::kDefaultImt);
763  runtime->SetDefaultImt(down_cast<mirror::ObjectArray<mirror::ArtMethod>*>(default_imt));
764
765  mirror::Object* callee_save_method = image_header.GetImageRoot(ImageHeader::kCalleeSaveMethod);
766  runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
767                               Runtime::kSaveAll);
768  callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsOnlySaveMethod);
769  runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
770                               Runtime::kRefsOnly);
771  callee_save_method = image_header.GetImageRoot(ImageHeader::kRefsAndArgsSaveMethod);
772  runtime->SetCalleeSaveMethod(down_cast<mirror::ArtMethod*>(callee_save_method),
773                               Runtime::kRefsAndArgs);
774
775  if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
776    LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
777             << ") " << *space.get();
778  }
779  return space.release();
780}
781
782OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
783  const ImageHeader& image_header = GetImageHeader();
784  std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
785
786  CHECK(image_header.GetOatDataBegin() != nullptr);
787
788  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, image_header.GetOatDataBegin(),
789                                    image_header.GetOatFileBegin(),
790                                    !Runtime::Current()->IsAotCompiler(),
791                                    nullptr, error_msg);
792  if (oat_file == nullptr) {
793    *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
794                              oat_filename.c_str(), GetName(), error_msg->c_str());
795    return nullptr;
796  }
797  uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
798  uint32_t image_oat_checksum = image_header.GetOatChecksum();
799  if (oat_checksum != image_oat_checksum) {
800    *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
801                              " in image %s", oat_checksum, image_oat_checksum, GetName());
802    return nullptr;
803  }
804  int32_t image_patch_delta = image_header.GetPatchDelta();
805  int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
806  if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
807    // We should have already relocated by this point. Bail out.
808    *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
809                              "in image %s", oat_patch_delta, image_patch_delta, GetName());
810    return nullptr;
811  }
812
813  return oat_file;
814}
815
816bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
817  CHECK(oat_file_.get() != nullptr);
818  for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
819    const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
820    uint32_t dex_file_location_checksum;
821    if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
822      *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
823                                "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
824      return false;
825    }
826    if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
827      *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
828                                "dex file '%s' (0x%x != 0x%x)",
829                                oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
830                                oat_dex_file->GetDexFileLocationChecksum(),
831                                dex_file_location_checksum);
832      return false;
833    }
834  }
835  return true;
836}
837
838const OatFile* ImageSpace::GetOatFile() const {
839  return oat_file_.get();
840}
841
842OatFile* ImageSpace::ReleaseOatFile() {
843  CHECK(oat_file_.get() != nullptr);
844  return oat_file_.release();
845}
846
847void ImageSpace::Dump(std::ostream& os) const {
848  os << GetType()
849      << " begin=" << reinterpret_cast<void*>(Begin())
850      << ",end=" << reinterpret_cast<void*>(End())
851      << ",size=" << PrettySize(Size())
852      << ",name=\"" << GetName() << "\"]";
853}
854
855}  // namespace space
856}  // namespace gc
857}  // namespace art
858