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