1 /*
  2  * Copyright (c) 2014, 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_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP
 26 #define SHARE_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP
 27 
 28 #include "cds/cds_globals.hpp"
 29 #include "cds/filemap.hpp"
 30 #include "cds/dumpTimeClassInfo.hpp"
 31 #include "cds/lambdaProxyClassDictionary.hpp"
 32 #include "cds/runTimeClassInfo.hpp"
 33 #include "classfile/classLoaderData.hpp"
 34 #include "classfile/packageEntry.hpp"
 35 #include "classfile/systemDictionary.hpp"
 36 #include "oops/klass.hpp"
 37 #include "oops/oopHandle.hpp"

 38 
 39 
 40 /*===============================================================================
 41 
 42     Handling of the classes in the AppCDS archive
 43 
 44     To ensure safety and to simplify the implementation, archived classes are
 45     "segregated" into 2 types. The following rules describe how they
 46     are stored and looked up.
 47 
 48 [1] Category of archived classes
 49 
 50     There are 2 disjoint groups of classes stored in the AppCDS archive:
 51 
 52     BUILTIN:              These classes may be defined ONLY by the BOOT/PLATFORM/APP
 53                           loaders.
 54 
 55     UNREGISTERED:         These classes may be defined ONLY by a ClassLoader
 56                           instance that's not listed above (using fingerprint matching)
 57 
 58 [2] How classes from different categories are specified in the classlist:
 59 
 60     Starting from JDK9, each class in the classlist may be specified with
 61     these keywords: "id", "super", "interfaces", "loader" and "source".
 62 
 63 
 64     BUILTIN               Only the "id" keyword may be (optionally) specified. All other
 65                           keywords are forbidden.
 66 
 67                           The named class is looked up from the jimage and from
 68                           Xbootclasspath/a and CLASSPATH.
 69 
 70     UNREGISTERED:         The "id", "super", and "source" keywords must all be
 71                           specified.
 72 
 73                           The "interfaces" keyword must be specified if the class implements
 74                           one or more local interfaces. The "interfaces" keyword must not be
 75                           specified if the class does not implement local interfaces.
 76 
 77                           The named class is looked up from the location specified in the
 78                           "source" keyword.
 79 
 80     Example classlist:
 81 
 82     # BUILTIN
 83     java/lang/Object id: 0
 84     java/lang/Cloneable id: 1
 85     java/lang/String
 86 
 87     # UNREGISTERED
 88     Bar id: 3 super: 0 interfaces: 1 source: /foo.jar
 89 
 90 
 91 [3] Identifying the category of archived classes
 92 
 93     BUILTIN:              (C->shared_classpath_index() >= 0)
 94     UNREGISTERED:         (C->shared_classpath_index() == UNREGISTERED_INDEX (-9999))
 95 
 96 [4] Lookup of archived classes at run time:
 97 
 98     (a) BUILTIN loaders:
 99 
100         search _builtin_dictionary
101 
102     (b) UNREGISTERED loaders:
103 
104         search _unregistered_dictionary for an entry that matches the
105         (name, clsfile_len, clsfile_crc32).
106 
107 ===============================================================================*/
108 #define UNREGISTERED_INDEX -9999
109 
110 class BootstrapInfo;
111 class ClassFileStream;
112 class ConstantPoolCache;
113 class Dictionary;
114 class DumpTimeClassInfo;
115 class DumpTimeSharedClassTable;
116 class LambdaProxyClassDictionary;
117 class RunTimeClassInfo;
118 class RunTimeSharedDictionary;
119 class DumpTimeLambdaProxyClassDictionary;
120 class LambdaProxyClassKey;
121 
122 class SharedClassLoadingMark {
123  private:
124   Thread* THREAD;
125   InstanceKlass* _klass;
126  public:
127   SharedClassLoadingMark(Thread* current, InstanceKlass* ik) : THREAD(current), _klass(ik) {}
128   ~SharedClassLoadingMark() {
129     assert(THREAD != nullptr, "Current thread is nullptr");
130     assert(_klass != nullptr, "InstanceKlass is nullptr");
131     if (HAS_PENDING_EXCEPTION) {
132       if (_klass->is_shared()) {
133         _klass->set_shared_loading_failed();
134       }
135     }
136   }
137 };
138 
139 class SystemDictionaryShared: public SystemDictionary {
140   friend class CleanupDumpTimeLambdaProxyClassTable;
141 
142   struct ArchiveInfo {
143     RunTimeSharedDictionary _builtin_dictionary;
144     RunTimeSharedDictionary _unregistered_dictionary;
145     LambdaProxyClassDictionary _lambda_proxy_class_dictionary;
146 
147     const RunTimeLambdaProxyClassInfo* lookup_lambda_proxy_class(RunTimeLambdaProxyClassKey* key) {
148       return _lambda_proxy_class_dictionary.lookup(key, key->hash(), 0);
149     }
150 
151     void print_on(const char* prefix, outputStream* st);
152     void print_table_statistics(const char* prefix, outputStream* st);
153   };
154 
155 public:
156   enum : char {
157     FROM_FIELD_IS_PROTECTED = 1 << 0,
158     FROM_IS_ARRAY           = 1 << 1,
159     FROM_IS_OBJECT          = 1 << 2
160   };
161 
162 private:
163 
164   static DumpTimeSharedClassTable* _dumptime_table;
165   static DumpTimeLambdaProxyClassDictionary* _dumptime_lambda_proxy_class_dictionary;
166 
167   static ArchiveInfo _static_archive;
168   static ArchiveInfo _dynamic_archive;
169 
170   static ArchiveInfo* get_archive(bool is_static_archive) {
171     return is_static_archive ? &_static_archive : &_dynamic_archive;
172   }
173 
174   static InstanceKlass* load_shared_class_for_builtin_loader(
175                                                Symbol* class_name,
176                                                Handle class_loader,
177                                                TRAPS);
178   static InstanceKlass* acquire_class_for_current_thread(
179                                  InstanceKlass *ik,
180                                  Handle class_loader,
181                                  Handle protection_domain,
182                                  const ClassFileStream* cfs,
183                                  TRAPS);
184 
185   // Guaranteed to return non-null value for non-shared classes.
186   // k must not be a shared class.
187   static DumpTimeClassInfo* get_info(InstanceKlass* k);
188   static DumpTimeClassInfo* get_info_locked(InstanceKlass* k);
189 
190   static void write_dictionary(RunTimeSharedDictionary* dictionary,
191                                bool is_builtin);
192   static void write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary* dictionary);
193   static void cleanup_lambda_proxy_class_dictionary();
194   static void reset_registered_lambda_proxy_class(InstanceKlass* ik);
195   static bool is_jfr_event_class(InstanceKlass *k);
196   static bool check_for_exclusion_impl(InstanceKlass* k);
197   static void remove_dumptime_info(InstanceKlass* k) NOT_CDS_RETURN;
198   static bool has_been_redefined(InstanceKlass* k);
199   static InstanceKlass* retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) NOT_CDS_RETURN_(nullptr);
200   DEBUG_ONLY(static bool _class_loading_may_happen;)
201 
202 public:










