< prev index next >

src/hotspot/share/memory/filemap.cpp

Print this page




 142   } else {
 143     // Get the hash value.  Use a static seed because the hash needs to return the same
 144     // value over multiple jvm invocations.
 145     unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
 146 
 147     // Truncate the ident, saving room for the 8 hex character hash value.
 148     strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
 149 
 150     // Append the hash code as eight hex digits.
 151     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
 152     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
 153   }
 154 }
 155 
 156 FileMapInfo::FileMapInfo() {
 157   assert(_current_info == NULL, "must be singleton"); // not thread safe
 158   _current_info = this;
 159   memset((void*)this, 0, sizeof(FileMapInfo));
 160   _file_offset = 0;
 161   _file_open = false;
 162   _header = new FileMapHeader();
 163   _header->_version = _invalid_version;
 164   _header->_has_platform_or_app_classes = true;
 165 }
 166 
 167 FileMapInfo::~FileMapInfo() {
 168   assert(_current_info == this, "must be singleton"); // not thread safe
 169   _current_info = NULL;
 170 }
 171 
 172 void FileMapInfo::populate_header(size_t alignment) {
 173   _header->populate(this, alignment);
 174 }
 175 
 176 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
 177   _magic = 0xf00baba2;
 178   _version = _current_version;
 179   _alignment = alignment;
 180   _obj_alignment = ObjectAlignmentInBytes;
 181   _compact_strings = CompactStrings;
 182   _narrow_oop_mode = Universe::narrow_oop_mode();
 183   _narrow_oop_base = Universe::narrow_oop_base();
 184   _narrow_oop_shift = Universe::narrow_oop_shift();
 185   _max_heap_size = MaxHeapSize;
 186   _narrow_klass_base = Universe::narrow_klass_base();
 187   _narrow_klass_shift = Universe::narrow_klass_shift();
 188   _shared_path_table_size = mapinfo->_shared_path_table_size;
 189   _shared_path_table = mapinfo->_shared_path_table;
 190   _shared_path_entry_size = mapinfo->_shared_path_entry_size;
 191 
 192   // The following fields are for sanity checks for whether this archive
 193   // will function correctly with this JVM and the bootclasspath it's
 194   // invoked with.
 195 
 196   // JVM version string ... changes on each build.
 197   get_header_version(_jvm_ident);
 198 


 476       }
 477     } else if (i >= module_paths_start_index) {
 478       if (shared_path(i)->validate(false /* not a class path entry */)) {
 479         log_info(class, path)("ok");
 480       }
 481     } else if (!PrintSharedArchiveAndExit) {
 482       _validating_shared_path_table = false;
 483       _shared_path_table = NULL;
 484       _shared_path_table_size = 0;
 485       return false;
 486     }
 487   }
 488 
 489   _validating_shared_path_table = false;
 490   return true;
 491 }
 492 
 493 // Read the FileMapInfo information from the file.
 494 
 495 bool FileMapInfo::init_from_file(int fd) {
 496   size_t sz = _header->data_size();
 497   char* addr = _header->data();
 498   size_t n = os::read(fd, addr, (unsigned int)sz);
 499   if (n != sz) {
 500     fail_continue("Unable to read the file header.");
 501     return false;
 502   }
 503   if (_header->_version != current_version()) {
 504     fail_continue("The shared archive file has the wrong version.");
 505     return false;
 506   }
 507   _file_offset = (long)n;
 508 
 509   size_t info_size = _header->_paths_misc_info_size;
 510   _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
 511   if (_paths_misc_info == NULL) {
 512     fail_continue("Unable to read the file header.");
 513     return false;
 514   }
 515   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
 516   if (n != info_size) {
 517     fail_continue("Unable to read the shared path info header.");
 518     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 519     _paths_misc_info = NULL;
 520     return false;
 521   }
 522 
 523   size_t len = lseek(fd, 0, SEEK_END);
 524   struct FileMapInfo::FileMapHeader::space_info* si =
 525     &_header->_space[MetaspaceShared::last_valid_region];
 526   // The last space might be empty
 527   if (si->_file_offset > len || len - si->_file_offset < si->_used) {
 528     fail_continue("The shared archive file has been truncated.");
 529     return false;
 530   }
 531 
 532   _file_offset += (long)n;
 533   return true;
 534 }
 535 
 536 
 537 // Read the FileMapInfo information from the file.
 538 bool FileMapInfo::open_for_read() {
 539   _full_path = Arguments::GetSharedArchivePath();
 540   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
 541   if (fd < 0) {
 542     if (errno == ENOENT) {
 543       // Not locating the shared archive is ok.
 544       fail_continue("Specified shared archive not found.");
 545     } else {


 574   remove(_full_path);
 575   int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
 576   if (fd < 0) {
 577     fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
 578               os::strerror(errno));
 579   }
 580   _fd = fd;
 581   _file_offset = 0;
 582   _file_open = true;
 583 }
 584 
 585 
 586 // Write the header to the file, seek to the next allocation boundary.
 587 
 588 void FileMapInfo::write_header() {
 589   int info_size = ClassLoader::get_shared_paths_misc_info_size();
 590 
 591   _header->_paths_misc_info_size = info_size;
 592 
 593   align_file_position();
 594   size_t sz = _header->data_size();
 595   char* addr = _header->data();
 596   write_bytes(addr, (int)sz); // skip the C++ vtable
 597   write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
 598   align_file_position();
 599 }
 600 
 601 
 602 // Dump region to file.
 603 
 604 void FileMapInfo::write_region(int region, char* base, size_t size,
 605                                bool read_only, bool allow_exec) {
 606   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
 607 
 608   if (_file_open) {
 609     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 610     log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
 611                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
 612                   region, size, p2i(base), _file_offset);
 613   } else {
 614     si->_file_offset = _file_offset;
 615   }
 616   if (MetaspaceShared::is_heap_region(region)) {
 617     assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
 618     if (base != NULL) {
 619       si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
 620     } else {
 621       si->_addr._offset = 0;
 622     }
 623   } else {
 624     si->_addr._base = base;
 625   }
 626   si->_used = size;
 627   si->_read_only = read_only;
 628   si->_allow_exec = allow_exec;
 629   si->_crc = ClassLoader::crc32(0, base, (jint)size);
 630   if (base != NULL) {
 631     write_bytes_aligned(base, (int)size);
 632   }
 633 }
 634 
 635 // Write out the given archive heap memory regions.  GC code combines multiple
 636 // consecutive archive GC regions into one MemRegion whenever possible and
 637 // produces the 'heap_mem' array.
 638 //
 639 // If the archive heap memory size is smaller than a single dump time GC region
 640 // size, there is only one MemRegion in the array.
 641 //
 642 // If the archive heap memory size is bigger than one dump time GC region size,
 643 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
 644 // the first/bottom archive GC region is a partial GC region (with the empty
 645 // portion at the higher address within the region), one MemRegion is used for
 646 // the bottom partial archive GC region. The rest of the consecutive archive
 647 // GC regions are combined into another MemRegion.
 648 //
 649 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
 650 //   + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
 651 //   + We have 1 or 2 consolidated heap memory regions: r0 and r1


 677   for (int i = first_region_id, arr_idx = 0;
 678            i < first_region_id + max_num_regions;
 679            i++, arr_idx++) {
 680     char* start = NULL;
 681     size_t size = 0;
 682     if (arr_idx < arr_len) {
 683       start = (char*)heap_mem->at(arr_idx).start();
 684       size = heap_mem->at(arr_idx).byte_size();
 685       total_size += size;
 686     }
 687 
 688     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
 689                   i, p2i(start), p2i(start + size), size);
 690     write_region(i, start, size, false, false);
 691   }
 692   return total_size;
 693 }
 694 
 695 // Dump bytes to file -- at the current file position.
 696 
 697 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
 698   if (_file_open) {
 699     int n = ::write(_fd, buffer, nbytes);
 700     if (n != nbytes) {
 701       // It is dangerous to leave the corrupted shared archive file around,
 702       // close and remove the file. See bug 6372906.
 703       close();
 704       remove(_full_path);
 705       fail_stop("Unable to write to shared archive file.");
 706     }
 707   }
 708   _file_offset += nbytes;
 709 }
 710 
 711 
 712 // Align file position to an allocation unit boundary.
 713 
 714 void FileMapInfo::align_file_position() {
 715   size_t new_file_offset = align_up(_file_offset,
 716                                          os::vm_allocation_granularity());
 717   if (new_file_offset != _file_offset) {
 718     _file_offset = new_file_offset;
 719     if (_file_open) {
 720       // Seek one byte back from the target and write a byte to insure
 721       // that the written file is the correct length.
 722       _file_offset -= 1;
 723       if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
 724         fail_stop("Unable to seek.");
 725       }
 726       char zero = 0;
 727       write_bytes(&zero, 1);
 728     }
 729   }
 730 }
 731 
 732 
 733 // Dump bytes to file -- at the current file position.
 734 
 735 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
 736   align_file_position();
 737   write_bytes(buffer, nbytes);
 738   align_file_position();
 739 }
 740 
 741 
 742 // Close the shared archive file.  This does NOT unmap mapped regions.
 743 
 744 void FileMapInfo::close() {
 745   if (_file_open) {
 746     if (::close(_fd) < 0) {
 747       fail_stop("Unable to close the shared archive file.");
 748     }
 749     _file_open = false;
 750     _fd = -1;
 751   }
 752 }
 753 
 754 
 755 // JVM/TI RedefineClasses() support:
 756 // Remap the shared readonly space to shared readwrite, private.
 757 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
 758   int idx = MetaspaceShared::ro;
 759   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx];
 760   if (!si->_read_only) {
 761     // the space is already readwrite so we are done
 762     return true;
 763   }
 764   size_t used = si->_used;
 765   size_t size = align_up(used, os::vm_allocation_granularity());
 766   if (!open_for_read()) {
 767     return false;
 768   }
 769   char *addr = _header->region_addr(idx);
 770   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
 771                                 addr, size, false /* !read_only */,
 772                                 si->_allow_exec);
 773   close();
 774   if (base == NULL) {
 775     fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
 776     return false;
 777   }
 778   if (base != addr) {
 779     fail_continue("Unable to remap shared readonly space at required address.");


 791   // Reserve the space first, then map otherwise map will go right over some
 792   // other reserved memory (like the code cache).
 793   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 794   if (!rs.is_reserved()) {
 795     fail_continue("Unable to reserve shared space at required address "
 796                   INTPTR_FORMAT, p2i(requested_addr));
 797     return rs;
 798   }
 799   // the reserved virtual memory is for mapping class data sharing archive
 800   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 801 
 802   return rs;
 803 }
 804 
 805 // Memory map a region in the address space.
 806 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
 807                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
 808 
 809 char* FileMapInfo::map_region(int i, char** top_ret) {
 810   assert(!MetaspaceShared::is_heap_region(i), "sanity");
 811   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
 812   size_t used = si->_used;
 813   size_t alignment = os::vm_allocation_granularity();
 814   size_t size = align_up(used, alignment);
 815   char *requested_addr = _header->region_addr(i);
 816 
 817   // If a tool agent is in use (debugging enabled), we must map the address space RW
 818   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
 819     si->_read_only = false;
 820   }
 821 
 822   // map the contents of the CDS archive in this memory
 823   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 824                               requested_addr, size, si->_read_only,
 825                               si->_allow_exec);
 826   if (base == NULL || base != requested_addr) {
 827     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 828     return NULL;
 829   }
 830 #ifdef _WINDOWS
 831   // This call is Windows-only because the memory_type gets recorded for the other platforms


 858 //
 859 // The open archive heap objects are mapped below the shared strings in
 860 // the runtime java heap. The mapped open archive heap data only contain
 861 // references to the shared strings and open archive objects initially.
 862 // During runtime execution, out-going references to any other java heap
 863 // regions may be added. GC may mark and update references in the mapped
 864 // open archive objects.
 865 void FileMapInfo::map_heap_regions() {
 866   if (MetaspaceShared::is_heap_object_archiving_allowed()) {
 867       log_info(cds)("Archived narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
 868                     narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
 869       log_info(cds)("Archived narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 870                     p2i(narrow_klass_base()), narrow_klass_shift());
 871 
 872     // Check that all the narrow oop and klass encodings match the archive
 873     if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
 874         narrow_oop_base() != Universe::narrow_oop_base() ||
 875         narrow_oop_shift() != Universe::narrow_oop_shift() ||
 876         narrow_klass_base() != Universe::narrow_klass_base() ||
 877         narrow_klass_shift() != Universe::narrow_klass_shift()) {
 878       if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
 879         log_info(cds)("Cached heap data from the CDS archive is being ignored. "
 880                       "The current CompressedOops/CompressedClassPointers encoding differs from "
 881                       "that archived due to heap size change. The archive was dumped using max heap "
 882                       "size " UINTX_FORMAT "M.", max_heap_size()/M);
 883         log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
 884                       Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()),
 885                       Universe::narrow_oop_shift());
 886         log_info(cds)("Current narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 887                       p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
 888       }
 889     } else {
 890       // First, map string regions as closed archive heap regions.
 891       // GC does not write into the regions.
 892       if (map_heap_data(&string_ranges,
 893                          MetaspaceShared::first_string,
 894                          MetaspaceShared::max_strings,
 895                          &num_string_ranges)) {
 896         StringTable::set_shared_string_mapped();
 897 
 898         // Now, map open_archive heap regions, GC can write into the regions.
 899         if (map_heap_data(&open_archive_heap_ranges,
 900                           MetaspaceShared::first_open_archive_heap_region,
 901                           MetaspaceShared::max_open_archive_heap_region,
 902                           &num_open_archive_heap_ranges,
 903                           true /* open */)) {
 904           MetaspaceShared::set_open_archive_heap_region_mapped();
 905         }
 906       }
 907     }
 908   } else {
 909     if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
 910       log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
 911                     "UseCompressedOops and UseCompressedClassPointers are required.");
 912     }
 913   }
 914 
 915   if (!StringTable::shared_string_mapped()) {
 916     assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
 917   }
 918 
 919   if (!MetaspaceShared::open_archive_heap_region_mapped()) {
 920     assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
 921   }
 922 }
 923 
 924 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
 925                                 int max, int* num, bool is_open_archive) {
 926   MemRegion * regions = new MemRegion[max];
 927   struct FileMapInfo::FileMapHeader::space_info* si;
 928   int region_num = 0;
 929 
 930   for (int i = first;
 931            i < first + max; i++) {
 932     si = &_header->_space[i];
 933     size_t used = si->_used;
 934     if (used > 0) {
 935       size_t size = used;
 936       char* requested_addr = (char*)((void*)CompressedOops::decode_not_null(
 937                                             (narrowOop)si->_addr._offset));
 938       regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
 939       region_num ++;
 940     }
 941   }
 942 
 943   if (region_num == 0) {
 944     return false; // no archived java heap data
 945   }
 946 
 947   // Check that ranges are within the java heap
 948   if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
 949     log_info(cds)("UseSharedSpaces: Unable to allocate region, "
 950                   "range is not within java heap.");
 951     return false;
 952   }
 953 
 954   // allocate from java heap
 955   if (!G1CollectedHeap::heap()->alloc_archive_regions(
 956              regions, region_num, is_open_archive)) {
 957     log_info(cds)("UseSharedSpaces: Unable to allocate region, "
 958                   "java heap range is already in use.");
 959     return false;
 960   }
 961 
 962   // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
 963   // for mapped regions as they are part of the reserved java heap, which is
 964   // already recorded.
 965   for (int i = 0; i < region_num; i++) {
 966     si = &_header->_space[first + i];
 967     char* addr = (char*)regions[i].start();
 968     char* base = os::map_memory(_fd, _full_path, si->_file_offset,
 969                                 addr, regions[i].byte_size(), si->_read_only,
 970                                 si->_allow_exec);
 971     if (base == NULL || base != addr) {
 972       // dealloc the regions from java heap
 973       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
 974       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap.");
 975       return false;
 976     }
 977   }
 978 
 979   if (!verify_mapped_heap_regions(first, region_num)) {
 980     // dealloc the regions from java heap
 981     dealloc_archive_heap_regions(regions, region_num, is_open_archive);
 982     log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
 983     return false;
 984   }
 985 
 986   // the shared heap data is mapped successfully


1013     G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
1014                                                   num_open_archive_heap_ranges);
1015   }
1016 }
1017 
1018 // dealloc the archive regions from java heap
1019 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) {
1020   if (num > 0) {
1021     assert(regions != NULL, "Null archive ranges array with non-zero count");
1022     G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open);
1023   }
1024 }
1025 #endif // INCLUDE_CDS_JAVA_HEAP
1026 
1027 bool FileMapInfo::verify_region_checksum(int i) {
1028   assert(i >= 0 && i < MetaspaceShared::n_regions, "invalid region");
1029   if (!VerifySharedSpaces) {
1030     return true;
1031   }
1032 
1033   size_t sz = _header->_space[i]._used;
1034 
1035   if (sz == 0) {
1036     return true; // no data
1037   }
1038   if ((MetaspaceShared::is_string_region(i) &&
1039        !StringTable::shared_string_mapped()) ||
1040       (MetaspaceShared::is_open_archive_heap_region(i) &&
1041        !MetaspaceShared::open_archive_heap_region_mapped())) {
1042     return true; // archived heap data is not mapped
1043   }
1044   const char* buf = _header->region_addr(i);
1045   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1046   if (crc != _header->_space[i]._crc) {
1047     fail_continue("Checksum verification failed.");
1048     return false;
1049   }
1050   return true;
1051 }
1052 
1053 // Unmap a memory region in the address space.
1054 
1055 void FileMapInfo::unmap_region(int i) {
1056   assert(!MetaspaceShared::is_heap_region(i), "sanity");
1057   struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
1058   size_t used = si->_used;
1059   size_t size = align_up(used, os::vm_allocation_granularity());
1060 
1061   if (used == 0) {
1062     return;
1063   }
1064 
1065   char* addr = _header->region_addr(i);
1066   if (!os::unmap_memory(addr, size)) {
1067     fail_stop("Unable to unmap shared space.");
1068   }
1069 }
1070 
1071 void FileMapInfo::assert_mark(bool check) {
1072   if (!check) {
1073     fail_stop("Mark mismatch while restoring from shared file.");
1074   }
1075 }
1076 
1077 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {


1095 //
1096 // Validation of the archive is done in two steps:
1097 //
1098 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
1099 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1100 //     region of the archive, which is not mapped yet.
1101 bool FileMapInfo::initialize() {
1102   assert(UseSharedSpaces, "UseSharedSpaces expected.");
1103 
1104   if (!open_for_read()) {
1105     return false;
1106   }
1107 
1108   init_from_file(_fd);
1109   if (!validate_header()) {
1110     return false;
1111   }
1112   return true;
1113 }
1114 
1115 char* FileMapInfo::FileMapHeader::region_addr(int idx) {
1116   if (MetaspaceShared::is_heap_region(idx)) {
1117     return _space[idx]._used > 0 ?
1118              (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
1119   } else {
1120     return _space[idx]._addr._base;
1121   }
1122 }
1123 
1124 int FileMapInfo::FileMapHeader::compute_crc() {
1125   char* header = data();
1126   // start computing from the field after _crc
1127   char* buf = (char*)&_crc + sizeof(int);
1128   size_t sz = data_size() - (buf - header);
1129   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1130   return crc;
1131 }
1132 
1133 // This function should only be called during run time with UseSharedSpaces enabled.
1134 bool FileMapInfo::FileMapHeader::validate() {
1135   if (VerifySharedSpaces && compute_crc() != _crc) {
1136     fail_continue("Header checksum verification failed.");
1137     return false;
1138   }
1139 
1140   if (!Arguments::has_jimage()) {
1141     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
1142     return false;
1143   }
1144 
1145   if (_version != current_version()) {
1146     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
1147     return false;
1148   }
1149   if (_magic != (int)0xf00baba2) {
1150     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
1151     return false;
1152   }
1153   char header_version[JVM_IDENT_MAX];
1154   get_header_version(header_version);
1155   if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
1156     log_info(class, path)("expected: %s", header_version);
1157     log_info(class, path)("actual:   %s", _jvm_ident);
1158     FileMapInfo::fail_continue("The shared archive file was created by a different"
1159                   " version or build of HotSpot");
1160     return false;
1161   }
1162   if (_obj_alignment != ObjectAlignmentInBytes) {
1163     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
1164                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
1165                   _obj_alignment, ObjectAlignmentInBytes);
1166     return false;
1167   }
1168   if (_compact_strings != CompactStrings) {
1169     FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"


1205         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
1206         status = false;
1207       }
1208     }
1209   }
1210 
1211   if (_paths_misc_info != NULL) {
1212     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
1213     _paths_misc_info = NULL;
1214   }
1215   return status;
1216 }
1217 
1218 // Check if a given address is within one of the shared regions
1219 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1220   assert(idx == MetaspaceShared::ro ||
1221          idx == MetaspaceShared::rw ||
1222          idx == MetaspaceShared::mc ||
1223          idx == MetaspaceShared::md, "invalid region index");
1224   char* base = _header->region_addr(idx);
1225   if (p >= base && p < base + _header->_space[idx]._used) {
1226     return true;
1227   }
1228   return false;
1229 }
1230 
1231 void FileMapInfo::print_shared_spaces() {
1232   tty->print_cr("Shared Spaces:");
1233   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
1234     struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
1235     char *base = _header->region_addr(i);
1236     tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
1237                         shared_region_name[i],
1238                         p2i(base), p2i(base + si->_used));
1239   }
1240 }
1241 
1242 // Unmap mapped regions of shared space.
1243 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1244   MetaspaceObj::set_shared_metaspace_range(NULL, NULL);
1245 
1246   FileMapInfo *map_info = FileMapInfo::current_info();
1247   if (map_info) {
1248     map_info->fail_continue("%s", msg);
1249     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
1250       char *addr = map_info->_header->region_addr(i);
1251       if (addr != NULL && !MetaspaceShared::is_heap_region(i)) {
1252         map_info->unmap_region(i);
1253         map_info->_header->_space[i]._addr._base = NULL;
1254       }
1255     }
1256     // Dealloc the archive heap regions only without unmapping. The regions are part
1257     // of the java heap. Unmapping of the heap regions are managed by GC.
1258     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1259                                            num_open_archive_heap_ranges,
1260                                            true);
1261     map_info->dealloc_archive_heap_regions(string_ranges,
1262                                            num_string_ranges,
1263                                            false);
1264   } else if (DumpSharedSpaces) {
1265     fail_stop("%s", msg);
1266   }
1267 }


 142   } else {
 143     // Get the hash value.  Use a static seed because the hash needs to return the same
 144     // value over multiple jvm invocations.
 145     unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
 146 
 147     // Truncate the ident, saving room for the 8 hex character hash value.
 148     strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
 149 
 150     // Append the hash code as eight hex digits.
 151     sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
 152     header_version[JVM_IDENT_MAX-1] = 0;  // Null terminate.
 153   }
 154 }
 155 
 156 FileMapInfo::FileMapInfo() {
 157   assert(_current_info == NULL, "must be singleton"); // not thread safe
 158   _current_info = this;
 159   memset((void*)this, 0, sizeof(FileMapInfo));
 160   _file_offset = 0;
 161   _file_open = false;
 162   _header = (FileMapHeader*)os::malloc(sizeof(FileMapHeader), mtInternal);
 163   _header->_version = INVALID_CDS_ARCHIVE_VERSION;
 164   _header->_has_platform_or_app_classes = true;
 165 }
 166 
 167 FileMapInfo::~FileMapInfo() {
 168   assert(_current_info == this, "must be singleton"); // not thread safe
 169   _current_info = NULL;
 170 }
 171 
 172 void FileMapInfo::populate_header(size_t alignment) {
 173   _header->populate(this, alignment);
 174 }
 175 
 176 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
 177   _magic = CDS_ARCHIVE_MAGIC;
 178   _version = CURRENT_CDS_ARCHIVE_VERSION;
 179   _alignment = alignment;
 180   _obj_alignment = ObjectAlignmentInBytes;
 181   _compact_strings = CompactStrings;
 182   _narrow_oop_mode = Universe::narrow_oop_mode();
 183   _narrow_oop_base = Universe::narrow_oop_base();
 184   _narrow_oop_shift = Universe::narrow_oop_shift();
 185   _max_heap_size = MaxHeapSize;
 186   _narrow_klass_base = Universe::narrow_klass_base();
 187   _narrow_klass_shift = Universe::narrow_klass_shift();
 188   _shared_path_table_size = mapinfo->_shared_path_table_size;
 189   _shared_path_table = mapinfo->_shared_path_table;
 190   _shared_path_entry_size = mapinfo->_shared_path_entry_size;
 191 
 192   // The following fields are for sanity checks for whether this archive
 193   // will function correctly with this JVM and the bootclasspath it's
 194   // invoked with.
 195 
 196   // JVM version string ... changes on each build.
 197   get_header_version(_jvm_ident);
 198 


 476       }
 477     } else if (i >= module_paths_start_index) {
 478       if (shared_path(i)->validate(false /* not a class path entry */)) {
 479         log_info(class, path)("ok");
 480       }
 481     } else if (!PrintSharedArchiveAndExit) {
 482       _validating_shared_path_table = false;
 483       _shared_path_table = NULL;
 484       _shared_path_table_size = 0;
 485       return false;
 486     }
 487   }
 488 
 489   _validating_shared_path_table = false;
 490   return true;
 491 }
 492 
 493 // Read the FileMapInfo information from the file.
 494 
 495 bool FileMapInfo::init_from_file(int fd) {
 496   size_t sz = sizeof(FileMapHeader);
 497   size_t n = os::read(fd, _header, (unsigned int)sz);

 498   if (n != sz) {
 499     fail_continue("Unable to read the file header.");
 500     return false;
 501   }
 502   if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
 503     fail_continue("The shared archive file has the wrong version.");
 504     return false;
 505   }
 506   _file_offset = (long)n;
 507 
 508   size_t info_size = _header->_paths_misc_info_size;
 509   _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
 510   if (_paths_misc_info == NULL) {
 511     fail_continue("Unable to read the file header.");
 512     return false;
 513   }
 514   n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
 515   if (n != info_size) {
 516     fail_continue("Unable to read the shared path info header.");
 517     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
 518     _paths_misc_info = NULL;
 519     return false;
 520   }
 521 
 522   size_t len = lseek(fd, 0, SEEK_END);
 523   CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);

 524   // The last space might be empty
 525   if (si->_file_offset > len || len - si->_file_offset < si->_used) {
 526     fail_continue("The shared archive file has been truncated.");
 527     return false;
 528   }
 529 
 530   _file_offset += (long)n;
 531   return true;
 532 }
 533 
 534 
 535 // Read the FileMapInfo information from the file.
 536 bool FileMapInfo::open_for_read() {
 537   _full_path = Arguments::GetSharedArchivePath();
 538   int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
 539   if (fd < 0) {
 540     if (errno == ENOENT) {
 541       // Not locating the shared archive is ok.
 542       fail_continue("Specified shared archive not found.");
 543     } else {


 572   remove(_full_path);
 573   int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
 574   if (fd < 0) {
 575     fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
 576               os::strerror(errno));
 577   }
 578   _fd = fd;
 579   _file_offset = 0;
 580   _file_open = true;
 581 }
 582 
 583 
 584 // Write the header to the file, seek to the next allocation boundary.
 585 
 586 void FileMapInfo::write_header() {
 587   int info_size = ClassLoader::get_shared_paths_misc_info_size();
 588 
 589   _header->_paths_misc_info_size = info_size;
 590 
 591   align_file_position();
 592   write_bytes(_header, sizeof(FileMapHeader));
 593   write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);


 594   align_file_position();
 595 }
 596 
 597 
 598 // Dump region to file.
 599 
 600 void FileMapInfo::write_region(int region, char* base, size_t size,
 601                                bool read_only, bool allow_exec) {
 602   CDSFileMapRegion* si = space_at(region);
 603 
 604   if (_file_open) {
 605     guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
 606     log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
 607                   " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
 608                   region, size, p2i(base), _file_offset);
 609   } else {
 610     si->_file_offset = _file_offset;
 611   }
 612   if (MetaspaceShared::is_heap_region(region)) {
 613     assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
 614     if (base != NULL) {
 615       si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
 616     } else {
 617       si->_addr._offset = 0;
 618     }
 619   } else {
 620     si->_addr._base = base;
 621   }
 622   si->_used = size;
 623   si->_read_only = read_only;
 624   si->_allow_exec = allow_exec;
 625   si->_crc = ClassLoader::crc32(0, base, (jint)size);
 626   if (base != NULL) {
 627     write_bytes_aligned(base, size);
 628   }
 629 }
 630 
 631 // Write out the given archive heap memory regions.  GC code combines multiple
 632 // consecutive archive GC regions into one MemRegion whenever possible and
 633 // produces the 'heap_mem' array.
 634 //
 635 // If the archive heap memory size is smaller than a single dump time GC region
 636 // size, there is only one MemRegion in the array.
 637 //
 638 // If the archive heap memory size is bigger than one dump time GC region size,
 639 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
 640 // the first/bottom archive GC region is a partial GC region (with the empty
 641 // portion at the higher address within the region), one MemRegion is used for
 642 // the bottom partial archive GC region. The rest of the consecutive archive
 643 // GC regions are combined into another MemRegion.
 644 //
 645 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
 646 //   + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
 647 //   + We have 1 or 2 consolidated heap memory regions: r0 and r1


 673   for (int i = first_region_id, arr_idx = 0;
 674            i < first_region_id + max_num_regions;
 675            i++, arr_idx++) {
 676     char* start = NULL;
 677     size_t size = 0;
 678     if (arr_idx < arr_len) {
 679       start = (char*)heap_mem->at(arr_idx).start();
 680       size = heap_mem->at(arr_idx).byte_size();
 681       total_size += size;
 682     }
 683 
 684     log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
 685                   i, p2i(start), p2i(start + size), size);
 686     write_region(i, start, size, false, false);
 687   }
 688   return total_size;
 689 }
 690 
 691 // Dump bytes to file -- at the current file position.
 692 
 693 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
 694   if (_file_open) {
 695     size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
 696     if (n != nbytes) {
 697       // It is dangerous to leave the corrupted shared archive file around,
 698       // close and remove the file. See bug 6372906.
 699       close();
 700       remove(_full_path);
 701       fail_stop("Unable to write to shared archive file.");
 702     }
 703   }
 704   _file_offset += nbytes;
 705 }
 706 
 707 
 708 // Align file position to an allocation unit boundary.
 709 
 710 void FileMapInfo::align_file_position() {
 711   size_t new_file_offset = align_up(_file_offset,
 712                                          os::vm_allocation_granularity());
 713   if (new_file_offset != _file_offset) {
 714     _file_offset = new_file_offset;
 715     if (_file_open) {
 716       // Seek one byte back from the target and write a byte to insure
 717       // that the written file is the correct length.
 718       _file_offset -= 1;
 719       if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
 720         fail_stop("Unable to seek.");
 721       }
 722       char zero = 0;
 723       write_bytes(&zero, 1);
 724     }
 725   }
 726 }
 727 
 728 
 729 // Dump bytes to file -- at the current file position.
 730 
 731 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
 732   align_file_position();
 733   write_bytes(buffer, nbytes);
 734   align_file_position();
 735 }
 736 
 737 
 738 // Close the shared archive file.  This does NOT unmap mapped regions.
 739 
 740 void FileMapInfo::close() {
 741   if (_file_open) {
 742     if (::close(_fd) < 0) {
 743       fail_stop("Unable to close the shared archive file.");
 744     }
 745     _file_open = false;
 746     _fd = -1;
 747   }
 748 }
 749 
 750 
 751 // JVM/TI RedefineClasses() support:
 752 // Remap the shared readonly space to shared readwrite, private.
 753 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
 754   int idx = MetaspaceShared::ro;
 755   CDSFileMapRegion* si = space_at(idx);
 756   if (!si->_read_only) {
 757     // the space is already readwrite so we are done
 758     return true;
 759   }
 760   size_t used = si->_used;
 761   size_t size = align_up(used, os::vm_allocation_granularity());
 762   if (!open_for_read()) {
 763     return false;
 764   }
 765   char *addr = _header->region_addr(idx);
 766   char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
 767                                 addr, size, false /* !read_only */,
 768                                 si->_allow_exec);
 769   close();
 770   if (base == NULL) {
 771     fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
 772     return false;
 773   }
 774   if (base != addr) {
 775     fail_continue("Unable to remap shared readonly space at required address.");


 787   // Reserve the space first, then map otherwise map will go right over some
 788   // other reserved memory (like the code cache).
 789   ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
 790   if (!rs.is_reserved()) {
 791     fail_continue("Unable to reserve shared space at required address "
 792                   INTPTR_FORMAT, p2i(requested_addr));
 793     return rs;
 794   }
 795   // the reserved virtual memory is for mapping class data sharing archive
 796   MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
 797 
 798   return rs;
 799 }
 800 
 801 // Memory map a region in the address space.
 802 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
 803                                             "String1", "String2", "OpenArchive1", "OpenArchive2" };
 804 
 805 char* FileMapInfo::map_region(int i, char** top_ret) {
 806   assert(!MetaspaceShared::is_heap_region(i), "sanity");
 807   CDSFileMapRegion* si = space_at(i);
 808   size_t used = si->_used;
 809   size_t alignment = os::vm_allocation_granularity();
 810   size_t size = align_up(used, alignment);
 811   char *requested_addr = _header->region_addr(i);
 812 
 813   // If a tool agent is in use (debugging enabled), we must map the address space RW
 814   if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
 815     si->_read_only = false;
 816   }
 817 
 818   // map the contents of the CDS archive in this memory
 819   char *base = os::map_memory(_fd, _full_path, si->_file_offset,
 820                               requested_addr, size, si->_read_only,
 821                               si->_allow_exec);
 822   if (base == NULL || base != requested_addr) {
 823     fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
 824     return NULL;
 825   }
 826 #ifdef _WINDOWS
 827   // This call is Windows-only because the memory_type gets recorded for the other platforms


 854 //
 855 // The open archive heap objects are mapped below the shared strings in
 856 // the runtime java heap. The mapped open archive heap data only contain
 857 // references to the shared strings and open archive objects initially.
 858 // During runtime execution, out-going references to any other java heap
 859 // regions may be added. GC may mark and update references in the mapped
 860 // open archive objects.
 861 void FileMapInfo::map_heap_regions() {
 862   if (MetaspaceShared::is_heap_object_archiving_allowed()) {
 863       log_info(cds)("Archived narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
 864                     narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
 865       log_info(cds)("Archived narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 866                     p2i(narrow_klass_base()), narrow_klass_shift());
 867 
 868     // Check that all the narrow oop and klass encodings match the archive
 869     if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
 870         narrow_oop_base() != Universe::narrow_oop_base() ||
 871         narrow_oop_shift() != Universe::narrow_oop_shift() ||
 872         narrow_klass_base() != Universe::narrow_klass_base() ||
 873         narrow_klass_shift() != Universe::narrow_klass_shift()) {
 874       if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) {
 875         log_info(cds)("Cached heap data from the CDS archive is being ignored. "
 876                       "The current CompressedOops/CompressedClassPointers encoding differs from "
 877                       "that archived due to heap size change. The archive was dumped using max heap "
 878                       "size " UINTX_FORMAT "M.", max_heap_size()/M);
 879         log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
 880                       Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()),
 881                       Universe::narrow_oop_shift());
 882         log_info(cds)("Current narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
 883                       p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
 884       }
 885     } else {
 886       // First, map string regions as closed archive heap regions.
 887       // GC does not write into the regions.
 888       if (map_heap_data(&string_ranges,
 889                          MetaspaceShared::first_string,
 890                          MetaspaceShared::max_strings,
 891                          &num_string_ranges)) {
 892         StringTable::set_shared_string_mapped();
 893 
 894         // Now, map open_archive heap regions, GC can write into the regions.
 895         if (map_heap_data(&open_archive_heap_ranges,
 896                           MetaspaceShared::first_open_archive_heap_region,
 897                           MetaspaceShared::max_open_archive_heap_region,
 898                           &num_open_archive_heap_ranges,
 899                           true /* open */)) {
 900           MetaspaceShared::set_open_archive_heap_region_mapped();
 901         }
 902       }
 903     }
 904   } else {
 905     if (log_is_enabled(Info, cds) && space_at(MetaspaceShared::first_string)->_used > 0) {
 906       log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
 907                     "UseCompressedOops and UseCompressedClassPointers are required.");
 908     }
 909   }
 910 
 911   if (!StringTable::shared_string_mapped()) {
 912     assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
 913   }
 914 
 915   if (!MetaspaceShared::open_archive_heap_region_mapped()) {
 916     assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
 917   }
 918 }
 919 
 920 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
 921                                 int max, int* num, bool is_open_archive) {
 922   MemRegion * regions = new MemRegion[max];
 923   CDSFileMapRegion* si;
 924   int region_num = 0;
 925 
 926   for (int i = first;
 927            i < first + max; i++) {
 928     si = space_at(i);
 929     size_t used = si->_used;
 930     if (used > 0) {
 931       size_t size = used;
 932       char* requested_addr = (char*)((void*)CompressedOops::decode_not_null(
 933                                             (narrowOop)si->_addr._offset));
 934       regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
 935       region_num ++;
 936     }
 937   }
 938 
 939   if (region_num == 0) {
 940     return false; // no archived java heap data
 941   }
 942 
 943   // Check that ranges are within the java heap
 944   if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
 945     log_info(cds)("UseSharedSpaces: Unable to allocate region, "
 946                   "range is not within java heap.");
 947     return false;
 948   }
 949 
 950   // allocate from java heap
 951   if (!G1CollectedHeap::heap()->alloc_archive_regions(
 952              regions, region_num, is_open_archive)) {
 953     log_info(cds)("UseSharedSpaces: Unable to allocate region, "
 954                   "java heap range is already in use.");
 955     return false;
 956   }
 957 
 958   // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
 959   // for mapped regions as they are part of the reserved java heap, which is
 960   // already recorded.
 961   for (int i = 0; i < region_num; i++) {
 962     si = space_at(first + i);
 963     char* addr = (char*)regions[i].start();
 964     char* base = os::map_memory(_fd, _full_path, si->_file_offset,
 965                                 addr, regions[i].byte_size(), si->_read_only,
 966                                 si->_allow_exec);
 967     if (base == NULL || base != addr) {
 968       // dealloc the regions from java heap
 969       dealloc_archive_heap_regions(regions, region_num, is_open_archive);
 970       log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap.");
 971       return false;
 972     }
 973   }
 974 
 975   if (!verify_mapped_heap_regions(first, region_num)) {
 976     // dealloc the regions from java heap
 977     dealloc_archive_heap_regions(regions, region_num, is_open_archive);
 978     log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
 979     return false;
 980   }
 981 
 982   // the shared heap data is mapped successfully


1009     G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
1010                                                   num_open_archive_heap_ranges);
1011   }
1012 }
1013 
1014 // dealloc the archive regions from java heap
1015 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) {
1016   if (num > 0) {
1017     assert(regions != NULL, "Null archive ranges array with non-zero count");
1018     G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open);
1019   }
1020 }
1021 #endif // INCLUDE_CDS_JAVA_HEAP
1022 
1023 bool FileMapInfo::verify_region_checksum(int i) {
1024   assert(i >= 0 && i < MetaspaceShared::n_regions, "invalid region");
1025   if (!VerifySharedSpaces) {
1026     return true;
1027   }
1028 
1029   size_t sz = space_at(i)->_used;
1030 
1031   if (sz == 0) {
1032     return true; // no data
1033   }
1034   if ((MetaspaceShared::is_string_region(i) &&
1035        !StringTable::shared_string_mapped()) ||
1036       (MetaspaceShared::is_open_archive_heap_region(i) &&
1037        !MetaspaceShared::open_archive_heap_region_mapped())) {
1038     return true; // archived heap data is not mapped
1039   }
1040   const char* buf = _header->region_addr(i);
1041   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1042   if (crc != space_at(i)->_crc) {
1043     fail_continue("Checksum verification failed.");
1044     return false;
1045   }
1046   return true;
1047 }
1048 
1049 // Unmap a memory region in the address space.
1050 
1051 void FileMapInfo::unmap_region(int i) {
1052   assert(!MetaspaceShared::is_heap_region(i), "sanity");
1053   CDSFileMapRegion* si = space_at(i);
1054   size_t used = si->_used;
1055   size_t size = align_up(used, os::vm_allocation_granularity());
1056 
1057   if (used == 0) {
1058     return;
1059   }
1060 
1061   char* addr = _header->region_addr(i);
1062   if (!os::unmap_memory(addr, size)) {
1063     fail_stop("Unable to unmap shared space.");
1064   }
1065 }
1066 
1067 void FileMapInfo::assert_mark(bool check) {
1068   if (!check) {
1069     fail_stop("Mark mismatch while restoring from shared file.");
1070   }
1071 }
1072 
1073 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {


1091 //
1092 // Validation of the archive is done in two steps:
1093 //
1094 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
1095 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1096 //     region of the archive, which is not mapped yet.
1097 bool FileMapInfo::initialize() {
1098   assert(UseSharedSpaces, "UseSharedSpaces expected.");
1099 
1100   if (!open_for_read()) {
1101     return false;
1102   }
1103 
1104   init_from_file(_fd);
1105   if (!validate_header()) {
1106     return false;
1107   }
1108   return true;
1109 }
1110 
1111 char* FileMapHeader::region_addr(int idx) {
1112   if (MetaspaceShared::is_heap_region(idx)) {
1113     return _space[idx]._used > 0 ?
1114              (char*)((void*)CompressedOops::decode_not_null((narrowOop)_space[idx]._addr._offset)) : NULL;
1115   } else {
1116     return _space[idx]._addr._base;
1117   }
1118 }
1119 
1120 int FileMapHeader::compute_crc() {
1121   char* start = (char*)this;
1122   // start computing from the field after _crc
1123   char* buf = (char*)&_crc + sizeof(_crc);
1124   size_t sz = sizeof(FileMapHeader) - (buf - start);
1125   int crc = ClassLoader::crc32(0, buf, (jint)sz);
1126   return crc;
1127 }
1128 
1129 // This function should only be called during run time with UseSharedSpaces enabled.
1130 bool FileMapHeader::validate() {
1131   if (VerifySharedSpaces && compute_crc() != _crc) {
1132     FileMapInfo::fail_continue("Header checksum verification failed.");
1133     return false;
1134   }
1135 
1136   if (!Arguments::has_jimage()) {
1137     FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
1138     return false;
1139   }
1140 
1141   if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
1142     FileMapInfo::fail_continue("The shared archive file is the wrong version.");
1143     return false;
1144   }
1145   if (_magic != CDS_ARCHIVE_MAGIC) {
1146     FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
1147     return false;
1148   }
1149   char header_version[JVM_IDENT_MAX];
1150   get_header_version(header_version);
1151   if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
1152     log_info(class, path)("expected: %s", header_version);
1153     log_info(class, path)("actual:   %s", _jvm_ident);
1154     FileMapInfo::fail_continue("The shared archive file was created by a different"
1155                   " version or build of HotSpot");
1156     return false;
1157   }
1158   if (_obj_alignment != ObjectAlignmentInBytes) {
1159     FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
1160                   " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
1161                   _obj_alignment, ObjectAlignmentInBytes);
1162     return false;
1163   }
1164   if (_compact_strings != CompactStrings) {
1165     FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"


1201         fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
1202         status = false;
1203       }
1204     }
1205   }
1206 
1207   if (_paths_misc_info != NULL) {
1208     FREE_C_HEAP_ARRAY(char, _paths_misc_info);
1209     _paths_misc_info = NULL;
1210   }
1211   return status;
1212 }
1213 
1214 // Check if a given address is within one of the shared regions
1215 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1216   assert(idx == MetaspaceShared::ro ||
1217          idx == MetaspaceShared::rw ||
1218          idx == MetaspaceShared::mc ||
1219          idx == MetaspaceShared::md, "invalid region index");
1220   char* base = _header->region_addr(idx);
1221   if (p >= base && p < base + space_at(idx)->_used) {
1222     return true;
1223   }
1224   return false;
1225 }
1226 
1227 void FileMapInfo::print_shared_spaces() {
1228   tty->print_cr("Shared Spaces:");
1229   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
1230     CDSFileMapRegion* si = space_at(i);
1231     char *base = _header->region_addr(i);
1232     tty->print("  %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
1233                         shared_region_name[i],
1234                         p2i(base), p2i(base + si->_used));
1235   }
1236 }
1237 
1238 // Unmap mapped regions of shared space.
1239 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1240   MetaspaceObj::set_shared_metaspace_range(NULL, NULL);
1241 
1242   FileMapInfo *map_info = FileMapInfo::current_info();
1243   if (map_info) {
1244     map_info->fail_continue("%s", msg);
1245     for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
1246       char *addr = map_info->_header->region_addr(i);
1247       if (addr != NULL && !MetaspaceShared::is_heap_region(i)) {
1248         map_info->unmap_region(i);
1249         map_info->space_at(i)->_addr._base = NULL;
1250       }
1251     }
1252     // Dealloc the archive heap regions only without unmapping. The regions are part
1253     // of the java heap. Unmapping of the heap regions are managed by GC.
1254     map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1255                                            num_open_archive_heap_ranges,
1256                                            true);
1257     map_info->dealloc_archive_heap_regions(string_ranges,
1258                                            num_string_ranges,
1259                                            false);
1260   } else if (DumpSharedSpaces) {
1261     fail_stop("%s", msg);
1262   }
1263 }
< prev index next >