1 /*
  2  * Copyright (c) 2019, 2024, 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/aotClassLinker.hpp"
 27 #include "cds/archiveBuilder.hpp"
 28 #include "cds/archiveHeapWriter.hpp"
 29 #include "cds/archiveUtils.inline.hpp"
 30 #include "cds/cds_globals.hpp"
 31 #include "cds/cdsConfig.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/vmOperations.hpp"
 52 #include "runtime/vmThread.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::find_all_archivable_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 = " JLONG_FORMAT "ms)",
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     log_info(cds)("Make classes shareable");
142     make_klasses_shareable();
143 
144     char* serialized_data;
145     {
146       // Write the symbol table and system dictionaries to the RO space.
147       // Note that these tables still point to the *original* objects, so
148       // they would need to call DynamicArchive::original_to_target() to
149       // get the correct addresses.
150       assert(current_dump_region() == ro_region(), "Must be RO space");
151       SymbolTable::write_to_archive(symbols());
152 
153       ArchiveBuilder::OtherROAllocMark mark;
154       SystemDictionaryShared::write_to_archive(false);
155 
156       DynamicArchive::dump_array_klasses();
157       AOTClassLinker::write_to_archive();
158       TrainingData::dump_training_data();
159 
160       serialized_data = ro_region()->top();
161       WriteClosure wc(ro_region());
162       ArchiveBuilder::serialize_dynamic_archivable_items(&wc);
163     }
164 
165     verify_estimate_size(_estimated_hashtable_bytes, "Hashtables");
166 
167     log_info(cds)("Adjust lambda proxy class dictionary");
168     SystemDictionaryShared::adjust_lambda_proxy_class_dictionary();
169 
170     log_info(cds)("Make training data shareable");
171     make_training_data_shareable();
172 
173     relocate_to_requested();
174 
175     write_archive(serialized_data);
176     release_header();
177     DynamicArchive::post_dump();
178 
179     post_dump();
180 
181     assert(_num_dump_regions_used == _total_dump_regions, "must be");
182     verify_universe("After CDS dynamic dump");
183   }
184 
185   virtual void iterate_roots(MetaspaceClosure* it) {
186     FileMapInfo::metaspace_pointers_do(it);
187     SystemDictionaryShared::dumptime_classes_do(it);
188     TrainingData::iterate_roots(it);
189     iterate_primitive_array_klasses(it);
190   }
191 
192   void iterate_primitive_array_klasses(MetaspaceClosure* it) {
193     for (int i = T_BOOLEAN; i <= T_LONG; i++) {
194       assert(is_java_primitive((BasicType)i), "sanity");
195       Klass* k = Universe::typeArrayKlass((BasicType)i);  // this give you "[I", etc
196       assert(MetaspaceShared::is_shared_static((void*)k),
197         "one-dimensional primitive array should be in static archive");
198       ArrayKlass* ak = ArrayKlass::cast(k);
199       while (ak != nullptr && ak->is_shared()) {
200         Klass* next_k = ak->array_klass_or_null();
201         if (next_k != nullptr) {
202           ak = ArrayKlass::cast(next_k);
203         } else {
204           ak = nullptr;
205         }
206       }
207       if (ak != nullptr) {
208         assert(ak->dimension() > 1, "sanity");
209         // this is the lowest dimension that's not in the static archive
210         it->push(&ak);
211       }
212     }
213   }
214 };
215 
216 void DynamicArchiveBuilder::init_header() {
217   FileMapInfo* mapinfo = new FileMapInfo(_archive_name, false);
218   assert(FileMapInfo::dynamic_info() == mapinfo, "must be");
219   FileMapInfo* base_info = FileMapInfo::current_info();
220   // header only be available after populate_header
221   mapinfo->populate_header(base_info->core_region_alignment());
222   _header = mapinfo->dynamic_header();
223 
224   _header->set_base_header_crc(base_info->crc());
225   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
226     _header->set_base_region_crc(i, base_info->region_crc(i));
227   }
228 }
229 
230 void DynamicArchiveBuilder::release_header() {
231   // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we
232   // have mapped a dynamic archive, but we actually have not. We are in a safepoint now.
233   // Let's free it so that if class loading happens after we leave the safepoint, nothing
234   // bad will happen.
235   assert(SafepointSynchronize::is_at_safepoint(), "must be");
236   FileMapInfo *mapinfo = FileMapInfo::dynamic_info();
237   assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be");
238   delete mapinfo;
239   assert(!DynamicArchive::is_mapped(), "must be");
240   _header = nullptr;
241 }
242 
243 void DynamicArchiveBuilder::post_dump() {
244   ArchivePtrMarker::reset_map_and_vs();
245   AOTClassLinker::dispose();
246 }
247 
248 void DynamicArchiveBuilder::sort_methods() {
249   InstanceKlass::disable_method_binary_search();
250   for (int i = 0; i < klasses()->length(); i++) {
251     Klass* k = get_buffered_addr(klasses()->at(i));
252     if (k->is_instance_klass()) {
253       sort_methods(InstanceKlass::cast(k));
254     }
255   }
256 }
257 
258 // The address order of the copied Symbols may be different than when the original
259 // klasses were created. Re-sort all the tables. See Method::sort_methods().
260 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const {
261   assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive");
262   if (MetaspaceShared::is_in_shared_metaspace(ik)) {
263     // We have reached a supertype that's already in the base archive
264     return;
265   }
266   assert(is_in_buffer_space(ik), "method sorting must be done on buffered class, not original class");
267   if (ik->java_mirror() == nullptr) {
268     // null mirror means this class has already been visited and methods are already sorted
269     return;
270   }
271   ik->remove_java_mirror();
272 
273   if (log_is_enabled(Debug, cds, dynamic)) {
274     ResourceMark rm;
275     log_debug(cds, dynamic)("sorting methods for " PTR_FORMAT " (" PTR_FORMAT ") %s",
276                             p2i(ik), p2i(to_requested(ik)), ik->external_name());
277   }
278 
279   // Method sorting may re-layout the [iv]tables, which would change the offset(s)
280   // of the locations in an InstanceKlass that would contain pointers. Let's clear
281   // all the existing pointer marking bits, and re-mark the pointers after sorting.
282   remark_pointers_for_instance_klass(ik, false);
283 
284   // Make sure all supertypes have been sorted
285   sort_methods(ik->java_super());
286   Array<InstanceKlass*>* interfaces = ik->local_interfaces();
287   int len = interfaces->length();
288   for (int i = 0; i < len; i++) {
289     sort_methods(interfaces->at(i));
290   }
291 
292 #ifdef ASSERT
293   if (ik->methods() != nullptr) {
294     for (int m = 0; m < ik->methods()->length(); m++) {
295       Symbol* name = ik->methods()->at(m)->name();
296       assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be");
297     }
298   }
299   if (ik->default_methods() != nullptr) {
300     for (int m = 0; m < ik->default_methods()->length(); m++) {
301       Symbol* name = ik->default_methods()->at(m)->name();
302       assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be");
303     }
304   }
305 #endif
306 
307   Method::sort_methods(ik->methods(), /*set_idnums=*/true, dynamic_dump_method_comparator);
308   if (ik->default_methods() != nullptr) {
309     Method::sort_methods(ik->default_methods(), /*set_idnums=*/false, dynamic_dump_method_comparator);
310   }
311   if (ik->is_linked()) {
312     // If the class has already been linked, we must relayout the i/v tables, whose order depends
313     // on the method sorting order.
314     // If the class is unlinked, we cannot layout the i/v tables yet. This is OK, as the
315     // i/v tables will be initialized at runtime after bytecode verification.
316     ik->vtable().initialize_vtable();
317     ik->itable().initialize_itable();
318   }
319 
320   // Set all the pointer marking bits after sorting.
321   remark_pointers_for_instance_klass(ik, true);
322 }
323 
324 template<bool should_mark>
325 class PointerRemarker: public MetaspaceClosure {
326 public:
327   virtual bool do_ref(Ref* ref, bool read_only) {
328     if (should_mark) {
329       ArchivePtrMarker::mark_pointer(ref->addr());
330     } else {
331       ArchivePtrMarker::clear_pointer(ref->addr());
332     }
333     return false; // don't recurse
334   }
335 };
336 
337 void DynamicArchiveBuilder::remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const {
338   if (should_mark) {
339     PointerRemarker<true> marker;
340     k->metaspace_pointers_do(&marker);
341     marker.finish();
342   } else {
343     PointerRemarker<false> marker;
344     k->metaspace_pointers_do(&marker);
345     marker.finish();
346   }
347 }
348 
349 void DynamicArchiveBuilder::write_archive(char* serialized_data) {
350   _header->set_shared_path_table(FileMapInfo::shared_path_table().table());
351   _header->set_serialized_data(serialized_data);
352 
353   FileMapInfo* dynamic_info = FileMapInfo::dynamic_info();
354   assert(dynamic_info != nullptr, "Sanity");
355 
356   dynamic_info->open_for_write();
357   ArchiveHeapInfo no_heap_for_dynamic_dump;
358   ArchiveBuilder::write_archive(dynamic_info, &no_heap_for_dynamic_dump);
359 
360   address base = _requested_dynamic_archive_bottom;
361   address top  = _requested_dynamic_archive_top;
362   size_t file_size = pointer_delta(top, base, sizeof(char));
363 
364   log_info(cds, dynamic)("Written dynamic archive " PTR_FORMAT " - " PTR_FORMAT
365                          " [" UINT32_FORMAT " bytes header, " SIZE_FORMAT " bytes total]",
366                          p2i(base), p2i(top), _header->header_size(), file_size);
367 
368   log_info(cds, dynamic)("%d klasses; %d symbols", klasses()->length(), symbols()->length());
369 }
370 
371 void DynamicArchiveBuilder::gather_array_klasses() {
372   for (int i = 0; i < klasses()->length(); i++) {
373     if (klasses()->at(i)->is_objArray_klass()) {
374       ObjArrayKlass* oak = ObjArrayKlass::cast(klasses()->at(i));
375       Klass* elem = oak->element_klass();
376       if (MetaspaceShared::is_shared_static(elem)) {
377         // Only capture the array klass whose element_klass is in the static archive.
378         // During run time, setup (see DynamicArchive::setup_array_klasses()) is needed
379         // so that the element_klass can find its array klasses from the dynamic archive.
380         DynamicArchive::append_array_klass(oak);
381       } else {
382         // The element_klass and its array klasses are in the same archive.
383         assert(!MetaspaceShared::is_shared_static(oak),
384           "we should not gather klasses that are already in the static archive");
385       }
386     }
387   }
388   log_debug(cds)("Total array klasses gathered for dynamic archive: %d", DynamicArchive::num_array_klasses());
389 }
390 
391 class VM_PopulateDynamicDumpSharedSpace: public VM_GC_Sync_Operation {
392   DynamicArchiveBuilder _builder;
393 public:
394   VM_PopulateDynamicDumpSharedSpace(const char* archive_name)
395   : VM_GC_Sync_Operation(), _builder(archive_name) {}
396   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
397   void doit() {
398     ResourceMark rm;
399     if (AllowArchivingWithJavaAgent) {
400       log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
401               "for testing purposes only and should not be used in a production environment");
402     }
403     FileMapInfo::check_nonempty_dir_in_shared_path_table();
404 
405     _builder.doit();
406   }
407   ~VM_PopulateDynamicDumpSharedSpace() {
408     RegeneratedClasses::cleanup();
409   }
410 };
411 
412 // _array_klasses and _dynamic_archive_array_klasses only hold the array klasses
413 // which have element klass in the static archive.
414 GrowableArray<ObjArrayKlass*>* DynamicArchive::_array_klasses = nullptr;
415 Array<ObjArrayKlass*>* DynamicArchive::_dynamic_archive_array_klasses = nullptr;
416 
417 void DynamicArchive::append_array_klass(ObjArrayKlass* ak) {
418   if (_array_klasses == nullptr) {
419     _array_klasses = new (mtClassShared) GrowableArray<ObjArrayKlass*>(50, mtClassShared);
420   }
421   _array_klasses->append(ak);
422 }
423 
424 void DynamicArchive::dump_array_klasses() {
425   assert(CDSConfig::is_dumping_dynamic_archive(), "sanity");
426   if (_array_klasses != nullptr) {
427     ArchiveBuilder* builder = ArchiveBuilder::current();
428     int num_array_klasses = _array_klasses->length();
429     _dynamic_archive_array_klasses =
430         ArchiveBuilder::new_ro_array<ObjArrayKlass*>(num_array_klasses);
431     for (int i = 0; i < num_array_klasses; i++) {
432       builder->write_pointer_in_buffer(_dynamic_archive_array_klasses->adr_at(i), _array_klasses->at(i));
433     }
434   }
435 }
436 
437 void DynamicArchive::setup_array_klasses() {
438   if (_dynamic_archive_array_klasses != nullptr) {
439     for (int i = 0; i < _dynamic_archive_array_klasses->length(); i++) {
440       ObjArrayKlass* oak = _dynamic_archive_array_klasses->at(i);
441       assert(!oak->is_typeArray_klass(), "all type array classes must be in static archive");
442 
443       Klass* elm = oak->element_klass();
444       assert(MetaspaceShared::is_shared_static((void*)elm), "must be");
445 
446       if (elm->is_instance_klass()) {
447         assert(InstanceKlass::cast(elm)->array_klasses() == nullptr, "must be");
448         InstanceKlass::cast(elm)->set_array_klasses(oak);
449       } else {
450         assert(elm->is_array_klass(), "sanity");
451         assert(ArrayKlass::cast(elm)->higher_dimension() == nullptr, "must be");
452         ArrayKlass::cast(elm)->set_higher_dimension(oak);
453       }
454     }
455     log_debug(cds)("Total array klasses read from dynamic archive: %d", _dynamic_archive_array_klasses->length());
456   }
457 }
458 
459 void DynamicArchive::serialize_array_klasses(SerializeClosure* soc) {
460   soc->do_ptr(&_dynamic_archive_array_klasses);
461 }
462 
463 void DynamicArchive::make_array_klasses_shareable() {
464   if (_array_klasses != nullptr) {
465     int num_array_klasses = _array_klasses->length();
466     for (int i = 0; i < num_array_klasses; i++) {
467       ObjArrayKlass* k = ArchiveBuilder::current()->get_buffered_addr(_array_klasses->at(i));
468       k->remove_unshareable_info();
469     }
470   }
471 }
472 
473 void DynamicArchive::post_dump() {
474   if (_array_klasses != nullptr) {
475     delete _array_klasses;
476     _array_klasses = nullptr;
477   }
478 }
479 
480 int DynamicArchive::num_array_klasses() {
481   return _array_klasses != nullptr ? _array_klasses->length() : 0;
482 }
483 
484 void DynamicArchive::check_for_dynamic_dump() {
485   if (CDSConfig::is_dumping_dynamic_archive() && !CDSConfig::is_using_archive()) {
486     // This could happen if SharedArchiveFile has failed to load:
487     // - -Xshare:off was specified
488     // - SharedArchiveFile points to an non-existent file.
489     // - SharedArchiveFile points to an archive that has failed CRC check
490     // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive
491 
492 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info."
493     if (RecordDynamicDumpInfo) {
494       log_error(cds)("-XX:+RecordDynamicDumpInfo%s", __THEMSG);
495       MetaspaceShared::unrecoverable_loading_error();
496     } else {
497       assert(ArchiveClassesAtExit != nullptr, "sanity");
498       log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG);
499     }
500 #undef __THEMSG
501     CDSConfig::disable_dumping_dynamic_archive();
502   }
503 }
504 
505 void DynamicArchive::dump_at_exit(JavaThread* current, const char* archive_name) {
506   ExceptionMark em(current);
507   ResourceMark rm(current);
508   CDSConfig::DumperThreadMark dumper_thread_mark(current);
509 
510   if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) {
511     return;
512   }
513 
514   log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name());
515 
516   JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes
517 
518   {
519     // FIXME-HACK - make sure we have at least one class in the dynamic archive
520     TempNewSymbol class_name = SymbolTable::new_symbol("sun/nio/cs/IBM850"); // unusual class; shouldn't be used by our tests cases.
521     SystemDictionary::resolve_or_null(class_name, Handle(), Handle(), THREAD);
522     guarantee(!HAS_PENDING_EXCEPTION, "must have this class");
523   }
524 
525   MetaspaceShared::link_shared_classes(false/*not from jcmd*/, THREAD);
526   if (!HAS_PENDING_EXCEPTION) {
527     // copy shared path table to saved.
528     TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
529     if (!HAS_PENDING_EXCEPTION) {
530       VM_PopulateDynamicDumpSharedSpace op(archive_name);
531       VMThread::execute(&op);
532       return;
533     }
534   }
535 
536   // One of the prepatory steps failed
537   oop ex = current->pending_exception();
538   log_error(cds)("Dynamic dump has failed");
539   log_error(cds)("%s: %s", ex->klass()->external_name(),
540                  java_lang_String::as_utf8_string(java_lang_Throwable::message(ex)));
541   CLEAR_PENDING_EXCEPTION;
542   CDSConfig::disable_dumping_dynamic_archive();  // Just for good measure
543 }
544 
545 // This is called by "jcmd VM.cds dynamic_dump"
546 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) {
547   CDSConfig::DumperThreadMark dumper_thread_mark(THREAD);
548   assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp");
549   assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp");
550   assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup");
551   MetaspaceShared::link_shared_classes(true/*from jcmd*/, CHECK);
552   // copy shared path table to saved.
553   TrainingData::init_dumptime_table(CHECK); // captures TrainingDataSetLocker
554   VM_PopulateDynamicDumpSharedSpace op(archive_name);
555   VMThread::execute(&op);
556 }
557 
558 bool DynamicArchive::validate(FileMapInfo* dynamic_info) {
559   assert(!dynamic_info->is_static(), "must be");
560   // Check if the recorded base archive matches with the current one
561   FileMapInfo* base_info = FileMapInfo::current_info();
562   DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header();
563 
564   // Check the header crc
565   if (dynamic_header->base_header_crc() != base_info->crc()) {
566     log_warning(cds)("Dynamic archive cannot be used: static archive header checksum verification failed.");
567     return false;
568   }
569 
570   // Check each space's crc
571   for (int i = 0; i < MetaspaceShared::n_regions; i++) {
572     if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) {
573       log_warning(cds)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i);
574       return false;
575     }
576   }
577 
578   return true;
579 }
580 
581 void DynamicArchiveHeader::print(outputStream* st) {
582   ResourceMark rm;
583 
584   st->print_cr("- base_header_crc:                0x%08x", base_header_crc());
585   for (int i = 0; i < NUM_CDS_REGIONS; i++) {
586     st->print_cr("- base_region_crc[%d]:             0x%08x", i, base_region_crc(i));
587   }
588 }