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_MODULEENTRY_HPP
 26 #define SHARE_CLASSFILE_MODULEENTRY_HPP
 27 
 28 #include "jni.h"
 29 #include "classfile/classLoaderData.hpp"
 30 #include "classfile/vmSymbols.hpp"
 31 #include "oops/oopHandle.hpp"
 32 #include "oops/symbol.hpp"
 33 #include "runtime/jniHandles.hpp"
 34 #include "runtime/mutexLocker.hpp"
 35 #include "utilities/growableArray.hpp"
 36 #include "utilities/hashtable.hpp"
 37 #include "utilities/macros.hpp"
 38 #include "utilities/ostream.hpp"
 39 #if INCLUDE_JFR
 40 #include "jfr/support/jfrTraceIdExtension.hpp"
 41 #endif
 42 
 43 #define UNNAMED_MODULE "unnamed module"
 44 #define UNNAMED_MODULE_LEN 14
 45 #define JAVAPKG "java"
 46 #define JAVAPKG_LEN 4
 47 #define JAVA_BASE_NAME "java.base"
 48 #define JAVA_BASE_NAME_LEN 9
 49 
 50 template <class T> class Array;
 51 class MetaspaceClosure;
 52 class ModuleClosure;
 53 
 54 // A ModuleEntry describes a module that has been defined by a call to JVM_DefineModule.
 55 // It contains:
 56 //   - Symbol* containing the module's name.
 57 //   - pointer to the java.lang.Module for this module.
 58 //   - pointer to the java.security.ProtectionDomain shared by classes defined to this module.
 59 //   - ClassLoaderData*, class loader of this module.
 60 //   - a growable array containg other module entries that this module can read.
 61 //   - a flag indicating if this module can read all unnamed modules.
 62 //
 63 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either
 64 // data structure.
 65 class ModuleEntry : public HashtableEntry<Symbol*, mtModule> {
 66 private:
 67   OopHandle _module;                   // java.lang.Module
 68   OopHandle _shared_pd;                // java.security.ProtectionDomain, cached
 69                                        // for shared classes from this module
 70   ClassLoaderData* _loader_data;
 71   GrowableArray<ModuleEntry*>* _reads; // list of modules that are readable by this module
 72   Symbol* _version;                    // module version number
 73   Symbol* _location;                   // module location
 74   CDS_ONLY(int _shared_path_index;)    // >=0 if classes in this module are in CDS archive
 75   bool _can_read_all_unnamed;
 76   bool _has_default_read_edges;        // JVMTI redefine/retransform support
 77   bool _must_walk_reads;               // walk module's reads list at GC safepoints to purge out dead modules
 78   bool _is_open;                       // whether the packages in the module are all unqualifiedly exported
 79   bool _is_patched;                    // whether the module is patched via --patch-module
 80   CDS_JAVA_HEAP_ONLY(int _archived_module_index;)
 81 
 82   JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
 83   enum {MODULE_READS_SIZE = 101};      // Initial size of list of modules that the module can read.
 84 
 85 public:
 86   void init() {
 87     _module = OopHandle();
 88     _shared_pd = OopHandle();
 89     _loader_data = NULL;
 90     _reads = NULL;
 91     _version = NULL;
 92     _location = NULL;
 93     _can_read_all_unnamed = false;
 94     _has_default_read_edges = false;
 95     _must_walk_reads = false;
 96     _is_patched = false;
 97     _is_open = false;
 98     CDS_ONLY(_shared_path_index = -1);
 99   }
100 
101   Symbol*          name() const                        { return literal(); }
102   void             set_name(Symbol* n)                 { set_literal(n); }
103 
104   oop              module() const;
105   OopHandle        module_handle() const               { return _module; }
106   void             set_module(OopHandle j)             { _module = j; }
107 
108   // The shared ProtectionDomain reference is set once the VM loads a shared class
109   // originated from the current Module. The referenced ProtectionDomain object is
110   // created by the ClassLoader when loading a class (shared or non-shared) from the
111   // Module for the first time. This ProtectionDomain object is used for all
112   // classes from the Module loaded by the same ClassLoader.
113   oop              shared_protection_domain();
114   void             set_shared_protection_domain(ClassLoaderData *loader_data, Handle pd);
115 
116   ClassLoaderData* loader_data() const                 { return _loader_data; }
117 
118   void set_loader_data(ClassLoaderData* cld) {
119     assert(!cld->has_class_mirror_holder(), "Unexpected has_class_mirror_holder cld");
120     _loader_data = cld;
121   }
122 
123   Symbol*          version() const                     { return _version; }
124   void             set_version(Symbol* version);
125 
126   Symbol*          location() const                    { return _location; }
127   void             set_location(Symbol* location);
128   bool             should_show_version();
129 
130   bool             can_read(ModuleEntry* m) const;
131   bool             has_reads_list() const;
132   void             add_read(ModuleEntry* m);
133   void             set_read_walk_required(ClassLoaderData* m_loader_data);
134 
135   bool             is_open() const                     { return _is_open; }
136   void             set_is_open(bool is_open);
137 
138   bool             is_named() const                    { return (name() != NULL); }
139 
140   bool can_read_all_unnamed() const {
141     assert(is_named() || _can_read_all_unnamed == true,
142            "unnamed modules can always read all unnamed modules");
143     return _can_read_all_unnamed;
144   }
145 
146   // Modules can only go from strict to loose.
147   void set_can_read_all_unnamed() { _can_read_all_unnamed = true; }
148 
149   bool has_default_read_edges() const {
150     return _has_default_read_edges;
151   }
152 
153   // Sets true and returns the previous value.
154   bool set_has_default_read_edges() {
155     MutexLocker ml(Module_lock);
156     bool prev = _has_default_read_edges;
157     _has_default_read_edges = true;
158     return prev;
159   }
160 
161   void set_is_patched() {
162       _is_patched = true;
163       CDS_ONLY(_shared_path_index = -1); // Mark all shared classes in this module invisible.
164   }
165   bool is_patched() {
166       return _is_patched;
167   }
168 
169   ModuleEntry* next() const {
170     return (ModuleEntry*)HashtableEntry<Symbol*, mtModule>::next();
171   }
172   ModuleEntry** next_addr() {
173     return (ModuleEntry**)HashtableEntry<Symbol*, mtModule>::next_addr();
174   }
175 
176   // iteration support for readability
177   void module_reads_do(ModuleClosure* const f);
178 
179   // Purge dead weak references out of reads list when any given class loader is unloaded.
180   void purge_reads();
181   void delete_reads();
182 
183   // Special handling for unnamed module, one per class loader
184   static ModuleEntry* create_unnamed_module(ClassLoaderData* cld);
185   static ModuleEntry* create_boot_unnamed_module(ClassLoaderData* cld);
186   static ModuleEntry* new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld);
187   void delete_unnamed_module();
188 
189   void print(outputStream* st = tty);
190   void verify();
191 
192   CDS_ONLY(int shared_path_index() { return _shared_path_index;})
193 
194   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
195 
196 #if INCLUDE_CDS_JAVA_HEAP
197   void iterate_symbols(MetaspaceClosure* closure);
198   ModuleEntry* allocate_archived_entry() const;
199   void init_as_archived_entry();
200   void init_archived_oops();
201   static ModuleEntry* get_archived_entry(ModuleEntry* orig_entry);
202   static Array<ModuleEntry*>* write_growable_array(GrowableArray<ModuleEntry*>* array);
203   static GrowableArray<ModuleEntry*>* restore_growable_array(Array<ModuleEntry*>* archived_array);
204   void load_from_archive(ClassLoaderData* loader_data);
205   void restore_archived_oops(ClassLoaderData* loader_data);
206   void clear_archived_oops();
207 #endif
208 };
209 
210 // Iterator interface
211 class ModuleClosure: public StackObj {
212  public:
213   virtual void do_module(ModuleEntry* module) = 0;
214 };
215 
216 
217 // The ModuleEntryTable is a Hashtable containing a list of all modules defined
218 // by a particular class loader.  Each module is represented as a ModuleEntry node.
219 //
220 // Each ModuleEntryTable contains a _javabase_module field which allows for the
221 // creation of java.base's ModuleEntry very early in bootstrapping before the
222 // corresponding JVM_DefineModule call for java.base occurs during module system
223 // initialization.  Setting up java.base's ModuleEntry early enables classes,
224 // loaded prior to the module system being initialized to be created with their
225 // PackageEntry node's correctly pointing at java.base's ModuleEntry.  No class
226 // outside of java.base is allowed to be loaded pre-module system initialization.
227 //
228 // The ModuleEntryTable's lookup is lock free.
229 //
230 class ModuleEntryTable : public Hashtable<Symbol*, mtModule> {
231   friend class VMStructs;
232 public:
233   enum Constants {
234     _moduletable_entry_size  = 109 // number of entries in module entry table
235   };
236 
237 private:
238   static ModuleEntry* _javabase_module;
239 
240   ModuleEntry* new_entry(unsigned int hash, Handle module_handle, bool is_open,
241                          Symbol* name, Symbol* version, Symbol* location, ClassLoaderData* loader_data);
242   void add_entry(int index, ModuleEntry* new_entry);
243 
244   int entry_size() const { return BasicHashtable<mtModule>::entry_size(); }
245 
246   ModuleEntry** bucket_addr(int i) {
247     return (ModuleEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i);
248   }
249 
250   static unsigned int compute_hash(Symbol* name) { return ((name == NULL) ? 0 : (unsigned int)(name->identity_hash())); }
251   int index_for(Symbol* name) const              { return hash_to_index(compute_hash(name)); }
252 
253 public:
254   ModuleEntryTable(int table_size);
255   ~ModuleEntryTable();
256 
257   ModuleEntry* bucket(int i) {
258     return (ModuleEntry*)Hashtable<Symbol*, mtModule>::bucket(i);
259   }
260 
261   // Create module in loader's module entry table.  Assume Module_lock
262   // has been locked by caller.
263   ModuleEntry* locked_create_entry(Handle module_handle,
264                                    bool is_open,
265                                    Symbol* module_name,
266                                    Symbol* module_version,
267                                    Symbol* module_location,
268                                    ClassLoaderData* loader_data);
269 
270   // Only lookup module within loader's module entry table.  The table read is lock-free.
271   ModuleEntry* lookup_only(Symbol* name);
272 
273   // purge dead weak references out of reads list
274   void purge_all_module_reads();
275 
276   // Special handling for java.base
277   static ModuleEntry* javabase_moduleEntry()                   { return _javabase_module; }
278   static void set_javabase_moduleEntry(ModuleEntry* java_base) {
279     assert(_javabase_module == NULL, "_javabase_module is already defined");
280     _javabase_module = java_base;
281   }
282 
283   static bool javabase_defined() { return ((_javabase_module != NULL) &&
284                                            (_javabase_module->module() != NULL)); }
285   static void finalize_javabase(Handle module_handle, Symbol* version, Symbol* location);
286   static void patch_javabase_entries(Handle module_handle);
287 
288   void print(outputStream* st = tty);
289   void verify();
290 
291 #if INCLUDE_CDS_JAVA_HEAP
292   void iterate_symbols(MetaspaceClosure* closure);
293   Array<ModuleEntry*>* allocate_archived_entries();
294   void init_archived_entries(Array<ModuleEntry*>* archived_modules);
295   void init_archived_oops(Array<ModuleEntry*>* archived_modules);
296   void load_archived_entries(ClassLoaderData* loader_data,
297                              Array<ModuleEntry*>* archived_modules);
298   void restore_archived_oops(ClassLoaderData* loader_data,
299                              Array<ModuleEntry*>* archived_modules);
300 #endif
301 };
302 
303 #endif // SHARE_CLASSFILE_MODULEENTRY_HPP