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 #include "precompiled.hpp" 26 #include "classfile/classFileStream.hpp" 27 #include "classfile/classLoader.hpp" 28 #include "classfile/classLoaderData.inline.hpp" 29 #include "classfile/classLoaderExt.hpp" 30 #include "classfile/javaClasses.hpp" 31 #include "classfile/jimage.hpp" 32 #include "classfile/moduleEntry.hpp" 33 #include "classfile/modules.hpp" 34 #include "classfile/packageEntry.hpp" 35 #include "classfile/klassFactory.hpp" 36 #include "classfile/systemDictionary.hpp" 37 #include "classfile/vmSymbols.hpp" 38 #include "compiler/compileBroker.hpp" 39 #include "gc/shared/collectedHeap.inline.hpp" 40 #include "gc/shared/generation.hpp" 41 #include "interpreter/bytecodeStream.hpp" 42 #include "interpreter/oopMapCache.hpp" 43 #include "logging/logTag.hpp" 44 #include "memory/allocation.inline.hpp" 45 #include "memory/filemap.hpp" 46 #include "memory/oopFactory.hpp" 47 #include "memory/resourceArea.hpp" 48 #include "memory/universe.inline.hpp" 49 #include "oops/instanceKlass.hpp" 50 #include "oops/instanceRefKlass.hpp" 51 #include "oops/objArrayOop.inline.hpp" 52 #include "oops/oop.inline.hpp" 53 #include "oops/symbol.hpp" 54 #include "prims/jvm_misc.hpp" 55 #include "runtime/arguments.hpp" 56 #include "runtime/compilationPolicy.hpp" 57 #include "runtime/fprofiler.hpp" 58 #include "runtime/handles.hpp" 59 #include "runtime/handles.inline.hpp" 60 #include "runtime/init.hpp" 61 #include "runtime/interfaceSupport.hpp" 62 #include "runtime/java.hpp" 63 #include "runtime/javaCalls.hpp" 64 #include "runtime/os.hpp" 65 #include "runtime/threadCritical.hpp" 66 #include "runtime/timer.hpp" 67 #include "runtime/vm_version.hpp" 68 #include "services/management.hpp" 69 #include "services/threadService.hpp" 70 #include "utilities/events.hpp" 71 #include "utilities/hashtable.inline.hpp" 72 #include "utilities/macros.hpp" 73 #if INCLUDE_CDS 74 #include "classfile/sharedClassUtil.hpp" 75 #include "classfile/sharedPathsMiscInfo.hpp" 76 #endif 77 78 // Entry points in zip.dll for loading zip/jar file entries 79 80 typedef void * * (JNICALL *ZipOpen_t)(const char *name, char **pmsg); 81 typedef void (JNICALL *ZipClose_t)(jzfile *zip); 82 typedef jzentry* (JNICALL *FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen); 83 typedef jboolean (JNICALL *ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf); 84 typedef jboolean (JNICALL *ReadMappedEntry_t)(jzfile *zip, jzentry *entry, unsigned char **buf, char *namebuf); 85 typedef jzentry* (JNICALL *GetNextEntry_t)(jzfile *zip, jint n); 86 typedef jboolean (JNICALL *ZipInflateFully_t)(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg); 87 typedef jint (JNICALL *Crc32_t)(jint crc, const jbyte *buf, jint len); 88 89 static ZipOpen_t ZipOpen = NULL; 90 static ZipClose_t ZipClose = NULL; 91 static FindEntry_t FindEntry = NULL; 92 static ReadEntry_t ReadEntry = NULL; 93 static ReadMappedEntry_t ReadMappedEntry = NULL; 94 static GetNextEntry_t GetNextEntry = NULL; 95 static canonicalize_fn_t CanonicalizeEntry = NULL; 96 static ZipInflateFully_t ZipInflateFully = NULL; 97 static Crc32_t Crc32 = NULL; 98 99 // Entry points for jimage.dll for loading jimage file entries 100 101 static JImageOpen_t JImageOpen = NULL; 102 static JImageClose_t JImageClose = NULL; 103 static JImagePackageToModule_t JImagePackageToModule = NULL; 104 static JImageFindResource_t JImageFindResource = NULL; 105 static JImageGetResource_t JImageGetResource = NULL; 106 static JImageResourceIterator_t JImageResourceIterator = NULL; 107 static JImage_ResourcePath_t JImageResourcePath = NULL; 108 109 // Globals 110 111 PerfCounter* ClassLoader::_perf_accumulated_time = NULL; 112 PerfCounter* ClassLoader::_perf_classes_inited = NULL; 113 PerfCounter* ClassLoader::_perf_class_init_time = NULL; 114 PerfCounter* ClassLoader::_perf_class_init_selftime = NULL; 115 PerfCounter* ClassLoader::_perf_classes_verified = NULL; 116 PerfCounter* ClassLoader::_perf_class_verify_time = NULL; 117 PerfCounter* ClassLoader::_perf_class_verify_selftime = NULL; 118 PerfCounter* ClassLoader::_perf_classes_linked = NULL; 119 PerfCounter* ClassLoader::_perf_class_link_time = NULL; 120 PerfCounter* ClassLoader::_perf_class_link_selftime = NULL; 121 PerfCounter* ClassLoader::_perf_class_parse_time = NULL; 122 PerfCounter* ClassLoader::_perf_class_parse_selftime = NULL; 123 PerfCounter* ClassLoader::_perf_sys_class_lookup_time = NULL; 124 PerfCounter* ClassLoader::_perf_shared_classload_time = NULL; 125 PerfCounter* ClassLoader::_perf_sys_classload_time = NULL; 126 PerfCounter* ClassLoader::_perf_app_classload_time = NULL; 127 PerfCounter* ClassLoader::_perf_app_classload_selftime = NULL; 128 PerfCounter* ClassLoader::_perf_app_classload_count = NULL; 129 PerfCounter* ClassLoader::_perf_define_appclasses = NULL; 130 PerfCounter* ClassLoader::_perf_define_appclass_time = NULL; 131 PerfCounter* ClassLoader::_perf_define_appclass_selftime = NULL; 132 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = NULL; 133 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL; 134 PerfCounter* ClassLoader::_sync_systemLoaderLockContentionRate = NULL; 135 PerfCounter* ClassLoader::_sync_nonSystemLoaderLockContentionRate = NULL; 136 PerfCounter* ClassLoader::_sync_JVMFindLoadedClassLockFreeCounter = NULL; 137 PerfCounter* ClassLoader::_sync_JVMDefineClassLockFreeCounter = NULL; 138 PerfCounter* ClassLoader::_sync_JNIDefineClassLockFreeCounter = NULL; 139 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL; 140 PerfCounter* ClassLoader::_isUnsyncloadClass = NULL; 141 PerfCounter* ClassLoader::_load_instance_class_failCounter = NULL; 142 143 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL; 144 ClassPathEntry* ClassLoader::_first_entry = NULL; 145 ClassPathEntry* ClassLoader::_last_entry = NULL; 146 int ClassLoader::_num_entries = 0; 147 ClassPathEntry* ClassLoader::_first_append_entry = NULL; 148 bool ClassLoader::_has_jimage = false; 149 #if INCLUDE_CDS 150 GrowableArray<char*>* ClassLoader::_boot_modules_array = NULL; 151 GrowableArray<char*>* ClassLoader::_platform_modules_array = NULL; 152 SharedPathsMiscInfo* ClassLoader::_shared_paths_misc_info = NULL; 153 #endif 154 155 // helper routines 156 bool string_starts_with(const char* str, const char* str_to_find) { 157 size_t str_len = strlen(str); 158 size_t str_to_find_len = strlen(str_to_find); 159 if (str_to_find_len > str_len) { 160 return false; 161 } 162 return (strncmp(str, str_to_find, str_to_find_len) == 0); 163 } 164 165 static const char* get_jimage_version_string() { 166 static char version_string[10] = ""; 167 if (version_string[0] == '\0') { 168 jio_snprintf(version_string, sizeof(version_string), "%d.%d", 169 Abstract_VM_Version::vm_major_version(), Abstract_VM_Version::vm_minor_version()); 170 } 171 return (const char*)version_string; 172 } 173 174 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) { 175 size_t str_len = strlen(str); 176 size_t str_to_find_len = strlen(str_to_find); 177 if (str_to_find_len > str_len) { 178 return false; 179 } 180 return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0); 181 } 182 183 // Used to obtain the package name from a fully qualified class name. 184 // It is the responsibility of the caller to establish a ResourceMark. 185 const char* ClassLoader::package_from_name(const char* const class_name, bool* bad_class_name) { 186 if (class_name == NULL) { 187 if (bad_class_name != NULL) { 188 *bad_class_name = true; 189 } 190 return NULL; 191 } 192 193 if (bad_class_name != NULL) { 194 *bad_class_name = false; 195 } 196 197 const char* const last_slash = strrchr(class_name, '/'); 198 if (last_slash == NULL) { 199 // No package name 200 return NULL; 201 } 202 203 char* class_name_ptr = (char*) class_name; 204 // Skip over '['s 205 if (*class_name_ptr == '[') { 206 do { 207 class_name_ptr++; 208 } while (*class_name_ptr == '['); 209 210 // Fully qualified class names should not contain a 'L'. 211 // Set bad_class_name to true to indicate that the package name 212 // could not be obtained due to an error condition. 213 // In this situation, is_same_class_package returns false. 214 if (*class_name_ptr == 'L') { 215 if (bad_class_name != NULL) { 216 *bad_class_name = true; 217 } 218 return NULL; 219 } 220 } 221 222 int length = last_slash - class_name_ptr; 223 224 // A class name could have just the slash character in the name. 225 if (length <= 0) { 226 // No package name 227 if (bad_class_name != NULL) { 228 *bad_class_name = true; 229 } 230 return NULL; 231 } 232 233 // drop name after last slash (including slash) 234 // Ex., "java/lang/String.class" => "java/lang" 235 char* pkg_name = NEW_RESOURCE_ARRAY(char, length + 1); 236 strncpy(pkg_name, class_name_ptr, length); 237 *(pkg_name+length) = '\0'; 238 239 return (const char *)pkg_name; 240 } 241 242 // Given a fully qualified class name, find its defining package in the class loader's 243 // package entry table. 244 static PackageEntry* get_package_entry(const char* class_name, ClassLoaderData* loader_data, TRAPS) { 245 ResourceMark rm(THREAD); 246 const char *pkg_name = ClassLoader::package_from_name(class_name); 247 if (pkg_name == NULL) { 248 return NULL; 249 } 250 PackageEntryTable* pkgEntryTable = loader_data->packages(); 251 TempNewSymbol pkg_symbol = SymbolTable::new_symbol(pkg_name, CHECK_NULL); 252 return pkgEntryTable->lookup_only(pkg_symbol); 253 } 254 255 ClassPathDirEntry::ClassPathDirEntry(const char* dir) : ClassPathEntry() { 256 char* copy = NEW_C_HEAP_ARRAY(char, strlen(dir)+1, mtClass); 257 strcpy(copy, dir); 258 _dir = copy; 259 } 260 261 262 ClassFileStream* ClassPathDirEntry::open_stream(const char* name, TRAPS) { 263 // construct full path name 264 char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, JVM_MAXPATHLEN); 265 if (jio_snprintf(path, JVM_MAXPATHLEN, "%s%s%s", _dir, os::file_separator(), name) == -1) { 266 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); 267 return NULL; 268 } 269 // check if file exists 270 struct stat st; 271 if (os::stat(path, &st) == 0) { 272 #if INCLUDE_CDS 273 if (DumpSharedSpaces) { 274 // We have already check in ClassLoader::check_shared_classpath() that the directory is empty, so 275 // we should never find a file underneath it -- unless user has added a new file while we are running 276 // the dump, in which case let's quit! 277 ShouldNotReachHere(); 278 } 279 #endif 280 // found file, open it 281 int file_handle = os::open(path, 0, 0); 282 if (file_handle != -1) { 283 // read contents into resource array 284 u1* buffer = NEW_RESOURCE_ARRAY(u1, st.st_size); 285 size_t num_read = os::read(file_handle, (char*) buffer, st.st_size); 286 // close file 287 os::close(file_handle); 288 // construct ClassFileStream 289 if (num_read == (size_t)st.st_size) { 290 if (UsePerfData) { 291 ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read); 292 } 293 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); 294 // Resource allocated 295 return new ClassFileStream(buffer, 296 st.st_size, 297 _dir, 298 ClassFileStream::verify); 299 } 300 } 301 } 302 FREE_RESOURCE_ARRAY(char, path, JVM_MAXPATHLEN); 303 return NULL; 304 } 305 306 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append) : ClassPathEntry() { 307 _zip = zip; 308 char *copy = NEW_C_HEAP_ARRAY(char, strlen(zip_name)+1, mtClass); 309 strcpy(copy, zip_name); 310 _zip_name = copy; 311 _is_boot_append = is_boot_append; 312 _multi_versioned = _unknown; 313 } 314 315 ClassPathZipEntry::~ClassPathZipEntry() { 316 if (ZipClose != NULL) { 317 (*ZipClose)(_zip); 318 } 319 FREE_C_HEAP_ARRAY(char, _zip_name); 320 } 321 322 u1* ClassPathZipEntry::open_entry(const char* name, jint* filesize, bool nul_terminate, TRAPS) { 323 // enable call to C land 324 JavaThread* thread = JavaThread::current(); 325 ThreadToNativeFromVM ttn(thread); 326 // check whether zip archive contains name 327 jint name_len; 328 jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len); 329 if (entry == NULL) return NULL; 330 u1* buffer; 331 char name_buf[128]; 332 char* filename; 333 if (name_len < 128) { 334 filename = name_buf; 335 } else { 336 filename = NEW_RESOURCE_ARRAY(char, name_len + 1); 337 } 338 339 // file found, get pointer to the entry in mmapped jar file. 340 if (ReadMappedEntry == NULL || 341 !(*ReadMappedEntry)(_zip, entry, &buffer, filename)) { 342 // mmapped access not available, perhaps due to compression, 343 // read contents into resource array 344 int size = (*filesize) + ((nul_terminate) ? 1 : 0); 345 buffer = NEW_RESOURCE_ARRAY(u1, size); 346 if (!(*ReadEntry)(_zip, entry, buffer, filename)) return NULL; 347 } 348 349 // return result 350 if (nul_terminate) { 351 buffer[*filesize] = 0; 352 } 353 return buffer; 354 } 355 356 #if INCLUDE_CDS 357 u1* ClassPathZipEntry::open_versioned_entry(const char* name, jint* filesize, TRAPS) { 358 u1* buffer = NULL; 359 if (DumpSharedSpaces && !_is_boot_append) { 360 // We presume default is multi-release enabled 361 const char* multi_ver = Arguments::get_property("jdk.util.jar.enableMultiRelease"); 362 const char* verstr = Arguments::get_property("jdk.util.jar.version"); 363 bool is_multi_ver = (multi_ver == NULL || 364 strcmp(multi_ver, "true") == 0 || 365 strcmp(multi_ver, "force") == 0) && 366 is_multiple_versioned(THREAD); 367 // command line version setting 368 int version = 0; 369 const int base_version = 8; // JDK8 370 int cur_ver = JDK_Version::current().major_version(); 371 if (verstr != NULL) { 372 version = atoi(verstr); 373 if (version < base_version || version > cur_ver) { 374 is_multi_ver = false; 375 // print out warning, do not use assertion here since it will continue to look 376 // for proper version. 377 warning("JDK%d is not supported in multiple version jars", version); 378 } 379 } 380 381 if (is_multi_ver) { 382 int n; 383 char* entry_name = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, JVM_MAXPATHLEN); 384 if (version > 0) { 385 n = jio_snprintf(entry_name, JVM_MAXPATHLEN, "META-INF/versions/%d/%s", version, name); 386 entry_name[n] = '\0'; 387 buffer = open_entry((const char*)entry_name, filesize, false, CHECK_NULL); 388 if (buffer == NULL) { 389 warning("Could not find %s in %s, try to find highest version instead", entry_name, _zip_name); 390 } 391 } 392 if (buffer == NULL) { 393 for (int i = cur_ver; i >= base_version; i--) { 394 n = jio_snprintf(entry_name, JVM_MAXPATHLEN, "META-INF/versions/%d/%s", i, name); 395 entry_name[n] = '\0'; 396 buffer = open_entry((const char*)entry_name, filesize, false, CHECK_NULL); 397 if (buffer != NULL) { 398 break; 399 } 400 } 401 } 402 FREE_RESOURCE_ARRAY(char, entry_name, JVM_MAXPATHLEN); 403 } 404 } 405 return buffer; 406 } 407 408 bool ClassPathZipEntry::is_multiple_versioned(TRAPS) { 409 assert(DumpSharedSpaces, "called only at dump time"); 410 if (_multi_versioned != _unknown) { 411 return (_multi_versioned == _yes) ? true : false; 412 } 413 jint size; 414 char* buffer = (char*)open_entry("META-INF/MANIFEST.MF", &size, true, CHECK_false); 415 if (buffer != NULL) { 416 char* p = buffer; 417 for ( ; *p; ++p) *p = tolower(*p); 418 if (strstr(buffer, "multi-release: true") != NULL) { 419 _multi_versioned = _yes; 420 return true; 421 } 422 } 423 _multi_versioned = _no; 424 return false; 425 } 426 #endif // INCLUDE_CDS 427 428 ClassFileStream* ClassPathZipEntry::open_stream(const char* name, TRAPS) { 429 jint filesize; 430 u1* buffer = open_versioned_entry(name, &filesize, CHECK_NULL); 431 if (buffer == NULL) { 432 buffer = open_entry(name, &filesize, false, CHECK_NULL); 433 if (buffer == NULL) { 434 return NULL; 435 } 436 } 437 if (UsePerfData) { 438 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize); 439 } 440 // Resource allocated 441 return new ClassFileStream(buffer, 442 filesize, 443 _zip_name, 444 ClassFileStream::verify); 445 } 446 447 // invoke function for each entry in the zip file 448 void ClassPathZipEntry::contents_do(void f(const char* name, void* context), void* context) { 449 JavaThread* thread = JavaThread::current(); 450 HandleMark handle_mark(thread); 451 ThreadToNativeFromVM ttn(thread); 452 for (int n = 0; ; n++) { 453 jzentry * ze = ((*GetNextEntry)(_zip, n)); 454 if (ze == NULL) break; 455 (*f)(ze->name, context); 456 } 457 } 458 459 ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) : 460 ClassPathEntry(), 461 _jimage(jimage) { 462 guarantee(jimage != NULL, "jimage file is null"); 463 guarantee(name != NULL, "jimage file name is null"); 464 size_t len = strlen(name) + 1; 465 _name = NEW_C_HEAP_ARRAY(const char, len, mtClass); 466 strncpy((char *)_name, name, len); 467 } 468 469 ClassPathImageEntry::~ClassPathImageEntry() { 470 if (_name != NULL) { 471 FREE_C_HEAP_ARRAY(const char, _name); 472 _name = NULL; 473 } 474 if (_jimage != NULL) { 475 (*JImageClose)(_jimage); 476 _jimage = NULL; 477 } 478 } 479 480 // For a class in a named module, look it up in the jimage file using this syntax: 481 // /<module-name>/<package-name>/<base-class> 482 // 483 // Assumptions: 484 // 1. There are no unnamed modules in the jimage file. 485 // 2. A package is in at most one module in the jimage file. 486 // 487 ClassFileStream* ClassPathImageEntry::open_stream(const char* name, TRAPS) { 488 jlong size; 489 JImageLocationRef location = (*JImageFindResource)(_jimage, "", get_jimage_version_string(), name, &size); 490 491 if (location == 0) { 492 ResourceMark rm; 493 const char* pkg_name = ClassLoader::package_from_name(name); 494 495 if (pkg_name != NULL) { 496 if (!Universe::is_module_initialized()) { 497 location = (*JImageFindResource)(_jimage, "java.base", get_jimage_version_string(), name, &size); 498 #if INCLUDE_CDS 499 // CDS uses the boot class loader to load classes whose packages are in 500 // modules defined for other class loaders. So, for now, get their module 501 // names from the "modules" jimage file. 502 if (DumpSharedSpaces && location == 0) { 503 const char* module_name = (*JImagePackageToModule)(_jimage, pkg_name); 504 if (module_name != NULL) { 505 location = (*JImageFindResource)(_jimage, module_name, get_jimage_version_string(), name, &size); 506 } 507 } 508 #endif 509 510 } else { 511 PackageEntry* package_entry = get_package_entry(name, ClassLoaderData::the_null_class_loader_data(), THREAD); 512 if (package_entry != NULL) { 513 ResourceMark rm; 514 // Get the module name 515 ModuleEntry* module = package_entry->module(); 516 assert(module != NULL, "Boot classLoader package missing module"); 517 assert(module->is_named(), "Boot classLoader package is in unnamed module"); 518 const char* module_name = module->name()->as_C_string(); 519 if (module_name != NULL) { 520 location = (*JImageFindResource)(_jimage, module_name, get_jimage_version_string(), name, &size); 521 } 522 } 523 } 524 } 525 } 526 if (location != 0) { 527 if (UsePerfData) { 528 ClassLoader::perf_sys_classfile_bytes_read()->inc(size); 529 } 530 char* data = NEW_RESOURCE_ARRAY(char, size); 531 (*JImageGetResource)(_jimage, location, data, size); 532 // Resource allocated 533 return new ClassFileStream((u1*)data, 534 (int)size, 535 _name, 536 ClassFileStream::verify); 537 } 538 539 return NULL; 540 } 541 542 #ifndef PRODUCT 543 bool ctw_visitor(JImageFile* jimage, 544 const char* module_name, const char* version, const char* package, 545 const char* name, const char* extension, void* arg) { 546 if (strcmp(extension, "class") == 0) { 547 Thread* THREAD = Thread::current(); 548 ResourceMark rm(THREAD); 549 char* path = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, JIMAGE_MAX_PATH); 550 jio_snprintf(path, JIMAGE_MAX_PATH - 1, "%s/%s.class", package, name); 551 ClassLoader::compile_the_world_in(path, *(Handle*)arg, THREAD); 552 return !HAS_PENDING_EXCEPTION; 553 } 554 return true; 555 } 556 557 void ClassPathImageEntry::compile_the_world(Handle loader, TRAPS) { 558 tty->print_cr("CompileTheWorld : Compiling all classes in %s", name()); 559 tty->cr(); 560 (*JImageResourceIterator)(_jimage, (JImageResourceVisitor_t)ctw_visitor, (void *)&loader); 561 if (HAS_PENDING_EXCEPTION) { 562 if (PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) { 563 CLEAR_PENDING_EXCEPTION; 564 tty->print_cr("\nCompileTheWorld : Ran out of memory\n"); 565 tty->print_cr("Increase class metadata storage if a limit was set"); 566 } else { 567 tty->print_cr("\nCompileTheWorld : Unexpected exception occurred\n"); 568 } 569 } 570 } 571 #endif 572 573 bool ClassPathImageEntry::is_jrt() { 574 return ClassLoader::is_jrt(name()); 575 } 576 577 #if INCLUDE_CDS 578 void ClassLoader::exit_with_path_failure(const char* error, const char* message) { 579 assert(DumpSharedSpaces, "only called at dump time"); 580 tty->print_cr("Hint: enable -Xlog:class+path=info to diagnose the failure"); 581 vm_exit_during_initialization(error, message); 582 } 583 #endif 584 585 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) { 586 _module_name = module_name; 587 _module_first_entry = NULL; 588 _module_last_entry = NULL; 589 } 590 591 ModuleClassPathList::~ModuleClassPathList() { 592 // Clean out each ClassPathEntry on list 593 ClassPathEntry* e = _module_first_entry; 594 while (e != NULL) { 595 ClassPathEntry* next_entry = e->next(); 596 delete e; 597 e = next_entry; 598 } 599 } 600 601 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) { 602 if (new_entry != NULL) { 603 if (_module_last_entry == NULL) { 604 _module_first_entry = _module_last_entry = new_entry; 605 } else { 606 _module_last_entry->set_next(new_entry); 607 _module_last_entry = new_entry; 608 } 609 } 610 } 611 612 void ClassLoader::trace_class_path(const char* msg, const char* name) { 613 if (log_is_enabled(Info, class, path)) { 614 ResourceMark rm; 615 outputStream* out = Log(class, path)::info_stream(); 616 if (msg) { 617 out->print("%s", msg); 618 } 619 if (name) { 620 if (strlen(name) < 256) { 621 out->print("%s", name); 622 } else { 623 // For very long paths, we need to print each character separately, 624 // as print_cr() has a length limit 625 while (name[0] != '\0') { 626 out->print("%c", name[0]); 627 name++; 628 } 629 } 630 } 631 out->cr(); 632 } 633 } 634 635 #if INCLUDE_CDS 636 void ClassLoader::check_shared_classpath(const char *path) { 637 if (strcmp(path, "") == 0) { 638 exit_with_path_failure("Cannot have empty path in archived classpaths", NULL); 639 } 640 641 struct stat st; 642 if (os::stat(path, &st) == 0) { 643 if ((st.st_mode & S_IFREG) != S_IFREG) { // is directory 644 if (!os::dir_is_empty(path)) { 645 tty->print_cr("Error: non-empty directory '%s'", path); 646 exit_with_path_failure("CDS allows only empty directories in archived classpaths", NULL); 647 } 648 } 649 } 650 } 651 #endif 652 653 void ClassLoader::setup_bootstrap_search_path() { 654 assert(_first_entry == NULL, "should not setup bootstrap class search path twice"); 655 const char* sys_class_path = Arguments::get_sysclasspath(); 656 const char* java_class_path = Arguments::get_appclasspath(); 657 if (PrintSharedArchiveAndExit) { 658 // Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily 659 // the same as the bootcp of the shared archive. 660 } else { 661 trace_class_path("bootstrap loader class path=", sys_class_path); 662 trace_class_path("classpath: ", java_class_path); 663 } 664 #if INCLUDE_CDS 665 if (DumpSharedSpaces) { 666 _shared_paths_misc_info->add_boot_classpath(sys_class_path); 667 } 668 #endif 669 setup_search_path(sys_class_path, true); 670 } 671 672 #if INCLUDE_CDS 673 int ClassLoader::get_shared_paths_misc_info_size() { 674 return _shared_paths_misc_info->get_used_bytes(); 675 } 676 677 void* ClassLoader::get_shared_paths_misc_info() { 678 return _shared_paths_misc_info->buffer(); 679 } 680 681 bool ClassLoader::check_shared_paths_misc_info(void *buf, int size) { 682 SharedPathsMiscInfo* checker = SharedClassUtil::allocate_shared_paths_misc_info((char*)buf, size); 683 bool result = checker->check(); 684 delete checker; 685 return result; 686 } 687 #endif 688 689 // Construct the array of module/path pairs as specified to --patch-module 690 // for the boot loader to search ahead of the jimage, if the class being 691 // loaded is defined to a module that has been specified to --patch-module. 692 void ClassLoader::setup_patch_mod_entries() { 693 Thread* THREAD = Thread::current(); 694 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix(); 695 int num_of_entries = patch_mod_args->length(); 696 697 // Set up the boot loader's patch_mod_entries list 698 _patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, true); 699 700 for (int i = 0; i < num_of_entries; i++) { 701 const char* module_name = (patch_mod_args->at(i))->module_name(); 702 Symbol* const module_sym = SymbolTable::lookup(module_name, (int)strlen(module_name), CHECK); 703 assert(module_sym != NULL, "Failed to obtain Symbol for module name"); 704 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym); 705 706 char* class_path = (patch_mod_args->at(i))->path_string(); 707 int len = (int)strlen(class_path); 708 int end = 0; 709 // Iterate over the module's class path entries 710 for (int start = 0; start < len; start = end) { 711 while (class_path[end] && class_path[end] != os::path_separator()[0]) { 712 end++; 713 } 714 EXCEPTION_MARK; 715 ResourceMark rm(THREAD); 716 char* path = NEW_RESOURCE_ARRAY(char, end - start + 1); 717 strncpy(path, &class_path[start], end - start); 718 path[end - start] = '\0'; 719 720 struct stat st; 721 if (os::stat(path, &st) == 0) { 722 // File or directory found 723 Thread* THREAD = Thread::current(); 724 ClassPathEntry* new_entry = create_class_path_entry(path, &st, false, false, CHECK); 725 // If the path specification is valid, enter it into this module's list 726 if (new_entry != NULL) { 727 module_cpl->add_to_list(new_entry); 728 } 729 } 730 731 while (class_path[end] == os::path_separator()[0]) { 732 end++; 733 } 734 } 735 736 // Record the module into the list of --patch-module entries only if 737 // valid ClassPathEntrys have been created 738 if (module_cpl->module_first_entry() != NULL) { 739 _patch_mod_entries->push(module_cpl); 740 } 741 } 742 } 743 744 void ClassLoader::setup_search_path(const char *class_path, bool bootstrap_search) { 745 int offset = 0; 746 int len = (int)strlen(class_path); 747 int end = 0; 748 bool mark_append_entry = false; 749 750 // Iterate over class path entries 751 for (int start = 0; start < len; start = end) { 752 while (class_path[end] && class_path[end] != os::path_separator()[0]) { 753 end++; 754 } 755 EXCEPTION_MARK; 756 ResourceMark rm(THREAD); 757 mark_append_entry = (mark_append_entry || 758 (bootstrap_search && (start == Arguments::bootclassloader_append_index()))); 759 char* path = NEW_RESOURCE_ARRAY(char, end - start + 1); 760 strncpy(path, &class_path[start], end - start); 761 path[end - start] = '\0'; 762 update_class_path_entry_list(path, false, mark_append_entry, false, bootstrap_search); 763 764 // Check on the state of the boot loader's append path 765 if (mark_append_entry && (_first_append_entry == NULL)) { 766 // Failure to mark the first append entry, most likely 767 // due to a non-existent path. Record the next entry 768 // as the first boot loader append entry. 769 mark_append_entry = true; 770 } else { 771 mark_append_entry = false; 772 } 773 774 #if INCLUDE_CDS 775 if (DumpSharedSpaces) { 776 check_shared_classpath(path); 777 } 778 #endif 779 while (class_path[end] == os::path_separator()[0]) { 780 end++; 781 } 782 } 783 } 784 785 ClassPathEntry* ClassLoader::create_class_path_entry(const char *path, const struct stat* st, 786 bool throw_exception, 787 bool is_boot_append, TRAPS) { 788 JavaThread* thread = JavaThread::current(); 789 ClassPathEntry* new_entry = NULL; 790 if ((st->st_mode & S_IFREG) == S_IFREG) { 791 ResourceMark rm(thread); 792 // Regular file, should be a zip or jimage file 793 // Canonicalized filename 794 char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN); 795 if (!get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) { 796 // This matches the classic VM 797 if (throw_exception) { 798 THROW_MSG_(vmSymbols::java_io_IOException(), "Bad pathname", NULL); 799 } else { 800 return NULL; 801 } 802 } 803 jint error; 804 JImageFile* jimage =(*JImageOpen)(canonical_path, &error); 805 if (jimage != NULL) { 806 new_entry = new ClassPathImageEntry(jimage, canonical_path); 807 } else { 808 char* error_msg = NULL; 809 jzfile* zip; 810 { 811 // enable call to C land 812 ThreadToNativeFromVM ttn(thread); 813 HandleMark hm(thread); 814 zip = (*ZipOpen)(canonical_path, &error_msg); 815 } 816 if (zip != NULL && error_msg == NULL) { 817 new_entry = new ClassPathZipEntry(zip, path, is_boot_append); 818 } else { 819 char *msg; 820 if (error_msg == NULL) { 821 msg = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(path) + 128); ; 822 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path); 823 } else { 824 int len = (int)(strlen(path) + strlen(error_msg) + 128); 825 msg = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, len); ; 826 jio_snprintf(msg, len - 1, "error in opening JAR file <%s> %s", error_msg, path); 827 } 828 // Don't complain about bad jar files added via -Xbootclasspath/a:. 829 if (throw_exception && is_init_completed()) { 830 THROW_MSG_(vmSymbols::java_lang_ClassNotFoundException(), msg, NULL); 831 } else { 832 return NULL; 833 } 834 } 835 } 836 log_info(class, path)("opened: %s", path); 837 log_info(class, load)("opened: %s", path); 838 } else { 839 // Directory 840 new_entry = new ClassPathDirEntry(path); 841 log_info(class, load)("path: %s", path); 842 } 843 return new_entry; 844 } 845 846 847 // Create a class path zip entry for a given path (return NULL if not found 848 // or zip/JAR file cannot be opened) 849 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) { 850 // check for a regular file 851 struct stat st; 852 if (os::stat(path, &st) == 0) { 853 if ((st.st_mode & S_IFREG) == S_IFREG) { 854 char canonical_path[JVM_MAXPATHLEN]; 855 if (get_canonical_path(path, canonical_path, JVM_MAXPATHLEN)) { 856 char* error_msg = NULL; 857 jzfile* zip; 858 { 859 // enable call to C land 860 JavaThread* thread = JavaThread::current(); 861 ThreadToNativeFromVM ttn(thread); 862 HandleMark hm(thread); 863 zip = (*ZipOpen)(canonical_path, &error_msg); 864 } 865 if (zip != NULL && error_msg == NULL) { 866 // create using canonical path 867 return new ClassPathZipEntry(zip, canonical_path, is_boot_append); 868 } 869 } 870 } 871 } 872 return NULL; 873 } 874 875 // The boot class loader must adhere to specfic visibility rules. 876 // Prior to loading a class in a named package, the package is checked 877 // to see if it is in a module defined to the boot loader. If the 878 // package is not in a module defined to the boot loader, the class 879 // must be loaded only in the boot loader's append path, which 880 // consists of [-Xbootclasspath/a]; [jvmti appended entries] 881 void ClassLoader::set_first_append_entry(ClassPathEntry *new_entry) { 882 if (_first_append_entry == NULL) { 883 _first_append_entry = new_entry; 884 } 885 } 886 887 // returns true if entry already on class path 888 bool ClassLoader::contains_entry(ClassPathEntry *entry) { 889 ClassPathEntry* e = _first_entry; 890 while (e != NULL) { 891 // assume zip entries have been canonicalized 892 if (strcmp(entry->name(), e->name()) == 0) { 893 return true; 894 } 895 e = e->next(); 896 } 897 return false; 898 } 899 900 void ClassLoader::add_to_list(ClassPathEntry *new_entry) { 901 if (new_entry != NULL) { 902 if (_last_entry == NULL) { 903 _first_entry = _last_entry = new_entry; 904 } else { 905 _last_entry->set_next(new_entry); 906 _last_entry = new_entry; 907 } 908 } 909 _num_entries ++; 910 } 911 912 void ClassLoader::prepend_to_list(ClassPathEntry *new_entry) { 913 if (new_entry != NULL) { 914 if (_last_entry == NULL) { 915 _first_entry = _last_entry = new_entry; 916 } else { 917 new_entry->set_next(_first_entry); 918 _first_entry = new_entry; 919 } 920 } 921 _num_entries ++; 922 } 923 924 void ClassLoader::add_to_list(const char *apath) { 925 update_class_path_entry_list((char*)apath, false, false, false, false); 926 } 927 928 void ClassLoader::prepend_to_list(const char *apath) { 929 update_class_path_entry_list((char*)apath, false, false, true, false); 930 } 931 932 // Returns true IFF the file/dir exists and the entry was successfully created. 933 bool ClassLoader::update_class_path_entry_list(const char *path, 934 bool check_for_duplicates, 935 bool mark_append_entry, 936 bool prepend_entry, 937 bool is_boot_append, 938 bool throw_exception) { 939 struct stat st; 940 if (os::stat(path, &st) == 0) { 941 // File or directory found 942 ClassPathEntry* new_entry = NULL; 943 Thread* THREAD = Thread::current(); 944 new_entry = create_class_path_entry(path, &st, throw_exception, is_boot_append, CHECK_(false)); 945 if (new_entry == NULL) { 946 return false; 947 } 948 949 // Ensure that the first boot loader append entry will always be set correctly. 950 assert((!mark_append_entry || 951 (mark_append_entry && (!check_for_duplicates || !contains_entry(new_entry)))), 952 "failed to mark boot loader's first append boundary"); 953 954 // Do not reorder the bootclasspath which would break get_system_package(). 955 // Add new entry to linked list 956 957 if (!check_for_duplicates || !contains_entry(new_entry)) { 958 ClassLoaderExt::add_class_path_entry(path, check_for_duplicates, new_entry, prepend_entry); 959 if (mark_append_entry) { 960 set_first_append_entry(new_entry); 961 } 962 } 963 return true; 964 } else { 965 #if INCLUDE_CDS 966 if (DumpSharedSpaces) { 967 _shared_paths_misc_info->add_nonexist_path(path); 968 } 969 #endif 970 return false; 971 } 972 } 973 974 void ClassLoader::print_bootclasspath() { 975 ClassPathEntry* e; 976 tty->print("[bootclasspath= "); 977 978 // Print --patch-module module/path specifications first 979 if (_patch_mod_entries != NULL) { 980 ResourceMark rm; 981 int num_of_entries = _patch_mod_entries->length(); 982 for (int i = 0; i < num_of_entries; i++) { 983 ModuleClassPathList* mpl = _patch_mod_entries->at(i); 984 tty->print("%s=", mpl->module_name()->as_C_string()); 985 e = mpl->module_first_entry(); 986 while (e != NULL) { 987 tty->print("%s", e->name()); 988 e = e->next(); 989 if (e != NULL) { 990 tty->print("%s", os::path_separator()); 991 } 992 } 993 tty->print(" ;"); 994 } 995 } 996 997 e = _first_entry; 998 while (e != NULL) { 999 tty->print("%s ;", e->name()); 1000 e = e->next(); 1001 } 1002 tty->print_cr("]"); 1003 } 1004 1005 void ClassLoader::load_zip_library() { 1006 assert(ZipOpen == NULL, "should not load zip library twice"); 1007 // First make sure native library is loaded 1008 os::native_java_library(); 1009 // Load zip library 1010 char path[JVM_MAXPATHLEN]; 1011 char ebuf[1024]; 1012 void* handle = NULL; 1013 if (os::dll_build_name(path, sizeof(path), Arguments::get_dll_dir(), "zip")) { 1014 handle = os::dll_load(path, ebuf, sizeof ebuf); 1015 } 1016 if (handle == NULL) { 1017 vm_exit_during_initialization("Unable to load ZIP library", path); 1018 } 1019 // Lookup zip entry points 1020 ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, os::dll_lookup(handle, "ZIP_Open")); 1021 ZipClose = CAST_TO_FN_PTR(ZipClose_t, os::dll_lookup(handle, "ZIP_Close")); 1022 FindEntry = CAST_TO_FN_PTR(FindEntry_t, os::dll_lookup(handle, "ZIP_FindEntry")); 1023 ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, os::dll_lookup(handle, "ZIP_ReadEntry")); 1024 ReadMappedEntry = CAST_TO_FN_PTR(ReadMappedEntry_t, os::dll_lookup(handle, "ZIP_ReadMappedEntry")); 1025 GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, os::dll_lookup(handle, "ZIP_GetNextEntry")); 1026 ZipInflateFully = CAST_TO_FN_PTR(ZipInflateFully_t, os::dll_lookup(handle, "ZIP_InflateFully")); 1027 Crc32 = CAST_TO_FN_PTR(Crc32_t, os::dll_lookup(handle, "ZIP_CRC32")); 1028 1029 // ZIP_Close is not exported on Windows in JDK5.0 so don't abort if ZIP_Close is NULL 1030 if (ZipOpen == NULL || FindEntry == NULL || ReadEntry == NULL || 1031 GetNextEntry == NULL || Crc32 == NULL) { 1032 vm_exit_during_initialization("Corrupted ZIP library", path); 1033 } 1034 1035 if (ZipInflateFully == NULL) { 1036 vm_exit_during_initialization("Corrupted ZIP library ZIP_InflateFully missing", path); 1037 } 1038 1039 // Lookup canonicalize entry in libjava.dll 1040 void *javalib_handle = os::native_java_library(); 1041 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, os::dll_lookup(javalib_handle, "Canonicalize")); 1042 // This lookup only works on 1.3. Do not check for non-null here 1043 } 1044 1045 void ClassLoader::load_jimage_library() { 1046 // First make sure native library is loaded 1047 os::native_java_library(); 1048 // Load jimage library 1049 char path[JVM_MAXPATHLEN]; 1050 char ebuf[1024]; 1051 void* handle = NULL; 1052 if (os::dll_build_name(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) { 1053 handle = os::dll_load(path, ebuf, sizeof ebuf); 1054 } 1055 if (handle == NULL) { 1056 vm_exit_during_initialization("Unable to load jimage library", path); 1057 } 1058 1059 // Lookup jimage entry points 1060 JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, os::dll_lookup(handle, "JIMAGE_Open")); 1061 guarantee(JImageOpen != NULL, "function JIMAGE_Open not found"); 1062 JImageClose = CAST_TO_FN_PTR(JImageClose_t, os::dll_lookup(handle, "JIMAGE_Close")); 1063 guarantee(JImageClose != NULL, "function JIMAGE_Close not found"); 1064 JImagePackageToModule = CAST_TO_FN_PTR(JImagePackageToModule_t, os::dll_lookup(handle, "JIMAGE_PackageToModule")); 1065 guarantee(JImagePackageToModule != NULL, "function JIMAGE_PackageToModule not found"); 1066 JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, os::dll_lookup(handle, "JIMAGE_FindResource")); 1067 guarantee(JImageFindResource != NULL, "function JIMAGE_FindResource not found"); 1068 JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, os::dll_lookup(handle, "JIMAGE_GetResource")); 1069 guarantee(JImageGetResource != NULL, "function JIMAGE_GetResource not found"); 1070 JImageResourceIterator = CAST_TO_FN_PTR(JImageResourceIterator_t, os::dll_lookup(handle, "JIMAGE_ResourceIterator")); 1071 guarantee(JImageResourceIterator != NULL, "function JIMAGE_ResourceIterator not found"); 1072 JImageResourcePath = CAST_TO_FN_PTR(JImage_ResourcePath_t, os::dll_lookup(handle, "JIMAGE_ResourcePath")); 1073 guarantee(JImageResourcePath != NULL, "function JIMAGE_ResourcePath not found"); 1074 } 1075 1076 jboolean ClassLoader::decompress(void *in, u8 inSize, void *out, u8 outSize, char **pmsg) { 1077 return (*ZipInflateFully)(in, inSize, out, outSize, pmsg); 1078 } 1079 1080 int ClassLoader::crc32(int crc, const char* buf, int len) { 1081 assert(Crc32 != NULL, "ZIP_CRC32 is not found"); 1082 return (*Crc32)(crc, (const jbyte*)buf, len); 1083 } 1084 1085 #if INCLUDE_CDS 1086 void ClassLoader::initialize_module_loader_map(JImageFile* jimage) { 1087 if (!DumpSharedSpaces) { 1088 return; // only needed for CDS dump time 1089 } 1090 1091 ResourceMark rm; 1092 jlong size; 1093 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", get_jimage_version_string(), MODULE_LOADER_MAP, &size); 1094 if (location == 0) { 1095 vm_exit_during_initialization( 1096 "Cannot find ModuleLoaderMap location from modules jimage.", NULL); 1097 } 1098 char* buffer = NEW_RESOURCE_ARRAY(char, size + 1); 1099 buffer[size] = '\0'; 1100 jlong read = (*JImageGetResource)(jimage, location, buffer, size); 1101 if (read != size) { 1102 vm_exit_during_initialization( 1103 "Cannot find ModuleLoaderMap resource from modules jimage.", NULL); 1104 } 1105 char* char_buf = (char*)buffer; 1106 int buflen = (int)strlen(char_buf); 1107 char* begin_ptr = char_buf; 1108 char* end_ptr = strchr(begin_ptr, '\n'); 1109 bool process_boot_modules = false; 1110 _boot_modules_array = new (ResourceObj::C_HEAP, mtModule) 1111 GrowableArray<char*>(INITIAL_BOOT_MODULES_ARRAY_SIZE, true); 1112 _platform_modules_array = new (ResourceObj::C_HEAP, mtModule) 1113 GrowableArray<char*>(INITIAL_PLATFORM_MODULES_ARRAY_SIZE, true); 1114 while (end_ptr != NULL && (end_ptr - char_buf) < buflen) { 1115 // Allocate a buffer from the C heap to be appended to the _boot_modules_array 1116 // or the _platform_modules_array. 1117 char* temp_name = NEW_C_HEAP_ARRAY(char, (size_t)(end_ptr - begin_ptr + 1), mtInternal); 1118 strncpy(temp_name, begin_ptr, end_ptr - begin_ptr); 1119 temp_name[end_ptr - begin_ptr] = '\0'; 1120 if (strncmp(temp_name, "BOOT", 4) == 0) { 1121 process_boot_modules = true; 1122 FREE_C_HEAP_ARRAY(char, temp_name); 1123 } else if (strncmp(temp_name, "PLATFORM", 8) == 0) { 1124 process_boot_modules = false; 1125 FREE_C_HEAP_ARRAY(char, temp_name); 1126 } else { 1127 // module name 1128 if (process_boot_modules) { 1129 _boot_modules_array->append(temp_name); 1130 } else { 1131 _platform_modules_array->append(temp_name); 1132 } 1133 } 1134 begin_ptr = ++end_ptr; 1135 end_ptr = strchr(begin_ptr, '\n'); 1136 } 1137 } 1138 #endif 1139 1140 // Function add_package extracts the package from the fully qualified class name 1141 // and checks if the package is in the boot loader's package entry table. If so, 1142 // then it sets the classpath_index in the package entry record. 1143 // 1144 // The classpath_index field is used to find the entry on the boot loader class 1145 // path for packages with classes loaded by the boot loader from -Xbootclasspath/a 1146 // in an unnamed module. It is also used to indicate (for all packages whose 1147 // classes are loaded by the boot loader) that at least one of the package's 1148 // classes has been loaded. 1149 bool ClassLoader::add_package(const char *fullq_class_name, s2 classpath_index, TRAPS) { 1150 assert(fullq_class_name != NULL, "just checking"); 1151 1152 // Get package name from fully qualified class name. 1153 ResourceMark rm; 1154 const char *cp = package_from_name(fullq_class_name); 1155 if (cp != NULL) { 1156 PackageEntryTable* pkg_entry_tbl = ClassLoaderData::the_null_class_loader_data()->packages(); 1157 TempNewSymbol pkg_symbol = SymbolTable::new_symbol(cp, CHECK_false); 1158 PackageEntry* pkg_entry = pkg_entry_tbl->lookup_only(pkg_symbol); 1159 if (pkg_entry != NULL) { 1160 assert(classpath_index != -1, "Unexpected classpath_index"); 1161 pkg_entry->set_classpath_index(classpath_index); 1162 } else { 1163 return false; 1164 } 1165 } 1166 return true; 1167 } 1168 1169 oop ClassLoader::get_system_package(const char* name, TRAPS) { 1170 // Look up the name in the boot loader's package entry table. 1171 if (name != NULL) { 1172 TempNewSymbol package_sym = SymbolTable::new_symbol(name, (int)strlen(name), CHECK_NULL); 1173 // Look for the package entry in the boot loader's package entry table. 1174 PackageEntry* package = 1175 ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym); 1176 1177 // Return NULL if package does not exist or if no classes in that package 1178 // have been loaded. 1179 if (package != NULL && package->has_loaded_class()) { 1180 ModuleEntry* module = package->module(); 1181 if (module->location() != NULL) { 1182 ResourceMark rm(THREAD); 1183 Handle ml = java_lang_String::create_from_str( 1184 module->location()->as_C_string(), THREAD); 1185 return ml(); 1186 } 1187 // Return entry on boot loader class path. 1188 Handle cph = java_lang_String::create_from_str( 1189 ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD); 1190 return cph(); 1191 } 1192 } 1193 return NULL; 1194 } 1195 1196 objArrayOop ClassLoader::get_system_packages(TRAPS) { 1197 ResourceMark rm(THREAD); 1198 // List of pointers to PackageEntrys that have loaded classes. 1199 GrowableArray<PackageEntry*>* loaded_class_pkgs = new GrowableArray<PackageEntry*>(50); 1200 { 1201 MutexLocker ml(Module_lock, THREAD); 1202 1203 PackageEntryTable* pe_table = 1204 ClassLoaderData::the_null_class_loader_data()->packages(); 1205 1206 // Collect the packages that have at least one loaded class. 1207 for (int x = 0; x < pe_table->table_size(); x++) { 1208 for (PackageEntry* package_entry = pe_table->bucket(x); 1209 package_entry != NULL; 1210 package_entry = package_entry->next()) { 1211 if (package_entry->has_loaded_class()) { 1212 loaded_class_pkgs->append(package_entry); 1213 } 1214 } 1215 } 1216 } 1217 1218 1219 // Allocate objArray and fill with java.lang.String 1220 objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), 1221 loaded_class_pkgs->length(), CHECK_NULL); 1222 objArrayHandle result(THREAD, r); 1223 for (int x = 0; x < loaded_class_pkgs->length(); x++) { 1224 PackageEntry* package_entry = loaded_class_pkgs->at(x); 1225 Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL); 1226 result->obj_at_put(x, str()); 1227 } 1228 return result(); 1229 } 1230 1231 #if INCLUDE_CDS 1232 s2 ClassLoader::module_to_classloader(const char* module_name) { 1233 1234 assert(DumpSharedSpaces, "dump time only"); 1235 assert(_boot_modules_array != NULL, "_boot_modules_array is NULL"); 1236 assert(_platform_modules_array != NULL, "_platform_modules_array is NULL"); 1237 1238 int array_size = _boot_modules_array->length(); 1239 for (int i = 0; i < array_size; i++) { 1240 if (strcmp(module_name, _boot_modules_array->at(i)) == 0) { 1241 return BOOT_LOADER; 1242 } 1243 } 1244 1245 array_size = _platform_modules_array->length(); 1246 for (int i = 0; i < array_size; i++) { 1247 if (strcmp(module_name, _platform_modules_array->at(i)) == 0) { 1248 return PLATFORM_LOADER; 1249 } 1250 } 1251 1252 return APP_LOADER; 1253 } 1254 1255 s2 ClassLoader::classloader_type(Symbol* class_name, ClassPathEntry* e, int classpath_index, TRAPS) { 1256 assert(DumpSharedSpaces, "Only used for CDS dump time"); 1257 1258 // obtain the classloader type based on the class name. 1259 // First obtain the package name based on the class name. Then obtain 1260 // the classloader type based on the package name from the jimage using 1261 // a jimage API. If the classloader type cannot be found from the 1262 // jimage, it is determined by the class path entry. 1263 jshort loader_type = ClassLoader::APP_LOADER; 1264 if (e->is_jrt()) { 1265 ResourceMark rm; 1266 TempNewSymbol pkg_name = InstanceKlass::package_from_name(class_name, CHECK_0); 1267 if (pkg_name != NULL) { 1268 const char* pkg_name_C_string = (const char*)(pkg_name->as_C_string()); 1269 ClassPathImageEntry* cpie = (ClassPathImageEntry*)e; 1270 JImageFile* jimage = cpie->jimage(); 1271 char* module_name = (char*)(*JImagePackageToModule)(jimage, pkg_name_C_string); 1272 if (module_name != NULL) { 1273 loader_type = ClassLoader::module_to_classloader(module_name); 1274 } 1275 } 1276 } else if (ClassLoaderExt::is_boot_classpath(classpath_index)) { 1277 loader_type = ClassLoader::BOOT_LOADER; 1278 } 1279 return loader_type; 1280 } 1281 #endif 1282 1283 // caller needs ResourceMark 1284 const char* ClassLoader::file_name_for_class_name(const char* class_name, 1285 int class_name_len) { 1286 assert(class_name != NULL, "invariant"); 1287 assert((int)strlen(class_name) == class_name_len, "invariant"); 1288 1289 static const char class_suffix[] = ".class"; 1290 1291 char* const file_name = NEW_RESOURCE_ARRAY(char, 1292 class_name_len + 1293 sizeof(class_suffix)); // includes term NULL 1294 1295 strncpy(file_name, class_name, class_name_len); 1296 strncpy(&file_name[class_name_len], class_suffix, sizeof(class_suffix)); 1297 1298 return file_name; 1299 } 1300 1301 instanceKlassHandle ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) { 1302 assert(name != NULL, "invariant"); 1303 assert(THREAD->is_Java_thread(), "must be a JavaThread"); 1304 1305 ResourceMark rm(THREAD); 1306 HandleMark hm(THREAD); 1307 1308 const char* const class_name = name->as_C_string(); 1309 1310 EventMark m("loading class %s", class_name); 1311 ThreadProfilerMark tpm(ThreadProfilerMark::classLoaderRegion); 1312 1313 const char* const file_name = file_name_for_class_name(class_name, 1314 name->utf8_length()); 1315 assert(file_name != NULL, "invariant"); 1316 1317 ClassLoaderExt::Context context(class_name, file_name, THREAD); 1318 1319 // Lookup stream for parsing .class file 1320 ClassFileStream* stream = NULL; 1321 s2 classpath_index = 0; 1322 ClassPathEntry* e = NULL; 1323 1324 // If DumpSharedSpaces is true, boot loader visibility boundaries are set 1325 // to be _first_entry to the end (all path entries). No --patch-module entries are 1326 // included since CDS and AppCDS are not supported if --patch-module is specified. 1327 // 1328 // If search_append_only is true, boot loader visibility boundaries are 1329 // set to be _first_append_entry to the end. This includes: 1330 // [-Xbootclasspath/a]; [jvmti appended entries] 1331 // 1332 // If both DumpSharedSpaces and search_append_only are false, boot loader 1333 // visibility boundaries are set to be _first_entry to the entry before 1334 // the _first_append_entry. This would include: 1335 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [exploded build | jimage] 1336 // 1337 // DumpSharedSpaces and search_append_only are mutually exclusive and cannot 1338 // be true at the same time. 1339 assert(!(DumpSharedSpaces && search_append_only), "DumpSharedSpaces and search_append_only are both true"); 1340 1341 // Load Attempt #1: --patch-module 1342 // Determine the class' defining module. If it appears in the _patch_mod_entries, 1343 // attempt to load the class from those locations specific to the module. 1344 // Note: The --patch-module entries are never searched if the boot loader's 1345 // visibility boundary is limited to only searching the append entries. 1346 if (_patch_mod_entries != NULL && !search_append_only && !DumpSharedSpaces) { 1347 // Find the module in the boot loader's module entry table 1348 PackageEntry* pkg_entry = get_package_entry(class_name, ClassLoaderData::the_null_class_loader_data(), THREAD); 1349 ModuleEntry* mod_entry = (pkg_entry != NULL) ? pkg_entry->module() : NULL; 1350 1351 // If the module system has not defined java.base yet, then 1352 // classes loaded are assumed to be defined to java.base. 1353 // When java.base is eventually defined by the module system, 1354 // all packages of classes that have been previously loaded 1355 // are verified in ModuleEntryTable::verify_javabase_packages(). 1356 if (!Universe::is_module_initialized() && 1357 !ModuleEntryTable::javabase_defined() && 1358 mod_entry == NULL) { 1359 mod_entry = ModuleEntryTable::javabase_module(); 1360 } 1361 1362 // The module must be a named module 1363 if (mod_entry != NULL && mod_entry->is_named()) { 1364 int num_of_entries = _patch_mod_entries->length(); 1365 const Symbol* class_module_name = mod_entry->name(); 1366 1367 // Loop through all the patch_mod entries looking for module 1368 for (int i = 0; i < num_of_entries; i++) { 1369 ModuleClassPathList* module_cpl = _patch_mod_entries->at(i); 1370 Symbol* module_cpl_name = module_cpl->module_name(); 1371 1372 if (module_cpl_name->fast_compare(class_module_name) == 0) { 1373 // Class' module has been located, attempt to load 1374 // the class from the module's ClassPathEntry list. 1375 e = module_cpl->module_first_entry(); 1376 while (e != NULL) { 1377 stream = e->open_stream(file_name, CHECK_NULL); 1378 // No context.check is required since both CDS 1379 // and AppCDS are turned off if --patch-module is specified. 1380 if (NULL != stream) { 1381 break; 1382 } 1383 e = e->next(); 1384 } 1385 // If the module was located in the patch_mod entries, break out 1386 // even if the class was not located successfully from that module's 1387 // ClassPathEntry list. There will not be another valid entry for 1388 // that module in the _patch_mod_entries array. 1389 break; 1390 } 1391 } 1392 } 1393 } 1394 1395 // Load Attempt #2: [exploded build | jimage] 1396 if (!search_append_only && (NULL == stream)) { 1397 e = _first_entry; 1398 while ((e != NULL) && (e != _first_append_entry)) { 1399 stream = e->open_stream(file_name, CHECK_NULL); 1400 if (!context.check(stream, classpath_index)) { 1401 return NULL; 1402 } 1403 if (NULL != stream) { 1404 break; 1405 } 1406 e = e->next(); 1407 ++classpath_index; 1408 } 1409 } 1410 1411 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries] 1412 if ((search_append_only || DumpSharedSpaces) && (NULL == stream)) { 1413 // For the boot loader append path search, must calculate 1414 // the starting classpath_index prior to attempting to 1415 // load the classfile. 1416 if (search_append_only) { 1417 ClassPathEntry *tmp_e = _first_entry; 1418 while ((tmp_e != NULL) && (tmp_e != _first_append_entry)) { 1419 tmp_e = tmp_e->next(); 1420 ++classpath_index; 1421 } 1422 } 1423 1424 e = _first_append_entry; 1425 while (e != NULL) { 1426 stream = e->open_stream(file_name, CHECK_NULL); 1427 if (!context.check(stream, classpath_index)) { 1428 return NULL; 1429 } 1430 if (NULL != stream) { 1431 break; 1432 } 1433 e = e->next(); 1434 ++classpath_index; 1435 } 1436 } 1437 1438 if (NULL == stream) { 1439 if (DumpSharedSpaces) { 1440 tty->print_cr("Preload Warning: Cannot find %s", class_name); 1441 } 1442 return NULL; 1443 } 1444 1445 stream->set_verify(context.should_verify(classpath_index)); 1446 1447 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 1448 Handle protection_domain; 1449 1450 instanceKlassHandle result = KlassFactory::create_from_stream(stream, 1451 name, 1452 loader_data, 1453 protection_domain, 1454 NULL, // host_klass 1455 NULL, // cp_patches 1456 NULL, // parsed_name 1457 THREAD); 1458 if (HAS_PENDING_EXCEPTION) { 1459 if (DumpSharedSpaces) { 1460 tty->print_cr("Preload Error: Failed to load %s", class_name); 1461 } 1462 return NULL; 1463 } 1464 1465 return context.record_result(name, e, classpath_index, result, THREAD); 1466 } 1467 1468 // Initialize the class loader's access to methods in libzip. Parse and 1469 // process the boot classpath into a list ClassPathEntry objects. Once 1470 // this list has been created, it must not change order (see class PackageInfo) 1471 // it can be appended to and is by jvmti and the kernel vm. 1472 1473 void ClassLoader::initialize() { 1474 EXCEPTION_MARK; 1475 1476 if (UsePerfData) { 1477 // jvmstat performance counters 1478 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time"); 1479 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime"); 1480 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self"); 1481 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime"); 1482 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self"); 1483 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime"); 1484 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self"); 1485 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses"); 1486 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses"); 1487 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses"); 1488 1489 NEWPERFTICKCOUNTER(_perf_class_parse_time, SUN_CLS, "parseClassTime"); 1490 NEWPERFTICKCOUNTER(_perf_class_parse_selftime, SUN_CLS, "parseClassTime.self"); 1491 NEWPERFTICKCOUNTER(_perf_sys_class_lookup_time, SUN_CLS, "lookupSysClassTime"); 1492 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime"); 1493 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime"); 1494 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime"); 1495 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self"); 1496 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount"); 1497 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses"); 1498 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime"); 1499 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self"); 1500 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes"); 1501 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes"); 1502 1503 1504 // The following performance counters are added for measuring the impact 1505 // of the bug fix of 6365597. They are mainly focused on finding out 1506 // the behavior of system & user-defined classloader lock, whether 1507 // ClassLoader.loadClass/findClass is being called synchronized or not. 1508 // Also two additional counters are created to see whether 'UnsyncloadClass' 1509 // flag is being set or not and how many times load_instance_class call 1510 // fails with linkageError etc. 1511 NEWPERFEVENTCOUNTER(_sync_systemLoaderLockContentionRate, SUN_CLS, 1512 "systemLoaderLockContentionRate"); 1513 NEWPERFEVENTCOUNTER(_sync_nonSystemLoaderLockContentionRate, SUN_CLS, 1514 "nonSystemLoaderLockContentionRate"); 1515 NEWPERFEVENTCOUNTER(_sync_JVMFindLoadedClassLockFreeCounter, SUN_CLS, 1516 "jvmFindLoadedClassNoLockCalls"); 1517 NEWPERFEVENTCOUNTER(_sync_JVMDefineClassLockFreeCounter, SUN_CLS, 1518 "jvmDefineClassNoLockCalls"); 1519 1520 NEWPERFEVENTCOUNTER(_sync_JNIDefineClassLockFreeCounter, SUN_CLS, 1521 "jniDefineClassNoLockCalls"); 1522 1523 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, 1524 "unsafeDefineClassCalls"); 1525 1526 NEWPERFEVENTCOUNTER(_isUnsyncloadClass, SUN_CLS, "isUnsyncloadClassSet"); 1527 NEWPERFEVENTCOUNTER(_load_instance_class_failCounter, SUN_CLS, 1528 "loadInstanceClassFailRate"); 1529 1530 // increment the isUnsyncloadClass counter if UnsyncloadClass is set. 1531 if (UnsyncloadClass) { 1532 _isUnsyncloadClass->inc(); 1533 } 1534 } 1535 1536 // lookup zip library entry points 1537 load_zip_library(); 1538 // lookup jimage library entry points 1539 load_jimage_library(); 1540 #if INCLUDE_CDS 1541 // initialize search path 1542 if (DumpSharedSpaces) { 1543 _shared_paths_misc_info = SharedClassUtil::allocate_shared_paths_misc_info(); 1544 } 1545 #endif 1546 setup_bootstrap_search_path(); 1547 } 1548 1549 #if INCLUDE_CDS 1550 void ClassLoader::initialize_shared_path() { 1551 if (DumpSharedSpaces) { 1552 ClassLoaderExt::setup_search_paths(); 1553 _shared_paths_misc_info->write_jint(0); // see comments in SharedPathsMiscInfo::check() 1554 } 1555 } 1556 #endif 1557 1558 jlong ClassLoader::classloader_time_ms() { 1559 return UsePerfData ? 1560 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1; 1561 } 1562 1563 jlong ClassLoader::class_init_count() { 1564 return UsePerfData ? _perf_classes_inited->get_value() : -1; 1565 } 1566 1567 jlong ClassLoader::class_init_time_ms() { 1568 return UsePerfData ? 1569 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1; 1570 } 1571 1572 jlong ClassLoader::class_verify_time_ms() { 1573 return UsePerfData ? 1574 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1; 1575 } 1576 1577 jlong ClassLoader::class_link_count() { 1578 return UsePerfData ? _perf_classes_linked->get_value() : -1; 1579 } 1580 1581 jlong ClassLoader::class_link_time_ms() { 1582 return UsePerfData ? 1583 Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1; 1584 } 1585 1586 int ClassLoader::compute_Object_vtable() { 1587 // hardwired for JDK1.2 -- would need to duplicate class file parsing 1588 // code to determine actual value from file 1589 // Would be value '11' if finals were in vtable 1590 int JDK_1_2_Object_vtable_size = 5; 1591 return JDK_1_2_Object_vtable_size * vtableEntry::size(); 1592 } 1593 1594 1595 void classLoader_init1() { 1596 ClassLoader::initialize(); 1597 } 1598 1599 // Complete the ClassPathEntry setup for the boot loader 1600 void classLoader_init2() { 1601 // Setup the list of module/path pairs for --patch-module processing 1602 // This must be done after the SymbolTable is created in order 1603 // to use fast_compare on module names instead of a string compare. 1604 if (Arguments::get_patch_mod_prefix() != NULL) { 1605 ClassLoader::setup_patch_mod_entries(); 1606 } 1607 1608 // Determine if this is an exploded build 1609 ClassLoader::set_has_jimage(); 1610 } 1611 1612 1613 bool ClassLoader::get_canonical_path(const char* orig, char* out, int len) { 1614 assert(orig != NULL && out != NULL && len > 0, "bad arguments"); 1615 if (CanonicalizeEntry != NULL) { 1616 JavaThread* THREAD = JavaThread::current(); 1617 JNIEnv* env = THREAD->jni_environment(); 1618 ResourceMark rm(THREAD); 1619 1620 // os::native_path writes into orig_copy 1621 char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(orig)+1); 1622 strcpy(orig_copy, orig); 1623 if ((CanonicalizeEntry)(env, os::native_path(orig_copy), out, len) < 0) { 1624 return false; 1625 } 1626 } else { 1627 // On JDK 1.2.2 the Canonicalize does not exist, so just do nothing 1628 strncpy(out, orig, len); 1629 out[len - 1] = '\0'; 1630 } 1631 return true; 1632 } 1633 1634 void ClassLoader::create_javabase() { 1635 Thread* THREAD = Thread::current(); 1636 1637 // Create java.base's module entry for the boot 1638 // class loader prior to loading j.l.Ojbect. 1639 ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data(); 1640 1641 // Get module entry table 1642 ModuleEntryTable* null_cld_modules = null_cld->modules(); 1643 if (null_cld_modules == NULL) { 1644 vm_exit_during_initialization("No ModuleEntryTable for the boot class loader"); 1645 } 1646 1647 { 1648 MutexLocker ml(Module_lock, THREAD); 1649 ModuleEntry* jb_module = null_cld_modules->locked_create_entry_or_null(Handle(NULL), vmSymbols::java_base(), NULL, NULL, null_cld); 1650 if (jb_module == NULL) { 1651 vm_exit_during_initialization("Unable to create ModuleEntry for java.base"); 1652 } 1653 ModuleEntryTable::set_javabase_module(jb_module); 1654 } 1655 } 1656 1657 void ClassLoader::set_has_jimage() { 1658 // Determine if this is an exploded build. When looking for 1659 // the jimage file, only search the piece of the boot 1660 // loader's boot class path which contains [exploded build | jimage]. 1661 // Do not search the boot loader's patch-module entries or append path. 1662 ClassPathEntry* e = _first_entry; 1663 ClassPathEntry* last_e = _first_append_entry; 1664 while ((e != NULL) && (e != last_e)) { 1665 JImageFile *jimage = e->jimage(); 1666 if (jimage != NULL && e->is_jrt()) { 1667 _has_jimage = true; 1668 #if INCLUDE_CDS 1669 ClassLoader::initialize_module_loader_map(jimage); 1670 #endif 1671 return; 1672 } 1673 e = e->next(); 1674 } 1675 } 1676 1677 #ifndef PRODUCT 1678 1679 // CompileTheWorld 1680 // 1681 // Iterates over all class path entries and forces compilation of all methods 1682 // in all classes found. Currently, only zip/jar archives are searched. 1683 // 1684 // The classes are loaded by the Java level bootstrap class loader, and the 1685 // initializer is called. If DelayCompilationDuringStartup is true (default), 1686 // the interpreter will run the initialization code. Note that forcing 1687 // initialization in this way could potentially lead to initialization order 1688 // problems, in which case we could just force the initialization bit to be set. 1689 1690 1691 // We need to iterate over the contents of a zip/jar file, so we replicate the 1692 // jzcell and jzfile definitions from zip_util.h but rename jzfile to real_jzfile, 1693 // since jzfile already has a void* definition. 1694 // 1695 // Note that this is only used in debug mode. 1696 // 1697 // HotSpot integration note: 1698 // Matches zip_util.h 1.14 99/06/01 from jdk1.3 beta H build 1699 1700 1701 // JDK 1.3 version 1702 typedef struct real_jzentry { /* Zip file entry */ 1703 char *name; /* entry name */ 1704 jint time; /* modification time */ 1705 jint size; /* size of uncompressed data */ 1706 jint csize; /* size of compressed data (zero if uncompressed) */ 1707 jint crc; /* crc of uncompressed data */ 1708 char *comment; /* optional zip file comment */ 1709 jbyte *extra; /* optional extra data */ 1710 jint pos; /* position of LOC header (if negative) or data */ 1711 } real_jzentry; 1712 1713 typedef struct real_jzfile { /* Zip file */ 1714 char *name; /* zip file name */ 1715 jint refs; /* number of active references */ 1716 jint fd; /* open file descriptor */ 1717 void *lock; /* read lock */ 1718 char *comment; /* zip file comment */ 1719 char *msg; /* zip error message */ 1720 void *entries; /* array of hash cells */ 1721 jint total; /* total number of entries */ 1722 unsigned short *table; /* Hash chain heads: indexes into entries */ 1723 jint tablelen; /* number of hash eads */ 1724 real_jzfile *next; /* next zip file in search list */ 1725 jzentry *cache; /* we cache the most recently freed jzentry */ 1726 /* Information on metadata names in META-INF directory */ 1727 char **metanames; /* array of meta names (may have null names) */ 1728 jint metacount; /* number of slots in metanames array */ 1729 /* If there are any per-entry comments, they are in the comments array */ 1730 char **comments; 1731 } real_jzfile; 1732 1733 void ClassPathDirEntry::compile_the_world(Handle loader, TRAPS) { 1734 // For now we only compile all methods in all classes in zip/jar files 1735 tty->print_cr("CompileTheWorld : Skipped classes in %s", _dir); 1736 tty->cr(); 1737 } 1738 1739 void ClassPathZipEntry::compile_the_world(Handle loader, TRAPS) { 1740 real_jzfile* zip = (real_jzfile*) _zip; 1741 tty->print_cr("CompileTheWorld : Compiling all classes in %s", zip->name); 1742 tty->cr(); 1743 // Iterate over all entries in zip file 1744 for (int n = 0; ; n++) { 1745 real_jzentry * ze = (real_jzentry *)((*GetNextEntry)(_zip, n)); 1746 if (ze == NULL) break; 1747 ClassLoader::compile_the_world_in(ze->name, loader, CHECK); 1748 } 1749 if (HAS_PENDING_EXCEPTION) { 1750 if (PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) { 1751 CLEAR_PENDING_EXCEPTION; 1752 tty->print_cr("\nCompileTheWorld : Ran out of memory\n"); 1753 tty->print_cr("Increase class metadata storage if a limit was set"); 1754 } else { 1755 tty->print_cr("\nCompileTheWorld : Unexpected exception occurred\n"); 1756 } 1757 } 1758 } 1759 1760 void ClassLoader::compile_the_world() { 1761 EXCEPTION_MARK; 1762 HandleMark hm(THREAD); 1763 ResourceMark rm(THREAD); 1764 1765 // Find bootstrap loader 1766 Handle system_class_loader (THREAD, SystemDictionary::java_system_loader()); 1767 // Iterate over all bootstrap class path entries 1768 ClassPathEntry* e = _first_entry; 1769 jlong start = os::javaTimeMillis(); 1770 while (e != NULL) { 1771 // We stop at "modules" jimage, unless it is the first bootstrap path entry 1772 if (e->is_jrt() && e != _first_entry) break; 1773 e->compile_the_world(system_class_loader, CATCH); 1774 e = e->next(); 1775 } 1776 jlong end = os::javaTimeMillis(); 1777 tty->print_cr("CompileTheWorld : Done (%d classes, %d methods, " JLONG_FORMAT " ms)", 1778 _compile_the_world_class_counter, _compile_the_world_method_counter, (end - start)); 1779 { 1780 // Print statistics as if before normal exit: 1781 extern void print_statistics(); 1782 print_statistics(); 1783 } 1784 vm_exit(0); 1785 } 1786 1787 int ClassLoader::_compile_the_world_class_counter = 0; 1788 int ClassLoader::_compile_the_world_method_counter = 0; 1789 static int _codecache_sweep_counter = 0; 1790 1791 // Filter out all exceptions except OOMs 1792 static void clear_pending_exception_if_not_oom(TRAPS) { 1793 if (HAS_PENDING_EXCEPTION && 1794 !PENDING_EXCEPTION->is_a(SystemDictionary::OutOfMemoryError_klass())) { 1795 CLEAR_PENDING_EXCEPTION; 1796 } 1797 // The CHECK at the caller will propagate the exception out 1798 } 1799 1800 /** 1801 * Returns if the given method should be compiled when doing compile-the-world. 1802 * 1803 * TODO: This should be a private method in a CompileTheWorld class. 1804 */ 1805 static bool can_be_compiled(methodHandle m, int comp_level) { 1806 assert(CompileTheWorld, "must be"); 1807 1808 // It's not valid to compile a native wrapper for MethodHandle methods 1809 // that take a MemberName appendix since the bytecode signature is not 1810 // correct. 1811 vmIntrinsics::ID iid = m->intrinsic_id(); 1812 if (MethodHandles::is_signature_polymorphic(iid) && MethodHandles::has_member_arg(iid)) { 1813 return false; 1814 } 1815 1816 return CompilationPolicy::can_be_compiled(m, comp_level); 1817 } 1818 1819 void ClassLoader::compile_the_world_in(char* name, Handle loader, TRAPS) { 1820 if (string_ends_with(name, ".class")) { 1821 // We have a .class file 1822 int len = (int)strlen(name); 1823 char buffer[2048]; 1824 strncpy(buffer, name, len - 6); 1825 buffer[len-6] = 0; 1826 // If the file has a period after removing .class, it's not really a 1827 // valid class file. The class loader will check everything else. 1828 if (strchr(buffer, '.') == NULL) { 1829 _compile_the_world_class_counter++; 1830 if (_compile_the_world_class_counter > CompileTheWorldStopAt) return; 1831 1832 // Construct name without extension 1833 TempNewSymbol sym = SymbolTable::new_symbol(buffer, CHECK); 1834 // Use loader to load and initialize class 1835 Klass* ik = SystemDictionary::resolve_or_null(sym, loader, Handle(), THREAD); 1836 instanceKlassHandle k (THREAD, ik); 1837 if (k.not_null() && !HAS_PENDING_EXCEPTION) { 1838 k->initialize(THREAD); 1839 } 1840 bool exception_occurred = HAS_PENDING_EXCEPTION; 1841 clear_pending_exception_if_not_oom(CHECK); 1842 if (CompileTheWorldPreloadClasses && k.not_null()) { 1843 ConstantPool::preload_and_initialize_all_classes(k->constants(), THREAD); 1844 if (HAS_PENDING_EXCEPTION) { 1845 // If something went wrong in preloading we just ignore it 1846 clear_pending_exception_if_not_oom(CHECK); 1847 tty->print_cr("Preloading failed for (%d) %s", _compile_the_world_class_counter, buffer); 1848 } 1849 } 1850 1851 if (_compile_the_world_class_counter >= CompileTheWorldStartAt) { 1852 if (k.is_null() || exception_occurred) { 1853 // If something went wrong (e.g. ExceptionInInitializerError) we skip this class 1854 tty->print_cr("CompileTheWorld (%d) : Skipping %s", _compile_the_world_class_counter, buffer); 1855 } else { 1856 tty->print_cr("CompileTheWorld (%d) : %s", _compile_the_world_class_counter, buffer); 1857 // Preload all classes to get around uncommon traps 1858 // Iterate over all methods in class 1859 int comp_level = CompilationPolicy::policy()->initial_compile_level(); 1860 for (int n = 0; n < k->methods()->length(); n++) { 1861 methodHandle m (THREAD, k->methods()->at(n)); 1862 if (can_be_compiled(m, comp_level)) { 1863 if (++_codecache_sweep_counter == CompileTheWorldSafepointInterval) { 1864 // Give sweeper a chance to keep up with CTW 1865 VM_ForceSafepoint op; 1866 VMThread::execute(&op); 1867 _codecache_sweep_counter = 0; 1868 } 1869 // Force compilation 1870 CompileBroker::compile_method(m, InvocationEntryBci, comp_level, 1871 methodHandle(), 0, CompileTask::Reason_CTW, THREAD); 1872 if (HAS_PENDING_EXCEPTION) { 1873 clear_pending_exception_if_not_oom(CHECK); 1874 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string()); 1875 } else { 1876 _compile_the_world_method_counter++; 1877 } 1878 if (TieredCompilation && TieredStopAtLevel >= CompLevel_full_optimization) { 1879 // Clobber the first compile and force second tier compilation 1880 CompiledMethod* nm = m->code(); 1881 if (nm != NULL && !m->is_method_handle_intrinsic()) { 1882 // Throw out the code so that the code cache doesn't fill up 1883 nm->make_not_entrant(); 1884 m->clear_code(); 1885 } 1886 CompileBroker::compile_method(m, InvocationEntryBci, CompLevel_full_optimization, 1887 methodHandle(), 0, CompileTask::Reason_CTW, THREAD); 1888 if (HAS_PENDING_EXCEPTION) { 1889 clear_pending_exception_if_not_oom(CHECK); 1890 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string()); 1891 } else { 1892 _compile_the_world_method_counter++; 1893 } 1894 } 1895 } else { 1896 tty->print_cr("CompileTheWorld (%d) : Skipping method: %s", _compile_the_world_class_counter, m->name_and_sig_as_C_string()); 1897 } 1898 1899 CompiledMethod* nm = m->code(); 1900 if (nm != NULL && !m->is_method_handle_intrinsic()) { 1901 // Throw out the code so that the code cache doesn't fill up 1902 nm->make_not_entrant(); 1903 m->clear_code(); 1904 } 1905 } 1906 } 1907 } 1908 } 1909 } 1910 } 1911 1912 #endif //PRODUCT 1913 1914 // Please keep following two functions at end of this file. With them placed at top or in middle of the file, 1915 // they could get inlined by agressive compiler, an unknown trick, see bug 6966589. 1916 void PerfClassTraceTime::initialize() { 1917 if (!UsePerfData) return; 1918 1919 if (_eventp != NULL) { 1920 // increment the event counter 1921 _eventp->inc(); 1922 } 1923 1924 // stop the current active thread-local timer to measure inclusive time 1925 _prev_active_event = -1; 1926 for (int i=0; i < EVENT_TYPE_COUNT; i++) { 1927 if (_timers[i].is_active()) { 1928 assert(_prev_active_event == -1, "should have only one active timer"); 1929 _prev_active_event = i; 1930 _timers[i].stop(); 1931 } 1932 } 1933 1934 if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) { 1935 // start the inclusive timer if not recursively called 1936 _t.start(); 1937 } 1938 1939 // start thread-local timer of the given event type 1940 if (!_timers[_event_type].is_active()) { 1941 _timers[_event_type].start(); 1942 } 1943 } 1944 1945 PerfClassTraceTime::~PerfClassTraceTime() { 1946 if (!UsePerfData) return; 1947 1948 // stop the thread-local timer as the event completes 1949 // and resume the thread-local timer of the event next on the stack 1950 _timers[_event_type].stop(); 1951 jlong selftime = _timers[_event_type].ticks(); 1952 1953 if (_prev_active_event >= 0) { 1954 _timers[_prev_active_event].start(); 1955 } 1956 1957 if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return; 1958 1959 // increment the counters only on the leaf call 1960 _t.stop(); 1961 _timep->inc(_t.ticks()); 1962 if (_selftimep != NULL) { 1963 _selftimep->inc(selftime); 1964 } 1965 // add all class loading related event selftime to the accumulated time counter 1966 ClassLoader::perf_accumulated_time()->inc(selftime); 1967 1968 // reset the timer 1969 _timers[_event_type].reset(); 1970 }