1 /*
  2  * Copyright (c) 2022, 2023, 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_CLASSPRELINKER_HPP
 26 #define SHARE_CDS_CLASSPRELINKER_HPP
 27 
 28 #include "interpreter/bytecodes.hpp"
 29 #include "oops/oopsHierarchy.hpp"
 30 #include "memory/allStatic.hpp"
 31 #include "memory/allocation.hpp"
 32 #include "runtime/handles.hpp"
 33 #include "utilities/exceptions.hpp"
 34 #include "utilities/macros.hpp"
 35 #include "utilities/resourceHash.hpp"
 36 
 37 class ConstantPool;
 38 class constantPoolHandle;
 39 class InstanceKlass;
 40 class Klass;
 41 class SerializeClosure;
 42 
 43 template <typename T> class Array;
 44 template <typename T> class GrowableArray;
 45 // ClassPrelinker is used to perform ahead-of-time linking of ConstantPool entries
 46 // for archived InstanceKlasses.
 47 //
 48 // At run time, Java classes are loaded dynamically and may be replaced with JVMTI.
 49 // Therefore, we take care to prelink only the ConstantPool entries that are
 50 // guatanteed to resolve to the same results at both dump time and run time.
 51 //
 52 // For example, a JVM_CONSTANT_Class reference to a supertype can be safely resolved
 53 // at dump time, because at run time we will load a class from the CDS archive only
 54 // if all of its supertypes are loaded from the CDS archive.
 55 class ClassPrelinker :  AllStatic {
 56   class PreloadedKlassRecorder;
 57   using ClassesTable = ResourceHashtable<InstanceKlass*, bool, 15889, AnyObj::C_HEAP, mtClassShared> ;
 58   static ClassesTable* _processed_classes;
 59   // Classes loaded inside vmClasses::resolve_all()
 60   static ClassesTable* _vm_classes;
 61   // Classes that will be automatically loaded into system dictionary at
 62   // VM start-up (this is a superset of _vm_classes)
 63   static ClassesTable* _preloaded_classes;
 64   static ClassesTable* _platform_initiated_classes; // classes initiated but not loaded by platform loader
 65   static ClassesTable* _app_initiated_classes;      // classes initiated but not loaded by app loader
 66   static int _num_vm_klasses;
 67   static bool _record_javabase_only;
 68   static bool _preload_javabase_only;
 69   struct PreloadedKlasses {
 70     Array<InstanceKlass*>* _boot;  // only java.base classes
 71     Array<InstanceKlass*>* _boot2; // boot classes in other modules
 72     Array<InstanceKlass*>* _platform;
 73     Array<InstanceKlass*>* _platform_initiated;
 74     Array<InstanceKlass*>* _app;
 75     Array<InstanceKlass*>* _app_initiated;
 76     PreloadedKlasses() : _boot(nullptr), _boot2(nullptr), _platform(nullptr), _app(nullptr) {}
 77   };
 78 
 79   static PreloadedKlasses _static_preloaded_klasses;
 80   static PreloadedKlasses _dynamic_preloaded_klasses;
 81   static Array<InstanceKlass*>* _unregistered_klasses_from_preimage;
 82 
 83   static void add_one_vm_class(InstanceKlass* ik);
 84 
 85 #ifdef ASSERT
 86   static bool is_in_archivebuilder_buffer(address p);
 87 #endif
 88 
 89   template <typename T>
 90   static bool is_in_archivebuilder_buffer(T p) {
 91     return is_in_archivebuilder_buffer((address)(p));
 92   }
 93   static void resolve_string(constantPoolHandle cp, int cp_index, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
 94   static Klass* maybe_resolve_class(constantPoolHandle cp, int cp_index, TRAPS);
 95   static bool can_archive_resolved_klass(InstanceKlass* cp_holder, Klass* resolved_klass);
 96   static Klass* find_loaded_class(Thread* current, oop class_loader, Symbol* name);
 97   static Klass* find_loaded_class(Thread* current, ConstantPool* cp, int class_cp_index);
 98   static void add_preloaded_klasses(Array<InstanceKlass*>* klasses);
 99   static void add_unrecorded_initiated_klasses(ClassesTable* table, Array<InstanceKlass*>* klasses);
100   static void add_extra_initiated_klasses(PreloadedKlasses* table);
101   static void add_initiated_klasses_for_loader(ClassLoaderData* loader_data, const char* loader_name, ClassesTable* table);
102   static void add_initiated_klass(InstanceKlass* ik, InstanceKlass* target);
103   static void add_initiated_klass(ClassesTable* initiated_classes, const char* loader_name, InstanceKlass* target);
104   static Array<InstanceKlass*>* record_preloaded_klasses(int loader_type);
105   static Array<InstanceKlass*>* record_initiated_klasses(ClassesTable* table);
106   static void runtime_preload(PreloadedKlasses* table, Handle loader, TRAPS);
107   static void preload_archived_hidden_class(Handle class_loader, InstanceKlass* ik,
108                                             const char* loader_name, TRAPS);
109   static void jvmti_agent_error(InstanceKlass* expected, InstanceKlass* actual, const char* type);
110 
111   // fmi = FieldRef/MethodRef/InterfaceMethodRef
112   static Klass* get_fmi_ref_resolved_archivable_klass(ConstantPool* cp, int cp_index);
113   static void maybe_resolve_fmi_ref(InstanceKlass* ik, Method* m, Bytecodes::Code bc, int raw_index,
114                                     GrowableArray<bool>* resolve_fmi_list, TRAPS);
115   static bool is_in_javabase(InstanceKlass* ik);
116   class RecordResolveIndysCLDClosure;
117   class RecordInitiatedClassesClosure;
118 
119   // helper
120   static Klass* resolve_boot_klass_or_fail(const char* class_name, TRAPS);
121 
122   // java/lang/reflect/Proxy caching
123   static void init_dynamic_proxy_cache(TRAPS);
124 public:
125   static void initialize();
126   static void dispose();
127 
128   static void setup_forced_preinit_classes();
129   static void maybe_preinit_class(InstanceKlass* ik, TRAPS);
130 
131   static void preresolve_class_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list);
132   static void preresolve_field_and_method_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list);
133   static void preresolve_indy_cp_entries(JavaThread* current, InstanceKlass* ik, GrowableArray<bool>* preresolve_list);
134   static void preresolve_invoker_class(JavaThread* current, InstanceKlass* ik);
135   static void apply_final_image_eager_linkage(TRAPS);
136 
137   static bool is_indy_archivable(ConstantPool* cp, int cp_index);
138 
139   // java/lang/Class$ReflectionData caching
140   static void record_reflection_data_flags_for_preimage(InstanceKlass* ik, TRAPS);
141   static int class_reflection_data_flags(InstanceKlass* ik, TRAPS);
142   static void generate_reflection_data(JavaThread* current, InstanceKlass* ik, int rd_flags);
143 
144   // java/lang/reflect/Proxy caching
145   static void trace_dynamic_proxy_class(oop loader, const char* proxy_name, objArrayOop interfaces, int access_flags);
146   static void define_dynamic_proxy_class(Handle loader, Handle proxy_name, Handle interfaces, int access_flags, TRAPS);
147 
148   // Is this class resolved as part of vmClasses::resolve_all()? If so, these
149   // classes are guatanteed to be loaded at runtime (and cannot be replaced by JVMTI)
150   // when CDS is enabled. Therefore, we can safely keep a direct reference to these
151   // classes.
152   static bool is_vm_class(InstanceKlass* ik);
153   static bool is_preloaded_class(InstanceKlass* ik);
154   static int  num_vm_klasses() { return _num_vm_klasses; }
155   // Resolve all constant pool entries that are safe to be stored in the
156   // CDS archive.
157   static void dumptime_resolve_constants(InstanceKlass* ik, TRAPS);
158 
159   // Can we resolve the klass entry at cp_index in this constant pool, and store
160   // the result in the CDS archive? Returns true if cp_index is guaranteed to
161   // resolve to the same InstanceKlass* at both dump time and run time.
162   static bool can_archive_resolved_klass(ConstantPool* cp, int cp_index);
163 
164   // Similar to can_archive_resolved_klass() -- returns true if cp_index is
165   // guaranteed to resolve to the same result both dump time and run time.
166   static bool can_archive_resolved_field(ConstantPool* cp, int cp_index);
167   static bool can_archive_resolved_method(ConstantPool* cp, int cp_index);
168 
169   static bool can_archive_preinitialized_mirror(InstanceKlass* src_ik);
170 
171   static void record_preloaded_klasses(bool is_static_archive);
172   static void record_initiated_klasses(bool is_static_archive);
173   static void record_unregistered_klasses();
174   static void record_final_image_eager_linkage();
175   static void serialize(SerializeClosure* soc, bool is_static_archive);
176 
177   static void runtime_preload(JavaThread* current, Handle loader) NOT_CDS_RETURN;
178   static void init_javabase_preloaded_classes(TRAPS) NOT_CDS_RETURN;
179   static void replay_training_at_init_for_javabase_preloaded_classes(TRAPS) NOT_CDS_RETURN;
180   static bool class_preloading_finished();
181 
182   static int  num_platform_initiated_classes();
183   static int  num_app_initiated_classes();
184   static void print_counters() NOT_CDS_RETURN;
185 };
186 
187 #endif // SHARE_CDS_CLASSPRELINKER_HPP