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 "classfile/moduleEntry.hpp" 27 #include "classfile/packageEntry.hpp" 28 #include "logging/log.hpp" 29 #include "memory/archiveUtils.hpp" 30 #include "memory/metaspaceShared.hpp" 31 #include "memory/resourceArea.hpp" 32 #include "oops/array.hpp" 33 #include "oops/symbol.hpp" 34 #include "runtime/handles.inline.hpp" 35 #include "utilities/events.hpp" 36 #include "utilities/growableArray.hpp" 37 #include "utilities/hashtable.inline.hpp" 38 #include "utilities/ostream.hpp" 39 #include "utilities/quickSort.hpp" 40 #include "utilities/resourceHash.hpp" 41 42 // Returns true if this package specifies m as a qualified export, including through an unnamed export 43 bool PackageEntry::is_qexported_to(ModuleEntry* m) const { 44 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 45 assert(m != NULL, "No module to lookup in this package's qualified exports list"); 46 if (is_exported_allUnnamed() && !m->is_named()) { 47 return true; 48 } else if (!has_qual_exports_list()) { 49 return false; 50 } else { 51 return _qualified_exports->contains(m); 52 } 53 } 54 55 // Add a module to the package's qualified export list. 56 void PackageEntry::add_qexport(ModuleEntry* m) { 57 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 58 if (!has_qual_exports_list()) { 59 // Lazily create a package's qualified exports list. 60 // Initial size is small, do not anticipate export lists to be large. 61 _qualified_exports = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(QUAL_EXP_SIZE, mtModule); 62 } 63 64 // Determine, based on this newly established export to module m, 65 // if this package's export list should be walked at a GC safepoint. 66 set_export_walk_required(m->loader_data()); 67 68 // Establish exportability to module m 69 _qualified_exports->append_if_missing(m); 70 } 71 72 // If the module's loader, that an export is being established to, is 73 // not the same loader as this module's and is not one of the 3 builtin 74 // class loaders, then this package's export list must be walked at GC 75 // safepoint. Modules have the same life cycle as their defining class 76 // loaders and should be removed if dead. 77 void PackageEntry::set_export_walk_required(ClassLoaderData* m_loader_data) { 78 assert_locked_or_safepoint(Module_lock); 79 ModuleEntry* this_pkg_mod = module(); 80 if (!_must_walk_exports && 81 (this_pkg_mod == NULL || this_pkg_mod->loader_data() != m_loader_data) && 82 !m_loader_data->is_builtin_class_loader_data()) { 83 _must_walk_exports = true; 84 if (log_is_enabled(Trace, module)) { 85 ResourceMark rm; 86 assert(name() != NULL, "PackageEntry without a valid name"); 87 log_trace(module)("PackageEntry::set_export_walk_required(): package %s defined in module %s, exports list must be walked", 88 name()->as_C_string(), 89 (this_pkg_mod == NULL || this_pkg_mod->name() == NULL) ? 90 UNNAMED_MODULE : this_pkg_mod->name()->as_C_string()); 91 } 92 } 93 } 94 95 // Set the package's exported states based on the value of the ModuleEntry. 96 void PackageEntry::set_exported(ModuleEntry* m) { 97 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 98 if (is_unqual_exported()) { 99 // An exception could be thrown, but choose to simply ignore. 100 // Illegal to convert an unqualified exported package to be qualifiedly exported 101 return; 102 } 103 104 if (m == NULL) { 105 // NULL indicates the package is being unqualifiedly exported. Clean up 106 // the qualified list at the next safepoint. 107 set_unqual_exported(); 108 } else { 109 // Add the exported module 110 add_qexport(m); 111 } 112 } 113 114 // Set the package as exported to all unnamed modules unless the package is 115 // already unqualifiedly exported. 116 void PackageEntry::set_is_exported_allUnnamed() { 117 assert(!module()->is_open(), "should have been checked already"); 118 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 119 if (!is_unqual_exported()) { 120 _export_flags = PKG_EXP_ALLUNNAMED; 121 } 122 } 123 124 // Remove dead module entries within the package's exported list. Note that 125 // if all of the modules on the _qualified_exports get purged the list does not 126 // get deleted. This prevents the package from illegally transitioning from 127 // exported to non-exported. 128 void PackageEntry::purge_qualified_exports() { 129 assert_locked_or_safepoint(Module_lock); 130 if (_must_walk_exports && 131 _qualified_exports != NULL && 132 !_qualified_exports->is_empty()) { 133 134 // This package's _must_walk_exports flag will be reset based 135 // on the remaining live modules on the exports list. 136 _must_walk_exports = false; 137 138 if (log_is_enabled(Trace, module)) { 139 ResourceMark rm; 140 assert(name() != NULL, "PackageEntry without a valid name"); 141 ModuleEntry* pkg_mod = module(); 142 log_trace(module)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked", 143 name()->as_C_string(), 144 (pkg_mod == NULL || pkg_mod->name() == NULL) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string()); 145 } 146 147 // Go backwards because this removes entries that are dead. 148 int len = _qualified_exports->length(); 149 for (int idx = len - 1; idx >= 0; idx--) { 150 ModuleEntry* module_idx = _qualified_exports->at(idx); 151 ClassLoaderData* cld_idx = module_idx->loader_data(); 152 if (cld_idx->is_unloading()) { 153 _qualified_exports->delete_at(idx); 154 } else { 155 // Update the need to walk this package's exports based on live modules 156 set_export_walk_required(cld_idx); 157 } 158 } 159 } 160 } 161 162 void PackageEntry::delete_qualified_exports() { 163 if (_qualified_exports != NULL) { 164 delete _qualified_exports; 165 } 166 _qualified_exports = NULL; 167 } 168 169 PackageEntryTable::PackageEntryTable(int table_size) 170 : Hashtable<Symbol*, mtModule>(table_size, sizeof(PackageEntry)) 171 { 172 } 173 174 PackageEntryTable::~PackageEntryTable() { 175 // Walk through all buckets and all entries in each bucket, 176 // freeing each entry. 177 for (int i = 0; i < table_size(); ++i) { 178 for (PackageEntry* p = bucket(i); p != NULL;) { 179 PackageEntry* to_remove = p; 180 // read next before freeing. 181 p = p->next(); 182 183 // Clean out the C heap allocated qualified exports list first before freeing the entry 184 to_remove->delete_qualified_exports(); 185 to_remove->name()->decrement_refcount(); 186 187 // Unlink from the Hashtable prior to freeing 188 unlink_entry(to_remove); 189 FREE_C_HEAP_ARRAY(char, to_remove); 190 } 191 } 192 assert(number_of_entries() == 0, "should have removed all entries"); 193 assert(new_entry_free_list() == NULL, "entry present on PackageEntryTable's free list"); 194 } 195 196 #if INCLUDE_CDS_JAVA_HEAP 197 typedef ResourceHashtable< 198 const PackageEntry*, 199 PackageEntry*, 200 primitive_hash<const PackageEntry*>, 201 primitive_equals<const PackageEntry*>, 202 557, // prime number 203 ResourceObj::C_HEAP> ArchivedPackageEntries; 204 static ArchivedPackageEntries* _archived_packages_entries = NULL; 205 206 PackageEntry* PackageEntry::allocate_archived_entry() const { 207 assert(!in_unnamed_module(), "unnamed packages/modules are not archived"); 208 PackageEntry* archived_entry = (PackageEntry*)MetaspaceShared::read_write_space_alloc(sizeof(PackageEntry)); 209 memcpy((void*)archived_entry, (void*)this, sizeof(PackageEntry)); 210 211 if (_archived_packages_entries == NULL) { 212 _archived_packages_entries = new (ResourceObj::C_HEAP, mtClass)ArchivedPackageEntries(); 213 } 214 assert(_archived_packages_entries->get(this) == NULL, "Each PackageEntry must not be shared across PackageEntryTables"); 215 _archived_packages_entries->put(this, archived_entry); 216 217 return archived_entry; 218 } 219 220 PackageEntry* PackageEntry::get_archived_entry(PackageEntry* orig_entry) { 221 PackageEntry** ptr = _archived_packages_entries->get(orig_entry); 222 assert(ptr != NULL && *ptr != NULL, "must have been allocated"); 223 return *ptr; 224 } 225 226 void PackageEntry::init_as_archived_entry() { 227 Array<ModuleEntry*>* archived_qualified_exports = ModuleEntry::write_archived_entry_array(_qualified_exports); 228 229 set_next(NULL); 230 set_literal(MetaspaceShared::get_relocated_symbol(literal())); 231 set_hash(0x0); // re-init at runtime 232 _module = ModuleEntry::get_archived_entry(_module); 233 _qualified_exports = (GrowableArray<ModuleEntry*>*)archived_qualified_exports; 234 _defined_by_cds_in_class_path = 0; 235 236 ArchivePtrMarker::mark_pointer((address*)literal_addr()); 237 ArchivePtrMarker::mark_pointer((address*)&_module); 238 ArchivePtrMarker::mark_pointer((address*)&_qualified_exports); 239 } 240 241 void PackageEntry::load_from_archive() { 242 _qualified_exports = ModuleEntry::read_archived_entry_array((Array<ModuleEntry*>*)_qualified_exports); 243 JFR_ONLY(INIT_ID(this);) 244 } 245 246 static int compare_package_by_name(PackageEntry* a, PackageEntry* b) { 247 return a->name()->fast_compare(b->name()); 248 } 249 250 Array<PackageEntry*>* PackageEntryTable::allocate_archived_entries() { 251 // First count the packages in named modules 252 int n, i; 253 for (n = 0, i = 0; i < table_size(); ++i) { 254 for (PackageEntry* p = bucket(i); p != NULL; p = p->next()) { 255 if (p->module()->name() != NULL) { 256 n++; 257 } 258 } 259 } 260 261 Array<PackageEntry*>* archived_packages = MetaspaceShared::new_rw_array<PackageEntry*>(n); 262 for (n = 0, i = 0; i < table_size(); ++i) { 263 for (PackageEntry* p = bucket(i); p != NULL; p = p->next()) { 264 if (p->module()->name() != NULL) { 265 // We don't archive unnamed modules, or packages in unnamed modules. They will be 266 // created on-demand at runtime as classes in such packages are loaded. 267 archived_packages->at_put(n++, p); 268 } 269 } 270 } 271 if (n > 1) { 272 QuickSort::sort(archived_packages->data(), n, (_sort_Fn)compare_package_by_name, true); 273 } 274 for (i = 0; i < n; i++) { 275 archived_packages->at_put(i, archived_packages->at(i)->allocate_archived_entry()); 276 ArchivePtrMarker::mark_pointer((address*)archived_packages->adr_at(i)); 277 } 278 return archived_packages; 279 } 280 281 void PackageEntryTable::init_archived_entries(Array<PackageEntry*>* archived_packages) { 282 for (int i = 0; i < archived_packages->length(); i++) { 283 PackageEntry* archived_entry = archived_packages->at(i); 284 archived_entry->init_as_archived_entry(); 285 } 286 } 287 288 void PackageEntryTable::load_archived_entries(Array<PackageEntry*>* archived_packages) { 289 assert(UseSharedSpaces, "runtime only"); 290 291 for (int i = 0; i < archived_packages->length(); i++) { 292 PackageEntry* archived_entry = archived_packages->at(i); 293 archived_entry->load_from_archive(); 294 295 unsigned int hash = compute_hash(archived_entry->name()); 296 archived_entry->set_hash(hash); 297 add_entry(hash_to_index(hash), archived_entry); 298 } 299 } 300 301 #endif // INCLUDE_CDS_JAVA_HEAP 302 303 PackageEntry* PackageEntryTable::new_entry(unsigned int hash, Symbol* name, ModuleEntry* module) { 304 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 305 PackageEntry* entry = (PackageEntry*)Hashtable<Symbol*, mtModule>::allocate_new_entry(hash, name); 306 307 JFR_ONLY(INIT_ID(entry);) 308 309 // Initialize fields specific to a PackageEntry 310 entry->init(); 311 entry->name()->increment_refcount(); 312 entry->set_module(module); 313 return entry; 314 } 315 316 void PackageEntryTable::add_entry(int index, PackageEntry* new_entry) { 317 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 318 Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry); 319 } 320 321 // Create package entry in loader's package entry table. Assume Module lock 322 // was taken by caller. 323 void PackageEntryTable::locked_create_entry(Symbol* name, ModuleEntry* module) { 324 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 325 assert(locked_lookup_only(name) == NULL, "Package entry already exists"); 326 PackageEntry* entry = new_entry(compute_hash(name), name, module); 327 add_entry(index_for(name), entry); 328 } 329 330 // Create package entry in loader's package entry table if it does not already 331 // exist. Assume Module lock was taken by caller. 332 void PackageEntryTable::locked_create_entry_if_not_exist(Symbol* name, ModuleEntry* module) { 333 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 334 // Check if package entry already exists. If not, create it. 335 if (locked_lookup_only(name) == NULL) { 336 locked_create_entry(name, module); 337 } 338 } 339 340 PackageEntry* PackageEntryTable::lookup(Symbol* name, ModuleEntry* module) { 341 MutexLocker ml(Module_lock); 342 PackageEntry* p = locked_lookup_only(name); 343 if (p != NULL) { 344 return p; 345 } else { 346 assert(module != NULL, "module should never be null"); 347 PackageEntry* entry = new_entry(compute_hash(name), name, module); 348 add_entry(index_for(name), entry); 349 return entry; 350 } 351 } 352 353 PackageEntry* PackageEntryTable::lookup_only(Symbol* name) { 354 assert(!Module_lock->owned_by_self(), "should not have the Module_lock - use locked_lookup_only"); 355 MutexLocker ml(Module_lock); 356 return locked_lookup_only(name); 357 } 358 359 PackageEntry* PackageEntryTable::locked_lookup_only(Symbol* name) { 360 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 361 int index = index_for(name); 362 for (PackageEntry* p = bucket(index); p != NULL; p = p->next()) { 363 if (p->name()->fast_compare(name) == 0) { 364 return p; 365 } 366 } 367 return NULL; 368 } 369 370 // Called when a define module for java.base is being processed. 371 // Verify the packages loaded thus far are in java.base's package list. 372 void PackageEntryTable::verify_javabase_packages(GrowableArray<Symbol*> *pkg_list) { 373 assert_lock_strong(Module_lock); 374 for (int i = 0; i < table_size(); i++) { 375 for (PackageEntry* entry = bucket(i); 376 entry != NULL; 377 entry = entry->next()) { 378 ModuleEntry* m = entry->module(); 379 Symbol* module_name = (m == NULL ? NULL : m->name()); 380 if (module_name != NULL && 381 (module_name->fast_compare(vmSymbols::java_base()) == 0) && 382 !pkg_list->contains(entry->name())) { 383 ResourceMark rm; 384 vm_exit_during_initialization("A non-" JAVA_BASE_NAME " package was loaded prior to module system initialization", entry->name()->as_C_string()); 385 } 386 } 387 } 388 } 389 390 // iteration of qualified exports 391 void PackageEntry::package_exports_do(ModuleClosure* f) { 392 assert_locked_or_safepoint(Module_lock); 393 assert(f != NULL, "invariant"); 394 395 if (has_qual_exports_list()) { 396 int qe_len = _qualified_exports->length(); 397 398 for (int i = 0; i < qe_len; ++i) { 399 f->do_module(_qualified_exports->at(i)); 400 } 401 } 402 } 403 404 bool PackageEntry::exported_pending_delete() const { 405 assert_locked_or_safepoint(Module_lock); 406 return (is_unqual_exported() && _qualified_exports != NULL); 407 } 408 409 // Remove dead entries from all packages' exported list 410 void PackageEntryTable::purge_all_package_exports() { 411 assert_locked_or_safepoint(Module_lock); 412 for (int i = 0; i < table_size(); i++) { 413 for (PackageEntry* entry = bucket(i); 414 entry != NULL; 415 entry = entry->next()) { 416 if (entry->exported_pending_delete()) { 417 // exported list is pending deletion due to a transition 418 // from qualified to unqualified 419 entry->delete_qualified_exports(); 420 } else if (entry->is_qual_exported()) { 421 entry->purge_qualified_exports(); 422 } 423 } 424 } 425 } 426 427 void PackageEntryTable::print(outputStream* st) { 428 st->print_cr("Package Entry Table (table_size=%d, entries=%d)", 429 table_size(), number_of_entries()); 430 for (int i = 0; i < table_size(); i++) { 431 for (PackageEntry* probe = bucket(i); 432 probe != NULL; 433 probe = probe->next()) { 434 probe->print(st); 435 } 436 } 437 } 438 439 // This function may be called from debuggers so access private fields directly 440 // to prevent triggering locking-related asserts that could result from calling 441 // getter methods. 442 void PackageEntry::print(outputStream* st) { 443 ResourceMark rm; 444 st->print_cr("package entry " PTR_FORMAT " name %s module %s classpath_index " 445 INT32_FORMAT " is_exported_unqualified %d is_exported_allUnnamed %d " "next " PTR_FORMAT, 446 p2i(this), name()->as_C_string(), 447 (module()->is_named() ? module()->name()->as_C_string() : UNNAMED_MODULE), 448 _classpath_index, _export_flags == PKG_EXP_UNQUALIFIED, 449 _export_flags == PKG_EXP_ALLUNNAMED, p2i(next())); 450 } 451 452 void PackageEntryTable::verify() { 453 verify_table<PackageEntry>("Package Entry Table"); 454 } 455 456 void PackageEntry::verify() { 457 guarantee(name() != NULL, "A package entry must have a corresponding symbol name."); 458 }