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