1 /* 2 * Copyright (c) 1997, 2025, 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 #ifndef SHARE_CLASSFILE_SYSTEMDICTIONARY_HPP 26 #define SHARE_CLASSFILE_SYSTEMDICTIONARY_HPP 27 28 #include "oops/oopHandle.hpp" 29 #include "runtime/handles.hpp" 30 #include "runtime/signature.hpp" 31 #include "utilities/vmEnums.hpp" 32 33 // The dictionary in each ClassLoaderData stores all loaded classes, either 34 // initiatied by its class loader or defined by its class loader: 35 // 36 // class loader -> ClassLoaderData -> Loaded and initiated loaded classes 37 // 38 // Classes are loaded lazily. The default VM class loader is 39 // represented as null. 40 41 // The underlying data structure is a concurrent hash table (Dictionary) per 42 // ClassLoaderData with a fixed number of buckets. During loading the 43 // class loader object is locked, (for the VM loader a private lock object is used). 44 // The global SystemDictionary_lock is held for all additions into the ClassLoaderData 45 // dictionaries. TODO: fix lock granularity so that class loading can 46 // be done concurrently, but only by different loaders. 47 // 48 // During loading a placeholder (name, loader) is temporarily placed in 49 // a side data structure, and is used to detect ClassCircularityErrors. 50 // 51 // When class loading is finished, a new entry is added to the dictionary 52 // of the class loader and the placeholder is removed. 53 // 54 // Clients of this class who are interested in finding if a class has 55 // been completely loaded -- not classes in the process of being loaded -- 56 // can read the dictionary unlocked. This is safe because 57 // - entries are only deleted when the class loader is not alive, when the 58 // entire dictionary is deleted. 59 // - entries must be fully formed before they are available to concurrent 60 // readers (we must ensure write ordering) 61 // 62 // Note that placeholders are deleted at any time, as they are removed 63 // when a class is completely loaded. Therefore, readers as well as writers 64 // of placeholders must hold the SystemDictionary_lock. 65 // 66 67 class BootstrapInfo; 68 class ClassFileStream; 69 class ClassLoadInfo; 70 class Dictionary; 71 class PackageEntry; 72 class GCTimer; 73 class EventClassLoad; 74 class Symbol; 75 76 template <class E> class GrowableArray; 77 78 class SystemDictionary : AllStatic { 79 friend class AOTLinkedClassBulkLoader; 80 friend class BootstrapInfo; 81 friend class LambdaProxyClassDictionary; 82 friend class vmClasses; 83 84 public: 85 86 // Returns a class with a given class name and class loader. Loads the 87 // class if needed. If not found a NoClassDefFoundError or a 88 // ClassNotFoundException is thrown, depending on the value on the 89 // throw_error flag. For most uses the throw_error argument should be set 90 // to true. 91 92 static Klass* resolve_or_fail(Symbol* class_name, Handle class_loader, bool throw_error, TRAPS); 93 // Convenient call for null loader and protection domain. 94 static Klass* resolve_or_fail(Symbol* class_name, bool throw_error, TRAPS) { 95 return resolve_or_fail(class_name, Handle(), throw_error, THREAD); 96 } 97 98 // Returns a class with a given class name and class loader. 99 // Loads the class if needed. If not found null is returned. 100 static Klass* resolve_or_null(Symbol* class_name, Handle class_loader, TRAPS); 101 // Version with null loader and protection domain 102 static Klass* resolve_or_null(Symbol* class_name, TRAPS) { 103 return resolve_or_null(class_name, Handle(), THREAD); 104 } 105 106 static InstanceKlass* resolve_with_circularity_detection(Symbol* class_name, 107 Symbol* next_name, 108 Handle class_loader, 109 bool is_superclass, 110 TRAPS); 111 112 // Resolve a superclass or superinterface. Called from ClassFileParser, 113 // parse_interfaces, resolve_instance_class_or_null, load_shared_class 114 // "class_name" is the class whose super class or interface is being resolved. 115 static InstanceKlass* resolve_super_or_fail(Symbol* class_name, Symbol* super_name, 116 Handle class_loader, 117 bool is_superclass, TRAPS) { 118 return resolve_with_circularity_detection(class_name, super_name, class_loader, is_superclass, THREAD); 119 } 120 121 private: 122 // Parse the stream to create a hidden class. 123 // Used by jvm_lookup_define_class. 124 static InstanceKlass* resolve_hidden_class_from_stream(ClassFileStream* st, 125 Symbol* class_name, 126 Handle class_loader, 127 const ClassLoadInfo& cl_info, 128 TRAPS); 129 130 // Resolve a class from stream (called by jni_DefineClass and JVM_DefineClass) 131 // This class is added to the SystemDictionary. 132 static InstanceKlass* resolve_class_from_stream(ClassFileStream* st, 133 Symbol* class_name, 134 Handle class_loader, 135 const ClassLoadInfo& cl_info, 136 TRAPS); 137 138 static oop get_system_class_loader_impl(TRAPS); 139 static oop get_platform_class_loader_impl(TRAPS); 140 141 public: 142 143 // Resolve either a hidden or normal class from a stream of bytes, based on ClassLoadInfo 144 static InstanceKlass* resolve_from_stream(ClassFileStream* st, 145 Symbol* class_name, 146 Handle class_loader, 147 const ClassLoadInfo& cl_info, 148 TRAPS); 149 150 // Lookup an already loaded class. If not found null is returned. 151 static InstanceKlass* find_instance_klass(Thread* current, Symbol* class_name, 152 Handle class_loader); 153 154 // Lookup an already loaded instance or array class. 155 // Do not make any queries to class loaders; consult only the cache. 156 // If not found null is returned. 157 static Klass* find_instance_or_array_klass(Thread* current, Symbol* class_name, 158 Handle class_loader); 159 160 // Lookup an instance or array class that has already been loaded 161 // either into the given class loader, or else into another class 162 // loader that is constrained (via loader constraints) to produce 163 // a consistent class. Do not take protection domains into account. 164 // Do not make any queries to class loaders; consult only the cache. 165 // Return null if the class is not found. 166 // 167 // This function is a strict superset of find_instance_or_array_klass. 168 // This function (the unchecked version) makes a conservative prediction 169 // of the result of the checked version, assuming successful lookup. 170 // If both functions return non-null, they must return the same value. 171 // Also, the unchecked version may sometimes be non-null where the 172 // checked version is null. This can occur in several ways: 173 // 1. No query has yet been made to the class loader. 174 // 2. The class loader was queried, but chose not to delegate. 175 // 3. ClassLoader.checkPackageAccess rejected a proposed protection domain. 176 // 4. Loading was attempted, but there was a linkage error of some sort. 177 // In all of these cases, the loader constraints on this type are 178 // satisfied, and it is safe for classes in the given class loader 179 // to manipulate strongly-typed values of the found class, subject 180 // to local linkage and access checks. 181 static Klass* find_constrained_instance_or_array_klass(Thread* current, 182 Symbol* class_name, 183 Handle class_loader); 184 185 static void classes_do(MetaspaceClosure* it); 186 // Iterate over all methods in all klasses 187 // Will not keep metadata alive. See ClassLoaderDataGraph::methods_do. 188 static void methods_do(void f(Method*)); 189 190 // Garbage collection support 191 192 // Unload (that is, break root links to) all unmarked classes and 193 // loaders. Returns "true" iff something was unloaded. 194 static bool do_unloading(GCTimer* gc_timer); 195 196 // Printing 197 static void print(); 198 static void print_on(outputStream* st); 199 static void dump(outputStream* st, bool verbose); 200 201 // Verification 202 static void verify(); 203 204 // Initialization 205 static void initialize(TRAPS); 206 207 public: 208 // Returns java system loader 209 static oop java_system_loader(); 210 211 // Returns java platform loader 212 static oop java_platform_loader(); 213 214 // Compute the java system and platform loaders 215 static void compute_java_loaders(TRAPS); 216 217 // Register a new class loader 218 static ClassLoaderData* register_loader(Handle class_loader, bool create_mirror_cld = false); 219 220 static void set_system_loader(ClassLoaderData *cld); 221 static void set_platform_loader(ClassLoaderData *cld); 222 223 static Symbol* check_signature_loaders(Symbol* signature, Klass* klass_being_linked, 224 Handle loader1, Handle loader2, bool is_method); 225 226 // JSR 292 227 // find a java.lang.invoke.MethodHandle.invoke* method for a given signature 228 // (asks Java to compute it if necessary, except in a compiler thread) 229 static Method* find_method_handle_invoker(Klass* klass, 230 Symbol* name, 231 Symbol* signature, 232 Klass* accessing_klass, 233 Handle *appendix_result, 234 TRAPS); 235 // for a given signature, find the internal MethodHandle method (linkTo* or invokeBasic) 236 // (does not ask Java, since this is a low-level intrinsic defined by the JVM) 237 static Method* find_method_handle_intrinsic(vmIntrinsicID iid, 238 Symbol* signature, 239 TRAPS); 240 241 static void get_all_method_handle_intrinsics(GrowableArray<Method*>* methods) NOT_CDS_RETURN; 242 static void restore_archived_method_handle_intrinsics() NOT_CDS_RETURN; 243 244 // compute java_mirror (java.lang.Class instance) for a type ("I", "[[B", "LFoo;", etc.) 245 static Handle find_java_mirror_for_type(Symbol* signature, 246 Klass* accessing_klass, 247 SignatureStream::FailureMode failure_mode, 248 TRAPS); 249 250 // find a java.lang.invoke.MethodType object for a given signature 251 // (asks Java to compute it if necessary, except in a compiler thread) 252 static Handle find_method_handle_type(Symbol* signature, 253 Klass* accessing_klass, 254 TRAPS); 255 256 // find a java.lang.Class object for a given signature 257 static Handle find_field_handle_type(Symbol* signature, 258 Klass* accessing_klass, 259 TRAPS); 260 261 // ask Java to compute a java.lang.invoke.MethodHandle object for a given CP entry 262 static Handle link_method_handle_constant(Klass* caller, 263 int ref_kind, //e.g., JVM_REF_invokeVirtual 264 Klass* callee, 265 Symbol* name, 266 Symbol* signature, 267 TRAPS); 268 269 // ask Java to compute a constant by invoking a BSM given a Dynamic_info CP entry 270 static void invoke_bootstrap_method(BootstrapInfo& bootstrap_specifier, TRAPS); 271 272 // Record the error when the first attempt to resolve a reference from a constant 273 // pool entry to a class fails. 274 static void add_resolution_error(const constantPoolHandle& pool, int which, 275 Symbol* error, const char* message, 276 Symbol* cause = nullptr, const char* cause_msg = nullptr); 277 static void delete_resolution_error(ConstantPool* pool); 278 static Symbol* find_resolution_error(const constantPoolHandle& pool, int which, 279 const char** message, 280 Symbol** cause, const char** cause_msg); 281 282 // Record a nest host resolution/validation error 283 static void add_nest_host_error(const constantPoolHandle& pool, int which, 284 const char* message); 285 static const char* find_nest_host_error(const constantPoolHandle& pool, int which); 286 287 static void add_to_initiating_loader(JavaThread* current, InstanceKlass* k, 288 ClassLoaderData* loader_data); 289 290 static OopHandle _java_system_loader; 291 static OopHandle _java_platform_loader; 292 293 private: 294 // Basic loading operations 295 static InstanceKlass* resolve_instance_class_or_null(Symbol* class_name, 296 Handle class_loader, 297 TRAPS); 298 static Klass* resolve_array_class_or_null(Symbol* class_name, 299 Handle class_loader, 300 TRAPS); 301 static void define_instance_class(InstanceKlass* k, Handle class_loader, TRAPS); 302 static InstanceKlass* find_or_define_helper(Symbol* class_name, 303 Handle class_loader, 304 InstanceKlass* k, TRAPS); 305 static InstanceKlass* load_instance_class_impl(Symbol* class_name, Handle class_loader, TRAPS); 306 static InstanceKlass* load_instance_class(Symbol* class_name, 307 Handle class_loader, TRAPS); 308 309 // Class loader constraints 310 static void check_constraints(InstanceKlass* k, ClassLoaderData* loader, 311 bool defining, TRAPS); 312 static void update_dictionary(JavaThread* current, InstanceKlass* k, ClassLoaderData* loader_data); 313 314 static bool is_shared_class_visible(Symbol* class_name, InstanceKlass* ik, 315 PackageEntry* pkg_entry, 316 Handle class_loader); 317 static bool is_shared_class_visible_impl(Symbol* class_name, 318 InstanceKlass* ik, 319 PackageEntry* pkg_entry, 320 Handle class_loader); 321 static bool check_shared_class_super_type(InstanceKlass* klass, InstanceKlass* super, 322 Handle class_loader, 323 bool is_superclass, TRAPS); 324 static bool check_shared_class_super_types(InstanceKlass* ik, Handle class_loader, TRAPS); 325 // Second part of load_shared_class 326 static void load_shared_class_misc(InstanceKlass* ik, ClassLoaderData* loader_data) NOT_CDS_RETURN; 327 static void restore_archived_method_handle_intrinsics_impl(TRAPS) NOT_CDS_RETURN; 328 329 protected: 330 // Used by SystemDictionaryShared and LambdaProxyClassDictionary 331 332 static bool add_loader_constraint(Symbol* name, Klass* klass_being_linked, Handle loader1, 333 Handle loader2); 334 static void post_class_load_event(EventClassLoad* event, const InstanceKlass* k, const ClassLoaderData* init_cld); 335 static bool preload_from_null_free_field(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS); 336 static void try_preload_from_loadable_descriptors(InstanceKlass* ik, Handle class_loader, Symbol* sig, int field_index, TRAPS); 337 static InstanceKlass* load_shared_class(InstanceKlass* ik, 338 Handle class_loader, 339 Handle protection_domain, 340 const ClassFileStream *cfs, 341 PackageEntry* pkg_entry, 342 TRAPS); 343 static Handle get_loader_lock_or_null(Handle class_loader); 344 static InstanceKlass* find_or_define_instance_class(Symbol* class_name, 345 Handle class_loader, 346 InstanceKlass* k, TRAPS); 347 348 public: 349 static bool is_system_class_loader(oop class_loader); 350 static bool is_platform_class_loader(oop class_loader); 351 static bool is_boot_class_loader(oop class_loader) { return class_loader == nullptr; } 352 static bool is_builtin_class_loader(oop class_loader) { 353 return is_boot_class_loader(class_loader) || 354 is_platform_class_loader(class_loader) || 355 is_system_class_loader(class_loader); 356 } 357 // Returns TRUE if the method is a non-public member of class java.lang.Object. 358 static bool is_nonpublic_Object_method(Method* m); 359 360 // Return Symbol or throw exception if name given is can not be a valid Symbol. 361 static Symbol* class_name_symbol(const char* name, Symbol* exception, TRAPS); 362 }; 363 364 #endif // SHARE_CLASSFILE_SYSTEMDICTIONARY_HPP