1 /*
   2  * Copyright (c) 2014, 2020, 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/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 OopHandle SystemDictionaryShared::_shared_protection_domains;
  66 OopHandle SystemDictionaryShared::_shared_jar_urls;
  67 OopHandle SystemDictionaryShared::_shared_jar_manifests;
  68 DEBUG_ONLY(bool SystemDictionaryShared::_no_class_loading_should_happen = false;)
  69 bool SystemDictionaryShared::_dump_in_progress = false;
  70 
  71 class DumpTimeSharedClassInfo: public CHeapObj<mtClass> {
  72   bool                         _excluded;
  73 public:
  74   struct DTLoaderConstraint {
  75     Symbol* _name;
  76     char _loader_type1;
  77     char _loader_type2;
  78     DTLoaderConstraint(Symbol* name, char l1, char l2) : _name(name), _loader_type1(l1), _loader_type2(l2) {
  79       _name->increment_refcount();
  80     }
  81     DTLoaderConstraint() : _name(NULL), _loader_type1('0'), _loader_type2('0') {}
  82     bool equals(const DTLoaderConstraint& t) {
  83       return t._name == _name &&
  84              ((t._loader_type1 == _loader_type1 && t._loader_type2 == _loader_type2) ||
  85               (t._loader_type2 == _loader_type1 && t._loader_type1 == _loader_type2));
  86     }
  87   };
  88 
  89   struct DTVerifierConstraint {
  90     Symbol* _name;
  91     Symbol* _from_name;
  92     DTVerifierConstraint() : _name(NULL), _from_name(NULL) {}
  93     DTVerifierConstraint(Symbol* n, Symbol* fn) : _name(n), _from_name(fn) {
  94       _name->increment_refcount();
  95       _from_name->increment_refcount();
  96     }
  97   };
  98 
  99   InstanceKlass*               _klass;
 100   bool                         _failed_verification;
 101   bool                         _is_archived_lambda_proxy;
 102   int                          _id;
 103   int                          _clsfile_size;
 104   int                          _clsfile_crc32;
 105   GrowableArray<DTVerifierConstraint>* _verifier_constraints;
 106   GrowableArray<char>*                 _verifier_constraint_flags;
 107   GrowableArray<DTLoaderConstraint>* _loader_constraints;
 108 
 109   DumpTimeSharedClassInfo() {
 110     _klass = NULL;
 111     _failed_verification = false;
 112     _is_archived_lambda_proxy = false;
 113     _id = -1;
 114     _clsfile_size = -1;
 115     _clsfile_crc32 = -1;
 116     _excluded = false;
 117     _verifier_constraints = NULL;
 118     _verifier_constraint_flags = NULL;
 119     _loader_constraints = NULL;
 120   }
 121 
 122   void add_verification_constraint(InstanceKlass* k, Symbol* name,
 123          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object);
 124   void record_linking_constraint(Symbol* name, Handle loader1, Handle loader2);
 125 
 126   bool is_builtin() {
 127     return SystemDictionaryShared::is_builtin(_klass);
 128   }
 129 
 130   int num_verifier_constraints() {
 131     if (_verifier_constraint_flags != NULL) {
 132       return _verifier_constraint_flags->length();
 133     } else {
 134       return 0;
 135     }
 136   }
 137 
 138   int num_loader_constraints() {
 139     if (_loader_constraints != NULL) {
 140       return _loader_constraints->length();
 141     } else {
 142       return 0;
 143     }
 144   }
 145 
 146   void metaspace_pointers_do(MetaspaceClosure* it) {
 147     it->push(&_klass);
 148     if (_verifier_constraints != NULL) {
 149       for (int i = 0; i < _verifier_constraints->length(); i++) {
 150         DTVerifierConstraint* cons = _verifier_constraints->adr_at(i);
 151         it->push(&cons->_name);
 152         it->push(&cons->_from_name);
 153       }
 154     }
 155     if (_loader_constraints != NULL) {
 156       for (int i = 0; i < _loader_constraints->length(); i++) {
 157         DTLoaderConstraint* lc = _loader_constraints->adr_at(i);
 158         it->push(&lc->_name);
 159       }
 160     }
 161   }
 162 
 163   void set_excluded() {
 164     _excluded = true;
 165   }
 166 
 167   bool is_excluded() {
 168     // _klass may become NULL due to DynamicArchiveBuilder::set_to_null
 169     return _excluded || _failed_verification || _klass == NULL;
 170   }
 171 
 172   void set_failed_verification() {
 173     _failed_verification = true;
 174   }
 175 
 176   bool failed_verification() {
 177     return _failed_verification;
 178   }
 179 };
 180 
 181 inline unsigned DumpTimeSharedClassTable_hash(InstanceKlass* const& k) {
 182   if (DumpSharedSpaces) {
 183     // Deterministic archive contents
 184     uintx delta = k->name() - MetaspaceShared::symbol_rs_base();
 185     return primitive_hash<uintx>(delta);
 186   } else {
 187     // Deterministic archive is not possible because classes can be loaded
 188     // in multiple threads.
 189     return primitive_hash<InstanceKlass*>(k);
 190   }
 191 }
 192 
 193 class DumpTimeSharedClassTable: public ResourceHashtable<
 194   InstanceKlass*,
 195   DumpTimeSharedClassInfo,
 196   &DumpTimeSharedClassTable_hash,
 197   primitive_equals<InstanceKlass*>,
 198   15889, // prime number
 199   ResourceObj::C_HEAP>
 200 {
 201   int _builtin_count;
 202   int _unregistered_count;
 203 public:
 204   DumpTimeSharedClassInfo* find_or_allocate_info_for(InstanceKlass* k, bool dump_in_progress) {
 205     bool created = false;
 206     DumpTimeSharedClassInfo* p;
 207     if (!dump_in_progress) {
 208       p = put_if_absent(k, &created);
 209     } else {
 210       p = get(k);
 211     }
 212     if (created) {
 213       assert(!SystemDictionaryShared::no_class_loading_should_happen(),
 214              "no new classes can be loaded while dumping archive");
 215       p->_klass = k;
 216     } else {
 217       if (!dump_in_progress) {
 218         assert(p->_klass == k, "Sanity");
 219       }
 220     }
 221     return p;
 222   }
 223 
 224   class CountClassByCategory : StackObj {
 225     DumpTimeSharedClassTable* _table;
 226   public:
 227     CountClassByCategory(DumpTimeSharedClassTable* table) : _table(table) {}
 228     bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
 229       if (!info.is_excluded()) {
 230         if (info.is_builtin()) {
 231           ++ _table->_builtin_count;
 232         } else {
 233           ++ _table->_unregistered_count;
 234         }
 235       }
 236       return true; // keep on iterating
 237     }
 238   };
 239 
 240   void update_counts() {
 241     _builtin_count = 0;
 242     _unregistered_count = 0;
 243     CountClassByCategory counter(this);
 244     iterate(&counter);
 245   }
 246 
 247   int count_of(bool is_builtin) const {
 248     if (is_builtin) {
 249       return _builtin_count;
 250     } else {
 251       return _unregistered_count;
 252     }
 253   }
 254 };
 255 
 256 class LambdaProxyClassKey {
 257   template <typename T> static void original_to_target(T& field) {
 258     if (field != NULL) {
 259       field = DynamicArchive::original_to_target(field);
 260       ArchivePtrMarker::mark_pointer(&field);
 261     }
 262   }
 263 
 264   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 
 271 public:
 272   LambdaProxyClassKey(InstanceKlass* caller_ik,
 273                       Symbol*        invoked_name,
 274                       Symbol*        invoked_type,
 275                       Symbol*        method_type,
 276                       Method*        member_method,
 277                       Symbol*        instantiated_method_type) :
 278     _caller_ik(caller_ik),
 279     _invoked_name(invoked_name),
 280     _invoked_type(invoked_type),
 281     _method_type(method_type),
 282     _member_method(member_method),
 283     _instantiated_method_type(instantiated_method_type) {}
 284 
 285   void original_to_target() {
 286     original_to_target(_caller_ik);
 287     original_to_target(_instantiated_method_type);
 288     original_to_target(_invoked_name);
 289     original_to_target(_invoked_type);
 290     original_to_target(_member_method);
 291     original_to_target(_method_type);
 292   }
 293 
 294   bool equals(LambdaProxyClassKey const& other) const {
 295     return _caller_ik == other._caller_ik &&
 296            _invoked_name == other._invoked_name &&
 297            _invoked_type == other._invoked_type &&
 298            _method_type == other._method_type &&
 299            _member_method == other._member_method &&
 300            _instantiated_method_type == other._instantiated_method_type;
 301   }
 302 
 303   unsigned int hash() const {
 304     return SystemDictionaryShared::hash_for_shared_dictionary(_caller_ik) +
 305            SystemDictionaryShared::hash_for_shared_dictionary(_invoked_name) +
 306            SystemDictionaryShared::hash_for_shared_dictionary(_invoked_type) +
 307            SystemDictionaryShared::hash_for_shared_dictionary(_method_type) +
 308            SystemDictionaryShared::hash_for_shared_dictionary(_instantiated_method_type);
 309   }
 310 
 311   unsigned int dumptime_hash() const {
 312     return primitive_hash<InstanceKlass*>(_caller_ik) +
 313            primitive_hash<Symbol*>(_invoked_name) +
 314            primitive_hash<Symbol*>(_invoked_type) +
 315            primitive_hash<Symbol*>(_method_type) +
 316            primitive_hash<Symbol*>(_instantiated_method_type);
 317   }
 318 
 319   static inline unsigned int DUMPTIME_HASH(LambdaProxyClassKey const& key) {
 320     return (key.dumptime_hash());
 321   }
 322 
 323   static inline bool DUMPTIME_EQUALS(
 324       LambdaProxyClassKey const& k1, LambdaProxyClassKey const& k2) {
 325     return (k1.equals(k2));
 326   }
 327 };
 328 
 329 
 330 class DumpTimeLambdaProxyClassInfo {
 331 public:
 332   GrowableArray<InstanceKlass*>* _proxy_klasses;
 333   DumpTimeLambdaProxyClassInfo() : _proxy_klasses(NULL) {}
 334   void add_proxy_klass(InstanceKlass* proxy_klass) {
 335     if (_proxy_klasses == NULL) {
 336       _proxy_klasses = new (ResourceObj::C_HEAP, mtClassShared)GrowableArray<InstanceKlass*>(5, mtClassShared);
 337     }
 338     assert(_proxy_klasses != NULL, "sanity");
 339     _proxy_klasses->append(proxy_klass);
 340   }
 341 };
 342 
 343 class RunTimeLambdaProxyClassInfo {
 344   LambdaProxyClassKey _key;
 345   InstanceKlass* _proxy_klass_head;
 346 public:
 347   RunTimeLambdaProxyClassInfo(LambdaProxyClassKey key, InstanceKlass* proxy_klass) :
 348     _key(key), _proxy_klass_head(proxy_klass) {}
 349 
 350   InstanceKlass* proxy_klass_head() const { return _proxy_klass_head; }
 351 
 352   // Used by LambdaProxyClassDictionary to implement OffsetCompactHashtable::EQUALS
 353   static inline bool EQUALS(
 354        const RunTimeLambdaProxyClassInfo* value, LambdaProxyClassKey* key, int len_unused) {
 355     return (value->_key.equals(*key));
 356   }
 357   void init(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
 358     _key = key;
 359     _key.original_to_target();
 360     _proxy_klass_head = DynamicArchive::original_to_target(info._proxy_klasses->at(0));
 361     ArchivePtrMarker::mark_pointer(&_proxy_klass_head);
 362   }
 363 
 364   unsigned int hash() const {
 365     return _key.hash();
 366   }
 367 };
 368 
 369 class LambdaProxyClassDictionary : public OffsetCompactHashtable<
 370   LambdaProxyClassKey*,
 371   const RunTimeLambdaProxyClassInfo*,
 372   RunTimeLambdaProxyClassInfo::EQUALS> {};
 373 
 374 LambdaProxyClassDictionary _lambda_proxy_class_dictionary;
 375 
 376 class DumpTimeLambdaProxyClassDictionary
 377   : public ResourceHashtable<LambdaProxyClassKey,
 378                              DumpTimeLambdaProxyClassInfo,
 379                              LambdaProxyClassKey::DUMPTIME_HASH,
 380                              LambdaProxyClassKey::DUMPTIME_EQUALS,
 381                              137, // prime number
 382                              ResourceObj::C_HEAP> {
 383 public:
 384   int _count;
 385 };
 386 
 387 DumpTimeLambdaProxyClassDictionary* _dumptime_lambda_proxy_class_dictionary = NULL;
 388 
 389 static void add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey key,
 390                                                            InstanceKlass* proxy_klass) {
 391   assert(DumpTimeTable_lock->owned_by_self(), "sanity");
 392   if (_dumptime_lambda_proxy_class_dictionary == NULL) {
 393     _dumptime_lambda_proxy_class_dictionary =
 394       new (ResourceObj::C_HEAP, mtClass)DumpTimeLambdaProxyClassDictionary();
 395   }
 396   DumpTimeLambdaProxyClassInfo* lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key);
 397   if (lambda_info == NULL) {
 398     DumpTimeLambdaProxyClassInfo info;
 399     info.add_proxy_klass(proxy_klass);
 400     _dumptime_lambda_proxy_class_dictionary->put(key, info);
 401     //lambda_info = _dumptime_lambda_proxy_class_dictionary->get(key);
 402     //assert(lambda_info->_proxy_klass == proxy_klass, "must be"); // debug only -- remove
 403     ++_dumptime_lambda_proxy_class_dictionary->_count;
 404   } else {
 405     lambda_info->add_proxy_klass(proxy_klass);
 406   }
 407 }
 408 
 409 class RunTimeSharedClassInfo {
 410 public:
 411   struct CrcInfo {
 412     int _clsfile_size;
 413     int _clsfile_crc32;
 414   };
 415 
 416   // This is different than  DumpTimeSharedClassInfo::DTVerifierConstraint. We use
 417   // u4 instead of Symbol* to save space on 64-bit CPU.
 418   struct RTVerifierConstraint {
 419     u4 _name;
 420     u4 _from_name;
 421     Symbol* name() { return (Symbol*)(SharedBaseAddress + _name);}
 422     Symbol* from_name() { return (Symbol*)(SharedBaseAddress + _from_name); }
 423   };
 424 
 425   struct RTLoaderConstraint {
 426     u4   _name;
 427     char _loader_type1;
 428     char _loader_type2;
 429     Symbol* constraint_name() {
 430       return (Symbol*)(SharedBaseAddress + _name);
 431     }
 432   };
 433 
 434   InstanceKlass* _klass;
 435   int _num_verifier_constraints;
 436   int _num_loader_constraints;
 437 
 438   // optional CrcInfo              _crc;  (only for UNREGISTERED classes)
 439   // optional InstanceKlass*       _nest_host
 440   // optional RTLoaderConstraint   _loader_constraint_types[_num_loader_constraints]
 441   // optional RTVerifierConstraint _verifier_constraints[_num_verifier_constraints]
 442   // optional char                 _verifier_constraint_flags[_num_verifier_constraints]
 443 
 444 private:
 445   static size_t header_size_size() {
 446     return sizeof(RunTimeSharedClassInfo);
 447   }
 448   static size_t crc_size(InstanceKlass* klass) {
 449     if (!SystemDictionaryShared::is_builtin(klass)) {
 450       return sizeof(CrcInfo);
 451     } else {
 452       return 0;
 453     }
 454   }
 455   static size_t verifier_constraints_size(int num_verifier_constraints) {
 456     return sizeof(RTVerifierConstraint) * num_verifier_constraints;
 457   }
 458   static size_t verifier_constraint_flags_size(int num_verifier_constraints) {
 459     return sizeof(char) * num_verifier_constraints;
 460   }
 461   static size_t loader_constraints_size(int num_loader_constraints) {
 462     return sizeof(RTLoaderConstraint) * num_loader_constraints;
 463   }
 464   static size_t nest_host_size(InstanceKlass* klass) {
 465     if (klass->is_hidden()) {
 466       return sizeof(InstanceKlass*);
 467     } else {
 468       return 0;
 469     }
 470   }
 471 
 472 public:
 473   static size_t byte_size(InstanceKlass* klass, int num_verifier_constraints, int num_loader_constraints) {
 474     return header_size_size() +
 475            crc_size(klass) +
 476            nest_host_size(klass) +
 477            loader_constraints_size(num_loader_constraints) +
 478            verifier_constraints_size(num_verifier_constraints) +
 479            verifier_constraint_flags_size(num_verifier_constraints);
 480   }
 481 
 482 private:
 483   size_t crc_offset() const {
 484     return header_size_size();
 485   }
 486 
 487   size_t nest_host_offset() const {
 488       return crc_offset() + crc_size(_klass);
 489   }
 490 
 491   size_t loader_constraints_offset() const  {
 492     return nest_host_offset() + nest_host_size(_klass);
 493   }
 494   size_t verifier_constraints_offset() const {
 495     return loader_constraints_offset() + loader_constraints_size(_num_loader_constraints);
 496   }
 497   size_t verifier_constraint_flags_offset() const {
 498     return verifier_constraints_offset() + verifier_constraints_size(_num_verifier_constraints);
 499   }
 500 
 501   void check_verifier_constraint_offset(int i) const {
 502     assert(0 <= i && i < _num_verifier_constraints, "sanity");
 503   }
 504 
 505   void check_loader_constraint_offset(int i) const {
 506     assert(0 <= i && i < _num_loader_constraints, "sanity");
 507   }
 508 
 509 public:
 510   CrcInfo* crc() const {
 511     assert(crc_size(_klass) > 0, "must be");
 512     return (CrcInfo*)(address(this) + crc_offset());
 513   }
 514   RTVerifierConstraint* verifier_constraints() {
 515     assert(_num_verifier_constraints > 0, "sanity");
 516     return (RTVerifierConstraint*)(address(this) + verifier_constraints_offset());
 517   }
 518   RTVerifierConstraint* verifier_constraint_at(int i) {
 519     check_verifier_constraint_offset(i);
 520     return verifier_constraints() + i;
 521   }
 522 
 523   char* verifier_constraint_flags() {
 524     assert(_num_verifier_constraints > 0, "sanity");
 525     return (char*)(address(this) + verifier_constraint_flags_offset());
 526   }
 527 
 528   InstanceKlass** nest_host_addr() {
 529     assert(_klass->is_hidden(), "sanity");
 530     return (InstanceKlass**)(address(this) + nest_host_offset());
 531   }
 532   InstanceKlass* nest_host() {
 533     return *nest_host_addr();
 534   }
 535   void set_nest_host(InstanceKlass* k) {
 536     *nest_host_addr() = k;
 537     ArchivePtrMarker::mark_pointer((address*)nest_host_addr());
 538   }
 539 
 540   RTLoaderConstraint* loader_constraints() {
 541     assert(_num_loader_constraints > 0, "sanity");
 542     return (RTLoaderConstraint*)(address(this) + loader_constraints_offset());
 543   }
 544 
 545   RTLoaderConstraint* loader_constraint_at(int i) {
 546     check_loader_constraint_offset(i);
 547     return loader_constraints() + i;
 548   }
 549 
 550   static u4 object_delta_u4(Symbol* sym) {
 551     if (DynamicDumpSharedSpaces) {
 552       sym = DynamicArchive::original_to_target(sym);
 553     }
 554     return MetaspaceShared::object_delta_u4(sym);
 555   }
 556 
 557   void init(DumpTimeSharedClassInfo& info) {
 558     _klass = info._klass;
 559     if (!SystemDictionaryShared::is_builtin(_klass)) {
 560       CrcInfo* c = crc();
 561       c->_clsfile_size = info._clsfile_size;
 562       c->_clsfile_crc32 = info._clsfile_crc32;
 563     }
 564     _num_verifier_constraints = info.num_verifier_constraints();
 565     _num_loader_constraints   = info.num_loader_constraints();
 566     int i;
 567     if (_num_verifier_constraints > 0) {
 568       RTVerifierConstraint* vf_constraints = verifier_constraints();
 569       char* flags = verifier_constraint_flags();
 570       for (i = 0; i < _num_verifier_constraints; i++) {
 571         vf_constraints[i]._name      = object_delta_u4(info._verifier_constraints->at(i)._name);
 572         vf_constraints[i]._from_name = object_delta_u4(info._verifier_constraints->at(i)._from_name);
 573       }
 574       for (i = 0; i < _num_verifier_constraints; i++) {
 575         flags[i] = info._verifier_constraint_flags->at(i);
 576       }
 577     }
 578 
 579     if (_num_loader_constraints > 0) {
 580       RTLoaderConstraint* ld_constraints = loader_constraints();
 581       for (i = 0; i < _num_loader_constraints; i++) {
 582         ld_constraints[i]._name = object_delta_u4(info._loader_constraints->at(i)._name);
 583         ld_constraints[i]._loader_type1 = info._loader_constraints->at(i)._loader_type1;
 584         ld_constraints[i]._loader_type2 = info._loader_constraints->at(i)._loader_type2;
 585       }
 586     }
 587     if (DynamicDumpSharedSpaces) {
 588       if (_klass->is_hidden()) {
 589         Thread* THREAD = Thread::current();
 590         InstanceKlass* n_h = _klass->nest_host(THREAD);
 591         n_h = DynamicArchive::original_to_target(n_h);
 592         set_nest_host(n_h);
 593       }
 594       _klass = DynamicArchive::original_to_target(info._klass);
 595     }
 596     ArchivePtrMarker::mark_pointer(&_klass);
 597   }
 598 
 599   bool matches(int clsfile_size, int clsfile_crc32) const {
 600     return crc()->_clsfile_size  == clsfile_size &&
 601            crc()->_clsfile_crc32 == clsfile_crc32;
 602   }
 603 
 604   char verifier_constraint_flag(int i) {
 605     check_verifier_constraint_offset(i);
 606     return verifier_constraint_flags()[i];
 607   }
 608 
 609 private:
 610   // ArchiveBuilder::make_shallow_copy() has reserved a pointer immediately
 611   // before archived InstanceKlasses. We can use this slot to do a quick
 612   // lookup of InstanceKlass* -> RunTimeSharedClassInfo* without
 613   // building a new hashtable.
 614   //
 615   //  info_pointer_addr(klass) --> 0x0100   RunTimeSharedClassInfo*
 616   //  InstanceKlass* klass     --> 0x0108   <C++ vtbl>
 617   //                               0x0110   fields from Klass ...
 618   static RunTimeSharedClassInfo** info_pointer_addr(InstanceKlass* klass) {
 619     return &((RunTimeSharedClassInfo**)klass)[-1];
 620   }
 621 
 622 public:
 623   static RunTimeSharedClassInfo* get_for(InstanceKlass* klass) {
 624     assert(klass->is_shared(), "don't call for non-shared class");
 625     return *info_pointer_addr(klass);
 626   }
 627   static void set_for(InstanceKlass* klass, RunTimeSharedClassInfo* record) {
 628     if (DynamicDumpSharedSpaces) {
 629       klass = DynamicArchive::original_to_buffer(klass);
 630       *info_pointer_addr(klass) = DynamicArchive::buffer_to_target(record);
 631     } else {
 632       *info_pointer_addr(klass) = record;
 633     }
 634 
 635     ArchivePtrMarker::mark_pointer(info_pointer_addr(klass));
 636   }
 637 
 638   // Used by RunTimeSharedDictionary to implement OffsetCompactHashtable::EQUALS
 639   static inline bool EQUALS(
 640        const RunTimeSharedClassInfo* value, Symbol* key, int len_unused) {
 641     return (value->_klass->name() == key);
 642   }
 643 };
 644 
 645 class RunTimeSharedDictionary : public OffsetCompactHashtable<
 646   Symbol*,
 647   const RunTimeSharedClassInfo*,
 648   RunTimeSharedClassInfo::EQUALS> {};
 649 
 650 static DumpTimeSharedClassTable* _dumptime_table = NULL;
 651 // SystemDictionaries in the base layer static archive
 652 static RunTimeSharedDictionary _builtin_dictionary;
 653 static RunTimeSharedDictionary _unregistered_dictionary;
 654 // SystemDictionaries in the top layer dynamic archive
 655 static RunTimeSharedDictionary _dynamic_builtin_dictionary;
 656 static RunTimeSharedDictionary _dynamic_unregistered_dictionary;
 657 
 658 
 659 Handle SystemDictionaryShared::create_jar_manifest(const char* manifest_chars, size_t size, TRAPS) {
 660   typeArrayOop buf = oopFactory::new_byteArray((int)size, CHECK_NH);
 661   typeArrayHandle bufhandle(THREAD, buf);
 662   ArrayAccess<>::arraycopy_from_native(reinterpret_cast<const jbyte*>(manifest_chars),
 663                                          buf, typeArrayOopDesc::element_offset<jbyte>(0), size);
 664   Handle bais = JavaCalls::construct_new_instance(SystemDictionary::ByteArrayInputStream_klass(),
 665                       vmSymbols::byte_array_void_signature(),
 666                       bufhandle, CHECK_NH);
 667   // manifest = new Manifest(ByteArrayInputStream)
 668   Handle manifest = JavaCalls::construct_new_instance(SystemDictionary::Jar_Manifest_klass(),
 669                       vmSymbols::input_stream_void_signature(),
 670                       bais, CHECK_NH);
 671   return manifest;
 672 }
 673 
 674 oop SystemDictionaryShared::shared_protection_domain(int index) {
 675   return ((objArrayOop)_shared_protection_domains.resolve())->obj_at(index);
 676 }
 677 
 678 oop SystemDictionaryShared::shared_jar_url(int index) {
 679   return ((objArrayOop)_shared_jar_urls.resolve())->obj_at(index);
 680 }
 681 
 682 oop SystemDictionaryShared::shared_jar_manifest(int index) {
 683   return ((objArrayOop)_shared_jar_manifests.resolve())->obj_at(index);
 684 }
 685 
 686 Handle SystemDictionaryShared::get_shared_jar_manifest(int shared_path_index, TRAPS) {
 687   Handle manifest ;
 688   if (shared_jar_manifest(shared_path_index) == NULL) {
 689     SharedClassPathEntry* ent = FileMapInfo::shared_path(shared_path_index);
 690     size_t size = (size_t)ent->manifest_size();
 691     if (size == 0) {
 692       return Handle();
 693     }
 694 
 695     // ByteArrayInputStream bais = new ByteArrayInputStream(buf);
 696     const char* src = ent->manifest();
 697     assert(src != NULL, "No Manifest data");
 698     manifest = create_jar_manifest(src, size, THREAD);
 699     atomic_set_shared_jar_manifest(shared_path_index, manifest());
 700   }
 701   manifest = Handle(THREAD, shared_jar_manifest(shared_path_index));
 702   assert(manifest.not_null(), "sanity");
 703   return manifest;
 704 }
 705 
 706 Handle SystemDictionaryShared::get_shared_jar_url(int shared_path_index, TRAPS) {
 707   Handle url_h;
 708   if (shared_jar_url(shared_path_index) == NULL) {
 709     JavaValue result(T_OBJECT);
 710     const char* path = FileMapInfo::shared_path_name(shared_path_index);
 711     Handle path_string = java_lang_String::create_from_str(path, CHECK_(url_h));
 712     Klass* classLoaders_klass =
 713         SystemDictionary::jdk_internal_loader_ClassLoaders_klass();
 714     JavaCalls::call_static(&result, classLoaders_klass,
 715                            vmSymbols::toFileURL_name(),
 716                            vmSymbols::toFileURL_signature(),
 717                            path_string, CHECK_(url_h));
 718 
 719     atomic_set_shared_jar_url(shared_path_index, (oop)result.get_jobject());
 720   }
 721 
 722   url_h = Handle(THREAD, shared_jar_url(shared_path_index));
 723   assert(url_h.not_null(), "sanity");
 724   return url_h;
 725 }
 726 
 727 Handle SystemDictionaryShared::get_package_name(Symbol* class_name, TRAPS) {
 728   ResourceMark rm(THREAD);
 729   Handle pkgname_string;
 730   TempNewSymbol pkg = ClassLoader::package_from_class_name(class_name);
 731   if (pkg != NULL) { // Package prefix found
 732     const char* pkgname = pkg->as_klass_external_name();
 733     pkgname_string = java_lang_String::create_from_str(pkgname,
 734                                                        CHECK_(pkgname_string));
 735   }
 736   return pkgname_string;
 737 }
 738 
 739 // Define Package for shared app classes from JAR file and also checks for
 740 // package sealing (all done in Java code)
 741 // See http://docs.oracle.com/javase/tutorial/deployment/jar/sealman.html
 742 void SystemDictionaryShared::define_shared_package(Symbol*  class_name,
 743                                                    Handle class_loader,
 744                                                    Handle manifest,
 745                                                    Handle url,
 746                                                    TRAPS) {
 747   assert(SystemDictionary::is_system_class_loader(class_loader()), "unexpected class loader");
 748   // get_package_name() returns a NULL handle if the class is in unnamed package
 749   Handle pkgname_string = get_package_name(class_name, CHECK);
 750   if (pkgname_string.not_null()) {
 751     Klass* app_classLoader_klass = SystemDictionary::jdk_internal_loader_ClassLoaders_AppClassLoader_klass();
 752     JavaValue result(T_OBJECT);
 753     JavaCallArguments args(3);
 754     args.set_receiver(class_loader);
 755     args.push_oop(pkgname_string);
 756     args.push_oop(manifest);
 757     args.push_oop(url);
 758     JavaCalls::call_virtual(&result, app_classLoader_klass,
 759                             vmSymbols::defineOrCheckPackage_name(),
 760                             vmSymbols::defineOrCheckPackage_signature(),
 761                             &args,
 762                             CHECK);
 763   }
 764 }
 765 
 766 // Get the ProtectionDomain associated with the CodeSource from the classloader.
 767 Handle SystemDictionaryShared::get_protection_domain_from_classloader(Handle class_loader,
 768                                                                       Handle url, TRAPS) {
 769   // CodeSource cs = new CodeSource(url, null);
 770   Handle cs = JavaCalls::construct_new_instance(SystemDictionary::CodeSource_klass(),
 771                   vmSymbols::url_code_signer_array_void_signature(),
 772                   url, Handle(), CHECK_NH);
 773 
 774   // protection_domain = SecureClassLoader.getProtectionDomain(cs);
 775   Klass* secureClassLoader_klass = SystemDictionary::SecureClassLoader_klass();
 776   JavaValue obj_result(T_OBJECT);
 777   JavaCalls::call_virtual(&obj_result, class_loader, secureClassLoader_klass,
 778                           vmSymbols::getProtectionDomain_name(),
 779                           vmSymbols::getProtectionDomain_signature(),
 780                           cs, CHECK_NH);
 781   return Handle(THREAD, (oop)obj_result.get_jobject());
 782 }
 783 
 784 // Returns the ProtectionDomain associated with the JAR file identified by the url.
 785 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader,
 786                                                             int shared_path_index,
 787                                                             Handle url,
 788                                                             TRAPS) {
 789   Handle protection_domain;
 790   if (shared_protection_domain(shared_path_index) == NULL) {
 791     Handle pd = get_protection_domain_from_classloader(class_loader, url, THREAD);
 792     atomic_set_shared_protection_domain(shared_path_index, pd());
 793   }
 794 
 795   // Acquire from the cache because if another thread beats the current one to
 796   // set the shared protection_domain and the atomic_set fails, the current thread
 797   // needs to get the updated protection_domain from the cache.
 798   protection_domain = Handle(THREAD, shared_protection_domain(shared_path_index));
 799   assert(protection_domain.not_null(), "sanity");
 800   return protection_domain;
 801 }
 802 
 803 // Returns the ProtectionDomain associated with the moduleEntry.
 804 Handle SystemDictionaryShared::get_shared_protection_domain(Handle class_loader,
 805                                                             ModuleEntry* mod, TRAPS) {
 806   ClassLoaderData *loader_data = mod->loader_data();
 807   if (mod->shared_protection_domain() == NULL) {
 808     Symbol* location = mod->location();
 809     if (location != NULL) {
 810       Handle location_string = java_lang_String::create_from_symbol(
 811                                      location, CHECK_NH);
 812       Handle url;
 813       JavaValue result(T_OBJECT);
 814       if (location->starts_with("jrt:/")) {
 815         url = JavaCalls::construct_new_instance(SystemDictionary::URL_klass(),
 816                                                 vmSymbols::string_void_signature(),
 817                                                 location_string, CHECK_NH);
 818       } else {
 819         Klass* classLoaders_klass =
 820           SystemDictionary::jdk_internal_loader_ClassLoaders_klass();
 821         JavaCalls::call_static(&result, classLoaders_klass, vmSymbols::toFileURL_name(),
 822                                vmSymbols::toFileURL_signature(),
 823                                location_string, CHECK_NH);
 824         url = Handle(THREAD, (oop)result.get_jobject());
 825       }
 826 
 827       Handle pd = get_protection_domain_from_classloader(class_loader, url,
 828                                                          CHECK_NH);
 829       mod->set_shared_protection_domain(loader_data, pd);
 830     }
 831   }
 832 
 833   Handle protection_domain(THREAD, mod->shared_protection_domain());
 834   assert(protection_domain.not_null(), "sanity");
 835   return protection_domain;
 836 }
 837 
 838 // Initializes the java.lang.Package and java.security.ProtectionDomain objects associated with
 839 // the given InstanceKlass.
 840 // Returns the ProtectionDomain for the InstanceKlass.
 841 Handle SystemDictionaryShared::init_security_info(Handle class_loader, InstanceKlass* ik, PackageEntry* pkg_entry, TRAPS) {
 842   Handle pd;
 843 
 844   if (ik != NULL) {
 845     int index = ik->shared_classpath_index();
 846     assert(index >= 0, "Sanity");
 847     SharedClassPathEntry* ent = FileMapInfo::shared_path(index);
 848     Symbol* class_name = ik->name();
 849 
 850     if (ent->is_modules_image()) {
 851       // For shared app/platform classes originated from the run-time image:
 852       //   The ProtectionDomains are cached in the corresponding ModuleEntries
 853       //   for fast access by the VM.
 854       // all packages from module image are already created during VM bootstrap in
 855       // Modules::define_module().
 856       assert(pkg_entry != NULL, "archived class in module image cannot be from unnamed package");
 857       ModuleEntry* mod_entry = pkg_entry->module();
 858       pd = get_shared_protection_domain(class_loader, mod_entry, THREAD);
 859     } else {
 860       // For shared app/platform classes originated from JAR files on the class path:
 861       //   Each of the 3 SystemDictionaryShared::_shared_xxx arrays has the same length
 862       //   as the shared classpath table in the shared archive (see
 863       //   FileMap::_shared_path_table in filemap.hpp for details).
 864       //
 865       //   If a shared InstanceKlass k is loaded from the class path, let
 866       //
 867       //     index = k->shared_classpath_index():
 868       //
 869       //   FileMap::_shared_path_table[index] identifies the JAR file that contains k.
 870       //
 871       //   k's protection domain is:
 872       //
 873       //     ProtectionDomain pd = _shared_protection_domains[index];
 874       //
 875       //   and k's Package is initialized using
 876       //
 877       //     manifest = _shared_jar_manifests[index];
 878       //     url = _shared_jar_urls[index];
 879       //     define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
 880       //
 881       //   Note that if an element of these 3 _shared_xxx arrays is NULL, it will be initialized by
 882       //   the corresponding SystemDictionaryShared::get_shared_xxx() function.
 883       Handle manifest = get_shared_jar_manifest(index, CHECK_(pd));
 884       Handle url = get_shared_jar_url(index, CHECK_(pd));
 885       int index_offset = index - ClassLoaderExt::app_class_paths_start_index();
 886       if (index_offset < PackageEntry::max_index_for_defined_in_class_path()) {
 887         if (pkg_entry == NULL || !pkg_entry->is_defined_by_cds_in_class_path(index_offset)) {
 888           // define_shared_package only needs to be called once for each package in a jar specified
 889           // in the shared class path.
 890           define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
 891           if (pkg_entry != NULL) {
 892             pkg_entry->set_defined_by_cds_in_class_path(index_offset);
 893           }
 894         }
 895       } else {
 896         define_shared_package(class_name, class_loader, manifest, url, CHECK_(pd));
 897       }
 898       pd = get_shared_protection_domain(class_loader, index, url, CHECK_(pd));
 899     }
 900   }
 901   return pd;
 902 }
 903 
 904 bool SystemDictionaryShared::is_sharing_possible(ClassLoaderData* loader_data) {
 905   oop class_loader = loader_data->class_loader();
 906   return (class_loader == NULL ||
 907           SystemDictionary::is_system_class_loader(class_loader) ||
 908           SystemDictionary::is_platform_class_loader(class_loader));
 909 }
 910 
 911 bool SystemDictionaryShared::has_platform_or_app_classes() {
 912   if (FileMapInfo::current_info()->has_platform_or_app_classes()) {
 913     return true;
 914   }
 915   if (DynamicArchive::is_mapped() &&
 916       FileMapInfo::dynamic_info()->has_platform_or_app_classes()) {
 917     return true;
 918   }
 919   return false;
 920 }
 921 
 922 // The following stack shows how this code is reached:
 923 //
 924 //   [0] SystemDictionaryShared::find_or_load_shared_class()
 925 //   [1] JVM_FindLoadedClass
 926 //   [2] java.lang.ClassLoader.findLoadedClass0()
 927 //   [3] java.lang.ClassLoader.findLoadedClass()
 928 //   [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull()
 929 //   [5] jdk.internal.loader.BuiltinClassLoader.loadClass()
 930 //   [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or
 931 //       jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass()
 932 //
 933 // AppCDS supports fast class loading for these 2 built-in class loaders:
 934 //    jdk.internal.loader.ClassLoaders$PlatformClassLoader
 935 //    jdk.internal.loader.ClassLoaders$AppClassLoader
 936 // with the following assumptions (based on the JDK core library source code):
 937 //
 938 // [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to
 939 //     load the named class.
 940 // [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name).
 941 // [c] At this point, if we can find the named class inside the
 942 //     shared_dictionary, we can perform further checks (see
 943 //     SystemDictionary::is_shared_class_visible) to ensure that this class
 944 //     was loaded by the same class loader during dump time.
 945 //
 946 // Given these assumptions, we intercept the findLoadedClass() call to invoke
 947 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from
 948 // the archive for the 2 built-in class loaders. This way,
 949 // we can improve start-up because we avoid decoding the classfile,
 950 // and avoid delegating to the parent loader.
 951 //
 952 // NOTE: there's a lot of assumption about the Java code. If any of that change, this
 953 // needs to be redesigned.
 954 
 955 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class(
 956                  Symbol* name, Handle class_loader, TRAPS) {
 957   InstanceKlass* k = NULL;
 958   if (UseSharedSpaces) {
 959     if (!has_platform_or_app_classes()) {
 960       return NULL;
 961     }
 962 
 963     if (SystemDictionary::is_system_class_loader(class_loader()) ||
 964         SystemDictionary::is_platform_class_loader(class_loader())) {
 965       // Fix for 4474172; see evaluation for more details
 966       class_loader = Handle(
 967         THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader()));
 968       ClassLoaderData *loader_data = register_loader(class_loader);
 969       Dictionary* dictionary = loader_data->dictionary();
 970 
 971       unsigned int d_hash = dictionary->compute_hash(name);
 972 
 973       bool DoObjectLock = true;
 974       if (is_parallelCapable(class_loader)) {
 975         DoObjectLock = false;
 976       }
 977 
 978       // Make sure we are synchronized on the class loader before we proceed
 979       //
 980       // Note: currently, find_or_load_shared_class is called only from
 981       // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader,
 982       // which are parallel-capable loaders, so this lock is NOT taken.
 983       Handle lockObject = compute_loader_lock_object(class_loader, THREAD);
 984       check_loader_lock_contention(lockObject, THREAD);
 985       ObjectLocker ol(lockObject, THREAD, DoObjectLock);
 986 
 987       {
 988         MutexLocker mu(THREAD, SystemDictionary_lock);
 989         InstanceKlass* check = find_class(d_hash, name, dictionary);
 990         if (check != NULL) {
 991           return check;
 992         }
 993       }
 994 
 995       k = load_shared_class_for_builtin_loader(name, class_loader, THREAD);
 996       if (k != NULL) {
 997         define_instance_class(k, CHECK_NULL);
 998       }
 999     }
1000   }
1001   return k;
1002 }
1003 
1004 PackageEntry* SystemDictionaryShared::get_package_entry_from_class_name(Handle class_loader, Symbol* class_name) {
1005   PackageEntry* pkg_entry = NULL;
1006   TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
1007   if (pkg_name != NULL) {
1008     pkg_entry = class_loader_data(class_loader)->packages()->lookup_only(pkg_name);
1009   }
1010   return pkg_entry;
1011 }
1012 
1013 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
1014                  Symbol* class_name, Handle class_loader, TRAPS) {
1015   assert(UseSharedSpaces, "must be");
1016   InstanceKlass* ik = find_builtin_class(class_name);
1017 
1018   if (ik != NULL) {
1019     if ((ik->is_shared_app_class() &&
1020          SystemDictionary::is_system_class_loader(class_loader()))  ||
1021         (ik->is_shared_platform_class() &&
1022          SystemDictionary::is_platform_class_loader(class_loader()))) {
1023       PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, class_name);
1024       Handle protection_domain =
1025         SystemDictionaryShared::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
1026       return load_shared_class(ik, class_loader, protection_domain, NULL, pkg_entry, THREAD);
1027     }
1028   }
1029   return NULL;
1030 }
1031 
1032 void SystemDictionaryShared::allocate_shared_protection_domain_array(int size, TRAPS) {
1033   if (_shared_protection_domains.resolve() == NULL) {
1034     oop spd = oopFactory::new_objArray(
1035         SystemDictionary::ProtectionDomain_klass(), size, CHECK);
1036     _shared_protection_domains = OopHandle(Universe::vm_global(), spd);
1037   }
1038 }
1039 
1040 void SystemDictionaryShared::allocate_shared_jar_url_array(int size, TRAPS) {
1041   if (_shared_jar_urls.resolve() == NULL) {
1042     oop sju = oopFactory::new_objArray(
1043         SystemDictionary::URL_klass(), size, CHECK);
1044     _shared_jar_urls = OopHandle(Universe::vm_global(), sju);
1045   }
1046 }
1047 
1048 void SystemDictionaryShared::allocate_shared_jar_manifest_array(int size, TRAPS) {
1049   if (_shared_jar_manifests.resolve() == NULL) {
1050     oop sjm = oopFactory::new_objArray(
1051         SystemDictionary::Jar_Manifest_klass(), size, CHECK);
1052     _shared_jar_manifests = OopHandle(Universe::vm_global(), sjm);
1053   }
1054 }
1055 
1056 void SystemDictionaryShared::allocate_shared_data_arrays(int size, TRAPS) {
1057   allocate_shared_protection_domain_array(size, CHECK);
1058   allocate_shared_jar_url_array(size, CHECK);
1059   allocate_shared_jar_manifest_array(size, CHECK);
1060 }
1061 
1062 // This function is called for loading only UNREGISTERED classes
1063 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
1064                                                           Handle class_loader,
1065                                                           Handle protection_domain,
1066                                                           const ClassFileStream* cfs,
1067                                                           TRAPS) {
1068   if (!UseSharedSpaces) {
1069     return NULL;
1070   }
1071   if (class_name == NULL) {  // don't do this for hidden and unsafe anonymous classes
1072     return NULL;
1073   }
1074   if (class_loader.is_null() ||
1075       SystemDictionary::is_system_class_loader(class_loader()) ||
1076       SystemDictionary::is_platform_class_loader(class_loader())) {
1077     // Do nothing for the BUILTIN loaders.
1078     return NULL;
1079   }
1080 
1081   const RunTimeSharedClassInfo* record = find_record(&_unregistered_dictionary, &_dynamic_unregistered_dictionary, class_name);
1082   if (record == NULL) {
1083     return NULL;
1084   }
1085 
1086   int clsfile_size  = cfs->length();
1087   int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
1088 
1089   if (!record->matches(clsfile_size, clsfile_crc32)) {
1090     return NULL;
1091   }
1092 
1093   return acquire_class_for_current_thread(record->_klass, class_loader,
1094                                           protection_domain, cfs,
1095                                           THREAD);
1096 }
1097 
1098 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread(
1099                    InstanceKlass *ik,
1100                    Handle class_loader,
1101                    Handle protection_domain,
1102                    const ClassFileStream *cfs,
1103                    TRAPS) {
1104   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
1105 
1106   {
1107     MutexLocker mu(THREAD, SharedDictionary_lock);
1108     if (ik->class_loader_data() != NULL) {
1109       //    ik is already loaded (by this loader or by a different loader)
1110       // or ik is being loaded by a different thread (by this loader or by a different loader)
1111       return NULL;
1112     }
1113 
1114     // No other thread has acquired this yet, so give it to *this thread*
1115     ik->set_class_loader_data(loader_data);
1116   }
1117 
1118   // No longer holding SharedDictionary_lock
1119   // No need to lock, as <ik> can be held only by a single thread.
1120   loader_data->add_class(ik);
1121 
1122   // Get the package entry.
1123   PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, ik->name());
1124 
1125   // Load and check super/interfaces, restore unsharable info
1126   InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
1127                                                   cfs, pkg_entry, THREAD);
1128   if (shared_klass == NULL || HAS_PENDING_EXCEPTION) {
1129     // TODO: clean up <ik> so it can be used again
1130     return NULL;
1131   }
1132 
1133   return shared_klass;
1134 }
1135 
1136 static ResourceHashtable<
1137   Symbol*, bool,
1138   primitive_hash<Symbol*>,
1139   primitive_equals<Symbol*>,
1140   6661,                             // prime number
1141   ResourceObj::C_HEAP> _loaded_unregistered_classes;
1142 
1143 bool SystemDictionaryShared::add_unregistered_class(InstanceKlass* k, TRAPS) {
1144   // We don't allow duplicated unregistered classes of the same name.
1145   assert(DumpSharedSpaces, "only when dumping");
1146   Symbol* name = k->name();
1147   bool created = false;
1148   _loaded_unregistered_classes.put_if_absent(name, true, &created);
1149   if (created) {
1150     MutexLocker mu_r(THREAD, Compile_lock); // add_to_hierarchy asserts this.
1151     SystemDictionary::add_to_hierarchy(k, CHECK_false);
1152   }
1153   return created;
1154 }
1155 
1156 // This function is called to resolve the super/interfaces of shared classes for
1157 // non-built-in loaders. E.g., ChildClass in the below example
1158 // where "super:" (and optionally "interface:") have been specified.
1159 //
1160 // java/lang/Object id: 0
1161 // Interface   id: 2 super: 0 source: cust.jar
1162 // ChildClass  id: 4 super: 0 interfaces: 2 source: cust.jar
1163 InstanceKlass* SystemDictionaryShared::dump_time_resolve_super_or_fail(
1164     Symbol* child_name, Symbol* class_name, Handle class_loader,
1165     Handle protection_domain, bool is_superclass, TRAPS) {
1166 
1167   assert(DumpSharedSpaces, "only when dumping");
1168 
1169   ClassListParser* parser = ClassListParser::instance();
1170   if (parser == NULL) {
1171     // We're still loading the well-known classes, before the ClassListParser is created.
1172     return NULL;
1173   }
1174   if (child_name->equals(parser->current_class_name())) {
1175     // When this function is called, all the numbered super and interface types
1176     // must have already been loaded. Hence this function is never recursively called.
1177     if (is_superclass) {
1178       return parser->lookup_super_for_current_class(class_name);
1179     } else {
1180       return parser->lookup_interface_for_current_class(class_name);
1181     }
1182   } else {
1183     // The VM is not trying to resolve a super type of parser->current_class_name().
1184     // Instead, it's resolving an error class (because parser->current_class_name() has
1185     // failed parsing or verification). Don't do anything here.
1186     return NULL;
1187   }
1188 }
1189 
1190 void SystemDictionaryShared::start_dumping() {
1191   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1192   _dump_in_progress = true;
1193 }
1194 
1195 DumpTimeSharedClassInfo* SystemDictionaryShared::find_or_allocate_info_for(InstanceKlass* k) {
1196   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1197   if (_dumptime_table == NULL) {
1198     _dumptime_table = new (ResourceObj::C_HEAP, mtClass)DumpTimeSharedClassTable();
1199   }
1200   return _dumptime_table->find_or_allocate_info_for(k, _dump_in_progress);
1201 }
1202 
1203 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
1204   Arguments::assert_is_dumping_archive();
1205   assert(!is_builtin(k), "must be unregistered class");
1206   DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k);
1207   if (info != NULL) {
1208     info->_clsfile_size  = cfs->length();
1209     info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
1210   }
1211 }
1212 
1213 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
1214   (void)find_or_allocate_info_for(k);
1215 }
1216 
1217 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
1218   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1219   DumpTimeSharedClassInfo* p = _dumptime_table->get(k);
1220   if (p == NULL) {
1221     return;
1222   }
1223   if (p->_verifier_constraints != NULL) {
1224     for (int i = 0; i < p->_verifier_constraints->length(); i++) {
1225       DumpTimeSharedClassInfo::DTVerifierConstraint constraint = p->_verifier_constraints->at(i);
1226       if (constraint._name != NULL ) {
1227         constraint._name->decrement_refcount();
1228       }
1229       if (constraint._from_name != NULL ) {
1230         constraint._from_name->decrement_refcount();
1231       }
1232     }
1233     FREE_C_HEAP_ARRAY(DumpTimeSharedClassInfo::DTVerifierConstraint, p->_verifier_constraints);
1234     p->_verifier_constraints = NULL;
1235     FREE_C_HEAP_ARRAY(char, p->_verifier_constraint_flags);
1236     p->_verifier_constraint_flags = NULL;
1237   }
1238   if (p->_loader_constraints != NULL) {
1239     for (int i = 0; i < p->_loader_constraints->length(); i++) {
1240       DumpTimeSharedClassInfo::DTLoaderConstraint ld =  p->_loader_constraints->at(i);
1241       if (ld._name != NULL) {
1242         ld._name->decrement_refcount();
1243       }
1244     }
1245     FREE_C_HEAP_ARRAY(DumpTimeSharedClassInfo::DTLoaderConstraint, p->_loader_constraints);
1246     p->_loader_constraints = NULL;
1247   }
1248   _dumptime_table->remove(k);
1249 }
1250 
1251 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) {
1252   while (k) {
1253     if (k->name()->equals("jdk/internal/event/Event")) {
1254       return true;
1255     }
1256     k = k->java_super();
1257   }
1258   return false;
1259 }
1260 
1261 bool SystemDictionaryShared::is_registered_lambda_proxy_class(InstanceKlass* ik) {
1262   DumpTimeSharedClassInfo* info = _dumptime_table->get(ik);
1263   return (info != NULL) ? info->_is_archived_lambda_proxy : false;
1264 }
1265 
1266 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
1267   assert(ik->is_shared(), "applicable to only a shared class");
1268   if (ik->is_hidden()) {
1269     return true;
1270   } else {
1271     return false;
1272   }
1273 }
1274 
1275 void SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) {
1276   ResourceMark rm;
1277   log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason);
1278 }
1279 
1280 bool SystemDictionaryShared::should_be_excluded(InstanceKlass* k) {
1281 
1282   if (k->is_unsafe_anonymous()) {
1283     warn_excluded(k, "Unsafe anonymous class");
1284     return true; // unsafe anonymous classes are not archived, skip
1285   }
1286 
1287   if (k->is_in_error_state()) {
1288     warn_excluded(k, "In error state");
1289     return true;
1290   }
1291   if (k->has_been_redefined()) {
1292     warn_excluded(k, "Has been redefined");
1293     return true;
1294   }
1295   if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
1296     // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
1297     // agent during dump time).
1298     warn_excluded(k, "Unsupported location");
1299     return true;
1300   }
1301   if (k->signers() != NULL) {
1302     // We cannot include signed classes in the archive because the certificates
1303     // used during dump time may be different than those used during
1304     // runtime (due to expiration, etc).
1305     warn_excluded(k, "Signed JAR");
1306     return true;
1307   }
1308   if (is_jfr_event_class(k)) {
1309     // We cannot include JFR event classes because they need runtime-specific
1310     // instrumentation in order to work with -XX:FlightRecorderOptions=retransform=false.
1311     // There are only a small number of these classes, so it's not worthwhile to
1312     // support them and make CDS more complicated.
1313     warn_excluded(k, "JFR event class");
1314     return true;
1315   }
1316   if (k->init_state() < InstanceKlass::linked) {
1317     // In CDS dumping, we will attempt to link all classes. Those that fail to link will
1318     // be recorded in DumpTimeSharedClassInfo.
1319     Arguments::assert_is_dumping_archive();
1320 
1321     // TODO -- rethink how this can be handled.
1322     // We should try to link ik, however, we can't do it here because
1323     // 1. We are at VM exit
1324     // 2. linking a class may cause other classes to be loaded, which means
1325     //    a custom ClassLoader.loadClass() may be called, at a point where the
1326     //    class loader doesn't expect it.
1327     if (has_class_failed_verification(k)) {
1328       warn_excluded(k, "Failed verification");
1329     } else {
1330       warn_excluded(k, "Not linked");
1331     }
1332     return true;
1333   }
1334   if (k->major_version() < 50 /*JAVA_6_VERSION*/) {
1335     ResourceMark rm;
1336     log_warning(cds)("Pre JDK 6 class not supported by CDS: %u.%u %s",
1337                      k->major_version(),  k->minor_version(), k->name()->as_C_string());
1338     return true;
1339   }
1340 
1341   InstanceKlass* super = k->java_super();
1342   if (super != NULL && should_be_excluded(super)) {
1343     ResourceMark rm;
1344     log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
1345     return true;
1346   }
1347 
1348   if (k->is_hidden() && !is_registered_lambda_proxy_class(k)) {
1349     warn_excluded(k, "Hidden class");
1350     return true;
1351   }
1352 
1353   Array<InstanceKlass*>* interfaces = k->local_interfaces();
1354   int len = interfaces->length();
1355   for (int i = 0; i < len; i++) {
1356     InstanceKlass* intf = interfaces->at(i);
1357     if (should_be_excluded(intf)) {
1358       log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
1359       return true;
1360     }
1361   }
1362 
1363   return false;
1364 }
1365 
1366 // k is a class before relocating by ArchiveBuilder
1367 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
1368   ResourceMark rm;
1369   const char* name = k->name()->as_C_string();
1370   DumpTimeSharedClassInfo* info = _dumptime_table->get(k);
1371   assert(_no_class_loading_should_happen, "class loading must be disabled");
1372   guarantee(info != NULL, "Class %s must be entered into _dumptime_table", name);
1373   guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
1374   if (is_builtin(k)) {
1375     if (k->is_hidden()) {
1376       assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name);
1377     }
1378     guarantee(!k->is_shared_unregistered_class(),
1379               "Class loader type must be set for BUILTIN class %s", name);
1380 
1381   } else {
1382     guarantee(k->is_shared_unregistered_class(),
1383               "Class loader type must not be set for UNREGISTERED class %s", name);
1384   }
1385 }
1386 
1387 class ExcludeDumpTimeSharedClasses : StackObj {
1388 public:
1389   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1390     if (SystemDictionaryShared::should_be_excluded(k)) {
1391       info.set_excluded();
1392     }
1393     return true; // keep on iterating
1394   }
1395 };
1396 
1397 void SystemDictionaryShared::check_excluded_classes() {
1398   ExcludeDumpTimeSharedClasses excl;
1399   _dumptime_table->iterate(&excl);
1400   _dumptime_table->update_counts();
1401 }
1402 
1403 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
1404   assert(_no_class_loading_should_happen, "sanity");
1405   Arguments::assert_is_dumping_archive();
1406   DumpTimeSharedClassInfo* p = find_or_allocate_info_for(k);
1407   return (p == NULL) ? true : p->is_excluded();
1408 }
1409 
1410 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
1411   Arguments::assert_is_dumping_archive();
1412   DumpTimeSharedClassInfo* p = find_or_allocate_info_for(ik);
1413   if (p != NULL) {
1414     p->set_failed_verification();
1415   }
1416 }
1417 
1418 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
1419   Arguments::assert_is_dumping_archive();
1420   if (_dumptime_table == NULL) {
1421     assert(DynamicDumpSharedSpaces, "sanity");
1422     assert(ik->is_shared(), "must be a shared class in the static archive");
1423     return false;
1424   }
1425   DumpTimeSharedClassInfo* p = _dumptime_table->get(ik);
1426   return (p == NULL) ? false : p->failed_verification();
1427 }
1428 
1429 class IterateDumpTimeSharedClassTable : StackObj {
1430   MetaspaceClosure *_it;
1431 public:
1432   IterateDumpTimeSharedClassTable(MetaspaceClosure* it) : _it(it) {}
1433 
1434   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1435     if (!info.is_excluded()) {
1436       info.metaspace_pointers_do(_it);
1437     }
1438     return true; // keep on iterating
1439   }
1440 };
1441 
1442 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
1443   assert_locked_or_safepoint(DumpTimeTable_lock);
1444   IterateDumpTimeSharedClassTable iter(it);
1445   _dumptime_table->iterate(&iter);
1446 }
1447 
1448 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
1449          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
1450   Arguments::assert_is_dumping_archive();
1451   DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k);
1452   if (info != NULL) {
1453     info->add_verification_constraint(k, name, from_name, from_field_is_protected,
1454                                       from_is_array, from_is_object);
1455   } else {
1456     return true;
1457   }
1458   if (DynamicDumpSharedSpaces) {
1459     // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
1460     // the initial run prior to creating the archive before vm exit. We will also perform verification
1461     // check when running with the archive.
1462     return false;
1463   } else {
1464     if (is_builtin(k)) {
1465       // For builtin class loaders, we can try to complete the verification check at dump time,
1466       // because we can resolve all the constraint classes. We will also perform verification check
1467       // when running with the archive.
1468       return false;
1469     } else {
1470       // For non-builtin class loaders, we cannot complete the verification check at dump time,
1471       // because at dump time we don't know how to resolve classes for such loaders.
1472       return true;
1473     }
1474   }
1475 }
1476 
1477 void DumpTimeSharedClassInfo::add_verification_constraint(InstanceKlass* k, Symbol* name,
1478          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
1479   if (_verifier_constraints == NULL) {
1480     _verifier_constraints = new(ResourceObj::C_HEAP, mtClass) GrowableArray<DTVerifierConstraint>(4, mtClass);
1481   }
1482   if (_verifier_constraint_flags == NULL) {
1483     _verifier_constraint_flags = new(ResourceObj::C_HEAP, mtClass) GrowableArray<char>(4, mtClass);
1484   }
1485   GrowableArray<DTVerifierConstraint>* vc_array = _verifier_constraints;
1486   for (int i = 0; i < vc_array->length(); i++) {
1487     DTVerifierConstraint* p = vc_array->adr_at(i);
1488     if (name == p->_name && from_name == p->_from_name) {
1489       return;
1490     }
1491   }
1492   DTVerifierConstraint cons(name, from_name);
1493   vc_array->append(cons);
1494 
1495   GrowableArray<char>* vcflags_array = _verifier_constraint_flags;
1496   char c = 0;
1497   c |= from_field_is_protected ? SystemDictionaryShared::FROM_FIELD_IS_PROTECTED : 0;
1498   c |= from_is_array           ? SystemDictionaryShared::FROM_IS_ARRAY           : 0;
1499   c |= from_is_object          ? SystemDictionaryShared::FROM_IS_OBJECT          : 0;
1500   vcflags_array->append(c);
1501 
1502   if (log_is_enabled(Trace, cds, verification)) {
1503     ResourceMark rm;
1504     log_trace(cds, verification)("add_verification_constraint: %s: %s must be subclass of %s [0x%x] array len %d flags len %d",
1505                                  k->external_name(), from_name->as_klass_external_name(),
1506                                  name->as_klass_external_name(), c, vc_array->length(), vcflags_array->length());
1507   }
1508 }
1509 
1510 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
1511                                                     InstanceKlass* lambda_ik,
1512                                                     Symbol* invoked_name,
1513                                                     Symbol* invoked_type,
1514                                                     Symbol* method_type,
1515                                                     Method* member_method,
1516                                                     Symbol* instantiated_method_type) {
1517 
1518   assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
1519   assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
1520   assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == NULL, "must not have class data");
1521 
1522   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1523 
1524   lambda_ik->assign_class_loader_type();
1525   lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
1526 
1527   DumpTimeSharedClassInfo* info = _dumptime_table->get(lambda_ik);
1528   if (info != NULL && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)) {
1529     // Set _is_archived_lambda_proxy in DumpTimeSharedClassInfo so that the lambda_ik
1530     // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
1531     info->_is_archived_lambda_proxy = true;
1532 
1533     LambdaProxyClassKey key(caller_ik,
1534                             invoked_name,
1535                             invoked_type,
1536                             method_type,
1537                             member_method,
1538                             instantiated_method_type);
1539     add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
1540   }
1541 }
1542 
1543 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
1544                                                                      Symbol* invoked_name,
1545                                                                      Symbol* invoked_type,
1546                                                                      Symbol* method_type,
1547                                                                      Method* member_method,
1548                                                                      Symbol* instantiated_method_type) {
1549   MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
1550   LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
1551                           method_type, member_method, instantiated_method_type);
1552   const RunTimeLambdaProxyClassInfo* info = _lambda_proxy_class_dictionary.lookup(&key, key.hash(), 0);
1553   InstanceKlass* proxy_klass = NULL;
1554   if (info != NULL) {
1555     InstanceKlass* curr_klass = info->proxy_klass_head();
1556     InstanceKlass* prev_klass = curr_klass;
1557     if (curr_klass->lambda_proxy_is_available()) {
1558       while (curr_klass->next_link() != NULL) {
1559         prev_klass = curr_klass;
1560         curr_klass = InstanceKlass::cast(curr_klass->next_link());
1561       }
1562       assert(curr_klass->is_hidden(), "must be");
1563       assert(curr_klass->lambda_proxy_is_available(), "must be");
1564 
1565       prev_klass->set_next_link(NULL);
1566       proxy_klass = curr_klass;
1567       proxy_klass->clear_lambda_proxy_is_available();
1568       if (log_is_enabled(Debug, cds)) {
1569         ResourceMark rm;
1570         log_debug(cds)("Loaded lambda proxy: %s", proxy_klass->external_name());
1571       }
1572     } else {
1573       if (log_is_enabled(Debug, cds)) {
1574         ResourceMark rm;
1575         log_debug(cds)("Used all archived lambda proxy classes for: %s %s%s",
1576                        caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
1577       }
1578     }
1579   }
1580   return proxy_klass;
1581 }
1582 
1583 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
1584   assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only");
1585   RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(lambda_ik);
1586   return record->nest_host();
1587 }
1588 
1589 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
1590                                                                          InstanceKlass* caller_ik,
1591                                                                          bool initialize, TRAPS) {
1592   Handle class_loader(THREAD, caller_ik->class_loader());
1593   Handle protection_domain;
1594   PackageEntry* pkg_entry = get_package_entry_from_class_name(class_loader, caller_ik->name());
1595   if (caller_ik->class_loader() != NULL) {
1596     protection_domain = SystemDictionaryShared::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
1597   }
1598 
1599   InstanceKlass* shared_nest_host = get_shared_nest_host(lambda_ik);
1600   assert(shared_nest_host != NULL, "unexpected NULL _nest_host");
1601 
1602   InstanceKlass* loaded_lambda =
1603     SystemDictionary::load_shared_lambda_proxy_class(lambda_ik, class_loader, protection_domain, pkg_entry, CHECK_NULL);
1604 
1605   // Ensures the nest host is the same as the lambda proxy's
1606   // nest host recorded at dump time.
1607   InstanceKlass* nest_host = caller_ik->nest_host(THREAD);
1608   assert(nest_host == shared_nest_host, "mismatched nest host");
1609 
1610   EventClassLoad class_load_start_event;
1611   {
1612     MutexLocker mu_r(THREAD, Compile_lock);
1613 
1614     // Add to class hierarchy, initialize vtables, and do possible
1615     // deoptimizations.
1616     SystemDictionary::add_to_hierarchy(loaded_lambda, CHECK_NULL); // No exception, but can block
1617     // But, do not add to dictionary.
1618   }
1619   loaded_lambda->link_class(CHECK_NULL);
1620   // notify jvmti
1621   if (JvmtiExport::should_post_class_load()) {
1622     JvmtiExport::post_class_load(THREAD->as_Java_thread(), loaded_lambda);
1623   }
1624   if (class_load_start_event.should_commit()) {
1625     SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
1626   }
1627 
1628   if (initialize) {
1629     loaded_lambda->initialize(CHECK_NULL);
1630   }
1631 
1632   return loaded_lambda;
1633 }
1634 
1635 static char get_loader_type_by(oop  loader) {
1636   assert(SystemDictionary::is_builtin_class_loader(loader), "Must be built-in loader");
1637   if (SystemDictionary::is_boot_class_loader(loader)) {
1638     return (char)ClassLoader::BOOT_LOADER;
1639   } else if (SystemDictionary::is_platform_class_loader(loader)) {
1640     return (char)ClassLoader::PLATFORM_LOADER;
1641   } else {
1642     assert(SystemDictionary::is_system_class_loader(loader), "Class loader mismatch");
1643     return (char)ClassLoader::APP_LOADER;
1644   }
1645 }
1646 
1647 static oop get_class_loader_by(char type) {
1648   if (type == (char)ClassLoader::BOOT_LOADER) {
1649     return (oop)NULL;
1650   } else if (type == (char)ClassLoader::PLATFORM_LOADER) {
1651     return SystemDictionary::java_platform_loader();
1652   } else {
1653     assert (type == (char)ClassLoader::APP_LOADER, "Sanity");
1654     return SystemDictionary::java_system_loader();
1655   }
1656 }
1657 
1658 void DumpTimeSharedClassInfo::record_linking_constraint(Symbol* name, Handle loader1, Handle loader2) {
1659   assert(loader1 != loader2, "sanity");
1660   LogTarget(Info, class, loader, constraints) log;
1661   if (_loader_constraints == NULL) {
1662     _loader_constraints = new (ResourceObj::C_HEAP, mtClass) GrowableArray<DTLoaderConstraint>(4, mtClass);
1663   }
1664   char lt1 = get_loader_type_by(loader1());
1665   char lt2 = get_loader_type_by(loader2());
1666   DTLoaderConstraint lc(name, lt1, lt2);
1667   for (int i = 0; i < _loader_constraints->length(); i++) {
1668     DTLoaderConstraint dt = _loader_constraints->at(i);
1669     if (lc.equals(dt)) {
1670       if (log.is_enabled()) {
1671         ResourceMark rm;
1672         // Use loader[0]/loader[1] to be consistent with the logs in loaderConstraints.cpp
1673         log.print("[CDS record loader constraint for class: %s constraint_name: %s loader[0]: %s loader[1]: %s already added]",
1674                   _klass->external_name(), name->as_C_string(),
1675                   ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1676                   ClassLoaderData::class_loader_data(loader2())->loader_name_and_id());
1677       }
1678       return;
1679     }
1680   }
1681   _loader_constraints->append(lc);
1682   if (log.is_enabled()) {
1683     ResourceMark rm;
1684     // Use loader[0]/loader[1] to be consistent with the logs in loaderConstraints.cpp
1685     log.print("[CDS record loader constraint for class: %s constraint_name: %s loader[0]: %s loader[1]: %s total %d]",
1686               _klass->external_name(), name->as_C_string(),
1687               ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1688               ClassLoaderData::class_loader_data(loader2())->loader_name_and_id(),
1689               _loader_constraints->length());
1690   }
1691 }
1692 
1693 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
1694                                                             TRAPS) {
1695   assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only");
1696   RunTimeSharedClassInfo* record = RunTimeSharedClassInfo::get_for(klass);
1697 
1698   int length = record->_num_verifier_constraints;
1699   if (length > 0) {
1700     for (int i = 0; i < length; i++) {
1701       RunTimeSharedClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
1702       Symbol* name      = vc->name();
1703       Symbol* from_name = vc->from_name();
1704       char c            = record->verifier_constraint_flag(i);
1705 
1706       if (log_is_enabled(Trace, cds, verification)) {
1707         ResourceMark rm(THREAD);
1708         log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
1709                                      klass->external_name(), from_name->as_klass_external_name(),
1710                                      name->as_klass_external_name(), c);
1711       }
1712 
1713       bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
1714       bool from_is_array           = (c & SystemDictionaryShared::FROM_IS_ARRAY)           ? true : false;
1715       bool from_is_object          = (c & SystemDictionaryShared::FROM_IS_OBJECT)          ? true : false;
1716 
1717       bool ok = VerificationType::resolve_and_check_assignability(klass, name,
1718          from_name, from_field_is_protected, from_is_array, from_is_object, CHECK);
1719       if (!ok) {
1720         ResourceMark rm(THREAD);
1721         stringStream ss;
1722 
1723         ss.print_cr("Bad type on operand stack");
1724         ss.print_cr("Exception Details:");
1725         ss.print_cr("  Location:\n    %s", klass->name()->as_C_string());
1726         ss.print_cr("  Reason:\n    Type '%s' is not assignable to '%s'",
1727                     from_name->as_quoted_ascii(), name->as_quoted_ascii());
1728         THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string());
1729       }
1730     }
1731   }
1732 }
1733 
1734 // Record class loader constraints that are checked inside
1735 // InstanceKlass::link_class(), so that these can be checked quickly
1736 // at runtime without laying out the vtable/itables.
1737 void SystemDictionaryShared::record_linking_constraint(Symbol* name, InstanceKlass* klass,
1738                                                     Handle loader1, Handle loader2, TRAPS) {
1739   // A linking constraint check is executed when:
1740   //   - klass extends or implements type S
1741   //   - klass overrides method S.M(...) with X.M
1742   //     - If klass defines the method M, X is
1743   //       the same as klass.
1744   //     - If klass does not define the method M,
1745   //       X must be a supertype of klass and X.M is
1746   //       a default method defined by X.
1747   //   - loader1 = X->class_loader()
1748   //   - loader2 = S->class_loader()
1749   //   - loader1 != loader2
1750   //   - M's paramater(s) include an object type T
1751   // We require that
1752   //   - whenever loader1 and loader2 try to
1753   //     resolve the type T, they must always resolve to
1754   //     the same InstanceKlass.
1755   // NOTE: type T may or may not be currently resolved in
1756   // either of these two loaders. The check itself does not
1757   // try to resolve T.
1758   oop klass_loader = klass->class_loader();
1759   assert(klass_loader != NULL, "should not be called for boot loader");
1760   assert(loader1 != loader2, "must be");
1761 
1762   if (!is_system_class_loader(klass_loader) &&
1763       !is_platform_class_loader(klass_loader)) {
1764     // If klass is loaded by system/platform loaders, we can
1765     // guarantee that klass and S must be loaded by the same
1766     // respective loader between dump time and run time, and
1767     // the exact same check on (name, loader1, loader2) will
1768     // be executed. Hence, we can cache this check and execute
1769     // it at runtime without walking the vtable/itables.
1770     //
1771     // This cannot be guaranteed for classes loaded by other
1772     // loaders, so we bail.
1773     return;
1774   }
1775 
1776   if (THREAD->is_VM_thread()) {
1777     assert(DynamicDumpSharedSpaces, "must be");
1778     // We are re-laying out the vtable/itables of the *copy* of
1779     // a class during the final stage of dynamic dumping. The
1780     // linking constraints for this class has already been recorded.
1781     return;
1782   }
1783   Arguments::assert_is_dumping_archive();
1784   DumpTimeSharedClassInfo* info = find_or_allocate_info_for(klass);
1785   if (info != NULL) {
1786     info->record_linking_constraint(name, loader1, loader2);
1787   }
1788 }
1789 
1790 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1791 // the purpose of checking class loader constraints.
1792 bool SystemDictionaryShared::check_linking_constraints(InstanceKlass* klass, TRAPS) {
1793   assert(!DumpSharedSpaces && UseSharedSpaces, "called at run time with CDS enabled only");
1794   LogTarget(Info, class, loader, constraints) log;
1795   if (klass->is_shared_boot_class()) {
1796     // No class loader constraint check performed for boot classes.
1797     return true;
1798   }
1799   if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1800     RunTimeSharedClassInfo* info = RunTimeSharedClassInfo::get_for(klass);
1801     assert(info != NULL, "Sanity");
1802     if (info->_num_loader_constraints > 0) {
1803       HandleMark hm(THREAD);
1804       for (int i = 0; i < info->_num_loader_constraints; i++) {
1805         RunTimeSharedClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1806         Symbol* name = lc->constraint_name();
1807         Handle loader1(THREAD, get_class_loader_by(lc->_loader_type1));
1808         Handle loader2(THREAD, get_class_loader_by(lc->_loader_type2));
1809         if (log.is_enabled()) {
1810           ResourceMark rm(THREAD);
1811           log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1812                     klass->external_name(), name->as_C_string(),
1813                     ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1814                     ClassLoaderData::class_loader_data(loader2())->loader_name_and_id());
1815         }
1816         if (!SystemDictionary::add_loader_constraint(name, klass, loader1, loader2, THREAD)) {
1817           // Loader constraint violation has been found. The caller
1818           // will re-layout the vtable/itables to produce the correct
1819           // exception.
1820           if (log.is_enabled()) {
1821             log.print(" failed]");
1822           }
1823           return false;
1824         }
1825         if (log.is_enabled()) {
1826             log.print(" succeeded]");
1827         }
1828       }
1829       return true; // for all recorded constraints added successully.
1830     }
1831   }
1832   if (log.is_enabled()) {
1833     ResourceMark rm(THREAD);
1834     log.print("[CDS has not recorded loader constraint for class %s]", klass->external_name());
1835   }
1836   return false;
1837 }
1838 
1839 class EstimateSizeForArchive : StackObj {
1840   size_t _shared_class_info_size;
1841   int _num_builtin_klasses;
1842   int _num_unregistered_klasses;
1843 
1844 public:
1845   EstimateSizeForArchive() {
1846     _shared_class_info_size = 0;
1847     _num_builtin_klasses = 0;
1848     _num_unregistered_klasses = 0;
1849   }
1850 
1851   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1852     if (!info.is_excluded()) {
1853       size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints());
1854       _shared_class_info_size += align_up(byte_size, BytesPerWord);
1855     }
1856     return true; // keep on iterating
1857   }
1858 
1859   size_t total() {
1860     return _shared_class_info_size;
1861   }
1862 };
1863 
1864 size_t SystemDictionaryShared::estimate_size_for_archive() {
1865   EstimateSizeForArchive est;
1866   _dumptime_table->iterate(&est);
1867   size_t total_size = est.total() +
1868     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1869     CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1870   if (_dumptime_lambda_proxy_class_dictionary != NULL) {
1871     total_size +=
1872       (sizeof(RunTimeLambdaProxyClassInfo) * _dumptime_lambda_proxy_class_dictionary->_count) +
1873       CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1874   } else {
1875     total_size += CompactHashtableWriter::estimate_size(0);
1876   }
1877   return total_size;
1878 }
1879 
1880 class CopyLambdaProxyClassInfoToArchive : StackObj {
1881   CompactHashtableWriter* _writer;
1882 public:
1883   CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1884     : _writer(writer) {}
1885   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1886     if (SystemDictionaryShared::is_excluded_class(info._proxy_klasses->at(0))) {
1887       return true;
1888     }
1889     ResourceMark rm;
1890     log_info(cds,dynamic)("Archiving hidden %s", info._proxy_klasses->at(0)->external_name());
1891     size_t byte_size = sizeof(RunTimeLambdaProxyClassInfo);
1892     RunTimeLambdaProxyClassInfo* runtime_info =
1893         (RunTimeLambdaProxyClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size);
1894     runtime_info->init(key, info);
1895     unsigned int hash = runtime_info->hash(); // Fields in runtime_info->_key already point to target space.
1896     u4 delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(runtime_info));
1897     _writer->add(hash, delta);
1898     return true;
1899   }
1900 };
1901 
1902 class AdjustLambdaProxyClassInfo : StackObj {
1903 public:
1904   AdjustLambdaProxyClassInfo() {}
1905   bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1906     if (SystemDictionaryShared::is_excluded_class(info._proxy_klasses->at(0))) {
1907       return true;
1908     }
1909     int len = info._proxy_klasses->length();
1910     if (len > 1) {
1911       for (int i = 0; i < len-1; i++) {
1912         InstanceKlass* ok0 = info._proxy_klasses->at(i+0); // this is original klass
1913         InstanceKlass* ok1 = info._proxy_klasses->at(i+1); // this is original klass
1914         InstanceKlass* bk0 = DynamicArchive::original_to_buffer(ok0);
1915         InstanceKlass* bk1 = DynamicArchive::original_to_buffer(ok1);
1916         assert(bk0->next_link() == 0, "must be called after Klass::remove_unshareable_info()");
1917         assert(bk1->next_link() == 0, "must be called after Klass::remove_unshareable_info()");
1918         bk0->set_next_link(bk1);
1919         bk1->set_lambda_proxy_is_available();
1920         ArchivePtrMarker::mark_pointer(bk0->next_link_addr());
1921       }
1922     }
1923     DynamicArchive::original_to_buffer(info._proxy_klasses->at(0))->set_lambda_proxy_is_available();
1924     return true;
1925   }
1926 };
1927 
1928 class CopySharedClassInfoToArchive : StackObj {
1929   CompactHashtableWriter* _writer;
1930   bool _is_builtin;
1931 public:
1932   CopySharedClassInfoToArchive(CompactHashtableWriter* writer,
1933                                bool is_builtin)
1934     : _writer(writer), _is_builtin(is_builtin) {}
1935 
1936   bool do_entry(InstanceKlass* k, DumpTimeSharedClassInfo& info) {
1937     if (!info.is_excluded() && info.is_builtin() == _is_builtin) {
1938       size_t byte_size = RunTimeSharedClassInfo::byte_size(info._klass, info.num_verifier_constraints(), info.num_loader_constraints());
1939       RunTimeSharedClassInfo* record;
1940       record = (RunTimeSharedClassInfo*)MetaspaceShared::read_only_space_alloc(byte_size);
1941       record->init(info);
1942 
1943       unsigned int hash;
1944       Symbol* name = info._klass->name();
1945       if (DynamicDumpSharedSpaces) {
1946         name = DynamicArchive::original_to_target(name);
1947       }
1948       hash = SystemDictionaryShared::hash_for_shared_dictionary(name);
1949       u4 delta;
1950       if (DynamicDumpSharedSpaces) {
1951         delta = MetaspaceShared::object_delta_u4(DynamicArchive::buffer_to_target(record));
1952       } else {
1953         delta = MetaspaceShared::object_delta_u4(record);
1954       }
1955       if (_is_builtin && info._klass->is_hidden()) {
1956         // skip
1957       } else {
1958         _writer->add(hash, delta);
1959       }
1960       if (log_is_enabled(Trace, cds, hashtables)) {
1961         ResourceMark rm;
1962         log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1963       }
1964 
1965       // Save this for quick runtime lookup of InstanceKlass* -> RunTimeSharedClassInfo*
1966       RunTimeSharedClassInfo::set_for(info._klass, record);
1967     }
1968     return true; // keep on iterating
1969   }
1970 };
1971 
1972 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
1973   CompactHashtableStats stats;
1974   dictionary->reset();
1975   CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
1976   CopyLambdaProxyClassInfoToArchive copy(&writer);
1977   _dumptime_lambda_proxy_class_dictionary->iterate(&copy);
1978   writer.dump(dictionary, "lambda proxy class dictionary");
1979 }
1980 
1981 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1982                                               bool is_builtin) {
1983   CompactHashtableStats stats;
1984   dictionary->reset();
1985   CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1986   CopySharedClassInfoToArchive copy(&writer, is_builtin);
1987   MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1988   _dumptime_table->iterate(&copy);
1989   writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1990 }
1991 
1992 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1993   if (is_static_archive) {
1994     write_dictionary(&_builtin_dictionary, true);
1995     write_dictionary(&_unregistered_dictionary, false);
1996   } else {
1997     write_dictionary(&_dynamic_builtin_dictionary, true);
1998     write_dictionary(&_dynamic_unregistered_dictionary, false);
1999   }
2000   if (_dumptime_lambda_proxy_class_dictionary != NULL) {
2001     write_lambda_proxy_class_dictionary(&_lambda_proxy_class_dictionary);
2002   }
2003 }
2004 
2005 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
2006   if (_dumptime_lambda_proxy_class_dictionary != NULL) {
2007     AdjustLambdaProxyClassInfo adjuster;
2008     _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
2009   }
2010 }
2011 
2012 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
2013                                                           bool is_static_archive) {
2014   if (is_static_archive) {
2015     _builtin_dictionary.serialize_header(soc);
2016     _unregistered_dictionary.serialize_header(soc);
2017   } else {
2018     _dynamic_builtin_dictionary.serialize_header(soc);
2019     _dynamic_unregistered_dictionary.serialize_header(soc);
2020     _lambda_proxy_class_dictionary.serialize_header(soc);
2021   }
2022 }
2023 
2024 void SystemDictionaryShared::serialize_well_known_klasses(SerializeClosure* soc) {
2025   for (int i = FIRST_WKID; i < WKID_LIMIT; i++) {
2026     soc->do_ptr((void**)&_well_known_klasses[i]);
2027   }
2028 }
2029 
2030 const RunTimeSharedClassInfo*
2031 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
2032   if (!UseSharedSpaces || !name->is_shared()) {
2033     // The names of all shared classes must also be a shared Symbol.
2034     return NULL;
2035   }
2036 
2037   unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary(name);
2038   const RunTimeSharedClassInfo* record = NULL;
2039   if (!MetaspaceShared::is_shared_dynamic(name)) {
2040     // The names of all shared classes in the static dict must also be in the
2041     // static archive
2042     record = static_dict->lookup(name, hash, 0);
2043   }
2044 
2045   if (record == NULL && DynamicArchive::is_mapped()) {
2046     record = dynamic_dict->lookup(name, hash, 0);
2047   }
2048 
2049   return record;
2050 }
2051 
2052 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) {
2053   const RunTimeSharedClassInfo* record = find_record(&_builtin_dictionary, &_dynamic_builtin_dictionary, name);
2054   if (record != NULL) {
2055     assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
2056     assert(check_alignment(record->_klass), "Address not aligned");
2057     return record->_klass;
2058   } else {
2059     return NULL;
2060   }
2061 }
2062 
2063 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
2064   assert(DumpSharedSpaces, "supported only when dumping");
2065   DumpTimeSharedClassInfo* info = find_or_allocate_info_for(k);
2066   info->_id = id;
2067 }
2068 
2069 class SharedDictionaryPrinter : StackObj {
2070   outputStream* _st;
2071   int _index;
2072 public:
2073   SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
2074 
2075   void do_value(const RunTimeSharedClassInfo* record) {
2076     ResourceMark rm;
2077     _st->print_cr("%4d:  %s", (_index++), record->_klass->external_name());
2078   }
2079 };
2080 
2081 class SharedLambdaDictionaryPrinter : StackObj {
2082   outputStream* _st;
2083   int _index;
2084 public:
2085   SharedLambdaDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
2086 
2087   void do_value(const RunTimeLambdaProxyClassInfo* record) {
2088     ResourceMark rm;
2089     _st->print_cr("%4d:  %s", (_index++), record->proxy_klass_head()->external_name());
2090     Klass* k = record->proxy_klass_head()->next_link();
2091     while (k != NULL) {
2092       _st->print_cr("%4d:  %s", (_index++), k->external_name());
2093       k = k->next_link();
2094     }
2095   }
2096 };
2097 
2098 void SystemDictionaryShared::print_on(outputStream* st) {
2099   if (UseSharedSpaces) {
2100     st->print_cr("Shared Dictionary");
2101     SharedDictionaryPrinter p(st);
2102     _builtin_dictionary.iterate(&p);
2103     _unregistered_dictionary.iterate(&p);
2104     if (DynamicArchive::is_mapped()) {
2105       _dynamic_builtin_dictionary.iterate(&p);
2106       _unregistered_dictionary.iterate(&p);
2107       if (!_lambda_proxy_class_dictionary.empty()) {
2108         st->print_cr("Shared Lambda Dictionary");
2109         SharedLambdaDictionaryPrinter ldp(st);
2110         _lambda_proxy_class_dictionary.iterate(&ldp);
2111       }
2112     }
2113   }
2114 }
2115 
2116 void SystemDictionaryShared::print_table_statistics(outputStream* st) {
2117   if (UseSharedSpaces) {
2118     _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
2119     _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
2120     if (DynamicArchive::is_mapped()) {
2121       _dynamic_builtin_dictionary.print_table_statistics(st, "Dynamic Builtin Shared Dictionary");
2122       _dynamic_unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
2123       _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");
2124     }
2125   }
2126 }
2127 
2128 bool SystemDictionaryShared::empty_dumptime_table() {
2129   if (_dumptime_table == NULL) {
2130     return true;
2131   }
2132   _dumptime_table->update_counts();
2133   if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){
2134     return true;
2135   }
2136   return false;
2137 }
2138 
2139 #if INCLUDE_CDS_JAVA_HEAP
2140 
2141 class ArchivedMirrorPatcher {
2142   static void update(Klass* k) {
2143     if (k->has_raw_archived_mirror()) {
2144       oop m = HeapShared::materialize_archived_object(k->archived_java_mirror_raw_narrow());
2145       if (m != NULL) {
2146         java_lang_Class::update_archived_mirror_native_pointers(m);
2147       }
2148     }
2149   }
2150 
2151 public:
2152   static void update_array_klasses(Klass* ak) {
2153     while (ak != NULL) {
2154       update(ak);
2155       ak = ArrayKlass::cast(ak)->higher_dimension();
2156     }
2157   }
2158 
2159   void do_value(const RunTimeSharedClassInfo* info) {
2160     InstanceKlass* ik = info->_klass;
2161     update(ik);
2162     update_array_klasses(ik->array_klasses());
2163   }
2164 };
2165 
2166 void SystemDictionaryShared::update_archived_mirror_native_pointers_for(RunTimeSharedDictionary* dict) {
2167   ArchivedMirrorPatcher patcher;
2168   dict->iterate(&patcher);
2169 }
2170 
2171 void SystemDictionaryShared::update_archived_mirror_native_pointers() {
2172   if (!HeapShared::open_archive_heap_region_mapped()) {
2173     return;
2174   }
2175   if (MetaspaceShared::relocation_delta() == 0) {
2176     return;
2177   }
2178   update_archived_mirror_native_pointers_for(&_builtin_dictionary);
2179   update_archived_mirror_native_pointers_for(&_unregistered_dictionary);
2180 
2181   for (int t = T_BOOLEAN; t <= T_LONG; t++) {
2182     Klass* k = Universe::typeArrayKlassObj((BasicType)t);
2183     ArchivedMirrorPatcher::update_array_klasses(k);
2184   }
2185 }
2186 #endif