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/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 FileMapInfo;
44 class KlassSubGraphInfo;
45 class MetaspaceObjToOopHandleTable;
46 class ResourceBitMap;
47
48 struct ArchivableStaticFieldInfo;
49
50 #define ARCHIVED_BOOT_LAYER_CLASS "jdk/internal/module/ArchivedBootLayer"
51 #define ARCHIVED_BOOT_LAYER_FIELD "archivedBootLayer"
52
53 // A dump time sub-graph info for Klass _k. It includes the entry points
54 // (static fields in _k's mirror) of the archived sub-graphs reachable
55 // from _k's mirror. It also contains a list of Klasses of the objects
56 // within the sub-graphs.
57 class KlassSubGraphInfo: public CHeapObj<mtClass> {
58 private:
59 // The class that contains the static field(s) as the entry point(s)
60 // of archived object sub-graph(s).
61 Klass* _k;
62 // A list of classes need to be loaded and initialized before the archived
63 // object sub-graphs can be accessed at runtime.
64 GrowableArray<Klass*>* _subgraph_object_klasses;
65 // A list of _k's static fields as the entry points of archived sub-graphs.
66 // For each entry field, it is a tuple of field_offset, field_value
67 GrowableArray<int>* _subgraph_entry_fields;
68
69 static bool is_non_early_klass(Klass* k);
70 static void check_allowed_klass(InstanceKlass* ik);
71 public:
72 KlassSubGraphInfo(Klass* k) :
73 _k(k), _subgraph_object_klasses(nullptr),
74 _subgraph_entry_fields(nullptr) {}
75
76 ~KlassSubGraphInfo() {
77 if (_subgraph_object_klasses != nullptr) {
78 delete _subgraph_object_klasses;
79 }
80 if (_subgraph_entry_fields != nullptr) {
81 delete _subgraph_entry_fields;
82 }
83 };
84
85 Klass* klass() { return _k; }
86 GrowableArray<Klass*>* subgraph_object_klasses() {
87 return _subgraph_object_klasses;
88 }
89 GrowableArray<int>* subgraph_entry_fields() {
90 return _subgraph_entry_fields;
91 }
92 void add_subgraph_entry_field(int static_field_offset, oop v);
93 void add_subgraph_object_klass(Klass *orig_k);
94 int num_subgraph_object_klasses() {
95 return _subgraph_object_klasses == nullptr ? 0 :
96 _subgraph_object_klasses->length();
97 }
98 };
99
100 // An archived record of object sub-graphs reachable from static
101 // fields within _k's mirror. The record is reloaded from the archive
102 // at runtime.
103 class ArchivedKlassSubGraphInfoRecord {
104 private:
105 Klass* _k;
106 bool _has_non_early_klasses;
107
108 // contains pairs of field offset and value for each subgraph entry field
109 Array<int>* _entry_field_records;
110
111 // klasses of objects in archived sub-graphs referenced from the entry points
112 // (static fields) in the containing class
113 Array<Klass*>* _subgraph_object_klasses;
114 public:
115 ArchivedKlassSubGraphInfoRecord() :
116 _k(nullptr), _entry_field_records(nullptr), _subgraph_object_klasses(nullptr) {}
117 void init(KlassSubGraphInfo* info);
118 Klass* klass() const { return _k; }
119 Array<int>* entry_field_records() const { return _entry_field_records; }
120 Array<Klass*>* subgraph_object_klasses() const { return _subgraph_object_klasses; }
121 bool has_non_early_klasses() const { return _has_non_early_klasses; }
122 };
123 #endif // INCLUDE_CDS_JAVA_HEAP
124
125 enum class HeapArchiveMode {
126 _uninitialized,
127 _mapping,
128 _streaming
129 };
130
131 class HeapShared: AllStatic {
132 friend class VerifySharedOopClosure;
133
134 public:
135 static void initialize_loading_mode(HeapArchiveMode mode) NOT_CDS_JAVA_HEAP_RETURN;
136 static void initialize_writing_mode() NOT_CDS_JAVA_HEAP_RETURN;
137
138 inline static bool is_loading() NOT_CDS_JAVA_HEAP_RETURN_(false);
139
140 inline static bool is_loading_streaming_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
141 inline static bool is_loading_mapping_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
142
143 inline static bool is_writing() NOT_CDS_JAVA_HEAP_RETURN_(false);
144
145 inline static bool is_writing_streaming_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
146 inline static bool is_writing_mapping_mode() NOT_CDS_JAVA_HEAP_RETURN_(false);
147
148 static bool is_subgraph_root_class(InstanceKlass* ik);
149
150 // Scratch objects for archiving Klass::java_mirror()
151 static oop scratch_java_mirror(BasicType t) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
152 static oop scratch_java_mirror(Klass* k) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
153 static oop scratch_java_mirror(oop java_mirror) NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
154 static bool is_archived_boot_layer_available(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN_(false);
155
156 static bool is_archived_heap_in_use() NOT_CDS_JAVA_HEAP_RETURN_(false);
157 static bool can_use_archived_heap() NOT_CDS_JAVA_HEAP_RETURN_(false);
158 static bool is_too_large_to_archive(size_t size);
159 static bool is_string_too_large_to_archive(oop string);
160 static bool is_too_large_to_archive(oop obj);
161
162 static void initialize_streaming() NOT_CDS_JAVA_HEAP_RETURN;
163 static void enable_gc() NOT_CDS_JAVA_HEAP_RETURN;
164 static void materialize_thread_object() NOT_CDS_JAVA_HEAP_RETURN;
165 static void archive_interned_string(oop string);
166 static void finalize_initialization(FileMapInfo* static_mapinfo) NOT_CDS_JAVA_HEAP_RETURN;
167
168 private:
169 #if INCLUDE_CDS_JAVA_HEAP
170 static HeapArchiveMode _heap_load_mode;
171 static HeapArchiveMode _heap_write_mode;
172
173 // statistics
174 constexpr static int ALLOC_STAT_SLOTS = 16;
175 static size_t _alloc_count[ALLOC_STAT_SLOTS];
176 static size_t _alloc_size[ALLOC_STAT_SLOTS];
177 static size_t _total_obj_count;
178 static size_t _total_obj_size; // in HeapWords
179
180 static void count_allocation(size_t size);
181 static void print_stats();
182 public:
183 static void debug_trace();
184 static unsigned oop_address_hash(oop const& p);
185 static bool oop_handle_equals(const OopHandle& a, const OopHandle& b);
186
187 class CopyKlassSubGraphInfoToArchive;
188
189 class CachedOopInfo {
190 // Used by CDSHeapVerifier.
191 OopHandle _orig_referrer;
192
193 // The location of this object inside {AOTMappedHeapWriter, AOTStreamedHeapWriter}::_buffer
194 size_t _buffer_offset;
195
196 // One or more fields in this object are pointing to non-null oops.
197 bool _has_oop_pointers;
198
199 // One or more fields in this object are pointing to MetaspaceObj
200 bool _has_native_pointers;
201
202 // >= 0 if this oop has been append to the list of roots
203 int _root_index;
204 public:
205 CachedOopInfo(OopHandle orig_referrer, bool has_oop_pointers)
206 : _orig_referrer(orig_referrer),
207 _buffer_offset(0),
208 _has_oop_pointers(has_oop_pointers),
209 _has_native_pointers(false),
210 _root_index(-1) {}
211 oop orig_referrer() const;
212 void set_buffer_offset(size_t offset) { _buffer_offset = offset; }
213 size_t buffer_offset() const { return _buffer_offset; }
214 bool has_oop_pointers() const { return _has_oop_pointers; }
215 bool has_native_pointers() const { return _has_native_pointers; }
216 void set_has_native_pointers() { _has_native_pointers = true; }
217 int root_index() const { return _root_index; }
218 void set_root_index(int i) { _root_index = i; }
219 };
220
221 private:
222 static const int INITIAL_TABLE_SIZE = 15889; // prime number
223 static const int MAX_TABLE_SIZE = 1000000;
224 static bool _use_identity_hash_for_archived_object_cache;
225
226 static unsigned archived_object_cache_hash(OopHandle const& oh);
227
228 typedef ResizeableHashTable<OopHandle, CachedOopInfo,
229 AnyObj::C_HEAP,
230 mtClassShared,
231 HeapShared::archived_object_cache_hash,
232 HeapShared::oop_handle_equals> ArchivedObjectCache;
233 static ArchivedObjectCache* _archived_object_cache;
234
235 class DumpTimeKlassSubGraphInfoTable
236 : public HashTable<Klass*, KlassSubGraphInfo,
237 137, // prime number
238 AnyObj::C_HEAP,
239 mtClassShared,
240 DumpTimeSharedClassTable_hash> {};
241
242 public: // solaris compiler wants this for RunTimeKlassSubGraphInfoTable
243 inline static bool record_equals_compact_hashtable_entry(
244 const ArchivedKlassSubGraphInfoRecord* value, const Klass* key, int len_unused) {
245 return (value->klass() == key);
246 }
247
248 private:
249 typedef OffsetCompactHashtable<
250 const Klass*,
251 const ArchivedKlassSubGraphInfoRecord*,
252 record_equals_compact_hashtable_entry
253 > RunTimeKlassSubGraphInfoTable;
254
255 static DumpTimeKlassSubGraphInfoTable* _dump_time_subgraph_info_table;
256 static RunTimeKlassSubGraphInfoTable _run_time_subgraph_info_table;
257
258 static CachedOopInfo make_cached_oop_info(oop obj, oop referrer);
259 static ArchivedKlassSubGraphInfoRecord* archive_subgraph_info(KlassSubGraphInfo* info);
260 static void archive_object_subgraphs(ArchivableStaticFieldInfo fields[]);
261
262 // Archive object sub-graph starting from the given static field
263 // in Klass k's mirror.
264 static void archive_reachable_objects_from_static_field(
265 InstanceKlass* k, const char* klass_name,
266 int field_offset, const char* field_name);
267
268 static void verify_subgraph_from_static_field(
269 InstanceKlass* k, int field_offset) PRODUCT_RETURN;
270 static void verify_reachable_objects_from(oop obj) PRODUCT_RETURN;
271 static void verify_subgraph_from(oop orig_obj) PRODUCT_RETURN;
272 static void check_special_subgraph_classes();
273
274 static KlassSubGraphInfo* init_subgraph_info(Klass *k);
275 static KlassSubGraphInfo* get_subgraph_info(Klass *k);
276
277 static void init_subgraph_entry_fields(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
278 static void init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[], TRAPS);
279
280 // UseCompressedOops only: Used by decode_from_archive
281 static address _narrow_oop_base;
282 static int _narrow_oop_shift;
283
284 // !UseCompressedOops only: used to relocate pointers to the archived objects
285 static ptrdiff_t _runtime_delta;
286
287 typedef ResizeableHashTable<oop, bool,
288 AnyObj::C_HEAP,
289 mtClassShared,
290 HeapShared::oop_address_hash> SeenObjectsTable;
291
292 static SeenObjectsTable *_seen_objects_table;
293
294 // The "special subgraph" contains all the archived objects that are reachable
295 // from the following roots:
296 // - interned strings
297 // - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
298 // - ConstantPool::resolved_references()
299 // - Universe::<xxx>_exception_instance()
300 static KlassSubGraphInfo* _dump_time_special_subgraph; // for collecting info during dump time
301 static ArchivedKlassSubGraphInfoRecord* _run_time_special_subgraph; // for initializing classes during run time.
302
303 static GrowableArrayCHeap<oop, mtClassShared>* _pending_roots;
304 static OopHandle _scratch_basic_type_mirrors[T_VOID+1];
305 static MetaspaceObjToOopHandleTable* _scratch_objects_table;
306
307 static void init_seen_objects_table() {
308 assert(_seen_objects_table == nullptr, "must be");
309 _seen_objects_table = new (mtClass)SeenObjectsTable(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
310 }
311 static void delete_seen_objects_table() {
312 assert(_seen_objects_table != nullptr, "must be");
313 delete _seen_objects_table;
314 _seen_objects_table = nullptr;
315 }
316
317 // Statistics (for one round of start_recording_subgraph ... done_recording_subgraph)
318 static size_t _num_new_walked_objs;
319 static size_t _num_new_archived_objs;
320 static size_t _num_old_recorded_klasses;
321
322 // Statistics (for all archived subgraphs)
323 static size_t _num_total_subgraph_recordings;
324 static size_t _num_total_walked_objs;
325 static size_t _num_total_archived_objs;
326 static size_t _num_total_recorded_klasses;
327 static size_t _num_total_verifications;
328
329 static void start_recording_subgraph(InstanceKlass *k, const char* klass_name);
330 static void done_recording_subgraph(InstanceKlass *k, const char* klass_name);
331
332 static bool has_been_seen_during_subgraph_recording(oop obj);
333 static void set_has_been_seen_during_subgraph_recording(oop obj);
334 static bool archive_object(oop obj, oop referrer, KlassSubGraphInfo* subgraph_info);
335
336 static void resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]);
337 static void resolve_classes_for_subgraph_of(JavaThread* current, Klass* k);
338 static void clear_archived_roots_of(Klass* k);
339 static const ArchivedKlassSubGraphInfoRecord*
340 resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS);
341 static void resolve_or_init(const char* klass_name, bool do_init, TRAPS);
342 static void resolve_or_init(Klass* k, bool do_init, TRAPS);
343 static void init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record);
344
345 static bool has_been_archived(oop orig_obj);
346 static void prepare_resolved_references();
347 static void archive_subgraphs();
348 static void copy_java_mirror(oop orig_mirror, oop scratch_m);
349
350 // PendingOop and PendingOopStack are used for recursively discovering all cacheable
351 // heap objects. The recursion is done using PendingOopStack so we won't overflow the
352 // C stack with deep reference chains.
353 class PendingOop {
354 oop _obj;
355 oop _referrer;
356 int _level;
357
358 public:
359 PendingOop() : _obj(nullptr), _referrer(nullptr), _level(-1) {}
360 PendingOop(oop obj, oop referrer, int level) : _obj(obj), _referrer(referrer), _level(level) {}
361
362 oop obj() const { return _obj; }
363 oop referrer() const { return _referrer; }
364 int level() const { return _level; }
365 };
366
367 class OopFieldPusher;
368 using PendingOopStack = GrowableArrayCHeap<PendingOop, mtClassShared>;
369
370 static PendingOop _object_being_archived;
371 static bool walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
372 oop orig_obj, oop referrer);
373
374 static void reset_archived_object_states(TRAPS);
375 static void ensure_determinism(TRAPS);
376 public:
377 static void prepare_for_archiving(TRAPS);
378 static void create_archived_object_cache() {
379 _archived_object_cache =
380 new (mtClass)ArchivedObjectCache(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
381 }
382 static void destroy_archived_object_cache() {
383 delete _archived_object_cache;
384 _archived_object_cache = nullptr;
385 }
386 static void make_archived_object_cache_gc_safe();
387 static ArchivedObjectCache* archived_object_cache() {
388 return _archived_object_cache;
389 }
390
391 static CachedOopInfo* get_cached_oop_info(oop orig_obj);
392
393 static int archive_exception_instance(oop exception);
394
395 static bool archive_reachable_objects_from(int level,
396 KlassSubGraphInfo* subgraph_info,
397 oop orig_obj);
398
399 static bool is_interned_string(oop obj);
400 static bool is_dumped_interned_string(oop o);
401
402 // Scratch objects for archiving Klass::java_mirror()
403 static void set_scratch_java_mirror(Klass* k, oop mirror);
404 static void remove_scratch_objects(Klass* k);
405 static bool is_metadata_field(oop src_obj, int offset);
406 template <typename T> static void do_metadata_offsets(oop src_obj, T callback);
407 static void remap_dumped_metadata(oop src_obj, address archived_object);
408 inline static void remap_loaded_metadata(oop obj);
409 inline static oop maybe_remap_referent(bool is_java_lang_ref, size_t field_offset, oop referent);
410 static void get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers);
411 static void set_has_native_pointers(oop src_obj);
412 static uintptr_t archive_location(oop src_obj);
413
414 // We use the HeapShared::roots() array to make sure that objects stored in the
415 // archived heap region are not prematurely collected. These roots include:
416 //
417 // - mirrors of classes that have not yet been loaded.
418 // - ConstantPool::resolved_references() of classes that have not yet been loaded.
419 // - ArchivedKlassSubGraphInfoRecords that have not been initialized
420 // - java.lang.Module objects that have not yet been added to the module graph
421 //
422 // When a mirror M becomes referenced by a newly loaded class K, M will be removed
423 // from HeapShared::roots() via clear_root(), and K will be responsible for
424 // keeping M alive.
425 //
426 // Other types of roots are also cleared similarly when they become referenced.
427
428 // Dump-time only. Returns the index of the root, which can be used at run time to read
429 // the root using get_root(index, ...).
430 static int append_root(oop obj);
431
432 // AOT-compile time only.
433 // Returns -1 if obj is not in the heap root set.
434 static int get_root_index(oop obj) NOT_CDS_JAVA_HEAP_RETURN_(-1);
435
436 static GrowableArrayCHeap<oop, mtClassShared>* pending_roots() { return _pending_roots; }
437
438 // Dump-time and runtime
439 static objArrayOop root_segment(int segment_idx);
440 static oop get_root(int index, bool clear=false);
441
442 // Run-time only
443 static void clear_root(int index);
444 static void get_segment_indexes(int index, int& segment_index, int& internal_index);
445 static void setup_test_class(const char* test_class_name) PRODUCT_RETURN;
446 #endif // INCLUDE_CDS_JAVA_HEAP
447
448 public:
449 static void finish_materialize_objects() NOT_CDS_JAVA_HEAP_RETURN;
450
451 static void write_heap(AOTMappedHeapInfo* mapped_heap_info, AOTStreamedHeapInfo* streamed_heap_info) NOT_CDS_JAVA_HEAP_RETURN;
452 static refArrayOop scratch_resolved_references(ConstantPool* src);
453 static void add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) NOT_CDS_JAVA_HEAP_RETURN;
454 static void remove_scratch_resolved_references(ConstantPool* src) NOT_CDS_JAVA_HEAP_RETURN;
455 static void init_dumping() NOT_CDS_JAVA_HEAP_RETURN;
456 static void init_scratch_objects_for_basic_type_mirrors(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
457 static void init_box_classes(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
458 static bool is_heap_region(int idx) {
459 CDS_JAVA_HEAP_ONLY(return (idx == AOTMetaspace::hp);)
460 NOT_CDS_JAVA_HEAP_RETURN_(false);
461 }
462 static void delete_tables_with_raw_oops() NOT_CDS_JAVA_HEAP_RETURN;
463
464 static void resolve_classes(JavaThread* current) NOT_CDS_JAVA_HEAP_RETURN;
465 static void initialize_from_archived_subgraph(JavaThread* current, Klass* k) NOT_CDS_JAVA_HEAP_RETURN;
466
467 static void init_for_dumping(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
468 static void init_heap_writer() NOT_CDS_JAVA_HEAP_RETURN;
469 static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
470 static void serialize_tables(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
471
472 #ifndef PRODUCT
473 static bool is_a_test_class_in_unnamed_module(Klass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
474 static void initialize_test_class_from_archive(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
475 #endif
476
477 static void initialize_java_lang_invoke(TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
478 static void init_classes_for_special_subgraph(Handle loader, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
479
480 static bool is_lambda_form_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
481 static bool is_lambda_proxy_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
482 static bool is_string_concat_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
483 static bool is_archivable_hidden_klass(InstanceKlass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
484
485 // Used by AOTArtifactFinder
486 static void start_scanning_for_oops();
487 static void end_scanning_for_oops();
488 static void scan_java_class(Klass* k);
489 static void scan_java_mirror(oop orig_mirror);
490 static void copy_and_rescan_aot_inited_mirror(InstanceKlass* ik);
491
492 static void log_heap_roots();
493
494 static intptr_t log_target_location(oop source_oop);
495 static void log_oop_info(outputStream* st, oop source_oop, address archived_object_start, address archived_object_end);
496 static void log_oop_info(outputStream* st, oop source_oop);
497 static void log_oop_details(oop source_oop, address buffered_addr);
498 };
499
500 #endif // SHARE_CDS_HEAPSHARED_HPP