1 /*
  2  * Copyright (c) 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/aotClassLinker.hpp"
 26 #include "cds/aotArtifactFinder.hpp"
 27 #include "cds/aotClassInitializer.hpp"
 28 #include "cds/dumpTimeClassInfo.inline.hpp"
 29 #include "cds/heapShared.hpp"
 30 #include "cds/lambdaProxyClassDictionary.hpp"
 31 #include "classfile/systemDictionaryShared.hpp"
 32 #include "logging/log.hpp"
 33 #include "memory/metaspaceClosure.hpp"
 34 #include "oops/instanceKlass.hpp"
 35 #include "oops/objArrayKlass.hpp"
 36 #include "oops/trainingData.hpp"
 37 #include "utilities/resourceHash.hpp"
 38 
 39 // All the classes that should be included in the AOT cache (in at least the "allocated" state)
 40 static GrowableArrayCHeap<Klass*, mtClassShared>* _all_cached_classes = nullptr;
 41 
 42 // This is a stack that tracks all the AOT-inited classes that are waiting to be passed
 43 // to HeapShared::copy_and_rescan_aot_inited_mirror().
 44 static GrowableArrayCHeap<InstanceKlass*, mtClassShared>* _pending_aot_inited_classes = nullptr;
 45 
 46 static const int TABLE_SIZE = 15889; // prime number
 47 using ClassesTable = ResourceHashtable<Klass*, bool, TABLE_SIZE, AnyObj::C_HEAP, mtClassShared>;
 48 static ClassesTable* _seen_classes;       // all classes that have been seen by AOTArtifactFinder
 49 static ClassesTable* _aot_inited_classes; // all classes that need to be AOT-initialized.
 50 
 51 void AOTArtifactFinder::initialize() {
 52   _all_cached_classes = new GrowableArrayCHeap<Klass*, mtClassShared>();
 53   _pending_aot_inited_classes = new GrowableArrayCHeap<InstanceKlass*, mtClassShared>();
 54   _seen_classes = new (mtClass)ClassesTable();
 55   _aot_inited_classes = new (mtClass)ClassesTable();
 56 }
 57 
 58 void AOTArtifactFinder::dispose() {
 59   delete _all_cached_classes;
 60   delete _seen_classes;
 61   delete _aot_inited_classes;
 62   delete _pending_aot_inited_classes;
 63   _all_cached_classes = nullptr;
 64   _seen_classes = nullptr;
 65   _aot_inited_classes = nullptr;
 66   _pending_aot_inited_classes = nullptr;
 67 }
 68 
 69 // Find all Klasses and oops that should be included in the AOT cache. See aotArtifactFinder.hpp
 70 void AOTArtifactFinder::find_artifacts() {
 71   // Some classes might have been marked as excluded as a side effect of running
 72   // AOTConstantPoolResolver. Make sure we check all the remaining ones.
 73   //
 74   // Note, if a class is not excluded, it does NOT mean it will be automatically included
 75   // into the AOT cache -- that will be decided by the code below.
 76   SystemDictionaryShared::finish_exclusion_checks();
 77 
 78   start_scanning_for_oops();
 79 
 80   // Add the primitive array classes
 81   for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 82     BasicType bt = (BasicType)i;
 83     if (is_java_primitive(bt)) {
 84       add_cached_type_array_class(Universe::typeArrayKlass(bt));
 85     }
 86   }
 87 
 88 #if INCLUDE_CDS_JAVA_HEAP
 89   // Add the mirrors that aren't associated with a Klass
 90   //    - primitive mirrors (E.g., "int.class" in Java code)
 91   //    - mirror of fillerArrayKlass
 92   if (CDSConfig::is_dumping_heap()) {
 93     for (int i = T_BOOLEAN; i < T_VOID+1; i++) {
 94       BasicType bt = (BasicType)i;
 95       if (!is_reference_type(bt)) {
 96         oop orig_mirror = Universe::java_mirror(bt);
 97         oop scratch_mirror = HeapShared::scratch_java_mirror(bt);
 98         HeapShared::scan_java_mirror(orig_mirror);
 99         log_trace(cds, heap, mirror)(
100             "Archived %s mirror object from " PTR_FORMAT,
101             type2name(bt), p2i(scratch_mirror));
102         Universe::set_archived_basic_type_mirror_index(bt, HeapShared::append_root(scratch_mirror));
103       }
104     }
105 
106     // Universe::fillerArrayKlass() isn't in the class hierarchy, so handle it specially.
107     HeapShared::scan_java_mirror(Universe::fillerArrayKlass()->java_mirror());
108   }
109 #endif
110 
111   // Add all the InstanceKlasses (and their array classes) that are always included.
112   SystemDictionaryShared::dumptime_table()->iterate_all_live_classes([&] (InstanceKlass* ik, DumpTimeClassInfo& info) {
113     // Skip "AOT tooling classes" in this block. They will be included in the AOT cache only if
114     // - One of their subtypes is included
115     // - One of their instances is found by HeapShared.
116     if (!info.is_excluded() && !info.is_aot_tooling_class()) {
117       bool add = false;
118       if (!ik->is_hidden()) {
119         // All non-hidden classes are always included into the AOT cache
120         add = true;
121       } else {
122         if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) {
123           // Legacy support of lambda proxies -- these are always included into the AOT cache
124           if (LambdaProxyClassDictionary::is_registered_lambda_proxy_class(ik)) {
125             add = true;
126           }
127         } else {
128           assert(!LambdaProxyClassDictionary::is_registered_lambda_proxy_class(ik),
129                  "registered lambda proxies are only for legacy lambda proxy support");
130         }
131       }
132 
133       if (add) {
134         add_cached_instance_class(ik);
135         if (AOTClassInitializer::can_archive_initialized_mirror(ik)) {
136           add_aot_inited_class(ik);
137         }
138       }
139     }
140   });
141 
142 #if INCLUDE_CDS_JAVA_HEAP
143   // Keep scanning until we discover no more class that need to be AOT-initialized.
144   if (CDSConfig::is_initing_classes_at_dump_time()) {
145     while (_pending_aot_inited_classes->length() > 0) {
146       InstanceKlass* ik = _pending_aot_inited_classes->pop();
147       HeapShared::copy_and_rescan_aot_inited_mirror(ik);
148     }
149   }
150 #endif
151 
152   // Exclude all the (hidden) classes that have not been discovered by the code above.
153   SystemDictionaryShared::dumptime_table()->iterate_all_live_classes([&] (InstanceKlass* k, DumpTimeClassInfo& info) {
154     if (!info.is_excluded() && _seen_classes->get(k) == nullptr) {
155       info.set_excluded();
156       info.set_has_checked_exclusion();
157       if (log_is_enabled(Debug, cds)) {
158         ResourceMark rm;
159         log_debug(cds)("Skipping %s: %s class", k->name()->as_C_string(),
160                       k->is_hidden() ? "Unreferenced hidden" : "AOT tooling");
161       }
162     }
163   });
164 
165   end_scanning_for_oops();
166 
167   TrainingData::cleanup_training_data();
168 }
169 
170 void AOTArtifactFinder::start_scanning_for_oops() {
171 #if INCLUDE_CDS_JAVA_HEAP
172   if (CDSConfig::is_dumping_heap()) {
173     HeapShared::start_scanning_for_oops();
174   }
175 #endif
176 }
177 
178 void AOTArtifactFinder::end_scanning_for_oops() {
179 #if INCLUDE_CDS_JAVA_HEAP
180   if (CDSConfig::is_dumping_heap()) {
181     HeapShared::end_scanning_for_oops();
182   }
183 #endif
184 }
185 
186 void AOTArtifactFinder::add_aot_inited_class(InstanceKlass* ik) {
187   if (CDSConfig::is_initing_classes_at_dump_time()) {
188     assert(ik->is_initialized(), "must be");
189     add_cached_instance_class(ik);
190 
191     bool created;
192     _aot_inited_classes->put_if_absent(ik, &created);
193     if (created) {
194       _pending_aot_inited_classes->push(ik);
195 
196       InstanceKlass* s = ik->java_super();
197       if (s != nullptr) {
198         add_aot_inited_class(s);
199       }
200 
201       Array<InstanceKlass*>* interfaces = ik->local_interfaces();
202       int len = interfaces->length();
203       for (int i = 0; i < len; i++) {
204         InstanceKlass* intf = interfaces->at(i);
205         if (intf->is_initialized()) {
206           add_aot_inited_class(intf);
207         }
208       }
209     }
210   }
211 }
212 
213 void AOTArtifactFinder::add_cached_instance_class(InstanceKlass* ik) {
214   bool created;
215   _seen_classes->put_if_absent(ik, &created);
216   if (created) {
217     _all_cached_classes->append(ik);
218     if (CDSConfig::is_dumping_final_static_archive() && ik->is_shared_unregistered_class()) {
219       // The following are not appliable to unregistered classes
220       return;
221     }
222     scan_oops_in_instance_class(ik);
223     if (ik->is_hidden() && CDSConfig::is_initing_classes_at_dump_time()) {
224       bool succeed = AOTClassLinker::try_add_candidate(ik);
225       guarantee(succeed, "All cached hidden classes must be aot-linkable");
226       add_aot_inited_class(ik);
227     }
228   }
229 }
230 
231 void AOTArtifactFinder::add_cached_type_array_class(TypeArrayKlass* tak) {
232   bool created;
233   _seen_classes->put_if_absent(tak, &created);
234   if (created) {
235     _all_cached_classes->append(tak);
236     scan_oops_in_array_class(tak);
237   }
238 }
239 
240 void AOTArtifactFinder::add_cached_class(Klass* k) {
241   if (k->is_typeArray_klass()) {
242     add_cached_type_array_class(TypeArrayKlass::cast(k));
243   } else if (k->is_objArray_klass()) {
244     add_cached_class(ObjArrayKlass::cast(k)->element_klass());
245   } else {
246     add_cached_instance_class(InstanceKlass::cast(k));
247   }
248 }
249 
250 void AOTArtifactFinder::scan_oops_in_instance_class(InstanceKlass* ik) {
251 #if INCLUDE_CDS_JAVA_HEAP
252   if (CDSConfig::is_dumping_heap()) {
253     HeapShared::scan_java_class(ik);
254     scan_oops_in_array_class(ik->array_klasses());
255   }
256 #endif
257 }
258 
259 void AOTArtifactFinder::scan_oops_in_array_class(ArrayKlass* ak) {
260 #if INCLUDE_CDS_JAVA_HEAP
261   if (CDSConfig::is_dumping_heap()) {
262     while (ak != nullptr) {
263       HeapShared::scan_java_class(ak);
264       ak = ak->array_klass_or_null();
265     }
266   }
267 #endif
268 }
269 
270 void AOTArtifactFinder::all_cached_classes_do(MetaspaceClosure* it) {
271   for (int i = 0; i < _all_cached_classes->length(); i++) {
272     it->push(_all_cached_classes->adr_at(i));
273   }
274 }