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