1 /* 2 * Copyright (c) 1997, 2024, 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 "cds/cds_globals.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/filemap.hpp" 29 #include "cds/heapShared.hpp" 30 #include "classfile/classFileStream.hpp" 31 #include "classfile/classLoader.inline.hpp" 32 #include "classfile/classLoaderData.inline.hpp" 33 #include "classfile/classLoaderExt.hpp" 34 #include "classfile/classLoadInfo.hpp" 35 #include "classfile/javaClasses.hpp" 36 #include "classfile/klassFactory.hpp" 37 #include "classfile/moduleEntry.hpp" 38 #include "classfile/modules.hpp" 39 #include "classfile/packageEntry.hpp" 40 #include "classfile/symbolTable.hpp" 41 #include "classfile/systemDictionary.hpp" 42 #include "classfile/systemDictionaryShared.hpp" 43 #include "classfile/vmClasses.hpp" 44 #include "classfile/vmSymbols.hpp" 45 #include "compiler/compileBroker.hpp" 46 #include "interpreter/bytecodeStream.hpp" 47 #include "interpreter/oopMapCache.hpp" 48 #include "jimage.hpp" 49 #include "jvm.h" 50 #include "logging/log.hpp" 51 #include "logging/logStream.hpp" 52 #include "logging/logTag.hpp" 53 #include "memory/allocation.inline.hpp" 54 #include "memory/oopFactory.hpp" 55 #include "memory/resourceArea.hpp" 56 #include "memory/universe.hpp" 57 #include "oops/instanceKlass.hpp" 58 #include "oops/instanceRefKlass.hpp" 59 #include "oops/klass.inline.hpp" 60 #include "oops/method.inline.hpp" 61 #include "oops/objArrayOop.inline.hpp" 62 #include "oops/oop.inline.hpp" 63 #include "oops/symbol.hpp" 64 #include "prims/jvm_misc.hpp" 65 #include "runtime/arguments.hpp" 66 #include "runtime/handles.inline.hpp" 67 #include "runtime/init.hpp" 68 #include "runtime/interfaceSupport.inline.hpp" 69 #include "runtime/java.hpp" 70 #include "runtime/javaCalls.hpp" 71 #include "runtime/os.hpp" 72 #include "runtime/perfData.inline.hpp" 73 #include "runtime/threadCritical.hpp" 74 #include "runtime/timer.hpp" 75 #include "runtime/vm_version.hpp" 76 #include "services/management.hpp" 77 #include "services/threadService.hpp" 78 #include "utilities/checkedCast.hpp" 79 #include "utilities/classpathStream.hpp" 80 #include "utilities/events.hpp" 81 #include "utilities/macros.hpp" 82 #include "utilities/ostream.hpp" 83 #include "utilities/utf8.hpp" 84 85 #include <stdlib.h> 86 #include <ctype.h> 87 88 // Entry point in java.dll for path canonicalization 89 90 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len); 91 92 static canonicalize_fn_t CanonicalizeEntry = nullptr; 93 94 // Entry points for jimage.dll for loading jimage file entries 95 96 static JImageOpen_t JImageOpen = nullptr; 97 static JImageClose_t JImageClose = nullptr; 98 static JImageFindResource_t JImageFindResource = nullptr; 99 static JImageGetResource_t JImageGetResource = nullptr; 100 101 // JimageFile pointer, or null if exploded JDK build. 102 static JImageFile* JImage_file = nullptr; 103 104 // Globals 105 106 PerfCounter* ClassLoader::_perf_accumulated_time = nullptr; 107 PerfCounter* ClassLoader::_perf_classes_inited = nullptr; 108 PerfCounter* ClassLoader::_perf_class_init_time = nullptr; 109 PerfCounter* ClassLoader::_perf_class_init_selftime = nullptr; 110 PerfCounter* ClassLoader::_perf_class_init_bytecodes_count = nullptr; 111 PerfCounter* ClassLoader::_perf_classes_verified = nullptr; 112 PerfCounter* ClassLoader::_perf_class_verify_time = nullptr; 113 PerfCounter* ClassLoader::_perf_class_verify_selftime = nullptr; 114 PerfCounter* ClassLoader::_perf_classes_linked = nullptr; 115 PerfCounter* ClassLoader::_perf_class_link_time = nullptr; 116 PerfCounter* ClassLoader::_perf_class_link_selftime = nullptr; 117 PerfCounter* ClassLoader::_perf_shared_classload_time = nullptr; 118 PerfCounter* ClassLoader::_perf_sys_classload_time = nullptr; 119 PerfCounter* ClassLoader::_perf_app_classload_time = nullptr; 120 PerfCounter* ClassLoader::_perf_app_classload_selftime = nullptr; 121 PerfCounter* ClassLoader::_perf_app_classload_count = nullptr; 122 PerfCounter* ClassLoader::_perf_define_appclasses = nullptr; 123 PerfCounter* ClassLoader::_perf_define_appclass_time = nullptr; 124 PerfCounter* ClassLoader::_perf_define_appclass_selftime = nullptr; 125 PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = nullptr; 126 PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = nullptr; 127 PerfCounter* ClassLoader::_perf_preload_total_time = nullptr; 128 PerfCounter* ClassLoader::_perf_preload_time = nullptr; 129 PerfCounter* ClassLoader::_perf_prelink_time = nullptr; 130 PerfCounter* ClassLoader::_perf_preinit_time = nullptr; 131 PerfCounter* ClassLoader::_perf_preresolve_time = nullptr; 132 PerfCounter* ClassLoader::_perf_ik_link_methods_time = nullptr; 133 PerfCounter* ClassLoader::_perf_method_adapters_time = nullptr; 134 PerfCounter* ClassLoader::_perf_ik_link_methods_count = nullptr; 135 PerfCounter* ClassLoader::_perf_method_adapters_count = nullptr; 136 PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = nullptr; 137 PerfCounter* ClassLoader::_perf_secondary_hash_time = nullptr; 138 139 PerfTickCounters* ClassLoader::_perf_resolve_indy_time = nullptr; 140 PerfTickCounters* ClassLoader::_perf_resolve_invokehandle_time = nullptr; 141 PerfTickCounters* ClassLoader::_perf_resolve_mh_time = nullptr; 142 PerfTickCounters* ClassLoader::_perf_resolve_mt_time = nullptr; 143 144 PerfCounter* ClassLoader::_perf_resolve_indy_count = nullptr; 145 PerfCounter* ClassLoader::_perf_resolve_invokehandle_count = nullptr; 146 PerfCounter* ClassLoader::_perf_resolve_mh_count = nullptr; 147 PerfCounter* ClassLoader::_perf_resolve_mt_count = nullptr; 148 149 void ClassLoader::print_counters(outputStream *st) { 150 st->print_cr("ClassLoader:"); 151 st->print_cr( " clinit: " JLONG_FORMAT_W(6) "us / " JLONG_FORMAT " events", 152 ClassLoader::class_init_time_ms(), ClassLoader::class_init_count()); 153 st->print_cr(" link methods: " JLONG_FORMAT_W(6) "us / " JLONG_FORMAT " events", 154 Management::ticks_to_us(_perf_ik_link_methods_time->get_value()) , _perf_ik_link_methods_count->get_value()); 155 st->print_cr(" method adapters: " JLONG_FORMAT_W(6) "us / " JLONG_FORMAT " events", 156 Management::ticks_to_us(_perf_method_adapters_time->get_value()) , _perf_method_adapters_count->get_value()); 157 if (CountBytecodes || CountBytecodesPerThread) { 158 st->print_cr("; executed " JLONG_FORMAT " bytecodes", ClassLoader::class_init_bytecodes_count()); 159 } 160 st->print_cr(" resolve..."); 161 st->print_cr(" invokedynamic: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events", 162 _perf_resolve_indy_time->elapsed_counter_value_us(), 163 _perf_resolve_indy_time->thread_counter_value_us(), 164 _perf_resolve_indy_count->get_value()); 165 st->print_cr(" invokehandle: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events", 166 _perf_resolve_invokehandle_time->elapsed_counter_value_us(), 167 _perf_resolve_invokehandle_time->thread_counter_value_us(), 168 _perf_resolve_invokehandle_count->get_value()); 169 st->print_cr(" CP_MethodHandle: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events", 170 _perf_resolve_mh_time->elapsed_counter_value_us(), 171 _perf_resolve_mh_time->thread_counter_value_us(), 172 _perf_resolve_mh_count->get_value()); 173 st->print_cr(" CP_MethodType: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) / " JLONG_FORMAT_W(5) " events", 174 _perf_resolve_mt_time->elapsed_counter_value_us(), 175 _perf_resolve_mt_time->thread_counter_value_us(), 176 _perf_resolve_mt_count->get_value()); 177 } 178 179 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr; 180 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr; 181 ClassPathEntry* ClassLoader::_jrt_entry = nullptr; 182 183 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr; 184 ClassPathEntry* volatile ClassLoader::_last_append_entry = nullptr; 185 #if INCLUDE_CDS 186 ClassPathEntry* ClassLoader::_app_classpath_entries = nullptr; 187 ClassPathEntry* ClassLoader::_last_app_classpath_entry = nullptr; 188 ClassPathEntry* ClassLoader::_module_path_entries = nullptr; 189 ClassPathEntry* ClassLoader::_last_module_path_entry = nullptr; 190 #endif 191 192 // helper routines 193 #if INCLUDE_CDS 194 static bool string_starts_with(const char* str, const char* str_to_find) { 195 size_t str_len = strlen(str); 196 size_t str_to_find_len = strlen(str_to_find); 197 if (str_to_find_len > str_len) { 198 return false; 199 } 200 return (strncmp(str, str_to_find, str_to_find_len) == 0); 201 } 202 #endif 203 204 static const char* get_jimage_version_string() { 205 static char version_string[10] = ""; 206 if (version_string[0] == '\0') { 207 jio_snprintf(version_string, sizeof(version_string), "%d.%d", 208 VM_Version::vm_major_version(), VM_Version::vm_minor_version()); 209 } 210 return (const char*)version_string; 211 } 212 213 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) { 214 size_t str_len = strlen(str); 215 size_t str_to_find_len = strlen(str_to_find); 216 if (str_to_find_len > str_len) { 217 return false; 218 } 219 return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0); 220 } 221 222 // Used to obtain the package name from a fully qualified class name. 223 Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) { 224 if (name == nullptr) { 225 if (bad_class_name != nullptr) { 226 *bad_class_name = true; 227 } 228 return nullptr; 229 } 230 231 int utf_len = name->utf8_length(); 232 const jbyte* base = (const jbyte*)name->base(); 233 const jbyte* start = base; 234 const jbyte* end = UTF8::strrchr(start, utf_len, JVM_SIGNATURE_SLASH); 235 if (end == nullptr) { 236 return nullptr; 237 } 238 // Skip over '['s 239 if (*start == JVM_SIGNATURE_ARRAY) { 240 do { 241 start++; 242 } while (start < end && *start == JVM_SIGNATURE_ARRAY); 243 244 // Fully qualified class names should not contain a 'L'. 245 // Set bad_class_name to true to indicate that the package name 246 // could not be obtained due to an error condition. 247 // In this situation, is_same_class_package returns false. 248 if (*start == JVM_SIGNATURE_CLASS) { 249 if (bad_class_name != nullptr) { 250 *bad_class_name = true; 251 } 252 return nullptr; 253 } 254 } 255 // A class name could have just the slash character in the name, 256 // in which case start > end 257 if (start >= end) { 258 // No package name 259 if (bad_class_name != nullptr) { 260 *bad_class_name = true; 261 } 262 return nullptr; 263 } 264 return SymbolTable::new_symbol(name, pointer_delta_as_int(start, base), pointer_delta_as_int(end, base)); 265 } 266 267 // Given a fully qualified package name, find its defining package in the class loader's 268 // package entry table. 269 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) { 270 if (pkg_name == nullptr) { 271 return nullptr; 272 } 273 PackageEntryTable* pkgEntryTable = loader_data->packages(); 274 return pkgEntryTable->lookup_only(pkg_name); 275 } 276 277 const char* ClassPathEntry::copy_path(const char* path) { 278 char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass); 279 strcpy(copy, path); 280 return copy; 281 } 282 283 ClassPathDirEntry::~ClassPathDirEntry() { 284 FREE_C_HEAP_ARRAY(char, _dir); 285 } 286 287 ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char* name) { 288 // construct full path name 289 assert((_dir != nullptr) && (name != nullptr), "sanity"); 290 size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1; 291 char* path = NEW_RESOURCE_ARRAY_IN_THREAD(current, char, path_len); 292 int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name); 293 assert(len == (int)(path_len - 1), "sanity"); 294 // check if file exists 295 struct stat st; 296 if (os::stat(path, &st) == 0) { 297 // found file, open it 298 int file_handle = os::open(path, 0, 0); 299 if (file_handle != -1) { 300 // read contents into resource array 301 u1* buffer = NEW_RESOURCE_ARRAY_IN_THREAD(current, u1, st.st_size); 302 size_t num_read = ::read(file_handle, (char*) buffer, st.st_size); 303 // close file 304 ::close(file_handle); 305 // construct ClassFileStream 306 if (num_read == (size_t)st.st_size) { 307 if (UsePerfData) { 308 ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read); 309 } 310 #ifdef ASSERT 311 // Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for 312 // debug builds so that we guard against use-after-free bugs. 313 FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len); 314 #endif 315 // We don't verify the length of the classfile stream fits in an int, but this is the 316 // bootloader so we have control of this. 317 // Resource allocated 318 return new ClassFileStream(buffer, 319 checked_cast<int>(st.st_size), 320 _dir); 321 } 322 } 323 } 324 FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len); 325 return nullptr; 326 } 327 328 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name, 329 bool from_class_path_attr, bool multi_release) : ClassPathEntry() { 330 _zip = zip; 331 _zip_name = copy_path(zip_name); 332 _from_class_path_attr = from_class_path_attr; 333 _multi_release = multi_release; 334 } 335 336 ClassPathZipEntry::~ClassPathZipEntry() { 337 ZipLibrary::close(_zip); 338 FREE_C_HEAP_ARRAY(char, _zip_name); 339 } 340 341 bool ClassPathZipEntry::has_entry(JavaThread* current, const char* name) { 342 ThreadToNativeFromVM ttn(current); 343 // check whether zip archive contains name 344 jint name_len; 345 jint filesize; 346 jzentry* entry = ZipLibrary::find_entry(_zip, name, &filesize, &name_len); 347 if (entry == nullptr) { 348 return false; 349 } else { 350 ZipLibrary::free_entry(_zip, entry); 351 return true; 352 } 353 } 354 355 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) { 356 // enable call to C land 357 ThreadToNativeFromVM ttn(current); 358 // check whether zip archive contains name 359 jint name_len; 360 jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len); 361 if (entry == nullptr) return nullptr; 362 u1* buffer; 363 char name_buf[128]; 364 char* filename; 365 if (name_len < 128) { 366 filename = name_buf; 367 } else { 368 filename = NEW_RESOURCE_ARRAY(char, name_len + 1); 369 } 370 371 // read contents into resource array 372 size_t size = (uint32_t)(*filesize); 373 if (nul_terminate) { 374 if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) { 375 return nullptr; // 32-bit integer overflow will occur. 376 } 377 size++; 378 } 379 380 // ZIP_ReadEntry also frees zentry 381 buffer = NEW_RESOURCE_ARRAY(u1, size); 382 if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) { 383 return nullptr; 384 } 385 386 // return result 387 if (nul_terminate) { 388 buffer[size - 1] = 0; 389 } 390 return buffer; 391 } 392 393 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) { 394 jint filesize; 395 u1* buffer = open_entry(current, name, &filesize, false); 396 if (buffer == nullptr) { 397 return nullptr; 398 } 399 if (UsePerfData) { 400 ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize); 401 } 402 // Resource allocated 403 return new ClassFileStream(buffer, 404 filesize, 405 _zip_name); 406 } 407 408 DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;) 409 410 JImageFile* ClassPathImageEntry::jimage() const { 411 return JImage_file; 412 } 413 414 JImageFile* ClassPathImageEntry::jimage_non_null() const { 415 assert(ClassLoader::has_jrt_entry(), "must be"); 416 assert(jimage() != nullptr, "should have been opened by ClassLoader::lookup_vm_options " 417 "and remained throughout normal JVM lifetime"); 418 return jimage(); 419 } 420 421 void ClassPathImageEntry::close_jimage() { 422 if (jimage() != nullptr) { 423 (*JImageClose)(jimage()); 424 JImage_file = nullptr; 425 } 426 } 427 428 ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) : 429 ClassPathEntry() { 430 guarantee(jimage != nullptr, "jimage file is null"); 431 guarantee(name != nullptr, "jimage file name is null"); 432 assert(_singleton == nullptr, "VM supports only one jimage"); 433 DEBUG_ONLY(_singleton = this); 434 size_t len = strlen(name) + 1; 435 _name = copy_path(name); 436 } 437 438 ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) { 439 return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data()); 440 } 441 442 // For a class in a named module, look it up in the jimage file using this syntax: 443 // /<module-name>/<package-name>/<base-class> 444 // 445 // Assumptions: 446 // 1. There are no unnamed modules in the jimage file. 447 // 2. A package is in at most one module in the jimage file. 448 // 449 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) { 450 jlong size; 451 JImageLocationRef location = (*JImageFindResource)(jimage_non_null(), "", get_jimage_version_string(), name, &size); 452 453 if (location == 0) { 454 TempNewSymbol class_name = SymbolTable::new_symbol(name); 455 TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name); 456 457 if (pkg_name != nullptr) { 458 if (!Universe::is_module_initialized()) { 459 location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size); 460 } else { 461 PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data); 462 if (package_entry != nullptr) { 463 ResourceMark rm(current); 464 // Get the module name 465 ModuleEntry* module = package_entry->module(); 466 assert(module != nullptr, "Boot classLoader package missing module"); 467 assert(module->is_named(), "Boot classLoader package is in unnamed module"); 468 const char* module_name = module->name()->as_C_string(); 469 if (module_name != nullptr) { 470 location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size); 471 } 472 } 473 } 474 } 475 } 476 if (location != 0) { 477 if (UsePerfData) { 478 ClassLoader::perf_sys_classfile_bytes_read()->inc(size); 479 } 480 char* data = NEW_RESOURCE_ARRAY(char, size); 481 (*JImageGetResource)(jimage_non_null(), location, data, size); 482 // Resource allocated 483 assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be"); 484 return new ClassFileStream((u1*)data, 485 checked_cast<int>(size), 486 _name, 487 true); // from_boot_loader_modules_image 488 } 489 490 return nullptr; 491 } 492 493 JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf, 494 const char* module_name, 495 const char* file_name, 496 jlong &size) { 497 return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size)); 498 } 499 500 bool ClassPathImageEntry::is_modules_image() const { 501 assert(this == _singleton, "VM supports a single jimage"); 502 assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry"); 503 return true; 504 } 505 506 #if INCLUDE_CDS 507 void ClassLoader::exit_with_path_failure(const char* error, const char* message) { 508 assert(CDSConfig::is_dumping_archive(), "sanity"); 509 tty->print_cr("Hint: enable -Xlog:class+path=info to diagnose the failure"); 510 vm_exit_during_cds_dumping(error, message); 511 } 512 #endif 513 514 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) { 515 _module_name = module_name; 516 _module_first_entry = nullptr; 517 _module_last_entry = nullptr; 518 } 519 520 ModuleClassPathList::~ModuleClassPathList() { 521 // Clean out each ClassPathEntry on list 522 ClassPathEntry* e = _module_first_entry; 523 while (e != nullptr) { 524 ClassPathEntry* next_entry = e->next(); 525 delete e; 526 e = next_entry; 527 } 528 } 529 530 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) { 531 if (new_entry != nullptr) { 532 if (_module_last_entry == nullptr) { 533 _module_first_entry = _module_last_entry = new_entry; 534 } else { 535 _module_last_entry->set_next(new_entry); 536 _module_last_entry = new_entry; 537 } 538 } 539 } 540 541 void ClassLoader::trace_class_path(const char* msg, const char* name) { 542 LogTarget(Info, class, path) lt; 543 if (lt.is_enabled()) { 544 LogStream ls(lt); 545 if (msg) { 546 ls.print("%s", msg); 547 } 548 if (name) { 549 if (strlen(name) < 256) { 550 ls.print("%s", name); 551 } else { 552 // For very long paths, we need to print each character separately, 553 // as print_cr() has a length limit 554 while (name[0] != '\0') { 555 ls.print("%c", name[0]); 556 name++; 557 } 558 } 559 } 560 ls.cr(); 561 } 562 } 563 564 void ClassLoader::setup_bootstrap_search_path(JavaThread* current) { 565 const char* bootcp = Arguments::get_boot_class_path(); 566 assert(bootcp != nullptr, "Boot class path must not be nullptr"); 567 if (PrintSharedArchiveAndExit) { 568 // Don't print bootcp - this is the bootcp of this current VM process, not necessarily 569 // the same as the boot classpath of the shared archive. 570 } else { 571 trace_class_path("bootstrap loader class path=", bootcp); 572 } 573 setup_bootstrap_search_path_impl(current, bootcp); 574 } 575 576 #if INCLUDE_CDS 577 void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) { 578 assert(CDSConfig::is_dumping_archive(), "sanity"); 579 580 ResourceMark rm(current); 581 ClasspathStream cp_stream(class_path); 582 583 int exclusion_start_index = INT_MAX; 584 size_t limit = SIZE_MAX; 585 if (CacheOnlyClassesIn != nullptr) { 586 char* appclasspath = Arguments::get_appclasspath(); 587 bool bad = false; 588 if (strstr(appclasspath, CacheOnlyClassesIn) != appclasspath) { 589 bad = true; 590 } else { 591 limit = strlen(CacheOnlyClassesIn); 592 if (limit > 0 && limit < strlen(appclasspath) 593 && CacheOnlyClassesIn[limit-1] != os::path_separator()[0] 594 && appclasspath[limit] != os::path_separator()[0]) { 595 bad = true; 596 } 597 } 598 if (bad) { 599 vm_exit_during_initialization(err_msg("CacheOnlyClassesIn \"%s\" must be a proper prefix of the CLASSPATH \"%s\"", 600 CacheOnlyClassesIn, appclasspath)); 601 } 602 } 603 604 while (cp_stream.has_next()) { 605 if (cp_stream.num_chars_consumed() >= limit && exclusion_start_index == INT_MAX) { 606 exclusion_start_index = num_boot_classpath_entries() + num_app_classpath_entries(); 607 } 608 const char* path = cp_stream.get_next(); 609 update_class_path_entry_list(current, path, /* check_for_duplicates */ true, 610 /* is_boot_append */ false, /* from_class_path_attr */ false); 611 } 612 613 ClassLoaderExt::set_app_class_exclusion_start_path_index(exclusion_start_index); 614 if (exclusion_start_index != INT_MAX) { 615 log_info(cds)("Exclude all app classes whose shared_classpath_index is greater than %d", exclusion_start_index); 616 } 617 } 618 619 void ClassLoader::add_to_module_path_entries(const char* path, 620 ClassPathEntry* entry) { 621 assert(entry != nullptr, "ClassPathEntry should not be nullptr"); 622 assert(CDSConfig::is_dumping_archive(), "sanity"); 623 624 // The entry does not exist, add to the list 625 if (_module_path_entries == nullptr) { 626 assert(_last_module_path_entry == nullptr, "Sanity"); 627 _module_path_entries = _last_module_path_entry = entry; 628 } else { 629 _last_module_path_entry->set_next(entry); 630 _last_module_path_entry = entry; 631 } 632 } 633 634 // Add a module path to the _module_path_entries list. 635 void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) { 636 assert(CDSConfig::is_dumping_archive(), "sanity"); 637 struct stat st; 638 if (os::stat(path, &st) != 0) { 639 tty->print_cr("os::stat error %d (%s). CDS dump aborted (path was \"%s\").", 640 errno, os::errno_name(errno), path); 641 vm_exit_during_initialization(); 642 } 643 // File or directory found 644 ClassPathEntry* new_entry = nullptr; 645 new_entry = create_class_path_entry(current, path, &st, 646 false /*is_boot_append */, false /* from_class_path_attr */); 647 if (new_entry != nullptr) { 648 // ClassLoaderExt::process_module_table() filters out non-jar entries before calling this function. 649 assert(new_entry->is_jar_file(), "module path entry %s is not a jar file", new_entry->name()); 650 add_to_module_path_entries(path, new_entry); 651 } 652 } 653 654 #endif // INCLUDE_CDS 655 656 void ClassLoader::close_jrt_image() { 657 // Not applicable for exploded builds 658 if (!ClassLoader::has_jrt_entry()) return; 659 _jrt_entry->close_jimage(); 660 } 661 662 // Construct the array of module/path pairs as specified to --patch-module 663 // for the boot loader to search ahead of the jimage, if the class being 664 // loaded is defined to a module that has been specified to --patch-module. 665 void ClassLoader::setup_patch_mod_entries() { 666 JavaThread* current = JavaThread::current(); 667 GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix(); 668 int num_of_entries = patch_mod_args->length(); 669 670 // Set up the boot loader's _patch_mod_entries list 671 _patch_mod_entries = new (mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, mtModule); 672 673 for (int i = 0; i < num_of_entries; i++) { 674 const char* module_name = (patch_mod_args->at(i))->module_name(); 675 Symbol* const module_sym = SymbolTable::new_symbol(module_name); 676 assert(module_sym != nullptr, "Failed to obtain Symbol for module name"); 677 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym); 678 679 char* class_path = (patch_mod_args->at(i))->path_string(); 680 ResourceMark rm(current); 681 ClasspathStream cp_stream(class_path); 682 683 while (cp_stream.has_next()) { 684 const char* path = cp_stream.get_next(); 685 struct stat st; 686 if (os::stat(path, &st) == 0) { 687 // File or directory found 688 ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false); 689 // If the path specification is valid, enter it into this module's list 690 if (new_entry != nullptr) { 691 module_cpl->add_to_list(new_entry); 692 } 693 } 694 } 695 696 // Record the module into the list of --patch-module entries only if 697 // valid ClassPathEntrys have been created 698 if (module_cpl->module_first_entry() != nullptr) { 699 _patch_mod_entries->push(module_cpl); 700 } 701 } 702 } 703 704 // Determine whether the module has been patched via the command-line 705 // option --patch-module 706 bool ClassLoader::is_in_patch_mod_entries(Symbol* module_name) { 707 if (_patch_mod_entries != nullptr && _patch_mod_entries->is_nonempty()) { 708 int table_len = _patch_mod_entries->length(); 709 for (int i = 0; i < table_len; i++) { 710 ModuleClassPathList* patch_mod = _patch_mod_entries->at(i); 711 if (module_name->fast_compare(patch_mod->module_name()) == 0) { 712 return true; 713 } 714 } 715 } 716 return false; 717 } 718 719 // Set up the _jrt_entry if present and boot append path 720 void ClassLoader::setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path) { 721 ResourceMark rm(current); 722 ClasspathStream cp_stream(class_path); 723 bool set_base_piece = true; 724 725 #if INCLUDE_CDS 726 if (CDSConfig::is_dumping_archive()) { 727 if (!Arguments::has_jimage()) { 728 vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr); 729 } 730 } 731 #endif 732 733 while (cp_stream.has_next()) { 734 const char* path = cp_stream.get_next(); 735 736 if (set_base_piece) { 737 // The first time through the bootstrap_search setup, it must be determined 738 // what the base or core piece of the boot loader search is. Either a java runtime 739 // image is present or this is an exploded module build situation. 740 assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME), 741 "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build"); 742 struct stat st; 743 if (os::stat(path, &st) == 0) { 744 // Directory found 745 if (JImage_file != nullptr) { 746 assert(Arguments::has_jimage(), "sanity check"); 747 const char* canonical_path = get_canonical_path(path, current); 748 assert(canonical_path != nullptr, "canonical_path issue"); 749 750 _jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path); 751 assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present"); 752 assert(_jrt_entry->jimage() != nullptr, "No java runtime image"); 753 } // else it's an exploded build. 754 } else { 755 // If path does not exist, exit 756 vm_exit_during_initialization("Unable to establish the boot loader search path", path); 757 } 758 set_base_piece = false; 759 } else { 760 // Every entry on the boot class path after the initial base piece, 761 // which is set by os::set_boot_path(), is considered an appended entry. 762 update_class_path_entry_list(current, path, /* check_for_duplicates */ false, 763 /* is_boot_append */ true, /* from_class_path_attr */ false); 764 } 765 } 766 } 767 768 // Gets the exploded path for the named module. The memory for the path 769 // is allocated on the C heap if `c_heap` is true otherwise in the resource area. 770 static const char* get_exploded_module_path(const char* module_name, bool c_heap) { 771 const char *home = Arguments::get_java_home(); 772 const char file_sep = os::file_separator()[0]; 773 // 10 represents the length of "modules" + 2 file separators + \0 774 size_t len = strlen(home) + strlen(module_name) + 10; 775 char *path = c_heap ? NEW_C_HEAP_ARRAY(char, len, mtModule) : NEW_RESOURCE_ARRAY(char, len); 776 jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name); 777 return path; 778 } 779 780 // During an exploded modules build, each module defined to the boot loader 781 // will be added to the ClassLoader::_exploded_entries array. 782 void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) { 783 assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable"); 784 assert(_exploded_entries != nullptr, "_exploded_entries was not initialized"); 785 786 // Find the module's symbol 787 ResourceMark rm(current); 788 const char *module_name = module_sym->as_C_string(); 789 const char *path = get_exploded_module_path(module_name, false); 790 791 struct stat st; 792 if (os::stat(path, &st) == 0) { 793 // Directory found 794 ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false); 795 796 // If the path specification is valid, enter it into this module's list. 797 // There is no need to check for duplicate modules in the exploded entry list, 798 // since no two modules with the same name can be defined to the boot loader. 799 // This is checked at module definition time in Modules::define_module. 800 if (new_entry != nullptr) { 801 ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym); 802 module_cpl->add_to_list(new_entry); 803 { 804 MutexLocker ml(current, Module_lock); 805 _exploded_entries->push(module_cpl); 806 } 807 log_info(class, load)("path: %s", path); 808 } 809 } 810 } 811 812 jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) { 813 // enable call to C land 814 ThreadToNativeFromVM ttn(thread); 815 HandleMark hm(thread); 816 return ZipLibrary::open(canonical_path, error_msg); 817 } 818 819 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current, 820 const char *path, const struct stat* st, 821 bool is_boot_append, 822 bool from_class_path_attr, 823 bool is_multi_release) { 824 ClassPathEntry* new_entry = nullptr; 825 if ((st->st_mode & S_IFMT) == S_IFREG) { 826 ResourceMark rm(current); 827 // Regular file, should be a zip file 828 // Canonicalized filename 829 const char* canonical_path = get_canonical_path(path, current); 830 if (canonical_path == nullptr) { 831 return nullptr; 832 } 833 char* error_msg = nullptr; 834 jzfile* zip = open_zip_file(canonical_path, &error_msg, current); 835 if (zip != nullptr && error_msg == nullptr) { 836 new_entry = new ClassPathZipEntry(zip, path, from_class_path_attr, is_multi_release); 837 } else { 838 #if INCLUDE_CDS 839 ClassLoaderExt::set_has_non_jar_in_classpath(); 840 #endif 841 return nullptr; 842 } 843 log_info(class, path)("opened: %s", path); 844 log_info(class, load)("opened: %s", path); 845 } else { 846 // Directory 847 new_entry = new ClassPathDirEntry(path); 848 log_info(class, load)("path: %s", path); 849 } 850 return new_entry; 851 } 852 853 854 // Create a class path zip entry for a given path (return null if not found 855 // or zip/JAR file cannot be opened) 856 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path) { 857 // check for a regular file 858 struct stat st; 859 if (os::stat(path, &st) == 0) { 860 if ((st.st_mode & S_IFMT) == S_IFREG) { 861 JavaThread* thread = JavaThread::current(); 862 ResourceMark rm(thread); 863 const char* canonical_path = get_canonical_path(path, thread); 864 if (canonical_path != nullptr) { 865 char* error_msg = nullptr; 866 jzfile* zip = open_zip_file(canonical_path, &error_msg, thread); 867 if (zip != nullptr && error_msg == nullptr) { 868 // create using canonical path 869 return new ClassPathZipEntry(zip, canonical_path, false, false); 870 } 871 } 872 } 873 } 874 return nullptr; 875 } 876 877 // The boot append entries are added with a lock, and read lock free. 878 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) { 879 if (new_entry != nullptr) { 880 MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag); 881 if (_last_append_entry == nullptr) { 882 _last_append_entry = new_entry; 883 assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty"); 884 Atomic::release_store(&_first_append_entry_list, new_entry); 885 } else { 886 _last_append_entry->set_next(new_entry); 887 _last_append_entry = new_entry; 888 } 889 } 890 } 891 892 // Record the path entries specified in -cp during dump time. The recorded 893 // information will be used at runtime for loading the archived app classes. 894 // 895 // Note that at dump time, ClassLoader::_app_classpath_entries are NOT used for 896 // loading app classes. Instead, the app class are loaded by the 897 // jdk/internal/loader/ClassLoaders$AppClassLoader instance. 898 bool ClassLoader::add_to_app_classpath_entries(JavaThread* current, 899 ClassPathEntry* entry, 900 bool check_for_duplicates) { 901 #if INCLUDE_CDS 902 assert(entry != nullptr, "ClassPathEntry should not be nullptr"); 903 ClassPathEntry* e = _app_classpath_entries; 904 if (check_for_duplicates) { 905 while (e != nullptr) { 906 if (strcmp(e->name(), entry->name()) == 0 && 907 e->from_class_path_attr() == entry->from_class_path_attr()) { 908 // entry already exists 909 return false; 910 } 911 e = e->next(); 912 } 913 } 914 915 // The entry does not exist, add to the list 916 if (_app_classpath_entries == nullptr) { 917 assert(_last_app_classpath_entry == nullptr, "Sanity"); 918 _app_classpath_entries = _last_app_classpath_entry = entry; 919 } else { 920 _last_app_classpath_entry->set_next(entry); 921 _last_app_classpath_entry = entry; 922 } 923 924 if (entry->is_jar_file()) { 925 ClassLoaderExt::process_jar_manifest(current, entry); 926 } 927 #endif 928 return true; 929 } 930 931 // Returns true IFF the file/dir exists and the entry was successfully created. 932 bool ClassLoader::update_class_path_entry_list(JavaThread* current, 933 const char *path, 934 bool check_for_duplicates, 935 bool is_boot_append, 936 bool from_class_path_attr) { 937 struct stat st; 938 if (os::stat(path, &st) == 0) { 939 // File or directory found 940 ClassPathEntry* new_entry = nullptr; 941 new_entry = create_class_path_entry(current, path, &st, is_boot_append, from_class_path_attr); 942 if (new_entry == nullptr) { 943 return false; 944 } 945 946 // Do not reorder the bootclasspath which would break get_system_package(). 947 // Add new entry to linked list 948 if (is_boot_append) { 949 add_to_boot_append_entries(new_entry); 950 } else { 951 if (!add_to_app_classpath_entries(current, new_entry, check_for_duplicates)) { 952 // new_entry is not saved, free it now 953 delete new_entry; 954 } 955 } 956 return true; 957 } else { 958 return false; 959 } 960 } 961 962 static void print_module_entry_table(const GrowableArray<ModuleClassPathList*>* const module_list) { 963 ResourceMark rm; 964 int num_of_entries = module_list->length(); 965 for (int i = 0; i < num_of_entries; i++) { 966 ClassPathEntry* e; 967 ModuleClassPathList* mpl = module_list->at(i); 968 tty->print("%s=", mpl->module_name()->as_C_string()); 969 e = mpl->module_first_entry(); 970 while (e != nullptr) { 971 tty->print("%s", e->name()); 972 e = e->next(); 973 if (e != nullptr) { 974 tty->print("%s", os::path_separator()); 975 } 976 } 977 tty->print(" ;"); 978 } 979 } 980 981 void ClassLoader::print_bootclasspath() { 982 ClassPathEntry* e; 983 tty->print("[bootclasspath= "); 984 985 // Print --patch-module module/path specifications first 986 if (_patch_mod_entries != nullptr) { 987 print_module_entry_table(_patch_mod_entries); 988 } 989 990 // [jimage | exploded modules build] 991 if (has_jrt_entry()) { 992 // Print the location of the java runtime image 993 tty->print("%s ;", _jrt_entry->name()); 994 } else { 995 // Print exploded module build path specifications 996 if (_exploded_entries != nullptr) { 997 print_module_entry_table(_exploded_entries); 998 } 999 } 1000 1001 // appended entries 1002 e = first_append_entry(); 1003 while (e != nullptr) { 1004 tty->print("%s ;", e->name()); 1005 e = e->next(); 1006 } 1007 tty->print_cr("]"); 1008 } 1009 1010 void* ClassLoader::dll_lookup(void* lib, const char* name, const char* path) { 1011 void* func = os::dll_lookup(lib, name); 1012 if (func == nullptr) { 1013 char msg[256] = ""; 1014 jio_snprintf(msg, sizeof(msg), "Could not resolve \"%s\"", name); 1015 vm_exit_during_initialization(msg, path); 1016 } 1017 return func; 1018 } 1019 1020 void ClassLoader::load_java_library() { 1021 assert(CanonicalizeEntry == nullptr, "should not load java library twice"); 1022 if (is_vm_statically_linked()) { 1023 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, os::lookup_function("JDK_Canonicalize")); 1024 assert(CanonicalizeEntry != nullptr, "could not lookup JDK_Canonicalize"); 1025 return; 1026 } 1027 1028 void *javalib_handle = os::native_java_library(); 1029 if (javalib_handle == nullptr) { 1030 vm_exit_during_initialization("Unable to load java library", nullptr); 1031 } 1032 1033 CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, dll_lookup(javalib_handle, "JDK_Canonicalize", nullptr)); 1034 assert(CanonicalizeEntry != nullptr, "could not lookup JDK_Canonicalize in java library"); 1035 } 1036 1037 void ClassLoader::load_jimage_library() { 1038 assert(JImageOpen == nullptr, "should not load jimage library twice"); 1039 1040 if (is_vm_statically_linked()) { 1041 JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, os::lookup_function("JIMAGE_Open")); 1042 JImageClose = CAST_TO_FN_PTR(JImageClose_t, os::lookup_function("JIMAGE_Close")); 1043 JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, os::lookup_function("JIMAGE_FindResource")); 1044 JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, os::lookup_function("JIMAGE_GetResource")); 1045 assert(JImageOpen != nullptr && JImageClose != nullptr && 1046 JImageFindResource != nullptr && JImageGetResource != nullptr, 1047 "could not lookup all jimage library functions"); 1048 return; 1049 } 1050 1051 char path[JVM_MAXPATHLEN]; 1052 char ebuf[1024]; 1053 void* handle = nullptr; 1054 if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) { 1055 handle = os::dll_load(path, ebuf, sizeof ebuf); 1056 } 1057 if (handle == nullptr) { 1058 vm_exit_during_initialization("Unable to load jimage library", path); 1059 } 1060 1061 JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path)); 1062 JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path)); 1063 JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path)); 1064 JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path)); 1065 assert(JImageOpen != nullptr && JImageClose != nullptr && 1066 JImageFindResource != nullptr && JImageGetResource != nullptr, 1067 "could not lookup all jimage library functions in jimage library"); 1068 } 1069 1070 int ClassLoader::crc32(int crc, const char* buf, int len) { 1071 return ZipLibrary::crc32(crc, (const jbyte*)buf, len); 1072 } 1073 1074 oop ClassLoader::get_system_package(const char* name, TRAPS) { 1075 // Look up the name in the boot loader's package entry table. 1076 if (name != nullptr) { 1077 TempNewSymbol package_sym = SymbolTable::new_symbol(name); 1078 // Look for the package entry in the boot loader's package entry table. 1079 PackageEntry* package = 1080 ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym); 1081 1082 // Return null if package does not exist or if no classes in that package 1083 // have been loaded. 1084 if (package != nullptr && package->has_loaded_class()) { 1085 ModuleEntry* module = package->module(); 1086 if (module->location() != nullptr) { 1087 ResourceMark rm(THREAD); 1088 Handle ml = java_lang_String::create_from_str( 1089 module->location()->as_C_string(), THREAD); 1090 return ml(); 1091 } 1092 // Return entry on boot loader class path. 1093 Handle cph = java_lang_String::create_from_str( 1094 ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD); 1095 return cph(); 1096 } 1097 } 1098 return nullptr; 1099 } 1100 1101 objArrayOop ClassLoader::get_system_packages(TRAPS) { 1102 ResourceMark rm(THREAD); 1103 // List of pointers to PackageEntrys that have loaded classes. 1104 PackageEntryTable* pe_table = 1105 ClassLoaderData::the_null_class_loader_data()->packages(); 1106 GrowableArray<PackageEntry*>* loaded_class_pkgs = pe_table->get_system_packages(); 1107 1108 // Allocate objArray and fill with java.lang.String 1109 objArrayOop r = oopFactory::new_objArray(vmClasses::String_klass(), 1110 loaded_class_pkgs->length(), CHECK_NULL); 1111 objArrayHandle result(THREAD, r); 1112 for (int x = 0; x < loaded_class_pkgs->length(); x++) { 1113 PackageEntry* package_entry = loaded_class_pkgs->at(x); 1114 Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL); 1115 result->obj_at_put(x, str()); 1116 } 1117 return result(); 1118 } 1119 1120 // caller needs ResourceMark 1121 const char* ClassLoader::file_name_for_class_name(const char* class_name, 1122 int class_name_len) { 1123 assert(class_name != nullptr, "invariant"); 1124 assert((int)strlen(class_name) == class_name_len, "invariant"); 1125 1126 static const char class_suffix[] = ".class"; 1127 size_t class_suffix_len = sizeof(class_suffix); 1128 1129 char* const file_name = NEW_RESOURCE_ARRAY(char, 1130 class_name_len + 1131 class_suffix_len); // includes term null 1132 1133 strncpy(file_name, class_name, class_name_len); 1134 strncpy(&file_name[class_name_len], class_suffix, class_suffix_len); 1135 1136 return file_name; 1137 } 1138 1139 static ClassPathEntry* find_first_module_cpe(ModuleEntry* mod_entry, 1140 const GrowableArray<ModuleClassPathList*>* const module_list) { 1141 int num_of_entries = module_list->length(); 1142 const Symbol* class_module_name = mod_entry->name(); 1143 1144 // Loop through all the modules in either the patch-module or exploded entries looking for module 1145 for (int i = 0; i < num_of_entries; i++) { 1146 ModuleClassPathList* module_cpl = module_list->at(i); 1147 Symbol* module_cpl_name = module_cpl->module_name(); 1148 1149 if (module_cpl_name->fast_compare(class_module_name) == 0) { 1150 // Class' module has been located. 1151 return module_cpl->module_first_entry(); 1152 } 1153 } 1154 return nullptr; 1155 } 1156 1157 1158 // Search the module list for the class file stream based on the file name and java package 1159 ClassFileStream* ClassLoader::search_module_entries(JavaThread* current, 1160 const GrowableArray<ModuleClassPathList*>* const module_list, 1161 PackageEntry* pkg_entry, // Java package entry derived from the class name 1162 const char* const file_name) { 1163 ClassFileStream* stream = nullptr; 1164 1165 // Find the defining module in the boot loader's module entry table 1166 ModuleEntry* mod_entry = (pkg_entry != nullptr) ? pkg_entry->module() : nullptr; 1167 1168 // If the module system has not defined java.base yet, then 1169 // classes loaded are assumed to be defined to java.base. 1170 // When java.base is eventually defined by the module system, 1171 // all packages of classes that have been previously loaded 1172 // are verified in ModuleEntryTable::verify_javabase_packages(). 1173 if (!Universe::is_module_initialized() && 1174 !ModuleEntryTable::javabase_defined() && 1175 mod_entry == nullptr) { 1176 mod_entry = ModuleEntryTable::javabase_moduleEntry(); 1177 } 1178 1179 // The module must be a named module 1180 ClassPathEntry* e = nullptr; 1181 if (mod_entry != nullptr && mod_entry->is_named()) { 1182 if (module_list == _exploded_entries) { 1183 // The exploded build entries can be added to at any time so a lock is 1184 // needed when searching them. 1185 assert(!ClassLoader::has_jrt_entry(), "Must be exploded build"); 1186 MutexLocker ml(current, Module_lock); 1187 e = find_first_module_cpe(mod_entry, module_list); 1188 } else { 1189 e = find_first_module_cpe(mod_entry, module_list); 1190 } 1191 } 1192 1193 // Try to load the class from the module's ClassPathEntry list. 1194 while (e != nullptr) { 1195 stream = e->open_stream(current, file_name); 1196 // No context.check is required since CDS is not supported 1197 // for an exploded modules build or if --patch-module is specified. 1198 if (nullptr != stream) { 1199 return stream; 1200 } 1201 e = e->next(); 1202 } 1203 // If the module was located, break out even if the class was not 1204 // located successfully from that module's ClassPathEntry list. 1205 // There will not be another valid entry for that module. 1206 return nullptr; 1207 } 1208 1209 // Called by the boot classloader to load classes 1210 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) { 1211 assert(name != nullptr, "invariant"); 1212 1213 ResourceMark rm(THREAD); 1214 HandleMark hm(THREAD); 1215 1216 const char* const class_name = name->as_C_string(); 1217 1218 EventMarkClassLoading m("Loading class %s", class_name); 1219 1220 const char* const file_name = file_name_for_class_name(class_name, 1221 name->utf8_length()); 1222 assert(file_name != nullptr, "invariant"); 1223 1224 // Lookup stream for parsing .class file 1225 ClassFileStream* stream = nullptr; 1226 s2 classpath_index = 0; 1227 ClassPathEntry* e = nullptr; 1228 1229 // If search_append_only is true, boot loader visibility boundaries are 1230 // set to be _first_append_entry to the end. This includes: 1231 // [-Xbootclasspath/a]; [jvmti appended entries] 1232 // 1233 // If search_append_only is false, boot loader visibility boundaries are 1234 // set to be the --patch-module entries plus the base piece. This includes: 1235 // [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build] 1236 // 1237 1238 // Load Attempt #1: --patch-module 1239 // Determine the class' defining module. If it appears in the _patch_mod_entries, 1240 // attempt to load the class from those locations specific to the module. 1241 // Specifications to --patch-module can contain a partial number of classes 1242 // that are part of the overall module definition. So if a particular class is not 1243 // found within its module specification, the search should continue to Load Attempt #2. 1244 // Note: The --patch-module entries are never searched if the boot loader's 1245 // visibility boundary is limited to only searching the append entries. 1246 if (_patch_mod_entries != nullptr && !search_append_only) { 1247 assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping"); 1248 stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name); 1249 } 1250 1251 // Load Attempt #2: [jimage | exploded build] 1252 if (!search_append_only && (nullptr == stream)) { 1253 if (has_jrt_entry()) { 1254 e = _jrt_entry; 1255 stream = _jrt_entry->open_stream(THREAD, file_name); 1256 } else { 1257 // Exploded build - attempt to locate class in its defining module's location. 1258 assert(_exploded_entries != nullptr, "No exploded build entries present"); 1259 assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build"); 1260 stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name); 1261 } 1262 } 1263 1264 // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries] 1265 if (search_append_only && (nullptr == stream)) { 1266 // For the boot loader append path search, the starting classpath_index 1267 // for the appended piece is always 1 to account for either the 1268 // _jrt_entry or the _exploded_entries. 1269 assert(classpath_index == 0, "The classpath_index has been incremented incorrectly"); 1270 classpath_index = 1; 1271 1272 e = first_append_entry(); 1273 while (e != nullptr) { 1274 stream = e->open_stream(THREAD, file_name); 1275 if (nullptr != stream) { 1276 break; 1277 } 1278 e = e->next(); 1279 ++classpath_index; 1280 } 1281 } 1282 1283 if (nullptr == stream) { 1284 return nullptr; 1285 } 1286 1287 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 1288 Handle protection_domain; 1289 ClassLoadInfo cl_info(protection_domain); 1290 1291 InstanceKlass* result = KlassFactory::create_from_stream(stream, 1292 name, 1293 loader_data, 1294 cl_info, 1295 CHECK_NULL); 1296 result->set_classpath_index(classpath_index); 1297 return result; 1298 } 1299 1300 #if INCLUDE_CDS 1301 static const char* skip_uri_protocol(const char* source) { 1302 if (strncmp(source, "file:", 5) == 0) { 1303 // file: protocol path could start with file:/ or file:/// 1304 // locate the char after all the forward slashes 1305 int offset = 5; 1306 while (*(source + offset) == '/') { 1307 offset++; 1308 } 1309 source += offset; 1310 // for non-windows platforms, move back one char as the path begins with a '/' 1311 #ifndef _WINDOWS 1312 source -= 1; 1313 #endif 1314 } else if (strncmp(source, "jrt:/", 5) == 0) { 1315 source += 5; 1316 } 1317 return source; 1318 } 1319 1320 static char decode_percent_encoded(const char *str, size_t& index) { 1321 if (str[index] == '%' 1322 && isxdigit(str[index + 1]) 1323 && isxdigit(str[index + 2])) { 1324 char hex[3]; 1325 hex[0] = str[index + 1]; 1326 hex[1] = str[index + 2]; 1327 hex[2] = '\0'; 1328 index += 2; 1329 return (char) strtol(hex, nullptr, 16); 1330 } 1331 return str[index]; 1332 } 1333 1334 char* ClassLoader::uri_to_path(const char* uri) { 1335 const size_t len = strlen(uri) + 1; 1336 char* path = NEW_RESOURCE_ARRAY(char, len); 1337 1338 uri = skip_uri_protocol(uri); 1339 1340 if (strncmp(uri, "//", 2) == 0) { 1341 // Skip the empty "authority" part 1342 uri += 2; 1343 } 1344 1345 #ifdef _WINDOWS 1346 if (uri[0] == '/') { 1347 // Absolute path name on Windows does not begin with a slash 1348 uri += 1; 1349 } 1350 #endif 1351 1352 size_t path_index = 0; 1353 for (size_t i = 0; i < strlen(uri); ++i) { 1354 char decoded = decode_percent_encoded(uri, i); 1355 path[path_index++] = decoded; 1356 } 1357 path[path_index] = '\0'; 1358 return path; 1359 } 1360 1361 // Record the shared classpath index and loader type for classes loaded 1362 // by the builtin loaders at dump time. 1363 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik, 1364 const ClassFileStream* stream, bool redefined) { 1365 assert(CDSConfig::is_dumping_archive(), "sanity"); 1366 assert(stream != nullptr, "sanity"); 1367 1368 if (ik->is_hidden()) { 1369 record_hidden_class(ik); 1370 return; 1371 } 1372 1373 oop loader = ik->class_loader(); 1374 char* src = (char*)stream->source(); 1375 if (src == nullptr) { 1376 if (loader == nullptr) { 1377 // JFR classes 1378 ik->set_shared_classpath_index(0); 1379 ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER); 1380 } 1381 return; 1382 } 1383 1384 assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build"); 1385 1386 ResourceMark rm(current); 1387 int classpath_index = -1; 1388 PackageEntry* pkg_entry = ik->package(); 1389 1390 if (FileMapInfo::get_number_of_shared_paths() > 0) { 1391 // Save the path from the file: protocol or the module name from the jrt: protocol 1392 // if no protocol prefix is found, path is the same as stream->source(). This path 1393 // must be valid since the class has been successfully parsed. 1394 const char* path = ClassLoader::uri_to_path(src); 1395 assert(path != nullptr, "sanity"); 1396 for (int i = 0; i < FileMapInfo::get_number_of_shared_paths(); i++) { 1397 SharedClassPathEntry* ent = FileMapInfo::shared_path(i); 1398 // A shared path has been validated during its creation in ClassLoader::create_class_path_entry(), 1399 // it must be valid here. 1400 assert(ent->name() != nullptr, "sanity"); 1401 // If the path (from the class stream source) is the same as the shared 1402 // class or module path, then we have a match. 1403 // src may come from the App/Platform class loaders, which would canonicalize 1404 // the file name. We cannot use strcmp to check for equality against ent->name(). 1405 // We must use os::same_files (which is faster than canonicalizing ent->name()). 1406 if (os::same_files(ent->name(), path)) { 1407 // null pkg_entry and pkg_entry in an unnamed module implies the class 1408 // is from the -cp or boot loader append path which consists of -Xbootclasspath/a 1409 // and jvmti appended entries. 1410 if ((pkg_entry == nullptr) || (pkg_entry->in_unnamed_module())) { 1411 // Ensure the index is within the -cp range before assigning 1412 // to the classpath_index. 1413 if (SystemDictionary::is_system_class_loader(loader) && 1414 (i >= ClassLoaderExt::app_class_paths_start_index()) && 1415 (i < ClassLoaderExt::app_module_paths_start_index())) { 1416 classpath_index = i; 1417 break; 1418 } else { 1419 if ((i >= 1) && 1420 (i < ClassLoaderExt::app_class_paths_start_index())) { 1421 // The class must be from boot loader append path which consists of 1422 // -Xbootclasspath/a and jvmti appended entries. 1423 assert(loader == nullptr, "sanity"); 1424 classpath_index = i; 1425 break; 1426 } 1427 } 1428 } else { 1429 // A class from a named module from the --module-path. Ensure the index is 1430 // within the --module-path range before assigning to the classpath_index. 1431 if ((pkg_entry != nullptr) && !(pkg_entry->in_unnamed_module()) && (i > 0)) { 1432 if (i >= ClassLoaderExt::app_module_paths_start_index() && 1433 i < FileMapInfo::get_number_of_shared_paths()) { 1434 classpath_index = i; 1435 break; 1436 } 1437 } 1438 } 1439 } 1440 // for index 0 and the stream->source() is the modules image or has the jrt: protocol. 1441 // The class must be from the runtime modules image. 1442 if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) { 1443 classpath_index = i; 1444 break; 1445 } 1446 } 1447 1448 // No path entry found for this class: most likely a shared class loaded by the 1449 // user defined classloader. 1450 if (classpath_index < 0 && !SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) { 1451 assert(ik->shared_classpath_index() < 0, "not assigned yet"); 1452 ik->set_shared_classpath_index(UNREGISTERED_INDEX); 1453 SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream); 1454 return; 1455 } 1456 } else { 1457 // The shared path table is set up after module system initialization. 1458 // The path table contains no entry before that. Any classes loaded prior 1459 // to the setup of the shared path table must be from the modules image. 1460 assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image"); 1461 assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup"); 1462 classpath_index = 0; 1463 } 1464 1465 const char* const class_name = ik->name()->as_C_string(); 1466 const char* const file_name = file_name_for_class_name(class_name, 1467 ik->name()->utf8_length()); 1468 assert(file_name != nullptr, "invariant"); 1469 ClassLoaderExt::record_result(checked_cast<s2>(classpath_index), ik, redefined); 1470 } 1471 1472 void ClassLoader::record_hidden_class(InstanceKlass* ik) { 1473 assert(ik->is_hidden(), "must be"); 1474 1475 s2 classloader_type; 1476 if (HeapShared::is_lambda_form_klass(ik)) { 1477 classloader_type = ClassLoader::BOOT_LOADER; 1478 } else { 1479 oop loader = ik->class_loader(); 1480 1481 if (loader == nullptr) { 1482 classloader_type = ClassLoader::BOOT_LOADER; 1483 } else if (SystemDictionary::is_platform_class_loader(loader)) { 1484 classloader_type = ClassLoader::PLATFORM_LOADER; 1485 } else if (SystemDictionary::is_system_class_loader(loader)) { 1486 classloader_type = ClassLoader::APP_LOADER; 1487 } else { 1488 // This class won't be archived, so no need to update its 1489 // classloader_type/classpath_index. 1490 return; 1491 } 1492 } 1493 ik->set_shared_class_loader_type(classloader_type); 1494 1495 if (HeapShared::is_lambda_proxy_klass(ik)) { 1496 InstanceKlass* nest_host = ik->nest_host_not_null(); 1497 ik->set_shared_classpath_index(nest_host->shared_classpath_index()); 1498 } else if (HeapShared::is_lambda_form_klass(ik)) { 1499 ik->set_shared_classpath_index(0); 1500 } else { 1501 // Generated invoker classes. 1502 if (classloader_type == ClassLoader::APP_LOADER) { 1503 ik->set_shared_classpath_index(ClassLoaderExt::app_class_paths_start_index()); 1504 } else { 1505 ik->set_shared_classpath_index(0); 1506 } 1507 } 1508 } 1509 #endif // INCLUDE_CDS 1510 1511 // Initialize the class loader's access to methods in libzip. Parse and 1512 // process the boot classpath into a list ClassPathEntry objects. Once 1513 // this list has been created, it must not change order (see class PackageInfo) 1514 // it can be appended to and is by jvmti. 1515 1516 void ClassLoader::initialize(TRAPS) { 1517 if (UsePerfData) { 1518 // jvmstat performance counters 1519 NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time"); 1520 NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime"); 1521 NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self"); 1522 NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime"); 1523 NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self"); 1524 NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime"); 1525 NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self"); 1526 NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses"); 1527 NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses"); 1528 NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses"); 1529 NEWPERFEVENTCOUNTER(_perf_class_init_bytecodes_count, SUN_CLS, "clinitBytecodesCount"); 1530 1531 NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime"); 1532 NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime"); 1533 NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime"); 1534 NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self"); 1535 NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount"); 1536 NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses"); 1537 NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime"); 1538 NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self"); 1539 NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes"); 1540 NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes"); 1541 NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls"); 1542 NEWPERFTICKCOUNTER(_perf_secondary_hash_time, SUN_CLS, "secondarySuperHashTime"); 1543 1544 if (log_is_enabled(Info, perf, class, link) || log_is_enabled(Info, init)) { 1545 NEWPERFTICKCOUNTER(_perf_preload_total_time, SUN_CLS, "preloadTotalTime"); 1546 NEWPERFTICKCOUNTER(_perf_preload_time, SUN_CLS, "preloadTime"); 1547 NEWPERFTICKCOUNTER(_perf_prelink_time, SUN_CLS, "prelinkTime"); 1548 NEWPERFTICKCOUNTER(_perf_preinit_time, SUN_CLS, "preinitTime"); 1549 NEWPERFTICKCOUNTER(_perf_preresolve_time, SUN_CLS, "preresolveTime"); 1550 1551 NEWPERFTICKCOUNTER(_perf_ik_link_methods_time, SUN_CLS, "linkMethodsTime"); 1552 NEWPERFTICKCOUNTER(_perf_method_adapters_time, SUN_CLS, "makeAdaptersTime"); 1553 NEWPERFEVENTCOUNTER(_perf_ik_link_methods_count, SUN_CLS, "linkMethodsCount"); 1554 NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount"); 1555 1556 NEWPERFTICKCOUNTERS(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time"); 1557 NEWPERFTICKCOUNTERS(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time"); 1558 NEWPERFTICKCOUNTERS(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time"); 1559 NEWPERFTICKCOUNTERS(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time"); 1560 1561 NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count"); 1562 NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count"); 1563 NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count"); 1564 NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count"); 1565 } 1566 } 1567 1568 // lookup java library entry points 1569 load_java_library(); 1570 // jimage library entry points are loaded below, in lookup_vm_options 1571 setup_bootstrap_search_path(THREAD); 1572 } 1573 1574 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) { 1575 jlong size; 1576 JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size); 1577 if (location == 0) 1578 return nullptr; 1579 char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass); 1580 (*JImageGetResource)(jimage, location, val, size); 1581 val[size] = '\0'; 1582 return val; 1583 } 1584 1585 // Lookup VM options embedded in the modules jimage file 1586 char* ClassLoader::lookup_vm_options() { 1587 jint error; 1588 char modules_path[JVM_MAXPATHLEN]; 1589 const char* fileSep = os::file_separator(); 1590 1591 // Initialize jimage library entry points 1592 load_jimage_library(); 1593 1594 jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep); 1595 JImage_file =(*JImageOpen)(modules_path, &error); 1596 if (JImage_file == nullptr) { 1597 return nullptr; 1598 } 1599 1600 const char *jimage_version = get_jimage_version_string(); 1601 char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options"); 1602 return options; 1603 } 1604 1605 bool ClassLoader::is_module_observable(const char* module_name) { 1606 assert(JImageOpen != nullptr, "jimage library should have been opened"); 1607 if (JImage_file == nullptr) { 1608 struct stat st; 1609 const char *path = get_exploded_module_path(module_name, true); 1610 bool res = os::stat(path, &st) == 0; 1611 FREE_C_HEAP_ARRAY(char, path); 1612 return res; 1613 } 1614 jlong size; 1615 const char *jimage_version = get_jimage_version_string(); 1616 return (*JImageFindResource)(JImage_file, module_name, jimage_version, "module-info.class", &size) != 0; 1617 } 1618 1619 #if INCLUDE_CDS 1620 void ClassLoader::initialize_shared_path(JavaThread* current) { 1621 if (CDSConfig::is_dumping_archive()) { 1622 ClassLoaderExt::setup_search_paths(current); 1623 } 1624 } 1625 1626 void ClassLoader::initialize_module_path(TRAPS) { 1627 if (CDSConfig::is_dumping_archive()) { 1628 ClassLoaderExt::setup_module_paths(THREAD); 1629 FileMapInfo::allocate_shared_path_table(CHECK); 1630 } 1631 } 1632 1633 // Helper function used by CDS code to get the number of module path 1634 // entries during shared classpath setup time. 1635 int ClassLoader::num_module_path_entries() { 1636 assert(CDSConfig::is_dumping_archive(), "sanity"); 1637 int num_entries = 0; 1638 ClassPathEntry* e= ClassLoader::_module_path_entries; 1639 while (e != nullptr) { 1640 num_entries ++; 1641 e = e->next(); 1642 } 1643 return num_entries; 1644 } 1645 #endif 1646 1647 jlong ClassLoader::classloader_time_ms() { 1648 return UsePerfData ? 1649 Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1; 1650 } 1651 1652 jlong ClassLoader::class_init_bytecodes_count() { 1653 return UsePerfData ? _perf_class_init_bytecodes_count->get_value() : -1; 1654 } 1655 1656 jlong ClassLoader::class_init_count() { 1657 return UsePerfData ? _perf_classes_inited->get_value() : -1; 1658 } 1659 1660 jlong ClassLoader::class_init_time_ms() { 1661 return UsePerfData ? 1662 Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1; 1663 } 1664 1665 jlong ClassLoader::class_verify_time_ms() { 1666 return UsePerfData ? 1667 Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1; 1668 } 1669 1670 jlong ClassLoader::class_link_count() { 1671 return UsePerfData ? _perf_classes_linked->get_value() : -1; 1672 } 1673 1674 jlong ClassLoader::class_link_time_ms() { 1675 return UsePerfData ? 1676 Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1; 1677 } 1678 1679 int ClassLoader::compute_Object_vtable() { 1680 // hardwired for JDK1.2 -- would need to duplicate class file parsing 1681 // code to determine actual value from file 1682 // Would be value '11' if finals were in vtable 1683 int JDK_1_2_Object_vtable_size = 5; 1684 return JDK_1_2_Object_vtable_size * vtableEntry::size(); 1685 } 1686 1687 1688 void classLoader_init1() { 1689 EXCEPTION_MARK; 1690 ClassLoader::initialize(THREAD); 1691 if (HAS_PENDING_EXCEPTION) { 1692 vm_exit_during_initialization("ClassLoader::initialize() failed unexpectedly"); 1693 } 1694 } 1695 1696 // Complete the ClassPathEntry setup for the boot loader 1697 void ClassLoader::classLoader_init2(JavaThread* current) { 1698 // Setup the list of module/path pairs for --patch-module processing 1699 // This must be done after the SymbolTable is created in order 1700 // to use fast_compare on module names instead of a string compare. 1701 if (Arguments::get_patch_mod_prefix() != nullptr) { 1702 setup_patch_mod_entries(); 1703 } 1704 1705 // Create the ModuleEntry for java.base (must occur after setup_patch_mod_entries 1706 // to successfully determine if java.base has been patched) 1707 create_javabase(); 1708 1709 // Setup the initial java.base/path pair for the exploded build entries. 1710 // As more modules are defined during module system initialization, more 1711 // entries will be added to the exploded build array. 1712 if (!has_jrt_entry()) { 1713 assert(!CDSConfig::is_dumping_archive(), "not supported with exploded module builds"); 1714 assert(!CDSConfig::is_using_archive(), "UsedSharedSpaces not supported with exploded module builds"); 1715 // Set up the boot loader's _exploded_entries list. Note that this gets 1716 // done before loading any classes, by the same thread that will 1717 // subsequently do the first class load. So, no lock is needed for this. 1718 assert(_exploded_entries == nullptr, "Should only get initialized once"); 1719 _exploded_entries = new (mtModule) 1720 GrowableArray<ModuleClassPathList*>(EXPLODED_ENTRY_SIZE, mtModule); 1721 add_to_exploded_build_list(current, vmSymbols::java_base()); 1722 } 1723 } 1724 1725 char* ClassLoader::get_canonical_path(const char* orig, Thread* thread) { 1726 assert(orig != nullptr, "bad arguments"); 1727 // caller needs to allocate ResourceMark for the following output buffer 1728 char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN); 1729 ResourceMark rm(thread); 1730 // os::native_path writes into orig_copy 1731 char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(orig)+1); 1732 strcpy(orig_copy, orig); 1733 if ((CanonicalizeEntry)(os::native_path(orig_copy), canonical_path, JVM_MAXPATHLEN) < 0) { 1734 return nullptr; 1735 } 1736 return canonical_path; 1737 } 1738 1739 void ClassLoader::create_javabase() { 1740 JavaThread* current = JavaThread::current(); 1741 1742 // Create java.base's module entry for the boot 1743 // class loader prior to loading j.l.Object. 1744 ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data(); 1745 1746 // Get module entry table 1747 ModuleEntryTable* null_cld_modules = null_cld->modules(); 1748 if (null_cld_modules == nullptr) { 1749 vm_exit_during_initialization("No ModuleEntryTable for the boot class loader"); 1750 } 1751 1752 { 1753 MutexLocker ml(current, Module_lock); 1754 if (ModuleEntryTable::javabase_moduleEntry() == nullptr) { // may have been inited by CDS. 1755 ModuleEntry* jb_module = null_cld_modules->locked_create_entry(Handle(), 1756 false, vmSymbols::java_base(), nullptr, nullptr, null_cld); 1757 if (jb_module == nullptr) { 1758 vm_exit_during_initialization("Unable to create ModuleEntry for " JAVA_BASE_NAME); 1759 } 1760 ModuleEntryTable::set_javabase_moduleEntry(jb_module); 1761 } 1762 } 1763 } 1764 1765 // Please keep following two functions at end of this file. With them placed at top or in middle of the file, 1766 // they could get inlined by aggressive compiler, an unknown trick, see bug 6966589. 1767 void PerfClassTraceTime::initialize() { 1768 if (!UsePerfData) return; 1769 1770 if (_eventp != nullptr) { 1771 // increment the event counter 1772 _eventp->inc(); 1773 } 1774 1775 // stop the current active thread-local timer to measure inclusive time 1776 _prev_active_event = -1; 1777 for (int i=0; i < EVENT_TYPE_COUNT; i++) { 1778 if (_timers[i].is_active()) { 1779 assert(_prev_active_event == -1, "should have only one active timer"); 1780 _prev_active_event = i; 1781 _timers[i].stop(); 1782 } 1783 } 1784 1785 if (_recursion_counters == nullptr || (_recursion_counters[_event_type])++ == 0) { 1786 // start the inclusive timer if not recursively called 1787 _t.start(); 1788 } 1789 1790 // start thread-local timer of the given event type 1791 if (!_timers[_event_type].is_active()) { 1792 _timers[_event_type].start(); 1793 } 1794 } 1795 1796 PerfClassTraceTime::~PerfClassTraceTime() { 1797 if (!UsePerfData) return; 1798 1799 // stop the thread-local timer as the event completes 1800 // and resume the thread-local timer of the event next on the stack 1801 _timers[_event_type].stop(); 1802 jlong selftime = _timers[_event_type].ticks(); 1803 1804 if (_prev_active_event >= 0) { 1805 _timers[_prev_active_event].start(); 1806 } 1807 1808 if (_recursion_counters != nullptr && --(_recursion_counters[_event_type]) > 0) return; 1809 1810 // increment the counters only on the leaf call 1811 _t.stop(); 1812 _timep->inc(_t.ticks()); 1813 if (_selftimep != nullptr) { 1814 _selftimep->inc(selftime); 1815 } 1816 // add all class loading related event selftime to the accumulated time counter 1817 ClassLoader::perf_accumulated_time()->inc(selftime); 1818 1819 // reset the timer 1820 _timers[_event_type].reset(); 1821 }