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 "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     // Skip "AOT tooling classes" in this block. They will be included in the AOT cache only if
113     // - One of their subtypes is included
114     // - One of their instances is found by HeapShared.
115     if (!info.is_excluded() && !info.is_aot_tooling_class()) {
116       bool add = false;
117       if (!ik->is_hidden()) {
118         // All non-hidden classes are always included into the AOT cache
119         add = true;
120       } else {
121         if (CDSConfig::is_dumping_lambdas_in_legacy_mode()) {
122           // Legacy support of lambda proxies -- these are always included into the AOT cache
123           if (LambdaProxyClassDictionary::is_registered_lambda_proxy_class(ik)) {
124             add = true;
125           }
126         } else {
127           assert(!LambdaProxyClassDictionary::is_registered_lambda_proxy_class(ik),
128                  "registered lambda proxies are only for legacy lambda proxy support");
129         }
130       }
131 
132       if (add) {
133         add_cached_instance_class(ik);
134         if (AOTClassInitializer::can_archive_initialized_mirror(ik)) {
135           add_aot_inited_class(ik);
136         }
137       }
138     }
139   });
140 
141 #if INCLUDE_CDS_JAVA_HEAP
142   // Keep scanning until we discover no more class that need to be AOT-initialized.
143   if (CDSConfig::is_initing_classes_at_dump_time()) {
144     while (_pending_aot_inited_classes->length() > 0) {
145       InstanceKlass* ik = _pending_aot_inited_classes->pop();
146       HeapShared::copy_and_rescan_aot_inited_mirror(ik);
147     }
148   }
149 #endif
150 
151   // Exclude all the (hidden) classes that have not been discovered by the code above.
152   SystemDictionaryShared::dumptime_table()->iterate_all_live_classes([&] (InstanceKlass* k, DumpTimeClassInfo& info) {
153     if (!info.is_excluded() && _seen_classes->get(k) == nullptr) {
154       info.set_excluded();
155       info.set_has_checked_exclusion();
156       if (log_is_enabled(Debug, cds)) {
157         ResourceMark rm;
158         log_debug(cds)("Skipping %s: %s class", k->name()->as_C_string(),
159                       k->is_hidden() ? "Unreferenced hidden" : "AOT tooling");
160       }
161     }
162   });
163 
164   end_scanning_for_oops();
165 }
166 
167 void AOTArtifactFinder::start_scanning_for_oops() {
168 #if INCLUDE_CDS_JAVA_HEAP
169   if (CDSConfig::is_dumping_heap()) {
170     HeapShared::start_scanning_for_oops();
171   }
172 #endif
173 }
174 
175 void AOTArtifactFinder::end_scanning_for_oops() {
176 #if INCLUDE_CDS_JAVA_HEAP
177   if (CDSConfig::is_dumping_heap()) {
178     HeapShared::end_scanning_for_oops();
179   }
180 #endif
181 }
182 
183 void AOTArtifactFinder::add_aot_inited_class(InstanceKlass* ik) {
184   if (CDSConfig::is_initing_classes_at_dump_time()) {
185     assert(ik->is_initialized(), "must be");
186     add_cached_instance_class(ik);
187 
188     bool created;
189     _aot_inited_classes->put_if_absent(ik, &created);
190     if (created) {
191       _pending_aot_inited_classes->push(ik);
192 
193       InstanceKlass* s = ik->java_super();
194       if (s != nullptr) {
195         add_aot_inited_class(s);
196       }
197 
198       Array<InstanceKlass*>* interfaces = ik->local_interfaces();
199       int len = interfaces->length();
200       for (int i = 0; i < len; i++) {
201         InstanceKlass* intf = interfaces->at(i);
202         if (intf->is_initialized()) {
203           add_aot_inited_class(intf);
204         }
205       }
206     }
207   }
208 }
209 
210 void AOTArtifactFinder::add_cached_instance_class(InstanceKlass* ik) {
211   bool created;
212   _seen_classes->put_if_absent(ik, &created);
213   if (created) {
214     _all_cached_classes->append(ik);
215     if (CDSConfig::is_dumping_final_static_archive() && ik->is_shared_unregistered_class()) {
216       // The following are not appliable to unregistered classes
217       return;
218     }
219     scan_oops_in_instance_class(ik);
220     if (ik->is_hidden() && CDSConfig::is_initing_classes_at_dump_time()) {
221       bool succeed = AOTClassLinker::try_add_candidate(ik);
222       guarantee(succeed, "All cached hidden classes must be aot-linkable");
223       add_aot_inited_class(ik);
224     }
225   }
226 }
227 
228 void AOTArtifactFinder::add_cached_type_array_class(TypeArrayKlass* tak) {
229   bool created;
230   _seen_classes->put_if_absent(tak, &created);
231   if (created) {
232     _all_cached_classes->append(tak);
233     scan_oops_in_array_class(tak);
234   }
235 }
236 
237 void AOTArtifactFinder::add_cached_class(Klass* k) {
238   if (k->is_typeArray_klass()) {
239     add_cached_type_array_class(TypeArrayKlass::cast(k));
240   } else if (k->is_objArray_klass()) {
241     add_cached_class(ObjArrayKlass::cast(k)->element_klass());
242   } else {
243     add_cached_instance_class(InstanceKlass::cast(k));
244   }
245 }
246 
247 void AOTArtifactFinder::scan_oops_in_instance_class(InstanceKlass* ik) {
248 #if INCLUDE_CDS_JAVA_HEAP
249   if (CDSConfig::is_dumping_heap()) {
250     HeapShared::scan_java_class(ik);
251     scan_oops_in_array_class(ik->array_klasses());
252   }
253 #endif
254 }
255 
256 void AOTArtifactFinder::scan_oops_in_array_class(ArrayKlass* ak) {
257 #if INCLUDE_CDS_JAVA_HEAP
258   if (CDSConfig::is_dumping_heap()) {
259     while (ak != nullptr) {
260       HeapShared::scan_java_class(ak);
261       ak = ak->array_klass_or_null();
262     }
263   }
264 #endif
265 }
266 
267 void AOTArtifactFinder::all_cached_classes_do(MetaspaceClosure* it) {
268   for (int i = 0; i < _all_cached_classes->length(); i++) {
269     it->push(_all_cached_classes->adr_at(i));
270   }
271 }