1 /* 2 * Copyright (c) 2014, 2020, 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 "oops/klass.hpp" 29 #include "classfile/packageEntry.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "memory/filemap.hpp" 32 33 34 /*=============================================================================== 35 36 Handling of the classes in the AppCDS archive 37 38 To ensure safety and to simplify the implementation, archived classes are 39 "segregated" into 2 types. The following rules describe how they 40 are stored and looked up. 41 42 [1] Category of archived classes 43 44 There are 2 disjoint groups of classes stored in the AppCDS archive: 45 46 BUILTIN: These classes may be defined ONLY by the BOOT/PLATFORM/APP 47 loaders. 48 49 UNREGISTERED: These classes may be defined ONLY by a ClassLoader 50 instance that's not listed above (using fingerprint matching) 51 52 [2] How classes from different categories are specified in the classlist: 53 54 Starting from JDK9, each class in the classlist may be specified with 55 these keywords: "id", "super", "interfaces", "loader" and "source". 56 57 58 BUILTIN Only the "id" keyword may be (optionally) specified. All other 59 keywords are forbidden. 60 61 The named class is looked up from the jimage and from 62 Xbootclasspath/a and CLASSPATH. 63 64 UNREGISTERED: The "id", "super", and "source" keywords must all be 65 specified. 66 67 The "interfaces" keyword must be specified if the class implements 68 one or more local interfaces. The "interfaces" keyword must not be 69 specified if the class does not implement local interfaces. 70 71 The named class is looked up from the location specified in the 72 "source" keyword. 73 74 Example classlist: 75 76 # BUILTIN 77 java/lang/Object id: 0 78 java/lang/Cloneable id: 1 79 java/lang/String 80 81 # UNREGISTERED 82 Bar id: 3 super: 0 interfaces: 1 source: /foo.jar 83 84 85 [3] Identifying the category of archived classes 86 87 BUILTIN: (C->shared_classpath_index() >= 0) 88 UNREGISTERED: (C->shared_classpath_index() == UNREGISTERED_INDEX (-9999)) 89 90 [4] Lookup of archived classes at run time: 91 92 (a) BUILTIN loaders: 93 94 search _builtin_dictionary 95 96 (b) UNREGISTERED loaders: 97 98 search _unregistered_dictionary for an entry that matches the 99 (name, clsfile_len, clsfile_crc32). 100 101 ===============================================================================*/ 102 #define UNREGISTERED_INDEX -9999 103 104 class ClassFileStream; 105 class Dictionary; 106 class DumpTimeSharedClassInfo; 107 class DumpTimeSharedClassTable; 108 class LambdaProxyClassDictionary; 109 class RunTimeSharedClassInfo; 110 class RunTimeSharedDictionary; 111 112 class SystemDictionaryShared: public SystemDictionary { 113 friend class ExcludeDumpTimeSharedClasses; 114 public: 115 enum { 116 FROM_FIELD_IS_PROTECTED = 1 << 0, 117 FROM_IS_ARRAY = 1 << 1, 118 FROM_IS_OBJECT = 1 << 2 119 }; 120 121 private: 122 // These _shared_xxxs arrays are used to initialize the java.lang.Package and 123 // java.security.ProtectionDomain objects associated with each shared class. 124 // 125 // See SystemDictionaryShared::init_security_info for more info. 126 static OopHandle _shared_protection_domains; 127 static OopHandle _shared_jar_urls; 128 static OopHandle _shared_jar_manifests; 129 130 static InstanceKlass* load_shared_class_for_builtin_loader( 131 Symbol* class_name, 132 Handle class_loader, 133 TRAPS); 134 static Handle get_package_name(Symbol* class_name, TRAPS); 135 136 static PackageEntry* get_package_entry_from_class_name(Handle class_loader, Symbol* class_name); 137 138 139 // Package handling: 140 // 141 // 1. For named modules in the runtime image 142 // BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces 143 // to get packages in named modules for shared classes. 144 // Package for non-shared classes in named module is also 145 // handled using JVM_GetSystemPackage(s). 146 // 147 // APP classes: VM calls ClassLoaders.AppClassLoader::definePackage(String, Module) 148 // to define package for shared app classes from named 149 // modules. 150 // 151 // PLATFORM classes: VM calls ClassLoaders.PlatformClassLoader::definePackage(String, Module) 152 // to define package for shared platform classes from named 153 // modules. 154 // 155 // 2. For unnamed modules 156 // BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces to 157 // get packages for shared boot classes in unnamed modules. 158 // 159 // APP classes: VM calls ClassLoaders.AppClassLoader::defineOrCheckPackage() 160 // with with the manifest and url from archived data. 161 // 162 // PLATFORM classes: No package is defined. 163 // 164 // The following two define_shared_package() functions are used to define 165 // package for shared APP and PLATFORM classes. 166 static void define_shared_package(Symbol* class_name, 167 Handle class_loader, 168 Handle manifest, 169 Handle url, 170 TRAPS); 171 172 static Handle get_shared_jar_manifest(int shared_path_index, TRAPS); 173 static Handle get_shared_jar_url(int shared_path_index, TRAPS); 174 static Handle get_protection_domain_from_classloader(Handle class_loader, 175 Handle url, TRAPS); 176 static Handle get_shared_protection_domain(Handle class_loader, 177 int shared_path_index, 178 Handle url, 179 TRAPS); 180 static Handle get_shared_protection_domain(Handle class_loader, 181 ModuleEntry* mod, TRAPS); 182 183 static void atomic_set_array_index(OopHandle array, int index, oop o) { 184 // Benign race condition: array.obj_at(index) may already be filled in. 185 // The important thing here is that all threads pick up the same result. 186 // It doesn't matter which racing thread wins, as long as only one 187 // result is used by all threads, and all future queries. 188 ((objArrayOop)array.resolve())->atomic_compare_exchange_oop(index, o, NULL); 189 } 190 191 static oop shared_protection_domain(int index); 192 static void atomic_set_shared_protection_domain(int index, oop pd) { 193 atomic_set_array_index(_shared_protection_domains, index, pd); 194 } 195 static void allocate_shared_protection_domain_array(int size, TRAPS); 196 static oop shared_jar_url(int index); 197 static void atomic_set_shared_jar_url(int index, oop url) { 198 atomic_set_array_index(_shared_jar_urls, index, url); 199 } 200 static void allocate_shared_jar_url_array(int size, TRAPS); 201 static oop shared_jar_manifest(int index); 202 static void atomic_set_shared_jar_manifest(int index, oop man) { 203 atomic_set_array_index(_shared_jar_manifests, index, man); 204 } 205 static void allocate_shared_jar_manifest_array(int size, TRAPS); 206 static InstanceKlass* acquire_class_for_current_thread( 207 InstanceKlass *ik, 208 Handle class_loader, 209 Handle protection_domain, 210 const ClassFileStream* cfs, 211 TRAPS); 212 static DumpTimeSharedClassInfo* find_or_allocate_info_for(InstanceKlass* k); 213 static void write_dictionary(RunTimeSharedDictionary* dictionary, 214 bool is_builtin); 215 static void write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary* dictionary); 216 static bool is_jfr_event_class(InstanceKlass *k); 217 static bool is_registered_lambda_proxy_class(InstanceKlass* ik); 218 static void warn_excluded(InstanceKlass* k, const char* reason); 219 static bool should_be_excluded(InstanceKlass* k); 220 221 static bool _dump_in_progress; 222 DEBUG_ONLY(static bool _no_class_loading_should_happen;) 223 224 public: 225 static bool is_hidden_lambda_proxy(InstanceKlass* ik); 226 static bool is_early_klass(InstanceKlass* k); // Was k loaded while JvmtiExport::is_early_phase()==true 227 static Handle init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS); 228 static InstanceKlass* find_builtin_class(Symbol* class_name); 229 230 static const RunTimeSharedClassInfo* find_record(RunTimeSharedDictionary* static_dict, 231 RunTimeSharedDictionary* dynamic_dict, 232 Symbol* name); 233 234 static bool has_platform_or_app_classes(); 235 236 // Called by PLATFORM/APP loader only 237 static InstanceKlass* find_or_load_shared_class(Symbol* class_name, 238 Handle class_loader, 239 TRAPS); 240 241 242 static void allocate_shared_data_arrays(int size, TRAPS); 243 244 // Check if sharing is supported for the class loader. 245 static bool is_sharing_possible(ClassLoaderData* loader_data); 246 247 static bool add_unregistered_class(InstanceKlass* k, TRAPS); 248 static InstanceKlass* dump_time_resolve_super_or_fail(Symbol* child_name, 249 Symbol* class_name, 250 Handle class_loader, 251 Handle protection_domain, 252 bool is_superclass, 253 TRAPS); 254 255 static void init_dumptime_info(InstanceKlass* k) NOT_CDS_RETURN; 256 static void remove_dumptime_info(InstanceKlass* k) NOT_CDS_RETURN; 257 258 static Dictionary* boot_loader_dictionary() { 259 return ClassLoaderData::the_null_class_loader_data()->dictionary(); 260 } 261 262 static void update_shared_entry(InstanceKlass* klass, int id); 263 static void set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs); 264 265 static InstanceKlass* lookup_from_stream(Symbol* class_name, 266 Handle class_loader, 267 Handle protection_domain, 268 const ClassFileStream* st, 269 TRAPS); 270 // "verification_constraints" are a set of checks performed by 271 // VerificationType::is_reference_assignable_from when verifying a shared class during 272 // dump time. 273 // 274 // With AppCDS, it is possible to override archived classes by calling 275 // ClassLoader.defineClass() directly. SystemDictionary::load_shared_class() already 276 // ensures that you cannot load a shared class if its super type(s) are changed. However, 277 // we need an additional check to ensure that the verification_constraints did not change 278 // between dump time and runtime. 279 static bool add_verification_constraint(InstanceKlass* k, Symbol* name, 280 Symbol* from_name, bool from_field_is_protected, 281 bool from_is_array, bool from_is_object) NOT_CDS_RETURN_(false); 282 static void check_verification_constraints(InstanceKlass* klass, 283 TRAPS) NOT_CDS_RETURN; 284 static void set_class_has_failed_verification(InstanceKlass* ik) NOT_CDS_RETURN; 285 static bool has_class_failed_verification(InstanceKlass* ik) NOT_CDS_RETURN_(false); 286 static void add_lambda_proxy_class(InstanceKlass* caller_ik, 287 InstanceKlass* lambda_ik, 288 Symbol* invoked_name, 289 Symbol* invoked_type, 290 Symbol* method_type, 291 Method* member_method, 292 Symbol* instantiated_method_type) NOT_CDS_RETURN; 293 static InstanceKlass* get_shared_lambda_proxy_class(InstanceKlass* caller_ik, 294 Symbol* invoked_name, 295 Symbol* invoked_type, 296 Symbol* method_type, 297 Method* member_method, 298 Symbol* instantiated_method_type) NOT_CDS_RETURN_(NULL); 299 static InstanceKlass* get_shared_nest_host(InstanceKlass* lambda_ik) NOT_CDS_RETURN_(NULL); 300 static InstanceKlass* prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik, 301 InstanceKlass* caller_ik, 302 bool initialize, TRAPS) NOT_CDS_RETURN_(NULL); 303 static bool check_linking_constraints(InstanceKlass* klass, TRAPS) NOT_CDS_RETURN_(false); 304 static void record_linking_constraint(Symbol* name, InstanceKlass* klass, 305 Handle loader1, Handle loader2, TRAPS) NOT_CDS_RETURN; 306 static bool is_builtin(InstanceKlass* k) { 307 return (k->shared_classpath_index() != UNREGISTERED_INDEX); 308 } 309 static void check_excluded_classes(); 310 static void validate_before_archiving(InstanceKlass* k); 311 static bool is_excluded_class(InstanceKlass* k); 312 static void dumptime_classes_do(class MetaspaceClosure* it); 313 static size_t estimate_size_for_archive(); 314 static void write_to_archive(bool is_static_archive = true); 315 static void adjust_lambda_proxy_class_dictionary(); 316 static void serialize_dictionary_headers(class SerializeClosure* soc, 317 bool is_static_archive = true); 318 static void serialize_well_known_klasses(class SerializeClosure* soc); 319 static void print() { return print_on(tty); } 320 static void print_on(outputStream* st) NOT_CDS_RETURN; 321 static void print_table_statistics(outputStream* st) NOT_CDS_RETURN; 322 static bool empty_dumptime_table() NOT_CDS_RETURN_(true); 323 static void start_dumping() NOT_CDS_RETURN; 324 static Handle create_jar_manifest(const char* man, size_t size, TRAPS) NOT_CDS_RETURN_(Handle()); 325 326 DEBUG_ONLY(static bool no_class_loading_should_happen() {return _no_class_loading_should_happen;}) 327 328 #ifdef ASSERT 329 class NoClassLoadingMark: public StackObj { 330 public: 331 NoClassLoadingMark() { 332 assert(!_no_class_loading_should_happen, "must not be nested"); 333 _no_class_loading_should_happen = true; 334 } 335 ~NoClassLoadingMark() { 336 _no_class_loading_should_happen = false; 337 } 338 }; 339 #endif 340 341 template <typename T> 342 static unsigned int hash_for_shared_dictionary(T* ptr) { 343 assert(ptr > (T*)SharedBaseAddress, "must be"); 344 address p = address(ptr) - SharedBaseAddress; 345 return primitive_hash<address>(p); 346 } 347 348 #if INCLUDE_CDS_JAVA_HEAP 349 private: 350 static void update_archived_mirror_native_pointers_for(RunTimeSharedDictionary* dict); 351 public: 352 static void update_archived_mirror_native_pointers() NOT_CDS_RETURN; 353 #endif 354 }; 355 356 #endif // SHARE_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP