18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "jvm.h"
27 #include "classfile/classLoader.inline.hpp"
28 #include "classfile/classLoaderExt.hpp"
29 #include "classfile/compactHashtable.inline.hpp"
30 #include "classfile/stringTable.hpp"
31 #include "classfile/symbolTable.hpp"
32 #include "classfile/systemDictionaryShared.hpp"
33 #include "classfile/altHashing.hpp"
34 #include "logging/log.hpp"
35 #include "logging/logStream.hpp"
36 #include "logging/logMessage.hpp"
37 #include "memory/filemap.hpp"
38 #include "memory/metadataFactory.hpp"
39 #include "memory/metaspaceClosure.hpp"
40 #include "memory/metaspaceShared.hpp"
41 #include "memory/oopFactory.hpp"
42 #include "oops/compressedOops.inline.hpp"
43 #include "oops/objArrayOop.hpp"
44 #include "prims/jvmtiExport.hpp"
45 #include "runtime/arguments.hpp"
46 #include "runtime/java.hpp"
47 #include "runtime/os.hpp"
48 #include "runtime/vm_version.hpp"
49 #include "services/memTracker.hpp"
50 #include "utilities/align.hpp"
51 #include "utilities/defaultStream.hpp"
52 #if INCLUDE_G1GC
53 #include "gc/g1/g1CollectedHeap.hpp"
54 #endif
55
56 # include <sys/stat.h>
57 # include <errno.h>
58
59 #ifndef O_BINARY // if defined (Win32) use binary files.
60 #define O_BINARY 0 // otherwise do nothing.
61 #endif
62
63 extern address JVM_FunctionAtStart();
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
199 ClassLoaderExt::finalize_shared_paths_misc_info();
200 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
201 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
202
203 _verify_local = BytecodeVerificationLocal;
204 _verify_remote = BytecodeVerificationRemote;
205 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
206 }
207
208 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
209 assert(DumpSharedSpaces, "dump time only");
210 _timestamp = 0;
508 fail_continue("The shared archive file has the wrong version.");
509 return false;
510 }
511 _file_offset = (long)n;
512
513 size_t info_size = _header->_paths_misc_info_size;
514 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
515 if (_paths_misc_info == NULL) {
516 fail_continue("Unable to read the file header.");
517 return false;
518 }
519 n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
520 if (n != info_size) {
521 fail_continue("Unable to read the shared path info header.");
522 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
523 _paths_misc_info = NULL;
524 return false;
525 }
526
527 size_t len = lseek(fd, 0, SEEK_END);
528 struct FileMapInfo::FileMapHeader::space_info* si =
529 &_header->_space[MetaspaceShared::last_valid_region];
530 // The last space might be empty
531 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
532 fail_continue("The shared archive file has been truncated.");
533 return false;
534 }
535
536 _file_offset += (long)n;
537 return true;
538 }
539
540
541 // Read the FileMapInfo information from the file.
542 bool FileMapInfo::open_for_read() {
543 _full_path = Arguments::GetSharedArchivePath();
544 int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
545 if (fd < 0) {
546 if (errno == ENOENT) {
547 // Not locating the shared archive is ok.
548 fail_continue("Specified shared archive not found.");
590 // Write the header to the file, seek to the next allocation boundary.
591
592 void FileMapInfo::write_header() {
593 int info_size = ClassLoader::get_shared_paths_misc_info_size();
594
595 _header->_paths_misc_info_size = info_size;
596
597 align_file_position();
598 size_t sz = _header->data_size();
599 char* addr = _header->data();
600 write_bytes(addr, (int)sz); // skip the C++ vtable
601 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
602 align_file_position();
603 }
604
605
606 // Dump region to file.
607
608 void FileMapInfo::write_region(int region, char* base, size_t size,
609 bool read_only, bool allow_exec) {
610 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
611
612 if (_file_open) {
613 guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
614 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
615 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
616 region, size, p2i(base), _file_offset);
617 } else {
618 si->_file_offset = _file_offset;
619 }
620 if (MetaspaceShared::is_heap_region(region)) {
621 assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
622 if (base != NULL) {
623 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
624 } else {
625 si->_addr._offset = 0;
626 }
627 } else {
628 si->_addr._base = base;
629 }
630 si->_used = size;
743 }
744
745
746 // Close the shared archive file. This does NOT unmap mapped regions.
747
748 void FileMapInfo::close() {
749 if (_file_open) {
750 if (::close(_fd) < 0) {
751 fail_stop("Unable to close the shared archive file.");
752 }
753 _file_open = false;
754 _fd = -1;
755 }
756 }
757
758
759 // JVM/TI RedefineClasses() support:
760 // Remap the shared readonly space to shared readwrite, private.
761 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
762 int idx = MetaspaceShared::ro;
763 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx];
764 if (!si->_read_only) {
765 // the space is already readwrite so we are done
766 return true;
767 }
768 size_t used = si->_used;
769 size_t size = align_up(used, os::vm_allocation_granularity());
770 if (!open_for_read()) {
771 return false;
772 }
773 char *addr = _header->region_addr(idx);
774 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
775 addr, size, false /* !read_only */,
776 si->_allow_exec);
777 close();
778 if (base == NULL) {
779 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
780 return false;
781 }
782 if (base != addr) {
783 fail_continue("Unable to remap shared readonly space at required address.");
795 // Reserve the space first, then map otherwise map will go right over some
796 // other reserved memory (like the code cache).
797 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
798 if (!rs.is_reserved()) {
799 fail_continue("Unable to reserve shared space at required address "
800 INTPTR_FORMAT, p2i(requested_addr));
801 return rs;
802 }
803 // the reserved virtual memory is for mapping class data sharing archive
804 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
805
806 return rs;
807 }
808
809 // Memory map a region in the address space.
810 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
811 "String1", "String2", "OpenArchive1", "OpenArchive2" };
812
813 char* FileMapInfo::map_region(int i, char** top_ret) {
814 assert(!MetaspaceShared::is_heap_region(i), "sanity");
815 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
816 size_t used = si->_used;
817 size_t alignment = os::vm_allocation_granularity();
818 size_t size = align_up(used, alignment);
819 char *requested_addr = _header->region_addr(i);
820
821 // If a tool agent is in use (debugging enabled), we must map the address space RW
822 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
823 si->_read_only = false;
824 }
825
826 // map the contents of the CDS archive in this memory
827 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
828 requested_addr, size, si->_read_only,
829 si->_allow_exec);
830 if (base == NULL || base != requested_addr) {
831 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
832 return NULL;
833 }
834 #ifdef _WINDOWS
835 // This call is Windows-only because the memory_type gets recorded for the other platforms
836 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
837 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
838 #endif
839
840
841 if (!verify_region_checksum(i)) {
842 return NULL;
843 }
844
845 *top_ret = base + size;
846 return base;
847 }
848
849 static MemRegion *string_ranges = NULL;
850 static MemRegion *open_archive_heap_ranges = NULL;
851 static int num_string_ranges = 0;
852 static int num_open_archive_heap_ranges = 0;
853
854 #if INCLUDE_CDS_JAVA_HEAP
855 //
856 // Map the shared string objects and open archive heap objects to the runtime
857 // java heap.
858 //
859 // The shared strings are mapped near the runtime java heap top. The
860 // mapped strings contain no out-going references to any other java heap
861 // regions. GC does not write into the mapped shared strings.
862 //
863 // The open archive heap objects are mapped below the shared strings in
864 // the runtime java heap. The mapped open archive heap data only contain
865 // references to the shared strings and open archive objects initially.
866 // During runtime execution, out-going references to any other java heap
867 // regions may be added. GC may mark and update references in the mapped
868 // open archive objects.
869 void FileMapInfo::map_heap_regions() {
870 if (MetaspaceShared::is_heap_object_archiving_allowed()) {
871 log_info(cds)("Archived narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
872 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
873 log_info(cds)("Archived narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
874 p2i(narrow_klass_base()), narrow_klass_shift());
875
876 // Check that all the narrow oop and klass encodings match the archive
877 if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
878 narrow_oop_base() != Universe::narrow_oop_base() ||
879 narrow_oop_shift() != Universe::narrow_oop_shift() ||
880 narrow_klass_base() != Universe::narrow_klass_base() ||
881 narrow_klass_shift() != Universe::narrow_klass_shift()) {
882 if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
883 log_info(cds)("Cached heap data from the CDS archive is being ignored. "
884 "The current CompressedOops/CompressedClassPointers encoding differs from "
885 "that archived due to heap size change. The archive was dumped using max heap "
886 "size " UINTX_FORMAT "M.", max_heap_size()/M);
887 log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
888 Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()),
889 Universe::narrow_oop_shift());
890 log_info(cds)("Current narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
891 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
892 }
893 } else {
894 // First, map string regions as closed archive heap regions.
895 // GC does not write into the regions.
896 if (map_heap_data(&string_ranges,
897 MetaspaceShared::first_string,
898 MetaspaceShared::max_strings,
899 &num_string_ranges)) {
900 StringTable::set_shared_string_mapped();
901
902 // Now, map open_archive heap regions, GC can write into the regions.
903 if (map_heap_data(&open_archive_heap_ranges,
904 MetaspaceShared::first_open_archive_heap_region,
905 MetaspaceShared::max_open_archive_heap_region,
906 &num_open_archive_heap_ranges,
907 true /* open */)) {
908 MetaspaceShared::set_open_archive_heap_region_mapped();
909 }
910 }
911 }
912 } else {
913 if (log_is_enabled(Info, cds) && _header->_space[MetaspaceShared::first_string]._used > 0) {
914 log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
915 "UseCompressedOops and UseCompressedClassPointers are required.");
916 }
917 }
918
919 if (!StringTable::shared_string_mapped()) {
920 assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
921 }
922
923 if (!MetaspaceShared::open_archive_heap_region_mapped()) {
924 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
925 }
926 }
927
928 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
929 int max, int* num, bool is_open_archive) {
930 MemRegion * regions = new MemRegion[max];
931 struct FileMapInfo::FileMapHeader::space_info* si;
932 int region_num = 0;
933
934 for (int i = first;
935 i < first + max; i++) {
936 si = &_header->_space[i];
937 size_t used = si->_used;
938 if (used > 0) {
939 size_t size = used;
940 char* requested_addr = (char*)((void*)CompressedOops::decode_not_null(
941 (narrowOop)si->_addr._offset));
942 regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
943 region_num ++;
944 }
945 }
946
947 if (region_num == 0) {
948 return false; // no archived java heap data
949 }
950
951 // Check that ranges are within the java heap
952 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
953 log_info(cds)("UseSharedSpaces: Unable to allocate region, "
954 "range is not within java heap.");
955 return false;
956 }
957
958 // allocate from java heap
959 if (!G1CollectedHeap::heap()->alloc_archive_regions(
960 regions, region_num, is_open_archive)) {
986 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
987 return false;
988 }
989
990 // the shared heap data is mapped successfully
991 *heap_mem = regions;
992 *num = region_num;
993 return true;
994 }
995
996 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
997 for (int i = first;
998 i <= first + num; i++) {
999 if (!verify_region_checksum(i)) {
1000 return false;
1001 }
1002 }
1003 return true;
1004 }
1005
1006 void FileMapInfo::fixup_mapped_heap_regions() {
1007 // If any string regions were found, call the fill routine to make them parseable.
1008 // Note that string_ranges may be non-NULL even if no ranges were found.
1009 if (num_string_ranges != 0) {
1010 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
1011 G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_string_ranges);
1012 }
1013
1014 // do the same for mapped open archive heap regions
1015 if (num_open_archive_heap_ranges != 0) {
1016 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
1017 G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
1018 num_open_archive_heap_ranges);
1019 }
1020 }
1021
1022 // dealloc the archive regions from java heap
1023 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num) {
1024 if (num > 0) {
1025 assert(regions != NULL, "Null archive ranges array with non-zero count");
1040 }
1041 if ((MetaspaceShared::is_string_region(i) &&
1042 !StringTable::shared_string_mapped()) ||
1043 (MetaspaceShared::is_open_archive_heap_region(i) &&
1044 !MetaspaceShared::open_archive_heap_region_mapped())) {
1045 return true; // archived heap data is not mapped
1046 }
1047 const char* buf = _header->region_addr(i);
1048 int crc = ClassLoader::crc32(0, buf, (jint)sz);
1049 if (crc != _header->_space[i]._crc) {
1050 fail_continue("Checksum verification failed.");
1051 return false;
1052 }
1053 return true;
1054 }
1055
1056 // Unmap a memory region in the address space.
1057
1058 void FileMapInfo::unmap_region(int i) {
1059 assert(!MetaspaceShared::is_heap_region(i), "sanity");
1060 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
1061 size_t used = si->_used;
1062 size_t size = align_up(used, os::vm_allocation_granularity());
1063
1064 if (used == 0) {
1065 return;
1066 }
1067
1068 char* addr = _header->region_addr(i);
1069 if (!os::unmap_memory(addr, size)) {
1070 fail_stop("Unable to unmap shared space.");
1071 }
1072 }
1073
1074 void FileMapInfo::assert_mark(bool check) {
1075 if (!check) {
1076 fail_stop("Mark mismatch while restoring from shared file.");
1077 }
1078 }
1079
1080 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1081 it->push(&_shared_path_table);
1082 for (int i=0; i<_shared_path_table_size; i++) {
1083 shared_path(i)->metaspace_pointers_do(it);
1084 }
1085 }
1086
1087
1088 FileMapInfo* FileMapInfo::_current_info = NULL;
1089 Array<u8>* FileMapInfo::_shared_path_table = NULL;
1090 int FileMapInfo::_shared_path_table_size = 0;
1091 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
1092 bool FileMapInfo::_validating_shared_path_table = false;
1093
1094 // Open the shared archive file, read and validate the header
1095 // information (version, boot classpath, etc.). If initialization
1096 // fails, shared spaces are disabled and the file is closed. [See
1097 // fail_continue.]
1098 //
1099 // Validation of the archive is done in two steps:
1100 //
1101 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
1102 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1103 // region of the archive, which is not mapped yet.
1104 bool FileMapInfo::initialize() {
1105 assert(UseSharedSpaces, "UseSharedSpaces expected.");
1106
1107 if (!open_for_read()) {
1108 return false;
|
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "jvm.h"
27 #include "classfile/classLoader.inline.hpp"
28 #include "classfile/classLoaderExt.hpp"
29 #include "classfile/compactHashtable.inline.hpp"
30 #include "classfile/stringTable.hpp"
31 #include "classfile/symbolTable.hpp"
32 #include "classfile/systemDictionaryShared.hpp"
33 #include "classfile/altHashing.hpp"
34 #include "logging/log.hpp"
35 #include "logging/logStream.hpp"
36 #include "logging/logMessage.hpp"
37 #include "memory/filemap.hpp"
38 #include "memory/heapShared.inline.hpp"
39 #include "memory/iterator.inline.hpp"
40 #include "memory/metadataFactory.hpp"
41 #include "memory/metaspaceClosure.hpp"
42 #include "memory/metaspaceShared.hpp"
43 #include "memory/oopFactory.hpp"
44 #include "oops/compressedOops.inline.hpp"
45 #include "oops/objArrayOop.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "prims/jvmtiExport.hpp"
48 #include "runtime/arguments.hpp"
49 #include "runtime/java.hpp"
50 #include "runtime/os.hpp"
51 #include "runtime/vm_version.hpp"
52 #include "services/memTracker.hpp"
53 #include "utilities/align.hpp"
54 #include "utilities/defaultStream.hpp"
55 #if INCLUDE_G1GC
56 #include "gc/g1/g1CollectedHeap.hpp"
57 #endif
58
59 # include <sys/stat.h>
60 # include <errno.h>
61
62 #ifndef O_BINARY // if defined (Win32) use binary files.
63 #define O_BINARY 0 // otherwise do nothing.
64 #endif
65
66 extern address JVM_FunctionAtStart();
174
175 void FileMapInfo::populate_header(size_t alignment) {
176 _header->populate(this, alignment);
177 }
178
179 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
180 _magic = 0xf00baba2;
181 _version = _current_version;
182 _alignment = alignment;
183 _obj_alignment = ObjectAlignmentInBytes;
184 _compact_strings = CompactStrings;
185 _narrow_oop_mode = Universe::narrow_oop_mode();
186 _narrow_oop_base = Universe::narrow_oop_base();
187 _narrow_oop_shift = Universe::narrow_oop_shift();
188 _max_heap_size = MaxHeapSize;
189 _narrow_klass_base = Universe::narrow_klass_base();
190 _narrow_klass_shift = Universe::narrow_klass_shift();
191 _shared_path_table_size = mapinfo->_shared_path_table_size;
192 _shared_path_table = mapinfo->_shared_path_table;
193 _shared_path_entry_size = mapinfo->_shared_path_entry_size;
194 if (MetaspaceShared::is_heap_object_archiving_allowed()) {
195 _g1_reserved = G1CollectedHeap::heap()->g1_reserved();
196 }
197
198 // The following fields are for sanity checks for whether this archive
199 // will function correctly with this JVM and the bootclasspath it's
200 // invoked with.
201
202 // JVM version string ... changes on each build.
203 get_header_version(_jvm_ident);
204
205 ClassLoaderExt::finalize_shared_paths_misc_info();
206 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
207 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
208
209 _verify_local = BytecodeVerificationLocal;
210 _verify_remote = BytecodeVerificationRemote;
211 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
212 }
213
214 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
215 assert(DumpSharedSpaces, "dump time only");
216 _timestamp = 0;
514 fail_continue("The shared archive file has the wrong version.");
515 return false;
516 }
517 _file_offset = (long)n;
518
519 size_t info_size = _header->_paths_misc_info_size;
520 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
521 if (_paths_misc_info == NULL) {
522 fail_continue("Unable to read the file header.");
523 return false;
524 }
525 n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
526 if (n != info_size) {
527 fail_continue("Unable to read the shared path info header.");
528 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
529 _paths_misc_info = NULL;
530 return false;
531 }
532
533 size_t len = lseek(fd, 0, SEEK_END);
534 FileMapHeader::space_info* si =
535 &_header->_space[MetaspaceShared::last_valid_region];
536 // The last space might be empty
537 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
538 fail_continue("The shared archive file has been truncated.");
539 return false;
540 }
541
542 _file_offset += (long)n;
543 return true;
544 }
545
546
547 // Read the FileMapInfo information from the file.
548 bool FileMapInfo::open_for_read() {
549 _full_path = Arguments::GetSharedArchivePath();
550 int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
551 if (fd < 0) {
552 if (errno == ENOENT) {
553 // Not locating the shared archive is ok.
554 fail_continue("Specified shared archive not found.");
596 // Write the header to the file, seek to the next allocation boundary.
597
598 void FileMapInfo::write_header() {
599 int info_size = ClassLoader::get_shared_paths_misc_info_size();
600
601 _header->_paths_misc_info_size = info_size;
602
603 align_file_position();
604 size_t sz = _header->data_size();
605 char* addr = _header->data();
606 write_bytes(addr, (int)sz); // skip the C++ vtable
607 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
608 align_file_position();
609 }
610
611
612 // Dump region to file.
613
614 void FileMapInfo::write_region(int region, char* base, size_t size,
615 bool read_only, bool allow_exec) {
616 FileMapHeader::space_info* si = &_header->_space[region];
617
618 if (_file_open) {
619 guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
620 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
621 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
622 region, size, p2i(base), _file_offset);
623 } else {
624 si->_file_offset = _file_offset;
625 }
626 if (MetaspaceShared::is_heap_region(region)) {
627 assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
628 if (base != NULL) {
629 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
630 } else {
631 si->_addr._offset = 0;
632 }
633 } else {
634 si->_addr._base = base;
635 }
636 si->_used = size;
749 }
750
751
752 // Close the shared archive file. This does NOT unmap mapped regions.
753
754 void FileMapInfo::close() {
755 if (_file_open) {
756 if (::close(_fd) < 0) {
757 fail_stop("Unable to close the shared archive file.");
758 }
759 _file_open = false;
760 _fd = -1;
761 }
762 }
763
764
765 // JVM/TI RedefineClasses() support:
766 // Remap the shared readonly space to shared readwrite, private.
767 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
768 int idx = MetaspaceShared::ro;
769 FileMapHeader::space_info* si = &_header->_space[idx];
770 if (!si->_read_only) {
771 // the space is already readwrite so we are done
772 return true;
773 }
774 size_t used = si->_used;
775 size_t size = align_up(used, os::vm_allocation_granularity());
776 if (!open_for_read()) {
777 return false;
778 }
779 char *addr = _header->region_addr(idx);
780 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
781 addr, size, false /* !read_only */,
782 si->_allow_exec);
783 close();
784 if (base == NULL) {
785 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
786 return false;
787 }
788 if (base != addr) {
789 fail_continue("Unable to remap shared readonly space at required address.");
801 // Reserve the space first, then map otherwise map will go right over some
802 // other reserved memory (like the code cache).
803 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
804 if (!rs.is_reserved()) {
805 fail_continue("Unable to reserve shared space at required address "
806 INTPTR_FORMAT, p2i(requested_addr));
807 return rs;
808 }
809 // the reserved virtual memory is for mapping class data sharing archive
810 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
811
812 return rs;
813 }
814
815 // Memory map a region in the address space.
816 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
817 "String1", "String2", "OpenArchive1", "OpenArchive2" };
818
819 char* FileMapInfo::map_region(int i, char** top_ret) {
820 assert(!MetaspaceShared::is_heap_region(i), "sanity");
821 FileMapHeader::space_info* si = &_header->_space[i];
822 size_t used = si->_used;
823 size_t alignment = os::vm_allocation_granularity();
824 size_t size = align_up(used, alignment);
825 char *requested_addr = _header->region_addr(i);
826
827 // If a tool agent is in use (debugging enabled), we must map the address space RW
828 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
829 si->_read_only = false;
830 }
831
832 // map the contents of the CDS archive in this memory
833 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
834 requested_addr, size, si->_read_only,
835 si->_allow_exec);
836 if (base == NULL || base != requested_addr) {
837 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
838 return NULL;
839 }
840 #ifdef _WINDOWS
841 // This call is Windows-only because the memory_type gets recorded for the other platforms
842 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
843 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
844 #endif
845
846
847 if (!verify_region_checksum(i)) {
848 return NULL;
849 }
850
851 *top_ret = base + size;
852 return base;
853 }
854
855 static MemRegion *string_ranges = NULL;
856 static MemRegion *open_archive_heap_ranges = NULL;
857 static int num_string_ranges = 0;
858 static int num_open_archive_heap_ranges = 0;
859
860 #if INCLUDE_CDS_JAVA_HEAP
861 bool FileMapInfo::has_heap_regions() {
862 return (_header->_space[MetaspaceShared::first_string]._used > 0);
863 }
864
865 //
866 // Map the shared string objects and open archive heap objects to the runtime
867 // java heap.
868 //
869 // The shared strings are mapped near the runtime java heap top. The
870 // mapped strings contain no out-going references to any other java heap
871 // regions. GC does not write into the mapped shared strings.
872 //
873 // The open archive heap objects are mapped below the shared strings in
874 // the runtime java heap. The mapped open archive heap data only contain
875 // references to the shared strings and open archive objects initially.
876 // During runtime execution, out-going references to any other java heap
877 // regions may be added. GC may mark and update references in the mapped
878 // open archive objects.
879 void FileMapInfo::map_heap_regions_impl() {
880 if (!MetaspaceShared::is_heap_object_archiving_allowed()) {
881 log_info(cds)("Cached heap data from the CDS archive is being ignored. UseG1GC, "
882 "UseCompressedOops and UseCompressedClassPointers are required.");
883 return;
884 }
885
886 if (narrow_klass_base() != Universe::narrow_klass_base() ||
887 narrow_klass_shift() != Universe::narrow_klass_shift()) {
888 log_info(cds)("Cached heap data from the CDS archive need to be relocated because");
889 log_info(cds)("the CDS archive was created with an incompatible heap size: " UINTX_FORMAT "M.", max_heap_size()/M);
890 log_info(cds)("Current narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
891 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
892 return;
893 }
894
895 log_info(cds)("Archived narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
896 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
897 log_info(cds)("Archived narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
898 p2i(narrow_klass_base()), narrow_klass_shift());
899
900 ptrdiff_t delta = 0;
901
902 if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
903 narrow_oop_base() != Universe::narrow_oop_base() ||
904 narrow_oop_shift() != Universe::narrow_oop_shift()) {
905 log_info(cds)("Cached heap data from the CDS archive need to be relocated because");
906 log_info(cds)("the CDS archive was created with an incompatible heap size: " UINTX_FORMAT "M.", max_heap_size()/M);
907 log_info(cds)("Current narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
908 Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()),
909 Universe::narrow_oop_shift());
910
911 _heap_pointers_need_relocation = true;
912
913 // dumptime heap end ------------v
914 // [ |archived heap regions| ] runtime heap end ------v
915 // [ |archived heap regions| ]
916 // |<-----delta-------------------->|
917 //
918 // At dump time, the archived heap region were near the top of the heap.
919 // At run time, that region may not be inside the heap, so we move it so
920 // that it's now near the top of teh runtime time. This can be done by
921 // the simple math of adding the delta as shown above.
922 address dumptime_heap_end = (address)_header->_g1_reserved.end();
923 address runtime_heap_end = (address)G1CollectedHeap::heap()->g1_reserved().end();
924 delta = runtime_heap_end - dumptime_heap_end;
925 }
926
927 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
928
929 // First, map string regions as closed archive heap regions.
930 // GC does not write into the regions.
931 if (map_heap_data(&string_ranges,
932 MetaspaceShared::first_string,
933 MetaspaceShared::max_strings,
934 &num_string_ranges)) {
935 StringTable::set_shared_string_mapped();
936
937 // Now, map open_archive heap regions, GC can write into the regions.
938 if (map_heap_data(&open_archive_heap_ranges,
939 MetaspaceShared::first_open_archive_heap_region,
940 MetaspaceShared::max_open_archive_heap_region,
941 &num_open_archive_heap_ranges,
942 true /* open */)) {
943 MetaspaceShared::set_open_archive_heap_region_mapped();
944 }
945 }
946 }
947
948 void FileMapInfo::map_heap_regions() {
949 if (has_heap_regions()) {
950 map_heap_regions_impl();
951 }
952
953 if (!StringTable::shared_string_mapped()) {
954 assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
955 }
956
957 if (!MetaspaceShared::open_archive_heap_region_mapped()) {
958 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
959 }
960 }
961
962 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
963 int max, int* num, bool is_open_archive) {
964 MemRegion * regions = new MemRegion[max];
965 FileMapHeader::space_info* si;
966 int region_num = 0;
967
968 for (int i = first;
969 i < first + max; i++) {
970 si = &_header->_space[i];
971 size_t size = si->_used;
972 if (size > 0) {
973 char* requested_addr = (char*)((void*)HeapShared::decode_not_null(
974 (narrowOop)si->_addr._offset));
975 regions[region_num] = MemRegion((HeapWord*)requested_addr, size / HeapWordSize);
976 region_num ++;
977 }
978 }
979
980 if (region_num == 0) {
981 return false; // no archived java heap data
982 }
983
984 // Check that ranges are within the java heap
985 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
986 log_info(cds)("UseSharedSpaces: Unable to allocate region, "
987 "range is not within java heap.");
988 return false;
989 }
990
991 // allocate from java heap
992 if (!G1CollectedHeap::heap()->alloc_archive_regions(
993 regions, region_num, is_open_archive)) {
1019 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
1020 return false;
1021 }
1022
1023 // the shared heap data is mapped successfully
1024 *heap_mem = regions;
1025 *num = region_num;
1026 return true;
1027 }
1028
1029 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
1030 for (int i = first;
1031 i <= first + num; i++) {
1032 if (!verify_region_checksum(i)) {
1033 return false;
1034 }
1035 }
1036 return true;
1037 }
1038
1039 void FileMapInfo::relocate_archived_heap_embedded_pointers() {
1040 if (!_heap_pointers_need_relocation) {
1041 return;
1042 }
1043
1044 relocate_archived_heap_embedded_pointers_impl(string_ranges,
1045 num_string_ranges);
1046
1047 relocate_archived_heap_embedded_pointers_impl(open_archive_heap_ranges,
1048 num_open_archive_heap_ranges);
1049 }
1050
1051 class RelocateInternalPointers: public BasicOopIterateClosure {
1052 public:
1053 virtual bool should_verify_oops(void) {
1054 return false;
1055 }
1056 virtual void do_oop(narrowOop *p) {
1057 narrowOop v = *p;
1058 if (!CompressedOops::is_null(v)) {
1059 oop o = HeapShared::decode_not_null(v);
1060 RawAccess<IS_NOT_NULL>::oop_store(p, o);
1061 }
1062 }
1063 virtual void do_oop(oop *p) {
1064 ShouldNotReachHere();
1065 }
1066 };
1067
1068 void FileMapInfo::relocate_archived_heap_embedded_pointers_impl(MemRegion *heap_mem, int num) {
1069 RelocateInternalPointers relocator;
1070
1071 for (int i=0; i<num; i++) {
1072 HeapWord* p = heap_mem[i].start();
1073 HeapWord* end = heap_mem[i].end();
1074 while (p < end) {
1075 oop o = (oop)p;
1076 o->oop_iterate(&relocator);
1077 p += o->size();
1078 }
1079 }
1080 }
1081
1082 // This internally allocates objects using SystemDictionary::Object_klass(), so it
1083 // must be called after the well-known classes are resolved.
1084 void FileMapInfo::fixup_mapped_heap_regions() {
1085 // If any string regions were found, call the fill routine to make them parseable.
1086 // Note that string_ranges may be non-NULL even if no ranges were found.
1087 if (num_string_ranges != 0) {
1088 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
1089 G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_string_ranges);
1090 }
1091
1092 // do the same for mapped open archive heap regions
1093 if (num_open_archive_heap_ranges != 0) {
1094 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
1095 G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
1096 num_open_archive_heap_ranges);
1097 }
1098 }
1099
1100 // dealloc the archive regions from java heap
1101 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num) {
1102 if (num > 0) {
1103 assert(regions != NULL, "Null archive ranges array with non-zero count");
1118 }
1119 if ((MetaspaceShared::is_string_region(i) &&
1120 !StringTable::shared_string_mapped()) ||
1121 (MetaspaceShared::is_open_archive_heap_region(i) &&
1122 !MetaspaceShared::open_archive_heap_region_mapped())) {
1123 return true; // archived heap data is not mapped
1124 }
1125 const char* buf = _header->region_addr(i);
1126 int crc = ClassLoader::crc32(0, buf, (jint)sz);
1127 if (crc != _header->_space[i]._crc) {
1128 fail_continue("Checksum verification failed.");
1129 return false;
1130 }
1131 return true;
1132 }
1133
1134 // Unmap a memory region in the address space.
1135
1136 void FileMapInfo::unmap_region(int i) {
1137 assert(!MetaspaceShared::is_heap_region(i), "sanity");
1138 FileMapHeader::space_info* si = &_header->_space[i];
1139 size_t used = si->_used;
1140 size_t size = align_up(used, os::vm_allocation_granularity());
1141
1142 if (used == 0) {
1143 return;
1144 }
1145
1146 char* addr = _header->region_addr(i);
1147 if (!os::unmap_memory(addr, size)) {
1148 fail_stop("Unable to unmap shared space.");
1149 }
1150 }
1151
1152 void FileMapInfo::assert_mark(bool check) {
1153 if (!check) {
1154 fail_stop("Mark mismatch while restoring from shared file.");
1155 }
1156 }
1157
1158 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1159 it->push(&_shared_path_table);
1160 for (int i=0; i<_shared_path_table_size; i++) {
1161 shared_path(i)->metaspace_pointers_do(it);
1162 }
1163 }
1164
1165
1166 FileMapInfo* FileMapInfo::_current_info = NULL;
1167 bool FileMapInfo::_heap_pointers_need_relocation = false;
1168 Array<u8>* FileMapInfo::_shared_path_table = NULL;
1169 int FileMapInfo::_shared_path_table_size = 0;
1170 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
1171 bool FileMapInfo::_validating_shared_path_table = false;
1172
1173 // Open the shared archive file, read and validate the header
1174 // information (version, boot classpath, etc.). If initialization
1175 // fails, shared spaces are disabled and the file is closed. [See
1176 // fail_continue.]
1177 //
1178 // Validation of the archive is done in two steps:
1179 //
1180 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
1181 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1182 // region of the archive, which is not mapped yet.
1183 bool FileMapInfo::initialize() {
1184 assert(UseSharedSpaces, "UseSharedSpaces expected.");
1185
1186 if (!open_for_read()) {
1187 return false;
|