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