1 /* 2 * Copyright (c) 2016, 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 "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/resourceHash.hpp" 47 48 ModuleEntry* ModuleEntryTable::_javabase_module = NULL; 49 50 oop ModuleEntry::module() const { return _module.resolve(); } 51 52 void ModuleEntry::set_location(Symbol* location) { 53 if (_location != NULL) { 54 // _location symbol's refcounts are managed by ModuleEntry, 55 // must decrement the old one before updating. 56 _location->decrement_refcount(); 57 } 58 59 _location = location; 60 61 if (location != NULL) { 62 location->increment_refcount(); 63 CDS_ONLY(if (UseSharedSpaces) { 64 _shared_path_index = FileMapInfo::get_module_shared_path_index(location); 65 }); 66 } 67 } 68 69 // Return true if the module's version should be displayed in error messages, 70 // logging, etc. 71 // Return false if the module's version is null, if it is unnamed, or if the 72 // module is not an upgradeable module. 73 // Detect if the module is not upgradeable by checking: 74 // 1. Module location is "jrt:/java." and its loader is boot or platform 75 // 2. Module location is "jrt:/jdk.", its loader is one of the builtin loaders 76 // and its version is the same as module java.base's version 77 // The above check is imprecise but should work in almost all cases. 78 bool ModuleEntry::should_show_version() { 79 if (version() == NULL || !is_named()) return false; 80 81 if (location() != NULL) { 82 ResourceMark rm; 83 const char* loc = location()->as_C_string(); 84 ClassLoaderData* cld = loader_data(); 85 86 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder"); 87 if ((cld->is_the_null_class_loader_data() || cld->is_platform_class_loader_data()) && 88 (strncmp(loc, "jrt:/java.", 10) == 0)) { 89 return false; 90 } 91 if ((ModuleEntryTable::javabase_moduleEntry()->version()->fast_compare(version()) == 0) && 92 cld->is_permanent_class_loader_data() && (strncmp(loc, "jrt:/jdk.", 9) == 0)) { 93 return false; 94 } 95 } 96 return true; 97 } 98 99 void ModuleEntry::set_version(Symbol* version) { 100 if (_version != NULL) { 101 // _version symbol's refcounts are managed by ModuleEntry, 102 // must decrement the old one before updating. 103 _version->decrement_refcount(); 104 } 105 106 _version = version; 107 108 if (version != NULL) { 109 version->increment_refcount(); 110 } 111 } 112 113 // Returns the shared ProtectionDomain 114 oop ModuleEntry::shared_protection_domain() { 115 return _shared_pd.resolve(); 116 } 117 118 // Set the shared ProtectionDomain atomically 119 void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data, 120 Handle pd_h) { 121 // Create a handle for the shared ProtectionDomain and save it atomically. 122 // init_handle_locked checks if someone beats us setting the _shared_pd cache. 123 loader_data->init_handle_locked(_shared_pd, pd_h); 124 } 125 126 // Returns true if this module can read module m 127 bool ModuleEntry::can_read(ModuleEntry* m) const { 128 assert(m != NULL, "No module to lookup in this module's reads list"); 129 130 // Unnamed modules read everyone and all modules 131 // read java.base. If either of these conditions 132 // hold, readability has been established. 133 if (!this->is_named() || 134 (m == ModuleEntryTable::javabase_moduleEntry())) { 135 return true; 136 } 137 138 MutexLocker m1(Module_lock); 139 // This is a guard against possible race between agent threads that redefine 140 // or retransform classes in this module. Only one of them is adding the 141 // default read edges to the unnamed modules of the boot and app class loaders 142 // with an upcall to jdk.internal.module.Modules.transformedByAgent. 143 // At the same time, another thread can instrument the module classes by 144 // injecting dependencies that require the default read edges for resolution. 145 if (this->has_default_read_edges() && !m->is_named()) { 146 ClassLoaderData* cld = m->loader_data(); 147 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder"); 148 if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) { 149 return true; // default read edge 150 } 151 } 152 if (!has_reads_list()) { 153 return false; 154 } else { 155 return _reads->contains(m); 156 } 157 } 158 159 // Add a new module to this module's reads list 160 void ModuleEntry::add_read(ModuleEntry* m) { 161 // Unnamed module is special cased and can read all modules 162 if (!is_named()) { 163 return; 164 } 165 166 MutexLocker m1(Module_lock); 167 if (m == NULL) { 168 set_can_read_all_unnamed(); 169 } else { 170 if (_reads == NULL) { 171 // Lazily create a module's reads list 172 _reads = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, mtModule); 173 } 174 175 // Determine, based on this newly established read edge to module m, 176 // if this module's read list should be walked at a GC safepoint. 177 set_read_walk_required(m->loader_data()); 178 179 // Establish readability to module m 180 _reads->append_if_missing(m); 181 } 182 } 183 184 // If the module's loader, that a read edge is being established to, is 185 // not the same loader as this module's and is not one of the 3 builtin 186 // class loaders, then this module's reads list must be walked at GC 187 // safepoint. Modules have the same life cycle as their defining class 188 // loaders and should be removed if dead. 189 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) { 190 assert(is_named(), "Cannot call set_read_walk_required on unnamed module"); 191 assert_locked_or_safepoint(Module_lock); 192 if (!_must_walk_reads && 193 loader_data() != m_loader_data && 194 !m_loader_data->is_builtin_class_loader_data()) { 195 _must_walk_reads = true; 196 if (log_is_enabled(Trace, module)) { 197 ResourceMark rm; 198 log_trace(module)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked", 199 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE); 200 } 201 } 202 } 203 204 // Set whether the module is open, i.e. all its packages are unqualifiedly exported 205 void ModuleEntry::set_is_open(bool is_open) { 206 assert_lock_strong(Module_lock); 207 _is_open = is_open; 208 } 209 210 // Returns true if the module has a non-empty reads list. As such, the unnamed 211 // module will return false. 212 bool ModuleEntry::has_reads_list() const { 213 assert_locked_or_safepoint(Module_lock); 214 return ((_reads != NULL) && !_reads->is_empty()); 215 } 216 217 // Purge dead module entries out of reads list. 218 void ModuleEntry::purge_reads() { 219 assert_locked_or_safepoint(Module_lock); 220 221 if (_must_walk_reads && has_reads_list()) { 222 // This module's _must_walk_reads flag will be reset based 223 // on the remaining live modules on the reads list. 224 _must_walk_reads = false; 225 226 if (log_is_enabled(Trace, module)) { 227 ResourceMark rm; 228 log_trace(module)("ModuleEntry::purge_reads(): module %s reads list being walked", 229 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE); 230 } 231 232 // Go backwards because this removes entries that are dead. 233 int len = _reads->length(); 234 for (int idx = len - 1; idx >= 0; idx--) { 235 ModuleEntry* module_idx = _reads->at(idx); 236 ClassLoaderData* cld_idx = module_idx->loader_data(); 237 if (cld_idx->is_unloading()) { 238 _reads->delete_at(idx); 239 } else { 240 // Update the need to walk this module's reads based on live modules 241 set_read_walk_required(cld_idx); 242 } 243 } 244 } 245 } 246 247 void ModuleEntry::module_reads_do(ModuleClosure* f) { 248 assert_locked_or_safepoint(Module_lock); 249 assert(f != NULL, "invariant"); 250 251 if (has_reads_list()) { 252 int reads_len = _reads->length(); 253 for (int i = 0; i < reads_len; ++i) { 254 f->do_module(_reads->at(i)); 255 } 256 } 257 } 258 259 void ModuleEntry::delete_reads() { 260 delete _reads; 261 _reads = NULL; 262 } 263 264 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) { 265 // The java.lang.Module for this loader's 266 // corresponding unnamed module can be found in the java.lang.ClassLoader object. 267 oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader()); 268 269 // Ensure that the unnamed module was correctly set when the class loader was constructed. 270 // Guarantee will cause a recognizable crash if the user code has circumvented calling the ClassLoader constructor. 271 ResourceMark rm; 272 guarantee(java_lang_Module::is_instance(module), 273 "The unnamed module for ClassLoader %s, is null or not an instance of java.lang.Module. The class loader has not been initialized correctly.", 274 cld->loader_name_and_id()); 275 276 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld); 277 278 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object. 279 java_lang_Module::set_module_entry(module, unnamed_module); 280 281 return unnamed_module; 282 } 283 284 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) { 285 // For the boot loader, the java.lang.Module for the unnamed module 286 // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At 287 // this point initially create the ModuleEntry for the unnamed module. 288 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld); 289 assert(unnamed_module != NULL, "boot loader unnamed module should not be null"); 290 return unnamed_module; 291 } 292 293 // When creating an unnamed module, this is called without holding the Module_lock. 294 // This is okay because the unnamed module gets created before the ClassLoaderData 295 // is available to other threads. 296 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) { 297 ModuleEntry* entry = NEW_C_HEAP_OBJ(ModuleEntry, mtModule); 298 299 // Initialize everything BasicHashtable would 300 entry->set_next(NULL); 301 entry->set_hash(0); 302 entry->set_literal(NULL); 303 304 // Initialize fields specific to a ModuleEntry 305 entry->init(); 306 307 // Unnamed modules can read all other unnamed modules. 308 entry->set_can_read_all_unnamed(); 309 310 if (!module_handle.is_null()) { 311 entry->set_module(cld->add_handle(module_handle)); 312 } 313 314 entry->set_loader_data(cld); 315 entry->_is_open = true; 316 317 JFR_ONLY(INIT_ID(entry);) 318 319 return entry; 320 } 321 322 void ModuleEntry::delete_unnamed_module() { 323 // Do not need unlink_entry() since the unnamed module is not in the hashtable 324 FREE_C_HEAP_OBJ(this); 325 } 326 327 ModuleEntryTable::ModuleEntryTable(int table_size) 328 : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry)) 329 { 330 } 331 332 ModuleEntryTable::~ModuleEntryTable() { 333 // Walk through all buckets and all entries in each bucket, 334 // freeing each entry. 335 for (int i = 0; i < table_size(); ++i) { 336 for (ModuleEntry* m = bucket(i); m != NULL;) { 337 ModuleEntry* to_remove = m; 338 // read next before freeing. 339 m = m->next(); 340 341 ResourceMark rm; 342 if (to_remove->name() != NULL) { 343 log_info(module, unload)("unloading module %s", to_remove->name()->as_C_string()); 344 } 345 log_debug(module)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ? 346 to_remove->name()->as_C_string() : UNNAMED_MODULE); 347 348 // Clean out the C heap allocated reads list first before freeing the entry 349 to_remove->delete_reads(); 350 if (to_remove->name() != NULL) { 351 to_remove->name()->decrement_refcount(); 352 } 353 if (to_remove->version() != NULL) { 354 to_remove->version()->decrement_refcount(); 355 } 356 if (to_remove->location() != NULL) { 357 to_remove->location()->decrement_refcount(); 358 } 359 360 // Unlink from the Hashtable prior to freeing 361 unlink_entry(to_remove); 362 FREE_C_HEAP_ARRAY(char, to_remove); 363 } 364 } 365 assert(number_of_entries() == 0, "should have removed all entries"); 366 assert(new_entry_free_list() == NULL, "entry present on ModuleEntryTable's free list"); 367 } 368 369 #if INCLUDE_CDS_JAVA_HEAP 370 typedef ResourceHashtable< 371 const ModuleEntry*, 372 ModuleEntry*, 373 primitive_hash<const ModuleEntry*>, 374 primitive_equals<const ModuleEntry*>, 375 557, // prime number 376 ResourceObj::C_HEAP> ArchivedModuleEntries; 377 static ArchivedModuleEntries* _archive_modules_entries = NULL; 378 379 ModuleEntry* ModuleEntry::allocate_archived_entry() const { 380 assert(is_named(), "unnamed packages/modules are not archived"); 381 ModuleEntry* archived_entry = (ModuleEntry*)MetaspaceShared::read_write_space_alloc(sizeof(ModuleEntry)); 382 memcpy((void*)archived_entry, (void*)this, sizeof(ModuleEntry)); 383 384 if (_archive_modules_entries == NULL) { 385 _archive_modules_entries = new (ResourceObj::C_HEAP, mtClass)ArchivedModuleEntries(); 386 } 387 assert(_archive_modules_entries->get(this) == NULL, "Each ModuleEntry must not be shared across ModuleEntryTables"); 388 _archive_modules_entries->put(this, archived_entry); 389 390 return archived_entry; 391 } 392 393 ModuleEntry* ModuleEntry::get_archived_entry(ModuleEntry* orig_entry) { 394 ModuleEntry** ptr = _archive_modules_entries->get(orig_entry); 395 assert(ptr != NULL && *ptr != NULL, "must have been allocated"); 396 return *ptr; 397 } 398 399 // GrowableArrays cannot be directly archived, as they need to be expandable at runtime. 400 // Write it out as an Array, and conver it back to GrowableArray at runtime. 401 Array<ModuleEntry*>* ModuleEntry::write_archived_entry_array(GrowableArray<ModuleEntry*>* array) { 402 Array<ModuleEntry*>* archived_array = NULL; 403 int length = (array == NULL) ? 0 : array->length(); 404 if (length > 0) { 405 archived_array = MetaspaceShared::new_ro_array<ModuleEntry*>(length); 406 for (int i = 0; i < length; i++) { 407 ModuleEntry* archived_entry = get_archived_entry(array->at(i)); 408 archived_array->at_put(i, archived_entry); 409 ArchivePtrMarker::mark_pointer((address*)archived_array->adr_at(i)); 410 } 411 } 412 413 return archived_array; 414 } 415 416 GrowableArray<ModuleEntry*>* ModuleEntry::read_archived_entry_array(Array<ModuleEntry*>* archived_array) { 417 GrowableArray<ModuleEntry*>* array = NULL; 418 int length = (archived_array == NULL) ? 0 : archived_array->length(); 419 if (length > 0) { 420 array = new (ResourceObj::C_HEAP, mtModule)GrowableArray<ModuleEntry*>(length, mtModule); 421 for (int i = 0; i < length; i++) { 422 ModuleEntry* archived_entry = archived_array->at(i); 423 array->append(archived_entry); 424 } 425 } 426 427 return array; 428 } 429 430 void ModuleEntry::init_as_archived_entry() { 431 Array<ModuleEntry*>* archived_reads = write_archived_entry_array(_reads); 432 433 set_next(NULL); 434 set_hash(0x0); // re-init at runtime 435 _loader_data = NULL; // re-init at runtime 436 _shared_path_index = FileMapInfo::get_module_shared_path_index(_location); 437 if (literal() != NULL) { 438 set_literal(MetaspaceShared::get_relocated_symbol(literal())); 439 ArchivePtrMarker::mark_pointer((address*)literal_addr()); 440 } 441 _reads = (GrowableArray<ModuleEntry*>*)archived_reads; 442 if (_version != NULL) { 443 _version = MetaspaceShared::get_relocated_symbol(_version); 444 } 445 if (_location != NULL) { 446 _location = MetaspaceShared::get_relocated_symbol(_location); 447 } 448 449 ArchivePtrMarker::mark_pointer((address*)&_reads); 450 ArchivePtrMarker::mark_pointer((address*)&_version); 451 ArchivePtrMarker::mark_pointer((address*)&_location); 452 } 453 454 void ModuleEntry::init_archived_oops() { 455 assert(DumpSharedSpaces, "static dump only"); 456 oop module_obj = module(); 457 if (module_obj != NULL) { 458 oop m = HeapShared::find_archived_heap_object(module_obj); 459 assert(m != NULL, "sanity"); 460 _archived_module_narrow_oop = CompressedOops::encode(m); 461 } 462 assert(shared_protection_domain() == NULL, "never set during -Xshare:dump"); 463 // Clear handles and restore at run time. Handles cannot be archived. 464 OopHandle null_handle; 465 _module = null_handle; 466 } 467 468 void ModuleEntry::load_from_archive(ClassLoaderData* loader_data) { 469 set_loader_data(loader_data); 470 _reads = read_archived_entry_array((Array<ModuleEntry*>*)_reads); 471 JFR_ONLY(INIT_ID(this);) 472 } 473 474 void ModuleEntry::restore_archive_oops(ClassLoaderData* loader_data) { 475 Handle module_handle(Thread::current(), HeapShared::materialize_archived_object(_archived_module_narrow_oop)); 476 assert(module_handle.not_null(), "huh"); 477 set_module(loader_data->add_handle(module_handle)); 478 479 // This was cleared to zero during dump time -- we didn't save the value 480 // because it may be affected by archive relocation. 481 java_lang_Module::set_module_entry(module_handle(), this); 482 483 if (loader_data->class_loader() != NULL) { 484 java_lang_Module::set_loader(module_handle(), loader_data->class_loader()); 485 } 486 } 487 488 static int compare_module_by_name(ModuleEntry** a, ModuleEntry** b) { 489 return a[0]->name()->fast_compare(b[0]->name()); 490 } 491 492 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() { 493 Array<ModuleEntry*>* archived_modules = MetaspaceShared::new_rw_array<ModuleEntry*>(number_of_entries()); 494 int n = 0; 495 for (int i = 0; i < table_size(); ++i) { 496 for (ModuleEntry* m = bucket(i); m != NULL; m = m->next()) { 497 archived_modules->at_put(n++, m); 498 } 499 } 500 if (n > 0) { 501 // Always allocate in the same order to produce deterministic archive. 502 qsort(archived_modules->adr_at(0), n, sizeof(ModuleEntry*), (_sort_Fn)compare_module_by_name); 503 for (int i = 0; i < n; i++) { 504 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry()); 505 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i)); 506 } 507 } 508 return archived_modules; 509 } 510 511 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) { 512 assert(DumpSharedSpaces, "dump time only"); 513 for (int i = 0; i < archived_modules->length(); i++) { 514 ModuleEntry* archived_entry = archived_modules->at(i); 515 archived_entry->init_as_archived_entry(); 516 } 517 } 518 519 void ModuleEntryTable::init_archived_oops(Array<ModuleEntry*>* archived_modules) { 520 assert(DumpSharedSpaces, "dump time only"); 521 for (int i = 0; i < archived_modules->length(); i++) { 522 ModuleEntry* archived_entry = archived_modules->at(i); 523 archived_entry->init_archived_oops(); 524 } 525 } 526 527 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data, 528 Array<ModuleEntry*>* archived_modules) { 529 assert(UseSharedSpaces, "runtime only"); 530 531 MutexLocker m1(Module_lock); 532 for (int i = 0; i < archived_modules->length(); i++) { 533 ModuleEntry* archived_entry = archived_modules->at(i); 534 archived_entry->load_from_archive(loader_data); 535 536 unsigned int hash = compute_hash(archived_entry->name()); 537 archived_entry->set_hash(hash); 538 add_entry(hash_to_index(hash), archived_entry); 539 } 540 } 541 542 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) { 543 assert(UseSharedSpaces, "runtime only"); 544 for (int i = 0; i < archived_modules->length(); i++) { 545 ModuleEntry* archived_entry = archived_modules->at(i); 546 archived_entry->restore_archive_oops(loader_data); 547 } 548 } 549 #endif // INCLUDE_CDS_JAVA_HEAP 550 551 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle, 552 bool is_open, Symbol* name, 553 Symbol* version, Symbol* location, 554 ClassLoaderData* loader_data) { 555 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 556 ModuleEntry* entry = (ModuleEntry*)Hashtable<Symbol*, mtModule>::allocate_new_entry(hash, name); 557 558 // Initialize fields specific to a ModuleEntry 559 entry->init(); 560 if (name != NULL) { 561 name->increment_refcount(); 562 } else { 563 // Unnamed modules can read all other unnamed modules. 564 entry->set_can_read_all_unnamed(); 565 } 566 567 if (!module_handle.is_null()) { 568 entry->set_module(loader_data->add_handle(module_handle)); 569 } 570 571 entry->set_loader_data(loader_data); 572 entry->set_version(version); 573 entry->set_location(location); 574 entry->set_is_open(is_open); 575 576 if (ClassLoader::is_in_patch_mod_entries(name)) { 577 entry->set_is_patched(); 578 if (log_is_enabled(Trace, module, patch)) { 579 ResourceMark rm; 580 log_trace(module, patch)("Marked module %s as patched from --patch-module", 581 name != NULL ? name->as_C_string() : UNNAMED_MODULE); 582 } 583 } 584 585 JFR_ONLY(INIT_ID(entry);) 586 587 return entry; 588 } 589 590 void ModuleEntryTable::add_entry(int index, ModuleEntry* new_entry) { 591 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 592 Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry); 593 } 594 595 // Create an entry in the class loader's module_entry_table. It is the 596 // caller's responsibility to ensure that the entry has not already been 597 // created. 598 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle, 599 bool is_open, 600 Symbol* module_name, 601 Symbol* module_version, 602 Symbol* module_location, 603 ClassLoaderData* loader_data) { 604 assert(module_name != NULL, "ModuleEntryTable locked_create_entry should never be called for unnamed module."); 605 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 606 assert(lookup_only(module_name) == NULL, "Module already exists"); 607 ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, is_open, module_name, 608 module_version, module_location, loader_data); 609 add_entry(index_for(module_name), entry); 610 return entry; 611 } 612 613 // lookup_only by Symbol* to find a ModuleEntry. 614 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) { 615 assert(name != NULL, "name cannot be NULL"); 616 int index = index_for(name); 617 for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) { 618 if (m->name()->fast_compare(name) == 0) { 619 return m; 620 } 621 } 622 return NULL; 623 } 624 625 // Remove dead modules from all other alive modules' reads list. 626 // This should only occur at class unloading. 627 void ModuleEntryTable::purge_all_module_reads() { 628 assert_locked_or_safepoint(Module_lock); 629 for (int i = 0; i < table_size(); i++) { 630 for (ModuleEntry* entry = bucket(i); 631 entry != NULL; 632 entry = entry->next()) { 633 entry->purge_reads(); 634 } 635 } 636 } 637 638 void ModuleEntryTable::finalize_javabase(Handle module_handle, Symbol* version, Symbol* location) { 639 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 640 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data(); 641 ModuleEntryTable* module_table = boot_loader_data->modules(); 642 643 assert(module_table != NULL, "boot loader's ModuleEntryTable not defined"); 644 645 if (module_handle.is_null()) { 646 fatal("Unable to finalize module definition for " JAVA_BASE_NAME); 647 } 648 649 // Set java.lang.Module, version and location for java.base 650 ModuleEntry* jb_module = javabase_moduleEntry(); 651 assert(jb_module != NULL, JAVA_BASE_NAME " ModuleEntry not defined"); 652 jb_module->set_version(version); 653 jb_module->set_location(location); 654 // Once java.base's ModuleEntry _module field is set with the known 655 // java.lang.Module, java.base is considered "defined" to the VM. 656 jb_module->set_module(boot_loader_data->add_handle(module_handle)); 657 658 // Store pointer to the ModuleEntry for java.base in the java.lang.Module object. 659 java_lang_Module::set_module_entry(module_handle(), jb_module); 660 } 661 662 // Within java.lang.Class instances there is a java.lang.Module field that must 663 // be set with the defining module. During startup, prior to java.base's definition, 664 // classes needing their module field set are added to the fixup_module_list. 665 // Their module field is set once java.base's java.lang.Module is known to the VM. 666 void ModuleEntryTable::patch_javabase_entries(Handle module_handle) { 667 if (module_handle.is_null()) { 668 fatal("Unable to patch the module field of classes loaded prior to " 669 JAVA_BASE_NAME "'s definition, invalid java.lang.Module"); 670 } 671 672 // Do the fixups for the basic primitive types 673 java_lang_Class::set_module(Universe::int_mirror(), module_handle()); 674 java_lang_Class::set_module(Universe::float_mirror(), module_handle()); 675 java_lang_Class::set_module(Universe::double_mirror(), module_handle()); 676 java_lang_Class::set_module(Universe::byte_mirror(), module_handle()); 677 java_lang_Class::set_module(Universe::bool_mirror(), module_handle()); 678 java_lang_Class::set_module(Universe::char_mirror(), module_handle()); 679 java_lang_Class::set_module(Universe::long_mirror(), module_handle()); 680 java_lang_Class::set_module(Universe::short_mirror(), module_handle()); 681 java_lang_Class::set_module(Universe::void_mirror(), module_handle()); 682 683 // Do the fixups for classes that have already been created. 684 GrowableArray <Klass*>* list = java_lang_Class::fixup_module_field_list(); 685 int list_length = list->length(); 686 for (int i = 0; i < list_length; i++) { 687 Klass* k = list->at(i); 688 assert(k->is_klass(), "List should only hold classes"); 689 java_lang_Class::fixup_module_field(k, module_handle); 690 k->class_loader_data()->dec_keep_alive(); 691 } 692 693 delete java_lang_Class::fixup_module_field_list(); 694 java_lang_Class::set_fixup_module_field_list(NULL); 695 } 696 697 void ModuleEntryTable::print(outputStream* st) { 698 st->print_cr("Module Entry Table (table_size=%d, entries=%d)", 699 table_size(), number_of_entries()); 700 for (int i = 0; i < table_size(); i++) { 701 for (ModuleEntry* probe = bucket(i); 702 probe != NULL; 703 probe = probe->next()) { 704 probe->print(st); 705 } 706 } 707 } 708 709 void ModuleEntry::print(outputStream* st) { 710 ResourceMark rm; 711 st->print_cr("entry " PTR_FORMAT " name %s module " PTR_FORMAT " loader %s version %s location %s strict %s next " PTR_FORMAT, 712 p2i(this), 713 name() == NULL ? UNNAMED_MODULE : name()->as_C_string(), 714 p2i(module()), 715 loader_data()->loader_name_and_id(), 716 version() != NULL ? version()->as_C_string() : "NULL", 717 location() != NULL ? location()->as_C_string() : "NULL", 718 BOOL_TO_STR(!can_read_all_unnamed()), p2i(next())); 719 } 720 721 void ModuleEntryTable::verify() { 722 verify_table<ModuleEntry>("Module Entry Table"); 723 } 724 725 void ModuleEntry::verify() { 726 guarantee(loader_data() != NULL, "A module entry must be associated with a loader."); 727 }