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