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 #ifndef SHARE_CDS_AOTLINKEDCLASSBULKLOADER_HPP
26 #define SHARE_CDS_AOTLINKEDCLASSBULKLOADER_HPP
27 
28 #include "memory/allocation.hpp"
29 #include "memory/allStatic.hpp"
30 #include "runtime/handles.hpp"
31 #include "utilities/exceptions.hpp"
32 #include "utilities/macros.hpp"
33 
34 class AOTLinkedClassTable;
35 class ClassLoaderData;
36 class InstanceKlass;
37 class SerializeClosure;
38 template <typename T> class Array;
39 enum class AOTLinkedClassCategory : int;
40 
41 // During a Production Run, the AOTLinkedClassBulkLoader loads all classes from
42 // the AOTLinkedClassTable into their respective ClassLoaders. This happens very early
43 // in the JVM bootstrap stage, before any Java bytecode is executed.
44 //
45 // IMPLEMENTATION NOTES:
46 // We also proactively link all the classes in the AOTLinkedClassTable, and move
47 // the AOT-initialized classes to the "initialized" state. Due to limitations
48 // of the current JVM bootstrap sequence, link_or_init_javabase_classes() and
49 // link_or_init_non_javabase_classes() need to be called after some Java bytecodes are
50 // executed. Future RFEs will move these calls to earlier stages.
51 class AOTLinkedClassBulkLoader :  AllStatic {
52   static void preload_classes_impl(TRAPS);
53   static void preload_classes_in_table(Array<InstanceKlass*>* classes,
54                                        const char* category_name, Handle loader, TRAPS);
55   static void initiate_loading(JavaThread* current, const char* category, Handle initiating_loader,
56                                Array<InstanceKlass*>* classes);
57   static void link_or_init_non_javabase_classes_impl(TRAPS);
58   static void link_or_init_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS);
59   static void replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) NOT_CDS_RETURN;
60 
61 #ifdef ASSERT
62   static void validate_module_of_preloaded_classes();
63   static void validate_module_of_preloaded_classes_in_table(Array<InstanceKlass*>* classes,
64                                                             const char* category_name, Handle loader);
65   static void validate_module(Klass* k, const char* category_name, oop module_oop);
66 #endif
67 
68 public:
69   static void serialize(SerializeClosure* soc) NOT_CDS_RETURN;
70   static void preload_classes(JavaThread* current);
71   static void link_or_init_javabase_classes(JavaThread* current) NOT_CDS_RETURN;
72   static void link_or_init_non_javabase_classes(JavaThread* current) NOT_CDS_RETURN;
73   static void exit_on_exception(JavaThread* current);
74 
75   static void replay_training_at_init_for_preloaded_classes(TRAPS) NOT_CDS_RETURN;
76 };
77 
78 #endif // SHARE_CDS_AOTLINKEDCLASSBULKLOADER_HPP