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