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::preload_archived_oops() {
550 (void)HeapShared::get_root(_archived_module_index, false /* clear */);
551 }
552
553 void ModuleEntry::restore_archived_oops(ClassLoaderData* loader_data) {
554 assert(CDSConfig::is_using_archive(), "runtime only");
555 Handle module_handle(Thread::current(), HeapShared::get_root(_archived_module_index, /*clear=*/true));
556 assert(module_handle.not_null(), "huh");
557 set_module_handle(loader_data->add_handle(module_handle));
558
559 // This was cleared to zero during dump time -- we didn't save the value
560 // because it may be affected by archive relocation.
561 java_lang_Module::set_module_entry(module_handle(), this);
562
563 assert(java_lang_Module::loader(module_handle()) == loader_data->class_loader(),
564 "must be set in dump time");
565
566 if (log_is_enabled(Info, aot, module)) {
567 ResourceMark rm;
568 LogStream ls(Log(aot, module)::info());
569 ls.print("Restored from archive: ");
570 print(&ls);
571 }
572 }
573
574 void ModuleEntry::clear_archived_oops() {
575 assert(CDSConfig::is_using_archive(), "runtime only");
576 HeapShared::clear_root(_archived_module_index);
577 }
578
579 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) {
580 assert(a == b || a->name() != b->name(), "no duplicated names");
581 return a->name()->fast_compare(b->name());
582 }
583
584 void ModuleEntryTable::iterate_symbols(MetaspaceClosure* closure) {
585 auto syms = [&] (const SymbolHandle& key, ModuleEntry*& m) {
586 m->iterate_symbols(closure);
587 };
588 _table.iterate_all(syms);
589 }
590
591 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() {
592 Array<ModuleEntry*>* archived_modules = ArchiveBuilder::new_rw_array<ModuleEntry*>(_table.number_of_entries());
593 int n = 0;
594 auto grab = [&] (const SymbolHandle& key, ModuleEntry*& m) {
595 archived_modules->at_put(n++, m);
600 // Always allocate in the same order to produce deterministic archive.
601 QuickSort::sort(archived_modules->data(), n, compare_module_by_name);
602 }
603 for (int i = 0; i < n; i++) {
604 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry());
605 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i));
606 }
607 return archived_modules;
608 }
609
610 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) {
611 assert(CDSConfig::is_dumping_full_module_graph(), "sanity");
612 for (int i = 0; i < archived_modules->length(); i++) {
613 ModuleEntry* archived_entry = archived_modules->at(i);
614 archived_entry->init_as_archived_entry();
615 }
616 }
617
618 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data,
619 Array<ModuleEntry*>* archived_modules) {
620 assert(CDSConfig::is_using_archive(), "runtime only");
621
622 for (int i = 0; i < archived_modules->length(); i++) {
623 ModuleEntry* archived_entry = archived_modules->at(i);
624 archived_entry->load_from_archive(loader_data);
625 _table.put(archived_entry->name(), archived_entry);
626 }
627 }
628
629 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) {
630 assert(CDSConfig::is_using_archive(), "runtime only");
631 for (int i = 0; i < archived_modules->length(); i++) {
632 ModuleEntry* archived_entry = archived_modules->at(i);
633 archived_entry->restore_archived_oops(loader_data);
634 }
635 }
636 #endif // INCLUDE_CDS_JAVA_HEAP
637
638 // Create an entry in the class loader's module_entry_table. It is the
639 // caller's responsibility to ensure that the entry has not already been
640 // created.
641 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle,
642 bool is_open,
643 Symbol* module_name,
644 Symbol* module_version,
645 Symbol* module_location,
646 ClassLoaderData* loader_data) {
647 assert(module_name != nullptr, "ModuleEntryTable locked_create_entry should never be called for unnamed module.");
648 assert(Module_lock->owned_by_self(), "should have the Module_lock");
649 assert(lookup_only(module_name) == nullptr, "Module already exists");
650 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::preload_archived_oops() {
558 (void)HeapShared::get_root(_archived_module_index, false /* clear */);
559 }
560
561 void ModuleEntry::restore_archived_oops(ClassLoaderData* loader_data) {
562 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
563 Handle module_handle(Thread::current(), HeapShared::get_root(_archived_module_index, /*clear=*/true));
564 assert(module_handle.not_null(), "huh");
565 set_module_handle(loader_data->add_handle(module_handle));
566
567 // This was cleared to zero during dump time -- we didn't save the value
568 // because it may be affected by archive relocation.
569 java_lang_Module::set_module_entry(module_handle(), this);
570
571 assert(java_lang_Module::loader(module_handle()) == loader_data->class_loader(),
572 "must be set in dump time");
573
574 if (log_is_enabled(Info, aot, module)) {
575 ResourceMark rm;
576 LogStream ls(Log(aot, module)::info());
577 ls.print("Restored from archive: ");
578 print(&ls);
579 }
580 }
581
582 void ModuleEntry::clear_archived_oops() {
583 assert(CDSConfig::is_using_archive() && !CDSConfig::is_using_full_module_graph(), "runtime only");
584 HeapShared::clear_root(_archived_module_index);
585 }
586
587 static int compare_module_by_name(ModuleEntry* a, ModuleEntry* b) {
588 assert(a == b || a->name() != b->name(), "no duplicated names");
589 return a->name()->fast_compare(b->name());
590 }
591
592 void ModuleEntryTable::iterate_symbols(MetaspaceClosure* closure) {
593 auto syms = [&] (const SymbolHandle& key, ModuleEntry*& m) {
594 m->iterate_symbols(closure);
595 };
596 _table.iterate_all(syms);
597 }
598
599 Array<ModuleEntry*>* ModuleEntryTable::allocate_archived_entries() {
600 Array<ModuleEntry*>* archived_modules = ArchiveBuilder::new_rw_array<ModuleEntry*>(_table.number_of_entries());
601 int n = 0;
602 auto grab = [&] (const SymbolHandle& key, ModuleEntry*& m) {
603 archived_modules->at_put(n++, m);
608 // Always allocate in the same order to produce deterministic archive.
609 QuickSort::sort(archived_modules->data(), n, compare_module_by_name);
610 }
611 for (int i = 0; i < n; i++) {
612 archived_modules->at_put(i, archived_modules->at(i)->allocate_archived_entry());
613 ArchivePtrMarker::mark_pointer((address*)archived_modules->adr_at(i));
614 }
615 return archived_modules;
616 }
617
618 void ModuleEntryTable::init_archived_entries(Array<ModuleEntry*>* archived_modules) {
619 assert(CDSConfig::is_dumping_full_module_graph(), "sanity");
620 for (int i = 0; i < archived_modules->length(); i++) {
621 ModuleEntry* archived_entry = archived_modules->at(i);
622 archived_entry->init_as_archived_entry();
623 }
624 }
625
626 void ModuleEntryTable::load_archived_entries(ClassLoaderData* loader_data,
627 Array<ModuleEntry*>* archived_modules) {
628 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
629
630 for (int i = 0; i < archived_modules->length(); i++) {
631 ModuleEntry* archived_entry = archived_modules->at(i);
632 archived_entry->load_from_archive(loader_data);
633 _table.put(archived_entry->name(), archived_entry);
634 }
635 }
636
637 void ModuleEntryTable::restore_archived_oops(ClassLoaderData* loader_data, Array<ModuleEntry*>* archived_modules) {
638 assert(CDSConfig::is_using_full_module_graph(), "runtime only");
639 for (int i = 0; i < archived_modules->length(); i++) {
640 ModuleEntry* archived_entry = archived_modules->at(i);
641 archived_entry->restore_archived_oops(loader_data);
642 }
643 }
644 #endif // INCLUDE_CDS_JAVA_HEAP
645
646 // Create an entry in the class loader's module_entry_table. It is the
647 // caller's responsibility to ensure that the entry has not already been
648 // created.
649 ModuleEntry* ModuleEntryTable::locked_create_entry(Handle module_handle,
650 bool is_open,
651 Symbol* module_name,
652 Symbol* module_version,
653 Symbol* module_location,
654 ClassLoaderData* loader_data) {
655 assert(module_name != nullptr, "ModuleEntryTable locked_create_entry should never be called for unnamed module.");
656 assert(Module_lock->owned_by_self(), "should have the Module_lock");
657 assert(lookup_only(module_name) == nullptr, "Module already exists");
658 ModuleEntry* entry = new ModuleEntry(module_handle, is_open, module_name,
|