1 /*
2 * Copyright (c) 2016, 2021, 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 "precompiled.hpp"
26 #include "jni.h"
27 #include "cds/archiveBuilder.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/filemap.hpp"
30 #include "cds/heapShared.hpp"
31 #include "classfile/classLoader.hpp"
32 #include "classfile/classLoaderData.inline.hpp"
33 #include "classfile/javaClasses.inline.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "logging/log.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "memory/universe.hpp"
38 #include "oops/oopHandle.inline.hpp"
39 #include "oops/symbol.hpp"
40 #include "runtime/handles.inline.hpp"
41 #include "runtime/safepoint.hpp"
42 #include "utilities/events.hpp"
43 #include "utilities/growableArray.hpp"
44 #include "utilities/hashtable.inline.hpp"
45 #include "utilities/ostream.hpp"
46 #include "utilities/quickSort.hpp"
47 #include "utilities/resourceHash.hpp"
48
49 ModuleEntry* ModuleEntryTable::_javabase_module = NULL;
50
module() const51 oop ModuleEntry::module() const { return _module.resolve(); }
52
set_location(Symbol * location)53 void ModuleEntry::set_location(Symbol* location) {
54 if (_location != NULL) {
55 // _location symbol's refcounts are managed by ModuleEntry,
56 // must decrement the old one before updating.
57 _location->decrement_refcount();
58 }
59
60 _location = location;
61
62 if (location != NULL) {
63 location->increment_refcount();
64 CDS_ONLY(if (UseSharedSpaces) {
65 _shared_path_index = FileMapInfo::get_module_shared_path_index(location);
66 });
67 }
68 }
69
70 // Return true if the module's version should be displayed in error messages,
71 // logging, etc.
72 // Return false if the module's version is null, if it is unnamed, or if the
73 // module is not an upgradeable module.
74 // Detect if the module is not upgradeable by checking:
75 // 1. Module location is "jrt:/java." and its loader is boot or platform
76 // 2. Module location is "jrt:/jdk.", its loader is one of the builtin loaders
77 // and its version is the same as module java.base's version
78 // The above check is imprecise but should work in almost all cases.
should_show_version()79 bool ModuleEntry::should_show_version() {
80 if (version() == NULL || !is_named()) return false;
81
82 if (location() != NULL) {
83 ResourceMark rm;
84 const char* loc = location()->as_C_string();
85 ClassLoaderData* cld = loader_data();
86
87 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder");
88 if ((cld->is_the_null_class_loader_data() || cld->is_platform_class_loader_data()) &&
89 (strncmp(loc, "jrt:/java.", 10) == 0)) {
90 return false;
91 }
92 if ((ModuleEntryTable::javabase_moduleEntry()->version()->fast_compare(version()) == 0) &&
93 cld->is_permanent_class_loader_data() && (strncmp(loc, "jrt:/jdk.", 9) == 0)) {
94 return false;
95 }
96 }
97 return true;
98 }
99
set_version(Symbol * version)100 void ModuleEntry::set_version(Symbol* version) {
101 if (_version != NULL) {
102 // _version symbol's refcounts are managed by ModuleEntry,
103 // must decrement the old one before updating.
104 _version->decrement_refcount();
105 }
106
107 _version = version;
108
109 if (version != NULL) {
110 version->increment_refcount();
111 }
112 }
113
114 // Returns the shared ProtectionDomain
shared_protection_domain()115 oop ModuleEntry::shared_protection_domain() {
116 return _shared_pd.resolve();
117 }
118
119 // Set the shared ProtectionDomain atomically
set_shared_protection_domain(ClassLoaderData * loader_data,Handle pd_h)120 void ModuleEntry::set_shared_protection_domain(ClassLoaderData *loader_data,
121 Handle pd_h) {
122 // Create a handle for the shared ProtectionDomain and save it atomically.
123 // init_handle_locked checks if someone beats us setting the _shared_pd cache.
124 loader_data->init_handle_locked(_shared_pd, pd_h);
125 }
126
127 // Returns true if this module can read module m
can_read(ModuleEntry * m) const128 bool ModuleEntry::can_read(ModuleEntry* m) const {
129 assert(m != NULL, "No module to lookup in this module's reads list");
130
131 // Unnamed modules read everyone and all modules
132 // read java.base. If either of these conditions
133 // hold, readability has been established.
134 if (!this->is_named() ||
135 (m == ModuleEntryTable::javabase_moduleEntry())) {
136 return true;
137 }
138
139 MutexLocker m1(Module_lock);
140 // This is a guard against possible race between agent threads that redefine
141 // or retransform classes in this module. Only one of them is adding the
142 // default read edges to the unnamed modules of the boot and app class loaders
143 // with an upcall to jdk.internal.module.Modules.transformedByAgent.
144 // At the same time, another thread can instrument the module classes by
145 // injecting dependencies that require the default read edges for resolution.
146 if (this->has_default_read_edges() && !m->is_named()) {
147 ClassLoaderData* cld = m->loader_data();
148 assert(!cld->has_class_mirror_holder(), "module's cld should have a ClassLoader holder not a Class holder");
149 if (cld->is_the_null_class_loader_data() || cld->is_system_class_loader_data()) {
150 return true; // default read edge
151 }
152 }
153 if (!has_reads_list()) {
154 return false;
155 } else {
156 return _reads->contains(m);
157 }
158 }
159
160 // Add a new module to this module's reads list
add_read(ModuleEntry * m)161 void ModuleEntry::add_read(ModuleEntry* m) {
162 // Unnamed module is special cased and can read all modules
163 if (!is_named()) {
164 return;
165 }
166
167 MutexLocker m1(Module_lock);
168 if (m == NULL) {
169 set_can_read_all_unnamed();
170 } else {
171 if (_reads == NULL) {
172 // Lazily create a module's reads list
173 _reads = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleEntry*>(MODULE_READS_SIZE, mtModule);
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.
set_read_walk_required(ClassLoaderData * m_loader_data)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() != NULL) ? 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
set_is_open(bool is_open)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.
has_reads_list() const213 bool ModuleEntry::has_reads_list() const {
214 assert_locked_or_safepoint(Module_lock);
215 return ((_reads != NULL) && !_reads->is_empty());
216 }
217
218 // Purge dead module entries out of reads list.
purge_reads()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() != NULL) ? 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
module_reads_do(ModuleClosure * f)248 void ModuleEntry::module_reads_do(ModuleClosure* f) {
249 assert_locked_or_safepoint(Module_lock);
250 assert(f != NULL, "invariant");
251
252 if (has_reads_list()) {
253 int reads_len = _reads->length();
254 for (int i = 0; i < reads_len; ++i) {
255 f->do_module(_reads->at(i));
256 }
257 }
258 }
259
delete_reads()260 void ModuleEntry::delete_reads() {
261 delete _reads;
262 _reads = NULL;
263 }
264
create_unnamed_module(ClassLoaderData * cld)265 ModuleEntry* ModuleEntry::create_unnamed_module(ClassLoaderData* cld) {
266 // The java.lang.Module for this loader's
267 // corresponding unnamed module can be found in the java.lang.ClassLoader object.
268 oop module = java_lang_ClassLoader::unnamedModule(cld->class_loader());
269
270 // Ensure that the unnamed module was correctly set when the class loader was constructed.
271 // Guarantee will cause a recognizable crash if the user code has circumvented calling the ClassLoader constructor.
272 ResourceMark rm;
273 guarantee(java_lang_Module::is_instance(module),
274 "The unnamed module for ClassLoader %s, is null or not an instance of java.lang.Module. The class loader has not been initialized correctly.",
275 cld->loader_name_and_id());
276
277 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(Thread::current(), module), cld);
278
279 // Store pointer to the ModuleEntry in the unnamed module's java.lang.Module object.
280 java_lang_Module::set_module_entry(module, unnamed_module);
281
282 return unnamed_module;
283 }
284
create_boot_unnamed_module(ClassLoaderData * cld)285 ModuleEntry* ModuleEntry::create_boot_unnamed_module(ClassLoaderData* cld) {
286 // For the boot loader, the java.lang.Module for the unnamed module
287 // is not known until a call to JVM_SetBootLoaderUnnamedModule is made. At
288 // this point initially create the ModuleEntry for the unnamed module.
289 ModuleEntry* unnamed_module = new_unnamed_module_entry(Handle(), cld);
290 assert(unnamed_module != NULL, "boot loader unnamed module should not be null");
291 return unnamed_module;
292 }
293
294 // When creating an unnamed module, this is called without holding the Module_lock.
295 // This is okay because the unnamed module gets created before the ClassLoaderData
296 // is available to other threads.
new_unnamed_module_entry(Handle module_handle,ClassLoaderData * cld)297 ModuleEntry* ModuleEntry::new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld) {
298 ModuleEntry* entry = NEW_C_HEAP_OBJ(ModuleEntry, mtModule);
299
300 // Initialize everything BasicHashtable would
301 entry->set_next(NULL);
302 entry->set_hash(0);
303 entry->set_literal(NULL);
304
305 // Initialize fields specific to a ModuleEntry
306 entry->init();
307
308 // Unnamed modules can read all other unnamed modules.
309 entry->set_can_read_all_unnamed();
310
311 if (!module_handle.is_null()) {
312 entry->set_module(cld->add_handle(module_handle));
313 }
314
315 entry->set_loader_data(cld);
316 entry->_is_open = true;
317
318 JFR_ONLY(INIT_ID(entry);)
319
320 return entry;
321 }
322
delete_unnamed_module()323 void ModuleEntry::delete_unnamed_module() {
324 // Do not need unlink_entry() since the unnamed module is not in the hashtable
325 FREE_C_HEAP_OBJ(this);
326 }
327
ModuleEntryTable(int table_size)328 ModuleEntryTable::ModuleEntryTable(int table_size)
329 : Hashtable<Symbol*, mtModule>(table_size, sizeof(ModuleEntry))
330 {
331 }
332
~ModuleEntryTable()333 ModuleEntryTable::~ModuleEntryTable() {
334 // Walk through all buckets and all entries in each bucket,
335 // freeing each entry.
336 for (int i = 0; i < table_size(); ++i) {
337 for (ModuleEntry* m = bucket(i); m != NULL;) {
338 ModuleEntry* to_remove = m;
339 // read next before freeing.
340 m = m->next();
341
342 ResourceMark rm;
343 if (to_remove->name() != NULL) {
344 log_info(module, unload)("unloading module %s", to_remove->name()->as_C_string());
345 }
346 log_debug(module)("ModuleEntryTable: deleting module: %s", to_remove->name() != NULL ?
347 to_remove->name()->as_C_string() : UNNAMED_MODULE);
348
349 // Clean out the C heap allocated reads list first before freeing the entry
350 to_remove->delete_reads();
351 if (to_remove->name() != NULL) {
352 to_remove->name()->decrement_refcount();
353 }
354 if (to_remove->version() != NULL) {
355 to_remove->version()->decrement_refcount();
356 }
357 if (to_remove->location() != NULL) {
358 to_remove->location()->decrement_refcount();
359 }
360 BasicHashtable<mtModule>::free_entry(to_remove);
361 }
362 }
363 assert(number_of_entries() == 0, "should have removed all entries");
364 }
365
set_loader_data(ClassLoaderData * cld)366 void ModuleEntry::set_loader_data(ClassLoaderData* cld) {
367 assert(!cld->has_class_mirror_holder(), "Unexpected has_class_mirror_holder cld");
368 _loader_data = cld;
369 }
370
371 #if INCLUDE_CDS_JAVA_HEAP
372 typedef ResourceHashtable<
373 const ModuleEntry*,
374 ModuleEntry*,
375 primitive_hash<const ModuleEntry*>,
376 primitive_equals<const ModuleEntry*>,
377 557, // prime number
378 ResourceObj::C_HEAP> ArchivedModuleEntries;
379 static ArchivedModuleEntries* _archive_modules_entries = NULL;
380
allocate_archived_entry() const381 ModuleEntry* ModuleEntry::allocate_archived_entry() const {
382 assert(is_named(), "unnamed packages/modules are not archived");
383 ModuleEntry* archived_entry = (ModuleEntry*)ArchiveBuilder::rw_region_alloc(sizeof(ModuleEntry));
384 memcpy((void*)archived_entry, (void*)this, sizeof(ModuleEntry));
385
386 if (_archive_modules_entries == NULL) {
387 _archive_modules_entries = new (ResourceObj::C_HEAP, mtClass)ArchivedModuleEntries();
388 }
389 assert(_archive_modules_entries->get(this) == NULL, "Each ModuleEntry must not be shared across ModuleEntryTables");
390 _archive_modules_entries->put(this, archived_entry);
391
392 return archived_entry;
393 }
394
get_archived_entry(ModuleEntry * orig_entry)395 ModuleEntry* ModuleEntry::get_archived_entry(ModuleEntry* orig_entry) {
396 ModuleEntry** ptr = _archive_modules_entries->get(orig_entry);
397 assert(ptr != NULL && *ptr != NULL, "must have been allocated");
398 return *ptr;
399 }
400
401 // This function is used to archive ModuleEntry::_reads and PackageEntry::_qualified_exports.
402 // GrowableArray cannot be directly archived, as it needs to be expandable at runtime.
403 // Write it out as an Array, and convert it back to GrowableArray at runtime.
write_growable_array(GrowableArray<ModuleEntry * > * array)404 Array<ModuleEntry*>* ModuleEntry::write_growable_array(GrowableArray<ModuleEntry*>* array) {
405 Array<ModuleEntry*>* archived_array = NULL;
406 int length = (array == NULL) ? 0 : array->length();
407 if (length > 0) {
408 archived_array = ArchiveBuilder::new_ro_array<ModuleEntry*>(length);
409 for (int i = 0; i < length; i++) {
410 ModuleEntry* archived_entry = get_archived_entry(array->at(i));
411 archived_array->at_put(i, archived_entry);
412 ArchivePtrMarker::mark_pointer((address*)archived_array->adr_at(i));
413 }
414 }
415
416 return archived_array;
417 }
418
restore_growable_array(Array<ModuleEntry * > * archived_array)419 GrowableArray<ModuleEntry*>* ModuleEntry::restore_growable_array(Array<ModuleEntry*>* archived_array) {
420 GrowableArray<ModuleEntry*>* array = NULL;
421 int length = (archived_array == NULL) ? 0 : archived_array->length();
422 if (length > 0) {
423 array = new (ResourceObj::C_HEAP, mtModule)GrowableArray<ModuleEntry*>(length, mtModule);
424 for (int i = 0; i < length; i++) {
425 ModuleEntry* archived_entry = archived_array->at(i);
426 array->append(archived_entry);
427 }
428 }
429
430 return array;
431 }
432
iterate_symbols(MetaspaceClosure * closure)433 void ModuleEntry::iterate_symbols(MetaspaceClosure* closure) {
434 closure->push(literal_addr()); // name
435 closure->push(&_version);
436 closure->push(&_location);
437 }
438
init_as_archived_entry()439 void ModuleEntry::init_as_archived_entry() {
440 Array<ModuleEntry*>* archived_reads = write_growable_array(_reads);
441
442 set_next(NULL);
443 set_hash(0x0); // re-init at runtime
444 _loader_data = NULL; // re-init at runtime
445 _shared_path_index = FileMapInfo::get_module_shared_path_index(_location);
446 if (literal() != NULL) {
447 set_literal(ArchiveBuilder::get_relocated_symbol(literal()));
448 ArchivePtrMarker::mark_pointer((address*)literal_addr());
449 }
450 _reads = (GrowableArray<ModuleEntry*>*)archived_reads;
451 if (_version != NULL) {
452 _version = ArchiveBuilder::get_relocated_symbol(_version);
453 }
454 if (_location != NULL) {
455 _location = ArchiveBuilder::get_relocated_symbol(_location);
456 }
457 JFR_ONLY(set_trace_id(0));// re-init at runtime
458
459 ArchivePtrMarker::mark_pointer((address*)&_reads);
460 ArchivePtrMarker::mark_pointer((address*)&_version);
461 ArchivePtrMarker::mark_pointer((address*)&_location);
462 }
463
init_archived_oops()464 void ModuleEntry::init_archived_oops() {
465 assert(DumpSharedSpaces, "static dump only");
466 oop module_obj = module();
467 if (module_obj != NULL) {
468 oop m = HeapShared::find_archived_heap_object(module_obj);
469 assert(m != NULL, "sanity");
470 _archived_module_index = HeapShared::append_root(m);
471 }
472 assert(shared_protection_domain() == NULL, "never set during -Xshare:dump");
473 // Clear handles and restore at run time. Handles cannot be archived.
474 OopHandle null_handle;
475 _module = null_handle;
476 }
477
load_from_archive(ClassLoaderData * loader_data)478 void ModuleEntry::load_from_archive(ClassLoaderData* loader_data) {
479 set_loader_data(loader_data);
480 _reads = restore_growable_array((Array<ModuleEntry*>*)_reads);
481 JFR_ONLY(INIT_ID(this);)
482 }
483
restore_archived_oops(ClassLoaderData * loader_data)484 void ModuleEntry::restore_archived_oops(ClassLoaderData* loader_data) {
485 Handle module_handle(Thread::current(), HeapShared::get_root(_archived_module_index, /*clear=*/true));
486 assert(module_handle.not_null(), "huh");
487 set_module(loader_data->add_handle(module_handle));
488
489 // This was cleared to zero during dump time -- we didn't save the value
490 // because it may be affected by archive relocation.
491 java_lang_Module::set_module_entry(module_handle(), this);
492
493 if (loader_data->class_loader() != NULL) {
494 java_lang_Module::set_loader(module_handle(), loader_data->class_loader());
495 }
496 }
497
clear_archived_oops()498 void ModuleEntry::clear_archived_oops() {
499 HeapShared::clear_root(_archived_module_index);
500 }
501
compare_module_by_name(ModuleEntry * a,ModuleEntry * b)502 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) {
503 assert(a == b || a->name() != b->name(), "no duplicated names");
504 return a->name()->fast_compare(b->name());
505 }
506
iterate_symbols(MetaspaceClosure * closure)507 void ModuleEntryTable::iterate_symbols(MetaspaceClosure* closure) {
508 for (int i = 0; i < table_size(); ++i) {
509 for (ModuleEntry* m = bucket(i); m != NULL; m = m->next()) {
510 m->iterate_symbols(closure);
511 }
512 }
513 }
514
allocate_archived_entries()515 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() {
516 Array<ModuleEntry*>* archived_modules = ArchiveBuilder::new_rw_array<ModuleEntry*>(number_of_entries());
517 int n = 0;
518 for (int i = 0; i < table_size(); ++i) {
519 for (ModuleEntry* m = bucket(i); m != NULL; m = m->next()) {
520 archived_modules->at_put(n++, m);
521 }
522 }
523 if (n > 1) {
524 // Always allocate in the same order to produce deterministic archive.
525 QuickSort::sort(archived_modules->data(), n, (_sort_Fn)compare_module_by_name, true);
526 }
527 for (int i = 0; i < n; i++) {
528 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry());
529 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i));
530 }
531 return archived_modules;
532 }
533
init_archived_entries(Array<ModuleEntry * > * archived_modules)534 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) {
535 assert(DumpSharedSpaces, "dump time only");
536 for (int i = 0; i < archived_modules->length(); i++) {
537 ModuleEntry* archived_entry = archived_modules->at(i);
538 archived_entry->init_as_archived_entry();
539 }
540 }
541
init_archived_oops(Array<ModuleEntry * > * archived_modules)542 void ModuleEntryTable::init_archived_oops(Array<ModuleEntry*>* archived_modules) {
543 assert(DumpSharedSpaces, "dump time only");
544 for (int i = 0; i < archived_modules->length(); i++) {
545 ModuleEntry* archived_entry = archived_modules->at(i);
546 archived_entry->init_archived_oops();
547 }
548 }
549
load_archived_entries(ClassLoaderData * loader_data,Array<ModuleEntry * > * archived_modules)550 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data,
551 Array<ModuleEntry*>* archived_modules) {
552 assert(UseSharedSpaces, "runtime only");
553
554 for (int i = 0; i < archived_modules->length(); i++) {
555 ModuleEntry* archived_entry = archived_modules->at(i);
556 archived_entry->load_from_archive(loader_data);
557
558 unsigned int hash = compute_hash(archived_entry->name());
559 archived_entry->set_hash(hash);
560 add_entry(hash_to_index(hash), archived_entry);
561 }
562 }
563
restore_archived_oops(ClassLoaderData * loader_data,Array<ModuleEntry * > * archived_modules)564 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) {
565 assert(UseSharedSpaces, "runtime only");
566 for (int i = 0; i < archived_modules->length(); i++) {
567 ModuleEntry* archived_entry = archived_modules->at(i);
568 archived_entry->restore_archived_oops(loader_data);
569 }
570 }
571 #endif // INCLUDE_CDS_JAVA_HEAP
572
new_entry(unsigned int hash,Handle module_handle,bool is_open,Symbol * name,Symbol * version,Symbol * location,ClassLoaderData * loader_data)573 ModuleEntry* ModuleEntryTable::new_entry(unsigned int hash, Handle module_handle,
574 bool is_open, Symbol* name,
575 Symbol* version, Symbol* location,
576 ClassLoaderData* loader_data) {
577 assert(Module_lock->owned_by_self(), "should have the Module_lock");
578 ModuleEntry* entry = (ModuleEntry*)Hashtable<Symbol*, mtModule>::new_entry(hash, name);
579
580 // Initialize fields specific to a ModuleEntry
581 entry->init();
582 if (name != NULL) {
583 name->increment_refcount();
584 } else {
585 // Unnamed modules can read all other unnamed modules.
586 entry->set_can_read_all_unnamed();
587 }
588
589 if (!module_handle.is_null()) {
590 entry->set_module(loader_data->add_handle(module_handle));
591 }
592
593 entry->set_loader_data(loader_data);
594 entry->set_version(version);
595 entry->set_location(location);
596 entry->set_is_open(is_open);
597
598 if (ClassLoader::is_in_patch_mod_entries(name)) {
599 entry->set_is_patched();
600 if (log_is_enabled(Trace, module, patch)) {
601 ResourceMark rm;
602 log_trace(module, patch)("Marked module %s as patched from --patch-module",
603 name != NULL ? name->as_C_string() : UNNAMED_MODULE);
604 }
605 }
606
607 JFR_ONLY(INIT_ID(entry);)
608
609 return entry;
610 }
611
add_entry(int index,ModuleEntry * new_entry)612 void ModuleEntryTable::add_entry(int index, ModuleEntry* new_entry) {
613 assert(Module_lock->owned_by_self(), "should have the Module_lock");
614 Hashtable<Symbol*, mtModule>::add_entry(index, (HashtableEntry<Symbol*, mtModule>*)new_entry);
615 }
616
617 // Create an entry in the class loader's module_entry_table. It is the
618 // caller's responsibility to ensure that the entry has not already been
619 // created.
locked_create_entry(Handle module_handle,bool is_open,Symbol * module_name,Symbol * module_version,Symbol * module_location,ClassLoaderData * loader_data)620 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle,
621 bool is_open,
622 Symbol* module_name,
623 Symbol* module_version,
624 Symbol* module_location,
625 ClassLoaderData* loader_data) {
626 assert(module_name != NULL, "ModuleEntryTable locked_create_entry should never be called for unnamed module.");
627 assert(Module_lock->owned_by_self(), "should have the Module_lock");
628 assert(lookup_only(module_name) == NULL, "Module already exists");
629 ModuleEntry* entry = new_entry(compute_hash(module_name), module_handle, is_open, module_name,
630 module_version, module_location, loader_data);
631 add_entry(index_for(module_name), entry);
632 return entry;
633 }
634
635 // lookup_only by Symbol* to find a ModuleEntry.
lookup_only(Symbol * name)636 ModuleEntry* ModuleEntryTable::lookup_only(Symbol* name) {
637 assert(name != NULL, "name cannot be NULL");
638 int index = index_for(name);
639 for (ModuleEntry* m = bucket(index); m != NULL; m = m->next()) {
640 if (m->name()->fast_compare(name) == 0) {
641 return m;
642 }
643 }
644 return NULL;
645 }
646
647 // Remove dead modules from all other alive modules' reads list.
648 // This should only occur at class unloading.
purge_all_module_reads()649 void ModuleEntryTable::purge_all_module_reads() {
650 assert_locked_or_safepoint(Module_lock);
651 for (int i = 0; i < table_size(); i++) {
652 for (ModuleEntry* entry = bucket(i);
653 entry != NULL;
654 entry = entry->next()) {
655 entry->purge_reads();
656 }
657 }
658 }
659
finalize_javabase(Handle module_handle,Symbol * version,Symbol * location)660 void ModuleEntryTable::finalize_javabase(Handle module_handle, Symbol* version, Symbol* location) {
661 assert(Module_lock->owned_by_self(), "should have the Module_lock");
662 ClassLoaderData* boot_loader_data = ClassLoaderData::the_null_class_loader_data();
663 ModuleEntryTable* module_table = boot_loader_data->modules();
664
665 assert(module_table != NULL, "boot loader's ModuleEntryTable not defined");
666
667 if (module_handle.is_null()) {
668 fatal("Unable to finalize module definition for " JAVA_BASE_NAME);
669 }
670
671 // Set java.lang.Module, version and location for java.base
672 ModuleEntry* jb_module = javabase_moduleEntry();
673 assert(jb_module != NULL, JAVA_BASE_NAME " ModuleEntry not defined");
674 jb_module->set_version(version);
675 jb_module->set_location(location);
676 // Once java.base's ModuleEntry _module field is set with the known
677 // java.lang.Module, java.base is considered "defined" to the VM.
678 jb_module->set_module(boot_loader_data->add_handle(module_handle));
679
680 // Store pointer to the ModuleEntry for java.base in the java.lang.Module object.
681 java_lang_Module::set_module_entry(module_handle(), jb_module);
682 }
683
684 // Within java.lang.Class instances there is a java.lang.Module field that must
685 // be set with the defining module. During startup, prior to java.base's definition,
686 // classes needing their module field set are added to the fixup_module_list.
687 // Their module field is set once java.base's java.lang.Module is known to the VM.
patch_javabase_entries(Handle module_handle)688 void ModuleEntryTable::patch_javabase_entries(Handle module_handle) {
689 if (module_handle.is_null()) {
690 fatal("Unable to patch the module field of classes loaded prior to "
691 JAVA_BASE_NAME "'s definition, invalid java.lang.Module");
692 }
693
694 // Do the fixups for the basic primitive types
695 java_lang_Class::set_module(Universe::int_mirror(), module_handle());
696 java_lang_Class::set_module(Universe::float_mirror(), module_handle());
697 java_lang_Class::set_module(Universe::double_mirror(), module_handle());
698 java_lang_Class::set_module(Universe::byte_mirror(), module_handle());
699 java_lang_Class::set_module(Universe::bool_mirror(), module_handle());
700 java_lang_Class::set_module(Universe::char_mirror(), module_handle());
701 java_lang_Class::set_module(Universe::long_mirror(), module_handle());
702 java_lang_Class::set_module(Universe::short_mirror(), module_handle());
703 java_lang_Class::set_module(Universe::void_mirror(), module_handle());
704
705 // Do the fixups for classes that have already been created.
706 GrowableArray <Klass*>* list = java_lang_Class::fixup_module_field_list();
707 int list_length = list->length();
708 for (int i = 0; i < list_length; i++) {
709 Klass* k = list->at(i);
710 assert(k->is_klass(), "List should only hold classes");
711 java_lang_Class::fixup_module_field(k, module_handle);
712 k->class_loader_data()->dec_keep_alive();
713 }
714
715 delete java_lang_Class::fixup_module_field_list();
716 java_lang_Class::set_fixup_module_field_list(NULL);
717 }
718
print(outputStream * st)719 void ModuleEntryTable::print(outputStream* st) {
720 st->print_cr("Module Entry Table (table_size=%d, entries=%d)",
721 table_size(), number_of_entries());
722 for (int i = 0; i < table_size(); i++) {
723 for (ModuleEntry* probe = bucket(i);
724 probe != NULL;
725 probe = probe->next()) {
726 probe->print(st);
727 }
728 }
729 }
730
print(outputStream * st)731 void ModuleEntry::print(outputStream* st) {
732 ResourceMark rm;
733 st->print_cr("entry " PTR_FORMAT " name %s module " PTR_FORMAT " loader %s version %s location %s strict %s next " PTR_FORMAT,
734 p2i(this),
735 name() == NULL ? UNNAMED_MODULE : name()->as_C_string(),
736 p2i(module()),
737 loader_data()->loader_name_and_id(),
738 version() != NULL ? version()->as_C_string() : "NULL",
739 location() != NULL ? location()->as_C_string() : "NULL",
740 BOOL_TO_STR(!can_read_all_unnamed()), p2i(next()));
741 }
742
verify()743 void ModuleEntryTable::verify() {
744 verify_table<ModuleEntry>("Module Entry Table");
745 }
746
verify()747 void ModuleEntry::verify() {
748 guarantee(loader_data() != NULL, "A module entry must be associated with a loader.");
749 }
750