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 #include "precompiled.hpp" 26 #include "classfile/classFileStream.hpp" 27 #include "classfile/classListParser.hpp" 28 #include "classfile/classLoader.hpp" 29 #include "classfile/classLoaderData.inline.hpp" 30 #include "classfile/classLoaderDataGraph.hpp" 31 #include "classfile/classLoaderExt.hpp" 32 #include "classfile/dictionary.hpp" 33 #include "classfile/javaClasses.hpp" 34 #include "classfile/symbolTable.hpp" 35 #include "classfile/systemDictionary.hpp" 36 #include "classfile/systemDictionaryShared.hpp" 37 #include "classfile/verificationType.hpp" 38 #include "classfile/vmSymbols.hpp" 39 #include "logging/log.hpp" 40 #include "memory/allocation.hpp" 41 #include "memory/archiveUtils.hpp" 42 #include "memory/filemap.hpp" 43 #include "memory/metadataFactory.hpp" 44 #include "memory/metaspaceClosure.hpp" 45 #include "memory/oopFactory.hpp" 46 #include "memory/resourceArea.hpp" 47 #include "memory/universe.hpp" 48 #include "memory/dynamicArchive.hpp" 49 #include "oops/instanceKlass.hpp" 50 #include "oops/klass.inline.hpp" 51 #include "oops/objArrayOop.inline.hpp" 52 #include "oops/oop.inline.hpp" 53 #include "oops/typeArrayOop.inline.hpp" 54 #include "runtime/handles.inline.hpp" 55 #include "runtime/java.hpp" 56 #include "runtime/javaCalls.hpp" 57 #include "runtime/mutexLocker.hpp" 58 #include "utilities/hashtable.inline.hpp" 59 #include "utilities/resourceHash.hpp" 60 #include "utilities/stringUtils.hpp" 61 62 63 objArrayOop SystemDictionaryShared::_shared_protection_domains = NULL; 64 objArrayOop SystemDictionaryShared::_shared_jar_urls = NULL; 65 objArrayOop SystemDictionaryShared::_shared_jar_manifests = NULL; 66 DEBUG_ONLY(bool SystemDictionaryShared::_no_class_loading_should_happen = false;) 67 68 class DumpTimeSharedClassInfo: public CHeapObj<mtClass> { 69 bool _excluded; 70 public: 71 struct DTConstraint { 72 Symbol* _name; 73 Symbol* _from_name; 74 DTConstraint() : _name(NULL), _from_name(NULL) {} 75 DTConstraint(Symbol* n, Symbol* fn) : _name(n), _from_name(fn) {} 76 }; 77 78 InstanceKlass* _klass; 79 bool _failed_verification; 80 int _id; 81 int _clsfile_size; 82 int _clsfile_crc32; 83 GrowableArray<DTConstraint>* _verifier_constraints; 84 GrowableArray<char>* _verifier_constraint_flags; 85 86 DumpTimeSharedClassInfo() { 87 _klass = NULL; 88 _failed_verification = false; 89 _id = -1; 90 _clsfile_size = -1; 91 _clsfile_crc32 = -1; 92 _excluded = false; 93 _verifier_constraints = NULL; 94 _verifier_constraint_flags = NULL; 95 } 96 97 void add_verification_constraint(InstanceKlass* k, Symbol* name, 98 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object); 99 100 bool is_builtin() { 101 return SystemDictionaryShared::is_builtin(_klass); 102 } 103 104 int num_constraints() { 105 if (_verifier_constraint_flags != NULL) { 106 return _verifier_constraint_flags->length(); 107 } else { 108 return 0; 109 } 110 } 111 112 void metaspace_pointers_do(MetaspaceClosure* it) { 113 it->push(&_klass); 114 if (_verifier_constraints != NULL) { 115 for (int i = 0; i < _verifier_constraints->length(); i++) { 116 DTConstraint* cons = _verifier_constraints->adr_at(i); 117 it->push(&cons->_name); 118 it->push(&cons->_from_name); 119 } 120 } 121 } 122 123 void set_excluded() { 124 _excluded = true; 125 } 126 127 bool is_excluded() { 128 // _klass may become NULL due to DynamicArchiveBuilder::set_to_null 129 return _excluded || _failed_verification || _klass == NULL; 130 } 131 132 void set_failed_verification() { 133 _failed_verification = true; 134 } 135 136 bool failed_verification() { 137 return _failed_verification; 138 } 139 }; 140 141 class DumpTimeSharedClassTable: public ResourceHashtable< 142 InstanceKlass*, 143 DumpTimeSharedClassInfo, 144 primitive_hash<InstanceKlass*>, 145 primitive_equals<InstanceKlass*>, 146 15889, // prime number 147 ResourceObj::C_HEAP> 148 { 149 int _builtin_count; 150 int _unregistered_count; 151 public: 152 DumpTimeSharedClassInfo* find_or_allocate_info_for(InstanceKlass* k) { 153 DumpTimeSharedClassInfo* p = get(k); 154 if (p == NULL) { 155 assert(!SystemDictionaryShared::no_class_loading_should_happen(), 156 "no new classes can be loaded while dumping archive"); 157 put(k, DumpTimeSharedClassInfo()); 158 p = get(k); 159 assert(p != NULL, "sanity"); 160 p->_klass = k; 161 } 162 return p; 163 } 164 165 class CountClassByCategory : StackObj { 166 DumpTimeSharedClassTable* _table; 167 public: 168 CountClassByCategory(DumpTimeSharedClassTable* table) : _table(table) {} 169 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 170 if (!info.is_excluded()) { 171 if (info.is_builtin()) { 172 ++ _table->_builtin_count; 173 } else { 174 ++ _table->_unregistered_count; 175 } 176 } 177 return true; // keep on iterating 178 } 179 }; 180 181 void update_counts() { 182 _builtin_count = 0; 183 _unregistered_count = 0; 184 CountClassByCategory counter(this); 185 iterate(&counter); 186 } 187 188 int count_of(bool is_builtin) const { 189 if (is_builtin) { 190 return _builtin_count; 191 } else { 192 return _unregistered_count; 193 } 194 } 195 }; 196 197 class RunTimeSharedClassInfo { 198 public: 199 struct CrcInfo { 200 int _clsfile_size; 201 int _clsfile_crc32; 202 }; 203 204 // This is different than DumpTimeSharedClassInfo::DTConstraint. We use 205 // u4 instead of Symbol* to save space on 64-bit CPU. 206 struct RTConstraint { 207 u4 _name; 208 u4 _from_name; 209 }; 210 211 InstanceKlass* _klass; 212 int _num_constraints; 213 214 // optional CrcInfo _crc; (only for UNREGISTERED classes) 215 // optional RTConstraint _verifier_constraints[_num_constraints] 216 // optional char _verifier_constraint_flags[_num_constraints] 217 218 private: 219 static size_t header_size_size() { 220 return sizeof(RunTimeSharedClassInfo); 221 } 222 static size_t crc_size(InstanceKlass* klass) { 223 if (!SystemDictionaryShared::is_builtin(klass)) { 224 return sizeof(CrcInfo); 225 } else { 226 return 0; 227 } 228 } 229 static size_t verifier_constraints_size(int num_constraints) { 230 return sizeof(RTConstraint) * num_constraints; 231 } 232 static size_t verifier_constraint_flags_size(int num_constraints) { 233 return sizeof(char) * num_constraints; 234 } 235 236 public: 237 static size_t byte_size(InstanceKlass* klass, int num_constraints) { 238 return header_size_size() + 239 crc_size(klass) + 240 verifier_constraints_size(num_constraints) + 241 verifier_constraint_flags_size(num_constraints); 242 } 243 244 private: 245 size_t crc_offset() const { 246 return header_size_size(); 247 } 248 size_t verifier_constraints_offset() const { 249 return crc_offset() + crc_size(_klass); 250 } 251 size_t verifier_constraint_flags_offset() const { 252 return verifier_constraints_offset() + verifier_constraints_size(_num_constraints); 253 } 254 255 void check_constraint_offset(int i) const { 256 assert(0 <= i && i < _num_constraints, "sanity"); 257 } 258 259 public: 260 CrcInfo* crc() const { 261 assert(crc_size(_klass) > 0, "must be"); 262 return (CrcInfo*)(address(this) + crc_offset()); 263 } 264 RTConstraint* verifier_constraints() { 265 assert(_num_constraints > 0, "sanity"); 266 return (RTConstraint*)(address(this) + verifier_constraints_offset()); 267 } 268 RTConstraint* verifier_constraint_at(int i) { 269 check_constraint_offset(i); 270 return verifier_constraints() + i; 271 } 272 273 char* verifier_constraint_flags() { 274 assert(_num_constraints > 0, "sanity"); 275 return (char*)(address(this) + verifier_constraint_flags_offset()); 276 } 277 278 static u4 object_delta_u4(Symbol* sym) { 279 if (DynamicDumpSharedSpaces) { 280 sym = DynamicArchive::original_to_target(sym); 281 } 282 return MetaspaceShared::object_delta_u4(sym); 283 } 284 285 void init(DumpTimeSharedClassInfo& info) { 286 _klass = info._klass; 287 if (!SystemDictionaryShared::is_builtin(_klass)) { 288 CrcInfo* c = crc(); 289 c->_clsfile_size = info._clsfile_size; 290 c->_clsfile_crc32 = info._clsfile_crc32; 291 } 292 _num_constraints = info.num_constraints(); 293 if (_num_constraints > 0) { 294 RTConstraint* constraints = verifier_constraints(); 295 char* flags = verifier_constraint_flags(); 296 int i; 297 for (i = 0; i < _num_constraints; i++) { 298 constraints[i]._name = object_delta_u4(info._verifier_constraints->at(i)._name); 299 constraints[i]._from_name = object_delta_u4(info._verifier_constraints->at(i)._from_name); 300 } 301 for (i = 0; i < _num_constraints; i++) { 302 flags[i] = info._verifier_constraint_flags->at(i); 303 } 304 } 305 if (DynamicDumpSharedSpaces) { 306 _klass = DynamicArchive::original_to_target(info._klass); 307 } 308 ArchivePtrMarker::mark_pointer(&_klass); 309 } 310 311 bool matches(int clsfile_size, int clsfile_crc32) const { 312 return crc()->_clsfile_size == clsfile_size && 313 crc()->_clsfile_crc32 == clsfile_crc32; 314 } 315 316 Symbol* get_constraint_name(int i) { 317 return (Symbol*)(SharedBaseAddress + verifier_constraint_at(i)->_name); 318 } 319 Symbol* get_constraint_from_name(int i) { 320 return (Symbol*)(SharedBaseAddress + verifier_constraint_at(i)->_from_name); 321 } 322 323 char get_constraint_flag(int i) { 324 check_constraint_offset(i); 325 return verifier_constraint_flags()[i]; 326 } 327 328 private: 329 // ArchiveCompactor::allocate() has reserved a pointer immediately before 330 // archived InstanceKlasses. We can use this slot to do a quick 331 // lookup of InstanceKlass* -> RunTimeSharedClassInfo* without 332 // building a new hashtable. 333 // 334 // info_pointer_addr(klass) --> 0x0100 RunTimeSharedClassInfo* 335 // InstanceKlass* klass --> 0x0108 <C++ vtbl> 336 // 0x0110 fields from Klass ... 337 static RunTimeSharedClassInfo** info_pointer_addr(InstanceKlass* klass) { 338 return &((RunTimeSharedClassInfo**)klass)[-1]; 339 } 340 341 public: 342 static RunTimeSharedClassInfo* get_for(InstanceKlass* klass) { 343 return *info_pointer_addr(klass); 344 } 345 static void set_for(InstanceKlass* klass, RunTimeSharedClassInfo* record) { 346 if (DynamicDumpSharedSpaces) { 347 klass = DynamicArchive::original_to_buffer(klass); 348 *info_pointer_addr(klass) = DynamicArchive::buffer_to_target(record); 349 } else { 350 *info_pointer_addr(klass) = record; 351 } 352 353 ArchivePtrMarker::mark_pointer(info_pointer_addr(klass)); 354 } 355 356 // Used by RunTimeSharedDictionary to implement OffsetCompactHashtable::EQUALS 357 static inline bool EQUALS( 358 const RunTimeSharedClassInfo* value, Symbol* key, int len_unused) { 359 return (value->_klass->name() == key); 360 } 361 }; 362 363 class RunTimeSharedDictionary : public OffsetCompactHashtable< 364 Symbol*, 365 const RunTimeSharedClassInfo*, 366 RunTimeSharedClassInfo::EQUALS> {}; 367 368 static DumpTimeSharedClassTable* _dumptime_table = NULL; 369 // SystemDictionaries in the base layer static archive 370 static RunTimeSharedDictionary _builtin_dictionary; 371 static RunTimeSharedDictionary _unregistered_dictionary; 372 // SystemDictionaries in the top layer dynamic archive 373 static RunTimeSharedDictionary _dynamic_builtin_dictionary; 374 static RunTimeSharedDictionary _dynamic_unregistered_dictionary; 375 376 oop SystemDictionaryShared::shared_protection_domain(int index) { 377 return _shared_protection_domains->obj_at(index); 378 } 379 380 oop SystemDictionaryShared::shared_jar_url(int index) { 381 return _shared_jar_urls->obj_at(index); 382 } 383 384 oop SystemDictionaryShared::shared_jar_manifest(int index) { 385 return _shared_jar_manifests->obj_at(index); 386 } 387 388 389 Handle SystemDictionaryShared::get_shared_jar_manifest(int shared_path_index, TRAPS) { 390 Handle manifest ; 391 if (shared_jar_manifest(shared_path_index) == NULL) { 392 SharedClassPathEntry* ent = FileMapInfo::shared_path(shared_path_index); 393 long size = ent->manifest_size(); 394 if (size <= 0) { 395 return Handle(); 396 } 397 398 // ByteArrayInputStream bais = new ByteArrayInputStream(buf); 399 const char* src = ent->manifest(); 400 assert(src != NULL, "No Manifest data"); 401 typeArrayOop buf = oopFactory::new_byteArray(size, CHECK_NH); 402 typeArrayHandle bufhandle(THREAD, buf); 403 ArrayAccess<>::arraycopy_from_native(reinterpret_cast<const jbyte*>(src), 404 buf, typeArrayOopDesc::element_offset<jbyte>(0), size); 405 406 Handle bais = JavaCalls::construct_new_instance(SystemDictionary::ByteArrayInputStream_klass(), 407 vmSymbols::byte_array_void_signature(), 408 bufhandle, CHECK_NH); 409 410 // manifest = new Manifest(bais) 411 manifest = JavaCalls::construct_new_instance(SystemDictionary::Jar_Manifest_klass(), 412 vmSymbols::input_stream_void_signature(), 413 bais, CHECK_NH); 414 atomic_set_shared_jar_manifest(shared_path_index, manifest()); 415 } 416 417 manifest = Handle(THREAD, shared_jar_manifest(shared_path_index)); 418 assert(manifest.not_null(), "sanity"); 419 return manifest; 420 } 421 422 Handle SystemDictionaryShared::get_shared_jar_url(int shared_path_index, TRAPS) { 423 Handle url_h; 424 if (shared_jar_url(shared_path_index) == NULL) { 425 JavaValue result(T_OBJECT); 426 const char* path = FileMapInfo::shared_path_name(shared_path_index); 427 Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h)); 428 Klass* classLoaders_klass = 429 SystemDictionary::jdk_internal_loader_ClassLoaders_klass(); 430 JavaCalls::call_static(&result, classLoaders_klass, 431 vmSymbols::toFileURL_name(), 432 vmSymbols::toFileURL_signature(), 433 path_string, CHECK_(url_h)); 434 435 atomic_set_shared_jar_url(shared_path_index, (oop)result.get_jobject()); 436 } 437 438 url_h = Handle(THREAD, shared_jar_url(shared_path_index)); 439 assert(url_h.not_null(), "sanity"); 440 return url_h; 441 } 442 443 Handle SystemDictionaryShared::get_package_name(Symbol* class_name, TRAPS) { 444 ResourceMark rm(THREAD); 445 Handle pkgname_string; 446 Symbol* pkg = ClassLoader::package_from_class_name(class_name); 447 if (pkg != NULL) { // Package prefix found 448 const char* pkgname = pkg->as_klass_external_name(); 449 pkgname_string = java_lang_String::create_from_str(pkgname, 450 CHECK_(pkgname_string)); 451 } 452 return pkgname_string; 453 } 454 455 // Define Package for shared app classes from JAR file and also checks for 456 // package sealing (all done in Java code) 457 // See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html 458 void SystemDictionaryShared::define_shared_package(Symbol* class_name, 459 Handle class_loader, 460 Handle manifest, 461 Handle url, 462 TRAPS) { 463 assert(SystemDictionary::is_system_class_loader(class_loader()), "unexpected class loader"); 464 // get_package_name() returns a NULL handle if the class is in unnamed package 465 Handle pkgname_string = get_package_name(class_name, CHECK); 466 if (pkgname_string.not_null()) { 467 Klass* app_classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(); 468 JavaValue result(T_OBJECT); 469 JavaCallArguments args(3); 470 args.set_receiver(class_loader); 471 args.push_oop(pkgname_string); 472 args.push_oop(manifest); 473 args.push_oop(url); 474 JavaCalls::call_virtual(&result, app_classLoader_klass, 475 vmSymbols::defineOrCheckPackage_name(), 476 vmSymbols::defineOrCheckPackage_signature(), 477 &args, 478 CHECK); 479 } 480 } 481 482 // Get the ProtectionDomain associated with the CodeSource from the classloader. 483 Handle SystemDictionaryShared::get_protection_domain_from_classloader(Handle class_loader, 484 Handle url, TRAPS) { 485 // CodeSource cs = new CodeSource(url, null); 486 Handle cs = JavaCalls::construct_new_instance(SystemDictionary::CodeSource_klass(), 487 vmSymbols::url_code_signer_array_void_signature(), 488 url, Handle(), CHECK_NH); 489 490 // protection_domain = SecureClassLoader.getProtectionDomain(cs); 491 Klass* secureClassLoader_klass = SystemDictionary::SecureClassLoader_klass(); 492 JavaValue obj_result(T_OBJECT); 493 JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass, 494 vmSymbols::getProtectionDomain_name(), 495 vmSymbols::getProtectionDomain_signature(), 496 cs, CHECK_NH); 497 return Handle(THREAD, (oop)obj_result.get_jobject()); 498 } 499 500 // Returns the ProtectionDomain associated with the JAR file identified by the url. 501 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader, 502 int shared_path_index, 503 Handle url, 504 TRAPS) { 505 Handle protection_domain; 506 if (shared_protection_domain(shared_path_index) == NULL) { 507 Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD); 508 atomic_set_shared_protection_domain(shared_path_index, pd()); 509 } 510 511 // Acquire from the cache because if another thread beats the current one to 512 // set the shared protection_domain and the atomic_set fails, the current thread 513 // needs to get the updated protection_domain from the cache. 514 protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index)); 515 assert(protection_domain.not_null(), "sanity"); 516 return protection_domain; 517 } 518 519 // Returns the ProtectionDomain associated with the moduleEntry. 520 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader, 521 ModuleEntry* mod, TRAPS) { 522 ClassLoaderData *loader_data = mod->loader_data(); 523 if (mod->shared_protection_domain() == NULL) { 524 Symbol* location = mod->location(); 525 if (location != NULL) { 526 Handle location_string = java_lang_String::create_from_symbol( 527 location, CHECK_NH); 528 Handle url; 529 JavaValue result(T_OBJECT); 530 if (location->starts_with("jrt:/")) { 531 url = JavaCalls::construct_new_instance(SystemDictionary::URL_klass(), 532 vmSymbols::string_void_signature(), 533 location_string, CHECK_NH); 534 } else { 535 Klass* classLoaders_klass = 536 SystemDictionary::jdk_internal_loader_ClassLoaders_klass(); 537 JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(), 538 vmSymbols::toFileURL_signature(), 539 location_string, CHECK_NH); 540 url = Handle(THREAD, (oop)result.get_jobject()); 541 } 542 543 Handle pd = get_protection_domain_from_classloader(class_loader, url, 544 CHECK_NH); 545 mod->set_shared_protection_domain(loader_data, pd); 546 } 547 } 548 549 Handle protection_domain(THREAD, mod->shared_protection_domain()); 550 assert(protection_domain.not_null(), "sanity"); 551 return protection_domain; 552 } 553 554 // Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with 555 // the given InstanceKlass. 556 // Returns the ProtectionDomain for the InstanceKlass. 557 Handle SystemDictionaryShared::init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS) { 558 Handle pd; 559 560 if (ik != NULL) { 561 int index = ik->shared_classpath_index(); 562 assert(index >= 0, "Sanity"); 563 SharedClassPathEntry* ent = FileMapInfo::shared_path(index); 564 Symbol* class_name = ik->name(); 565 566 if (ent->is_modules_image()) { 567 // For shared app/platform classes originated from the run-time image: 568 // The ProtectionDomains are cached in the corresponding ModuleEntries 569 // for fast access by the VM. 570 // all packages from module image are already created during VM bootstrap in 571 // Modules::define_module(). 572 assert(pkg_entry != NULL, "archived class in module image cannot be from unnamed package"); 573 ModuleEntry* mod_entry = pkg_entry->module(); 574 pd = get_shared_protection_domain(class_loader, mod_entry, THREAD); 575 } else { 576 // For shared app/platform classes originated from JAR files on the class path: 577 // Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length 578 // as the shared classpath table in the shared archive (see 579 // FileMap::_shared_path_table in filemap.hpp for details). 580 // 581 // If a shared InstanceKlass k is loaded from the class path, let 582 // 583 // index = k->shared_classpath_index(): 584 // 585 // FileMap::_shared_path_table[index] identifies the JAR file that contains k. 586 // 587 // k's protection domain is: 588 // 589 // ProtectionDomain pd = _shared_protection_domains[index]; 590 // 591 // and k's Package is initialized using 592 // 593 // manifest = _shared_jar_manifests[index]; 594 // url = _shared_jar_urls[index]; 595 // define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 596 // 597 // Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by 598 // the corresponding SystemDictionaryShared::get_shared_xxx() function. 599 Handle manifest = get_shared_jar_manifest(index, CHECK_(pd)); 600 Handle url = get_shared_jar_url(index, CHECK_(pd)); 601 define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd)); 602 pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd)); 603 } 604 } 605 return pd; 606 } 607 608 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) { 609 oop class_loader = loader_data->class_loader(); 610 return (class_loader == NULL || 611 SystemDictionary::is_system_class_loader(class_loader) || 612 SystemDictionary::is_platform_class_loader(class_loader)); 613 } 614 615 // Currently AppCDS only archives classes from the run-time image, the 616 // -Xbootclasspath/a path, the class path, and the module path. 617 // 618 // Check if a shared class can be loaded by the specific classloader. Following 619 // are the "visible" archived classes for different classloaders. 620 // 621 // NULL classloader: 622 // - see SystemDictionary::is_shared_class_visible() 623 // Platform classloader: 624 // - Module class from runtime image. ModuleEntry must be defined in the 625 // classloader. 626 // App classloader: 627 // - Module Class from runtime image and module path. ModuleEntry must be defined in the 628 // classloader. 629 // - Class from -cp. The class must have no PackageEntry defined in any of the 630 // boot/platform/app classloader, or must be in the unnamed module defined in the 631 // AppClassLoader. 632 bool SystemDictionaryShared::is_shared_class_visible_for_classloader( 633 InstanceKlass* ik, 634 Handle class_loader, 635 Symbol* pkg_name, 636 PackageEntry* pkg_entry, 637 ModuleEntry* mod_entry, 638 TRAPS) { 639 assert(class_loader.not_null(), "Class loader should not be NULL"); 640 assert(Universe::is_module_initialized(), "Module system is not initialized"); 641 ResourceMark rm(THREAD); 642 643 int path_index = ik->shared_classpath_index(); 644 SharedClassPathEntry* ent = 645 (SharedClassPathEntry*)FileMapInfo::shared_path(path_index); 646 647 if (SystemDictionary::is_platform_class_loader(class_loader())) { 648 assert(ent != NULL, "shared class for PlatformClassLoader should have valid SharedClassPathEntry"); 649 // The PlatformClassLoader can only load archived class originated from the 650 // run-time image. The class' PackageEntry/ModuleEntry must be 651 // defined by the PlatformClassLoader. 652 if (mod_entry != NULL) { 653 // PackageEntry/ModuleEntry is found in the classloader. Check if the 654 // ModuleEntry's location agrees with the archived class' origination. 655 if (ent->is_modules_image() && mod_entry->location()->starts_with("jrt:")) { 656 return true; // Module class from the runtime image 657 } 658 } 659 } else if (SystemDictionary::is_system_class_loader(class_loader())) { 660 assert(ent != NULL, "shared class for system loader should have valid SharedClassPathEntry"); 661 if (pkg_name == NULL) { 662 // The archived class is in the unnamed package. Currently, the boot image 663 // does not contain any class in the unnamed package. 664 assert(!ent->is_modules_image(), "Class in the unnamed package must be from the classpath"); 665 if (path_index >= ClassLoaderExt::app_class_paths_start_index()) { 666 assert(path_index < ClassLoaderExt::app_module_paths_start_index(), "invalid path_index"); 667 return true; 668 } 669 } else { 670 // Check if this is from a PackageEntry/ModuleEntry defined in the AppClassloader. 671 if (pkg_entry == NULL) { 672 // It's not guaranteed that the class is from the classpath if the 673 // PackageEntry cannot be found from the AppClassloader. Need to check 674 // the boot and platform classloader as well. 675 ClassLoaderData* platform_loader_data = 676 ClassLoaderData::class_loader_data_or_null(SystemDictionary::java_platform_loader()); // can be NULL during bootstrap 677 if ((platform_loader_data == NULL || 678 ClassLoader::get_package_entry(pkg_name, platform_loader_data) == NULL) && 679 ClassLoader::get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data()) == NULL) { 680 // The PackageEntry is not defined in any of the boot/platform/app classloaders. 681 // The archived class must from -cp path and not from the runtime image. 682 if (!ent->is_modules_image() && path_index >= ClassLoaderExt::app_class_paths_start_index() && 683 path_index < ClassLoaderExt::app_module_paths_start_index()) { 684 return true; 685 } 686 } 687 } else if (mod_entry != NULL) { 688 // The package/module is defined in the AppClassLoader. We support 689 // archiving application module class from the runtime image or from 690 // a named module from a module path. 691 // Packages from the -cp path are in the unnamed_module. 692 if (ent->is_modules_image() && mod_entry->location()->starts_with("jrt:")) { 693 // shared module class from runtime image 694 return true; 695 } else if (pkg_entry->in_unnamed_module() && path_index >= ClassLoaderExt::app_class_paths_start_index() && 696 path_index < ClassLoaderExt::app_module_paths_start_index()) { 697 // shared class from -cp 698 DEBUG_ONLY( \ 699 ClassLoaderData* loader_data = class_loader_data(class_loader); \ 700 assert(mod_entry == loader_data->unnamed_module(), "the unnamed module is not defined in the classloader");) 701 return true; 702 } else { 703 if(!pkg_entry->in_unnamed_module() && 704 (path_index >= ClassLoaderExt::app_module_paths_start_index())&& 705 (path_index < FileMapInfo::get_number_of_shared_paths()) && 706 (strcmp(ent->name(), ClassLoader::skip_uri_protocol(mod_entry->location()->as_C_string())) == 0)) { 707 // shared module class from module path 708 return true; 709 } else { 710 assert(path_index < FileMapInfo::get_number_of_shared_paths(), "invalid path_index"); 711 } 712 } 713 } 714 } 715 } else { 716 // TEMP: if a shared class can be found by a custom loader, consider it visible now. 717 // FIXME: is this actually correct? 718 return true; 719 } 720 return false; 721 } 722 723 bool SystemDictionaryShared::has_platform_or_app_classes() { 724 if (FileMapInfo::current_info()->has_platform_or_app_classes()) { 725 return true; 726 } 727 if (DynamicArchive::is_mapped() && 728 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) { 729 return true; 730 } 731 return false; 732 } 733 734 // The following stack shows how this code is reached: 735 // 736 // [0] SystemDictionaryShared::find_or_load_shared_class() 737 // [1] JVM_FindLoadedClass 738 // [2] java.lang.ClassLoader.findLoadedClass0() 739 // [3] java.lang.ClassLoader.findLoadedClass() 740 // [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull() 741 // [5] jdk.internal.loader.BuiltinClassLoader.loadClass() 742 // [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or 743 // jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass() 744 // 745 // AppCDS supports fast class loading for these 2 built-in class loaders: 746 // jdk.internal.loader.ClassLoaders$PlatformClassLoader 747 // jdk.internal.loader.ClassLoaders$AppClassLoader 748 // with the following assumptions (based on the JDK core library source code): 749 // 750 // [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to 751 // load the named class. 752 // [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name). 753 // [c] At this point, if we can find the named class inside the 754 // shared_dictionary, we can perform further checks (see 755 // is_shared_class_visible_for_classloader() to ensure that this class 756 // was loaded by the same class loader during dump time. 757 // 758 // Given these assumptions, we intercept the findLoadedClass() call to invoke 759 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from 760 // the archive for the 2 built-in class loaders. This way, 761 // we can improve start-up because we avoid decoding the classfile, 762 // and avoid delegating to the parent loader. 763 // 764 // NOTE: there's a lot of assumption about the Java code. If any of that change, this 765 // needs to be redesigned. 766 767 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class( 768 Symbol* name, Handle class_loader, TRAPS) { 769 InstanceKlass* k = NULL; 770 if (UseSharedSpaces) { 771 if (!has_platform_or_app_classes()) { 772 return NULL; 773 } 774 775 if (SystemDictionary::is_system_class_loader(class_loader()) || 776 SystemDictionary::is_platform_class_loader(class_loader())) { 777 // Fix for 4474172; see evaluation for more details 778 class_loader = Handle( 779 THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader())); 780 ClassLoaderData *loader_data = register_loader(class_loader); 781 Dictionary* dictionary = loader_data->dictionary(); 782 783 unsigned int d_hash = dictionary->compute_hash(name); 784 785 bool DoObjectLock = true; 786 if (is_parallelCapable(class_loader)) { 787 DoObjectLock = false; 788 } 789 790 // Make sure we are synchronized on the class loader before we proceed 791 // 792 // Note: currently, find_or_load_shared_class is called only from 793 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader, 794 // which are parallel-capable loaders, so this lock is NOT taken. 795 Handle lockObject = compute_loader_lock_object(class_loader, THREAD); 796 check_loader_lock_contention(lockObject, THREAD); 797 ObjectLocker ol(lockObject, THREAD, DoObjectLock); 798 799 { 800 MutexLocker mu(THREAD, SystemDictionary_lock); 801 InstanceKlass* check = find_class(d_hash, name, dictionary); 802 if (check != NULL) { 803 return check; 804 } 805 } 806 807 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD); 808 if (k != NULL) { 809 define_instance_class(k, CHECK_NULL); 810 } 811 } 812 } 813 return k; 814 } 815 816 PackageEntry* SystemDictionaryShared::get_package_entry_from_class_name(Handle class_loader, Symbol* class_name) { 817 PackageEntry* pkg_entry = NULL; 818 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name); 819 if (pkg_name != NULL) { 820 pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name); 821 } 822 return pkg_entry; 823 } 824 825 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader( 826 Symbol* class_name, Handle class_loader, TRAPS) { 827 assert(UseSharedSpaces, "must be"); 828 InstanceKlass* ik = find_builtin_class(class_name); 829 830 if (ik != NULL) { 831 if ((ik->is_shared_app_class() && 832 SystemDictionary::is_system_class_loader(class_loader())) || 833 (ik->is_shared_platform_class() && 834 SystemDictionary::is_platform_class_loader(class_loader()))) { 835 PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, class_name); 836 Handle protection_domain = 837 SystemDictionaryShared::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL); 838 return load_shared_class(ik, class_loader, protection_domain, NULL, pkg_entry, THREAD); 839 } 840 } 841 return NULL; 842 } 843 844 void SystemDictionaryShared::oops_do(OopClosure* f) { 845 f->do_oop((oop*)&_shared_protection_domains); 846 f->do_oop((oop*)&_shared_jar_urls); 847 f->do_oop((oop*)&_shared_jar_manifests); 848 } 849 850 void SystemDictionaryShared::allocate_shared_protection_domain_array(int size, TRAPS) { 851 if (_shared_protection_domains == NULL) { 852 _shared_protection_domains = oopFactory::new_objArray( 853 SystemDictionary::ProtectionDomain_klass(), size, CHECK); 854 } 855 } 856 857 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) { 858 if (_shared_jar_urls == NULL) { 859 _shared_jar_urls = oopFactory::new_objArray( 860 SystemDictionary::URL_klass(), size, CHECK); 861 } 862 } 863 864 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) { 865 if (_shared_jar_manifests == NULL) { 866 _shared_jar_manifests = oopFactory::new_objArray( 867 SystemDictionary::Jar_Manifest_klass(), size, CHECK); 868 } 869 } 870 871 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) { 872 allocate_shared_protection_domain_array(size, CHECK); 873 allocate_shared_jar_url_array(size, CHECK); 874 allocate_shared_jar_manifest_array(size, CHECK); 875 } 876 877 // This function is called for loading only UNREGISTERED classes 878 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name, 879 Handle class_loader, 880 Handle protection_domain, 881 const ClassFileStream* cfs, 882 TRAPS) { 883 if (!UseSharedSpaces) { 884 return NULL; 885 } 886 if (class_name == NULL) { // don't do this for anonymous classes 887 return NULL; 888 } 889 if (class_loader.is_null() || 890 SystemDictionary::is_system_class_loader(class_loader()) || 891 SystemDictionary::is_platform_class_loader(class_loader())) { 892 // Do nothing for the BUILTIN loaders. 893 return NULL; 894 } 895 896 const RunTimeSharedClassInfo* record = find_record(&_unregistered_dictionary, &_dynamic_unregistered_dictionary, class_name); 897 if (record == NULL) { 898 return NULL; 899 } 900 901 int clsfile_size = cfs->length(); 902 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 903 904 if (!record->matches(clsfile_size, clsfile_crc32)) { 905 return NULL; 906 } 907 908 return acquire_class_for_current_thread(record->_klass, class_loader, 909 protection_domain, cfs, 910 THREAD); 911 } 912 913 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 914 InstanceKlass *ik, 915 Handle class_loader, 916 Handle protection_domain, 917 const ClassFileStream *cfs, 918 TRAPS) { 919 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 920 921 { 922 MutexLocker mu(THREAD, SharedDictionary_lock); 923 if (ik->class_loader_data() != NULL) { 924 // ik is already loaded (by this loader or by a different loader) 925 // or ik is being loaded by a different thread (by this loader or by a different loader) 926 return NULL; 927 } 928 929 // No other thread has acquired this yet, so give it to *this thread* 930 ik->set_class_loader_data(loader_data); 931 } 932 933 // No longer holding SharedDictionary_lock 934 // No need to lock, as <ik> can be held only by a single thread. 935 loader_data->add_class(ik); 936 937 // Get the package entry. 938 PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, ik->name()); 939 940 // Load and check super/interfaces, restore unsharable info 941 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, 942 cfs, pkg_entry, THREAD); 943 if (shared_klass == NULL || HAS_PENDING_EXCEPTION) { 944 // TODO: clean up <ik> so it can be used again 945 return NULL; 946 } 947 948 return shared_klass; 949 } 950 951 static ResourceHashtable< 952 Symbol*, bool, 953 primitive_hash<Symbol*>, 954 primitive_equals<Symbol*>, 955 6661, // prime number 956 ResourceObj::C_HEAP> _loaded_unregistered_classes; 957 958 bool SystemDictionaryShared::add_unregistered_class(InstanceKlass* k, TRAPS) { 959 assert(DumpSharedSpaces, "only when dumping"); 960 961 Symbol* name = k->name(); 962 if (_loaded_unregistered_classes.get(name) != NULL) { 963 // We don't allow duplicated unregistered classes of the same name. 964 return false; 965 } else { 966 bool isnew = _loaded_unregistered_classes.put(name, true); 967 assert(isnew, "sanity"); 968 MutexLocker mu_r(THREAD, Compile_lock); // add_to_hierarchy asserts this. 969 SystemDictionary::add_to_hierarchy(k, CHECK_false); 970 return true; 971 } 972 } 973 974 // This function is called to resolve the super/interfaces of shared classes for 975 // non-built-in loaders. E.g., ChildClass in the below example 976 // where "super:" (and optionally "interface:") have been specified. 977 // 978 // java/lang/Object id: 0 979 // Interface id: 2 super: 0 source: cust.jar 980 // ChildClass id: 4 super: 0 interfaces: 2 source: cust.jar 981 InstanceKlass* SystemDictionaryShared::dump_time_resolve_super_or_fail( 982 Symbol* child_name, Symbol* class_name, Handle class_loader, 983 Handle protection_domain, bool is_superclass, TRAPS) { 984 985 assert(DumpSharedSpaces, "only when dumping"); 986 987 ClassListParser* parser = ClassListParser::instance(); 988 if (parser == NULL) { 989 // We're still loading the well-known classes, before the ClassListParser is created. 990 return NULL; 991 } 992 if (child_name->equals(parser->current_class_name())) { 993 // When this function is called, all the numbered super and interface types 994 // must have already been loaded. Hence this function is never recursively called. 995 if (is_superclass) { 996 return parser->lookup_super_for_current_class(class_name); 997 } else { 998 return parser->lookup_interface_for_current_class(class_name); 999 } 1000 } else { 1001 // The VM is not trying to resolve a super type of parser->current_class_name(). 1002 // Instead, it's resolving an error class (because parser->current_class_name() has 1003 // failed parsing or verification). Don't do anything here. 1004 return NULL; 1005 } 1006 } 1007 1008 DumpTimeSharedClassInfo* SystemDictionaryShared::find_or_allocate_info_for(InstanceKlass* k) { 1009 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1010 if (_dumptime_table == NULL) { 1011 _dumptime_table = new (ResourceObj::C_HEAP, mtClass)DumpTimeSharedClassTable(); 1012 } 1013 return _dumptime_table->find_or_allocate_info_for(k); 1014 } 1015 1016 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) { 1017 Arguments::assert_is_dumping_archive(); 1018 assert(!is_builtin(k), "must be unregistered class"); 1019 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k); 1020 info->_clsfile_size = cfs->length(); 1021 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 1022 } 1023 1024 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) { 1025 (void)find_or_allocate_info_for(k); 1026 } 1027 1028 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) { 1029 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1030 DumpTimeSharedClassInfo* p = _dumptime_table->get(k); 1031 if (p == NULL) { 1032 return; 1033 } 1034 if (p->_verifier_constraints != NULL) { 1035 for (int i = 0; i < p->_verifier_constraints->length(); i++) { 1036 DumpTimeSharedClassInfo::DTConstraint constraint = p->_verifier_constraints->at(i); 1037 if (constraint._name != NULL ) { 1038 constraint._name->decrement_refcount(); 1039 } 1040 if (constraint._from_name != NULL ) { 1041 constraint._from_name->decrement_refcount(); 1042 } 1043 } 1044 FREE_C_HEAP_ARRAY(DTConstraint, p->_verifier_constraints); 1045 p->_verifier_constraints = NULL; 1046 } 1047 FREE_C_HEAP_ARRAY(char, p->_verifier_constraint_flags); 1048 p->_verifier_constraint_flags = NULL; 1049 _dumptime_table->remove(k); 1050 } 1051 1052 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) { 1053 while (k) { 1054 if (k->name()->equals("jdk/internal/event/Event")) { 1055 return true; 1056 } 1057 k = k->java_super(); 1058 } 1059 return false; 1060 } 1061 1062 void SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) { 1063 ResourceMark rm; 1064 log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason); 1065 } 1066 1067 bool SystemDictionaryShared::should_be_excluded(InstanceKlass* k) { 1068 if (k->class_loader_data()->is_unsafe_anonymous()) { 1069 warn_excluded(k, "Unsafe anonymous class"); 1070 return true; // unsafe anonymous classes are not archived, skip 1071 } 1072 if (k->is_in_error_state()) { 1073 warn_excluded(k, "In error state"); 1074 return true; 1075 } 1076 if (k->has_been_redefined()) { 1077 warn_excluded(k, "Has been redefined"); 1078 return true; 1079 } 1080 if (k->shared_classpath_index() < 0 && is_builtin(k)) { 1081 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native 1082 // agent during dump time). 1083 warn_excluded(k, "Unsupported location"); 1084 return true; 1085 } 1086 if (k->signers() != NULL) { 1087 // We cannot include signed classes in the archive because the certificates 1088 // used during dump time may be different than those used during 1089 // runtime (due to expiration, etc). 1090 warn_excluded(k, "Signed JAR"); 1091 return true; 1092 } 1093 if (is_jfr_event_class(k)) { 1094 // We cannot include JFR event classes because they need runtime-specific 1095 // instrumentation in order to work with -XX:FlightRecorderOptions=retransform=false. 1096 // There are only a small number of these classes, so it's not worthwhile to 1097 // support them and make CDS more complicated. 1098 warn_excluded(k, "JFR event class"); 1099 return true; 1100 } 1101 if (k->init_state() < InstanceKlass::linked) { 1102 // In CDS dumping, we will attempt to link all classes. Those that fail to link will 1103 // be recorded in DumpTimeSharedClassInfo. 1104 Arguments::assert_is_dumping_archive(); 1105 1106 // TODO -- rethink how this can be handled. 1107 // We should try to link ik, however, we can't do it here because 1108 // 1. We are at VM exit 1109 // 2. linking a class may cause other classes to be loaded, which means 1110 // a custom ClassLoader.loadClass() may be called, at a point where the 1111 // class loader doesn't expect it. 1112 if (has_class_failed_verification(k)) { 1113 warn_excluded(k, "Failed verification"); 1114 } else { 1115 warn_excluded(k, "Not linked"); 1116 } 1117 return true; 1118 } 1119 if (k->major_version() < 50 /*JAVA_6_VERSION*/) { 1120 ResourceMark rm; 1121 log_warning(cds)("Pre JDK 6 class not supported by CDS: %u.%u %s", 1122 k->major_version(), k->minor_version(), k->name()->as_C_string()); 1123 return true; 1124 } 1125 1126 InstanceKlass* super = k->java_super(); 1127 if (super != NULL && should_be_excluded(super)) { 1128 ResourceMark rm; 1129 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string()); 1130 return true; 1131 } 1132 1133 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 1134 int len = interfaces->length(); 1135 for (int i = 0; i < len; i++) { 1136 InstanceKlass* intf = interfaces->at(i); 1137 if (should_be_excluded(intf)) { 1138 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string()); 1139 return true; 1140 } 1141 } 1142 1143 return false; 1144 } 1145 1146 // k is a class before relocating by ArchiveCompactor 1147 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) { 1148 ResourceMark rm; 1149 const char* name = k->name()->as_C_string(); 1150 DumpTimeSharedClassInfo* info = _dumptime_table->get(k); 1151 assert(_no_class_loading_should_happen, "class loading must be disabled"); 1152 guarantee(info != NULL, "Class %s must be entered into _dumptime_table", name); 1153 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name); 1154 if (is_builtin(k)) { 1155 guarantee(!k->is_shared_unregistered_class(), 1156 "Class loader type must be set for BUILTIN class %s", name); 1157 } else { 1158 guarantee(k->is_shared_unregistered_class(), 1159 "Class loader type must not be set for UNREGISTERED class %s", name); 1160 } 1161 } 1162 1163 class ExcludeDumpTimeSharedClasses : StackObj { 1164 public: 1165 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1166 if (SystemDictionaryShared::should_be_excluded(k)) { 1167 info.set_excluded(); 1168 } 1169 return true; // keep on iterating 1170 } 1171 }; 1172 1173 void SystemDictionaryShared::check_excluded_classes() { 1174 ExcludeDumpTimeSharedClasses excl; 1175 _dumptime_table->iterate(&excl); 1176 _dumptime_table->update_counts(); 1177 } 1178 1179 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) { 1180 assert(_no_class_loading_should_happen, "sanity"); 1181 Arguments::assert_is_dumping_archive(); 1182 return find_or_allocate_info_for(k)->is_excluded(); 1183 } 1184 1185 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) { 1186 Arguments::assert_is_dumping_archive(); 1187 find_or_allocate_info_for(ik)->set_failed_verification(); 1188 } 1189 1190 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) { 1191 Arguments::assert_is_dumping_archive(); 1192 if (_dumptime_table == NULL) { 1193 assert(DynamicDumpSharedSpaces, "sanity"); 1194 assert(ik->is_shared(), "must be a shared class in the static archive"); 1195 return false; 1196 } 1197 DumpTimeSharedClassInfo* p = _dumptime_table->get(ik); 1198 return (p == NULL) ? false : p->failed_verification(); 1199 } 1200 1201 class IterateDumpTimeSharedClassTable : StackObj { 1202 MetaspaceClosure *_it; 1203 public: 1204 IterateDumpTimeSharedClassTable(MetaspaceClosure* it) : _it(it) {} 1205 1206 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1207 if (!info.is_excluded()) { 1208 info.metaspace_pointers_do(_it); 1209 } 1210 return true; // keep on iterating 1211 } 1212 }; 1213 1214 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) { 1215 IterateDumpTimeSharedClassTable iter(it); 1216 _dumptime_table->iterate(&iter); 1217 } 1218 1219 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name, 1220 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 1221 Arguments::assert_is_dumping_archive(); 1222 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k); 1223 info->add_verification_constraint(k, name, from_name, from_field_is_protected, 1224 from_is_array, from_is_object); 1225 1226 if (DynamicDumpSharedSpaces) { 1227 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during 1228 // the initial run prior to creating the archive before vm exit. We will also perform verification 1229 // check when running with the archive. 1230 return false; 1231 } else { 1232 if (is_builtin(k)) { 1233 // For builtin class loaders, we can try to complete the verification check at dump time, 1234 // because we can resolve all the constraint classes. We will also perform verification check 1235 // when running with the archive. 1236 return false; 1237 } else { 1238 // For non-builtin class loaders, we cannot complete the verification check at dump time, 1239 // because at dump time we don't know how to resolve classes for such loaders. 1240 return true; 1241 } 1242 } 1243 } 1244 1245 void DumpTimeSharedClassInfo::add_verification_constraint(InstanceKlass* k, Symbol* name, 1246 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 1247 if (_verifier_constraints == NULL) { 1248 _verifier_constraints = new(ResourceObj::C_HEAP, mtClass) GrowableArray<DTConstraint>(4, true, mtClass); 1249 } 1250 if (_verifier_constraint_flags == NULL) { 1251 _verifier_constraint_flags = new(ResourceObj::C_HEAP, mtClass) GrowableArray<char>(4, true, mtClass); 1252 } 1253 GrowableArray<DTConstraint>* vc_array = _verifier_constraints; 1254 for (int i = 0; i < vc_array->length(); i++) { 1255 DTConstraint* p = vc_array->adr_at(i); 1256 if (name == p->_name && from_name == p->_from_name) { 1257 return; 1258 } 1259 } 1260 DTConstraint cons(name, from_name); 1261 vc_array->append(cons); 1262 1263 GrowableArray<char>* vcflags_array = _verifier_constraint_flags; 1264 char c = 0; 1265 c |= from_field_is_protected ? SystemDictionaryShared::FROM_FIELD_IS_PROTECTED : 0; 1266 c |= from_is_array ? SystemDictionaryShared::FROM_IS_ARRAY : 0; 1267 c |= from_is_object ? SystemDictionaryShared::FROM_IS_OBJECT : 0; 1268 vcflags_array->append(c); 1269 1270 if (log_is_enabled(Trace, cds, verification)) { 1271 ResourceMark rm; 1272 log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s [0x%x]", 1273 k->external_name(), from_name->as_klass_external_name(), 1274 name->as_klass_external_name(), c); 1275 } 1276 } 1277 1278 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 1279 TRAPS) { 1280 assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only"); 1281 RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(klass); 1282 1283 int length = record->_num_constraints; 1284 if (length > 0) { 1285 for (int i = 0; i < length; i++) { 1286 Symbol* name = record->get_constraint_name(i); 1287 Symbol* from_name = record->get_constraint_from_name(i); 1288 char c = record->get_constraint_flag(i); 1289 1290 if (log_is_enabled(Trace, cds, verification)) { 1291 ResourceMark rm(THREAD); 1292 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]", 1293 klass->external_name(), from_name->as_klass_external_name(), 1294 name->as_klass_external_name(), c); 1295 } 1296 1297 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false; 1298 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false; 1299 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false; 1300 1301 bool ok = VerificationType::resolve_and_check_assignability(klass, name, 1302 from_name, from_field_is_protected, from_is_array, from_is_object, CHECK); 1303 if (!ok) { 1304 ResourceMark rm(THREAD); 1305 stringStream ss; 1306 1307 ss.print_cr("Bad type on operand stack"); 1308 ss.print_cr("Exception Details:"); 1309 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 1310 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 1311 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 1312 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 1313 } 1314 } 1315 } 1316 } 1317 1318 class EstimateSizeForArchive : StackObj { 1319 size_t _shared_class_info_size; 1320 int _num_builtin_klasses; 1321 int _num_unregistered_klasses; 1322 1323 public: 1324 EstimateSizeForArchive() { 1325 _shared_class_info_size = 0; 1326 _num_builtin_klasses = 0; 1327 _num_unregistered_klasses = 0; 1328 } 1329 1330 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1331 if (!info.is_excluded()) { 1332 size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_constraints()); 1333 _shared_class_info_size += align_up(byte_size, BytesPerWord); 1334 } 1335 return true; // keep on iterating 1336 } 1337 1338 size_t total() { 1339 return _shared_class_info_size; 1340 } 1341 }; 1342 1343 size_t SystemDictionaryShared::estimate_size_for_archive() { 1344 EstimateSizeForArchive est; 1345 _dumptime_table->iterate(&est); 1346 return est.total() + 1347 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) + 1348 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false)); 1349 } 1350 1351 class CopySharedClassInfoToArchive : StackObj { 1352 CompactHashtableWriter* _writer; 1353 bool _is_builtin; 1354 public: 1355 CopySharedClassInfoToArchive(CompactHashtableWriter* writer, 1356 bool is_builtin, 1357 bool is_static_archive) 1358 : _writer(writer), _is_builtin(is_builtin) {} 1359 1360 bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) { 1361 if (!info.is_excluded() && info.is_builtin() == _is_builtin) { 1362 size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_constraints()); 1363 RunTimeSharedClassInfo* record; 1364 record = (RunTimeSharedClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size); 1365 record->init(info); 1366 1367 unsigned int hash; 1368 Symbol* name = info._klass->name(); 1369 if (DynamicDumpSharedSpaces) { 1370 name = DynamicArchive::original_to_target(name); 1371 } 1372 hash = SystemDictionaryShared::hash_for_shared_dictionary(name); 1373 u4 delta; 1374 if (DynamicDumpSharedSpaces) { 1375 delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(record)); 1376 } else { 1377 delta = MetaspaceShared::object_delta_u4(record); 1378 } 1379 _writer->add(hash, delta); 1380 if (log_is_enabled(Trace, cds, hashtables)) { 1381 ResourceMark rm; 1382 log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name()); 1383 } 1384 1385 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeSharedClassInfo* 1386 RunTimeSharedClassInfo::set_for(info._klass, record); 1387 } 1388 return true; // keep on iterating 1389 } 1390 }; 1391 1392 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary, 1393 bool is_builtin, 1394 bool is_static_archive) { 1395 CompactHashtableStats stats; 1396 dictionary->reset(); 1397 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats); 1398 CopySharedClassInfoToArchive copy(&writer, is_builtin, is_static_archive); 1399 _dumptime_table->iterate(©); 1400 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary"); 1401 } 1402 1403 void SystemDictionaryShared::write_to_archive(bool is_static_archive) { 1404 if (is_static_archive) { 1405 write_dictionary(&_builtin_dictionary, true); 1406 write_dictionary(&_unregistered_dictionary, false); 1407 } else { 1408 write_dictionary(&_dynamic_builtin_dictionary, true); 1409 write_dictionary(&_dynamic_unregistered_dictionary, false); 1410 } 1411 } 1412 1413 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc, 1414 bool is_static_archive) { 1415 if (is_static_archive) { 1416 _builtin_dictionary.serialize_header(soc); 1417 _unregistered_dictionary.serialize_header(soc); 1418 } else { 1419 _dynamic_builtin_dictionary.serialize_header(soc); 1420 _dynamic_unregistered_dictionary.serialize_header(soc); 1421 } 1422 } 1423 1424 void SystemDictionaryShared::serialize_well_known_klasses(SerializeClosure* soc) { 1425 for (int i = FIRST_WKID; i < WKID_LIMIT; i++) { 1426 soc->do_ptr((void**)&_well_known_klasses[i]); 1427 } 1428 } 1429 1430 const RunTimeSharedClassInfo* 1431 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) { 1432 if (!UseSharedSpaces || !name->is_shared()) { 1433 // The names of all shared classes must also be a shared Symbol. 1434 return NULL; 1435 } 1436 1437 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(name); 1438 const RunTimeSharedClassInfo* record = NULL; 1439 if (!MetaspaceShared::is_shared_dynamic(name)) { 1440 // The names of all shared classes in the static dict must also be in the 1441 // static archive 1442 record = static_dict->lookup(name, hash, 0); 1443 } 1444 1445 if (record == NULL && DynamicArchive::is_mapped()) { 1446 record = dynamic_dict->lookup(name, hash, 0); 1447 } 1448 1449 return record; 1450 } 1451 1452 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) { 1453 const RunTimeSharedClassInfo* record = find_record(&_builtin_dictionary, &_dynamic_builtin_dictionary, name); 1454 if (record != NULL) { 1455 return record->_klass; 1456 } else { 1457 return NULL; 1458 } 1459 } 1460 1461 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) { 1462 assert(DumpSharedSpaces, "supported only when dumping"); 1463 DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k); 1464 info->_id = id; 1465 } 1466 1467 class SharedDictionaryPrinter : StackObj { 1468 outputStream* _st; 1469 int _index; 1470 public: 1471 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 1472 1473 void do_value(const RunTimeSharedClassInfo* record) { 1474 ResourceMark rm; 1475 _st->print_cr("%4d: %s", (_index++), record->_klass->external_name()); 1476 } 1477 }; 1478 1479 void SystemDictionaryShared::print_on(outputStream* st) { 1480 if (UseSharedSpaces) { 1481 st->print_cr("Shared Dictionary"); 1482 SharedDictionaryPrinter p(st); 1483 _builtin_dictionary.iterate(&p); 1484 _unregistered_dictionary.iterate(&p); 1485 if (DynamicArchive::is_mapped()) { 1486 _dynamic_builtin_dictionary.iterate(&p); 1487 _unregistered_dictionary.iterate(&p); 1488 } 1489 } 1490 } 1491 1492 void SystemDictionaryShared::print_table_statistics(outputStream* st) { 1493 if (UseSharedSpaces) { 1494 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary"); 1495 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 1496 if (DynamicArchive::is_mapped()) { 1497 _dynamic_builtin_dictionary.print_table_statistics(st, "Dynamic Builtin Shared Dictionary"); 1498 _dynamic_unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 1499 } 1500 } 1501 } 1502 1503 bool SystemDictionaryShared::empty_dumptime_table() { 1504 if (_dumptime_table == NULL) { 1505 return true; 1506 } 1507 _dumptime_table->update_counts(); 1508 if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){ 1509 return true; 1510 } 1511 return false; 1512 }