203   static bool is_registered_lambda_proxy_class(InstanceKlass* ik);

204   static bool is_hidden_lambda_proxy(InstanceKlass* ik);
205   static bool is_early_klass(InstanceKlass* k);   // Was k loaded while JvmtiExport::is_early_phase()==true
206   static bool has_archived_enum_objs(InstanceKlass* ik);
207   static void set_has_archived_enum_objs(InstanceKlass* ik);
208 
209   static InstanceKlass* find_builtin_class(Symbol* class_name);
210 
211   static const RunTimeClassInfo* find_record(RunTimeSharedDictionary* static_dict,
212                                                    RunTimeSharedDictionary* dynamic_dict,
213                                                    Symbol* name);
214 
215   static bool has_platform_or_app_classes();
216 
217   // Called by PLATFORM/APP loader only
218   static InstanceKlass* find_or_load_shared_class(Symbol* class_name,
219                                                Handle class_loader,
220                                                TRAPS);
221 
222 
223   static void allocate_shared_data_arrays(int size, TRAPS);
224 
225   static bool is_builtin_loader(ClassLoaderData* loader_data);
226 
227   static InstanceKlass* lookup_super_for_unregistered_class(Symbol* class_name,
228                                                             Symbol* super_name,  bool is_superclass);
229 
230   static void initialize() NOT_CDS_RETURN;
231   static void init_dumptime_info(InstanceKlass* k) NOT_CDS_RETURN;
232   static void handle_class_unloading(InstanceKlass* k) NOT_CDS_RETURN;
233 




