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