1 /* 2 * Copyright (c) 1997, 2020, 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/arguments.hpp" 30 #include "runtime/handles.hpp" 31 #include "runtime/perfData.hpp" 32 #include "utilities/exceptions.hpp" 33 #include "utilities/macros.hpp" 34 35 // The VM class loader. 36 #include <sys/stat.h> 37 38 // Name of boot "modules" image 39 #define MODULES_IMAGE_NAME "modules" 40 41 // Class path entry (directory or zip file) 42 43 class JImageFile; 44 class ClassFileStream; 45 class PackageEntry; 46 template <typename T> class GrowableArray; 47 48 class ClassPathEntry : public CHeapObj<mtClass> { 49 private: 50 ClassPathEntry* volatile _next; 51 protected: 52 const char* copy_path(const char*path); 53 public: 54 ClassPathEntry* next() const; 55 virtual ~ClassPathEntry() {} 56 void set_next(ClassPathEntry* next); 57 virtual bool is_modules_image() const { return false; } 58 virtual bool is_jar_file() const { return false; } 59 // Is this entry created from the "Class-path" attribute from a JAR Manifest? 60 virtual bool from_class_path_attr() const { return false; } 61 virtual const char* name() const = 0; 62 virtual JImageFile* jimage() const { return NULL; } 63 virtual void close_jimage() {} 64 // Constructor 65 ClassPathEntry() : _next(NULL) {} 66 // Attempt to locate file_name through this class path entry. 67 // Returns a class file parsing stream if successfull. 68 virtual ClassFileStream* open_stream(const char* name, TRAPS) = 0; 69 // Open the stream for a specific class loader 70 virtual ClassFileStream* open_stream_for_loader(const char* name, ClassLoaderData* loader_data, TRAPS) { 71 return open_stream(name, THREAD); 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(const char* name, TRAPS); 85 }; 86 87 // Type definitions for zip file and zip file entry 88 typedef void* jzfile; 89 typedef struct { 90 char *name; /* entry name */ 91 jlong time; /* modification time */ 92 jlong size; /* size of uncompressed data */ 93 jlong csize; /* size of compressed data (zero if uncompressed) */ 94 jint crc; /* crc of uncompressed data */ 95 char *comment; /* optional zip file comment */ 96 jbyte *extra; /* optional extra data */ 97 jlong pos; /* position of LOC header (if negative) or data */ 98 } jzentry; 99 100 class ClassPathZipEntry: public ClassPathEntry { 101 private: 102 jzfile* _zip; // The zip archive 103 const char* _zip_name; // Name of zip archive 104 bool _from_class_path_attr; // From the "Class-path" attribute of a jar file 105 public: 106 bool is_jar_file() const { return true; } 107 bool from_class_path_attr() const { return _from_class_path_attr; } 108 const char* name() const { return _zip_name; } 109 ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append, bool from_class_path_attr); 110 virtual ~ClassPathZipEntry(); 111 u1* open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS); 112 ClassFileStream* open_stream(const char* name, TRAPS); 113 void contents_do(void f(const char* name, void* context), void* context); 114 }; 115 116 117 // For java image files 118 class ClassPathImageEntry: public ClassPathEntry { 119 private: 120 JImageFile* _jimage; 121 const char* _name; 122 DEBUG_ONLY(static ClassPathImageEntry* _singleton;) 123 public: 124 bool is_modules_image() const; 125 bool is_open() const { return _jimage != NULL; } 126 const char* name() const { return _name == NULL ? "" : _name; } 127 JImageFile* jimage() const { return _jimage; } 128 void close_jimage(); 129 ClassPathImageEntry(JImageFile* jimage, const char* name); 130 virtual ~ClassPathImageEntry(); 131 ClassFileStream* open_stream(const char* name, TRAPS); 132 ClassFileStream* open_stream_for_loader(const char* name, ClassLoaderData* loader_data, TRAPS); 133 }; 134 135 // ModuleClassPathList contains a linked list of ClassPathEntry's 136 // that have been specified for a specific module. Currently, 137 // the only way to specify a module/path pair is via the --patch-module 138 // command line option. 139 class ModuleClassPathList : public CHeapObj<mtClass> { 140 private: 141 Symbol* _module_name; 142 // First and last entries of class path entries for a specific module 143 ClassPathEntry* _module_first_entry; 144 ClassPathEntry* _module_last_entry; 145 public: 146 Symbol* module_name() const { return _module_name; } 147 ClassPathEntry* module_first_entry() const { return _module_first_entry; } 148 ModuleClassPathList(Symbol* module_name); 149 ~ModuleClassPathList(); 150 void add_to_list(ClassPathEntry* new_entry); 151 }; 152 153 class ClassLoader: AllStatic { 154 public: 155 enum ClassLoaderType { 156 BOOT_LOADER = 1, /* boot loader */ 157 PLATFORM_LOADER = 2, /* PlatformClassLoader */ 158 APP_LOADER = 3 /* AppClassLoader */ 159 }; 160 protected: 161 162 // Performance counters 163 static PerfCounter* _perf_accumulated_time; 164 static PerfCounter* _perf_classes_inited; 165 static PerfCounter* _perf_class_init_time; 166 static PerfCounter* _perf_class_init_selftime; 167 static PerfCounter* _perf_classes_verified; 168 static PerfCounter* _perf_class_verify_time; 169 static PerfCounter* _perf_class_verify_selftime; 170 static PerfCounter* _perf_classes_linked; 171 static PerfCounter* _perf_class_link_time; 172 static PerfCounter* _perf_class_link_selftime; 173 static PerfCounter* _perf_sys_class_lookup_time; 174 static PerfCounter* _perf_shared_classload_time; 175 static PerfCounter* _perf_sys_classload_time; 176 static PerfCounter* _perf_app_classload_time; 177 static PerfCounter* _perf_app_classload_selftime; 178 static PerfCounter* _perf_app_classload_count; 179 static PerfCounter* _perf_define_appclasses; 180 static PerfCounter* _perf_define_appclass_time; 181 static PerfCounter* _perf_define_appclass_selftime; 182 static PerfCounter* _perf_app_classfile_bytes_read; 183 static PerfCounter* _perf_sys_classfile_bytes_read; 184 185 static PerfCounter* _sync_systemLoaderLockContentionRate; 186 static PerfCounter* _sync_nonSystemLoaderLockContentionRate; 187 static PerfCounter* _sync_JVMFindLoadedClassLockFreeCounter; 188 static PerfCounter* _sync_JVMDefineClassLockFreeCounter; 189 static PerfCounter* _sync_JNIDefineClassLockFreeCounter; 190 191 static PerfCounter* _unsafe_defineClassCallCounter; 192 193 // The boot class path consists of 3 ordered pieces: 194 // 1. the module/path pairs specified to --patch-module 195 // --patch-module=<module>=<file>(<pathsep><file>)* 196 // 2. the base piece 197 // [jimage | build with exploded modules] 198 // 3. boot loader append path 199 // [-Xbootclasspath/a]; [jvmti appended entries] 200 // 201 // The boot loader must obey this order when attempting 202 // to load a class. 203 204 // 1. Contains the module/path pairs specified to --patch-module 205 static GrowableArray<ModuleClassPathList*>* _patch_mod_entries; 206 207 // 2. the base piece 208 // Contains the ClassPathEntry of the modular java runtime image. 209 // If no java runtime image is present, this indicates a 210 // build with exploded modules is being used instead. 211 static ClassPathEntry* _jrt_entry; 212 static GrowableArray<ModuleClassPathList*>* _exploded_entries; 213 enum { EXPLODED_ENTRY_SIZE = 80 }; // Initial number of exploded modules 214 215 // 3. the boot loader's append path 216 // [-Xbootclasspath/a]; [jvmti appended entries] 217 // Note: boot loader append path does not support named modules. 218 static ClassPathEntry* _first_append_entry; 219 // Last entry in linked list of appended ClassPathEntry instances 220 static ClassPathEntry* _last_append_entry; 221 222 // Info used by CDS 223 CDS_ONLY(static ClassPathEntry* _app_classpath_entries;) 224 CDS_ONLY(static ClassPathEntry* _last_app_classpath_entry;) 225 CDS_ONLY(static ClassPathEntry* _module_path_entries;) 226 CDS_ONLY(static ClassPathEntry* _last_module_path_entry;) 227 CDS_ONLY(static void setup_app_search_path(const char* class_path);) 228 CDS_ONLY(static void setup_module_search_path(const char* path, TRAPS);) 229 static void add_to_app_classpath_entries(const char* path, 230 ClassPathEntry* entry, 231 bool check_for_duplicates); 232 CDS_ONLY(static void add_to_module_path_entries(const char* path, 233 ClassPathEntry* entry);) 234 public: 235 CDS_ONLY(static ClassPathEntry* app_classpath_entries() {return _app_classpath_entries;}) 236 CDS_ONLY(static ClassPathEntry* module_path_entries() {return _module_path_entries;}) 237 238 static bool has_bootclasspath_append() { return _first_append_entry != NULL; } 239 240 protected: 241 // Initialization: 242 // - setup the boot loader's system class path 243 // - setup the boot loader's patch mod entries, if present 244 // - create the ModuleEntry for java.base 245 static void setup_bootstrap_search_path(); 246 static void setup_boot_search_path(const char *class_path); 247 static void setup_patch_mod_entries(); 248 static void create_javabase(); 249 250 static void* dll_lookup(void* lib, const char* name, const char* path); 251 static void load_java_library(); 252 static void load_zip_library(); 253 static void load_jimage_library(); 254 255 private: 256 static int _libzip_loaded; // used to sync loading zip. 257 static void release_load_zip_library(); 258 static inline void load_zip_library_if_needed(); 259 260 public: 261 static ClassPathEntry* create_class_path_entry(const char *path, const struct stat* st, 262 bool throw_exception, 263 bool is_boot_append, 264 bool from_class_path_attr, TRAPS); 265 266 // Canonicalizes path names, so strcmp will work properly. This is mainly 267 // to avoid confusing the zip library 268 static bool get_canonical_path(const char* orig, char* out, int len); 269 static const char* file_name_for_class_name(const char* class_name, 270 int class_name_len); 271 static PackageEntry* get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data); 272 static int crc32(int crc, const char* buf, int len); 273 static bool update_class_path_entry_list(const char *path, 274 bool check_for_duplicates, 275 bool is_boot_append, 276 bool from_class_path_attr, 277 bool throw_exception=true); 278 CDS_ONLY(static void update_module_path_entry_list(const char *path, TRAPS);) 279 static void print_bootclasspath(); 280 281 // Timing 282 static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; } 283 static PerfCounter* perf_classes_inited() { return _perf_classes_inited; } 284 static PerfCounter* perf_class_init_time() { return _perf_class_init_time; } 285 static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; } 286 static PerfCounter* perf_classes_verified() { return _perf_classes_verified; } 287 static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; } 288 static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; } 289 static PerfCounter* perf_classes_linked() { return _perf_classes_linked; } 290 static PerfCounter* perf_class_link_time() { return _perf_class_link_time; } 291 static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; } 292 static PerfCounter* perf_sys_class_lookup_time() { return _perf_sys_class_lookup_time; } 293 static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; } 294 static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; } 295 static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; } 296 static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; } 297 static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; } 298 static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; } 299 static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; } 300 static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; } 301 static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; } 302 static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; } 303 304 // Record how often system loader lock object is contended 305 static PerfCounter* sync_systemLoaderLockContentionRate() { 306 return _sync_systemLoaderLockContentionRate; 307 } 308 309 // Record how often non system loader lock object is contended 310 static PerfCounter* sync_nonSystemLoaderLockContentionRate() { 311 return _sync_nonSystemLoaderLockContentionRate; 312 } 313 314 // Record how many calls to JVM_FindLoadedClass w/o holding a lock 315 static PerfCounter* sync_JVMFindLoadedClassLockFreeCounter() { 316 return _sync_JVMFindLoadedClassLockFreeCounter; 317 } 318 319 // Record how many calls to JVM_DefineClass w/o holding a lock 320 static PerfCounter* sync_JVMDefineClassLockFreeCounter() { 321 return _sync_JVMDefineClassLockFreeCounter; 322 } 323 324 // Record how many calls to jni_DefineClass w/o holding a lock 325 static PerfCounter* sync_JNIDefineClassLockFreeCounter() { 326 return _sync_JNIDefineClassLockFreeCounter; 327 } 328 329 // Record how many calls to Unsafe_DefineClass 330 static PerfCounter* unsafe_defineClassCallCounter() { 331 return _unsafe_defineClassCallCounter; 332 } 333 334 // Modular java runtime image is present vs. a build with exploded modules 335 static bool has_jrt_entry() { return (_jrt_entry != NULL); } 336 static ClassPathEntry* get_jrt_entry() { return _jrt_entry; } 337 static void close_jrt_image(); 338 339 // Add a module's exploded directory to the boot loader's exploded module build list 340 static void add_to_exploded_build_list(Symbol* module_name, TRAPS); 341 342 // Attempt load of individual class from either the patched or exploded modules build lists 343 static ClassFileStream* search_module_entries(const GrowableArray<ModuleClassPathList*>* const module_list, 344 const char* const class_name, 345 const char* const file_name, TRAPS); 346 347 // Load individual .class file 348 static InstanceKlass* load_class(Symbol* class_name, bool search_append_only, TRAPS); 349 350 // If the specified package has been loaded by the system, then returns 351 // the name of the directory or ZIP file that the package was loaded from. 352 // Returns null if the package was not loaded. 353 // Note: The specified name can either be the name of a class or package. 354 // If a package name is specified, then it must be "/"-separator and also 355 // end with a trailing "/". 356 static oop get_system_package(const char* name, TRAPS); 357 358 // Returns an array of Java strings representing all of the currently 359 // loaded system packages. 360 // Note: The package names returned are "/"-separated and end with a 361 // trailing "/". 362 static objArrayOop get_system_packages(TRAPS); 363 364 // Initialization 365 static void initialize(); 366 static void classLoader_init2(TRAPS); 367 CDS_ONLY(static void initialize_shared_path();) 368 CDS_ONLY(static void initialize_module_path(TRAPS);) 369 370 static int compute_Object_vtable(); 371 372 static ClassPathEntry* classpath_entry(int n); 373 374 static bool is_in_patch_mod_entries(Symbol* module_name); 375 376 #if INCLUDE_CDS 377 // Sharing dump and restore 378 379 // Helper function used by CDS code to get the number of boot classpath 380 // entries during shared classpath setup time. 381 static int num_boot_classpath_entries(); 382 383 static ClassPathEntry* get_next_boot_classpath_entry(ClassPathEntry* e); 384 385 // Helper function used by CDS code to get the number of app classpath 386 // entries during shared classpath setup time. 387 static int num_app_classpath_entries(); 388 389 // Helper function used by CDS code to get the number of module path 390 // entries during shared classpath setup time. 391 static int num_module_path_entries(); 392 static void exit_with_path_failure(const char* error, const char* message); 393 static char* skip_uri_protocol(char* source); 394 static void record_result(InstanceKlass* ik, const ClassFileStream* stream, TRAPS); 395 #endif 396 397 static char* lookup_vm_options(); 398 399 static JImageLocationRef jimage_find_resource(JImageFile* jf, const char* module_name, 400 const char* file_name, jlong &size); 401 402 static void trace_class_path(const char* msg, const char* name = NULL); 403 404 // VM monitoring and management support 405 static jlong classloader_time_ms(); 406 static jlong class_method_total_size(); 407 static jlong class_init_count(); 408 static jlong class_init_time_ms(); 409 static jlong class_verify_time_ms(); 410 static jlong class_link_count(); 411 static jlong class_link_time_ms(); 412 413 // indicates if class path already contains a entry (exact match by name) 414 static bool contains_append_entry(const char* name); 415 416 // adds a class path to the boot append entries 417 static void add_to_boot_append_entries(ClassPathEntry* new_entry); 418 419 // creates a class path zip entry (returns NULL if JAR file cannot be opened) 420 static ClassPathZipEntry* create_class_path_zip_entry(const char *apath, bool is_boot_append); 421 422 static bool string_ends_with(const char* str, const char* str_to_find); 423 424 // Extract package name from a fully qualified class name 425 // *bad_class_name is set to true if there's a problem with parsing class_name, to 426 // distinguish from a class_name with no package name, as both cases have a NULL return value 427 static Symbol* package_from_class_name(const Symbol* class_name, bool* bad_class_name = NULL); 428 429 // Debugging 430 static void verify() PRODUCT_RETURN; 431 }; 432 433 // PerfClassTraceTime is used to measure time for class loading related events. 434 // This class tracks cumulative time and exclusive time for specific event types. 435 // During the execution of one event, other event types (e.g. class loading and 436 // resolution) as well as recursive calls of the same event type could happen. 437 // Only one elapsed timer (cumulative) and one thread-local self timer (exclusive) 438 // (i.e. only one event type) are active at a time even multiple PerfClassTraceTime 439 // instances have been created as multiple events are happening. 440 class PerfClassTraceTime { 441 public: 442 enum { 443 CLASS_LOAD = 0, 444 CLASS_LINK = 1, 445 CLASS_VERIFY = 2, 446 CLASS_CLINIT = 3, 447 DEFINE_CLASS = 4, 448 EVENT_TYPE_COUNT = 5 449 }; 450 protected: 451 // _t tracks time from initialization to destruction of this timer instance 452 // including time for all other event types, and recursive calls of this type. 453 // When a timer is called recursively, the elapsedTimer _t would not be used. 454 elapsedTimer _t; 455 PerfLongCounter* _timep; 456 PerfLongCounter* _selftimep; 457 PerfLongCounter* _eventp; 458 // pointer to thread-local recursion counter and timer array 459 // The thread_local timers track cumulative time for specific event types 460 // exclusive of time for other event types, but including recursive calls 461 // of the same type. 462 int* _recursion_counters; 463 elapsedTimer* _timers; 464 int _event_type; 465 int _prev_active_event; 466 467 public: 468 469 inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */ 470 PerfLongCounter* selftimep, /* counter incremented with exclusive time */ 471 PerfLongCounter* eventp, /* event counter */ 472 int* recursion_counters, /* thread-local recursion counter array */ 473 elapsedTimer* timers, /* thread-local timer array */ 474 int type /* event type */ ) : 475 _timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) { 476 initialize(); 477 } 478 479 inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */ 480 elapsedTimer* timers, /* thread-local timer array */ 481 int type /* event type */ ) : 482 _timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) { 483 initialize(); 484 } 485 486 ~PerfClassTraceTime(); 487 void initialize(); 488 }; 489 490 #endif // SHARE_CLASSFILE_CLASSLOADER_HPP