/* * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
#ifdef ASSERT bool HeapShared::is_archived_object_during_dumptime(oop p) {
assert(HeapShared::can_write(), "must be");
assert(DumpSharedSpaces, "this function is only used with -Xshare:dump"); return Universe::heap()->is_archived_object(p);
} #endif
staticbool is_subgraph_root_class_of(ArchivableStaticFieldInfo fields[], InstanceKlass* ik) { for (int i = 0; fields[i].valid(); i++) { if (fields[i].klass == ik) { returntrue;
}
} returnfalse;
}
unsigned HeapShared::oop_hash(oop const& p) { // Do not call p->identity_hash() as that will update the // object header. return primitive_hash(cast_from_oop<intptr_t>(p));
}
// Clean up jdk.internal.loader.ClassLoaders::bootLoader(), which is not // directly used for class loading, but rather is used by the core library // to keep track of resources, etc, loaded by the null class loader. // // Note, this object is non-null, and is not the same as // ClassLoaderData::the_null_class_loader_data()->class_loader(), // which is null.
log_debug(cds)("Resetting boot loader");
JavaValue result(T_OBJECT);
JavaCalls::call_static(&result,
vmClasses::jdk_internal_loader_ClassLoaders_klass(),
vmSymbols::bootLoader_name(),
vmSymbols::void_BuiltinClassLoader_signature(),
CHECK);
Handle boot_loader(THREAD, result.get_oop());
reset_states(boot_loader(), CHECK);
}
int HeapShared::append_root(oop obj) {
assert(DumpSharedSpaces, "dump-time only");
// No GC should happen since we aren't scanning _pending_roots.
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
if (_pending_roots == NULL) {
_pending_roots = new GrowableArrayCHeap<oop, mtClassShared>(500);
}
return _pending_roots->append(obj);
}
objArrayOop HeapShared::roots() { if (DumpSharedSpaces) {
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread"); if (!HeapShared::can_write()) { return NULL;
}
} else {
assert(UseSharedSpaces, "must be");
}
objArrayOop roots = (objArrayOop)_roots.resolve();
assert(roots != NULL, "should have been initialized"); return roots;
}
// Returns an objArray that contains all the roots of the archived objects
oop HeapShared::get_root(int index, bool clear) {
assert(index >= 0, "sanity"); if (DumpSharedSpaces) {
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
assert(_pending_roots != NULL, "sanity"); return _pending_roots->at(index);
} else {
assert(UseSharedSpaces, "must be");
assert(!_roots.is_empty(), "must have loaded shared heap");
oop result = roots()->obj_at(index); if (clear) {
clear_root(index);
} return result;
}
}
void HeapShared::clear_root(int index) {
assert(index >= 0, "sanity");
assert(UseSharedSpaces, "must be"); if (ArchiveHeapLoader::is_fully_available()) { if (log_is_enabled(Debug, cds, heap)) {
oop old = roots()->obj_at(index);
log_debug(cds, heap)("Clearing root %d: was " PTR_FORMAT, index, p2i(old));
}
roots()->obj_at_put(index, NULL);
}
}
assert(!obj->is_stackChunk(), "do not archive stack chunks");
oop ao = find_archived_heap_object(obj); if (ao != NULL) { // already archived return ao;
}
int len = obj->size(); if (G1CollectedHeap::heap()->is_archive_alloc_too_large(len)) {
log_debug(cds, heap)("Cannot archive, object (" PTR_FORMAT ") is too large: " SIZE_FORMAT,
p2i(obj), (size_t)obj->size()); return NULL;
}
oop archived_oop = cast_to_oop(G1CollectedHeap::heap()->archive_mem_allocate(len)); if (archived_oop != NULL) {
Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(obj), cast_from_oop<HeapWord*>(archived_oop), len); // Reinitialize markword to remove age/marking/locking/etc. // // We need to retain the identity_hash, because it may have been used by some hashtables // in the shared heap. This also has the side effect of pre-initializing the // identity_hash for all shared objects, so they are less likely to be written // into during run time, increasing the potential of memory sharing. int hash_original = obj->identity_hash();
archived_oop->set_mark(markWord::prototype().copy_set_hash(hash_original));
assert(archived_oop->mark().is_unlocked(), "sanity");
void HeapShared::mark_one_native_pointer(oop archived_obj, int offset) {
Metadata* ptr = archived_obj->metadata_field_acquire(offset); if (ptr != NULL) { // Set the native pointer to the requested address (at runtime, if the metadata // is mapped at the default location, it will be at this address).
address buffer_addr = ArchiveBuilder::current()->get_buffered_addr((address)ptr);
address requested_addr = ArchiveBuilder::current()->to_requested(buffer_addr);
archived_obj->metadata_field_put(offset, (Metadata*)requested_addr);
// Remember this pointer. At runtime, if the metadata is mapped at a non-default // location, the pointer needs to be patched (see ArchiveHeapLoader::patch_native_pointers()).
_native_pointers->append(archived_obj->field_addr<Metadata*>(offset));
log_debug(cds, heap, mirror)( "Marked metadata field at %d: " PTR_FORMAT " ==> " PTR_FORMAT,
offset, p2i(ptr), p2i(requested_addr));
}
}
// -- Handling of Enum objects // Java Enum classes have synthetic <clinit> methods that look like this // enum MyEnum {FOO, BAR} // MyEnum::<clinint> { // /*static final MyEnum*/ MyEnum::FOO = new MyEnum("FOO"); // /*static final MyEnum*/ MyEnum::BAR = new MyEnum("BAR"); // } // // If MyEnum::FOO object is referenced by any of the archived subgraphs, we must // ensure the archived value equals (in object address) to the runtime value of // MyEnum::FOO. // // However, since MyEnum::<clinint> is synthetically generated by javac, there's // no way of programmatically handling this inside the Java code (as you would handle // ModuleLayer::EMPTY_LAYER, for example). // // Instead, we archive all static field of such Enum classes. At runtime, // HeapShared::initialize_enum_klass() will skip the <clinit> method and pull // the static fields out of the archived heap. void HeapShared::check_enum_obj(int level,
KlassSubGraphInfo* subgraph_info,
oop orig_obj, bool is_closed_archive) {
Klass* k = orig_obj->klass();
Klass* buffered_k = ArchiveBuilder::get_buffered_klass(k); if (!k->is_instance_klass()) { return;
}
InstanceKlass* ik = InstanceKlass::cast(k); if (ik->java_super() == vmClasses::Enum_klass() && !ik->has_archived_enum_objs()) {
ResourceMark rm;
ik->set_has_archived_enum_objs();
buffered_k->set_has_archived_enum_objs();
oop mirror = ik->java_mirror();
for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { if (fs.access_flags().is_static()) {
fieldDescriptor& fd = fs.field_descriptor(); if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) {
guarantee(false, "static field %s::%s must be T_OBJECT or T_ARRAY",
ik->external_name(), fd.name()->as_C_string());
}
oop oop_field = mirror->obj_field(fd.offset()); if (oop_field == NULL) {
guarantee(false, "static field %s::%s must not be null",
ik->external_name(), fd.name()->as_C_string());
} elseif (oop_field->klass() != ik && oop_field->klass() != ik->array_klass_or_null()) {
guarantee(false, "static field %s::%s is of the wrong type",
ik->external_name(), fd.name()->as_C_string());
}
oop archived_oop_field = archive_reachable_objects_from(level, subgraph_info, oop_field, is_closed_archive); int root_index = append_root(archived_oop_field);
log_info(cds, heap)("Archived enum obj @%d %s::%s (" INTPTR_FORMAT " -> " INTPTR_FORMAT ")",
root_index, ik->external_name(), fd.name()->as_C_string(),
p2i((oopDesc*)oop_field), p2i((oopDesc*)archived_oop_field));
SystemDictionaryShared::add_enum_klass_static_field(ik, root_index);
}
}
}
}
// See comments in HeapShared::check_enum_obj() bool HeapShared::initialize_enum_klass(InstanceKlass* k, TRAPS) { if (!ArchiveHeapLoader::is_fully_available()) { returnfalse;
}
RunTimeClassInfo* info = RunTimeClassInfo::get_for(k);
assert(info != NULL, "sanity");
oop mirror = k->java_mirror(); int i = 0; for (JavaFieldStream fs(k); !fs.done(); fs.next()) { if (fs.access_flags().is_static()) { int root_index = info->enum_klass_static_field_root_index_at(i++);
fieldDescriptor& fd = fs.field_descriptor();
assert(fd.field_type() == T_OBJECT || fd.field_type() == T_ARRAY, "must be");
mirror->obj_field_put(fd.offset(), get_root(root_index, /*clear=*/true));
}
} returntrue;
}
void HeapShared::run_full_gc_in_vm_thread() { if (HeapShared::can_write()) { // Avoid fragmentation while archiving heap objects. // We do this inside a safepoint, so that no further allocation can happen after GC // has finished. if (GCLocker::is_active()) { // Just checking for safety ... // This should not happen during -Xshare:dump. If you see this, probably the Java core lib // has been modified such that JNI code is executed in some clean up threads after // we have finished class loading.
log_warning(cds)("GC locker is held, unable to start extra compacting GC. This may produce suboptimal results.");
} else {
log_info(cds)("Run GC ...");
Universe::heap()->collect_as_vm_thread(GCCause::_archive_time_gc);
log_info(cds)("Run GC done");
}
}
}
// Copy _pending_archive_roots into an objArray void HeapShared::copy_roots() { // HeapShared::roots() points into an ObjArray in the open archive region. A portion of the // objects in this array are discovered during HeapShared::archive_objects(). For example, // in HeapShared::archive_reachable_objects_from() -> HeapShared::check_enum_obj(). // However, HeapShared::archive_objects() happens inside a safepoint, so we can't // allocate a "regular" ObjArray and pass the result to HeapShared::archive_object(). // Instead, we have to roll our own alloc/copy routine here. int length = _pending_roots != NULL ? _pending_roots->length() : 0;
size_t size = objArrayOopDesc::object_size(length);
Klass* k = Universe::objectArrayKlassObj(); // already relocated to point to archived klass
HeapWord* mem = G1CollectedHeap::heap()->archive_mem_allocate(size);
memset(mem, 0, size * BytesPerWord);
{ // This is copied from MemAllocator::finish
oopDesc::set_mark(mem, markWord::prototype());
oopDesc::release_set_klass(mem, k);
}
{ // This is copied from ObjArrayAllocator::initialize
arrayOopDesc::set_length(mem, length);
}
_roots = OopHandle(Universe::vm_global(), cast_to_oop(mem)); for (int i = 0; i < length; i++) {
roots()->obj_at_put(i, _pending_roots->at(i));
}
log_info(cds)("archived obj roots[%d] = " SIZE_FORMAT " words, klass = %p, obj = %p", length, size, k, mem);
}
// Get the subgraph_info for Klass k. A new subgraph_info is created if // there is no existing one for k. The subgraph_info records the "buffered" // address of the class.
KlassSubGraphInfo* HeapShared::init_subgraph_info(Klass* k, bool is_full_module_graph) {
assert(DumpSharedSpaces, "dump time only"); bool created;
Klass* buffered_k = ArchiveBuilder::get_buffered_klass(k);
KlassSubGraphInfo* info =
_dump_time_subgraph_info_table->put_if_absent(k, KlassSubGraphInfo(buffered_k, is_full_module_graph),
&created);
assert(created, "must not initialize twice"); return info;
}
KlassSubGraphInfo* HeapShared::get_subgraph_info(Klass* k) {
assert(DumpSharedSpaces, "dump time only");
KlassSubGraphInfo* info = _dump_time_subgraph_info_table->get(k);
assert(info != NULL, "must have been initialized"); return info;
}
// Add an entry field to the current KlassSubGraphInfo. void KlassSubGraphInfo::add_subgraph_entry_field( int static_field_offset, oop v, bool is_closed_archive) {
assert(DumpSharedSpaces, "dump time only"); if (_subgraph_entry_fields == NULL) {
_subgraph_entry_fields = new (mtClass) GrowableArray<int>(10, mtClass);
}
_subgraph_entry_fields->append(static_field_offset);
_subgraph_entry_fields->append(HeapShared::append_root(v));
}
// Add the Klass* for an object in the current KlassSubGraphInfo's subgraphs. // Only objects of boot classes can be included in sub-graph. void KlassSubGraphInfo::add_subgraph_object_klass(Klass* orig_k) {
assert(DumpSharedSpaces, "dump time only");
Klass* buffered_k = ArchiveBuilder::get_buffered_klass(orig_k);
if (_subgraph_object_klasses == NULL) {
_subgraph_object_klasses = new (mtClass) GrowableArray<Klass*>(50, mtClass);
}
assert(ArchiveBuilder::current()->is_in_buffer_space(buffered_k), "must be a shared class");
if (_k == buffered_k) { // Don't add the Klass containing the sub-graph to it's own klass // initialization list. return;
}
if (buffered_k->is_instance_klass()) {
assert(InstanceKlass::cast(buffered_k)->is_shared_boot_class(), "must be boot class"); // vmClasses::xxx_klass() are not updated, need to check // the original Klass* if (orig_k == vmClasses::String_klass() ||
orig_k == vmClasses::Object_klass()) { // Initialized early during VM initialization. No need to be added // to the sub-graph object class list. return;
}
check_allowed_klass(InstanceKlass::cast(orig_k));
} elseif (buffered_k->is_objArray_klass()) {
Klass* abk = ObjArrayKlass::cast(buffered_k)->bottom_klass(); if (abk->is_instance_klass()) {
assert(InstanceKlass::cast(abk)->is_shared_boot_class(), "must be boot class");
check_allowed_klass(InstanceKlass::cast(ObjArrayKlass::cast(orig_k)->bottom_klass()));
} if (buffered_k == Universe::objectArrayKlassObj()) { // Initialized early during Universe::genesis. No need to be added // to the list. return;
}
} else {
assert(buffered_k->is_typeArray_klass(), "must be"); // Primitive type arrays are created early during Universe::genesis. return;
}
if (log_is_enabled(Debug, cds, heap)) { if (!_subgraph_object_klasses->contains(buffered_k)) {
ResourceMark rm;
log_debug(cds, heap)("Adding klass %s", orig_k->external_name());
}
}
void KlassSubGraphInfo::check_allowed_klass(InstanceKlass* ik) { if (ik->module()->name() == vmSymbols::java_base()) {
assert(ik->package() != NULL, "classes in java.base cannot be in unnamed package"); return;
}
#ifndef PRODUCT if (!ik->module()->is_named() && ik->package() == NULL) { // This class is loaded by ArchiveHeapTestClass return;
} constchar* extra_msg = ", or in an unnamed package of an unnamed module"; #else constchar* extra_msg = ""; #endif
ResourceMark rm;
log_error(cds, heap)("Class %s not allowed in archive heap. Must be in java.base%s",
ik->external_name(), extra_msg);
os::_exit(1);
}
bool KlassSubGraphInfo::is_non_early_klass(Klass* k) { if (k->is_objArray_klass()) {
k = ObjArrayKlass::cast(k)->bottom_klass();
} if (k->is_instance_klass()) { if (!SystemDictionaryShared::is_early_klass(InstanceKlass::cast(k))) {
ResourceMark rm;
log_info(cds, heap)("non-early: %s", k->external_name()); returntrue;
} else { returnfalse;
}
} else { returnfalse;
}
}
// Initialize an archived subgraph_info_record from the given KlassSubGraphInfo. void ArchivedKlassSubGraphInfoRecord::init(KlassSubGraphInfo* info) {
_k = info->klass();
_entry_field_records = NULL;
_subgraph_object_klasses = NULL;
_is_full_module_graph = info->is_full_module_graph();
if (_is_full_module_graph) { // Consider all classes referenced by the full module graph as early -- we will be // allocating objects of these classes during JVMTI early phase, so they cannot // be processed by (non-early) JVMTI ClassFileLoadHook
_has_non_early_klasses = false;
} else {
_has_non_early_klasses = info->has_non_early_klasses();
}
if (_has_non_early_klasses) {
ResourceMark rm;
log_info(cds, heap)( "Subgraph of klass %s has non-early klasses and cannot be used when JVMTI ClassFileLoadHook is enabled",
_k->external_name());
}
// populate the entry fields
GrowableArray<int>* entry_fields = info->subgraph_entry_fields(); if (entry_fields != NULL) { int num_entry_fields = entry_fields->length();
assert(num_entry_fields % 2 == 0, "sanity");
_entry_field_records =
ArchiveBuilder::new_ro_array<int>(num_entry_fields); for (int i = 0 ; i < num_entry_fields; i++) {
_entry_field_records->at_put(i, entry_fields->at(i));
}
}
// the Klasses of the objects in the sub-graphs
GrowableArray<Klass*>* subgraph_object_klasses = info->subgraph_object_klasses(); if (subgraph_object_klasses != NULL) { int num_subgraphs_klasses = subgraph_object_klasses->length();
_subgraph_object_klasses =
ArchiveBuilder::new_ro_array<Klass*>(num_subgraphs_klasses); for (int i = 0; i < num_subgraphs_klasses; i++) {
Klass* subgraph_k = subgraph_object_klasses->at(i); if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm;
log_info(cds, heap)( "Archived object klass %s (%2d) => %s",
_k->external_name(), i, subgraph_k->external_name());
}
_subgraph_object_klasses->at_put(i, subgraph_k);
ArchivePtrMarker::mark_pointer(_subgraph_object_klasses->adr_at(i));
}
}
// Build the records of archived subgraph infos, which include: // - Entry points to all subgraphs from the containing class mirror. The entry // points are static fields in the mirror. For each entry point, the field // offset, value and is_closed_archive flag are recorded in the sub-graph // info. The value is stored back to the corresponding field at runtime. // - A list of klasses that need to be loaded/initialized before archived // java object sub-graph can be accessed at runtime. void HeapShared::write_subgraph_info_table() { // Allocate the contents of the hashtable(s) inside the RO region of the CDS archive.
DumpTimeKlassSubGraphInfoTable* d_table = _dump_time_subgraph_info_table;
CompactHashtableStats stats;
if (soc->reading()) {
soc->do_oop(&roots_oop); // read from archive
assert(oopDesc::is_oop_or_null(roots_oop), "is oop"); // Create an OopHandle only if we have actually mapped or loaded the roots if (roots_oop != NULL) {
assert(ArchiveHeapLoader::is_fully_available(), "must be");
_roots = OopHandle(Universe::vm_global(), roots_oop);
}
} else { // writing
roots_oop = roots();
soc->do_oop(&roots_oop); // write to archive
}
}
if (VerifyArchivedFields > 1 && is_init_completed()) { // At this time, the oop->klass() of some archived objects in the heap may not // have been loaded into the system dictionary yet. Nevertheless, oop->klass() should // have enough information (object size, oop maps, etc) so that a GC can be safely // performed. // // -XX:VerifyArchivedFields=2 force a GC to happen in such an early stage // to check for GC safety.
log_info(cds, heap)("Trigger GC %s initializing static field(s) in %s",
which, k->external_name());
FlagSetting fs1(VerifyBeforeGC, true);
FlagSetting fs2(VerifyDuringGC, true);
FlagSetting fs3(VerifyAfterGC, true);
Universe::heap()->collect(GCCause::_java_lang_system_gc);
}
}
}
// Before GC can execute, we must ensure that all oops reachable from HeapShared::roots() // have a valid klass. I.e., oopDesc::klass() must have already been resolved. // // Note: if a ArchivedKlassSubGraphInfoRecord contains non-early classes, and JVMTI // ClassFileLoadHook is enabled, it's possible for this class to be dynamically replaced. In // this case, we will not load the ArchivedKlassSubGraphInfoRecord and will clear its roots. void HeapShared::resolve_classes(JavaThread* current) {
assert(UseSharedSpaces, "runtime only!"); if (!ArchiveHeapLoader::is_fully_available()) { return; // nothing to do
}
resolve_classes_for_subgraphs(current, closed_archive_subgraph_entry_fields);
resolve_classes_for_subgraphs(current, open_archive_subgraph_entry_fields);
resolve_classes_for_subgraphs(current, fmg_open_archive_subgraph_entry_fields);
}
void HeapShared::resolve_classes_for_subgraphs(JavaThread* current, ArchivableStaticFieldInfo fields[]) { for (int i = 0; fields[i].valid(); i++) {
ArchivableStaticFieldInfo* info = &fields[i];
TempNewSymbol klass_name = SymbolTable::new_symbol(info->klass_name);
InstanceKlass* k = SystemDictionaryShared::find_builtin_class(klass_name);
assert(k != NULL && k->is_shared_boot_class(), "sanity");
resolve_classes_for_subgraph_of(current, k);
}
}
void HeapShared::initialize_from_archived_subgraph(JavaThread* current, Klass* k) {
JavaThread* THREAD = current; if (!ArchiveHeapLoader::is_fully_available()) { return; // nothing to do
}
ExceptionMark em(THREAD); const ArchivedKlassSubGraphInfoRecord* record =
resolve_or_init_classes_for_subgraph_of(k, /*do_init=*/true, THREAD);
if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION; // None of the field value will be set if there was an exception when initializing the classes. // The java code will not see any of the archived objects in the // subgraphs referenced from k in this case. return;
}
if (record != NULL) {
init_archived_fields_for(k, record);
}
}
const ArchivedKlassSubGraphInfoRecord*
HeapShared::resolve_or_init_classes_for_subgraph_of(Klass* k, bool do_init, TRAPS) {
assert(!DumpSharedSpaces, "Should not be called with DumpSharedSpaces");
if (!k->is_shared()) { return NULL;
} unsignedint hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k); const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0);
// Initialize from archived data. Currently this is done only // during VM initialization time. No lock is needed. if (record != NULL) { if (record->is_full_module_graph() && !MetaspaceShared::use_full_module_graph()) { if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm(THREAD);
log_info(cds, heap)("subgraph %s cannot be used because full module graph is disabled",
k->external_name());
} return NULL;
}
if (record->has_non_early_klasses() && JvmtiExport::should_post_class_file_load_hook()) { if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm(THREAD);
log_info(cds, heap)("subgraph %s cannot be used because JVMTI ClassFileLoadHook is enabled",
k->external_name());
} return NULL;
}
// Load/link/initialize the klasses of the objects in the subgraph. // NULL class loader is used.
Array<Klass*>* klasses = record->subgraph_object_klasses(); if (klasses != NULL) { for (int i = 0; i < klasses->length(); i++) {
Klass* klass = klasses->at(i); if (!klass->is_shared()) { return NULL;
}
resolve_or_init(klass, do_init, CHECK_NULL);
}
}
}
return record;
}
void HeapShared::resolve_or_init(Klass* k, bool do_init, TRAPS) { if (!do_init) { if (k->class_loader_data() == NULL) {
Klass* resolved_k = SystemDictionary::resolve_or_null(k->name(), CHECK);
assert(resolved_k == k, "classes used by archived heap must not be replaced by JVMTI ClassFileLoadHook");
}
} else {
assert(k->class_loader_data() != NULL, "must have been resolved by HeapShared::resolve_classes"); if (k->is_instance_klass()) {
InstanceKlass* ik = InstanceKlass::cast(k);
ik->initialize(CHECK);
} elseif (k->is_objArray_klass()) {
ObjArrayKlass* oak = ObjArrayKlass::cast(k);
oak->initialize(CHECK);
}
}
}
// Load the subgraph entry fields from the record and store them back to // the corresponding fields within the mirror.
oop m = k->java_mirror();
Array<int>* entry_field_records = record->entry_field_records(); if (entry_field_records != NULL) { int efr_len = entry_field_records->length();
assert(efr_len % 2 == 0, "sanity"); for (int i = 0; i < efr_len; i += 2) { int field_offset = entry_field_records->at(i); int root_index = entry_field_records->at(i+1);
oop v = get_root(root_index, /*clear=*/true);
m->obj_field_put(field_offset, v);
log_debug(cds, heap)(" " PTR_FORMAT " init field @ %2d = " PTR_FORMAT, p2i(k), field_offset, p2i(v));
}
// Done. Java code can see the archived sub-graphs referenced from k's // mirror after this point. if (log_is_enabled(Info, cds, heap)) {
ResourceMark rm;
log_info(cds, heap)("initialize_from_archived_subgraph %s " PTR_FORMAT "%s",
k->external_name(), p2i(k), JvmtiExport::is_early_phase() ? " (early)" : "");
}
}
verify_the_heap(k, "after ");
}
void HeapShared::clear_archived_roots_of(Klass* k) { unsignedint hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(k); const ArchivedKlassSubGraphInfoRecord* record = _run_time_subgraph_info_table.lookup(k, hash, 0); if (record != NULL) {
Array<int>* entry_field_records = record->entry_field_records(); if (entry_field_records != NULL) { int efr_len = entry_field_records->length();
assert(efr_len % 2 == 0, "sanity"); for (int i = 0; i < efr_len; i += 2) { int root_index = entry_field_records->at(i+1);
clear_root(root_index);
}
}
}
}
class WalkOopAndArchiveClosure: public BasicOopIterateClosure { int _level; bool _is_closed_archive; bool _record_klasses_only;
KlassSubGraphInfo* _subgraph_info;
oop _orig_referencing_obj;
oop _archived_referencing_obj;
protected: template <class T> void do_oop_work(T *p) {
oop obj = RawAccess<>::oop_load(p); if (!CompressedOops::is_null(obj)) {
assert(!HeapShared::is_archived_object_during_dumptime(obj), "original objects must not point to archived objects");
void HeapShared::check_closed_region_object(InstanceKlass* k) { // Check fields in the object for (JavaFieldStream fs(k); !fs.done(); fs.next()) { if (!fs.access_flags().is_static()) {
BasicType ft = fs.field_descriptor().field_type(); if (!fs.access_flags().is_final() && is_reference_type(ft)) {
ResourceMark rm;
log_warning(cds, heap)( "Please check reference field in %s instance in closed archive heap region: %s %s",
k->external_name(), (fs.name())->as_C_string(),
(fs.signature())->as_C_string());
}
}
}
}
void HeapShared::check_module_oop(oop orig_module_obj) {
assert(DumpSharedSpaces, "must be");
assert(java_lang_Module::is_instance(orig_module_obj), "must be");
ModuleEntry* orig_module_ent = java_lang_Module::module_entry_raw(orig_module_obj); if (orig_module_ent == NULL) { // These special Module objects are created in Java code. They are not // defined via Modules::define_module(), so they don't have a ModuleEntry: // java.lang.Module::ALL_UNNAMED_MODULE // java.lang.Module::EVERYONE_MODULE // jdk.internal.loader.ClassLoaders$BootClassLoader::unnamedModule
assert(java_lang_Module::name(orig_module_obj) == NULL, "must be unnamed");
log_info(cds, heap)("Module oop with No ModuleEntry* @[" PTR_FORMAT "]", p2i(orig_module_obj));
} else {
ClassLoaderData* loader_data = orig_module_ent->loader_data();
assert(loader_data->is_builtin_class_loader_data(), "must be");
}
}
// (1) If orig_obj has not been archived yet, archive it. // (2) If orig_obj has not been seen yet (since start_recording_subgraph() was called), // trace all objects that are reachable from it, and make sure these objects are archived. // (3) Record the klasses of all orig_obj and all reachable objects.
oop HeapShared::archive_reachable_objects_from(int level,
KlassSubGraphInfo* subgraph_info,
oop orig_obj, bool is_closed_archive) {
assert(orig_obj != NULL, "must be");
assert(!is_archived_object_during_dumptime(orig_obj), "sanity");
if (!JavaClasses::is_supported_for_archiving(orig_obj)) { // This object has injected fields that cannot be supported easily, so we disallow them for now. // If you get an error here, you probably made a change in the JDK library that has added // these objects that are referenced (directly or indirectly) by static fields.
ResourceMark rm;
log_error(cds, heap)("Cannot archive object of class %s", orig_obj->klass()->external_name());
os::_exit(1);
}
// java.lang.Class instances cannot be included in an archived object sub-graph. We only support // them as Klass::_archived_mirror because they need to be specially restored at run time. // // If you get an error here, you probably made a change in the JDK library that has added a Class // object that is referenced (directly or indirectly) by static fields. if (java_lang_Class::is_instance(orig_obj)) {
log_error(cds, heap)("(%d) Unknown java.lang.Class object is in the archived sub-graph", level);
os::_exit(1);
}
oop archived_obj = find_archived_heap_object(orig_obj); if (java_lang_String::is_instance(orig_obj) && archived_obj != NULL) { // To save time, don't walk strings that are already archived. They just contain // pointers to a type array, whose klass doesn't need to be recorded. return archived_obj;
}
if (has_been_seen_during_subgraph_recording(orig_obj)) { // orig_obj has already been archived and traced. Nothing more to do. return archived_obj;
} else {
set_has_been_seen_during_subgraph_recording(orig_obj);
}
bool record_klasses_only = (archived_obj != NULL); if (archived_obj == NULL) {
++_num_new_archived_objs;
archived_obj = archive_object(orig_obj); if (archived_obj == NULL) { // Skip archiving the sub-graph referenced from the current entry field.
ResourceMark rm;
log_error(cds, heap)( "Cannot archive the sub-graph referenced from %s object ("
PTR_FORMAT ") size " SIZE_FORMAT ", skipped.",
orig_obj->klass()->external_name(), p2i(orig_obj), orig_obj->size() * HeapWordSize); if (level == 1) { // Don't archive a subgraph root that's too big. For archives static fields, that's OK // as the Java code will take care of initializing this field dynamically. return NULL;
} else { // We don't know how to handle an object that has been archived, but some of its reachable // objects cannot be archived. Bail out for now. We might need to fix this in the future if // we have a real use case.
os::_exit(1);
}
}
if (java_lang_Module::is_instance(orig_obj)) {
check_module_oop(orig_obj);
java_lang_Module::set_module_entry(archived_obj, NULL);
java_lang_Module::set_loader(archived_obj, NULL);
} elseif (java_lang_ClassLoader::is_instance(orig_obj)) { // class_data will be restored explicitly at run time.
guarantee(orig_obj == SystemDictionary::java_platform_loader() ||
orig_obj == SystemDictionary::java_system_loader() ||
java_lang_ClassLoader::loader_data(orig_obj) == NULL, "must be");
java_lang_ClassLoader::release_set_loader_data(archived_obj, NULL);
}
}
assert(archived_obj != NULL, "must be");
Klass *orig_k = orig_obj->klass();
subgraph_info->add_subgraph_object_klass(orig_k);
// // Start from the given static field in a java mirror and archive the // complete sub-graph of java heap objects that are reached directly // or indirectly from the starting object by following references. // Sub-graph archiving restrictions (current): // // - All classes of objects in the archived sub-graph (including the // entry class) must be boot class only. // - No java.lang.Class instance (java mirror) can be included inside // an archived sub-graph. Mirror can only be the sub-graph entry object. // // The Java heap object sub-graph archiving process (see // WalkOopAndArchiveClosure): // // 1) Java object sub-graph archiving starts from a given static field // within a Class instance (java mirror). If the static field is a // reference field and points to a non-null java object, proceed to // the next step. // // 2) Archives the referenced java object. If an archived copy of the // current object already exists, updates the pointer in the archived // copy of the referencing object to point to the current archived object. // Otherwise, proceed to the next step. // // 3) Follows all references within the current java object and recursively // archive the sub-graph of objects starting from each reference. // // 4) Updates the pointer in the archived copy of referencing object to // point to the current archived object. // // 5) The Klass of the current java object is added to the list of Klasses // for loading and initializing before any object in the archived graph can // be accessed at runtime. // void HeapShared::archive_reachable_objects_from_static_field(InstanceKlass *k, constchar* klass_name, int field_offset, constchar* field_name, bool is_closed_archive) {
assert(DumpSharedSpaces, "dump time only");
assert(k->is_shared_boot_class(), "must be boot class");
oop m = k->java_mirror();
KlassSubGraphInfo* subgraph_info = get_subgraph_info(k);
oop f = m->obj_field(field_offset);
if (!CompressedOops::is_null(f)) { if (log_is_enabled(Trace, cds, heap)) {
LogTarget(Trace, cds, heap) log;
LogStream out(log);
f->print_on(&out);
}
oop af = archive_reachable_objects_from(1, subgraph_info, f, is_closed_archive);
if (af == NULL) {
log_error(cds, heap)("Archiving failed %s::%s (some reachable objects cannot be archived)",
klass_name, field_name);
} else { // Note: the field value is not preserved in the archived mirror. // Record the field as a new subGraph entry point. The recorded // information is restored from the archive at runtime.
subgraph_info->add_subgraph_entry_field(field_offset, af, is_closed_archive);
log_info(cds, heap)("Archived field %s::%s => " PTR_FORMAT, klass_name, field_name, p2i(af));
}
} else { // The field contains null, we still need to record the entry point, // so it can be restored at runtime.
subgraph_info->add_subgraph_entry_field(field_offset, NULL, false);
}
}
#ifndef PRODUCT class VerifySharedOopClosure: public BasicOopIterateClosure { private: bool _is_archived;
void HeapShared::verify_subgraph_from_static_field(InstanceKlass* k, int field_offset) {
assert(DumpSharedSpaces, "dump time only");
assert(k->is_shared_boot_class(), "must be boot class");
oop m = k->java_mirror();
oop f = m->obj_field(field_offset); if (!CompressedOops::is_null(f)) {
verify_subgraph_from(f);
}
}
void HeapShared::verify_subgraph_from(oop orig_obj) {
oop archived_obj = find_archived_heap_object(orig_obj); if (archived_obj == NULL) { // It's OK for the root of a subgraph to be not archived. See comments in // archive_reachable_objects_from(). return;
}
// Verify that all objects reachable from orig_obj are archived.
init_seen_objects_table();
verify_reachable_objects_from(orig_obj, false);
delete_seen_objects_table();
// Note: we could also verify that all objects reachable from the archived // copy of orig_obj can only point to archived objects, with: // init_seen_objects_table(); // verify_reachable_objects_from(archived_obj, true); // init_seen_objects_table(); // but that's already done in G1HeapVerifier::verify_archive_regions so we // won't do it here.
}
HeapShared::SeenObjectsTable* HeapShared::_seen_objects_table = NULL; int HeapShared::_num_new_walked_objs; int HeapShared::_num_new_archived_objs; int HeapShared::_num_old_recorded_klasses;
int HeapShared::_num_total_subgraph_recordings = 0; int HeapShared::_num_total_walked_objs = 0; int HeapShared::_num_total_archived_objs = 0; int HeapShared::_num_total_recorded_klasses = 0; int HeapShared::_num_total_verifications = 0;
if (is_test_class) {
log_warning(cds)("Loading ArchiveHeapTestClass %s ...", ArchiveHeapTestClass);
}
Klass* k = SystemDictionary::resolve_or_fail(klass_name, true, THREAD); if (HAS_PENDING_EXCEPTION) {
CLEAR_PENDING_EXCEPTION;
stringStream st;
st.print("Fail to initialize archive heap: %s cannot be loaded by the boot loader", info->klass_name);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
}
if (!k->is_instance_klass()) {
stringStream st;
st.print("Fail to initialize archive heap: %s is not an instance class", info->klass_name);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
}
InstanceKlass* ik = InstanceKlass::cast(k);
assert(InstanceKlass::cast(ik)->is_shared_boot_class(), "Only support boot classes");
if (is_test_class) { if (ik->module()->is_named()) { // We don't want ArchiveHeapTestClass to be abused to easily load/initialize arbitrary // core-lib classes. You need to at least append to the bootclasspath.
stringStream st;
st.print("ArchiveHeapTestClass %s is not in unnamed module", ArchiveHeapTestClass);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
}
if (ik->package() != NULL) { // This restriction makes HeapShared::is_a_test_class_in_unnamed_module() easy.
stringStream st;
st.print("ArchiveHeapTestClass %s is not in unnamed package", ArchiveHeapTestClass);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
}
} else { if (ik->module()->name() != vmSymbols::java_base()) { // We don't want to deal with cases when a module is unavailable at runtime. // FUTURE -- load from archived heap only when module graph has not changed // between dump and runtime.
stringStream st;
st.print("%s is not in java.base module", info->klass_name);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
}
}
if (is_test_class) {
log_warning(cds)("Initializing ArchiveHeapTestClass %s ...", ArchiveHeapTestClass);
}
ik->initialize(CHECK);
ArchivableStaticFieldFinder finder(ik, field_name);
ik->do_local_static_fields(&finder); if (!finder.found()) {
stringStream st;
st.print("Unable to find the static T_OBJECT field %s::%s", info->klass_name, info->field_name);
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), st.as_string());
}
#ifndef PRODUCT void HeapShared::setup_test_class(constchar* test_class_name) {
ArchivableStaticFieldInfo* p = open_archive_subgraph_entry_fields; int num_slots = sizeof(open_archive_subgraph_entry_fields) / sizeof(ArchivableStaticFieldInfo);
assert(p[num_slots - 2].klass_name == NULL, "must have empty slot that's patched below");
assert(p[num_slots - 1].klass_name == NULL, "must have empty slot that marks the end of the list");
// See if ik is one of the test classes that are pulled in by -XX:ArchiveHeapTestClass // during runtime. This may be called before the module system is initialized so // we cannot rely on InstanceKlass::module(), etc. bool HeapShared::is_a_test_class_in_unnamed_module(Klass* ik) { if (_test_class != NULL) { if (ik == _test_class) { returntrue;
}
Array<Klass*>* klasses = _test_class_record->subgraph_object_klasses(); if (klasses == NULL) { returnfalse;
}
for (int i = 0; i < klasses->length(); i++) {
Klass* k = klasses->at(i); if (k == ik) {
Symbol* name; if (k->is_instance_klass()) {
name = InstanceKlass::cast(k)->name();
} elseif (k->is_objArray_klass()) {
Klass* bk = ObjArrayKlass::cast(k)->bottom_klass(); if (!bk->is_instance_klass()) { returnfalse;
}
name = bk->name();
} else { returnfalse;
}
// See KlassSubGraphInfo::check_allowed_klass() - only two types of // classes are allowed: // (A) java.base classes (which must not be in the unnamed module) // (B) test classes which must be in the unnamed package of the unnamed module. // So if we see a '/' character in the class name, it must be in (A); // otherwise it must be in (B). if (name->index_of_at(0, "/", 1) >= 0) { returnfalse; // (A)
}
returntrue; // (B)
}
}
}
returnfalse;
} #endif
void HeapShared::init_for_dumping(TRAPS) { if (HeapShared::can_write()) {
setup_test_class(ArchiveHeapTestClass);
_dumped_interned_strings = new (mtClass)DumpedInternedStrings();
--> --------------------
--> maximum size reached
--> --------------------
¤ Dauer der Verarbeitung: 0.24 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.