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.inline.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();
64 extern address JVM_FunctionAtEnd();
65
66 // Complain and stop. All error conditions occurring during the writing of
67 // an archive file should stop the process. Unrecoverable errors during
68 // the reading of the archive file should stop the process.
69
70 static void fail(const char *msg, va_list ap) {
71 // This occurs very early during initialization: tty is not initialized.
72 jio_fprintf(defaultStream::error_stream(),
73 "An error has occurred while processing the"
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
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 _max_used_path_index = ClassLoaderExt::max_used_path_index();
203
204 _verify_local = BytecodeVerificationLocal;
205 _verify_remote = BytecodeVerificationRemote;
206 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
207 }
208
209 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
210 assert(DumpSharedSpaces, "dump time only");
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
648 //
649 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
650 // Otherwise:
651 //
652 // "X" represented space that's occupied by heap objects.
653 // "_" represented unused spaced in the heap region.
654 //
655 //
656 // |ah0 | ah1 | ah2| ...... | ahn |
657 // |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
658 // |<-r0->| |<- r1 ----------------->|
659 // ^^^
660 // |
661 // +-- gap
662 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
663 int first_region_id, int max_num_regions) {
664 assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
665
666 int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
667 if(arr_len > max_num_regions) {
668 fail_stop("Unable to write archive heap memory regions: "
669 "number of memory regions exceeds maximum due to fragmentation");
670 }
671
672 size_t total_size = 0;
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
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.");
776 return false;
777 }
778 si->_read_only = false;
779 return true;
780 }
781
782 // Map the whole region at once, assumed to be allocated contiguously.
783 ReservedSpace FileMapInfo::reserve_shared_memory() {
784 char* requested_addr = _header->region_addr(0);
785 size_t size = FileMapInfo::core_spaces_size();
786
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
828 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
829 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
830 #endif
831
832
833 if (!verify_region_checksum(i)) {
834 return NULL;
835 }
836
837 *top_ret = base + size;
838 return base;
839 }
840
841 static MemRegion *string_ranges = NULL;
842 static MemRegion *open_archive_heap_ranges = NULL;
843 static int num_string_ranges = 0;
844 static int num_open_archive_heap_ranges = 0;
845
846 #if INCLUDE_CDS_JAVA_HEAP
847 //
848 // Map the shared string objects and open archive heap objects to the runtime
849 // java heap.
850 //
851 // The shared strings are mapped near the runtime java heap top. The
852 // mapped strings contain no out-going references to any other java heap
853 // regions. GC does not write into the mapped shared strings.
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
983 *heap_mem = regions;
984 *num = region_num;
985 return true;
986 }
987
988 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
989 assert(num > 0, "sanity");
990 for (int i = first; i < first + num; i++) {
991 if (!verify_region_checksum(i)) {
992 return false;
993 }
994 }
995 return true;
996 }
997
998 void FileMapInfo::fixup_mapped_heap_regions() {
999 // If any string regions were found, call the fill routine to make them parseable.
1000 // Note that string_ranges may be non-NULL even if no ranges were found.
1001 if (num_string_ranges != 0) {
1002 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
1003 G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_string_ranges);
1004 }
1005
1006 // do the same for mapped open archive heap regions
1007 if (num_open_archive_heap_ranges != 0) {
1008 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
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");
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) {
1074 it->push(&_shared_path_table);
1075 for (int i=0; i<_shared_path_table_size; i++) {
1076 shared_path(i)->metaspace_pointers_do(it);
1077 }
1078 }
1079
1080
1081 FileMapInfo* FileMapInfo::_current_info = NULL;
1082 Array<u8>* FileMapInfo::_shared_path_table = NULL;
1083 int FileMapInfo::_shared_path_table_size = 0;
1084 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
1085 bool FileMapInfo::_validating_shared_path_table = false;
1086
1087 // Open the shared archive file, read and validate the header
1088 // information (version, boot classpath, etc.). If initialization
1089 // fails, shared spaces are disabled and the file is closed. [See
1090 // fail_continue.]
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()) {
1200 if (!PrintSharedArchiveAndExit) {
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 }
|
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.inline.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 #include "gc/g1/heapRegion.hpp"
58 #endif
59
60 # include <sys/stat.h>
61 # include <errno.h>
62
63 #ifndef O_BINARY // if defined (Win32) use binary files.
64 #define O_BINARY 0 // otherwise do nothing.
65 #endif
66
67 extern address JVM_FunctionAtStart();
68 extern address JVM_FunctionAtEnd();
69
70 // Complain and stop. All error conditions occurring during the writing of
71 // an archive file should stop the process. Unrecoverable errors during
72 // the reading of the archive file should stop the process.
73
74 static void fail(const char *msg, va_list ap) {
75 // This occurs very early during initialization: tty is not initialized.
76 jio_fprintf(defaultStream::error_stream(),
77 "An error has occurred while processing the"
175
176 void FileMapInfo::populate_header(size_t alignment) {
177 _header->populate(this, alignment);
178 }
179
180 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
181 _magic = CDS_ARCHIVE_MAGIC;
182 _version = CURRENT_CDS_ARCHIVE_VERSION;
183 _alignment = alignment;
184 _obj_alignment = ObjectAlignmentInBytes;
185 _compact_strings = CompactStrings;
186 _narrow_oop_mode = Universe::narrow_oop_mode();
187 _narrow_oop_base = Universe::narrow_oop_base();
188 _narrow_oop_shift = Universe::narrow_oop_shift();
189 _max_heap_size = MaxHeapSize;
190 _narrow_klass_base = Universe::narrow_klass_base();
191 _narrow_klass_shift = Universe::narrow_klass_shift();
192 _shared_path_table_size = mapinfo->_shared_path_table_size;
193 _shared_path_table = mapinfo->_shared_path_table;
194 _shared_path_entry_size = mapinfo->_shared_path_entry_size;
195 if (MetaspaceShared::is_heap_object_archiving_allowed()) {
196 _heap_reserved = Universe::heap()->reserved_region();
197 }
198
199 // The following fields are for sanity checks for whether this archive
200 // will function correctly with this JVM and the bootclasspath it's
201 // invoked with.
202
203 // JVM version string ... changes on each build.
204 get_header_version(_jvm_ident);
205
206 ClassLoaderExt::finalize_shared_paths_misc_info();
207 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
208 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
209 _max_used_path_index = ClassLoaderExt::max_used_path_index();
210
211 _verify_local = BytecodeVerificationLocal;
212 _verify_remote = BytecodeVerificationRemote;
213 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
214 }
215
216 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
217 assert(DumpSharedSpaces, "dump time only");
643 // size, there is only one MemRegion in the array.
644 //
645 // If the archive heap memory size is bigger than one dump time GC region size,
646 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
647 // the first/bottom archive GC region is a partial GC region (with the empty
648 // portion at the higher address within the region), one MemRegion is used for
649 // the bottom partial archive GC region. The rest of the consecutive archive
650 // GC regions are combined into another MemRegion.
651 //
652 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
653 // + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
654 // + We have 1 or 2 consolidated heap memory regions: r0 and r1
655 //
656 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
657 // Otherwise:
658 //
659 // "X" represented space that's occupied by heap objects.
660 // "_" represented unused spaced in the heap region.
661 //
662 //
663 // |ah0 | ah1 | ah2| ...... | ahn|
664 // |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
665 // |<-r0->| |<- r1 ----------------->|
666 // ^^^
667 // |
668 // +-- gap
669 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
670 GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
671 int first_region_id, int max_num_regions) {
672 assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
673
674 int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
675 if(arr_len > max_num_regions) {
676 fail_stop("Unable to write archive heap memory regions: "
677 "number of memory regions exceeds maximum due to fragmentation");
678 }
679
680 size_t total_size = 0;
681 for (int i = first_region_id, arr_idx = 0;
682 i < first_region_id + max_num_regions;
683 i++, arr_idx++) {
684 char* start = NULL;
685 size_t size = 0;
686 if (arr_idx < arr_len) {
687 start = (char*)heap_mem->at(arr_idx).start();
688 size = heap_mem->at(arr_idx).byte_size();
689 total_size += size;
690 }
691
692 log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
693 i, p2i(start), p2i(start + size), size);
694 write_region(i, start, size, false, false);
695 if (size > 0) {
696 space_at(i)->_oopmap = oopmaps->at(arr_idx)._oopmap;
697 space_at(i)->_oopmap_size_in_bits = oopmaps->at(arr_idx)._oopmap_size_in_bits;
698 }
699 }
700 return total_size;
701 }
702
703 // Dump bytes to file -- at the current file position.
704
705 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
706 if (_file_open) {
707 size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
708 if (n != nbytes) {
709 // It is dangerous to leave the corrupted shared archive file around,
710 // close and remove the file. See bug 6372906.
711 close();
712 remove(_full_path);
713 fail_stop("Unable to write to shared archive file.");
714 }
715 }
716 _file_offset += nbytes;
717 }
718
757 _file_open = false;
758 _fd = -1;
759 }
760 }
761
762
763 // JVM/TI RedefineClasses() support:
764 // Remap the shared readonly space to shared readwrite, private.
765 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
766 int idx = MetaspaceShared::ro;
767 CDSFileMapRegion* si = space_at(idx);
768 if (!si->_read_only) {
769 // the space is already readwrite so we are done
770 return true;
771 }
772 size_t used = si->_used;
773 size_t size = align_up(used, os::vm_allocation_granularity());
774 if (!open_for_read()) {
775 return false;
776 }
777 char *addr = region_addr(idx);
778 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
779 addr, size, false /* !read_only */,
780 si->_allow_exec);
781 close();
782 if (base == NULL) {
783 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
784 return false;
785 }
786 if (base != addr) {
787 fail_continue("Unable to remap shared readonly space at required address.");
788 return false;
789 }
790 si->_read_only = false;
791 return true;
792 }
793
794 // Map the whole region at once, assumed to be allocated contiguously.
795 ReservedSpace FileMapInfo::reserve_shared_memory() {
796 char* requested_addr = region_addr(0);
797 size_t size = FileMapInfo::core_spaces_size();
798
799 // Reserve the space first, then map otherwise map will go right over some
800 // other reserved memory (like the code cache).
801 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
802 if (!rs.is_reserved()) {
803 fail_continue("Unable to reserve shared space at required address "
804 INTPTR_FORMAT, p2i(requested_addr));
805 return rs;
806 }
807 // the reserved virtual memory is for mapping class data sharing archive
808 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
809
810 return rs;
811 }
812
813 // Memory map a region in the address space.
814 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
815 "String1", "String2", "OpenArchive1", "OpenArchive2" };
816
817 char* FileMapInfo::map_region(int i, char** top_ret) {
818 assert(!MetaspaceShared::is_heap_region(i), "sanity");
819 CDSFileMapRegion* si = space_at(i);
820 size_t used = si->_used;
821 size_t alignment = os::vm_allocation_granularity();
822 size_t size = align_up(used, alignment);
823 char *requested_addr = region_addr(i);
824
825 // If a tool agent is in use (debugging enabled), we must map the address space RW
826 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
827 si->_read_only = false;
828 }
829
830 // map the contents of the CDS archive in this memory
831 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
832 requested_addr, size, si->_read_only,
833 si->_allow_exec);
834 if (base == NULL || base != requested_addr) {
835 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
836 return NULL;
837 }
838 #ifdef _WINDOWS
839 // This call is Windows-only because the memory_type gets recorded for the other platforms
840 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
841 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
842 #endif
843
844
845 if (!verify_region_checksum(i)) {
846 return NULL;
847 }
848
849 *top_ret = base + size;
850 return base;
851 }
852
853 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) {
854 if (with_current_oop_encoding_mode) {
855 return (address)CompressedOops::decode_not_null(offset_of_space(spc));
856 } else {
857 return (address)HeapShared::decode_with_archived_oop_encoding_mode(offset_of_space(spc));
858 }
859 }
860
861 static MemRegion *string_ranges = NULL;
862 static MemRegion *open_archive_heap_ranges = NULL;
863 static int num_string_ranges = 0;
864 static int num_open_archive_heap_ranges = 0;
865
866 #if INCLUDE_CDS_JAVA_HEAP
867 bool FileMapInfo::has_heap_regions() {
868 return (_header->_space[MetaspaceShared::first_string]._used > 0);
869 }
870
871 // Returns the address range of the archived heap regions computed using the
872 // current oop encoding mode. This range may be different than the one seen at
873 // dump time due to encoding mode differences. The result is used in determining
874 // if/how these regions should be relocated at run time.
875 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() {
876 address start = (address) max_uintx;
877 address end = NULL;
878
879 for (int i = MetaspaceShared::first_string; i <= MetaspaceShared::last_valid_region; i++) {
880 CDSFileMapRegion* si = space_at(i);
881 size_t size = si->_used;
882 if (size > 0) {
883 address s = start_address_with_current_oop_encoding_mode(si);
884 address e = s + size;
885 if (start > s) {
886 start = s;
887 }
888 if (end < e) {
889 end = e;
890 }
891 }
892 }
893 assert(end != NULL, "must have at least one used heap region");
894 return MemRegion((HeapWord*)start, (HeapWord*)end);
895 }
896
897 //
898 // Map the shared string objects and open archive heap objects to the runtime
899 // java heap.
900 //
901 // The shared strings are mapped close to the end of the java heap top in
902 // closed archive regions. The mapped strings contain no out-going references
903 // to any other java heap regions. GC does not write into the mapped shared strings.
904 //
905 // The open archive heap objects are mapped below the shared strings in
906 // the runtime java heap. The mapped open archive heap data only contain
907 // references to the shared strings and open archive objects initially.
908 // During runtime execution, out-going references to any other java heap
909 // regions may be added. GC may mark and update references in the mapped
910 // open archive objects.
911 void FileMapInfo::map_heap_regions_impl() {
912 if (!MetaspaceShared::is_heap_object_archiving_allowed()) {
913 log_info(cds)("CDS heap data is being ignored. UseG1GC, "
914 "UseCompressedOops and UseCompressedClassPointers are required.");
915 return;
916 }
917
918 MemRegion heap_reserved = Universe::heap()->reserved_region();
919
920 log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:",
921 max_heap_size()/M);
922 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
923 p2i(narrow_klass_base()), narrow_klass_shift());
924 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
925 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
926
927 log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT,
928 heap_reserved.byte_size()/M, HeapRegion::GrainBytes);
929 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
930 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
931 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
932 Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()), Universe::narrow_oop_shift());
933
934 if (narrow_klass_base() != Universe::narrow_klass_base() ||
935 narrow_klass_shift() != Universe::narrow_klass_shift()) {
936 log_info(cds)("CDS heap data cannot be used because the archive was created with an incompatible narrow klass encoding mode.");
937 return;
938 }
939
940 if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
941 narrow_oop_base() != Universe::narrow_oop_base() ||
942 narrow_oop_shift() != Universe::narrow_oop_shift()) {
943 log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode.");
944 _heap_pointers_need_patching = true;
945 } else {
946 MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode();
947 if (!heap_reserved.contains(range)) {
948 log_info(cds)("CDS heap data need to be relocated because");
949 log_info(cds)("the desired range " PTR_FORMAT " - " PTR_FORMAT, p2i(range.start()), p2i(range.end()));
950 log_info(cds)("is outside of the heap " PTR_FORMAT " - " PTR_FORMAT, p2i(heap_reserved.start()), p2i(heap_reserved.end()));
951 _heap_pointers_need_patching = true;
952 }
953 }
954
955 ptrdiff_t delta = 0;
956 if (_heap_pointers_need_patching) {
957 // dumptime heap end ------------v
958 // [ |archived heap regions| ] runtime heap end ------v
959 // [ |archived heap regions| ]
960 // |<-----delta-------------------->|
961 //
962 // At dump time, the archived heap regions were near the top of the heap.
963 // At run time, they may not be inside the heap, so we move them so
964 // that they are now near the top of the runtime time. This can be done by
965 // the simple math of adding the delta as shown above.
966 address dumptime_heap_end = (address)_header->_heap_reserved.end();
967 address runtime_heap_end = (address)heap_reserved.end();
968 delta = runtime_heap_end - dumptime_heap_end;
969 }
970
971 log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta);
972 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
973
974 CDSFileMapRegion* si = space_at(MetaspaceShared::first_string);
975 address relocated_strings_bottom = start_address_with_archived_oop_encoding_mode(si);
976 if (!is_aligned(relocated_strings_bottom + delta, HeapRegion::GrainBytes)) {
977 // Align the bottom of the string regions at G1 region boundary. This will avoid
978 // the situation where the highest open region and the lowest string region sharing
979 // the same G1 region. Otherwise we will fail to map the open regions.
980 size_t align = size_t(relocated_strings_bottom) % HeapRegion::GrainBytes;
981 delta -= align;
982 assert(is_aligned(relocated_strings_bottom + delta, HeapRegion::GrainBytes), "must be");
983
984 log_info(cds)("CDS heap data need to be relocated lower by a further " SIZE_FORMAT
985 " bytes to be aligned with HeapRegion::GrainBytes", align);
986
987 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
988 _heap_pointers_need_patching = true;
989 }
990
991 // First, map string regions as closed archive heap regions.
992 // GC does not write into the regions.
993 if (map_heap_data(&string_ranges,
994 MetaspaceShared::first_string,
995 MetaspaceShared::max_strings,
996 &num_string_ranges)) {
997 StringTable::set_shared_string_mapped();
998
999 // Now, map open_archive heap regions, GC can write into the regions.
1000 if (map_heap_data(&open_archive_heap_ranges,
1001 MetaspaceShared::first_open_archive_heap_region,
1002 MetaspaceShared::max_open_archive_heap_region,
1003 &num_open_archive_heap_ranges,
1004 true /* open */)) {
1005 MetaspaceShared::set_open_archive_heap_region_mapped();
1006 }
1007 }
1008 }
1009
1010 void FileMapInfo::map_heap_regions() {
1011 if (has_heap_regions()) {
1012 map_heap_regions_impl();
1013 }
1014
1015 if (!StringTable::shared_string_mapped()) {
1016 assert(string_ranges == NULL && num_string_ranges == 0, "sanity");
1017 }
1018
1019 if (!MetaspaceShared::open_archive_heap_region_mapped()) {
1020 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
1021 }
1022 }
1023
1024 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
1025 int max, int* num, bool is_open_archive) {
1026 MemRegion * regions = new MemRegion[max];
1027 CDSFileMapRegion* si;
1028 int region_num = 0;
1029
1030 for (int i = first;
1031 i < first + max; i++) {
1032 si = space_at(i);
1033 size_t size = si->_used;
1034 if (size > 0) {
1035 HeapWord* start = (HeapWord*)start_address_with_archived_oop_encoding_mode(si);
1036 regions[region_num] = MemRegion(start, size / HeapWordSize);
1037 region_num ++;
1038 log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes",
1039 i, p2i(start), size);
1040 }
1041 }
1042
1043 if (region_num == 0) {
1044 return false; // no archived java heap data
1045 }
1046
1047 // Check that ranges are within the java heap
1048 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
1049 log_info(cds)("UseSharedSpaces: Unable to allocate region, range is not within java heap.");
1050 return false;
1051 }
1052
1053 // allocate from java heap
1054 if (!G1CollectedHeap::heap()->alloc_archive_regions(
1055 regions, region_num, is_open_archive)) {
1056 log_info(cds)("UseSharedSpaces: Unable to allocate region, java heap range is already in use.");
1057 return false;
1058 }
1059
1060 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
1061 // for mapped regions as they are part of the reserved java heap, which is
1062 // already recorded.
1063 for (int i = 0; i < region_num; i++) {
1064 si = space_at(first + i);
1065 char* addr = (char*)regions[i].start();
1066 char* base = os::map_memory(_fd, _full_path, si->_file_offset,
1067 addr, regions[i].byte_size(), si->_read_only,
1068 si->_allow_exec);
1069 if (base == NULL || base != addr) {
1070 // dealloc the regions from java heap
1071 dealloc_archive_heap_regions(regions, region_num, is_open_archive);
1072 log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
1073 INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
1074 p2i(addr), regions[i].byte_size());
1075 return false;
1076 }
1077 }
1078
1079 if (!verify_mapped_heap_regions(first, region_num)) {
1080 // dealloc the regions from java heap
1081 dealloc_archive_heap_regions(regions, region_num, is_open_archive);
1082 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
1083 return false;
1084 }
1085
1086 // the shared heap data is mapped successfully
1087 *heap_mem = regions;
1088 *num = region_num;
1089 return true;
1090 }
1091
1092 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
1093 assert(num > 0, "sanity");
1094 for (int i = first; i < first + num; i++) {
1095 if (!verify_region_checksum(i)) {
1096 return false;
1097 }
1098 }
1099 return true;
1100 }
1101
1102 void FileMapInfo::patch_archived_heap_embedded_pointers() {
1103 if (!_heap_pointers_need_patching) {
1104 return;
1105 }
1106
1107 patch_archived_heap_embedded_pointers(string_ranges,
1108 num_string_ranges,
1109 MetaspaceShared::first_string);
1110
1111 patch_archived_heap_embedded_pointers(open_archive_heap_ranges,
1112 num_open_archive_heap_ranges,
1113 MetaspaceShared::first_open_archive_heap_region);
1114 }
1115
1116 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
1117 int first_region_idx) {
1118 for (int i=0; i<num_ranges; i++) {
1119 CDSFileMapRegion* si = space_at(i + first_region_idx);
1120 HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->_oopmap,
1121 si->_oopmap_size_in_bits);
1122 }
1123 }
1124
1125 // This internally allocates objects using SystemDictionary::Object_klass(), so it
1126 // must be called after the well-known classes are resolved.
1127 void FileMapInfo::fixup_mapped_heap_regions() {
1128 // If any string regions were found, call the fill routine to make them parseable.
1129 // Note that string_ranges may be non-NULL even if no ranges were found.
1130 if (num_string_ranges != 0) {
1131 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
1132 G1CollectedHeap::heap()->fill_archive_regions(string_ranges, num_string_ranges);
1133 }
1134
1135 // do the same for mapped open archive heap regions
1136 if (num_open_archive_heap_ranges != 0) {
1137 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
1138 G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
1139 num_open_archive_heap_ranges);
1140 }
1141 }
1142
1143 // dealloc the archive regions from java heap
1144 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) {
1145 if (num > 0) {
1146 assert(regions != NULL, "Null archive ranges array with non-zero count");
1149 }
1150 #endif // INCLUDE_CDS_JAVA_HEAP
1151
1152 bool FileMapInfo::verify_region_checksum(int i) {
1153 assert(i >= 0 && i < MetaspaceShared::n_regions, "invalid region");
1154 if (!VerifySharedSpaces) {
1155 return true;
1156 }
1157
1158 size_t sz = space_at(i)->_used;
1159
1160 if (sz == 0) {
1161 return true; // no data
1162 }
1163 if ((MetaspaceShared::is_string_region(i) &&
1164 !StringTable::shared_string_mapped()) ||
1165 (MetaspaceShared::is_open_archive_heap_region(i) &&
1166 !MetaspaceShared::open_archive_heap_region_mapped())) {
1167 return true; // archived heap data is not mapped
1168 }
1169 const char* buf = region_addr(i);
1170 int crc = ClassLoader::crc32(0, buf, (jint)sz);
1171 if (crc != space_at(i)->_crc) {
1172 fail_continue("Checksum verification failed.");
1173 return false;
1174 }
1175 return true;
1176 }
1177
1178 // Unmap a memory region in the address space.
1179
1180 void FileMapInfo::unmap_region(int i) {
1181 assert(!MetaspaceShared::is_heap_region(i), "sanity");
1182 CDSFileMapRegion* si = space_at(i);
1183 size_t used = si->_used;
1184 size_t size = align_up(used, os::vm_allocation_granularity());
1185
1186 if (used == 0) {
1187 return;
1188 }
1189
1190 char* addr = region_addr(i);
1191 if (!os::unmap_memory(addr, size)) {
1192 fail_stop("Unable to unmap shared space.");
1193 }
1194 }
1195
1196 void FileMapInfo::assert_mark(bool check) {
1197 if (!check) {
1198 fail_stop("Mark mismatch while restoring from shared file.");
1199 }
1200 }
1201
1202 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1203 it->push(&_shared_path_table);
1204 for (int i=0; i<_shared_path_table_size; i++) {
1205 shared_path(i)->metaspace_pointers_do(it);
1206 }
1207 }
1208
1209
1210 FileMapInfo* FileMapInfo::_current_info = NULL;
1211 bool FileMapInfo::_heap_pointers_need_patching = false;
1212 Array<u8>* FileMapInfo::_shared_path_table = NULL;
1213 int FileMapInfo::_shared_path_table_size = 0;
1214 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
1215 bool FileMapInfo::_validating_shared_path_table = false;
1216
1217 // Open the shared archive file, read and validate the header
1218 // information (version, boot classpath, etc.). If initialization
1219 // fails, shared spaces are disabled and the file is closed. [See
1220 // fail_continue.]
1221 //
1222 // Validation of the archive is done in two steps:
1223 //
1224 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
1225 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1226 // region of the archive, which is not mapped yet.
1227 bool FileMapInfo::initialize() {
1228 assert(UseSharedSpaces, "UseSharedSpaces expected.");
1229
1230 if (!open_for_read()) {
1231 return false;
1232 }
1233
1234 init_from_file(_fd);
1235 if (!validate_header()) {
1236 return false;
1237 }
1238 return true;
1239 }
1240
1241 char* FileMapInfo::region_addr(int idx) {
1242 CDSFileMapRegion* si = space_at(idx);
1243 if (MetaspaceShared::is_heap_region(idx)) {
1244 assert(DumpSharedSpaces, "The following doesn't work at runtime");
1245 return si->_used > 0 ?
1246 (char*)start_address_with_current_oop_encoding_mode(si) : NULL;
1247 } else {
1248 return si->_addr._base;
1249 }
1250 }
1251
1252 int FileMapHeader::compute_crc() {
1253 char* start = (char*)this;
1254 // start computing from the field after _crc
1255 char* buf = (char*)&_crc + sizeof(_crc);
1256 size_t sz = sizeof(FileMapHeader) - (buf - start);
1257 int crc = ClassLoader::crc32(0, buf, (jint)sz);
1258 return crc;
1259 }
1260
1261 // This function should only be called during run time with UseSharedSpaces enabled.
1262 bool FileMapHeader::validate() {
1263 if (VerifySharedSpaces && compute_crc() != _crc) {
1264 FileMapInfo::fail_continue("Header checksum verification failed.");
1265 return false;
1266 }
1267
1268 if (!Arguments::has_jimage()) {
1332 if (!PrintSharedArchiveAndExit) {
1333 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
1334 status = false;
1335 }
1336 }
1337 }
1338
1339 if (_paths_misc_info != NULL) {
1340 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
1341 _paths_misc_info = NULL;
1342 }
1343 return status;
1344 }
1345
1346 // Check if a given address is within one of the shared regions
1347 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1348 assert(idx == MetaspaceShared::ro ||
1349 idx == MetaspaceShared::rw ||
1350 idx == MetaspaceShared::mc ||
1351 idx == MetaspaceShared::md, "invalid region index");
1352 char* base = region_addr(idx);
1353 if (p >= base && p < base + space_at(idx)->_used) {
1354 return true;
1355 }
1356 return false;
1357 }
1358
1359 // Unmap mapped regions of shared space.
1360 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1361 MetaspaceObj::set_shared_metaspace_range(NULL, NULL);
1362
1363 FileMapInfo *map_info = FileMapInfo::current_info();
1364 if (map_info) {
1365 map_info->fail_continue("%s", msg);
1366 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
1367 if (!MetaspaceShared::is_heap_region(i)) {
1368 char *addr = map_info->region_addr(i);
1369 if (addr != NULL) {
1370 map_info->unmap_region(i);
1371 map_info->space_at(i)->_addr._base = NULL;
1372 }
1373 }
1374 }
1375 // Dealloc the archive heap regions only without unmapping. The regions are part
1376 // of the java heap. Unmapping of the heap regions are managed by GC.
1377 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1378 num_open_archive_heap_ranges,
1379 true);
1380 map_info->dealloc_archive_heap_regions(string_ranges,
1381 num_string_ranges,
1382 false);
1383 } else if (DumpSharedSpaces) {
1384 fail_stop("%s", msg);
1385 }
1386 }
|