234   static Dictionary* boot_loader_dictionary() {
235     return ClassLoaderData::the_null_class_loader_data()->dictionary();
236   }
237 
238   static void update_shared_entry(InstanceKlass* klass, int id);
239   static void set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs);
240 
241   static InstanceKlass* lookup_from_stream(Symbol* class_name,
242                                            Handle class_loader,
243                                            Handle protection_domain,
244                                            const ClassFileStream* st,
245                                            TRAPS);
246   // "verification_constraints" are a set of checks performed by
247   // VerificationType::is_reference_assignable_from when verifying a shared class during
248   // dump time.
249   //
250   // With AppCDS, it is possible to override archived classes by calling
251   // ClassLoader.defineClass() directly. SystemDictionary::load_shared_class() already
252   // ensures that you cannot load a shared class if its super type(s) are changed. However,
253   // we need an additional check to ensure that the verification_constraints did not change
254   // between dump time and runtime.
255   static bool add_verification_constraint(InstanceKlass* k, Symbol* name,
256                   Symbol* from_name, bool from_field_is_protected,
257                   bool from_is_array, bool from_is_object) NOT_CDS_RETURN_(false);
258   static void check_verification_constraints(InstanceKlass* klass,
259                                              TRAPS) NOT_CDS_RETURN;
260   static void add_enum_klass_static_field(InstanceKlass* ik, int root_index);
261   static void set_class_has_failed_verification(InstanceKlass* ik) NOT_CDS_RETURN;
262   static bool has_class_failed_verification(InstanceKlass* ik) NOT_CDS_RETURN_(false);
263   static void add_lambda_proxy_class(InstanceKlass* caller_ik,
264                                      InstanceKlass* lambda_ik,
265                                      Symbol* invoked_name,
266                                      Symbol* invoked_type,
267                                      Symbol* method_type,
268                                      Method* member_method,
269                                      Symbol* instantiated_method_type, TRAPS) NOT_CDS_RETURN;
270   static void add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key,
271                                                              InstanceKlass* proxy_klass) NOT_CDS_RETURN;
272   static InstanceKlass* get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
273                                                       Symbol* invoked_name,
274                                                       Symbol* invoked_type,
275                                                       Symbol* method_type,
276                                                       Method* member_method,
277                                                       Symbol* instantiated_method_type) NOT_CDS_RETURN_(nullptr);
278   static InstanceKlass* get_shared_nest_host(InstanceKlass* lambda_ik) NOT_CDS_RETURN_(nullptr);
279   static InstanceKlass* prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
280                                                           InstanceKlass* caller_ik, TRAPS) NOT_CDS_RETURN_(nullptr);
281   static bool check_linking_constraints(Thread* current, InstanceKlass* klass) NOT_CDS_RETURN_(false);
282   static void record_linking_constraint(Symbol* name, InstanceKlass* klass,
283                                      Handle loader1, Handle loader2) NOT_CDS_RETURN;
284   static bool is_builtin(InstanceKlass* k) {
285     return (k->shared_classpath_index() != UNREGISTERED_INDEX);
286   }
287   static bool add_unregistered_class(Thread* current, InstanceKlass* k);
288 
289   static void finish_exclusion_checks();
290   static DumpTimeSharedClassTable* dumptime_table() { return _dumptime_table; }
291 
292   static bool should_be_excluded(Klass* k);
293   static bool check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info);
294   static void validate_before_archiving(InstanceKlass* k);
295   static bool is_excluded_class(InstanceKlass* k);
296   static void set_excluded(InstanceKlass* k);
297   static void set_excluded_locked(InstanceKlass* k);
298   static bool warn_excluded(InstanceKlass* k, const char* reason);
299   static void dumptime_classes_do(class MetaspaceClosure* it);
300   static size_t estimate_size_for_archive();
301   static void write_to_archive(bool is_static_archive = true);
302   static void adjust_lambda_proxy_class_dictionary();

303   static void serialize_dictionary_headers(class SerializeClosure* soc,
304                                            bool is_static_archive = true);
305   static void serialize_vm_classes(class SerializeClosure* soc);
306   static void print() { return print_on(tty); }
307   static void print_on(outputStream* st) NOT_CDS_RETURN;
308   static void print_shared_archive(outputStream* st, bool is_static = true) NOT_CDS_RETURN;
309   static void print_table_statistics(outputStream* st) NOT_CDS_RETURN;
310   static bool is_dumptime_table_empty() NOT_CDS_RETURN_(true);
311   static bool is_supported_invokedynamic(BootstrapInfo* bsi) NOT_CDS_RETURN_(false);
312   DEBUG_ONLY(static bool class_loading_may_happen() {return _class_loading_may_happen;})



313 
314 #ifdef ASSERT
315   // This object marks a critical period when writing the CDS archive. During this
316   // period, the JVM must not load any new classes, so as to avoid adding new
317   // items in the SystemDictionaryShared::_dumptime_table.
318   class NoClassLoadingMark: public StackObj {
319   public:
320     NoClassLoadingMark() {
321       assert(_class_loading_may_happen, "must not be nested");
322       _class_loading_may_happen = false;
323     }
324     ~NoClassLoadingMark() {
325       _class_loading_may_happen = true;
326     }
327   };
328 #endif
329 
330   template <typename T>
331   static unsigned int hash_for_shared_dictionary_quick(T* ptr) {
332     assert(MetaspaceObj::is_shared((const MetaspaceObj*)ptr), "must be");
333     assert(ptr > (T*)SharedBaseAddress, "must be");
334     uintx offset = uintx(ptr) - uintx(SharedBaseAddress);
335     return primitive_hash<uintx>(offset);
336   }
337 
338   static unsigned int hash_for_shared_dictionary(address ptr);


339 };
340 
341 #endif // SHARE_CLASSFILE_SYSTEMDICTIONARYSHARED_HPP
--- EOF ---