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