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;
264 _from_class_path_attr = false;
265
266 struct stat st;
267 if (os::stat(cpe->name(), &st) == 0) {
268 if ((st.st_mode & S_IFMT) == S_IFDIR) {
269 _type = dir_entry;
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 ?
330 "Timestamp mismatch" :
331 "File size mismatch");
332 } else {
333 FileMapInfo::fail_continue("A jar file is not the one used while building"
334 " the shared archive file: %s", name);
335 }
336 }
337
338 if (PrintSharedArchiveAndExit && !ok) {
339 // If PrintSharedArchiveAndExit is enabled, don't report failure to the
340 // caller. Please see above comments for more details.
341 ok = true;
342 MetaspaceShared::set_archive_loading_failed();
343 }
344 return ok;
345 }
346
347 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
348 it->push(&_name);
349 it->push(&_manifest);
350 }
351
352 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) {
353 it->push(&_table);
354 for (int i=0; i<_size; i++) {
355 path_at(i)->metaspace_pointers_do(it);
356 }
357 }
358
359 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) {
360 size_t entry_size = sizeof(SharedClassPathEntry);
361 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
362 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
363 int num_module_path_entries = ClassLoader::num_module_path_entries();
364 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
365 size_t bytes = entry_size * num_entries;
366
367 _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
368 _size = num_entries;
369 }
370
371 void FileMapInfo::allocate_shared_path_table() {
372 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity");
373
374 Thread* THREAD = Thread::current();
375 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
376 ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
377
378 assert(jrt != NULL,
379 "No modular java runtime image present when allocating the CDS classpath entry table");
380
381 _shared_path_table.dumptime_init(loader_data, THREAD);
382
383 // 1. boot class path
384 int i = 0;
385 ClassPathEntry* cpe = jrt;
386 while (cpe != NULL) {
387 bool is_jrt = (cpe == jrt);
388 const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
389 log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
390 SharedClassPathEntry* ent = shared_path(i);
391 ent->init(is_jrt, cpe, THREAD);
392 if (!is_jrt) { // No need to do the modules image.
393 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
394 update_shared_classpath(cpe, ent, THREAD);
395 }
396 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
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 }
438
439 for (int i = 0; i <= last; i++) {
440 SharedClassPathEntry *e = shared_path(i);
441 if (e->is_dir()) {
442 const char* path = e->name();
443 if (!os::dir_is_empty(path)) {
444 tty->print_cr("Error: non-empty directory '%s'", path);
445 has_nonempty_dir = true;
446 }
447 }
448 }
449
450 if (has_nonempty_dir) {
451 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
452 }
453 }
454
455 class ManifestStream: public ResourceObj {
456 private:
457 u1* _buffer_start; // Buffer bottom
458 u1* _buffer_end; // Buffer top (one past last element)
459 u1* _current; // Current buffer position
460
461 public:
462 // Constructor
463 ManifestStream(u1* buffer, int length) : _buffer_start(buffer),
464 _current(buffer) {
465 _buffer_end = buffer + length;
466 }
467
468 static bool is_attr(u1* attr, const char* name) {
469 return strncmp((const char*)attr, name, strlen(name)) == 0;
470 }
471
472 static char* copy_attr(u1* value, size_t len) {
473 char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
474 strncpy(buf, (char*)value, len);
483 while (_current < _buffer_end) {
484 if (*_current == '\n') {
485 *_current = '\0';
486 u1* value = (u1*)strchr((char*)attr, ':');
487 if (value != NULL) {
488 assert(*(value+1) == ' ', "Unrecognized format" );
489 if (strstr((char*)attr, "-Digest") != NULL) {
490 isSigned = true;
491 break;
492 }
493 }
494 *_current = '\n'; // restore
495 attr = _current + 1;
496 }
497 _current ++;
498 }
499 return isSigned;
500 }
501 };
502
503 void FileMapInfo::update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
504 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
505 ResourceMark rm(THREAD);
506 jint manifest_size;
507
508 if (cpe->is_jar_file()) {
509 assert(ent->is_jar(), "the shared class path entry is not a JAR file");
510 char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
511 if (manifest != NULL) {
512 ManifestStream* stream = new ManifestStream((u1*)manifest,
513 manifest_size);
514 if (stream->check_is_signed()) {
515 ent->set_is_signed();
516 } else {
517 // Copy the manifest into the shared archive
518 manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
519 Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
520 manifest_size,
521 THREAD);
522 char* p = (char*)(buf->data());
523 memcpy(p, manifest, manifest_size);
524 ent->set_manifest(buf);
525 }
526 }
527 }
528 }
529
530 char* FileMapInfo::skip_first_path_entry(const char* path) {
531 size_t path_sep_len = strlen(os::path_separator());
532 char* p = strstr((char*)path, os::path_separator());
533 if (p != NULL) {
534 debug_only( {
535 size_t image_name_len = strlen(MODULES_IMAGE_NAME);
536 assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0,
537 "first entry must be the modules image");
538 } );
539 p += path_sep_len;
540 } else {
541 debug_only( {
542 assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME),
543 "first entry must be the modules image");
544 } );
545 }
546 return p;
547 }
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 */)) {
755 log_info(class, path)("ok");
756 } else {
757 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
758 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
759 }
760 return false;
761 }
762 }
763 }
764
765 if (_header->_max_used_path_index == 0) {
766 // default archive only contains the module image in the bootclasspath
767 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
768 } else {
769 if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
770 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
771 return false;
772 }
773 }
774
775 _validating_shared_path_table = false;
776
777 #if INCLUDE_JVMTI
778 if (_classpath_entries_for_jvmti != NULL) {
779 os::free(_classpath_entries_for_jvmti);
780 }
781 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
782 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
783 memset((void*)_classpath_entries_for_jvmti, 0, sz);
784 #endif
785
786 return true;
787 }
788
789 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
790 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
791 if (fd < 0) {
792 // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths()
793 // requires a shared archive name. The open_for_read() function will log a message regarding
794 // failure in opening a shared archive.
795 return false;
796 }
797
798 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
799 void* header = os::malloc(sz, mtInternal);
800 memset(header, 0, sz);
801 size_t n = os::read(fd, header, (unsigned int)sz);
802 if (n != sz) {
803 os::free(header);
804 os::close(fd);
805 vm_exit_during_initialization("Unable to read header from shared archive", archive_name);
806 return false;
807 }
808 if (is_static) {
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) {
1726 fail_stop("Unable to unmap shared space.");
1727 }
1728 }
1729
1730 void FileMapInfo::assert_mark(bool check) {
1731 if (!check) {
1732 fail_stop("Mark mismatch while restoring from shared file.");
1733 }
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_as_non_existent(const char* path, TRAPS) {
259 _type = non_existent_entry;
260 set_name(path, THREAD);
261 }
262
263 void SharedClassPathEntry::init(bool is_modules_image,
264 ClassPathEntry* cpe, TRAPS) {
265 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
266 _timestamp = 0;
267 _filesize = 0;
268 _from_class_path_attr = false;
269
270 struct stat st;
271 if (os::stat(cpe->name(), &st) == 0) {
272 if ((st.st_mode & S_IFMT) == S_IFDIR) {
273 _type = dir_entry;
274 } else {
275 // The timestamp of the modules_image is not checked at runtime.
276 if (is_modules_image) {
277 _type = modules_image_entry;
278 } else {
279 _type = jar_entry;
280 _timestamp = st.st_mtime;
281 _from_class_path_attr = cpe->from_class_path_attr();
282 }
283 _filesize = st.st_size;
284 }
285 } else {
286 // The file/dir must exist, or it would not have been added
287 // into ClassLoader::classpath_entry().
288 //
289 // If we can't access a jar file in the boot path, then we can't
290 // make assumptions about where classes get loaded from.
291 FileMapInfo::fail_stop("Unable to open file %s.", cpe->name());
292 }
293
294 // No need to save the name of the module file, as it will be computed at run time
295 // to allow relocation of the JDK directory.
296 const char* name = is_modules_image ? "" : cpe->name();
297 set_name(name, THREAD);
298 }
299
300 void SharedClassPathEntry::set_name(const char* name, TRAPS) {
301 size_t len = strlen(name) + 1;
302 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
303 strcpy(_name->data(), name);
304 }
305
306 const char* SharedClassPathEntry::name() const {
307 if (UseSharedSpaces && is_modules_image()) {
308 // In order to validate the runtime modules image file size against the archived
309 // size information, we need to obtain the runtime modules image path. The recorded
310 // dump time modules image path in the archive may be different from the runtime path
311 // if the JDK image has beed moved after generating the archive.
312 return ClassLoader::get_jrt_entry()->name();
313 } else {
314 return _name->data();
315 }
316 }
317
318 bool SharedClassPathEntry::validate(bool is_class_path) const {
319 assert(UseSharedSpaces, "runtime only");
320
321 struct stat st;
322 const char* name = this->name();
323
324 bool ok = true;
325 log_info(class, path)("checking shared classpath entry: %s", name);
326 if (os::stat(name, &st) != 0 && is_class_path) {
327 // If the archived module path entry does not exist at runtime, it is not fatal
328 // (no need to invalid the shared archive) because the shared runtime visibility check
329 // filters out any archived module classes that do not have a matching runtime
330 // module path location.
331 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
332 ok = false;
333 } else if (is_dir()) {
334 if (!os::dir_is_empty(name)) {
335 FileMapInfo::fail_continue("directory is not empty: %s", name);
336 ok = false;
337 }
338 } else if ((has_timestamp() && _timestamp != st.st_mtime) ||
339 _filesize != st.st_size) {
340 ok = false;
341 if (PrintSharedArchiveAndExit) {
342 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
343 "Timestamp mismatch" :
344 "File size mismatch");
345 } else {
346 FileMapInfo::fail_continue("A jar file is not the one used while building"
347 " the shared archive file: %s", name);
348 }
349 }
350
351 if (PrintSharedArchiveAndExit && !ok) {
352 // If PrintSharedArchiveAndExit is enabled, don't report failure to the
353 // caller. Please see above comments for more details.
354 ok = true;
355 MetaspaceShared::set_archive_loading_failed();
356 }
357 return ok;
358 }
359
360 bool SharedClassPathEntry::check_non_existent() const {
361 assert(_type == non_existent_entry, "must be");
362 log_info(class, path)("should be non-existent: %s", name());
363 struct stat st;
364 if (os::stat(name(), &st) != 0) {
365 log_info(class, path)("ok");
366 return true; // file doesn't exist
367 } else {
368 return false;
369 }
370 }
371
372
373 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
374 it->push(&_name);
375 it->push(&_manifest);
376 }
377
378 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) {
379 it->push(&_table);
380 for (int i=0; i<_size; i++) {
381 path_at(i)->metaspace_pointers_do(it);
382 }
383 }
384
385 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) {
386 size_t entry_size = sizeof(SharedClassPathEntry);
387 int num_entries = 0;
388 num_entries += ClassLoader::num_boot_classpath_entries();
389 num_entries += ClassLoader::num_app_classpath_entries();
390 num_entries += ClassLoader::num_module_path_entries();
391 num_entries += FileMapInfo::num_non_existent_class_paths();
392 size_t bytes = entry_size * num_entries;
393
394 _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
395 _size = num_entries;
396 }
397
398 void FileMapInfo::allocate_shared_path_table() {
399 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity");
400
401 EXCEPTION_MARK; // The following calls should never throw, but would exit VM on error.
402 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
403 ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
404
405 assert(jrt != NULL,
406 "No modular java runtime image present when allocating the CDS classpath entry table");
407
408 _shared_path_table.dumptime_init(loader_data, THREAD);
409
410 // 1. boot class path
411 int i = 0;
412 i = add_shared_classpaths(i, "boot", jrt, THREAD);
413 i = add_shared_classpaths(i, "app", ClassLoader::app_classpath_entries(), THREAD);
414 i = add_shared_classpaths(i, "module", ClassLoader::module_path_entries(), THREAD);
415
416 for (int x = 0; x < num_non_existent_class_paths(); x++, i++) {
417 const char* path = _non_existent_class_paths->at(x);
418 shared_path(i)->init_as_non_existent(path, THREAD);
419 }
420
421 assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
422 }
423
424 int FileMapInfo::add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS) {
425 while (cpe != NULL) {
426 bool is_jrt = (cpe == ClassLoader::get_jrt_entry());
427 const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
428 log_info(class, path)("add %s shared path (%s) %s", which, type, cpe->name());
429 SharedClassPathEntry* ent = shared_path(i);
430 ent->init(is_jrt, cpe, THREAD);
431 if (cpe->is_jar_file()) {
432 update_jar_manifest(cpe, ent, THREAD);
433 }
434 if (is_jrt) {
435 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
436 } else {
437 cpe = cpe->next();
438 }
439 i++;
440 }
441
442 return i;
443 }
444
445 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
446 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
447
448 bool has_nonempty_dir = false;
449
450 int last = _shared_path_table.size() - 1;
451 if (last > ClassLoaderExt::max_used_path_index()) {
452 // no need to check any path beyond max_used_path_index
453 last = ClassLoaderExt::max_used_path_index();
454 }
455
456 for (int i = 0; i <= last; i++) {
457 SharedClassPathEntry *e = shared_path(i);
458 if (e->is_dir()) {
459 const char* path = e->name();
460 if (!os::dir_is_empty(path)) {
461 tty->print_cr("Error: non-empty directory '%s'", path);
462 has_nonempty_dir = true;
463 }
464 }
465 }
466
467 if (has_nonempty_dir) {
468 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
469 }
470 }
471
472 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
473 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
474 log_info(class, path)("non-existent Class-Path entry %s", path);
475 if (_non_existent_class_paths == NULL) {
476 _non_existent_class_paths = new (ResourceObj::C_HEAP, mtInternal)GrowableArray<const char*>(10, true);
477 }
478 _non_existent_class_paths->append(os::strdup(path));
479 }
480
481 int FileMapInfo::num_non_existent_class_paths() {
482 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
483 if (_non_existent_class_paths != NULL) {
484 return _non_existent_class_paths->length();
485 } else {
486 return 0;
487 }
488 }
489
490 class ManifestStream: public ResourceObj {
491 private:
492 u1* _buffer_start; // Buffer bottom
493 u1* _buffer_end; // Buffer top (one past last element)
494 u1* _current; // Current buffer position
495
496 public:
497 // Constructor
498 ManifestStream(u1* buffer, int length) : _buffer_start(buffer),
499 _current(buffer) {
500 _buffer_end = buffer + length;
501 }
502
503 static bool is_attr(u1* attr, const char* name) {
504 return strncmp((const char*)attr, name, strlen(name)) == 0;
505 }
506
507 static char* copy_attr(u1* value, size_t len) {
508 char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
509 strncpy(buf, (char*)value, len);
518 while (_current < _buffer_end) {
519 if (*_current == '\n') {
520 *_current = '\0';
521 u1* value = (u1*)strchr((char*)attr, ':');
522 if (value != NULL) {
523 assert(*(value+1) == ' ', "Unrecognized format" );
524 if (strstr((char*)attr, "-Digest") != NULL) {
525 isSigned = true;
526 break;
527 }
528 }
529 *_current = '\n'; // restore
530 attr = _current + 1;
531 }
532 _current ++;
533 }
534 return isSigned;
535 }
536 };
537
538 void FileMapInfo::update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
539 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
540 ResourceMark rm(THREAD);
541 jint manifest_size;
542
543 assert(cpe->is_jar_file() && ent->is_jar(), "the shared class path entry is not a JAR file");
544 char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
545 if (manifest != NULL) {
546 ManifestStream* stream = new ManifestStream((u1*)manifest,
547 manifest_size);
548 if (stream->check_is_signed()) {
549 ent->set_is_signed();
550 } else {
551 // Copy the manifest into the shared archive
552 manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
553 Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
554 manifest_size,
555 THREAD);
556 char* p = (char*)(buf->data());
557 memcpy(p, manifest, manifest_size);
558 ent->set_manifest(buf);
559 }
560 }
561 }
562
563 char* FileMapInfo::skip_first_path_entry(const char* path) {
564 size_t path_sep_len = strlen(os::path_separator());
565 char* p = strstr((char*)path, os::path_separator());
566 if (p != NULL) {
567 debug_only( {
568 size_t image_name_len = strlen(MODULES_IMAGE_NAME);
569 assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0,
570 "first entry must be the modules image");
571 } );
572 p += path_sep_len;
573 } else {
574 debug_only( {
575 assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME),
576 "first entry must be the modules image");
577 } );
578 }
579 return p;
580 }
720 if (rp_len < shared_app_paths_len) {
721 return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
722 }
723 if (shared_app_paths_len != 0 && rp_len != 0) {
724 // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
725 ResourceMark rm;
726 GrowableArray<char*>* rp_array = create_path_array(appcp);
727 if (rp_array->length() == 0) {
728 // None of the jar file specified in the runtime -cp exists.
729 return fail("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
730 }
731 int j = _header->_app_class_paths_start_index;
732 mismatch = check_paths(j, shared_app_paths_len, rp_array);
733 if (mismatch) {
734 return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
735 }
736 }
737 return true;
738 }
739
740 void FileMapInfo::log_paths(const char* msg, int start_idx, int end_idx) {
741 LogTarget(Info, class, path) lt;
742 if (lt.is_enabled()) {
743 LogStream ls(lt);
744 ls.print("%s", msg);
745 const char* prefix = "";
746 for (int i = start_idx; i < end_idx; i++) {
747 ls.print("%s%s", prefix, shared_path(i)->name());
748 prefix = os::path_separator();
749 }
750 ls.cr();
751 }
752 }
753
754 bool FileMapInfo::validate_shared_path_table() {
755 assert(UseSharedSpaces, "runtime only");
756
757 _validating_shared_path_table = true;
758
759 // Load the shared path table info from the archive header
760 _shared_path_table = _header->_shared_path_table;
761 if (DynamicDumpSharedSpaces) {
762 // Only support dynamic dumping with the usage of the default CDS archive
763 // or a simple base archive.
764 // If the base layer archive contains additional path component besides
765 // the runtime image and the -cp, dynamic dumping is disabled.
766 //
767 // When dynamic archiving is enabled, the _shared_path_table is overwritten
768 // to include the application path and stored in the top layer archive.
769 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
770 if (_header->_app_class_paths_start_index > 1) {
771 DynamicDumpSharedSpaces = false;
772 warning(
773 "Dynamic archiving is disabled because base layer archive has appended boot classpath");
774 }
775 if (_header->_num_module_paths > 0) {
776 DynamicDumpSharedSpaces = false;
777 warning(
778 "Dynamic archiving is disabled because base layer archive has module path");
779 }
780 }
781
782 log_paths("Expecting BOOT path=", 0, _header->_app_class_paths_start_index);
783 log_paths("Expecting -Djava.class.path=", _header->_app_class_paths_start_index, _header->_app_module_paths_start_index);
784
785 int module_paths_start_index = _header->_app_module_paths_start_index;
786 int shared_app_paths_len = 0;
787
788 // validate the path entries up to the _max_used_path_index
789 for (int i=0; i < _header->_max_used_path_index + 1; i++) {
790 if (i < module_paths_start_index) {
791 if (shared_path(i)->validate()) {
792 // Only count the app class paths not from the "Class-path" attribute of a jar manifest.
793 if (!shared_path(i)->from_class_path_attr() && i >= _header->_app_class_paths_start_index) {
794 shared_app_paths_len++;
795 }
796 log_info(class, path)("ok");
797 } else {
798 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
799 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
800 }
801 return false;
802 }
803 } else if (i >= module_paths_start_index) {
804 if (shared_path(i)->validate(false /* not a class path entry */)) {
805 log_info(class, path)("ok");
806 } else {
807 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
808 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
809 }
810 return false;
811 }
812 }
813 }
814
815 if (_header->_max_used_path_index == 0) {
816 // default archive only contains the module image in the bootclasspath
817 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
818 } else {
819 if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
820 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
821 return false;
822 }
823 }
824
825 if (!validate_non_existent_class_paths()) {
826 return false;
827 }
828
829 _validating_shared_path_table = false;
830
831 #if INCLUDE_JVMTI
832 if (_classpath_entries_for_jvmti != NULL) {
833 os::free(_classpath_entries_for_jvmti);
834 }
835 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
836 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
837 memset((void*)_classpath_entries_for_jvmti, 0, sz);
838 #endif
839
840 return true;
841 }
842
843 bool FileMapInfo::validate_non_existent_class_paths() const {
844 assert(UseSharedSpaces, "runtime only");
845 for (int i = _header->_app_module_paths_start_index + _header->_num_module_paths;
846 i < get_number_of_shared_paths();
847 i++) {
848 SharedClassPathEntry* ent = shared_path(i);
849 if (!ent->check_non_existent()) {
850 fail_continue("file must not exist: %s", ent->name());
851 return false;
852 }
853 }
854 return true;
855 }
856
857
858
859 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
860 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
861 if (fd < 0) {
862 // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths()
863 // requires a shared archive name. The open_for_read() function will log a message regarding
864 // failure in opening a shared archive.
865 return false;
866 }
867
868 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
869 void* header = os::malloc(sz, mtInternal);
870 memset(header, 0, sz);
871 size_t n = os::read(fd, header, (unsigned int)sz);
872 if (n != sz) {
873 os::free(header);
874 os::close(fd);
875 vm_exit_during_initialization("Unable to read header from shared archive", archive_name);
876 return false;
877 }
878 if (is_static) {
908 // read the header as a dynamic archive header
909 size_t sz = sizeof(DynamicArchiveHeader);
910 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal);
911 size_t n = os::read(fd, dynamic_header, (unsigned int)sz);
912 if (n != sz) {
913 fail_continue("Unable to read the file header.");
914 os::free(dynamic_header);
915 os::close(fd);
916 return false;
917 }
918 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
919 // Not a dynamic header, no need to proceed further.
920 *size = 0;
921 os::free(dynamic_header);
922 os::close(fd);
923 return false;
924 }
925 if (dynamic_header->_base_archive_is_default) {
926 *base_archive_name = Arguments::get_default_shared_archive_path();
927 } else {
928 // read the base archive name
929 size_t name_size = dynamic_header->_base_archive_name_size;
930 if (name_size == 0) {
931 os::free(dynamic_header);
932 os::close(fd);
933 return false;
934 }
935 *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal);
936 n = os::read(fd, *base_archive_name, (unsigned int)name_size);
937 if (n != name_size) {
938 fail_continue("Unable to read the base archive name from the header.");
939 FREE_C_HEAP_ARRAY(char, *base_archive_name);
940 *base_archive_name = NULL;
941 os::free(dynamic_header);
942 os::close(fd);
943 return false;
944 }
945 }
946
947 os::free(dynamic_header);
998 char header_version[JVM_IDENT_MAX];
999 get_header_version(header_version);
1000 if (strncmp(_header->_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
1001 log_info(cds)("_jvm_ident expected: %s", header_version);
1002 log_info(cds)(" actual: %s", _header->_jvm_ident);
1003 FileMapInfo::fail_continue("The shared archive file was created by a different"
1004 " version or build of HotSpot");
1005 return false;
1006 }
1007
1008 if (VerifySharedSpaces) {
1009 int expected_crc = _header->compute_crc();
1010 if (expected_crc != _header->_crc) {
1011 log_info(cds)("_crc expected: %d", expected_crc);
1012 log_info(cds)(" actual: %d", _header->_crc);
1013 FileMapInfo::fail_continue("Header checksum verification failed.");
1014 return false;
1015 }
1016 }
1017
1018 _file_offset = n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name
1019
1020 if (is_static) {
1021 // just checking the last region is sufficient since the archive is written
1022 // in sequential order
1023 size_t len = lseek(fd, 0, SEEK_END);
1024 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
1025 // The last space might be empty
1026 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
1027 fail_continue("The shared archive file has been truncated.");
1028 return false;
1029 }
1030
1031 SharedBaseAddress = _header->_shared_base_address;
1032 }
1033
1034 return true;
1035 }
1036
1037
1038 // Read the FileMapInfo information from the file.
1080 chmod(_full_path, _S_IREAD | _S_IWRITE);
1081 #endif
1082
1083 // Use remove() to delete the existing file because, on Unix, this will
1084 // allow processes that have it open continued access to the file.
1085 remove(_full_path);
1086 int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
1087 if (fd < 0) {
1088 fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
1089 os::strerror(errno));
1090 }
1091 _fd = fd;
1092 _file_offset = 0;
1093 _file_open = true;
1094 }
1095
1096
1097 // Write the header to the file, seek to the next allocation boundary.
1098
1099 void FileMapInfo::write_header() {
1100 char* base_archive_name = NULL;
1101 if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1102 base_archive_name = (char*)Arguments::GetSharedArchivePath();
1103 _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1;
1104 _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile);
1105 }
1106
1107 assert(is_file_position_aligned(), "must be");
1108 write_bytes(_header, _header->_header_size);
1109 if (base_archive_name != NULL) {
1110 write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size);
1111 }
1112 align_file_position();
1113 }
1114
1115 // Dump region to file.
1116 // This is called twice for each region during archiving, once before
1117 // the archive file is open (_file_open is false) and once after.
1118 void FileMapInfo::write_region(int region, char* base, size_t size,
1119 bool read_only, bool allow_exec) {
1120 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only");
1121
1122 CDSFileMapRegion* si = space_at(region);
1123 char* target_base = base;
1124 if (DynamicDumpSharedSpaces) {
1125 target_base = DynamicArchive::buffer_to_target(base);
1126 }
1127
1128 if (_file_open) {
1777 fail_stop("Unable to unmap shared space.");
1778 }
1779 }
1780
1781 void FileMapInfo::assert_mark(bool check) {
1782 if (!check) {
1783 fail_stop("Mark mismatch while restoring from shared file.");
1784 }
1785 }
1786
1787 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
1788 _shared_path_table.metaspace_pointers_do(it);
1789 }
1790
1791 FileMapInfo* FileMapInfo::_current_info = NULL;
1792 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
1793 bool FileMapInfo::_heap_pointers_need_patching = false;
1794 SharedPathTable FileMapInfo::_shared_path_table;
1795 bool FileMapInfo::_validating_shared_path_table = false;
1796 bool FileMapInfo::_memory_mapping_failed = false;
1797 GrowableArray<const char*>* FileMapInfo::_non_existent_class_paths = NULL;
1798
1799 // Open the shared archive file, read and validate the header
1800 // information (version, boot classpath, etc.). If initialization
1801 // fails, shared spaces are disabled and the file is closed. [See
1802 // fail_continue.]
1803 //
1804 // Validation of the archive is done in two steps:
1805 //
1806 // [1] validate_header() - done here.
1807 // [2] validate_shared_path_table - this is done later, because the table is in the RW
1808 // region of the archive, which is not mapped yet.
1809 bool FileMapInfo::initialize(bool is_static) {
1810 assert(UseSharedSpaces, "UseSharedSpaces expected.");
1811
1812 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1813 // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
1814 // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
1815 // during the JVMTI "early" stage, so we can still use CDS if
1816 // JvmtiExport::has_early_class_hook_env() is false.
1817 FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
1818 return false;
1819 }
1820
1821 if (!open_for_read()) {
1822 return false;
1823 }
1824
1825 init_from_file(_fd, is_static);
1826 // UseSharedSpaces could be disabled if the checking of some of the header fields in
1890
1891 // Java agents are allowed during run time. Therefore, the following condition is not
1892 // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
1893 // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
1894 // while AllowArchivingWithJavaAgent is set during the current run.
1895 if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
1896 FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
1897 "from the setting in the shared archive.");
1898 return false;
1899 }
1900
1901 if (_allow_archiving_with_java_agent) {
1902 warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
1903 "for testing purposes only and should not be used in a production environment");
1904 }
1905
1906 return true;
1907 }
1908
1909 bool FileMapInfo::validate_header(bool is_static) {
1910 return _header->validate();
1911 }
1912
1913 // Check if a given address is within one of the shared regions
1914 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
1915 assert(idx == MetaspaceShared::ro ||
1916 idx == MetaspaceShared::rw ||
1917 idx == MetaspaceShared::mc ||
1918 idx == MetaspaceShared::md, "invalid region index");
1919 char* base = region_addr(idx);
1920 if (p >= base && p < base + space_at(idx)->_used) {
1921 return true;
1922 }
1923 return false;
1924 }
1925
1926 // Unmap mapped regions of shared space.
1927 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
1928 MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
1929
1930 FileMapInfo *map_info = FileMapInfo::current_info();
1942 // Dealloc the archive heap regions only without unmapping. The regions are part
1943 // of the java heap. Unmapping of the heap regions are managed by GC.
1944 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
1945 num_open_archive_heap_ranges,
1946 true);
1947 map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
1948 num_closed_archive_heap_ranges,
1949 false);
1950 } else if (DumpSharedSpaces) {
1951 fail_stop("%s", msg);
1952 }
1953 }
1954
1955 #if INCLUDE_JVMTI
1956 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
1957
1958 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
1959 ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
1960 if (ent == NULL) {
1961 if (i == 0) {
1962 ent = ClassLoader::get_jrt_entry();
1963 assert(ent != NULL, "must be");
1964 } else {
1965 SharedClassPathEntry* scpe = shared_path(i);
1966 assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
1967
1968 const char* path = scpe->name();
1969 struct stat st;
1970 if (os::stat(path, &st) != 0) {
1971 char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ;
1972 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
1973 THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
1974 } else {
1975 ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, false, CHECK_NULL);
1976 }
1977 }
1978
1979 MutexLocker mu(CDSClassFileStream_lock, THREAD);
1980 if (_classpath_entries_for_jvmti[i] == NULL) {
1981 _classpath_entries_for_jvmti[i] = ent;
1982 } else {
|