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/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 } 68 } 69 70 // Return true if the module's version should be displayed in error messages, 71 // logging, etc. 72 // Return false if the module's version is null, if it is unnamed, or if the 73 // module is not an upgradeable module. 74 // Detect if the module is not upgradeable by checking: 75 // 1. Module location is "jrt:/java." and its loader is boot or platform 76 // 2. Module location is "jrt:/jdk.", its loader is one of the builtin loaders 77 // and its version is the same as module java.base's version 78 // The above check is imprecise but should work in almost all cases. 79 bool ModuleEntry::should_show_version() { 80 if (version() == NULL || !is_named()) return false; 81 82 if (location() != NULL) { 83 ResourceMark rm; 84 const char* loc = location()->as_C_string(); 85 ClassLoaderData* cld = loader_data(); 86 87 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder"); 88 if ((cld->is_the_null_class_loader_data() || cld->is_platform_class_loader_data()) && 89 (strncmp(loc, "jrt:/java.", 10) == 0)) { 90 return false; 91 } 92 if ((ModuleEntryTable::javabase_moduleEntry()->version()->fast_compare(version()) == 0) && 93 cld->is_permanent_class_loader_data() && (strncmp(loc, "jrt:/jdk.", 9) == 0)) { 94 return false; 95 } 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 145 // injecting dependencies that require the default read edges for resolution. 146 if (this->has_default_read_edges() && !m->is_named()) { 147 ClassLoaderData* cld = m->loader_data(); 148 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder"); 149 if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) { 150 return true; // default read edge 151 } 152 } 153 if (!has_reads_list()) { 154 return false; 155 } else { 156 return _reads->contains(m); 157 } 158 } 159 160 // Add a new module to this module's reads list 161 void ModuleEntry::add_read(ModuleEntry* m) { 162 // Unnamed module is special cased and can read all modules 163 if (!is_named()) { 164 return; 165 } 166 167 MutexLocker m1(Module_lock); 168 if (m == NULL) { 169 set_can_read_all_unnamed(); 170 } else { 171 if (_reads == NULL) { 172 // Lazily create a module's reads list 173 _reads = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, mtModule); 174 } 175 176 // Determine, based on this newly established read edge to module m, 177 // if this module's read list should be walked at a GC safepoint. 178 set_read_walk_required(m->loader_data()); 179 180 // Establish readability to module m 181 _reads->append_if_missing(m); 182 } 183 } 184 185 // If the module's loader, that a read edge is being established to, is 186 // not the same loader as this module's and is not one of the 3 builtin 187 // class loaders, then this module's reads list must be walked at GC 188 // safepoint. Modules have the same life cycle as their defining class 189 // loaders and should be removed if dead. 190 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) { 191 assert(is_named(), "Cannot call set_read_walk_required on unnamed module"); 192 assert_locked_or_safepoint(Module_lock); 193 if (!_must_walk_reads && 194 loader_data() != m_loader_data && 195 !m_loader_data->is_builtin_class_loader_data()) { 196 _must_walk_reads = true; 197 if (log_is_enabled(Trace, module)) { 198 ResourceMark rm; 199 log_trace(module)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked", 200 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE); 201 } 202 } 203 } 204 205 // Set whether the module is open, i.e. all its packages are unqualifiedly exported 206 void ModuleEntry::set_is_open(bool is_open) { 207 assert_lock_strong(Module_lock); 208 _is_open = is_open; 209 } 210 211 // Returns true if the module has a non-empty reads list. As such, the unnamed 212 // module will return false. 213 bool ModuleEntry::has_reads_list() const { 214 assert_locked_or_safepoint(Module_lock); 215 return ((_reads != NULL) && !_reads->is_empty()); 216 } 217 218 // Purge dead module entries out of reads list. 219 void ModuleEntry::purge_reads() { 220 assert_locked_or_safepoint(Module_lock); 221 222 if (_must_walk_reads && has_reads_list()) { 223 // This module's _must_walk_reads flag will be reset based 224 // on the remaining live modules on the reads list. 225 _must_walk_reads = false; 226 227 if (log_is_enabled(Trace, module)) { 228 ResourceMark rm; 229 log_trace(module)("ModuleEntry::purge_reads(): module %s reads list being walked", 230 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE); 231 } 232 233 // Go backwards because this removes entries that are dead. 234 int len = _reads->length(); 235 for (int idx = len - 1; idx >= 0; idx--) { 236 ModuleEntry* module_idx = _reads->at(idx); 237 ClassLoaderData* cld_idx = module_idx->loader_data(); 238 if (cld_idx->is_unloading()) { 239 _reads->delete_at(idx); 240 } else { 241 // Update the need to walk this module's reads based on live modules 242 set_read_walk_required(cld_idx); 243 } 244 } 245 } 246 } 247 248 void ModuleEntry::module_reads_do(ModuleClosure* f) { 249 assert_locked_or_safepoint(Module_lock); 250 assert(f != NULL, "invariant"); 251 252 if (has_reads_list()) { 253 int reads_len = _reads->length(); 254 for (int i = 0; i < reads_len; ++i) { 255 f->do_module(_reads->at(i)); 256 } 257 } 258 } 259 260 void ModuleEntry::delete_reads() { 261 delete _reads; 262 _reads = NULL; 263 } 264 265 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) { 266 // The java.lang.Module for this loader's 267 // corresponding unnamed module can be found in the java.lang.ClassLoader object. 268 oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader()); 269 270 // Ensure that the unnamed module was correctly set when the class loader was constructed. 271 // Guarantee will cause a recognizable crash if the user code has circumvented calling the ClassLoader constructor. 272 ResourceMark rm; 273 guarantee(java_lang_Module::is_instance(module), 274 "The unnamed module for ClassLoader %s, is null or not an instance of java.lang.Module. The class loader has not been initialized correctly.", 275 cld->loader_name_and_id()); 276 277 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld); 278 279 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object. 280 java_lang_Module::set_module_entry(module, unnamed_module); 281 282 return unnamed_module; 283 } 284 285 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) { 286 // For the boot loader, the java.lang.Module for the unnamed module 287 // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At 288 // this point initially create the ModuleEntry for the unnamed module. 289 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld); 290 assert(unnamed_module != NULL, "boot loader unnamed module should not be null"); 291 return unnamed_module; 292 } 293 294 // When creating an unnamed module, this is called without holding the Module_lock. 295 // This is okay because the unnamed module gets created before the ClassLoaderData 296 // is available to other threads. 297 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) { 298 ModuleEntry* entry = NEW_C_HEAP_OBJ(ModuleEntry, mtModule); 299 300 // Initialize everything BasicHashtable would 301 entry->set_next(NULL); 302 entry->set_hash(0); 303 entry->set_literal(NULL); 304 305 // Initialize fields specific to a ModuleEntry 306 entry->init(); 307 308 // Unnamed modules can read all other unnamed modules. 309 entry->set_can_read_all_unnamed(); 310 311 if (!module_handle.is_null()) { 312 entry->set_module(cld->add_handle(module_handle)); 313 } 314 315 entry->set_loader_data(cld); 316 entry->_is_open = true; 317 318 JFR_ONLY(INIT_ID(entry);) 319 320 return entry; 321 } 322 323 void ModuleEntry::delete_unnamed_module() { 324 // Do not need unlink_entry() since the unnamed module is not in the hashtable 325 FREE_C_HEAP_OBJ(this); 326 } 327 328 ModuleEntryTable::ModuleEntryTable(int table_size) 329 : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry)) 330 { 331 } 332 333 ModuleEntryTable::~ModuleEntryTable() { 334 // Walk through all buckets and all entries in each bucket, 335 // freeing each entry. 336 for (int i = 0; i < table_size(); ++i) { 337 for (ModuleEntry* m = bucket(i); m != NULL;) { 338 ModuleEntry* to_remove = m; 339 // read next before freeing. 340 m = m->next(); 341 342 ResourceMark rm; 343 if (to_remove->name() != NULL) { 344 log_info(module, unload)("unloading module %s", to_remove->name()->as_C_string()); 345 } 346 log_debug(module)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ? 347 to_remove->name()->as_C_string() : UNNAMED_MODULE); 348 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 return a->name()->fast_compare(b->name()); 491 } 492 493 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() { 494 Array<ModuleEntry*>* archived_modules = MetaspaceShared::new_rw_array<ModuleEntry*>(number_of_entries()); 495 int n = 0; 496 for (int i = 0; i < table_size(); ++i) { 497 for (ModuleEntry* m = bucket(i); m != NULL; m = m->next()) { 498 archived_modules->at_put(n++, m); 499 } 500 } 501 if (n > 1) { 502 // Always allocate in the same order to produce deterministic archive. 503 QuickSort::sort(archived_modules->data(), n, (_sort_Fn)compare_module_by_name, true); 504 } 505 for (int i = 0; i < n; i++) { 506 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry()); 507 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i)); 508 } 509 return archived_modules; 510 } 511 512 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) { 513 assert(DumpSharedSpaces, "dump time only"); 514 for (int i = 0; i < archived_modules->length(); i++) { 515 ModuleEntry* archived_entry = archived_modules->at(i); 516 archived_entry->init_as_archived_entry(); 517 } 518 } 519 520 void ModuleEntryTable::init_archived_oops(Array<ModuleEntry*>* archived_modules) { 521 assert(DumpSharedSpaces, "dump time only"); 522 for (int i = 0; i < archived_modules->length(); i++) { 523 ModuleEntry* archived_entry = archived_modules->at(i); 524 archived_entry->init_archived_oops(); 525 } 526 } 527 528 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data, 529 Array<ModuleEntry*>* archived_modules) { 530 assert(UseSharedSpaces, "runtime only"); 531 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 }