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 #include "cds/aotArtifactFinder.hpp"
26 #include "cds/aotClassInitializer.hpp"
27 #include "cds/aotClassLocation.hpp"
28 #include "cds/aotLogging.hpp"
29 #include "cds/aotMetaspace.hpp"
30 #include "cds/aotOopChecker.hpp"
31 #include "cds/aotReferenceObjSupport.hpp"
32 #include "cds/archiveBuilder.hpp"
33 #include "cds/archiveHeapLoader.hpp"
34 #include "cds/archiveHeapWriter.hpp"
35 #include "cds/archiveUtils.hpp"
36 #include "cds/cdsConfig.hpp"
37 #include "cds/cdsEnumKlass.hpp"
38 #include "cds/cdsHeapVerifier.hpp"
39 #include "cds/heapShared.hpp"
40 #include "cds/regeneratedClasses.hpp"
41 #include "classfile/classLoaderData.hpp"
42 #include "classfile/javaClasses.inline.hpp"
43 #include "classfile/modules.hpp"
44 #include "classfile/stringTable.hpp"
45 #include "classfile/symbolTable.hpp"
46 #include "classfile/systemDictionary.hpp"
47 #include "classfile/systemDictionaryShared.hpp"
48 #include "classfile/vmClasses.hpp"
49 #include "classfile/vmSymbols.hpp"
50 #include "gc/shared/collectedHeap.hpp"
51 #include "gc/shared/gcLocker.hpp"
52 #include "gc/shared/gcVMOperations.hpp"
53 #include "logging/log.hpp"
54 #include "logging/logStream.hpp"
55 #include "memory/iterator.inline.hpp"
56 #include "memory/resourceArea.hpp"
57 #include "memory/universe.hpp"
58 #include "oops/compressedOops.inline.hpp"
59 #include "oops/fieldStreams.inline.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/oopHandle.inline.hpp"
63 #include "oops/typeArrayOop.inline.hpp"
64 #include "prims/jvmtiExport.hpp"
65 #include "runtime/arguments.hpp"
66 #include "runtime/fieldDescriptor.inline.hpp"
67 #include "runtime/init.hpp"
68 #include "runtime/javaCalls.hpp"
69 #include "runtime/mutexLocker.hpp"
70 #include "runtime/safepointVerifiers.hpp"
71 #include "utilities/bitMap.inline.hpp"
72 #include "utilities/copy.hpp"
73 #if INCLUDE_G1GC
74 #include "gc/g1/g1CollectedHeap.hpp"
75 #endif
76
77 #if INCLUDE_CDS_JAVA_HEAP
78
79 struct ArchivableStaticFieldInfo {
80 const char* klass_name;
81 const char* field_name;
82 InstanceKlass* klass;
83 int offset;
84 BasicType type;
85
86 ArchivableStaticFieldInfo(const char* k, const char* f)
87 : klass_name(k), field_name(f), klass(nullptr), offset(0), type(T_ILLEGAL) {}
88
89 bool valid() {
90 return klass_name != nullptr;
91 }
92 };
93
94 DumpedInternedStrings *HeapShared::_dumped_interned_strings = nullptr;
95
96 size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS];
97 size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS];
98 size_t HeapShared::_total_obj_count;
99 size_t HeapShared::_total_obj_size;
100
101 #ifndef PRODUCT
102 #define ARCHIVE_TEST_FIELD_NAME "archivedObjects"
103 static Array<char>* _archived_ArchiveHeapTestClass = nullptr;
104 static const char* _test_class_name = nullptr;
105 static Klass* _test_class = nullptr;
106 static const ArchivedKlassSubGraphInfoRecord* _test_class_record = nullptr;
107 #endif
108
109
110 //
111 // If you add new entries to the following tables, you should know what you're doing!
112 //
113
114 static ArchivableStaticFieldInfo archive_subgraph_entry_fields[] = {
115 {"java/lang/Integer$IntegerCache", "archivedCache"},
116 {"java/lang/Long$LongCache", "archivedCache"},
117 {"java/lang/Byte$ByteCache", "archivedCache"},
118 {"java/lang/Short$ShortCache", "archivedCache"},
119 {"java/lang/Character$CharacterCache", "archivedCache"},
120 {"java/util/jar/Attributes$Name", "KNOWN_NAMES"},
121 {"sun/util/locale/BaseLocale", "constantBaseLocales"},
122 {"jdk/internal/module/ArchivedModuleGraph", "archivedModuleGraph"},
123 {"java/util/ImmutableCollections", "archivedObjects"},
124 {"java/lang/ModuleLayer", "EMPTY_LAYER"},
125 {"java/lang/module/Configuration", "EMPTY_CONFIGURATION"},
126 {"jdk/internal/math/FDBigInteger", "archivedCaches"},
127
128 #ifndef PRODUCT
129 {nullptr, nullptr}, // Extra slot for -XX:ArchiveHeapTestClass
130 #endif
131 {nullptr, nullptr},
132 };
133
134 // full module graph
135 static ArchivableStaticFieldInfo fmg_archive_subgraph_entry_fields[] = {
136 {"jdk/internal/loader/ArchivedClassLoaders", "archivedClassLoaders"},
137 {ARCHIVED_BOOT_LAYER_CLASS, ARCHIVED_BOOT_LAYER_FIELD},
138 {"java/lang/Module$ArchivedData", "archivedData"},
139 {nullptr, nullptr},
140 };
141
142 KlassSubGraphInfo* HeapShared::_dump_time_special_subgraph;
143 ArchivedKlassSubGraphInfoRecord* HeapShared::_run_time_special_subgraph;
144 GrowableArrayCHeap<oop, mtClassShared>* HeapShared::_pending_roots = nullptr;
145 GrowableArrayCHeap<OopHandle, mtClassShared>* HeapShared::_root_segments = nullptr;
146 int HeapShared::_root_segment_max_size_elems;
147 OopHandle HeapShared::_scratch_basic_type_mirrors[T_VOID+1];
148 MetaspaceObjToOopHandleTable* HeapShared::_scratch_objects_table = nullptr;
149
150 static bool is_subgraph_root_class_of(ArchivableStaticFieldInfo fields[], InstanceKlass* ik) {
151 for (int i = 0; fields[i].valid(); i++) {
152 if (fields[i].klass == ik) {
153 return true;
154 }
155 }
156 return false;
157 }
158
159 bool HeapShared::is_subgraph_root_class(InstanceKlass* ik) {
160 return is_subgraph_root_class_of(archive_subgraph_entry_fields, ik) ||
161 is_subgraph_root_class_of(fmg_archive_subgraph_entry_fields, ik);
162 }
163
164 oop HeapShared::CachedOopInfo::orig_referrer() const {
165 return _orig_referrer.resolve();
166 }
167
168 unsigned HeapShared::oop_hash(oop const& p) {
169 assert(SafepointSynchronize::is_at_safepoint() ||
170 JavaThread::current()->is_in_no_safepoint_scope(), "sanity");
171 // Do not call p->identity_hash() as that will update the
172 // object header.
173 return primitive_hash(cast_from_oop<intptr_t>(p));
174 }
175
176 unsigned int HeapShared::oop_handle_hash_raw(const OopHandle& oh) {
177 return oop_hash(oh.resolve());
178 }
179
180 unsigned int HeapShared::oop_handle_hash(const OopHandle& oh) {
181 oop o = oh.resolve();
182 if (o == nullptr) {
183 return 0;
184 } else {
185 return o->identity_hash();
186 }
187 }
188
189 bool HeapShared::oop_handle_equals(const OopHandle& a, const OopHandle& b) {
190 return a.resolve() == b.resolve();
191 }
192
193 static void reset_states(oop obj, TRAPS) {
194 Handle h_obj(THREAD, obj);
195 InstanceKlass* klass = InstanceKlass::cast(obj->klass());
196 TempNewSymbol method_name = SymbolTable::new_symbol("resetArchivedStates");
197 Symbol* method_sig = vmSymbols::void_method_signature();
198
199 while (klass != nullptr) {
200 Method* method = klass->find_method(method_name, method_sig);
201 if (method != nullptr) {
202 assert(method->is_private(), "must be");
203 if (log_is_enabled(Debug, aot)) {
204 ResourceMark rm(THREAD);
205 log_debug(aot)(" calling %s", method->name_and_sig_as_C_string());
206 }
207 JavaValue result(T_VOID);
208 JavaCalls::call_special(&result, h_obj, klass,
209 method_name, method_sig, CHECK);
210 }
211 klass = klass->super();
212 }
213 }
214
215 void HeapShared::reset_archived_object_states(TRAPS) {
216 assert(CDSConfig::is_dumping_heap(), "dump-time only");
217 log_debug(aot)("Resetting platform loader");
218 reset_states(SystemDictionary::java_platform_loader(), CHECK);
219 log_debug(aot)("Resetting system loader");
220 reset_states(SystemDictionary::java_system_loader(), CHECK);
221
222 // Clean up jdk.internal.loader.ClassLoaders::bootLoader(), which is not
223 // directly used for class loading, but rather is used by the core library
224 // to keep track of resources, etc, loaded by the null class loader.
225 //
226 // Note, this object is non-null, and is not the same as
227 // ClassLoaderData::the_null_class_loader_data()->class_loader(),
228 // which is null.
229 log_debug(aot)("Resetting boot loader");
230 JavaValue result(T_OBJECT);
231 JavaCalls::call_static(&result,
232 vmClasses::jdk_internal_loader_ClassLoaders_klass(),
233 vmSymbols::bootLoader_name(),
234 vmSymbols::void_BuiltinClassLoader_signature(),
235 CHECK);
236 Handle boot_loader(THREAD, result.get_oop());
237 reset_states(boot_loader(), CHECK);
238 }
239
240 HeapShared::ArchivedObjectCache* HeapShared::_archived_object_cache = nullptr;
241
242 bool HeapShared::has_been_archived(oop obj) {
243 assert(CDSConfig::is_dumping_heap(), "dump-time only");
244 OopHandle oh(&obj);
245 return archived_object_cache()->get(oh) != nullptr;
246 }
247
248 int HeapShared::append_root(oop obj) {
249 assert(CDSConfig::is_dumping_heap(), "dump-time only");
250 if (obj != nullptr) {
251 assert(has_been_archived(obj), "must be");
252 }
253 // No GC should happen since we aren't scanning _pending_roots.
254 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
255
256 return _pending_roots->append(obj);
257 }
258
259 objArrayOop HeapShared::root_segment(int segment_idx) {
260 if (CDSConfig::is_dumping_heap()) {
261 assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
262 } else {
263 assert(CDSConfig::is_using_archive(), "must be");
264 }
265
266 objArrayOop segment = (objArrayOop)_root_segments->at(segment_idx).resolve();
267 assert(segment != nullptr, "should have been initialized");
268 return segment;
269 }
270
271 void HeapShared::get_segment_indexes(int idx, int& seg_idx, int& int_idx) {
272 assert(_root_segment_max_size_elems > 0, "sanity");
273
274 // Try to avoid divisions for the common case.
275 if (idx < _root_segment_max_size_elems) {
276 seg_idx = 0;
277 int_idx = idx;
278 } else {
279 seg_idx = idx / _root_segment_max_size_elems;
280 int_idx = idx % _root_segment_max_size_elems;
281 }
282
283 assert(idx == seg_idx * _root_segment_max_size_elems + int_idx,
284 "sanity: %d index maps to %d segment and %d internal", idx, seg_idx, int_idx);
285 }
286
287 // Returns an objArray that contains all the roots of the archived objects
288 oop HeapShared::get_root(int index, bool clear) {
289 assert(index >= 0, "sanity");
290 assert(!CDSConfig::is_dumping_heap() && CDSConfig::is_using_archive(), "runtime only");
291 assert(!_root_segments->is_empty(), "must have loaded shared heap");
292 int seg_idx, int_idx;
293 get_segment_indexes(index, seg_idx, int_idx);
294 oop result = root_segment(seg_idx)->obj_at(int_idx);
295 if (clear) {
296 clear_root(index);
297 }
298 return result;
299 }
300
301 void HeapShared::clear_root(int index) {
302 assert(index >= 0, "sanity");
303 assert(CDSConfig::is_using_archive(), "must be");
304 if (ArchiveHeapLoader::is_in_use()) {
305 int seg_idx, int_idx;
306 get_segment_indexes(index, seg_idx, int_idx);
307 if (log_is_enabled(Debug, aot, heap)) {
308 oop old = root_segment(seg_idx)->obj_at(int_idx);
309 log_debug(aot, heap)("Clearing root %d: was " PTR_FORMAT, index, p2i(old));
310 }
311 root_segment(seg_idx)->obj_at_put(int_idx, nullptr);
312 }
313 }
314
315 bool HeapShared::archive_object(oop obj, oop referrer, KlassSubGraphInfo* subgraph_info) {
316 assert(CDSConfig::is_dumping_heap(), "dump-time only");
317
318 assert(!obj->is_stackChunk(), "do not archive stack chunks");
319 if (has_been_archived(obj)) {
320 return true;
321 }
322
323 if (ArchiveHeapWriter::is_too_large_to_archive(obj->size())) {
324 log_debug(aot, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: %zu",
325 p2i(obj), obj->size());
326 debug_trace();
327 return false;
328 } else {
329 AOTOopChecker::check(obj); // Make sure contents of this oop are safe.
330
331 count_allocation(obj->size());
332 ArchiveHeapWriter::add_source_obj(obj);
333 CachedOopInfo info = make_cached_oop_info(obj, referrer);
334
335 OopHandle oh(Universe::vm_global(), obj);
336 archived_object_cache()->put_when_absent(oh, info);
337 archived_object_cache()->maybe_grow();
338 mark_native_pointers(obj);
339
340 Klass* k = obj->klass();
341 if (k->is_instance_klass()) {
342 // Whenever we see a non-array Java object of type X, we mark X to be aot-initialized.
343 // This ensures that during the production run, whenever Java code sees a cached object
344 // of type X, we know that X is already initialized. (see TODO comment below ...)
345
346 if (InstanceKlass::cast(k)->is_enum_subclass()
347 // We can't rerun <clinit> of enum classes (see cdsEnumKlass.cpp) so
348 // we must store them as AOT-initialized.
349 || (subgraph_info == _dump_time_special_subgraph))
350 // TODO: we do this only for the special subgraph for now. Extending this to
351 // other subgraphs would require more refactoring of the core library (such as
352 // move some initialization logic into runtimeSetup()).
353 //
354 // For the other subgraphs, we have a weaker mechanism to ensure that
355 // all classes in a subgraph are initialized before the subgraph is programmatically
356 // returned from jdk.internal.misc.CDS::initializeFromArchive().
357 // See HeapShared::initialize_from_archived_subgraph().
358 {
359 AOTArtifactFinder::add_aot_inited_class(InstanceKlass::cast(k));
360 }
361
362 if (java_lang_Class::is_instance(obj)) {
363 Klass* mirror_k = java_lang_Class::as_Klass(obj);
364 if (mirror_k != nullptr) {
365 AOTArtifactFinder::add_cached_class(mirror_k);
366 }
367 } else if (java_lang_invoke_ResolvedMethodName::is_instance(obj)) {
368 Method* m = java_lang_invoke_ResolvedMethodName::vmtarget(obj);
369 if (m != nullptr) {
370 if (RegeneratedClasses::has_been_regenerated(m)) {
371 m = RegeneratedClasses::get_regenerated_object(m);
372 }
373 InstanceKlass* method_holder = m->method_holder();
374 AOTArtifactFinder::add_cached_class(method_holder);
375 }
376 }
377 }
378
379 if (log_is_enabled(Debug, aot, heap)) {
380 ResourceMark rm;
381 LogTarget(Debug, aot, heap) log;
382 LogStream out(log);
383 out.print("Archived heap object " PTR_FORMAT " : %s ",
384 p2i(obj), obj->klass()->external_name());
385 if (java_lang_Class::is_instance(obj)) {
386 Klass* k = java_lang_Class::as_Klass(obj);
387 if (k != nullptr) {
388 out.print("%s", k->external_name());
389 } else {
390 out.print("primitive");
391 }
392 }
393 out.cr();
394 }
395
396 return true;
397 }
398 }
399
400 class MetaspaceObjToOopHandleTable: public HashTable<MetaspaceObj*, OopHandle,
401 36137, // prime number
402 AnyObj::C_HEAP,
403 mtClassShared> {
404 public:
405 oop get_oop(MetaspaceObj* ptr) {
406 MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
407 OopHandle* handle = get(ptr);
408 if (handle != nullptr) {
409 return handle->resolve();
410 } else {
411 return nullptr;
412 }
413 }
414 void set_oop(MetaspaceObj* ptr, oop o) {
415 MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
416 OopHandle handle(Universe::vm_global(), o);
417 bool is_new = put(ptr, handle);
418 assert(is_new, "cannot set twice");
419 }
420 void remove_oop(MetaspaceObj* ptr) {
421 MutexLocker ml(ScratchObjects_lock, Mutex::_no_safepoint_check_flag);
422 OopHandle* handle = get(ptr);
423 if (handle != nullptr) {
424 handle->release(Universe::vm_global());
425 remove(ptr);
426 }
427 }
428 };
429
430 void HeapShared::add_scratch_resolved_references(ConstantPool* src, objArrayOop dest) {
431 if (SystemDictionaryShared::is_builtin_loader(src->pool_holder()->class_loader_data())) {
432 _scratch_objects_table->set_oop(src, dest);
433 }
434 }
435
436 objArrayOop HeapShared::scratch_resolved_references(ConstantPool* src) {
437 return (objArrayOop)_scratch_objects_table->get_oop(src);
438 }
439
440 void HeapShared::init_dumping() {
441 _scratch_objects_table = new (mtClass)MetaspaceObjToOopHandleTable();
442 _pending_roots = new GrowableArrayCHeap<oop, mtClassShared>(500);
443 }
444
445 void HeapShared::init_scratch_objects_for_basic_type_mirrors(TRAPS) {
446 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
447 BasicType bt = (BasicType)i;
448 if (!is_reference_type(bt)) {
449 oop m = java_lang_Class::create_basic_type_mirror(type2name(bt), bt, CHECK);
450 _scratch_basic_type_mirrors[i] = OopHandle(Universe::vm_global(), m);
451 }
452 }
453 }
454
455 // Given java_mirror that represents a (primitive or reference) type T,
456 // return the "scratch" version that represents the same type T.
457 // Note that if java_mirror will be returned if it's already a
458 // scratch mirror.
459 //
460 // See java_lang_Class::create_scratch_mirror() for more info.
461 oop HeapShared::scratch_java_mirror(oop java_mirror) {
462 assert(java_lang_Class::is_instance(java_mirror), "must be");
463
464 for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
465 BasicType bt = (BasicType)i;
466 if (!is_reference_type(bt)) {
467 if (_scratch_basic_type_mirrors[i].resolve() == java_mirror) {
468 return java_mirror;
469 }
470 }
471 }
472
473 if (java_lang_Class::is_primitive(java_mirror)) {
474 return scratch_java_mirror(java_lang_Class::as_BasicType(java_mirror));
475 } else {
476 return scratch_java_mirror(java_lang_Class::as_Klass(java_mirror));
477 }
478 }
479
480 oop HeapShared::scratch_java_mirror(BasicType t) {
481 assert((uint)t < T_VOID+1, "range check");
482 assert(!is_reference_type(t), "sanity");
483 return _scratch_basic_type_mirrors[t].resolve();
484 }
485
486 oop HeapShared::scratch_java_mirror(Klass* k) {
487 return _scratch_objects_table->get_oop(k);
488 }
489
490 void HeapShared::set_scratch_java_mirror(Klass* k, oop mirror) {
491 _scratch_objects_table->set_oop(k, mirror);
492 }
493
494 void HeapShared::remove_scratch_objects(Klass* k) {
495 // Klass is being deallocated. Java mirror can still be alive, and it should not
496 // point to dead klass. We need to break the link from mirror to the Klass.
497 // See how InstanceKlass::deallocate_contents does it for normal mirrors.
498 oop mirror = _scratch_objects_table->get_oop(k);
499 if (mirror != nullptr) {
500 java_lang_Class::set_klass(mirror, nullptr);
501 }
502 _scratch_objects_table->remove_oop(k);
503 if (k->is_instance_klass()) {
504 _scratch_objects_table->remove(InstanceKlass::cast(k)->constants());
505 }
506 }
507
508 //TODO: we eventually want a more direct test for these kinds of things.
509 //For example the JVM could record some bit of context from the creation
510 //of the klass, such as who called the hidden class factory. Using
511 //string compares on names is fragile and will break as soon as somebody
512 //changes the names in the JDK code. See discussion in JDK-8342481 for
513 //related ideas about marking AOT-related classes.
514 bool HeapShared::is_lambda_form_klass(InstanceKlass* ik) {
515 return ik->is_hidden() &&
516 (ik->name()->starts_with("java/lang/invoke/LambdaForm$MH+") ||
517 ik->name()->starts_with("java/lang/invoke/LambdaForm$DMH+") ||
518 ik->name()->starts_with("java/lang/invoke/LambdaForm$BMH+") ||
519 ik->name()->starts_with("java/lang/invoke/LambdaForm$VH+"));
520 }
521
522 bool HeapShared::is_lambda_proxy_klass(InstanceKlass* ik) {
523 return ik->is_hidden() && (ik->name()->index_of_at(0, "$$Lambda+", 9) > 0);
524 }
525
526 bool HeapShared::is_string_concat_klass(InstanceKlass* ik) {
527 return ik->is_hidden() && ik->name()->starts_with("java/lang/String$$StringConcat");
528 }
529
530 bool HeapShared::is_archivable_hidden_klass(InstanceKlass* ik) {
531 return CDSConfig::is_dumping_method_handles() &&
532 (is_lambda_form_klass(ik) || is_lambda_proxy_klass(ik) || is_string_concat_klass(ik));
533 }
534
535
536 void HeapShared::copy_and_rescan_aot_inited_mirror(InstanceKlass* ik) {
537 ik->set_has_aot_initialized_mirror();
538
539 oop orig_mirror;
540 if (RegeneratedClasses::is_regenerated_object(ik)) {
541 InstanceKlass* orig_ik = RegeneratedClasses::get_original_object(ik);
542 precond(orig_ik->is_initialized());
543 orig_mirror = orig_ik->java_mirror();
544 } else {
545 precond(ik->is_initialized());
546 orig_mirror = ik->java_mirror();
547 }
548
549 oop m = scratch_java_mirror(ik);
550 int nfields = 0;
551 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
552 if (fs.access_flags().is_static()) {
553 fieldDescriptor& fd = fs.field_descriptor();
554 int offset = fd.offset();
555 switch (fd.field_type()) {
556 case T_OBJECT:
557 case T_ARRAY:
558 {
559 oop field_obj = orig_mirror->obj_field(offset);
560 if (offset == java_lang_Class::reflection_data_offset()) {
561 // Class::reflectData use SoftReference, which cannot be archived. Set it
562 // to null and it will be recreated at runtime.
563 field_obj = nullptr;
564 }
565 m->obj_field_put(offset, field_obj);
566 if (field_obj != nullptr) {
567 bool success = archive_reachable_objects_from(1, _dump_time_special_subgraph, field_obj);
568 assert(success, "sanity");
569 }
570 }
571 break;
572 case T_BOOLEAN:
573 m->bool_field_put(offset, orig_mirror->bool_field(offset));
574 break;
575 case T_BYTE:
576 m->byte_field_put(offset, orig_mirror->byte_field(offset));
577 break;
578 case T_SHORT:
579 m->short_field_put(offset, orig_mirror->short_field(offset));
580 break;
581 case T_CHAR:
582 m->char_field_put(offset, orig_mirror->char_field(offset));
583 break;
584 case T_INT:
585 m->int_field_put(offset, orig_mirror->int_field(offset));
586 break;
587 case T_LONG:
588 m->long_field_put(offset, orig_mirror->long_field(offset));
589 break;
590 case T_FLOAT:
591 m->float_field_put(offset, orig_mirror->float_field(offset));
592 break;
593 case T_DOUBLE:
594 m->double_field_put(offset, orig_mirror->double_field(offset));
595 break;
596 default:
597 ShouldNotReachHere();
598 }
599 nfields ++;
600 }
601 }
602
603 oop class_data = java_lang_Class::class_data(orig_mirror);
604 java_lang_Class::set_class_data(m, class_data);
605 if (class_data != nullptr) {
606 bool success = archive_reachable_objects_from(1, _dump_time_special_subgraph, class_data);
607 assert(success, "sanity");
608 }
609
610 if (log_is_enabled(Debug, aot, init)) {
611 ResourceMark rm;
612 log_debug(aot, init)("copied %3d field(s) in aot-initialized mirror %s%s%s", nfields, ik->external_name(),
613 ik->is_hidden() ? " (hidden)" : "",
614 ik->is_enum_subclass() ? " (enum)" : "");
615 }
616 }
617
618 void HeapShared::copy_java_mirror(oop orig_mirror, oop scratch_m) {
619 // We need to retain the identity_hash, because it may have been used by some hashtables
620 // in the shared heap.
621 if (!orig_mirror->fast_no_hash_check()) {
622 intptr_t src_hash = orig_mirror->identity_hash();
623 if (UseCompactObjectHeaders) {
624 narrowKlass nk = CompressedKlassPointers::encode(orig_mirror->klass());
625 scratch_m->set_mark(markWord::prototype().set_narrow_klass(nk).copy_set_hash(src_hash));
626 } else {
627 // For valhalla, the prototype header is the same as markWord::prototype();
628 scratch_m->set_mark(markWord::prototype().copy_set_hash(src_hash));
629 }
630 assert(scratch_m->mark().is_unlocked(), "sanity");
631
632 DEBUG_ONLY(intptr_t archived_hash = scratch_m->identity_hash());
633 assert(src_hash == archived_hash, "Different hash codes: original " INTPTR_FORMAT ", archived " INTPTR_FORMAT, src_hash, archived_hash);
634 }
635
636 if (CDSConfig::is_dumping_aot_linked_classes()) {
637 java_lang_Class::set_module(scratch_m, java_lang_Class::module(orig_mirror));
638 java_lang_Class::set_protection_domain(scratch_m, java_lang_Class::protection_domain(orig_mirror));
639 }
640 }
641
642 static objArrayOop get_archived_resolved_references(InstanceKlass* src_ik) {
643 if (SystemDictionaryShared::is_builtin_loader(src_ik->class_loader_data())) {
644 objArrayOop rr = src_ik->constants()->resolved_references_or_null();
645 if (rr != nullptr && !ArchiveHeapWriter::is_too_large_to_archive(rr)) {
646 return HeapShared::scratch_resolved_references(src_ik->constants());
647 }
648 }
649 return nullptr;
650 }
651
652 void HeapShared::archive_strings() {
653 oop shared_strings_array = StringTable::init_shared_strings_array();
654 bool success = archive_reachable_objects_from(1, _dump_time_special_subgraph, shared_strings_array);
655 assert(success, "shared strings array must not point to arrays or strings that are too large to archive");
656 StringTable::set_shared_strings_array_index(append_root(shared_strings_array));
657 }
658
659 int HeapShared::archive_exception_instance(oop exception) {
660 bool success = archive_reachable_objects_from(1, _dump_time_special_subgraph, exception);
661 assert(success, "sanity");
662 return append_root(exception);
663 }
664
665 void HeapShared::mark_native_pointers(oop orig_obj) {
666 if (java_lang_Class::is_instance(orig_obj)) {
667 ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::klass_offset());
668 ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_Class::array_klass_offset());
669 } else if (java_lang_invoke_ResolvedMethodName::is_instance(orig_obj)) {
670 ArchiveHeapWriter::mark_native_pointer(orig_obj, java_lang_invoke_ResolvedMethodName::vmtarget_offset());
671 }
672 }
673
674 void HeapShared::get_pointer_info(oop src_obj, bool& has_oop_pointers, bool& has_native_pointers) {
675 OopHandle oh(&src_obj);
676 CachedOopInfo* info = archived_object_cache()->get(oh);
677 assert(info != nullptr, "must be");
678 has_oop_pointers = info->has_oop_pointers();
679 has_native_pointers = info->has_native_pointers();
680 }
681
682 void HeapShared::set_has_native_pointers(oop src_obj) {
683 OopHandle oh(&src_obj);
684 CachedOopInfo* info = archived_object_cache()->get(oh);
685 assert(info != nullptr, "must be");
686 info->set_has_native_pointers();
687 }
688
689 // Between start_scanning_for_oops() and end_scanning_for_oops(), we discover all Java heap objects that
690 // should be stored in the AOT cache. The scanning is coordinated by AOTArtifactFinder.
691 void HeapShared::start_scanning_for_oops() {
692 {
693 NoSafepointVerifier nsv;
694
695 // The special subgraph doesn't belong to any class. We use Object_klass() here just
696 // for convenience.
697 _dump_time_special_subgraph = init_subgraph_info(vmClasses::Object_klass(), false);
698
699 // Cache for recording where the archived objects are copied to
700 create_archived_object_cache();
701
702 if (UseCompressedOops || UseG1GC) {
703 aot_log_info(aot)("Heap range = [" PTR_FORMAT " - " PTR_FORMAT "]",
704 UseCompressedOops ? p2i(CompressedOops::begin()) :
705 p2i((address)G1CollectedHeap::heap()->reserved().start()),
706 UseCompressedOops ? p2i(CompressedOops::end()) :
707 p2i((address)G1CollectedHeap::heap()->reserved().end()));
708 }
709
710 archive_subgraphs();
711 }
712
713 init_seen_objects_table();
714 Universe::archive_exception_instances();
715 }
716
717 void HeapShared::end_scanning_for_oops() {
718 archive_strings();
719 delete_seen_objects_table();
720 }
721
722 void HeapShared::write_heap(ArchiveHeapInfo *heap_info) {
723 {
724 NoSafepointVerifier nsv;
725 CDSHeapVerifier::verify();
726 check_special_subgraph_classes();
727 }
728
729 StringTable::write_shared_table();
730 ArchiveHeapWriter::write(_pending_roots, heap_info);
731
732 ArchiveBuilder::OtherROAllocMark mark;
733 write_subgraph_info_table();
734 }
735
736 void HeapShared::scan_java_mirror(oop orig_mirror) {
737 oop m = scratch_java_mirror(orig_mirror);
738 if (m != nullptr) { // nullptr if for custom class loader
739 copy_java_mirror(orig_mirror, m);
740 bool success = archive_reachable_objects_from(1, _dump_time_special_subgraph, m);
741 assert(success, "sanity");
742 }
743 }
744
745 void HeapShared::scan_java_class(Klass* orig_k) {
746 scan_java_mirror(orig_k->java_mirror());
747
748 if (orig_k->is_instance_klass()) {
749 InstanceKlass* orig_ik = InstanceKlass::cast(orig_k);
750 orig_ik->constants()->prepare_resolved_references_for_archiving();
751 objArrayOop rr = get_archived_resolved_references(orig_ik);
752 if (rr != nullptr) {
753 bool success = HeapShared::archive_reachable_objects_from(1, _dump_time_special_subgraph, rr);
754 assert(success, "must be");
755 }
756 }
757 }
758
759 void HeapShared::archive_subgraphs() {
760 assert(CDSConfig::is_dumping_heap(), "must be");
761
762 archive_object_subgraphs(archive_subgraph_entry_fields,
763 false /* is_full_module_graph */);
764
765 if (CDSConfig::is_dumping_full_module_graph()) {
766 archive_object_subgraphs(fmg_archive_subgraph_entry_fields,
767 true /* is_full_module_graph */);
768 Modules::verify_archived_modules();
769 }
770 }
771
772 //
773 // Subgraph archiving support
774 //
775 HeapShared::DumpTimeKlassSubGraphInfoTable* HeapShared::_dump_time_subgraph_info_table = nullptr;
776 HeapShared::RunTimeKlassSubGraphInfoTable HeapShared::_run_time_subgraph_info_table;
777
778 // Get the subgraph_info for Klass k. A new subgraph_info is created if
779 // there is no existing one for k. The subgraph_info records the "buffered"
780 // address of the class.
781 KlassSubGraphInfo* HeapShared::init_subgraph_info(Klass* k, bool is_full_module_graph) {
782 assert(CDSConfig::is_dumping_heap(), "dump time only");
783 bool created;
784 KlassSubGraphInfo* info =
785 _dump_time_subgraph_info_table->put_if_absent(k, KlassSubGraphInfo(k, is_full_module_graph),
786 &created);
787 assert(created, "must not initialize twice");
788 return info;
789 }
790
791 KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
792 assert(CDSConfig::is_dumping_heap(), "dump time only");
793 KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(k);
794 assert(info != nullptr, "must have been initialized");
795 return info;
796 }
797
798 // Add an entry field to the current KlassSubGraphInfo.
799 void KlassSubGraphInfo::add_subgraph_entry_field(int static_field_offset, oop v) {
800 assert(CDSConfig::is_dumping_heap(), "dump time only");
801 if (_subgraph_entry_fields == nullptr) {
802 _subgraph_entry_fields =
803 new (mtClass) GrowableArray<int>(10, mtClass);
804 }
805 _subgraph_entry_fields->append(static_field_offset);
806 _subgraph_entry_fields->append(HeapShared::append_root(v));
807 }
808
809 // Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs.
810 // Only objects of boot classes can be included in sub-graph.
811 void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) {
812 assert(CDSConfig::is_dumping_heap(), "dump time only");
813
814 if (_subgraph_object_klasses == nullptr) {
815 _subgraph_object_klasses =
816 new (mtClass) GrowableArray<Klass*>(50, mtClass);
817 }
818
819 if (_k == orig_k) {
820 // Don't add the Klass containing the sub-graph to it's own klass
821 // initialization list.
822 return;
823 }
824
825 if (orig_k->is_instance_klass()) {
826 #ifdef ASSERT
827 InstanceKlass* ik = InstanceKlass::cast(orig_k);
828 if (CDSConfig::is_dumping_method_handles()) {
829 // -XX:AOTInitTestClass must be used carefully in regression tests to
830 // include only classes that are safe to aot-initialize.
831 assert(ik->class_loader() == nullptr ||
832 HeapShared::is_lambda_proxy_klass(ik) ||
833 AOTClassInitializer::has_test_class(),
834 "we can archive only instances of boot classes or lambda proxy classes");
835 } else {
836 assert(ik->class_loader() == nullptr, "must be boot class");
837 }
838 #endif
839 // vmClasses::xxx_klass() are not updated, need to check
840 // the original Klass*
841 if (orig_k == vmClasses::String_klass() ||
842 orig_k == vmClasses::Object_klass()) {
843 // Initialized early during VM initialization. No need to be added
844 // to the sub-graph object class list.
845 return;
846 }
847 check_allowed_klass(InstanceKlass::cast(orig_k));
848 } else if (orig_k->is_objArray_klass()) {
849 Klass* abk = ObjArrayKlass::cast(orig_k)->bottom_klass();
850 if (abk->is_instance_klass()) {
851 assert(InstanceKlass::cast(abk)->defined_by_boot_loader(),
852 "must be boot class");
853 check_allowed_klass(InstanceKlass::cast(ObjArrayKlass::cast(orig_k)->bottom_klass()));
854 }
855 if (orig_k == Universe::objectArrayKlass()) {
856 // Initialized early during Universe::genesis. No need to be added
857 // to the list.
858 return;
859 }
860 } else {
861 assert(orig_k->is_typeArray_klass(), "must be");
862 // Primitive type arrays are created early during Universe::genesis.
863 return;
864 }
865
866 if (log_is_enabled(Debug, aot, heap)) {
867 if (!_subgraph_object_klasses->contains(orig_k)) {
868 ResourceMark rm;
869 log_debug(aot, heap)("Adding klass %s", orig_k->external_name());
870 }
871 }
872
873 _subgraph_object_klasses->append_if_missing(orig_k);
874 _has_non_early_klasses |= is_non_early_klass(orig_k);
875 }
876
877 void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) {
878 #ifndef PRODUCT
879 if (AOTClassInitializer::has_test_class()) {
880 // The tests can cache arbitrary types of objects.
881 return;
882 }
883 #endif
884
885 if (ik->module()->name() == vmSymbols::java_base()) {
886 assert(ik->package() != nullptr, "classes in java.base cannot be in unnamed package");
887 return;
888 }
889
890 const char* lambda_msg = "";
891 if (CDSConfig::is_dumping_method_handles()) {
892 lambda_msg = ", or a lambda proxy class";
893 if (HeapShared::is_lambda_proxy_klass(ik) &&
894 (ik->class_loader() == nullptr ||
895 ik->class_loader() == SystemDictionary::java_platform_loader() ||
896 ik->class_loader() == SystemDictionary::java_system_loader())) {
897 return;
898 }
899 }
900
901 #ifndef PRODUCT
902 if (!ik->module()->is_named() && ik->package() == nullptr && ArchiveHeapTestClass != nullptr) {
903 // This class is loaded by ArchiveHeapTestClass
904 return;
905 }
906 const char* testcls_msg = ", or a test class in an unnamed package of an unnamed module";
907 #else
908 const char* testcls_msg = "";
909 #endif
910
911 ResourceMark rm;
912 log_error(aot, heap)("Class %s not allowed in archive heap. Must be in java.base%s%s",
913 ik->external_name(), lambda_msg, testcls_msg);
914 AOTMetaspace::unrecoverable_writing_error();
915 }
916
917 bool KlassSubGraphInfo::is_non_early_klass(Klass* k) {
918 if (k->is_objArray_klass()) {
919 k = ObjArrayKlass::cast(k)->bottom_klass();
920 }
921 if (k->is_instance_klass()) {
922 if (!SystemDictionaryShared::is_early_klass(InstanceKlass::cast(k))) {
923 ResourceMark rm;
924 log_info(aot, heap)("non-early: %s", k->external_name());
925 return true;
926 } else {
927 return false;
928 }
929 } else {
930 return false;
931 }
932 }
933
934 // Initialize an archived subgraph_info_record from the given KlassSubGraphInfo.
935 void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
936 _k = ArchiveBuilder::get_buffered_klass(info->klass());
937 _entry_field_records = nullptr;
938 _subgraph_object_klasses = nullptr;
939 _is_full_module_graph = info->is_full_module_graph();
940
941 if (_is_full_module_graph) {
942 // Consider all classes referenced by the full module graph as early -- we will be
943 // allocating objects of these classes during JVMTI early phase, so they cannot
944 // be processed by (non-early) JVMTI ClassFileLoadHook
945 _has_non_early_klasses = false;
946 } else {
947 _has_non_early_klasses = info->has_non_early_klasses();
948 }
949
950 if (_has_non_early_klasses) {
951 ResourceMark rm;
952 log_info(aot, heap)(
953 "Subgraph of klass %s has non-early klasses and cannot be used when JVMTI ClassFileLoadHook is enabled",
954 _k->external_name());
955 }
956
957 // populate the entry fields
958 GrowableArray<int>* entry_fields = info->subgraph_entry_fields();
959 if (entry_fields != nullptr) {
960 int num_entry_fields = entry_fields->length();
961 assert(num_entry_fields % 2 == 0, "sanity");
962 _entry_field_records =
963 ArchiveBuilder::new_ro_array<int>(num_entry_fields);
964 for (int i = 0 ; i < num_entry_fields; i++) {
965 _entry_field_records->at_put(i, entry_fields->at(i));
966 }
967 }
968
969 // <recorded_klasses> has the Klasses of all the objects that are referenced by this subgraph.
970 // Copy those that need to be explicitly initialized into <_subgraph_object_klasses>.
971 GrowableArray<Klass*>* recorded_klasses = info->subgraph_object_klasses();
972 if (recorded_klasses != nullptr) {
973 // AOT-inited classes are automatically marked as "initialized" during bootstrap. When
974 // programmatically loading a subgraph, we only need to explicitly initialize the classes
975 // that are not aot-inited.
976 int num_to_copy = 0;
977 for (int i = 0; i < recorded_klasses->length(); i++) {
978 Klass* subgraph_k = ArchiveBuilder::get_buffered_klass(recorded_klasses->at(i));
979 if (!subgraph_k->has_aot_initialized_mirror()) {
980 num_to_copy ++;
981 }
982 }
983
984 _subgraph_object_klasses = ArchiveBuilder::new_ro_array<Klass*>(num_to_copy);
985 bool is_special = (_k == ArchiveBuilder::get_buffered_klass(vmClasses::Object_klass()));
986 for (int i = 0, n = 0; i < recorded_klasses->length(); i++) {
987 Klass* subgraph_k = ArchiveBuilder::get_buffered_klass(recorded_klasses->at(i));
988 if (subgraph_k->has_aot_initialized_mirror()) {
989 continue;
990 }
991 if (log_is_enabled(Info, aot, heap)) {
992 ResourceMark rm;
993 const char* owner_name = is_special ? "<special>" : _k->external_name();
994 if (subgraph_k->is_instance_klass()) {
995 InstanceKlass* src_ik = InstanceKlass::cast(ArchiveBuilder::current()->get_source_addr(subgraph_k));
996 }
997 log_info(aot, heap)(
998 "Archived object klass %s (%2d) => %s",
999 owner_name, n, subgraph_k->external_name());
1000 }
1001 _subgraph_object_klasses->at_put(n, subgraph_k);
1002 ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(n));
1003 n++;
1004 }
1005 }
1006
1007 ArchivePtrMarker::mark_pointer(&_k);
1008 ArchivePtrMarker::mark_pointer(&_entry_field_records);
1009 ArchivePtrMarker::mark_pointer(&_subgraph_object_klasses);
1010 }
1011
1012 class HeapShared::CopyKlassSubGraphInfoToArchive : StackObj {
1013 CompactHashtableWriter* _writer;
1014 public:
1015 CopyKlassSubGraphInfoToArchive(CompactHashtableWriter* writer) : _writer(writer) {}
1016
1017 bool do_entry(Klass* klass, KlassSubGraphInfo& info) {
1018 if (info.subgraph_object_klasses() != nullptr || info.subgraph_entry_fields() != nullptr) {
1019 ArchivedKlassSubGraphInfoRecord* record = HeapShared::archive_subgraph_info(&info);
1020 Klass* buffered_k = ArchiveBuilder::get_buffered_klass(klass);
1021 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary((address)buffered_k);
1022 u4 delta = ArchiveBuilder::current()->any_to_offset_u4(record);
1023 _writer->add(hash, delta);
1024 }
1025 return true; // keep on iterating
1026 }
1027 };
1028
1029 ArchivedKlassSubGraphInfoRecord* HeapShared::archive_subgraph_info(KlassSubGraphInfo* info) {
1030 ArchivedKlassSubGraphInfoRecord* record =
1031 (ArchivedKlassSubGraphInfoRecord*)ArchiveBuilder::ro_region_alloc(sizeof(ArchivedKlassSubGraphInfoRecord));
1032 record->init(info);
1033 if (info == _dump_time_special_subgraph) {
1034 _run_time_special_subgraph = record;
1035 }
1036 return record;
1037 }
1038
1039 // Build the records of archived subgraph infos, which include:
1040 // - Entry points to all subgraphs from the containing class mirror. The entry
1041 // points are static fields in the mirror. For each entry point, the field
1042 // offset, and value are recorded in the sub-graph
1043 // info. The value is stored back to the corresponding field at runtime.
1044 // - A list of klasses that need to be loaded/initialized before archived
1045 // java object sub-graph can be accessed at runtime.
1046 void HeapShared::write_subgraph_info_table() {
1047 // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
1048 DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
1049 CompactHashtableStats stats;
1050
1051 _run_time_subgraph_info_table.reset();
1052
1053 CompactHashtableWriter writer(d_table->_count, &stats);
1054 CopyKlassSubGraphInfoToArchive copy(&writer);
1055 d_table->iterate(©);
1056 writer.dump(&_run_time_subgraph_info_table, "subgraphs");
1057
1058 #ifndef PRODUCT
1059 if (ArchiveHeapTestClass != nullptr) {
1060 size_t len = strlen(ArchiveHeapTestClass) + 1;
1061 Array<char>* array = ArchiveBuilder::new_ro_array<char>((int)len);
1062 strncpy(array->adr_at(0), ArchiveHeapTestClass, len);
1063 _archived_ArchiveHeapTestClass = array;
1064 }
1065 #endif
1066 if (log_is_enabled(Info, aot, heap)) {
1067 print_stats();
1068 }
1069 }
1070
1071 void HeapShared::add_root_segment(objArrayOop segment_oop) {
1072 assert(segment_oop != nullptr, "must be");
1073 assert(ArchiveHeapLoader::is_in_use(), "must be");
1074 if (_root_segments == nullptr) {
1075 _root_segments = new GrowableArrayCHeap<OopHandle, mtClassShared>(10);
1076 }
1077 _root_segments->push(OopHandle(Universe::vm_global(), segment_oop));
1078 }
1079
1080 void HeapShared::init_root_segment_sizes(int max_size_elems) {
1081 _root_segment_max_size_elems = max_size_elems;
1082 }
1083
1084 void HeapShared::serialize_tables(SerializeClosure* soc) {
1085
1086 #ifndef PRODUCT
1087 soc->do_ptr(&_archived_ArchiveHeapTestClass);
1088 if (soc->reading() && _archived_ArchiveHeapTestClass != nullptr) {
1089 _test_class_name = _archived_ArchiveHeapTestClass->adr_at(0);
1090 setup_test_class(_test_class_name);
1091 }
1092 #endif
1093
1094 _run_time_subgraph_info_table.serialize_header(soc);
1095 soc->do_ptr(&_run_time_special_subgraph);
1096 }
1097
1098 static void verify_the_heap(Klass* k, const char* which) {
1099 if (VerifyArchivedFields > 0) {
1100 ResourceMark rm;
1101 log_info(aot, heap)("Verify heap %s initializing static field(s) in %s",
1102 which, k->external_name());
1103
1104 VM_Verify verify_op;
1105 VMThread::execute(&verify_op);
1106
1107 if (VerifyArchivedFields > 1 && is_init_completed()) {
1108 // At this time, the oop->klass() of some archived objects in the heap may not
1109 // have been loaded into the system dictionary yet. Nevertheless, oop->klass() should
1110 // have enough information (object size, oop maps, etc) so that a GC can be safely
1111 // performed.
1112 //
1113 // -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage
1114 // to check for GC safety.
1115 log_info(aot, heap)("Trigger GC %s initializing static field(s) in %s",
1116 which, k->external_name());
1117 FlagSetting fs1(VerifyBeforeGC, true);
1118 FlagSetting fs2(VerifyDuringGC, true);
1119 FlagSetting fs3(VerifyAfterGC, true);
1120 Universe::heap()->collect(GCCause::_java_lang_system_gc);
1121 }
1122 }
1123 }
1124
1125 // Before GC can execute, we must ensure that all oops reachable from HeapShared::roots()
1126 // have a valid klass. I.e., oopDesc::klass() must have already been resolved.
1127 //
1128 // Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI
1129 // ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In
1130 // this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots.
1131 void HeapShared::resolve_classes(JavaThread* current) {
1132 assert(CDSConfig::is_using_archive(), "runtime only!");
1133 if (!ArchiveHeapLoader::is_in_use()) {
1134 return; // nothing to do
1135 }
1136 resolve_classes_for_subgraphs(current, archive_subgraph_entry_fields);
1137 resolve_classes_for_subgraphs(current, fmg_archive_subgraph_entry_fields);
1138 }
1139
1140 void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) {
1141 for (int i = 0; fields[i].valid(); i++) {
1142 ArchivableStaticFieldInfo* info = &fields[i];
1143 TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
1144 InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name);
1145 assert(k != nullptr && k->defined_by_boot_loader(), "sanity");
1146 resolve_classes_for_subgraph_of(current, k);
1147 }
1148 }
1149
1150 void HeapShared::resolve_classes_for_subgraph_of(JavaThread* current, Klass* k) {
1151 JavaThread* THREAD = current;
1152 ExceptionMark em(THREAD);
1153 const ArchivedKlassSubGraphInfoRecord* record =
1154 resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
1155 if (HAS_PENDING_EXCEPTION) {
1156 CLEAR_PENDING_EXCEPTION;
1157 }
1158 if (record == nullptr) {
1159 clear_archived_roots_of(k);
1160 }
1161 }
1162
1163 void HeapShared::initialize_java_lang_invoke(TRAPS) {
1164 if (CDSConfig::is_using_aot_linked_classes() || CDSConfig::is_dumping_method_handles()) {
1165 resolve_or_init("java/lang/invoke/Invokers$Holder", true, CHECK);
1166 resolve_or_init("java/lang/invoke/MethodHandle", true, CHECK);
1167 resolve_or_init("java/lang/invoke/MethodHandleNatives", true, CHECK);
1168 resolve_or_init("java/lang/invoke/DirectMethodHandle$Holder", true, CHECK);
1169 resolve_or_init("java/lang/invoke/DelegatingMethodHandle$Holder", true, CHECK);
1170 resolve_or_init("java/lang/invoke/LambdaForm$Holder", true, CHECK);
1171 resolve_or_init("java/lang/invoke/BoundMethodHandle$Species_L", true, CHECK);
1172 }
1173 }
1174
1175 // Initialize the InstanceKlasses of objects that are reachable from the following roots:
1176 // - interned strings
1177 // - Klass::java_mirror() -- including aot-initialized mirrors such as those of Enum klasses.
1178 // - ConstantPool::resolved_references()
1179 // - Universe::<xxx>_exception_instance()
1180 //
1181 // For example, if this enum class is initialized at AOT cache assembly time:
1182 //
1183 // enum Fruit {
1184 // APPLE, ORANGE, BANANA;
1185 // static final Set<Fruit> HAVE_SEEDS = new HashSet<>(Arrays.asList(APPLE, ORANGE));
1186 // }
1187 //
1188 // the aot-initialized mirror of Fruit has a static field that references HashSet, which
1189 // should be initialized before any Java code can access the Fruit class. Note that
1190 // HashSet itself doesn't necessary need to be an aot-initialized class.
1191 void HeapShared::init_classes_for_special_subgraph(Handle class_loader, TRAPS) {
1192 if (!ArchiveHeapLoader::is_in_use()) {
1193 return;
1194 }
1195
1196 assert( _run_time_special_subgraph != nullptr, "must be");
1197 Array<Klass*>* klasses = _run_time_special_subgraph->subgraph_object_klasses();
1198 if (klasses != nullptr) {
1199 for (int pass = 0; pass < 2; pass ++) {
1200 for (int i = 0; i < klasses->length(); i++) {
1201 Klass* k = klasses->at(i);
1202 if (k->class_loader_data() == nullptr) {
1203 // This class is not yet loaded. We will initialize it in a later phase.
1204 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
1205 // but k is part of AOTLinkedClassCategory::BOOT2.
1206 continue;
1207 }
1208 if (k->class_loader() == class_loader()) {
1209 if (pass == 0) {
1210 if (k->is_instance_klass()) {
1211 InstanceKlass::cast(k)->link_class(CHECK);
1212 }
1213 } else {
1214 resolve_or_init(k, /*do_init*/true, CHECK);
1215 }
1216 }
1217 }
1218 }
1219 }
1220 }
1221
1222 void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
1223 JavaThread* THREAD = current;
1224 if (!ArchiveHeapLoader::is_in_use()) {
1225 return; // nothing to do
1226 }
1227
1228 if (k->name()->equals("jdk/internal/module/ArchivedModuleGraph") &&
1229 !CDSConfig::is_using_optimized_module_handling() &&
1230 // archive was created with --module-path
1231 AOTClassLocationConfig::runtime()->num_module_paths() > 0) {
1232 // ArchivedModuleGraph was created with a --module-path that's different than the runtime --module-path.
1233 // Thus, it might contain references to modules that do not exist at runtime. We cannot use it.
1234 log_info(aot, heap)("Skip initializing ArchivedModuleGraph subgraph: is_using_optimized_module_handling=%s num_module_paths=%d",
1235 BOOL_TO_STR(CDSConfig::is_using_optimized_module_handling()),
1236 AOTClassLocationConfig::runtime()->num_module_paths());
1237 return;
1238 }
1239
1240 ExceptionMark em(THREAD);
1241 const ArchivedKlassSubGraphInfoRecord* record =
1242 resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
1243
1244 if (HAS_PENDING_EXCEPTION) {
1245 CLEAR_PENDING_EXCEPTION;
1246 // None of the field value will be set if there was an exception when initializing the classes.
1247 // The java code will not see any of the archived objects in the
1248 // subgraphs referenced from k in this case.
1249 return;
1250 }
1251
1252 if (record != nullptr) {
1253 init_archived_fields_for(k, record);
1254 }
1255 }
1256
1257 const ArchivedKlassSubGraphInfoRecord*
1258 HeapShared::resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS) {
1259 assert(!CDSConfig::is_dumping_heap(), "Should not be called when dumping heap");
1260
1261 if (!k->in_aot_cache()) {
1262 return nullptr;
1263 }
1264 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
1265 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
1266
1267 #ifndef PRODUCT
1268 if (_test_class_name != nullptr && k->name()->equals(_test_class_name) && record != nullptr) {
1269 _test_class = k;
1270 _test_class_record = record;
1271 }
1272 #endif
1273
1274 // Initialize from archived data. Currently this is done only
1275 // during VM initialization time. No lock is needed.
1276 if (record == nullptr) {
1277 if (log_is_enabled(Info, aot, heap)) {
1278 ResourceMark rm(THREAD);
1279 log_info(aot, heap)("subgraph %s is not recorded",
1280 k->external_name());
1281 }
1282 return nullptr;
1283 } else {
1284 if (record->is_full_module_graph() && !CDSConfig::is_using_full_module_graph()) {
1285 if (log_is_enabled(Info, aot, heap)) {
1286 ResourceMark rm(THREAD);
1287 log_info(aot, heap)("subgraph %s cannot be used because full module graph is disabled",
1288 k->external_name());
1289 }
1290 return nullptr;
1291 }
1292
1293 if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) {
1294 if (log_is_enabled(Info, aot, heap)) {
1295 ResourceMark rm(THREAD);
1296 log_info(aot, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
1297 k->external_name());
1298 }
1299 return nullptr;
1300 }
1301
1302 if (log_is_enabled(Info, aot, heap)) {
1303 ResourceMark rm;
1304 log_info(aot, heap)("%s subgraph %s ", do_init ? "init" : "resolve", k->external_name());
1305 }
1306
1307 resolve_or_init(k, do_init, CHECK_NULL);
1308
1309 // Load/link/initialize the klasses of the objects in the subgraph.
1310 // nullptr class loader is used.
1311 Array<Klass*>* klasses = record->subgraph_object_klasses();
1312 if (klasses != nullptr) {
1313 for (int i = 0; i < klasses->length(); i++) {
1314 Klass* klass = klasses->at(i);
1315 if (!klass->in_aot_cache()) {
1316 return nullptr;
1317 }
1318 resolve_or_init(klass, do_init, CHECK_NULL);
1319 }
1320 }
1321 }
1322
1323 return record;
1324 }
1325
1326 void HeapShared::resolve_or_init(const char* klass_name, bool do_init, TRAPS) {
1327 TempNewSymbol klass_name_sym = SymbolTable::new_symbol(klass_name);
1328 InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name_sym);
1329 if (k == nullptr) {
1330 return;
1331 }
1332 assert(k->defined_by_boot_loader(), "sanity");
1333 resolve_or_init(k, false, CHECK);
1334 if (do_init) {
1335 resolve_or_init(k, true, CHECK);
1336 }
1337 }
1338
1339 void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) {
1340 if (!do_init) {
1341 if (k->class_loader_data() == nullptr) {
1342 Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
1343 if (resolved_k->is_array_klass()) {
1344 assert(resolved_k == k || resolved_k == k->super(), "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
1345 } else {
1346 assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
1347 }
1348 }
1349 } else {
1350 assert(k->class_loader_data() != nullptr, "must have been resolved by HeapShared::resolve_classes");
1351 if (k->is_instance_klass()) {
1352 InstanceKlass* ik = InstanceKlass::cast(k);
1353 ik->initialize(CHECK);
1354 } else if (k->is_objArray_klass()) {
1355 ObjArrayKlass* oak = ObjArrayKlass::cast(k);
1356 oak->initialize(CHECK);
1357 }
1358 }
1359 }
1360
1361 void HeapShared::init_archived_fields_for(Klass* k, const ArchivedKlassSubGraphInfoRecord* record) {
1362 verify_the_heap(k, "before");
1363
1364 // Load the subgraph entry fields from the record and store them back to
1365 // the corresponding fields within the mirror.
1366 oop m = k->java_mirror();
1367 Array<int>* entry_field_records = record->entry_field_records();
1368 if (entry_field_records != nullptr) {
1369 int efr_len = entry_field_records->length();
1370 assert(efr_len % 2 == 0, "sanity");
1371 for (int i = 0; i < efr_len; i += 2) {
1372 int field_offset = entry_field_records->at(i);
1373 int root_index = entry_field_records->at(i+1);
1374 oop v = get_root(root_index, /*clear=*/true);
1375 if (k->has_aot_initialized_mirror()) {
1376 assert(v == m->obj_field(field_offset), "must be aot-initialized");
1377 } else {
1378 m->obj_field_put(field_offset, v);
1379 }
1380 log_debug(aot, heap)(" " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
1381 }
1382
1383 // Done. Java code can see the archived sub-graphs referenced from k's
1384 // mirror after this point.
1385 if (log_is_enabled(Info, aot, heap)) {
1386 ResourceMark rm;
1387 log_info(aot, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT "%s%s",
1388 k->external_name(), p2i(k), JvmtiExport::is_early_phase() ? " (early)" : "",
1389 k->has_aot_initialized_mirror() ? " (aot-inited)" : "");
1390 }
1391 }
1392
1393 verify_the_heap(k, "after ");
1394 }
1395
1396 void HeapShared::clear_archived_roots_of(Klass* k) {
1397 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k);
1398 const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
1399 if (record != nullptr) {
1400 Array<int>* entry_field_records = record->entry_field_records();
1401 if (entry_field_records != nullptr) {
1402 int efr_len = entry_field_records->length();
1403 assert(efr_len % 2 == 0, "sanity");
1404 for (int i = 0; i < efr_len; i += 2) {
1405 int root_index = entry_field_records->at(i+1);
1406 clear_root(root_index);
1407 }
1408 }
1409 }
1410 }
1411
1412 // Push all oop fields (or oop array elemenets in case of an objArray) in
1413 // _referencing_obj onto the _stack.
1414 class HeapShared::OopFieldPusher: public BasicOopIterateClosure {
1415 PendingOopStack* _stack;
1416 GrowableArray<oop> _found_oop_fields;
1417 int _level;
1418 bool _record_klasses_only;
1419 KlassSubGraphInfo* _subgraph_info;
1420 oop _referencing_obj;
1421 bool _is_java_lang_ref;
1422 public:
1423 OopFieldPusher(PendingOopStack* stack,
1424 int level,
1425 bool record_klasses_only,
1426 KlassSubGraphInfo* subgraph_info,
1427 oop orig) :
1428 _stack(stack),
1429 _found_oop_fields(),
1430 _level(level),
1431 _record_klasses_only(record_klasses_only),
1432 _subgraph_info(subgraph_info),
1433 _referencing_obj(orig) {
1434 _is_java_lang_ref = AOTReferenceObjSupport::check_if_ref_obj(orig);
1435 }
1436 void do_oop(narrowOop *p) { OopFieldPusher::do_oop_work(p); }
1437 void do_oop( oop *p) { OopFieldPusher::do_oop_work(p); }
1438
1439 ~OopFieldPusher() {
1440 while (_found_oop_fields.length() > 0) {
1441 // This produces the exact same traversal order as the previous version
1442 // of OopFieldPusher that recurses on the C stack -- a depth-first search,
1443 // walking the oop fields in _referencing_obj by ascending field offsets.
1444 oop obj = _found_oop_fields.pop();
1445 _stack->push(PendingOop(obj, _referencing_obj, _level + 1));
1446 }
1447 }
1448
1449 protected:
1450 template <class T> void do_oop_work(T *p) {
1451 int field_offset = pointer_delta_as_int((char*)p, cast_from_oop<char*>(_referencing_obj));
1452 oop obj = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(_referencing_obj, field_offset);
1453 if (!CompressedOops::is_null(obj)) {
1454 if (_is_java_lang_ref && AOTReferenceObjSupport::skip_field(field_offset)) {
1455 // Do not follow these fields. They will be cleared to null.
1456 return;
1457 }
1458
1459 if (!_record_klasses_only && log_is_enabled(Debug, aot, heap)) {
1460 ResourceMark rm;
1461 log_debug(aot, heap)("(%d) %s[%d] ==> " PTR_FORMAT " size %zu %s", _level,
1462 _referencing_obj->klass()->external_name(), field_offset,
1463 p2i(obj), obj->size() * HeapWordSize, obj->klass()->external_name());
1464 if (log_is_enabled(Trace, aot, heap)) {
1465 LogTarget(Trace, aot, heap) log;
1466 LogStream out(log);
1467 obj->print_on(&out);
1468 }
1469 }
1470
1471 _found_oop_fields.push(obj);
1472 }
1473 }
1474
1475 public:
1476 oop referencing_obj() { return _referencing_obj; }
1477 KlassSubGraphInfo* subgraph_info() { return _subgraph_info; }
1478 };
1479
1480 // Checks if an oop has any non-null oop fields
1481 class PointsToOopsChecker : public BasicOopIterateClosure {
1482 bool _result;
1483
1484 template <class T> void check(T *p) {
1485 _result |= (HeapAccess<>::oop_load(p) != nullptr);
1486 }
1487
1488 public:
1489 PointsToOopsChecker() : _result(false) {}
1490 void do_oop(narrowOop *p) { check(p); }
1491 void do_oop( oop *p) { check(p); }
1492 bool result() { return _result; }
1493 };
1494
1495 HeapShared::CachedOopInfo HeapShared::make_cached_oop_info(oop obj, oop referrer) {
1496 PointsToOopsChecker points_to_oops_checker;
1497 obj->oop_iterate(&points_to_oops_checker);
1498 return CachedOopInfo(OopHandle(Universe::vm_global(), referrer), points_to_oops_checker.result());
1499 }
1500
1501 void HeapShared::init_box_classes(TRAPS) {
1502 if (ArchiveHeapLoader::is_in_use()) {
1503 vmClasses::Boolean_klass()->initialize(CHECK);
1504 vmClasses::Character_klass()->initialize(CHECK);
1505 vmClasses::Float_klass()->initialize(CHECK);
1506 vmClasses::Double_klass()->initialize(CHECK);
1507 vmClasses::Byte_klass()->initialize(CHECK);
1508 vmClasses::Short_klass()->initialize(CHECK);
1509 vmClasses::Integer_klass()->initialize(CHECK);
1510 vmClasses::Long_klass()->initialize(CHECK);
1511 vmClasses::Void_klass()->initialize(CHECK);
1512 }
1513 }
1514
1515 // (1) If orig_obj has not been archived yet, archive it.
1516 // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called),
1517 // trace all objects that are reachable from it, and make sure these objects are archived.
1518 // (3) Record the klasses of all objects that are reachable from orig_obj (including those that
1519 // were already archived when this function is called)
1520 bool HeapShared::archive_reachable_objects_from(int level,
1521 KlassSubGraphInfo* subgraph_info,
1522 oop orig_obj) {
1523 assert(orig_obj != nullptr, "must be");
1524 PendingOopStack stack;
1525 stack.push(PendingOop(orig_obj, nullptr, level));
1526
1527 while (stack.length() > 0) {
1528 PendingOop po = stack.pop();
1529 _object_being_archived = po;
1530 bool status = walk_one_object(&stack, po.level(), subgraph_info, po.obj(), po.referrer());
1531 _object_being_archived = PendingOop();
1532
1533 if (!status) {
1534 // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1535 // as the Java code will take care of initializing this field dynamically.
1536 assert(level == 1, "VM should have exited with unarchivable objects for _level > 1");
1537 return false;
1538 }
1539 }
1540
1541 return true;
1542 }
1543
1544 bool HeapShared::walk_one_object(PendingOopStack* stack, int level, KlassSubGraphInfo* subgraph_info,
1545 oop orig_obj, oop referrer) {
1546 assert(orig_obj != nullptr, "must be");
1547 if (!JavaClasses::is_supported_for_archiving(orig_obj)) {
1548 // This object has injected fields that cannot be supported easily, so we disallow them for now.
1549 // If you get an error here, you probably made a change in the JDK library that has added
1550 // these objects that are referenced (directly or indirectly) by static fields.
1551 ResourceMark rm;
1552 log_error(aot, heap)("Cannot archive object " PTR_FORMAT " of class %s", p2i(orig_obj), orig_obj->klass()->external_name());
1553 debug_trace();
1554 AOTMetaspace::unrecoverable_writing_error();
1555 }
1556
1557 if (log_is_enabled(Debug, aot, heap) && java_lang_Class::is_instance(orig_obj)) {
1558 ResourceMark rm;
1559 LogTarget(Debug, aot, heap) log;
1560 LogStream out(log);
1561 out.print("Found java mirror " PTR_FORMAT " ", p2i(orig_obj));
1562 Klass* k = java_lang_Class::as_Klass(orig_obj);
1563 if (k != nullptr) {
1564 out.print("%s", k->external_name());
1565 } else {
1566 out.print("primitive");
1567 }
1568 out.print_cr("; scratch mirror = " PTR_FORMAT,
1569 p2i(scratch_java_mirror(orig_obj)));
1570 }
1571
1572 if (java_lang_Class::is_instance(orig_obj)) {
1573 Klass* k = java_lang_Class::as_Klass(orig_obj);
1574 if (RegeneratedClasses::has_been_regenerated(k)) {
1575 orig_obj = RegeneratedClasses::get_regenerated_object(k)->java_mirror();
1576 }
1577 }
1578
1579 if (CDSConfig::is_initing_classes_at_dump_time()) {
1580 if (java_lang_Class::is_instance(orig_obj)) {
1581 orig_obj = scratch_java_mirror(orig_obj);
1582 assert(orig_obj != nullptr, "must be archived");
1583 }
1584 } else if (java_lang_Class::is_instance(orig_obj) && subgraph_info != _dump_time_special_subgraph) {
1585 // Without CDSConfig::is_initing_classes_at_dump_time(), we only allow archived objects to
1586 // point to the mirrors of (1) j.l.Object, (2) primitive classes, and (3) box classes. These are initialized
1587 // very early by HeapShared::init_box_classes().
1588 if (orig_obj == vmClasses::Object_klass()->java_mirror()
1589 || java_lang_Class::is_primitive(orig_obj)
1590 || orig_obj == vmClasses::Boolean_klass()->java_mirror()
1591 || orig_obj == vmClasses::Character_klass()->java_mirror()
1592 || orig_obj == vmClasses::Float_klass()->java_mirror()
1593 || orig_obj == vmClasses::Double_klass()->java_mirror()
1594 || orig_obj == vmClasses::Byte_klass()->java_mirror()
1595 || orig_obj == vmClasses::Short_klass()->java_mirror()
1596 || orig_obj == vmClasses::Integer_klass()->java_mirror()
1597 || orig_obj == vmClasses::Long_klass()->java_mirror()
1598 || orig_obj == vmClasses::Void_klass()->java_mirror()) {
1599 orig_obj = scratch_java_mirror(orig_obj);
1600 assert(orig_obj != nullptr, "must be archived");
1601 } else {
1602 // If you get an error here, you probably made a change in the JDK library that has added a Class
1603 // object that is referenced (directly or indirectly) by an ArchivableStaticFieldInfo
1604 // defined at the top of this file.
1605 log_error(aot, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
1606 debug_trace();
1607 AOTMetaspace::unrecoverable_writing_error();
1608 }
1609 }
1610
1611 if (has_been_seen_during_subgraph_recording(orig_obj)) {
1612 // orig_obj has already been archived and traced. Nothing more to do.
1613 return true;
1614 } else {
1615 set_has_been_seen_during_subgraph_recording(orig_obj);
1616 }
1617
1618 bool already_archived = has_been_archived(orig_obj);
1619 bool record_klasses_only = already_archived;
1620 if (!already_archived) {
1621 ++_num_new_archived_objs;
1622 if (!archive_object(orig_obj, referrer, subgraph_info)) {
1623 // Skip archiving the sub-graph referenced from the current entry field.
1624 ResourceMark rm;
1625 log_error(aot, heap)(
1626 "Cannot archive the sub-graph referenced from %s object ("
1627 PTR_FORMAT ") size %zu, skipped.",
1628 orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize);
1629 if (level == 1) {
1630 // Don't archive a subgraph root that's too big. For archives static fields, that's OK
1631 // as the Java code will take care of initializing this field dynamically.
1632 return false;
1633 } else {
1634 // We don't know how to handle an object that has been archived, but some of its reachable
1635 // objects cannot be archived. Bail out for now. We might need to fix this in the future if
1636 // we have a real use case.
1637 AOTMetaspace::unrecoverable_writing_error();
1638 }
1639 }
1640 }
1641
1642 Klass *orig_k = orig_obj->klass();
1643 subgraph_info->add_subgraph_object_klass(orig_k);
1644
1645 {
1646 // Find all the oops that are referenced by orig_obj, push them onto the stack
1647 // so we can work on them next.
1648 ResourceMark rm;
1649 OopFieldPusher pusher(stack, level, record_klasses_only, subgraph_info, orig_obj);
1650 orig_obj->oop_iterate(&pusher);
1651 }
1652
1653 if (CDSConfig::is_initing_classes_at_dump_time()) {
1654 // The classes of all archived enum instances have been marked as aot-init,
1655 // so there's nothing else to be done in the production run.
1656 } else {
1657 // This is legacy support for enum classes before JEP 483 -- we cannot rerun
1658 // the enum's <clinit> in the production run, so special handling is needed.
1659 if (CDSEnumKlass::is_enum_obj(orig_obj)) {
1660 CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
1661 }
1662 }
1663
1664 return true;
1665 }
1666
1667 //
1668 // Start from the given static field in a java mirror and archive the
1669 // complete sub-graph of java heap objects that are reached directly
1670 // or indirectly from the starting object by following references.
1671 // Sub-graph archiving restrictions (current):
1672 //
1673 // - All classes of objects in the archived sub-graph (including the
1674 // entry class) must be boot class only.
1675 // - No java.lang.Class instance (java mirror) can be included inside
1676 // an archived sub-graph. Mirror can only be the sub-graph entry object.
1677 //
1678 // The Java heap object sub-graph archiving process (see OopFieldPusher):
1679 //
1680 // 1) Java object sub-graph archiving starts from a given static field
1681 // within a Class instance (java mirror). If the static field is a
1682 // reference field and points to a non-null java object, proceed to
1683 // the next step.
1684 //
1685 // 2) Archives the referenced java object. If an archived copy of the
1686 // current object already exists, updates the pointer in the archived
1687 // copy of the referencing object to point to the current archived object.
1688 // Otherwise, proceed to the next step.
1689 //
1690 // 3) Follows all references within the current java object and recursively
1691 // archive the sub-graph of objects starting from each reference.
1692 //
1693 // 4) Updates the pointer in the archived copy of referencing object to
1694 // point to the current archived object.
1695 //
1696 // 5) The Klass of the current java object is added to the list of Klasses
1697 // for loading and initializing before any object in the archived graph can
1698 // be accessed at runtime.
1699 //
1700 void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k,
1701 const char* klass_name,
1702 int field_offset,
1703 const char* field_name) {
1704 assert(CDSConfig::is_dumping_heap(), "dump time only");
1705 assert(k->defined_by_boot_loader(), "must be boot class");
1706
1707 oop m = k->java_mirror();
1708
1709 KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
1710 oop f = m->obj_field(field_offset);
1711
1712 log_debug(aot, heap)("Start archiving from: %s::%s (" PTR_FORMAT ")", klass_name, field_name, p2i(f));
1713
1714 if (!CompressedOops::is_null(f)) {
1715 if (log_is_enabled(Trace, aot, heap)) {
1716 LogTarget(Trace, aot, heap) log;
1717 LogStream out(log);
1718 f->print_on(&out);
1719 }
1720
1721 bool success = archive_reachable_objects_from(1, subgraph_info, f);
1722 if (!success) {
1723 log_error(aot, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)",
1724 klass_name, field_name);
1725 } else {
1726 // Note: the field value is not preserved in the archived mirror.
1727 // Record the field as a new subGraph entry point. The recorded
1728 // information is restored from the archive at runtime.
1729 subgraph_info->add_subgraph_entry_field(field_offset, f);
1730 log_info(aot, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(f));
1731 }
1732 } else {
1733 // The field contains null, we still need to record the entry point,
1734 // so it can be restored at runtime.
1735 subgraph_info->add_subgraph_entry_field(field_offset, nullptr);
1736 }
1737 }
1738
1739 #ifndef PRODUCT
1740 class VerifySharedOopClosure: public BasicOopIterateClosure {
1741 public:
1742 void do_oop(narrowOop *p) { VerifySharedOopClosure::do_oop_work(p); }
1743 void do_oop( oop *p) { VerifySharedOopClosure::do_oop_work(p); }
1744
1745 protected:
1746 template <class T> void do_oop_work(T *p) {
1747 oop obj = RawAccess<>::oop_load(p);
1748 if (!CompressedOops::is_null(obj)) {
1749 HeapShared::verify_reachable_objects_from(obj);
1750 }
1751 }
1752 };
1753
1754 void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) {
1755 assert(CDSConfig::is_dumping_heap(), "dump time only");
1756 assert(k->defined_by_boot_loader(), "must be boot class");
1757
1758 oop m = k->java_mirror();
1759 oop f = m->obj_field(field_offset);
1760 if (!CompressedOops::is_null(f)) {
1761 verify_subgraph_from(f);
1762 }
1763 }
1764
1765 void HeapShared::verify_subgraph_from(oop orig_obj) {
1766 if (!has_been_archived(orig_obj)) {
1767 // It's OK for the root of a subgraph to be not archived. See comments in
1768 // archive_reachable_objects_from().
1769 return;
1770 }
1771
1772 // Verify that all objects reachable from orig_obj are archived.
1773 init_seen_objects_table();
1774 verify_reachable_objects_from(orig_obj);
1775 delete_seen_objects_table();
1776 }
1777
1778 void HeapShared::verify_reachable_objects_from(oop obj) {
1779 _num_total_verifications ++;
1780 if (java_lang_Class::is_instance(obj)) {
1781 obj = scratch_java_mirror(obj);
1782 assert(obj != nullptr, "must be");
1783 }
1784 if (!has_been_seen_during_subgraph_recording(obj)) {
1785 set_has_been_seen_during_subgraph_recording(obj);
1786 assert(has_been_archived(obj), "must be");
1787 VerifySharedOopClosure walker;
1788 obj->oop_iterate(&walker);
1789 }
1790 }
1791 #endif
1792
1793 void HeapShared::check_special_subgraph_classes() {
1794 if (CDSConfig::is_initing_classes_at_dump_time()) {
1795 // We can have aot-initialized classes (such as Enums) that can reference objects
1796 // of arbitrary types. Currently, we trust the JEP 483 implementation to only
1797 // aot-initialize classes that are "safe".
1798 //
1799 // TODO: we need an automatic tool that checks the safety of aot-initialized
1800 // classes (when we extend the set of aot-initialized classes beyond JEP 483)
1801 return;
1802 } else {
1803 // In this case, the special subgraph should contain a few specific types
1804 GrowableArray<Klass*>* klasses = _dump_time_special_subgraph->subgraph_object_klasses();
1805 int num = klasses->length();
1806 for (int i = 0; i < num; i++) {
1807 Klass* subgraph_k = klasses->at(i);
1808 Symbol* name = subgraph_k->name();
1809 if (subgraph_k->is_instance_klass() &&
1810 name != vmSymbols::java_lang_Class() &&
1811 name != vmSymbols::java_lang_String() &&
1812 name != vmSymbols::java_lang_ArithmeticException() &&
1813 name != vmSymbols::java_lang_ArrayIndexOutOfBoundsException() &&
1814 name != vmSymbols::java_lang_ArrayStoreException() &&
1815 name != vmSymbols::java_lang_ClassCastException() &&
1816 name != vmSymbols::java_lang_InternalError() &&
1817 name != vmSymbols::java_lang_NullPointerException()) {
1818 ResourceMark rm;
1819 fatal("special subgraph cannot have objects of type %s", subgraph_k->external_name());
1820 }
1821 }
1822 }
1823 }
1824
1825 HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = nullptr;
1826 HeapShared::PendingOop HeapShared::_object_being_archived;
1827 int HeapShared::_num_new_walked_objs;
1828 int HeapShared::_num_new_archived_objs;
1829 int HeapShared::_num_old_recorded_klasses;
1830
1831 int HeapShared::_num_total_subgraph_recordings = 0;
1832 int HeapShared::_num_total_walked_objs = 0;
1833 int HeapShared::_num_total_archived_objs = 0;
1834 int HeapShared::_num_total_recorded_klasses = 0;
1835 int HeapShared::_num_total_verifications = 0;
1836
1837 bool HeapShared::has_been_seen_during_subgraph_recording(oop obj) {
1838 return _seen_objects_table->get(obj) != nullptr;
1839 }
1840
1841 void HeapShared::set_has_been_seen_during_subgraph_recording(oop obj) {
1842 assert(!has_been_seen_during_subgraph_recording(obj), "sanity");
1843 _seen_objects_table->put_when_absent(obj, true);
1844 _seen_objects_table->maybe_grow();
1845 ++ _num_new_walked_objs;
1846 }
1847
1848 void HeapShared::start_recording_subgraph(InstanceKlass *k, const char* class_name, bool is_full_module_graph) {
1849 log_info(aot, heap)("Start recording subgraph(s) for archived fields in %s", class_name);
1850 init_subgraph_info(k, is_full_module_graph);
1851 init_seen_objects_table();
1852 _num_new_walked_objs = 0;
1853 _num_new_archived_objs = 0;
1854 _num_old_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses();
1855 }
1856
1857 void HeapShared::done_recording_subgraph(InstanceKlass *k, const char* class_name) {
1858 int num_new_recorded_klasses = get_subgraph_info(k)->num_subgraph_object_klasses() -
1859 _num_old_recorded_klasses;
1860 log_info(aot, heap)("Done recording subgraph(s) for archived fields in %s: "
1861 "walked %d objs, archived %d new objs, recorded %d classes",
1862 class_name, _num_new_walked_objs, _num_new_archived_objs,
1863 num_new_recorded_klasses);
1864
1865 delete_seen_objects_table();
1866
1867 _num_total_subgraph_recordings ++;
1868 _num_total_walked_objs += _num_new_walked_objs;
1869 _num_total_archived_objs += _num_new_archived_objs;
1870 _num_total_recorded_klasses += num_new_recorded_klasses;
1871 }
1872
1873 class ArchivableStaticFieldFinder: public FieldClosure {
1874 InstanceKlass* _ik;
1875 Symbol* _field_name;
1876 bool _found;
1877 int _offset;
1878 public:
1879 ArchivableStaticFieldFinder(InstanceKlass* ik, Symbol* field_name) :
1880 _ik(ik), _field_name(field_name), _found(false), _offset(-1) {}
1881
1882 virtual void do_field(fieldDescriptor* fd) {
1883 if (fd->name() == _field_name) {
1884 assert(!_found, "fields can never be overloaded");
1885 if (is_reference_type(fd->field_type())) {
1886 _found = true;
1887 _offset = fd->offset();
1888 }
1889 }
1890 }
1891 bool found() { return _found; }
1892 int offset() { return _offset; }
1893 };
1894
1895 void HeapShared::init_subgraph_entry_fields(ArchivableStaticFieldInfo fields[],
1896 TRAPS) {
1897 for (int i = 0; fields[i].valid(); i++) {
1898 ArchivableStaticFieldInfo* info = &fields[i];
1899 TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
1900 TempNewSymbol field_name = SymbolTable::new_symbol(info->field_name);
1901 ResourceMark rm; // for stringStream::as_string() etc.
1902
1903 #ifndef PRODUCT
1904 bool is_test_class = (ArchiveHeapTestClass != nullptr) && (strcmp(info->klass_name, ArchiveHeapTestClass) == 0);
1905 const char* test_class_name = ArchiveHeapTestClass;
1906 #else
1907 bool is_test_class = false;
1908 const char* test_class_name = ""; // avoid C++ printf checks warnings.
1909 #endif
1910
1911 if (is_test_class) {
1912 log_warning(aot)("Loading ArchiveHeapTestClass %s ...", test_class_name);
1913 }
1914
1915 Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD);
1916 if (HAS_PENDING_EXCEPTION) {
1917 CLEAR_PENDING_EXCEPTION;
1918 stringStream st;
1919 st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name);
1920 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1921 }
1922
1923 if (!k->is_instance_klass()) {
1924 stringStream st;
1925 st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name);
1926 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1927 }
1928
1929 InstanceKlass* ik = InstanceKlass::cast(k);
1930 assert(InstanceKlass::cast(ik)->defined_by_boot_loader(),
1931 "Only support boot classes");
1932
1933 if (is_test_class) {
1934 if (ik->module()->is_named()) {
1935 // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary
1936 // core-lib classes. You need to at least append to the bootclasspath.
1937 stringStream st;
1938 st.print("ArchiveHeapTestClass %s is not in unnamed module", test_class_name);
1939 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1940 }
1941
1942 if (ik->package() != nullptr) {
1943 // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy.
1944 stringStream st;
1945 st.print("ArchiveHeapTestClass %s is not in unnamed package", test_class_name);
1946 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1947 }
1948 } else {
1949 if (ik->module()->name() != vmSymbols::java_base()) {
1950 // We don't want to deal with cases when a module is unavailable at runtime.
1951 // FUTURE -- load from archived heap only when module graph has not changed
1952 // between dump and runtime.
1953 stringStream st;
1954 st.print("%s is not in java.base module", info->klass_name);
1955 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1956 }
1957 }
1958
1959 if (is_test_class) {
1960 log_warning(aot)("Initializing ArchiveHeapTestClass %s ...", test_class_name);
1961 }
1962 ik->initialize(CHECK);
1963
1964 ArchivableStaticFieldFinder finder(ik, field_name);
1965 ik->do_local_static_fields(&finder);
1966 if (!finder.found()) {
1967 stringStream st;
1968 st.print("Unable to find the static T_OBJECT field %s::%s", info->klass_name, info->field_name);
1969 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
1970 }
1971
1972 info->klass = ik;
1973 info->offset = finder.offset();
1974 }
1975 }
1976
1977 void HeapShared::init_subgraph_entry_fields(TRAPS) {
1978 assert(CDSConfig::is_dumping_heap(), "must be");
1979 _dump_time_subgraph_info_table = new (mtClass)DumpTimeKlassSubGraphInfoTable();
1980 init_subgraph_entry_fields(archive_subgraph_entry_fields, CHECK);
1981 if (CDSConfig::is_dumping_full_module_graph()) {
1982 init_subgraph_entry_fields(fmg_archive_subgraph_entry_fields, CHECK);
1983 }
1984 }
1985
1986 #ifndef PRODUCT
1987 void HeapShared::setup_test_class(const char* test_class_name) {
1988 ArchivableStaticFieldInfo* p = archive_subgraph_entry_fields;
1989 int num_slots = sizeof(archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
1990 assert(p[num_slots - 2].klass_name == nullptr, "must have empty slot that's patched below");
1991 assert(p[num_slots - 1].klass_name == nullptr, "must have empty slot that marks the end of the list");
1992
1993 if (test_class_name != nullptr) {
1994 p[num_slots - 2].klass_name = test_class_name;
1995 p[num_slots - 2].field_name = ARCHIVE_TEST_FIELD_NAME;
1996 }
1997 }
1998
1999 // See if ik is one of the test classes that are pulled in by -XX:ArchiveHeapTestClass
2000 // during runtime. This may be called before the module system is initialized so
2001 // we cannot rely on InstanceKlass::module(), etc.
2002 bool HeapShared::is_a_test_class_in_unnamed_module(Klass* ik) {
2003 if (_test_class != nullptr) {
2004 if (ik == _test_class) {
2005 return true;
2006 }
2007 Array<Klass*>* klasses = _test_class_record->subgraph_object_klasses();
2008 if (klasses == nullptr) {
2009 return false;
2010 }
2011
2012 for (int i = 0; i < klasses->length(); i++) {
2013 Klass* k = klasses->at(i);
2014 if (k == ik) {
2015 Symbol* name;
2016 if (k->is_instance_klass()) {
2017 name = InstanceKlass::cast(k)->name();
2018 } else if (k->is_objArray_klass()) {
2019 Klass* bk = ObjArrayKlass::cast(k)->bottom_klass();
2020 if (!bk->is_instance_klass()) {
2021 return false;
2022 }
2023 name = bk->name();
2024 } else {
2025 return false;
2026 }
2027
2028 // See KlassSubGraphInfo::check_allowed_klass() - we only allow test classes
2029 // to be:
2030 // (A) java.base classes (which must not be in the unnamed module)
2031 // (B) test classes which must be in the unnamed package of the unnamed module.
2032 // So if we see a '/' character in the class name, it must be in (A);
2033 // otherwise it must be in (B).
2034 if (name->index_of_at(0, "/", 1) >= 0) {
2035 return false; // (A)
2036 }
2037
2038 return true; // (B)
2039 }
2040 }
2041 }
2042
2043 return false;
2044 }
2045
2046 void HeapShared::initialize_test_class_from_archive(JavaThread* current) {
2047 Klass* k = _test_class;
2048 if (k != nullptr && ArchiveHeapLoader::is_in_use()) {
2049 JavaThread* THREAD = current;
2050 ExceptionMark em(THREAD);
2051 const ArchivedKlassSubGraphInfoRecord* record =
2052 resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/false, THREAD);
2053
2054 // The _test_class is in the unnamed module, so it can't call CDS.initializeFromArchive()
2055 // from its <clinit> method. So we set up its "archivedObjects" field first, before
2056 // calling its <clinit>. This is not strictly clean, but it's a convenient way to write unit
2057 // test cases (see test/hotspot/jtreg/runtime/cds/appcds/cacheObject/ArchiveHeapTestClass.java).
2058 if (record != nullptr) {
2059 init_archived_fields_for(k, record);
2060 }
2061 resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
2062 }
2063 }
2064 #endif
2065
2066 void HeapShared::init_for_dumping(TRAPS) {
2067 if (CDSConfig::is_dumping_heap()) {
2068 setup_test_class(ArchiveHeapTestClass);
2069 _dumped_interned_strings = new (mtClass)DumpedInternedStrings(INITIAL_TABLE_SIZE, MAX_TABLE_SIZE);
2070 init_subgraph_entry_fields(CHECK);
2071 }
2072 }
2073
2074 void HeapShared::archive_object_subgraphs(ArchivableStaticFieldInfo fields[],
2075 bool is_full_module_graph) {
2076 _num_total_subgraph_recordings = 0;
2077 _num_total_walked_objs = 0;
2078 _num_total_archived_objs = 0;
2079 _num_total_recorded_klasses = 0;
2080 _num_total_verifications = 0;
2081
2082 // For each class X that has one or more archived fields:
2083 // [1] Dump the subgraph of each archived field
2084 // [2] Create a list of all the class of the objects that can be reached
2085 // by any of these static fields.
2086 // At runtime, these classes are initialized before X's archived fields
2087 // are restored by HeapShared::initialize_from_archived_subgraph().
2088 for (int i = 0; fields[i].valid(); ) {
2089 ArchivableStaticFieldInfo* info = &fields[i];
2090 const char* klass_name = info->klass_name;
2091
2092 if (CDSConfig::is_valhalla_preview() && strcmp(klass_name, "jdk/internal/module/ArchivedModuleGraph") == 0) {
2093 // FIXME -- ArchivedModuleGraph doesn't work when java.base is patched with valhalla classes.
2094 i++;
2095 continue;
2096 }
2097
2098 start_recording_subgraph(info->klass, klass_name, is_full_module_graph);
2099
2100 // If you have specified consecutive fields of the same klass in
2101 // fields[], these will be archived in the same
2102 // {start_recording_subgraph ... done_recording_subgraph} pass to
2103 // save time.
2104 for (; fields[i].valid(); i++) {
2105 ArchivableStaticFieldInfo* f = &fields[i];
2106 if (f->klass_name != klass_name) {
2107 break;
2108 }
2109
2110 archive_reachable_objects_from_static_field(f->klass, f->klass_name,
2111 f->offset, f->field_name);
2112 }
2113 done_recording_subgraph(info->klass, klass_name);
2114 }
2115
2116 log_info(aot, heap)("Archived subgraph records = %d",
2117 _num_total_subgraph_recordings);
2118 log_info(aot, heap)(" Walked %d objects", _num_total_walked_objs);
2119 log_info(aot, heap)(" Archived %d objects", _num_total_archived_objs);
2120 log_info(aot, heap)(" Recorded %d klasses", _num_total_recorded_klasses);
2121
2122 #ifndef PRODUCT
2123 for (int i = 0; fields[i].valid(); i++) {
2124 ArchivableStaticFieldInfo* f = &fields[i];
2125 verify_subgraph_from_static_field(f->klass, f->offset);
2126 }
2127 log_info(aot, heap)(" Verified %d references", _num_total_verifications);
2128 #endif
2129 }
2130
2131 // Keep track of the contents of the archived interned string table. This table
2132 // is used only by CDSHeapVerifier.
2133 void HeapShared::add_to_dumped_interned_strings(oop string) {
2134 assert_at_safepoint(); // DumpedInternedStrings uses raw oops
2135 assert(!ArchiveHeapWriter::is_string_too_large_to_archive(string), "must be");
2136 bool created;
2137 _dumped_interned_strings->put_if_absent(string, true, &created);
2138 if (created) {
2139 // Prevent string deduplication from changing the value field to
2140 // something not in the archive.
2141 java_lang_String::set_deduplication_forbidden(string);
2142 _dumped_interned_strings->maybe_grow();
2143 }
2144 }
2145
2146 bool HeapShared::is_dumped_interned_string(oop o) {
2147 return _dumped_interned_strings->get(o) != nullptr;
2148 }
2149
2150 // These tables should be used only within the CDS safepoint, so
2151 // delete them before we exit the safepoint. Otherwise the table will
2152 // contain bad oops after a GC.
2153 void HeapShared::delete_tables_with_raw_oops() {
2154 assert(_seen_objects_table == nullptr, "should have been deleted");
2155
2156 delete _dumped_interned_strings;
2157 _dumped_interned_strings = nullptr;
2158
2159 ArchiveHeapWriter::delete_tables_with_raw_oops();
2160 }
2161
2162 void HeapShared::debug_trace() {
2163 ResourceMark rm;
2164 oop referrer = _object_being_archived.referrer();
2165 if (referrer != nullptr) {
2166 LogStream ls(Log(aot, heap)::error());
2167 ls.print_cr("Reference trace");
2168 CDSHeapVerifier::trace_to_root(&ls, referrer);
2169 }
2170 }
2171
2172 #ifndef PRODUCT
2173 // At dump-time, find the location of all the non-null oop pointers in an archived heap
2174 // region. This way we can quickly relocate all the pointers without using
2175 // BasicOopIterateClosure at runtime.
2176 class FindEmbeddedNonNullPointers: public BasicOopIterateClosure {
2177 void* _start;
2178 BitMap *_oopmap;
2179 int _num_total_oops;
2180 int _num_null_oops;
2181 public:
2182 FindEmbeddedNonNullPointers(void* start, BitMap* oopmap)
2183 : _start(start), _oopmap(oopmap), _num_total_oops(0), _num_null_oops(0) {}
2184
2185 virtual void do_oop(narrowOop* p) {
2186 assert(UseCompressedOops, "sanity");
2187 _num_total_oops ++;
2188 narrowOop v = *p;
2189 if (!CompressedOops::is_null(v)) {
2190 size_t idx = p - (narrowOop*)_start;
2191 _oopmap->set_bit(idx);
2192 } else {
2193 _num_null_oops ++;
2194 }
2195 }
2196 virtual void do_oop(oop* p) {
2197 assert(!UseCompressedOops, "sanity");
2198 _num_total_oops ++;
2199 if ((*p) != nullptr) {
2200 size_t idx = p - (oop*)_start;
2201 _oopmap->set_bit(idx);
2202 } else {
2203 _num_null_oops ++;
2204 }
2205 }
2206 int num_total_oops() const { return _num_total_oops; }
2207 int num_null_oops() const { return _num_null_oops; }
2208 };
2209 #endif
2210
2211 void HeapShared::count_allocation(size_t size) {
2212 _total_obj_count ++;
2213 _total_obj_size += size;
2214 for (int i = 0; i < ALLOC_STAT_SLOTS; i++) {
2215 if (size <= (size_t(1) << i)) {
2216 _alloc_count[i] ++;
2217 _alloc_size[i] += size;
2218 return;
2219 }
2220 }
2221 }
2222
2223 static double avg_size(size_t size, size_t count) {
2224 double avg = 0;
2225 if (count > 0) {
2226 avg = double(size * HeapWordSize) / double(count);
2227 }
2228 return avg;
2229 }
2230
2231 void HeapShared::print_stats() {
2232 size_t huge_count = _total_obj_count;
2233 size_t huge_size = _total_obj_size;
2234
2235 for (int i = 0; i < ALLOC_STAT_SLOTS; i++) {
2236 size_t byte_size_limit = (size_t(1) << i) * HeapWordSize;
2237 size_t count = _alloc_count[i];
2238 size_t size = _alloc_size[i];
2239 log_info(aot, heap)("%8zu objects are <= %-6zu"
2240 " bytes (total %8zu bytes, avg %8.1f bytes)",
2241 count, byte_size_limit, size * HeapWordSize, avg_size(size, count));
2242 huge_count -= count;
2243 huge_size -= size;
2244 }
2245
2246 log_info(aot, heap)("%8zu huge objects (total %8zu bytes"
2247 ", avg %8.1f bytes)",
2248 huge_count, huge_size * HeapWordSize,
2249 avg_size(huge_size, huge_count));
2250 log_info(aot, heap)("%8zu total objects (total %8zu bytes"
2251 ", avg %8.1f bytes)",
2252 _total_obj_count, _total_obj_size * HeapWordSize,
2253 avg_size(_total_obj_size, _total_obj_count));
2254 }
2255
2256 bool HeapShared::is_archived_boot_layer_available(JavaThread* current) {
2257 TempNewSymbol klass_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_CLASS);
2258 InstanceKlass* k = SystemDictionary::find_instance_klass(current, klass_name, Handle());
2259 if (k == nullptr) {
2260 return false;
2261 } else {
2262 TempNewSymbol field_name = SymbolTable::new_symbol(ARCHIVED_BOOT_LAYER_FIELD);
2263 TempNewSymbol field_signature = SymbolTable::new_symbol("Ljdk/internal/module/ArchivedBootLayer;");
2264 fieldDescriptor fd;
2265 if (k->find_field(field_name, field_signature, true, &fd) != nullptr) {
2266 oop m = k->java_mirror();
2267 oop f = m->obj_field(fd.offset());
2268 if (CompressedOops::is_null(f)) {
2269 return false;
2270 }
2271 } else {
2272 return false;
2273 }
2274 }
2275 return true;
2276 }
2277
2278 #endif // INCLUDE_CDS_JAVA_HEAP