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