1 /*
2 * Copyright (c) 2018, 2026, 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/cds_globals.hpp"
30 #include "cds/dumpTimeClassInfo.hpp"
31 #include "classfile/compactHashtable.hpp"
32 #include "classfile/javaClasses.hpp"
33 #include "gc/shared/gc_globals.hpp"
34 #include "memory/allocation.hpp"
35 #include "memory/allStatic.hpp"
36 #include "oops/compressedOops.hpp"
37 #include "oops/oop.hpp"
38 #include "oops/oopHandle.hpp"
39 #include "oops/oopsHierarchy.hpp"
40 #include "utilities/growableArray.hpp"
41 #include "utilities/hashTable.hpp"
42
43 #if INCLUDE_CDS_JAVA_HEAP
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 archive_interned_string(oop string);
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 bool oop_handle_equals(const OopHandle& a, const OopHandle& b);
323
324 class CopyKlassSubGraphInfoToArchive;
325
326 class CachedOopInfo {
327 // Used by CDSHeapVerifier.
328 OopHandle _orig_referrer;
329
330 // The location of this object inside {AOTMappedHeapWriter, AOTStreamedHeapWriter}::_buffer
331 size_t _buffer_offset;
332
333 // One or more fields in this object are pointing to non-null oops.
334 bool _has_oop_pointers;
335
336 // One or more fields in this object are pointing to MetaspaceObj
337 bool _has_native_pointers;
338
339 // >= 0 if this oop has been append to the list of roots
340 int _root_index;
341 public:
342 CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers)
343 : _orig_referrer(orig_referrer),
344 _buffer_offset(0),
345 _has_oop_pointers(has_oop_pointers),
346 _has_native_pointers(false),
347 _root_index(-1) {}
348 oop orig_referrer() const;
349 void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
350 size_t buffer_offset() const { return _buffer_offset; }
351 bool has_oop_pointers() const { return _has_oop_pointers; }
352 bool has_native_pointers() const { return _has_native_pointers; }
353 void set_has_native_pointers() { _has_native_pointers = true; }
354 int root_index() const { return _root_index; }
355 void set_root_index(int i) { _root_index = i; }
356 };
357
358 private:
359 static const int INITIAL_TABLE_SIZE = 15889; // prime number
360 static const int MAX_TABLE_SIZE = 1000000;
361 static bool _use_identity_hash_for_archived_object_cache;
362
363 static unsigned archived_object_cache_hash(OopHandle const& oh);
364
365 typedef ResizeableHashTable<OopHandle, CachedOopInfo,
366 AnyObj::C_HEAP,
367 mtClassShared,
368 HeapShared::archived_object_cache_hash,
369 HeapShared::oop_handle_equals> ArchivedObjectCache;
370 static ArchivedObjectCache* _archived_object_cache;
371
372 class DumpTimeKlassSubGraphInfoTable
373 : public HashTable<Klass*, KlassSubGraphInfo,
374 137, // prime number
375 AnyObj::C_HEAP,
376 mtClassShared,
377 DumpTimeSharedClassTable_hash> {};
378
379 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
380 inline static bool record_equals_compact_hashtable_entry(
381 const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
382 return (value->klass() == key);
383 }
384
385 private:
386 typedef OffsetCompactHashtable<
387 const Klass*,
388 const ArchivedKlassSubGraphInfoRecord*,
389 record_equals_compact_hashtable_entry
390 > RunTimeKlassSubGraphInfoTable;
391
392 static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table;
393 static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table;
394
395 static CachedOopInfo make_cached_oop_info(oop obj, oop referrer);
396 static ArchivedKlassSubGraphInfoRecord* archive_subgraph_info(KlassSubGraphInfo* info);
397 static void archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
398 bool is_full_module_graph);
399
400 // Archive object sub-graph starting from the given static field
401 // in Klass k's mirror.
402 static void archive_reachable_objects_from_static_field(
403 InstanceKlass* k, const char* klass_name,
404 int field_offset, const char* field_name);
405
406 static void verify_subgraph_from_static_field(
407 InstanceKlass* k, int field_offset) PRODUCT_RETURN;
408 static void verify_reachable_objects_from(oop obj) PRODUCT_RETURN;
409 static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
410 static void check_special_subgraph_classes();
411
412 static KlassSubGraphInfo* init_subgraph_info(Klass *k, bool is_full_module_graph);
413 static KlassSubGraphInfo* get_subgraph_info(Klass *k);
414
415 static void init_subgraph_entry_fields(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
416 static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], TRAPS);
417
418 // UseCompressedOops only: Used by decode_from_archive
419 static address _narrow_oop_base;
420 static int _narrow_oop_shift;
421
422 // !UseCompressedOops only: used to relocate pointers to the archived objects
423 static ptrdiff_t _runtime_delta;
424
425 typedef ResizeableHashTable<oop, bool,
426 AnyObj::C_HEAP,
427 mtClassShared,
428 HeapShared::oop_hash> SeenObjectsTable;
429
430 static SeenObjectsTable *_seen_objects_table;
431
432 // The "special subgraph" contains all the archived objects that are reachable
433 // from the following roots:
434 // - interned strings
435 // - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
436 // - ConstantPool::resolved_references()
437 // - Universe::<xxx>_exception_instance()
438 static KlassSubGraphInfo* _dump_time_special_subgraph; // for collecting info during dump time
439 static ArchivedKlassSubGraphInfoRecord* _run_time_special_subgraph; // for initializing classes during run time.
440
441 static GrowableArrayCHeap<oop, mtClassShared>* _pending_roots;
442 static GrowableArrayCHeap<const char*, mtClassShared>* _context; // for debugging unarchivable objects
443 static OopHandle _scratch_basic_type_mirrors[T_VOID+1];
444 static MetaspaceObjToOopHandleTable* _scratch_objects_table;
445
446 static void init_seen_objects_table() {
447 assert(_seen_objects_table == nullptr, "must be");
448 _seen_objects_table = new (mtClass)SeenObjectsTable(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
449 }
450 static void delete_seen_objects_table() {
451 assert(_seen_objects_table != nullptr, "must be");
452 delete _seen_objects_table;
453 _seen_objects_table = nullptr;
454 }
455
456 class ContextMark;
457
458 // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
459 static size_t _num_new_walked_objs;
460 static size_t _num_new_archived_objs;
461 static size_t _num_old_recorded_klasses;
462
463 // Statistics (for all archived subgraphs)
464 static size_t _num_total_subgraph_recordings;
465 static size_t _num_total_walked_objs;
466 static size_t _num_total_archived_objs;
467 static size_t _num_total_recorded_klasses;
468 static size_t _num_total_verifications;
469
470 static void start_recording_subgraph(InstanceKlass *k, const char* klass_name,
471 bool is_full_module_graph);
472 static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
473
474 static bool has_been_seen_during_subgraph_recording(oop obj);
475 static void set_has_been_seen_during_subgraph_recording(oop obj);
476 static bool archive_object(oop obj, oop referrer, KlassSubGraphInfo* subgraph_info);
477
478 static void resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]);
479 static void resolve_classes_for_subgraph_of(JavaThread* current, Klass* k);
480 static void clear_archived_roots_of(Klass* k);
481 static const ArchivedKlassSubGraphInfoRecord*
482 resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS);
483 static void resolve_or_init(const char* klass_name, bool do_init, TRAPS);
484 static void resolve_or_init(Klass* k, bool do_init, TRAPS);
485 static void init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record);
486
487 static bool has_been_archived(oop orig_obj);
488 static void prepare_resolved_references();
489 static void archive_subgraphs();
490 static void copy_java_mirror(oop orig_mirror, oop scratch_m);
491
492 // PendingOop and PendingOopStack are used for recursively discovering all cacheable
493 // heap objects. The recursion is done using PendingOopStack so we won't overflow the
494 // C stack with deep reference chains.
495 class PendingOop {
496 oop _obj;
497 oop _referrer;
498 int _level;
499
500 public:
501 PendingOop() : _obj(nullptr), _referrer(nullptr), _level(-1) {}
502 PendingOop(oop obj, oop referrer, int level) : _obj(obj), _referrer(referrer), _level(level) {}
503
504 oop obj() const { return _obj; }
505 oop referrer() const { return _referrer; }
506 int level() const { return _level; }
507 };
508
509 class OopFieldPusher;
510 using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
511
512 static PendingOop _object_being_archived;
513 static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
514 oop orig_obj, oop referrer);
515
516 public:
517 static void exit_on_error();
518 static void reset_archived_object_states(TRAPS);
519 static void create_archived_object_cache() {
520 _archived_object_cache =
521 new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
522 }
523 static void destroy_archived_object_cache() {
524 delete _archived_object_cache;
525 _archived_object_cache = nullptr;
526 }
527 static void make_archived_object_cache_gc_safe();
528 static ArchivedObjectCache* archived_object_cache() {
529 return _archived_object_cache;
530 }
531
532 static CachedOopInfo* get_cached_oop_info(oop orig_obj);
533
534 static int archive_exception_instance(oop exception);
535
536 static bool archive_reachable_objects_from(int level,
537 KlassSubGraphInfo* subgraph_info,
538 oop orig_obj);
539
540 static bool is_interned_string(oop obj);
541 static bool is_dumped_interned_string(oop o);
542
543 static void track_scratch_object(oop orig_obj, oop scratch_obj);
544
545 // Scratch objects for archiving Klass::java_mirror()
546 static void set_scratch_java_mirror(Klass* k, oop mirror);
547 static void remove_scratch_objects(Klass* k);
548 static bool is_metadata_field(oop src_obj, int offset);
549 template <typename T> static void do_metadata_offsets(oop src_obj, T callback);
550 static void remap_dumped_metadata(oop src_obj, address archived_object);
551 inline static void remap_loaded_metadata(oop obj);
552 inline static oop maybe_remap_referent(bool is_java_lang_ref, size_t field_offset, oop referent);
553 static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
554 static void set_has_native_pointers(oop src_obj);
555 static uintptr_t archive_location(oop src_obj);
556
557 // We use the HeapShared::roots() array to make sure that objects stored in the
558 // archived heap region are not prematurely collected. These roots include:
559 //
560 // - mirrors of classes that have not yet been loaded.
561 // - ConstantPool::resolved_references() of classes that have not yet been loaded.
562 // - ArchivedKlassSubGraphInfoRecords that have not been initialized
563 // - java.lang.Module objects that have not yet been added to the module graph
564 //
565 // When a mirror M becomes referenced by a newly loaded class K, M will be removed
566 // from HeapShared::roots() via clear_root(), and K will be responsible for
567 // keeping M alive.
568 //
569 // Other types of roots are also cleared similarly when they become referenced.
570
571 // Dump-time only. Returns the index of the root, which can be used at run time to read
572 // the root using get_root(index, ...).
573 static int append_root(oop obj);
574
575 // AOT-compile time only.
576 // Returns -1 if obj is not in the heap root set.
577 static int get_root_index(oop obj) NOT_CDS_JAVA_HEAP_RETURN_(-1);
578
579 static GrowableArrayCHeap<oop, mtClassShared>* pending_roots() { return _pending_roots; }
580
581 // Dump-time and runtime
582 static objArrayOop root_segment(int segment_idx);
583 static oop get_root(int index, bool clear=false);
584
585 // Run-time only
586 static void clear_root(int index);
587 static void get_segment_indexes(int index, int& segment_index, int& internal_index);
588 static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
589 #endif // INCLUDE_CDS_JAVA_HEAP
590
591 public:
592 static void finish_materialize_objects() NOT_CDS_JAVA_HEAP_RETURN;
593
594 static void write_heap(ArchiveMappedHeapInfo* mapped_heap_info, ArchiveStreamedHeapInfo* streamed_heap_info) NOT_CDS_JAVA_HEAP_RETURN;
595 static objArrayOop scratch_resolved_references(ConstantPool* src);
596 static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
597 static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
598 static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
599 static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
600 static bool is_heap_region(int idx) {
601 CDS_JAVA_HEAP_ONLY(return (idx == AOTMetaspace::hp);)
602 NOT_CDS_JAVA_HEAP_RETURN_(false);
603 }
604 static void delete_tables_with_raw_oops() NOT_CDS_JAVA_HEAP_RETURN;
605
606 static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
607 static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
608
609 static void init_for_dumping(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
610 static void init_heap_writer() NOT_CDS_JAVA_HEAP_RETURN;
611 static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
612 static void serialize_tables(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
613
614 #ifndef PRODUCT
615 static bool is_a_test_class_in_unnamed_module(Klass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
616 static void initialize_test_class_from_archive(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
617 #endif
618
619 static void initialize_java_lang_invoke(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
620 static void init_classes_for_special_subgraph(Handle loader, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
621
622 static bool is_core_java_lang_invoke_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
623 static bool is_lambda_form_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
624 static bool is_lambda_proxy_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
625 static bool is_string_concat_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
626 static bool is_archivable_hidden_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
627
628 // Used by AOTArtifactFinder
629 static void start_scanning_for_oops();
630 static void end_scanning_for_oops();
631 static void scan_java_class(Klass* k);
632 static void scan_java_mirror(oop orig_mirror);
633 static void copy_and_rescan_aot_inited_mirror(InstanceKlass* ik);
634
635 static void log_heap_roots();
636
637 static intptr_t log_target_location(oop source_oop);
638 static void log_oop_info(outputStream* st, oop source_oop, address archived_object_start, address archived_object_end);
639 static void log_oop_info(outputStream* st, oop source_oop);
640 static void log_oop_details(oop source_oop, address buffered_addr);
641 };
642
643 #endif // SHARE_CDS_HEAPSHARED_HPP