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