1 /*
  2  * Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "cds/archiveBuilder.hpp"
 27 #include "cds/archiveHeapWriter.hpp"
 28 #include "cds/archiveUtils.inline.hpp"
 29 #include "cds/cds_globals.hpp"
 30 #include "cds/cdsConfig.hpp"
 31 #include "cds/classPrelinker.hpp"
 32 #include "cds/dynamicArchive.hpp"
 33 #include "cds/metaspaceShared.hpp"
 34 #include "cds/regeneratedClasses.hpp"
 35 #include "classfile/classLoader.hpp"
 36 #include "classfile/classLoaderData.inline.hpp"
 37 #include "classfile/symbolTable.hpp"
 38 #include "classfile/systemDictionaryShared.hpp"
 39 #include "classfile/vmSymbols.hpp"
 40 #include "gc/shared/collectedHeap.hpp"
 41 #include "gc/shared/gcVMOperations.hpp"
 42 #include "gc/shared/gc_globals.hpp"
 43 #include "jvm.h"
 44 #include "logging/log.hpp"
 45 #include "memory/metaspaceClosure.hpp"
 46 #include "memory/resourceArea.hpp"
 47 #include "oops/klass.inline.hpp"
 48 #include "runtime/arguments.hpp"
 49 #include "runtime/os.hpp"
 50 #include "runtime/sharedRuntime.hpp"
 51 #include "runtime/vmThread.hpp"
 52 #include "runtime/vmOperations.hpp"
 53 #include "utilities/align.hpp"
 54 #include "utilities/bitMap.inline.hpp"
 55 
 56 
 57 class DynamicArchiveBuilder : public ArchiveBuilder {
 58   const char* _archive_name;
 59 public:
 60   DynamicArchiveBuilder(const char* archive_name) : _archive_name(archive_name) {}
 61   void mark_pointer(address* ptr_loc) {
 62     ArchivePtrMarker::mark_pointer(ptr_loc);
 63   }
 64 
 65   static int dynamic_dump_method_comparator(Method* a, Method* b) {
 66     Symbol* a_name = a->name();
 67     Symbol* b_name = b->name();
 68 
 69     if (a_name == b_name) {
 70       return 0;
 71     }
 72 
 73     u4 a_offset = ArchiveBuilder::current()->any_to_offset_u4(a_name);
 74     u4 b_offset = ArchiveBuilder::current()->any_to_offset_u4(b_name);
 75 
 76     if (a_offset < b_offset) {
 77       return -1;
 78     } else {
 79       assert(a_offset > b_offset, "must be");
 80       return 1;
 81     }
 82   }
 83 
 84 public:
 85   DynamicArchiveHeader *_header;
 86 
 87   void init_header();
 88   void release_header();
 89   void post_dump();
 90   void sort_methods();
 91   void sort_methods(InstanceKlass* ik) const;
 92   void remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const;
 93   void write_archive(char* serialized_data);
 94   void gather_array_klasses();
 95 
 96 public:
 97   DynamicArchiveBuilder() : ArchiveBuilder() { }
 98 
 99   // Do this before and after the archive dump to see if any corruption
100   // is caused by dynamic dumping.
101   void verify_universe(const char* info) {
102     if (VerifyBeforeExit) {
103       log_info(cds)("Verify %s", info);
104       // Among other things, this ensures that Eden top is correct.
105       Universe::heap()->prepare_for_verify();
106       Universe::verify(info);
107     }
108   }
109 
110   void doit() {
111     verify_universe("Before CDS dynamic dump");
112     DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm);
113 
114     // Block concurrent class unloading from changing the _dumptime_table
115     MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
116     SystemDictionaryShared::check_excluded_classes();
117 
118     if (SystemDictionaryShared::is_dumptime_table_empty()) {
119       log_warning(cds, dynamic)("There is no class to be included in the dynamic archive.");
120       return;
121     }
122 
123     log_info(cds,dynamic)("CDS dynamic dump: clinit = %ldms)",
124                           ClassLoader::class_init_time_ms());
125 
126     init_header();
127     gather_source_objs();
128     gather_array_klasses();
129     reserve_buffer();
130 
131     log_info(cds, dynamic)("Copying %d klasses and %d symbols",
132                            klasses()->length(), symbols()->length());
133     dump_rw_metadata();
134     dump_ro_metadata();
135     relocate_metaspaceobj_embedded_pointers();
136 
137     verify_estimate_size(_estimated_metaspaceobj_bytes, "MetaspaceObjs");
138 
139     sort_methods();
140 
141     {
142       ArchiveBuilder::OtherROAllocMark mark;
143       ClassPrelinker::record_preloaded_klasses(false);
144     }
145 
146     log_info(cds)("Make classes shareable");
147     make_klasses_shareable();
148 
149     char* serialized_data;
150     {
151       // Write the symbol table and system dictionaries to the RO space.
152       // Note that these tables still point to the *original* objects, so
153       // they would need to call DynamicArchive::original_to_target() to
154       // get the correct addresses.
155       assert(current_dump_space() == ro_region(), "Must be RO space");
156       SymbolTable::write_to_archive(symbols());
157 
158       ArchiveBuilder::OtherROAllocMark mark;
159       SystemDictionaryShared::write_to_archive(false);
160 
161       DynamicArchive::dump_array_klasses();
162       ClassPrelinker::record_initiated_klasses(false);
163       TrainingData::dump_training_data();
164 
165       serialized_data = ro_region()->top();
166       WriteClosure wc(ro_region());
167       ArchiveBuilder::serialize_dynamic_archivable_items(&wc);
168     }
169 
170     verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
171 
172     log_info(cds)("Adjust lambda proxy class dictionary");
173     SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
174 
175     log_info(cds)("Adjust method info dictionary");
176     SystemDictionaryShared::adjust_method_info_dictionary();
177 
178     log_info(cds)("Adjust training data dictionary");
179     TrainingData::adjust_training_data_dictionary();
180 
181     relocate_to_requested();
182 
183     write_archive(serialized_data);
184     release_header();
185     DynamicArchive::post_dump();
186 
187     post_dump();
188 
189     assert(_num_dump_regions_used == _total_dump_regions, "must be");
190     verify_universe("After CDS dynamic dump");
191   }
192 
193   virtual void iterate_roots(MetaspaceClosure* it) {
194     FileMapInfo::metaspace_pointers_do(it);
195     SystemDictionaryShared::dumptime_classes_do(it);
196     TrainingData::iterate_roots(it);
197     iterate_primitive_array_klasses(it);
198   }
199 
200   void iterate_primitive_array_klasses(MetaspaceClosure* it) {
201     for (int i = T_BOOLEAN; i <= T_LONG; i++) {
202       assert(is_java_primitive((BasicType)i), "sanity");
203       Klass* k = Universe::typeArrayKlassObj((BasicType)i);  // this give you "[I", etc
204       assert(MetaspaceShared::is_shared_static((void*)k),
205         "one-dimensional primitive array should be in static archive");
206       ArrayKlass* ak = ArrayKlass::cast(k);
207       while (ak != nullptr && ak->is_shared()) {
208         Klass* next_k = ak->array_klass_or_null();
209         if (next_k != nullptr) {
210           ak = ArrayKlass::cast(next_k);
211         } else {
212           ak = nullptr;
213         }
214       }
215       if (ak != nullptr) {
216         assert(ak->dimension() > 1, "sanity");
217         // this is the lowest dimension that's not in the static archive
218         it->push(&ak);
219       }
220     }
221   }
222 };
223 
224 void DynamicArchiveBuilder::init_header() {
225   FileMapInfo* mapinfo = new FileMapInfo(_archive_name, false);
226   assert(FileMapInfo::dynamic_info() == mapinfo, "must be");
227   FileMapInfo* base_info = FileMapInfo::current_info();
228   // header only be available after populate_header
229   mapinfo->populate_header(base_info->core_region_alignment());
230   _header = mapinfo->dynamic_header();
231 
232   _header->set_base_header_crc(base_info->crc());
233   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
234     _header->set_base_region_crc(i, base_info->region_crc(i));
235   }
236 }
237 
238 void DynamicArchiveBuilder::release_header() {
239   // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
240   // have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
241   // Let's free it so that if class loading happens after we leave the safepoint, nothing
242   // bad will happen.
243   assert(SafepointSynchronize::is_at_safepoint(), "must be");
244   FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
245   assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be");
246   delete mapinfo;
247   assert(!DynamicArchive::is_mapped(), "must be");
248   _header = nullptr;
249 }
250 
251 void DynamicArchiveBuilder::post_dump() {
252   ArchivePtrMarker::reset_map_and_vs();
253   ClassPrelinker::dispose();
254 }
255 
256 void DynamicArchiveBuilder::sort_methods() {
257   InstanceKlass::disable_method_binary_search();
258   for (int i = 0; i < klasses()->length(); i++) {
259     Klass* k = get_buffered_addr(klasses()->at(i));
260     if (k->is_instance_klass()) {
261       sort_methods(InstanceKlass::cast(k));
262     }
263   }
264 }
265 
266 // The address order of the copied Symbols may be different than when the original
267 // klasses were created. Re-sort all the tables. See Method::sort_methods().
268 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
269   assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
270   if (MetaspaceShared::is_in_shared_metaspace(ik)) {
271     // We have reached a supertype that's already in the base archive
272     return;
273   }
274   assert(is_in_buffer_space(ik), "method sorting must be done on buffered class, not original class");
275   if (ik->java_mirror() == nullptr) {
276     // null mirror means this class has already been visited and methods are already sorted
277     return;
278   }
279   ik->remove_java_mirror();
280 
281   if (log_is_enabled(Debug, cds, dynamic)) {
282     ResourceMark rm;
283     log_debug(cds, dynamic)("sorting methods for " PTR_FORMAT " (" PTR_FORMAT ") %s",
284                             p2i(ik), p2i(to_requested(ik)), ik->external_name());
285   }
286 
287   // Method sorting may re-layout the [iv]tables, which would change the offset(s)
288   // of the locations in an InstanceKlass that would contain pointers. Let's clear
289   // all the existing pointer marking bits, and re-mark the pointers after sorting.
290   remark_pointers_for_instance_klass(ik, false);
291 
292   // Make sure all supertypes have been sorted
293   sort_methods(ik->java_super());
294   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
295   int len = interfaces->length();
296   for (int i = 0; i < len; i++) {
297     sort_methods(interfaces->at(i));
298   }
299 
300 #ifdef ASSERT
301   if (ik->methods() != nullptr) {
302     for (int m = 0; m < ik->methods()->length(); m++) {
303       Symbol* name = ik->methods()->at(m)->name();
304       assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be");
305     }
306   }
307   if (ik->default_methods() != nullptr) {
308     for (int m = 0; m < ik->default_methods()->length(); m++) {
309       Symbol* name = ik->default_methods()->at(m)->name();
310       assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be");
311     }
312   }
313 #endif
314 
315   Method::sort_methods(ik->methods(), /*set_idnums=*/true, dynamic_dump_method_comparator);
316   if (ik->default_methods() != nullptr) {
317     Method::sort_methods(ik->default_methods(), /*set_idnums=*/false, dynamic_dump_method_comparator);
318   }
319   if (ik->is_linked()) {
320     // If the class has already been linked, we must relayout the i/v tables, whose order depends
321     // on the method sorting order.
322     // If the class is unlinked, we cannot layout the i/v tables yet. This is OK, as the
323     // i/v tables will be initialized at runtime after bytecode verification.
324     ik->vtable().initialize_vtable();
325     ik->itable().initialize_itable();
326   }
327 
328   // Set all the pointer marking bits after sorting.
329   remark_pointers_for_instance_klass(ik, true);
330 }
331 
332 template<bool should_mark>
333 class PointerRemarker: public MetaspaceClosure {
334 public:
335   virtual bool do_ref(Ref* ref, bool read_only) {
336     if (should_mark) {
337       ArchivePtrMarker::mark_pointer(ref->addr());
338     } else {
339       ArchivePtrMarker::clear_pointer(ref->addr());
340     }
341     return false; // don't recurse
342   }
343 };
344 
345 void DynamicArchiveBuilder::remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const {
346   if (should_mark) {
347     PointerRemarker<true> marker;
348     k->metaspace_pointers_do(&marker);
349     marker.finish();
350   } else {
351     PointerRemarker<false> marker;
352     k->metaspace_pointers_do(&marker);
353     marker.finish();
354   }
355 }
356 
357 void DynamicArchiveBuilder::write_archive(char* serialized_data) {
358   _header->set_shared_path_table(FileMapInfo::shared_path_table().table());
359   _header->set_serialized_data(serialized_data);
360 
361   FileMapInfo* dynamic_info = FileMapInfo::dynamic_info();
362   assert(dynamic_info != nullptr, "Sanity");
363 
364   dynamic_info->open_for_write();
365   ArchiveHeapInfo no_heap_for_dynamic_dump;
366   ArchiveBuilder::write_archive(dynamic_info, &no_heap_for_dynamic_dump);
367 
368   address base = _requested_dynamic_archive_bottom;
369   address top  = _requested_dynamic_archive_top;
370   size_t file_size = pointer_delta(top, base, sizeof(char));
371 
372   log_info(cds, dynamic)("Written dynamic archive " PTR_FORMAT " - " PTR_FORMAT
373                          " [" UINT32_FORMAT " bytes header, " SIZE_FORMAT " bytes total]",
374                          p2i(base), p2i(top), _header->header_size(), file_size);
375 
376   log_info(cds, dynamic)("%d klasses; %d symbols", klasses()->length(), symbols()->length());
377 }
378 
379 void DynamicArchiveBuilder::gather_array_klasses() {
380   for (int i = 0; i < klasses()->length(); i++) {
381     if (klasses()->at(i)->is_objArray_klass()) {
382       ObjArrayKlass* oak = ObjArrayKlass::cast(klasses()->at(i));
383       Klass* elem = oak->element_klass();
384       if (MetaspaceShared::is_shared_static(elem)) {
385         // Only capture the array klass whose element_klass is in the static archive.
386         // During run time, setup (see DynamicArchive::setup_array_klasses()) is needed
387         // so that the element_klass can find its array klasses from the dynamic archive.
388         DynamicArchive::append_array_klass(oak);
389       } else {
390         // The element_klass and its array klasses are in the same archive.
391         assert(!MetaspaceShared::is_shared_static(oak),
392           "we should not gather klasses that are already in the static archive");
393       }
394     }
395   }
396   log_debug(cds)("Total array klasses gathered for dynamic archive: %d", DynamicArchive::num_array_klasses());
397 }
398 
399 class VM_PopulateDynamicDumpSharedSpace: public VM_GC_Sync_Operation {
400   DynamicArchiveBuilder _builder;
401 public:
402   VM_PopulateDynamicDumpSharedSpace(const char* archive_name)
403   : VM_GC_Sync_Operation(), _builder(archive_name) {}
404   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
405   void doit() {
406     ResourceMark rm;
407     if (AllowArchivingWithJavaAgent) {
408       log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
409               "for testing purposes only and should not be used in a production environment");
410     }
411     FileMapInfo::check_nonempty_dir_in_shared_path_table();
412 
413     _builder.doit();
414   }
415   ~VM_PopulateDynamicDumpSharedSpace() {
416     RegeneratedClasses::cleanup();
417   }
418 };
419 
420 // _array_klasses and _dynamic_archive_array_klasses only hold the array klasses
421 // which have element klass in the static archive.
422 GrowableArray<ObjArrayKlass*>* DynamicArchive::_array_klasses = nullptr;
423 Array<ObjArrayKlass*>* DynamicArchive::_dynamic_archive_array_klasses = nullptr;
424 
425 void DynamicArchive::append_array_klass(ObjArrayKlass* ak) {
426   if (_array_klasses == nullptr) {
427     _array_klasses = new (mtClassShared) GrowableArray<ObjArrayKlass*>(50, mtClassShared);
428   }
429   _array_klasses->append(ak);
430 }
431 
432 void DynamicArchive::dump_array_klasses() {
433   assert(CDSConfig::is_dumping_dynamic_archive(), "sanity");
434   if (_array_klasses != nullptr) {
435     ArchiveBuilder* builder = ArchiveBuilder::current();
436     int num_array_klasses = _array_klasses->length();
437     _dynamic_archive_array_klasses =
438         ArchiveBuilder::new_ro_array<ObjArrayKlass*>(num_array_klasses);
439     for (int i = 0; i < num_array_klasses; i++) {
440       builder->write_pointer_in_buffer(_dynamic_archive_array_klasses->adr_at(i), _array_klasses->at(i));
441     }
442   }
443 }
444 
445 void DynamicArchive::setup_array_klasses() {
446   if (_dynamic_archive_array_klasses != nullptr) {
447     for (int i = 0; i < _dynamic_archive_array_klasses->length(); i++) {
448       ObjArrayKlass* oak = _dynamic_archive_array_klasses->at(i);
449       assert(!oak->is_typeArray_klass(), "all type array classes must be in static archive");
450 
451       Klass* elm = oak->element_klass();
452       assert(MetaspaceShared::is_shared_static((void*)elm), "must be");
453 
454       if (elm->is_instance_klass()) {
455         assert(InstanceKlass::cast(elm)->array_klasses() == nullptr, "must be");
456         InstanceKlass::cast(elm)->set_array_klasses(oak);
457       } else {
458         assert(elm->is_array_klass(), "sanity");
459         assert(ArrayKlass::cast(elm)->higher_dimension() == nullptr, "must be");
460         ArrayKlass::cast(elm)->set_higher_dimension(oak);
461       }
462     }
463     log_debug(cds)("Total array klasses read from dynamic archive: %d", _dynamic_archive_array_klasses->length());
464   }
465 }
466 
467 void DynamicArchive::serialize_array_klasses(SerializeClosure* soc) {
468   soc->do_ptr(&_dynamic_archive_array_klasses);
469 }
470 
471 void DynamicArchive::make_array_klasses_shareable() {
472   if (_array_klasses != nullptr) {
473     int num_array_klasses = _array_klasses->length();
474     for (int i = 0; i < num_array_klasses; i++) {
475       ObjArrayKlass* k = ArchiveBuilder::current()->get_buffered_addr(_array_klasses->at(i));
476       k->remove_unshareable_info();
477     }
478   }
479 }
480 
481 void DynamicArchive::post_dump() {
482   if (_array_klasses != nullptr) {
483     delete _array_klasses;
484     _array_klasses = nullptr;
485   }
486 }
487 
488 int DynamicArchive::num_array_klasses() {
489   return _array_klasses != nullptr ? _array_klasses->length() : 0;
490 }
491 
492 void DynamicArchive::check_for_dynamic_dump() {
493   if (CDSConfig::is_dumping_dynamic_archive() && !UseSharedSpaces) {
494     // This could happen if SharedArchiveFile has failed to load:
495     // - -Xshare:off was specified
496     // - SharedArchiveFile points to an non-existent file.
497     // - SharedArchiveFile points to an archive that has failed CRC check
498     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
499 
500 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
501     if (RecordDynamicDumpInfo) {
502       log_error(cds)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
503       MetaspaceShared::unrecoverable_loading_error();
504     } else {
505       assert(ArchiveClassesAtExit != nullptr, "sanity");
506       log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
507     }
508 #undef __THEMSG
509     CDSConfig::disable_dumping_dynamic_archive();
510   }
511 }
512 
513 void DynamicArchive::dump_at_exit(JavaThread* current, const char* archive_name) {
514   ExceptionMark em(current);
515   ResourceMark rm(current);
516 
517   if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
518     return;
519   }
520 
521   log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
522 
523   JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
524 
525   {
526     // FIXME-HACK - make sure we have at least one class in the dynamic archive
527     TempNewSymbol class_name = SymbolTable::new_symbol("sun/nio/cs/IBM850"); // unusual class; shouldn't be used by our tests cases.
528     SystemDictionary::resolve_or_null(class_name, Handle(), Handle(), THREAD);
529     guarantee(!HAS_PENDING_EXCEPTION, "must have this class");
530   }
531 
532   MetaspaceShared::link_shared_classes(false/*not from jcmd*/, THREAD);
533   if (!HAS_PENDING_EXCEPTION) {
534     // copy shared path table to saved.
535     TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
536     if (!HAS_PENDING_EXCEPTION) {
537       VM_PopulateDynamicDumpSharedSpace op(archive_name);
538       VMThread::execute(&op);
539       return;
540     }
541   }
542 
543   // One of the prepatory steps failed
544   oop ex = current->pending_exception();
545   log_error(cds)("Dynamic dump has failed");
546   log_error(cds)("%s: %s", ex->klass()->external_name(),
547                  java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
548   CLEAR_PENDING_EXCEPTION;
549   CDSConfig::disable_dumping_dynamic_archive();  // Just for good measure
550 }
551 
552 // This is called by "jcmd VM.cds dynamic_dump"
553 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
554   assert(UseSharedSpaces && RecordDynamicDumpInfo, "already checked in arguments.cpp");
555   assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
556   assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
557   MetaspaceShared::link_shared_classes(true/*from jcmd*/, CHECK);
558   // copy shared path table to saved.
559   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
560   VM_PopulateDynamicDumpSharedSpace op(archive_name);
561   VMThread::execute(&op);
562 }
563 
564 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
565   assert(!dynamic_info->is_static(), "must be");
566   // Check if the recorded base archive matches with the current one
567   FileMapInfo* base_info = FileMapInfo::current_info();
568   DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
569 
570   // Check the header crc
571   if (dynamic_header->base_header_crc() != base_info->crc()) {
572     log_warning(cds)("Dynamic archive cannot be used: static archive header checksum verification failed.");
573     return false;
574   }
575 
576   // Check each space's crc
577   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
578     if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
579       log_warning(cds)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
580       return false;
581     }
582   }
583 
584   return true;
585 }
586 
587 void DynamicArchiveHeader::print(outputStream* st) {
588   ResourceMark rm;
589 
590   st->print_cr("- base_header_crc:                0x%08x", base_header_crc());
591   for (int i = 0; i < NUM_CDS_REGIONS; i++) {
592     st->print_cr("- base_region_crc[%d]:             0x%08x", i, base_region_crc(i));
593   }
594 }