1 /*
   2  * Copyright (c) 2018, 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/javaClasses.inline.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "logging/log.hpp"
  30 #include "logging/logMessage.hpp"
  31 #include "logging/logStream.hpp"
  32 #include "memory/heapShared.hpp"
  33 #include "memory/iterator.inline.hpp"
  34 #include "memory/metadataFactory.hpp"
  35 #include "memory/metaspaceClosure.hpp"
  36 #include "memory/metaspaceShared.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "oops/compressedOops.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "runtime/fieldDescriptor.inline.hpp"
  41 
  42 #if INCLUDE_CDS_JAVA_HEAP
  43 KlassSubGraphInfo* HeapShared::_subgraph_info_list = NULL;
  44 int HeapShared::_num_archived_subgraph_info_records = 0;
  45 Array<ArchivedKlassSubGraphInfoRecord>* HeapShared::_archived_subgraph_info_records = NULL;
  46 
  47 // Currently there is only one class mirror (ArchivedModuleGraph) with archived
  48 // sub-graphs.
  49 KlassSubGraphInfo* HeapShared::find_subgraph_info(Klass* k) {
  50   KlassSubGraphInfo* info = _subgraph_info_list;
  51   while (info != NULL) {
  52     if (info->klass() == k) {
  53       return info;
  54     }
  55     info = info->next();
  56   }
  57   return NULL;
  58 }
  59 
  60 // Get the subgraph_info for Klass k. A new subgraph_info is created if
  61 // there is no existing one for k. The subgraph_info records the relocated
  62 // Klass* of the original k.
  63 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
  64   Klass* relocated_k = MetaspaceShared::get_relocated_klass(k);
  65   KlassSubGraphInfo* info = find_subgraph_info(relocated_k);
  66   if (info != NULL) {
  67     return info;
  68   }
  69 
  70   info = new KlassSubGraphInfo(relocated_k, _subgraph_info_list);
  71   _subgraph_info_list = info;
  72   return info;
  73 }
  74 
  75 int HeapShared::num_of_subgraph_infos() {
  76   int num = 0;
  77   KlassSubGraphInfo* info = _subgraph_info_list;
  78   while (info != NULL) {
  79     num ++;
  80     info = info->next();
  81   }
  82   return num;
  83 }
  84 
  85 // Add an entry field to the current KlassSubGraphInfo.
  86 void KlassSubGraphInfo::add_subgraph_entry_field(int static_field_offset, oop v) {
  87   assert(DumpSharedSpaces, "dump time only");
  88   if (_subgraph_entry_fields == NULL) {
  89     _subgraph_entry_fields =
  90       new(ResourceObj::C_HEAP, mtClass) GrowableArray<juint>(10, true);
  91   }
  92   _subgraph_entry_fields->append((juint)static_field_offset);
  93   _subgraph_entry_fields->append(CompressedOops::encode(v));
  94 }
  95 
  96 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
  97 // Only objects of boot classes can be included in sub-graph.
  98 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k, Klass *relocated_k) {
  99   assert(DumpSharedSpaces, "dump time only");
 100   assert(relocated_k == MetaspaceShared::get_relocated_klass(orig_k),
 101          "must be the relocated Klass in the shared space");
 102 
 103   if (_subgraph_object_klasses == NULL) {
 104     _subgraph_object_klasses =
 105       new(ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(50, true);
 106   }
 107 
 108   assert(relocated_k->is_shared(), "must be a shared class");
 109 
 110   if (_k == relocated_k) {
 111     // Don't add the Klass containing the sub-graph to it's own klass
 112     // initialization list.
 113     return;
 114   }
 115 
 116   if (relocated_k->is_instance_klass()) {
 117     assert(InstanceKlass::cast(relocated_k)->is_shared_boot_class(),
 118           "must be boot class");
 119     // SystemDictionary::xxx_klass() are not updated, need to check
 120     // the original Klass*
 121     if (orig_k == SystemDictionary::String_klass() ||
 122         orig_k == SystemDictionary::Object_klass()) {
 123       // Initialized early during VM initialization. No need to be added
 124       // to the sub-graph object class list.
 125       return;
 126     }
 127   } else if (relocated_k->is_objArray_klass()) {
 128     Klass* abk = ObjArrayKlass::cast(relocated_k)->bottom_klass();
 129     if (abk->is_instance_klass()) {
 130       assert(InstanceKlass::cast(abk)->is_shared_boot_class(),
 131             "must be boot class");
 132     }
 133     if (relocated_k == Universe::objectArrayKlassObj()) {
 134       // Initialized early during Universe::genesis. No need to be added
 135       // to the list.
 136       return;
 137     }
 138   } else {
 139     assert(relocated_k->is_typeArray_klass(), "must be");
 140     // Primitive type arrays are created early during Universe::genesis.
 141     return;
 142   }
 143 
 144   _subgraph_object_klasses->append_if_missing(relocated_k);
 145 }
 146 
 147 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo.
 148 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
 149   _k = info->klass();
 150   _next = NULL;
 151   _entry_field_records = NULL;
 152   _subgraph_klasses = NULL;
 153 
 154   // populate the entry fields
 155   GrowableArray<juint>* entry_fields = info->subgraph_entry_fields();
 156   if (entry_fields != NULL) {
 157     int num_entry_fields = entry_fields->length();
 158     assert(num_entry_fields % 2 == 0, "sanity");
 159     _entry_field_records =
 160       MetaspaceShared::new_ro_array<juint>(num_entry_fields);
 161     for (int i = 0 ; i < num_entry_fields; i++) {
 162       _entry_field_records->at_put(i, entry_fields->at(i));
 163     }
 164   }
 165 
 166   // the Klasses of the objects in the sub-graphs
 167   GrowableArray<Klass*>* subgraph_klasses = info->subgraph_object_klasses();
 168   if (subgraph_klasses != NULL) {
 169     int num_subgraphs_klasses = subgraph_klasses->length();
 170     _subgraph_klasses =
 171       MetaspaceShared::new_ro_array<Klass*>(num_subgraphs_klasses);
 172     for (int i = 0; i < num_subgraphs_klasses; i++) {
 173       Klass* subgraph_k = subgraph_klasses->at(i);
 174       if (log_is_enabled(Info, cds, heap)) {
 175         ResourceMark rm;
 176         log_info(cds, heap)(
 177           "Archived object klass (%d): %s in %s sub-graphs",
 178           i, subgraph_k->external_name(), _k->external_name());
 179       }
 180       _subgraph_klasses->at_put(i, subgraph_k);
 181     }
 182   }
 183 }
 184 
 185 // Build the records of archived subgraph infos, which include:
 186 // - Entry points to all subgraphs from the containing class mirror. The entry
 187 //   points are static fields in the mirror. For each entry point, the field
 188 //   offset and value are recorded in the sub-graph info. The value are stored
 189 //   back to the corresponding field at runtime.
 190 // - A list of klasses that need to be loaded/initialized before archived
 191 //   java object sub-graph can be accessed at runtime.
 192 //
 193 // The records are saved in the archive file and reloaded at runtime. Currently
 194 // there is only one class mirror (ArchivedModuleGraph) with archived sub-graphs.
 195 //
 196 // Layout of the archived subgraph info records:
 197 //
 198 // records_size | num_records | records*
 199 // ArchivedKlassSubGraphInfoRecord | entry_fields | subgraph_object_klasses
 200 size_t HeapShared::build_archived_subgraph_info_records(int num_records) {
 201   // remember the start address
 202   char* start_p = MetaspaceShared::read_only_space_top();
 203 
 204   // now populate the archived subgraph infos, which will be saved in the
 205   // archive file
 206   _archived_subgraph_info_records =
 207     MetaspaceShared::new_ro_array<ArchivedKlassSubGraphInfoRecord>(num_records);
 208   KlassSubGraphInfo* info = _subgraph_info_list;
 209   int i = 0;
 210   while (info != NULL) {
 211     assert(i < _archived_subgraph_info_records->length(), "sanity");
 212     ArchivedKlassSubGraphInfoRecord* record =
 213       _archived_subgraph_info_records->adr_at(i);
 214     record->init(info);
 215     info = info->next();
 216     i ++;
 217   }
 218 
 219   // _subgraph_info_list is no longer needed
 220   delete _subgraph_info_list;
 221   _subgraph_info_list = NULL;
 222 
 223   char* end_p = MetaspaceShared::read_only_space_top();
 224   size_t records_size = end_p - start_p;
 225   return records_size;
 226 }
 227 
 228 // Write the subgraph info records in the shared _ro region
 229 void HeapShared::write_archived_subgraph_infos() {
 230   assert(DumpSharedSpaces, "dump time only");
 231 
 232   Array<intptr_t>* records_header = MetaspaceShared::new_ro_array<intptr_t>(3);
 233 
 234   _num_archived_subgraph_info_records = num_of_subgraph_infos();
 235   size_t records_size = build_archived_subgraph_info_records(
 236                              _num_archived_subgraph_info_records);
 237 
 238   // Now write the header information:
 239   // records_size, num_records, _archived_subgraph_info_records
 240   assert(records_header != NULL, "sanity");
 241   intptr_t* p = (intptr_t*)(records_header->data());
 242   *p = (intptr_t)records_size;
 243   p ++;
 244   *p = (intptr_t)_num_archived_subgraph_info_records;
 245   p ++;
 246   *p = (intptr_t)_archived_subgraph_info_records;
 247 }
 248 
 249 char* HeapShared::read_archived_subgraph_infos(char* buffer) {
 250   Array<intptr_t>* records_header = (Array<intptr_t>*)buffer;
 251   intptr_t* p = (intptr_t*)(records_header->data());
 252   size_t records_size = (size_t)(*p);
 253   p ++;
 254   _num_archived_subgraph_info_records = *p;
 255   p ++;
 256   _archived_subgraph_info_records =
 257     (Array<ArchivedKlassSubGraphInfoRecord>*)(*p);
 258 
 259   buffer = (char*)_archived_subgraph_info_records + records_size;
 260   return buffer;
 261 }
 262 
 263 void HeapShared::initialize_from_archived_subgraph(Klass* k) {
 264   if (!MetaspaceShared::open_archive_heap_region_mapped()) {
 265     return; // nothing to do
 266   }
 267 
 268   if (_num_archived_subgraph_info_records == 0) {
 269     return; // no subgraph info records
 270   }
 271 
 272   // Initialize from archived data. Currently only ArchivedModuleGraph
 273   // has archived object subgraphs, which is used during VM initialization
 274   // time when bootstraping the system modules. No lock is needed.
 275   Thread* THREAD = Thread::current();
 276   for (int i = 0; i < _archived_subgraph_info_records->length(); i++) {
 277     ArchivedKlassSubGraphInfoRecord* record = _archived_subgraph_info_records->adr_at(i);
 278     if (record->klass() == k) {
 279       int i;
 280       // Found the archived subgraph info record for the requesting klass.
 281       // Load/link/initialize the klasses of the objects in the subgraph.
 282       // NULL class loader is used.
 283       Array<Klass*>* klasses = record->subgraph_klasses();
 284       if (klasses != NULL) {
 285         for (i = 0; i < klasses->length(); i++) {
 286           Klass* obj_k = klasses->at(i);
 287           Klass* resolved_k = SystemDictionary::resolve_or_null(
 288                                                 (obj_k)->name(), THREAD);
 289           if (resolved_k != obj_k) {
 290             return;
 291           }
 292           if ((obj_k)->is_instance_klass()) {
 293             InstanceKlass* ik = InstanceKlass::cast(obj_k);
 294             ik->initialize(THREAD);
 295           } else if ((obj_k)->is_objArray_klass()) {
 296             ObjArrayKlass* oak = ObjArrayKlass::cast(obj_k);
 297             oak->initialize(THREAD);
 298           }
 299         }
 300       }
 301 
 302       if (HAS_PENDING_EXCEPTION) {
 303         CLEAR_PENDING_EXCEPTION;
 304         // None of the field value will be set if there was an exception.
 305         // The java code will not see any of the archived objects in the
 306         // subgraphs referenced from k in this case.
 307         return;
 308       }
 309 
 310       // Load the subgraph entry fields from the record and store them back to
 311       // the corresponding fields within the mirror.
 312       oop m = k->java_mirror();
 313       Array<juint>* entry_field_records = record->entry_field_records();
 314       if (entry_field_records != NULL) {
 315         int efr_len = entry_field_records->length();
 316         assert(efr_len % 2 == 0, "sanity");
 317         for (i = 0; i < efr_len;) {
 318           int field_offset = entry_field_records->at(i);
 319           // The object refereced by the field becomes 'known' by GC from this
 320           // point. All objects in the subgraph reachable from the object are
 321           // also 'known' by GC.
 322           oop v = MetaspaceShared::materialize_archived_object(
 323             CompressedOops::decode(entry_field_records->at(i+1)));
 324           m->obj_field_put(field_offset, v);
 325           i += 2;
 326         }
 327       }
 328 
 329       // Done. Java code can see the archived sub-graphs referenced from k's
 330       // mirror after this point.
 331       return;
 332     }
 333   }
 334 }
 335 
 336 class WalkOopAndArchiveClosure: public BasicOopIterateClosure {
 337   int _level;
 338   KlassSubGraphInfo* _subgraph_info;
 339   oop _orig_referencing_obj;
 340   oop _archived_referencing_obj;
 341  public:
 342   WalkOopAndArchiveClosure(int level, KlassSubGraphInfo* subgraph_info,
 343            oop orig, oop archived) : _level(level),
 344                                      _subgraph_info(subgraph_info),
 345                                      _orig_referencing_obj(orig),
 346                                      _archived_referencing_obj(archived) {}
 347   void do_oop(narrowOop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
 348   void do_oop(      oop *p) { WalkOopAndArchiveClosure::do_oop_work(p); }
 349 
 350  protected:
 351   template <class T> void do_oop_work(T *p) {
 352     oop obj = RawAccess<>::oop_load(p);
 353     if (!CompressedOops::is_null(obj)) {
 354       // A java.lang.Class instance can not be included in an archived
 355       // object sub-graph.
 356       if (java_lang_Class::is_instance(obj)) {
 357         log_error(cds, heap)("Unknown java.lang.Class object is in the archived sub-graph\n");
 358         vm_exit(1);
 359       }
 360 
 361       LogTarget(Debug, cds, heap) log;
 362       LogStream ls(log);
 363       outputStream* out = &ls;
 364       {
 365         ResourceMark rm;
 366         log.print("(%d) %s <--- referenced from:  %s",
 367                   _level, obj->klass()->external_name(),
 368                   CompressedOops::is_null(_orig_referencing_obj) ?
 369                          "" : _orig_referencing_obj->klass()->external_name());
 370         obj->print_on(out);
 371       }
 372 
 373       if (MetaspaceShared::is_archive_object(obj)) {
 374         // The current oop is an archived oop, nothing needs to be done
 375         log.print("--- object is already archived ---");
 376         return;
 377       }
 378 
 379       size_t field_delta = pointer_delta(
 380         p, _orig_referencing_obj, sizeof(char));
 381       T* new_p = (T*)(address(_archived_referencing_obj) + field_delta);
 382       oop archived = MetaspaceShared::find_archived_heap_object(obj);
 383       if (archived != NULL) {
 384         // There is an archived copy existing, update reference to point
 385         // to the archived copy
 386         RawAccess<IS_NOT_NULL>::oop_store(new_p, archived);
 387         log.print(
 388           "--- found existing archived copy, store archived " PTR_FORMAT " in " PTR_FORMAT,
 389           p2i(archived), p2i(new_p));
 390         return;
 391       }
 392 
 393       int l = _level + 1;
 394       Thread* THREAD = Thread::current();
 395       // Archive the current oop before iterating through its references
 396       archived = MetaspaceShared::archive_heap_object(obj, THREAD);
 397       if (archived == NULL) {
 398         ResourceMark rm;
 399         LogTarget(Error, cds, heap) log_err;
 400         LogStream ls_err(log_err);
 401         outputStream* out_err = &ls_err;
 402         log_err.print("Failed to archive %s object ("
 403                       PTR_FORMAT "), size[" SIZE_FORMAT "] in sub-graph",
 404                       obj->klass()->external_name(), p2i(obj), (size_t)obj->size());
 405         obj->print_on(out_err);
 406         vm_exit(1);
 407       }
 408       assert(MetaspaceShared::is_archive_object(archived), "must be archived");
 409       log.print("=== archiving oop " PTR_FORMAT " ==> " PTR_FORMAT,
 410                  p2i(obj), p2i(archived));
 411 
 412       // Following the references in the current oop and archive any
 413       // encountered objects during the process
 414       WalkOopAndArchiveClosure walker(l, _subgraph_info, obj, archived);
 415       obj->oop_iterate(&walker);
 416 
 417       // Update the reference in the archived copy of the referencing object
 418       RawAccess<IS_NOT_NULL>::oop_store(new_p, archived);
 419       log.print("=== store archived " PTR_FORMAT " in " PTR_FORMAT,
 420                 p2i(archived), p2i(new_p));
 421 
 422       // Add the klass to the list of classes that need to be loaded before
 423       // module system initialization
 424       Klass *orig_k = obj->klass();
 425       Klass *relocated_k = archived->klass();
 426       _subgraph_info->add_subgraph_object_klass(orig_k, relocated_k);
 427     }
 428   }
 429 };
 430 
 431 //
 432 // Start from the given static field in a java mirror and archive the
 433 // complete sub-graph of java heap objects that are reached directly
 434 // or indirectly from the starting object by following references.
 435 // Currently, only ArchivedModuleGraph class instance (mirror) has archived
 436 // object subgraphs. Sub-graph archiving restrictions (current):
 437 //
 438 // - All classes of objects in the archived sub-graph (including the
 439 //   entry class) must be boot class only.
 440 // - No java.lang.Class instance (java mirror) can be included inside
 441 //   an archived sub-graph. Mirror can only be the sub-graph entry object.
 442 //
 443 // The Java heap object sub-graph archiving process (see
 444 // WalkOopAndArchiveClosure):
 445 //
 446 // 1) Java object sub-graph archiving starts from a given static field
 447 // within a Class instance (java mirror). If the static field is a
 448 // refererence field and points to a non-null java object, proceed to
 449 // the next step.
 450 //
 451 // 2) Archives the referenced java object. If an archived copy of the
 452 // current object already exists, updates the pointer in the archived
 453 // copy of the referencing object to point to the current archived object.
 454 // Otherwise, proceed to the next step.
 455 //
 456 // 3) Follows all references within the current java object and recursively
 457 // archive the sub-graph of objects starting from each reference.
 458 //
 459 // 4) Updates the pointer in the archived copy of referencing object to
 460 //    point to the current archived object.
 461 //
 462 // 5) The Klass of the current java object is added to the list of Klasses
 463 // for loading and initialzing before any object in the archived graph can
 464 // be accessed at runtime.
 465 //
 466 void HeapShared::archive_reachable_objects_from_static_field(Klass *k,
 467                                                              int field_offset,
 468                                                              BasicType field_type,
 469                                                              TRAPS) {
 470   assert(DumpSharedSpaces, "dump time only");
 471   assert(k->is_instance_klass(), "sanity");
 472   assert(InstanceKlass::cast(k)->is_shared_boot_class(),
 473          "must be boot class");
 474 
 475   oop m = k->java_mirror();
 476   oop archived_m = MetaspaceShared::find_archived_heap_object(m);
 477   if (CompressedOops::is_null(archived_m)) {
 478     return;
 479   }
 480 
 481   if (field_type == T_OBJECT || field_type == T_ARRAY) {
 482     // obtain k's subGraph Info
 483     KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
 484 
 485     // get the object referenced by the field
 486     oop f = m->obj_field(field_offset);
 487     if (!CompressedOops::is_null(f)) {
 488       LogTarget(Debug, cds, heap) log;
 489       LogStream ls(log);
 490       outputStream* out = &ls;
 491       log.print("Start from: ");
 492       f->print_on(out);
 493 
 494       // get the archived copy of the field referenced object
 495       oop af = MetaspaceShared::archive_heap_object(f, THREAD);
 496       if (af == NULL) {
 497         // Skip archiving the sub-graph referenced from the current entry field.
 498         ResourceMark rm;
 499         log_info(cds, heap)(
 500           "Cannot archive the sub-graph referenced from %s object ("
 501           PTR_FORMAT ") size[" SIZE_FORMAT "], skipped.",
 502           f->klass()->external_name(), p2i(f), (size_t)f->size());
 503         return;
 504       }
 505       if (!MetaspaceShared::is_archive_object(f)) {
 506         WalkOopAndArchiveClosure walker(1, subgraph_info, f, af);
 507         f->oop_iterate(&walker);
 508       }
 509 
 510       // The field value is not preserved in the archived mirror.
 511       // Record the field as a new subGraph entry point. The recorded
 512       // information is restored from the archive at runtime.
 513       subgraph_info->add_subgraph_entry_field(field_offset, af);
 514       Klass *relocated_k = af->klass();
 515       Klass *orig_k = f->klass();
 516       subgraph_info->add_subgraph_object_klass(orig_k, relocated_k);
 517       ResourceMark rm;
 518       log_info(cds, heap)(
 519           "Archived the sub-graph referenced from %s object " PTR_FORMAT,
 520           f->klass()->external_name(), p2i(f));
 521     } else {
 522       // The field contains null, we still need to record the entry point,
 523       // so it can be restored at runtime.
 524       subgraph_info->add_subgraph_entry_field(field_offset, NULL);
 525     }
 526   } else {
 527     ShouldNotReachHere();
 528   }
 529 }
 530 
 531 struct ArchivableStaticFieldInfo {
 532   const char* class_name;
 533   const char* field_name;
 534   InstanceKlass* klass;
 535   int offset;
 536   BasicType type;
 537 };
 538 
 539 // If you add new entries to this table, you should know what you're doing!
 540 static ArchivableStaticFieldInfo archivable_static_fields[] = {
 541   {"jdk/internal/module/ArchivedModuleGraph",  "archivedSystemModules"},
 542   {"jdk/internal/module/ArchivedModuleGraph",  "archivedModuleFinder"},
 543   {"jdk/internal/module/ArchivedModuleGraph",  "archivedMainModule"},
 544   {"jdk/internal/module/ArchivedModuleGraph",  "archivedConfiguration"},
 545   {"java/util/ImmutableCollections$ListN",     "EMPTY_LIST"},
 546   {"java/util/ImmutableCollections$MapN",      "EMPTY_MAP"},
 547   {"java/util/ImmutableCollections$SetN",      "EMPTY_SET"},
 548   {"java/lang/Integer$IntegerCache",           "archivedCache"},
 549   {"java/lang/module/Configuration",           "EMPTY_CONFIGURATION"},
 550 };
 551 
 552 const static int num_archivable_static_fields = sizeof(archivable_static_fields) / sizeof(ArchivableStaticFieldInfo);
 553 
 554 class ArchivableStaticFieldFinder: public FieldClosure {
 555   InstanceKlass* _ik;
 556   Symbol* _field_name;
 557   bool _found;
 558   int _offset;
 559   BasicType _type;
 560 public:
 561   ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) :
 562     _ik(ik), _field_name(field_name), _found(false), _offset(-1), _type(T_ILLEGAL) {}
 563 
 564   virtual void do_field(fieldDescriptor* fd) {
 565     if (fd->name() == _field_name) {
 566       assert(!_found, "fields cannot be overloaded");
 567       _found = true;
 568       _offset = fd->offset();
 569       _type = fd->field_type();
 570       assert(_type == T_OBJECT || _type == T_ARRAY, "can archive only obj or array fields");
 571     }
 572   }
 573   bool found()     { return _found;  }
 574   int offset()     { return _offset; }
 575   BasicType type() { return _type;   }
 576 };
 577 
 578 void HeapShared::init_archivable_static_fields(Thread* THREAD) {
 579   for (int i = 0; i < num_archivable_static_fields; i++) {
 580     ArchivableStaticFieldInfo* info = &archivable_static_fields[i];
 581     TempNewSymbol class_name =  SymbolTable::new_symbol(info->class_name, THREAD);
 582     TempNewSymbol field_name =  SymbolTable::new_symbol(info->field_name, THREAD);
 583 
 584     Klass* k = SystemDictionary::resolve_or_null(class_name, THREAD);
 585     assert(k != NULL && !HAS_PENDING_EXCEPTION, "class must exist");
 586     InstanceKlass* ik = InstanceKlass::cast(k);
 587 
 588     ArchivableStaticFieldFinder finder(ik, field_name);
 589     ik->do_local_static_fields(&finder);
 590     assert(finder.found(), "field must exist");
 591 
 592     info->klass = ik;
 593     info->offset = finder.offset();
 594     info->type = finder.type();
 595   }
 596 }
 597 
 598 void HeapShared::archive_module_graph_objects(Thread* THREAD) {
 599   for (int i = 0; i < num_archivable_static_fields; i++) {
 600     ArchivableStaticFieldInfo* info = &archivable_static_fields[i];
 601     archive_reachable_objects_from_static_field(info->klass, info->offset, info->type, CHECK);
 602   }
 603 }
 604 #endif // INCLUDE_CDS_JAVA_HEAP