1 /* 2 * Copyright (c) 2014, 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_SYSTEMDICTIONARYSHARED_HPP 26 #define SHARE_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP 27 28 #include "cds/cds_globals.hpp" 29 #include "cds/filemap.hpp" 30 #include "cds/dumpTimeClassInfo.hpp" 31 #include "cds/runTimeClassInfo.hpp" 32 #include "classfile/classLoaderData.hpp" 33 #include "classfile/packageEntry.hpp" 34 #include "classfile/systemDictionary.hpp" 35 #include "oops/klass.hpp" 36 #include "oops/oopHandle.hpp" 37 38 39 /*=============================================================================== 40 41 Handling of the classes in the AppCDS archive 42 43 To ensure safety and to simplify the implementation, archived classes are 44 "segregated" into 2 types. The following rules describe how they 45 are stored and looked up. 46 47 [1] Category of archived classes 48 49 There are 2 disjoint groups of classes stored in the AppCDS archive: 50 51 BUILTIN: These classes may be defined ONLY by the BOOT/PLATFORM/APP 52 loaders. 53 54 UNREGISTERED: These classes may be defined ONLY by a ClassLoader 55 instance that's not listed above (using fingerprint matching) 56 57 [2] How classes from different categories are specified in the classlist: 58 59 Starting from JDK9, each class in the classlist may be specified with 60 these keywords: "id", "super", "interfaces", "loader" and "source". 61 62 63 BUILTIN Only the "id" keyword may be (optionally) specified. All other 64 keywords are forbidden. 65 66 The named class is looked up from the jimage and from 67 Xbootclasspath/a and CLASSPATH. 68 69 UNREGISTERED: The "id", "super", and "source" keywords must all be 70 specified. 71 72 The "interfaces" keyword must be specified if the class implements 73 one or more local interfaces. The "interfaces" keyword must not be 74 specified if the class does not implement local interfaces. 75 76 The named class is looked up from the location specified in the 77 "source" keyword. 78 79 Example classlist: 80 81 # BUILTIN 82 java/lang/Object id: 0 83 java/lang/Cloneable id: 1 84 java/lang/String 85 86 # UNREGISTERED 87 Bar id: 3 super: 0 interfaces: 1 source: /foo.jar 88 89 90 [3] Identifying the category of archived classes 91 92 BUILTIN: (C->shared_classpath_index() >= 0) 93 UNREGISTERED: (C->shared_classpath_index() == UNREGISTERED_INDEX (-9999)) 94 95 [4] Lookup of archived classes at run time: 96 97 (a) BUILTIN loaders: 98 99 search _builtin_dictionary 100 101 (b) UNREGISTERED loaders: 102 103 search _unregistered_dictionary for an entry that matches the 104 (name, clsfile_len, clsfile_crc32). 105 106 ===============================================================================*/ 107 #define UNREGISTERED_INDEX -9999 108 109 class BootstrapInfo; 110 class ClassFileStream; 111 class ConstantPoolCache; 112 class Dictionary; 113 class DumpTimeClassInfo; 114 class DumpTimeSharedClassTable; 115 class RunTimeClassInfo; 116 class RunTimeSharedDictionary; 117 118 class SharedClassLoadingMark { 119 private: 120 Thread* THREAD; 121 InstanceKlass* _klass; 122 public: 123 SharedClassLoadingMark(Thread* current, InstanceKlass* ik) : THREAD(current), _klass(ik) {} 124 ~SharedClassLoadingMark() { 125 assert(THREAD != nullptr, "Current thread is nullptr"); 126 assert(_klass != nullptr, "InstanceKlass is nullptr"); 127 if (HAS_PENDING_EXCEPTION) { 128 if (_klass->is_shared()) { 129 _klass->set_shared_loading_failed(); 130 } 131 } 132 } 133 }; 134 135 class SystemDictionaryShared: public SystemDictionary { 136 friend class LambdaProxyClassDictionary; 137 138 struct ArchiveInfo { 139 RunTimeSharedDictionary _builtin_dictionary; 140 RunTimeSharedDictionary _unregistered_dictionary; 141 142 void print_on(const char* prefix, outputStream* st, bool is_static_archive); 143 void print_table_statistics(const char* prefix, outputStream* st, bool is_static_archive); 144 }; 145 146 private: 147 148 static DumpTimeSharedClassTable* _dumptime_table; 149 150 static ArchiveInfo _static_archive; 151 static ArchiveInfo _dynamic_archive; 152 153 static ArchiveInfo* get_archive(bool is_static_archive) { 154 return is_static_archive ? &_static_archive : &_dynamic_archive; 155 } 156 157 static InstanceKlass* load_shared_class_for_builtin_loader( 158 Symbol* class_name, 159 Handle class_loader, 160 TRAPS); 161 static InstanceKlass* acquire_class_for_current_thread( 162 InstanceKlass *ik, 163 Handle class_loader, 164 Handle protection_domain, 165 const ClassFileStream* cfs, 166 TRAPS); 167 168 // Guaranteed to return non-null value for non-shared classes. 169 // k must not be a shared class. 170 static DumpTimeClassInfo* get_info(InstanceKlass* k); 171 static DumpTimeClassInfo* get_info_locked(InstanceKlass* k); 172 173 static void write_dictionary(RunTimeSharedDictionary* dictionary, 174 bool is_builtin); 175 static bool is_jfr_event_class(InstanceKlass *k); 176 static bool check_for_exclusion_impl(InstanceKlass* k); 177 static void remove_dumptime_info(InstanceKlass* k) NOT_CDS_RETURN; 178 static bool has_been_redefined(InstanceKlass* k); 179 DEBUG_ONLY(static bool _class_loading_may_happen;) 180 181 static void copy_verification_constraints_from_preimage(InstanceKlass* klass); 182 static void copy_linking_constraints_from_preimage(InstanceKlass* klass); 183 184 public: 185 static bool is_early_klass(InstanceKlass* k); // Was k loaded while JvmtiExport::is_early_phase()==true 186 static bool has_archived_enum_objs(InstanceKlass* ik); 187 static void set_has_archived_enum_objs(InstanceKlass* ik); 188 189 static InstanceKlass* find_builtin_class(Symbol* class_name); 190 191 static const RunTimeClassInfo* find_record(RunTimeSharedDictionary* static_dict, 192 RunTimeSharedDictionary* dynamic_dict, 193 Symbol* name); 194 195 static bool has_platform_or_app_classes(); 196 197 // Called by PLATFORM/APP loader only 198 static InstanceKlass* find_or_load_shared_class(Symbol* class_name, 199 Handle class_loader, 200 TRAPS); 201 202 203 static void allocate_shared_data_arrays(int size, TRAPS); 204 205 static bool is_builtin_loader(ClassLoaderData* loader_data); 206 207 static InstanceKlass* lookup_super_for_unregistered_class(Symbol* class_name, 208 Symbol* super_name, bool is_superclass); 209 210 static void initialize() NOT_CDS_RETURN; 211 static void init_dumptime_info(InstanceKlass* k) NOT_CDS_RETURN; 212 static void init_dumptime_info_from_preimage(InstanceKlass* k) NOT_CDS_RETURN; 213 static void handle_class_unloading(InstanceKlass* k) NOT_CDS_RETURN; 214 215 static Dictionary* boot_loader_dictionary() { 216 return ClassLoaderData::the_null_class_loader_data()->dictionary(); 217 } 218 219 static void update_shared_entry(InstanceKlass* klass, int id); 220 static void set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs); 221 222 static InstanceKlass* lookup_from_stream(Symbol* class_name, 223 Handle class_loader, 224 Handle protection_domain, 225 const ClassFileStream* st, 226 TRAPS); 227 // "verification_constraints" are a set of checks performed by 228 // VerificationType::is_reference_assignable_from when verifying a shared class during 229 // dump time. 230 // 231 // With AppCDS, it is possible to override archived classes by calling 232 // ClassLoader.defineClass() directly. SystemDictionary::load_shared_class() already 233 // ensures that you cannot load a shared class if its super type(s) are changed. However, 234 // we need an additional check to ensure that the verification_constraints did not change 235 // between dump time and runtime. 236 static void add_verification_constraint(InstanceKlass* k, Symbol* name, 237 Symbol* from_name, bool from_field_is_protected, 238 bool from_is_array, bool from_is_object, 239 bool* skip_assignability_check); 240 static void check_verification_constraints(InstanceKlass* klass, 241 TRAPS) NOT_CDS_RETURN; 242 static void add_enum_klass_static_field(InstanceKlass* ik, int root_index); 243 static void set_class_has_failed_verification(InstanceKlass* ik) NOT_CDS_RETURN; 244 static bool has_class_failed_verification(InstanceKlass* ik) NOT_CDS_RETURN_(false); 245 static bool check_linking_constraints(Thread* current, InstanceKlass* klass) NOT_CDS_RETURN_(false); 246 static void record_linking_constraint(Symbol* name, InstanceKlass* klass, 247 Handle loader1, Handle loader2) NOT_CDS_RETURN; 248 static bool is_builtin(const InstanceKlass* k) { 249 return (k->shared_classpath_index() != UNREGISTERED_INDEX); 250 } 251 static bool add_unregistered_class(Thread* current, InstanceKlass* k); 252 static InstanceKlass* get_unregistered_class(Symbol* name); 253 static void copy_unregistered_class_size_and_crc32(InstanceKlass* klass); 254 255 static void finish_exclusion_checks(); 256 static DumpTimeSharedClassTable* dumptime_table() { return _dumptime_table; } 257 258 static bool should_be_excluded(Klass* k); 259 static bool check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info); 260 static void validate_before_archiving(InstanceKlass* k); 261 static bool is_excluded_class(InstanceKlass* k); 262 static void set_excluded(InstanceKlass* k); 263 static void set_excluded_locked(InstanceKlass* k); 264 static void set_from_class_file_load_hook(InstanceKlass* k) NOT_CDS_RETURN; 265 static bool warn_excluded(InstanceKlass* k, const char* reason); 266 static void dumptime_classes_do(class MetaspaceClosure* it); 267 static void write_to_archive(bool is_static_archive = true); 268 static void serialize_dictionary_headers(class SerializeClosure* soc, 269 bool is_static_archive = true); 270 static void serialize_vm_classes(class SerializeClosure* soc); 271 static const char* loader_type_for_shared_class(Klass* k); 272 static void print() { return print_on(tty); } 273 static void print_on(outputStream* st) NOT_CDS_RETURN; 274 static void print_shared_archive(outputStream* st, bool is_static = true) NOT_CDS_RETURN; 275 static void print_table_statistics(outputStream* st) NOT_CDS_RETURN; 276 static bool is_dumptime_table_empty() NOT_CDS_RETURN_(true); 277 DEBUG_ONLY(static bool class_loading_may_happen() {return _class_loading_may_happen;}) 278 279 #ifdef ASSERT 280 // This object marks a critical period when writing the CDS archive. During this 281 // period, the JVM must not load any new classes, so as to avoid adding new 282 // items in the SystemDictionaryShared::_dumptime_table. 283 class NoClassLoadingMark: public StackObj { 284 public: 285 NoClassLoadingMark() { 286 assert(_class_loading_may_happen, "must not be nested"); 287 _class_loading_may_happen = false; 288 } 289 ~NoClassLoadingMark() { 290 _class_loading_may_happen = true; 291 } 292 }; 293 #endif 294 295 template <typename T> 296 static unsigned int hash_for_shared_dictionary_quick(T* ptr) { 297 assert(MetaspaceObj::is_shared((const MetaspaceObj*)ptr), "must be"); 298 assert(ptr > (T*)SharedBaseAddress, "must be"); 299 uintx offset = uintx(ptr) - uintx(SharedBaseAddress); 300 return primitive_hash<uintx>(offset); 301 } 302 303 static unsigned int hash_for_shared_dictionary(address ptr); 304 }; 305 306 #endif // SHARE_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP