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