1 /*
   2  * Copyright (c) 2016, 2017, 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 #ifndef SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP
  26 #define SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP
  27 
  28 #include "classfile/moduleEntry.hpp"
  29 #include "oops/symbol.hpp"
  30 #include "utilities/growableArray.hpp"
  31 #include "utilities/hashtable.hpp"
  32 #include "utilities/ostream.hpp"
  33 
  34 // A PackageEntry basically represents a Java package.  It contains:
  35 //   - Symbol* containing the package's name.
  36 //   - ModuleEntry* for this package's containing module.
  37 //   - a field indicating if the package is exported unqualifiedly or to all
  38 //     unnamed modules.
  39 //   - a growable array containing other module entries that this
  40 //     package is exported to.
  41 //
  42 // Packages can be exported in the following 3 ways:
  43 //   - not exported:        the package does not have qualified or unqualified exports.
  44 //   - qualified exports:   the package has been explicitly qualified to at least
  45 //                            one particular module or has been qualifiedly exported
  46 //                            to all unnamed modules.
  47 //                            Note: being exported to all unnamed is a form of a qualified
  48 //                            export. It is equivalent to the package being explicitly
  49 //                            exported to all current and future unnamed modules.
  50 //   - unqualified exports: the package is exported to all modules.
  51 //
  52 // A package can transition from:
  53 //   - being not exported, to being exported either in a qualified or unqualified manner
  54 //   - being qualifiedly exported, to unqualifiedly exported. Its exported scope is widened.
  55 //
  56 // A package cannot transition from:
  57 //   - being unqualifiedly exported, to exported qualifiedly to a specific module.
  58 //       This transition attempt is silently ignored in set_exported.
  59 //   - being qualifiedly exported to not exported.
  60 //       Because transitions are only allowed from less exposure to greater exposure,
  61 //       the transition from qualifiedly exported to not exported would be considered
  62 //       a backward direction.  Therefore the implementation considers a package as
  63 //       qualifiedly exported even if its export-list exists but is empty.
  64 //
  65 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either
  66 // data structure.
  67 
  68 // PKG_EXP_UNQUALIFIED and PKG_EXP_ALLUNNAMED indicate whether the package is
  69 // exported unqualifiedly or exported to all unnamed modules.  They are used to
  70 // set the value of _export_flags.  Field _export_flags and the _qualified_exports
  71 // list are used to determine a package's export state.
  72 // Valid states are:
  73 //
  74 //   1. Package is not exported
  75 //      _export_flags is zero and _qualified_exports is null
  76 //   2. Package is unqualifiedly exported
  77 //      _export_flags is set to PKG_EXP_UNQUALIFIED
  78 //      _qualified_exports may or may not be null depending on whether the package
  79 //        transitioned from qualifiedly exported to unqualifiedly exported.
  80 //   3. Package is qualifiedly exported
  81 //      _export_flags may be set to PKG_EXP_ALLUNNAMED if the package is also
  82 //        exported to all unnamed modules
  83 //      _qualified_exports will be non-null
  84 //   4. Package is exported to all unnamed modules
  85 //      _export_flags is set to PKG_EXP_ALLUNNAMED
  86 //      _qualified_exports may or may not be null depending on whether the package
  87 //        is also qualifiedly exported to one or more named modules.
  88 #define PKG_EXP_UNQUALIFIED  0x0001
  89 #define PKG_EXP_ALLUNNAMED   0x0002
  90 #define PKG_EXP_UNQUALIFIED_OR_ALL_UNAMED (PKG_EXP_UNQUALIFIED | PKG_EXP_ALLUNNAMED)
  91 
  92 class PackageEntry : public HashtableEntry<Symbol*, mtModule> {
  93 private:
  94   ModuleEntry* _module;
  95   // Indicates if package is exported unqualifiedly or to all unnamed. Access to
  96   // this field is protected by the Module_lock.
  97   int _export_flags;
  98   // Used to indicate for packages with classes loaded by the boot loader that
  99   // a class in that package has been loaded.  And, for packages with classes
 100   // loaded by the boot loader from -Xbootclasspath/a in an unnamed module, it
 101   // indicates from which class path entry.
 102   s2 _classpath_index;
 103   bool _must_walk_exports;
 104   // Contains list of modules this package is qualifiedly exported to.  Access
 105   // to this list is protected by the Module_lock.
 106   GrowableArray<ModuleEntry*>* _qualified_exports;
 107   TRACE_DEFINE_TRACE_ID_FIELD;
 108 
 109   // Initial size of a package entry's list of qualified exports.
 110   enum {QUAL_EXP_SIZE = 43};
 111 
 112 public:
 113   void init() {
 114     _module = NULL;
 115     _export_flags = 0;
 116     _classpath_index = -1;
 117     _must_walk_exports = false;
 118     _qualified_exports = NULL;
 119   }
 120 
 121   // package name
 122   Symbol*            name() const               { return literal(); }
 123 
 124   // the module containing the package definition
 125   ModuleEntry*       module() const             { return _module; }
 126   void               set_module(ModuleEntry* m) { _module = m; }
 127 
 128   // package's export state
 129   bool is_exported() const { // qualifiedly or unqualifiedly exported
 130     assert_locked_or_safepoint(Module_lock);
 131     return ((_export_flags & PKG_EXP_UNQUALIFIED_OR_ALL_UNAMED) != 0) || has_qual_exports_list();
 132   }
 133   // Returns true if the package has any explicit qualified exports or is exported to all unnamed
 134   bool is_qual_exported() const {
 135     assert_locked_or_safepoint(Module_lock);
 136     return (has_qual_exports_list() || is_exported_allUnnamed());
 137   }
 138   // Returns true if there are any explicit qualified exports.  Note that even
 139   // if the _qualified_exports list is now empty (because the modules that were
 140   // on the list got gc-ed and deleted from the list) this method may still
 141   // return true.
 142   bool has_qual_exports_list() const {
 143     assert_locked_or_safepoint(Module_lock);
 144     return (!is_unqual_exported() && _qualified_exports != NULL);
 145   }
 146   bool is_exported_allUnnamed() const {
 147     assert_locked_or_safepoint(Module_lock);
 148     return (_export_flags == PKG_EXP_ALLUNNAMED);
 149   }
 150   bool is_unqual_exported() const {
 151     assert_locked_or_safepoint(Module_lock);
 152     return (_export_flags == PKG_EXP_UNQUALIFIED);
 153   }
 154 
 155   // Explicitly set _export_flags to PKG_EXP_UNQUALIFIED and clear
 156   // PKG_EXP_ALLUNNAMED, if it was set.
 157   void set_unqual_exported() {
 158     assert(Module_lock->owned_by_self(), "should have the Module_lock");
 159     _export_flags = PKG_EXP_UNQUALIFIED;
 160   }
 161 
 162   bool exported_pending_delete() const;
 163 
 164   void set_exported(ModuleEntry* m);
 165 
 166   void set_is_exported_allUnnamed();
 167 
 168   void set_classpath_index(s2 classpath_index) {
 169     _classpath_index = classpath_index;
 170   }
 171   s2 classpath_index() const { return _classpath_index; }
 172 
 173   bool has_loaded_class() const { return _classpath_index != -1; }
 174 
 175   // returns true if the package is defined in the unnamed module
 176   bool in_unnamed_module() const  { return !_module->is_named(); }
 177 
 178   // returns true if the package specifies m as a qualified export, including through an unnamed export
 179   bool is_qexported_to(ModuleEntry* m) const;
 180 
 181   // add the module to the package's qualified exports
 182   void add_qexport(ModuleEntry* m);
 183   void set_export_walk_required(ClassLoaderData* m_loader_data);
 184 
 185   PackageEntry* next() const {
 186     return (PackageEntry*)HashtableEntry<Symbol*, mtModule>::next();
 187   }
 188 
 189   PackageEntry** next_addr() {
 190     return (PackageEntry**)HashtableEntry<Symbol*, mtModule>::next_addr();
 191   }
 192 
 193   // iteration of qualified exports
 194   void package_exports_do(ModuleClosure* const f);
 195 
 196   TRACE_DEFINE_TRACE_ID_METHODS;
 197 
 198   // Purge dead weak references out of exported list when any given class loader is unloaded.
 199   void purge_qualified_exports();
 200   void delete_qualified_exports();
 201 
 202   void print(outputStream* st = tty);
 203   void verify();
 204 };
 205 
 206 // The PackageEntryTable is a Hashtable containing a list of all packages defined
 207 // by a particular class loader.  Each package is represented as a PackageEntry node.
 208 // The PackageEntryTable's lookup is lock free.
 209 //
 210 class PackageEntryTable : public Hashtable<Symbol*, mtModule> {
 211   friend class VMStructs;
 212 public:
 213   enum Constants {
 214     _packagetable_entry_size = 1009  // number of entries in package entry table
 215   };
 216 
 217 private:
 218   PackageEntry* new_entry(unsigned int hash, Symbol* name, ModuleEntry* module);
 219   void add_entry(int index, PackageEntry* new_entry);
 220 
 221   int entry_size() const { return BasicHashtable<mtModule>::entry_size(); }
 222 
 223   PackageEntry** bucket_addr(int i) {
 224     return (PackageEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i);
 225   }
 226 
 227   static unsigned int compute_hash(Symbol* name) { return (unsigned int)(name->identity_hash()); }
 228   int index_for(Symbol* name) const { return hash_to_index(compute_hash(name)); }
 229 
 230 public:
 231   PackageEntryTable(int table_size);
 232   ~PackageEntryTable();
 233 
 234   PackageEntry* bucket(int i) {
 235     return (PackageEntry*)Hashtable<Symbol*, mtModule>::bucket(i);
 236   }
 237 
 238   // Create package in loader's package entry table and return the entry.
 239   // If entry already exists, return null.  Assume Module lock was taken by caller.
 240   PackageEntry* locked_create_entry_or_null(Symbol* name, ModuleEntry* module);
 241 
 242   // lookup Package with loader's package entry table, if not found add
 243   PackageEntry* lookup(Symbol* name, ModuleEntry* module);
 244 
 245   // Only lookup Package within loader's package entry table.  The table read is lock-free.
 246   PackageEntry* lookup_only(Symbol* Package);
 247 
 248   void verify_javabase_packages(GrowableArray<Symbol*> *pkg_list);
 249 
 250   // purge dead weak references out of exported list
 251   void purge_all_package_exports();
 252 
 253   void print(outputStream* st = tty);
 254   void verify();
 255 };
 256 
 257 #endif // SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP