1 /*
  2  * Copyright (c) 2016, 2023, 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 "oops/oopHandle.hpp"
 30 #include "oops/symbol.hpp"
 31 #include "oops/symbolHandle.hpp"
 32 #include "runtime/mutexLocker.hpp"
 33 #include "utilities/growableArray.hpp"
 34 #include "utilities/macros.hpp"
 35 #include "utilities/ostream.hpp"
 36 #include "utilities/resourceHash.hpp"
 37 #if INCLUDE_JFR
 38 #include "jfr/support/jfrTraceIdExtension.hpp"
 39 #endif
 40 
 41 #define UNNAMED_MODULE "unnamed module"
 42 #define UNNAMED_MODULE_LEN 14
 43 #define JAVAPKG "java"
 44 #define JAVAPKG_LEN 4
 45 #define JAVA_BASE_NAME "java.base"
 46 #define JAVA_BASE_NAME_LEN 9
 47 
 48 template <class T> class Array;
 49 class ClassLoaderData;
 50 class MetaspaceClosure;
 51 class ModuleClosure;
 52 
 53 // A ModuleEntry describes a module that has been defined by a call to JVM_DefineModule.
 54 // It contains:
 55 //   - Symbol* containing the module's name.
 56 //   - pointer to the java.lang.Module for this module.
 57 //   - pointer to the java.security.ProtectionDomain shared by classes defined to this module.
 58 //   - ClassLoaderData*, class loader of this module.
 59 //   - a growable array containing other module entries that this module can read.
 60 //   - a flag indicating if this module can read all unnamed modules.
 61 //
 62 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either
 63 // data structure.  This lock must be taken on all accesses to either table.
 64 class ModuleEntry : public CHeapObj<mtModule> {
 65 private:
 66   OopHandle _module;                   // java.lang.Module
 67   OopHandle _shared_pd;                // java.security.ProtectionDomain, cached
 68                                        // for shared classes from this module
 69   Symbol*          _name;              // name of 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   ModuleEntry(Handle module_handle,
 87               bool is_open, Symbol* name,
 88               Symbol* version, Symbol* location,
 89               ClassLoaderData* loader_data);
 90 
 91   ~ModuleEntry();
 92 
 93   Symbol*          name() const                        { return _name; }
 94   oop              module() const;
 95   OopHandle        module_handle() const               { return _module; }
 96   void             set_module(OopHandle j)             { _module = j; }
 97 
 98   // The shared ProtectionDomain reference is set once the VM loads a shared class
 99   // originated from the current Module. The referenced ProtectionDomain object is
100   // created by the ClassLoader when loading a class (shared or non-shared) from the
101   // Module for the first time. This ProtectionDomain object is used for all
102   // classes from the Module loaded by the same ClassLoader.
103   oop              shared_protection_domain();
104   void             set_shared_protection_domain(ClassLoaderData *loader_data, Handle pd);
105 
106   ClassLoaderData* loader_data() const                 { return _loader_data; }
107   void set_loader_data(ClassLoaderData* cld);
108 
109   Symbol*          version() const                     { return _version; }
110   void             set_version(Symbol* version);
111 
112   Symbol*          location() const                    { return _location; }
113   void             set_location(Symbol* location);
114   bool             should_show_version();
115 
116   bool             can_read(ModuleEntry* m) const;
117   bool             has_reads_list() const;
118   void             add_read(ModuleEntry* m);
119   void             set_read_walk_required(ClassLoaderData* m_loader_data);
120 
121   bool             is_open() const                     { return _is_open; }
122   void             set_is_open(bool is_open);
123 
124   bool             is_named() const                    { return (_name != nullptr); }
125 
126   bool can_read_all_unnamed() const {
127     assert(is_named() || _can_read_all_unnamed == true,
128            "unnamed modules can always read all unnamed modules");
129     return _can_read_all_unnamed;
130   }
131 
132   // Modules can only go from strict to loose.
133   void set_can_read_all_unnamed() { _can_read_all_unnamed = true; }
134 
135   bool has_default_read_edges() const {
136     return _has_default_read_edges;
137   }
138 
139   // Sets true and returns the previous value.
140   bool set_has_default_read_edges() {
141     MutexLocker ml(Module_lock);
142     bool prev = _has_default_read_edges;
143     _has_default_read_edges = true;
144     return prev;
145   }
146 
147   void set_is_patched() {
148       _is_patched = true;
149       CDS_ONLY(_shared_path_index = -1); // Mark all shared classes in this module invisible.
150   }
151   bool is_patched() {
152       return _is_patched;
153   }
154 
155   // iteration support for readability
156   void module_reads_do(ModuleClosure* const f);
157 
158   // Purge dead weak references out of reads list when any given class loader is unloaded.
159   void purge_reads();
160   void delete_reads();
161 
162   // Special handling for unnamed module, one per class loader
163   static ModuleEntry* create_unnamed_module(ClassLoaderData* cld);
164   static ModuleEntry* create_boot_unnamed_module(ClassLoaderData* cld);
165   static ModuleEntry* new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld);
166 
167   void print(outputStream* st = tty);
168   void verify();
169 
170   CDS_ONLY(int shared_path_index() { return _shared_path_index;})
171 
172   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
173 
174 #if INCLUDE_CDS_JAVA_HEAP
175   void iterate_symbols(MetaspaceClosure* closure);
176   ModuleEntry* allocate_archived_entry() const;
177   void init_as_archived_entry();
178   static ModuleEntry* get_archived_entry(ModuleEntry* orig_entry);
179   bool has_been_archived();
180   static Array<ModuleEntry*>* write_growable_array(GrowableArray<ModuleEntry*>* array);
181   static GrowableArray<ModuleEntry*>* restore_growable_array(Array<ModuleEntry*>* archived_array);
182   void load_from_archive(ClassLoaderData* loader_data);
183   void restore_archived_oops(ClassLoaderData* loader_data);
184   void clear_archived_oops();
185   void update_oops_in_archived_module(int root_oop_index);
186   static void verify_archived_module_entries() PRODUCT_RETURN;
187 #endif
188 };
189 
190 // Iterator interface
191 class ModuleClosure: public StackObj {
192  public:
193   virtual void do_module(ModuleEntry* module) = 0;
194 };
195 
196 
197 // The ModuleEntryTable is a Hashtable containing a list of all modules defined
198 // by a particular class loader.  Each module is represented as a ModuleEntry node.
199 //
200 // Each ModuleEntryTable contains a _javabase_module field which allows for the
201 // creation of java.base's ModuleEntry very early in bootstrapping before the
202 // corresponding JVM_DefineModule call for java.base occurs during module system
203 // initialization.  Setting up java.base's ModuleEntry early enables classes,
204 // loaded prior to the module system being initialized to be created with their
205 // PackageEntry node's correctly pointing at java.base's ModuleEntry.  No class
206 // outside of java.base is allowed to be loaded pre-module system initialization.
207 //
208 class ModuleEntryTable : public CHeapObj<mtModule> {
209 private:
210   static ModuleEntry* _javabase_module;
211   ResourceHashtable<SymbolHandle, ModuleEntry*, 109, AnyObj::C_HEAP, mtModule,
212                     SymbolHandle::compute_hash> _table;
213 
214 public:
215   ModuleEntryTable();
216   ~ModuleEntryTable();
217 
218   // Create module in loader's module entry table.  Assume Module_lock
219   // has been locked by caller.
220   ModuleEntry* locked_create_entry(Handle module_handle,
221                                    bool is_open,
222                                    Symbol* module_name,
223                                    Symbol* module_version,
224                                    Symbol* module_location,
225                                    ClassLoaderData* loader_data);
226 
227   // Only lookup module within loader's module entry table.
228   ModuleEntry* lookup_only(Symbol* name);
229 
230   // purge dead weak references out of reads list
231   void purge_all_module_reads();
232 
233   // Special handling for java.base
234   static ModuleEntry* javabase_moduleEntry()                   { return _javabase_module; }
235   static void set_javabase_moduleEntry(ModuleEntry* java_base) {
236     assert(_javabase_module == nullptr, "_javabase_module is already defined");
237     _javabase_module = java_base;
238   }
239 
240   static bool javabase_defined() { return ((_javabase_module != nullptr) &&
241                                            (_javabase_module->module() != nullptr)); }
242   static void finalize_javabase(Handle module_handle, Symbol* version, Symbol* location);
243   static void patch_javabase_entries(JavaThread* current, Handle module_handle);
244 
245   void modules_do(void f(ModuleEntry*));
246   void modules_do(ModuleClosure* closure);
247 
248   void print(outputStream* st = tty);
249   void verify();
250 
251 #if INCLUDE_CDS_JAVA_HEAP
252   void iterate_symbols(MetaspaceClosure* closure);
253   Array<ModuleEntry*>* allocate_archived_entries();
254   void init_archived_entries(Array<ModuleEntry*>* archived_modules);
255   void load_archived_entries(ClassLoaderData* loader_data,
256                              Array<ModuleEntry*>* archived_modules);
257   void restore_archived_oops(ClassLoaderData* loader_data,
258                              Array<ModuleEntry*>* archived_modules);
259 #endif
260 };
261 
262 #endif // SHARE_CLASSFILE_MODULEENTRY_HPP