50 #include "classfile/stringTable.hpp"
51 #include "classfile/symbolTable.hpp"
52 #include "classfile/systemDictionary.hpp"
53 #include "classfile/systemDictionaryShared.hpp"
54 #include "classfile/vmClasses.hpp"
55 #include "classfile/vmSymbols.hpp"
56 #include "code/codeCache.hpp"
57 #include "gc/shared/gcVMOperations.hpp"
58 #include "interpreter/bytecodeStream.hpp"
59 #include "interpreter/bytecodes.hpp"
60 #include "jvm_io.h"
61 #include "logging/log.hpp"
62 #include "logging/logMessage.hpp"
63 #include "logging/logStream.hpp"
64 #include "memory/metaspace.hpp"
65 #include "memory/metaspaceClosure.hpp"
66 #include "memory/resourceArea.hpp"
67 #include "memory/universe.hpp"
68 #include "nmt/memTracker.hpp"
69 #include "oops/compressedKlass.hpp"
70 #include "oops/instanceMirrorKlass.hpp"
71 #include "oops/klass.inline.hpp"
72 #include "oops/objArrayOop.hpp"
73 #include "oops/oop.inline.hpp"
74 #include "oops/oopHandle.hpp"
75 #include "prims/jvmtiExport.hpp"
76 #include "runtime/arguments.hpp"
77 #include "runtime/globals.hpp"
78 #include "runtime/globals_extension.hpp"
79 #include "runtime/handles.inline.hpp"
80 #include "runtime/javaCalls.hpp"
81 #include "runtime/os.inline.hpp"
82 #include "runtime/safepointVerifiers.hpp"
83 #include "runtime/sharedRuntime.hpp"
84 #include "runtime/vmOperations.hpp"
85 #include "runtime/vmThread.hpp"
86 #include "sanitizers/leak.hpp"
87 #include "utilities/align.hpp"
88 #include "utilities/bitMap.inline.hpp"
89 #include "utilities/defaultStream.hpp"
92
93 ReservedSpace MetaspaceShared::_symbol_rs;
94 VirtualSpace MetaspaceShared::_symbol_vs;
95 bool MetaspaceShared::_archive_loading_failed = false;
96 bool MetaspaceShared::_remapped_readwrite = false;
97 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
98 intx MetaspaceShared::_relocation_delta;
99 char* MetaspaceShared::_requested_base_address;
100 bool MetaspaceShared::_use_optimized_module_handling = true;
101
102 // The CDS archive is divided into the following regions:
103 // rw - read-write metadata
104 // ro - read-only metadata and read-only tables
105 // hp - heap region
106 // bm - bitmap for relocating the above 7 regions.
107 //
108 // The rw and ro regions are linearly allocated, in the order of rw->ro.
109 // These regions are aligned with MetaspaceShared::core_region_alignment().
110 //
111 // These 2 regions are populated in the following steps:
112 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
113 // temporarily allocated outside of the shared regions.
114 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
115 // [2] C++ vtables are copied into the rw region.
116 // [3] ArchiveBuilder copies RW metadata into the rw region.
117 // [4] ArchiveBuilder copies RO metadata into the ro region.
118 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
119 // are copied into the ro region as read-only tables.
120 //
121 // The heap region is populated by HeapShared::archive_objects.
122 //
123 // The bitmap region is used to relocate the ro/rw/hp regions.
124
125 static DumpRegion _symbol_region("symbols");
126
127 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
128 return _symbol_region.allocate(num_bytes);
129 }
130
131 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
132 // such as linux-aarch64 and macos-x64 ...
707 if (end != nullptr) *end = '\0';
708 }
709 size_t classlist_path_len = strlen(default_classlist);
710 if (classlist_path_len >= 3) {
711 if (strcmp(default_classlist + classlist_path_len - 3, "lib") != 0) {
712 if (classlist_path_len < buf_size - 4) {
713 jio_snprintf(default_classlist + classlist_path_len,
714 buf_size - classlist_path_len,
715 "%slib", os::file_separator());
716 classlist_path_len += 4;
717 }
718 }
719 }
720 if (classlist_path_len < buf_size - 10) {
721 jio_snprintf(default_classlist + classlist_path_len,
722 buf_size - classlist_path_len,
723 "%sclasslist", os::file_separator());
724 }
725 }
726
727 void MetaspaceShared::preload_classes(TRAPS) {
728 char default_classlist[JVM_MAXPATHLEN];
729 const char* classlist_path;
730
731 get_default_classlist(default_classlist, sizeof(default_classlist));
732 if (SharedClassListFile == nullptr) {
733 classlist_path = default_classlist;
734 } else {
735 classlist_path = SharedClassListFile;
736 }
737
738 log_info(cds)("Loading classes to share ...");
739 ClassListParser::parse_classlist(classlist_path,
740 ClassListParser::_parse_all, CHECK);
741 if (ExtraSharedClassListFile) {
742 ClassListParser::parse_classlist(ExtraSharedClassListFile,
743 ClassListParser::_parse_all, CHECK);
744 }
745 if (classlist_path != default_classlist) {
746 struct stat statbuf;
747 if (os::stat(default_classlist, &statbuf) == 0) {
748 // File exists, let's use it.
749 ClassListParser::parse_classlist(default_classlist,
750 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
751 }
752 }
753
754 // Exercise the manifest processing code to ensure classes used by CDS at runtime
755 // are always archived
756 const char* dummy = "Manifest-Version: 1.0\n";
757 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
758
759 log_info(cds)("Loading classes to share: done.");
760 }
761
762 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
763 preload_classes(CHECK);
764
765 if (SharedArchiveConfigFile) {
766 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
767 read_extra_data(THREAD, SharedArchiveConfigFile);
768 log_info(cds)("Reading extra data: done.");
769 }
770
771 // Rewrite and link classes
772 log_info(cds)("Rewriting and linking classes ...");
773
774 // Link any classes which got missed. This would happen if we have loaded classes that
775 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
776 // fails verification, all other interfaces that were not specified in the classlist but
777 // are implemented by K are not verified.
778 link_shared_classes(false/*not from jcmd*/, CHECK);
779 log_info(cds)("Rewriting and linking classes: done");
780
781 #if INCLUDE_CDS_JAVA_HEAP
782 if (CDSConfig::is_dumping_heap()) {
783 if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
854 BytecodeVerificationLocal = BytecodeVerificationRemote;
855 }
856 ik->link_class(THREAD);
857 if (HAS_PENDING_EXCEPTION) {
858 ResourceMark rm(THREAD);
859 log_warning(cds)("Preload Warning: Verification failed for %s",
860 ik->external_name());
861 CLEAR_PENDING_EXCEPTION;
862 SystemDictionaryShared::set_class_has_failed_verification(ik);
863 }
864 ik->compute_has_loops_flag_for_methods();
865 BytecodeVerificationLocal = saved;
866 return true;
867 } else {
868 return false;
869 }
870 }
871
872 #if INCLUDE_CDS_JAVA_HEAP
873 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
874 if(!HeapShared::can_write()) {
875 log_info(cds)(
876 "Archived java heap is not supported as UseG1GC "
877 "and UseCompressedClassPointers are required."
878 "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
879 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
880 return;
881 }
882 // Find all the interned strings that should be dumped.
883 int i;
884 for (i = 0; i < klasses->length(); i++) {
885 Klass* k = klasses->at(i);
886 if (k->is_instance_klass()) {
887 InstanceKlass* ik = InstanceKlass::cast(k);
888 if (ik->is_linked()) {
889 ik->constants()->add_dumped_interned_strings();
890 }
891 }
892 }
893 if (_extra_interned_strings != nullptr) {
|
50 #include "classfile/stringTable.hpp"
51 #include "classfile/symbolTable.hpp"
52 #include "classfile/systemDictionary.hpp"
53 #include "classfile/systemDictionaryShared.hpp"
54 #include "classfile/vmClasses.hpp"
55 #include "classfile/vmSymbols.hpp"
56 #include "code/codeCache.hpp"
57 #include "gc/shared/gcVMOperations.hpp"
58 #include "interpreter/bytecodeStream.hpp"
59 #include "interpreter/bytecodes.hpp"
60 #include "jvm_io.h"
61 #include "logging/log.hpp"
62 #include "logging/logMessage.hpp"
63 #include "logging/logStream.hpp"
64 #include "memory/metaspace.hpp"
65 #include "memory/metaspaceClosure.hpp"
66 #include "memory/resourceArea.hpp"
67 #include "memory/universe.hpp"
68 #include "nmt/memTracker.hpp"
69 #include "oops/compressedKlass.hpp"
70 #include "oops/flatArrayKlass.hpp"
71 #include "oops/inlineKlass.hpp"
72 #include "oops/instanceMirrorKlass.hpp"
73 #include "oops/klass.inline.hpp"
74 #include "oops/objArrayOop.hpp"
75 #include "oops/oop.inline.hpp"
76 #include "oops/oopHandle.hpp"
77 #include "prims/jvmtiExport.hpp"
78 #include "runtime/arguments.hpp"
79 #include "runtime/globals.hpp"
80 #include "runtime/globals_extension.hpp"
81 #include "runtime/handles.inline.hpp"
82 #include "runtime/javaCalls.hpp"
83 #include "runtime/os.inline.hpp"
84 #include "runtime/safepointVerifiers.hpp"
85 #include "runtime/sharedRuntime.hpp"
86 #include "runtime/vmOperations.hpp"
87 #include "runtime/vmThread.hpp"
88 #include "sanitizers/leak.hpp"
89 #include "utilities/align.hpp"
90 #include "utilities/bitMap.inline.hpp"
91 #include "utilities/defaultStream.hpp"
94
95 ReservedSpace MetaspaceShared::_symbol_rs;
96 VirtualSpace MetaspaceShared::_symbol_vs;
97 bool MetaspaceShared::_archive_loading_failed = false;
98 bool MetaspaceShared::_remapped_readwrite = false;
99 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
100 intx MetaspaceShared::_relocation_delta;
101 char* MetaspaceShared::_requested_base_address;
102 bool MetaspaceShared::_use_optimized_module_handling = true;
103
104 // The CDS archive is divided into the following regions:
105 // rw - read-write metadata
106 // ro - read-only metadata and read-only tables
107 // hp - heap region
108 // bm - bitmap for relocating the above 7 regions.
109 //
110 // The rw and ro regions are linearly allocated, in the order of rw->ro.
111 // These regions are aligned with MetaspaceShared::core_region_alignment().
112 //
113 // These 2 regions are populated in the following steps:
114 // [0] All classes are loaded in MetaspaceShared::loadable_descriptors(). All metadata are
115 // temporarily allocated outside of the shared regions.
116 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
117 // [2] C++ vtables are copied into the rw region.
118 // [3] ArchiveBuilder copies RW metadata into the rw region.
119 // [4] ArchiveBuilder copies RO metadata into the ro region.
120 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
121 // are copied into the ro region as read-only tables.
122 //
123 // The heap region is populated by HeapShared::archive_objects.
124 //
125 // The bitmap region is used to relocate the ro/rw/hp regions.
126
127 static DumpRegion _symbol_region("symbols");
128
129 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
130 return _symbol_region.allocate(num_bytes);
131 }
132
133 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
134 // such as linux-aarch64 and macos-x64 ...
709 if (end != nullptr) *end = '\0';
710 }
711 size_t classlist_path_len = strlen(default_classlist);
712 if (classlist_path_len >= 3) {
713 if (strcmp(default_classlist + classlist_path_len - 3, "lib") != 0) {
714 if (classlist_path_len < buf_size - 4) {
715 jio_snprintf(default_classlist + classlist_path_len,
716 buf_size - classlist_path_len,
717 "%slib", os::file_separator());
718 classlist_path_len += 4;
719 }
720 }
721 }
722 if (classlist_path_len < buf_size - 10) {
723 jio_snprintf(default_classlist + classlist_path_len,
724 buf_size - classlist_path_len,
725 "%sclasslist", os::file_separator());
726 }
727 }
728
729 void MetaspaceShared::loadable_descriptors(TRAPS) {
730 char default_classlist[JVM_MAXPATHLEN];
731 const char* classlist_path;
732
733 get_default_classlist(default_classlist, sizeof(default_classlist));
734 if (SharedClassListFile == nullptr) {
735 classlist_path = default_classlist;
736 } else {
737 classlist_path = SharedClassListFile;
738 }
739
740 log_info(cds)("Loading classes to share ...");
741 ClassListParser::parse_classlist(classlist_path,
742 ClassListParser::_parse_all, CHECK);
743 if (ExtraSharedClassListFile) {
744 ClassListParser::parse_classlist(ExtraSharedClassListFile,
745 ClassListParser::_parse_all, CHECK);
746 }
747 if (classlist_path != default_classlist) {
748 struct stat statbuf;
749 if (os::stat(default_classlist, &statbuf) == 0) {
750 // File exists, let's use it.
751 ClassListParser::parse_classlist(default_classlist,
752 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
753 }
754 }
755
756 // Exercise the manifest processing code to ensure classes used by CDS at runtime
757 // are always archived
758 const char* dummy = "Manifest-Version: 1.0\n";
759 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
760
761 log_info(cds)("Loading classes to share: done.");
762 }
763
764 void MetaspaceShared::preload_and_dump_impl(StaticArchiveBuilder& builder, TRAPS) {
765 loadable_descriptors(CHECK);
766
767 if (SharedArchiveConfigFile) {
768 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
769 read_extra_data(THREAD, SharedArchiveConfigFile);
770 log_info(cds)("Reading extra data: done.");
771 }
772
773 // Rewrite and link classes
774 log_info(cds)("Rewriting and linking classes ...");
775
776 // Link any classes which got missed. This would happen if we have loaded classes that
777 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
778 // fails verification, all other interfaces that were not specified in the classlist but
779 // are implemented by K are not verified.
780 link_shared_classes(false/*not from jcmd*/, CHECK);
781 log_info(cds)("Rewriting and linking classes: done");
782
783 #if INCLUDE_CDS_JAVA_HEAP
784 if (CDSConfig::is_dumping_heap()) {
785 if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
856 BytecodeVerificationLocal = BytecodeVerificationRemote;
857 }
858 ik->link_class(THREAD);
859 if (HAS_PENDING_EXCEPTION) {
860 ResourceMark rm(THREAD);
861 log_warning(cds)("Preload Warning: Verification failed for %s",
862 ik->external_name());
863 CLEAR_PENDING_EXCEPTION;
864 SystemDictionaryShared::set_class_has_failed_verification(ik);
865 }
866 ik->compute_has_loops_flag_for_methods();
867 BytecodeVerificationLocal = saved;
868 return true;
869 } else {
870 return false;
871 }
872 }
873
874 #if INCLUDE_CDS_JAVA_HEAP
875 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
876 if (CDSConfig::is_valhalla_preview()) {
877 log_info(cds)("Archived java heap is not yet supported with Valhalla preview");
878 return;
879 }
880 if(!HeapShared::can_write()) {
881 log_info(cds)(
882 "Archived java heap is not supported as UseG1GC "
883 "and UseCompressedClassPointers are required."
884 "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
885 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
886 return;
887 }
888 // Find all the interned strings that should be dumped.
889 int i;
890 for (i = 0; i < klasses->length(); i++) {
891 Klass* k = klasses->at(i);
892 if (k->is_instance_klass()) {
893 InstanceKlass* ik = InstanceKlass::cast(k);
894 if (ik->is_linked()) {
895 ik->constants()->add_dumped_interned_strings();
896 }
897 }
898 }
899 if (_extra_interned_strings != nullptr) {
|