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