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