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