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