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