32 #include "classfile/systemDictionaryShared.hpp" |
32 #include "classfile/systemDictionaryShared.hpp" |
33 #include "classfile/altHashing.hpp" |
33 #include "classfile/altHashing.hpp" |
34 #include "logging/log.hpp" |
34 #include "logging/log.hpp" |
35 #include "logging/logStream.hpp" |
35 #include "logging/logStream.hpp" |
36 #include "logging/logMessage.hpp" |
36 #include "logging/logMessage.hpp" |
|
37 #include "memory/dynamicArchive.hpp" |
37 #include "memory/filemap.hpp" |
38 #include "memory/filemap.hpp" |
38 #include "memory/heapShared.inline.hpp" |
39 #include "memory/heapShared.inline.hpp" |
39 #include "memory/iterator.inline.hpp" |
40 #include "memory/iterator.inline.hpp" |
40 #include "memory/metadataFactory.hpp" |
41 #include "memory/metadataFactory.hpp" |
41 #include "memory/metaspaceClosure.hpp" |
42 #include "memory/metaspaceClosure.hpp" |
42 #include "memory/metaspaceShared.hpp" |
43 #include "memory/metaspaceShared.hpp" |
43 #include "memory/oopFactory.hpp" |
44 #include "memory/oopFactory.hpp" |
44 #include "memory/universe.hpp" |
45 #include "memory/universe.hpp" |
|
46 #include "oops/compressedOops.hpp" |
45 #include "oops/compressedOops.inline.hpp" |
47 #include "oops/compressedOops.inline.hpp" |
46 #include "oops/objArrayOop.hpp" |
48 #include "oops/objArrayOop.hpp" |
47 #include "oops/oop.inline.hpp" |
49 #include "oops/oop.inline.hpp" |
48 #include "prims/jvmtiExport.hpp" |
50 #include "prims/jvmtiExport.hpp" |
49 #include "runtime/arguments.hpp" |
51 #include "runtime/arguments.hpp" |
157 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); |
170 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); |
158 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. |
171 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. |
159 } |
172 } |
160 } |
173 } |
161 |
174 |
162 FileMapInfo::FileMapInfo() { |
175 FileMapInfo::FileMapInfo(bool is_static) { |
163 assert(_current_info == NULL, "must be singleton"); // not thread safe |
|
164 _current_info = this; |
|
165 memset((void*)this, 0, sizeof(FileMapInfo)); |
176 memset((void*)this, 0, sizeof(FileMapInfo)); |
|
177 _is_static = is_static; |
|
178 size_t header_size; |
|
179 if (is_static) { |
|
180 assert(_current_info == NULL, "must be singleton"); // not thread safe |
|
181 _current_info = this; |
|
182 header_size = sizeof(FileMapHeader); |
|
183 } else { |
|
184 assert(_dynamic_archive_info == NULL, "must be singleton"); // not thread safe |
|
185 _dynamic_archive_info = this; |
|
186 header_size = sizeof(DynamicArchiveHeader); |
|
187 } |
|
188 _header = (FileMapHeader*)os::malloc(header_size, mtInternal); |
|
189 memset((void*)_header, 0, header_size); |
|
190 _header->_header_size = header_size; |
|
191 _header->_version = INVALID_CDS_ARCHIVE_VERSION; |
|
192 _header->_has_platform_or_app_classes = true; |
166 _file_offset = 0; |
193 _file_offset = 0; |
167 _file_open = false; |
194 _file_open = false; |
168 _header = (FileMapHeader*)os::malloc(sizeof(FileMapHeader), mtInternal); |
|
169 _header->_version = INVALID_CDS_ARCHIVE_VERSION; |
|
170 _header->_has_platform_or_app_classes = true; |
|
171 } |
195 } |
172 |
196 |
173 FileMapInfo::~FileMapInfo() { |
197 FileMapInfo::~FileMapInfo() { |
174 assert(_current_info == this, "must be singleton"); // not thread safe |
198 if (_is_static) { |
175 _current_info = NULL; |
199 assert(_current_info == this, "must be singleton"); // not thread safe |
|
200 _current_info = NULL; |
|
201 } else { |
|
202 assert(_dynamic_archive_info == this, "must be singleton"); // not thread safe |
|
203 _dynamic_archive_info = NULL; |
|
204 } |
176 } |
205 } |
177 |
206 |
178 void FileMapInfo::populate_header(size_t alignment) { |
207 void FileMapInfo::populate_header(size_t alignment) { |
179 _header->populate(this, alignment); |
208 _header->populate(this, alignment); |
180 } |
209 } |
181 |
210 |
182 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { |
211 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) { |
183 _magic = CDS_ARCHIVE_MAGIC; |
212 if (DynamicDumpSharedSpaces) { |
|
213 _magic = CDS_DYNAMIC_ARCHIVE_MAGIC; |
|
214 } else { |
|
215 _magic = CDS_ARCHIVE_MAGIC; |
|
216 } |
184 _version = CURRENT_CDS_ARCHIVE_VERSION; |
217 _version = CURRENT_CDS_ARCHIVE_VERSION; |
185 _alignment = alignment; |
218 _alignment = alignment; |
186 _obj_alignment = ObjectAlignmentInBytes; |
219 _obj_alignment = ObjectAlignmentInBytes; |
187 _compact_strings = CompactStrings; |
220 _compact_strings = CompactStrings; |
188 _narrow_oop_mode = CompressedOops::mode(); |
221 _narrow_oop_mode = CompressedOops::mode(); |
189 _narrow_oop_base = CompressedOops::base(); |
222 _narrow_oop_base = CompressedOops::base(); |
190 _narrow_oop_shift = CompressedOops::shift(); |
223 _narrow_oop_shift = CompressedOops::shift(); |
191 _max_heap_size = MaxHeapSize; |
224 _max_heap_size = MaxHeapSize; |
192 _narrow_klass_base = CompressedKlassPointers::base(); |
225 _narrow_klass_base = CompressedKlassPointers::base(); |
193 _narrow_klass_shift = CompressedKlassPointers::shift(); |
226 _narrow_klass_shift = CompressedKlassPointers::shift(); |
194 _shared_path_table_size = mapinfo->_shared_path_table_size; |
|
195 _shared_path_table = mapinfo->_shared_path_table; |
227 _shared_path_table = mapinfo->_shared_path_table; |
196 _shared_path_entry_size = mapinfo->_shared_path_entry_size; |
|
197 if (HeapShared::is_heap_object_archiving_allowed()) { |
228 if (HeapShared::is_heap_object_archiving_allowed()) { |
198 _heap_reserved = Universe::heap()->reserved_region(); |
229 _heap_reserved = Universe::heap()->reserved_region(); |
199 } |
230 } |
200 |
231 |
201 // The following fields are for sanity checks for whether this archive |
232 // The following fields are for sanity checks for whether this archive |
206 get_header_version(_jvm_ident); |
237 get_header_version(_jvm_ident); |
207 |
238 |
208 ClassLoaderExt::finalize_shared_paths_misc_info(); |
239 ClassLoaderExt::finalize_shared_paths_misc_info(); |
209 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index(); |
240 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index(); |
210 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index(); |
241 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index(); |
|
242 _num_module_paths = ClassLoader::num_module_path_entries(); |
211 _max_used_path_index = ClassLoaderExt::max_used_path_index(); |
243 _max_used_path_index = ClassLoaderExt::max_used_path_index(); |
212 |
244 |
213 _verify_local = BytecodeVerificationLocal; |
245 _verify_local = BytecodeVerificationLocal; |
214 _verify_remote = BytecodeVerificationRemote; |
246 _verify_remote = BytecodeVerificationRemote; |
215 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes(); |
247 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes(); |
216 _shared_base_address = SharedBaseAddress; |
248 _shared_base_address = SharedBaseAddress; |
217 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent; |
249 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent; |
|
250 // the following 2 fields will be set in write_header for dynamic archive header |
|
251 _base_archive_name_size = 0; |
|
252 _base_archive_is_default = false; |
218 } |
253 } |
219 |
254 |
220 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) { |
255 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) { |
221 assert(DumpSharedSpaces, "dump time only"); |
256 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only"); |
222 _timestamp = 0; |
257 _timestamp = 0; |
223 _filesize = 0; |
258 _filesize = 0; |
224 |
259 |
225 struct stat st; |
260 struct stat st; |
226 if (os::stat(name, &st) == 0) { |
261 if (os::stat(name, &st) == 0) { |
295 |
330 |
296 if (PrintSharedArchiveAndExit && !ok) { |
331 if (PrintSharedArchiveAndExit && !ok) { |
297 // If PrintSharedArchiveAndExit is enabled, don't report failure to the |
332 // If PrintSharedArchiveAndExit is enabled, don't report failure to the |
298 // caller. Please see above comments for more details. |
333 // caller. Please see above comments for more details. |
299 ok = true; |
334 ok = true; |
|
335 MetaspaceShared::set_archive_loading_failed(); |
300 } |
336 } |
301 return ok; |
337 return ok; |
302 } |
338 } |
303 |
339 |
304 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) { |
340 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) { |
305 it->push(&_name); |
341 it->push(&_name); |
306 it->push(&_manifest); |
342 it->push(&_manifest); |
307 } |
343 } |
308 |
344 |
309 void FileMapInfo::allocate_shared_path_table() { |
345 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) { |
310 assert(DumpSharedSpaces, "Sanity"); |
346 it->push(&_table); |
311 |
347 for (int i=0; i<_size; i++) { |
312 Thread* THREAD = Thread::current(); |
348 path_at(i)->metaspace_pointers_do(it); |
313 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); |
349 } |
314 ClassPathEntry* jrt = ClassLoader::get_jrt_entry(); |
350 } |
315 |
351 |
316 assert(jrt != NULL, |
352 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) { |
317 "No modular java runtime image present when allocating the CDS classpath entry table"); |
353 size_t entry_size = sizeof(SharedClassPathEntry); |
318 |
|
319 size_t entry_size = sizeof(SharedClassPathEntry); // assert ( should be 8 byte aligned??) |
|
320 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries(); |
354 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries(); |
321 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries(); |
355 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries(); |
322 int num_module_path_entries = ClassLoader::num_module_path_entries(); |
356 int num_module_path_entries = ClassLoader::num_module_path_entries(); |
323 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries; |
357 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries; |
324 size_t bytes = entry_size * num_entries; |
358 size_t bytes = entry_size * num_entries; |
325 |
359 |
326 _shared_path_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD); |
360 _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD); |
327 _shared_path_table_size = num_entries; |
361 _size = num_entries; |
328 _shared_path_entry_size = entry_size; |
362 } |
|
363 |
|
364 void FileMapInfo::allocate_shared_path_table() { |
|
365 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity"); |
|
366 |
|
367 Thread* THREAD = Thread::current(); |
|
368 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); |
|
369 ClassPathEntry* jrt = ClassLoader::get_jrt_entry(); |
|
370 |
|
371 assert(jrt != NULL, |
|
372 "No modular java runtime image present when allocating the CDS classpath entry table"); |
|
373 |
|
374 _shared_path_table.dumptime_init(loader_data, THREAD); |
329 |
375 |
330 // 1. boot class path |
376 // 1. boot class path |
331 int i = 0; |
377 int i = 0; |
332 ClassPathEntry* cpe = jrt; |
378 ClassPathEntry* cpe = jrt; |
333 while (cpe != NULL) { |
379 while (cpe != NULL) { |
477 |
523 |
478 bool FileMapInfo::validate_shared_path_table() { |
524 bool FileMapInfo::validate_shared_path_table() { |
479 assert(UseSharedSpaces, "runtime only"); |
525 assert(UseSharedSpaces, "runtime only"); |
480 |
526 |
481 _validating_shared_path_table = true; |
527 _validating_shared_path_table = true; |
|
528 |
|
529 // Load the shared path table info from the archive header |
482 _shared_path_table = _header->_shared_path_table; |
530 _shared_path_table = _header->_shared_path_table; |
483 _shared_path_entry_size = _header->_shared_path_entry_size; |
531 if (DynamicDumpSharedSpaces) { |
484 _shared_path_table_size = _header->_shared_path_table_size; |
532 // Only support dynamic dumping with the usage of the default CDS archive |
|
533 // or a simple base archive. |
|
534 // If the base layer archive contains additional path component besides |
|
535 // the runtime image and the -cp, dynamic dumping is disabled. |
|
536 // |
|
537 // When dynamic archiving is enabled, the _shared_path_table is overwritten |
|
538 // to include the application path and stored in the top layer archive. |
|
539 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); |
|
540 if (_header->_app_class_paths_start_index > 1) { |
|
541 DynamicDumpSharedSpaces = false; |
|
542 warning( |
|
543 "Dynamic archiving is disabled because base layer archive has appended boot classpath"); |
|
544 } |
|
545 if (_header->_num_module_paths > 0) { |
|
546 DynamicDumpSharedSpaces = false; |
|
547 warning( |
|
548 "Dynamic archiving is disabled because base layer archive has module path"); |
|
549 } |
|
550 } |
485 |
551 |
486 int module_paths_start_index = _header->_app_module_paths_start_index; |
552 int module_paths_start_index = _header->_app_module_paths_start_index; |
487 |
553 |
488 // validate the path entries up to the _max_used_path_index |
554 // validate the path entries up to the _max_used_path_index |
489 for (int i=0; i < _header->_max_used_path_index + 1; i++) { |
555 for (int i=0; i < _header->_max_used_path_index + 1; i++) { |
490 if (i < module_paths_start_index) { |
556 if (i < module_paths_start_index) { |
491 if (shared_path(i)->validate()) { |
557 if (shared_path(i)->validate()) { |
492 log_info(class, path)("ok"); |
558 log_info(class, path)("ok"); |
493 } else { |
559 } else { |
494 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled"); |
560 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) { |
|
561 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled"); |
|
562 } |
495 return false; |
563 return false; |
496 } |
564 } |
497 } else if (i >= module_paths_start_index) { |
565 } else if (i >= module_paths_start_index) { |
498 if (shared_path(i)->validate(false /* not a class path entry */)) { |
566 if (shared_path(i)->validate(false /* not a class path entry */)) { |
499 log_info(class, path)("ok"); |
567 log_info(class, path)("ok"); |
500 } else { |
568 } else { |
501 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled"); |
569 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) { |
|
570 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled"); |
|
571 } |
502 return false; |
572 return false; |
503 } |
573 } |
504 } |
574 } |
505 } |
575 } |
506 |
576 |
508 |
578 |
509 #if INCLUDE_JVMTI |
579 #if INCLUDE_JVMTI |
510 if (_classpath_entries_for_jvmti != NULL) { |
580 if (_classpath_entries_for_jvmti != NULL) { |
511 os::free(_classpath_entries_for_jvmti); |
581 os::free(_classpath_entries_for_jvmti); |
512 } |
582 } |
513 size_t sz = sizeof(ClassPathEntry*) * _shared_path_table_size; |
583 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths(); |
514 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass); |
584 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass); |
515 memset(_classpath_entries_for_jvmti, 0, sz); |
585 memset((void*)_classpath_entries_for_jvmti, 0, sz); |
516 #endif |
586 #endif |
517 |
587 |
518 return true; |
588 return true; |
519 } |
589 } |
520 |
590 |
|
591 bool FileMapInfo::same_files(const char* file1, const char* file2) { |
|
592 if (strcmp(file1, file2) == 0) { |
|
593 return true; |
|
594 } |
|
595 |
|
596 bool is_same = false; |
|
597 // if the two paths diff only in case |
|
598 struct stat st1; |
|
599 struct stat st2; |
|
600 int ret1; |
|
601 int ret2; |
|
602 ret1 = os::stat(file1, &st1); |
|
603 ret2 = os::stat(file2, &st2); |
|
604 if (ret1 < 0 || ret2 < 0) { |
|
605 // one of the files is invalid. So they are not the same. |
|
606 is_same = false; |
|
607 } else if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) { |
|
608 // different files |
|
609 is_same = false; |
|
610 #ifndef _WINDOWS |
|
611 } else if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) { |
|
612 // same files |
|
613 is_same = true; |
|
614 #else |
|
615 } else if ((st1.st_size == st2.st_size) && (st1.st_ctime == st2.st_ctime) && |
|
616 (st1.st_mtime == st2.st_mtime)) { |
|
617 // same files |
|
618 is_same = true; |
|
619 #endif |
|
620 } |
|
621 return is_same; |
|
622 } |
|
623 |
|
624 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) { |
|
625 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0); |
|
626 if (fd < 0) { |
|
627 // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths() |
|
628 // requires a shared archive name. The open_for_read() function will log a message regarding |
|
629 // failure in opening a shared archive. |
|
630 return false; |
|
631 } |
|
632 |
|
633 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader); |
|
634 void* header = os::malloc(sz, mtInternal); |
|
635 memset(header, 0, sz); |
|
636 size_t n = os::read(fd, header, (unsigned int)sz); |
|
637 if (n != sz) { |
|
638 os::free(header); |
|
639 os::close(fd); |
|
640 vm_exit_during_initialization("Unable to read header from shared archive", archive_name); |
|
641 return false; |
|
642 } |
|
643 if (is_static) { |
|
644 FileMapHeader* static_header = (FileMapHeader*)header; |
|
645 if (static_header->_magic != CDS_ARCHIVE_MAGIC) { |
|
646 os::free(header); |
|
647 os::close(fd); |
|
648 vm_exit_during_initialization("Not a base shared archive", archive_name); |
|
649 return false; |
|
650 } |
|
651 } else { |
|
652 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)header; |
|
653 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) { |
|
654 os::free(header); |
|
655 os::close(fd); |
|
656 vm_exit_during_initialization("Not a top shared archive", archive_name); |
|
657 return false; |
|
658 } |
|
659 } |
|
660 os::free(header); |
|
661 os::close(fd); |
|
662 return true; |
|
663 } |
|
664 |
|
665 bool FileMapInfo::get_base_archive_name_from_header(const char* archive_name, |
|
666 int* size, char** base_archive_name) { |
|
667 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0); |
|
668 if (fd < 0) { |
|
669 *size = 0; |
|
670 return false; |
|
671 } |
|
672 |
|
673 // read the header as a dynamic archive header |
|
674 size_t sz = sizeof(DynamicArchiveHeader); |
|
675 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal); |
|
676 size_t n = os::read(fd, dynamic_header, (unsigned int)sz); |
|
677 if (n != sz) { |
|
678 fail_continue("Unable to read the file header."); |
|
679 os::free(dynamic_header); |
|
680 os::close(fd); |
|
681 return false; |
|
682 } |
|
683 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) { |
|
684 // Not a dynamic header, no need to proceed further. |
|
685 *size = 0; |
|
686 os::free(dynamic_header); |
|
687 os::close(fd); |
|
688 return false; |
|
689 } |
|
690 if (dynamic_header->_base_archive_is_default) { |
|
691 *base_archive_name = Arguments::get_default_shared_archive_path(); |
|
692 } else { |
|
693 // skip over the _paths_misc_info |
|
694 sz = dynamic_header->_paths_misc_info_size; |
|
695 lseek(fd, (long)sz, SEEK_CUR); |
|
696 // read the base archive name |
|
697 size_t name_size = dynamic_header->_base_archive_name_size; |
|
698 if (name_size == 0) { |
|
699 os::free(dynamic_header); |
|
700 os::close(fd); |
|
701 return false; |
|
702 } |
|
703 *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal); |
|
704 n = os::read(fd, *base_archive_name, (unsigned int)name_size); |
|
705 if (n != name_size) { |
|
706 fail_continue("Unable to read the base archive name from the header."); |
|
707 FREE_C_HEAP_ARRAY(char, *base_archive_name); |
|
708 *base_archive_name = NULL; |
|
709 os::free(dynamic_header); |
|
710 os::close(fd); |
|
711 return false; |
|
712 } |
|
713 } |
|
714 |
|
715 os::free(dynamic_header); |
|
716 os::close(fd); |
|
717 return true; |
|
718 } |
|
719 |
|
720 void FileMapInfo::restore_shared_path_table() { |
|
721 _shared_path_table = _current_info->_header->_shared_path_table; |
|
722 } |
|
723 |
521 // Read the FileMapInfo information from the file. |
724 // Read the FileMapInfo information from the file. |
522 |
725 |
523 bool FileMapInfo::init_from_file(int fd) { |
726 bool FileMapInfo::init_from_file(int fd, bool is_static) { |
524 size_t sz = sizeof(FileMapHeader); |
727 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader); |
525 size_t n = os::read(fd, _header, (unsigned int)sz); |
728 size_t n = os::read(fd, _header, (unsigned int)sz); |
526 if (n != sz) { |
729 if (n != sz) { |
527 fail_continue("Unable to read the file header."); |
730 fail_continue("Unable to read the file header."); |
528 return false; |
731 return false; |
529 } |
732 } |
530 if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) { |
733 if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) { |
531 fail_continue("The shared archive file has the wrong version."); |
734 fail_continue("The shared archive file has the wrong version."); |
532 return false; |
735 return false; |
533 } |
736 } |
534 _file_offset = (long)n; |
737 _file_offset = n; |
535 |
738 |
536 size_t info_size = _header->_paths_misc_info_size; |
739 size_t info_size = _header->_paths_misc_info_size; |
537 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); |
740 _paths_misc_info = NEW_C_HEAP_ARRAY(char, info_size, mtClass); |
538 if (_paths_misc_info == NULL) { |
|
539 fail_continue("Unable to read the file header."); |
|
540 return false; |
|
541 } |
|
542 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); |
741 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); |
543 if (n != info_size) { |
742 if (n != info_size) { |
544 fail_continue("Unable to read the shared path info header."); |
743 fail_continue("Unable to read the shared path info header."); |
545 FREE_C_HEAP_ARRAY(char, _paths_misc_info); |
744 FREE_C_HEAP_ARRAY(char, _paths_misc_info); |
546 _paths_misc_info = NULL; |
745 _paths_misc_info = NULL; |
547 return false; |
746 return false; |
548 } |
747 } |
549 |
748 _file_offset += n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name |
550 size_t len = lseek(fd, 0, SEEK_END); |
749 |
551 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region); |
750 if (is_static) { |
552 // The last space might be empty |
751 if (_header->_magic != CDS_ARCHIVE_MAGIC) { |
553 if (si->_file_offset > len || len - si->_file_offset < si->_used) { |
752 fail_continue("Incorrect static archive magic number"); |
554 fail_continue("The shared archive file has been truncated."); |
753 return false; |
555 return false; |
754 } |
556 } |
755 // just checking the last region is sufficient since the archive is written |
557 |
756 // in sequential order |
558 _file_offset += (long)n; |
757 size_t len = lseek(fd, 0, SEEK_END); |
559 SharedBaseAddress = _header->_shared_base_address; |
758 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region); |
|
759 // The last space might be empty |
|
760 if (si->_file_offset > len || len - si->_file_offset < si->_used) { |
|
761 fail_continue("The shared archive file has been truncated."); |
|
762 return false; |
|
763 } |
|
764 |
|
765 SharedBaseAddress = _header->_shared_base_address; |
|
766 } |
|
767 |
560 return true; |
768 return true; |
561 } |
769 } |
562 |
770 |
563 |
771 |
564 // Read the FileMapInfo information from the file. |
772 // Read the FileMapInfo information from the file. |
565 bool FileMapInfo::open_for_read() { |
773 bool FileMapInfo::open_for_read(const char* path) { |
566 _full_path = Arguments::GetSharedArchivePath(); |
774 if (_file_open) { |
|
775 return true; |
|
776 } |
|
777 if (path == NULL) { |
|
778 _full_path = Arguments::GetSharedArchivePath(); |
|
779 } else { |
|
780 _full_path = path; |
|
781 } |
567 int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0); |
782 int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0); |
568 if (fd < 0) { |
783 if (fd < 0) { |
569 if (errno == ENOENT) { |
784 if (errno == ENOENT) { |
570 // Not locating the shared archive is ok. |
785 // Not locating the shared archive is ok. |
571 fail_continue("Specified shared archive not found."); |
786 fail_continue("Specified shared archive not found (%s).", _full_path); |
572 } else { |
787 } else { |
573 fail_continue("Failed to open shared archive file (%s).", |
788 fail_continue("Failed to open shared archive file (%s).", |
574 os::strerror(errno)); |
789 os::strerror(errno)); |
575 } |
790 } |
576 return false; |
791 return false; |
615 void FileMapInfo::write_header() { |
833 void FileMapInfo::write_header() { |
616 int info_size = ClassLoader::get_shared_paths_misc_info_size(); |
834 int info_size = ClassLoader::get_shared_paths_misc_info_size(); |
617 |
835 |
618 _header->_paths_misc_info_size = info_size; |
836 _header->_paths_misc_info_size = info_size; |
619 |
837 |
|
838 char* base_archive_name = NULL; |
|
839 if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) { |
|
840 base_archive_name = (char*)Arguments::GetSharedArchivePath(); |
|
841 _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1; |
|
842 _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile); |
|
843 } |
|
844 |
|
845 assert(is_file_position_aligned(), "must be"); |
|
846 write_bytes(_header, _header->_header_size); |
|
847 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size); |
|
848 if (base_archive_name != NULL) { |
|
849 write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size); |
|
850 } |
620 align_file_position(); |
851 align_file_position(); |
621 write_bytes(_header, sizeof(FileMapHeader)); |
852 } |
622 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size); |
|
623 align_file_position(); |
|
624 } |
|
625 |
|
626 |
853 |
627 // Dump region to file. |
854 // Dump region to file. |
628 |
855 // This is called twice for each region during archiving, once before |
|
856 // the archive file is open (_file_open is false) and once after. |
629 void FileMapInfo::write_region(int region, char* base, size_t size, |
857 void FileMapInfo::write_region(int region, char* base, size_t size, |
630 bool read_only, bool allow_exec) { |
858 bool read_only, bool allow_exec) { |
|
859 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only"); |
|
860 |
631 CDSFileMapRegion* si = space_at(region); |
861 CDSFileMapRegion* si = space_at(region); |
|
862 char* target_base = base; |
|
863 if (DynamicDumpSharedSpaces) { |
|
864 target_base = DynamicArchive::buffer_to_target(base); |
|
865 } |
632 |
866 |
633 if (_file_open) { |
867 if (_file_open) { |
634 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); |
868 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); |
635 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08) |
869 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08) |
636 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08), |
870 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08), |
637 region, size, p2i(base), _file_offset); |
871 region, size, p2i(target_base), _file_offset); |
638 } else { |
872 } else { |
639 si->_file_offset = _file_offset; |
873 si->_file_offset = _file_offset; |
640 } |
874 } |
|
875 |
641 if (HeapShared::is_heap_region(region)) { |
876 if (HeapShared::is_heap_region(region)) { |
642 assert((base - (char*)CompressedOops::base()) % HeapWordSize == 0, "Sanity"); |
877 assert((target_base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity"); |
643 if (base != NULL) { |
878 if (target_base != NULL) { |
644 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base); |
879 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)target_base); |
645 } else { |
880 } else { |
646 si->_addr._offset = 0; |
881 si->_addr._offset = 0; |
647 } |
882 } |
648 } else { |
883 } else { |
649 si->_addr._base = base; |
884 si->_addr._base = target_base; |
650 } |
885 } |
651 si->_used = size; |
886 si->_used = size; |
652 si->_read_only = read_only; |
887 si->_read_only = read_only; |
653 si->_allow_exec = allow_exec; |
888 si->_allow_exec = allow_exec; |
|
889 |
|
890 // Use the current 'base' when computing the CRC value and writing out data |
654 si->_crc = ClassLoader::crc32(0, base, (jint)size); |
891 si->_crc = ClassLoader::crc32(0, base, (jint)size); |
655 if (base != NULL) { |
892 if (base != NULL) { |
656 write_bytes_aligned(base, size); |
893 write_bytes_aligned(base, size); |
657 } |
894 } |
658 } |
895 } |
841 |
1081 |
842 // Memory map a region in the address space. |
1082 // Memory map a region in the address space. |
843 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData", |
1083 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData", |
844 "String1", "String2", "OpenArchive1", "OpenArchive2" }; |
1084 "String1", "String2", "OpenArchive1", "OpenArchive2" }; |
845 |
1085 |
|
1086 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) { |
|
1087 char* prev_top = NULL; |
|
1088 char* curr_base; |
|
1089 char* curr_top; |
|
1090 int i = 0; |
|
1091 for (i = 0; i < (int)len; i++) { |
|
1092 curr_base = map_region(regions[i], &curr_top); |
|
1093 if (curr_base == NULL) { |
|
1094 return NULL; |
|
1095 } |
|
1096 if (i > 0) { |
|
1097 // We require that mc->rw->ro->md->od to be laid out consecutively, with no |
|
1098 // gaps between them. That way, we can ensure that the OS won't be able to |
|
1099 // allocate any new memory spaces inside _shared_metaspace_{base,top}, which |
|
1100 // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace(). |
|
1101 assert(curr_base == prev_top, "must be"); |
|
1102 } |
|
1103 log_info(cds)("Mapped region #%d at base %p top %p", regions[i], curr_base, curr_top); |
|
1104 saved_base[i] = curr_base; |
|
1105 prev_top = curr_top; |
|
1106 } |
|
1107 return curr_top; |
|
1108 } |
|
1109 |
846 char* FileMapInfo::map_region(int i, char** top_ret) { |
1110 char* FileMapInfo::map_region(int i, char** top_ret) { |
847 assert(!HeapShared::is_heap_region(i), "sanity"); |
1111 assert(!HeapShared::is_heap_region(i), "sanity"); |
848 CDSFileMapRegion* si = space_at(i); |
1112 CDSFileMapRegion* si = space_at(i); |
849 size_t used = si->_used; |
1113 size_t used = si->_used; |
850 size_t alignment = os::vm_allocation_granularity(); |
1114 size_t alignment = os::vm_allocation_granularity(); |
1203 G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open); |
1468 G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open); |
1204 } |
1469 } |
1205 } |
1470 } |
1206 #endif // INCLUDE_CDS_JAVA_HEAP |
1471 #endif // INCLUDE_CDS_JAVA_HEAP |
1207 |
1472 |
|
1473 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) { |
|
1474 int crc = ClassLoader::crc32(0, buf, (jint)size); |
|
1475 if (crc != expected_crc) { |
|
1476 fail_continue("Checksum verification failed."); |
|
1477 return false; |
|
1478 } |
|
1479 return true; |
|
1480 } |
|
1481 |
1208 bool FileMapInfo::verify_region_checksum(int i) { |
1482 bool FileMapInfo::verify_region_checksum(int i) { |
1209 assert(VerifySharedSpaces, "sanity"); |
1483 assert(VerifySharedSpaces, "sanity"); |
1210 |
1484 |
1211 size_t sz = space_at(i)->_used; |
1485 size_t sz = space_at(i)->_used; |
1212 |
1486 |
1213 if (sz == 0) { |
1487 if (sz == 0) { |
1214 return true; // no data |
1488 return true; // no data |
1215 } |
1489 } |
1216 if ((HeapShared::is_closed_archive_heap_region(i) && |
1490 |
1217 !HeapShared::closed_archive_heap_region_mapped()) || |
1491 return region_crc_check(region_addr(i), sz, space_at(i)->_crc); |
1218 (HeapShared::is_open_archive_heap_region(i) && |
1492 } |
1219 !HeapShared::open_archive_heap_region_mapped())) { |
1493 |
1220 return true; // archived heap data is not mapped |
1494 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) { |
1221 } |
1495 for (int i = 0; i < (int)len; i++) { |
1222 const char* buf = region_addr(i); |
1496 if (saved_base[i] != NULL) { |
1223 int crc = ClassLoader::crc32(0, buf, (jint)sz); |
1497 unmap_region(regions[i]); |
1224 if (crc != space_at(i)->_crc) { |
1498 } |
1225 fail_continue("Checksum verification failed."); |
1499 } |
1226 return false; |
|
1227 } |
|
1228 return true; |
|
1229 } |
1500 } |
1230 |
1501 |
1231 // Unmap a memory region in the address space. |
1502 // Unmap a memory region in the address space. |
1232 |
1503 |
1233 void FileMapInfo::unmap_region(int i) { |
1504 void FileMapInfo::unmap_region(int i) { |