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