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