1 /*
  2  * Copyright (c) 2018, 2025, 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 #ifndef SHARE_CDS_HEAPSHARED_HPP
 26 #define SHARE_CDS_HEAPSHARED_HPP
 27 
 28 #include "cds/aotMetaspace.hpp"
 29 #include "cds/dumpTimeClassInfo.hpp"
 30 #include "classfile/compactHashtable.hpp"
 31 #include "classfile/javaClasses.hpp"
 32 #include "gc/shared/gc_globals.hpp"
 33 #include "memory/allocation.hpp"
 34 #include "memory/allStatic.hpp"
 35 #include "oops/compressedOops.hpp"
 36 #include "oops/oop.hpp"
 37 #include "oops/oopHandle.hpp"
 38 #include "oops/oopsHierarchy.hpp"
 39 #include "utilities/growableArray.hpp"
 40 #include "utilities/hashTable.hpp"
 41 
 42 #if INCLUDE_CDS_JAVA_HEAP
 43 class DumpedInternedStrings;
 44 class FileMapInfo;
 45 class KlassSubGraphInfo;
 46 class MetaspaceObjToOopHandleTable;
 47 class ResourceBitMap;
 48 
 49 struct ArchivableStaticFieldInfo;
 50 
 51 #define ARCHIVED_BOOT_LAYER_CLASS "jdk/internal/module/ArchivedBootLayer"
 52 #define ARCHIVED_BOOT_LAYER_FIELD "archivedBootLayer"
 53 
 54 // A dump time sub-graph info for Klass _k. It includes the entry points
 55 // (static fields in _k's mirror) of the archived sub-graphs reachable
 56 // from _k's mirror. It also contains a list of Klasses of the objects
 57 // within the sub-graphs.
 58 class KlassSubGraphInfo: public CHeapObj<mtClass> {
 59  private:
 60   // The class that contains the static field(s) as the entry point(s)
 61   // of archived object sub-graph(s).
 62   Klass* _k;
 63   // A list of classes need to be loaded and initialized before the archived
 64   // object sub-graphs can be accessed at runtime.
 65   GrowableArray<Klass*>* _subgraph_object_klasses;
 66   // A list of _k's static fields as the entry points of archived sub-graphs.
 67   // For each entry field, it is a tuple of field_offset, field_value
 68   GrowableArray<int>* _subgraph_entry_fields;
 69 
 70   // Does this KlassSubGraphInfo belong to the archived full module graph
 71   bool _is_full_module_graph;
 72 
 73   // Does this KlassSubGraphInfo references any classes that were loaded while
 74   // JvmtiExport::is_early_phase()!=true. If so, this KlassSubGraphInfo cannot be
 75   // used at runtime if JVMTI ClassFileLoadHook is enabled.
 76   bool _has_non_early_klasses;
 77   static bool is_non_early_klass(Klass* k);
 78   static void check_allowed_klass(InstanceKlass* ik);
 79  public:
 80   KlassSubGraphInfo(Klass* k, bool is_full_module_graph) :
 81     _k(k),  _subgraph_object_klasses(nullptr),
 82     _subgraph_entry_fields(nullptr),
 83     _is_full_module_graph(is_full_module_graph),
 84     _has_non_early_klasses(false) {}
 85 
 86   ~KlassSubGraphInfo() {
 87     if (_subgraph_object_klasses != nullptr) {
 88       delete _subgraph_object_klasses;
 89     }
 90     if (_subgraph_entry_fields != nullptr) {
 91       delete _subgraph_entry_fields;
 92     }
 93   };
 94 
 95   Klass* klass()            { return _k; }
 96   GrowableArray<Klass*>* subgraph_object_klasses() {
 97     return _subgraph_object_klasses;
 98   }
 99   GrowableArray<int>* subgraph_entry_fields() {
100     return _subgraph_entry_fields;
101   }
102   void add_subgraph_entry_field(int static_field_offset, oop v);
103   void add_subgraph_object_klass(Klass *orig_k);
104   int num_subgraph_object_klasses() {
105     return _subgraph_object_klasses == nullptr ? 0 :
106            _subgraph_object_klasses->length();
107   }
108   bool is_full_module_graph() const { return _is_full_module_graph; }
109   bool has_non_early_klasses() const { return _has_non_early_klasses; }
110 };
111 
112 // An archived record of object sub-graphs reachable from static
113 // fields within _k's mirror. The record is reloaded from the archive
114 // at runtime.
115 class ArchivedKlassSubGraphInfoRecord {
116  private:
117   Klass* _k;
118   bool _is_full_module_graph;
119   bool _has_non_early_klasses;
120 
121   // contains pairs of field offset and value for each subgraph entry field
122   Array<int>* _entry_field_records;
123 
124   // klasses of objects in archived sub-graphs referenced from the entry points
125   // (static fields) in the containing class
126   Array<Klass*>* _subgraph_object_klasses;
127  public:
128   ArchivedKlassSubGraphInfoRecord() :
129     _k(nullptr), _entry_field_records(nullptr), _subgraph_object_klasses(nullptr) {}
130   void init(KlassSubGraphInfo* info);
131   Klass* klass() const { return _k; }
132   Array<int>* entry_field_records() const { return _entry_field_records; }
133   Array<Klass*>* subgraph_object_klasses() const { return _subgraph_object_klasses; }
134   bool is_full_module_graph() const { return _is_full_module_graph; }
135   bool has_non_early_klasses() const { return _has_non_early_klasses; }
136 };
137 #endif // INCLUDE_CDS_JAVA_HEAP
138 
139 enum class HeapArchiveMode {
140   _uninitialized,
141   _mapping,
142   _streaming
143 };
144 
145 class ArchiveMappedHeapHeader {
146   size_t           _ptrmap_start_pos; // The first bit in the ptrmap corresponds to this position in the heap.
147   size_t           _oopmap_start_pos; // The first bit in the oopmap corresponds to this position in the heap.
148   HeapRootSegments _root_segments;    // Heap root segments info
149 
150 public:
151   ArchiveMappedHeapHeader();
152   ArchiveMappedHeapHeader(size_t ptrmap_start_pos,
153                           size_t oopmap_start_pos,
154                           HeapRootSegments root_segments);
155 
156   size_t ptrmap_start_pos() const { return _ptrmap_start_pos; }
157   size_t oopmap_start_pos() const { return _oopmap_start_pos; }
158   HeapRootSegments root_segments() const { return _root_segments; }
159 
160   // This class is trivially copyable and assignable.
161   ArchiveMappedHeapHeader(const ArchiveMappedHeapHeader&) = default;
162   ArchiveMappedHeapHeader& operator=(const ArchiveMappedHeapHeader&) = default;
163 };
164 
165 
166 class ArchiveStreamedHeapHeader {
167   size_t _forwarding_offset;                      // Offset of forwarding information in the heap region.
168   size_t _roots_offset;                           // Start position for the roots
169   size_t _root_highest_object_index_table_offset; // Offset of root dfs depth information
170   size_t _num_roots;                              // Number of embedded roots
171   size_t _num_archived_objects;                   // The number of archived heap objects
172 
173 public:
174   ArchiveStreamedHeapHeader();
175   ArchiveStreamedHeapHeader(size_t forwarding_offset,
176                             size_t roots_offset,
177                             size_t num_roots,
178                             size_t root_highest_object_index_table_offset,
179                             size_t num_archived_objects);
180 
181   size_t forwarding_offset() const { return _forwarding_offset; }
182   size_t roots_offset() const { return _roots_offset; }
183   size_t num_roots() const { return _num_roots; }
184   size_t root_highest_object_index_table_offset() const { return _root_highest_object_index_table_offset; }
185   size_t num_archived_objects() const { return _num_archived_objects; }
186 
187   // This class is trivially copyable and assignable.
188   ArchiveStreamedHeapHeader(const ArchiveStreamedHeapHeader&) = default;
189   ArchiveStreamedHeapHeader& operator=(const ArchiveStreamedHeapHeader&) = default;
190 };
191 
192 class ArchiveMappedHeapInfo {
193   MemRegion _buffer_region;             // Contains the archived objects to be written into the CDS archive.
194   CHeapBitMap _oopmap;
195   CHeapBitMap _ptrmap;
196   HeapRootSegments _root_segments;
197   size_t _oopmap_start_pos;             // How many zeros were removed from the beginning of the bit map?
198   size_t _ptrmap_start_pos;             // How many zeros were removed from the beginning of the bit map?
199 
200 public:
201   ArchiveMappedHeapInfo() :
202     _buffer_region(),
203     _oopmap(128, mtClassShared),
204     _ptrmap(128, mtClassShared),
205     _root_segments(),
206     _oopmap_start_pos(),
207     _ptrmap_start_pos() {}
208   bool is_used() { return !_buffer_region.is_empty(); }
209 
210   MemRegion buffer_region() { return _buffer_region; }
211   void set_buffer_region(MemRegion r) { _buffer_region = r; }
212 
213   char* buffer_start() { return (char*)_buffer_region.start(); }
214   size_t buffer_byte_size() { return _buffer_region.byte_size();    }
215 
216   CHeapBitMap* oopmap() { return &_oopmap; }
217   CHeapBitMap* ptrmap() { return &_ptrmap; }
218 
219   void set_oopmap_start_pos(size_t start_pos) { _oopmap_start_pos = start_pos; }
220   void set_ptrmap_start_pos(size_t start_pos) { _ptrmap_start_pos = start_pos; }
221 
222   void set_root_segments(HeapRootSegments segments) { _root_segments = segments; };
223   HeapRootSegments root_segments() { return _root_segments; }
224 
225   ArchiveMappedHeapHeader create_header();
226 };
227 
228 class ArchiveStreamedHeapInfo {
229   MemRegion _buffer_region;             // Contains the archived objects to be written into the CDS archive.
230   CHeapBitMap _oopmap;
231   size_t _roots_offset;                 // Offset of the HeapShared::roots() object, from the bottom
232                                         // of the archived heap objects, in bytes.
233   size_t _num_roots;
234 
235   size_t _forwarding_offset;            // Offset of forwarding information from the bottom
236   size_t _root_highest_object_index_table_offset; // Offset to root dfs depth information
237   size_t _num_archived_objects;         // The number of archived objects written into the CDS archive.
238 
239 public:
240   ArchiveStreamedHeapInfo()
241     : _buffer_region(),
242       _oopmap(128, mtClassShared),
243       _roots_offset(),
244       _forwarding_offset(),
245       _root_highest_object_index_table_offset(),
246       _num_archived_objects() {}
247 
248   bool is_used() { return !_buffer_region.is_empty(); }
249 
250   void set_buffer_region(MemRegion r) { _buffer_region = r; }
251   MemRegion buffer_region() { return _buffer_region; }
252   char* buffer_start() { return (char*)_buffer_region.start(); }
253   size_t buffer_byte_size() { return _buffer_region.byte_size();    }
254 
255   CHeapBitMap* oopmap() { return &_oopmap; }
256   void set_roots_offset(size_t n) { _roots_offset = n; }
257   size_t roots_offset() { return _roots_offset; }
258   void set_num_roots(size_t n) { _num_roots = n; }
259   size_t num_roots() { return _num_roots; }
260   void set_forwarding_offset(size_t n) { _forwarding_offset = n; }
261   void set_root_highest_object_index_table_offset(size_t n) { _root_highest_object_index_table_offset = n; }
262   void set_num_archived_objects(size_t n) { _num_archived_objects = n; }
263   size_t num_archived_objects() { return _num_archived_objects; }
264 
265   ArchiveStreamedHeapHeader create_header();
266 };
267 
268 class HeapShared: AllStatic {
269   friend class VerifySharedOopClosure;
270 
271 public:
272   static void initialize_loading_mode(HeapArchiveMode mode) NOT_CDS_JAVA_HEAP_RETURN;
273   static void initialize_writing_mode() NOT_CDS_JAVA_HEAP_RETURN;
274 
275   inline static bool is_loading() NOT_CDS_JAVA_HEAP_RETURN_(false);
276 
277   inline static bool is_loading_streaming_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
278   inline static bool is_loading_mapping_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
279 
280   inline static bool is_writing() NOT_CDS_JAVA_HEAP_RETURN_(false);
281 
282   inline static bool is_writing_streaming_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
283   inline static bool is_writing_mapping_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
284 
285   static bool is_subgraph_root_class(InstanceKlass* ik);
286 
287   // Scratch objects for archiving Klass::java_mirror()
288   static oop scratch_java_mirror(BasicType t)     NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
289   static oop scratch_java_mirror(Klass* k)        NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
290   static oop scratch_java_mirror(oop java_mirror) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
291   static bool is_archived_boot_layer_available(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN_(false);
292 
293   static bool is_archived_heap_in_use() NOT_CDS_JAVA_HEAP_RETURN_(false);
294   static bool can_use_archived_heap() NOT_CDS_JAVA_HEAP_RETURN_(false);
295   static bool is_too_large_to_archive(size_t size);
296   static bool is_string_too_large_to_archive(oop string);
297   static bool is_too_large_to_archive(oop obj);
298 
299   static void initialize_streaming() NOT_CDS_JAVA_HEAP_RETURN;
300   static void enable_gc() NOT_CDS_JAVA_HEAP_RETURN;
301   static void materialize_thread_object() NOT_CDS_JAVA_HEAP_RETURN;
302   static void add_to_dumped_interned_strings(oop string) NOT_CDS_JAVA_HEAP_RETURN;
303   static void finalize_initialization(FileMapInfo* static_mapinfo) NOT_CDS_JAVA_HEAP_RETURN;
304 
305 private:
306 #if INCLUDE_CDS_JAVA_HEAP
307   static HeapArchiveMode _heap_load_mode;
308   static HeapArchiveMode _heap_write_mode;
309 
310   // statistics
311   constexpr static int ALLOC_STAT_SLOTS = 16;
312   static size_t _alloc_count[ALLOC_STAT_SLOTS];
313   static size_t _alloc_size[ALLOC_STAT_SLOTS];
314   static size_t _total_obj_count;
315   static size_t _total_obj_size; // in HeapWords
316 
317   static void count_allocation(size_t size);
318   static void print_stats();
319 public:
320   static void debug_trace();
321   static unsigned oop_hash(oop const& p);
322   static unsigned oop_handle_hash(OopHandle const& oh);
323   static unsigned oop_handle_hash_raw(OopHandle const& oh);
324   static bool oop_handle_equals(const OopHandle& a, const OopHandle& b);
325   static unsigned string_oop_hash(oop const& string) {
326     return java_lang_String::hash_code(string);
327   }
328 
329   class CopyKlassSubGraphInfoToArchive;
330 
331   class CachedOopInfo {
332     // Used by CDSHeapVerifier.
333     OopHandle _orig_referrer;
334 
335     // The location of this object inside ArchiveHeapWriter::_buffer
336     size_t _buffer_offset;
337 
338     // One or more fields in this object are pointing to non-null oops.
339     bool _has_oop_pointers;
340 
341     // One or more fields in this object are pointing to MetaspaceObj
342     bool _has_native_pointers;
343   public:
344     CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers)
345       : _orig_referrer(orig_referrer),
346         _buffer_offset(0),
347         _has_oop_pointers(has_oop_pointers),
348         _has_native_pointers(false) {}
349     oop orig_referrer() const;
350     void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
351     size_t buffer_offset()          const { return _buffer_offset;   }
352     bool has_oop_pointers()         const { return _has_oop_pointers; }
353     bool has_native_pointers()      const { return _has_native_pointers; }
354     void set_has_native_pointers()        { _has_native_pointers = true; }
355   };
356 
357 private:
358   static const int INITIAL_TABLE_SIZE = 15889; // prime number
359   static const int MAX_TABLE_SIZE     = 1000000;
360   typedef ResizeableHashTable<OopHandle, CachedOopInfo,
361       AnyObj::C_HEAP,
362       mtClassShared,
363       HeapShared::oop_handle_hash_raw,
364       HeapShared::oop_handle_equals> ArchivedObjectCache;
365   static ArchivedObjectCache* _archived_object_cache;
366 
367   class DumpTimeKlassSubGraphInfoTable
368     : public HashTable<Klass*, KlassSubGraphInfo,
369                                137, // prime number
370                                AnyObj::C_HEAP,
371                                mtClassShared,
372                                DumpTimeSharedClassTable_hash> {};
373 
374 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
375   inline static bool record_equals_compact_hashtable_entry(
376        const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
377     return (value->klass() == key);
378   }
379 
380 private:
381   typedef OffsetCompactHashtable<
382     const Klass*,
383     const ArchivedKlassSubGraphInfoRecord*,
384     record_equals_compact_hashtable_entry
385     > RunTimeKlassSubGraphInfoTable;
386 
387   static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table;
388   static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table;
389 
390   static CachedOopInfo make_cached_oop_info(oop obj, oop referrer);
391   static ArchivedKlassSubGraphInfoRecord* archive_subgraph_info(KlassSubGraphInfo* info);
392   static void archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
393                                        bool is_full_module_graph);
394 
395   // Archive object sub-graph starting from the given static field
396   // in Klass k's mirror.
397   static void archive_reachable_objects_from_static_field(
398     InstanceKlass* k, const char* klass_name,
399     int field_offset, const char* field_name);
400 
401   static void verify_subgraph_from_static_field(
402     InstanceKlass* k, int field_offset) PRODUCT_RETURN;
403   static void verify_reachable_objects_from(oop obj) PRODUCT_RETURN;
404   static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
405   static void check_special_subgraph_classes();
406 
407   static KlassSubGraphInfo* init_subgraph_info(Klass *k, bool is_full_module_graph);
408   static KlassSubGraphInfo* get_subgraph_info(Klass *k);
409 
410   static void init_subgraph_entry_fields(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
411   static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], TRAPS);
412 
413   // UseCompressedOops only: Used by decode_from_archive
414   static address _narrow_oop_base;
415   static int     _narrow_oop_shift;
416 
417   // !UseCompressedOops only: used to relocate pointers to the archived objects
418   static ptrdiff_t _runtime_delta;
419 
420   typedef ResizeableHashTable<oop, bool,
421       AnyObj::C_HEAP,
422       mtClassShared,
423       HeapShared::oop_hash> SeenObjectsTable;
424 
425   static SeenObjectsTable *_seen_objects_table;
426 
427   // The "special subgraph" contains all the archived objects that are reachable
428   // from the following roots:
429   //    - interned strings
430   //    - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
431   //    - ConstantPool::resolved_references()
432   //    - Universe::<xxx>_exception_instance()
433   static KlassSubGraphInfo* _dump_time_special_subgraph;              // for collecting info during dump time
434   static ArchivedKlassSubGraphInfoRecord* _run_time_special_subgraph; // for initializing classes during run time.
435 
436   static GrowableArrayCHeap<oop, mtClassShared>* _pending_roots;
437   static OopHandle _scratch_basic_type_mirrors[T_VOID+1];
438   static MetaspaceObjToOopHandleTable* _scratch_objects_table;
439 
440   static void init_seen_objects_table() {
441     assert(_seen_objects_table == nullptr, "must be");
442     _seen_objects_table = new (mtClass)SeenObjectsTable(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
443   }
444   static void delete_seen_objects_table() {
445     assert(_seen_objects_table != nullptr, "must be");
446     delete _seen_objects_table;
447     _seen_objects_table = nullptr;
448   }
449 
450   // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
451   static size_t _num_new_walked_objs;
452   static size_t _num_new_archived_objs;
453   static size_t _num_old_recorded_klasses;
454 
455   // Statistics (for all archived subgraphs)
456   static size_t _num_total_subgraph_recordings;
457   static size_t _num_total_walked_objs;
458   static size_t _num_total_archived_objs;
459   static size_t _num_total_recorded_klasses;
460   static size_t _num_total_verifications;
461 
462   static void start_recording_subgraph(InstanceKlass *k, const char* klass_name,
463                                        bool is_full_module_graph);
464   static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
465 
466   static bool has_been_seen_during_subgraph_recording(oop obj);
467   static void set_has_been_seen_during_subgraph_recording(oop obj);
468   static bool archive_object(oop obj, oop referrer, KlassSubGraphInfo* subgraph_info);
469 
470   static void resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]);
471   static void resolve_classes_for_subgraph_of(JavaThread* current, Klass* k);
472   static void clear_archived_roots_of(Klass* k);
473   static const ArchivedKlassSubGraphInfoRecord*
474                resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS);
475   static void resolve_or_init(const char* klass_name, bool do_init, TRAPS);
476   static void resolve_or_init(Klass* k, bool do_init, TRAPS);
477   static void init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record);
478 
479   static bool has_been_archived(oop orig_obj);
480   static void prepare_resolved_references();
481   static void archive_strings();
482   static void archive_subgraphs();
483   static void copy_java_mirror(oop orig_mirror, oop scratch_m);
484 
485   // PendingOop and PendingOopStack are used for recursively discovering all cacheable
486   // heap objects. The recursion is done using PendingOopStack so we won't overflow the
487   // C stack with deep reference chains.
488   class PendingOop {
489     oop _obj;
490     oop _referrer;
491     int _level;
492 
493   public:
494     PendingOop() : _obj(nullptr), _referrer(nullptr), _level(-1) {}
495     PendingOop(oop obj, oop referrer, int level) : _obj(obj), _referrer(referrer), _level(level) {}
496 
497     oop obj()      const { return _obj; }
498     oop referrer() const { return _referrer; }
499     int level()    const { return _level; }
500   };
501 
502   class OopFieldPusher;
503   using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
504 
505   static PendingOop _object_being_archived;
506   static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
507                               oop orig_obj, oop referrer);
508 
509  public:
510   static void reset_archived_object_states(TRAPS);
511   static void create_archived_object_cache() {
512     _archived_object_cache =
513       new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
514   }
515   static void destroy_archived_object_cache() {
516     delete _archived_object_cache;
517     _archived_object_cache = nullptr;
518   }
519   static ArchivedObjectCache* archived_object_cache() {
520     return _archived_object_cache;
521   }
522 
523   static CachedOopInfo* get_cached_oop_info(oop orig_obj);
524 
525   static int archive_exception_instance(oop exception);
526 
527   static bool archive_reachable_objects_from(int level,
528                                              KlassSubGraphInfo* subgraph_info,
529                                              oop orig_obj);
530 
531   static bool is_dumped_interned_string(oop o);
532 
533   // Scratch objects for archiving Klass::java_mirror()
534   static void set_scratch_java_mirror(Klass* k, oop mirror);
535   static void remove_scratch_objects(Klass* k);
536   static bool is_metadata_field(oop src_obj, int offset);
537   template <typename T> static void do_metadata_offsets(oop src_obj, T callback);
538   static void remap_dumped_metadata(oop src_obj, address archived_object);
539   inline static void remap_loaded_metadata(oop obj);
540   inline static oop maybe_remap_referent(bool is_java_lang_ref, size_t field_offset, oop referent);
541   static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
542   static void set_has_native_pointers(oop src_obj);
543   static uintptr_t archive_location(oop src_obj);
544 
545   // We use the HeapShared::roots() array to make sure that objects stored in the
546   // archived heap region are not prematurely collected. These roots include:
547   //
548   //    - mirrors of classes that have not yet been loaded.
549   //    - ConstantPool::resolved_references() of classes that have not yet been loaded.
550   //    - ArchivedKlassSubGraphInfoRecords that have not been initialized
551   //    - java.lang.Module objects that have not yet been added to the module graph
552   //
553   // When a mirror M becomes referenced by a newly loaded class K, M will be removed
554   // from HeapShared::roots() via clear_root(), and K will be responsible for
555   // keeping M alive.
556   //
557   // Other types of roots are also cleared similarly when they become referenced.
558 
559   // Dump-time only. Returns the index of the root, which can be used at run time to read
560   // the root using get_root(index, ...).
561   static int append_root(oop obj);
562   static GrowableArrayCHeap<oop, mtClassShared>* pending_roots() { return _pending_roots; }
563 
564   // Dump-time and runtime
565   static objArrayOop root_segment(int segment_idx);
566   static oop get_root(int index, bool clear=false);
567 
568   // Run-time only
569   static void clear_root(int index);
570 
571   static void get_segment_indexes(int index, int& segment_index, int& internal_index);
572 
573   static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
574 #endif // INCLUDE_CDS_JAVA_HEAP
575 
576  public:
577   static void finish_materialize_objects() NOT_CDS_JAVA_HEAP_RETURN;
578 
579   static void write_heap(ArchiveMappedHeapInfo* mapped_heap_info, ArchiveStreamedHeapInfo* streamed_heap_info) NOT_CDS_JAVA_HEAP_RETURN;
580   static objArrayOop scratch_resolved_references(ConstantPool* src);
581   static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
582   static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
583   static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
584   static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
585   static bool is_heap_region(int idx) {
586     CDS_JAVA_HEAP_ONLY(return (idx == AOTMetaspace::hp);)
587     NOT_CDS_JAVA_HEAP_RETURN_(false);
588   }
589   static void delete_tables_with_raw_oops() NOT_CDS_JAVA_HEAP_RETURN;
590 
591   static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
592   static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
593 
594   static void init_for_dumping(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
595   static void init_heap_writer() NOT_CDS_JAVA_HEAP_RETURN;
596   static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
597   static void serialize_tables(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
598 
599 #ifndef PRODUCT
600   static bool is_a_test_class_in_unnamed_module(Klass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
601   static void initialize_test_class_from_archive(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
602 #endif
603 
604   static void initialize_java_lang_invoke(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
605   static void init_classes_for_special_subgraph(Handle loader, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
606 
607   static bool is_lambda_form_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
608   static bool is_lambda_proxy_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
609   static bool is_string_concat_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
610   static bool is_archivable_hidden_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
611 
612   // Used by AOTArtifactFinder
613   static void start_scanning_for_oops();
614   static void end_scanning_for_oops();
615   static void scan_java_class(Klass* k);
616   static void scan_java_mirror(oop orig_mirror);
617   static void copy_and_rescan_aot_inited_mirror(InstanceKlass* ik);
618 
619   static void log_heap_roots();
620 
621   static intptr_t log_target_location(oop source_oop);
622   static void log_oop_info(outputStream* st, oop source_oop, address archived_object_start, address archived_object_end);
623   static void log_oop_info(outputStream* st, oop source_oop);
624   static void log_oop_details(oop source_oop, address buffered_addr);
625 };
626 
627 #endif // SHARE_CDS_HEAPSHARED_HPP