1 /* 2 * Copyright (c) 2014, 2018, 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_VM_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP 26 #define SHARE_VM_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP 27 28 #include "oops/klass.hpp" 29 #include "classfile/dictionary.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 several types. The following rules describe how they 40 are stored and looked up. 41 42 [1] Category of archived classes 43 44 There are 3 disjoint groups of classes stored in the AppCDS archive. They are 45 categorized as by their SharedDictionaryEntry::loader_type() 46 47 BUILTIN: These classes may be defined ONLY by the BOOT/PLATFORM/APP 48 loaders. 49 50 UNREGISTERED: These classes may be defined ONLY by a ClassLoader 51 instance that's not listed above (using fingerprint matching) 52 53 [2] How classes from different categories are specified in the classlist: 54 55 Starting from JDK9, each class in the classlist may be specified with 56 these keywords: "id", "super", "interfaces", "loader" and "source". 57 58 59 BUILTIN Only the "id" keyword may be (optionally) specified. All other 60 keywords are forbidden. 61 62 The named class is looked up from the jimage and from 63 Xbootclasspath/a and CLASSPATH. 64 65 UNREGISTERED: The "id", "super", and "source" keywords must all be 66 specified. 67 68 The "interfaces" keyword must be specified if the class implements 69 one or more local interfaces. The "interfaces" keyword must not be 70 specified if the class does not implement local interfaces. 71 72 The named class is looked up from the location specified in the 73 "source" keyword. 74 75 Example classlist: 76 77 # BUILTIN 78 java/lang/Object id: 0 79 java/lang/Cloneable id: 1 80 java/lang/String 81 82 # UNREGISTERED 83 Bar id: 3 super: 0 interfaces: 1 source: /foo.jar 84 85 86 [3] Identifying the loader_type of archived classes in the shared dictionary 87 88 Each archived Klass* C is associated with a SharedDictionaryEntry* E 89 90 BUILTIN: (C->shared_classpath_index() >= 0) 91 UNREGISTERED: (C->shared_classpath_index() < 0) 92 93 [4] Lookup of archived classes at run time: 94 95 (a) BUILTIN loaders: 96 97 Search the shared directory for a BUILTIN class with a matching name. 98 99 (b) UNREGISTERED loaders: 100 101 The search originates with SystemDictionaryShared::lookup_from_stream(). 102 103 Search the shared directory for a UNREGISTERED class with a matching 104 (name, clsfile_len, clsfile_crc32) tuple. 105 106 ===============================================================================*/ 107 #define UNREGISTERED_INDEX -9999 108 109 class ClassFileStream; 110 111 // Archived classes need extra information not needed by traditionally loaded classes. 112 // To keep footprint small, we add these in the dictionary entry instead of the InstanceKlass. 113 class SharedDictionaryEntry : public DictionaryEntry { 114 115 public: 116 enum LoaderType { 117 LT_BUILTIN, 118 LT_UNREGISTERED 119 }; 120 121 enum { 122 FROM_FIELD_IS_PROTECTED = 1 << 0, 123 FROM_IS_ARRAY = 1 << 1, 124 FROM_IS_OBJECT = 1 << 2 125 }; 126 127 int _id; 128 int _clsfile_size; 129 int _clsfile_crc32; 130 void* _verifier_constraints; // FIXME - use a union here to avoid type casting?? 131 void* _verifier_constraint_flags; 132 133 // See "Identifying the loader_type of archived classes" comments above. 134 LoaderType loader_type() const { 135 InstanceKlass* k = instance_klass(); 136 137 if ((k->shared_classpath_index() != UNREGISTERED_INDEX)) { 138 return LT_BUILTIN; 139 } else { 140 return LT_UNREGISTERED; 141 } 142 } 143 144 SharedDictionaryEntry* next() { 145 return (SharedDictionaryEntry*)(DictionaryEntry::next()); 146 } 147 148 bool is_builtin() const { 149 return loader_type() == LT_BUILTIN; 150 } 151 bool is_unregistered() const { 152 return loader_type() == LT_UNREGISTERED; 153 } 154 155 void add_verification_constraint(Symbol* name, 156 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object); 157 int finalize_verification_constraints(); 158 void check_verification_constraints(InstanceKlass* klass, TRAPS); 159 void metaspace_pointers_do(MetaspaceClosure* it) NOT_CDS_RETURN; 160 }; 161 162 class SharedDictionary : public Dictionary { 163 SharedDictionaryEntry* get_entry_for_builtin_loader(const Symbol* name) const; 164 SharedDictionaryEntry* get_entry_for_unregistered_loader(const Symbol* name, 165 int clsfile_size, 166 int clsfile_crc32) const; 167 168 // Convenience functions 169 SharedDictionaryEntry* bucket(int index) const { 170 return (SharedDictionaryEntry*)(Dictionary::bucket(index)); 171 } 172 173 public: 174 SharedDictionaryEntry* find_entry_for(InstanceKlass* klass); 175 176 bool add_non_builtin_klass(const Symbol* class_name, 177 ClassLoaderData* loader_data, 178 InstanceKlass* obj); 179 180 void update_entry(InstanceKlass* klass, int id); 181 182 InstanceKlass* find_class_for_builtin_loader(const Symbol* name) const; 183 InstanceKlass* find_class_for_unregistered_loader(const Symbol* name, 184 int clsfile_size, 185 int clsfile_crc32) const; 186 bool class_exists_for_unregistered_loader(const Symbol* name) { 187 return (get_entry_for_unregistered_loader(name, -1, -1) != NULL); 188 } 189 }; 190 191 class SystemDictionaryShared: public SystemDictionary { 192 private: 193 // These _shared_xxxs arrays are used to initialize the java.lang.Package and 194 // java.security.ProtectionDomain objects associated with each shared class. 195 // 196 // See SystemDictionaryShared::init_security_info for more info. 197 static objArrayOop _shared_protection_domains; 198 static objArrayOop _shared_jar_urls; 199 static objArrayOop _shared_jar_manifests; 200 201 static InstanceKlass* load_shared_class_for_builtin_loader( 202 Symbol* class_name, 203 Handle class_loader, 204 TRAPS); 205 static Handle get_package_name(Symbol* class_name, TRAPS); 206 207 208 // Package handling: 209 // 210 // 1. For named modules in the runtime image 211 // BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces 212 // to get packages in named modules for shared classes. 213 // Package for non-shared classes in named module is also 214 // handled using JVM_GetSystemPackage(s). 215 // 216 // APP classes: VM calls ClassLoaders.AppClassLoader::definePackage(String, Module) 217 // to define package for shared app classes from named 218 // modules. 219 // 220 // PLATFORM classes: VM calls ClassLoaders.PlatformClassLoader::definePackage(String, Module) 221 // to define package for shared platform classes from named 222 // modules. 223 // 224 // 2. For unnamed modules 225 // BOOT classes: Reuses the existing JVM_GetSystemPackage(s) interfaces to 226 // get packages for shared boot classes in unnamed modules. 227 // 228 // APP classes: VM calls ClassLoaders.AppClassLoader::defineOrCheckPackage() 229 // with with the manifest and url from archived data. 230 // 231 // PLATFORM classes: No package is defined. 232 // 233 // The following two define_shared_package() functions are used to define 234 // package for shared APP and PLATFORM classes. 235 static void define_shared_package(Symbol* class_name, 236 Handle class_loader, 237 Handle manifest, 238 Handle url, 239 TRAPS); 240 static void define_shared_package(Symbol* class_name, 241 Handle class_loader, 242 ModuleEntry* mod_entry, 243 TRAPS); 244 245 static Handle get_shared_jar_manifest(int shared_path_index, TRAPS); 246 static Handle get_shared_jar_url(int shared_path_index, TRAPS); 247 static Handle get_protection_domain_from_classloader(Handle class_loader, 248 Handle url, TRAPS); 249 static Handle get_shared_protection_domain(Handle class_loader, 250 int shared_path_index, 251 Handle url, 252 TRAPS); 253 static Handle get_shared_protection_domain(Handle class_loader, 254 ModuleEntry* mod, TRAPS); 255 static Handle init_security_info(Handle class_loader, InstanceKlass* ik, TRAPS); 256 257 static void atomic_set_array_index(objArrayOop array, int index, oop o) { 258 // Benign race condition: array.obj_at(index) may already be filled in. 259 // The important thing here is that all threads pick up the same result. 260 // It doesn't matter which racing thread wins, as long as only one 261 // result is used by all threads, and all future queries. 262 array->atomic_compare_exchange_oop(index, o, NULL); 263 } 264 265 static oop shared_protection_domain(int index); 266 static void atomic_set_shared_protection_domain(int index, oop pd) { 267 atomic_set_array_index(_shared_protection_domains, index, pd); 268 } 269 static void allocate_shared_protection_domain_array(int size, TRAPS); 270 static oop shared_jar_url(int index); 271 static void atomic_set_shared_jar_url(int index, oop url) { 272 atomic_set_array_index(_shared_jar_urls, index, url); 273 } 274 static void allocate_shared_jar_url_array(int size, TRAPS); 275 static oop shared_jar_manifest(int index); 276 static void atomic_set_shared_jar_manifest(int index, oop man) { 277 atomic_set_array_index(_shared_jar_manifests, index, man); 278 } 279 static void allocate_shared_jar_manifest_array(int size, TRAPS); 280 static InstanceKlass* acquire_class_for_current_thread( 281 InstanceKlass *ik, 282 Handle class_loader, 283 Handle protection_domain, 284 TRAPS); 285 286 public: 287 // Called by PLATFORM/APP loader only 288 static InstanceKlass* find_or_load_shared_class(Symbol* class_name, 289 Handle class_loader, 290 TRAPS); 291 292 293 static void allocate_shared_data_arrays(int size, TRAPS); 294 static void oops_do(OopClosure* f); 295 296 // Check if sharing is supported for the class loader. 297 static bool is_sharing_possible(ClassLoaderData* loader_data); 298 static bool is_shared_class_visible_for_classloader(InstanceKlass* ik, 299 Handle class_loader, 300 const char* pkg_string, 301 Symbol* pkg_name, 302 PackageEntry* pkg_entry, 303 ModuleEntry* mod_entry, 304 TRAPS); 305 static PackageEntry* get_package_entry(Symbol* pkg, 306 ClassLoaderData *loader_data) { 307 if (loader_data != NULL) { 308 PackageEntryTable* pkgEntryTable = loader_data->packages(); 309 return pkgEntryTable->lookup_only(pkg); 310 } 311 return NULL; 312 } 313 314 static bool add_non_builtin_klass(Symbol* class_name, ClassLoaderData* loader_data, 315 InstanceKlass* k, TRAPS); 316 static InstanceKlass* dump_time_resolve_super_or_fail(Symbol* child_name, 317 Symbol* class_name, 318 Handle class_loader, 319 Handle protection_domain, 320 bool is_superclass, 321 TRAPS); 322 323 static size_t dictionary_entry_size() { 324 return (DumpSharedSpaces) ? sizeof(SharedDictionaryEntry) : sizeof(DictionaryEntry); 325 } 326 static void init_shared_dictionary_entry(InstanceKlass* k, DictionaryEntry* entry) NOT_CDS_RETURN; 327 static bool is_builtin(DictionaryEntry* ent) { 328 // Can't use virtual function is_builtin because DictionaryEntry doesn't initialize 329 // vtable because it's not constructed properly. 330 SharedDictionaryEntry* entry = (SharedDictionaryEntry*)ent; 331 return entry->is_builtin(); 332 } 333 334 // For convenient access to the SharedDictionaryEntry's of the archived classes. 335 static SharedDictionary* shared_dictionary() { 336 assert(!DumpSharedSpaces, "not for dumping"); 337 return (SharedDictionary*)SystemDictionary::shared_dictionary(); 338 } 339 340 static SharedDictionary* boot_loader_dictionary() { 341 return (SharedDictionary*)ClassLoaderData::the_null_class_loader_data()->dictionary(); 342 } 343 344 static void update_shared_entry(InstanceKlass* klass, int id) { 345 assert(DumpSharedSpaces, "sanity"); 346 assert((SharedDictionary*)(klass->class_loader_data()->dictionary()) != NULL, "sanity"); 347 ((SharedDictionary*)(klass->class_loader_data()->dictionary()))->update_entry(klass, id); 348 } 349 350 static void set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs); 351 352 static InstanceKlass* lookup_from_stream(const Symbol* class_name, 353 Handle class_loader, 354 Handle protection_domain, 355 const ClassFileStream* st, 356 TRAPS); 357 // "verification_constraints" are a set of checks performed by 358 // VerificationType::is_reference_assignable_from when verifying a shared class during 359 // dump time. 360 // 361 // With AppCDS, it is possible to override archived classes by calling 362 // ClassLoader.defineClass() directly. SystemDictionary::load_shared_class() already 363 // ensures that you cannot load a shared class if its super type(s) are changed. However, 364 // we need an additional check to ensure that the verification_constraints did not change 365 // between dump time and runtime. 366 static bool add_verification_constraint(InstanceKlass* k, Symbol* name, 367 Symbol* from_name, bool from_field_is_protected, 368 bool from_is_array, bool from_is_object) NOT_CDS_RETURN_(false); 369 static void finalize_verification_constraints(InstanceKlass* k) NOT_CDS_RETURN; 370 static void check_verification_constraints(InstanceKlass* klass, 371 TRAPS) NOT_CDS_RETURN; 372 }; 373 374 #endif // SHARE_VM_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP