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(); }
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,
|
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(); }
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,
|