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/archiveBuilder.hpp"
27 #include "cds/archiveUtils.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/heapShared.hpp"
30 #include "classfile/classLoader.hpp"
31 #include "classfile/classLoaderData.inline.hpp"
32 #include "classfile/classLoaderDataShared.hpp"
33 #include "classfile/javaClasses.inline.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "classfile/systemDictionary.hpp"
36 #include "classfile/systemDictionaryShared.hpp"
37 #include "jni.h"
38 #include "logging/log.hpp"
39 #include "logging/logStream.hpp"
40 #include "memory/resourceArea.hpp"
41 #include "memory/universe.hpp"
42 #include "oops/oopHandle.inline.hpp"
43 #include "oops/symbol.hpp"
44 #include "runtime/handles.inline.hpp"
45 #include "runtime/safepoint.hpp"
46 #include "utilities/events.hpp"
47 #include "utilities/growableArray.hpp"
48 #include "utilities/hashTable.hpp"
49 #include "utilities/ostream.hpp"
50 #include "utilities/quickSort.hpp"
51
52 ModuleEntry* ModuleEntryTable::_javabase_module = nullptr;
53
54 oop ModuleEntry::module_oop() const { return _module_handle.resolve(); }
453 ls.print("Stored in archive: ");
454 archived_entry->print(&ls);
455 }
456 return archived_entry;
457 }
458
459 bool ModuleEntry::has_been_archived() {
460 assert(!ArchiveBuilder::current()->is_in_buffer_space(this), "must be called on original ModuleEntry");
461 return _archive_modules_entries->contains(this);
462 }
463
464 ModuleEntry* ModuleEntry::get_archived_entry(ModuleEntry* orig_entry) {
465 ModuleEntry** ptr = _archive_modules_entries->get(orig_entry);
466 assert(ptr != nullptr && *ptr != nullptr, "must have been allocated");
467 return *ptr;
468 }
469
470 // This function is used to archive ModuleEntry::_reads and PackageEntry::_qualified_exports.
471 // GrowableArray cannot be directly archived, as it needs to be expandable at runtime.
472 // Write it out as an Array, and convert it back to GrowableArray at runtime.
473 Array<ModuleEntry*>* ModuleEntry::write_growable_array(GrowableArray<ModuleEntry*>* array) {
474 Array<ModuleEntry*>* archived_array = nullptr;
475 int length = (array == nullptr) ? 0 : array->length();
476 if (length > 0) {
477 archived_array = ArchiveBuilder::new_ro_array<ModuleEntry*>(length);
478 for (int i = 0; i < length; i++) {
479 ModuleEntry* archived_entry = get_archived_entry(array->at(i));
480 archived_array->at_put(i, archived_entry);
481 ArchivePtrMarker::mark_pointer((address*)archived_array->adr_at(i));
482 }
483 }
484
485 return archived_array;
486 }
487
488 GrowableArray<ModuleEntry*>* ModuleEntry::restore_growable_array(Array<ModuleEntry*>* archived_array) {
489 GrowableArray<ModuleEntry*>* array = nullptr;
490 int length = (archived_array == nullptr) ? 0 : archived_array->length();
491 if (length > 0) {
492 array = new (mtModule) GrowableArray<ModuleEntry*>(length, mtModule);
493 for (int i = 0; i < length; i++) {
494 ModuleEntry* archived_entry = archived_array->at(i);
495 array->append(archived_entry);
496 }
497 }
498
499 return array;
500 }
501
502 void ModuleEntry::iterate_symbols(MetaspaceClosure* closure) {
503 closure->push(&_name);
504 closure->push(&_version);
505 closure->push(&_location);
506 }
507
508 void ModuleEntry::init_as_archived_entry() {
509 set_archived_reads(write_growable_array(reads()));
510
511 _loader_data = nullptr; // re-init at runtime
512 if (name() != nullptr) {
513 _shared_path_index = AOTClassLocationConfig::dumptime()->get_module_shared_path_index(_location);
514 _name = ArchiveBuilder::get_buffered_symbol(_name);
515 ArchivePtrMarker::mark_pointer((address*)&_name);
516 } else {
517 // _shared_path_index is used only by SystemDictionary::is_shared_class_visible_impl()
518 // for checking classes in named modules.
519 _shared_path_index = -1;
520 }
521 if (_version != nullptr) {
522 _version = ArchiveBuilder::get_buffered_symbol(_version);
523 }
524 if (_location != nullptr) {
525 _location = ArchiveBuilder::get_buffered_symbol(_location);
526 }
527 JFR_ONLY(set_trace_id(0);) // re-init at runtime
528
529 ArchivePtrMarker::mark_pointer((address*)&_reads);
530 ArchivePtrMarker::mark_pointer((address*)&_version);
531 ArchivePtrMarker::mark_pointer((address*)&_location);
532 }
533
534 #ifndef PRODUCT
535 void ModuleEntry::verify_archived_module_entries() {
536 assert(_num_archived_module_entries == _num_inited_module_entries,
537 "%d ModuleEntries have been archived but %d of them have been properly initialized with archived java.lang.Module objects",
538 _num_archived_module_entries, _num_inited_module_entries);
539 }
540 #endif // PRODUCT
541
542 void ModuleEntry::load_from_archive(ClassLoaderData* loader_data) {
543 assert(CDSConfig::is_using_archive(), "runtime only");
544 set_loader_data(loader_data);
545 set_reads(restore_growable_array(archived_reads()));
546 JFR_ONLY(INIT_ID(this);)
547 }
548
549 void ModuleEntry::restore_archived_oops(ClassLoaderData* loader_data) {
550 assert(CDSConfig::is_using_archive(), "runtime only");
551 Handle module_handle(Thread::current(), HeapShared::get_root(_archived_module_index, /*clear=*/true));
552 assert(module_handle.not_null(), "huh");
553 set_module_handle(loader_data->add_handle(module_handle));
554
555 // This was cleared to zero during dump time -- we didn't save the value
556 // because it may be affected by archive relocation.
557 java_lang_Module::set_module_entry(module_handle(), this);
558
559 assert(java_lang_Module::loader(module_handle()) == loader_data->class_loader(),
560 "must be set in dump time");
561
562 if (log_is_enabled(Info, aot, module)) {
563 ResourceMark rm;
564 LogStream ls(Log(aot, module)::info());
565 ls.print("Restored from archive: ");
566 print(&ls);
567 }
568 }
569
570 void ModuleEntry::clear_archived_oops() {
571 assert(CDSConfig::is_using_archive(), "runtime only");
572 HeapShared::clear_root(_archived_module_index);
573 }
574
575 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) {
576 assert(a == b || a->name() != b->name(), "no duplicated names");
577 return a->name()->fast_compare(b->name());
578 }
579
580 void ModuleEntryTable::iterate_symbols(MetaspaceClosure* closure) {
581 auto syms = [&] (const SymbolHandle& key, ModuleEntry*& m) {
582 m->iterate_symbols(closure);
583 };
584 _table.iterate_all(syms);
585 }
586
587 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() {
588 Array<ModuleEntry*>* archived_modules = ArchiveBuilder::new_rw_array<ModuleEntry*>(_table.number_of_entries());
589 int n = 0;
590 auto grab = [&] (const SymbolHandle& key, ModuleEntry*& m) {
591 archived_modules->at_put(n++, m);
596 // Always allocate in the same order to produce deterministic archive.
597 QuickSort::sort(archived_modules->data(), n, compare_module_by_name);
598 }
599 for (int i = 0; i < n; i++) {
600 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry());
601 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i));
602 }
603 return archived_modules;
604 }
605
606 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) {
607 assert(CDSConfig::is_dumping_full_module_graph(), "sanity");
608 for (int i = 0; i < archived_modules->length(); i++) {
609 ModuleEntry* archived_entry = archived_modules->at(i);
610 archived_entry->init_as_archived_entry();
611 }
612 }
613
614 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data,
615 Array<ModuleEntry*>* archived_modules) {
616 assert(CDSConfig::is_using_archive(), "runtime only");
617
618 for (int i = 0; i < archived_modules->length(); i++) {
619 ModuleEntry* archived_entry = archived_modules->at(i);
620 archived_entry->load_from_archive(loader_data);
621 _table.put(archived_entry->name(), archived_entry);
622 }
623 }
624
625 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) {
626 assert(CDSConfig::is_using_archive(), "runtime only");
627 for (int i = 0; i < archived_modules->length(); i++) {
628 ModuleEntry* archived_entry = archived_modules->at(i);
629 archived_entry->restore_archived_oops(loader_data);
630 }
631 }
632 #endif // INCLUDE_CDS_JAVA_HEAP
633
634 // Create an entry in the class loader's module_entry_table. It is the
635 // caller's responsibility to ensure that the entry has not already been
636 // created.
637 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle,
638 bool is_open,
639 Symbol* module_name,
640 Symbol* module_version,
641 Symbol* module_location,
642 ClassLoaderData* loader_data) {
643 assert(module_name != nullptr, "ModuleEntryTable locked_create_entry should never be called for unnamed module.");
644 assert(Module_lock->owned_by_self(), "should have the Module_lock");
645 assert(lookup_only(module_name) == nullptr, "Module already exists");
646 ModuleEntry* entry = new ModuleEntry(module_handle, is_open, module_name,
|
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/archiveBuilder.hpp"
27 #include "cds/archiveUtils.hpp"
28 #include "cds/cdsConfig.hpp"
29 #include "cds/heapShared.hpp"
30 #include "classfile/classLoader.hpp"
31 #include "classfile/classLoaderData.inline.hpp"
32 #include "classfile/classLoaderDataShared.hpp"
33 #include "classfile/javaClasses.inline.hpp"
34 #include "classfile/moduleEntry.hpp"
35 #include "classfile/modules.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/resourceArea.hpp"
42 #include "memory/universe.hpp"
43 #include "oops/oopHandle.inline.hpp"
44 #include "oops/symbol.hpp"
45 #include "runtime/handles.inline.hpp"
46 #include "runtime/safepoint.hpp"
47 #include "utilities/events.hpp"
48 #include "utilities/growableArray.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(); }
454 ls.print("Stored in archive: ");
455 archived_entry->print(&ls);
456 }
457 return archived_entry;
458 }
459
460 bool ModuleEntry::has_been_archived() {
461 assert(!ArchiveBuilder::current()->is_in_buffer_space(this), "must be called on original ModuleEntry");
462 return _archive_modules_entries->contains(this);
463 }
464
465 ModuleEntry* ModuleEntry::get_archived_entry(ModuleEntry* orig_entry) {
466 ModuleEntry** ptr = _archive_modules_entries->get(orig_entry);
467 assert(ptr != nullptr && *ptr != nullptr, "must have been allocated");
468 return *ptr;
469 }
470
471 // This function is used to archive ModuleEntry::_reads and PackageEntry::_qualified_exports.
472 // GrowableArray cannot be directly archived, as it needs to be expandable at runtime.
473 // Write it out as an Array, and convert it back to GrowableArray at runtime.
474 Array<ModuleEntry*>* ModuleEntry::write_growable_array(ModuleEntry* module, GrowableArray<ModuleEntry*>* array) {
475 Array<ModuleEntry*>* archived_array = nullptr;
476 int length = (array == nullptr) ? 0 : array->length();
477 if (module->is_named()) {
478 if (Modules::is_dynamic_proxy_module(module)) {
479 // This is a dynamically generated module. Its opens and exports will be
480 // restored at runtime in the Java code. See comments in ArchivedData::restore().
481 return nullptr;
482 }
483 }
484 if (length > 0) {
485 archived_array = ArchiveBuilder::new_ro_array<ModuleEntry*>(length);
486 for (int i = 0; i < length; i++) {
487 ModuleEntry* archived_entry = get_archived_entry(array->at(i));
488 archived_array->at_put(i, archived_entry);
489 ArchivePtrMarker::mark_pointer((address*)archived_array->adr_at(i));
490 }
491 }
492
493 return archived_array;
494 }
495
496 GrowableArray<ModuleEntry*>* ModuleEntry::restore_growable_array(Array<ModuleEntry*>* archived_array) {
497 GrowableArray<ModuleEntry*>* array = nullptr;
498 int length = (archived_array == nullptr) ? 0 : archived_array->length();
499 if (length > 0) {
500 array = new (mtModule) GrowableArray<ModuleEntry*>(length, mtModule);
501 for (int i = 0; i < length; i++) {
502 ModuleEntry* archived_entry = archived_array->at(i);
503 array->append(archived_entry);
504 }
505 }
506
507 return array;
508 }
509
510 void ModuleEntry::iterate_symbols(MetaspaceClosure* closure) {
511 closure->push(&_name);
512 closure->push(&_version);
513 closure->push(&_location);
514 }
515
516 void ModuleEntry::init_as_archived_entry() {
517 set_archived_reads(write_growable_array(this, reads()));
518
519 _loader_data = nullptr; // re-init at runtime
520 if (name() != nullptr) {
521 _shared_path_index = AOTClassLocationConfig::dumptime()->get_module_shared_path_index(_location);
522 _name = ArchiveBuilder::get_buffered_symbol(_name);
523 ArchivePtrMarker::mark_pointer((address*)&_name);
524 } else {
525 // _shared_path_index is used only by SystemDictionary::is_shared_class_visible_impl()
526 // for checking classes in named modules.
527 _shared_path_index = -1;
528 }
529 if (_version != nullptr) {
530 _version = ArchiveBuilder::get_buffered_symbol(_version);
531 }
532 if (_location != nullptr) {
533 _location = ArchiveBuilder::get_buffered_symbol(_location);
534 }
535 JFR_ONLY(set_trace_id(0);) // re-init at runtime
536
537 ArchivePtrMarker::mark_pointer((address*)&_reads);
538 ArchivePtrMarker::mark_pointer((address*)&_version);
539 ArchivePtrMarker::mark_pointer((address*)&_location);
540 }
541
542 #ifndef PRODUCT
543 void ModuleEntry::verify_archived_module_entries() {
544 assert(_num_archived_module_entries == _num_inited_module_entries,
545 "%d ModuleEntries have been archived but %d of them have been properly initialized with archived java.lang.Module objects",
546 _num_archived_module_entries, _num_inited_module_entries);
547 }
548 #endif // PRODUCT
549
550 void ModuleEntry::load_from_archive(ClassLoaderData* loader_data) {
551 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
552 set_loader_data(loader_data);
553 set_reads(restore_growable_array(archived_reads()));
554 JFR_ONLY(INIT_ID(this);)
555 }
556
557 void ModuleEntry::restore_archived_oops(ClassLoaderData* loader_data) {
558 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
559 Handle module_handle(Thread::current(), HeapShared::get_root(_archived_module_index, /*clear=*/true));
560 assert(module_handle.not_null(), "huh");
561 set_module_handle(loader_data->add_handle(module_handle));
562
563 // This was cleared to zero during dump time -- we didn't save the value
564 // because it may be affected by archive relocation.
565 java_lang_Module::set_module_entry(module_handle(), this);
566
567 assert(java_lang_Module::loader(module_handle()) == loader_data->class_loader(),
568 "must be set in dump time");
569
570 if (log_is_enabled(Info, aot, module)) {
571 ResourceMark rm;
572 LogStream ls(Log(aot, module)::info());
573 ls.print("Restored from archive: ");
574 print(&ls);
575 }
576 }
577
578 void ModuleEntry::clear_archived_oops() {
579 assert(CDSConfig::is_using_archive() && !CDSConfig::is_using_full_module_graph(), "runtime only");
580 HeapShared::clear_root(_archived_module_index);
581 }
582
583 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) {
584 assert(a == b || a->name() != b->name(), "no duplicated names");
585 return a->name()->fast_compare(b->name());
586 }
587
588 void ModuleEntryTable::iterate_symbols(MetaspaceClosure* closure) {
589 auto syms = [&] (const SymbolHandle& key, ModuleEntry*& m) {
590 m->iterate_symbols(closure);
591 };
592 _table.iterate_all(syms);
593 }
594
595 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() {
596 Array<ModuleEntry*>* archived_modules = ArchiveBuilder::new_rw_array<ModuleEntry*>(_table.number_of_entries());
597 int n = 0;
598 auto grab = [&] (const SymbolHandle& key, ModuleEntry*& m) {
599 archived_modules->at_put(n++, m);
604 // Always allocate in the same order to produce deterministic archive.
605 QuickSort::sort(archived_modules->data(), n, compare_module_by_name);
606 }
607 for (int i = 0; i < n; i++) {
608 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry());
609 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i));
610 }
611 return archived_modules;
612 }
613
614 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) {
615 assert(CDSConfig::is_dumping_full_module_graph(), "sanity");
616 for (int i = 0; i < archived_modules->length(); i++) {
617 ModuleEntry* archived_entry = archived_modules->at(i);
618 archived_entry->init_as_archived_entry();
619 }
620 }
621
622 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data,
623 Array<ModuleEntry*>* archived_modules) {
624 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
625
626 for (int i = 0; i < archived_modules->length(); i++) {
627 ModuleEntry* archived_entry = archived_modules->at(i);
628 archived_entry->load_from_archive(loader_data);
629 _table.put(archived_entry->name(), archived_entry);
630 }
631 }
632
633 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) {
634 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
635 for (int i = 0; i < archived_modules->length(); i++) {
636 ModuleEntry* archived_entry = archived_modules->at(i);
637 archived_entry->restore_archived_oops(loader_data);
638 }
639 }
640 #endif // INCLUDE_CDS_JAVA_HEAP
641
642 // Create an entry in the class loader's module_entry_table. It is the
643 // caller's responsibility to ensure that the entry has not already been
644 // created.
645 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle,
646 bool is_open,
647 Symbol* module_name,
648 Symbol* module_version,
649 Symbol* module_location,
650 ClassLoaderData* loader_data) {
651 assert(module_name != nullptr, "ModuleEntryTable locked_create_entry should never be called for unnamed module.");
652 assert(Module_lock->owned_by_self(), "should have the Module_lock");
653 assert(lookup_only(module_name) == nullptr, "Module already exists");
654 ModuleEntry* entry = new ModuleEntry(module_handle, is_open, module_name,
|