1 /*
   2  * Copyright (c) 2012, 2017, 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 "classfile/classListParser.hpp"
  27 #include "classfile/classLoaderExt.hpp"
  28 #include "classfile/dictionary.hpp"
  29 #include "classfile/loaderConstraints.hpp"
  30 #include "classfile/placeholders.hpp"
  31 #include "classfile/sharedClassUtil.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/systemDictionaryShared.hpp"
  35 #include "code/codeCache.hpp"
  36 #include "gc/shared/gcLocker.hpp"
  37 #include "interpreter/bytecodeStream.hpp"
  38 #include "interpreter/bytecodes.hpp"
  39 #include "logging/log.hpp"
  40 #include "logging/logMessage.hpp"
  41 #include "memory/filemap.hpp"
  42 #include "memory/metaspace.hpp"
  43 #include "memory/metaspaceShared.hpp"
  44 #include "memory/resourceArea.hpp"
  45 #include "oops/instanceClassLoaderKlass.hpp"
  46 #include "oops/instanceMirrorKlass.hpp"
  47 #include "oops/instanceRefKlass.hpp"
  48 #include "oops/objArrayKlass.hpp"
  49 #include "oops/objArrayOop.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/typeArrayKlass.hpp"
  52 #include "runtime/timerTrace.hpp"
  53 #include "runtime/os.hpp"
  54 #include "runtime/signature.hpp"
  55 #include "runtime/vmThread.hpp"
  56 #include "runtime/vm_operations.hpp"
  57 #include "utilities/defaultStream.hpp"
  58 #include "utilities/hashtable.inline.hpp"
  59 
  60 int MetaspaceShared::_max_alignment = 0;
  61 
  62 ReservedSpace* MetaspaceShared::_shared_rs = NULL;
  63 
  64 MetaspaceSharedStats MetaspaceShared::_stats;
  65 
  66 bool MetaspaceShared::_has_error_classes;
  67 bool MetaspaceShared::_archive_loading_failed = false;
  68 bool MetaspaceShared::_remapped_readwrite = false;
  69 address MetaspaceShared::_cds_i2i_entry_code_buffers = NULL;
  70 size_t MetaspaceShared::_cds_i2i_entry_code_buffers_size = 0;
  71 SharedMiscRegion MetaspaceShared::_mc;
  72 SharedMiscRegion MetaspaceShared::_md;
  73 SharedMiscRegion MetaspaceShared::_od;
  74 
  75 void SharedMiscRegion::initialize(ReservedSpace rs, size_t committed_byte_size,  SharedSpaceType space_type) {
  76   _vs.initialize(rs, committed_byte_size);
  77   _alloc_top = _vs.low();
  78   _space_type = space_type;
  79 }
  80 
  81 // NOT thread-safe, but this is called during dump time in single-threaded mode.
  82 char* SharedMiscRegion::alloc(size_t num_bytes) {
  83   assert(DumpSharedSpaces, "dump time only");
  84   size_t alignment = sizeof(char*);
  85   num_bytes = align_size_up(num_bytes, alignment);
  86   _alloc_top = (char*)align_ptr_up(_alloc_top, alignment);
  87   if (_alloc_top + num_bytes > _vs.high()) {
  88     report_out_of_shared_space(_space_type);
  89   }
  90 
  91   char* p = _alloc_top;
  92   _alloc_top += num_bytes;
  93 
  94   memset(p, 0, num_bytes);
  95   return p;
  96 }
  97 
  98 void MetaspaceShared::initialize_shared_rs(ReservedSpace* rs) {
  99   assert(DumpSharedSpaces, "dump time only");
 100   _shared_rs = rs;
 101 
 102   size_t core_spaces_size = FileMapInfo::core_spaces_size();
 103   size_t metadata_size = SharedReadOnlySize + SharedReadWriteSize;
 104 
 105   // Split into the core and optional sections
 106   ReservedSpace core_data = _shared_rs->first_part(core_spaces_size);
 107   ReservedSpace optional_data = _shared_rs->last_part(core_spaces_size);
 108 
 109   // The RO/RW and the misc sections
 110   ReservedSpace shared_ro_rw = core_data.first_part(metadata_size);
 111   ReservedSpace misc_section = core_data.last_part(metadata_size);
 112 
 113   // Now split the misc code and misc data sections.
 114   ReservedSpace md_rs   = misc_section.first_part(SharedMiscDataSize);
 115   ReservedSpace mc_rs   = misc_section.last_part(SharedMiscDataSize);
 116 
 117   _md.initialize(md_rs, SharedMiscDataSize, SharedMiscData);
 118   _mc.initialize(mc_rs, SharedMiscCodeSize, SharedMiscCode);
 119   _od.initialize(optional_data, metadata_size, SharedOptional);
 120 }
 121 
 122 // Read/write a data stream for restoring/preserving metadata pointers and
 123 // miscellaneous data from/to the shared archive file.
 124 
 125 void MetaspaceShared::serialize(SerializeClosure* soc, GrowableArray<MemRegion> *string_space,
 126                                 size_t* space_size) {
 127   int tag = 0;
 128   soc->do_tag(--tag);
 129 
 130   // Verify the sizes of various metadata in the system.
 131   soc->do_tag(sizeof(Method));
 132   soc->do_tag(sizeof(ConstMethod));
 133   soc->do_tag(arrayOopDesc::base_offset_in_bytes(T_BYTE));
 134   soc->do_tag(sizeof(ConstantPool));
 135   soc->do_tag(sizeof(ConstantPoolCache));
 136   soc->do_tag(objArrayOopDesc::base_offset_in_bytes());
 137   soc->do_tag(typeArrayOopDesc::base_offset_in_bytes(T_BYTE));
 138   soc->do_tag(sizeof(Symbol));
 139 
 140   // Dump/restore miscellaneous metadata.
 141   Universe::serialize(soc, true);
 142   soc->do_tag(--tag);
 143 
 144   // Dump/restore references to commonly used names and signatures.
 145   vmSymbols::serialize(soc);
 146   soc->do_tag(--tag);
 147 
 148   // Dump/restore the symbol and string tables
 149   SymbolTable::serialize(soc);
 150   StringTable::serialize(soc, string_space, space_size);
 151   soc->do_tag(--tag);
 152 
 153   soc->do_tag(666);
 154 }
 155 
 156 address MetaspaceShared::cds_i2i_entry_code_buffers(size_t total_size) {
 157   if (DumpSharedSpaces) {
 158     if (_cds_i2i_entry_code_buffers == NULL) {
 159       _cds_i2i_entry_code_buffers = (address)misc_data_space_alloc(total_size);
 160       _cds_i2i_entry_code_buffers_size = total_size;
 161     }
 162   } else if (UseSharedSpaces) {
 163     assert(_cds_i2i_entry_code_buffers != NULL, "must already been initialized");
 164   } else {
 165     return NULL;
 166   }
 167 
 168   assert(_cds_i2i_entry_code_buffers_size == total_size, "must not change");
 169   return _cds_i2i_entry_code_buffers;
 170 }
 171 
 172 // CDS code for dumping shared archive.
 173 
 174 // Global object for holding classes that have been loaded.  Since this
 175 // is run at a safepoint just before exit, this is the entire set of classes.
 176 static GrowableArray<Klass*>* _global_klass_objects;
 177 class CollectClassesClosure : public KlassClosure {
 178   void do_klass(Klass* k) {
 179     _global_klass_objects->append_if_missing(k);
 180   }
 181 };
 182 
 183 static void remove_unshareable_in_classes() {
 184   for (int i = 0; i < _global_klass_objects->length(); i++) {
 185     Klass* k = _global_klass_objects->at(i);
 186     k->remove_unshareable_info();
 187   }
 188 }
 189 
 190 static void rewrite_nofast_bytecode(Method* method) {
 191   RawBytecodeStream bcs(method);
 192   while (!bcs.is_last_bytecode()) {
 193     Bytecodes::Code opcode = bcs.raw_next();
 194     switch (opcode) {
 195     case Bytecodes::_getfield:      *bcs.bcp() = Bytecodes::_nofast_getfield;      break;
 196     case Bytecodes::_putfield:      *bcs.bcp() = Bytecodes::_nofast_putfield;      break;
 197     case Bytecodes::_aload_0:       *bcs.bcp() = Bytecodes::_nofast_aload_0;       break;
 198     case Bytecodes::_iload: {
 199       if (!bcs.is_wide()) {
 200         *bcs.bcp() = Bytecodes::_nofast_iload;
 201       }
 202       break;
 203     }
 204     default: break;
 205     }
 206   }
 207 }
 208 
 209 // Walk all methods in the class list to ensure that they won't be modified at
 210 // run time. This includes:
 211 // [1] Rewrite all bytecodes as needed, so that the ConstMethod* will not be modified
 212 //     at run time by RewriteBytecodes/RewriteFrequentPairs
 213 // [2] Assign a fingerprint, so one doesn't need to be assigned at run-time.
 214 static void rewrite_nofast_bytecodes_and_calculate_fingerprints() {
 215   for (int i = 0; i < _global_klass_objects->length(); i++) {
 216     Klass* k = _global_klass_objects->at(i);
 217     if (k->is_instance_klass()) {
 218       InstanceKlass* ik = InstanceKlass::cast(k);
 219       for (int i = 0; i < ik->methods()->length(); i++) {
 220         Method* m = ik->methods()->at(i);
 221         rewrite_nofast_bytecode(m);
 222         Fingerprinter fp(m);
 223         // The side effect of this call sets method's fingerprint field.
 224         fp.fingerprint();
 225       }
 226     }
 227   }
 228 }
 229 
 230 // Objects of the Metadata types (such as Klass and ConstantPool) have C++ vtables.
 231 // (In GCC this is the field <Type>::_vptr, i.e., first word in the object.)
 232 //
 233 // Addresses of the vtables and the methods may be different across JVM runs,
 234 // if libjvm.so is dynamically loaded at a different base address.
 235 //
 236 // To ensure that the Metadata objects in the CDS archive always have the correct vtable:
 237 //
 238 // + at dump time:  we redirect the _vptr to point to our own vtables inside
 239 //                  the CDS image
 240 // + at run time:   we clone the actual contents of the vtables from libjvm.so
 241 //                  into our own tables.
 242 
 243 // Currently, the archive contain ONLY the following types of objects that have C++ vtables.
 244 #define CPP_VTABLE_PATCH_TYPES_DO(f) \
 245   f(ConstantPool) \
 246   f(InstanceKlass) \
 247   f(InstanceClassLoaderKlass) \
 248   f(InstanceMirrorKlass) \
 249   f(InstanceRefKlass) \
 250   f(Method) \
 251   f(ObjArrayKlass) \
 252   f(TypeArrayKlass)
 253 
 254 class CppVtableInfo {
 255   intptr_t _vtable_size;
 256   intptr_t _cloned_vtable[1];
 257 public:
 258   static int num_slots(int vtable_size) {
 259     return 1 + vtable_size; // Need to add the space occupied by _vtable_size;
 260   }
 261   int vtable_size()           { return int(uintx(_vtable_size)); }
 262   void set_vtable_size(int n) { _vtable_size = intptr_t(n); }
 263   intptr_t* cloned_vtable()   { return &_cloned_vtable[0]; }
 264   void zero()                 { memset(_cloned_vtable, 0, sizeof(intptr_t) * vtable_size()); }
 265   // Returns the address of the next CppVtableInfo that can be placed immediately after this CppVtableInfo
 266   intptr_t* next(int vtable_size) {
 267     return &_cloned_vtable[vtable_size];
 268   }
 269 };
 270 
 271 template <class T> class CppVtableCloner : public T {
 272   static intptr_t* vtable_of(Metadata& m) {
 273     return *((intptr_t**)&m);
 274   }
 275   static CppVtableInfo* _info;
 276 
 277   static int get_vtable_length(const char* name);
 278 
 279 public:
 280   // Allocate and initialize the C++ vtable, starting from top, but do not go past end.
 281   static intptr_t* allocate(const char* name, intptr_t* top, intptr_t* end);
 282 
 283   // Clone the vtable to ...
 284   static intptr_t* clone_vtable(const char* name, CppVtableInfo* info);
 285 
 286   static void zero_vtable_clone() {
 287     assert(DumpSharedSpaces, "dump-time only");
 288     _info->zero();
 289   }
 290 
 291   // Switch the vtable pointer to point to the cloned vtable.
 292   static void patch(Metadata* obj) {
 293     assert(DumpSharedSpaces, "dump-time only");
 294     *(void**)obj = (void*)(_info->cloned_vtable());
 295   }
 296 
 297   static bool is_valid_shared_object(const T* obj) {
 298     intptr_t* vptr = *(intptr_t**)obj;
 299     return vptr == _info->cloned_vtable();
 300   }
 301 };
 302 
 303 template <class T> CppVtableInfo* CppVtableCloner<T>::_info = NULL;
 304 
 305 template <class T>
 306 intptr_t* CppVtableCloner<T>::allocate(const char* name, intptr_t* top, intptr_t* end) {
 307   int n = get_vtable_length(name);
 308   _info = (CppVtableInfo*)top;
 309   intptr_t* next = _info->next(n);
 310 
 311   if (next > end) {
 312     report_out_of_shared_space(SharedMiscData);
 313   }
 314   _info->set_vtable_size(n);
 315 
 316   intptr_t* p = clone_vtable(name, _info);
 317   assert(p == next, "must be");
 318 
 319   return p;
 320 }
 321 
 322 template <class T>
 323 intptr_t* CppVtableCloner<T>::clone_vtable(const char* name, CppVtableInfo* info) {
 324   if (!DumpSharedSpaces) {
 325     assert(_info == 0, "_info is initialized only at dump time");
 326     _info = info; // Remember it -- it will be used by MetaspaceShared::is_valid_shared_method()
 327   }
 328   T tmp; // Allocate temporary dummy metadata object to get to the original vtable.
 329   int n = info->vtable_size();
 330   intptr_t* srcvtable = vtable_of(tmp);
 331   intptr_t* dstvtable = info->cloned_vtable();
 332 
 333   // We already checked (and, if necessary, adjusted n) when the vtables were allocated, so we are
 334   // safe to do memcpy.
 335   log_debug(cds, vtables)("Copying %3d vtable entries for %s", n, name);
 336   memcpy(dstvtable, srcvtable, sizeof(intptr_t) * n);
 337   return dstvtable + n;
 338 }
 339 
 340 // To determine the size of the vtable for each type, we use the following
 341 // trick by declaring 2 subclasses:
 342 //
 343 //   class CppVtableTesterA: public InstanceKlass {virtual int   last_virtual_method() {return 1;}    };
 344 //   class CppVtableTesterB: public InstanceKlass {virtual void* last_virtual_method() {return NULL}; };
 345 //
 346 // CppVtableTesterA and CppVtableTesterB's vtables have the following properties:
 347 // - Their size (N+1) is exactly one more than the size of InstanceKlass's vtable (N)
 348 // - The first N entries have are exactly the same as in InstanceKlass's vtable.
 349 // - Their last entry is different.
 350 //
 351 // So to determine the value of N, we just walk CppVtableTesterA and CppVtableTesterB's tables
 352 // and find the first entry that's different.
 353 //
 354 // This works on all C++ compilers supported by Oracle, but you may need to tweak it for more
 355 // esoteric compilers.
 356 
 357 template <class T> class CppVtableTesterB: public T {
 358 public:
 359   virtual int last_virtual_method() {return 1;}
 360 };
 361 
 362 template <class T> class CppVtableTesterA : public T {
 363 public:
 364   virtual void* last_virtual_method() {
 365     // Make this different than CppVtableTesterB::last_virtual_method so the C++
 366     // compiler/linker won't alias the two functions.
 367     return NULL;
 368   }
 369 };
 370 
 371 template <class T>
 372 int CppVtableCloner<T>::get_vtable_length(const char* name) {
 373   CppVtableTesterA<T> a;
 374   CppVtableTesterB<T> b;
 375 
 376   intptr_t* avtable = vtable_of(a);
 377   intptr_t* bvtable = vtable_of(b);
 378 
 379   // Start at slot 1, because slot 0 may be RTTI (on Solaris/Sparc)
 380   int vtable_len = 1;
 381   for (; ; vtable_len++) {
 382     if (avtable[vtable_len] != bvtable[vtable_len]) {
 383       break;
 384     }
 385   }
 386   log_debug(cds, vtables)("Found   %3d vtable entries for %s", vtable_len, name);
 387 
 388   return vtable_len;
 389 }
 390 
 391 #define ALLOC_CPP_VTABLE_CLONE(c) \
 392   top = CppVtableCloner<c>::allocate(#c, top, end);
 393 
 394 #define CLONE_CPP_VTABLE(c) \
 395   p = CppVtableCloner<c>::clone_vtable(#c, (CppVtableInfo*)p);
 396 
 397 #define ZERO_CPP_VTABLE(c) \
 398  CppVtableCloner<c>::zero_vtable_clone();
 399 
 400 // This can be called at both dump time and run time.
 401 intptr_t* MetaspaceShared::clone_cpp_vtables(intptr_t* p) {
 402   assert(DumpSharedSpaces || UseSharedSpaces, "sanity");
 403   CPP_VTABLE_PATCH_TYPES_DO(CLONE_CPP_VTABLE);
 404   return p;
 405 }
 406 
 407 void MetaspaceShared::zero_cpp_vtable_clones_for_writing() {
 408   assert(DumpSharedSpaces, "dump-time only");
 409   CPP_VTABLE_PATCH_TYPES_DO(ZERO_CPP_VTABLE);
 410 }
 411 
 412 // Allocate and initialize the C++ vtables, starting from top, but do not go past end.
 413 intptr_t* MetaspaceShared::allocate_cpp_vtable_clones(intptr_t* top, intptr_t* end) {
 414   assert(DumpSharedSpaces, "dump-time only");
 415   // Layout (each slot is a intptr_t):
 416   //   [number of slots in the first vtable = n1]
 417   //   [ <n1> slots for the first vtable]
 418   //   [number of slots in the first second = n2]
 419   //   [ <n2> slots for the second vtable]
 420   //   ...
 421   // The order of the vtables is the same as the CPP_VTAB_PATCH_TYPES_DO macro.
 422   CPP_VTABLE_PATCH_TYPES_DO(ALLOC_CPP_VTABLE_CLONE);
 423   return top;
 424 }
 425 
 426 // Switch the vtable pointer to point to the cloned vtable. We assume the
 427 // vtable pointer is in first slot in object.
 428 void MetaspaceShared::patch_cpp_vtable_pointers() {
 429   int n = _global_klass_objects->length();
 430   for (int i = 0; i < n; i++) {
 431     Klass* obj = _global_klass_objects->at(i);
 432     if (obj->is_instance_klass()) {
 433       InstanceKlass* ik = InstanceKlass::cast(obj);
 434       if (ik->is_class_loader_instance_klass()) {
 435         CppVtableCloner<InstanceClassLoaderKlass>::patch(ik);
 436       } else if (ik->is_reference_instance_klass()) {
 437         CppVtableCloner<InstanceRefKlass>::patch(ik);
 438       } else if (ik->is_mirror_instance_klass()) {
 439         CppVtableCloner<InstanceMirrorKlass>::patch(ik);
 440       } else {
 441         CppVtableCloner<InstanceKlass>::patch(ik);
 442       }
 443       ConstantPool* cp = ik->constants();
 444       CppVtableCloner<ConstantPool>::patch(cp);
 445       for (int j = 0; j < ik->methods()->length(); j++) {
 446         Method* m = ik->methods()->at(j);
 447         CppVtableCloner<Method>::patch(m);
 448         assert(CppVtableCloner<Method>::is_valid_shared_object(m), "must be");
 449       }
 450     } else if (obj->is_objArray_klass()) {
 451       CppVtableCloner<ObjArrayKlass>::patch(obj);
 452     } else {
 453       assert(obj->is_typeArray_klass(), "sanity");
 454       CppVtableCloner<TypeArrayKlass>::patch(obj);
 455     }
 456   }
 457 }
 458 
 459 bool MetaspaceShared::is_valid_shared_method(const Method* m) {
 460   assert(is_in_shared_space(m), "must be");
 461   return CppVtableCloner<Method>::is_valid_shared_object(m);
 462 }
 463 
 464 // Closure for serializing initialization data out to a data area to be
 465 // written to the shared file.
 466 
 467 class WriteClosure : public SerializeClosure {
 468 private:
 469   intptr_t* top;
 470   char* end;
 471 
 472   inline void check_space() {
 473     if ((char*)top + sizeof(intptr_t) > end) {
 474       report_out_of_shared_space(SharedMiscData);
 475     }
 476   }
 477 
 478 public:
 479   WriteClosure(char* md_top, char* md_end) {
 480     top = (intptr_t*)md_top;
 481     end = md_end;
 482   }
 483 
 484   char* get_top() { return (char*)top; }
 485 
 486   void do_ptr(void** p) {
 487     check_space();
 488     *top = (intptr_t)*p;
 489     ++top;
 490   }
 491 
 492   void do_u4(u4* p) {
 493     void* ptr = (void*)(uintx(*p));
 494     do_ptr(&ptr);
 495   }
 496 
 497   void do_tag(int tag) {
 498     check_space();
 499     *top = (intptr_t)tag;
 500     ++top;
 501   }
 502 
 503   void do_region(u_char* start, size_t size) {
 504     if ((char*)top + size > end) {
 505       report_out_of_shared_space(SharedMiscData);
 506     }
 507     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
 508     assert(size % sizeof(intptr_t) == 0, "bad size");
 509     do_tag((int)size);
 510     while (size > 0) {
 511       *top = *(intptr_t*)start;
 512       ++top;
 513       start += sizeof(intptr_t);
 514       size -= sizeof(intptr_t);
 515     }
 516   }
 517 
 518   bool reading() const { return false; }
 519 };
 520 
 521 // This is for dumping detailed statistics for the allocations
 522 // in the shared spaces.
 523 class DumpAllocClosure : public Metaspace::AllocRecordClosure {
 524 public:
 525 
 526   // Here's poor man's enum inheritance
 527 #define SHAREDSPACE_OBJ_TYPES_DO(f) \
 528   METASPACE_OBJ_TYPES_DO(f) \
 529   f(SymbolHashentry) \
 530   f(SymbolBucket) \
 531   f(StringHashentry) \
 532   f(StringBucket) \
 533   f(Other)
 534 
 535 #define SHAREDSPACE_OBJ_TYPE_DECLARE(name) name ## Type,
 536 #define SHAREDSPACE_OBJ_TYPE_NAME_CASE(name) case name ## Type: return #name;
 537 
 538   enum Type {
 539     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
 540     SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_DECLARE)
 541     _number_of_types
 542   };
 543 
 544   static const char * type_name(Type type) {
 545     switch(type) {
 546     SHAREDSPACE_OBJ_TYPES_DO(SHAREDSPACE_OBJ_TYPE_NAME_CASE)
 547     default:
 548       ShouldNotReachHere();
 549       return NULL;
 550     }
 551   }
 552 
 553 public:
 554   enum {
 555     RO = 0,
 556     RW = 1
 557   };
 558 
 559   int _counts[2][_number_of_types];
 560   int _bytes [2][_number_of_types];
 561   int _which;
 562 
 563   DumpAllocClosure() {
 564     memset(_counts, 0, sizeof(_counts));
 565     memset(_bytes,  0, sizeof(_bytes));
 566   };
 567 
 568   void iterate_metaspace(Metaspace* space, int which) {
 569     assert(which == RO || which == RW, "sanity");
 570     _which = which;
 571     space->iterate(this);
 572   }
 573 
 574   virtual void doit(address ptr, MetaspaceObj::Type type, int byte_size) {
 575     assert(int(type) >= 0 && type < MetaspaceObj::_number_of_types, "sanity");
 576     _counts[_which][type] ++;
 577     _bytes [_which][type] += byte_size;
 578   }
 579 
 580   void dump_stats(int ro_all, int rw_all, int md_all, int mc_all);
 581 };
 582 
 583 void DumpAllocClosure::dump_stats(int ro_all, int rw_all, int md_all, int mc_all) {
 584   rw_all += (md_all + mc_all); // md and mc are all mapped Read/Write
 585   int other_bytes = md_all + mc_all;
 586 
 587   // Calculate size of data that was not allocated by Metaspace::allocate()
 588   MetaspaceSharedStats *stats = MetaspaceShared::stats();
 589 
 590   // symbols
 591   _counts[RO][SymbolHashentryType] = stats->symbol.hashentry_count;
 592   _bytes [RO][SymbolHashentryType] = stats->symbol.hashentry_bytes;
 593   _bytes [RO][TypeArrayU4Type]    -= stats->symbol.hashentry_bytes;
 594 
 595   _counts[RO][SymbolBucketType] = stats->symbol.bucket_count;
 596   _bytes [RO][SymbolBucketType] = stats->symbol.bucket_bytes;
 597   _bytes [RO][TypeArrayU4Type] -= stats->symbol.bucket_bytes;
 598 
 599   // strings
 600   _counts[RO][StringHashentryType] = stats->string.hashentry_count;
 601   _bytes [RO][StringHashentryType] = stats->string.hashentry_bytes;
 602   _bytes [RO][TypeArrayU4Type]    -= stats->string.hashentry_bytes;
 603 
 604   _counts[RO][StringBucketType] = stats->string.bucket_count;
 605   _bytes [RO][StringBucketType] = stats->string.bucket_bytes;
 606   _bytes [RO][TypeArrayU4Type] -= stats->string.bucket_bytes;
 607 
 608   // TODO: count things like dictionary, vtable, etc
 609   _bytes[RW][OtherType] =  other_bytes;
 610 
 611   // prevent divide-by-zero
 612   if (ro_all < 1) {
 613     ro_all = 1;
 614   }
 615   if (rw_all < 1) {
 616     rw_all = 1;
 617   }
 618 
 619   int all_ro_count = 0;
 620   int all_ro_bytes = 0;
 621   int all_rw_count = 0;
 622   int all_rw_bytes = 0;
 623 
 624 // To make fmt_stats be a syntactic constant (for format warnings), use #define.
 625 #define fmt_stats "%-20s: %8d %10d %5.1f | %8d %10d %5.1f | %8d %10d %5.1f"
 626   const char *sep = "--------------------+---------------------------+---------------------------+--------------------------";
 627   const char *hdr = "                        ro_cnt   ro_bytes     % |   rw_cnt   rw_bytes     % |  all_cnt  all_bytes     %";
 628 
 629   ResourceMark rm;
 630   LogMessage(cds) msg;
 631   stringStream info_stream;
 632 
 633   info_stream.print_cr("Detailed metadata info (rw includes md and mc):");
 634   info_stream.print_cr("%s", hdr);
 635   info_stream.print_cr("%s", sep);
 636   for (int type = 0; type < int(_number_of_types); type ++) {
 637     const char *name = type_name((Type)type);
 638     int ro_count = _counts[RO][type];
 639     int ro_bytes = _bytes [RO][type];
 640     int rw_count = _counts[RW][type];
 641     int rw_bytes = _bytes [RW][type];
 642     int count = ro_count + rw_count;
 643     int bytes = ro_bytes + rw_bytes;
 644 
 645     double ro_perc = 100.0 * double(ro_bytes) / double(ro_all);
 646     double rw_perc = 100.0 * double(rw_bytes) / double(rw_all);
 647     double perc    = 100.0 * double(bytes)    / double(ro_all + rw_all);
 648 
 649     info_stream.print_cr(fmt_stats, name,
 650                          ro_count, ro_bytes, ro_perc,
 651                          rw_count, rw_bytes, rw_perc,
 652                          count, bytes, perc);
 653 
 654     all_ro_count += ro_count;
 655     all_ro_bytes += ro_bytes;
 656     all_rw_count += rw_count;
 657     all_rw_bytes += rw_bytes;
 658   }
 659 
 660   int all_count = all_ro_count + all_rw_count;
 661   int all_bytes = all_ro_bytes + all_rw_bytes;
 662 
 663   double all_ro_perc = 100.0 * double(all_ro_bytes) / double(ro_all);
 664   double all_rw_perc = 100.0 * double(all_rw_bytes) / double(rw_all);
 665   double all_perc    = 100.0 * double(all_bytes)    / double(ro_all + rw_all);
 666 
 667   info_stream.print_cr("%s", sep);
 668   info_stream.print_cr(fmt_stats, "Total",
 669                        all_ro_count, all_ro_bytes, all_ro_perc,
 670                        all_rw_count, all_rw_bytes, all_rw_perc,
 671                        all_count, all_bytes, all_perc);
 672 
 673   assert(all_ro_bytes == ro_all, "everything should have been counted");
 674   assert(all_rw_bytes == rw_all, "everything should have been counted");
 675 
 676   msg.info("%s", info_stream.as_string());
 677 #undef fmt_stats
 678 }
 679 
 680 // Populate the shared space.
 681 
 682 class VM_PopulateDumpSharedSpace: public VM_Operation {
 683 private:
 684   ClassLoaderData* _loader_data;
 685   GrowableArray<Klass*> *_class_promote_order;
 686   VirtualSpace _md_vs;
 687   VirtualSpace _mc_vs;
 688   VirtualSpace _od_vs;
 689   GrowableArray<MemRegion> *_string_regions;
 690 
 691 public:
 692   VM_PopulateDumpSharedSpace(ClassLoaderData* loader_data,
 693                              GrowableArray<Klass*> *class_promote_order) :
 694     _loader_data(loader_data) {
 695     _class_promote_order = class_promote_order;
 696   }
 697 
 698   VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; }
 699   void doit();   // outline because gdb sucks
 700 
 701 private:
 702   void handle_misc_data_space_failure(bool success) {
 703     if (!success) {
 704       report_out_of_shared_space(SharedMiscData);
 705     }
 706   }
 707 }; // class VM_PopulateDumpSharedSpace
 708 
 709 void VM_PopulateDumpSharedSpace::doit() {
 710   Thread* THREAD = VMThread::vm_thread();
 711   NOT_PRODUCT(SystemDictionary::verify();)
 712   // The following guarantee is meant to ensure that no loader constraints
 713   // exist yet, since the constraints table is not shared.  This becomes
 714   // more important now that we don't re-initialize vtables/itables for
 715   // shared classes at runtime, where constraints were previously created.
 716   guarantee(SystemDictionary::constraints()->number_of_entries() == 0,
 717             "loader constraints are not saved");
 718   guarantee(SystemDictionary::placeholders()->number_of_entries() == 0,
 719           "placeholders are not saved");
 720   // Revisit and implement this if we prelink method handle call sites:
 721   guarantee(SystemDictionary::invoke_method_table() == NULL ||
 722             SystemDictionary::invoke_method_table()->number_of_entries() == 0,
 723             "invoke method table is not saved");
 724 
 725   // At this point, many classes have been loaded.
 726   // Gather systemDictionary classes in a global array and do everything to
 727   // that so we don't have to walk the SystemDictionary again.
 728   _global_klass_objects = new GrowableArray<Klass*>(1000);
 729   CollectClassesClosure collect_classes;
 730   ClassLoaderDataGraph::loaded_classes_do(&collect_classes);
 731 
 732   tty->print_cr("Number of classes %d", _global_klass_objects->length());
 733   {
 734     int num_type_array = 0, num_obj_array = 0, num_inst = 0;
 735     for (int i = 0; i < _global_klass_objects->length(); i++) {
 736       Klass* k = _global_klass_objects->at(i);
 737       if (k->is_instance_klass()) {
 738         num_inst ++;
 739       } else if (k->is_objArray_klass()) {
 740         num_obj_array ++;
 741       } else {
 742         assert(k->is_typeArray_klass(), "sanity");
 743         num_type_array ++;
 744       }
 745     }
 746     tty->print_cr("    instance classes   = %5d", num_inst);
 747     tty->print_cr("    obj array classes  = %5d", num_obj_array);
 748     tty->print_cr("    type array classes = %5d", num_type_array);
 749   }
 750 
 751 
 752   // Ensure the ConstMethods won't be modified at run-time
 753   tty->print("Updating ConstMethods ... ");
 754   rewrite_nofast_bytecodes_and_calculate_fingerprints();
 755   tty->print_cr("done. ");
 756 
 757   // Remove all references outside the metadata
 758   tty->print("Removing unshareable information ... ");
 759   remove_unshareable_in_classes();
 760   tty->print_cr("done. ");
 761 
 762   // Set up the misc data, misc code and optional data segments.
 763   _md_vs = *MetaspaceShared::misc_data_region()->virtual_space();
 764   _mc_vs = *MetaspaceShared::misc_code_region()->virtual_space();
 765   _od_vs = *MetaspaceShared::optional_data_region()->virtual_space();
 766   char* md_low = _md_vs.low();
 767   char* md_top = MetaspaceShared::misc_data_region()->alloc_top();
 768   char* md_end = _md_vs.high();
 769   char* mc_low = _mc_vs.low();
 770   char* mc_top = MetaspaceShared::misc_code_region()->alloc_top();
 771   char* mc_end = _mc_vs.high();
 772   char* od_low = _od_vs.low();
 773   char* od_top = MetaspaceShared::optional_data_region()->alloc_top();
 774   char* od_end = _od_vs.high();
 775 
 776   char* vtbl_list = md_top;
 777   md_top = (char*)MetaspaceShared::allocate_cpp_vtable_clones((intptr_t*)md_top, (intptr_t*)md_end);
 778 
 779   // We don't use MC section anymore. We will remove it in a future RFE. For now, put one
 780   // byte inside so the region writing/mapping code works.
 781   mc_top ++;
 782 
 783   // Reorder the system dictionary.  (Moving the symbols affects
 784   // how the hash table indices are calculated.)
 785   // Not doing this either.
 786 
 787   SystemDictionary::reorder_dictionary();
 788   NOT_PRODUCT(SystemDictionary::verify();)
 789   SystemDictionary::copy_buckets(&md_top, md_end);
 790 
 791   SystemDictionary::copy_table(&md_top, md_end);
 792 
 793   // Write the other data to the output array.
 794   // SymbolTable, StringTable and extra information for system dictionary
 795   NOT_PRODUCT(SymbolTable::verify());
 796   NOT_PRODUCT(StringTable::verify());
 797   size_t ss_bytes = 0;
 798   char* ss_low;
 799   // The string space has maximum two regions. See FileMapInfo::write_string_regions() for details.
 800   _string_regions = new GrowableArray<MemRegion>(2);
 801 
 802   WriteClosure wc(md_top, md_end);
 803   MetaspaceShared::serialize(&wc, _string_regions, &ss_bytes);
 804   md_top = wc.get_top();
 805   ss_low = _string_regions->is_empty() ? NULL : (char*)_string_regions->first().start();
 806 
 807   // Print shared spaces all the time
 808   Metaspace* ro_space = _loader_data->ro_metaspace();
 809   Metaspace* rw_space = _loader_data->rw_metaspace();
 810 
 811   // Allocated size of each space (may not be all occupied)
 812   const size_t ro_alloced = ro_space->capacity_bytes_slow(Metaspace::NonClassType);
 813   const size_t rw_alloced = rw_space->capacity_bytes_slow(Metaspace::NonClassType);
 814   const size_t md_alloced = md_end-md_low;
 815   const size_t mc_alloced = mc_end-mc_low;
 816   const size_t od_alloced = od_end-od_low;
 817   const size_t total_alloced = ro_alloced + rw_alloced + md_alloced + mc_alloced
 818                              + ss_bytes + od_alloced;
 819 
 820   // Occupied size of each space.
 821   const size_t ro_bytes = ro_space->used_bytes_slow(Metaspace::NonClassType);
 822   const size_t rw_bytes = rw_space->used_bytes_slow(Metaspace::NonClassType);
 823   const size_t md_bytes = size_t(md_top - md_low);
 824   const size_t mc_bytes = size_t(mc_top - mc_low);
 825   const size_t od_bytes = size_t(od_top - od_low);
 826 
 827   // Percent of total size
 828   const size_t total_bytes = ro_bytes + rw_bytes + md_bytes + mc_bytes + ss_bytes + od_bytes;
 829   const double ro_t_perc = ro_bytes / double(total_bytes) * 100.0;
 830   const double rw_t_perc = rw_bytes / double(total_bytes) * 100.0;
 831   const double md_t_perc = md_bytes / double(total_bytes) * 100.0;
 832   const double mc_t_perc = mc_bytes / double(total_bytes) * 100.0;
 833   const double ss_t_perc = ss_bytes / double(total_bytes) * 100.0;
 834   const double od_t_perc = od_bytes / double(total_bytes) * 100.0;
 835 
 836   // Percent of fullness of each space
 837   const double ro_u_perc = ro_bytes / double(ro_alloced) * 100.0;
 838   const double rw_u_perc = rw_bytes / double(rw_alloced) * 100.0;
 839   const double md_u_perc = md_bytes / double(md_alloced) * 100.0;
 840   const double mc_u_perc = mc_bytes / double(mc_alloced) * 100.0;
 841   const double od_u_perc = od_bytes / double(od_alloced) * 100.0;
 842   const double total_u_perc = total_bytes / double(total_alloced) * 100.0;
 843 
 844 #define fmt_space "%s space: " SIZE_FORMAT_W(9) " [ %4.1f%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used] at " INTPTR_FORMAT
 845   tty->print_cr(fmt_space, "ro", ro_bytes, ro_t_perc, ro_alloced, ro_u_perc, p2i(ro_space->bottom()));
 846   tty->print_cr(fmt_space, "rw", rw_bytes, rw_t_perc, rw_alloced, rw_u_perc, p2i(rw_space->bottom()));
 847   tty->print_cr(fmt_space, "md", md_bytes, md_t_perc, md_alloced, md_u_perc, p2i(md_low));
 848   tty->print_cr(fmt_space, "mc", mc_bytes, mc_t_perc, mc_alloced, mc_u_perc, p2i(mc_low));
 849   tty->print_cr(fmt_space, "st", ss_bytes, ss_t_perc, ss_bytes,   100.0,     p2i(ss_low));
 850   tty->print_cr(fmt_space, "od", od_bytes, od_t_perc, od_alloced, od_u_perc, p2i(od_low));
 851   tty->print_cr("total   : " SIZE_FORMAT_W(9) " [100.0%% of total] out of " SIZE_FORMAT_W(9) " bytes [%5.1f%% used]",
 852                  total_bytes, total_alloced, total_u_perc);
 853 
 854   // During patching, some virtual methods may be called, so at this point
 855   // the vtables must contain valid methods (as filled in by CppVtableCloner::allocate).
 856   MetaspaceShared::patch_cpp_vtable_pointers();
 857 
 858   // The vtable clones contain addresses of the current process.
 859   // We don't want to write these addresses into the archive.
 860   MetaspaceShared::zero_cpp_vtable_clones_for_writing();
 861 
 862   // Create and write the archive file that maps the shared spaces.
 863 
 864   FileMapInfo* mapinfo = new FileMapInfo();
 865   mapinfo->populate_header(MetaspaceShared::max_alignment());
 866   mapinfo->set_misc_data_patching_start(vtbl_list);
 867   mapinfo->set_cds_i2i_entry_code_buffers(MetaspaceShared::cds_i2i_entry_code_buffers());
 868   mapinfo->set_cds_i2i_entry_code_buffers_size(MetaspaceShared::cds_i2i_entry_code_buffers_size());
 869 
 870   for (int pass=1; pass<=2; pass++) {
 871     if (pass == 1) {
 872       // The first pass doesn't actually write the data to disk. All it
 873       // does is to update the fields in the mapinfo->_header.
 874     } else {
 875       // After the first pass, the contents of mapinfo->_header are finalized,
 876       // so we can compute the header's CRC, and write the contents of the header
 877       // and the regions into disk.
 878       mapinfo->open_for_write();
 879       mapinfo->set_header_crc(mapinfo->compute_header_crc());
 880     }
 881     mapinfo->write_header();
 882     mapinfo->write_space(MetaspaceShared::ro, _loader_data->ro_metaspace(), true);
 883     mapinfo->write_space(MetaspaceShared::rw, _loader_data->rw_metaspace(), false);
 884     mapinfo->write_region(MetaspaceShared::md, _md_vs.low(),
 885                           pointer_delta(md_top, _md_vs.low(), sizeof(char)),
 886                           SharedMiscDataSize,
 887                           false, true);
 888     mapinfo->write_region(MetaspaceShared::mc, _mc_vs.low(),
 889                           pointer_delta(mc_top, _mc_vs.low(), sizeof(char)),
 890                           SharedMiscCodeSize,
 891                           true, true);
 892     mapinfo->write_string_regions(_string_regions);
 893     mapinfo->write_region(MetaspaceShared::od, _od_vs.low(),
 894                           pointer_delta(od_top, _od_vs.low(), sizeof(char)),
 895                           pointer_delta(od_end, _od_vs.low(), sizeof(char)),
 896                           true, false);
 897   }
 898 
 899   mapinfo->close();
 900 
 901   // Restore the vtable in case we invoke any virtual methods.
 902   MetaspaceShared::clone_cpp_vtables((intptr_t*)vtbl_list);
 903 
 904   if (log_is_enabled(Info, cds)) {
 905     DumpAllocClosure dac;
 906     dac.iterate_metaspace(_loader_data->ro_metaspace(), DumpAllocClosure::RO);
 907     dac.iterate_metaspace(_loader_data->rw_metaspace(), DumpAllocClosure::RW);
 908 
 909     dac.dump_stats(int(ro_bytes), int(rw_bytes), int(md_bytes), int(mc_bytes));
 910   }
 911 #undef fmt_space
 912 }
 913 
 914 class LinkSharedClassesClosure : public KlassClosure {
 915   Thread* THREAD;
 916   bool    _made_progress;
 917  public:
 918   LinkSharedClassesClosure(Thread* thread) : THREAD(thread), _made_progress(false) {}
 919 
 920   void reset()               { _made_progress = false; }
 921   bool made_progress() const { return _made_progress; }
 922 
 923   void do_klass(Klass* k) {
 924     if (k->is_instance_klass()) {
 925       InstanceKlass* ik = InstanceKlass::cast(k);
 926       // Link the class to cause the bytecodes to be rewritten and the
 927       // cpcache to be created. Class verification is done according
 928       // to -Xverify setting.
 929       _made_progress |= MetaspaceShared::try_link_class(ik, THREAD);
 930       guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
 931     }
 932   }
 933 };
 934 
 935 class CheckSharedClassesClosure : public KlassClosure {
 936   bool    _made_progress;
 937  public:
 938   CheckSharedClassesClosure() : _made_progress(false) {}
 939 
 940   void reset()               { _made_progress = false; }
 941   bool made_progress() const { return _made_progress; }
 942   void do_klass(Klass* k) {
 943     if (k->is_instance_klass() && InstanceKlass::cast(k)->check_sharing_error_state()) {
 944       _made_progress = true;
 945     }
 946   }
 947 };
 948 
 949 void MetaspaceShared::check_shared_class_loader_type(Klass* k) {
 950   if (k->is_instance_klass()) {
 951     InstanceKlass* ik = InstanceKlass::cast(k);
 952     u2 loader_type = ik->loader_type();
 953     ResourceMark rm;
 954     guarantee(loader_type != 0,
 955               "Class loader type is not set for this class %s", ik->name()->as_C_string());
 956   }
 957 }
 958 
 959 void MetaspaceShared::link_and_cleanup_shared_classes(TRAPS) {
 960   // We need to iterate because verification may cause additional classes
 961   // to be loaded.
 962   LinkSharedClassesClosure link_closure(THREAD);
 963   do {
 964     link_closure.reset();
 965     ClassLoaderDataGraph::loaded_classes_do(&link_closure);
 966     guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
 967   } while (link_closure.made_progress());
 968 
 969   if (_has_error_classes) {
 970     // Mark all classes whose super class or interfaces failed verification.
 971     CheckSharedClassesClosure check_closure;
 972     do {
 973       // Not completely sure if we need to do this iteratively. Anyway,
 974       // we should come here only if there are unverifiable classes, which
 975       // shouldn't happen in normal cases. So better safe than sorry.
 976       check_closure.reset();
 977       ClassLoaderDataGraph::loaded_classes_do(&check_closure);
 978     } while (check_closure.made_progress());
 979 
 980     if (IgnoreUnverifiableClassesDuringDump) {
 981       // This is useful when running JCK or SQE tests. You should not
 982       // enable this when running real apps.
 983       SystemDictionary::remove_classes_in_error_state();
 984     } else {
 985       tty->print_cr("Please remove the unverifiable classes from your class list and try again");
 986       exit(1);
 987     }
 988   }
 989 
 990   // Copy the verification constraints from C_HEAP-alloced GrowableArrays to RO-alloced
 991   // Arrays
 992   SystemDictionaryShared::finalize_verification_constraints();
 993 }
 994 
 995 void MetaspaceShared::prepare_for_dumping() {
 996   Arguments::check_unsupported_dumping_properties();
 997   ClassLoader::initialize_shared_path();
 998   FileMapInfo::allocate_classpath_entry_table();
 999 }
1000 
1001 // Preload classes from a list, populate the shared spaces and dump to a
1002 // file.
1003 void MetaspaceShared::preload_and_dump(TRAPS) {
1004   { TraceTime timer("Dump Shared Spaces", TRACETIME_LOG(Info, startuptime));
1005     ResourceMark rm;
1006     char class_list_path_str[JVM_MAXPATHLEN];
1007 
1008     tty->print_cr("Allocated shared space: " SIZE_FORMAT " bytes at " PTR_FORMAT,
1009                   MetaspaceShared::shared_rs()->size(),
1010                   p2i(MetaspaceShared::shared_rs()->base()));
1011 
1012     // Preload classes to be shared.
1013     // Should use some os:: method rather than fopen() here. aB.
1014     const char* class_list_path;
1015     if (SharedClassListFile == NULL) {
1016       // Construct the path to the class list (in jre/lib)
1017       // Walk up two directories from the location of the VM and
1018       // optionally tack on "lib" (depending on platform)
1019       os::jvm_path(class_list_path_str, sizeof(class_list_path_str));
1020       for (int i = 0; i < 3; i++) {
1021         char *end = strrchr(class_list_path_str, *os::file_separator());
1022         if (end != NULL) *end = '\0';
1023       }
1024       int class_list_path_len = (int)strlen(class_list_path_str);
1025       if (class_list_path_len >= 3) {
1026         if (strcmp(class_list_path_str + class_list_path_len - 3, "lib") != 0) {
1027           if (class_list_path_len < JVM_MAXPATHLEN - 4) {
1028             jio_snprintf(class_list_path_str + class_list_path_len,
1029                          sizeof(class_list_path_str) - class_list_path_len,
1030                          "%slib", os::file_separator());
1031             class_list_path_len += 4;
1032           }
1033         }
1034       }
1035       if (class_list_path_len < JVM_MAXPATHLEN - 10) {
1036         jio_snprintf(class_list_path_str + class_list_path_len,
1037                      sizeof(class_list_path_str) - class_list_path_len,
1038                      "%sclasslist", os::file_separator());
1039       }
1040       class_list_path = class_list_path_str;
1041     } else {
1042       class_list_path = SharedClassListFile;
1043     }
1044 
1045     int class_count = 0;
1046     GrowableArray<Klass*>* class_promote_order = new GrowableArray<Klass*>();
1047 
1048     // sun.io.Converters
1049     static const char obj_array_sig[] = "[[Ljava/lang/Object;";
1050     SymbolTable::new_permanent_symbol(obj_array_sig, THREAD);
1051 
1052     // java.util.HashMap
1053     static const char map_entry_array_sig[] = "[Ljava/util/Map$Entry;";
1054     SymbolTable::new_permanent_symbol(map_entry_array_sig, THREAD);
1055 
1056     // Need to allocate the op here:
1057     // op.misc_data_space_alloc() will be called during preload_and_dump().
1058     ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1059     VM_PopulateDumpSharedSpace op(loader_data, class_promote_order);
1060 
1061     tty->print_cr("Loading classes to share ...");
1062     _has_error_classes = false;
1063     class_count += preload_and_dump(class_list_path, class_promote_order,
1064                                     THREAD);
1065     if (ExtraSharedClassListFile) {
1066       class_count += preload_and_dump(ExtraSharedClassListFile, class_promote_order,
1067                                       THREAD);
1068     }
1069     tty->print_cr("Loading classes to share: done.");
1070 
1071     log_info(cds)("Shared spaces: preloaded %d classes", class_count);
1072 
1073     // Rewrite and link classes
1074     tty->print_cr("Rewriting and linking classes ...");
1075 
1076     // Link any classes which got missed. This would happen if we have loaded classes that
1077     // were not explicitly specified in the classlist. E.g., if an interface implemented by class K
1078     // fails verification, all other interfaces that were not specified in the classlist but
1079     // are implemented by K are not verified.
1080     link_and_cleanup_shared_classes(CATCH);
1081     tty->print_cr("Rewriting and linking classes: done");
1082 
1083     VMThread::execute(&op);
1084   }
1085 
1086   if (PrintSystemDictionaryAtExit) {
1087     SystemDictionary::print();
1088   }
1089 
1090   // Since various initialization steps have been undone by this process,
1091   // it is not reasonable to continue running a java process.
1092   exit(0);
1093 }
1094 
1095 
1096 int MetaspaceShared::preload_and_dump(const char* class_list_path,
1097                                       GrowableArray<Klass*>* class_promote_order,
1098                                       TRAPS) {
1099   ClassListParser parser(class_list_path);
1100   int class_count = 0;
1101 
1102     while (parser.parse_one_line()) {
1103       Klass* klass = ClassLoaderExt::load_one_class(&parser, THREAD);
1104 
1105       CLEAR_PENDING_EXCEPTION;
1106       if (klass != NULL) {
1107         if (log_is_enabled(Trace, cds)) {
1108           ResourceMark rm;
1109           log_trace(cds)("Shared spaces preloaded: %s", klass->external_name());
1110         }
1111 
1112         InstanceKlass* ik = InstanceKlass::cast(klass);
1113 
1114         // Should be class load order as per -Xlog:class+preorder
1115         class_promote_order->append(ik);
1116 
1117         // Link the class to cause the bytecodes to be rewritten and the
1118         // cpcache to be created. The linking is done as soon as classes
1119         // are loaded in order that the related data structures (klass and
1120         // cpCache) are located together.
1121         try_link_class(ik, THREAD);
1122         guarantee(!HAS_PENDING_EXCEPTION, "exception in link_class");
1123 
1124         class_count++;
1125       }
1126     }
1127 
1128   return class_count;
1129 }
1130 
1131 // Returns true if the class's status has changed
1132 bool MetaspaceShared::try_link_class(InstanceKlass* ik, TRAPS) {
1133   assert(DumpSharedSpaces, "should only be called during dumping");
1134   if (ik->init_state() < InstanceKlass::linked) {
1135     bool saved = BytecodeVerificationLocal;
1136     if (!(ik->is_shared_boot_class())) {
1137       // The verification decision is based on BytecodeVerificationRemote
1138       // for non-system classes. Since we are using the NULL classloader
1139       // to load non-system classes during dumping, we need to temporarily
1140       // change BytecodeVerificationLocal to be the same as
1141       // BytecodeVerificationRemote. Note this can cause the parent system
1142       // classes also being verified. The extra overhead is acceptable during
1143       // dumping.
1144       BytecodeVerificationLocal = BytecodeVerificationRemote;
1145     }
1146     ik->link_class(THREAD);
1147     if (HAS_PENDING_EXCEPTION) {
1148       ResourceMark rm;
1149       tty->print_cr("Preload Warning: Verification failed for %s",
1150                     ik->external_name());
1151       CLEAR_PENDING_EXCEPTION;
1152       ik->set_in_error_state();
1153       _has_error_classes = true;
1154     }
1155     BytecodeVerificationLocal = saved;
1156     return true;
1157   } else {
1158     return false;
1159   }
1160 }
1161 
1162 // Closure for serializing initialization data in from a data area
1163 // (ptr_array) read from the shared file.
1164 
1165 class ReadClosure : public SerializeClosure {
1166 private:
1167   intptr_t** _ptr_array;
1168 
1169   inline intptr_t nextPtr() {
1170     return *(*_ptr_array)++;
1171   }
1172 
1173 public:
1174   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
1175 
1176   void do_ptr(void** p) {
1177     assert(*p == NULL, "initializing previous initialized pointer.");
1178     intptr_t obj = nextPtr();
1179     assert((intptr_t)obj >= 0 || (intptr_t)obj < -100,
1180            "hit tag while initializing ptrs.");
1181     *p = (void*)obj;
1182   }
1183 
1184   void do_u4(u4* p) {
1185     intptr_t obj = nextPtr();
1186     *p = (u4)(uintx(obj));
1187   }
1188 
1189   void do_tag(int tag) {
1190     int old_tag;
1191     old_tag = (int)(intptr_t)nextPtr();
1192     // do_int(&old_tag);
1193     assert(tag == old_tag, "old tag doesn't match");
1194     FileMapInfo::assert_mark(tag == old_tag);
1195   }
1196 
1197   void do_region(u_char* start, size_t size) {
1198     assert((intptr_t)start % sizeof(intptr_t) == 0, "bad alignment");
1199     assert(size % sizeof(intptr_t) == 0, "bad size");
1200     do_tag((int)size);
1201     while (size > 0) {
1202       *(intptr_t*)start = nextPtr();
1203       start += sizeof(intptr_t);
1204       size -= sizeof(intptr_t);
1205     }
1206   }
1207 
1208   bool reading() const { return true; }
1209 };
1210 
1211 // Return true if given address is in the mapped shared space.
1212 bool MetaspaceShared::is_in_shared_space(const void* p) {
1213   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_space(p);
1214 }
1215 
1216 // Return true if given address is in the misc data region
1217 bool MetaspaceShared::is_in_shared_region(const void* p, int idx) {
1218   return UseSharedSpaces && FileMapInfo::current_info()->is_in_shared_region(p, idx);
1219 }
1220 
1221 bool MetaspaceShared::is_string_region(int idx) {
1222   return (idx >= MetaspaceShared::first_string &&
1223           idx < MetaspaceShared::first_string + MetaspaceShared::max_strings);
1224 }
1225 
1226 void MetaspaceShared::print_shared_spaces() {
1227   if (UseSharedSpaces) {
1228     FileMapInfo::current_info()->print_shared_spaces();
1229   }
1230 }
1231 
1232 
1233 // Map shared spaces at requested addresses and return if succeeded.
1234 bool MetaspaceShared::map_shared_spaces(FileMapInfo* mapinfo) {
1235   size_t image_alignment = mapinfo->alignment();
1236 
1237 #ifndef _WINDOWS
1238   // Map in the shared memory and then map the regions on top of it.
1239   // On Windows, don't map the memory here because it will cause the
1240   // mappings of the regions to fail.
1241   ReservedSpace shared_rs = mapinfo->reserve_shared_memory();
1242   if (!shared_rs.is_reserved()) return false;
1243 #endif
1244 
1245   assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
1246 
1247   char* _ro_base = NULL;
1248   char* _rw_base = NULL;
1249   char* _md_base = NULL;
1250   char* _mc_base = NULL;
1251   char* _od_base = NULL;
1252 
1253   // Map each shared region
1254   if ((_ro_base = mapinfo->map_region(ro)) != NULL &&
1255       mapinfo->verify_region_checksum(ro) &&
1256       (_rw_base = mapinfo->map_region(rw)) != NULL &&
1257       mapinfo->verify_region_checksum(rw) &&
1258       (_md_base = mapinfo->map_region(md)) != NULL &&
1259       mapinfo->verify_region_checksum(md) &&
1260       (_mc_base = mapinfo->map_region(mc)) != NULL &&
1261       mapinfo->verify_region_checksum(mc) &&
1262       (_od_base = mapinfo->map_region(od)) != NULL &&
1263       mapinfo->verify_region_checksum(od) &&
1264       (image_alignment == (size_t)max_alignment()) &&
1265       mapinfo->validate_classpath_entry_table()) {
1266     // Success (no need to do anything)
1267     return true;
1268   } else {
1269     // If there was a failure in mapping any of the spaces, unmap the ones
1270     // that succeeded
1271     if (_ro_base != NULL) mapinfo->unmap_region(ro);
1272     if (_rw_base != NULL) mapinfo->unmap_region(rw);
1273     if (_md_base != NULL) mapinfo->unmap_region(md);
1274     if (_mc_base != NULL) mapinfo->unmap_region(mc);
1275     if (_od_base != NULL) mapinfo->unmap_region(od);
1276 #ifndef _WINDOWS
1277     // Release the entire mapped region
1278     shared_rs.release();
1279 #endif
1280     // If -Xshare:on is specified, print out the error message and exit VM,
1281     // otherwise, set UseSharedSpaces to false and continue.
1282     if (RequireSharedSpaces || PrintSharedArchiveAndExit) {
1283       vm_exit_during_initialization("Unable to use shared archive.", "Failed map_region for using -Xshare:on.");
1284     } else {
1285       FLAG_SET_DEFAULT(UseSharedSpaces, false);
1286     }
1287     return false;
1288   }
1289 }
1290 
1291 // Read the miscellaneous data from the shared file, and
1292 // serialize it out to its various destinations.
1293 
1294 void MetaspaceShared::initialize_shared_spaces() {
1295   FileMapInfo *mapinfo = FileMapInfo::current_info();
1296   _cds_i2i_entry_code_buffers = mapinfo->cds_i2i_entry_code_buffers();
1297   _cds_i2i_entry_code_buffers_size = mapinfo->cds_i2i_entry_code_buffers_size();
1298   char* buffer = mapinfo->misc_data_patching_start();
1299 
1300   buffer = (char*)clone_cpp_vtables((intptr_t*)buffer);
1301 
1302   int sharedDictionaryLen = *(intptr_t*)buffer;
1303   buffer += sizeof(intptr_t);
1304   int number_of_entries = *(intptr_t*)buffer;
1305   buffer += sizeof(intptr_t);
1306   SystemDictionary::set_shared_dictionary((HashtableBucket<mtClass>*)buffer,
1307                                           sharedDictionaryLen,
1308                                           number_of_entries);
1309   buffer += sharedDictionaryLen;
1310 
1311   // The following data in the shared misc data region are the linked
1312   // list elements (HashtableEntry objects) for the shared dictionary
1313   // table.
1314 
1315   int len = *(intptr_t*)buffer;     // skip over shared dictionary entries
1316   buffer += sizeof(intptr_t);
1317   buffer += len;
1318 
1319   // Verify various attributes of the archive, plus initialize the
1320   // shared string/symbol tables
1321   intptr_t* array = (intptr_t*)buffer;
1322   ReadClosure rc(&array);
1323   serialize(&rc, NULL, NULL);
1324 
1325   // Initialize the run-time symbol table.
1326   SymbolTable::create_table();
1327 
1328   // Close the mapinfo file
1329   mapinfo->close();
1330 
1331   if (PrintSharedArchiveAndExit) {
1332     if (PrintSharedDictionary) {
1333       tty->print_cr("\nShared classes:\n");
1334       SystemDictionary::print_shared(false);
1335     }
1336     if (_archive_loading_failed) {
1337       tty->print_cr("archive is invalid");
1338       vm_exit(1);
1339     } else {
1340       tty->print_cr("archive is valid");
1341       vm_exit(0);
1342     }
1343   }
1344 }
1345 
1346 void MetaspaceShared::fixup_shared_string_regions() {
1347   FileMapInfo *mapinfo = FileMapInfo::current_info();
1348   mapinfo->fixup_string_regions();
1349 }
1350 
1351 // JVM/TI RedefineClasses() support:
1352 bool MetaspaceShared::remap_shared_readonly_as_readwrite() {
1353   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1354 
1355   if (UseSharedSpaces) {
1356     // remap the shared readonly space to shared readwrite, private
1357     FileMapInfo* mapinfo = FileMapInfo::current_info();
1358     if (!mapinfo->remap_shared_readonly_as_readwrite()) {
1359       return false;
1360     }
1361     _remapped_readwrite = true;
1362   }
1363   return true;
1364 }
1365 
1366 int MetaspaceShared::count_class(const char* classlist_file) {
1367   if (classlist_file == NULL) {
1368     return 0;
1369   }
1370   char class_name[256];
1371   int class_count = 0;
1372   FILE* file = fopen(classlist_file, "r");
1373   if (file != NULL) {
1374     while ((fgets(class_name, sizeof class_name, file)) != NULL) {
1375       if (*class_name == '#') { // comment
1376         continue;
1377       }
1378       class_count++;
1379     }
1380     fclose(file);
1381   } else {
1382     char errmsg[JVM_MAXPATHLEN];
1383     os::lasterror(errmsg, JVM_MAXPATHLEN);
1384     tty->print_cr("Loading classlist failed: %s", errmsg);
1385     exit(1);
1386   }
1387 
1388   return class_count;
1389 }
1390 
1391 // the sizes are good for typical large applications that have a lot of shared
1392 // classes
1393 void MetaspaceShared::estimate_regions_size() {
1394   int class_count = count_class(SharedClassListFile);
1395   class_count += count_class(ExtraSharedClassListFile);
1396 
1397   if (class_count > LargeThresholdClassCount) {
1398     if (class_count < HugeThresholdClassCount) {
1399       SET_ESTIMATED_SIZE(Large, ReadOnly);
1400       SET_ESTIMATED_SIZE(Large, ReadWrite);
1401       SET_ESTIMATED_SIZE(Large, MiscData);
1402       SET_ESTIMATED_SIZE(Large, MiscCode);
1403     } else {
1404       SET_ESTIMATED_SIZE(Huge,  ReadOnly);
1405       SET_ESTIMATED_SIZE(Huge,  ReadWrite);
1406       SET_ESTIMATED_SIZE(Huge,  MiscData);
1407       SET_ESTIMATED_SIZE(Huge,  MiscCode);
1408     }
1409   }
1410 }