1 /*
   2  * Copyright (c) 2020, 2025, 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 "cds/aotArtifactFinder.hpp"
  26 #include "cds/aotClassLinker.hpp"
  27 #include "cds/aotLinkedClassBulkLoader.hpp"
  28 #include "cds/archiveBuilder.hpp"
  29 #include "cds/archiveHeapWriter.hpp"
  30 #include "cds/archiveUtils.hpp"
  31 #include "cds/cdsConfig.hpp"
  32 #include "cds/cppVtables.hpp"
  33 #include "cds/dumpAllocStats.hpp"
  34 #include "cds/dynamicArchive.hpp"
  35 #include "cds/finalImageRecipes.hpp"
  36 #include "cds/heapShared.hpp"
  37 #include "cds/metaspaceShared.hpp"
  38 #include "cds/regeneratedClasses.hpp"
  39 #include "classfile/classLoader.hpp"
  40 #include "classfile/classLoaderDataShared.hpp"
  41 #include "classfile/classLoaderExt.hpp"
  42 #include "classfile/javaClasses.hpp"
  43 #include "classfile/symbolTable.hpp"
  44 #include "classfile/systemDictionaryShared.hpp"
  45 #include "classfile/vmClasses.hpp"
  46 #include "code/SCCache.hpp"
  47 #include "interpreter/abstractInterpreter.hpp"
  48 #include "jvm.h"
  49 #include "logging/log.hpp"
  50 #include "logging/logStream.hpp"
  51 #include "memory/allStatic.hpp"
  52 #include "memory/memoryReserver.hpp"
  53 #include "memory/memRegion.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "oops/compressedKlass.inline.hpp"
  56 #include "oops/instanceKlass.hpp"
  57 #include "oops/methodCounters.hpp"
  58 #include "oops/methodData.hpp"
  59 #include "oops/objArrayKlass.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oopHandle.inline.hpp"
  62 #include "oops/trainingData.hpp"
  63 #include "runtime/arguments.hpp"
  64 #include "runtime/fieldDescriptor.inline.hpp"
  65 #include "runtime/globals_extension.hpp"
  66 #include "runtime/javaThread.hpp"
  67 #include "runtime/sharedRuntime.hpp"
  68 #include "utilities/align.hpp"
  69 #include "utilities/bitMap.inline.hpp"
  70 #include "utilities/formatBuffer.hpp"
  71 
  72 ArchiveBuilder* ArchiveBuilder::_current = nullptr;
  73 
  74 ArchiveBuilder::OtherROAllocMark::~OtherROAllocMark() {
  75   char* newtop = ArchiveBuilder::current()->_ro_region.top();
  76   ArchiveBuilder::alloc_stats()->record_other_type(int(newtop - _oldtop), true);
  77 }
  78 
  79 ArchiveBuilder::SourceObjList::SourceObjList() : _ptrmap(16 * K, mtClassShared) {
  80   _total_bytes = 0;
  81   _objs = new (mtClassShared) GrowableArray<SourceObjInfo*>(128 * K, mtClassShared);
  82 }
  83 
  84 ArchiveBuilder::SourceObjList::~SourceObjList() {
  85   delete _objs;
  86 }
  87 
  88 void ArchiveBuilder::SourceObjList::append(SourceObjInfo* src_info) {
  89   // Save this source object for copying
  90   src_info->set_id(_objs->length());
  91   _objs->append(src_info);
  92 
  93   // Prepare for marking the pointers in this source object
  94   assert(is_aligned(_total_bytes, sizeof(address)), "must be");
  95   src_info->set_ptrmap_start(_total_bytes / sizeof(address));
  96   _total_bytes = align_up(_total_bytes + (uintx)src_info->size_in_bytes(), sizeof(address));
  97   src_info->set_ptrmap_end(_total_bytes / sizeof(address));
  98 
  99   BitMap::idx_t bitmap_size_needed = BitMap::idx_t(src_info->ptrmap_end());
 100   if (_ptrmap.size() <= bitmap_size_needed) {
 101     _ptrmap.resize((bitmap_size_needed + 1) * 2);
 102   }
 103 }
 104 
 105 void ArchiveBuilder::SourceObjList::remember_embedded_pointer(SourceObjInfo* src_info, MetaspaceClosure::Ref* ref) {
 106   // src_obj contains a pointer. Remember the location of this pointer in _ptrmap,
 107   // so that we can copy/relocate it later.
 108   src_info->set_has_embedded_pointer();
 109   address src_obj = src_info->source_addr();
 110   address* field_addr = ref->addr();
 111   assert(src_info->ptrmap_start() < _total_bytes, "sanity");
 112   assert(src_info->ptrmap_end() <= _total_bytes, "sanity");
 113   assert(*field_addr != nullptr, "should have checked");
 114 
 115   intx field_offset_in_bytes = ((address)field_addr) - src_obj;
 116   DEBUG_ONLY(int src_obj_size = src_info->size_in_bytes();)
 117   assert(field_offset_in_bytes >= 0, "must be");
 118   assert(field_offset_in_bytes + intx(sizeof(intptr_t)) <= intx(src_obj_size), "must be");
 119   assert(is_aligned(field_offset_in_bytes, sizeof(address)), "must be");
 120 
 121   BitMap::idx_t idx = BitMap::idx_t(src_info->ptrmap_start() + (uintx)(field_offset_in_bytes / sizeof(address)));
 122   _ptrmap.set_bit(BitMap::idx_t(idx));
 123 }
 124 
 125 class RelocateEmbeddedPointers : public BitMapClosure {
 126   ArchiveBuilder* _builder;
 127   address _buffered_obj;
 128   BitMap::idx_t _start_idx;
 129 public:
 130   RelocateEmbeddedPointers(ArchiveBuilder* builder, address buffered_obj, BitMap::idx_t start_idx) :
 131     _builder(builder), _buffered_obj(buffered_obj), _start_idx(start_idx) {}
 132 
 133   bool do_bit(BitMap::idx_t bit_offset) {
 134     size_t field_offset = size_t(bit_offset - _start_idx) * sizeof(address);
 135     address* ptr_loc = (address*)(_buffered_obj + field_offset);
 136 
 137     address old_p = *ptr_loc;
 138     address new_p = _builder->get_buffered_addr(old_p);
 139 
 140     log_trace(cds)("Ref: [" PTR_FORMAT "] -> " PTR_FORMAT " => " PTR_FORMAT,
 141                    p2i(ptr_loc), p2i(old_p), p2i(new_p));
 142 
 143     ArchivePtrMarker::set_and_mark_pointer(ptr_loc, new_p);
 144     return true; // keep iterating the bitmap
 145   }
 146 };
 147 
 148 void ArchiveBuilder::SourceObjList::relocate(int i, ArchiveBuilder* builder) {
 149   SourceObjInfo* src_info = objs()->at(i);
 150   assert(src_info->should_copy(), "must be");
 151   BitMap::idx_t start = BitMap::idx_t(src_info->ptrmap_start()); // inclusive
 152   BitMap::idx_t end = BitMap::idx_t(src_info->ptrmap_end());     // exclusive
 153 
 154   RelocateEmbeddedPointers relocator(builder, src_info->buffered_addr(), start);
 155   _ptrmap.iterate(&relocator, start, end);
 156 }
 157 
 158 ArchiveBuilder::ArchiveBuilder() :
 159   _current_dump_region(nullptr),
 160   _buffer_bottom(nullptr),
 161   _num_dump_regions_used(0),
 162   _requested_static_archive_bottom(nullptr),
 163   _requested_static_archive_top(nullptr),
 164   _requested_dynamic_archive_bottom(nullptr),
 165   _requested_dynamic_archive_top(nullptr),
 166   _mapped_static_archive_bottom(nullptr),
 167   _mapped_static_archive_top(nullptr),
 168   _buffer_to_requested_delta(0),
 169   _rw_region("rw", MAX_SHARED_DELTA),
 170   _ro_region("ro", MAX_SHARED_DELTA),
 171   _cc_region("cc", MAX_SHARED_DELTA),
 172   _ptrmap(mtClassShared),
 173   _rw_ptrmap(mtClassShared),
 174   _ro_ptrmap(mtClassShared),
 175   _cc_ptrmap(mtClassShared),
 176   _rw_src_objs(),
 177   _ro_src_objs(),
 178   _src_obj_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 179   _buffered_to_src_table(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE),
 180   _total_heap_region_size(0)
 181 {
 182   _klasses = new (mtClassShared) GrowableArray<Klass*>(4 * K, mtClassShared);
 183   _symbols = new (mtClassShared) GrowableArray<Symbol*>(256 * K, mtClassShared);
 184   _entropy_seed = 0x12345678;
 185   assert(_current == nullptr, "must be");
 186   _current = this;
 187 }
 188 
 189 ArchiveBuilder::~ArchiveBuilder() {
 190   assert(_current == this, "must be");
 191   _current = nullptr;
 192 
 193   for (int i = 0; i < _symbols->length(); i++) {
 194     _symbols->at(i)->decrement_refcount();
 195   }
 196 
 197   delete _klasses;
 198   delete _symbols;
 199   if (_shared_rs.is_reserved()) {
 200     MemoryReserver::release(_shared_rs);
 201   }
 202 
 203   AOTArtifactFinder::dispose();
 204 }
 205 
 206 // Returns a deterministic sequence of pseudo random numbers. The main purpose is NOT
 207 // for randomness but to get good entropy for the identity_hash() of archived Symbols,
 208 // while keeping the contents of static CDS archives deterministic to ensure
 209 // reproducibility of JDK builds.
 210 int ArchiveBuilder::entropy() {
 211   assert(SafepointSynchronize::is_at_safepoint(), "needed to ensure deterministic sequence");
 212   _entropy_seed = os::next_random(_entropy_seed);
 213   return static_cast<int>(_entropy_seed);
 214 }
 215 
 216 class GatherKlassesAndSymbols : public UniqueMetaspaceClosure {
 217   ArchiveBuilder* _builder;
 218 
 219 public:
 220   GatherKlassesAndSymbols(ArchiveBuilder* builder) : _builder(builder) {}
 221 
 222   virtual bool do_unique_ref(Ref* ref, bool read_only) {
 223     return _builder->gather_klass_and_symbol(ref, read_only);
 224   }
 225 };
 226 
 227 bool ArchiveBuilder::gather_klass_and_symbol(MetaspaceClosure::Ref* ref, bool read_only) {
 228   if (ref->obj() == nullptr) {
 229     return false;
 230   }
 231   if (get_follow_mode(ref) != make_a_copy) {
 232     return false;
 233   }
 234   if (ref->msotype() == MetaspaceObj::ClassType) {
 235     Klass* klass = (Klass*)ref->obj();
 236     assert(klass->is_klass(), "must be");
 237     if (!is_excluded(klass)) {
 238       _klasses->append(klass);
 239       if (klass->is_hidden()) {
 240         assert(klass->is_instance_klass(), "must be");
 241       }
 242     }
 243   } else if (ref->msotype() == MetaspaceObj::SymbolType) {
 244     // Make sure the symbol won't be GC'ed while we are dumping the archive.
 245     Symbol* sym = (Symbol*)ref->obj();
 246     sym->increment_refcount();
 247     _symbols->append(sym);
 248   }
 249 
 250   return true; // recurse
 251 }
 252 
 253 void ArchiveBuilder::gather_klasses_and_symbols() {
 254   ResourceMark rm;
 255 
 256   AOTArtifactFinder::initialize();
 257   AOTArtifactFinder::find_artifacts();
 258 
 259   log_info(cds)("Gathering classes and symbols ... ");
 260   GatherKlassesAndSymbols doit(this);
 261   iterate_roots(&doit);
 262 #if INCLUDE_CDS_JAVA_HEAP
 263   if (CDSConfig::is_dumping_full_module_graph()) {
 264     ClassLoaderDataShared::iterate_symbols(&doit);
 265   }
 266 #endif
 267   doit.finish();
 268 
 269   if (CDSConfig::is_dumping_static_archive()) {
 270     // To ensure deterministic contents in the static archive, we need to ensure that
 271     // we iterate the MetaspaceObjs in a deterministic order. It doesn't matter where
 272     // the MetaspaceObjs are located originally, as they are copied sequentially into
 273     // the archive during the iteration.
 274     //
 275     // The only issue here is that the symbol table and the system directories may be
 276     // randomly ordered, so we copy the symbols and klasses into two arrays and sort
 277     // them deterministically.
 278     //
 279     // During -Xshare:dump, the order of Symbol creation is strictly determined by
 280     // the SharedClassListFile (class loading is done in a single thread and the JIT
 281     // is disabled). Also, Symbols are allocated in monotonically increasing addresses
 282     // (see Symbol::operator new(size_t, int)). So if we iterate the Symbols by
 283     // ascending address order, we ensure that all Symbols are copied into deterministic
 284     // locations in the archive.
 285     //
 286     // TODO: in the future, if we want to produce deterministic contents in the
 287     // dynamic archive, we might need to sort the symbols alphabetically (also see
 288     // DynamicArchiveBuilder::sort_methods()).
 289     log_info(cds)("Sorting symbols ... ");
 290     _symbols->sort(compare_symbols_by_address);
 291     sort_klasses();
 292   }
 293 
 294   AOTClassLinker::add_candidates();
 295 }
 296 
 297 int ArchiveBuilder::compare_symbols_by_address(Symbol** a, Symbol** b) {
 298   if (a[0] < b[0]) {
 299     return -1;
 300   } else {
 301     assert(a[0] > b[0], "Duplicated symbol %s unexpected", (*a)->as_C_string());
 302     return 1;
 303   }
 304 }
 305 
 306 int ArchiveBuilder::compare_klass_by_name(Klass** a, Klass** b) {
 307   return a[0]->name()->fast_compare(b[0]->name());
 308 }
 309 
 310 void ArchiveBuilder::sort_klasses() {
 311   log_info(cds)("Sorting classes ... ");
 312   _klasses->sort(compare_klass_by_name);
 313 }
 314 
 315 address ArchiveBuilder::reserve_buffer() {
 316   // SCCache::max_aot_code_size() accounts for cached code region.
 317   size_t buffer_size = LP64_ONLY(CompressedClassSpaceSize) NOT_LP64(256 * M) + SCCache::max_aot_code_size();
 318   ReservedSpace rs = MemoryReserver::reserve(buffer_size,
 319                                              MetaspaceShared::core_region_alignment(),
 320                                              os::vm_page_size());
 321   if (!rs.is_reserved()) {
 322     log_error(cds)("Failed to reserve %zu bytes of output buffer.", buffer_size);
 323     MetaspaceShared::unrecoverable_writing_error();
 324   }
 325 
 326   // buffer_bottom is the lowest address of the 2 core regions (rw, ro) when
 327   // we are copying the class metadata into the buffer.
 328   address buffer_bottom = (address)rs.base();
 329   log_info(cds)("Reserved output buffer space at " PTR_FORMAT " [%zu bytes]",
 330                 p2i(buffer_bottom), buffer_size);
 331   _shared_rs = rs;
 332 
 333   _buffer_bottom = buffer_bottom;
 334   _current_dump_region = &_rw_region;
 335   _num_dump_regions_used = 1;
 336   _current_dump_region->init(&_shared_rs, &_shared_vs);
 337 
 338   ArchivePtrMarker::initialize(&_ptrmap, &_shared_vs);
 339 
 340   // The bottom of the static archive should be mapped at this address by default.
 341   _requested_static_archive_bottom = (address)MetaspaceShared::requested_base_address();
 342 
 343   // The bottom of the archive (that I am writing now) should be mapped at this address by default.
 344   address my_archive_requested_bottom;
 345 
 346   if (CDSConfig::is_dumping_static_archive()) {
 347     my_archive_requested_bottom = _requested_static_archive_bottom;
 348   } else {
 349     _mapped_static_archive_bottom = (address)MetaspaceObj::shared_metaspace_base();
 350     _mapped_static_archive_top  = (address)MetaspaceObj::shared_metaspace_top();
 351     assert(_mapped_static_archive_top >= _mapped_static_archive_bottom, "must be");
 352     size_t static_archive_size = _mapped_static_archive_top - _mapped_static_archive_bottom;
 353 
 354     // At run time, we will mmap the dynamic archive at my_archive_requested_bottom
 355     _requested_static_archive_top = _requested_static_archive_bottom + static_archive_size;
 356     my_archive_requested_bottom = align_up(_requested_static_archive_top, MetaspaceShared::core_region_alignment());
 357 
 358     _requested_dynamic_archive_bottom = my_archive_requested_bottom;
 359   }
 360 
 361   _buffer_to_requested_delta = my_archive_requested_bottom - _buffer_bottom;
 362 
 363   address my_archive_requested_top = my_archive_requested_bottom + buffer_size;
 364   if (my_archive_requested_bottom <  _requested_static_archive_bottom ||
 365       my_archive_requested_top    <= _requested_static_archive_bottom) {
 366     // Size overflow.
 367     log_error(cds)("my_archive_requested_bottom = " INTPTR_FORMAT, p2i(my_archive_requested_bottom));
 368     log_error(cds)("my_archive_requested_top    = " INTPTR_FORMAT, p2i(my_archive_requested_top));
 369     log_error(cds)("SharedBaseAddress (" INTPTR_FORMAT ") is too high. "
 370                    "Please rerun java -Xshare:dump with a lower value", p2i(_requested_static_archive_bottom));
 371     MetaspaceShared::unrecoverable_writing_error();
 372   }
 373 
 374   if (CDSConfig::is_dumping_static_archive()) {
 375     // We don't want any valid object to be at the very bottom of the archive.
 376     // See ArchivePtrMarker::mark_pointer().
 377     rw_region()->allocate(16);
 378   }
 379 
 380   return buffer_bottom;
 381 }
 382 
 383 void ArchiveBuilder::iterate_sorted_roots(MetaspaceClosure* it) {
 384   int num_symbols = _symbols->length();
 385   for (int i = 0; i < num_symbols; i++) {
 386     it->push(_symbols->adr_at(i));
 387   }
 388 
 389   int num_klasses = _klasses->length();
 390   for (int i = 0; i < num_klasses; i++) {
 391     it->push(_klasses->adr_at(i));
 392   }
 393 
 394   iterate_roots(it);
 395 }
 396 
 397 class GatherSortedSourceObjs : public MetaspaceClosure {
 398   ArchiveBuilder* _builder;
 399 
 400 public:
 401   GatherSortedSourceObjs(ArchiveBuilder* builder) : _builder(builder) {}
 402 
 403   virtual bool do_ref(Ref* ref, bool read_only) {
 404     return _builder->gather_one_source_obj(ref, read_only);
 405   }
 406 };
 407 
 408 bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* ref, bool read_only) {
 409   address src_obj = ref->obj();
 410   if (src_obj == nullptr) {
 411     return false;
 412   }
 413 
 414   remember_embedded_pointer_in_enclosing_obj(ref);
 415   if (RegeneratedClasses::has_been_regenerated(src_obj)) {
 416     // No need to copy it. We will later relocate it to point to the regenerated klass/method.
 417     return false;
 418   }
 419 
 420   FollowMode follow_mode = get_follow_mode(ref);
 421   SourceObjInfo src_info(ref, read_only, follow_mode);
 422   bool created;
 423   SourceObjInfo* p = _src_obj_table.put_if_absent(src_obj, src_info, &created);
 424   if (created) {
 425     if (_src_obj_table.maybe_grow()) {
 426       log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
 427     }
 428   }
 429 
 430 #ifdef ASSERT
 431   if (ref->msotype() == MetaspaceObj::MethodType) {
 432     Method* m = (Method*)ref->obj();
 433     assert(!RegeneratedClasses::has_been_regenerated((address)m->method_holder()),
 434            "Should not archive methods in a class that has been regenerated");
 435   }
 436 #endif
 437 
 438   assert(p->read_only() == src_info.read_only(), "must be");
 439 
 440   if (created && src_info.should_copy()) {
 441     if (read_only) {
 442       _ro_src_objs.append(p);
 443     } else {
 444       _rw_src_objs.append(p);
 445     }
 446     return true; // Need to recurse into this ref only if we are copying it
 447   } else {
 448     return false;
 449   }
 450 }
 451 
 452 void ArchiveBuilder::record_regenerated_object(address orig_src_obj, address regen_src_obj) {
 453   // Record the fact that orig_src_obj has been replaced by regen_src_obj. All calls to get_buffered_addr(orig_src_obj)
 454   // should return the same value as get_buffered_addr(regen_src_obj).
 455   SourceObjInfo* p = _src_obj_table.get(regen_src_obj);
 456   assert(p != nullptr, "regenerated object should always be dumped");
 457   SourceObjInfo orig_src_info(orig_src_obj, p);
 458   bool created;
 459   _src_obj_table.put_if_absent(orig_src_obj, orig_src_info, &created);
 460   assert(created, "We shouldn't have archived the original copy of a regenerated object");
 461 }
 462 
 463 // Remember that we have a pointer inside ref->enclosing_obj() that points to ref->obj()
 464 void ArchiveBuilder::remember_embedded_pointer_in_enclosing_obj(MetaspaceClosure::Ref* ref) {
 465   assert(ref->obj() != nullptr, "should have checked");
 466 
 467   address enclosing_obj = ref->enclosing_obj();
 468   if (enclosing_obj == nullptr) {
 469     return;
 470   }
 471 
 472   // We are dealing with 3 addresses:
 473   // address o    = ref->obj(): We have found an object whose address is o.
 474   // address* mpp = ref->mpp(): The object o is pointed to by a pointer whose address is mpp.
 475   //                            I.e., (*mpp == o)
 476   // enclosing_obj            : If non-null, it is the object which has a field that points to o.
 477   //                            mpp is the address if that field.
 478   //
 479   // Example: We have an array whose first element points to a Method:
 480   //     Method* o                     = 0x0000abcd;
 481   //     Array<Method*>* enclosing_obj = 0x00001000;
 482   //     enclosing_obj->at_put(0, o);
 483   //
 484   // We the MetaspaceClosure iterates on the very first element of this array, we have
 485   //     ref->obj()           == 0x0000abcd   (the Method)
 486   //     ref->mpp()           == 0x00001008   (the location of the first element in the array)
 487   //     ref->enclosing_obj() == 0x00001000   (the Array that contains the Method)
 488   //
 489   // We use the above information to mark the bitmap to indicate that there's a pointer on address 0x00001008.
 490   SourceObjInfo* src_info = _src_obj_table.get(enclosing_obj);
 491   if (src_info == nullptr || !src_info->should_copy()) {
 492     // source objects of point_to_it/set_to_null types are not copied
 493     // so we don't need to remember their pointers.
 494   } else {
 495     if (src_info->read_only()) {
 496       _ro_src_objs.remember_embedded_pointer(src_info, ref);
 497     } else {
 498       _rw_src_objs.remember_embedded_pointer(src_info, ref);
 499     }
 500   }
 501 }
 502 
 503 void ArchiveBuilder::gather_source_objs() {
 504   ResourceMark rm;
 505   log_info(cds)("Gathering all archivable objects ... ");
 506   gather_klasses_and_symbols();
 507   GatherSortedSourceObjs doit(this);
 508   iterate_sorted_roots(&doit);
 509   doit.finish();
 510 }
 511 
 512 bool ArchiveBuilder::is_excluded(Klass* klass) {
 513   if (klass->is_instance_klass()) {
 514     InstanceKlass* ik = InstanceKlass::cast(klass);
 515     return SystemDictionaryShared::is_excluded_class(ik);
 516   } else if (klass->is_objArray_klass()) {
 517     Klass* bottom = ObjArrayKlass::cast(klass)->bottom_klass();
 518     if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_shared_static(bottom)) {
 519       // The bottom class is in the static archive so it's clearly not excluded.
 520       return false;
 521     } else if (bottom->is_instance_klass()) {
 522       return SystemDictionaryShared::is_excluded_class(InstanceKlass::cast(bottom));
 523     }
 524   }
 525 
 526   return false;
 527 }
 528 
 529 ArchiveBuilder::FollowMode ArchiveBuilder::get_follow_mode(MetaspaceClosure::Ref *ref) {
 530   address obj = ref->obj();
 531   if (CDSConfig::is_dumping_dynamic_archive() && MetaspaceShared::is_in_shared_metaspace(obj)) {
 532     // Don't dump existing shared metadata again.
 533     return point_to_it;
 534   } else if (ref->msotype() == MetaspaceObj::MethodDataType ||
 535              ref->msotype() == MetaspaceObj::MethodCountersType ||
 536              ref->msotype() == MetaspaceObj::KlassTrainingDataType ||
 537              ref->msotype() == MetaspaceObj::MethodTrainingDataType ||
 538              ref->msotype() == MetaspaceObj::CompileTrainingDataType) {
 539       return TrainingData::need_data() ? make_a_copy : set_to_null;
 540   } else if (ref->msotype() == MetaspaceObj::AdapterHandlerEntryType) {
 541     if (CDSConfig::is_dumping_adapters()) {
 542       AdapterHandlerEntry* entry = (AdapterHandlerEntry*)ref->obj();
 543       return AdapterHandlerLibrary::is_abstract_method_adapter(entry) ? set_to_null : make_a_copy;
 544     } else {
 545       return set_to_null;
 546     }
 547   } else {
 548     if (ref->msotype() == MetaspaceObj::ClassType) {
 549       Klass* klass = (Klass*)ref->obj();
 550       assert(klass->is_klass(), "must be");
 551       if (is_excluded(klass)) {
 552         ResourceMark rm;
 553         log_debug(cds, dynamic)("Skipping class (excluded): %s", klass->external_name());
 554         return set_to_null;
 555       }
 556     }
 557 
 558     return make_a_copy;
 559   }
 560 }
 561 
 562 void ArchiveBuilder::start_dump_region(DumpRegion* next) {
 563   current_dump_region()->pack(next);
 564   _current_dump_region = next;
 565   _num_dump_regions_used ++;
 566 }
 567 
 568 char* ArchiveBuilder::ro_strdup(const char* s) {
 569   char* archived_str = ro_region_alloc((int)strlen(s) + 1);
 570   strcpy(archived_str, s);
 571   return archived_str;
 572 }
 573 
 574 // The objects that have embedded pointers will sink
 575 // towards the end of the list. This ensures we have a maximum
 576 // number of leading zero bits in the relocation bitmap.
 577 int ArchiveBuilder::compare_src_objs(SourceObjInfo** a, SourceObjInfo** b) {
 578   if ((*a)->has_embedded_pointer() && !(*b)->has_embedded_pointer()) {
 579     return 1;
 580   } else if (!(*a)->has_embedded_pointer() && (*b)->has_embedded_pointer()) {
 581     return -1;
 582   } else {
 583     // This is necessary to keep the sorting order stable. Otherwise the
 584     // archive's contents may not be deterministic.
 585     return (*a)->id() - (*b)->id();
 586   }
 587 }
 588 
 589 void ArchiveBuilder::sort_metadata_objs() {
 590   _rw_src_objs.objs()->sort(compare_src_objs);
 591   _ro_src_objs.objs()->sort(compare_src_objs);
 592 }
 593 
 594 void ArchiveBuilder::dump_rw_metadata() {
 595   ResourceMark rm;
 596   log_info(cds)("Allocating RW objects ... ");
 597   make_shallow_copies(&_rw_region, &_rw_src_objs);
 598 
 599 #if INCLUDE_CDS_JAVA_HEAP
 600   if (CDSConfig::is_dumping_full_module_graph()) {
 601     // Archive the ModuleEntry's and PackageEntry's of the 3 built-in loaders
 602     char* start = rw_region()->top();
 603     ClassLoaderDataShared::allocate_archived_tables();
 604     alloc_stats()->record_modules(rw_region()->top() - start, /*read_only*/false);
 605   }
 606 #endif
 607 }
 608 
 609 void ArchiveBuilder::dump_ro_metadata() {
 610   ResourceMark rm;
 611   log_info(cds)("Allocating RO objects ... ");
 612 
 613   start_dump_region(&_ro_region);
 614   make_shallow_copies(&_ro_region, &_ro_src_objs);
 615 
 616 #if INCLUDE_CDS_JAVA_HEAP
 617   if (CDSConfig::is_dumping_full_module_graph()) {
 618     char* start = ro_region()->top();
 619     ClassLoaderDataShared::init_archived_tables();
 620     alloc_stats()->record_modules(ro_region()->top() - start, /*read_only*/true);
 621   }
 622 #endif
 623 
 624   RegeneratedClasses::record_regenerated_objects();
 625 }
 626 
 627 void ArchiveBuilder::make_shallow_copies(DumpRegion *dump_region,
 628                                          const ArchiveBuilder::SourceObjList* src_objs) {
 629   for (int i = 0; i < src_objs->objs()->length(); i++) {
 630     make_shallow_copy(dump_region, src_objs->objs()->at(i));
 631   }
 632   log_info(cds)("done (%d objects)", src_objs->objs()->length());
 633 }
 634 
 635 void ArchiveBuilder::make_shallow_copy(DumpRegion *dump_region, SourceObjInfo* src_info) {
 636   address src = src_info->source_addr();
 637   int bytes = src_info->size_in_bytes();
 638   char* dest;
 639   char* oldtop;
 640   char* newtop;
 641 
 642   oldtop = dump_region->top();
 643   if (src_info->msotype() == MetaspaceObj::ClassType) {
 644     // Allocate space for a pointer directly in front of the future InstanceKlass, so
 645     // we can do a quick lookup from InstanceKlass* -> RunTimeClassInfo*
 646     // without building another hashtable. See RunTimeClassInfo::get_for()
 647     // in systemDictionaryShared.cpp.
 648     Klass* klass = (Klass*)src;
 649     if (klass->is_instance_klass()) {
 650       SystemDictionaryShared::validate_before_archiving(InstanceKlass::cast(klass));
 651       dump_region->allocate(sizeof(address));
 652     }
 653     // Allocate space for the future InstanceKlass with proper alignment
 654     const size_t alignment =
 655 #ifdef _LP64
 656       UseCompressedClassPointers ?
 657         nth_bit(ArchiveBuilder::precomputed_narrow_klass_shift()) :
 658         SharedSpaceObjectAlignment;
 659 #else
 660       SharedSpaceObjectAlignment;
 661 #endif
 662     dest = dump_region->allocate(bytes, alignment);
 663   } else {
 664     dest = dump_region->allocate(bytes);
 665   }
 666   newtop = dump_region->top();
 667 
 668   memcpy(dest, src, bytes);
 669 
 670   // Update the hash of buffered sorted symbols for static dump so that the symbols have deterministic contents
 671   if (CDSConfig::is_dumping_static_archive() && (src_info->msotype() == MetaspaceObj::SymbolType)) {
 672     Symbol* buffered_symbol = (Symbol*)dest;
 673     assert(((Symbol*)src)->is_permanent(), "archived symbols must be permanent");
 674     buffered_symbol->update_identity_hash();
 675   }
 676 
 677   {
 678     bool created;
 679     _buffered_to_src_table.put_if_absent((address)dest, src, &created);
 680     assert(created, "must be");
 681     if (_buffered_to_src_table.maybe_grow()) {
 682       log_info(cds, hashtables)("Expanded _buffered_to_src_table table to %d", _buffered_to_src_table.table_size());
 683     }
 684   }
 685 
 686   intptr_t* archived_vtable = CppVtables::get_archived_vtable(src_info->msotype(), (address)dest);
 687   if (archived_vtable != nullptr) {
 688     *(address*)dest = (address)archived_vtable;
 689     ArchivePtrMarker::mark_pointer((address*)dest);
 690   }
 691 
 692   log_trace(cds)("Copy: " PTR_FORMAT " ==> " PTR_FORMAT " %d", p2i(src), p2i(dest), bytes);
 693   src_info->set_buffered_addr((address)dest);
 694 
 695   _alloc_stats.record(src_info->msotype(), int(newtop - oldtop), src_info->read_only());
 696 
 697   DEBUG_ONLY(_alloc_stats.verify((int)dump_region->used(), src_info->read_only()));
 698 }
 699 
 700 // This is used by code that hand-assembles data structures, such as the LambdaProxyClassKey, that are
 701 // not handled by MetaspaceClosure.
 702 void ArchiveBuilder::write_pointer_in_buffer(address* ptr_location, address src_addr) {
 703   assert(is_in_buffer_space(ptr_location), "must be");
 704   if (src_addr == nullptr) {
 705     *ptr_location = nullptr;
 706     ArchivePtrMarker::clear_pointer(ptr_location);
 707   } else {
 708     *ptr_location = get_buffered_addr(src_addr);
 709     ArchivePtrMarker::mark_pointer(ptr_location);
 710   }
 711 }
 712 
 713 void ArchiveBuilder::mark_and_relocate_to_buffered_addr(address* ptr_location) {
 714   assert(*ptr_location != nullptr, "sanity");
 715   if (!is_in_mapped_static_archive(*ptr_location)) {
 716     *ptr_location = get_buffered_addr(*ptr_location);
 717   }
 718   ArchivePtrMarker::mark_pointer(ptr_location);
 719 }
 720 
 721 bool ArchiveBuilder::has_been_buffered(address src_addr) const {
 722   if (RegeneratedClasses::has_been_regenerated(src_addr) ||
 723       _src_obj_table.get(src_addr) == nullptr ||
 724       get_buffered_addr(src_addr) == nullptr) {
 725     return false;
 726   } else {
 727     return true;
 728   }
 729 }
 730 
 731 address ArchiveBuilder::get_buffered_addr(address src_addr) const {
 732   SourceObjInfo* p = _src_obj_table.get(src_addr);
 733   assert(p != nullptr, "src_addr " INTPTR_FORMAT " is used but has not been archived",
 734          p2i(src_addr));
 735 
 736   return p->buffered_addr();
 737 }
 738 
 739 bool ArchiveBuilder::has_been_archived(address src_addr) const {
 740   SourceObjInfo* p = _src_obj_table.get(src_addr);
 741   return (p != nullptr);
 742 }
 743 
 744 address ArchiveBuilder::get_source_addr(address buffered_addr) const {
 745   assert(is_in_buffer_space(buffered_addr), "must be");
 746   address* src_p = _buffered_to_src_table.get(buffered_addr);
 747   assert(src_p != nullptr && *src_p != nullptr, "must be");
 748   return *src_p;
 749 }
 750 
 751 void ArchiveBuilder::relocate_embedded_pointers(ArchiveBuilder::SourceObjList* src_objs) {
 752   for (int i = 0; i < src_objs->objs()->length(); i++) {
 753     src_objs->relocate(i, this);
 754   }
 755 }
 756 
 757 void ArchiveBuilder::relocate_metaspaceobj_embedded_pointers() {
 758   log_info(cds)("Relocating embedded pointers in core regions ... ");
 759   relocate_embedded_pointers(&_rw_src_objs);
 760   relocate_embedded_pointers(&_ro_src_objs);
 761 }
 762 
 763 #define ADD_COUNT(x) \
 764   x += 1; \
 765   x ## _a += aotlinked ? 1 : 0; \
 766   x ## _i += inited ? 1 : 0;
 767 
 768 #define DECLARE_INSTANCE_KLASS_COUNTER(x) \
 769   int x = 0; \
 770   int x ## _a = 0; \
 771   int x ## _i = 0;
 772 
 773 void ArchiveBuilder::make_klasses_shareable() {
 774   DECLARE_INSTANCE_KLASS_COUNTER(num_instance_klasses);
 775   DECLARE_INSTANCE_KLASS_COUNTER(num_boot_klasses);
 776   DECLARE_INSTANCE_KLASS_COUNTER(num_vm_klasses);
 777   DECLARE_INSTANCE_KLASS_COUNTER(num_platform_klasses);
 778   DECLARE_INSTANCE_KLASS_COUNTER(num_app_klasses);
 779   DECLARE_INSTANCE_KLASS_COUNTER(num_old_klasses);
 780   DECLARE_INSTANCE_KLASS_COUNTER(num_hidden_klasses);
 781   DECLARE_INSTANCE_KLASS_COUNTER(num_enum_klasses);
 782   DECLARE_INSTANCE_KLASS_COUNTER(num_unregistered_klasses);
 783   int num_unlinked_klasses = 0;
 784   int num_obj_array_klasses = 0;
 785   int num_type_array_klasses = 0;
 786 
 787   int boot_unlinked = 0;
 788   int platform_unlinked = 0;
 789   int app_unlinked = 0;
 790   int unreg_unlinked = 0;
 791 
 792   for (int i = 0; i < klasses()->length(); i++) {
 793     // Some of the code in ConstantPool::remove_unshareable_info() requires the classes
 794     // to be in linked state, so it must be call here before the next loop, which returns
 795     // all classes to unlinked state.
 796     Klass* k = get_buffered_addr(klasses()->at(i));
 797     if (k->is_instance_klass()) {
 798       InstanceKlass::cast(k)->constants()->remove_unshareable_info();
 799     }
 800   }
 801 
 802   for (int i = 0; i < klasses()->length(); i++) {
 803     const char* type;
 804     const char* unlinked = "";
 805     const char* kind = "";
 806     const char* hidden = "";
 807     const char* old = "";
 808     const char* generated = "";
 809     const char* aotlinked_msg = "";
 810     const char* inited_msg = "";
 811     Klass* k = get_buffered_addr(klasses()->at(i));
 812     bool inited = false;
 813     k->remove_java_mirror();
 814 #ifdef _LP64
 815     if (UseCompactObjectHeaders) {
 816       Klass* requested_k = to_requested(k);
 817       address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
 818       const int narrow_klass_shift = precomputed_narrow_klass_shift();
 819       narrowKlass nk = CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
 820       k->set_prototype_header(markWord::prototype().set_narrow_klass(nk));
 821     }
 822 #endif //_LP64
 823     if (k->is_objArray_klass()) {
 824       // InstanceKlass and TypeArrayKlass will in turn call remove_unshareable_info
 825       // on their array classes.
 826       num_obj_array_klasses ++;
 827       type = "array";
 828     } else if (k->is_typeArray_klass()) {
 829       num_type_array_klasses ++;
 830       type = "array";
 831       k->remove_unshareable_info();
 832     } else {
 833       assert(k->is_instance_klass(), " must be");
 834       InstanceKlass* ik = InstanceKlass::cast(k);
 835       InstanceKlass* src_ik = get_source_addr(ik);
 836       bool aotlinked = AOTClassLinker::is_candidate(src_ik);
 837       inited = ik->has_aot_initialized_mirror();
 838       ADD_COUNT(num_instance_klasses);
 839       if (CDSConfig::is_dumping_dynamic_archive()) {
 840         // For static dump, class loader type are already set.
 841         ik->assign_class_loader_type();
 842       }
 843       if (ik->is_hidden()) {
 844         ADD_COUNT(num_hidden_klasses);
 845         hidden = " hidden";
 846         oop loader = k->class_loader();
 847         if (loader == nullptr) {
 848           type = "boot";
 849           ADD_COUNT(num_boot_klasses);
 850         } else if (loader == SystemDictionary::java_platform_loader()) {
 851           type = "plat";
 852           ADD_COUNT(num_platform_klasses);
 853         } else if (loader == SystemDictionary::java_system_loader()) {
 854           type = "app";
 855           ADD_COUNT(num_app_klasses);
 856         } else {
 857           type = "bad";
 858           assert(0, "shouldn't happen");
 859         }
 860         if (CDSConfig::is_dumping_method_handles()) {
 861           assert(HeapShared::is_archivable_hidden_klass(ik), "sanity");
 862         } else {
 863           // Legacy CDS support for lambda proxies
 864           CDS_JAVA_HEAP_ONLY(assert(HeapShared::is_lambda_proxy_klass(ik), "sanity");)
 865         }
 866       } else if (ik->is_shared_boot_class()) {
 867         type = "boot";
 868         ADD_COUNT(num_boot_klasses);
 869       } else if (ik->is_shared_platform_class()) {
 870         type = "plat";
 871         ADD_COUNT(num_platform_klasses);
 872       } else if (ik->is_shared_app_class()) {
 873         type = "app";
 874         ADD_COUNT(num_app_klasses);
 875       } else {
 876         assert(ik->is_shared_unregistered_class(), "must be");
 877         type = "unreg";
 878         ADD_COUNT(num_unregistered_klasses);
 879       }
 880 
 881       if (AOTClassLinker::is_vm_class(src_ik)) {
 882         ADD_COUNT(num_vm_klasses);
 883       }
 884 
 885       if (!ik->is_linked()) {
 886         num_unlinked_klasses ++;
 887         unlinked = " unlinked";
 888         if (ik->is_shared_boot_class()) {
 889           boot_unlinked ++;
 890         } else if (ik->is_shared_platform_class()) {
 891           platform_unlinked ++;
 892         } else if (ik->is_shared_app_class()) {
 893           app_unlinked ++;
 894         } else {
 895           unreg_unlinked ++;
 896         }
 897       }
 898 
 899       if (ik->is_interface()) {
 900         kind = " interface";
 901       } else if (src_ik->is_enum_subclass()) {
 902         kind = " enum";
 903         ADD_COUNT(num_enum_klasses);
 904       }
 905 
 906       if (!ik->can_be_verified_at_dumptime()) {
 907         ADD_COUNT(num_old_klasses);
 908         old = " old";
 909       }
 910 
 911       if (ik->is_generated_shared_class()) {
 912         generated = " generated";
 913       }
 914       if (aotlinked) {
 915         aotlinked_msg = " aot-linked";
 916       }
 917       if (inited) {
 918         if (InstanceKlass::cast(k)->static_field_size() == 0) {
 919           inited_msg = " inited (no static fields)";
 920         } else {
 921           inited_msg = " inited";
 922         }
 923       }
 924 
 925       MetaspaceShared::rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread::current(), ik);
 926       ik->remove_unshareable_info();
 927     }
 928 
 929     if (log_is_enabled(Debug, cds, class)) {
 930       ResourceMark rm;
 931       log_debug(cds, class)("klasses[%5d] = " PTR_FORMAT " %-5s %s%s%s%s%s%s%s%s", i,
 932                             p2i(to_requested(k)), type, k->external_name(),
 933                             kind, hidden, old, unlinked, generated, aotlinked_msg, inited_msg);
 934     }
 935   }
 936 
 937 #define STATS_FORMAT    "= %5d, aot-linked = %5d, inited = %5d"
 938 #define STATS_PARAMS(x) num_ ## x, num_ ## x ## _a, num_ ## x ## _i
 939 
 940   log_info(cds)("Number of classes %d", num_instance_klasses + num_obj_array_klasses + num_type_array_klasses);
 941   log_info(cds)("    instance classes   " STATS_FORMAT, STATS_PARAMS(instance_klasses));
 942   log_info(cds)("      boot             " STATS_FORMAT, STATS_PARAMS(boot_klasses));
 943   log_info(cds)("        vm             " STATS_FORMAT, STATS_PARAMS(vm_klasses));
 944   log_info(cds)("      platform         " STATS_FORMAT, STATS_PARAMS(platform_klasses));
 945   log_info(cds)("      app              " STATS_FORMAT, STATS_PARAMS(app_klasses));
 946   log_info(cds)("      unregistered     " STATS_FORMAT, STATS_PARAMS(unregistered_klasses));
 947   log_info(cds)("      (enum)           " STATS_FORMAT, STATS_PARAMS(enum_klasses));
 948   log_info(cds)("      (hidden)         " STATS_FORMAT, STATS_PARAMS(hidden_klasses));
 949   log_info(cds)("      (old)            " STATS_FORMAT, STATS_PARAMS(old_klasses));
 950   log_info(cds)("      (unlinked)       = %5d, boot = %d, plat = %d, app = %d, unreg = %d",
 951                 num_unlinked_klasses, boot_unlinked, platform_unlinked, app_unlinked, unreg_unlinked);
 952   log_info(cds)("    obj array classes  = %5d", num_obj_array_klasses);
 953   log_info(cds)("    type array classes = %5d", num_type_array_klasses);
 954   log_info(cds)("               symbols = %5d", _symbols->length());
 955 
 956 #undef STATS_FORMAT
 957 #undef STATS_PARAMS
 958 
 959   DynamicArchive::make_array_klasses_shareable();
 960 }
 961 
 962 void ArchiveBuilder::make_training_data_shareable() {
 963   auto clean_td = [&] (address& src_obj,  SourceObjInfo& info) {
 964     if (!is_in_buffer_space(info.buffered_addr())) {
 965       return;
 966     }
 967 
 968     if (info.msotype() == MetaspaceObj::KlassTrainingDataType ||
 969         info.msotype() == MetaspaceObj::MethodTrainingDataType ||
 970         info.msotype() == MetaspaceObj::CompileTrainingDataType) {
 971       TrainingData* buffered_td = (TrainingData*)info.buffered_addr();
 972       buffered_td->remove_unshareable_info();
 973     } else if (info.msotype() == MetaspaceObj::MethodDataType) {
 974       MethodData* buffered_mdo = (MethodData*)info.buffered_addr();
 975       buffered_mdo->remove_unshareable_info();
 976     } else if (info.msotype() == MetaspaceObj::MethodCountersType) {
 977       MethodCounters* buffered_mc = (MethodCounters*)info.buffered_addr();
 978       buffered_mc->remove_unshareable_info();
 979     }
 980   };
 981   _src_obj_table.iterate_all(clean_td);
 982 }
 983 
 984 void ArchiveBuilder::serialize_dynamic_archivable_items(SerializeClosure* soc) {
 985   SymbolTable::serialize_shared_table_header(soc, false);
 986   SystemDictionaryShared::serialize_dictionary_headers(soc, false);
 987   DynamicArchive::serialize_array_klasses(soc);
 988   AOTLinkedClassBulkLoader::serialize(soc, false);
 989 }
 990 
 991 uintx ArchiveBuilder::buffer_to_offset(address p) const {
 992   address requested_p = to_requested(p);
 993   assert(requested_p >= _requested_static_archive_bottom, "must be");
 994   return requested_p - _requested_static_archive_bottom;
 995 }
 996 
 997 uintx ArchiveBuilder::any_to_offset(address p) const {
 998   if (is_in_mapped_static_archive(p)) {
 999     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1000     return p - _mapped_static_archive_bottom;
1001   }
1002   if (!is_in_buffer_space(p)) {
1003     // p must be a "source" address
1004     p = get_buffered_addr(p);
1005   }
1006   return buffer_to_offset(p);
1007 }
1008 
1009 void ArchiveBuilder::start_cc_region() {
1010   ro_region()->pack();
1011   start_dump_region(&_cc_region);
1012 }
1013 
1014 void ArchiveBuilder::end_cc_region() {
1015   _cc_region.pack();
1016 }
1017 
1018 address ArchiveBuilder::offset_to_buffered_address(u4 offset) const {
1019   address requested_addr = _requested_static_archive_bottom + offset;
1020   address buffered_addr = requested_addr - _buffer_to_requested_delta;
1021   assert(is_in_buffer_space(buffered_addr), "bad offset");
1022   return buffered_addr;
1023 }
1024 
1025 #if INCLUDE_CDS_JAVA_HEAP
1026 narrowKlass ArchiveBuilder::get_requested_narrow_klass(Klass* k) {
1027   assert(CDSConfig::is_dumping_heap(), "sanity");
1028   k = get_buffered_klass(k);
1029   Klass* requested_k = to_requested(k);
1030   const int narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
1031 #ifdef ASSERT
1032   const size_t klass_alignment = MAX2(SharedSpaceObjectAlignment, (size_t)nth_bit(narrow_klass_shift));
1033   assert(is_aligned(k, klass_alignment), "Klass " PTR_FORMAT " misaligned.", p2i(k));
1034 #endif
1035   address narrow_klass_base = _requested_static_archive_bottom; // runtime encoding base == runtime mapping start
1036   // Note: use the "raw" version of encode that takes explicit narrow klass base and shift. Don't use any
1037   // of the variants that do sanity checks, nor any of those that use the current - dump - JVM's encoding setting.
1038   return CompressedKlassPointers::encode_not_null_without_asserts(requested_k, narrow_klass_base, narrow_klass_shift);
1039 }
1040 #endif // INCLUDE_CDS_JAVA_HEAP
1041 
1042 // RelocateBufferToRequested --- Relocate all the pointers in rw/ro,
1043 // so that the archive can be mapped to the "requested" location without runtime relocation.
1044 //
1045 // - See ArchiveBuilder header for the definition of "buffer", "mapped" and "requested"
1046 // - ArchivePtrMarker::ptrmap() marks all the pointers in the rw/ro regions
1047 // - Every pointer must have one of the following values:
1048 //   [a] nullptr:
1049 //       No relocation is needed. Remove this pointer from ptrmap so we don't need to
1050 //       consider it at runtime.
1051 //   [b] Points into an object X which is inside the buffer:
1052 //       Adjust this pointer by _buffer_to_requested_delta, so it points to X
1053 //       when the archive is mapped at the requested location.
1054 //   [c] Points into an object Y which is inside mapped static archive:
1055 //       - This happens only during dynamic dump
1056 //       - Adjust this pointer by _mapped_to_requested_static_archive_delta,
1057 //         so it points to Y when the static archive is mapped at the requested location.
1058 template <bool STATIC_DUMP>
1059 class RelocateBufferToRequested : public BitMapClosure {
1060   ArchiveBuilder* _builder;
1061   address _buffer_bottom;
1062   intx _buffer_to_requested_delta;
1063   intx _mapped_to_requested_static_archive_delta;
1064   size_t _max_non_null_offset;
1065 
1066  public:
1067   RelocateBufferToRequested(ArchiveBuilder* builder) {
1068     _builder = builder;
1069     _buffer_bottom = _builder->buffer_bottom();
1070     _buffer_to_requested_delta = builder->buffer_to_requested_delta();
1071     _mapped_to_requested_static_archive_delta = builder->requested_static_archive_bottom() - builder->mapped_static_archive_bottom();
1072     _max_non_null_offset = 0;
1073 
1074     address bottom = _builder->buffer_bottom();
1075     address top = _builder->buffer_top();
1076     address new_bottom = bottom + _buffer_to_requested_delta;
1077     address new_top = top + _buffer_to_requested_delta;
1078     log_debug(cds)("Relocating archive from [" INTPTR_FORMAT " - " INTPTR_FORMAT "] to "
1079                    "[" INTPTR_FORMAT " - " INTPTR_FORMAT "]",
1080                    p2i(bottom), p2i(top),
1081                    p2i(new_bottom), p2i(new_top));
1082   }
1083 
1084   bool do_bit(size_t offset) {
1085     address* p = (address*)_buffer_bottom + offset;
1086     assert(_builder->is_in_buffer_space(p), "pointer must live in buffer space");
1087 
1088     if (*p == nullptr) {
1089       // todo -- clear bit, etc
1090       ArchivePtrMarker::ptrmap()->clear_bit(offset);
1091     } else {
1092       if (STATIC_DUMP) {
1093         assert(_builder->is_in_buffer_space(*p), "old pointer must point inside buffer space");
1094         *p += _buffer_to_requested_delta;
1095         assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1096       } else {
1097         if (_builder->is_in_buffer_space(*p)) {
1098           *p += _buffer_to_requested_delta;
1099           // assert is in requested dynamic archive
1100         } else {
1101           assert(_builder->is_in_mapped_static_archive(*p), "old pointer must point inside buffer space or mapped static archive");
1102           *p += _mapped_to_requested_static_archive_delta;
1103           assert(_builder->is_in_requested_static_archive(*p), "new pointer must point inside requested archive");
1104         }
1105       }
1106       _max_non_null_offset = offset;
1107     }
1108 
1109     return true; // keep iterating
1110   }
1111 
1112   void doit() {
1113     ArchivePtrMarker::ptrmap()->iterate(this);
1114     ArchivePtrMarker::compact(_max_non_null_offset);
1115   }
1116 };
1117 
1118 #ifdef _LP64
1119 int ArchiveBuilder::precomputed_narrow_klass_shift() {
1120   // Legacy Mode:
1121   //    We use 32 bits for narrowKlass, which should cover the full 4G Klass range. Shift can be 0.
1122   // CompactObjectHeader Mode:
1123   //    narrowKlass is much smaller, and we use the highest possible shift value to later get the maximum
1124   //    Klass encoding range.
1125   //
1126   // Note that all of this may change in the future, if we decide to correct the pre-calculated
1127   // narrow Klass IDs at archive load time.
1128   assert(UseCompressedClassPointers, "Only needed for compressed class pointers");
1129   return UseCompactObjectHeaders ?  CompressedKlassPointers::max_shift() : 0;
1130 }
1131 #endif // _LP64
1132 
1133 void ArchiveBuilder::relocate_to_requested() {
1134   if (!ro_region()->is_packed()) {
1135     ro_region()->pack();
1136   }
1137 
1138   size_t my_archive_size = buffer_top() - buffer_bottom();
1139 
1140   if (CDSConfig::is_dumping_static_archive()) {
1141     _requested_static_archive_top = _requested_static_archive_bottom + my_archive_size;
1142     RelocateBufferToRequested<true> patcher(this);
1143     patcher.doit();
1144   } else {
1145     assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
1146     _requested_dynamic_archive_top = _requested_dynamic_archive_bottom + my_archive_size;
1147     RelocateBufferToRequested<false> patcher(this);
1148     patcher.doit();
1149   }
1150 }
1151 
1152 // Write detailed info to a mapfile to analyze contents of the archive.
1153 // static dump:
1154 //   java -Xshare:dump -Xlog:cds+map=trace:file=cds.map:none:filesize=0
1155 // dynamic dump:
1156 //   java -cp MyApp.jar -XX:ArchiveClassesAtExit=MyApp.jsa \
1157 //        -Xlog:cds+map=trace:file=cds.map:none:filesize=0 MyApp
1158 //
1159 // We need to do some address translation because the buffers used at dump time may be mapped to
1160 // a different location at runtime. At dump time, the buffers may be at arbitrary locations
1161 // picked by the OS. At runtime, we try to map at a fixed location (SharedBaseAddress). For
1162 // consistency, we log everything using runtime addresses.
1163 class ArchiveBuilder::CDSMapLogger : AllStatic {
1164   static intx buffer_to_runtime_delta() {
1165     // Translate the buffers used by the RW/RO regions to their eventual (requested) locations
1166     // at runtime.
1167     return ArchiveBuilder::current()->buffer_to_requested_delta();
1168   }
1169 
1170   // rw/ro regions only
1171   static void log_metaspace_region(const char* name, DumpRegion* region,
1172                                    const ArchiveBuilder::SourceObjList* src_objs) {
1173     address region_base = address(region->base());
1174     address region_top  = address(region->top());
1175     log_region(name, region_base, region_top, region_base + buffer_to_runtime_delta());
1176     log_metaspace_objects(region, src_objs);
1177   }
1178 
1179 #define _LOG_PREFIX PTR_FORMAT ": @@ %-17s %d"
1180 
1181   static void log_klass(Klass* k, address runtime_dest, const char* type_name, int bytes, Thread* current) {
1182     ResourceMark rm(current);
1183     log_debug(cds, map)(_LOG_PREFIX " %s",
1184                         p2i(runtime_dest), type_name, bytes, k->external_name());
1185   }
1186   static void log_method(Method* m, address runtime_dest, const char* type_name, int bytes, Thread* current) {
1187     ResourceMark rm(current);
1188     log_debug(cds, map)(_LOG_PREFIX " %s",
1189                         p2i(runtime_dest), type_name, bytes,  m->external_name());
1190   }
1191 
1192   // rw/ro regions only
1193   static void log_metaspace_objects(DumpRegion* region, const ArchiveBuilder::SourceObjList* src_objs) {
1194     address last_obj_base = address(region->base());
1195     address last_obj_end  = address(region->base());
1196     address region_end    = address(region->end());
1197     Thread* current = Thread::current();
1198     for (int i = 0; i < src_objs->objs()->length(); i++) {
1199       SourceObjInfo* src_info = src_objs->at(i);
1200       address src = src_info->source_addr();
1201       address dest = src_info->buffered_addr();
1202       log_as_hex(last_obj_base, dest, last_obj_base + buffer_to_runtime_delta());
1203       address runtime_dest = dest + buffer_to_runtime_delta();
1204       int bytes = src_info->size_in_bytes();
1205 
1206       MetaspaceObj::Type type = src_info->msotype();
1207       const char* type_name = MetaspaceObj::type_name(type);
1208 
1209       switch (type) {
1210       case MetaspaceObj::ClassType:
1211         log_klass((Klass*)src, runtime_dest, type_name, bytes, current);
1212         break;
1213       case MetaspaceObj::ConstantPoolType:
1214         log_klass(((ConstantPool*)src)->pool_holder(),
1215                     runtime_dest, type_name, bytes, current);
1216         break;
1217       case MetaspaceObj::ConstantPoolCacheType:
1218         log_klass(((ConstantPoolCache*)src)->constant_pool()->pool_holder(),
1219                     runtime_dest, type_name, bytes, current);
1220         break;
1221       case MetaspaceObj::MethodType:
1222         log_method((Method*)src, runtime_dest, type_name, bytes, current);
1223         break;
1224       case MetaspaceObj::ConstMethodType:
1225         log_method(((ConstMethod*)src)->method(), runtime_dest, type_name, bytes, current);
1226         break;
1227       case MetaspaceObj::SymbolType:
1228         {
1229           ResourceMark rm(current);
1230           Symbol* s = (Symbol*)src;
1231           log_debug(cds, map)(_LOG_PREFIX " %s", p2i(runtime_dest), type_name, bytes,
1232                               s->as_quoted_ascii());
1233         }
1234         break;
1235       default:
1236         log_debug(cds, map)(_LOG_PREFIX, p2i(runtime_dest), type_name, bytes);
1237         break;
1238       }
1239 
1240       last_obj_base = dest;
1241       last_obj_end  = dest + bytes;
1242     }
1243 
1244     log_as_hex(last_obj_base, last_obj_end, last_obj_base + buffer_to_runtime_delta());
1245     if (last_obj_end < region_end) {
1246       log_debug(cds, map)(PTR_FORMAT ": @@ Misc data %zu bytes",
1247                           p2i(last_obj_end + buffer_to_runtime_delta()),
1248                           size_t(region_end - last_obj_end));
1249       log_as_hex(last_obj_end, region_end, last_obj_end + buffer_to_runtime_delta());
1250     }
1251   }
1252 
1253 #undef _LOG_PREFIX
1254 
1255   // Log information about a region, whose address at dump time is [base .. top). At
1256   // runtime, this region will be mapped to requested_base. requested_base is 0 if this
1257   // region will be mapped at os-selected addresses (such as the bitmap region), or will
1258   // be accessed with os::read (the header).
1259   //
1260   // Note: across -Xshare:dump runs, base may be different, but requested_base should
1261   // be the same as the archive contents should be deterministic.
1262   static void log_region(const char* name, address base, address top, address requested_base) {
1263     size_t size = top - base;
1264     base = requested_base;
1265     top = requested_base + size;
1266     log_info(cds, map)("[%-18s " PTR_FORMAT " - " PTR_FORMAT " %9zu bytes]",
1267                        name, p2i(base), p2i(top), size);
1268   }
1269 
1270 #if INCLUDE_CDS_JAVA_HEAP
1271   static void log_heap_region(ArchiveHeapInfo* heap_info) {
1272     MemRegion r = heap_info->buffer_region();
1273     address start = address(r.start()); // start of the current oop inside the buffer
1274     address end = address(r.end());
1275     log_region("heap", start, end, ArchiveHeapWriter::buffered_addr_to_requested_addr(start));
1276 
1277     LogStreamHandle(Info, cds, map) st;
1278 
1279     HeapRootSegments segments = heap_info->heap_root_segments();
1280     assert(segments.base_offset() == 0, "Sanity");
1281 
1282     for (size_t seg_idx = 0; seg_idx < segments.count(); seg_idx++) {
1283       address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start);
1284       st.print_cr(PTR_FORMAT ": Heap roots segment [%d]",
1285                   p2i(requested_start), segments.size_in_elems(seg_idx));
1286       start += segments.size_in_bytes(seg_idx);
1287     }
1288     log_heap_roots();
1289 
1290     while (start < end) {
1291       size_t byte_size;
1292       oop source_oop = ArchiveHeapWriter::buffered_addr_to_source_obj(start);
1293       address requested_start = ArchiveHeapWriter::buffered_addr_to_requested_addr(start);
1294       st.print(PTR_FORMAT ": @@ Object ", p2i(requested_start));
1295 
1296       if (source_oop != nullptr) {
1297         // This is a regular oop that got archived.
1298         // Don't print the requested addr again as we have just printed it at the beginning of the line.
1299         // Example:
1300         // 0x00000007ffd27938: @@ Object (0xfffa4f27) java.util.HashMap
1301         print_oop_info_cr(&st, source_oop, /*print_requested_addr=*/false);
1302         byte_size = source_oop->size() * BytesPerWord;
1303       } else if ((byte_size = ArchiveHeapWriter::get_filler_size_at(start)) > 0) {
1304         // We have a filler oop, which also does not exist in BufferOffsetToSourceObjectTable.
1305         // Example:
1306         // 0x00000007ffc3ffd8: @@ Object filler 40 bytes
1307         st.print_cr("filler %zu bytes", byte_size);
1308       } else {
1309         ShouldNotReachHere();
1310       }
1311 
1312       address oop_end = start + byte_size;
1313       log_as_hex(start, oop_end, requested_start, /*is_heap=*/true);
1314 
1315       if (source_oop != nullptr) {
1316         log_oop_details(heap_info, source_oop, /*buffered_addr=*/start);
1317       }
1318       start = oop_end;
1319     }
1320   }
1321 
1322   // ArchivedFieldPrinter is used to print the fields of archived objects. We can't
1323   // use _source_obj->print_on(), because we want to print the oop fields
1324   // in _source_obj with their requested addresses using print_oop_info_cr().
1325   class ArchivedFieldPrinter : public FieldClosure {
1326     ArchiveHeapInfo* _heap_info;
1327     outputStream* _st;
1328     oop _source_obj;
1329     address _buffered_addr;
1330   public:
1331     ArchivedFieldPrinter(ArchiveHeapInfo* heap_info, outputStream* st, oop src_obj, address buffered_addr) :
1332       _heap_info(heap_info), _st(st), _source_obj(src_obj), _buffered_addr(buffered_addr) {}
1333 
1334     void do_field(fieldDescriptor* fd) {
1335       _st->print(" - ");
1336       BasicType ft = fd->field_type();
1337       switch (ft) {
1338       case T_ARRAY:
1339       case T_OBJECT:
1340         {
1341           fd->print_on(_st); // print just the name and offset
1342           oop obj = _source_obj->obj_field(fd->offset());
1343           if (java_lang_Class::is_instance(obj)) {
1344             obj = HeapShared::scratch_java_mirror(obj);
1345           }
1346           print_oop_info_cr(_st, obj);
1347         }
1348         break;
1349       default:
1350         if (ArchiveHeapWriter::is_marked_as_native_pointer(_heap_info, _source_obj, fd->offset())) {
1351           print_as_native_pointer(fd);
1352         } else {
1353           fd->print_on_for(_st, cast_to_oop(_buffered_addr)); // name, offset, value
1354           _st->cr();
1355         }
1356       }
1357     }
1358 
1359     void print_as_native_pointer(fieldDescriptor* fd) {
1360       LP64_ONLY(assert(fd->field_type() == T_LONG, "must be"));
1361       NOT_LP64 (assert(fd->field_type() == T_INT,  "must be"));
1362 
1363       // We have a field that looks like an integer, but it's actually a pointer to a MetaspaceObj.
1364       address source_native_ptr = (address)
1365           LP64_ONLY(_source_obj->long_field(fd->offset()))
1366           NOT_LP64( _source_obj->int_field (fd->offset()));
1367       ArchiveBuilder* builder = ArchiveBuilder::current();
1368 
1369       // The value of the native pointer at runtime.
1370       address requested_native_ptr = builder->to_requested(builder->get_buffered_addr(source_native_ptr));
1371 
1372       // The address of _source_obj at runtime
1373       oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(_source_obj);
1374       // The address of this field in the requested space
1375       assert(requested_obj != nullptr, "Attempting to load field from null oop");
1376       address requested_field_addr = cast_from_oop<address>(requested_obj) + fd->offset();
1377 
1378       fd->print_on(_st);
1379       _st->print_cr(PTR_FORMAT " (marked metadata pointer @" PTR_FORMAT " )",
1380                     p2i(requested_native_ptr), p2i(requested_field_addr));
1381     }
1382   };
1383 
1384   // Print the fields of instanceOops, or the elements of arrayOops
1385   static void log_oop_details(ArchiveHeapInfo* heap_info, oop source_oop, address buffered_addr) {
1386     LogStreamHandle(Trace, cds, map, oops) st;
1387     if (st.is_enabled()) {
1388       Klass* source_klass = source_oop->klass();
1389       ArchiveBuilder* builder = ArchiveBuilder::current();
1390       Klass* requested_klass = builder->to_requested(builder->get_buffered_addr(source_klass));
1391 
1392       st.print(" - klass: ");
1393       source_klass->print_value_on(&st);
1394       st.print(" " PTR_FORMAT, p2i(requested_klass));
1395       st.cr();
1396 
1397       if (source_oop->is_typeArray()) {
1398         TypeArrayKlass::cast(source_klass)->oop_print_elements_on(typeArrayOop(source_oop), &st);
1399       } else if (source_oop->is_objArray()) {
1400         objArrayOop source_obj_array = objArrayOop(source_oop);
1401         for (int i = 0; i < source_obj_array->length(); i++) {
1402           st.print(" -%4d: ", i);
1403           oop obj = source_obj_array->obj_at(i);
1404           if (java_lang_Class::is_instance(obj)) {
1405             obj = HeapShared::scratch_java_mirror(obj);
1406           }
1407           print_oop_info_cr(&st, obj);
1408         }
1409       } else {
1410         st.print_cr(" - fields (%zu words):", source_oop->size());
1411         ArchivedFieldPrinter print_field(heap_info, &st, source_oop, buffered_addr);
1412         InstanceKlass::cast(source_klass)->print_nonstatic_fields(&print_field);
1413 
1414         if (java_lang_Class::is_instance(source_oop)) {
1415           oop scratch_mirror = source_oop;
1416           st.print(" - signature: ");
1417           print_class_signature_for_mirror(&st, scratch_mirror);
1418           st.cr();
1419 
1420           Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror);
1421           if (src_klass != nullptr && src_klass->is_instance_klass()) {
1422             oop rr = HeapShared::scratch_resolved_references(InstanceKlass::cast(src_klass)->constants());
1423             st.print(" - archived_resolved_references: ");
1424             print_oop_info_cr(&st, rr);
1425 
1426             // We need to print the fields in the scratch_mirror, not the original mirror.
1427             // (if a class is not aot-initialized, static fields in its scratch mirror will be cleared).
1428             assert(scratch_mirror == HeapShared::scratch_java_mirror(src_klass->java_mirror()), "sanity");
1429             st.print_cr("- ---- static fields (%d):", java_lang_Class::static_oop_field_count(scratch_mirror));
1430             InstanceKlass::cast(src_klass)->do_local_static_fields(&print_field);
1431           }
1432         }
1433       }
1434     }
1435   }
1436 
1437   static void print_class_signature_for_mirror(outputStream* st, oop scratch_mirror) {
1438     assert(java_lang_Class::is_instance(scratch_mirror), "sanity");
1439     if (java_lang_Class::is_primitive(scratch_mirror)) {
1440       for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
1441         BasicType bt = (BasicType)i;
1442         if (!is_reference_type(bt) && scratch_mirror == HeapShared::scratch_java_mirror(bt)) {
1443           oop orig_mirror = Universe::java_mirror(bt);
1444           java_lang_Class::print_signature(orig_mirror, st);
1445           return;
1446         }
1447       }
1448       ShouldNotReachHere();
1449     }
1450     java_lang_Class::print_signature(scratch_mirror, st);
1451   }
1452 
1453   static void log_heap_roots() {
1454     LogStreamHandle(Trace, cds, map, oops) st;
1455     if (st.is_enabled()) {
1456       for (int i = 0; i < HeapShared::pending_roots()->length(); i++) {
1457         st.print("roots[%4d]: ", i);
1458         print_oop_info_cr(&st, HeapShared::pending_roots()->at(i).resolve());
1459       }
1460     }
1461   }
1462 
1463   // Example output:
1464   // - The first number is the requested address (if print_requested_addr == true)
1465   // - The second number is the narrowOop version of the requested address (if UseCompressedOops == true)
1466   //     0x00000007ffc7e840 (0xfff8fd08) java.lang.Class Ljava/util/Array;
1467   //     0x00000007ffc000f8 (0xfff8001f) [B length: 11
1468   static void print_oop_info_cr(outputStream* st, oop source_oop, bool print_requested_addr = true) {
1469     if (source_oop == nullptr) {
1470       st->print_cr("null");
1471     } else {
1472       ResourceMark rm;
1473       oop requested_obj = ArchiveHeapWriter::source_obj_to_requested_obj(source_oop);
1474       if (print_requested_addr) {
1475         st->print(PTR_FORMAT " ", p2i(requested_obj));
1476       }
1477       if (UseCompressedOops) {
1478         st->print("(0x%08x) ", CompressedOops::narrow_oop_value(requested_obj));
1479       }
1480       if (source_oop->is_array()) {
1481         int array_len = arrayOop(source_oop)->length();
1482         st->print_cr("%s length: %d", source_oop->klass()->external_name(), array_len);
1483       } else {
1484         st->print("%s", source_oop->klass()->external_name());
1485 
1486         if (java_lang_String::is_instance(source_oop)) {
1487           st->print(" ");
1488           java_lang_String::print(source_oop, st);
1489         } else if (java_lang_invoke_MethodType::is_instance(source_oop)) {
1490           st->print(" ");
1491           java_lang_invoke_MethodType::print_signature(source_oop, st);
1492         } else if (java_lang_Class::is_instance(source_oop)) {
1493           oop scratch_mirror = source_oop;
1494 
1495           st->print(" ");
1496           print_class_signature_for_mirror(st, scratch_mirror);
1497 
1498           Klass* src_klass = java_lang_Class::as_Klass(scratch_mirror);
1499           if (src_klass != nullptr && src_klass->is_instance_klass()) {
1500             InstanceKlass* buffered_klass =
1501               ArchiveBuilder::current()->get_buffered_addr(InstanceKlass::cast(src_klass));
1502             if (buffered_klass->has_aot_initialized_mirror()) {
1503               st->print(" (aot-inited)");
1504             }
1505           }
1506         }
1507         st->cr();
1508       }
1509     }
1510   }
1511 #endif // INCLUDE_CDS_JAVA_HEAP
1512 
1513   // Log all the data [base...top). Pretend that the base address
1514   // will be mapped to requested_base at run-time.
1515   static void log_as_hex(address base, address top, address requested_base, bool is_heap = false) {
1516     assert(top >= base, "must be");
1517 
1518     LogStreamHandle(Trace, cds, map) lsh;
1519     if (lsh.is_enabled()) {
1520       int unitsize = sizeof(address);
1521       if (is_heap && UseCompressedOops) {
1522         // This makes the compressed oop pointers easier to read, but
1523         // longs and doubles will be split into two words.
1524         unitsize = sizeof(narrowOop);
1525       }
1526       os::print_hex_dump(&lsh, base, top, unitsize, /* print_ascii=*/true, /* bytes_per_line=*/32, requested_base);
1527     }
1528   }
1529 
1530   static void log_header(FileMapInfo* mapinfo) {
1531     LogStreamHandle(Info, cds, map) lsh;
1532     if (lsh.is_enabled()) {
1533       mapinfo->print(&lsh);
1534     }
1535   }
1536 
1537 public:
1538   static void log(ArchiveBuilder* builder, FileMapInfo* mapinfo,
1539                   ArchiveHeapInfo* heap_info,
1540                   char* bitmap, size_t bitmap_size_in_bytes) {
1541     log_info(cds, map)("%s CDS archive map for %s", CDSConfig::is_dumping_static_archive() ? "Static" : "Dynamic", mapinfo->full_path());
1542 
1543     address header = address(mapinfo->header());
1544     address header_end = header + mapinfo->header()->header_size();
1545     log_region("header", header, header_end, nullptr);
1546     log_header(mapinfo);
1547     log_as_hex(header, header_end, nullptr);
1548 
1549     DumpRegion* rw_region = &builder->_rw_region;
1550     DumpRegion* ro_region = &builder->_ro_region;
1551 
1552     log_metaspace_region("rw region", rw_region, &builder->_rw_src_objs);
1553     log_metaspace_region("ro region", ro_region, &builder->_ro_src_objs);
1554 
1555     address bitmap_end = address(bitmap + bitmap_size_in_bytes);
1556     log_region("bitmap", address(bitmap), bitmap_end, nullptr);
1557     log_as_hex((address)bitmap, bitmap_end, nullptr);
1558 
1559 #if INCLUDE_CDS_JAVA_HEAP
1560     if (heap_info->is_used()) {
1561       log_heap_region(heap_info);
1562     }
1563 #endif
1564 
1565     log_info(cds, map)("[End of CDS archive map]");
1566   }
1567 }; // end ArchiveBuilder::CDSMapLogger
1568 
1569 void ArchiveBuilder::print_stats() {
1570   _alloc_stats.print_stats(int(_ro_region.used()), int(_rw_region.used()));
1571 }
1572 
1573 void ArchiveBuilder::write_archive(FileMapInfo* mapinfo, ArchiveHeapInfo* heap_info) {
1574   // Make sure NUM_CDS_REGIONS (exported in cds.h) agrees with
1575   // MetaspaceShared::n_regions (internal to hotspot).
1576   assert(NUM_CDS_REGIONS == MetaspaceShared::n_regions, "sanity");
1577 
1578   write_region(mapinfo, MetaspaceShared::rw, &_rw_region, /*read_only=*/false,/*allow_exec=*/false);
1579   write_region(mapinfo, MetaspaceShared::ro, &_ro_region, /*read_only=*/true, /*allow_exec=*/false);
1580   write_region(mapinfo, MetaspaceShared::cc, &_cc_region, /*read_only=*/false,/*allow_exec=*/false);
1581 
1582   // Split pointer map into read-write and read-only bitmaps
1583   ArchivePtrMarker::initialize_rw_ro_cc_maps(&_rw_ptrmap, &_ro_ptrmap, &_cc_ptrmap);
1584 
1585   size_t bitmap_size_in_bytes;
1586   char* bitmap = mapinfo->write_bitmap_region(ArchivePtrMarker::rw_ptrmap(),
1587                                               ArchivePtrMarker::ro_ptrmap(),
1588                                               ArchivePtrMarker::cc_ptrmap(),
1589                                               heap_info,
1590                                               bitmap_size_in_bytes);
1591 
1592   if (heap_info->is_used()) {
1593     _total_heap_region_size = mapinfo->write_heap_region(heap_info);
1594   }
1595 
1596   print_region_stats(mapinfo, heap_info);
1597 
1598   mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1599   mapinfo->set_header_crc(mapinfo->compute_header_crc());
1600   // After this point, we should not write any data into mapinfo->header() since this
1601   // would corrupt its checksum we have calculated before.
1602   mapinfo->write_header();
1603   mapinfo->close();
1604 
1605   if (log_is_enabled(Info, cds)) {
1606     print_stats();
1607   }
1608 
1609   if (log_is_enabled(Info, cds, map)) {
1610     CDSMapLogger::log(this, mapinfo, heap_info,
1611                       bitmap, bitmap_size_in_bytes);
1612   }
1613   CDS_JAVA_HEAP_ONLY(HeapShared::destroy_archived_object_cache());
1614   FREE_C_HEAP_ARRAY(char, bitmap);
1615 }
1616 
1617 void ArchiveBuilder::write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region, bool read_only,  bool allow_exec) {
1618   mapinfo->write_region(region_idx, dump_region->base(), dump_region->used(), read_only, allow_exec);
1619 }
1620 
1621 void ArchiveBuilder::print_region_stats(FileMapInfo *mapinfo, ArchiveHeapInfo* heap_info) {
1622   // Print statistics of all the regions
1623   const size_t bitmap_used = mapinfo->region_at(MetaspaceShared::bm)->used();
1624   const size_t bitmap_reserved = mapinfo->region_at(MetaspaceShared::bm)->used_aligned();
1625   const size_t total_reserved = _ro_region.reserved()  + _rw_region.reserved() +
1626                                 bitmap_reserved +
1627                                 _total_heap_region_size;
1628   const size_t total_bytes = _ro_region.used()  + _rw_region.used() +
1629                              bitmap_used +
1630                              _total_heap_region_size;
1631   const double total_u_perc = percent_of(total_bytes, total_reserved);
1632 
1633   _rw_region.print(total_reserved);
1634   _ro_region.print(total_reserved);
1635   _cc_region.print(total_reserved);
1636 
1637   print_bitmap_region_stats(bitmap_used, total_reserved);
1638 
1639   if (heap_info->is_used()) {
1640     print_heap_region_stats(heap_info, total_reserved);
1641   }
1642 
1643   log_debug(cds)("total   : %9zu [100.0%% of total] out of %9zu bytes [%5.1f%% used]",
1644                  total_bytes, total_reserved, total_u_perc);
1645 }
1646 
1647 void ArchiveBuilder::print_bitmap_region_stats(size_t size, size_t total_size) {
1648   log_debug(cds)("bm space: %9zu [ %4.1f%% of total] out of %9zu bytes [100.0%% used]",
1649                  size, size/double(total_size)*100.0, size);
1650 }
1651 
1652 void ArchiveBuilder::print_heap_region_stats(ArchiveHeapInfo *info, size_t total_size) {
1653   char* start = info->buffer_start();
1654   size_t size = info->buffer_byte_size();
1655   char* top = start + size;
1656   log_debug(cds)("hp space: %9zu [ %4.1f%% of total] out of %9zu bytes [100.0%% used] at " INTPTR_FORMAT,
1657                      size, size/double(total_size)*100.0, size, p2i(start));
1658 }
1659 
1660 void ArchiveBuilder::report_out_of_space(const char* name, size_t needed_bytes) {
1661   // This is highly unlikely to happen on 64-bits because we have reserved a 4GB space.
1662   // On 32-bit we reserve only 256MB so you could run out of space with 100,000 classes
1663   // or so.
1664   _rw_region.print_out_of_space_msg(name, needed_bytes);
1665   _ro_region.print_out_of_space_msg(name, needed_bytes);
1666 
1667   log_error(cds)("Unable to allocate from '%s' region: Please reduce the number of shared classes.", name);
1668   MetaspaceShared::unrecoverable_writing_error();
1669 }