1 /*
  2  * Copyright (c) 2024, 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/aotClassInitializer.hpp"
 26 #include "cds/aotClassLinker.hpp"
 27 #include "cds/aotLinkedClassBulkLoader.hpp"
 28 #include "cds/aotLinkedClassTable.hpp"
 29 #include "cds/cdsConfig.hpp"
 30 #include "cds/heapShared.hpp"
 31 #include "classfile/classLoaderData.hpp"
 32 #include "classfile/classLoaderDataShared.hpp"
 33 #include "classfile/javaClasses.hpp"
 34 #include "classfile/systemDictionary.hpp"
 35 #include "classfile/systemDictionaryShared.hpp"
 36 #include "classfile/vmClasses.hpp"
 37 #include "compiler/compilationPolicy.hpp"
 38 #include "gc/shared/gcVMOperations.hpp"
 39 #include "memory/resourceArea.hpp"
 40 #include "oops/instanceKlass.hpp"
 41 #include "oops/klass.inline.hpp"
 42 #include "oops/trainingData.hpp"
 43 #include "runtime/handles.inline.hpp"
 44 #include "runtime/java.hpp"
 45 #include "runtime/serviceThread.hpp"
 46 #include "utilities/growableArray.hpp"
 47 
 48 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc) {
 49   AOTLinkedClassTable::get()->serialize(soc);
 50 }
 51 
 52 // This function is called before the VM executes any Java code (include AOT-compiled Java methods).
 53 //
 54 // We populate the boot/platform/app class loaders with classes from the AOT cache. This is a fundamental
 55 // step in restoring the JVM's state from the snapshot recorded in the AOT cache: other AOT optimizations
 56 // such as AOT compiled methods can make direct references to the preloaded classes, knowing that
 57 // these classes are guaranteed to be in at least the "loaded" state.
 58 //
 59 // Note: we can't link the classes yet because SharedRuntime is not yet ready to generate adapters.
 60 void AOTLinkedClassBulkLoader::preload_classes(JavaThread* current) {
 61   preload_classes_impl(current);
 62   if (current->has_pending_exception()) {
 63     exit_on_exception(current);
 64   }
 65 }
 66 
 67 void AOTLinkedClassBulkLoader::preload_classes_impl(TRAPS) {
 68   precond(CDSConfig::is_using_aot_linked_classes());
 69 
 70   ClassLoaderDataShared::restore_archived_modules_for_preloading_classes(THREAD);
 71   Handle h_platform_loader(THREAD, SystemDictionary::java_platform_loader());
 72   Handle h_system_loader(THREAD, SystemDictionary::java_system_loader());
 73 
 74   AOTLinkedClassTable* table = AOTLinkedClassTable::get();
 75 
 76   preload_classes_in_table(table->boot1(), "boot1", Handle(), CHECK);
 77   preload_classes_in_table(table->boot2(), "boot2", Handle(), CHECK);
 78 
 79   initiate_loading(THREAD, "plat", h_platform_loader, table->boot1());
 80   initiate_loading(THREAD, "plat", h_platform_loader, table->boot2());
 81   preload_classes_in_table(table->platform(), "plat", h_platform_loader, CHECK);
 82 
 83   initiate_loading(THREAD, "app", h_system_loader, table->boot1());
 84   initiate_loading(THREAD, "app", h_system_loader, table->boot2());
 85   initiate_loading(THREAD, "app", h_system_loader, table->platform());
 86   preload_classes_in_table(table->app(), "app", h_system_loader, CHECK);
 87 }
 88 
 89 void AOTLinkedClassBulkLoader::preload_classes_in_table(Array<InstanceKlass*>* classes,
 90                                                         const char* category_name, Handle loader, TRAPS) {
 91   if (classes == nullptr) {
 92     return;
 93   }
 94 
 95   for (int i = 0; i < classes->length(); i++) {
 96     InstanceKlass* ik = classes->at(i);
 97     if (log_is_enabled(Info, aot, load)) {
 98       ResourceMark rm(THREAD);
 99       log_info(aot, load)("%-5s %s%s", category_name, ik->external_name(),
100                           ik->is_hidden() ? " (hidden)" : "");
101     }
102 
103     SystemDictionary::preload_class(loader, ik, CHECK);
104 
105     if (ik->is_hidden()) {
106       DEBUG_ONLY({
107         // Make sure we don't make this hidden class available by name, even if we don't
108         // use any special ClassLoaderData.
109         ResourceMark rm(THREAD);
110         assert(SystemDictionary::find_instance_klass(THREAD, ik->name(), loader) == nullptr,
111                "hidden classes cannot be accessible by name: %s", ik->external_name());
112       });
113     } else {
114       precond(SystemDictionary::find_instance_klass(THREAD, ik->name(), loader) == ik);
115     }
116   }
117 }
118 
119 // Some cached heap objects may hold references to methods in aot-linked
120 // classes (via MemberName). We need to make sure all classes are
121 // linked before executing any bytecode.
122 void AOTLinkedClassBulkLoader::link_classes(JavaThread* current) {
123   link_classes_impl(current);
124   if (current->has_pending_exception()) {
125     exit_on_exception(current);
126   }
127 }
128 
129 void AOTLinkedClassBulkLoader::link_classes_impl(TRAPS) {
130   precond(CDSConfig::is_using_aot_linked_classes());
131 
132   AOTLinkedClassTable* table = AOTLinkedClassTable::get();
133 
134   link_classes_in_table(table->boot1(), CHECK);
135   link_classes_in_table(table->boot2(), CHECK);
136   link_classes_in_table(table->platform(), CHECK);
137   link_classes_in_table(table->app(), CHECK);
138 }
139 
140 void AOTLinkedClassBulkLoader::link_classes_in_table(Array<InstanceKlass*>* classes, TRAPS) {
141   if (classes != nullptr) {
142     for (int i = 0; i < classes->length(); i++) {
143       // NOTE: CDSConfig::is_preserving_verification_constraints() is required
144       // when storing ik in the AOT cache. This means we don't have to verify
145       // ik at all.
146       //
147       // Without is_preserving_verification_constraints(), ik->link_class() may cause
148       // class loading, which may result in invocation of ClassLoader::loadClass() calls,
149       // which CANNOT happen because we are not ready to execute any Java byecodes yet
150       // at this point.
151       InstanceKlass* ik = classes->at(i);
152       ik->link_class(CHECK);
153     }
154   }
155 }
156 
157 #ifdef ASSERT
158 void AOTLinkedClassBulkLoader::validate_module_of_preloaded_classes() {
159   oop javabase_module_oop = ModuleEntryTable::javabase_moduleEntry()->module_oop();
160   for (int i = T_BOOLEAN; i < T_LONG+1; i++) {
161     TypeArrayKlass* tak = Universe::typeArrayKlass((BasicType)i);
162     validate_module(tak, "boot1", javabase_module_oop);
163   }
164 
165   JavaThread* current = JavaThread::current();
166   Handle h_platform_loader(current, SystemDictionary::java_platform_loader());
167   Handle h_system_loader(current, SystemDictionary::java_system_loader());
168   AOTLinkedClassTable* table = AOTLinkedClassTable::get();
169 
170   validate_module_of_preloaded_classes_in_table(table->boot1(), "boot1", Handle());
171   validate_module_of_preloaded_classes_in_table(table->boot2(), "boot2", Handle());
172   validate_module_of_preloaded_classes_in_table(table->platform(), "plat", h_platform_loader);
173   validate_module_of_preloaded_classes_in_table(table->app(), "app", h_system_loader);
174 }
175 
176 void AOTLinkedClassBulkLoader::validate_module_of_preloaded_classes_in_table(Array<InstanceKlass*>* classes,
177                                                                              const char* category_name, Handle loader) {
178   if (classes == nullptr) {
179     return;
180   }
181 
182   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
183   for (int i = 0; i < classes->length(); i++) {
184     InstanceKlass* ik = classes->at(i);
185     PackageEntry* pkg_entry = ik->package();
186     oop module_oop;
187     if (pkg_entry == nullptr) {
188       module_oop = loader_data->unnamed_module()->module_oop();
189     } else {
190       module_oop = pkg_entry->module()->module_oop();
191     }
192 
193     validate_module(ik, category_name, module_oop);
194   }
195 }
196 
197 void AOTLinkedClassBulkLoader::validate_module(Klass* k, const char* category_name, oop module_oop) {
198   assert(module_oop != nullptr, "module system must have been initialized");
199 
200   if (log_is_enabled(Debug, aot, module)) {
201     ResourceMark rm;
202     log_debug(aot, module)("Validate module of %-5s %s", category_name, k->external_name());
203   }
204   precond(java_lang_Class::module(k->java_mirror()) == module_oop);
205 
206   ArrayKlass* ak = k->array_klass_or_null();
207   while (ak != nullptr) {
208     if (log_is_enabled(Debug, aot, module)) {
209       ResourceMark rm;
210       log_debug(aot, module)("Validate module of %-5s %s", category_name, ak->external_name());
211     }
212     precond(java_lang_Class::module(ak->java_mirror()) == module_oop);
213     ak = ak->array_klass_or_null();
214   }
215 }
216 #endif
217 
218 void AOTLinkedClassBulkLoader::init_javabase_classes(JavaThread* current) {
219   init_classes_for_loader(Handle(), AOTLinkedClassTable::get()->boot1(), current);
220   if (current->has_pending_exception()) {
221     exit_on_exception(current);
222   }
223 }
224 
225 void AOTLinkedClassBulkLoader::init_non_javabase_classes(JavaThread* current) {
226   init_non_javabase_classes_impl(current);
227   if (current->has_pending_exception()) {
228     exit_on_exception(current);
229   }
230 }
231 
232 void AOTLinkedClassBulkLoader::init_non_javabase_classes_impl(TRAPS) {
233   assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
234 
235   DEBUG_ONLY(validate_module_of_preloaded_classes());
236 
237   // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
238   // the platform/system class loader should already have been initialized as part
239   // of the FMG support.
240   assert(CDSConfig::is_using_full_module_graph(), "must be");
241 
242   Handle h_platform_loader(THREAD, SystemDictionary::java_platform_loader());
243   Handle h_system_loader(THREAD, SystemDictionary::java_system_loader());
244 
245   assert(h_platform_loader() != nullptr, "must be");
246   assert(h_system_loader() != nullptr,   "must be");
247 
248   AOTLinkedClassTable* table = AOTLinkedClassTable::get();
249   init_classes_for_loader(Handle(), table->boot2(), CHECK);
250   init_classes_for_loader(h_platform_loader, table->platform(), CHECK);
251   init_classes_for_loader(h_system_loader, table->app(), CHECK);
252 
253   if (Universe::is_fully_initialized() && VerifyDuringStartup) {
254     // Make sure we're still in a clean state.
255     VM_Verify verify_op;
256     VMThread::execute(&verify_op);
257   }
258 
259   if (AOTPrintTrainingInfo) {
260     tty->print_cr("==================== archived_training_data ** after all classes preloaded ====================");
261     TrainingData::print_archived_training_data_on(tty);
262   }
263 }
264 
265 // For the AOT cache to function properly, all classes in the AOTLinkedClassTable
266 // must be loaded and linked. In addition, AOT-initialized classes must be moved to
267 // the initialized state.
268 //
269 // We can encounter a failure during the loading, linking, or initialization of
270 // classes in the AOTLinkedClassTable only if:
271 //   - We ran out of memory,
272 //   - There is a serious error in the VM implemenation
273 // When this happens, the VM may be in an inconsistent state (e.g., we have a cached
274 // heap object of class X, but X is not linked). We must exit the JVM now.
275 
276 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
277   assert(current->has_pending_exception(), "precondition");
278   ResourceMark rm(current);
279   if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
280     log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
281                    "%zuM", MaxHeapSize/M);
282   } else {
283     oop message = java_lang_Throwable::message(current->pending_exception());
284     log_error(aot)("%s: %s", current->pending_exception()->klass()->external_name(),
285                    message == nullptr ? "(no message)" : java_lang_String::as_utf8_string(message));
286   }
287   vm_exit_during_initialization("Unexpected exception when loading aot-linked classes.");
288 }
289 
290 // Initiate loading of the <classes> in the <initiating_loader>. The <classes> should have already been loaded
291 // by a parent loader of the <initiating_loader>. This is necessary for handling pre-resolved CP entries.
292 //
293 // For example, we initiate the loading of java/lang/String in the AppClassLoader. This will allow
294 // any App classes to have a pre-resolved ConstantPool entry that references java/lang/String.
295 //
296 // TODO: we can limit the number of initiated classes to only those that are actually referenced by
297 // AOT-linked classes loaded by <initiating_loader>.
298 void AOTLinkedClassBulkLoader::initiate_loading(JavaThread* current, const char* category_name,
299                                                 Handle initiating_loader, Array<InstanceKlass*>* classes) {
300   if (classes == nullptr) {
301     return;
302   }
303 
304   assert(initiating_loader() == SystemDictionary::java_platform_loader() ||
305          initiating_loader() == SystemDictionary::java_system_loader(), "must be");
306   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(initiating_loader());
307   MonitorLocker mu1(SystemDictionary_lock);
308 
309   for (int i = 0; i < classes->length(); i++) {
310     InstanceKlass* ik = classes->at(i);
311     assert(ik->is_loaded(), "must have already been loaded by a parent loader");
312     assert(ik->class_loader() != initiating_loader(), "must be a parent loader");
313     assert(ik->class_loader() == nullptr ||
314            ik->class_loader() == SystemDictionary::java_platform_loader(), "must be");
315     if (ik->is_public() && !ik->is_hidden()) {
316       if (log_is_enabled(Info, aot, load)) {
317         ResourceMark rm(current);
318         const char* defining_loader = (ik->class_loader() == nullptr ? "boot" : "plat");
319         log_info(aot, load)("%-5s %s (initiated, defined by %s)", category_name, ik->external_name(),
320                             defining_loader);
321       }
322       SystemDictionary::add_to_initiating_loader(current, ik, loader_data);
323     }
324   }
325 }
326 
327 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
328 // - classes that were AOT-initialized by AOTClassInitializer
329 // - the classes of all objects that are reachable from the archived mirrors of
330 //   the AOT-linked classes for <class_loader>.
331 void AOTLinkedClassBulkLoader::init_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
332   if (classes != nullptr) {
333     for (int i = 0; i < classes->length(); i++) {
334       InstanceKlass* ik = classes->at(i);
335       assert(ik->class_loader_data() != nullptr, "must be");
336       if (ik->has_aot_initialized_mirror()) {
337         ik->initialize_with_aot_initialized_mirror(CHECK);
338       }
339     }
340   }
341 
342   HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
343 }
344 
345 void AOTLinkedClassBulkLoader::replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) {
346   if (classes != nullptr) {
347     for (int i = 0; i < classes->length(); i++) {
348       InstanceKlass* ik = classes->at(i);
349       if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
350         CompilationPolicy::replay_training_at_init(ik, CHECK);
351       }
352     }
353   }
354 }
355 
356 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
357   if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
358     AOTLinkedClassTable* table = AOTLinkedClassTable::get();
359     replay_training_at_init(table->boot1(),    CHECK);
360     replay_training_at_init(table->boot2(),    CHECK);
361     replay_training_at_init(table->platform(), CHECK);
362     replay_training_at_init(table->app(),      CHECK);
363   }
364 }