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