1 /* 2 * Copyright (c) 2016, 2026, 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 "cds/aotGrowableArray.inline.hpp" 26 #include "cds/aotMetaspace.hpp" 27 #include "cds/archiveBuilder.hpp" 28 #include "cds/archiveUtils.hpp" 29 #include "cds/cdsConfig.hpp" 30 #include "classfile/classLoaderData.hpp" 31 #include "classfile/moduleEntry.hpp" 32 #include "classfile/modules.hpp" 33 #include "classfile/packageEntry.hpp" 34 #include "classfile/vmSymbols.hpp" 35 #include "logging/log.hpp" 36 #include "logging/logStream.hpp" 37 #include "memory/metadataFactory.hpp" 38 #include "memory/resourceArea.hpp" 39 #include "oops/array.hpp" 40 #include "oops/symbol.hpp" 41 #include "runtime/handles.inline.hpp" 42 #include "runtime/java.hpp" 43 #include "utilities/events.hpp" 44 #include "utilities/hashTable.hpp" 45 #include "utilities/ostream.hpp" 46 #include "utilities/quickSort.hpp" 47 48 PackageEntry::PackageEntry(Symbol* name, ModuleEntry* module) : 49 _name(name), 50 _module(module), 51 _export_flags(0), 52 _classpath_index(-1), 53 _must_walk_exports(false), 54 _qualified_exports(nullptr), 55 _defined_by_cds_in_class_path(0) 56 { 57 // name can't be null -- a class in the default package gets a PackageEntry of nullptr. 58 _name->increment_refcount(); 59 60 JFR_ONLY(INIT_ID(this);) 61 } 62 63 PackageEntry::~PackageEntry() { 64 delete_qualified_exports(); 65 _name->decrement_refcount(); 66 } 67 68 // Returns true if this package specifies m as a qualified export, including through an unnamed export 69 bool PackageEntry::is_qexported_to(ModuleEntry* m) const { 70 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 71 assert(m != nullptr, "No module to lookup in this package's qualified exports list"); 72 if (is_exported_allUnnamed() && !m->is_named()) { 73 return true; 74 } else if (!has_qual_exports_list()) { 75 return false; 76 } else { 77 return _qualified_exports->contains(m); 78 } 79 } 80 81 // Add a module to the package's qualified export list. 82 void PackageEntry::add_qexport(ModuleEntry* m) { 83 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 84 if (!has_qual_exports_list()) { 85 // Lazily create a package's qualified exports list. 86 // Initial size is small, do not anticipate export lists to be large. 87 _qualified_exports = new (mtModule) AOTGrowableArray<ModuleEntry*>(QUAL_EXP_SIZE, mtModule); 88 } 89 90 // Determine, based on this newly established export to module m, 91 // if this package's export list should be walked at a GC safepoint. 92 set_export_walk_required(m->loader_data()); 93 94 // Establish exportability to module m 95 _qualified_exports->append_if_missing(m); 96 } 97 98 // If the module's loader, that an export is being established to, is 99 // not the same loader as this module's and is not one of the 3 builtin 100 // class loaders, then this package's export list must be walked at GC 101 // safepoint. Modules have the same life cycle as their defining class 102 // loaders and should be removed if dead. 103 void PackageEntry::set_export_walk_required(ClassLoaderData* m_loader_data) { 104 assert_locked_or_safepoint(Module_lock); 105 ModuleEntry* this_pkg_mod = module(); 106 if (!_must_walk_exports && 107 (this_pkg_mod == nullptr || this_pkg_mod->loader_data() != m_loader_data) && 108 !m_loader_data->is_builtin_class_loader_data()) { 109 _must_walk_exports = true; 110 if (log_is_enabled(Trace, module)) { 111 ResourceMark rm; 112 assert(name() != nullptr, "PackageEntry without a valid name"); 113 log_trace(module)("PackageEntry::set_export_walk_required(): package %s defined in module %s, exports list must be walked", 114 name()->as_C_string(), 115 (this_pkg_mod == nullptr || this_pkg_mod->name() == nullptr) ? 116 UNNAMED_MODULE : this_pkg_mod->name()->as_C_string()); 117 } 118 } 119 } 120 121 // Set the package's exported states based on the value of the ModuleEntry. 122 void PackageEntry::set_exported(ModuleEntry* m) { 123 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 124 if (is_unqual_exported()) { 125 // An exception could be thrown, but choose to simply ignore. 126 // Illegal to convert an unqualified exported package to be qualifiedly exported 127 return; 128 } 129 130 if (m == nullptr) { 131 // null indicates the package is being unqualifiedly exported. Clean up 132 // the qualified list at the next safepoint. 133 set_unqual_exported(); 134 } else { 135 // Add the exported module 136 add_qexport(m); 137 } 138 } 139 140 // Set the package as exported to all unnamed modules unless the package is 141 // already unqualifiedly exported. 142 void PackageEntry::set_is_exported_allUnnamed() { 143 assert(!module()->is_open(), "should have been checked already"); 144 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 145 if (!is_unqual_exported()) { 146 _export_flags = PKG_EXP_ALLUNNAMED; 147 } 148 } 149 150 // Remove dead module entries within the package's exported list. Note that 151 // if all of the modules on the _qualified_exports get purged the list does not 152 // get deleted. This prevents the package from illegally transitioning from 153 // exported to non-exported. 154 void PackageEntry::purge_qualified_exports() { 155 assert_locked_or_safepoint(Module_lock); 156 if (_must_walk_exports && 157 _qualified_exports != nullptr && 158 !_qualified_exports->is_empty()) { 159 160 // This package's _must_walk_exports flag will be reset based 161 // on the remaining live modules on the exports list. 162 _must_walk_exports = false; 163 164 if (log_is_enabled(Trace, module)) { 165 ResourceMark rm; 166 assert(name() != nullptr, "PackageEntry without a valid name"); 167 ModuleEntry* pkg_mod = module(); 168 log_trace(module)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked", 169 name()->as_C_string(), 170 (pkg_mod == nullptr || pkg_mod->name() == nullptr) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string()); 171 } 172 173 // Go backwards because this removes entries that are dead. 174 int len = _qualified_exports->length(); 175 for (int idx = len - 1; idx >= 0; idx--) { 176 ModuleEntry* module_idx = _qualified_exports->at(idx); 177 ClassLoaderData* cld_idx = module_idx->loader_data(); 178 if (cld_idx->is_unloading()) { 179 _qualified_exports->delete_at(idx); 180 } else { 181 // Update the need to walk this package's exports based on live modules 182 set_export_walk_required(cld_idx); 183 } 184 } 185 } 186 } 187 188 void PackageEntry::delete_qualified_exports() { 189 if (_qualified_exports != nullptr && !AOTMetaspace::in_aot_cache(_qualified_exports)) { 190 delete _qualified_exports; 191 } 192 _qualified_exports = nullptr; 193 } 194 195 void PackageEntry::pack_qualified_exports() { 196 if (_qualified_exports != nullptr) { 197 _qualified_exports->shrink_to_fit(); 198 } 199 } 200 201 void PackageEntry::metaspace_pointers_do(MetaspaceClosure* it) { 202 it->push(&_name); 203 it->push(&_module); 204 205 ModuleEntry* module_entry = module(); 206 if (!(module_entry->is_named() && Modules::is_dynamic_proxy_module(module_entry))) { 207 // This is a dynamically generated module. Its _qualified_exports will be 208 // restored at runtime in the Java code. See comments in ArchivedData::restore(). 209 it->push(&_qualified_exports); 210 } 211 } 212 213 PackageEntryTable::PackageEntryTable() { } 214 215 PackageEntryTable::~PackageEntryTable() { 216 class PackageEntryTableDeleter : public StackObj { 217 public: 218 bool do_entry(const SymbolHandle& name, PackageEntry*& entry) { 219 if (log_is_enabled(Info, module, unload) || log_is_enabled(Debug, module)) { 220 ResourceMark rm; 221 const char* str = name->as_C_string(); 222 log_info(module, unload)("unloading package %s", str); 223 log_debug(module)("PackageEntry: deleting package: %s", str); 224 } 225 delete entry; 226 return true; 227 } 228 }; 229 230 PackageEntryTableDeleter deleter; 231 _table.unlink(&deleter); 232 assert(_table.number_of_entries() == 0, "should have removed all entries"); 233 } 234 235 #if INCLUDE_CDS_JAVA_HEAP 236 bool PackageEntry::should_be_archived() const { 237 return module()->should_be_archived(); 238 } 239 240 void PackageEntry::remove_unshareable_info() { 241 ModuleEntry* module_entry = ArchiveBuilder::current()->get_source_addr(module()); 242 if (module_entry->is_named() && Modules::is_dynamic_proxy_module(module_entry)) { 243 // See comments in PackageEntry::metaspace_pointers_do() 244 _qualified_exports = nullptr; 245 } 246 if (_qualified_exports != nullptr) { 247 _qualified_exports->set_in_aot_cache(); 248 } 249 _defined_by_cds_in_class_path = 0; 250 JFR_ONLY(set_trace_id(0);) // re-init at runtime 251 } 252 253 void PackageEntry::load_from_archive() { 254 JFR_ONLY(INIT_ID(this);) 255 } 256 257 static int compare_package_by_name(PackageEntry* a, PackageEntry* b) { 258 assert(a == b || a->name() != b->name(), "no duplicated names"); 259 return a->name()->fast_compare(b->name()); 260 } 261 262 Array<PackageEntry*>* PackageEntryTable::build_aot_table(ClassLoaderData* loader_data, TRAPS) { 263 // First count the packages in named modules 264 int n = 0; 265 auto count = [&] (const SymbolHandle& key, PackageEntry*& p) { 266 if (p->should_be_archived()) { 267 n++; 268 } 269 }; 270 _table.iterate_all(count); 271 272 Array<PackageEntry*>* archived_packages = MetadataFactory::new_array<PackageEntry*>(loader_data, n, nullptr, CHECK_NULL); 273 // reset n 274 n = 0; 275 auto grab = [&] (const SymbolHandle& key, PackageEntry*& p) { 276 if (p->should_be_archived()) { 277 p->pack_qualified_exports(); 278 archived_packages->at_put(n++, p); 279 280 LogStreamHandle(Info, aot, package) st; 281 if (st.is_enabled()) { 282 st.print("archived "); 283 p->print(&st); 284 } 285 } 286 }; 287 _table.iterate_all(grab); 288 289 if (n > 1) { 290 // Always allocate in the same order to produce deterministic archive. 291 QuickSort::sort(archived_packages->data(), n, compare_package_by_name); 292 } 293 294 return archived_packages; 295 } 296 297 void PackageEntryTable::load_archived_entries(Array<PackageEntry*>* archived_packages) { 298 assert(CDSConfig::is_using_archive(), "runtime only"); 299 300 for (int i = 0; i < archived_packages->length(); i++) { 301 PackageEntry* archived_entry = archived_packages->at(i); 302 archived_entry->load_from_archive(); 303 _table.put(archived_entry->name(), archived_entry); 304 } 305 } 306 307 #endif // INCLUDE_CDS_JAVA_HEAP 308 309 // Create package entry in loader's package entry table. Assume Module lock 310 // was taken by caller. 311 void PackageEntryTable::locked_create_entry(Symbol* name, ModuleEntry* module) { 312 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 313 assert(locked_lookup_only(name) == nullptr, "Package entry already exists"); 314 PackageEntry* entry = new PackageEntry(name, module); 315 bool created = _table.put(name, entry); 316 assert(created, "must be"); 317 } 318 319 // Create package entry in loader's package entry table if it does not already 320 // exist. Assume Module lock was taken by caller. 321 PackageEntry* PackageEntryTable::locked_create_entry_if_absent(Symbol* name, ModuleEntry* module) { 322 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 323 // Check if package entry already exists. If not, create it. 324 bool created; 325 PackageEntry* entry = new PackageEntry(name, module); 326 PackageEntry** old_entry = _table.put_if_absent(name, entry, &created); 327 if (created) { 328 return entry; 329 } else { 330 delete entry; 331 return *old_entry; 332 } 333 } 334 335 PackageEntry* PackageEntryTable::create_entry_if_absent(Symbol* name, ModuleEntry* module) { 336 MutexLocker ml(Module_lock); 337 return locked_create_entry_if_absent(name, module); 338 } 339 340 PackageEntry* PackageEntryTable::lookup_only(Symbol* name) { 341 assert(!Module_lock->owned_by_self(), "should not have the Module_lock - use locked_lookup_only"); 342 MutexLocker ml(Module_lock); 343 return locked_lookup_only(name); 344 } 345 346 PackageEntry* PackageEntryTable::locked_lookup_only(Symbol* name) { 347 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 348 PackageEntry** entry = _table.get(name); 349 return entry == nullptr ? nullptr : *entry; 350 } 351 352 // Called when a define module for java.base is being processed. 353 // Verify the packages loaded thus far are in java.base's package list. 354 void PackageEntryTable::verify_javabase_packages(GrowableArray<Symbol*> *pkg_list) { 355 assert_lock_strong(Module_lock); 356 auto verifier = [&] (const SymbolHandle& name, PackageEntry*& entry) { 357 ModuleEntry* m = entry->module(); 358 Symbol* module_name = (m == nullptr ? nullptr : m->name()); 359 if (module_name != nullptr && 360 (module_name->fast_compare(vmSymbols::java_base()) == 0) && 361 !pkg_list->contains(entry->name())) { 362 ResourceMark rm; 363 vm_exit_during_initialization("A non-" JAVA_BASE_NAME " package was loaded prior to module system initialization", 364 entry->name()->as_C_string()); 365 } 366 }; 367 _table.iterate_all(verifier); 368 } 369 370 // iteration of qualified exports 371 void PackageEntry::package_exports_do(ModuleClosure* f) { 372 assert_locked_or_safepoint(Module_lock); 373 assert(f != nullptr, "invariant"); 374 375 if (has_qual_exports_list()) { 376 int qe_len = _qualified_exports->length(); 377 378 for (int i = 0; i < qe_len; ++i) { 379 f->do_module(_qualified_exports->at(i)); 380 } 381 } 382 } 383 384 bool PackageEntry::exported_pending_delete() const { 385 assert_locked_or_safepoint(Module_lock); 386 return (is_unqual_exported() && _qualified_exports != nullptr); 387 } 388 389 // Remove dead entries from all packages' exported list 390 void PackageEntryTable::purge_all_package_exports() { 391 assert_locked_or_safepoint(Module_lock); 392 auto purge = [&] (const SymbolHandle& name, PackageEntry*& entry) { 393 if (entry->exported_pending_delete()) { 394 // exported list is pending deletion due to a transition 395 // from qualified to unqualified 396 entry->delete_qualified_exports(); 397 } else if (entry->is_qual_exported()) { 398 entry->purge_qualified_exports(); 399 } 400 }; 401 _table.iterate_all(purge); 402 } 403 404 void PackageEntryTable::packages_do(void f(PackageEntry*)) { 405 auto doit = [&] (const SymbolHandle&name, PackageEntry*& entry) { 406 f(entry); 407 }; 408 assert_locked_or_safepoint(Module_lock); 409 _table.iterate_all(doit); 410 } 411 412 413 GrowableArray<PackageEntry*>* PackageEntryTable::get_system_packages() { 414 GrowableArray<PackageEntry*>* loaded_class_pkgs = new GrowableArray<PackageEntry*>(50); 415 auto grab = [&] (const SymbolHandle& name, PackageEntry*& entry) { 416 if (entry->has_loaded_class()) { 417 loaded_class_pkgs->append(entry); 418 } 419 }; 420 421 MutexLocker ml(Module_lock); 422 _table.iterate_all(grab); 423 // Returns a resource allocated object so caller must have ResourceMark 424 return loaded_class_pkgs; 425 } 426 427 void PackageEntryTable::print(outputStream* st) { 428 auto printer = [&] (const SymbolHandle& name, PackageEntry*& entry) { 429 entry->print(st); 430 }; 431 st->print_cr("Package Entry Table (table_size=%d, entries=%d)", 432 _table.table_size(), _table.number_of_entries()); 433 _table.iterate_all(printer); 434 } 435 436 // This function may be called from debuggers so access private fields directly 437 // to prevent triggering locking-related asserts that could result from calling 438 // getter methods. 439 void PackageEntry::print(outputStream* st) { 440 ResourceMark rm; 441 st->print_cr("package entry " PTR_FORMAT " name %s module %s classpath_index " 442 INT32_FORMAT " is_exported_unqualified %d is_exported_allUnnamed %d ", 443 p2i(this), name()->as_C_string(), 444 (module()->is_named() ? module()->name()->as_C_string() : UNNAMED_MODULE), 445 _classpath_index, _export_flags == PKG_EXP_UNQUALIFIED, 446 _export_flags == PKG_EXP_ALLUNNAMED); 447 } --- EOF ---