1 /*
   2  * Copyright (c) 2016, 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 flag indicating if package is exported unqualifiedly
  38 //   - a flag indicating if this package is exported to all 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: _is_exported_allUnnamed is a form of a qualified
  48 //                            export. It is equivalent to the package being
  49 //                            explicitly 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 //
  60 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either
  61 // data structure.
  62 class PackageEntry : public HashtableEntry<Symbol*, mtModule> {
  63 private:
  64   ModuleEntry* _module;
  65   // Used to indicate for packages with classes loaded by the boot loader that
  66   // a class in that package has been loaded.  And, for packages with classes
  67   // loaded by the boot loader from -Xbootclasspath/a in an unnamed module, it
  68   // indicates from which class path entry.
  69   s2 _classpath_index;
  70   bool _is_exported_unqualified;
  71   bool _is_exported_allUnnamed;
  72   bool _must_walk_exports;
  73   GrowableArray<ModuleEntry*>* _exported_pending_delete; // transitioned from qualified to unqualified, delete at safepoint
  74   GrowableArray<ModuleEntry*>* _qualified_exports;
  75   TRACE_DEFINE_TRACE_ID_FIELD;
  76 
  77   // Initial size of a package entry's list of qualified exports.
  78   enum {QUAL_EXP_SIZE = 43};
  79 
  80 public:
  81   void init() {
  82     _module = NULL;
  83     _classpath_index = -1;
  84     _is_exported_unqualified = false;
  85     _is_exported_allUnnamed = false;
  86     _must_walk_exports = false;
  87     _exported_pending_delete = NULL;
  88     _qualified_exports = NULL;
  89   }
  90 
  91   // package name
  92   Symbol*            name() const               { return literal(); }
  93   void               set_name(Symbol* n)        { set_literal(n); }
  94 
  95   // the module containing the package definition
  96   ModuleEntry*       module() const             { return _module; }
  97   void               set_module(ModuleEntry* m) { _module = m; }
  98 
  99   // package's export state
 100   bool is_exported() const { // qualifiedly or unqualifiedly exported
 101       return (is_unqual_exported() || has_qual_exports_list() || is_exported_allUnnamed());
 102   }
 103   // Returns true if the package has any explicit qualified exports or is exported to all unnamed
 104   bool is_qual_exported() const {
 105     return (has_qual_exports_list() || is_exported_allUnnamed());
 106   }
 107   // Returns true if there are any explicit qualified exports
 108   bool has_qual_exports_list() const {
 109     assert(!(_qualified_exports != NULL && _is_exported_unqualified),
 110            "_qualified_exports set at same time as _is_exported_unqualified");
 111     return (_qualified_exports != NULL);
 112   }
 113   bool is_exported_allUnnamed() const {
 114     assert(!(_is_exported_allUnnamed && _is_exported_unqualified),
 115            "_is_exported_allUnnamed set at same time as _is_exported_unqualified");
 116     return _is_exported_allUnnamed;
 117   }
 118   bool is_unqual_exported() const {
 119     assert(!(_qualified_exports != NULL && _is_exported_unqualified),
 120            "_qualified_exports set at same time as _is_exported_unqualified");
 121     assert(!(_is_exported_allUnnamed && _is_exported_unqualified),
 122            "_is_exported_allUnnamed set at same time as _is_exported_unqualified");
 123     return _is_exported_unqualified;
 124   }
 125   void set_unqual_exported() {
 126     assert(Module_lock->owned_by_self(), "should have the Module_lock");
 127     _is_exported_unqualified = true;
 128     _is_exported_allUnnamed = false;
 129     _qualified_exports = NULL;
 130   }
 131   bool exported_pending_delete() const     { return (_exported_pending_delete != NULL); }
 132 
 133   void set_exported(ModuleEntry* m);
 134 
 135   void set_is_exported_allUnnamed();
 136 
 137   void set_classpath_index(s2 classpath_index) {
 138     _classpath_index = classpath_index;
 139   }
 140   s2 classpath_index() const { return _classpath_index; }
 141 
 142   bool has_loaded_class() const { return _classpath_index != -1; }
 143 
 144   // returns true if the package is defined in the unnamed module
 145   bool in_unnamed_module() const  { return !_module->is_named(); }
 146 
 147   // returns true if the package specifies m as a qualified export, including through an unnamed export
 148   bool is_qexported_to(ModuleEntry* m) const;
 149 
 150   // add the module to the package's qualified exports
 151   void add_qexport(ModuleEntry* m);
 152   void set_export_walk_required(ClassLoaderData* m_loader_data);
 153 
 154   PackageEntry* next() const {
 155     return (PackageEntry*)HashtableEntry<Symbol*, mtModule>::next();
 156   }
 157 
 158   PackageEntry** next_addr() {
 159     return (PackageEntry**)HashtableEntry<Symbol*, mtModule>::next_addr();
 160   }
 161 
 162   // iteration of qualified exports
 163   void package_exports_do(ModuleClosure* const f);
 164 
 165   TRACE_DEFINE_TRACE_ID_METHODS;
 166 
 167   // Purge dead weak references out of exported list when any given class loader is unloaded.
 168   void purge_qualified_exports();
 169   void delete_qualified_exports();
 170 
 171   void print(outputStream* st = tty);
 172   void verify();
 173 };
 174 
 175 // The PackageEntryTable is a Hashtable containing a list of all packages defined
 176 // by a particular class loader.  Each package is represented as a PackageEntry node.
 177 // The PackageEntryTable's lookup is lock free.
 178 //
 179 class PackageEntryTable : public Hashtable<Symbol*, mtModule> {
 180   friend class VMStructs;
 181 public:
 182   enum Constants {
 183     _packagetable_entry_size = 1009  // number of entries in package entry table
 184   };
 185 
 186 private:
 187   PackageEntry* new_entry(unsigned int hash, Symbol* name, ModuleEntry* module);
 188   void add_entry(int index, PackageEntry* new_entry);
 189 
 190   int entry_size() const { return BasicHashtable<mtModule>::entry_size(); }
 191 
 192   PackageEntry** bucket_addr(int i) {
 193     return (PackageEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i);
 194   }
 195 
 196   static unsigned int compute_hash(Symbol* name) { return (unsigned int)(name->identity_hash()); }
 197   int index_for(Symbol* name) const { return hash_to_index(compute_hash(name)); }
 198 
 199 public:
 200   PackageEntryTable(int table_size);
 201   ~PackageEntryTable();
 202 
 203   PackageEntry* bucket(int i) {
 204     return (PackageEntry*)Hashtable<Symbol*, mtModule>::bucket(i);
 205   }
 206 
 207   // Create package in loader's package entry table and return the entry.
 208   // If entry already exists, return null.  Assume Module lock was taken by caller.
 209   PackageEntry* locked_create_entry_or_null(Symbol* name, ModuleEntry* module);
 210 
 211   // lookup Package with loader's package entry table, if not found add
 212   PackageEntry* lookup(Symbol* name, ModuleEntry* module);
 213 
 214   // Only lookup Package within loader's package entry table.  The table read is lock-free.
 215   PackageEntry* lookup_only(Symbol* Package);
 216 
 217   void verify_javabase_packages(GrowableArray<Symbol*> *pkg_list);
 218 
 219   // purge dead weak references out of exported list
 220   void purge_all_package_exports();
 221 
 222   void print(outputStream* st = tty);
 223   void verify();
 224 };
 225 
 226 #endif // SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP