< prev index next >

src/hotspot/share/classfile/systemDictionaryShared.cpp

Print this page




  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/classFileStream.hpp"
  27 #include "classfile/classListParser.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/classLoaderData.inline.hpp"
  30 #include "classfile/classLoaderDataGraph.hpp"
  31 #include "classfile/classLoaderExt.hpp"
  32 #include "classfile/dictionary.hpp"
  33 #include "classfile/javaClasses.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/systemDictionary.hpp"
  36 #include "classfile/systemDictionaryShared.hpp"
  37 #include "classfile/verificationType.hpp"
  38 #include "classfile/vmSymbols.hpp"

  39 #include "logging/log.hpp"
  40 #include "memory/allocation.hpp"
  41 #include "memory/archiveUtils.hpp"

  42 #include "memory/filemap.hpp"
  43 #include "memory/heapShared.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/metaspaceClosure.hpp"
  46 #include "memory/oopFactory.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "memory/dynamicArchive.hpp"
  50 #include "oops/instanceKlass.hpp"
  51 #include "oops/klass.inline.hpp"
  52 #include "oops/objArrayOop.inline.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/typeArrayOop.inline.hpp"
  55 #include "runtime/handles.inline.hpp"
  56 #include "runtime/java.hpp"
  57 #include "runtime/javaCalls.hpp"
  58 #include "runtime/mutexLocker.hpp"
  59 #include "utilities/hashtable.inline.hpp"
  60 #include "utilities/resourceHash.hpp"
  61 #include "utilities/stringUtils.hpp"
  62 
  63 
  64 objArrayOop SystemDictionaryShared::_shared_protection_domains  =  NULL;
  65 objArrayOop SystemDictionaryShared::_shared_jar_urls            =  NULL;
  66 objArrayOop SystemDictionaryShared::_shared_jar_manifests       =  NULL;
  67 DEBUG_ONLY(bool SystemDictionaryShared::_no_class_loading_should_happen = false;)
  68 
  69 class DumpTimeSharedClassInfo: public CHeapObj<mtClass> {


  84     }
  85   };
  86 
  87   struct DTVerifierConstraint {
  88     Symbol* _name;
  89     Symbol* _from_name;
  90     DTVerifierConstraint() : _name(NULL), _from_name(NULL) {}
  91     DTVerifierConstraint(Symbol* n, Symbol* fn) : _name(n), _from_name(fn) {
  92       _name->increment_refcount();
  93       _from_name->increment_refcount();
  94     }
  95   };
  96 
  97   InstanceKlass*               _klass;
  98   bool                         _failed_verification;
  99   int                          _id;
 100   int                          _clsfile_size;
 101   int                          _clsfile_crc32;
 102   GrowableArray<DTVerifierConstraint>* _verifier_constraints;
 103   GrowableArray<char>*                 _verifier_constraint_flags;

 104   GrowableArray<DTLoaderConstraint>* _loader_constraints;
 105 
 106   DumpTimeSharedClassInfo() {
 107     _klass = NULL;
 108     _failed_verification = false;
 109     _id = -1;
 110     _clsfile_size = -1;
 111     _clsfile_crc32 = -1;
 112     _excluded = false;
 113     _verifier_constraints = NULL;
 114     _verifier_constraint_flags = NULL;

 115     _loader_constraints = NULL;
 116   }
 117 
 118   void add_verification_constraint(InstanceKlass* k, Symbol* name,
 119          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object);
 120   void record_linking_constraint(Symbol* name, Handle loader1, Handle loader2);
 121 
 122   bool is_builtin() {
 123     return SystemDictionaryShared::is_builtin(_klass);
 124   }
 125 
 126   int num_verifier_constraints() {
 127     if (_verifier_constraint_flags != NULL) {
 128       return _verifier_constraint_flags->length();
 129     } else {
 130       return 0;
 131     }
 132   }
 133 
 134   int num_loader_constraints() {


 225       return true; // keep on iterating
 226     }
 227   };
 228 
 229   void update_counts() {
 230     _builtin_count = 0;
 231     _unregistered_count = 0;
 232     CountClassByCategory counter(this);
 233     iterate(&counter);
 234   }
 235 
 236   int count_of(bool is_builtin) const {
 237     if (is_builtin) {
 238       return _builtin_count;
 239     } else {
 240       return _unregistered_count;
 241     }
 242   }
 243 };
 244 
























































































































































 245 class RunTimeSharedClassInfo {
 246 public:
 247   struct CrcInfo {
 248     int _clsfile_size;
 249     int _clsfile_crc32;
 250   };
 251 
 252   // This is different than  DumpTimeSharedClassInfo::DTVerifierConstraint. We use
 253   // u4 instead of Symbol* to save space on 64-bit CPU.
 254   struct RTVerifierConstraint {
 255     u4 _name;
 256     u4 _from_name;
 257     Symbol* name() { return (Symbol*)(SharedBaseAddress + _name);}
 258     Symbol* from_name() { return (Symbol*)(SharedBaseAddress + _from_name); }
 259   };
 260 
 261   struct RTLoaderConstraint {
 262     u4   _name;
 263     char _loader_type1;
 264     char _loader_type2;
 265     Symbol* constraint_name() {
 266       return (Symbol*)(SharedBaseAddress + _name);
 267     }
 268   };
 269 
 270   InstanceKlass* _klass;
 271   int _num_verifier_constraints;
 272   int _num_loader_constraints;
 273 
 274   // optional CrcInfo              _crc;  (only for UNREGISTERED classes)

 275   // optional RTLoaderConstraint   _loader_constraint_types[_num_loader_constraints]
 276   // optional RTVerifierConstraint _verifier_constraints[_num_verifier_constraints]
 277   // optional char                 _verifier_constraint_flags[_num_verifier_constraints]
 278 
 279 private:
 280   static size_t header_size_size() {
 281     return sizeof(RunTimeSharedClassInfo);
 282   }
 283   static size_t crc_size(InstanceKlass* klass) {
 284     if (!SystemDictionaryShared::is_builtin(klass)) {
 285       return sizeof(CrcInfo);
 286     } else {
 287       return 0;
 288     }
 289   }
 290   static size_t verifier_constraints_size(int num_verifier_constraints) {
 291     return sizeof(RTVerifierConstraint) * num_verifier_constraints;
 292   }
 293   static size_t verifier_constraint_flags_size(int num_verifier_constraints) {
 294     return sizeof(char) * num_verifier_constraints;
 295   }
 296   static size_t loader_constraints_size(int num_loader_constraints) {
 297     return sizeof(RTLoaderConstraint) * num_loader_constraints;
 298   }







 299 
 300 public:
 301   static size_t byte_size(InstanceKlass* klass, int num_verifier_constraints, int num_loader_constraints) {
 302     return header_size_size() +
 303            crc_size(klass) +

 304            loader_constraints_size(num_loader_constraints) +
 305            verifier_constraints_size(num_verifier_constraints) +
 306            verifier_constraint_flags_size(num_verifier_constraints);
 307   }
 308 
 309 private:
 310   size_t crc_offset() const {
 311     return header_size_size();
 312   }
 313 
 314   size_t loader_constraints_offset() const  {
 315     return crc_offset() + crc_size(_klass);
 316   }




 317   size_t verifier_constraints_offset() const {
 318     return loader_constraints_offset() + loader_constraints_size(_num_loader_constraints);
 319   }
 320   size_t verifier_constraint_flags_offset() const {
 321     return verifier_constraints_offset() + verifier_constraints_size(_num_verifier_constraints);
 322   }
 323 
 324   void check_verifier_constraint_offset(int i) const {
 325     assert(0 <= i && i < _num_verifier_constraints, "sanity");
 326   }
 327 
 328   void check_loader_constraint_offset(int i) const {
 329     assert(0 <= i && i < _num_loader_constraints, "sanity");
 330   }
 331 
 332 public:
 333   CrcInfo* crc() const {
 334     assert(crc_size(_klass) > 0, "must be");
 335     return (CrcInfo*)(address(this) + crc_offset());
 336   }
 337   RTVerifierConstraint* verifier_constraints() {
 338     assert(_num_verifier_constraints > 0, "sanity");
 339     return (RTVerifierConstraint*)(address(this) + verifier_constraints_offset());
 340   }
 341   RTVerifierConstraint* verifier_constraint_at(int i) {
 342     check_verifier_constraint_offset(i);
 343     return verifier_constraints() + i;
 344   }
 345 
 346   char* verifier_constraint_flags() {
 347     assert(_num_verifier_constraints > 0, "sanity");
 348     return (char*)(address(this) + verifier_constraint_flags_offset());
 349   }
 350 












 351   RTLoaderConstraint* loader_constraints() {
 352     assert(_num_loader_constraints > 0, "sanity");
 353     return (RTLoaderConstraint*)(address(this) + loader_constraints_offset());
 354   }
 355 
 356   RTLoaderConstraint* loader_constraint_at(int i) {
 357     check_loader_constraint_offset(i);
 358     return loader_constraints() + i;
 359   }
 360 
 361   static u4 object_delta_u4(Symbol* sym) {
 362     if (DynamicDumpSharedSpaces) {
 363       sym = DynamicArchive::original_to_target(sym);
 364     }
 365     return MetaspaceShared::object_delta_u4(sym);
 366   }
 367 
 368   void init(DumpTimeSharedClassInfo& info) {
 369     _klass = info._klass;
 370     if (!SystemDictionaryShared::is_builtin(_klass)) {


 379       RTVerifierConstraint* vf_constraints = verifier_constraints();
 380       char* flags = verifier_constraint_flags();
 381       for (i = 0; i < _num_verifier_constraints; i++) {
 382         vf_constraints[i]._name      = object_delta_u4(info._verifier_constraints->at(i)._name);
 383         vf_constraints[i]._from_name = object_delta_u4(info._verifier_constraints->at(i)._from_name);
 384       }
 385       for (i = 0; i < _num_verifier_constraints; i++) {
 386         flags[i] = info._verifier_constraint_flags->at(i);
 387       }
 388     }
 389 
 390     if (_num_loader_constraints > 0) {
 391       RTLoaderConstraint* ld_constraints = loader_constraints();
 392       for (i = 0; i < _num_loader_constraints; i++) {
 393         ld_constraints[i]._name = object_delta_u4(info._loader_constraints->at(i)._name);
 394         ld_constraints[i]._loader_type1 = info._loader_constraints->at(i)._loader_type1;
 395         ld_constraints[i]._loader_type2 = info._loader_constraints->at(i)._loader_type2;
 396       }
 397     }
 398     if (DynamicDumpSharedSpaces) {







 399       _klass = DynamicArchive::original_to_target(info._klass);
 400     }
 401     ArchivePtrMarker::mark_pointer(&_klass);
 402   }
 403 
 404   bool matches(int clsfile_size, int clsfile_crc32) const {
 405     return crc()->_clsfile_size  == clsfile_size &&
 406            crc()->_clsfile_crc32 == clsfile_crc32;
 407   }
 408 
 409   char verifier_constraint_flag(int i) {
 410     check_verifier_constraint_offset(i);
 411     return verifier_constraint_flags()[i];
 412   }
 413 
 414 private:
 415   // ArchiveCompactor::allocate() has reserved a pointer immediately before
 416   // archived InstanceKlasses. We can use this slot to do a quick
 417   // lookup of InstanceKlass* -> RunTimeSharedClassInfo* without
 418   // building a new hashtable.
 419   //
 420   //  info_pointer_addr(klass) --> 0x0100   RunTimeSharedClassInfo*
 421   //  InstanceKlass* klass     --> 0x0108   <C++ vtbl>
 422   //                               0x0110   fields from Klass ...
 423   static RunTimeSharedClassInfo** info_pointer_addr(InstanceKlass* klass) {
 424     return &((RunTimeSharedClassInfo**)klass)[-1];
 425   }
 426 
 427 public:
 428   static RunTimeSharedClassInfo* get_for(InstanceKlass* klass) {

 429     return *info_pointer_addr(klass);
 430   }
 431   static void set_for(InstanceKlass* klass, RunTimeSharedClassInfo* record) {
 432     if (DynamicDumpSharedSpaces) {
 433       klass = DynamicArchive::original_to_buffer(klass);
 434       *info_pointer_addr(klass) = DynamicArchive::buffer_to_target(record);
 435     } else {
 436       *info_pointer_addr(klass) = record;
 437     }
 438 
 439     ArchivePtrMarker::mark_pointer(info_pointer_addr(klass));
 440   }
 441 
 442   // Used by RunTimeSharedDictionary to implement OffsetCompactHashtable::EQUALS
 443   static inline bool EQUALS(
 444        const RunTimeSharedClassInfo* value, Symbol* key, int len_unused) {
 445     return (value->_klass->name() == key);
 446   }
 447 };
 448 


1134       if (ld._name != NULL) {
1135         ld._name->decrement_refcount();
1136       }
1137     }
1138     FREE_C_HEAP_ARRAY(DumpTimeSharedClassInfo::DTLoaderConstraint, p->_loader_constraints);
1139     p->_loader_constraints = NULL;
1140   }
1141   _dumptime_table->remove(k);
1142 }
1143 
1144 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) {
1145   while (k) {
1146     if (k->name()->equals("jdk/internal/event/Event")) {
1147       return true;
1148     }
1149     k = k->java_super();
1150   }
1151   return false;
1152 }
1153 

























1154 void SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) {
1155   ResourceMark rm;
1156   log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason);
1157 }
1158 
1159 bool SystemDictionaryShared::should_be_excluded(InstanceKlass* k) {
1160   if (k->is_hidden() || k->is_unsafe_anonymous()) {
1161     warn_excluded(k, "Hidden or Unsafe anonymous class");
1162     return true; // hidden and unsafe anonymous classes are not archived, skip

1163   }

1164   if (k->is_in_error_state()) {
1165     warn_excluded(k, "In error state");
1166     return true;
1167   }
1168   if (k->has_been_redefined()) {
1169     warn_excluded(k, "Has been redefined");
1170     return true;
1171   }
1172   if (k->shared_classpath_index() < 0 && is_builtin(k)) {
1173     // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
1174     // agent during dump time).
1175     warn_excluded(k, "Unsupported location");
1176     return true;
1177   }
1178   if (k->signers() != NULL) {
1179     // We cannot include signed classes in the archive because the certificates
1180     // used during dump time may be different than those used during
1181     // runtime (due to expiration, etc).
1182     warn_excluded(k, "Signed JAR");
1183     return true;
1184   }
1185   if (is_jfr_event_class(k)) {
1186     // We cannot include JFR event classes because they need runtime-specific
1187     // instrumentation in order to work with -XX:FlightRecorderOptions=retransform=false.
1188     // There are only a small number of these classes, so it's not worthwhile to
1189     // support them and make CDS more complicated.
1190     warn_excluded(k, "JFR event class");
1191     return true;
1192   }


1205       warn_excluded(k, "Failed verification");
1206     } else {
1207       warn_excluded(k, "Not linked");
1208     }
1209     return true;
1210   }
1211   if (k->major_version() < 50 /*JAVA_6_VERSION*/) {
1212     ResourceMark rm;
1213     log_warning(cds)("Pre JDK 6 class not supported by CDS: %u.%u %s",
1214                      k->major_version(),  k->minor_version(), k->name()->as_C_string());
1215     return true;
1216   }
1217 
1218   InstanceKlass* super = k->java_super();
1219   if (super != NULL && should_be_excluded(super)) {
1220     ResourceMark rm;
1221     log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
1222     return true;
1223   }
1224 





1225   Array<InstanceKlass*>* interfaces = k->local_interfaces();
1226   int len = interfaces->length();
1227   for (int i = 0; i < len; i++) {
1228     InstanceKlass* intf = interfaces->at(i);
1229     if (should_be_excluded(intf)) {
1230       log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
1231       return true;
1232     }
1233   }
1234 
1235   return false;
1236 }
1237 
1238 // k is a class before relocating by ArchiveCompactor
1239 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
1240   ResourceMark rm;
1241   const char* name = k->name()->as_C_string();
1242   DumpTimeSharedClassInfo* info = _dumptime_table->get(k);
1243   assert(_no_class_loading_should_happen, "class loading must be disabled");
1244   guarantee(info != NULL, "Class %s must be entered into _dumptime_table", name);
1245   guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
1246   if (is_builtin(k)) {



1247     guarantee(!k->is_shared_unregistered_class(),
1248               "Class loader type must be set for BUILTIN class %s", name);

1249   } else {
1250     guarantee(k->is_shared_unregistered_class(),
1251               "Class loader type must not be set for UNREGISTERED class %s", name);
1252   }
1253 }
1254 
1255 class ExcludeDumpTimeSharedClasses : StackObj {
1256 public:
1257   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1258     if (SystemDictionaryShared::should_be_excluded(k)) {
1259       info.set_excluded();
1260     }
1261     return true; // keep on iterating
1262   }
1263 };
1264 
1265 void SystemDictionaryShared::check_excluded_classes() {
1266   ExcludeDumpTimeSharedClasses excl;
1267   _dumptime_table->iterate(&excl);
1268   _dumptime_table->update_counts();


1344   }
1345   GrowableArray<DTVerifierConstraint>* vc_array = _verifier_constraints;
1346   for (int i = 0; i < vc_array->length(); i++) {
1347     DTVerifierConstraint* p = vc_array->adr_at(i);
1348     if (name == p->_name && from_name == p->_from_name) {
1349       return;
1350     }
1351   }
1352   DTVerifierConstraint cons(name, from_name);
1353   vc_array->append(cons);
1354 
1355   GrowableArray<char>* vcflags_array = _verifier_constraint_flags;
1356   char c = 0;
1357   c |= from_field_is_protected ? SystemDictionaryShared::FROM_FIELD_IS_PROTECTED : 0;
1358   c |= from_is_array           ? SystemDictionaryShared::FROM_IS_ARRAY           : 0;
1359   c |= from_is_object          ? SystemDictionaryShared::FROM_IS_OBJECT          : 0;
1360   vcflags_array->append(c);
1361 
1362   if (log_is_enabled(Trace, cds, verification)) {
1363     ResourceMark rm;
1364     log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s [0x%x]",
1365                                  k->external_name(), from_name->as_klass_external_name(),
1366                                  name->as_klass_external_name(), c);
























































































1367   }




































1368 }
1369 
1370 static char get_loader_type_by(oop  loader) {
1371   assert(SystemDictionary::is_builtin_class_loader(loader), "Must be built-in loader");
1372   if (SystemDictionary::is_boot_class_loader(loader)) {
1373     return (char)ClassLoader::BOOT_LOADER;
1374   } else if (SystemDictionary::is_platform_class_loader(loader)) {
1375     return (char)ClassLoader::PLATFORM_LOADER;
1376   } else {
1377     assert(SystemDictionary::is_system_class_loader(loader), "Class loader mismatch");
1378     return (char)ClassLoader::APP_LOADER;
1379   }
1380 }
1381 
1382 static oop get_class_loader_by(char type) {
1383   if (type == (char)ClassLoader::BOOT_LOADER) {
1384     return (oop)NULL;
1385   } else if (type == (char)ClassLoader::PLATFORM_LOADER) {
1386     return SystemDictionary::java_platform_loader();
1387   } else {


1580     _num_builtin_klasses = 0;
1581     _num_unregistered_klasses = 0;
1582   }
1583 
1584   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1585     if (!info.is_excluded()) {
1586       size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints());
1587       _shared_class_info_size += align_up(byte_size, BytesPerWord);
1588     }
1589     return true; // keep on iterating
1590   }
1591 
1592   size_t total() {
1593     return _shared_class_info_size;
1594   }
1595 };
1596 
1597 size_t SystemDictionaryShared::estimate_size_for_archive() {
1598   EstimateSizeForArchive est;
1599   _dumptime_table->iterate(&est);
1600   return est.total() +
1601     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1602     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));








1603 }
1604 
















































1605 class CopySharedClassInfoToArchive : StackObj {
1606   CompactHashtableWriter* _writer;
1607   bool _is_builtin;
1608 public:
1609   CopySharedClassInfoToArchive(CompactHashtableWriter* writer,
1610                                bool is_builtin,
1611                                bool is_static_archive)
1612     : _writer(writer), _is_builtin(is_builtin) {}
1613 
1614   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1615     if (!info.is_excluded() && info.is_builtin() == _is_builtin) {
1616       size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints());
1617       RunTimeSharedClassInfo* record;
1618       record = (RunTimeSharedClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size);
1619       record->init(info);
1620 
1621       unsigned int hash;
1622       Symbol* name = info._klass->name();
1623       if (DynamicDumpSharedSpaces) {
1624         name = DynamicArchive::original_to_target(name);
1625       }
1626       hash = SystemDictionaryShared::hash_for_shared_dictionary(name);
1627       u4 delta;
1628       if (DynamicDumpSharedSpaces) {
1629         delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(record));
1630       } else {
1631         delta = MetaspaceShared::object_delta_u4(record);
1632       }



1633       _writer->add(hash, delta);

1634       if (log_is_enabled(Trace, cds, hashtables)) {
1635         ResourceMark rm;
1636         log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1637       }
1638 
1639       // Save this for quick runtime lookup of InstanceKlass* -> RunTimeSharedClassInfo*
1640       RunTimeSharedClassInfo::set_for(info._klass, record);
1641     }
1642     return true; // keep on iterating
1643   }
1644 };
1645 









1646 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1647                                               bool is_builtin,
1648                                               bool is_static_archive) {
1649   CompactHashtableStats stats;
1650   dictionary->reset();
1651   CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1652   CopySharedClassInfoToArchive copy(&writer, is_builtin, is_static_archive);
1653   _dumptime_table->iterate(&copy);
1654   writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1655 }
1656 
1657 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1658   if (is_static_archive) {
1659     write_dictionary(&_builtin_dictionary, true);
1660     write_dictionary(&_unregistered_dictionary, false);
1661   } else {
1662     write_dictionary(&_dynamic_builtin_dictionary, true);
1663     write_dictionary(&_dynamic_unregistered_dictionary, false);
1664   }










1665 }
1666 
1667 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
1668                                                           bool is_static_archive) {
1669   if (is_static_archive) {
1670     _builtin_dictionary.serialize_header(soc);
1671     _unregistered_dictionary.serialize_header(soc);
1672   } else {
1673     _dynamic_builtin_dictionary.serialize_header(soc);
1674     _dynamic_unregistered_dictionary.serialize_header(soc);

1675   }
1676 }
1677 
1678 void SystemDictionaryShared::serialize_well_known_klasses(SerializeClosure* soc) {
1679   for (int i = FIRST_WKID; i < WKID_LIMIT; i++) {
1680     soc->do_ptr((void**)&_well_known_klasses[i]);
1681   }
1682 }
1683 
1684 const RunTimeSharedClassInfo*
1685 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1686   if (!UseSharedSpaces || !name->is_shared()) {
1687     // The names of all shared classes must also be a shared Symbol.
1688     return NULL;
1689   }
1690 
1691   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(name);
1692   const RunTimeSharedClassInfo* record = NULL;
1693   if (!MetaspaceShared::is_shared_dynamic(name)) {
1694     // The names of all shared classes in the static dict must also be in the
1695     // static archive
1696     record = static_dict->lookup(name, hash, 0);
1697   }
1698 
1699   if (record == NULL && DynamicArchive::is_mapped()) {
1700     record = dynamic_dict->lookup(name, hash, 0);
1701   }
1702 
1703   return record;
1704 }
1705 
1706 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) {
1707   const RunTimeSharedClassInfo* record = find_record(&_builtin_dictionary, &_dynamic_builtin_dictionary, name);
1708   if (record != NULL) {

1709     return record->_klass;
1710   } else {
1711     return NULL;
1712   }
1713 }
1714 
1715 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1716   assert(DumpSharedSpaces, "supported only when dumping");
1717   DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k);
1718   info->_id = id;
1719 }
1720 
1721 class SharedDictionaryPrinter : StackObj {
1722   outputStream* _st;
1723   int _index;
1724 public:
1725   SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1726 
1727   void do_value(const RunTimeSharedClassInfo* record) {
1728     ResourceMark rm;
1729     _st->print_cr("%4d:  %s", (_index++), record->_klass->external_name());
1730   }
1731 };
1732 

















1733 void SystemDictionaryShared::print_on(outputStream* st) {
1734   if (UseSharedSpaces) {
1735     st->print_cr("Shared Dictionary");
1736     SharedDictionaryPrinter p(st);
1737     _builtin_dictionary.iterate(&p);
1738     _unregistered_dictionary.iterate(&p);
1739     if (DynamicArchive::is_mapped()) {
1740       _dynamic_builtin_dictionary.iterate(&p);
1741       _unregistered_dictionary.iterate(&p);





1742     }
1743   }
1744 }
1745 
1746 void SystemDictionaryShared::print_table_statistics(outputStream* st) {
1747   if (UseSharedSpaces) {
1748     _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
1749     _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
1750     if (DynamicArchive::is_mapped()) {
1751       _dynamic_builtin_dictionary.print_table_statistics(st, "Dynamic Builtin Shared Dictionary");
1752       _dynamic_unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");

1753     }
1754   }
1755 }
1756 
1757 bool SystemDictionaryShared::empty_dumptime_table() {
1758   if (_dumptime_table == NULL) {
1759     return true;
1760   }
1761   _dumptime_table->update_counts();
1762   if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){
1763     return true;
1764   }
1765   return false;
1766 }
1767 
1768 #if INCLUDE_CDS_JAVA_HEAP
1769 
1770 class ArchivedMirrorPatcher {
1771   static void update(Klass* k) {
1772     if (k->has_raw_archived_mirror()) {




  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/classFileStream.hpp"
  27 #include "classfile/classListParser.hpp"
  28 #include "classfile/classLoader.hpp"
  29 #include "classfile/classLoaderData.inline.hpp"
  30 #include "classfile/classLoaderDataGraph.hpp"
  31 #include "classfile/classLoaderExt.hpp"
  32 #include "classfile/dictionary.hpp"
  33 #include "classfile/javaClasses.hpp"
  34 #include "classfile/symbolTable.hpp"
  35 #include "classfile/systemDictionary.hpp"
  36 #include "classfile/systemDictionaryShared.hpp"
  37 #include "classfile/verificationType.hpp"
  38 #include "classfile/vmSymbols.hpp"
  39 #include "jfr/jfrEvents.hpp"
  40 #include "logging/log.hpp"
  41 #include "memory/allocation.hpp"
  42 #include "memory/archiveUtils.hpp"
  43 #include "memory/dynamicArchive.hpp"
  44 #include "memory/filemap.hpp"
  45 #include "memory/heapShared.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/metaspaceClosure.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"

  51 #include "oops/instanceKlass.hpp"
  52 #include "oops/klass.inline.hpp"
  53 #include "oops/objArrayOop.inline.hpp"
  54 #include "oops/oop.inline.hpp"
  55 #include "oops/typeArrayOop.inline.hpp"
  56 #include "runtime/handles.inline.hpp"
  57 #include "runtime/java.hpp"
  58 #include "runtime/javaCalls.hpp"
  59 #include "runtime/mutexLocker.hpp"
  60 #include "utilities/hashtable.inline.hpp"
  61 #include "utilities/resourceHash.hpp"
  62 #include "utilities/stringUtils.hpp"
  63 
  64 
  65 objArrayOop SystemDictionaryShared::_shared_protection_domains  =  NULL;
  66 objArrayOop SystemDictionaryShared::_shared_jar_urls            =  NULL;
  67 objArrayOop SystemDictionaryShared::_shared_jar_manifests       =  NULL;
  68 DEBUG_ONLY(bool SystemDictionaryShared::_no_class_loading_should_happen = false;)
  69 
  70 class DumpTimeSharedClassInfo: public CHeapObj<mtClass> {


  85     }
  86   };
  87 
  88   struct DTVerifierConstraint {
  89     Symbol* _name;
  90     Symbol* _from_name;
  91     DTVerifierConstraint() : _name(NULL), _from_name(NULL) {}
  92     DTVerifierConstraint(Symbol* n, Symbol* fn) : _name(n), _from_name(fn) {
  93       _name->increment_refcount();
  94       _from_name->increment_refcount();
  95     }
  96   };
  97 
  98   InstanceKlass*               _klass;
  99   bool                         _failed_verification;
 100   int                          _id;
 101   int                          _clsfile_size;
 102   int                          _clsfile_crc32;
 103   GrowableArray<DTVerifierConstraint>* _verifier_constraints;
 104   GrowableArray<char>*                 _verifier_constraint_flags;
 105   bool                         _is_archived_lambda_proxy;
 106   GrowableArray<DTLoaderConstraint>* _loader_constraints;
 107 
 108   DumpTimeSharedClassInfo() {
 109     _klass = NULL;
 110     _failed_verification = false;
 111     _id = -1;
 112     _clsfile_size = -1;
 113     _clsfile_crc32 = -1;
 114     _excluded = false;
 115     _verifier_constraints = NULL;
 116     _verifier_constraint_flags = NULL;
 117     _is_archived_lambda_proxy = false;
 118     _loader_constraints = NULL;
 119   }
 120 
 121   void add_verification_constraint(InstanceKlass* k, Symbol* name,
 122          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object);
 123   void record_linking_constraint(Symbol* name, Handle loader1, Handle loader2);
 124 
 125   bool is_builtin() {
 126     return SystemDictionaryShared::is_builtin(_klass);
 127   }
 128 
 129   int num_verifier_constraints() {
 130     if (_verifier_constraint_flags != NULL) {
 131       return _verifier_constraint_flags->length();
 132     } else {
 133       return 0;
 134     }
 135   }
 136 
 137   int num_loader_constraints() {


 228       return true; // keep on iterating
 229     }
 230   };
 231 
 232   void update_counts() {
 233     _builtin_count = 0;
 234     _unregistered_count = 0;
 235     CountClassByCategory counter(this);
 236     iterate(&counter);
 237   }
 238 
 239   int count_of(bool is_builtin) const {
 240     if (is_builtin) {
 241       return _builtin_count;
 242     } else {
 243       return _unregistered_count;
 244     }
 245   }
 246 };
 247 
 248 class LambdaProxyClassKey {
 249   template <typename T> static void original_to_target(T& field) {
 250     if (field != NULL) {
 251       field = DynamicArchive::original_to_target(field);
 252       ArchivePtrMarker::mark_pointer(&field);
 253     }
 254   }
 255 
 256   InstanceKlass*               _caller_ik;
 257   Symbol*                      _invoked_name;
 258   Symbol*                      _invoked_type;
 259   Symbol*                      _method_type;
 260   Method*                      _member_method;
 261   Symbol*                      _instantiated_method_type;
 262 
 263 public:
 264   LambdaProxyClassKey(InstanceKlass* caller_ik,
 265                       Symbol*        invoked_name,
 266                       Symbol*        invoked_type,
 267                       Symbol*        method_type,
 268                       Method*        member_method,
 269                       Symbol*        instantiated_method_type) :
 270     _caller_ik(caller_ik),
 271     _invoked_name(invoked_name),
 272     _invoked_type(invoked_type),
 273     _method_type(method_type),
 274     _member_method(member_method),
 275     _instantiated_method_type(instantiated_method_type) {}
 276 
 277   void original_to_target() {
 278     original_to_target(_caller_ik);
 279     original_to_target(_instantiated_method_type);
 280     original_to_target(_invoked_name);
 281     original_to_target(_invoked_type);
 282     original_to_target(_member_method);
 283     original_to_target(_method_type);
 284   }
 285 
 286   bool equals(LambdaProxyClassKey const& other) const {
 287     return _caller_ik == other._caller_ik &&
 288            _invoked_name == other._invoked_name &&
 289            _invoked_type == other._invoked_type &&
 290            _method_type == other._method_type &&
 291            _member_method == other._member_method &&
 292            _instantiated_method_type == other._instantiated_method_type;
 293   }
 294 
 295   unsigned int hash() const {
 296     return SystemDictionaryShared::hash_for_shared_dictionary(_caller_ik) +
 297            SystemDictionaryShared::hash_for_shared_dictionary(_invoked_name) +
 298            SystemDictionaryShared::hash_for_shared_dictionary(_invoked_type) +
 299            SystemDictionaryShared::hash_for_shared_dictionary(_method_type) +
 300            SystemDictionaryShared::hash_for_shared_dictionary(_instantiated_method_type);
 301   }
 302 
 303   unsigned int dumptime_hash() const {
 304     return primitive_hash<InstanceKlass*>(_caller_ik) +
 305            primitive_hash<Symbol*>(_invoked_name) +
 306            primitive_hash<Symbol*>(_invoked_type) +
 307            primitive_hash<Symbol*>(_method_type) +
 308            primitive_hash<Symbol*>(_instantiated_method_type);
 309   }
 310 
 311   static inline unsigned int DUMPTIME_HASH(LambdaProxyClassKey const& key) {
 312     return (key.dumptime_hash());
 313   }
 314 
 315   static inline bool DUMPTIME_EQUALS(
 316       LambdaProxyClassKey const& k1, LambdaProxyClassKey const& k2) {
 317     return (k1.equals(k2));
 318   }
 319 };
 320 
 321 
 322 class DumpTimeLambdaProxyClassInfo {
 323 public:
 324   GrowableArray<InstanceKlass*>* _proxy_klass;
 325   DumpTimeLambdaProxyClassInfo() : _proxy_klass(NULL) {}
 326   void add_proxy_klass(InstanceKlass* proxy_klass) {
 327     if (_proxy_klass == NULL) {
 328       _proxy_klass = new (ResourceObj::C_HEAP, mtInternal)GrowableArray<InstanceKlass*>(5, true);
 329     }
 330     assert(_proxy_klass != NULL, "sanity");
 331     _proxy_klass->append(proxy_klass);
 332   }
 333 };
 334 
 335 class RunTimeLambdaProxyClassInfo {
 336   LambdaProxyClassKey _key;
 337   InstanceKlass* _proxy_klass;
 338 public:
 339   RunTimeLambdaProxyClassInfo(LambdaProxyClassKey key, InstanceKlass* proxy_klass) :
 340     _key(key), _proxy_klass(proxy_klass) {}
 341 
 342   InstanceKlass* proxy_klass() const { return _proxy_klass; }
 343 
 344   // Used by LambdaProxyClassDictionary to implement OffsetCompactHashtable::EQUALS
 345   static inline bool EQUALS(
 346        const RunTimeLambdaProxyClassInfo* value, LambdaProxyClassKey* key, int len_unused) {
 347     return (value->_key.equals(*key));
 348   }
 349   void init(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
 350     _key = key;
 351     _key.original_to_target();
 352     _proxy_klass = DynamicArchive::original_to_target(info._proxy_klass->at(0));
 353     ArchivePtrMarker::mark_pointer(&_proxy_klass);
 354   }
 355 
 356   unsigned int hash() const {
 357     return _key.hash();
 358   }
 359 };
 360 
 361 class LambdaProxyClassDictionary : public OffsetCompactHashtable<
 362   LambdaProxyClassKey*,
 363   const RunTimeLambdaProxyClassInfo*,
 364   RunTimeLambdaProxyClassInfo::EQUALS> {};
 365 
 366 LambdaProxyClassDictionary _lambda_proxy_class_dictionary;
 367 
 368 class DumpTimeLambdaProxyClassDictionary
 369   : public ResourceHashtable<LambdaProxyClassKey,
 370                              DumpTimeLambdaProxyClassInfo,
 371                              LambdaProxyClassKey::DUMPTIME_HASH,
 372                              LambdaProxyClassKey::DUMPTIME_EQUALS,
 373                              137, // prime number
 374                              ResourceObj::C_HEAP> {
 375 public:
 376   int _count;
 377 };
 378 
 379 DumpTimeLambdaProxyClassDictionary* _dumptime_lambda_proxy_class_dictionary = NULL;
 380 
 381 static void add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey key,
 382                                                            InstanceKlass* proxy_klass) {
 383   if (_dumptime_lambda_proxy_class_dictionary == NULL) {
 384     _dumptime_lambda_proxy_class_dictionary =
 385       new (ResourceObj::C_HEAP, mtClass)DumpTimeLambdaProxyClassDictionary();
 386   }
 387   DumpTimeLambdaProxyClassInfo* lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key);
 388   if (lambda_info == NULL) {
 389     DumpTimeLambdaProxyClassInfo info;
 390     info.add_proxy_klass(proxy_klass);
 391     _dumptime_lambda_proxy_class_dictionary->put(key, info);
 392     //lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key);
 393     //assert(lambda_info->_proxy_klass == proxy_klass, "must be"); // debug only -- remove
 394     ++_dumptime_lambda_proxy_class_dictionary->_count;
 395   } else {
 396     lambda_info->add_proxy_klass(proxy_klass);
 397   }
 398 }
 399 
 400 class RunTimeSharedClassInfo {
 401 public:
 402   struct CrcInfo {
 403     int _clsfile_size;
 404     int _clsfile_crc32;
 405   };
 406 
 407   // This is different than  DumpTimeSharedClassInfo::DTVerifierConstraint. We use
 408   // u4 instead of Symbol* to save space on 64-bit CPU.
 409   struct RTVerifierConstraint {
 410     u4 _name;
 411     u4 _from_name;
 412     Symbol* name() { return (Symbol*)(SharedBaseAddress + _name);}
 413     Symbol* from_name() { return (Symbol*)(SharedBaseAddress + _from_name); }
 414   };
 415 
 416   struct RTLoaderConstraint {
 417     u4   _name;
 418     char _loader_type1;
 419     char _loader_type2;
 420     Symbol* constraint_name() {
 421       return (Symbol*)(SharedBaseAddress + _name);
 422     }
 423   };
 424 
 425   InstanceKlass* _klass;
 426   int _num_verifier_constraints;
 427   int _num_loader_constraints;
 428 
 429   // optional CrcInfo              _crc;  (only for UNREGISTERED classes)
 430   // optional InstanceKlass*       _nest_host
 431   // optional RTLoaderConstraint   _loader_constraint_types[_num_loader_constraints]
 432   // optional RTVerifierConstraint _verifier_constraints[_num_verifier_constraints]
 433   // optional char                 _verifier_constraint_flags[_num_verifier_constraints]
 434 
 435 private:
 436   static size_t header_size_size() {
 437     return sizeof(RunTimeSharedClassInfo);
 438   }
 439   static size_t crc_size(InstanceKlass* klass) {
 440     if (!SystemDictionaryShared::is_builtin(klass)) {
 441       return sizeof(CrcInfo);
 442     } else {
 443       return 0;
 444     }
 445   }
 446   static size_t verifier_constraints_size(int num_verifier_constraints) {
 447     return sizeof(RTVerifierConstraint) * num_verifier_constraints;
 448   }
 449   static size_t verifier_constraint_flags_size(int num_verifier_constraints) {
 450     return sizeof(char) * num_verifier_constraints;
 451   }
 452   static size_t loader_constraints_size(int num_loader_constraints) {
 453     return sizeof(RTLoaderConstraint) * num_loader_constraints;
 454   }
 455   static size_t nest_host_size(InstanceKlass* klass) {
 456     if (klass->is_hidden()) {
 457       return sizeof(InstanceKlass*);
 458     } else {
 459       return 0;
 460     }
 461   }
 462 
 463 public:
 464   static size_t byte_size(InstanceKlass* klass, int num_verifier_constraints, int num_loader_constraints) {
 465     return header_size_size() +
 466            crc_size(klass) +
 467            nest_host_size(klass) +
 468            loader_constraints_size(num_loader_constraints) +
 469            verifier_constraints_size(num_verifier_constraints) +
 470            verifier_constraint_flags_size(num_verifier_constraints);
 471   }
 472 
 473 private:
 474   size_t crc_offset() const {
 475     return header_size_size();
 476   }
 477 
 478   size_t nest_host_offset() const {
 479       return crc_offset() + crc_size(_klass);
 480   }
 481 
 482   size_t loader_constraints_offset() const  {
 483     return nest_host_offset() + nest_host_size(_klass);
 484   }
 485   size_t verifier_constraints_offset() const {
 486     return loader_constraints_offset() + loader_constraints_size(_num_loader_constraints);
 487   }
 488   size_t verifier_constraint_flags_offset() const {
 489     return verifier_constraints_offset() + verifier_constraints_size(_num_verifier_constraints);
 490   }
 491 
 492   void check_verifier_constraint_offset(int i) const {
 493     assert(0 <= i && i < _num_verifier_constraints, "sanity");
 494   }
 495 
 496   void check_loader_constraint_offset(int i) const {
 497     assert(0 <= i && i < _num_loader_constraints, "sanity");
 498   }
 499 
 500 public:
 501   CrcInfo* crc() const {
 502     assert(crc_size(_klass) > 0, "must be");
 503     return (CrcInfo*)(address(this) + crc_offset());
 504   }
 505   RTVerifierConstraint* verifier_constraints() {
 506     assert(_num_verifier_constraints > 0, "sanity");
 507     return (RTVerifierConstraint*)(address(this) + verifier_constraints_offset());
 508   }
 509   RTVerifierConstraint* verifier_constraint_at(int i) {
 510     check_verifier_constraint_offset(i);
 511     return verifier_constraints() + i;
 512   }
 513 
 514   char* verifier_constraint_flags() {
 515     assert(_num_verifier_constraints > 0, "sanity");
 516     return (char*)(address(this) + verifier_constraint_flags_offset());
 517   }
 518 
 519   InstanceKlass** nest_host_addr() {
 520     assert(_klass->is_hidden(), "sanity");
 521     return (InstanceKlass**)(address(this) + nest_host_offset());
 522   }
 523   InstanceKlass* nest_host() {
 524     return *nest_host_addr();
 525   }
 526   void set_nest_host(InstanceKlass* k) {
 527     *nest_host_addr() = k;
 528     ArchivePtrMarker::mark_pointer((address*)nest_host_addr());
 529   }
 530 
 531   RTLoaderConstraint* loader_constraints() {
 532     assert(_num_loader_constraints > 0, "sanity");
 533     return (RTLoaderConstraint*)(address(this) + loader_constraints_offset());
 534   }
 535 
 536   RTLoaderConstraint* loader_constraint_at(int i) {
 537     check_loader_constraint_offset(i);
 538     return loader_constraints() + i;
 539   }
 540 
 541   static u4 object_delta_u4(Symbol* sym) {
 542     if (DynamicDumpSharedSpaces) {
 543       sym = DynamicArchive::original_to_target(sym);
 544     }
 545     return MetaspaceShared::object_delta_u4(sym);
 546   }
 547 
 548   void init(DumpTimeSharedClassInfo& info) {
 549     _klass = info._klass;
 550     if (!SystemDictionaryShared::is_builtin(_klass)) {


 559       RTVerifierConstraint* vf_constraints = verifier_constraints();
 560       char* flags = verifier_constraint_flags();
 561       for (i = 0; i < _num_verifier_constraints; i++) {
 562         vf_constraints[i]._name      = object_delta_u4(info._verifier_constraints->at(i)._name);
 563         vf_constraints[i]._from_name = object_delta_u4(info._verifier_constraints->at(i)._from_name);
 564       }
 565       for (i = 0; i < _num_verifier_constraints; i++) {
 566         flags[i] = info._verifier_constraint_flags->at(i);
 567       }
 568     }
 569 
 570     if (_num_loader_constraints > 0) {
 571       RTLoaderConstraint* ld_constraints = loader_constraints();
 572       for (i = 0; i < _num_loader_constraints; i++) {
 573         ld_constraints[i]._name = object_delta_u4(info._loader_constraints->at(i)._name);
 574         ld_constraints[i]._loader_type1 = info._loader_constraints->at(i)._loader_type1;
 575         ld_constraints[i]._loader_type2 = info._loader_constraints->at(i)._loader_type2;
 576       }
 577     }
 578     if (DynamicDumpSharedSpaces) {
 579       if (_klass->is_hidden()) {
 580         Thread* THREAD = Thread::current();
 581         InstanceKlass* n_h = _klass->nest_host(THREAD);
 582         n_h = DynamicArchive::original_to_target(n_h);
 583         set_nest_host(n_h);
 584         ArchivePtrMarker::mark_pointer(&n_h);
 585       }
 586       _klass = DynamicArchive::original_to_target(info._klass);
 587     }
 588     ArchivePtrMarker::mark_pointer(&_klass);
 589   }
 590 
 591   bool matches(int clsfile_size, int clsfile_crc32) const {
 592     return crc()->_clsfile_size  == clsfile_size &&
 593            crc()->_clsfile_crc32 == clsfile_crc32;
 594   }
 595 
 596   char verifier_constraint_flag(int i) {
 597     check_verifier_constraint_offset(i);
 598     return verifier_constraint_flags()[i];
 599   }
 600 
 601 private:
 602   // ArchiveCompactor::allocate() has reserved a pointer immediately before
 603   // archived InstanceKlasses. We can use this slot to do a quick
 604   // lookup of InstanceKlass* -> RunTimeSharedClassInfo* without
 605   // building a new hashtable.
 606   //
 607   //  info_pointer_addr(klass) --> 0x0100   RunTimeSharedClassInfo*
 608   //  InstanceKlass* klass     --> 0x0108   <C++ vtbl>
 609   //                               0x0110   fields from Klass ...
 610   static RunTimeSharedClassInfo** info_pointer_addr(InstanceKlass* klass) {
 611     return &((RunTimeSharedClassInfo**)klass)[-1];
 612   }
 613 
 614 public:
 615   static RunTimeSharedClassInfo* get_for(InstanceKlass* klass) {
 616     assert(klass->is_shared(), "don't call for non-shared class");
 617     return *info_pointer_addr(klass);
 618   }
 619   static void set_for(InstanceKlass* klass, RunTimeSharedClassInfo* record) {
 620     if (DynamicDumpSharedSpaces) {
 621       klass = DynamicArchive::original_to_buffer(klass);
 622       *info_pointer_addr(klass) = DynamicArchive::buffer_to_target(record);
 623     } else {
 624       *info_pointer_addr(klass) = record;
 625     }
 626 
 627     ArchivePtrMarker::mark_pointer(info_pointer_addr(klass));
 628   }
 629 
 630   // Used by RunTimeSharedDictionary to implement OffsetCompactHashtable::EQUALS
 631   static inline bool EQUALS(
 632        const RunTimeSharedClassInfo* value, Symbol* key, int len_unused) {
 633     return (value->_klass->name() == key);
 634   }
 635 };
 636 


1322       if (ld._name != NULL) {
1323         ld._name->decrement_refcount();
1324       }
1325     }
1326     FREE_C_HEAP_ARRAY(DumpTimeSharedClassInfo::DTLoaderConstraint, p->_loader_constraints);
1327     p->_loader_constraints = NULL;
1328   }
1329   _dumptime_table->remove(k);
1330 }
1331 
1332 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) {
1333   while (k) {
1334     if (k->name()->equals("jdk/internal/event/Event")) {
1335       return true;
1336     }
1337     k = k->java_super();
1338   }
1339   return false;
1340 }
1341 
1342 bool SystemDictionaryShared::is_registered_lambda_proxy_class(InstanceKlass* ik) {
1343   DumpTimeSharedClassInfo* info = _dumptime_table->get(ik);
1344   return (info != NULL) ? info->_is_archived_lambda_proxy && !ik->is_non_strong_hidden() : false;
1345 }
1346 
1347 bool SystemDictionaryShared::is_in_shared_lambda_proxy_table(InstanceKlass* ik) {
1348   assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only");
1349   RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(ik);
1350   if (record != NULL && record->nest_host() != NULL) {
1351     return true;
1352   } else {
1353     return false;
1354   }
1355 }
1356 
1357 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
1358   assert(ik->is_shared(), "applicable to only a shared class");
1359   if (ik->is_hidden()) {
1360     assert(is_in_shared_lambda_proxy_table(ik), "we don't archive other hidden classes");
1361     return true;
1362   } else {
1363     return false;
1364   }
1365 }
1366 
1367 void SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) {
1368   ResourceMark rm;
1369   log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason);
1370 }
1371 
1372 bool SystemDictionaryShared::should_be_excluded(InstanceKlass* k) {
1373 
1374   if (k->is_unsafe_anonymous()) {
1375     warn_excluded(k, "Unsafe anonymous class");
1376     return true; // unsafe anonymous classes are not archived, skip
1377   }
1378 
1379   if (k->is_in_error_state()) {
1380     warn_excluded(k, "In error state");
1381     return true;
1382   }
1383   if (k->has_been_redefined()) {
1384     warn_excluded(k, "Has been redefined");
1385     return true;
1386   }
1387   if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
1388     // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
1389     // agent during dump time).
1390     warn_excluded(k, "Unsupported location");
1391     return true;
1392   }
1393   if (k->signers() != NULL) {
1394     // We cannot include signed classes in the archive because the certificates
1395     // used during dump time may be different than those used during
1396     // runtime (due to expiration, etc).
1397     warn_excluded(k, "Signed JAR");
1398     return true;
1399   }
1400   if (is_jfr_event_class(k)) {
1401     // We cannot include JFR event classes because they need runtime-specific
1402     // instrumentation in order to work with -XX:FlightRecorderOptions=retransform=false.
1403     // There are only a small number of these classes, so it's not worthwhile to
1404     // support them and make CDS more complicated.
1405     warn_excluded(k, "JFR event class");
1406     return true;
1407   }


