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