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/vmClasses.hpp" 29 #include "classfile/systemDictionaryShared.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 // -- Handling of Enum objects 44 // Java Enum classes have synthetic <clinit> methods that look like this 45 // enum MyEnum {FOO, BAR} 46 // MyEnum::<clinint> { 47 // /*static final MyEnum*/ MyEnum::FOO = new MyEnum("FOO"); 48 // /*static final MyEnum*/ MyEnum::BAR = new MyEnum("BAR"); 49 // } 50 // 51 // If MyEnum::FOO object is referenced by any of the archived subgraphs, we must 52 // ensure the archived value equals (in object address) to the runtime value of 53 // MyEnum::FOO. 54 // 55 // However, since MyEnum::<clinint> is synthetically generated by javac, there's 56 // no way of programmatically handling this inside the Java code (as you would handle 57 // ModuleLayer::EMPTY_LAYER, for example). 58 // 59 // Instead, we archive all static field of such Enum classes. At runtime, 60 // HeapShared::initialize_enum_klass() skips the <clinit> method and instead pulls 61 // the static fields out of the archived heap. 62 void CDSEnumKlass::handle_enum_obj(int level, 63 KlassSubGraphInfo* subgraph_info, 64 oop orig_obj) { 65 assert(level > 1, "must never be called at the first (outermost) level"); 66 assert(is_enum_obj(orig_obj), "must be"); 67 68 InstanceKlass* ik = InstanceKlass::cast(orig_obj->klass()); 69 if (ik->has_archived_enum_objs()) { 70 return; 71 } 72 73 ik->set_has_archived_enum_objs(); 74 75 oop mirror = ik->java_mirror(); 76 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) { 77 if (fs.access_flags().is_static()) { 78 archive_static_field(level, subgraph_info, ik, mirror, fs); 79 } 80 } 81 } 82 83 void CDSEnumKlass::archive_static_field(int level, KlassSubGraphInfo* subgraph_info, 84 InstanceKlass* ik, oop mirror, JavaFieldStream& fs) { 85 ResourceMark rm; 86 fieldDescriptor& fd = fs.field_descriptor(); 87 if (fd.field_type() != T_OBJECT && fd.field_type() != T_ARRAY) { 88 guarantee(false, "static field %s::%s must be T_OBJECT or T_ARRAY", 89 ik->external_name(), fd.name()->as_C_string()); 90 } 91 oop oop_field = mirror->obj_field(fd.offset()); 92 // There should be no oops for ObjArrayKlass but InstanceKlass::array_klasses holds a list of ObjArrayKlass, 93 // therefore we need the super of the refined array klass. 94 Klass* oop_field_klass = oop_field->is_refined_objArray() ? oop_field->klass()->super() : oop_field->klass(); 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