1420       warn_excluded(k, "Failed verification");
1421     } else {
1422       warn_excluded(k, "Not linked");
1423     }
1424     return true;
1425   }
1426   if (k->major_version() < 50 /*JAVA_6_VERSION*/) {
1427     ResourceMark rm;
1428     log_warning(cds)("Pre JDK 6 class not supported by CDS: %u.%u %s",
1429                      k->major_version(),  k->minor_version(), k->name()->as_C_string());
1430     return true;
1431   }
1432 
1433   InstanceKlass* super = k->java_super();
1434   if (super != NULL && should_be_excluded(super)) {
1435     ResourceMark rm;
1436     log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
1437     return true;
1438   }
1439 
1440   if (k->is_hidden() && !is_registered_lambda_proxy_class(k)) {
1441     warn_excluded(k, "Hidden class");
1442     return true;
1443   }
1444 
1445   Array<InstanceKlass*>* interfaces = k->local_interfaces();
1446   int len = interfaces->length();
1447   for (int i = 0; i < len; i++) {
1448     InstanceKlass* intf = interfaces->at(i);
1449     if (should_be_excluded(intf)) {
1450       log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
1451       return true;
1452     }
1453   }
1454 
1455   return false;
1456 }
1457 
1458 // k is a class before relocating by ArchiveCompactor
1459 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
1460   ResourceMark rm;
1461   const char* name = k->name()->as_C_string();
1462   DumpTimeSharedClassInfo* info = _dumptime_table->get(k);
1463   assert(_no_class_loading_should_happen, "class loading must be disabled");
1464   guarantee(info != NULL, "Class %s must be entered into _dumptime_table", name);
1465   guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
1466   if (is_builtin(k)) {
1467     if (k->is_hidden()) {
1468       assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name);
1469     }
1470     guarantee(!k->is_shared_unregistered_class(),
1471               "Class loader type must be set for BUILTIN class %s", name);
1472 
1473   } else {
1474     guarantee(k->is_shared_unregistered_class(),
1475               "Class loader type must not be set for UNREGISTERED class %s", name);
1476   }
1477 }
1478 
1479 class ExcludeDumpTimeSharedClasses : StackObj {
1480 public:
1481   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1482     if (SystemDictionaryShared::should_be_excluded(k)) {
1483       info.set_excluded();
1484     }
1485     return true; // keep on iterating
1486   }
1487 };
1488 
1489 void SystemDictionaryShared::check_excluded_classes() {
1490   ExcludeDumpTimeSharedClasses excl;
1491   _dumptime_table->iterate(&excl);
1492   _dumptime_table->update_counts();


1568   }
1569   GrowableArray<DTVerifierConstraint>* vc_array = _verifier_constraints;
1570   for (int i = 0; i < vc_array->length(); i++) {
1571     DTVerifierConstraint* p = vc_array->adr_at(i);
1572     if (name == p->_name && from_name == p->_from_name) {
1573       return;
1574     }
1575   }
1576   DTVerifierConstraint cons(name, from_name);
1577   vc_array->append(cons);
1578 
1579   GrowableArray<char>* vcflags_array = _verifier_constraint_flags;
1580   char c = 0;
1581   c |= from_field_is_protected ? SystemDictionaryShared::FROM_FIELD_IS_PROTECTED : 0;
1582   c |= from_is_array           ? SystemDictionaryShared::FROM_IS_ARRAY           : 0;
1583   c |= from_is_object          ? SystemDictionaryShared::FROM_IS_OBJECT          : 0;
1584   vcflags_array->append(c);
1585 
1586   if (log_is_enabled(Trace, cds, verification)) {
1587     ResourceMark rm;
1588     log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s [0x%x] array len %d flags len %d",
1589                                  k->external_name(), from_name->as_klass_external_name(),
1590                                  name->as_klass_external_name(), c, vc_array->length(), vcflags_array->length());
1591   }
1592 }
1593 
1594 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
1595                                                     InstanceKlass* lambda_ik,
1596                                                     Symbol* invoked_name,
1597                                                     Symbol* invoked_type,
1598                                                     Symbol* method_type,
1599                                                     Method* member_method,
1600                                                     Symbol* instantiated_method_type) {
1601 
1602   assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
1603   assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
1604 
1605   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1606 
1607   lambda_ik->assign_class_loader_type();
1608 
1609   DumpTimeSharedClassInfo* info = _dumptime_table->get(lambda_ik);
1610   if (info != NULL) {
1611     // Set _is_archived_lambda_proxy in DumpTimeSharedClassInfo so that the lambda_ik
1612     // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
1613     info->_is_archived_lambda_proxy = true;
1614   }
1615 
1616   LambdaProxyClassKey key(caller_ik,
1617                           invoked_name,
1618                           invoked_type,
1619                           method_type,
1620                           member_method,
1621                           instantiated_method_type);
1622   add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
1623 }
1624 
1625 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
1626                                                                      Symbol* invoked_name,
1627                                                                      Symbol* invoked_type,
1628                                                                      Symbol* method_type,
1629                                                                      Method* member_method,
1630                                                                      Symbol* instantiated_method_type) {
1631   MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
1632   LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
1633                           method_type, member_method, instantiated_method_type);
1634   const RunTimeLambdaProxyClassInfo* info = _lambda_proxy_class_dictionary.lookup(&key, key.hash(), 0);
1635   InstanceKlass* proxy_klass = NULL;
1636   if (info != NULL) {
1637     InstanceKlass* curr_klass = info->proxy_klass();
1638     InstanceKlass* prev_klass = curr_klass;
1639     if (curr_klass->lambda_proxy_is_available()) {
1640       while (curr_klass->next_link() != NULL) {
1641         prev_klass = curr_klass;
1642         curr_klass = InstanceKlass::cast(curr_klass->next_link());
1643       }
1644       assert(curr_klass->is_hidden(), "must be");
1645       assert(curr_klass->lambda_proxy_is_available(), "must be");
1646 
1647       prev_klass->set_next_link(NULL);
1648       proxy_klass = curr_klass;
1649       proxy_klass->clear_lambda_proxy_is_available();
1650       if (log_is_enabled(Debug, cds)) {
1651         ResourceMark rm;
1652         log_debug(cds)("Loaded lambda proxy: %s", proxy_klass->external_name());
1653       }
1654     } else {
1655       if (log_is_enabled(Debug, cds)) {
1656         ResourceMark rm;
1657         log_debug(cds)("Used all archived lambda proxy classes for: %s %s%s",
1658                        caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
1659       }
1660     }
1661   }
1662   return proxy_klass;
1663 }
1664 
1665 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
1666   assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only");
1667   RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(lambda_ik);
1668   return record->nest_host();
1669 }
1670 
1671 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
1672                                                                          InstanceKlass* caller_ik,
1673                                                                          bool initialize, TRAPS) {
1674   Handle class_loader(THREAD, caller_ik->class_loader());
1675   Handle protection_domain;
1676   PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, caller_ik->name());
1677   if (caller_ik->class_loader() != NULL) {
1678     protection_domain = SystemDictionaryShared::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
1679   }
1680 
1681   InstanceKlass* shared_nest_host = get_shared_nest_host(lambda_ik);
1682   assert(shared_nest_host != NULL, "unexpected NULL _nest_host");
1683 
1684   InstanceKlass* loaded_lambda =
1685     SystemDictionary::load_shared_lambda_proxy_class(lambda_ik, class_loader, protection_domain, pkg_entry, CHECK_NULL);
1686 
1687   // Ensures the nest host is the same as the lambda proxy's
1688   // nest host recorded at dump time.
1689   InstanceKlass* nest_host = caller_ik->nest_host(THREAD);
1690   assert(nest_host == shared_nest_host, "mismatched nest host");
1691 
1692   EventClassLoad class_load_start_event;
1693   {
1694     MutexLocker mu_r(THREAD, Compile_lock);
1695 
1696     // Add to class hierarchy, initialize vtables, and do possible
1697     // deoptimizations.
1698     SystemDictionary::add_to_hierarchy(loaded_lambda, CHECK_NULL); // No exception, but can block
1699     // But, do not add to dictionary.
1700   }
1701   loaded_lambda->link_class(CHECK_NULL);
1702   // notify jvmti
1703   if (JvmtiExport::should_post_class_load()) {
1704       assert(THREAD->is_Java_thread(), "thread->is_Java_thread()");
1705       JvmtiExport::post_class_load((JavaThread *) THREAD, loaded_lambda);
1706   }
1707   if (class_load_start_event.should_commit()) {
1708     SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
1709   }
1710 
1711   if (initialize) {
1712     loaded_lambda->initialize(CHECK_NULL);
1713   }
1714 
1715   return loaded_lambda;
1716 }
1717 
1718 static char get_loader_type_by(oop  loader) {
1719   assert(SystemDictionary::is_builtin_class_loader(loader), "Must be built-in loader");
1720   if (SystemDictionary::is_boot_class_loader(loader)) {
1721     return (char)ClassLoader::BOOT_LOADER;
1722   } else if (SystemDictionary::is_platform_class_loader(loader)) {
1723     return (char)ClassLoader::PLATFORM_LOADER;
1724   } else {
1725     assert(SystemDictionary::is_system_class_loader(loader), "Class loader mismatch");
1726     return (char)ClassLoader::APP_LOADER;
1727   }
1728 }
1729 
1730 static oop get_class_loader_by(char type) {
1731   if (type == (char)ClassLoader::BOOT_LOADER) {
1732     return (oop)NULL;
1733   } else if (type == (char)ClassLoader::PLATFORM_LOADER) {
1734     return SystemDictionary::java_platform_loader();
1735   } else {


1928     _num_builtin_klasses = 0;
1929     _num_unregistered_klasses = 0;
1930   }
1931 
1932   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1933     if (!info.is_excluded()) {
1934       size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints());
1935       _shared_class_info_size += align_up(byte_size, BytesPerWord);
1936     }
1937     return true; // keep on iterating
1938   }
1939 
1940   size_t total() {
1941     return _shared_class_info_size;
1942   }
1943 };
1944 
1945 size_t SystemDictionaryShared::estimate_size_for_archive() {
1946   EstimateSizeForArchive est;
1947   _dumptime_table->iterate(&est);
1948   size_t total_size = est.total() +
1949     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1950     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1951   if (_dumptime_lambda_proxy_class_dictionary != NULL) {
1952     total_size +=
1953       (sizeof(RunTimeLambdaProxyClassInfo) * _dumptime_lambda_proxy_class_dictionary->_count) +
1954       CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1955   } else {
1956     total_size += CompactHashtableWriter::estimate_size(0);
1957   }
1958   return total_size;
1959 }
1960 
1961 class CopyLambdaProxyClassInfoToArchive : StackObj {
1962   CompactHashtableWriter* _writer;
1963 public:
1964   CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1965     : _writer(writer) {}
1966   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1967     if (SystemDictionaryShared::is_excluded_class(info._proxy_klass->at(0))) {
1968       return true;
1969     }
1970     ResourceMark rm;
1971     log_info(cds,dynamic)("Archiving hidden %s", info._proxy_klass->at(0)->external_name());
1972     size_t byte_size = sizeof(RunTimeLambdaProxyClassInfo);
1973     RunTimeLambdaProxyClassInfo* runtime_info =
1974         (RunTimeLambdaProxyClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size);
1975     runtime_info->init(key, info);
1976     unsigned int hash = runtime_info->hash(); // Fields in runtime_info->_key already point to target space.
1977     u4 delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(runtime_info));
1978     _writer->add(hash, delta);
1979     return true;
1980   }
1981 };
1982 
1983 class AdjustLambdaProxyClassInfo : StackObj {
1984 public:
1985   AdjustLambdaProxyClassInfo() {}
1986   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1987     if (SystemDictionaryShared::is_excluded_class(info._proxy_klass->at(0))) {
1988       return true;
1989     }
1990     int len = info._proxy_klass->length();
1991     if (len > 1) {
1992       for (int i = 0; i < len-1; i++) {
1993         InstanceKlass* ok0 = info._proxy_klass->at(i+0); // this is original klass
1994         InstanceKlass* ok1 = info._proxy_klass->at(i+1); // this is original klass
1995         InstanceKlass* bk0 = DynamicArchive::original_to_buffer(ok0);
1996         InstanceKlass* bk1 = DynamicArchive::original_to_buffer(ok1);
1997         assert(bk0->next_link() == 0, "must be called after Klass::remove_unshareable_info()");
1998         assert(bk1->next_link() == 0, "must be called after Klass::remove_unshareable_info()");
1999         bk0->set_next_link(bk1);
2000         bk1->set_lambda_proxy_is_available();
2001         ArchivePtrMarker::mark_pointer(bk0->next_link_addr());
2002       }
2003     }
2004     DynamicArchive::original_to_buffer(info._proxy_klass->at(0))->set_lambda_proxy_is_available();
2005     return true;
2006   }
2007 };
2008 
2009 class CopySharedClassInfoToArchive : StackObj {
2010   CompactHashtableWriter* _writer;
2011   bool _is_builtin;
2012 public:
2013   CopySharedClassInfoToArchive(CompactHashtableWriter* writer,
2014                                bool is_builtin,
2015                                bool is_static_archive)
2016     : _writer(writer), _is_builtin(is_builtin) {}
2017 
2018   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
2019     if (!info.is_excluded() && info.is_builtin() == _is_builtin) {
2020       size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints());
2021       RunTimeSharedClassInfo* record;
2022       record = (RunTimeSharedClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size);
2023       record->init(info);
2024 
2025       unsigned int hash;
2026       Symbol* name = info._klass->name();
2027       if (DynamicDumpSharedSpaces) {
2028         name = DynamicArchive::original_to_target(name);
2029       }
2030       hash = SystemDictionaryShared::hash_for_shared_dictionary(name);
2031       u4 delta;
2032       if (DynamicDumpSharedSpaces) {
2033         delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(record));
2034       } else {
2035         delta = MetaspaceShared::object_delta_u4(record);
2036       }
2037       if (_is_builtin && info._klass->is_hidden()) {
2038         // skip
2039       } else {
2040         _writer->add(hash, delta);
2041       }
2042       if (log_is_enabled(Trace, cds, hashtables)) {
2043         ResourceMark rm;
2044         log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
2045       }
2046 
2047       // Save this for quick runtime lookup of InstanceKlass* -> RunTimeSharedClassInfo*
2048       RunTimeSharedClassInfo::set_for(info._klass, record);
2049     }
2050     return true; // keep on iterating
2051   }
2052 };
2053 
2054 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
2055   CompactHashtableStats stats;
2056   dictionary->reset();
2057   CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
2058   CopyLambdaProxyClassInfoToArchive copy(&writer);
2059   _dumptime_lambda_proxy_class_dictionary->iterate(&copy);
2060   writer.dump(dictionary, "lambda proxy class dictionary");
2061 }
2062 
2063 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
2064                                               bool is_builtin,
2065                                               bool is_static_archive) {
2066   CompactHashtableStats stats;
2067   dictionary->reset();
2068   CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
2069   CopySharedClassInfoToArchive copy(&writer, is_builtin, is_static_archive);
2070   _dumptime_table->iterate(&copy);
2071   writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
2072 }
2073 
2074 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
2075   if (is_static_archive) {
2076     write_dictionary(&_builtin_dictionary, true);
2077     write_dictionary(&_unregistered_dictionary, false);
2078   } else {
2079     write_dictionary(&_dynamic_builtin_dictionary, true);
2080     write_dictionary(&_dynamic_unregistered_dictionary, false);
2081   }
2082   if (_dumptime_lambda_proxy_class_dictionary != NULL) {
2083     write_lambda_proxy_class_dictionary(&_lambda_proxy_class_dictionary);
2084   }
2085 }
2086 
2087 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
2088   if (_dumptime_lambda_proxy_class_dictionary != NULL) {
2089     AdjustLambdaProxyClassInfo adjuster;
2090     _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
2091   }
2092 }
2093 
2094 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
2095                                                           bool is_static_archive) {
2096   if (is_static_archive) {
2097     _builtin_dictionary.serialize_header(soc);
2098     _unregistered_dictionary.serialize_header(soc);
2099   } else {
2100     _dynamic_builtin_dictionary.serialize_header(soc);
2101     _dynamic_unregistered_dictionary.serialize_header(soc);
2102     _lambda_proxy_class_dictionary.serialize_header(soc);
2103   }
2104 }
2105 
2106 void SystemDictionaryShared::serialize_well_known_klasses(SerializeClosure* soc) {
2107   for (int i = FIRST_WKID; i < WKID_LIMIT; i++) {
2108     soc->do_ptr((void**)&_well_known_klasses[i]);
2109   }
2110 }
2111 
2112 const RunTimeSharedClassInfo*
2113 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
2114   if (!UseSharedSpaces || !name->is_shared()) {
2115     // The names of all shared classes must also be a shared Symbol.
2116     return NULL;
2117   }
2118 
2119   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(name);
2120   const RunTimeSharedClassInfo* record = NULL;
2121   if (!MetaspaceShared::is_shared_dynamic(name)) {
2122     // The names of all shared classes in the static dict must also be in the
2123     // static archive
2124     record = static_dict->lookup(name, hash, 0);
2125   }
2126 
2127   if (record == NULL && DynamicArchive::is_mapped()) {
2128     record = dynamic_dict->lookup(name, hash, 0);
2129   }
2130 
2131   return record;
2132 }
2133 
2134 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) {
2135   const RunTimeSharedClassInfo* record = find_record(&_builtin_dictionary, &_dynamic_builtin_dictionary, name);
2136   if (record != NULL) {
2137     assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
2138     return record->_klass;
2139   } else {
2140     return NULL;
2141   }
2142 }
2143 
2144 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
2145   assert(DumpSharedSpaces, "supported only when dumping");
2146   DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k);
2147   info->_id = id;
2148 }
2149 
2150 class SharedDictionaryPrinter : StackObj {
2151   outputStream* _st;
2152   int _index;
2153 public:
2154   SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
2155 
2156   void do_value(const RunTimeSharedClassInfo* record) {
2157     ResourceMark rm;
2158     _st->print_cr("%4d:  %s", (_index++), record->_klass->external_name());
2159   }
2160 };
2161 
2162 class SharedLambdaDictionaryPrinter : StackObj {
2163   outputStream* _st;
2164   int _index;
2165 public:
2166   SharedLambdaDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
2167 
2168   void do_value(const RunTimeLambdaProxyClassInfo* record) {
2169     ResourceMark rm;
2170     _st->print_cr("%4d:  %s", (_index++), record->proxy_klass()->external_name());
2171     Klass* k = record->proxy_klass()->next_link();
2172     while (k != NULL) {
2173       _st->print_cr("%4d:  %s", (_index++), k->external_name());
2174       k = k->next_link();
2175     }
2176   }
2177 };
2178 
2179 void SystemDictionaryShared::print_on(outputStream* st) {
2180   if (UseSharedSpaces) {
2181     st->print_cr("Shared Dictionary");
2182     SharedDictionaryPrinter p(st);
2183     _builtin_dictionary.iterate(&p);
2184     _unregistered_dictionary.iterate(&p);
2185     if (DynamicArchive::is_mapped()) {
2186       _dynamic_builtin_dictionary.iterate(&p);
2187       _unregistered_dictionary.iterate(&p);
2188       if (!_lambda_proxy_class_dictionary.empty()) {
2189         st->print_cr("Shared Lambda Dictionary");
2190         SharedLambdaDictionaryPrinter ldp(st);
2191         _lambda_proxy_class_dictionary.iterate(&ldp);
2192       }
2193     }
2194   }
2195 }
2196 
2197 void SystemDictionaryShared::print_table_statistics(outputStream* st) {
2198   if (UseSharedSpaces) {
2199     _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
2200     _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
2201     if (DynamicArchive::is_mapped()) {
2202       _dynamic_builtin_dictionary.print_table_statistics(st, "Dynamic Builtin Shared Dictionary");
2203       _dynamic_unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
2204       _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");
2205     }
2206   }
2207 }
2208 
2209 bool SystemDictionaryShared::empty_dumptime_table() {
2210   if (_dumptime_table == NULL) {
2211     return true;
2212   }
2213   _dumptime_table->update_counts();
2214   if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){
2215     return true;
2216   }
2217   return false;
2218 }
2219 
2220 #if INCLUDE_CDS_JAVA_HEAP
2221 
2222 class ArchivedMirrorPatcher {
2223   static void update(Klass* k) {
2224     if (k->has_raw_archived_mirror()) {


< prev index next >