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