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