8312125: Refactor CDS enum class handling
Reviewed-by: matsaave, ccheung
This commit is contained in:
parent
bb1f8a1698
commit
9c7a6eabb9
src/hotspot/share
136
src/hotspot/share/cds/cdsEnumKlass.cpp
Normal file
136
src/hotspot/share/cds/cdsEnumKlass.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2023, 2024, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "precompiled.hpp"
|
||||
#include "cds/archiveHeapLoader.hpp"
|
||||
#include "cds/cdsEnumKlass.hpp"
|
||||
#include "cds/heapShared.hpp"
|
||||
#include "classfile/vmClasses.hpp"
|
||||
#include "classfile/systemDictionaryShared.hpp"
|
||||
#include "memory/resourceArea.hpp"
|
||||
#include "oops/fieldStreams.inline.hpp"
|
||||
#include "oops/oop.inline.hpp"
|
||||
#include "runtime/fieldDescriptor.inline.hpp"
|
||||
|
||||
#if INCLUDE_CDS_JAVA_HEAP
|
||||
|
||||
bool CDSEnumKlass::is_enum_obj(oop orig_obj) {
|
||||
Klass* k = orig_obj->klass();
|
||||
Klass* buffered_k = ArchiveBuilder::get_buffered_klass(k);
|
||||
return k->is_instance_klass() &&
|
||||
InstanceKlass::cast(k)->java_super() == vmClasses::Enum_klass();
|
||||
}
|
||||
|
||||
// -- 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() skips the <clinit> method and instead pulls
|
||||
// the static fields out of the archived heap.
|
||||
void CDSEnumKlass::handle_enum_obj(int level,
|
||||
KlassSubGraphInfo* subgraph_info,
|
||||
oop orig_obj) {
|
||||
assert(level > 1, "must never be called at the first (outermost) level");
|
||||
assert(is_enum_obj(orig_obj), "must be");
|
||||
|
||||
InstanceKlass* ik = InstanceKlass::cast(orig_obj->klass());
|
||||
if (ik->has_archived_enum_objs()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ik->set_has_archived_enum_objs();
|
||||
ArchiveBuilder::get_buffered_klass(ik)->set_has_archived_enum_objs();
|
||||
|
||||
oop mirror = ik->java_mirror();
|
||||
for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
|
||||
if (fs.access_flags().is_static()) {
|
||||
archive_static_field(level, subgraph_info, ik, mirror, fs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CDSEnumKlass::archive_static_field(int level, KlassSubGraphInfo* subgraph_info,
|
||||
InstanceKlass* ik, oop mirror, JavaFieldStream& fs) {
|
||||
ResourceMark rm;
|
||||
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 == nullptr) {
|
||||
guarantee(false, "static field %s::%s must not be null",
|
||||
ik->external_name(), fd.name()->as_C_string());
|
||||
} else if (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());
|
||||
}
|
||||
bool success = HeapShared::archive_reachable_objects_from(level, subgraph_info, oop_field);
|
||||
assert(success, "VM should have exited with unarchivable objects for _level > 1");
|
||||
int root_index = HeapShared::append_root(oop_field);
|
||||
log_info(cds, heap)("Archived enum obj @%d %s::%s (" INTPTR_FORMAT ")",
|
||||
root_index, ik->external_name(), fd.name()->as_C_string(),
|
||||
p2i((oopDesc*)oop_field));
|
||||
SystemDictionaryShared::add_enum_klass_static_field(ik, root_index);
|
||||
}
|
||||
|
||||
bool CDSEnumKlass::initialize_enum_klass(InstanceKlass* k, TRAPS) {
|
||||
if (!ArchiveHeapLoader::is_in_use()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RunTimeClassInfo* info = RunTimeClassInfo::get_for(k);
|
||||
assert(info != nullptr, "sanity");
|
||||
|
||||
if (log_is_enabled(Info, cds, heap)) {
|
||||
ResourceMark rm;
|
||||
log_info(cds, heap)("Initializing Enum class: %s", k->external_name());
|
||||
}
|
||||
|
||||
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(), HeapShared::get_root(root_index, /*clear=*/true));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif // INCLUDE_CDS_JAVA_HEAP
|
50
src/hotspot/share/cds/cdsEnumKlass.hpp
Normal file
50
src/hotspot/share/cds/cdsEnumKlass.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2023, 2024, 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef SHARE_CDS_CDSENUMKLASS_HPP
|
||||
#define SHARE_CDS_CDSENUMKLASS_HPP
|
||||
|
||||
#include "memory/allStatic.hpp"
|
||||
#include "oops/oop.hpp"
|
||||
#include "utilities/exceptions.hpp"
|
||||
#include "utilities/macros.hpp"
|
||||
|
||||
class InstanceKlass;
|
||||
class JavaFieldStream;
|
||||
class KlassSubGraphInfo;
|
||||
|
||||
class CDSEnumKlass: AllStatic {
|
||||
public:
|
||||
static bool is_enum_obj(oop orig_obj);
|
||||
static void handle_enum_obj(int level,
|
||||
KlassSubGraphInfo* subgraph_info,
|
||||
oop orig_obj);
|
||||
static bool initialize_enum_klass(InstanceKlass* k, TRAPS) NOT_CDS_JAVA_HEAP_RETURN_(false);
|
||||
|
||||
private:
|
||||
static void archive_static_field(int level, KlassSubGraphInfo* subgraph_info,
|
||||
InstanceKlass* ik, oop mirror, JavaFieldStream& fs);
|
||||
};
|
||||
|
||||
#endif // SHARE_CDS_CDSENUMKLASS_HPP
|
@ -28,6 +28,7 @@
|
||||
#include "cds/archiveHeapWriter.hpp"
|
||||
#include "cds/archiveUtils.hpp"
|
||||
#include "cds/cdsConfig.hpp"
|
||||
#include "cds/cdsEnumKlass.hpp"
|
||||
#include "cds/cdsHeapVerifier.hpp"
|
||||
#include "cds/heapShared.hpp"
|
||||
#include "cds/metaspaceShared.hpp"
|
||||
@ -451,95 +452,6 @@ void HeapShared::set_has_native_pointers(oop src_obj) {
|
||||
info->set_has_native_pointers();
|
||||
}
|
||||
|
||||
// -- 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) {
|
||||
assert(level > 1, "must never be called at the first (outermost) level");
|
||||
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 == nullptr) {
|
||||
guarantee(false, "static field %s::%s must not be null",
|
||||
ik->external_name(), fd.name()->as_C_string());
|
||||
} else if (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());
|
||||
}
|
||||
bool success = archive_reachable_objects_from(level, subgraph_info, oop_field);
|
||||
assert(success, "VM should have exited with unarchivable objects for _level > 1");
|
||||
int root_index = append_root(oop_field);
|
||||
log_info(cds, heap)("Archived enum obj @%d %s::%s (" INTPTR_FORMAT ")",
|
||||
root_index, ik->external_name(), fd.name()->as_C_string(),
|
||||
p2i((oopDesc*)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_in_use()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
RunTimeClassInfo* info = RunTimeClassInfo::get_for(k);
|
||||
assert(info != nullptr, "sanity");
|
||||
|
||||
if (log_is_enabled(Info, cds, heap)) {
|
||||
ResourceMark rm;
|
||||
log_info(cds, heap)("Initializing Enum class: %s", k->external_name());
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void HeapShared::archive_objects(ArchiveHeapInfo *heap_info) {
|
||||
{
|
||||
NoSafepointVerifier nsv;
|
||||
@ -1241,7 +1153,9 @@ bool HeapShared::archive_reachable_objects_from(int level,
|
||||
WalkOopAndArchiveClosure walker(level, record_klasses_only, subgraph_info, orig_obj);
|
||||
orig_obj->oop_iterate(&walker);
|
||||
|
||||
check_enum_obj(level + 1, subgraph_info, orig_obj);
|
||||
if (CDSEnumKlass::is_enum_obj(orig_obj)) {
|
||||
CDSEnumKlass::handle_enum_obj(level + 1, subgraph_info, orig_obj);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -212,9 +212,6 @@ public:
|
||||
};
|
||||
|
||||
private:
|
||||
static void check_enum_obj(int level, KlassSubGraphInfo* subgraph_info,
|
||||
oop orig_obj);
|
||||
|
||||
static const int INITIAL_TABLE_SIZE = 15889; // prime number
|
||||
static const int MAX_TABLE_SIZE = 1000000;
|
||||
typedef ResizeableResourceHashtable<oop, CachedOopInfo,
|
||||
@ -428,7 +425,6 @@ private:
|
||||
static void write_subgraph_info_table() NOT_CDS_JAVA_HEAP_RETURN;
|
||||
static void init_roots(oop roots_oop) NOT_CDS_JAVA_HEAP_RETURN;
|
||||
static void serialize_tables(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
|
||||
static bool initialize_enum_klass(InstanceKlass* k, TRAPS) NOT_CDS_JAVA_HEAP_RETURN_(false);
|
||||
|
||||
static bool is_a_test_class_in_unnamed_module(Klass* ik) NOT_CDS_JAVA_HEAP_RETURN_(false);
|
||||
};
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include "precompiled.hpp"
|
||||
#include "cds/archiveUtils.hpp"
|
||||
#include "cds/cdsConfig.hpp"
|
||||
#include "cds/cdsEnumKlass.hpp"
|
||||
#include "cds/classListWriter.hpp"
|
||||
#include "cds/heapShared.hpp"
|
||||
#include "cds/metaspaceShared.hpp"
|
||||
@ -1576,7 +1577,7 @@ void InstanceKlass::call_class_initializer(TRAPS) {
|
||||
// This is needed to ensure the consistency of the archived heap objects.
|
||||
if (has_archived_enum_objs()) {
|
||||
assert(is_shared(), "must be");
|
||||
bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
|
||||
bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user