1 /*
2 * Copyright (c) 2023, 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/archiveHeapLoader.hpp"
26 #include "cds/cdsEnumKlass.hpp"
27 #include "cds/heapShared.hpp"
28 #include "classfile/systemDictionaryShared.hpp"
29 #include "classfile/vmClasses.hpp"
30 #include "memory/resourceArea.hpp"
31 #include "oops/fieldStreams.inline.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/fieldDescriptor.inline.hpp"
34
35 #if INCLUDE_CDS_JAVA_HEAP
36
37 bool CDSEnumKlass::is_enum_obj(oop orig_obj) {
38 Klass* k = orig_obj->klass();
39 return k->is_instance_klass() &&
40 InstanceKlass::cast(k)->is_enum_subclass();
41 }
42
43 // !!! This is legacy support for enum classes before JEP 483. This file is not used when
44 // !!! CDSConfig::is_initing_classes_at_dump_time()==true.
45 //
46 // Java Enum classes have synthetic <clinit> methods that look like this
47 // enum MyEnum {FOO, BAR}
48 // MyEnum::<clinint> {
49 // /*static final MyEnum*/ MyEnum::FOO = new MyEnum("FOO");
50 // /*static final MyEnum*/ MyEnum::BAR = new MyEnum("BAR");
51 // }
52 //
53 // If MyEnum::FOO object is referenced by any of the archived subgraphs, we must
54 // ensure the archived value equals (in object address) to the runtime value of
55 // MyEnum::FOO.
56 //
57 // However, since MyEnum::<clinint> is synthetically generated by javac, there's
58 // no way of programmatically handling this inside the Java code (as you would handle
59 // ModuleLayer::EMPTY_LAYER, for example).
60 //
61 // Instead, we archive all static field of such Enum classes. At runtime,
62 // HeapShared::initialize_enum_klass() skips the <clinit> method and instead pulls
63 // the static fields out of the archived heap.
64 void CDSEnumKlass::handle_enum_obj(int level,
65 KlassSubGraphInfo* subgraph_info,
66 oop orig_obj) {
67 assert(!CDSConfig::is_initing_classes_at_dump_time(), "only for legacy support of enums");
68 assert(level > 1, "must never be called at the first (outermost) level");
69 assert(is_enum_obj(orig_obj), "must be");
70
71 InstanceKlass* ik = InstanceKlass::cast(orig_obj->klass());
72 if (ik->has_archived_enum_objs()) {
73 return;
74 }
75
76 ik->set_has_archived_enum_objs();
77
78 oop mirror = ik->java_mirror();
79 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
80 if (fs.access_flags().is_static()) {
81 archive_static_field(level, subgraph_info, ik, mirror, fs);
82 }
83 }
84 }
85
86 void CDSEnumKlass::archive_static_field(int level, KlassSubGraphInfo* subgraph_info,
87 InstanceKlass* ik, oop mirror, JavaFieldStream& fs) {
88 ResourceMark rm;
89 fieldDescriptor& fd = fs.field_descriptor();
90 if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) {
91 guarantee(false, "static field %s::%s must be T_OBJECT or T_ARRAY",
92 ik->external_name(), fd.name()->as_C_string());
93 }
94 oop oop_field = mirror->obj_field(fd.offset());
95 if (oop_field == nullptr) {
96 guarantee(false, "static field %s::%s must not be null",
97 ik->external_name(), fd.name()->as_C_string());
98 } else if (oop_field->klass() != ik && oop_field->klass() != ik->array_klass_or_null()) {
99 guarantee(false, "static field %s::%s is of the wrong type",
100 ik->external_name(), fd.name()->as_C_string());
101 }
102 bool success = HeapShared::archive_reachable_objects_from(level, subgraph_info, oop_field);
103 assert(success, "VM should have exited with unarchivable objects for _level > 1");
104 int root_index = HeapShared::append_root(oop_field);
105 log_info(aot, heap)("Archived enum obj @%d %s::%s (" INTPTR_FORMAT ")",
106 root_index, ik->external_name(), fd.name()->as_C_string(),
107 p2i((oopDesc*)oop_field));
108 SystemDictionaryShared::add_enum_klass_static_field(ik, root_index);
109 }
110
111 bool CDSEnumKlass::initialize_enum_klass(InstanceKlass* k, TRAPS) {
112 if (!ArchiveHeapLoader::is_in_use()) {
113 return false;
114 }
115
116 RunTimeClassInfo* info = RunTimeClassInfo::get_for(k);
117 assert(info != nullptr, "sanity");
118
119 if (log_is_enabled(Info, aot, heap)) {
120 ResourceMark rm;
121 log_info(aot, heap)("Initializing Enum class: %s", k->external_name());
122 }
123
124 oop mirror = k->java_mirror();
125 int i = 0;
126 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
127 if (fs.access_flags().is_static()) {
128 int root_index = info->enum_klass_static_field_root_index_at(i++);
129 fieldDescriptor& fd = fs.field_descriptor();
130 assert(fd.field_type() == T_OBJECT || fd.field_type() == T_ARRAY, "must be");
131 mirror->obj_field_put(fd.offset(), HeapShared::get_root(root_index, /*clear=*/true));
132 }
133 }
134 return true;
135 }
136 #endif // INCLUDE_CDS_JAVA_HEAP