1 /*
   2  * Copyright (c) 2016, 2019, 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/resourceArea.hpp"
  30 #include "oops/symbol.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "utilities/events.hpp"
  33 #include "utilities/growableArray.hpp"
  34 #include "utilities/hashtable.inline.hpp"
  35 #include "utilities/ostream.hpp"
  36 
  37 // Returns true if this package specifies m as a qualified export, including through an unnamed export
  38 bool PackageEntry::is_qexported_to(ModuleEntry* m) const {
  39   assert(Module_lock->owned_by_self(), "should have the Module_lock");
  40   assert(m != NULL, "No module to lookup in this package's qualified exports list");
  41   if (is_exported_allUnnamed() && !m->is_named()) {
  42     return true;
  43   } else if (!has_qual_exports_list()) {
  44     return false;
  45   } else {
  46     return _qualified_exports->contains(m);
  47   }
  48 }
  49 
  50 // Add a module to the package's qualified export list.
  51 void PackageEntry::add_qexport(ModuleEntry* m) {
  52   assert(Module_lock->owned_by_self(), "should have the Module_lock");
  53   if (!has_qual_exports_list()) {
  54     // Lazily create a package's qualified exports list.
  55     // Initial size is small, do not anticipate export lists to be large.
  56     _qualified_exports = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(QUAL_EXP_SIZE, mtModule);
  57   }
  58 
  59   // Determine, based on this newly established export to module m,
  60   // if this package's export list should be walked at a GC safepoint.
  61   set_export_walk_required(m->loader_data());
  62 
  63   // Establish exportability to module m
  64   _qualified_exports->append_if_missing(m);
  65 }
  66 
  67 // If the module's loader, that an export is being established to, is
  68 // not the same loader as this module's and is not one of the 3 builtin
  69 // class loaders, then this package's export list must be walked at GC
  70 // safepoint. Modules have the same life cycle as their defining class
  71 // loaders and should be removed if dead.
  72 void PackageEntry::set_export_walk_required(ClassLoaderData* m_loader_data) {
  73   assert_locked_or_safepoint(Module_lock);
  74   ModuleEntry* this_pkg_mod = module();
  75   if (!_must_walk_exports &&
  76       (this_pkg_mod == NULL || this_pkg_mod->loader_data() != m_loader_data) &&
  77       !m_loader_data->is_builtin_class_loader_data()) {
  78     _must_walk_exports = true;
  79     if (log_is_enabled(Trace, module)) {
  80       ResourceMark rm;
  81       assert(name() != NULL, "PackageEntry without a valid name");
  82       log_trace(module)("PackageEntry::set_export_walk_required(): package %s defined in module %s, exports list must be walked",
  83                         name()->as_C_string(),
  84                         (this_pkg_mod == NULL || this_pkg_mod->name() == NULL) ?
  85                           UNNAMED_MODULE : this_pkg_mod->name()->as_C_string());
  86     }
  87   }
  88 }
  89 
  90 // Set the package's exported states based on the value of the ModuleEntry.
  91 void PackageEntry::set_exported(ModuleEntry* m) {
  92   assert(Module_lock->owned_by_self(), "should have the Module_lock");
  93   if (is_unqual_exported()) {
  94     // An exception could be thrown, but choose to simply ignore.
  95     // Illegal to convert an unqualified exported package to be qualifiedly exported
  96     return;
  97   }
  98 
  99   if (m == NULL) {
 100     // NULL indicates the package is being unqualifiedly exported.  Clean up
 101     // the qualified list at the next safepoint.
 102     set_unqual_exported();
 103   } else {
 104     // Add the exported module
 105     add_qexport(m);
 106   }
 107 }
 108 
 109 // Set the package as exported to all unnamed modules unless the package is
 110 // already unqualifiedly exported.
 111 void PackageEntry::set_is_exported_allUnnamed() {
 112   assert(!module()->is_open(), "should have been checked already");
 113   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 114   if (!is_unqual_exported()) {
 115    _export_flags = PKG_EXP_ALLUNNAMED;
 116   }
 117 }
 118 
 119 // Remove dead module entries within the package's exported list.  Note that
 120 // if all of the modules on the _qualified_exports get purged the list does not
 121 // get deleted.  This prevents the package from illegally transitioning from
 122 // exported to non-exported.
 123 void PackageEntry::purge_qualified_exports() {
 124   assert_locked_or_safepoint(Module_lock);
 125   if (_must_walk_exports &&
 126       _qualified_exports != NULL &&
 127       !_qualified_exports->is_empty()) {
 128 
 129     // This package's _must_walk_exports flag will be reset based
 130     // on the remaining live modules on the exports list.
 131     _must_walk_exports = false;
 132 
 133     if (log_is_enabled(Trace, module)) {
 134       ResourceMark rm;
 135       assert(name() != NULL, "PackageEntry without a valid name");
 136       ModuleEntry* pkg_mod = module();
 137       log_trace(module)("PackageEntry::purge_qualified_exports(): package %s defined in module %s, exports list being walked",
 138                         name()->as_C_string(),
 139                         (pkg_mod == NULL || pkg_mod->name() == NULL) ? UNNAMED_MODULE : pkg_mod->name()->as_C_string());
 140     }
 141 
 142     // Go backwards because this removes entries that are dead.
 143     int len = _qualified_exports->length();
 144     for (int idx = len - 1; idx >= 0; idx--) {
 145       ModuleEntry* module_idx = _qualified_exports->at(idx);
 146       ClassLoaderData* cld_idx = module_idx->loader_data();
 147       if (cld_idx->is_unloading()) {
 148         _qualified_exports->delete_at(idx);
 149       } else {
 150         // Update the need to walk this package's exports based on live modules
 151         set_export_walk_required(cld_idx);
 152       }
 153     }
 154   }
 155 }
 156 
 157 void PackageEntry::delete_qualified_exports() {
 158   if (_qualified_exports != NULL) {
 159     delete _qualified_exports;
 160   }
 161   _qualified_exports = NULL;
 162 }
 163 
 164 PackageEntryTable::PackageEntryTable(int table_size)
 165   : Hashtable<Symbol*, mtModule>(table_size, sizeof(PackageEntry))
 166 {
 167 }
 168 
 169 PackageEntryTable::~PackageEntryTable() {
 170   // Walk through all buckets and all entries in each bucket,
 171   // freeing each entry.
 172   for (int i = 0; i < table_size(); ++i) {
 173     for (PackageEntry* p = bucket(i); p != NULL;) {
 174       PackageEntry* to_remove = p;
 175       // read next before freeing.
 176       p = p->next();
 177 
 178       // Clean out the C heap allocated qualified exports list first before freeing the entry
 179       to_remove->delete_qualified_exports();
 180       to_remove->name()->decrement_refcount();
 181 
 182       // Unlink from the Hashtable prior to freeing
 183       unlink_entry(to_remove);
 184       FREE_C_HEAP_ARRAY(char, to_remove);
 185     }
 186   }
 187   assert(number_of_entries() == 0, "should have removed all entries");
 188   assert(new_entry_free_list() == NULL, "entry present on PackageEntryTable's free list");
 189 }
 190 
 191 PackageEntry* PackageEntryTable::new_entry(unsigned int hash, Symbol* name, ModuleEntry* module) {
 192   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 193   PackageEntry* entry = (PackageEntry*)Hashtable<Symbol*, mtModule>::allocate_new_entry(hash, name);
 194 
 195   JFR_ONLY(INIT_ID(entry);)
 196 
 197   // Initialize fields specific to a PackageEntry
 198   entry->init();
 199   entry->name()->increment_refcount();
 200   entry->set_module(module);
 201   return entry;
 202 }
 203 
 204 void PackageEntryTable::add_entry(int index, PackageEntry* new_entry) {
 205   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 206   Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry);
 207 }
 208 
 209 // Create package entry in loader's package entry table.  Assume Module lock
 210 // was taken by caller.
 211 void PackageEntryTable::locked_create_entry(Symbol* name, ModuleEntry* module) {
 212   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 213   assert(locked_lookup_only(name) == NULL, "Package entry already exists");
 214   PackageEntry* entry = new_entry(compute_hash(name), name, module);
 215   add_entry(index_for(name), entry);
 216 }
 217 
 218 // Create package entry in loader's package entry table if it does not already
 219 // exist.  Assume Module lock was taken by caller.
 220 void PackageEntryTable::locked_create_entry_if_not_exist(Symbol* name, ModuleEntry* module) {
 221   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 222   // Check if package entry already exists.  If not, create it.
 223   if (locked_lookup_only(name) == NULL) {
 224     locked_create_entry(name, module);
 225   }
 226 }
 227 
 228 PackageEntry* PackageEntryTable::lookup(Symbol* name, ModuleEntry* module) {
 229   MutexLocker ml(Module_lock);
 230   PackageEntry* p = locked_lookup_only(name);
 231   if (p != NULL) {
 232     return p;
 233   } else {
 234     assert(module != NULL, "module should never be null");
 235     PackageEntry* entry = new_entry(compute_hash(name), name, module);
 236     add_entry(index_for(name), entry);
 237     return entry;
 238   }
 239 }
 240 
 241 PackageEntry* PackageEntryTable::lookup_only(Symbol* name) {
 242   assert(!Module_lock->owned_by_self(), "should not have the Module_lock - use locked_lookup_only");
 243   MutexLocker ml(Module_lock);
 244   return locked_lookup_only(name);
 245 }
 246 
 247 PackageEntry* PackageEntryTable::locked_lookup_only(Symbol* name) {
 248   assert(Module_lock->owned_by_self(), "should have the Module_lock");
 249   int index = index_for(name);
 250   for (PackageEntry* p = bucket(index); p != NULL; p = p->next()) {
 251     if (p->name()->fast_compare(name) == 0) {
 252       return p;
 253     }
 254   }
 255   return NULL;
 256 }
 257 
 258 // Called when a define module for java.base is being processed.
 259 // Verify the packages loaded thus far are in java.base's package list.
 260 void PackageEntryTable::verify_javabase_packages(GrowableArray<Symbol*> *pkg_list) {
 261   assert_lock_strong(Module_lock);
 262   for (int i = 0; i < table_size(); i++) {
 263     for (PackageEntry* entry = bucket(i);
 264                        entry != NULL;
 265                        entry = entry->next()) {
 266       ModuleEntry* m = entry->module();
 267       Symbol* module_name = (m == NULL ? NULL : m->name());
 268       if (module_name != NULL &&
 269           (module_name->fast_compare(vmSymbols::java_base()) == 0) &&
 270           !pkg_list->contains(entry->name())) {
 271         ResourceMark rm;
 272         vm_exit_during_initialization("A non-" JAVA_BASE_NAME " package was loaded prior to module system initialization", entry->name()->as_C_string());
 273       }
 274     }
 275   }
 276 
 277 }
 278 
 279 // iteration of qualified exports
 280 void PackageEntry::package_exports_do(ModuleClosure* f) {
 281   assert_locked_or_safepoint(Module_lock);
 282   assert(f != NULL, "invariant");
 283 
 284   if (has_qual_exports_list()) {
 285     int qe_len = _qualified_exports->length();
 286 
 287     for (int i = 0; i < qe_len; ++i) {
 288       f->do_module(_qualified_exports->at(i));
 289     }
 290   }
 291 }
 292 
 293 bool PackageEntry::exported_pending_delete() const {
 294   assert_locked_or_safepoint(Module_lock);
 295   return (is_unqual_exported() && _qualified_exports != NULL);
 296 }
 297 
 298 // Remove dead entries from all packages' exported list
 299 void PackageEntryTable::purge_all_package_exports() {
 300   assert_locked_or_safepoint(Module_lock);
 301   for (int i = 0; i < table_size(); i++) {
 302     for (PackageEntry* entry = bucket(i);
 303                        entry != NULL;
 304                        entry = entry->next()) {
 305       if (entry->exported_pending_delete()) {
 306         // exported list is pending deletion due to a transition
 307         // from qualified to unqualified
 308         entry->delete_qualified_exports();
 309       } else if (entry->is_qual_exported()) {
 310         entry->purge_qualified_exports();
 311       }
 312     }
 313   }
 314 }
 315 
 316 void PackageEntryTable::print(outputStream* st) {
 317   st->print_cr("Package Entry Table (table_size=%d, entries=%d)",
 318                table_size(), number_of_entries());
 319   for (int i = 0; i < table_size(); i++) {
 320     for (PackageEntry* probe = bucket(i);
 321                        probe != NULL;
 322                        probe = probe->next()) {
 323       probe->print(st);
 324     }
 325   }
 326 }
 327 
 328 // This function may be called from debuggers so access private fields directly
 329 // to prevent triggering locking-related asserts that could result from calling
 330 // getter methods.
 331 void PackageEntry::print(outputStream* st) {
 332   ResourceMark rm;
 333   st->print_cr("package entry " PTR_FORMAT " name %s module %s classpath_index "
 334                INT32_FORMAT " is_exported_unqualified %d is_exported_allUnnamed %d " "next " PTR_FORMAT,
 335                p2i(this), name()->as_C_string(),
 336                (module()->is_named() ? module()->name()->as_C_string() : UNNAMED_MODULE),
 337                _classpath_index, _export_flags == PKG_EXP_UNQUALIFIED,
 338                _export_flags == PKG_EXP_ALLUNNAMED, p2i(next()));
 339 }
 340 
 341 void PackageEntryTable::verify() {
 342   verify_table<PackageEntry>("Package Entry Table");
 343 }
 344 
 345 void PackageEntry::verify() {
 346   guarantee(name() != NULL, "A package entry must have a corresponding symbol name.");
 347 }