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/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 } 63 } 64 65 // Return true if the module's version should be displayed in error messages, 66 // logging, etc. 67 // Return false if the module's version is null, if it is unnamed, or if the 68 // module is not an upgradeable module. 69 // Detect if the module is not upgradeable by checking: 70 // 1. Module location is "jrt:/java." and its loader is boot or platform 71 // 2. Module location is "jrt:/jdk.", its loader is one of the builtin loaders 72 // and its version is the same as module java.base's version 73 // The above check is imprecise but should work in almost all cases. 74 bool ModuleEntry::should_show_version() { 75 if (version() == NULL || !is_named()) return false; 76 77 if (location() != NULL) { 78 ResourceMark rm; 79 const char* loc = location()->as_C_string(); 80 ClassLoaderData* cld = loader_data(); 81 82 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder"); 83 if ((cld->is_the_null_class_loader_data() || cld->is_platform_class_loader_data()) && 84 (strncmp(loc, "jrt:/java.", 10) == 0)) { 85 return false; 86 } 87 if ((ModuleEntryTable::javabase_moduleEntry()->version()->fast_compare(version()) == 0) && 88 cld->is_permanent_class_loader_data() && (strncmp(loc, "jrt:/jdk.", 9) == 0)) { 89 return false; 90 } 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 140 // injecting dependencies that require the default read edges for resolution. 141 if (this->has_default_read_edges() && !m->is_named()) { 142 ClassLoaderData* cld = m->loader_data(); 143 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder"); 144 if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) { 145 return true; // default read edge 146 } 147 } 148 if (!has_reads_list()) { 149 return false; 150 } else { 151 return _reads->contains(m); 152 } 153 } 154 155 // Add a new module to this module's reads list 156 void ModuleEntry::add_read(ModuleEntry* m) { 157 // Unnamed module is special cased and can read all modules 158 if (!is_named()) { 159 return; 160 } 161 162 MutexLocker m1(Module_lock); 163 if (m == NULL) { 164 set_can_read_all_unnamed(); 165 } else { 166 if (_reads == NULL) { 167 // Lazily create a module's reads list 168 _reads = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, mtModule); 169 } 170 171 // Determine, based on this newly established read edge to module m, 172 // if this module's read list should be walked at a GC safepoint. 173 set_read_walk_required(m->loader_data()); 174 175 // Establish readability to module m 176 _reads->append_if_missing(m); 177 } 178 } 179 180 // If the module's loader, that a read edge is being established to, is 181 // not the same loader as this module's and is not one of the 3 builtin 182 // class loaders, then this module's reads list must be walked at GC 183 // safepoint. Modules have the same life cycle as their defining class 184 // loaders and should be removed if dead. 185 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) { 186 assert(is_named(), "Cannot call set_read_walk_required on unnamed module"); 187 assert_locked_or_safepoint(Module_lock); 188 if (!_must_walk_reads && 189 loader_data() != m_loader_data && 190 !m_loader_data->is_builtin_class_loader_data()) { 191 _must_walk_reads = true; 192 if (log_is_enabled(Trace, module)) { 193 ResourceMark rm; 194 log_trace(module)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked", 195 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE); 196 } 197 } 198 } 199 200 // Set whether the module is open, i.e. all its packages are unqualifiedly exported 201 void ModuleEntry::set_is_open(bool is_open) { 202 assert_lock_strong(Module_lock); 203 _is_open = is_open; 204 } 205 206 // Returns true if the module has a non-empty reads list. As such, the unnamed 207 // module will return false. 208 bool ModuleEntry::has_reads_list() const { 209 assert_locked_or_safepoint(Module_lock); 210 return ((_reads != NULL) && !_reads->is_empty()); 211 } 212 213 // Purge dead module entries out of reads list. 214 void ModuleEntry::purge_reads() { 215 assert_locked_or_safepoint(Module_lock); 216 217 if (_must_walk_reads && has_reads_list()) { 218 // This module's _must_walk_reads flag will be reset based 219 // on the remaining live modules on the reads list. 220 _must_walk_reads = false; 221 222 if (log_is_enabled(Trace, module)) { 223 ResourceMark rm; 224 log_trace(module)("ModuleEntry::purge_reads(): module %s reads list being walked", 225 (name() != NULL) ? name()->as_C_string() : UNNAMED_MODULE); 226 } 227 228 // Go backwards because this removes entries that are dead. 229 int len = _reads->length(); 230 for (int idx = len - 1; idx >= 0; idx--) { 231 ModuleEntry* module_idx = _reads->at(idx); 232 ClassLoaderData* cld_idx = module_idx->loader_data(); 233 if (cld_idx->is_unloading()) { 234 _reads->delete_at(idx); 235 } else { 236 // Update the need to walk this module's reads based on live modules 237 set_read_walk_required(cld_idx); 238 } 239 } 240 } 241 } 242 243 void ModuleEntry::module_reads_do(ModuleClosure* f) { 244 assert_locked_or_safepoint(Module_lock); 245 assert(f != NULL, "invariant"); 246 247 if (has_reads_list()) { 248 int reads_len = _reads->length(); 249 for (int i = 0; i < reads_len; ++i) { 250 f->do_module(_reads->at(i)); 251 } 252 } 253 } 254 255 void ModuleEntry::delete_reads() { 256 delete _reads; 257 _reads = NULL; 258 } 259 260 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) { 261 // The java.lang.Module for this loader's 262 // corresponding unnamed module can be found in the java.lang.ClassLoader object. 263 oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader()); 264 265 // Ensure that the unnamed module was correctly set when the class loader was constructed. 266 // Guarantee will cause a recognizable crash if the user code has circumvented calling the ClassLoader constructor. 267 ResourceMark rm; 268 guarantee(java_lang_Module::is_instance(module), 269 "The unnamed module for ClassLoader %s, is null or not an instance of java.lang.Module. The class loader has not been initialized correctly.", 270 cld->loader_name_and_id()); 271 272 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld); 273 274 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object. 275 java_lang_Module::set_module_entry(module, unnamed_module); 276 277 return unnamed_module; 278 } 279 280 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) { 281 // For the boot loader, the java.lang.Module for the unnamed module 282 // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At 283 // this point initially create the ModuleEntry for the unnamed module. 284 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld); 285 assert(unnamed_module != NULL, "boot loader unnamed module should not be null"); 286 return unnamed_module; 287 } 288 289 // When creating an unnamed module, this is called without holding the Module_lock. 290 // This is okay because the unnamed module gets created before the ClassLoaderData 291 // is available to other threads. 292 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) { 293 ModuleEntry* entry = NEW_C_HEAP_OBJ(ModuleEntry, mtModule); 294 295 // Initialize everything BasicHashtable would 296 entry->set_next(NULL); 297 entry->set_hash(0); 298 entry->set_literal(NULL); 299 300 // Initialize fields specific to a ModuleEntry 301 entry->init(); 302 303 // Unnamed modules can read all other unnamed modules. 304 entry->set_can_read_all_unnamed(); 305 306 if (!module_handle.is_null()) { 307 entry->set_module(cld->add_handle(module_handle)); 308 } 309 310 entry->set_loader_data(cld); 311 entry->_is_open = true; 312 313 JFR_ONLY(INIT_ID(entry);) 314 315 return entry; 316 } 317 318 void ModuleEntry::delete_unnamed_module() { 319 // Do not need unlink_entry() since the unnamed module is not in the hashtable 320 FREE_C_HEAP_OBJ(this); 321 } 322 323 ModuleEntryTable::ModuleEntryTable(int table_size) 324 : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry)) 325 { 326 } 327 328 ModuleEntryTable::~ModuleEntryTable() { 329 // Walk through all buckets and all entries in each bucket, 330 // freeing each entry. 331 for (int i = 0; i < table_size(); ++i) { 332 for (ModuleEntry* m = bucket(i); m != NULL;) { 333 ModuleEntry* to_remove = m; 334 // read next before freeing. 335 m = m->next(); 336 337 ResourceMark rm; 338 if (to_remove->name() != NULL) { 339 log_info(module, unload)("unloading module %s", to_remove->name()->as_C_string()); 340 } 341 log_debug(module)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ? 342 to_remove->name()->as_C_string() : UNNAMED_MODULE); 343 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 } 384 385 entry->set_loader_data(loader_data); 386 entry->set_version(version); 387 entry->set_location(location); 388 entry->set_is_open(is_open); 389 390 if (ClassLoader::is_in_patch_mod_entries(name)) { 391 entry->set_is_patched(); 392 if (log_is_enabled(Trace, module, patch)) { 393 ResourceMark rm; 394 log_trace(module, patch)("Marked module %s as patched from --patch-module", 395 name != NULL ? name->as_C_string() : UNNAMED_MODULE); 396 } 397 } 398 399 JFR_ONLY(INIT_ID(entry);) 400 401 return entry; 402 } 403 404 void ModuleEntryTable::add_entry(int index, ModuleEntry* new_entry) { 405 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 406 Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry); 407 } 408 409 // Create an entry in the class loader's module_entry_table. It is the 410 // caller's responsibility to ensure that the entry has not already been 411 // created. 412 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle, 413 bool is_open, 414 Symbol* module_name, 415 Symbol* module_version, 416 Symbol* module_location, 417 ClassLoaderData* loader_data) { 418 assert(module_name != NULL, "ModuleEntryTable locked_create_entry should never be called for unnamed module."); 419 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 420 assert(lookup_only(module_name) == NULL, "Module already exists"); 421 ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, is_open, module_name, 422 module_version, module_location, loader_data); 423 add_entry(index_for(module_name), entry); 424 return entry; 425 } 426 427 // lookup_only by Symbol* to find a ModuleEntry. 428 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) { 429 assert(name != NULL, "name cannot be NULL"); 430 int index = index_for(name); 431 for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) { 432 if (m->name()->fast_compare(name) == 0) { 433 return m; 434 } 435 } 436 return NULL; 437 } 438 439 // Remove dead modules from all other alive modules' reads list. 440 // This should only occur at class unloading. 441 void ModuleEntryTable::purge_all_module_reads() { 442 assert_locked_or_safepoint(Module_lock); 443 for (int i = 0; i < table_size(); i++) { 444 for (ModuleEntry* entry = bucket(i); 445 entry != NULL; 446 entry = entry->next()) { 447 entry->purge_reads(); 448 } 449 } 450 } 451 452 void ModuleEntryTable::finalize_javabase(Handle module_handle, Symbol* version, Symbol* location) { 453 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 454 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data(); 455 ModuleEntryTable* module_table = boot_loader_data->modules(); 456 457 assert(module_table != NULL, "boot loader's ModuleEntryTable not defined"); 458 459 if (module_handle.is_null()) { 460 fatal("Unable to finalize module definition for " JAVA_BASE_NAME); 461 } 462 463 // Set java.lang.Module, version and location for java.base 464 ModuleEntry* jb_module = javabase_moduleEntry(); 465 assert(jb_module != NULL, JAVA_BASE_NAME " ModuleEntry not defined"); 466 jb_module->set_version(version); 467 jb_module->set_location(location); 468 // Once java.base's ModuleEntry _module field is set with the known 469 // java.lang.Module, java.base is considered "defined" to the VM. 470 jb_module->set_module(boot_loader_data->add_handle(module_handle)); 471 472 // Store pointer to the ModuleEntry for java.base in the java.lang.Module object. 473 java_lang_Module::set_module_entry(module_handle(), jb_module); 474 } 475 476 // Within java.lang.Class instances there is a java.lang.Module field that must 477 // be set with the defining module. During startup, prior to java.base's definition, 478 // classes needing their module field set are added to the fixup_module_list. 479 // Their module field is set once java.base's java.lang.Module is known to the VM. 480 void ModuleEntryTable::patch_javabase_entries(Handle module_handle) { 481 if (module_handle.is_null()) { 482 fatal("Unable to patch the module field of classes loaded prior to " 483 JAVA_BASE_NAME "'s definition, invalid java.lang.Module"); 484 } 485 486 // Do the fixups for the basic primitive types 487 java_lang_Class::set_module(Universe::int_mirror(), module_handle()); 488 java_lang_Class::set_module(Universe::float_mirror(), module_handle()); 489 java_lang_Class::set_module(Universe::double_mirror(), module_handle()); 490 java_lang_Class::set_module(Universe::byte_mirror(), module_handle()); 491 java_lang_Class::set_module(Universe::bool_mirror(), module_handle()); 492 java_lang_Class::set_module(Universe::char_mirror(), module_handle()); 493 java_lang_Class::set_module(Universe::long_mirror(), module_handle()); 494 java_lang_Class::set_module(Universe::short_mirror(), module_handle()); 495 java_lang_Class::set_module(Universe::void_mirror(), module_handle()); 496 497 // Do the fixups for classes that have already been created. 498 GrowableArray <Klass*>* list = java_lang_Class::fixup_module_field_list(); 499 int list_length = list->length(); 500 for (int i = 0; i < list_length; i++) { 501 Klass* k = list->at(i); 502 assert(k->is_klass(), "List should only hold classes"); 503 java_lang_Class::fixup_module_field(k, module_handle); 504 k->class_loader_data()->dec_keep_alive(); 505 } 506 507 delete java_lang_Class::fixup_module_field_list(); 508 java_lang_Class::set_fixup_module_field_list(NULL); 509 } 510 511 void ModuleEntryTable::print(outputStream* st) { 512 st->print_cr("Module Entry Table (table_size=%d, entries=%d)", 513 table_size(), number_of_entries()); 514 for (int i = 0; i < table_size(); i++) { 515 for (ModuleEntry* probe = bucket(i); 516 probe != NULL; 517 probe = probe->next()) { 518 probe->print(st); 519 } 520 } 521 } 522 523 void ModuleEntry::print(outputStream* st) { 524 ResourceMark rm; 525 st->print_cr("entry " PTR_FORMAT " name %s module " PTR_FORMAT " loader %s version %s location %s strict %s next " PTR_FORMAT, 526 p2i(this), 527 name() == NULL ? UNNAMED_MODULE : name()->as_C_string(), 528 p2i(module()), 529 loader_data()->loader_name_and_id(), 530 version() != NULL ? version()->as_C_string() : "NULL", 531 location() != NULL ? location()->as_C_string() : "NULL", 532 BOOL_TO_STR(!can_read_all_unnamed()), p2i(next())); 533 } 534 535 void ModuleEntryTable::verify() { 536 verify_table<ModuleEntry>("Module Entry Table"); 537 } 538 539 void ModuleEntry::verify() { 540 guarantee(loader_data() != NULL, "A module entry must be associated with a loader."); 541 }