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