1 /*
2 * Copyright (c) 1997, 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_CLASSLOADER_HPP
26 #define SHARE_CLASSFILE_CLASSLOADER_HPP
27
28 #include "jimage.hpp"
29 #include "runtime/handles.hpp"
30 #include "runtime/perfDataTypes.hpp"
31 #include "utilities/exceptions.hpp"
32 #include "utilities/macros.hpp"
33 #include "utilities/ostream.hpp"
34 #include "utilities/zipLibrary.hpp"
35
36 // The VM class loader.
37 #include <sys/stat.h>
38
39 // Name of boot "modules" image
40 #define MODULES_IMAGE_NAME "modules"
41
42 // Class path entry (directory or zip file)
43
44 class JImageFile;
45 class ClassFileStream;
46 class PackageEntry;
47 template <typename T> class GrowableArray;
48
49 class ClassPathEntry : public CHeapObj<mtClass> {
50 private:
51 ClassPathEntry* volatile _next;
52 protected:
53 const char* copy_path(const char*path);
54 public:
55 ClassPathEntry* next() const;
56 virtual ~ClassPathEntry() {}
57 void set_next(ClassPathEntry* next);
58
59 virtual bool is_modules_image() const { return false; }
60 virtual bool is_jar_file() const { return false; }
61 virtual const char* name() const = 0;
62 virtual JImageFile* jimage() const { return nullptr; }
63 virtual void close_jimage() {}
64 // Constructor
65 ClassPathEntry() : _next(nullptr) {}
66 // Attempt to locate file_name through this class path entry.
67 // Returns a class file parsing stream if successful.
68 virtual ClassFileStream* open_stream(JavaThread* current, const char* name) = 0;
69 // Open the stream for a specific class loader
70 virtual ClassFileStream* open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
71 return open_stream(current, name);
72 }
73 };
74
75 class ClassPathDirEntry: public ClassPathEntry {
76 private:
77 const char* _dir; // Name of directory
78 public:
79 const char* name() const { return _dir; }
80 ClassPathDirEntry(const char* dir) {
81 _dir = copy_path(dir);
82 }
83 virtual ~ClassPathDirEntry();
84 ClassFileStream* open_stream(JavaThread* current, const char* name);
85 };
86
87 class ClassPathZipEntry: public ClassPathEntry {
88 private:
89 jzfile* _zip; // The zip archive
90 const char* _zip_name; // Name of zip archive
91 public:
92 bool is_jar_file() const { return true; }
93 const char* name() const { return _zip_name; }
94 ClassPathZipEntry(jzfile* zip, const char* zip_name);
95 virtual ~ClassPathZipEntry();
96 bool has_entry(JavaThread* current, const char* name);
97 u1* open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate);
98 ClassFileStream* open_stream(JavaThread* current, const char* name);
99 };
100
101
102 // A singleton path entry which takes ownership of the initialized JImageFile
103 // reference. Not used for exploded builds.
104 class ClassPathImageEntry: public ClassPathEntry {
105 private:
106 const char* _name;
107 DEBUG_ONLY(static ClassPathImageEntry* _singleton;)
108 public:
109 bool is_modules_image() const;
110 const char* name() const { return _name == nullptr ? "" : _name; }
111 // Called to close the JImage during os::abort (normally not called).
112 void close_jimage();
113 // Takes effective ownership of the static JImageFile pointer.
114 ClassPathImageEntry(const char* name);
115 virtual ~ClassPathImageEntry() { ShouldNotReachHere(); }
116
117 ClassFileStream* open_stream(JavaThread* current, const char* name);
118 ClassFileStream* open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data);
119 };
120
121 // ModuleClassPathList contains a linked list of ClassPathEntry's
122 // that have been specified for a specific module. Currently,
123 // the only way to specify a module/path pair is via the --patch-module
124 // command line option.
125 class ModuleClassPathList : public CHeapObj<mtClass> {
126 private:
127 Symbol* _module_name;
128 // First and last entries of class path entries for a specific module
129 ClassPathEntry* _module_first_entry;
130 ClassPathEntry* _module_last_entry;
131 public:
132 Symbol* module_name() const { return _module_name; }
133 ClassPathEntry* module_first_entry() const { return _module_first_entry; }
134 ModuleClassPathList(Symbol* module_name);
135 ~ModuleClassPathList();
136 void add_to_list(ClassPathEntry* new_entry);
137 };
138
139 class ClassLoader: AllStatic {
140 public:
141 enum ClassLoaderType {
142 OTHER = 0,
143 BOOT_LOADER = 1, /* boot loader */
144 PLATFORM_LOADER = 2, /* PlatformClassLoader */
145 APP_LOADER = 3 /* AppClassLoader */
146 };
147 protected:
148
149 // Performance counters
150 static PerfCounter* _perf_accumulated_time;
151 static PerfCounter* _perf_classes_inited;
152 static PerfCounter* _perf_class_init_time;
153 static PerfCounter* _perf_class_init_selftime;
154 static PerfCounter* _perf_classes_verified;
155 static PerfCounter* _perf_class_verify_time;
156 static PerfCounter* _perf_class_verify_selftime;
157 static PerfCounter* _perf_classes_linked;
158 static PerfCounter* _perf_class_link_time;
159 static PerfCounter* _perf_class_link_selftime;
160 static PerfCounter* _perf_shared_classload_time;
161 static PerfCounter* _perf_sys_classload_time;
162 static PerfCounter* _perf_app_classload_time;
163 static PerfCounter* _perf_app_classload_selftime;
164 static PerfCounter* _perf_app_classload_count;
165 static PerfCounter* _perf_define_appclasses;
166 static PerfCounter* _perf_define_appclass_time;
167 static PerfCounter* _perf_define_appclass_selftime;
168 static PerfCounter* _perf_app_classfile_bytes_read;
169 static PerfCounter* _perf_sys_classfile_bytes_read;
170 static PerfCounter* _perf_ik_link_methods_time;
171 static PerfCounter* _perf_method_adapters_time;
172 static PerfCounter* _perf_ik_link_methods_count;
173 static PerfCounter* _perf_method_adapters_count;
174
175 static PerfCounter* _perf_resolve_indy_time;
176 static PerfCounter* _perf_resolve_invokehandle_time;
177 static PerfCounter* _perf_resolve_mh_time;
178 static PerfCounter* _perf_resolve_mt_time;
179
180 static PerfCounter* _perf_resolve_indy_count;
181 static PerfCounter* _perf_resolve_invokehandle_count;
182 static PerfCounter* _perf_resolve_mh_count;
183 static PerfCounter* _perf_resolve_mt_count;
184
185 static PerfCounter* _unsafe_defineClassCallCounter;
186
187 // Count the time taken to hash the scondary superclass arrays.
188 static PerfCounter* _perf_secondary_hash_time;
189
190 // The boot class path consists of 3 ordered pieces:
191 // 1. the module/path pairs specified to --patch-module
192 // --patch-module=<module>=<file>(<pathsep><file>)*
193 // 2. the base piece
194 // [jimage | build with exploded modules]
195 // 3. boot loader append path
196 // [-Xbootclasspath/a]; [jvmti appended entries]
197 //
198 // The boot loader must obey this order when attempting
199 // to load a class.
200
201 // 1. Contains the module/path pairs specified to --patch-module
202 static GrowableArray<ModuleClassPathList*>* _patch_mod_entries;
203
204 // 2. the base piece
205 // Contains the ClassPathImageEntry of the modular java runtime image.
206 // If no java runtime image is present, this indicates a
207 // build with exploded modules is being used instead.
208 static ClassPathImageEntry* _jrt_entry;
209 static GrowableArray<ModuleClassPathList*>* _exploded_entries;
210 enum { EXPLODED_ENTRY_SIZE = 80 }; // Initial number of exploded modules
211
212 // 3. the boot loader's append path
213 // [-Xbootclasspath/a]; [jvmti appended entries]
214 // Note: boot loader append path does not support named modules.
215 static ClassPathEntry* volatile _first_append_entry_list;
216 static ClassPathEntry* first_append_entry() {
217 return AtomicAccess::load_acquire(&_first_append_entry_list);
218 }
219
220 // Last entry in linked list of appended ClassPathEntry instances
221 static ClassPathEntry* volatile _last_append_entry;
222
223 public:
224
225 static bool has_bootclasspath_append() { return first_append_entry() != nullptr; }
226
227 protected:
228 // Initialization:
229 // - setup the boot loader's system class path
230 // - setup the boot loader's patch mod entries, if present
231 // - create the ModuleEntry for java.base
232 static void setup_bootstrap_search_path(JavaThread* current);
233 static void setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path);
234 static void setup_patch_mod_entries();
235 static void create_javabase();
236
237 static void* dll_lookup(void* lib, const char* name, const char* path);
238 static void load_java_library();
239 static void load_jimage_library();
240
241 public:
242 static void* zip_library_handle();
243 static jzfile* open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread);
244 static ClassPathEntry* create_class_path_entry(JavaThread* current,
245 const char *path, const struct stat* st);
246
247 // Canonicalizes path names, so strcmp will work properly. This is mainly
248 // to avoid confusing the zip library
249 static char* get_canonical_path(const char* orig, Thread* thread);
250 static const char* file_name_for_class_name(const char* class_name,
251 int class_name_len);
252 static PackageEntry* get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data);
253 static int crc32(int crc, const char* buf, int len);
254 static bool update_class_path_entry_list(JavaThread* current,
255 const char *path);
256 static void print_bootclasspath();
257
258 // Timing
259 static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; }
260 static PerfCounter* perf_classes_inited() { return _perf_classes_inited; }
261 static PerfCounter* perf_class_init_time() { return _perf_class_init_time; }
262 static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; }
263 static PerfCounter* perf_classes_verified() { return _perf_classes_verified; }
264 static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; }
265 static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; }
266 static PerfCounter* perf_classes_linked() { return _perf_classes_linked; }
267 static PerfCounter* perf_class_link_time() { return _perf_class_link_time; }
268 static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; }
269 static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; }
270 static PerfCounter* perf_secondary_hash_time() {
271 return _perf_secondary_hash_time;
272 }
273 static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; }
274 static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; }
275 static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; }
276 static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; }
277 static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; }
278 static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; }
279 static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }
280 static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }
281 static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }
282
283 static PerfCounter* perf_ik_link_methods_time() { return _perf_ik_link_methods_time; }
284 static PerfCounter* perf_method_adapters_time() { return _perf_method_adapters_time; }
285 static PerfCounter* perf_ik_link_methods_count() { return _perf_ik_link_methods_count; }
286 static PerfCounter* perf_method_adapters_count() { return _perf_method_adapters_count; }
287
288 static PerfCounter* perf_resolve_invokedynamic_time() { return _perf_resolve_indy_time; }
289 static PerfCounter* perf_resolve_invokehandle_time() { return _perf_resolve_invokehandle_time; }
290 static PerfCounter* perf_resolve_method_handle_time() { return _perf_resolve_mh_time; }
291 static PerfCounter* perf_resolve_method_type_time() { return _perf_resolve_mt_time; }
292
293 static PerfCounter* perf_resolve_invokedynamic_count() { return _perf_resolve_indy_count; }
294 static PerfCounter* perf_resolve_invokehandle_count() { return _perf_resolve_invokehandle_count; }
295 static PerfCounter* perf_resolve_method_handle_count() { return _perf_resolve_mh_count; }
296 static PerfCounter* perf_resolve_method_type_count() { return _perf_resolve_mt_count; }
297
298 static void print_counters(outputStream *st);
299
300 // Record how many calls to Unsafe_DefineClass
301 static PerfCounter* unsafe_defineClassCallCounter() {
302 return _unsafe_defineClassCallCounter;
303 }
304
305 // Modular java runtime image is present vs. a build with exploded modules
306 static bool has_jrt_entry() { return (_jrt_entry != nullptr); }
307 static ClassPathEntry* get_jrt_entry() { return _jrt_entry; }
308 static void close_jrt_image();
309
310 // Add a module's exploded directory to the boot loader's exploded module build list
311 static void add_to_exploded_build_list(JavaThread* current, Symbol* module_name);
312
313 // Search the module list for the class file stream based on the file name and java package
314 static ClassFileStream* search_module_entries(JavaThread* current,
315 const GrowableArray<ModuleClassPathList*>* const module_list,
316 PackageEntry* pkg_entry, // Java package entry derived from the class name
317 const char* const file_name);
318
319 // Load individual .class file
320 static InstanceKlass* load_class(Symbol* class_name, PackageEntry* pkg_entry, bool search_append_only, TRAPS);
321
322 // If the specified package has been loaded by the system, then returns
323 // the name of the directory or ZIP file that the package was loaded from.
324 // Returns null if the package was not loaded.
325 // Note: The specified name can either be the name of a class or package.
326 // If a package name is specified, then it must be "/"-separator and also
327 // end with a trailing "/".
328 static oop get_system_package(const char* name, TRAPS);
329
330 // Returns an array of Java strings representing all of the currently
331 // loaded system packages.
332 // Note: The package names returned are "/"-separated and end with a
333 // trailing "/".
334 static objArrayOop get_system_packages(TRAPS);
335
336 // Initialization
337 static void initialize(TRAPS);
338 static void classLoader_init2(JavaThread* current);
339
340 static int compute_Object_vtable();
341
342 static ClassPathEntry* classpath_entry(int n);
343
344 static bool is_in_patch_mod_entries(Symbol* module_name);
345
346 #if INCLUDE_CDS
347 static char* uri_to_path(const char* uri);
348 static void record_result(JavaThread* current, InstanceKlass* ik,
349 const ClassFileStream* stream, bool redefined);
350 static void record_result_for_builtin_loader(s2 classpath_index, InstanceKlass* result, bool redefined);
351 static void record_hidden_class(InstanceKlass* ik);
352 static void append_boot_classpath(ClassPathEntry* new_entry);
353 #endif
354
355 // Retrieves additional VM options prior to flags processing. Options held
356 // in the JImage file are retrieved without fully initializing it. (this is
357 // the only JImage lookup which can succeed before init_jimage() is called).
358 static char* lookup_vm_options();
359
360 // Called once, after all flags are processed, to finish initializing the
361 // JImage file. Until this is called, jimage_find_resource(), and any other
362 // JImage resource lookups or access will fail.
363 static void init_jimage(bool enable_preview);
364
365 // Determines if the named module is present in the
366 // modules jimage file or in the exploded modules directory.
367 static bool is_module_observable(const char* module_name);
368
369 static void trace_class_path(const char* msg, const char* name = nullptr);
370
371 // VM monitoring and management support
372 static jlong classloader_time_ms();
373 static jlong class_method_total_size();
374 static jlong class_init_count();
375 static jlong class_init_time_ms();
376 static jlong class_verify_time_ms();
377 static jlong class_link_count();
378 static jlong class_link_time_ms();
379
380 // adds a class path to the boot append entries
381 static void add_to_boot_append_entries(ClassPathEntry* new_entry);
382
383 // creates a class path zip entry (returns null if JAR file cannot be opened)
384 static ClassPathZipEntry* create_class_path_zip_entry(const char *path);
385
386 static bool string_ends_with(const char* str, const char* str_to_find);
387
388 // Extract package name from a fully qualified class name
389 // *bad_class_name is set to true if there's a problem with parsing class_name, to
390 // distinguish from a class_name with no package name, as both cases have a null return value
391 static Symbol* package_from_class_name(const Symbol* class_name, bool* bad_class_name = nullptr);
392
393 // Debugging
394 static void verify() PRODUCT_RETURN;
395 };
396
397 // PerfClassTraceTime is used to measure time for class loading related events.
398 // This class tracks cumulative time and exclusive time for specific event types.
399 // During the execution of one event, other event types (e.g. class loading and
400 // resolution) as well as recursive calls of the same event type could happen.
401 // Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)
402 // (i.e. only one event type) are active at a time even multiple PerfClassTraceTime
403 // instances have been created as multiple events are happening.
404 class PerfClassTraceTime {
405 public:
406 enum {
407 CLASS_LOAD = 0,
408 CLASS_LINK = 1,
409 CLASS_VERIFY = 2,
410 CLASS_CLINIT = 3,
411 DEFINE_CLASS = 4,
412 EVENT_TYPE_COUNT = 5
413 };
414 protected:
415 // _t tracks time from initialization to destruction of this timer instance
416 // including time for all other event types, and recursive calls of this type.
417 // When a timer is called recursively, the elapsedTimer _t would not be used.
418 elapsedTimer _t;
419 PerfLongCounter* _timep;
420 PerfLongCounter* _selftimep;
421 PerfLongCounter* _eventp;
422 // pointer to thread-local recursion counter and timer array
423 // The thread_local timers track cumulative time for specific event types
424 // exclusive of time for other event types, but including recursive calls
425 // of the same type.
426 int* _recursion_counters;
427 elapsedTimer* _timers;
428 int _event_type;
429 int _prev_active_event;
430
431 public:
432
433 inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */
434 PerfLongCounter* selftimep, /* counter incremented with exclusive time */
435 PerfLongCounter* eventp, /* event counter */
436 int* recursion_counters, /* thread-local recursion counter array */
437 elapsedTimer* timers, /* thread-local timer array */
438 int type /* event type */ ) :
439 _timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {
440 initialize();
441 }
442
443 inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */
444 elapsedTimer* timers, /* thread-local timer array */
445 int type /* event type */ ) :
446 _timep(timep), _selftimep(nullptr), _eventp(nullptr), _recursion_counters(nullptr), _timers(timers), _event_type(type) {
447 initialize();
448 }
449
450 ~PerfClassTraceTime();
451 void initialize();
452 };
453
454 #endif // SHARE_CLASSFILE_CLASSLOADER_HPP