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