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