1 /* 2 * Copyright (c) 2021, 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 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/archiveHeapLoader.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/aotLinkedClassBulkLoader.hpp" 29 #include "classfile/classLoader.hpp" 30 #include "classfile/classLoaderData.hpp" 31 #include "classfile/dictionary.hpp" 32 #include "classfile/javaClasses.hpp" 33 #include "classfile/systemDictionary.hpp" 34 #include "classfile/vmClasses.hpp" 35 #include "classfile/vmSymbols.hpp" 36 #include "gc/shared/collectedHeap.hpp" 37 #include "memory/metaspaceClosure.hpp" 38 #include "memory/universe.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/instanceRefKlass.hpp" 41 #include "oops/instanceStackChunkKlass.hpp" 42 #include "prims/jvmtiExport.hpp" 43 #include "runtime/globals.hpp" 44 45 InstanceKlass* vmClasses::_klasses[static_cast<int>(vmClassID::LIMIT)] 46 = { nullptr /*, nullptr...*/ }; 47 InstanceKlass* vmClasses::_box_klasses[T_VOID+1] = { nullptr /*, nullptr...*/ }; 48 49 bool vmClasses::is_loaded(InstanceKlass* klass) { 50 return klass != nullptr && klass->is_loaded(); 51 } 52 53 // Compact table of the vmSymbolIDs of all the VM classes (stored as short to save space) 54 static const short vm_class_name_ids[] = { 55 #define VM_CLASS_NAME(name, symbol) ((short)VM_SYMBOL_ENUM_NAME(symbol)), 56 VM_CLASSES_DO(VM_CLASS_NAME) 57 #undef VM_CLASS_NAME 58 0 59 }; 60 61 62 #ifdef ASSERT 63 bool vmClasses::contain(Symbol* class_name) { 64 int sid; 65 for (int i = 0; (sid = vm_class_name_ids[i]) != 0; i++) { 66 Symbol* symbol = vmSymbols::symbol_at(vmSymbols::as_SID(sid)); 67 if (class_name == symbol) { 68 return true; 69 } 70 } 71 return false; 72 } 73 74 bool vmClasses::contain(Klass* k) { 75 return contain(k->name()); 76 } 77 #endif 78 79 bool vmClasses::resolve(vmClassID id, TRAPS) { 80 InstanceKlass** klassp = &_klasses[as_int(id)]; 81 82 #if INCLUDE_CDS 83 if (CDSConfig::is_using_archive() && !JvmtiExport::should_post_class_prepare()) { 84 InstanceKlass* k = *klassp; 85 assert(k->is_shared_boot_class(), "must be"); 86 87 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 88 resolve_shared_class(k, loader_data, Handle(), CHECK_false); 89 return true; 90 } 91 #endif // INCLUDE_CDS 92 93 if (!is_loaded(*klassp)) { 94 int sid = vm_class_name_ids[as_int(id)]; 95 Symbol* symbol = vmSymbols::symbol_at(vmSymbols::as_SID(sid)); 96 Klass* k = SystemDictionary::resolve_or_fail(symbol, true, CHECK_false); 97 (*klassp) = InstanceKlass::cast(k); 98 } 99 return ((*klassp) != nullptr); 100 } 101 102 void vmClasses::resolve_until(vmClassID limit_id, vmClassID &start_id, TRAPS) { 103 assert((int)start_id <= (int)limit_id, "IDs are out of order!"); 104 for (auto id : EnumRange<vmClassID>{start_id, limit_id}) { // (inclusive start, exclusive end) 105 resolve(id, CHECK); 106 } 107 108 // move the starting value forward to the limit: 109 start_id = limit_id; 110 } 111 112 void vmClasses::resolve_all(TRAPS) { 113 assert(!Object_klass_loaded(), "well-known classes should only be initialized once"); 114 115 // Create the ModuleEntry for java.base. This call needs to be done here, 116 // after vmSymbols::initialize() is called but before any classes are pre-loaded. 117 ClassLoader::classLoader_init2(THREAD); 118 119 // Preload commonly used klasses 120 vmClassID scan = vmClassID::FIRST; 121 // first do Object, then String, Class 122 resolve_through(VM_CLASS_ID(Object_klass), scan, CHECK); 123 CollectedHeap::set_filler_object_klass(vmClasses::Object_klass()); 124 #if INCLUDE_CDS 125 if (CDSConfig::is_using_archive()) { 126 // It's unsafe to access the archived heap regions before they 127 // are fixed up, so we must do the fixup as early as possible 128 // before the archived java objects are accessed by functions 129 // such as java_lang_Class::restore_archived_mirror and 130 // ConstantPool::restore_unshareable_info (restores the archived 131 // resolved_references array object). 132 // 133 // ArchiveHeapLoader::fixup_regions fills the empty 134 // spaces in the archived heap regions and may use 135 // vmClasses::Object_klass(), so we can do this only after 136 // Object_klass is resolved. See the above resolve_through() 137 // call. No mirror objects are accessed/restored in the above call. 138 // Mirrors are restored after java.lang.Class is loaded. 139 ArchiveHeapLoader::fixup_region(); 140 141 // Initialize the constant pool for the Object_class 142 assert(Object_klass()->is_shared(), "must be"); 143 Object_klass()->constants()->restore_unshareable_info(CHECK); 144 resolve_through(VM_CLASS_ID(Class_klass), scan, CHECK); 145 } else 146 #endif 147 { 148 resolve_through(VM_CLASS_ID(Class_klass), scan, CHECK); 149 } 150 151 assert(vmClasses::Object_klass() != nullptr, "well-known classes should now be initialized"); 152 153 java_lang_Object::register_natives(CHECK); 154 155 // Calculate offsets for String and Class classes since they are loaded and 156 // can be used after this point. These are no-op when CDS is enabled. 157 java_lang_String::compute_offsets(); 158 java_lang_Class::compute_offsets(); 159 160 // Fixup mirrors for classes loaded before java.lang.Class. 161 Universe::initialize_basic_type_mirrors(CHECK); 162 Universe::fixup_mirrors(CHECK); 163 164 if (CDSConfig::is_using_archive()) { 165 // These should already have been initialized during CDS dump. 166 assert(vmClasses::Reference_klass()->reference_type() == REF_NONE, "sanity"); 167 assert(vmClasses::SoftReference_klass()->reference_type() == REF_SOFT, "sanity"); 168 assert(vmClasses::WeakReference_klass()->reference_type() == REF_WEAK, "sanity"); 169 assert(vmClasses::FinalReference_klass()->reference_type() == REF_FINAL, "sanity"); 170 assert(vmClasses::PhantomReference_klass()->reference_type() == REF_PHANTOM, "sanity"); 171 } else { 172 // If CDS is not enabled, the references classes must be initialized in 173 // this order before the rest of the vmClasses can be resolved. 174 resolve_through(VM_CLASS_ID(Reference_klass), scan, CHECK); 175 176 // The offsets for jlr.Reference must be computed before 177 // InstanceRefKlass::update_nonstatic_oop_maps is called. That function uses 178 // the offsets to remove the referent and discovered fields from the oop maps, 179 // as they are treated in a special way by the GC. Removing these oops from the 180 // oop maps must be done before the usual subclasses of jlr.Reference are loaded. 181 java_lang_ref_Reference::compute_offsets(); 182 183 // Preload ref klasses and set reference types 184 InstanceRefKlass::update_nonstatic_oop_maps(vmClasses::Reference_klass()); 185 186 resolve_through(VM_CLASS_ID(PhantomReference_klass), scan, CHECK); 187 } 188 189 resolve_until(vmClassID::LIMIT, scan, CHECK); 190 191 CollectedHeap::set_filler_object_klass(vmClasses::FillerObject_klass()); 192 193 _box_klasses[T_BOOLEAN] = vmClasses::Boolean_klass(); 194 _box_klasses[T_CHAR] = vmClasses::Character_klass(); 195 _box_klasses[T_FLOAT] = vmClasses::Float_klass(); 196 _box_klasses[T_DOUBLE] = vmClasses::Double_klass(); 197 _box_klasses[T_BYTE] = vmClasses::Byte_klass(); 198 _box_klasses[T_SHORT] = vmClasses::Short_klass(); 199 _box_klasses[T_INT] = vmClasses::Integer_klass(); 200 _box_klasses[T_LONG] = vmClasses::Long_klass(); 201 202 #ifdef ASSERT 203 if (CDSConfig::is_using_archive()) { 204 JVMTI_ONLY(assert(JvmtiExport::is_early_phase(), 205 "All well known classes must be resolved in JVMTI early phase")); 206 for (auto id : EnumRange<vmClassID>{}) { 207 InstanceKlass* k = _klasses[as_int(id)]; 208 assert(k->is_shared(), "must not be replaced by JVMTI class file load hook"); 209 } 210 } 211 #endif 212 213 InstanceStackChunkKlass::init_offset_of_stack(); 214 if (CDSConfig::is_using_aot_linked_classes()) { 215 AOTLinkedClassBulkLoader::load_javabase_boot_classes(THREAD); 216 } 217 } 218 219 #if INCLUDE_CDS 220 221 void vmClasses::resolve_shared_class(InstanceKlass* klass, ClassLoaderData* loader_data, Handle domain, TRAPS) { 222 assert(!Universe::is_fully_initialized(), "We can make short cuts only during VM initialization"); 223 assert(klass->is_shared(), "Must be shared class"); 224 if (klass->class_loader_data() != nullptr) { 225 return; 226 } 227 228 // add super and interfaces first 229 Klass* super = klass->super(); 230 if (super != nullptr && super->class_loader_data() == nullptr) { 231 assert(super->is_instance_klass(), "Super should be instance klass"); 232 resolve_shared_class(InstanceKlass::cast(super), loader_data, domain, CHECK); 233 } 234 235 Array<InstanceKlass*>* ifs = klass->local_interfaces(); 236 for (int i = 0; i < ifs->length(); i++) { 237 InstanceKlass* ik = ifs->at(i); 238 if (ik->class_loader_data() == nullptr) { 239 resolve_shared_class(ik, loader_data, domain, CHECK); 240 } 241 } 242 243 klass->restore_unshareable_info(loader_data, domain, nullptr, THREAD); 244 SystemDictionary::load_shared_class_misc(klass, loader_data); 245 Dictionary* dictionary = loader_data->dictionary(); 246 dictionary->add_klass(THREAD, klass->name(), klass); 247 klass->add_to_hierarchy(THREAD); 248 assert(klass->is_loaded(), "Must be in at least loaded state"); 249 } 250 251 #endif // INCLUDE_CDS 252 253 // Tells if a given klass is a box (wrapper class, such as java.lang.Integer). 254 // If so, returns the basic type it holds. If not, returns T_OBJECT. 255 BasicType vmClasses::box_klass_type(Klass* k) { 256 assert(k != nullptr, ""); 257 for (int i = T_BOOLEAN; i < T_VOID+1; i++) { 258 if (_box_klasses[i] == k) 259 return (BasicType)i; 260 } 261 return T_OBJECT; 262 }