1 /*
  2  * Copyright (c) 2016, 2024, 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 
 72   union {
 73     GrowableArray<ModuleEntry*>* _reads;  // list of modules that are readable by this module
 74     Array<ModuleEntry*>* _archived_reads; // List of readable modules stored in the CDS archive
 75   };
 76   Symbol* _version;                    // module version number
 77   Symbol* _location;                   // module location
 78   CDS_ONLY(int _shared_path_index;)    // >=0 if classes in this module are in CDS archive
 79   bool _can_read_all_unnamed;
 80   bool _has_default_read_edges;        // JVMTI redefine/retransform support
 81   bool _must_walk_reads;               // walk module's reads list at GC safepoints to purge out dead modules
 82   bool _is_open;                       // whether the packages in the module are all unqualifiedly exported
 83   bool _is_patched;                    // whether the module is patched via --patch-module
 84   DEBUG_ONLY(bool _reads_is_archived);
 85   CDS_JAVA_HEAP_ONLY(int _archived_module_index;)
 86 
 87   JFR_ONLY(DEFINE_TRACE_ID_FIELD;)
 88   enum {MODULE_READS_SIZE = 101};      // Initial size of list of modules that the module can read.
 89 
 90 public:
 91   ModuleEntry(Handle module_handle,
 92               bool is_open, Symbol* name,
 93               Symbol* version, Symbol* location,
 94               ClassLoaderData* loader_data);
 95 
 96   ~ModuleEntry();
 97 
 98   Symbol*          name() const                        { return _name; }
 99   oop              module() const;
100   OopHandle        module_handle() const               { return _module; }
101   void             set_module(OopHandle j)             { _module = j; }
102 
103   // The shared ProtectionDomain reference is set once the VM loads a shared class
104   // originated from the current Module. The referenced ProtectionDomain object is
105   // created by the ClassLoader when loading a class (shared or non-shared) from the
106   // Module for the first time. This ProtectionDomain object is used for all
107   // classes from the Module loaded by the same ClassLoader.
108   oop              shared_protection_domain();
109   void             set_shared_protection_domain(ClassLoaderData *loader_data, Handle pd);
110 
111   ClassLoaderData* loader_data() const                 { return _loader_data; }
112   void set_loader_data(ClassLoaderData* cld);
113 
114   Symbol*          version() const                     { return _version; }
115   void             set_version(Symbol* version);
116 
117   Symbol*          location() const                    { return _location; }
118   void             set_location(Symbol* location);
119   bool             should_show_version();
120 
121   bool             can_read(ModuleEntry* m) const;
122   bool             has_reads_list() const;
123   GrowableArray<ModuleEntry*>* reads() const {
124     assert(!_reads_is_archived, "sanity");
125     return _reads;
126   }
127   void set_reads(GrowableArray<ModuleEntry*>* r) {
128     _reads = r;
129     DEBUG_ONLY(_reads_is_archived = false);
130   }
131   Array<ModuleEntry*>* archived_reads() const {
132     assert(_reads_is_archived, "sanity");
133     return _archived_reads;
134   }
135   void set_archived_reads(Array<ModuleEntry*>* r) {
136     _archived_reads = r;
137     DEBUG_ONLY(_reads_is_archived = true);
138   }
139   void             add_read(ModuleEntry* m);
140   void             set_read_walk_required(ClassLoaderData* m_loader_data);
141 
142   bool             is_open() const                     { return _is_open; }
143   void             set_is_open(bool is_open);
144 
145   bool             is_named() const                    { return (_name != nullptr); }
146 
147   bool can_read_all_unnamed() const {
148     assert(is_named() || _can_read_all_unnamed == true,
149            "unnamed modules can always read all unnamed modules");
150     return _can_read_all_unnamed;
151   }
152 
153   // Modules can only go from strict to loose.
154   void set_can_read_all_unnamed() { _can_read_all_unnamed = true; }
155 
156   bool has_default_read_edges() const {
157     return _has_default_read_edges;
158   }
159 
160   // Sets true and returns the previous value.
161   bool set_has_default_read_edges() {
162     MutexLocker ml(Module_lock);
163     bool prev = _has_default_read_edges;
164     _has_default_read_edges = true;
165     return prev;
166   }
167 
168   void set_is_patched() {
169       _is_patched = true;
170       CDS_ONLY(_shared_path_index = -1); // Mark all shared classes in this module invisible.
171   }
172   bool is_patched() {
173       return _is_patched;
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 
188   // Note caller requires ResourceMark
189   const char* name_as_C_string() {
190     return is_named() ? name()->as_C_string() : UNNAMED_MODULE;
191   }
192   void print(outputStream* st = tty);
193   void verify();
194 
195   CDS_ONLY(int shared_path_index() { return _shared_path_index;})
196 
197   JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
198 
199 #if INCLUDE_CDS_JAVA_HEAP
200   void iterate_symbols(MetaspaceClosure* closure);
201   ModuleEntry* allocate_archived_entry() const;
202   void init_as_archived_entry();
203   static ModuleEntry* get_archived_entry(ModuleEntry* orig_entry);
204   bool has_been_archived();
205   static Array<ModuleEntry*>* write_growable_array(ModuleEntry* module, GrowableArray<ModuleEntry*>* array);
206   static GrowableArray<ModuleEntry*>* restore_growable_array(Array<ModuleEntry*>* archived_array);
207   void load_from_archive(ClassLoaderData* loader_data);
208   void restore_archived_oops(ClassLoaderData* loader_data);
209   void clear_archived_oops();
210   void update_oops_in_archived_module(int root_oop_index);
211   static void verify_archived_module_entries() PRODUCT_RETURN;
212 #endif
213 };
214 
215 // Iterator interface
216 class ModuleClosure: public StackObj {
217  public:
218   virtual void do_module(ModuleEntry* module) = 0;
219 };
220 
221 
222 // The ModuleEntryTable is a Hashtable containing a list of all modules defined
223 // by a particular class loader.  Each module is represented as a ModuleEntry node.
224 //
225 // Each ModuleEntryTable contains a _javabase_module field which allows for the
226 // creation of java.base's ModuleEntry very early in bootstrapping before the
227 // corresponding JVM_DefineModule call for java.base occurs during module system
228 // initialization.  Setting up java.base's ModuleEntry early enables classes,
229 // loaded prior to the module system being initialized to be created with their
230 // PackageEntry node's correctly pointing at java.base's ModuleEntry.  No class
231 // outside of java.base is allowed to be loaded pre-module system initialization.
232 //
233 class ModuleEntryTable : public CHeapObj<mtModule> {
234 private:
235   static ModuleEntry* _javabase_module;
236   ResourceHashtable<SymbolHandle, ModuleEntry*, 109, AnyObj::C_HEAP, mtModule,
237                     SymbolHandle::compute_hash> _table;
238 
239 public:
240   ModuleEntryTable();
241   ~ModuleEntryTable();
242 
243   // Create module in loader's module entry table.  Assume Module_lock
244   // has been locked by caller.
245   ModuleEntry* locked_create_entry(Handle module_handle,
246                                    bool is_open,
247                                    Symbol* module_name,
248                                    Symbol* module_version,
249                                    Symbol* module_location,
250                                    ClassLoaderData* loader_data);
251 
252   // Only lookup module within loader's module entry table.
253   ModuleEntry* lookup_only(Symbol* name);
254 
255   // purge dead weak references out of reads list
256   void purge_all_module_reads();
257 
258   // Special handling for java.base
259   static ModuleEntry* javabase_moduleEntry()                   { return _javabase_module; }
260   static void set_javabase_moduleEntry(ModuleEntry* java_base) {
261     assert(_javabase_module == nullptr, "_javabase_module is already defined");
262     _javabase_module = java_base;
263   }
264 
265   static bool javabase_defined() { return ((_javabase_module != nullptr) &&
266                                            (_javabase_module->module() != nullptr)); }
267   static void finalize_javabase(Handle module_handle, Symbol* version, Symbol* location);
268   static void patch_javabase_entries(JavaThread* current, Handle module_handle);
269 
270   void modules_do(void f(ModuleEntry*));
271   void modules_do(ModuleClosure* closure);
272 
273   void print(outputStream* st = tty);
274   void verify();
275 
276 #if INCLUDE_CDS_JAVA_HEAP
277   void iterate_symbols(MetaspaceClosure* closure);
278   Array<ModuleEntry*>* allocate_archived_entries();
279   void init_archived_entries(Array<ModuleEntry*>* archived_modules);
280   void load_archived_entries(ClassLoaderData* loader_data,
281                              Array<ModuleEntry*>* archived_modules);
282   void restore_archived_oops(ClassLoaderData* loader_data,
283                              Array<ModuleEntry*>* archived_modules);
284 #endif
285 };
286 
287 #endif // SHARE_CLASSFILE_MODULEENTRY_HPP