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