223 _obj_alignment = ObjectAlignmentInBytes;
224 _compact_strings = CompactStrings;
225 _narrow_oop_mode = CompressedOops::mode();
226 _narrow_oop_base = CompressedOops::base();
227 _narrow_oop_shift = CompressedOops::shift();
228 _max_heap_size = MaxHeapSize;
229 _narrow_klass_base = CompressedKlassPointers::base();
230 _narrow_klass_shift = CompressedKlassPointers::shift();
231 _shared_path_table = mapinfo->_shared_path_table;
232 if (HeapShared::is_heap_object_archiving_allowed()) {
233 _heap_reserved = Universe::heap()->reserved_region();
234 }
235
236 // The following fields are for sanity checks for whether this archive
237 // will function correctly with this JVM and the bootclasspath it's
238 // invoked with.
239
240 // JVM version string ... changes on each build.
241 get_header_version(_jvm_ident);
242
243 ClassLoaderExt::finalize_shared_paths_misc_info();
244 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
245 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
246 _num_module_paths = ClassLoader::num_module_path_entries();
247 _max_used_path_index = ClassLoaderExt::max_used_path_index();
248
249 _verify_local = BytecodeVerificationLocal;
250 _verify_remote = BytecodeVerificationRemote;
251 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
252 _shared_base_address = SharedBaseAddress;
253 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
254 // the following 2 fields will be set in write_header for dynamic archive header
255 _base_archive_name_size = 0;
256 _base_archive_is_default = false;
257 }
258
259 void SharedClassPathEntry::init(bool is_modules_image,
260 ClassPathEntry* cpe, TRAPS) {
261 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
262 _timestamp = 0;
263 _filesize = 0;
270 } else {
271 // The timestamp of the modules_image is not checked at runtime.
272 if (is_modules_image) {
273 _type = modules_image_entry;
274 } else {
275 _type = jar_entry;
276 _timestamp = st.st_mtime;
277 _from_class_path_attr = cpe->from_class_path_attr();
278 }
279 _filesize = st.st_size;
280 }
281 } else {
282 // The file/dir must exist, or it would not have been added
283 // into ClassLoader::classpath_entry().
284 //
285 // If we can't access a jar file in the boot path, then we can't
286 // make assumptions about where classes get loaded from.
287 FileMapInfo::fail_stop("Unable to open file %s.", cpe->name());
288 }
289
290 size_t len = strlen(cpe->name()) + 1;
291 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
292 strcpy(_name->data(), cpe->name());
293 }
294
295 bool SharedClassPathEntry::validate(bool is_class_path) {
296 assert(UseSharedSpaces, "runtime only");
297
298 struct stat st;
299 const char* name;
300
301 // In order to validate the runtime modules image file size against the archived
302 // size information, we need to obtain the runtime modules image path. The recorded
303 // dump time modules image path in the archive may be different from the runtime path
304 // if the JDK image has beed moved after generating the archive.
305 if (is_modules_image()) {
306 name = ClassLoader::get_jrt_entry()->name();
307 } else {
308 name = this->name();
309 }
310
311 bool ok = true;
312 log_info(class, path)("checking shared classpath entry: %s", name);
313 if (os::stat(name, &st) != 0 && is_class_path) {
314 // If the archived module path entry does not exist at runtime, it is not fatal
315 // (no need to invalid the shared archive) because the shared runtime visibility check
316 // filters out any archived module classes that do not have a matching runtime
317 // module path location.
318 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
319 ok = false;
320 } else if (is_dir()) {
321 if (!os::dir_is_empty(name)) {
322 FileMapInfo::fail_continue("directory is not empty: %s", name);
323 ok = false;
324 }
325 } else if ((has_timestamp() && _timestamp != st.st_mtime) ||
326 _filesize != st.st_size) {
327 ok = false;
328 if (PrintSharedArchiveAndExit) {
329 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
397 i++;
398 }
399 assert(i == ClassLoader::num_boot_classpath_entries(),
400 "number of boot class path entry mismatch");
401
402 // 2. app class path
403 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
404 while (acpe != NULL) {
405 log_info(class, path)("add app shared path %s", acpe->name());
406 SharedClassPathEntry* ent = shared_path(i);
407 ent->init(false, acpe, THREAD);
408 EXCEPTION_MARK;
409 update_shared_classpath(acpe, ent, THREAD);
410 acpe = acpe->next();
411 i++;
412 }
413
414 // 3. module path
415 ClassPathEntry *mpe = ClassLoader::module_path_entries();
416 while (mpe != NULL) {
417 log_info(class, path)("add module path %s",mpe->name());
418 SharedClassPathEntry* ent = shared_path(i);
419 ent->init(false, mpe, THREAD);
420 EXCEPTION_MARK;
421 update_shared_classpath(mpe, ent, THREAD);
422 mpe = mpe->next();
423 i++;
424 }
425 assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
426 }
427
428 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
429 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
430
431 bool has_nonempty_dir = false;
432
433 int last = _shared_path_table.size() - 1;
434 if (last > ClassLoaderExt::max_used_path_index()) {
435 // no need to check any path beyond max_used_path_index
436 last = ClassLoaderExt::max_used_path_index();
437 }
687 if (rp_len < shared_app_paths_len) {
688 return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
689 }
690 if (shared_app_paths_len != 0 && rp_len != 0) {
691 // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
692 ResourceMark rm;
693 GrowableArray<char*>* rp_array = create_path_array(appcp);
694 if (rp_array->length() == 0) {
695 // None of the jar file specified in the runtime -cp exists.
696 return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
697 }
698 int j = _header->_app_class_paths_start_index;
699 mismatch = check_paths(j, shared_app_paths_len, rp_array);
700 if (mismatch) {
701 return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
702 }
703 }
704 return true;
705 }
706
707 bool FileMapInfo::validate_shared_path_table() {
708 assert(UseSharedSpaces, "runtime only");
709
710 _validating_shared_path_table = true;
711
712 // Load the shared path table info from the archive header
713 _shared_path_table = _header->_shared_path_table;
714 if (DynamicDumpSharedSpaces) {
715 // Only support dynamic dumping with the usage of the default CDS archive
716 // or a simple base archive.
717 // If the base layer archive contains additional path component besides
718 // the runtime image and the -cp, dynamic dumping is disabled.
719 //
720 // When dynamic archiving is enabled, the _shared_path_table is overwritten
721 // to include the application path and stored in the top layer archive.
722 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
723 if (_header->_app_class_paths_start_index > 1) {
724 DynamicDumpSharedSpaces = false;
725 warning(
726 "Dynamic archiving is disabled because base layer archive has appended boot classpath");
727 }
728 if (_header->_num_module_paths > 0) {
729 DynamicDumpSharedSpaces = false;
730 warning(
731 "Dynamic archiving is disabled because base layer archive has module path");
732 }
733 }
734
735 int module_paths_start_index = _header->_app_module_paths_start_index;
736 int shared_app_paths_len = 0;
737
738 // validate the path entries up to the _max_used_path_index
739 for (int i=0; i < _header->_max_used_path_index + 1; i++) {
740 if (i < module_paths_start_index) {
741 if (shared_path(i)->validate()) {
742 // Only count the app class paths not from the "Class-path" attribute of a jar manifest.
743 if (!shared_path(i)->from_class_path_attr() && i >= _header->_app_class_paths_start_index) {
744 shared_app_paths_len++;
745 }
746 log_info(class, path)("ok");
747 } else {
748 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
749 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
750 }
751 return false;
752 }
753 } else if (i >= module_paths_start_index) {
754 if (shared_path(i)->validate(false /* not a class path entry */)) {
838 // read the header as a dynamic archive header
839 size_t sz = sizeof(DynamicArchiveHeader);
840 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal);
841 size_t n = os::read(fd, dynamic_header, (unsigned int)sz);
842 if (n != sz) {
843 fail_continue("Unable to read the file header.");
844 os::free(dynamic_header);
845 os::close(fd);
846 return false;
847 }
848 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
849 // Not a dynamic header, no need to proceed further.
850 *size = 0;
851 os::free(dynamic_header);
852 os::close(fd);
853 return false;
854 }
855 if (dynamic_header->_base_archive_is_default) {
856 *base_archive_name = Arguments::get_default_shared_archive_path();
857 } else {
858 // skip over the _paths_misc_info
859 sz = dynamic_header->_paths_misc_info_size;
860 lseek(fd, (long)sz, SEEK_CUR);
861 // read the base archive name
862 size_t name_size = dynamic_header->_base_archive_name_size;
863 if (name_size == 0) {
864 os::free(dynamic_header);
865 os::close(fd);
866 return false;
867 }
868 *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal);
869 n = os::read(fd, *base_archive_name, (unsigned int)name_size);
870 if (n != name_size) {
871 fail_continue("Unable to read the base archive name from the header.");
872 FREE_C_HEAP_ARRAY(char, *base_archive_name);
873 *base_archive_name = NULL;
874 os::free(dynamic_header);
875 os::close(fd);
876 return false;
877 }
878 }
879
880 os::free(dynamic_header);
931 char header_version[JVM_IDENT_MAX];
932 get_header_version(header_version);
933 if (strncmp(_header->_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
934 log_info(cds)("_jvm_ident expected: %s", header_version);
935 log_info(cds)(" actual: %s", _header->_jvm_ident);
936 FileMapInfo::fail_continue("The shared archive file was created by a different"
937 " version or build of HotSpot");
938 return false;
939 }
940
941 if (VerifySharedSpaces) {
942 int expected_crc = _header->compute_crc();
943 if (expected_crc != _header->_crc) {
944 log_info(cds)("_crc expected: %d", expected_crc);
945 log_info(cds)(" actual: %d", _header->_crc);
946 FileMapInfo::fail_continue("Header checksum verification failed.");
947 return false;
948 }
949 }
950
951 _file_offset = n;
952
953 size_t info_size = _header->_paths_misc_info_size;
954 _paths_misc_info = NEW_C_HEAP_ARRAY(char, info_size, mtClass);
955 n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
956 if (n != info_size) {
957 fail_continue("Unable to read the shared path info header.");
958 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
959 _paths_misc_info = NULL;
960 return false;
961 }
962 _file_offset += n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name
963
964 if (is_static) {
965 // just checking the last region is sufficient since the archive is written
966 // in sequential order
967 size_t len = lseek(fd, 0, SEEK_END);
968 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
969 // The last space might be empty
970 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
971 fail_continue("The shared archive file has been truncated.");
972 return false;
973 }
974
975 SharedBaseAddress = _header->_shared_base_address;
976 }
977
978 return true;
979 }
980
981
982 // Read the FileMapInfo information from the file.
1024 chmod(_full_path, _S_IREAD | _S_IWRITE);
1025 #endif
1026
1027 // Use remove() to delete the existing file because, on Unix, this will
1028 // allow processes that have it open continued access to the file.
1029 remove(_full_path);
1030 int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
1031 if (fd < 0) {
1032 fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
1033 os::strerror(errno));
1034 }
1035 _fd = fd;
1036 _file_offset = 0;
1037 _file_open = true;
1038 }
1039
1040
1041 // Write the header to the file, seek to the next allocation boundary.
1042
1043 void FileMapInfo::write_header() {
1044 int info_size = ClassLoader::get_shared_paths_misc_info_size();
1045
1046 _header->_paths_misc_info_size = info_size;
1047
1048 char* base_archive_name = NULL;
1049 if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1050 base_archive_name = (char*)Arguments::GetSharedArchivePath();
1051 _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1;
1052 _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile);
1053 }
1054
1055 assert(is_file_position_aligned(), "must be");
1056 write_bytes(_header, _header->_header_size);
1057 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);
1058 if (base_archive_name != NULL) {
1059 write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size);
1060 }
1061 align_file_position();
1062 }
1063
1064 // Dump region to file.
1065 // This is called twice for each region during archiving, once before
1066 // the archive file is open (_file_open is false) and once after.
1067 void FileMapInfo::write_region(int region, char* base, size_t size,
1068 bool read_only, bool allow_exec) {
1069 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only");
1070
1071 CDSFileMapRegion* si = space_at(region);
1072 char* target_base = base;
1073 if (DynamicDumpSharedSpaces) {
1074 target_base = DynamicArchive::buffer_to_target(base);
1075 }
1076
1077 if (_file_open) {
1734 }
1735
1736 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1737 _shared_path_table.metaspace_pointers_do(it);
1738 }
1739
1740 FileMapInfo* FileMapInfo::_current_info = NULL;
1741 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
1742 bool FileMapInfo::_heap_pointers_need_patching = false;
1743 SharedPathTable FileMapInfo::_shared_path_table;
1744 bool FileMapInfo::_validating_shared_path_table = false;
1745 bool FileMapInfo::_memory_mapping_failed = false;
1746
1747 // Open the shared archive file, read and validate the header
1748 // information (version, boot classpath, etc.). If initialization
1749 // fails, shared spaces are disabled and the file is closed. [See
1750 // fail_continue.]
1751 //
1752 // Validation of the archive is done in two steps:
1753 //
1754 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
1755 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1756 // region of the archive, which is not mapped yet.
1757 bool FileMapInfo::initialize(bool is_static) {
1758 assert(UseSharedSpaces, "UseSharedSpaces expected.");
1759
1760 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1761 // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
1762 // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
1763 // during the JVMTI "early" stage, so we can still use CDS if
1764 // JvmtiExport::has_early_class_hook_env() is false.
1765 FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
1766 return false;
1767 }
1768
1769 if (!open_for_read()) {
1770 return false;
1771 }
1772
1773 init_from_file(_fd, is_static);
1774 // UseSharedSpaces could be disabled if the checking of some of the header fields in
1838
1839 // Java agents are allowed during run time. Therefore, the following condition is not
1840 // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
1841 // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
1842 // while AllowArchivingWithJavaAgent is set during the current run.
1843 if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
1844 FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
1845 "from the setting in the shared archive.");
1846 return false;
1847 }
1848
1849 if (_allow_archiving_with_java_agent) {
1850 warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1851 "for testing purposes only and should not be used in a production environment");
1852 }
1853
1854 return true;
1855 }
1856
1857 bool FileMapInfo::validate_header(bool is_static) {
1858 bool status = _header->validate();
1859
1860 if (status) {
1861 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size, is_static)) {
1862 if (!PrintSharedArchiveAndExit) {
1863 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
1864 status = false;
1865 }
1866 }
1867 }
1868
1869 if (_paths_misc_info != NULL) {
1870 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
1871 _paths_misc_info = NULL;
1872 }
1873 return status;
1874 }
1875
1876 // Check if a given address is within one of the shared regions
1877 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1878 assert(idx == MetaspaceShared::ro ||
1879 idx == MetaspaceShared::rw ||
1880 idx == MetaspaceShared::mc ||
1881 idx == MetaspaceShared::md, "invalid region index");
1882 char* base = region_addr(idx);
1883 if (p >= base && p < base + space_at(idx)->_used) {
1884 return true;
1885 }
1886 return false;
1887 }
1888
1889 // Unmap mapped regions of shared space.
1890 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1891 MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
1892
1893 FileMapInfo *map_info = FileMapInfo::current_info();
1905 // Dealloc the archive heap regions only without unmapping. The regions are part
1906 // of the java heap. Unmapping of the heap regions are managed by GC.
1907 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1908 num_open_archive_heap_ranges,
1909 true);
1910 map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
1911 num_closed_archive_heap_ranges,
1912 false);
1913 } else if (DumpSharedSpaces) {
1914 fail_stop("%s", msg);
1915 }
1916 }
1917
1918 #if INCLUDE_JVMTI
1919 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
1920
1921 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
1922 ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
1923 if (ent == NULL) {
1924 if (i == 0) {
1925 ent = ClassLoader:: get_jrt_entry();
1926 assert(ent != NULL, "must be");
1927 } else {
1928 SharedClassPathEntry* scpe = shared_path(i);
1929 assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
1930
1931 const char* path = scpe->name();
1932 struct stat st;
1933 if (os::stat(path, &st) != 0) {
1934 char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ;
1935 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
1936 THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
1937 } else {
1938 ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, false, CHECK_NULL);
1939 }
1940 }
1941
1942 MutexLocker mu(CDSClassFileStream_lock, THREAD);
1943 if (_classpath_entries_for_jvmti[i] == NULL) {
1944 _classpath_entries_for_jvmti[i] = ent;
1945 } else {
|
223 _obj_alignment = ObjectAlignmentInBytes;
224 _compact_strings = CompactStrings;
225 _narrow_oop_mode = CompressedOops::mode();
226 _narrow_oop_base = CompressedOops::base();
227 _narrow_oop_shift = CompressedOops::shift();
228 _max_heap_size = MaxHeapSize;
229 _narrow_klass_base = CompressedKlassPointers::base();
230 _narrow_klass_shift = CompressedKlassPointers::shift();
231 _shared_path_table = mapinfo->_shared_path_table;
232 if (HeapShared::is_heap_object_archiving_allowed()) {
233 _heap_reserved = Universe::heap()->reserved_region();
234 }
235
236 // The following fields are for sanity checks for whether this archive
237 // will function correctly with this JVM and the bootclasspath it's
238 // invoked with.
239
240 // JVM version string ... changes on each build.
241 get_header_version(_jvm_ident);
242
243 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
244 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
245 _num_module_paths = ClassLoader::num_module_path_entries();
246 _max_used_path_index = ClassLoaderExt::max_used_path_index();
247
248 _verify_local = BytecodeVerificationLocal;
249 _verify_remote = BytecodeVerificationRemote;
250 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
251 _shared_base_address = SharedBaseAddress;
252 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
253 // the following 2 fields will be set in write_header for dynamic archive header
254 _base_archive_name_size = 0;
255 _base_archive_is_default = false;
256 }
257
258 void SharedClassPathEntry::init(bool is_modules_image,
259 ClassPathEntry* cpe, TRAPS) {
260 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
261 _timestamp = 0;
262 _filesize = 0;
269 } else {
270 // The timestamp of the modules_image is not checked at runtime.
271 if (is_modules_image) {
272 _type = modules_image_entry;
273 } else {
274 _type = jar_entry;
275 _timestamp = st.st_mtime;
276 _from_class_path_attr = cpe->from_class_path_attr();
277 }
278 _filesize = st.st_size;
279 }
280 } else {
281 // The file/dir must exist, or it would not have been added
282 // into ClassLoader::classpath_entry().
283 //
284 // If we can't access a jar file in the boot path, then we can't
285 // make assumptions about where classes get loaded from.
286 FileMapInfo::fail_stop("Unable to open file %s.", cpe->name());
287 }
288
289 // No need to save the name of the module file, as it will be computed at run time
290 // to allow relocation of the JDK directory.
291 const char* name = is_modules_image ? "" : cpe->name();
292 size_t len = strlen(name) + 1;
293 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
294 strcpy(_name->data(), name);
295 }
296
297 const char* SharedClassPathEntry::name() const {
298 if (UseSharedSpaces && is_modules_image()) {
299 // In order to validate the runtime modules image file size against the archived
300 // size information, we need to obtain the runtime modules image path. The recorded
301 // dump time modules image path in the archive may be different from the runtime path
302 // if the JDK image has beed moved after generating the archive.
303 return ClassLoader::get_jrt_entry()->name();
304 } else {
305 return _name->data();
306 }
307 }
308
309 bool SharedClassPathEntry::validate(bool is_class_path) {
310 assert(UseSharedSpaces, "runtime only");
311
312 struct stat st;
313 const char* name = this->name();
314
315 bool ok = true;
316 log_info(class, path)("checking shared classpath entry: %s", name);
317 if (os::stat(name, &st) != 0 && is_class_path) {
318 // If the archived module path entry does not exist at runtime, it is not fatal
319 // (no need to invalid the shared archive) because the shared runtime visibility check
320 // filters out any archived module classes that do not have a matching runtime
321 // module path location.
322 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
323 ok = false;
324 } else if (is_dir()) {
325 if (!os::dir_is_empty(name)) {
326 FileMapInfo::fail_continue("directory is not empty: %s", name);
327 ok = false;
328 }
329 } else if ((has_timestamp() && _timestamp != st.st_mtime) ||
330 _filesize != st.st_size) {
331 ok = false;
332 if (PrintSharedArchiveAndExit) {
333 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
401 i++;
402 }
403 assert(i == ClassLoader::num_boot_classpath_entries(),
404 "number of boot class path entry mismatch");
405
406 // 2. app class path
407 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
408 while (acpe != NULL) {
409 log_info(class, path)("add app shared path %s", acpe->name());
410 SharedClassPathEntry* ent = shared_path(i);
411 ent->init(false, acpe, THREAD);
412 EXCEPTION_MARK;
413 update_shared_classpath(acpe, ent, THREAD);
414 acpe = acpe->next();
415 i++;
416 }
417
418 // 3. module path
419 ClassPathEntry *mpe = ClassLoader::module_path_entries();
420 while (mpe != NULL) {
421 log_info(class, path)("add module path %s", mpe->name());
422 SharedClassPathEntry* ent = shared_path(i);
423 ent->init(false, mpe, THREAD);
424 EXCEPTION_MARK;
425 update_shared_classpath(mpe, ent, THREAD);
426 mpe = mpe->next();
427 i++;
428 }
429 assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
430 }
431
432 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
433 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
434
435 bool has_nonempty_dir = false;
436
437 int last = _shared_path_table.size() - 1;
438 if (last > ClassLoaderExt::max_used_path_index()) {
439 // no need to check any path beyond max_used_path_index
440 last = ClassLoaderExt::max_used_path_index();
441 }
691 if (rp_len < shared_app_paths_len) {
692 return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
693 }
694 if (shared_app_paths_len != 0 && rp_len != 0) {
695 // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
696 ResourceMark rm;
697 GrowableArray<char*>* rp_array = create_path_array(appcp);
698 if (rp_array->length() == 0) {
699 // None of the jar file specified in the runtime -cp exists.
700 return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
701 }
702 int j = _header->_app_class_paths_start_index;
703 mismatch = check_paths(j, shared_app_paths_len, rp_array);
704 if (mismatch) {
705 return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
706 }
707 }
708 return true;
709 }
710
711 void FileMapInfo::log_paths(const char* msg, int start_idx, int end_idx) {
712 LogTarget(Info, class, path) lt;
713 if (lt.is_enabled()) {
714 LogStream ls(lt);
715 ls.print("%s", msg);
716 const char* prefix = "";
717 for (int i = start_idx; i < end_idx; i++) {
718 ls.print("%s%s", prefix, shared_path(i)->name());
719 prefix = os::path_separator();
720 }
721 ls.cr();
722 }
723 }
724
725 bool FileMapInfo::validate_shared_path_table() {
726 assert(UseSharedSpaces, "runtime only");
727
728 _validating_shared_path_table = true;
729
730 // Load the shared path table info from the archive header
731 _shared_path_table = _header->_shared_path_table;
732 if (DynamicDumpSharedSpaces) {
733 // Only support dynamic dumping with the usage of the default CDS archive
734 // or a simple base archive.
735 // If the base layer archive contains additional path component besides
736 // the runtime image and the -cp, dynamic dumping is disabled.
737 //
738 // When dynamic archiving is enabled, the _shared_path_table is overwritten
739 // to include the application path and stored in the top layer archive.
740 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
741 if (_header->_app_class_paths_start_index > 1) {
742 DynamicDumpSharedSpaces = false;
743 warning(
744 "Dynamic archiving is disabled because base layer archive has appended boot classpath");
745 }
746 if (_header->_num_module_paths > 0) {
747 DynamicDumpSharedSpaces = false;
748 warning(
749 "Dynamic archiving is disabled because base layer archive has module path");
750 }
751 }
752
753 log_paths("Expecting BOOT path=", 0, _header->_app_class_paths_start_index);
754 log_paths("Expecting -Djava.class.path=", _header->_app_class_paths_start_index, _header->_app_module_paths_start_index);
755
756 int module_paths_start_index = _header->_app_module_paths_start_index;
757 int shared_app_paths_len = 0;
758
759 // validate the path entries up to the _max_used_path_index
760 for (int i=0; i < _header->_max_used_path_index + 1; i++) {
761 if (i < module_paths_start_index) {
762 if (shared_path(i)->validate()) {
763 // Only count the app class paths not from the "Class-path" attribute of a jar manifest.
764 if (!shared_path(i)->from_class_path_attr() && i >= _header->_app_class_paths_start_index) {
765 shared_app_paths_len++;
766 }
767 log_info(class, path)("ok");
768 } else {
769 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
770 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
771 }
772 return false;
773 }
774 } else if (i >= module_paths_start_index) {
775 if (shared_path(i)->validate(false /* not a class path entry */)) {
859 // read the header as a dynamic archive header
860 size_t sz = sizeof(DynamicArchiveHeader);
861 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal);
862 size_t n = os::read(fd, dynamic_header, (unsigned int)sz);
863 if (n != sz) {
864 fail_continue("Unable to read the file header.");
865 os::free(dynamic_header);
866 os::close(fd);
867 return false;
868 }
869 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
870 // Not a dynamic header, no need to proceed further.
871 *size = 0;
872 os::free(dynamic_header);
873 os::close(fd);
874 return false;
875 }
876 if (dynamic_header->_base_archive_is_default) {
877 *base_archive_name = Arguments::get_default_shared_archive_path();
878 } else {
879 // read the base archive name
880 size_t name_size = dynamic_header->_base_archive_name_size;
881 if (name_size == 0) {
882 os::free(dynamic_header);
883 os::close(fd);
884 return false;
885 }
886 *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal);
887 n = os::read(fd, *base_archive_name, (unsigned int)name_size);
888 if (n != name_size) {
889 fail_continue("Unable to read the base archive name from the header.");
890 FREE_C_HEAP_ARRAY(char, *base_archive_name);
891 *base_archive_name = NULL;
892 os::free(dynamic_header);
893 os::close(fd);
894 return false;
895 }
896 }
897
898 os::free(dynamic_header);
949 char header_version[JVM_IDENT_MAX];
950 get_header_version(header_version);
951 if (strncmp(_header->_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
952 log_info(cds)("_jvm_ident expected: %s", header_version);
953 log_info(cds)(" actual: %s", _header->_jvm_ident);
954 FileMapInfo::fail_continue("The shared archive file was created by a different"
955 " version or build of HotSpot");
956 return false;
957 }
958
959 if (VerifySharedSpaces) {
960 int expected_crc = _header->compute_crc();
961 if (expected_crc != _header->_crc) {
962 log_info(cds)("_crc expected: %d", expected_crc);
963 log_info(cds)(" actual: %d", _header->_crc);
964 FileMapInfo::fail_continue("Header checksum verification failed.");
965 return false;
966 }
967 }
968
969 _file_offset = n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name
970
971 if (is_static) {
972 // just checking the last region is sufficient since the archive is written
973 // in sequential order
974 size_t len = lseek(fd, 0, SEEK_END);
975 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
976 // The last space might be empty
977 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
978 fail_continue("The shared archive file has been truncated.");
979 return false;
980 }
981
982 SharedBaseAddress = _header->_shared_base_address;
983 }
984
985 return true;
986 }
987
988
989 // Read the FileMapInfo information from the file.
1031 chmod(_full_path, _S_IREAD | _S_IWRITE);
1032 #endif
1033
1034 // Use remove() to delete the existing file because, on Unix, this will
1035 // allow processes that have it open continued access to the file.
1036 remove(_full_path);
1037 int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
1038 if (fd < 0) {
1039 fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
1040 os::strerror(errno));
1041 }
1042 _fd = fd;
1043 _file_offset = 0;
1044 _file_open = true;
1045 }
1046
1047
1048 // Write the header to the file, seek to the next allocation boundary.
1049
1050 void FileMapInfo::write_header() {
1051 char* base_archive_name = NULL;
1052 if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1053 base_archive_name = (char*)Arguments::GetSharedArchivePath();
1054 _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1;
1055 _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile);
1056 }
1057
1058 assert(is_file_position_aligned(), "must be");
1059 write_bytes(_header, _header->_header_size);
1060 if (base_archive_name != NULL) {
1061 write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size);
1062 }
1063 align_file_position();
1064 }
1065
1066 // Dump region to file.
1067 // This is called twice for each region during archiving, once before
1068 // the archive file is open (_file_open is false) and once after.
1069 void FileMapInfo::write_region(int region, char* base, size_t size,
1070 bool read_only, bool allow_exec) {
1071 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only");
1072
1073 CDSFileMapRegion* si = space_at(region);
1074 char* target_base = base;
1075 if (DynamicDumpSharedSpaces) {
1076 target_base = DynamicArchive::buffer_to_target(base);
1077 }
1078
1079 if (_file_open) {
1736 }
1737
1738 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1739 _shared_path_table.metaspace_pointers_do(it);
1740 }
1741
1742 FileMapInfo* FileMapInfo::_current_info = NULL;
1743 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
1744 bool FileMapInfo::_heap_pointers_need_patching = false;
1745 SharedPathTable FileMapInfo::_shared_path_table;
1746 bool FileMapInfo::_validating_shared_path_table = false;
1747 bool FileMapInfo::_memory_mapping_failed = false;
1748
1749 // Open the shared archive file, read and validate the header
1750 // information (version, boot classpath, etc.). If initialization
1751 // fails, shared spaces are disabled and the file is closed. [See
1752 // fail_continue.]
1753 //
1754 // Validation of the archive is done in two steps:
1755 //
1756 // [1] validate_header() - done here.
1757 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1758 // region of the archive, which is not mapped yet.
1759 bool FileMapInfo::initialize(bool is_static) {
1760 assert(UseSharedSpaces, "UseSharedSpaces expected.");
1761
1762 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1763 // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
1764 // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
1765 // during the JVMTI "early" stage, so we can still use CDS if
1766 // JvmtiExport::has_early_class_hook_env() is false.
1767 FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
1768 return false;
1769 }
1770
1771 if (!open_for_read()) {
1772 return false;
1773 }
1774
1775 init_from_file(_fd, is_static);
1776 // UseSharedSpaces could be disabled if the checking of some of the header fields in
1840
1841 // Java agents are allowed during run time. Therefore, the following condition is not
1842 // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
1843 // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
1844 // while AllowArchivingWithJavaAgent is set during the current run.
1845 if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
1846 FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
1847 "from the setting in the shared archive.");
1848 return false;
1849 }
1850
1851 if (_allow_archiving_with_java_agent) {
1852 warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1853 "for testing purposes only and should not be used in a production environment");
1854 }
1855
1856 return true;
1857 }
1858
1859 bool FileMapInfo::validate_header(bool is_static) {
1860 return _header->validate();
1861 }
1862
1863 // Check if a given address is within one of the shared regions
1864 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1865 assert(idx == MetaspaceShared::ro ||
1866 idx == MetaspaceShared::rw ||
1867 idx == MetaspaceShared::mc ||
1868 idx == MetaspaceShared::md, "invalid region index");
1869 char* base = region_addr(idx);
1870 if (p >= base && p < base + space_at(idx)->_used) {
1871 return true;
1872 }
1873 return false;
1874 }
1875
1876 // Unmap mapped regions of shared space.
1877 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1878 MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
1879
1880 FileMapInfo *map_info = FileMapInfo::current_info();
1892 // Dealloc the archive heap regions only without unmapping. The regions are part
1893 // of the java heap. Unmapping of the heap regions are managed by GC.
1894 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1895 num_open_archive_heap_ranges,
1896 true);
1897 map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
1898 num_closed_archive_heap_ranges,
1899 false);
1900 } else if (DumpSharedSpaces) {
1901 fail_stop("%s", msg);
1902 }
1903 }
1904
1905 #if INCLUDE_JVMTI
1906 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
1907
1908 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
1909 ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
1910 if (ent == NULL) {
1911 if (i == 0) {
1912 ent = ClassLoader::get_jrt_entry();
1913 assert(ent != NULL, "must be");
1914 } else {
1915 SharedClassPathEntry* scpe = shared_path(i);
1916 assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
1917
1918 const char* path = scpe->name();
1919 struct stat st;
1920 if (os::stat(path, &st) != 0) {
1921 char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ;
1922 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
1923 THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
1924 } else {
1925 ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, false, CHECK_NULL);
1926 }
1927 }
1928
1929 MutexLocker mu(CDSClassFileStream_lock, THREAD);
1930 if (_classpath_entries_for_jvmti[i] == NULL) {
1931 _classpath_entries_for_jvmti[i] = ent;
1932 } else {
|