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 "jni.h" 27 #include "classfile/classLoader.hpp" 28 #include "classfile/classLoaderData.inline.hpp" 29 #include "classfile/javaClasses.inline.hpp" 30 #include "classfile/moduleEntry.hpp" 31 #include "logging/log.hpp" 32 #include "memory/filemap.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "memory/universe.hpp" 35 #include "oops/oopHandle.inline.hpp" 36 #include "oops/symbol.hpp" 37 #include "runtime/handles.inline.hpp" 38 #include "runtime/safepoint.hpp" 39 #include "utilities/events.hpp" 40 #include "utilities/growableArray.hpp" 41 #include "utilities/hashtable.inline.hpp" 42 #include "utilities/ostream.hpp" 43 44 ModuleEntry* ModuleEntryTable::_javabase_module = NULL; 45 46 oop ModuleEntry::module() const { return _module.resolve(); } 47 48 void ModuleEntry::set_location(Symbol* location) { 49 if (_location != NULL) { 50 // _location symbol's refcounts are managed by ModuleEntry, 51 // must decrement the old one before updating. 52 _location->decrement_refcount(); 53 } 54 55 _location = location; 56 57 if (location != NULL) { 58 location->increment_refcount(); 59 CDS_ONLY(if (UseSharedSpaces) { 60 _shared_path_index = FileMapInfo::get_module_shared_path_index(location); 61 }); 62 } 91 } 92 return true; 93 } 94 95 void ModuleEntry::set_version(Symbol* version) { 96 if (_version != NULL) { 97 // _version symbol's refcounts are managed by ModuleEntry, 98 // must decrement the old one before updating. 99 _version->decrement_refcount(); 100 } 101 102 _version = version; 103 104 if (version != NULL) { 105 version->increment_refcount(); 106 } 107 } 108 109 // Returns the shared ProtectionDomain 110 oop ModuleEntry::shared_protection_domain() { 111 return _pd.resolve(); 112 } 113 114 // Set the shared ProtectionDomain atomically 115 void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data, 116 Handle pd_h) { 117 // Create a handle for the shared ProtectionDomain and save it atomically. 118 // init_handle_locked checks if someone beats us setting the _pd cache. 119 loader_data->init_handle_locked(_pd, pd_h); 120 } 121 122 // Returns true if this module can read module m 123 bool ModuleEntry::can_read(ModuleEntry* m) const { 124 assert(m != NULL, "No module to lookup in this module's reads list"); 125 126 // Unnamed modules read everyone and all modules 127 // read java.base. If either of these conditions 128 // hold, readability has been established. 129 if (!this->is_named() || 130 (m == ModuleEntryTable::javabase_moduleEntry())) { 131 return true; 132 } 133 134 MutexLocker m1(Module_lock); 135 // This is a guard against possible race between agent threads that redefine 136 // or retransform classes in this module. Only one of them is adding the 137 // default read edges to the unnamed modules of the boot and app class loaders 138 // with an upcall to jdk.internal.module.Modules.transformedByAgent. 139 // At the same time, another thread can instrument the module classes by 344 // Clean out the C heap allocated reads list first before freeing the entry 345 to_remove->delete_reads(); 346 if (to_remove->name() != NULL) { 347 to_remove->name()->decrement_refcount(); 348 } 349 if (to_remove->version() != NULL) { 350 to_remove->version()->decrement_refcount(); 351 } 352 if (to_remove->location() != NULL) { 353 to_remove->location()->decrement_refcount(); 354 } 355 356 // Unlink from the Hashtable prior to freeing 357 unlink_entry(to_remove); 358 FREE_C_HEAP_ARRAY(char, to_remove); 359 } 360 } 361 assert(number_of_entries() == 0, "should have removed all entries"); 362 assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list"); 363 } 364 365 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle, 366 bool is_open, Symbol* name, 367 Symbol* version, Symbol* location, 368 ClassLoaderData* loader_data) { 369 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 370 ModuleEntry* entry = (ModuleEntry*)Hashtable<Symbol*, mtModule>::allocate_new_entry(hash, name); 371 372 // Initialize fields specific to a ModuleEntry 373 entry->init(); 374 if (name != NULL) { 375 name->increment_refcount(); 376 } else { 377 // Unnamed modules can read all other unnamed modules. 378 entry->set_can_read_all_unnamed(); 379 } 380 381 if (!module_handle.is_null()) { 382 entry->set_module(loader_data->add_handle(module_handle)); 383 } | 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 "jni.h" 27 #include "classfile/classLoader.hpp" 28 #include "classfile/classLoaderData.inline.hpp" 29 #include "classfile/javaClasses.inline.hpp" 30 #include "classfile/moduleEntry.hpp" 31 #include "logging/log.hpp" 32 #include "memory/archiveUtils.hpp" 33 #include "memory/filemap.hpp" 34 #include "memory/heapShared.hpp" 35 #include "memory/metaspaceShared.hpp" 36 #include "memory/resourceArea.hpp" 37 #include "memory/universe.hpp" 38 #include "oops/oopHandle.inline.hpp" 39 #include "oops/symbol.hpp" 40 #include "runtime/handles.inline.hpp" 41 #include "runtime/safepoint.hpp" 42 #include "utilities/events.hpp" 43 #include "utilities/growableArray.hpp" 44 #include "utilities/hashtable.inline.hpp" 45 #include "utilities/ostream.hpp" 46 #include "utilities/quickSort.hpp" 47 #include "utilities/resourceHash.hpp" 48 49 ModuleEntry* ModuleEntryTable::_javabase_module = NULL; 50 51 oop ModuleEntry::module() const { return _module.resolve(); } 52 53 void ModuleEntry::set_location(Symbol* location) { 54 if (_location != NULL) { 55 // _location symbol's refcounts are managed by ModuleEntry, 56 // must decrement the old one before updating. 57 _location->decrement_refcount(); 58 } 59 60 _location = location; 61 62 if (location != NULL) { 63 location->increment_refcount(); 64 CDS_ONLY(if (UseSharedSpaces) { 65 _shared_path_index = FileMapInfo::get_module_shared_path_index(location); 66 }); 67 } 96 } 97 return true; 98 } 99 100 void ModuleEntry::set_version(Symbol* version) { 101 if (_version != NULL) { 102 // _version symbol's refcounts are managed by ModuleEntry, 103 // must decrement the old one before updating. 104 _version->decrement_refcount(); 105 } 106 107 _version = version; 108 109 if (version != NULL) { 110 version->increment_refcount(); 111 } 112 } 113 114 // Returns the shared ProtectionDomain 115 oop ModuleEntry::shared_protection_domain() { 116 return _shared_pd.resolve(); 117 } 118 119 // Set the shared ProtectionDomain atomically 120 void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data, 121 Handle pd_h) { 122 // Create a handle for the shared ProtectionDomain and save it atomically. 123 // init_handle_locked checks if someone beats us setting the _shared_pd cache. 124 loader_data->init_handle_locked(_shared_pd, pd_h); 125 } 126 127 // Returns true if this module can read module m 128 bool ModuleEntry::can_read(ModuleEntry* m) const { 129 assert(m != NULL, "No module to lookup in this module's reads list"); 130 131 // Unnamed modules read everyone and all modules 132 // read java.base. If either of these conditions 133 // hold, readability has been established. 134 if (!this->is_named() || 135 (m == ModuleEntryTable::javabase_moduleEntry())) { 136 return true; 137 } 138 139 MutexLocker m1(Module_lock); 140 // This is a guard against possible race between agent threads that redefine 141 // or retransform classes in this module. Only one of them is adding the 142 // default read edges to the unnamed modules of the boot and app class loaders 143 // with an upcall to jdk.internal.module.Modules.transformedByAgent. 144 // At the same time, another thread can instrument the module classes by 349 // Clean out the C heap allocated reads list first before freeing the entry 350 to_remove->delete_reads(); 351 if (to_remove->name() != NULL) { 352 to_remove->name()->decrement_refcount(); 353 } 354 if (to_remove->version() != NULL) { 355 to_remove->version()->decrement_refcount(); 356 } 357 if (to_remove->location() != NULL) { 358 to_remove->location()->decrement_refcount(); 359 } 360 361 // Unlink from the Hashtable prior to freeing 362 unlink_entry(to_remove); 363 FREE_C_HEAP_ARRAY(char, to_remove); 364 } 365 } 366 assert(number_of_entries() == 0, "should have removed all entries"); 367 assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list"); 368 } 369 370 #if INCLUDE_CDS_JAVA_HEAP 371 typedef ResourceHashtable< 372 const ModuleEntry*, 373 ModuleEntry*, 374 primitive_hash<const ModuleEntry*>, 375 primitive_equals<const ModuleEntry*>, 376 557, // prime number 377 ResourceObj::C_HEAP> ArchivedModuleEntries; 378 static ArchivedModuleEntries* _archive_modules_entries = NULL; 379 380 ModuleEntry* ModuleEntry::allocate_archived_entry() const { 381 assert(is_named(), "unnamed packages/modules are not archived"); 382 ModuleEntry* archived_entry = (ModuleEntry*)MetaspaceShared::read_write_space_alloc(sizeof(ModuleEntry)); 383 memcpy((void*)archived_entry, (void*)this, sizeof(ModuleEntry)); 384 385 if (_archive_modules_entries == NULL) { 386 _archive_modules_entries = new (ResourceObj::C_HEAP, mtClass)ArchivedModuleEntries(); 387 } 388 assert(_archive_modules_entries->get(this) == NULL, "Each ModuleEntry must not be shared across ModuleEntryTables"); 389 _archive_modules_entries->put(this, archived_entry); 390 391 return archived_entry; 392 } 393 394 ModuleEntry* ModuleEntry::get_archived_entry(ModuleEntry* orig_entry) { 395 ModuleEntry** ptr = _archive_modules_entries->get(orig_entry); 396 assert(ptr != NULL && *ptr != NULL, "must have been allocated"); 397 return *ptr; 398 } 399 400 // GrowableArrays cannot be directly archived, as they need to be expandable at runtime. 401 // Write it out as an Array, and convert it back to GrowableArray at runtime. 402 Array<ModuleEntry*>* ModuleEntry::write_archived_entry_array(GrowableArray<ModuleEntry*>* array) { 403 Array<ModuleEntry*>* archived_array = NULL; 404 int length = (array == NULL) ? 0 : array->length(); 405 if (length > 0) { 406 archived_array = MetaspaceShared::new_ro_array<ModuleEntry*>(length); 407 for (int i = 0; i < length; i++) { 408 ModuleEntry* archived_entry = get_archived_entry(array->at(i)); 409 archived_array->at_put(i, archived_entry); 410 ArchivePtrMarker::mark_pointer((address*)archived_array->adr_at(i)); 411 } 412 } 413 414 return archived_array; 415 } 416 417 GrowableArray<ModuleEntry*>* ModuleEntry::read_archived_entry_array(Array<ModuleEntry*>* archived_array) { 418 GrowableArray<ModuleEntry*>* array = NULL; 419 int length = (archived_array == NULL) ? 0 : archived_array->length(); 420 if (length > 0) { 421 array = new (ResourceObj::C_HEAP, mtModule)GrowableArray<ModuleEntry*>(length, mtModule); 422 for (int i = 0; i < length; i++) { 423 ModuleEntry* archived_entry = archived_array->at(i); 424 array->append(archived_entry); 425 } 426 } 427 428 return array; 429 } 430 431 void ModuleEntry::init_as_archived_entry() { 432 Array<ModuleEntry*>* archived_reads = write_archived_entry_array(_reads); 433 434 set_next(NULL); 435 set_hash(0x0); // re-init at runtime 436 _loader_data = NULL; // re-init at runtime 437 _shared_path_index = FileMapInfo::get_module_shared_path_index(_location); 438 if (literal() != NULL) { 439 set_literal(MetaspaceShared::get_relocated_symbol(literal())); 440 ArchivePtrMarker::mark_pointer((address*)literal_addr()); 441 } 442 _reads = (GrowableArray<ModuleEntry*>*)archived_reads; 443 if (_version != NULL) { 444 _version = MetaspaceShared::get_relocated_symbol(_version); 445 } 446 if (_location != NULL) { 447 _location = MetaspaceShared::get_relocated_symbol(_location); 448 } 449 450 ArchivePtrMarker::mark_pointer((address*)&_reads); 451 ArchivePtrMarker::mark_pointer((address*)&_version); 452 ArchivePtrMarker::mark_pointer((address*)&_location); 453 } 454 455 void ModuleEntry::init_archived_oops() { 456 assert(DumpSharedSpaces, "static dump only"); 457 oop module_obj = module(); 458 if (module_obj != NULL) { 459 oop m = HeapShared::find_archived_heap_object(module_obj); 460 assert(m != NULL, "sanity"); 461 _archived_module_narrow_oop = CompressedOops::encode(m); 462 } 463 assert(shared_protection_domain() == NULL, "never set during -Xshare:dump"); 464 // Clear handles and restore at run time. Handles cannot be archived. 465 OopHandle null_handle; 466 _module = null_handle; 467 } 468 469 void ModuleEntry::load_from_archive(ClassLoaderData* loader_data) { 470 set_loader_data(loader_data); 471 _reads = read_archived_entry_array((Array<ModuleEntry*>*)_reads); 472 JFR_ONLY(INIT_ID(this);) 473 } 474 475 void ModuleEntry::restore_archive_oops(ClassLoaderData* loader_data) { 476 Handle module_handle(Thread::current(), HeapShared::materialize_archived_object(_archived_module_narrow_oop)); 477 assert(module_handle.not_null(), "huh"); 478 set_module(loader_data->add_handle(module_handle)); 479 480 // This was cleared to zero during dump time -- we didn't save the value 481 // because it may be affected by archive relocation. 482 java_lang_Module::set_module_entry(module_handle(), this); 483 484 if (loader_data->class_loader() != NULL) { 485 java_lang_Module::set_loader(module_handle(), loader_data->class_loader()); 486 } 487 } 488 489 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) { 490 assert(a == b || a->name() != b->name(), "no duplicated names"); 491 return a->name()->fast_compare(b->name()); 492 } 493 494 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() { 495 Array<ModuleEntry*>* archived_modules = MetaspaceShared::new_rw_array<ModuleEntry*>(number_of_entries()); 496 int n = 0; 497 for (int i = 0; i < table_size(); ++i) { 498 for (ModuleEntry* m = bucket(i); m != NULL; m = m->next()) { 499 archived_modules->at_put(n++, m); 500 } 501 } 502 if (n > 1) { 503 // Always allocate in the same order to produce deterministic archive. 504 QuickSort::sort(archived_modules->data(), n, (_sort_Fn)compare_module_by_name, true); 505 } 506 for (int i = 0; i < n; i++) { 507 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry()); 508 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i)); 509 } 510 return archived_modules; 511 } 512 513 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) { 514 assert(DumpSharedSpaces, "dump time only"); 515 for (int i = 0; i < archived_modules->length(); i++) { 516 ModuleEntry* archived_entry = archived_modules->at(i); 517 archived_entry->init_as_archived_entry(); 518 } 519 } 520 521 void ModuleEntryTable::init_archived_oops(Array<ModuleEntry*>* archived_modules) { 522 assert(DumpSharedSpaces, "dump time only"); 523 for (int i = 0; i < archived_modules->length(); i++) { 524 ModuleEntry* archived_entry = archived_modules->at(i); 525 archived_entry->init_archived_oops(); 526 } 527 } 528 529 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data, 530 Array<ModuleEntry*>* archived_modules) { 531 assert(UseSharedSpaces, "runtime only"); 532 533 for (int i = 0; i < archived_modules->length(); i++) { 534 ModuleEntry* archived_entry = archived_modules->at(i); 535 archived_entry->load_from_archive(loader_data); 536 537 unsigned int hash = compute_hash(archived_entry->name()); 538 archived_entry->set_hash(hash); 539 add_entry(hash_to_index(hash), archived_entry); 540 } 541 } 542 543 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) { 544 assert(UseSharedSpaces, "runtime only"); 545 for (int i = 0; i < archived_modules->length(); i++) { 546 ModuleEntry* archived_entry = archived_modules->at(i); 547 archived_entry->restore_archive_oops(loader_data); 548 } 549 } 550 #endif // INCLUDE_CDS_JAVA_HEAP 551 552 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle, 553 bool is_open, Symbol* name, 554 Symbol* version, Symbol* location, 555 ClassLoaderData* loader_data) { 556 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 557 ModuleEntry* entry = (ModuleEntry*)Hashtable<Symbol*, mtModule>::allocate_new_entry(hash, name); 558 559 // Initialize fields specific to a ModuleEntry 560 entry->init(); 561 if (name != NULL) { 562 name->increment_refcount(); 563 } else { 564 // Unnamed modules can read all other unnamed modules. 565 entry->set_can_read_all_unnamed(); 566 } 567 568 if (!module_handle.is_null()) { 569 entry->set_module(loader_data->add_handle(module_handle)); 570 } |