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