image_space.cc revision 1bc977cf2f8199311a97f2ba9431a184540e3e9c
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 <lz4.h>
21#include <random>
22#include <sys/statvfs.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include "art_method.h"
27#include "base/macros.h"
28#include "base/stl_util.h"
29#include "base/scoped_flock.h"
30#include "base/time_utils.h"
31#include "base/unix_file/fd_file.h"
32#include "gc/accounting/space_bitmap-inl.h"
33#include "mirror/class-inl.h"
34#include "mirror/object-inl.h"
35#include "oat_file.h"
36#include "os.h"
37#include "space-inl.h"
38#include "utils.h"
39
40namespace art {
41namespace gc {
42namespace space {
43
44Atomic<uint32_t> ImageSpace::bitmap_index_(0);
45
46ImageSpace::ImageSpace(const std::string& image_filename,
47                       const char* image_location,
48                       MemMap* mem_map,
49                       accounting::ContinuousSpaceBitmap* live_bitmap,
50                       uint8_t* end)
51    : MemMapSpace(image_filename,
52                  mem_map,
53                  mem_map->Begin(),
54                  end,
55                  end,
56                  kGcRetentionPolicyNeverCollect),
57      oat_file_non_owned_(nullptr),
58      image_location_(image_location) {
59  DCHECK(live_bitmap != nullptr);
60  live_bitmap_.reset(live_bitmap);
61}
62
63static int32_t ChooseRelocationOffsetDelta(int32_t min_delta, int32_t max_delta) {
64  CHECK_ALIGNED(min_delta, kPageSize);
65  CHECK_ALIGNED(max_delta, kPageSize);
66  CHECK_LT(min_delta, max_delta);
67
68  int32_t r = GetRandomNumber<int32_t>(min_delta, max_delta);
69  if (r % 2 == 0) {
70    r = RoundUp(r, kPageSize);
71  } else {
72    r = RoundDown(r, kPageSize);
73  }
74  CHECK_LE(min_delta, r);
75  CHECK_GE(max_delta, r);
76  CHECK_ALIGNED(r, kPageSize);
77  return r;
78}
79
80// We are relocating or generating the core image. We should get rid of everything. It is all
81// out-of-date. We also don't really care if this fails since it is just a convenience.
82// Adapted from prune_dex_cache(const char* subdir) in frameworks/native/cmds/installd/commands.c
83// Note this should only be used during first boot.
84static void RealPruneDalvikCache(const std::string& cache_dir_path);
85
86static void PruneDalvikCache(InstructionSet isa) {
87  CHECK_NE(isa, kNone);
88  // Prune the base /data/dalvik-cache.
89  RealPruneDalvikCache(GetDalvikCacheOrDie(".", false));
90  // Prune /data/dalvik-cache/<isa>.
91  RealPruneDalvikCache(GetDalvikCacheOrDie(GetInstructionSetString(isa), false));
92}
93
94static void RealPruneDalvikCache(const std::string& cache_dir_path) {
95  if (!OS::DirectoryExists(cache_dir_path.c_str())) {
96    return;
97  }
98  DIR* cache_dir = opendir(cache_dir_path.c_str());
99  if (cache_dir == nullptr) {
100    PLOG(WARNING) << "Unable to open " << cache_dir_path << " to delete it's contents";
101    return;
102  }
103
104  for (struct dirent* de = readdir(cache_dir); de != nullptr; de = readdir(cache_dir)) {
105    const char* name = de->d_name;
106    if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
107      continue;
108    }
109    // We only want to delete regular files and symbolic links.
110    if (de->d_type != DT_REG && de->d_type != DT_LNK) {
111      if (de->d_type != DT_DIR) {
112        // We do expect some directories (namely the <isa> for pruning the base dalvik-cache).
113        LOG(WARNING) << "Unexpected file type of " << std::hex << de->d_type << " encountered.";
114      }
115      continue;
116    }
117    std::string cache_file(cache_dir_path);
118    cache_file += '/';
119    cache_file += name;
120    if (TEMP_FAILURE_RETRY(unlink(cache_file.c_str())) != 0) {
121      PLOG(ERROR) << "Unable to unlink " << cache_file;
122      continue;
123    }
124  }
125  CHECK_EQ(0, TEMP_FAILURE_RETRY(closedir(cache_dir))) << "Unable to close directory.";
126}
127
128// We write out an empty file to the zygote's ISA specific cache dir at the start of
129// every zygote boot and delete it when the boot completes. If we find a file already
130// present, it usually means the boot didn't complete. We wipe the entire dalvik
131// cache if that's the case.
132static void MarkZygoteStart(const InstructionSet isa, const uint32_t max_failed_boots) {
133  const std::string isa_subdir = GetDalvikCacheOrDie(GetInstructionSetString(isa), false);
134  const std::string boot_marker = isa_subdir + "/.booting";
135  const char* file_name = boot_marker.c_str();
136
137  uint32_t num_failed_boots = 0;
138  std::unique_ptr<File> file(OS::OpenFileReadWrite(file_name));
139  if (file.get() == nullptr) {
140    file.reset(OS::CreateEmptyFile(file_name));
141
142    if (file.get() == nullptr) {
143      PLOG(WARNING) << "Failed to create boot marker.";
144      return;
145    }
146  } else {
147    if (!file->ReadFully(&num_failed_boots, sizeof(num_failed_boots))) {
148      PLOG(WARNING) << "Failed to read boot marker.";
149      file->Erase();
150      return;
151    }
152  }
153
154  if (max_failed_boots != 0 && num_failed_boots > max_failed_boots) {
155    LOG(WARNING) << "Incomplete boot detected. Pruning dalvik cache";
156    RealPruneDalvikCache(isa_subdir);
157  }
158
159  ++num_failed_boots;
160  VLOG(startup) << "Number of failed boots on : " << boot_marker << " = " << num_failed_boots;
161
162  if (lseek(file->Fd(), 0, SEEK_SET) == -1) {
163    PLOG(WARNING) << "Failed to write boot marker.";
164    file->Erase();
165    return;
166  }
167
168  if (!file->WriteFully(&num_failed_boots, sizeof(num_failed_boots))) {
169    PLOG(WARNING) << "Failed to write boot marker.";
170    file->Erase();
171    return;
172  }
173
174  if (file->FlushCloseOrErase() != 0) {
175    PLOG(WARNING) << "Failed to flush boot marker.";
176  }
177}
178
179static bool GenerateImage(const std::string& image_filename, InstructionSet image_isa,
180                          std::string* error_msg) {
181  const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
182  std::vector<std::string> boot_class_path;
183  Split(boot_class_path_string, ':', &boot_class_path);
184  if (boot_class_path.empty()) {
185    *error_msg = "Failed to generate image because no boot class path specified";
186    return false;
187  }
188  // We should clean up so we are more likely to have room for the image.
189  if (Runtime::Current()->IsZygote()) {
190    LOG(INFO) << "Pruning dalvik-cache since we are generating an image and will need to recompile";
191    PruneDalvikCache(image_isa);
192  }
193
194  std::vector<std::string> arg_vector;
195
196  std::string dex2oat(Runtime::Current()->GetCompilerExecutable());
197  arg_vector.push_back(dex2oat);
198
199  std::string image_option_string("--image=");
200  image_option_string += image_filename;
201  arg_vector.push_back(image_option_string);
202
203  for (size_t i = 0; i < boot_class_path.size(); i++) {
204    arg_vector.push_back(std::string("--dex-file=") + boot_class_path[i]);
205  }
206
207  std::string oat_file_option_string("--oat-file=");
208  oat_file_option_string += ImageHeader::GetOatLocationFromImageLocation(image_filename);
209  arg_vector.push_back(oat_file_option_string);
210
211  // Note: we do not generate a fully debuggable boot image so we do not pass the
212  // compiler flag --debuggable here.
213
214  Runtime::Current()->AddCurrentRuntimeFeaturesAsDex2OatArguments(&arg_vector);
215  CHECK_EQ(image_isa, kRuntimeISA)
216      << "We should always be generating an image for the current isa.";
217
218  int32_t base_offset = ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
219                                                    ART_BASE_ADDRESS_MAX_DELTA);
220  LOG(INFO) << "Using an offset of 0x" << std::hex << base_offset << " from default "
221            << "art base address of 0x" << std::hex << ART_BASE_ADDRESS;
222  arg_vector.push_back(StringPrintf("--base=0x%x", ART_BASE_ADDRESS + base_offset));
223
224  if (!kIsTargetBuild) {
225    arg_vector.push_back("--host");
226  }
227
228  const std::vector<std::string>& compiler_options = Runtime::Current()->GetImageCompilerOptions();
229  for (size_t i = 0; i < compiler_options.size(); ++i) {
230    arg_vector.push_back(compiler_options[i].c_str());
231  }
232
233  std::string command_line(Join(arg_vector, ' '));
234  LOG(INFO) << "GenerateImage: " << command_line;
235  return Exec(arg_vector, error_msg);
236}
237
238bool ImageSpace::FindImageFilename(const char* image_location,
239                                   const InstructionSet image_isa,
240                                   std::string* system_filename,
241                                   bool* has_system,
242                                   std::string* cache_filename,
243                                   bool* dalvik_cache_exists,
244                                   bool* has_cache,
245                                   bool* is_global_cache) {
246  *has_system = false;
247  *has_cache = false;
248  // image_location = /system/framework/boot.art
249  // system_image_location = /system/framework/<image_isa>/boot.art
250  std::string system_image_filename(GetSystemImageFilename(image_location, image_isa));
251  if (OS::FileExists(system_image_filename.c_str())) {
252    *system_filename = system_image_filename;
253    *has_system = true;
254  }
255
256  bool have_android_data = false;
257  *dalvik_cache_exists = false;
258  std::string dalvik_cache;
259  GetDalvikCache(GetInstructionSetString(image_isa), true, &dalvik_cache,
260                 &have_android_data, dalvik_cache_exists, is_global_cache);
261
262  if (have_android_data && *dalvik_cache_exists) {
263    // Always set output location even if it does not exist,
264    // so that the caller knows where to create the image.
265    //
266    // image_location = /system/framework/boot.art
267    // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
268    std::string error_msg;
269    if (!GetDalvikCacheFilename(image_location, dalvik_cache.c_str(), cache_filename, &error_msg)) {
270      LOG(WARNING) << error_msg;
271      return *has_system;
272    }
273    *has_cache = OS::FileExists(cache_filename->c_str());
274  }
275  return *has_system || *has_cache;
276}
277
278static bool ReadSpecificImageHeader(const char* filename, ImageHeader* image_header) {
279    std::unique_ptr<File> image_file(OS::OpenFileForReading(filename));
280    if (image_file.get() == nullptr) {
281      return false;
282    }
283    const bool success = image_file->ReadFully(image_header, sizeof(ImageHeader));
284    if (!success || !image_header->IsValid()) {
285      return false;
286    }
287    return true;
288}
289
290// Relocate the image at image_location to dest_filename and relocate it by a random amount.
291static bool RelocateImage(const char* image_location, const char* dest_filename,
292                               InstructionSet isa, std::string* error_msg) {
293  // We should clean up so we are more likely to have room for the image.
294  if (Runtime::Current()->IsZygote()) {
295    LOG(INFO) << "Pruning dalvik-cache since we are relocating an image and will need to recompile";
296    PruneDalvikCache(isa);
297  }
298
299  std::string patchoat(Runtime::Current()->GetPatchoatExecutable());
300
301  std::string input_image_location_arg("--input-image-location=");
302  input_image_location_arg += image_location;
303
304  std::string output_image_filename_arg("--output-image-file=");
305  output_image_filename_arg += dest_filename;
306
307  std::string input_oat_location_arg("--input-oat-location=");
308  input_oat_location_arg += ImageHeader::GetOatLocationFromImageLocation(image_location);
309
310  std::string output_oat_filename_arg("--output-oat-file=");
311  output_oat_filename_arg += ImageHeader::GetOatLocationFromImageLocation(dest_filename);
312
313  std::string instruction_set_arg("--instruction-set=");
314  instruction_set_arg += GetInstructionSetString(isa);
315
316  std::string base_offset_arg("--base-offset-delta=");
317  StringAppendF(&base_offset_arg, "%d", ChooseRelocationOffsetDelta(ART_BASE_ADDRESS_MIN_DELTA,
318                                                                    ART_BASE_ADDRESS_MAX_DELTA));
319
320  std::vector<std::string> argv;
321  argv.push_back(patchoat);
322
323  argv.push_back(input_image_location_arg);
324  argv.push_back(output_image_filename_arg);
325
326  argv.push_back(input_oat_location_arg);
327  argv.push_back(output_oat_filename_arg);
328
329  argv.push_back(instruction_set_arg);
330  argv.push_back(base_offset_arg);
331
332  std::string command_line(Join(argv, ' '));
333  LOG(INFO) << "RelocateImage: " << command_line;
334  return Exec(argv, error_msg);
335}
336
337static ImageHeader* ReadSpecificImageHeader(const char* filename, std::string* error_msg) {
338  std::unique_ptr<ImageHeader> hdr(new ImageHeader);
339  if (!ReadSpecificImageHeader(filename, hdr.get())) {
340    *error_msg = StringPrintf("Unable to read image header for %s", filename);
341    return nullptr;
342  }
343  return hdr.release();
344}
345
346ImageHeader* ImageSpace::ReadImageHeaderOrDie(const char* image_location,
347                                              const InstructionSet image_isa) {
348  std::string error_msg;
349  ImageHeader* image_header = ReadImageHeader(image_location, image_isa, &error_msg);
350  if (image_header == nullptr) {
351    LOG(FATAL) << error_msg;
352  }
353  return image_header;
354}
355
356ImageHeader* ImageSpace::ReadImageHeader(const char* image_location,
357                                         const InstructionSet image_isa,
358                                         std::string* error_msg) {
359  std::string system_filename;
360  bool has_system = false;
361  std::string cache_filename;
362  bool has_cache = false;
363  bool dalvik_cache_exists = false;
364  bool is_global_cache = false;
365  if (FindImageFilename(image_location, image_isa, &system_filename, &has_system,
366                        &cache_filename, &dalvik_cache_exists, &has_cache, &is_global_cache)) {
367    if (Runtime::Current()->ShouldRelocate()) {
368      if (has_system && has_cache) {
369        std::unique_ptr<ImageHeader> sys_hdr(new ImageHeader);
370        std::unique_ptr<ImageHeader> cache_hdr(new ImageHeader);
371        if (!ReadSpecificImageHeader(system_filename.c_str(), sys_hdr.get())) {
372          *error_msg = StringPrintf("Unable to read image header for %s at %s",
373                                    image_location, system_filename.c_str());
374          return nullptr;
375        }
376        if (!ReadSpecificImageHeader(cache_filename.c_str(), cache_hdr.get())) {
377          *error_msg = StringPrintf("Unable to read image header for %s at %s",
378                                    image_location, cache_filename.c_str());
379          return nullptr;
380        }
381        if (sys_hdr->GetOatChecksum() != cache_hdr->GetOatChecksum()) {
382          *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
383                                    image_location);
384          return nullptr;
385        }
386        return cache_hdr.release();
387      } else if (!has_cache) {
388        *error_msg = StringPrintf("Unable to find a relocated version of image file %s",
389                                  image_location);
390        return nullptr;
391      } else if (!has_system && has_cache) {
392        // This can probably just use the cache one.
393        return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
394      }
395    } else {
396      // We don't want to relocate, Just pick the appropriate one if we have it and return.
397      if (has_system && has_cache) {
398        // We want the cache if the checksum matches, otherwise the system.
399        std::unique_ptr<ImageHeader> system(ReadSpecificImageHeader(system_filename.c_str(),
400                                                                    error_msg));
401        std::unique_ptr<ImageHeader> cache(ReadSpecificImageHeader(cache_filename.c_str(),
402                                                                   error_msg));
403        if (system.get() == nullptr ||
404            (cache.get() != nullptr && cache->GetOatChecksum() == system->GetOatChecksum())) {
405          return cache.release();
406        } else {
407          return system.release();
408        }
409      } else if (has_system) {
410        return ReadSpecificImageHeader(system_filename.c_str(), error_msg);
411      } else if (has_cache) {
412        return ReadSpecificImageHeader(cache_filename.c_str(), error_msg);
413      }
414    }
415  }
416
417  *error_msg = StringPrintf("Unable to find image file for %s", image_location);
418  return nullptr;
419}
420
421static bool ChecksumsMatch(const char* image_a, const char* image_b) {
422  ImageHeader hdr_a;
423  ImageHeader hdr_b;
424  return ReadSpecificImageHeader(image_a, &hdr_a) && ReadSpecificImageHeader(image_b, &hdr_b)
425      && hdr_a.GetOatChecksum() == hdr_b.GetOatChecksum();
426}
427
428static bool ImageCreationAllowed(bool is_global_cache, std::string* error_msg) {
429  // Anyone can write into a "local" cache.
430  if (!is_global_cache) {
431    return true;
432  }
433
434  // Only the zygote is allowed to create the global boot image.
435  if (Runtime::Current()->IsZygote()) {
436    return true;
437  }
438
439  *error_msg = "Only the zygote can create the global boot image.";
440  return false;
441}
442
443static constexpr uint64_t kLowSpaceValue = 50 * MB;
444static constexpr uint64_t kTmpFsSentinelValue = 384 * MB;
445
446// Read the free space of the cache partition and make a decision whether to keep the generated
447// image. This is to try to mitigate situations where the system might run out of space later.
448static bool CheckSpace(const std::string& cache_filename, std::string* error_msg) {
449  // Using statvfs vs statvfs64 because of b/18207376, and it is enough for all practical purposes.
450  struct statvfs buf;
451
452  int res = TEMP_FAILURE_RETRY(statvfs(cache_filename.c_str(), &buf));
453  if (res != 0) {
454    // Could not stat. Conservatively tell the system to delete the image.
455    *error_msg = "Could not stat the filesystem, assuming low-memory situation.";
456    return false;
457  }
458
459  uint64_t fs_overall_size = buf.f_bsize * static_cast<uint64_t>(buf.f_blocks);
460  // Zygote is privileged, but other things are not. Use bavail.
461  uint64_t fs_free_size = buf.f_bsize * static_cast<uint64_t>(buf.f_bavail);
462
463  // Take the overall size as an indicator for a tmpfs, which is being used for the decryption
464  // environment. We do not want to fail quickening the boot image there, as it is beneficial
465  // for time-to-UI.
466  if (fs_overall_size > kTmpFsSentinelValue) {
467    if (fs_free_size < kLowSpaceValue) {
468      *error_msg = StringPrintf("Low-memory situation: only %4.2f megabytes available after image"
469                                " generation, need at least %" PRIu64 ".",
470                                static_cast<double>(fs_free_size) / MB,
471                                kLowSpaceValue / MB);
472      return false;
473    }
474  }
475  return true;
476}
477
478ImageSpace* ImageSpace::Create(const char* image_location,
479                               const InstructionSet image_isa,
480                               bool secondary_image,
481                               std::string* error_msg) {
482  std::string system_filename;
483  bool has_system = false;
484  std::string cache_filename;
485  bool has_cache = false;
486  bool dalvik_cache_exists = false;
487  bool is_global_cache = true;
488  const bool found_image = FindImageFilename(image_location, image_isa, &system_filename,
489                                             &has_system, &cache_filename, &dalvik_cache_exists,
490                                             &has_cache, &is_global_cache);
491
492  if (Runtime::Current()->IsZygote() && !secondary_image) {
493    MarkZygoteStart(image_isa, Runtime::Current()->GetZygoteMaxFailedBoots());
494  }
495
496  ImageSpace* space;
497  bool relocate = Runtime::Current()->ShouldRelocate();
498  bool can_compile = Runtime::Current()->IsImageDex2OatEnabled();
499  if (found_image) {
500    const std::string* image_filename;
501    bool is_system = false;
502    bool relocated_version_used = false;
503    if (relocate) {
504      if (!dalvik_cache_exists) {
505        *error_msg = StringPrintf("Requiring relocation for image '%s' at '%s' but we do not have "
506                                  "any dalvik_cache to find/place it in.",
507                                  image_location, system_filename.c_str());
508        return nullptr;
509      }
510      if (has_system) {
511        if (has_cache && ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
512          // We already have a relocated version
513          image_filename = &cache_filename;
514          relocated_version_used = true;
515        } else {
516          // We cannot have a relocated version, Relocate the system one and use it.
517
518          std::string reason;
519          bool success;
520
521          // Check whether we are allowed to relocate.
522          if (!can_compile) {
523            reason = "Image dex2oat disabled by -Xnoimage-dex2oat.";
524            success = false;
525          } else if (!ImageCreationAllowed(is_global_cache, &reason)) {
526            // Whether we can write to the cache.
527            success = false;
528          } else if (secondary_image) {
529            reason = "Should not have to patch secondary image.";
530            success = false;
531          } else {
532            // Try to relocate.
533            success = RelocateImage(image_location, cache_filename.c_str(), image_isa, &reason);
534          }
535
536          if (success) {
537            relocated_version_used = true;
538            image_filename = &cache_filename;
539          } else {
540            *error_msg = StringPrintf("Unable to relocate image '%s' from '%s' to '%s': %s",
541                                      image_location, system_filename.c_str(),
542                                      cache_filename.c_str(), reason.c_str());
543            // We failed to create files, remove any possibly garbage output.
544            // Since ImageCreationAllowed was true above, we are the zygote
545            // and therefore the only process expected to generate these for
546            // the device.
547            PruneDalvikCache(image_isa);
548            return nullptr;
549          }
550        }
551      } else {
552        CHECK(has_cache);
553        // We can just use cache's since it should be fine. This might or might not be relocated.
554        image_filename = &cache_filename;
555      }
556    } else {
557      if (has_system && has_cache) {
558        // Check they have the same cksum. If they do use the cache. Otherwise system.
559        if (ChecksumsMatch(system_filename.c_str(), cache_filename.c_str())) {
560          image_filename = &cache_filename;
561          relocated_version_used = true;
562        } else {
563          image_filename = &system_filename;
564          is_system = true;
565        }
566      } else if (has_system) {
567        image_filename = &system_filename;
568        is_system = true;
569      } else {
570        CHECK(has_cache);
571        image_filename = &cache_filename;
572      }
573    }
574    {
575      // Note that we must not use the file descriptor associated with
576      // ScopedFlock::GetFile to Init the image file. We want the file
577      // descriptor (and the associated exclusive lock) to be released when
578      // we leave Create.
579      ScopedFlock image_lock;
580      image_lock.Init(image_filename->c_str(), error_msg);
581      VLOG(startup) << "Using image file " << image_filename->c_str() << " for image location "
582                    << image_location;
583      // If we are in /system we can assume the image is good. We can also
584      // assume this if we are using a relocated image (i.e. image checksum
585      // matches) since this is only different by the offset. We need this to
586      // make sure that host tests continue to work.
587      space = ImageSpace::Init(image_filename->c_str(), image_location,
588                               !(is_system || relocated_version_used), error_msg);
589    }
590    if (space != nullptr) {
591      return space;
592    }
593
594    if (relocated_version_used) {
595      // Something is wrong with the relocated copy (even though checksums match). Cleanup.
596      // This can happen if the .oat is corrupt, since the above only checks the .art checksums.
597      // TODO: Check the oat file validity earlier.
598      *error_msg = StringPrintf("Attempted to use relocated version of %s at %s generated from %s "
599                                "but image failed to load: %s",
600                                image_location, cache_filename.c_str(), system_filename.c_str(),
601                                error_msg->c_str());
602      PruneDalvikCache(image_isa);
603      return nullptr;
604    } else if (is_system) {
605      // If the /system file exists, it should be up-to-date, don't try to generate it.
606      *error_msg = StringPrintf("Failed to load /system image '%s': %s",
607                                image_filename->c_str(), error_msg->c_str());
608      return nullptr;
609    } else {
610      // Otherwise, log a warning and fall through to GenerateImage.
611      LOG(WARNING) << *error_msg;
612    }
613  }
614
615  if (!can_compile) {
616    *error_msg = "Not attempting to compile image because -Xnoimage-dex2oat";
617    return nullptr;
618  } else if (!dalvik_cache_exists) {
619    *error_msg = StringPrintf("No place to put generated image.");
620    return nullptr;
621  } else if (!ImageCreationAllowed(is_global_cache, error_msg)) {
622    return nullptr;
623  } else if (secondary_image) {
624    *error_msg = "Cannot compile a secondary image.";
625    return nullptr;
626  } else if (!GenerateImage(cache_filename, image_isa, error_msg)) {
627    *error_msg = StringPrintf("Failed to generate image '%s': %s",
628                              cache_filename.c_str(), error_msg->c_str());
629    // We failed to create files, remove any possibly garbage output.
630    // Since ImageCreationAllowed was true above, we are the zygote
631    // and therefore the only process expected to generate these for
632    // the device.
633    PruneDalvikCache(image_isa);
634    return nullptr;
635  } else {
636    // Check whether there is enough space left over after we have generated the image.
637    if (!CheckSpace(cache_filename, error_msg)) {
638      // No. Delete the generated image and try to run out of the dex files.
639      PruneDalvikCache(image_isa);
640      return nullptr;
641    }
642
643    // Note that we must not use the file descriptor associated with
644    // ScopedFlock::GetFile to Init the image file. We want the file
645    // descriptor (and the associated exclusive lock) to be released when
646    // we leave Create.
647    ScopedFlock image_lock;
648    image_lock.Init(cache_filename.c_str(), error_msg);
649    space = ImageSpace::Init(cache_filename.c_str(), image_location, true, error_msg);
650    if (space == nullptr) {
651      *error_msg = StringPrintf("Failed to load generated image '%s': %s",
652                                cache_filename.c_str(), error_msg->c_str());
653    }
654    return space;
655  }
656}
657
658void ImageSpace::VerifyImageAllocations() {
659  uint8_t* current = Begin() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
660  while (current < End()) {
661    CHECK_ALIGNED(current, kObjectAlignment);
662    auto* obj = reinterpret_cast<mirror::Object*>(current);
663    CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
664    CHECK(live_bitmap_->Test(obj)) << PrettyTypeOf(obj);
665    if (kUseBakerOrBrooksReadBarrier) {
666      obj->AssertReadBarrierPointer();
667    }
668    current += RoundUp(obj->SizeOf(), kObjectAlignment);
669  }
670}
671
672ImageSpace* ImageSpace::Init(const char* image_filename, const char* image_location,
673                             bool validate_oat_file, std::string* error_msg) {
674  CHECK(image_filename != nullptr);
675  CHECK(image_location != nullptr);
676
677  uint64_t start_time = 0;
678  if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
679    start_time = NanoTime();
680    LOG(INFO) << "ImageSpace::Init entering image_filename=" << image_filename;
681  }
682
683  std::unique_ptr<File> file(OS::OpenFileForReading(image_filename));
684  if (file.get() == nullptr) {
685    *error_msg = StringPrintf("Failed to open '%s'", image_filename);
686    return nullptr;
687  }
688  ImageHeader image_header;
689  bool success = file->ReadFully(&image_header, sizeof(image_header));
690  if (!success || !image_header.IsValid()) {
691    *error_msg = StringPrintf("Invalid image header in '%s'", image_filename);
692    return nullptr;
693  }
694  // Check that the file is larger or equal to the header size + data size.
695  const uint64_t image_file_size = static_cast<uint64_t>(file->GetLength());
696  if (image_file_size < sizeof(ImageHeader) + image_header.GetDataSize()) {
697    *error_msg = StringPrintf("Image file truncated: %" PRIu64 " vs. %" PRIu64 ".",
698                              image_file_size,
699                              image_header.GetDataSize());
700    return nullptr;
701  }
702
703  if (VLOG_IS_ON(startup)) {
704    LOG(INFO) << "Dumping image sections";
705    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
706      const auto section_idx = static_cast<ImageHeader::ImageSections>(i);
707      auto& section = image_header.GetImageSection(section_idx);
708      LOG(INFO) << section_idx << " start="
709          << reinterpret_cast<void*>(image_header.GetImageBegin() + section.Offset()) << " "
710          << section;
711    }
712  }
713
714  const auto& bitmap_section = image_header.GetImageSection(ImageHeader::kSectionImageBitmap);
715  // The location we want to map from is the first aligned page after the end of the stored
716  // (possibly compressed) data.
717  const size_t image_bitmap_offset = RoundUp(sizeof(image_header) + image_header.GetDataSize(),
718                                             kPageSize);
719  const size_t end_of_bitmap = image_bitmap_offset + bitmap_section.Size();
720  if (end_of_bitmap != image_file_size) {
721    *error_msg = StringPrintf(
722        "Image file size does not equal end of bitmap: size=%" PRIu64 " vs. %zu.", image_file_size,
723        end_of_bitmap);
724    return nullptr;
725  }
726
727  // Note: The image header is part of the image due to mmap page alignment required of offset.
728  std::unique_ptr<MemMap> map;
729  if (image_header.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
730    map.reset(MemMap::MapFileAtAddress(image_header.GetImageBegin(),
731                                       image_header.GetImageSize(),
732                                       PROT_READ | PROT_WRITE,
733                                       MAP_PRIVATE,
734                                       file->Fd(),
735                                       0,
736                                       /*low_4gb*/false,
737                                       /*reuse*/false,
738                                       image_filename,
739                                       error_msg));
740  } else {
741    // Reserve output and decompress into it.
742    map.reset(MemMap::MapAnonymous(image_location,
743                                   image_header.GetImageBegin(),
744                                   image_header.GetImageSize(),
745                                   PROT_READ | PROT_WRITE,
746                                   /*low_4gb*/false,
747                                   /*reuse*/false,
748                                   error_msg));
749    if (map != nullptr) {
750      const size_t stored_size = image_header.GetDataSize();
751      const size_t write_offset = sizeof(image_header);  // Skip the header.
752      std::unique_ptr<MemMap> temp_map(MemMap::MapFile(sizeof(ImageHeader) + stored_size,
753                                                       PROT_READ,
754                                                       MAP_PRIVATE,
755                                                       file->Fd(),
756                                                       /*offset*/0,
757                                                       /*low_4gb*/false,
758                                                       image_filename,
759                                                       error_msg));
760      if (temp_map == nullptr) {
761        DCHECK(!error_msg->empty());
762        return nullptr;
763      }
764      memcpy(map->Begin(), &image_header, sizeof(image_header));
765      const uint64_t start = NanoTime();
766      const size_t decompressed_size = LZ4_decompress_safe(
767          reinterpret_cast<char*>(temp_map->Begin()) + sizeof(ImageHeader),
768          reinterpret_cast<char*>(map->Begin()) + write_offset,
769          stored_size,
770          map->Size());
771      // TODO: VLOG(image)
772      VLOG(class_linker) << "Decompressing image took " << PrettyDuration(NanoTime() - start);
773      if (decompressed_size + sizeof(ImageHeader) != image_header.GetImageSize()) {
774        *error_msg = StringPrintf("Decompressed size does not match expected image size %zu vs %zu",
775                                  decompressed_size + sizeof(ImageHeader),
776                                  image_header.GetImageSize());
777        return nullptr;
778      }
779    }
780  }
781
782  if (map == nullptr) {
783    DCHECK(!error_msg->empty());
784    return nullptr;
785  }
786  CHECK_EQ(image_header.GetImageBegin(), map->Begin());
787  DCHECK_EQ(0, memcmp(&image_header, map->Begin(), sizeof(ImageHeader)));
788
789  std::unique_ptr<MemMap> image_bitmap_map(MemMap::MapFileAtAddress(nullptr,
790                                                                    bitmap_section.Size(),
791                                                                    PROT_READ, MAP_PRIVATE,
792                                                                    file->Fd(),
793                                                                    image_bitmap_offset,
794                                                                    /*low_4gb*/false,
795                                                                    /*reuse*/false,
796                                                                    image_filename,
797                                                                    error_msg));
798  if (image_bitmap_map == nullptr) {
799    *error_msg = StringPrintf("Failed to map image bitmap: %s", error_msg->c_str());
800    return nullptr;
801  }
802  uint32_t bitmap_index = bitmap_index_.FetchAndAddSequentiallyConsistent(1);
803  std::string bitmap_name(StringPrintf("imagespace %s live-bitmap %u", image_filename,
804                                       bitmap_index));
805  // Bitmap only needs to cover until the end of the mirror objects section.
806  const ImageSection& image_objects = image_header.GetImageSection(ImageHeader::kSectionObjects);
807  std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
808      accounting::ContinuousSpaceBitmap::CreateFromMemMap(
809          bitmap_name,
810          image_bitmap_map.release(),
811          reinterpret_cast<uint8_t*>(map->Begin()),
812          image_objects.End()));
813  if (bitmap == nullptr) {
814    *error_msg = StringPrintf("Could not create bitmap '%s'", bitmap_name.c_str());
815    return nullptr;
816  }
817
818  // We only want the mirror object, not the ArtFields and ArtMethods.
819  uint8_t* const image_end =
820      map->Begin() + image_header.GetImageSection(ImageHeader::kSectionObjects).End();
821  std::unique_ptr<ImageSpace> space(new ImageSpace(image_filename,
822                                                   image_location,
823                                                   map.release(),
824                                                   bitmap.release(),
825                                                   image_end));
826
827  // VerifyImageAllocations() will be called later in Runtime::Init()
828  // as some class roots like ArtMethod::java_lang_reflect_ArtMethod_
829  // and ArtField::java_lang_reflect_ArtField_, which are used from
830  // Object::SizeOf() which VerifyImageAllocations() calls, are not
831  // set yet at this point.
832
833  space->oat_file_.reset(space->OpenOatFile(image_filename, error_msg));
834  if (space->oat_file_.get() == nullptr) {
835    DCHECK(!error_msg->empty());
836    return nullptr;
837  }
838  space->oat_file_non_owned_ = space->oat_file_.get();
839
840  if (validate_oat_file && !space->ValidateOatFile(error_msg)) {
841    DCHECK(!error_msg->empty());
842    return nullptr;
843  }
844
845  Runtime* runtime = Runtime::Current();
846  runtime->SetInstructionSet(space->oat_file_->GetOatHeader().GetInstructionSet());
847
848  if (!runtime->HasResolutionMethod()) {
849    runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod));
850    runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod));
851    runtime->SetImtUnimplementedMethod(
852        image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod));
853    runtime->SetCalleeSaveMethod(
854        image_header.GetImageMethod(ImageHeader::kCalleeSaveMethod), Runtime::kSaveAll);
855    runtime->SetCalleeSaveMethod(
856        image_header.GetImageMethod(ImageHeader::kRefsOnlySaveMethod), Runtime::kRefsOnly);
857    runtime->SetCalleeSaveMethod(
858        image_header.GetImageMethod(ImageHeader::kRefsAndArgsSaveMethod), Runtime::kRefsAndArgs);
859  }
860
861  if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
862    LOG(INFO) << "ImageSpace::Init exiting (" << PrettyDuration(NanoTime() - start_time)
863             << ") " << *space.get();
864  }
865  return space.release();
866}
867
868OatFile* ImageSpace::OpenOatFile(const char* image_path, std::string* error_msg) const {
869  const ImageHeader& image_header = GetImageHeader();
870  std::string oat_filename = ImageHeader::GetOatLocationFromImageLocation(image_path);
871
872  CHECK(image_header.GetOatDataBegin() != nullptr);
873
874  OatFile* oat_file = OatFile::Open(oat_filename,
875                                    oat_filename,
876                                    image_header.GetOatDataBegin(),
877                                    image_header.GetOatFileBegin(),
878                                    !Runtime::Current()->IsAotCompiler(),
879                                    nullptr,
880                                    error_msg);
881  if (oat_file == nullptr) {
882    *error_msg = StringPrintf("Failed to open oat file '%s' referenced from image %s: %s",
883                              oat_filename.c_str(), GetName(), error_msg->c_str());
884    return nullptr;
885  }
886  uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
887  uint32_t image_oat_checksum = image_header.GetOatChecksum();
888  if (oat_checksum != image_oat_checksum) {
889    *error_msg = StringPrintf("Failed to match oat file checksum 0x%x to expected oat checksum 0x%x"
890                              " in image %s", oat_checksum, image_oat_checksum, GetName());
891    return nullptr;
892  }
893  int32_t image_patch_delta = image_header.GetPatchDelta();
894  int32_t oat_patch_delta = oat_file->GetOatHeader().GetImagePatchDelta();
895  if (oat_patch_delta != image_patch_delta && !image_header.CompilePic()) {
896    // We should have already relocated by this point. Bail out.
897    *error_msg = StringPrintf("Failed to match oat file patch delta %d to expected patch delta %d "
898                              "in image %s", oat_patch_delta, image_patch_delta, GetName());
899    return nullptr;
900  }
901
902  return oat_file;
903}
904
905bool ImageSpace::ValidateOatFile(std::string* error_msg) const {
906  CHECK(oat_file_.get() != nullptr);
907  for (const OatFile::OatDexFile* oat_dex_file : oat_file_->GetOatDexFiles()) {
908    const std::string& dex_file_location = oat_dex_file->GetDexFileLocation();
909    uint32_t dex_file_location_checksum;
910    if (!DexFile::GetChecksum(dex_file_location.c_str(), &dex_file_location_checksum, error_msg)) {
911      *error_msg = StringPrintf("Failed to get checksum of dex file '%s' referenced by image %s: "
912                                "%s", dex_file_location.c_str(), GetName(), error_msg->c_str());
913      return false;
914    }
915    if (dex_file_location_checksum != oat_dex_file->GetDexFileLocationChecksum()) {
916      *error_msg = StringPrintf("ValidateOatFile found checksum mismatch between oat file '%s' and "
917                                "dex file '%s' (0x%x != 0x%x)",
918                                oat_file_->GetLocation().c_str(), dex_file_location.c_str(),
919                                oat_dex_file->GetDexFileLocationChecksum(),
920                                dex_file_location_checksum);
921      return false;
922    }
923  }
924  return true;
925}
926
927const OatFile* ImageSpace::GetOatFile() const {
928  return oat_file_non_owned_;
929}
930
931std::unique_ptr<const OatFile> ImageSpace::ReleaseOatFile() {
932  CHECK(oat_file_ != nullptr);
933  return std::move(oat_file_);
934}
935
936void ImageSpace::Dump(std::ostream& os) const {
937  os << GetType()
938      << " begin=" << reinterpret_cast<void*>(Begin())
939      << ",end=" << reinterpret_cast<void*>(End())
940      << ",size=" << PrettySize(Size())
941      << ",name=\"" << GetName() << "\"]";
942}
943
944void ImageSpace::CreateMultiImageLocations(const std::string& input_image_file_name,
945                                           const std::string& boot_classpath,
946                                           std::vector<std::string>* image_file_names) {
947  DCHECK(image_file_names != nullptr);
948
949  std::vector<std::string> images;
950  Split(boot_classpath, ':', &images);
951
952  // Add the rest into the list. We have to adjust locations, possibly:
953  //
954  // For example, image_file_name is /a/b/c/d/e.art
955  //              images[0] is          f/c/d/e.art
956  // ----------------------------------------------
957  //              images[1] is          g/h/i/j.art  -> /a/b/h/i/j.art
958
959  // Derive pattern.
960  std::vector<std::string> left;
961  Split(input_image_file_name, '/', &left);
962  std::vector<std::string> right;
963  Split(images[0], '/', &right);
964
965  size_t common = 1;
966  while (common < left.size() && common < right.size()) {
967    if (left[left.size() - common - 1] != right[right.size() - common - 1]) {
968      break;
969    }
970    common++;
971  }
972
973  std::vector<std::string> prefix_vector(left.begin(), left.end() - common);
974  std::string common_prefix = Join(prefix_vector, '/');
975  if (!common_prefix.empty() && common_prefix[0] != '/' && input_image_file_name[0] == '/') {
976    common_prefix = "/" + common_prefix;
977  }
978
979  // Apply pattern to images[1] .. images[n].
980  for (size_t i = 1; i < images.size(); ++i) {
981    std::string image = images[i];
982
983    size_t rslash = std::string::npos;
984    for (size_t j = 0; j < common; ++j) {
985      if (rslash != std::string::npos) {
986        rslash--;
987      }
988
989      rslash = image.rfind('/', rslash);
990      if (rslash == std::string::npos) {
991        rslash = 0;
992      }
993      if (rslash == 0) {
994        break;
995      }
996    }
997    std::string image_part = image.substr(rslash);
998
999    std::string new_image = common_prefix + (StartsWith(image_part, "/") ? "" : "/") +
1000        image_part;
1001    image_file_names->push_back(new_image);
1002  }
1003}
1004
1005}  // namespace space
1006}  // namespace gc
1007}  // namespace art
1008