1 /*
  2  * Copyright (c) 2016, 2026, 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 #include "cds/aotClassLocation.hpp"
 26 #include "cds/aotGrowableArray.inline.hpp"
 27 #include "cds/archiveBuilder.hpp"
 28 #include "cds/archiveUtils.hpp"
 29 #include "cds/cdsConfig.hpp"
 30 #include "cds/heapShared.hpp"
 31 #include "classfile/classLoader.hpp"
 32 #include "classfile/classLoaderData.inline.hpp"
 33 #include "classfile/classLoaderDataShared.hpp"
 34 #include "classfile/javaClasses.inline.hpp"
 35 #include "classfile/moduleEntry.hpp"
 36 #include "classfile/systemDictionary.hpp"
 37 #include "classfile/systemDictionaryShared.hpp"
 38 #include "jni.h"
 39 #include "logging/log.hpp"
 40 #include "logging/logStream.hpp"
 41 #include "memory/metadataFactory.hpp"
 42 #include "memory/resourceArea.hpp"
 43 #include "memory/universe.hpp"
 44 #include "oops/oopHandle.inline.hpp"
 45 #include "oops/symbol.hpp"
 46 #include "runtime/handles.inline.hpp"
 47 #include "runtime/safepoint.hpp"
 48 #include "utilities/events.hpp"
 49 #include "utilities/hashTable.hpp"
 50 #include "utilities/ostream.hpp"
 51 #include "utilities/quickSort.hpp"
 52 
 53 ModuleEntry* ModuleEntryTable::_javabase_module = nullptr;
 54 
 55 oop ModuleEntry::module_oop() const { return _module_handle.resolve(); }
 56 
 57 void ModuleEntry::set_location(Symbol* location) {
 58   // _location symbol's refcounts are managed by ModuleEntry,
 59   // must decrement the old one before updating.
 60   Symbol::maybe_decrement_refcount(_location);
 61 
 62   _location = location;
 63 
 64   if (location != nullptr) {
 65     location->increment_refcount();
 66     CDS_ONLY(if (CDSConfig::is_using_archive()) {
 67         _shared_path_index = AOTClassLocationConfig::runtime()->get_module_shared_path_index(_location);
 68       });
 69   }
 70 }
 71 
 72 // Return true if the module's version should be displayed in error messages,
 73 // logging, etc.
 74 // Return false if the module's version is null, if it is unnamed, or if the
 75 // module is not an upgradeable module.
 76 // Detect if the module is not upgradeable by checking:
 77 //     1. Module location is "jrt:/java." and its loader is boot or platform
 78 //     2. Module location is "jrt:/jdk.", its loader is one of the builtin loaders
 79 //        and its version is the same as module java.base's version
 80 // The above check is imprecise but should work in almost all cases.
 81 bool ModuleEntry::should_show_version() {
 82   if (version() == nullptr || !is_named()) return false;
 83 
 84   if (location() != nullptr) {
 85     ResourceMark rm;
 86     const char* loc = location()->as_C_string();
 87     ClassLoaderData* cld = loader_data();
 88 
 89     assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder");
 90     if ((cld->is_the_null_class_loader_data() || cld->is_platform_class_loader_data()) &&
 91         (strncmp(loc, "jrt:/java.", 10) == 0)) {
 92       return false;
 93     }
 94     if ((ModuleEntryTable::javabase_moduleEntry()->version()->fast_compare(version()) == 0) &&
 95         cld->is_permanent_class_loader_data() && (strncmp(loc, "jrt:/jdk.", 9) == 0)) {
 96       return false;
 97     }
 98   }
 99   return true;
100 }
101 
102 void ModuleEntry::set_version(Symbol* version) {
103   // _version symbol's refcounts are managed by ModuleEntry,
104   // must decrement the old one before updating.
105   Symbol::maybe_decrement_refcount(_version);
106 
107   _version = version;
108 
109   Symbol::maybe_increment_refcount(version);
110 }
111 
112 // Returns the shared ProtectionDomain
113 oop ModuleEntry::shared_protection_domain() {
114   return _shared_pd.resolve();
115 }
116 
117 // Set the shared ProtectionDomain atomically
118 void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data,
119                                                Handle pd_h) {
120   // Create a handle for the shared ProtectionDomain and save it atomically.
121   // init_handle_locked checks if someone beats us setting the _shared_pd cache.
122   loader_data->init_handle_locked(_shared_pd, pd_h);
123 }
124 
125 // Returns true if this module can read module m
126 bool ModuleEntry::can_read(ModuleEntry* m) const {
127   assert(m != nullptr, "No module to lookup in this module's reads list");
128 
129   // Unnamed modules read everyone and all modules
130   // read java.base.  If either of these conditions
131   // hold, readability has been established.
132   if (!this->is_named() ||
133       (m == ModuleEntryTable::javabase_moduleEntry())) {
134     return true;
135   }
136 
137   MutexLocker m1(Module_lock);
138   // This is a guard against possible race between agent threads that redefine
139   // or retransform classes in this module. Only one of them is adding the
140   // default read edges to the unnamed modules of the boot and app class loaders
141   // with an upcall to jdk.internal.module.Modules.transformedByAgent.
142   // At the same time, another thread can instrument the module classes by
143   // injecting dependencies that require the default read edges for resolution.
144   if (this->has_default_read_edges() && !m->is_named()) {
145     ClassLoaderData* cld = m->loader_data();
146     assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder");
147     if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) {
148       return true; // default read edge
149     }
150   }
151   if (!has_reads_list()) {
152     return false;
153   } else {
154     return reads()->contains(m);
155   }
156 }
157 
158 // Add a new module to this module's reads list
159 void ModuleEntry::add_read(ModuleEntry* m) {
160   // Unnamed module is special cased and can read all modules
161   if (!is_named()) {
162     return;
163   }
164 
165   MutexLocker m1(Module_lock);
166   if (m == nullptr) {
167     set_can_read_all_unnamed();
168   } else {
169     if (reads() == nullptr) {
170       // Lazily create a module's reads list
171       AOTGrowableArray<ModuleEntry*>* new_reads = new (mtModule) AOTGrowableArray<ModuleEntry*>(MODULE_READS_SIZE, mtModule);
172       set_reads(new_reads);
173     }
174 
175     // Determine, based on this newly established read edge to module m,
176     // if this module's read list should be walked at a GC safepoint.
177     set_read_walk_required(m->loader_data());
178 
179     // Establish readability to module m
180     reads()->append_if_missing(m);
181   }
182 }
183 
184 // If the module's loader, that a read edge is being established to, is
185 // not the same loader as this module's and is not one of the 3 builtin
186 // class loaders, then this module's reads list must be walked at GC
187 // safepoint. Modules have the same life cycle as their defining class
188 // loaders and should be removed if dead.
189 void ModuleEntry::set_read_walk_required(ClassLoaderData* m_loader_data) {
190   assert(is_named(), "Cannot call set_read_walk_required on unnamed module");
191   assert_locked_or_safepoint(Module_lock);
192   if (!_must_walk_reads &&
193       loader_data() != m_loader_data &&
194       !m_loader_data->is_builtin_class_loader_data()) {
195     _must_walk_reads = true;
196     if (log_is_enabled(Trace, module)) {
197       ResourceMark rm;
198       log_trace(module)("ModuleEntry::set_read_walk_required(): module %s reads list must be walked",
199                         (name() != nullptr) ? name()->as_C_string() : UNNAMED_MODULE);
200     }
201   }
202 }
203 
204 // Set whether the module is open, i.e. all its packages are unqualifiedly exported
205 void ModuleEntry::set_is_open(bool is_open) {
206   assert_lock_strong(Module_lock);
207   _is_open = is_open;
208 }
209 
210 // Returns true if the module has a non-empty reads list. As such, the unnamed
211 // module will return false.
212 bool ModuleEntry::has_reads_list() const {
213   assert_locked_or_safepoint(Module_lock);
214   return ((reads() != nullptr) && !reads()->is_empty());
215 }
216 
217 // Purge dead module entries out of reads list.
218 void ModuleEntry::purge_reads() {
219   assert_locked_or_safepoint(Module_lock);
220 
221   if (_must_walk_reads && has_reads_list()) {
222     // This module's _must_walk_reads flag will be reset based
223     // on the remaining live modules on the reads list.
224     _must_walk_reads = false;
225 
226     if (log_is_enabled(Trace, module)) {
227       ResourceMark rm;
228       log_trace(module)("ModuleEntry::purge_reads(): module %s reads list being walked",
229                         (name() != nullptr) ? name()->as_C_string() : UNNAMED_MODULE);
230     }
231 
232     // Go backwards because this removes entries that are dead.
233     int len = reads()->length();
234     for (int idx = len - 1; idx >= 0; idx--) {
235       ModuleEntry* module_idx = reads()->at(idx);
236       ClassLoaderData* cld_idx = module_idx->loader_data();
237       if (cld_idx->is_unloading()) {
238         reads()->delete_at(idx);
239       } else {
240         // Update the need to walk this module's reads based on live modules
241         set_read_walk_required(cld_idx);
242       }
243     }
244   }
245 }
246 
247 void ModuleEntry::module_reads_do(ModuleClosure* f) {
248   assert_locked_or_safepoint(Module_lock);
249   assert(f != nullptr, "invariant");
250 
251   if (has_reads_list()) {
252     int reads_len = reads()->length();
253     for (ModuleEntry* m : *reads()) {
254       f->do_module(m);
255     }
256   }
257 }
258 
259 void ModuleEntry::delete_reads() {
260   delete reads();
261   _reads = nullptr;
262 }
263 
264 ModuleEntry::ModuleEntry(Handle module_handle,
265                          bool is_open, Symbol* name,
266                          Symbol* version, Symbol* location,
267                          ClassLoaderData* loader_data) :
268     _name(name),
269     _loader_data(loader_data),
270     _reads(nullptr),
271     _version(nullptr),
272     _location(nullptr),
273     CDS_ONLY(_shared_path_index(-1) COMMA)
274     _can_read_all_unnamed(false),
275     _has_default_read_edges(false),
276     _must_walk_reads(false),
277     _is_open(is_open),
278     _is_patched(false) {
279 
280   // Initialize fields specific to a ModuleEntry
281   if (_name == nullptr) {
282     // Unnamed modules can read all other unnamed modules.
283     set_can_read_all_unnamed();
284   } else {
285     _name->increment_refcount();
286   }
287 
288   if (!module_handle.is_null()) {
289     _module_handle = loader_data->add_handle(module_handle);
290   }
291 
292   set_version(version);
293 
294   // may need to add CDS info
295   set_location(location);
296 
297   if (name != nullptr && ClassLoader::is_in_patch_mod_entries(name)) {
298     set_is_patched();
299     if (log_is_enabled(Trace, module, patch)) {
300       ResourceMark rm;
301       log_trace(module, patch)("Marked module %s as patched from --patch-module",
302                                name != nullptr ? name->as_C_string() : UNNAMED_MODULE);
303     }
304   }
305 
306   JFR_ONLY(INIT_ID(this);)
307 }
308 
309 ModuleEntry::~ModuleEntry() {
310   // Clean out the C heap allocated reads list first before freeing the entry
311   delete_reads();
312   Symbol::maybe_decrement_refcount(_name);
313   Symbol::maybe_decrement_refcount(_version);
314   Symbol::maybe_decrement_refcount(_location);
315 }
316 
317 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) {
318   // The java.lang.Module for this loader's
319   // corresponding unnamed module can be found in the java.lang.ClassLoader object.
320   oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader());
321 
322 #if INCLUDE_CDS_JAVA_HEAP
323   ModuleEntry* archived_unnamed_module = ClassLoaderDataShared::archived_unnamed_module(cld);
324   if (archived_unnamed_module != nullptr) {
325     archived_unnamed_module->load_from_archive(cld);
326     archived_unnamed_module->restore_archived_oops(cld);
327     return archived_unnamed_module;
328   }
329 #endif
330 
331   // Ensure that the unnamed module was correctly set when the class loader was constructed.
332   // Guarantee will cause a recognizable crash if the user code has circumvented calling the ClassLoader constructor.
333   ResourceMark rm;
334   guarantee(java_lang_Module::is_instance(module),
335             "The unnamed module for ClassLoader %s, is null or not an instance of java.lang.Module. The class loader has not been initialized correctly.",
336             cld->loader_name_and_id());
337 
338   ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld);
339 
340   // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
341   java_lang_Module::set_module_entry(module, unnamed_module);
342 
343   return unnamed_module;
344 }
345 
346 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) {
347 #if INCLUDE_CDS_JAVA_HEAP
348   ModuleEntry* archived_unnamed_module = ClassLoaderDataShared::archived_boot_unnamed_module();
349   if (archived_unnamed_module != nullptr) {
350     archived_unnamed_module->load_from_archive(cld);
351     // It's too early to call archived_unnamed_module->restore_archived_oops(cld).
352     // We will do it inside Modules::set_bootloader_unnamed_module()
353     return archived_unnamed_module;
354   }
355 #endif
356 
357   // For the boot loader, the java.lang.Module for the unnamed module
358   // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At
359   // this point initially create the ModuleEntry for the unnamed module.
360   ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld);
361   assert(unnamed_module != nullptr, "boot loader unnamed module should not be null");
362   return unnamed_module;
363 }
364 
365 // When creating an unnamed module, this is called without holding the Module_lock.
366 // This is okay because the unnamed module gets created before the ClassLoaderData
367 // is available to other threads.
368 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) {
369   ModuleEntry* entry = new ModuleEntry(module_handle, /*is_open*/true, /*name*/nullptr,
370                                        /*version*/ nullptr, /*location*/ nullptr,
371                                        cld);
372   // Unnamed modules can read all other unnamed modules.
373   assert(entry->can_read_all_unnamed(), "constructor set that");
374   return entry;
375 }
376 
377 ModuleEntryTable::ModuleEntryTable() { }
378 
379 ModuleEntryTable::~ModuleEntryTable() {
380   class ModuleEntryTableDeleter : public StackObj {
381    public:
382     bool do_entry(const SymbolHandle& name, ModuleEntry*& entry) {
383       if (log_is_enabled(Info, module, unload) || log_is_enabled(Debug, module)) {
384         ResourceMark rm;
385         const char* str = name->as_C_string();
386         log_info(module, unload)("unloading module %s", str);
387         log_debug(module)("ModuleEntryTable: deleting module: %s", str);
388       }
389       delete entry;
390       return true;
391     }
392   };
393 
394   ModuleEntryTableDeleter deleter;
395   _table.unlink(&deleter);
396   assert(_table.number_of_entries() == 0, "should have removed all entries");
397 }
398 
399 void ModuleEntry::set_loader_data(ClassLoaderData* cld) {
400   assert(!cld->has_class_mirror_holder(), "Unexpected has_class_mirror_holder cld");
401   _loader_data = cld;
402 }
403 
404 void ModuleEntry::metaspace_pointers_do(MetaspaceClosure* it) {
405   it->push(&_name);
406   it->push(&_reads);
407   it->push(&_version);
408   it->push(&_location);
409 }
410 
411 #if INCLUDE_CDS_JAVA_HEAP
412 bool ModuleEntry::should_be_archived() const {
413   return SystemDictionaryShared::is_builtin_loader(loader_data());
414 }
415 
416 void ModuleEntry::remove_unshareable_info() {
417   _archived_module_index = HeapShared::append_root(module_oop());
418 
419   if (_reads != nullptr) {
420     _reads->set_in_aot_cache();
421   }
422 
423   // Clear handles and restore at run time. Handles cannot be archived.
424   if (CDSConfig::is_dumping_final_static_archive()) {
425     OopHandle null_handle;
426     _shared_pd = null_handle;
427   } else {
428     assert(shared_protection_domain() == nullptr, "never set during -Xshare:dump");
429   }
430 
431   OopHandle null_handle;
432   _module_handle = null_handle;
433 
434   _loader_data = nullptr;  // re-init at runtime
435   if (name() != nullptr) {
436     Symbol* src_location = ArchiveBuilder::current()->get_source_addr(_location);
437     _shared_path_index = AOTClassLocationConfig::dumptime()->get_module_shared_path_index(src_location);
438   } else {
439     // _shared_path_index is used only by SystemDictionary::is_shared_class_visible_impl()
440     // for checking classes in named modules.
441     _shared_path_index = -1;
442   }
443   JFR_ONLY(set_trace_id(0);) // re-init at runtime
444 }
445 
446 void ModuleEntry::load_from_archive(ClassLoaderData* loader_data) {
447   assert(CDSConfig::is_using_archive(), "runtime only");
448   set_loader_data(loader_data);
449   JFR_ONLY(INIT_ID(this);)
450 }
451 
452 void ModuleEntry::preload_archived_oops() {
453   (void)HeapShared::get_root(_archived_module_index, false /* clear */);
454 }
455 
456 void ModuleEntry::restore_archived_oops(ClassLoaderData* loader_data) {
457   assert(CDSConfig::is_using_archive(), "runtime only");
458   Handle module_handle(Thread::current(), HeapShared::get_root(_archived_module_index, /*clear=*/true));
459   assert(module_handle.not_null(), "huh");
460   set_module_handle(loader_data->add_handle(module_handle));
461 
462   // This was cleared to zero during dump time -- we didn't save the value
463   // because it may be affected by archive relocation.
464   java_lang_Module::set_module_entry(module_handle(), this);
465 
466   assert(java_lang_Module::loader(module_handle()) == loader_data->class_loader(),
467          "must be set in dump time");
468 
469   if (log_is_enabled(Info, aot, module)) {
470     ResourceMark rm;
471     LogStream ls(Log(aot, module)::info());
472     ls.print("Restored from archive: ");
473     print(&ls);
474   }
475 }
476 
477 void ModuleEntry::clear_archived_oops() {
478   assert(CDSConfig::is_using_archive(), "runtime only");
479   HeapShared::clear_root(_archived_module_index);
480 }
481 
482 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) {
483   assert(a == b || a->name() != b->name(), "no duplicated names");
484   return a->name()->fast_compare(b->name());
485 }
486 
487 Array<ModuleEntry*>* ModuleEntryTable::build_aot_table(ClassLoaderData* loader_data, TRAPS) {
488   Array<ModuleEntry*>* aot_table =
489     MetadataFactory::new_array<ModuleEntry*>(loader_data, _table.number_of_entries(), nullptr, CHECK_NULL);
490   int n = 0;
491   auto grab = [&] (const SymbolHandle& key, ModuleEntry*& m) {
492     m->pack_reads();
493     aot_table->at_put(n++, m);
494     if (log_is_enabled(Info, aot, module)) {
495       ResourceMark rm;
496       LogStream ls(Log(aot, module)::info());
497       ls.print("Stored in archive: ");
498       m->print(&ls);
499     }
500   };
501   _table.iterate_all(grab);
502 
503   if (n > 1) {
504     // Always allocate in the same order to produce deterministic archive.
505     QuickSort::sort(aot_table->data(), n, compare_module_by_name);
506   }
507 
508   return aot_table;
509 }
510 
511 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data,
512                                              Array<ModuleEntry*>* archived_modules) {
513   assert(CDSConfig::is_using_archive(), "runtime only");
514 
515   for (int i = 0; i < archived_modules->length(); i++) {
516     ModuleEntry* archived_entry = archived_modules->at(i);
517     archived_entry->load_from_archive(loader_data);
518     _table.put(archived_entry->name(), archived_entry);
519   }
520 }
521 
522 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) {
523   assert(CDSConfig::is_using_archive(), "runtime only");
524   for (int i = 0; i < archived_modules->length(); i++) {
525     ModuleEntry* archived_entry = archived_modules->at(i);
526     archived_entry->restore_archived_oops(loader_data);
527   }
528 }
529 #endif // INCLUDE_CDS_JAVA_HEAP
530 
531 // Create an entry in the class loader's module_entry_table.  It is the
532 // caller's responsibility to ensure that the entry has not already been
533 // created.
534 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle,
535                                                    bool is_open,
536                                                    Symbol* module_name,
537                                                    Symbol* module_version,
538                                                    Symbol* module_location,
539                                                    ClassLoaderData* loader_data) {
540   assert(module_name != nullptr, "ModuleEntryTable locked_create_entry should never be called for unnamed module.");
541   assert(Module_lock->owned_by_self(), "should have the Module_lock");
542   assert(lookup_only(module_name) == nullptr, "Module already exists");
543   ModuleEntry* entry = new ModuleEntry(module_handle, is_open, module_name,
544                                        module_version, module_location, loader_data);
545   bool created = _table.put(module_name, entry);
546   assert(created, "should be");
547   return entry;
548 }
549 
550 // lookup_only by Symbol* to find a ModuleEntry.
551 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) {
552   assert_locked_or_safepoint(Module_lock);
553   assert(name != nullptr, "name cannot be nullptr");
554   ModuleEntry** entry = _table.get(name);
555   return (entry == nullptr) ? nullptr : *entry;
556 }
557 
558 // Remove dead modules from all other alive modules' reads list.
559 // This should only occur at class unloading.
560 void ModuleEntryTable::purge_all_module_reads() {
561   assert_locked_or_safepoint(Module_lock);
562   auto purge = [&] (const SymbolHandle& key, ModuleEntry*& entry) {
563     entry->purge_reads();
564   };
565   _table.iterate_all(purge);
566 }
567 
568 void ModuleEntryTable::finalize_javabase(Handle module_handle, Symbol* version, Symbol* location) {
569   assert(Module_lock->owned_by_self(), "should have the Module_lock");
570   ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
571   ModuleEntryTable* module_table = boot_loader_data->modules();
572 
573   assert(module_table != nullptr, "boot loader's ModuleEntryTable not defined");
574 
575   if (module_handle.is_null()) {
576     fatal("Unable to finalize module definition for " JAVA_BASE_NAME);
577   }
578 
579   // Set java.lang.Module, version and location for java.base
580   ModuleEntry* jb_module = javabase_moduleEntry();
581   assert(jb_module != nullptr, JAVA_BASE_NAME " ModuleEntry not defined");
582   jb_module->set_version(version);
583   jb_module->set_location(location);
584   // Once java.base's ModuleEntry _module field is set with the known
585   // java.lang.Module, java.base is considered "defined" to the VM.
586   jb_module->set_module_handle(boot_loader_data->add_handle(module_handle));
587 
588   // Store pointer to the ModuleEntry for java.base in the java.lang.Module object.
589   java_lang_Module::set_module_entry(module_handle(), jb_module);
590 }
591 
592 // Within java.lang.Class instances there is a java.lang.Module field that must
593 // be set with the defining module.  During startup, prior to java.base's definition,
594 // classes needing their module field set are added to the fixup_module_list.
595 // Their module field is set once java.base's java.lang.Module is known to the VM.
596 void ModuleEntryTable::patch_javabase_entries(JavaThread* current, Handle module_handle) {
597   assert(!CDSConfig::is_using_aot_linked_classes(), "patching is not necessary with AOT-linked classes");
598 
599   if (module_handle.is_null()) {
600     fatal("Unable to patch the module field of classes loaded prior to "
601           JAVA_BASE_NAME "'s definition, invalid java.lang.Module");
602   }
603 
604   // Do the fixups for the basic primitive types
605   java_lang_Class::set_module(Universe::int_mirror(), module_handle());
606   java_lang_Class::set_module(Universe::float_mirror(), module_handle());
607   java_lang_Class::set_module(Universe::double_mirror(), module_handle());
608   java_lang_Class::set_module(Universe::byte_mirror(), module_handle());
609   java_lang_Class::set_module(Universe::bool_mirror(), module_handle());
610   java_lang_Class::set_module(Universe::char_mirror(), module_handle());
611   java_lang_Class::set_module(Universe::long_mirror(), module_handle());
612   java_lang_Class::set_module(Universe::short_mirror(), module_handle());
613   java_lang_Class::set_module(Universe::void_mirror(), module_handle());
614 
615   // Do the fixups for classes that have already been created.
616   GrowableArray <Klass*>* list = java_lang_Class::fixup_module_field_list();
617   int list_length = list->length();
618   for (int i = 0; i < list_length; i++) {
619     Klass* k = list->at(i);
620     assert(k->is_klass(), "List should only hold classes");
621 #ifndef PRODUCT
622     if (HeapShared::is_a_test_class_in_unnamed_module(k)) {
623       // We allow -XX:ArchiveHeapTestClass to archive additional classes
624       // into the CDS heap, but these must be in the unnamed module.
625       ModuleEntry* unnamed_module = ClassLoaderData::the_null_class_loader_data()->unnamed_module();
626       Handle unnamed_module_handle(current, unnamed_module->module_oop());
627       java_lang_Class::fixup_module_field(k, unnamed_module_handle);
628     } else
629 #endif
630     {
631       java_lang_Class::fixup_module_field(k, module_handle);
632     }
633     k->class_loader_data()->dec_keep_alive_ref_count();
634   }
635 
636   delete java_lang_Class::fixup_module_field_list();
637   java_lang_Class::set_fixup_module_field_list(nullptr);
638 }
639 
640 void ModuleEntryTable::print(outputStream* st) {
641   ResourceMark rm;
642   auto printer = [&] (const SymbolHandle& name, ModuleEntry*& entry) {
643     entry->print(st);
644   };
645   st->print_cr("Module Entry Table (table_size=%d, entries=%d)",
646                _table.table_size(), _table.number_of_entries());
647   assert_locked_or_safepoint(Module_lock);
648   _table.iterate_all(printer);
649 }
650 
651 void ModuleEntryTable::modules_do(void f(ModuleEntry*)) {
652   auto do_f = [&] (const SymbolHandle& key, ModuleEntry*& entry) {
653     f(entry);
654   };
655   assert_lock_strong(Module_lock);
656   _table.iterate_all(do_f);
657 }
658 
659 void ModuleEntryTable::modules_do(ModuleClosure* closure) {
660   auto do_f = [&] (const SymbolHandle& key, ModuleEntry*& entry) {
661     closure->do_module(entry);
662   };
663   assert_lock_strong(Module_lock);
664   _table.iterate_all(do_f);
665 }
666 
667 void ModuleEntry::print(outputStream* st) const {
668   st->print_cr("entry " PTR_FORMAT " name %s module " PTR_FORMAT " loader %s version %s location %s strict %s",
669                p2i(this),
670                name_as_C_string(),
671                p2i(module_oop()),
672                loader_data()->loader_name_and_id(),
673                version() != nullptr ? version()->as_C_string() : "nullptr",
674                location() != nullptr ? location()->as_C_string() : "nullptr",
675                BOOL_TO_STR(!can_read_all_unnamed()));
676 }
677 
678 void ModuleEntryTable::verify() {
679   auto do_f = [&] (const SymbolHandle& key, ModuleEntry*& entry) {
680     entry->verify();
681   };
682   assert_locked_or_safepoint(Module_lock);
683   _table.iterate_all(do_f);
684 }
685 
686 void ModuleEntry::verify() {
687   guarantee(loader_data() != nullptr, "A module entry must be associated with a loader.");
688 }