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/aotCacheAccess.hpp"
 26 #include "cds/aotClassInitializer.hpp"
 27 #include "cds/aotClassLinker.hpp"
 28 #include "cds/aotLinkedClassBulkLoader.hpp"
 29 #include "cds/aotLinkedClassTable.hpp"
 30 #include "cds/archiveBuilder.hpp"
 31 #include "cds/archiveUtils.inline.hpp"
 32 #include "cds/cdsConfig.hpp"
 33 #include "cds/cdsProtectionDomain.hpp"
 34 #include "cds/heapShared.hpp"
 35 #include "cds/lambdaFormInvokers.inline.hpp"
 36 #include "classfile/classLoaderDataGraph.hpp"
 37 #include "classfile/classLoaderData.hpp"
 38 #include "classfile/classLoaderExt.hpp"
 39 #include "classfile/classLoader.hpp"
 40 #include "classfile/dictionary.hpp"
 41 #include "classfile/javaClasses.hpp"
 42 #include "classfile/systemDictionary.hpp"
 43 #include "classfile/systemDictionaryShared.hpp"
 44 #include "classfile/vmClasses.hpp"
 45 #include "compiler/compilationPolicy.hpp"
 46 #include "gc/shared/gcVMOperations.hpp"
 47 #include "memory/resourceArea.hpp"
 48 #include "oops/constantPool.inline.hpp"
 49 #include "oops/instanceKlass.hpp"
 50 #include "oops/klass.inline.hpp"
 51 #include "oops/trainingData.hpp"
 52 #include "runtime/handles.inline.hpp"
 53 #include "runtime/javaCalls.hpp"
 54 #include "runtime/java.hpp"
 55 #include "runtime/perfData.inline.hpp"
 56 #include "runtime/timer.hpp"
 57 #include "services/management.hpp"
 58 
 59 bool AOTLinkedClassBulkLoader::_boot2_completed = false;
 60 bool AOTLinkedClassBulkLoader::_platform_completed = false;
 61 bool AOTLinkedClassBulkLoader::_app_completed = false;
 62 bool AOTLinkedClassBulkLoader::_all_completed = false;
 63 
 64 static PerfCounter* _perf_classes_preloaded = nullptr;
 65 static PerfTickCounters* _perf_class_preload_counters = nullptr;
 66 
 67 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) {
 68   AOTLinkedClassTable::get(is_static_archive)->serialize(soc);
 69 
 70   if (is_static_archive) {
 71     if (soc->reading() && UsePerfData) {
 72       JavaThread* THREAD = JavaThread::current();
 73       NEWPERFEVENTCOUNTER(_perf_classes_preloaded, SUN_CLS, "preloadedClasses");
 74       NEWPERFTICKCOUNTERS(_perf_class_preload_counters, SUN_CLS, "classPreload");
 75     }
 76   }
 77 }
 78 
 79 bool AOTLinkedClassBulkLoader::class_preloading_finished() {
 80   if (!CDSConfig::is_using_aot_linked_classes()) {
 81     return true;
 82   } else {
 83     // The ConstantPools of preloaded classes have references to other preloaded classes. We don't
 84     // want any Java code (including JVMCI compiler) to use these classes until all of them
 85     // are loaded.
 86     return Atomic::load_acquire(&_all_completed);
 87   }
 88 }
 89 
 90 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) {
 91   assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
 92   load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes
 93 }
 94 
 95 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) {
 96   assert(CDSConfig::is_using_aot_linked_classes(), "sanity");
 97 
 98   // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result,
 99   // the platform/system class loader should already have been initialized as part
100   // of the FMG support.
101   if (!CDSConfig::is_dumping_final_static_archive()) {
102     assert(CDSConfig::is_using_full_module_graph(), "must be");
103   }
104   assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
105   assert(SystemDictionary::java_system_loader() != nullptr,   "must be");
106 
107   load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base
108   _boot2_completed = true;
109 
110   load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader());
111   _platform_completed = true;
112 
113   load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader());
114 
115   if (AOTPrintTrainingInfo) {
116     tty->print_cr("==================== archived_training_data ** after all classes preloaded ====================");
117     TrainingData::print_archived_training_data_on(tty);
118   }
119 
120   if (log_is_enabled(Info, cds, jit)) {
121     AOTCacheAccess::test_heap_access_api();
122   }
123 
124   _app_completed = true;
125   Atomic::release_store(&_all_completed, true);
126 }
127 
128 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) {
129   load_classes_in_loader_impl(class_category, class_loader_oop, current);
130   if (current->has_pending_exception()) {
131     // We cannot continue, as we might have loaded some of the aot-linked classes, which
132     // may have dangling C++ pointers to other aot-linked classes that we have failed to load.
133     exit_on_exception(current);
134   }
135 }
136 
137 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) {
138   assert(current->has_pending_exception(), "precondition");
139   ResourceMark rm(current);
140   if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) {
141     log_error(aot)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = "
142                    "%zuM", MaxHeapSize/M);
143   } else {
144     log_error(aot)("%s: %s", current->pending_exception()->klass()->external_name(),
145                    java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception())));
146   }
147   vm_exit_during_initialization("Unexpected exception when loading aot-linked classes.");
148 }
149 
150 void AOTLinkedClassBulkLoader::load_classes_in_loader_impl(AOTLinkedClassCategory class_category, oop class_loader_oop, TRAPS) {
151   Handle h_loader(THREAD, class_loader_oop);
152   load_table(AOTLinkedClassTable::for_static_archive(),  class_category, h_loader, CHECK);
153   load_table(AOTLinkedClassTable::for_dynamic_archive(), class_category, h_loader, CHECK);
154 
155   // Initialize the InstanceKlasses of all archived heap objects that are reachable from the
156   // archived java class mirrors.
157   //
158   // Only the classes in the static archive can have archived mirrors.
159   AOTLinkedClassTable* static_table = AOTLinkedClassTable::for_static_archive();
160   switch (class_category) {
161   case AOTLinkedClassCategory::BOOT1:
162     // Delayed until finish_loading_javabase_classes(), as the VM is not ready to
163     // execute some of the <clinit> methods.
164     break;
165   case AOTLinkedClassCategory::BOOT2:
166     init_required_classes_for_loader(h_loader, static_table->boot2(), CHECK);
167     break;
168   case AOTLinkedClassCategory::PLATFORM:
169     init_required_classes_for_loader(h_loader, static_table->platform(), CHECK);
170     break;
171   case AOTLinkedClassCategory::APP:
172     init_required_classes_for_loader(h_loader, static_table->app(), CHECK);
173     break;
174   case AOTLinkedClassCategory::UNREGISTERED:
175     ShouldNotReachHere();
176     break;
177   }
178 
179   if (Universe::is_fully_initialized() && VerifyDuringStartup) {
180     // Make sure we're still in a clean state.
181     VM_Verify verify_op;
182     VMThread::execute(&verify_op);
183   }
184 }
185 
186 void AOTLinkedClassBulkLoader::load_table(AOTLinkedClassTable* table, AOTLinkedClassCategory class_category, Handle loader, TRAPS) {
187   if (class_category != AOTLinkedClassCategory::BOOT1) {
188     assert(Universe::is_module_initialized(), "sanity");
189   }
190 
191   const char* category_name = AOTClassLinker::class_category_name(class_category);
192   switch (class_category) {
193   case AOTLinkedClassCategory::BOOT1:
194     load_classes_impl(class_category, table->boot(), category_name, loader, CHECK);
195     break;
196 
197   case AOTLinkedClassCategory::BOOT2:
198     load_classes_impl(class_category, table->boot2(), category_name, loader, CHECK);
199     break;
200 
201   case AOTLinkedClassCategory::PLATFORM:
202     {
203       initiate_loading(THREAD, category_name, loader, table->boot());
204       initiate_loading(THREAD, category_name, loader, table->boot2());
205       load_classes_impl(class_category, table->platform(), category_name, loader, CHECK);
206     }
207     break;
208   case AOTLinkedClassCategory::APP:
209     {
210       initiate_loading(THREAD, category_name, loader, table->boot());
211       initiate_loading(THREAD, category_name, loader, table->boot2());
212       initiate_loading(THREAD, category_name, loader, table->platform());
213       load_classes_impl(class_category, table->app(), category_name, loader, CHECK);
214     }
215     break;
216   case AOTLinkedClassCategory::UNREGISTERED:
217   default:
218     ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category.
219     break;
220   }
221 }
222 
223 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes,
224                                                  const char* category_name, Handle loader, TRAPS) {
225   if (classes == nullptr) {
226     return;
227   }
228 
229   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
230 
231   for (int i = 0; i < classes->length(); i++) {
232     if (UsePerfData) {
233       _perf_classes_preloaded->inc();
234     }
235     InstanceKlass* ik = classes->at(i);
236     if (log_is_enabled(Info, aot, load)) {
237       ResourceMark rm(THREAD);
238       log_info(aot, load)("%-5s %s%s%s", category_name, ik->external_name(),
239                           ik->is_loaded() ? " (already loaded)" : "",
240                           ik->is_hidden() ? " (hidden)" : "");
241     }
242 
243     if (!ik->is_loaded()) {
244       if (ik->is_hidden()) {
245         load_hidden_class(loader_data, ik, CHECK);
246       } else {
247         InstanceKlass* actual;
248         if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
249           actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK);
250         } else {
251           actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK);
252         }
253 
254         if (actual != ik) {
255           ResourceMark rm(THREAD);
256           log_error(aot)("Unable to resolve %s class from %s: %s", category_name, CDSConfig::type_of_archive_being_loaded(), ik->external_name());
257           log_error(aot)("Expected: " INTPTR_FORMAT ", actual: " INTPTR_FORMAT, p2i(ik), p2i(actual));
258           log_error(aot)("JVMTI class retransformation is not supported when archive was generated with -XX:+AOTClassLinking.");
259           MetaspaceShared::unrecoverable_loading_error();
260         }
261         assert(actual->is_loaded(), "must be");
262       }
263     }
264   }
265 }
266 
267 // Initiate loading of the <classes> in the <initiating_loader>. The <classes> should have already been loaded
268 // by a parent loader of the <initiating_loader>. This is necessary for handling pre-resolved CP entries.
269 //
270 // For example, we initiate the loading of java/lang/String in the AppClassLoader. This will allow
271 // any App classes to have a pre-resolved ConstantPool entry that references java/lang/String.
272 //
273 // TODO: we can limit the number of initiated classes to only those that are actually referenced by
274 // AOT-linked classes loaded by <initiating_loader>.
275 void AOTLinkedClassBulkLoader::initiate_loading(JavaThread* current, const char* category_name,
276                                                 Handle initiating_loader, Array<InstanceKlass*>* classes) {
277   if (classes == nullptr) {
278     return;
279   }
280 
281   assert(initiating_loader() == SystemDictionary::java_platform_loader() ||
282          initiating_loader() == SystemDictionary::java_system_loader(), "must be");
283   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(initiating_loader());
284   MonitorLocker mu1(SystemDictionary_lock);
285 
286   for (int i = 0; i < classes->length(); i++) {
287     InstanceKlass* ik = classes->at(i);
288     assert(ik->is_loaded(), "must have already been loaded by a parent loader");
289     assert(ik->class_loader() != initiating_loader(), "must be a parent loader");
290     assert(ik->class_loader() == nullptr ||
291            ik->class_loader() == SystemDictionary::java_platform_loader(), "must be");
292     if (ik->is_public() && !ik->is_hidden()) {
293       if (log_is_enabled(Info, aot, load)) {
294         ResourceMark rm(current);
295         const char* defining_loader = (ik->class_loader() == nullptr ? "boot" : "plat");
296         log_info(aot, load)("%s %s (initiated, defined by %s)", category_name, ik->external_name(),
297                             defining_loader);
298       }
299       SystemDictionary::add_to_initiating_loader(current, ik, loader_data);
300     }
301   }
302 }
303 
304 // Currently, we archive only three types of hidden classes:
305 //    - LambdaForms
306 //    - lambda proxy classes
307 //    - StringConcat classes
308 // See HeapShared::is_archivable_hidden_klass().
309 //
310 // LambdaForm classes (with names like java/lang/invoke/LambdaForm$MH+0x800000015) logically
311 // belong to the boot loader, but they are usually stored in their own special ClassLoaderData to
312 // facilitate class unloading, as a LambdaForm may refer to a class loaded by a custom loader
313 // that may be unloaded.
314 //
315 // We only support AOT-resolution of indys in the boot/platform/app loader, so there's no need
316 // to support class unloading. For simplicity, we put all archived LambdaForm classes in the
317 // "main" ClassLoaderData of the boot loader.
318 //
319 // (Even if we were to support other loaders, we would still feel free to ignore any requirement
320 // of class unloading, for any class asset in the AOT cache.  Anything that makes it into the AOT
321 // cache has a lifetime dispensation from unloading.  After all, the AOT cache never grows, and
322 // we can assume that the user is content with its size, and doesn't need its footprint to shrink.)
323 //
324 // Lambda proxy classes are normally stored in the same ClassLoaderData as their nest hosts, and
325 // StringConcat are normally stored in the main ClassLoaderData of the boot class loader. We
326 // do the same for the archived copies of such classes.
327 void AOTLinkedClassBulkLoader::load_hidden_class(ClassLoaderData* loader_data, InstanceKlass* ik, TRAPS) {
328   assert(HeapShared::is_lambda_form_klass(ik) ||
329          HeapShared::is_lambda_proxy_klass(ik) ||
330          HeapShared::is_string_concat_klass(ik), "sanity");
331   DEBUG_ONLY({
332       assert(ik->java_super()->is_loaded(), "must be");
333       for (int i = 0; i < ik->local_interfaces()->length(); i++) {
334         assert(ik->local_interfaces()->at(i)->is_loaded(), "must be");
335       }
336     });
337 
338   Handle pd;
339   PackageEntry* pkg_entry = nullptr;
340 
341   // Since a hidden class does not have a name, it cannot be reloaded
342   // normally via the system dictionary. Instead, we have to finish the
343   // loading job here.
344 
345   if (HeapShared::is_lambda_proxy_klass(ik)) {
346     InstanceKlass* nest_host = ik->nest_host_not_null();
347     assert(nest_host->is_loaded(), "must be");
348     pd = Handle(THREAD, nest_host->protection_domain());
349     pkg_entry = nest_host->package();
350   }
351 
352   ik->restore_unshareable_info(loader_data, pd, pkg_entry, CHECK);
353   SystemDictionary::load_shared_class_misc(ik, loader_data);
354   ik->add_to_hierarchy(THREAD);
355   assert(ik->is_loaded(), "Must be in at least loaded state");
356 
357   DEBUG_ONLY({
358       // Make sure we don't make this hidden class available by name, even if we don't
359       // use any special ClassLoaderData.
360       Handle loader(THREAD, loader_data->class_loader());
361       ResourceMark rm(THREAD);
362       assert(SystemDictionary::resolve_or_null(ik->name(), loader, THREAD) == nullptr,
363              "hidden classes cannot be accessible by name: %s", ik->external_name());
364       if (HAS_PENDING_EXCEPTION) {
365         CLEAR_PENDING_EXCEPTION;
366       }
367     });
368 }
369 
370 void AOTLinkedClassBulkLoader::finish_loading_javabase_classes(TRAPS) {
371   init_required_classes_for_loader(Handle(), AOTLinkedClassTable::for_static_archive()->boot(), CHECK);
372 }
373 
374 // Some AOT-linked classes for <class_loader> must be initialized early. This includes
375 // - classes that were AOT-initialized by AOTClassInitializer
376 // - the classes of all objects that are reachable from the archived mirrors of
377 //   the AOT-linked classes for <class_loader>.
378 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) {
379   if (classes != nullptr) {
380     for (int i = 0; i < classes->length(); i++) {
381       InstanceKlass* ik = classes->at(i);
382       if (ik->class_loader_data() == nullptr) {
383         // This class is not yet loaded. We will initialize it in a later phase.
384         // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes
385         // but k is part of AOTLinkedClassCategory::BOOT2.
386         continue;
387       }
388       if (ik->has_aot_initialized_mirror()) {
389         ik->initialize_with_aot_initialized_mirror(CHECK);
390       } else {
391         // Some cached heap objects may hold references to methods in aot-linked
392         // classes (via MemberName). We need to make sure all classes are
393         // linked to allow such MemberNames to be invoked.
394         if (ik->is_rewritten()) {
395           // (ik->is_rewritten() == false) means the class failed verification
396           // during the assembly phase, so there's no need to link it here.
397           ik->link_class(CHECK);
398         }
399       }
400     }
401   }
402 
403   HeapShared::init_classes_for_special_subgraph(class_loader, CHECK);
404 }
405 
406 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) {
407   if (!CDSConfig::is_using_aot_linked_classes()) {
408     return false;
409   }
410 
411   if (_all_completed) { // no more pending aot-linked classes
412     return false;
413   }
414 
415   if (k->is_objArray_klass()) {
416     k = ObjArrayKlass::cast(k)->bottom_klass();
417   }
418   if (!k->is_instance_klass()) {
419     // type array klasses (and their higher dimensions),
420     // must have been loaded before a GC can ever happen.
421     return false;
422   }
423 
424   // There's a small window during VM start-up where a not-yet loaded aot-linked
425   // class k may be discovered by the GC during VM initialization. This can happen
426   // when the heap contains an aot-cached instance of k, but k is not ready to be
427   // loaded yet. (TODO: JDK-8342429 eliminates this possibility)
428   //
429   // The following checks try to limit this window as much as possible for each of
430   // the four AOTLinkedClassCategory of classes that can be aot-linked.
431 
432   InstanceKlass* ik = InstanceKlass::cast(k);
433   if (ik->defined_by_boot_loader()) {
434     if (ik->module() != nullptr && ik->in_javabase_module()) {
435       // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in
436       // java.base must have been loaded before a GC can ever happen.
437       return false;
438     } else {
439       // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until
440       // module system is ready.
441       return !_boot2_completed;
442     }
443   } else if (ik->defined_by_platform_loader()) {
444     // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until
445     // the platform class loader is initialized.
446     return !_platform_completed;
447   } else if (ik->defined_by_app_loader()) {
448     // AOTLinkedClassCategory::APP cannot be loaded until the app class loader
449     // is initialized.
450     return !_app_completed;
451   } else {
452     return false;
453   }
454 }
455 
456 void AOTLinkedClassBulkLoader::replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) {
457   if (classes != nullptr) {
458     for (int i = 0; i < classes->length(); i++) {
459       InstanceKlass* ik = classes->at(i);
460       if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) {
461         CompilationPolicy::replay_training_at_init(ik, CHECK);
462       }
463     }
464   }
465 }
466 
467 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) {
468   if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) {
469     // Only static archive can have training data.
470     AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive();
471     replay_training_at_init(table->boot(),     CHECK);
472     replay_training_at_init(table->boot2(),    CHECK);
473     replay_training_at_init(table->platform(), CHECK);
474     replay_training_at_init(table->app(),      CHECK);
475   }
476 }
477 
478 void AOTLinkedClassBulkLoader::print_counters_on(outputStream* st) {
479   if (UsePerfData && _perf_class_preload_counters != nullptr) {
480     st->print_cr("AOTLinkedClassBulkLoader:");
481     st->print_cr("  preload:           " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) " (thread) / " JLONG_FORMAT_W(5) " events",
482                  _perf_class_preload_counters->elapsed_counter_value_us(),
483                  _perf_class_preload_counters->thread_counter_value_us(),
484                  _perf_classes_preloaded->get_value());
485   }
486 }