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