1 /*
   2  * Copyright (c) 2015, 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_MODULEENTRY_HPP
  26 #define SHARE_VM_CLASSFILE_MODULEENTRY_HPP
  27 
  28 #include "classfile/classLoaderData.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "oops/symbol.hpp"
  31 #include "prims/jni.h"
  32 #include "runtime/mutexLocker.hpp"
  33 #include "trace/traceMacros.hpp"
  34 #include "utilities/growableArray.hpp"
  35 #include "utilities/hashtable.hpp"
  36 
  37 #define UNNAMED_MODULE "Unnamed Module"
  38 
  39 class ModuleClosure;
  40 
  41 // A ModuleEntry describes a module that has been defined by a call to JVM_DefineModule.
  42 // It contains:
  43 //   - Symbol* containing the module's name.
  44 //   - pointer to the java.lang.reflect.Module for this module.
  45 //   - ClassLoaderData*, class loader of this module.
  46 //   - a growable array containg other module entries that this module can read.
  47 //   - a flag indicating if any of the packages defined within this module have qualified
  48 //     exports.
  49 //
  50 class ModuleEntry : public HashtableEntry<Symbol*, mtClass> {
  51 private:
  52   jobject _jlrM;                       // java.lang.reflect.Module
  53   ClassLoaderData* _loader;
  54   GrowableArray<ModuleEntry*>* _reads; // list of modules that are readable by this module
  55   Symbol* _version;                    // module version number
  56   Symbol* _location;                   // module location
  57   bool _can_read_unnamed;
  58   bool _has_default_read_edges;        // JVMTI redefine/retransform support
  59   TRACE_DEFINE_TRACE_ID_FIELD;
  60 
  61 public:
  62   void init() {
  63     _jlrM = NULL;
  64     _loader = NULL;
  65     _reads = NULL;
  66     _version = NULL;
  67     _location = NULL;
  68     _can_read_unnamed = false;
  69     _has_default_read_edges = false;
  70   }
  71 
  72   Symbol*            name() const                   { return literal(); }
  73   void               set_name(Symbol* n)            { set_literal(n); }
  74 
  75   jobject            jlrM_module() const            { return _jlrM; }
  76   void               set_jlrM_module(jobject j)     { _jlrM = j; }
  77 
  78   ClassLoaderData*   loader() const                 { return _loader; }
  79   void               set_loader(ClassLoaderData* l) { _loader = l; }
  80 
  81   Symbol*            version() const                { return _version; }
  82   void               set_version(Symbol* version)   { _version = version; }
  83 
  84   Symbol*            location() const               { return _location; }
  85   void               set_location(Symbol* location) { _location = location; }
  86 
  87   bool               can_read(ModuleEntry* m) const;
  88   bool               has_reads() const;
  89   void               add_read(ModuleEntry* m);
  90 
  91   bool               is_named() const               { return (literal() != NULL); }
  92 
  93   bool can_read_unnamed() const {
  94     assert(is_named() || _can_read_unnamed == true,
  95            "unnamed modules can always read all unnamed modules");
  96     return _can_read_unnamed;
  97   }
  98 
  99   // Modules can only go from strict to loose.
 100   void set_can_read_unnamed() { _can_read_unnamed = true; }
 101 
 102   bool has_default_read_edges() const {
 103     return _has_default_read_edges;
 104   }
 105 
 106   // Sets true and returns the previous value.
 107   bool set_has_default_read_edges() {
 108     MutexLocker ml(Module_lock);
 109     bool prev = _has_default_read_edges;
 110     _has_default_read_edges = true;
 111     return prev;
 112   }
 113 
 114   ModuleEntry* next() const {
 115     return (ModuleEntry*)HashtableEntry<Symbol*, mtClass>::next();
 116   }
 117   ModuleEntry** next_addr() {
 118     return (ModuleEntry**)HashtableEntry<Symbol*, mtClass>::next_addr();
 119   }
 120 
 121   // iteration support for readability
 122   void module_reads_do(ModuleClosure* const f);
 123 
 124   TRACE_DEFINE_TRACE_ID_METHODS;
 125 
 126   // Purge dead weak references out of reads list when any given class loader is unloaded.
 127   void purge_reads();
 128   void delete_reads();
 129 
 130   void print() PRODUCT_RETURN;
 131   void verify();
 132 };
 133 
 134 // Iterator interface
 135 class ModuleClosure: public StackObj {
 136  public:
 137   virtual void do_module(ModuleEntry* const module) = 0;
 138 };
 139 
 140 
 141 // The ModuleEntryTable is a Hashtable containing a list of all modules defined
 142 // by a particular class loader.  Each module is represented as a ModuleEntry node.
 143 //
 144 // Each ModuleEntryTable contains a _javabase_module field which allows for the
 145 // creation of java.base's ModuleEntry very early in bootstrapping before the
 146 // corresponding JVM_DefineModule call for java.base occurs during module system
 147 // initialization.  Setting up java.base's ModuleEntry early enables classes,
 148 // loaded prior to the module system being initialized to be created with their
 149 // PackageEntry node's correctly pointing at java.base's ModuleEntry.  No class
 150 // outside of java.base is allowed to be loaded pre-module system initialization.
 151 //
 152 // The ModuleEntryTable's lookup is lock free.
 153 //
 154 class ModuleEntryTable : public Hashtable<Symbol*, mtClass> {
 155   friend class VMStructs;
 156 public:
 157   enum Constants {
 158     _moduletable_entry_size  = 109 // number of entries in module entry table
 159   };
 160 
 161 private:
 162   static ModuleEntry* _javabase_module;
 163   ModuleEntry* _unnamed_module;
 164 
 165   ModuleEntry* new_entry(unsigned int hash, Handle jlrM_handle, Symbol* name, Symbol* version,
 166                          Symbol* location, ClassLoaderData* class_loader);
 167   void add_entry(int index, ModuleEntry* new_entry);
 168 
 169 public:
 170   ModuleEntryTable(int table_size);
 171   ~ModuleEntryTable();
 172 
 173   ModuleEntry* unnamed_module() { return _unnamed_module; }
 174   void set_unnamed_module(ModuleEntry* m) { _unnamed_module = m; }
 175 
 176   int entry_size() const { return BasicHashtable<mtClass>::entry_size(); }
 177 
 178   // Create module in loader's module entry table, if already exists then
 179   // return null.  Assume Module_lock has been locked by caller.
 180   ModuleEntry* locked_create_entry_or_null(Handle jlrM_handle,
 181                                            Symbol* module_name,
 182                                            Symbol* module_version,
 183                                            Symbol* module_location,
 184                                            ClassLoaderData* loader_data);
 185 
 186   // only lookup module within loader's module entry table
 187   ModuleEntry* lookup_only(Symbol* name);
 188 
 189   ModuleEntry* bucket(int i) {
 190     return (ModuleEntry*)Hashtable<Symbol*, mtClass>::bucket(i);
 191   }
 192 
 193   ModuleEntry** bucket_addr(int i) {
 194     return (ModuleEntry**)Hashtable<Symbol*, mtClass>::bucket_addr(i);
 195   }
 196 
 197   static ModuleEntryTable* create_module_entry_table(ClassLoaderData* loader_data);
 198 
 199   // Special handling for java.base
 200   static ModuleEntry* javabase_module()                   { return _javabase_module; }
 201   static void set_javabase_module(ModuleEntry* java_base) { _javabase_module = java_base; }
 202   static bool javabase_defined()                          { return ((_javabase_module != NULL) &&
 203                                                                     (_javabase_module->jlrM_module() != NULL)); }
 204   static void patch_javabase_entries(Handle jlrM_module, TRAPS);
 205 
 206   static unsigned int compute_hash(Symbol* name) { return ((name == NULL) ? 0 : (unsigned int)(name->identity_hash())); }
 207   int index_for(Symbol* name) const              { return hash_to_index(compute_hash(name)); }
 208 
 209   // purge dead weak references out of reads list
 210   void purge_all_module_reads();
 211 
 212   void print() PRODUCT_RETURN;
 213   void verify();
 214 };
 215 
 216 #endif // SHARE_VM_CLASSFILE_MODULEENTRY_HPP