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