1 /*
2 * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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 *
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/os.inline.hpp"
81 #include "runtime/safepointVerifiers.hpp"
82 #include "runtime/sharedRuntime.hpp"
83 #include "runtime/vmOperations.hpp"
84 #include "runtime/vmThread.hpp"
85 #include "sanitizers/leak.hpp"
86 #include "utilities/align.hpp"
87 #include "utilities/bitMap.inline.hpp"
88 #include "utilities/defaultStream.hpp"
89 #include "utilities/ostream.hpp"
91
92 ReservedSpace MetaspaceShared::_symbol_rs;
93 VirtualSpace MetaspaceShared::_symbol_vs;
94 bool MetaspaceShared::_archive_loading_failed = false;
95 bool MetaspaceShared::_remapped_readwrite = false;
96 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
97 intx MetaspaceShared::_relocation_delta;
98 char* MetaspaceShared::_requested_base_address;
99 bool MetaspaceShared::_use_optimized_module_handling = true;
100
101 // The CDS archive is divided into the following regions:
102 // rw - read-write metadata
103 // ro - read-only metadata and read-only tables
104 // hp - heap region
105 // bm - bitmap for relocating the above 7 regions.
106 //
107 // The rw and ro regions are linearly allocated, in the order of rw->ro.
108 // These regions are aligned with MetaspaceShared::core_region_alignment().
109 //
110 // These 2 regions are populated in the following steps:
111 // [0] All classes are loaded in MetaspaceShared::preload_classes(). All metadata are
112 // temporarily allocated outside of the shared regions.
113 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
114 // [2] C++ vtables are copied into the rw region.
115 // [3] ArchiveBuilder copies RW metadata into the rw region.
116 // [4] ArchiveBuilder copies RO metadata into the ro region.
117 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
118 // are copied into the ro region as read-only tables.
119 //
120 // The heap region is populated by HeapShared::archive_objects.
121 //
122 // The bitmap region is used to relocate the ro/rw/hp regions.
123
124 static DumpRegion _symbol_region("symbols");
125
126 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
127 return _symbol_region.allocate(num_bytes);
128 }
129
130 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
131 // such as linux-aarch64 and macos-x64 ...
708 if (end != nullptr) *end = '\0';
709 }
710 size_t classlist_path_len = strlen(default_classlist);
711 if (classlist_path_len >= 3) {
712 if (strcmp(default_classlist + classlist_path_len - 3, "lib") != 0) {
713 if (classlist_path_len < buf_size - 4) {
714 jio_snprintf(default_classlist + classlist_path_len,
715 buf_size - classlist_path_len,
716 "%slib", os::file_separator());
717 classlist_path_len += 4;
718 }
719 }
720 }
721 if (classlist_path_len < buf_size - 10) {
722 jio_snprintf(default_classlist + classlist_path_len,
723 buf_size - classlist_path_len,
724 "%sclasslist", os::file_separator());
725 }
726 }
727
728 void MetaspaceShared::preload_classes(TRAPS) {
729 char default_classlist[JVM_MAXPATHLEN];
730 const char* classlist_path;
731
732 get_default_classlist(default_classlist, sizeof(default_classlist));
733 if (SharedClassListFile == nullptr) {
734 classlist_path = default_classlist;
735 } else {
736 classlist_path = SharedClassListFile;
737 }
738
739 log_info(cds)("Loading classes to share ...");
740 int class_count = ClassListParser::parse_classlist(classlist_path,
741 ClassListParser::_parse_all, CHECK);
742 if (ExtraSharedClassListFile) {
743 class_count += ClassListParser::parse_classlist(ExtraSharedClassListFile,
744 ClassListParser::_parse_all, CHECK);
745 }
746 if (classlist_path != default_classlist) {
747 struct stat statbuf;
748 if (os::stat(default_classlist, &statbuf) == 0) {
749 // File exists, let's use it.
750 class_count += ClassListParser::parse_classlist(default_classlist,
751 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
752 }
753 }
754
755 // Exercise the manifest processing code to ensure classes used by CDS at runtime
756 // are always archived
757 const char* dummy = "Manifest-Version: 1.0\n";
758 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
759
760 log_info(cds)("Loading classes to share: done.");
761 log_info(cds)("Shared spaces: preloaded %d classes", class_count);
762 }
763
764 void MetaspaceShared::preload_and_dump_impl(TRAPS) {
765 preload_classes(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)) {
821 BytecodeVerificationLocal = BytecodeVerificationRemote;
822 }
823 ik->link_class(THREAD);
824 if (HAS_PENDING_EXCEPTION) {
825 ResourceMark rm(THREAD);
826 log_warning(cds)("Preload Warning: Verification failed for %s",
827 ik->external_name());
828 CLEAR_PENDING_EXCEPTION;
829 SystemDictionaryShared::set_class_has_failed_verification(ik);
830 }
831 ik->compute_has_loops_flag_for_methods();
832 BytecodeVerificationLocal = saved;
833 return true;
834 } else {
835 return false;
836 }
837 }
838
839 #if INCLUDE_CDS_JAVA_HEAP
840 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
841 if(!HeapShared::can_write()) {
842 log_info(cds)(
843 "Archived java heap is not supported as UseG1GC "
844 "and UseCompressedClassPointers are required."
845 "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
846 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
847 return;
848 }
849 // Find all the interned strings that should be dumped.
850 int i;
851 for (i = 0; i < klasses->length(); i++) {
852 Klass* k = klasses->at(i);
853 if (k->is_instance_klass()) {
854 InstanceKlass* ik = InstanceKlass::cast(k);
855 if (ik->is_linked()) {
856 ik->constants()->add_dumped_interned_strings();
857 }
858 }
859 }
860 if (_extra_interned_strings != nullptr) {
|
1 /*
2 * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
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 *
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/os.inline.hpp"
83 #include "runtime/safepointVerifiers.hpp"
84 #include "runtime/sharedRuntime.hpp"
85 #include "runtime/vmOperations.hpp"
86 #include "runtime/vmThread.hpp"
87 #include "sanitizers/leak.hpp"
88 #include "utilities/align.hpp"
89 #include "utilities/bitMap.inline.hpp"
90 #include "utilities/defaultStream.hpp"
91 #include "utilities/ostream.hpp"
93
94 ReservedSpace MetaspaceShared::_symbol_rs;
95 VirtualSpace MetaspaceShared::_symbol_vs;
96 bool MetaspaceShared::_archive_loading_failed = false;
97 bool MetaspaceShared::_remapped_readwrite = false;
98 void* MetaspaceShared::_shared_metaspace_static_top = nullptr;
99 intx MetaspaceShared::_relocation_delta;
100 char* MetaspaceShared::_requested_base_address;
101 bool MetaspaceShared::_use_optimized_module_handling = true;
102
103 // The CDS archive is divided into the following regions:
104 // rw - read-write metadata
105 // ro - read-only metadata and read-only tables
106 // hp - heap region
107 // bm - bitmap for relocating the above 7 regions.
108 //
109 // The rw and ro regions are linearly allocated, in the order of rw->ro.
110 // These regions are aligned with MetaspaceShared::core_region_alignment().
111 //
112 // These 2 regions are populated in the following steps:
113 // [0] All classes are loaded in MetaspaceShared::loadable_descriptors(). All metadata are
114 // temporarily allocated outside of the shared regions.
115 // [1] We enter a safepoint and allocate a buffer for the rw/ro regions.
116 // [2] C++ vtables are copied into the rw region.
117 // [3] ArchiveBuilder copies RW metadata into the rw region.
118 // [4] ArchiveBuilder copies RO metadata into the ro region.
119 // [5] SymbolTable, StringTable, SystemDictionary, and a few other read-only data
120 // are copied into the ro region as read-only tables.
121 //
122 // The heap region is populated by HeapShared::archive_objects.
123 //
124 // The bitmap region is used to relocate the ro/rw/hp regions.
125
126 static DumpRegion _symbol_region("symbols");
127
128 char* MetaspaceShared::symbol_space_alloc(size_t num_bytes) {
129 return _symbol_region.allocate(num_bytes);
130 }
131
132 // os::vm_allocation_granularity() is usually 4K for most OSes. However, some platforms
133 // such as linux-aarch64 and macos-x64 ...
710 if (end != nullptr) *end = '\0';
711 }
712 size_t classlist_path_len = strlen(default_classlist);
713 if (classlist_path_len >= 3) {
714 if (strcmp(default_classlist + classlist_path_len - 3, "lib") != 0) {
715 if (classlist_path_len < buf_size - 4) {
716 jio_snprintf(default_classlist + classlist_path_len,
717 buf_size - classlist_path_len,
718 "%slib", os::file_separator());
719 classlist_path_len += 4;
720 }
721 }
722 }
723 if (classlist_path_len < buf_size - 10) {
724 jio_snprintf(default_classlist + classlist_path_len,
725 buf_size - classlist_path_len,
726 "%sclasslist", os::file_separator());
727 }
728 }
729
730 void MetaspaceShared::loadable_descriptors(TRAPS) {
731 char default_classlist[JVM_MAXPATHLEN];
732 const char* classlist_path;
733
734 get_default_classlist(default_classlist, sizeof(default_classlist));
735 if (SharedClassListFile == nullptr) {
736 classlist_path = default_classlist;
737 } else {
738 classlist_path = SharedClassListFile;
739 }
740
741 log_info(cds)("Loading classes to share ...");
742 int class_count = ClassListParser::parse_classlist(classlist_path,
743 ClassListParser::_parse_all, CHECK);
744 if (ExtraSharedClassListFile) {
745 class_count += ClassListParser::parse_classlist(ExtraSharedClassListFile,
746 ClassListParser::_parse_all, CHECK);
747 }
748 if (classlist_path != default_classlist) {
749 struct stat statbuf;
750 if (os::stat(default_classlist, &statbuf) == 0) {
751 // File exists, let's use it.
752 class_count += ClassListParser::parse_classlist(default_classlist,
753 ClassListParser::_parse_lambda_forms_invokers_only, CHECK);
754 }
755 }
756
757 // Exercise the manifest processing code to ensure classes used by CDS at runtime
758 // are always archived
759 const char* dummy = "Manifest-Version: 1.0\n";
760 CDSProtectionDomain::create_jar_manifest(dummy, strlen(dummy), CHECK);
761
762 log_info(cds)("Loading classes to share: done.");
763 log_info(cds)("Shared spaces: preloaded %d classes", class_count);
764 }
765
766 void MetaspaceShared::preload_and_dump_impl(TRAPS) {
767 loadable_descriptors(CHECK);
768
769 if (SharedArchiveConfigFile) {
770 log_info(cds)("Reading extra data from %s ...", SharedArchiveConfigFile);
771 read_extra_data(THREAD, SharedArchiveConfigFile);
772 log_info(cds)("Reading extra data: done.");
773 }
774
775 // Rewrite and link classes
776 log_info(cds)("Rewriting and linking classes ...");
777
778 // Link any classes which got missed. This would happen if we have loaded classes that
779 // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
780 // fails verification, all other interfaces that were not specified in the classlist but
781 // are implemented by K are not verified.
782 link_shared_classes(false/*not from jcmd*/, CHECK);
783 log_info(cds)("Rewriting and linking classes: done");
784
785 #if INCLUDE_CDS_JAVA_HEAP
786 if (CDSConfig::is_dumping_heap()) {
787 if (!HeapShared::is_archived_boot_layer_available(THREAD)) {
823 BytecodeVerificationLocal = BytecodeVerificationRemote;
824 }
825 ik->link_class(THREAD);
826 if (HAS_PENDING_EXCEPTION) {
827 ResourceMark rm(THREAD);
828 log_warning(cds)("Preload Warning: Verification failed for %s",
829 ik->external_name());
830 CLEAR_PENDING_EXCEPTION;
831 SystemDictionaryShared::set_class_has_failed_verification(ik);
832 }
833 ik->compute_has_loops_flag_for_methods();
834 BytecodeVerificationLocal = saved;
835 return true;
836 } else {
837 return false;
838 }
839 }
840
841 #if INCLUDE_CDS_JAVA_HEAP
842 void VM_PopulateDumpSharedSpace::dump_java_heap_objects(GrowableArray<Klass*>* klasses) {
843 if (CDSConfig::is_valhalla_preview()) {
844 log_info(cds)("Archived java heap is not yet supported with Valhalla preview");
845 return;
846 }
847 if(!HeapShared::can_write()) {
848 log_info(cds)(
849 "Archived java heap is not supported as UseG1GC "
850 "and UseCompressedClassPointers are required."
851 "Current settings: UseG1GC=%s, UseCompressedClassPointers=%s.",
852 BOOL_TO_STR(UseG1GC), BOOL_TO_STR(UseCompressedClassPointers));
853 return;
854 }
855 // Find all the interned strings that should be dumped.
856 int i;
857 for (i = 0; i < klasses->length(); i++) {
858 Klass* k = klasses->at(i);
859 if (k->is_instance_klass()) {
860 InstanceKlass* ik = InstanceKlass::cast(k);
861 if (ik->is_linked()) {
862 ik->constants()->add_dumped_interned_strings();
863 }
864 }
865 }
866 if (_extra_interned_strings != nullptr) {
|