546 // Dump bytes to file -- at the current file position.
547
548 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
549 if (_file_open) {
550 int n = ::write(_fd, buffer, nbytes);
551 if (n != nbytes) {
552 // It is dangerous to leave the corrupted shared archive file around,
553 // close and remove the file. See bug 6372906.
554 close();
555 remove(_full_path);
556 fail_stop("Unable to write to shared archive file.");
557 }
558 }
559 _file_offset += nbytes;
560 }
561
562
563 // Align file position to an allocation unit boundary.
564
565 void FileMapInfo::align_file_position() {
566 size_t new_file_offset = align_size_up(_file_offset,
567 os::vm_allocation_granularity());
568 if (new_file_offset != _file_offset) {
569 _file_offset = new_file_offset;
570 if (_file_open) {
571 // Seek one byte back from the target and write a byte to insure
572 // that the written file is the correct length.
573 _file_offset -= 1;
574 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
575 fail_stop("Unable to seek.");
576 }
577 char zero = 0;
578 write_bytes(&zero, 1);
579 }
580 }
581 }
582
583
584 // Dump bytes to file -- at the current file position.
585
586 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
596 if (_file_open) {
597 if (::close(_fd) < 0) {
598 fail_stop("Unable to close the shared archive file.");
599 }
600 _file_open = false;
601 _fd = -1;
602 }
603 }
604
605
606 // JVM/TI RedefineClasses() support:
607 // Remap the shared readonly space to shared readwrite, private.
608 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
609 int idx = 0;
610 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx];
611 if (!si->_read_only) {
612 // the space is already readwrite so we are done
613 return true;
614 }
615 size_t used = si->_used;
616 size_t size = align_size_up(used, os::vm_allocation_granularity());
617 if (!open_for_read()) {
618 return false;
619 }
620 char *addr = _header->region_addr(idx);
621 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
622 addr, size, false /* !read_only */,
623 si->_allow_exec);
624 close();
625 if (base == NULL) {
626 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
627 return false;
628 }
629 if (base != addr) {
630 fail_continue("Unable to remap shared readonly space at required address.");
631 return false;
632 }
633 si->_read_only = false;
634 return true;
635 }
636
647 if (!rs.is_reserved()) {
648 fail_continue("Unable to reserve shared space at required address "
649 INTPTR_FORMAT, p2i(requested_addr));
650 return rs;
651 }
652 // the reserved virtual memory is for mapping class data sharing archive
653 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
654
655 return rs;
656 }
657
658 // Memory map a region in the address space.
659 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode",
660 "String1", "String2", "OptionalData" };
661
662 char* FileMapInfo::map_region(int i) {
663 assert(!MetaspaceShared::is_string_region(i), "sanity");
664 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
665 size_t used = si->_used;
666 size_t alignment = os::vm_allocation_granularity();
667 size_t size = align_size_up(used, alignment);
668 char *requested_addr = _header->region_addr(i);
669
670 // If a tool agent is in use (debugging enabled), we must map the address space RW
671 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
672 si->_read_only = false;
673 }
674
675 // map the contents of the CDS archive in this memory
676 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
677 requested_addr, size, si->_read_only,
678 si->_allow_exec);
679 if (base == NULL || base != requested_addr) {
680 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
681 return NULL;
682 }
683 #ifdef _WINDOWS
684 // This call is Windows-only because the memory_type gets recorded for the other platforms
685 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
686 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
687 #endif
814 return true; // no data
815 }
816 if (MetaspaceShared::is_string_region(i) && StringTable::shared_string_ignored()) {
817 return true; // shared string data are not mapped
818 }
819 const char* buf = _header->region_addr(i);
820 int crc = ClassLoader::crc32(0, buf, (jint)sz);
821 if (crc != _header->_space[i]._crc) {
822 fail_continue("Checksum verification failed.");
823 return false;
824 }
825 return true;
826 }
827
828 // Unmap a memory region in the address space.
829
830 void FileMapInfo::unmap_region(int i) {
831 assert(!MetaspaceShared::is_string_region(i), "sanity");
832 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
833 size_t used = si->_used;
834 size_t size = align_size_up(used, os::vm_allocation_granularity());
835
836 if (used == 0) {
837 return;
838 }
839
840 char* addr = _header->region_addr(i);
841 if (!os::unmap_memory(addr, size)) {
842 fail_stop("Unable to unmap shared space.");
843 }
844 }
845
846 // dealloc the archived string region from java heap
847 void FileMapInfo::dealloc_string_regions() {
848 #if INCLUDE_ALL_GCS
849 if (num_ranges > 0) {
850 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
851 G1CollectedHeap::heap()->dealloc_archive_regions(string_ranges, num_ranges);
852 }
853 #endif
854 }
|
546 // Dump bytes to file -- at the current file position.
547
548 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
549 if (_file_open) {
550 int n = ::write(_fd, buffer, nbytes);
551 if (n != nbytes) {
552 // It is dangerous to leave the corrupted shared archive file around,
553 // close and remove the file. See bug 6372906.
554 close();
555 remove(_full_path);
556 fail_stop("Unable to write to shared archive file.");
557 }
558 }
559 _file_offset += nbytes;
560 }
561
562
563 // Align file position to an allocation unit boundary.
564
565 void FileMapInfo::align_file_position() {
566 size_t new_file_offset = align_up(_file_offset,
567 os::vm_allocation_granularity());
568 if (new_file_offset != _file_offset) {
569 _file_offset = new_file_offset;
570 if (_file_open) {
571 // Seek one byte back from the target and write a byte to insure
572 // that the written file is the correct length.
573 _file_offset -= 1;
574 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
575 fail_stop("Unable to seek.");
576 }
577 char zero = 0;
578 write_bytes(&zero, 1);
579 }
580 }
581 }
582
583
584 // Dump bytes to file -- at the current file position.
585
586 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
596 if (_file_open) {
597 if (::close(_fd) < 0) {
598 fail_stop("Unable to close the shared archive file.");
599 }
600 _file_open = false;
601 _fd = -1;
602 }
603 }
604
605
606 // JVM/TI RedefineClasses() support:
607 // Remap the shared readonly space to shared readwrite, private.
608 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
609 int idx = 0;
610 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx];
611 if (!si->_read_only) {
612 // the space is already readwrite so we are done
613 return true;
614 }
615 size_t used = si->_used;
616 size_t size = align_up(used, os::vm_allocation_granularity());
617 if (!open_for_read()) {
618 return false;
619 }
620 char *addr = _header->region_addr(idx);
621 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
622 addr, size, false /* !read_only */,
623 si->_allow_exec);
624 close();
625 if (base == NULL) {
626 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
627 return false;
628 }
629 if (base != addr) {
630 fail_continue("Unable to remap shared readonly space at required address.");
631 return false;
632 }
633 si->_read_only = false;
634 return true;
635 }
636
647 if (!rs.is_reserved()) {
648 fail_continue("Unable to reserve shared space at required address "
649 INTPTR_FORMAT, p2i(requested_addr));
650 return rs;
651 }
652 // the reserved virtual memory is for mapping class data sharing archive
653 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
654
655 return rs;
656 }
657
658 // Memory map a region in the address space.
659 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode",
660 "String1", "String2", "OptionalData" };
661
662 char* FileMapInfo::map_region(int i) {
663 assert(!MetaspaceShared::is_string_region(i), "sanity");
664 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
665 size_t used = si->_used;
666 size_t alignment = os::vm_allocation_granularity();
667 size_t size = align_up(used, alignment);
668 char *requested_addr = _header->region_addr(i);
669
670 // If a tool agent is in use (debugging enabled), we must map the address space RW
671 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
672 si->_read_only = false;
673 }
674
675 // map the contents of the CDS archive in this memory
676 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
677 requested_addr, size, si->_read_only,
678 si->_allow_exec);
679 if (base == NULL || base != requested_addr) {
680 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
681 return NULL;
682 }
683 #ifdef _WINDOWS
684 // This call is Windows-only because the memory_type gets recorded for the other platforms
685 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
686 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
687 #endif
814 return true; // no data
815 }
816 if (MetaspaceShared::is_string_region(i) && StringTable::shared_string_ignored()) {
817 return true; // shared string data are not mapped
818 }
819 const char* buf = _header->region_addr(i);
820 int crc = ClassLoader::crc32(0, buf, (jint)sz);
821 if (crc != _header->_space[i]._crc) {
822 fail_continue("Checksum verification failed.");
823 return false;
824 }
825 return true;
826 }
827
828 // Unmap a memory region in the address space.
829
830 void FileMapInfo::unmap_region(int i) {
831 assert(!MetaspaceShared::is_string_region(i), "sanity");
832 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
833 size_t used = si->_used;
834 size_t size = align_up(used, os::vm_allocation_granularity());
835
836 if (used == 0) {
837 return;
838 }
839
840 char* addr = _header->region_addr(i);
841 if (!os::unmap_memory(addr, size)) {
842 fail_stop("Unable to unmap shared space.");
843 }
844 }
845
846 // dealloc the archived string region from java heap
847 void FileMapInfo::dealloc_string_regions() {
848 #if INCLUDE_ALL_GCS
849 if (num_ranges > 0) {
850 assert(string_ranges != NULL, "Null string_ranges array with non-zero count");
851 G1CollectedHeap::heap()->dealloc_archive_regions(string_ranges, num_ranges);
852 }
853 #endif
854 }
|