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