< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page

  79 #include "utilities/events.hpp"
  80 #include "utilities/macros.hpp"
  81 #include "utilities/ostream.hpp"
  82 #include "utilities/utf8.hpp"
  83 
  84 #include <ctype.h>
  85 
  86 // Entry point in java.dll for path canonicalization
  87 
  88 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
  89 
  90 static canonicalize_fn_t CanonicalizeEntry  = nullptr;
  91 
  92 // Entry points for jimage.dll for loading jimage file entries
  93 
  94 static JImageOpen_t                    JImageOpen             = nullptr;
  95 static JImageClose_t                   JImageClose            = nullptr;
  96 static JImageFindResource_t            JImageFindResource     = nullptr;
  97 static JImageGetResource_t             JImageGetResource      = nullptr;
  98 
  99 // JimageFile pointer, or null if exploded JDK build.
 100 static JImageFile*                     JImage_file            = nullptr;
 101 









 102 // Globals
 103 
 104 PerfCounter*    ClassLoader::_perf_accumulated_time = nullptr;
 105 PerfCounter*    ClassLoader::_perf_classes_inited = nullptr;
 106 PerfCounter*    ClassLoader::_perf_class_init_time = nullptr;
 107 PerfCounter*    ClassLoader::_perf_class_init_selftime = nullptr;
 108 PerfCounter*    ClassLoader::_perf_classes_verified = nullptr;
 109 PerfCounter*    ClassLoader::_perf_class_verify_time = nullptr;
 110 PerfCounter*    ClassLoader::_perf_class_verify_selftime = nullptr;
 111 PerfCounter*    ClassLoader::_perf_classes_linked = nullptr;
 112 PerfCounter*    ClassLoader::_perf_class_link_time = nullptr;
 113 PerfCounter*    ClassLoader::_perf_class_link_selftime = nullptr;
 114 PerfCounter*    ClassLoader::_perf_shared_classload_time = nullptr;
 115 PerfCounter*    ClassLoader::_perf_sys_classload_time = nullptr;
 116 PerfCounter*    ClassLoader::_perf_app_classload_time = nullptr;
 117 PerfCounter*    ClassLoader::_perf_app_classload_selftime = nullptr;
 118 PerfCounter*    ClassLoader::_perf_app_classload_count = nullptr;
 119 PerfCounter*    ClassLoader::_perf_define_appclasses = nullptr;
 120 PerfCounter*    ClassLoader::_perf_define_appclass_time = nullptr;
 121 PerfCounter*    ClassLoader::_perf_define_appclass_selftime = nullptr;

 137 PerfCounter*    ClassLoader::_perf_resolve_indy_count = nullptr;
 138 PerfCounter*    ClassLoader::_perf_resolve_invokehandle_count = nullptr;
 139 PerfCounter*    ClassLoader::_perf_resolve_mh_count = nullptr;
 140 PerfCounter*    ClassLoader::_perf_resolve_mt_count = nullptr;
 141 
 142 void ClassLoader::print_counters(outputStream *st) {
 143   st->print_cr("ClassLoader:");
 144   st->print_cr("  clinit:               " JLONG_FORMAT "ms / " JLONG_FORMAT " events", ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
 145   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());
 146   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());
 147   st->print_cr("  resolve...");
 148   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());
 149   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());
 150   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());
 151   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());
 152   st->cr();
 153 }
 154 
 155 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
 156 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
 157 ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
 158 
 159 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
 160 ClassPathEntry* volatile ClassLoader::_last_append_entry  = nullptr;
 161 
 162 // helper routines
 163 #if INCLUDE_CDS
 164 static bool string_starts_with(const char* str, const char* str_to_find) {
 165   size_t str_len = strlen(str);
 166   size_t str_to_find_len = strlen(str_to_find);
 167   if (str_to_find_len > str_len) {
 168     return false;
 169   }
 170   return (strncmp(str, str_to_find, str_to_find_len) == 0);
 171 }
 172 #endif
 173 
 174 static const char* get_jimage_version_string() {
 175   static char version_string[10] = "";
 176   if (version_string[0] == '\0') {
 177     jio_snprintf(version_string, sizeof(version_string), "%d.%d",
 178                  VM_Version::vm_major_version(), VM_Version::vm_minor_version());
 179   }
 180   return (const char*)version_string;
 181 }
 182 
 183 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
 184   size_t str_len = strlen(str);
 185   size_t str_to_find_len = strlen(str_to_find);
 186   if (str_to_find_len > str_len) {
 187     return false;
 188   }
 189   return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
 190 }
 191 
 192 // Used to obtain the package name from a fully qualified class name.
 193 Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {
 194   if (name == nullptr) {
 195     if (bad_class_name != nullptr) {
 196       *bad_class_name = true;
 197     }
 198     return nullptr;
 199   }
 200 
 201   int utf_len = name->utf8_length();
 202   const jbyte* base = (const jbyte*)name->base();

 217     // In this situation, is_same_class_package returns false.
 218     if (*start == JVM_SIGNATURE_CLASS) {
 219       if (bad_class_name != nullptr) {
 220         *bad_class_name = true;
 221       }
 222       return nullptr;
 223     }
 224   }
 225   // A class name could have just the slash character in the name,
 226   // in which case start > end
 227   if (start >= end) {
 228     // No package name
 229     if (bad_class_name != nullptr) {
 230       *bad_class_name = true;
 231     }
 232     return nullptr;
 233   }
 234   return SymbolTable::new_symbol(name, pointer_delta_as_int(start, base), pointer_delta_as_int(end, base));
 235 }
 236 



























































 237 // Given a fully qualified package name, find its defining package in the class loader's
 238 // package entry table.
 239 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
 240   if (pkg_name == nullptr) {
 241     return nullptr;
 242   }
 243   PackageEntryTable* pkgEntryTable = loader_data->packages();
 244   return pkgEntryTable->lookup_only(pkg_name);
 245 }
 246 
 247 const char* ClassPathEntry::copy_path(const char* path) {
 248   char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
 249   strcpy(copy, path);
 250   return copy;
 251 }
 252 
 253 ClassPathDirEntry::~ClassPathDirEntry() {
 254   FREE_C_HEAP_ARRAY(char, _dir);
 255 }
 256 

 355   return buffer;
 356 }
 357 
 358 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
 359   jint filesize;
 360   u1* buffer = open_entry(current, name, &filesize, false);
 361   if (buffer == nullptr) {
 362     return nullptr;
 363   }
 364   if (UsePerfData) {
 365     ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
 366   }
 367   // Resource allocated
 368   return new ClassFileStream(buffer,
 369                              filesize,
 370                              _zip_name);
 371 }
 372 
 373 DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;)
 374 
 375 JImageFile* ClassPathImageEntry::jimage() const {
 376   return JImage_file;
 377 }
 378 
 379 JImageFile* ClassPathImageEntry::jimage_non_null() const {
 380   assert(ClassLoader::has_jrt_entry(), "must be");
 381   assert(jimage() != nullptr, "should have been opened by ClassLoader::lookup_vm_options "
 382                            "and remained throughout normal JVM lifetime");
 383   return jimage();
 384 }
 385 
 386 void ClassPathImageEntry::close_jimage() {
 387   if (jimage() != nullptr) {
 388     (*JImageClose)(jimage());
 389     JImage_file = nullptr;
 390   }
 391 }
 392 
 393 ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) :
 394   ClassPathEntry() {
 395   guarantee(jimage != nullptr, "jimage file is null");
 396   guarantee(name != nullptr, "jimage file name is null");

 397   assert(_singleton == nullptr, "VM supports only one jimage");
 398   DEBUG_ONLY(_singleton = this);
 399   size_t len = strlen(name) + 1;
 400   _name = copy_path(name);
 401 }
 402 
 403 ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
 404   return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
 405 }
 406 
 407 // For a class in a named module, look it up in the jimage file using this syntax:
 408 //    /<module-name>/<package-name>/<base-class>
 409 //
 410 // Assumptions:
 411 //     1. There are no unnamed modules in the jimage file.
 412 //     2. A package is in at most one module in the jimage file.
 413 //
 414 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {


 415   jlong size;
 416   JImageLocationRef location = 0;
 417 
 418   TempNewSymbol class_name = SymbolTable::new_symbol(name);
 419   TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
 420 
 421   if (pkg_name != nullptr) {
 422     if (!Universe::is_module_initialized()) {
 423       location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);
 424     } else {
 425       PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
 426       if (package_entry != nullptr) {
 427         ResourceMark rm(current);
 428         // Get the module name
 429         ModuleEntry* module = package_entry->module();
 430         assert(module != nullptr, "Boot classLoader package missing module");
 431         assert(module->is_named(), "Boot classLoader package is in unnamed module");
 432         const char* module_name = module->name()->as_C_string();
 433         if (module_name != nullptr) {
 434           location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);
 435         }
 436       }
 437     }
 438   }
 439 
 440   if (location != 0) {
 441     if (UsePerfData) {
 442       ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
 443     }
 444     char* data = NEW_RESOURCE_ARRAY(char, size);
 445     (*JImageGetResource)(jimage_non_null(), location, data, size);
 446     // Resource allocated
 447     assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");
 448     return new ClassFileStream((u1*)data,
 449                                checked_cast<int>(size),
 450                                _name,
 451                                true); // from_boot_loader_modules_image
 452   }
 453 
 454   return nullptr;
 455 }
 456 
 457 JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,
 458                                                     const char* module_name,
 459                                                     const char* file_name,
 460                                                     jlong &size) {
 461   return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));
 462 }
 463 
 464 bool ClassPathImageEntry::is_modules_image() const {
 465   assert(this == _singleton, "VM supports a single jimage");
 466   assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");
 467   return true;
 468 }
 469 
 470 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
 471   _module_name = module_name;
 472   _module_first_entry = nullptr;
 473   _module_last_entry = nullptr;
 474 }
 475 
 476 ModuleClassPathList::~ModuleClassPathList() {
 477   // Clean out each ClassPathEntry on list
 478   ClassPathEntry* e = _module_first_entry;
 479   while (e != nullptr) {
 480     ClassPathEntry* next_entry = e->next();
 481     delete e;
 482     e = next_entry;
 483   }
 484 }
 485 
 486 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {

 601 #if INCLUDE_CDS
 602   if (CDSConfig::is_dumping_archive()) {
 603     if (!Arguments::has_jimage()) {
 604       vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
 605     }
 606   }
 607 #endif
 608 
 609   while (cp_stream.has_next()) {
 610     const char* path = cp_stream.get_next();
 611 
 612     if (set_base_piece) {
 613       // The first time through the bootstrap_search setup, it must be determined
 614       // what the base or core piece of the boot loader search is.  Either a java runtime
 615       // image is present or this is an exploded module build situation.
 616       assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
 617              "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
 618       struct stat st;
 619       if (os::stat(path, &st) == 0) {
 620         // Directory found
 621         if (JImage_file != nullptr) {
 622           assert(Arguments::has_jimage(), "sanity check");
 623           const char* canonical_path = get_canonical_path(path, current);
 624           assert(canonical_path != nullptr, "canonical_path issue");
 625 
 626           _jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);


 627           assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");
 628           assert(_jrt_entry->jimage() != nullptr, "No java runtime image");
 629         } // else it's an exploded build.
 630       } else {
 631         // If path does not exist, exit
 632         vm_exit_during_initialization("Unable to establish the boot loader search path", path);
 633       }
 634       set_base_piece = false;
 635     } else {
 636       // Every entry on the boot class path after the initial base piece,
 637       // which is set by os::set_boot_path(), is considered an appended entry.
 638       update_class_path_entry_list(current, path);
 639     }
 640   }
 641 }
 642 
 643 // Gets the exploded path for the named module. The memory for the path
 644 // is allocated on the C heap if `c_heap` is true otherwise in the resource area.
 645 static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
 646   const char *home = Arguments::get_java_home();
 647   const char file_sep = os::file_separator()[0];
 648   // 10 represents the length of "modules" + 2 file separators + \0
 649   size_t len = strlen(home) + strlen(module_name) + 10;
 650   char *path = c_heap ? NEW_C_HEAP_ARRAY(char, len, mtModule) : NEW_RESOURCE_ARRAY(char, len);
 651   jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);
 652   return path;
 653 }
 654 










 655 // During an exploded modules build, each module defined to the boot loader
 656 // will be added to the ClassLoader::_exploded_entries array.
 657 void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {
 658   assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");
 659   assert(_exploded_entries != nullptr, "_exploded_entries was not initialized");
 660 
 661   // Find the module's symbol
 662   ResourceMark rm(current);
 663   const char *module_name = module_sym->as_C_string();
 664   const char *path = get_exploded_module_path(module_name, false);
 665 
 666   struct stat st;
 667   if (os::stat(path, &st) == 0) {
 668     // Directory found
 669     ClassPathEntry* new_entry = create_class_path_entry(current, path, &st);
 670 
 671     // If the path specification is valid, enter it into this module's list.
 672     // There is no need to check for duplicate modules in the exploded entry list,
 673     // since no two modules with the same name can be defined to the boot loader.
 674     // This is checked at module definition time in Modules::define_module.
 675     if (new_entry != nullptr) {
 676       ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);















 677       module_cpl->add_to_list(new_entry);
 678       {
 679         MutexLocker ml(current, Module_lock);
 680         _exploded_entries->push(module_cpl);
 681       }
 682       log_info(class, load)("path: %s", path);
 683     }
 684   }
 685 }
 686 
 687 jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {
 688   // enable call to C land
 689   ThreadToNativeFromVM ttn(thread);
 690   HandleMark hm(thread);
 691   return ZipLibrary::open(canonical_path, error_msg);
 692 }
 693 
 694 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
 695                                                      const char *path, const struct stat* st) {
 696   ClassPathEntry* new_entry = nullptr;
 697   if ((st->st_mode & S_IFMT) == S_IFREG) {
 698     ResourceMark rm(current);
 699     // Regular file, should be a zip file
 700     // Canonicalized filename
 701     const char* canonical_path = get_canonical_path(path, current);
 702     if (canonical_path == nullptr) {

 900 
 901     // Return null if package does not exist or if no classes in that package
 902     // have been loaded.
 903     if (package != nullptr && package->has_loaded_class()) {
 904       ModuleEntry* module = package->module();
 905       if (module->location() != nullptr) {
 906         ResourceMark rm(THREAD);
 907         Handle ml = java_lang_String::create_from_str(
 908           module->location()->as_C_string(), THREAD);
 909         return ml();
 910       }
 911       // Return entry on boot loader class path.
 912       Handle cph = java_lang_String::create_from_str(
 913         ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD);
 914       return cph();
 915     }
 916   }
 917   return nullptr;
 918 }
 919 
 920 objArrayOop ClassLoader::get_system_packages(TRAPS) {
 921   ResourceMark rm(THREAD);
 922   // List of pointers to PackageEntrys that have loaded classes.
 923   PackageEntryTable* pe_table =
 924       ClassLoaderData::the_null_class_loader_data()->packages();
 925   GrowableArray<PackageEntry*>* loaded_class_pkgs = pe_table->get_system_packages();
 926 
 927   // Allocate objArray and fill with java.lang.String
 928   objArrayOop r = oopFactory::new_objArray(vmClasses::String_klass(),
 929                                            loaded_class_pkgs->length(), CHECK_NULL);
 930   objArrayHandle result(THREAD, r);

 931   for (int x = 0; x < loaded_class_pkgs->length(); x++) {
 932     PackageEntry* package_entry = loaded_class_pkgs->at(x);
 933     Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL);
 934     result->obj_at_put(x, str());
 935   }
 936   return result();
 937 }
 938 
 939 // caller needs ResourceMark
 940 const char* ClassLoader::file_name_for_class_name(const char* class_name,
 941                                                   int class_name_len) {
 942   assert(class_name != nullptr, "invariant");
 943   assert((int)strlen(class_name) == class_name_len, "invariant");
 944 
 945   static const char class_suffix[] = ".class";
 946   size_t class_suffix_len = sizeof(class_suffix);
 947 
 948   char* const file_name = NEW_RESOURCE_ARRAY(char,
 949                                              class_name_len +
 950                                              class_suffix_len); // includes term null

1027 
1028 // Called by the boot classloader to load classes
1029 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1030   assert(name != nullptr, "invariant");
1031 
1032   ResourceMark rm(THREAD);
1033   HandleMark hm(THREAD);
1034 
1035   const char* const class_name = name->as_C_string();
1036 
1037   EventMarkClassLoading m("Loading class %s", class_name);
1038 
1039   const char* const file_name = file_name_for_class_name(class_name,
1040                                                          name->utf8_length());
1041   assert(file_name != nullptr, "invariant");
1042 
1043   // Lookup stream for parsing .class file
1044   ClassFileStream* stream = nullptr;
1045   s2 classpath_index = 0;
1046   ClassPathEntry* e = nullptr;

1047 
1048   // If search_append_only is true, boot loader visibility boundaries are
1049   // set to be _first_append_entry to the end. This includes:
1050   //   [-Xbootclasspath/a]; [jvmti appended entries]
1051   //
1052   // If search_append_only is false, boot loader visibility boundaries are
1053   // set to be the --patch-module entries plus the base piece. This includes:
1054   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1055   //
1056 
1057   // Load Attempt #1: --patch-module
1058   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1059   // attempt to load the class from those locations specific to the module.
1060   // Specifications to --patch-module can contain a partial number of classes
1061   // that are part of the overall module definition.  So if a particular class is not
1062   // found within its module specification, the search should continue to Load Attempt #2.
1063   // Note: The --patch-module entries are never searched if the boot loader's
1064   //       visibility boundary is limited to only searching the append entries.
1065   if (_patch_mod_entries != nullptr && !search_append_only) {
1066     assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1067     stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);














1068   }
1069 
1070   // Load Attempt #2: [jimage | exploded build]
1071   if (!search_append_only && (nullptr == stream)) {
1072     if (has_jrt_entry()) {
1073       e = _jrt_entry;
1074       stream = _jrt_entry->open_stream(THREAD, file_name);
1075     } else {
1076       // Exploded build - attempt to locate class in its defining module's location.
1077       assert(_exploded_entries != nullptr, "No exploded build entries present");
1078       assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1079       stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1080     }
1081   }
1082 
1083   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1084   if (search_append_only && (nullptr == stream)) {
1085     // For the boot loader append path search, the starting classpath_index
1086     // for the appended piece is always 1 to account for either the
1087     // _jrt_entry or the _exploded_entries.

1096       }
1097       e = e->next();
1098       ++classpath_index;
1099     }
1100   }
1101 
1102   if (nullptr == stream) {
1103     return nullptr;
1104   }
1105 
1106   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1107   Handle protection_domain;
1108   ClassLoadInfo cl_info(protection_domain);
1109 
1110   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1111                                                            name,
1112                                                            loader_data,
1113                                                            cl_info,
1114                                                            CHECK_NULL);
1115   result->set_classpath_index(classpath_index);



1116   return result;
1117 }
1118 
1119 #if INCLUDE_CDS
1120 static const char* skip_uri_protocol(const char* source) {
1121   if (strncmp(source, "file:", 5) == 0) {
1122     // file: protocol path could start with file:/ or file:///
1123     // locate the char after all the forward slashes
1124     int offset = 5;
1125     while (*(source + offset) == '/') {
1126         offset++;
1127     }
1128     source += offset;
1129   // for non-windows platforms, move back one char as the path begins with a '/'
1130 #ifndef _WINDOWS
1131     source -= 1;
1132 #endif
1133   } else if (strncmp(source, "jrt:/", 5) == 0) {
1134     source += 5;
1135   }

1172   for (size_t i = 0; i < strlen(uri); ++i) {
1173     char decoded = decode_percent_encoded(uri, i);
1174     path[path_index++] = decoded;
1175   }
1176   path[path_index] = '\0';
1177   return path;
1178 }
1179 
1180 // Record the shared classpath index and loader type for classes loaded
1181 // by the builtin loaders at dump time.
1182 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1183                                 const ClassFileStream* stream, bool redefined) {
1184   assert(CDSConfig::is_dumping_archive(), "sanity");
1185   assert(stream != nullptr, "sanity");
1186 
1187   if (ik->is_hidden()) {
1188     record_hidden_class(ik);
1189     return;
1190   }
1191 




1192   oop loader = ik->class_loader();
1193   char* src = (char*)stream->source();
1194   if (src == nullptr) {
1195     ik->set_shared_classpath_index(-1); // unsupported location
1196     return;
1197   }
1198 
1199   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1200     // A class loaded by a user-defined classloader.
1201     assert(ik->shared_classpath_index() < 0, "not assigned yet");
1202     ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1203     SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1204     return;
1205   }
1206 
1207   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1208 
1209   ResourceMark rm(current);
1210   int classpath_index = -1;
1211   bool found_invalid = false;

1380       NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1381 
1382       NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1383       NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1384       NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1385       NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1386 
1387       NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1388       NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1389       NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1390       NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1391     }
1392   }
1393 
1394   // lookup java library entry points
1395   load_java_library();
1396   // jimage library entry points are loaded below, in lookup_vm_options
1397   setup_bootstrap_search_path(THREAD);
1398 }
1399 
1400 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1401   jlong size;
1402   JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1403   if (location == 0)
1404     return nullptr;
1405   char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1406   (*JImageGetResource)(jimage, location, val, size);
1407   val[size] = '\0';
1408   return val;
1409 }
1410 
1411 // Lookup VM options embedded in the modules jimage file
1412 char* ClassLoader::lookup_vm_options() {
1413   jint error;
1414   char modules_path[JVM_MAXPATHLEN];
1415   const char* fileSep = os::file_separator();
1416 
1417   // Initialize jimage library entry points
1418   load_jimage_library();
1419 
1420   jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1421   JImage_file =(*JImageOpen)(modules_path, &error);
1422   if (JImage_file == nullptr) {
1423     if (Arguments::has_jimage()) {
1424       // The modules file exists but is unreadable or corrupt
1425       vm_exit_during_initialization(err_msg("Unable to load %s", modules_path));







1426     }
1427     return nullptr;
1428   }


1429 
1430   const char *jimage_version = get_jimage_version_string();
1431   char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options");
1432   return options;

1433 }
1434 
1435 bool ClassLoader::is_module_observable(const char* module_name) {
1436   assert(JImageOpen != nullptr, "jimage library should have been opened");
1437   if (JImage_file == nullptr) {
1438     struct stat st;
1439     const char *path = get_exploded_module_path(module_name, true);
1440     bool res = os::stat(path, &st) == 0;
1441     FREE_C_HEAP_ARRAY(char, path);
1442     return res;
1443   }

1444   jlong size;
1445   const char *jimage_version = get_jimage_version_string();
1446   return (*JImageFindResource)(JImage_file, module_name, jimage_version, "module-info.class", &size) != 0;
1447 }
1448 
1449 jlong ClassLoader::classloader_time_ms() {
1450   return UsePerfData ?
1451     Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1452 }
1453 
1454 jlong ClassLoader::class_init_count() {
1455   return UsePerfData ? _perf_classes_inited->get_value() : -1;
1456 }
1457 
1458 jlong ClassLoader::class_init_time_ms() {
1459   return UsePerfData ?
1460     Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1461 }
1462 
1463 jlong ClassLoader::class_verify_time_ms() {
1464   return UsePerfData ?
1465     Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1466 }

  79 #include "utilities/events.hpp"
  80 #include "utilities/macros.hpp"
  81 #include "utilities/ostream.hpp"
  82 #include "utilities/utf8.hpp"
  83 
  84 #include <ctype.h>
  85 
  86 // Entry point in java.dll for path canonicalization
  87 
  88 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
  89 
  90 static canonicalize_fn_t CanonicalizeEntry  = nullptr;
  91 
  92 // Entry points for jimage.dll for loading jimage file entries
  93 
  94 static JImageOpen_t                    JImageOpen             = nullptr;
  95 static JImageClose_t                   JImageClose            = nullptr;
  96 static JImageFindResource_t            JImageFindResource     = nullptr;
  97 static JImageGetResource_t             JImageGetResource      = nullptr;
  98 
  99 // JImageFile pointer, or null if exploded JDK build.
 100 static JImageFile*                     JImage_file            = nullptr;
 101 
 102 // PreviewMode status to control preview behaviour. JImage_file is unusable
 103 // for normal lookup until (Preview_mode != PREVIEW_MODE_UNINITIALIZED).
 104 enum PreviewMode {
 105   PREVIEW_MODE_UNINITIALIZED = 0,
 106   PREVIEW_MODE_DEFAULT = 1,
 107   PREVIEW_MODE_ENABLE_PREVIEW = 2
 108 };
 109 static PreviewMode                     Preview_mode           = PREVIEW_MODE_UNINITIALIZED;
 110 
 111 // Globals
 112 
 113 PerfCounter*    ClassLoader::_perf_accumulated_time = nullptr;
 114 PerfCounter*    ClassLoader::_perf_classes_inited = nullptr;
 115 PerfCounter*    ClassLoader::_perf_class_init_time = nullptr;
 116 PerfCounter*    ClassLoader::_perf_class_init_selftime = nullptr;
 117 PerfCounter*    ClassLoader::_perf_classes_verified = nullptr;
 118 PerfCounter*    ClassLoader::_perf_class_verify_time = nullptr;
 119 PerfCounter*    ClassLoader::_perf_class_verify_selftime = nullptr;
 120 PerfCounter*    ClassLoader::_perf_classes_linked = nullptr;
 121 PerfCounter*    ClassLoader::_perf_class_link_time = nullptr;
 122 PerfCounter*    ClassLoader::_perf_class_link_selftime = nullptr;
 123 PerfCounter*    ClassLoader::_perf_shared_classload_time = nullptr;
 124 PerfCounter*    ClassLoader::_perf_sys_classload_time = nullptr;
 125 PerfCounter*    ClassLoader::_perf_app_classload_time = nullptr;
 126 PerfCounter*    ClassLoader::_perf_app_classload_selftime = nullptr;
 127 PerfCounter*    ClassLoader::_perf_app_classload_count = nullptr;
 128 PerfCounter*    ClassLoader::_perf_define_appclasses = nullptr;
 129 PerfCounter*    ClassLoader::_perf_define_appclass_time = nullptr;
 130 PerfCounter*    ClassLoader::_perf_define_appclass_selftime = nullptr;

 146 PerfCounter*    ClassLoader::_perf_resolve_indy_count = nullptr;
 147 PerfCounter*    ClassLoader::_perf_resolve_invokehandle_count = nullptr;
 148 PerfCounter*    ClassLoader::_perf_resolve_mh_count = nullptr;
 149 PerfCounter*    ClassLoader::_perf_resolve_mt_count = nullptr;
 150 
 151 void ClassLoader::print_counters(outputStream *st) {
 152   st->print_cr("ClassLoader:");
 153   st->print_cr("  clinit:               " JLONG_FORMAT "ms / " JLONG_FORMAT " events", ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
 154   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());
 155   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());
 156   st->print_cr("  resolve...");
 157   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());
 158   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());
 159   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());
 160   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());
 161   st->cr();
 162 }
 163 
 164 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
 165 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
 166 ClassPathImageEntry* ClassLoader::_jrt_entry = nullptr;
 167 
 168 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
 169 ClassPathEntry* volatile ClassLoader::_last_append_entry  = nullptr;
 170 
 171 // helper routines
 172 #if INCLUDE_CDS
 173 static bool string_starts_with(const char* str, const char* str_to_find) {
 174   size_t str_len = strlen(str);
 175   size_t str_to_find_len = strlen(str_to_find);
 176   if (str_to_find_len > str_len) {
 177     return false;
 178   }
 179   return (strncmp(str, str_to_find, str_to_find_len) == 0);
 180 }
 181 #endif
 182 









 183 bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {
 184   size_t str_len = strlen(str);
 185   size_t str_to_find_len = strlen(str_to_find);
 186   if (str_to_find_len > str_len) {
 187     return false;
 188   }
 189   return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);
 190 }
 191 
 192 // Used to obtain the package name from a fully qualified class name.
 193 Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {
 194   if (name == nullptr) {
 195     if (bad_class_name != nullptr) {
 196       *bad_class_name = true;
 197     }
 198     return nullptr;
 199   }
 200 
 201   int utf_len = name->utf8_length();
 202   const jbyte* base = (const jbyte*)name->base();

 217     // In this situation, is_same_class_package returns false.
 218     if (*start == JVM_SIGNATURE_CLASS) {
 219       if (bad_class_name != nullptr) {
 220         *bad_class_name = true;
 221       }
 222       return nullptr;
 223     }
 224   }
 225   // A class name could have just the slash character in the name,
 226   // in which case start > end
 227   if (start >= end) {
 228     // No package name
 229     if (bad_class_name != nullptr) {
 230       *bad_class_name = true;
 231     }
 232     return nullptr;
 233   }
 234   return SymbolTable::new_symbol(name, pointer_delta_as_int(start, base), pointer_delta_as_int(end, base));
 235 }
 236 
 237 // --------------------------------
 238 // The following jimage_xxx static functions encapsulate all JImage_file and Preview_mode access.
 239 // This is done to make it easy to reason about the JImage file state (exists vs initialized etc.).
 240 
 241 // Opens the named JImage file and sets the JImage file reference.
 242 // Returns true if opening the JImage file was successful (see also jimage_exists()).
 243 static bool jimage_open(const char* modules_path) {
 244   // Currently 'error' is not set to anything useful, so ignore it here.
 245   jint error;
 246   JImage_file = (*JImageOpen)(modules_path, &error);
 247   return JImage_file != nullptr;
 248 }
 249 
 250 // Closes and clears the JImage file reference (this will only be called during shutdown).
 251 static void jimage_close() {
 252   if (JImage_file != nullptr) {
 253     (*JImageClose)(JImage_file);
 254     JImage_file = nullptr;
 255   }
 256 }
 257 
 258 // Returns whether a JImage file was opened (but NOT whether it was initialized yet).
 259 static bool jimage_exists() {
 260   return JImage_file != nullptr;
 261 }
 262 
 263 // Returns the JImage file reference (which may or may not be initialized).
 264 static JImageFile* jimage_non_null() {
 265   assert(jimage_exists(), "should have been opened by ClassLoader::lookup_vm_options "
 266                           "and remained throughout normal JVM lifetime");
 267   return JImage_file;
 268 }
 269 
 270 // Returns true if jimage_init() has been called. Once the JImage file is initialized,
 271 // jimage_is_preview_enabled() can be called to correctly determine the access mode.
 272 static bool jimage_is_initialized() {
 273   return jimage_exists() && Preview_mode != PREVIEW_MODE_UNINITIALIZED;
 274 }
 275 
 276 // Returns the access mode for an initialized JImage file (reflects --enable-preview).
 277 static bool is_preview_enabled() {
 278   return Preview_mode == PREVIEW_MODE_ENABLE_PREVIEW;
 279 }
 280 
 281 // Looks up the location of a named JImage resource. This "raw" lookup function allows
 282 // the preview mode to be manually specified, so must not be accessible outside this
 283 // class. ClassPathImageEntry manages all calls for resources after startup is complete.
 284 static JImageLocationRef jimage_find_resource(const char* module_name,
 285                                               const char* file_name,
 286                                               bool is_preview,
 287                                               jlong *size) {
 288   return ((*JImageFindResource)(jimage_non_null(),
 289                                 module_name,
 290                                 file_name,
 291                                 is_preview,
 292                                 size));
 293 }
 294 // --------------------------------
 295 
 296 // Given a fully qualified package name, find its defining package in the class loader's
 297 // package entry table.
 298 PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {
 299   if (pkg_name == nullptr) {
 300     return nullptr;
 301   }
 302   PackageEntryTable* pkgEntryTable = loader_data->packages();
 303   return pkgEntryTable->lookup_only(pkg_name);
 304 }
 305 
 306 const char* ClassPathEntry::copy_path(const char* path) {
 307   char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);
 308   strcpy(copy, path);
 309   return copy;
 310 }
 311 
 312 ClassPathDirEntry::~ClassPathDirEntry() {
 313   FREE_C_HEAP_ARRAY(char, _dir);
 314 }
 315 

 414   return buffer;
 415 }
 416 
 417 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
 418   jint filesize;
 419   u1* buffer = open_entry(current, name, &filesize, false);
 420   if (buffer == nullptr) {
 421     return nullptr;
 422   }
 423   if (UsePerfData) {
 424     ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);
 425   }
 426   // Resource allocated
 427   return new ClassFileStream(buffer,
 428                              filesize,
 429                              _zip_name);
 430 }
 431 
 432 DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = nullptr;)
 433 











 434 void ClassPathImageEntry::close_jimage() {
 435   jimage_close();



 436 }
 437 
 438 ClassPathImageEntry::ClassPathImageEntry(const char* name) :
 439   ClassPathEntry() {
 440   guarantee(jimage_is_initialized(), "jimage is not initialized");
 441   guarantee(name != nullptr, "jimage file name is null");
 442 
 443   assert(_singleton == nullptr, "VM supports only one jimage");
 444   DEBUG_ONLY(_singleton = this);
 445   size_t len = strlen(name) + 1;
 446   _name = copy_path(name);
 447 }
 448 
 449 ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {
 450   return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());
 451 }
 452 
 453 // For a class in a named module, look it up in the jimage file using this syntax:
 454 //    /<module-name>/<package-name>/<base-class>
 455 //
 456 // Assumptions:
 457 //     1. There are no unnamed modules in the jimage file.
 458 //     2. A package is in at most one module in the jimage file.
 459 //
 460 ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {
 461   const bool is_preview = is_preview_enabled();
 462 
 463   jlong size;
 464   JImageLocationRef location = 0;
 465 
 466   TempNewSymbol class_name = SymbolTable::new_symbol(name);
 467   TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);
 468 
 469   if (pkg_name != nullptr) {
 470     if (!Universe::is_module_initialized()) {
 471       location = jimage_find_resource(JAVA_BASE_NAME, name, is_preview, &size);
 472     } else {
 473       PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);
 474       if (package_entry != nullptr) {
 475         ResourceMark rm(current);
 476         // Get the module name
 477         ModuleEntry* module = package_entry->module();
 478         assert(module != nullptr, "Boot classLoader package missing module");
 479         assert(module->is_named(), "Boot classLoader package is in unnamed module");
 480         const char* module_name = module->name()->as_C_string();
 481         if (module_name != nullptr) {
 482           location = jimage_find_resource(module_name, name, is_preview, &size);
 483         }
 484       }
 485     }
 486   }
 487 
 488   if (location != 0) {
 489     if (UsePerfData) {
 490       ClassLoader::perf_sys_classfile_bytes_read()->inc(size);
 491     }
 492     char* data = NEW_RESOURCE_ARRAY(char, size);
 493     (*JImageGetResource)(jimage_non_null(), location, data, size);
 494     // Resource allocated
 495     assert(this == ClassLoader::get_jrt_entry(), "must be");
 496     return new ClassFileStream((u1*)data,
 497                                checked_cast<int>(size),
 498                                _name,
 499                                true); // from_boot_loader_modules_image
 500   }
 501 
 502   return nullptr;
 503 }
 504 







 505 bool ClassPathImageEntry::is_modules_image() const {
 506   assert(this == _singleton, "VM supports a single jimage");
 507   assert(this == ClassLoader::get_jrt_entry(), "must be used for jrt entry");
 508   return true;
 509 }
 510 
 511 ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {
 512   _module_name = module_name;
 513   _module_first_entry = nullptr;
 514   _module_last_entry = nullptr;
 515 }
 516 
 517 ModuleClassPathList::~ModuleClassPathList() {
 518   // Clean out each ClassPathEntry on list
 519   ClassPathEntry* e = _module_first_entry;
 520   while (e != nullptr) {
 521     ClassPathEntry* next_entry = e->next();
 522     delete e;
 523     e = next_entry;
 524   }
 525 }
 526 
 527 void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {

 642 #if INCLUDE_CDS
 643   if (CDSConfig::is_dumping_archive()) {
 644     if (!Arguments::has_jimage()) {
 645       vm_exit_during_initialization("CDS is not supported in exploded JDK build", nullptr);
 646     }
 647   }
 648 #endif
 649 
 650   while (cp_stream.has_next()) {
 651     const char* path = cp_stream.get_next();
 652 
 653     if (set_base_piece) {
 654       // The first time through the bootstrap_search setup, it must be determined
 655       // what the base or core piece of the boot loader search is.  Either a java runtime
 656       // image is present or this is an exploded module build situation.
 657       assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),
 658              "Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");
 659       struct stat st;
 660       if (os::stat(path, &st) == 0) {
 661         // Directory found
 662         if (jimage_exists()) {
 663           assert(Arguments::has_jimage(), "sanity check");
 664           const char* canonical_path = get_canonical_path(path, current);
 665           assert(canonical_path != nullptr, "canonical_path issue");
 666 
 667           // Hand over lifecycle control of the JImage file to the _jrt_entry singleton
 668           // (see ClassPathImageEntry::close_jimage). The image must be initialized by now.
 669           _jrt_entry = new ClassPathImageEntry(canonical_path);
 670           assert(_jrt_entry != nullptr && _jrt_entry->is_modules_image(), "No java runtime image present");

 671         } // else it's an exploded build.
 672       } else {
 673         // If path does not exist, exit
 674         vm_exit_during_initialization("Unable to establish the boot loader search path", path);
 675       }
 676       set_base_piece = false;
 677     } else {
 678       // Every entry on the boot class path after the initial base piece,
 679       // which is set by os::set_boot_path(), is considered an appended entry.
 680       update_class_path_entry_list(current, path);
 681     }
 682   }
 683 }
 684 
 685 // Gets the exploded path for the named module. The memory for the path
 686 // is allocated on the C heap if `c_heap` is true otherwise in the resource area.
 687 static const char* get_exploded_module_path(const char* module_name, bool c_heap) {
 688   const char *home = Arguments::get_java_home();
 689   const char file_sep = os::file_separator()[0];
 690   // 10 represents the length of "modules" (7) + 2 file separators + \0
 691   size_t len = strlen(home) + strlen(module_name) + 10;
 692   char *path = c_heap ? NEW_C_HEAP_ARRAY(char, len, mtModule) : NEW_RESOURCE_ARRAY(char, len);
 693   jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);
 694   return path;
 695 }
 696 
 697 // Gets a preview path for a given class path as a resource.
 698 static const char* get_preview_path(const char* path) {
 699   const char file_sep = os::file_separator()[0];
 700   // 18 represents the length of "META-INF" (8) + "preview" (7) + 2 file separators + \0
 701   size_t len = strlen(path) + 18;
 702   char *preview_path = NEW_RESOURCE_ARRAY(char, len);
 703   jio_snprintf(preview_path, len, "%s%cMETA-INF%cpreview", path, file_sep, file_sep);
 704   return preview_path;
 705 }
 706 
 707 // During an exploded modules build, each module defined to the boot loader
 708 // will be added to the ClassLoader::_exploded_entries array.
 709 void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {
 710   assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");
 711   assert(_exploded_entries != nullptr, "_exploded_entries was not initialized");
 712 
 713   // Find the module's symbol
 714   ResourceMark rm(current);
 715   const char *module_name = module_sym->as_C_string();
 716   const char *path = get_exploded_module_path(module_name, false);
 717 
 718   struct stat st;
 719   if (os::stat(path, &st) == 0) {
 720     // Directory found
 721     ClassPathEntry* new_entry = create_class_path_entry(current, path, &st);

 722     // If the path specification is valid, enter it into this module's list.
 723     // There is no need to check for duplicate modules in the exploded entry list,
 724     // since no two modules with the same name can be defined to the boot loader.
 725     // This is checked at module definition time in Modules::define_module.
 726     if (new_entry != nullptr) {
 727       ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);
 728       log_info(class, load)("path: %s", path);
 729 
 730       // If we are in preview mode, attempt to add a preview entry *before* the
 731       // new class path entry if a preview path exists.
 732       if (is_preview_enabled()) {
 733         const char* preview_path = get_preview_path(path);
 734         if (os::stat(preview_path, &st) == 0) {
 735           ClassPathEntry* preview_entry = create_class_path_entry(current, preview_path, &st);
 736           if (preview_entry != nullptr) {
 737             module_cpl->add_to_list(preview_entry);
 738             log_info(class, load)("preview path: %s", preview_path);
 739           }
 740         }
 741       }
 742 
 743       module_cpl->add_to_list(new_entry);
 744       {
 745         MutexLocker ml(current, Module_lock);
 746         _exploded_entries->push(module_cpl);
 747       }

 748     }
 749   }
 750 }
 751 
 752 jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {
 753   // enable call to C land
 754   ThreadToNativeFromVM ttn(thread);
 755   HandleMark hm(thread);
 756   return ZipLibrary::open(canonical_path, error_msg);
 757 }
 758 
 759 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
 760                                                      const char *path, const struct stat* st) {
 761   ClassPathEntry* new_entry = nullptr;
 762   if ((st->st_mode & S_IFMT) == S_IFREG) {
 763     ResourceMark rm(current);
 764     // Regular file, should be a zip file
 765     // Canonicalized filename
 766     const char* canonical_path = get_canonical_path(path, current);
 767     if (canonical_path == nullptr) {

 965 
 966     // Return null if package does not exist or if no classes in that package
 967     // have been loaded.
 968     if (package != nullptr && package->has_loaded_class()) {
 969       ModuleEntry* module = package->module();
 970       if (module->location() != nullptr) {
 971         ResourceMark rm(THREAD);
 972         Handle ml = java_lang_String::create_from_str(
 973           module->location()->as_C_string(), THREAD);
 974         return ml();
 975       }
 976       // Return entry on boot loader class path.
 977       Handle cph = java_lang_String::create_from_str(
 978         ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD);
 979       return cph();
 980     }
 981   }
 982   return nullptr;
 983 }
 984 
 985 refArrayOop ClassLoader::get_system_packages(TRAPS) {
 986   ResourceMark rm(THREAD);
 987   // List of pointers to PackageEntrys that have loaded classes.
 988   PackageEntryTable* pe_table =
 989       ClassLoaderData::the_null_class_loader_data()->packages();
 990   GrowableArray<PackageEntry*>* loaded_class_pkgs = pe_table->get_system_packages();
 991 
 992   // Allocate objArray and fill with java.lang.String
 993   refArrayOop r = oopFactory::new_refArray(vmClasses::String_klass(),
 994                                            loaded_class_pkgs->length(),
 995                                            CHECK_NULL);
 996   refArrayHandle result(THREAD, r);
 997   for (int x = 0; x < loaded_class_pkgs->length(); x++) {
 998     PackageEntry* package_entry = loaded_class_pkgs->at(x);
 999     Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL);
1000     result->obj_at_put(x, str());
1001   }
1002   return result();
1003 }
1004 
1005 // caller needs ResourceMark
1006 const char* ClassLoader::file_name_for_class_name(const char* class_name,
1007                                                   int class_name_len) {
1008   assert(class_name != nullptr, "invariant");
1009   assert((int)strlen(class_name) == class_name_len, "invariant");
1010 
1011   static const char class_suffix[] = ".class";
1012   size_t class_suffix_len = sizeof(class_suffix);
1013 
1014   char* const file_name = NEW_RESOURCE_ARRAY(char,
1015                                              class_name_len +
1016                                              class_suffix_len); // includes term null

1093 
1094 // Called by the boot classloader to load classes
1095 InstanceKlass* ClassLoader::load_class(Symbol* name, PackageEntry* pkg_entry, bool search_append_only, TRAPS) {
1096   assert(name != nullptr, "invariant");
1097 
1098   ResourceMark rm(THREAD);
1099   HandleMark hm(THREAD);
1100 
1101   const char* const class_name = name->as_C_string();
1102 
1103   EventMarkClassLoading m("Loading class %s", class_name);
1104 
1105   const char* const file_name = file_name_for_class_name(class_name,
1106                                                          name->utf8_length());
1107   assert(file_name != nullptr, "invariant");
1108 
1109   // Lookup stream for parsing .class file
1110   ClassFileStream* stream = nullptr;
1111   s2 classpath_index = 0;
1112   ClassPathEntry* e = nullptr;
1113   bool is_patched = false;
1114 
1115   // If search_append_only is true, boot loader visibility boundaries are
1116   // set to be _first_append_entry to the end. This includes:
1117   //   [-Xbootclasspath/a]; [jvmti appended entries]
1118   //
1119   // If search_append_only is false, boot loader visibility boundaries are
1120   // set to be the --patch-module entries plus the base piece. This includes:
1121   //   [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]
1122   //
1123 
1124   // Load Attempt #1: --patch-module
1125   // Determine the class' defining module.  If it appears in the _patch_mod_entries,
1126   // attempt to load the class from those locations specific to the module.
1127   // Specifications to --patch-module can contain a partial number of classes
1128   // that are part of the overall module definition.  So if a particular class is not
1129   // found within its module specification, the search should continue to Load Attempt #2.
1130   // Note: The --patch-module entries are never searched if the boot loader's
1131   //       visibility boundary is limited to only searching the append entries.
1132   if (_patch_mod_entries != nullptr && !search_append_only) {
1133     // At CDS dump time, the --patch-module entries are ignored. That means a
1134     // class is still loaded from the runtime image even if it might
1135     // appear in the _patch_mod_entries. The runtime shared class visibility
1136     // check will determine if a shared class is visible based on the runtime
1137     // environment, including the runtime --patch-module setting.
1138     if (!Arguments::is_valhalla_enabled()) {
1139       // Dynamic dumping requires UseSharedSpaces to be enabled. Since --patch-module
1140       // is not supported with UseSharedSpaces, we can never come here during dynamic dumping.
1141       assert(!CDSConfig::is_dumping_archive(), "CDS doesn't support --patch-module during dumping");
1142     }
1143     if (Arguments::is_valhalla_enabled() || !CDSConfig::is_dumping_static_archive()) {
1144       stream = search_module_entries(THREAD, _patch_mod_entries, pkg_entry, file_name);
1145       if (stream != nullptr) {
1146         is_patched = true;
1147       }
1148     }
1149   }
1150 
1151   // Load Attempt #2: [jimage | exploded build]
1152   if (!search_append_only && (nullptr == stream)) {
1153     if (has_jrt_entry()) {
1154       e = _jrt_entry;
1155       stream = _jrt_entry->open_stream(THREAD, file_name);
1156     } else {
1157       // Exploded build - attempt to locate class in its defining module's location.
1158       assert(_exploded_entries != nullptr, "No exploded build entries present");
1159       assert(!CDSConfig::is_dumping_archive(), "CDS dumping doesn't support exploded build");
1160       stream = search_module_entries(THREAD, _exploded_entries, pkg_entry, file_name);
1161     }
1162   }
1163 
1164   // Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]
1165   if (search_append_only && (nullptr == stream)) {
1166     // For the boot loader append path search, the starting classpath_index
1167     // for the appended piece is always 1 to account for either the
1168     // _jrt_entry or the _exploded_entries.

1177       }
1178       e = e->next();
1179       ++classpath_index;
1180     }
1181   }
1182 
1183   if (nullptr == stream) {
1184     return nullptr;
1185   }
1186 
1187   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
1188   Handle protection_domain;
1189   ClassLoadInfo cl_info(protection_domain);
1190 
1191   InstanceKlass* result = KlassFactory::create_from_stream(stream,
1192                                                            name,
1193                                                            loader_data,
1194                                                            cl_info,
1195                                                            CHECK_NULL);
1196   result->set_classpath_index(classpath_index);
1197   if (is_patched) {
1198     result->set_shared_classpath_index(0);
1199   }
1200   return result;
1201 }
1202 
1203 #if INCLUDE_CDS
1204 static const char* skip_uri_protocol(const char* source) {
1205   if (strncmp(source, "file:", 5) == 0) {
1206     // file: protocol path could start with file:/ or file:///
1207     // locate the char after all the forward slashes
1208     int offset = 5;
1209     while (*(source + offset) == '/') {
1210         offset++;
1211     }
1212     source += offset;
1213   // for non-windows platforms, move back one char as the path begins with a '/'
1214 #ifndef _WINDOWS
1215     source -= 1;
1216 #endif
1217   } else if (strncmp(source, "jrt:/", 5) == 0) {
1218     source += 5;
1219   }

1256   for (size_t i = 0; i < strlen(uri); ++i) {
1257     char decoded = decode_percent_encoded(uri, i);
1258     path[path_index++] = decoded;
1259   }
1260   path[path_index] = '\0';
1261   return path;
1262 }
1263 
1264 // Record the shared classpath index and loader type for classes loaded
1265 // by the builtin loaders at dump time.
1266 void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik,
1267                                 const ClassFileStream* stream, bool redefined) {
1268   assert(CDSConfig::is_dumping_archive(), "sanity");
1269   assert(stream != nullptr, "sanity");
1270 
1271   if (ik->is_hidden()) {
1272     record_hidden_class(ik);
1273     return;
1274   }
1275 
1276   if (ik->shared_classpath_index() == 0 && ik->defined_by_boot_loader()) {
1277     return;
1278   }
1279 
1280   oop loader = ik->class_loader();
1281   char* src = (char*)stream->source();
1282   if (src == nullptr) {
1283     ik->set_shared_classpath_index(-1); // unsupported location
1284     return;
1285   }
1286 
1287   if (!SystemDictionaryShared::is_builtin_loader(ik->class_loader_data())) {
1288     // A class loaded by a user-defined classloader.
1289     assert(ik->shared_classpath_index() < 0, "not assigned yet");
1290     ik->set_shared_classpath_index(UNREGISTERED_INDEX);
1291     SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);
1292     return;
1293   }
1294 
1295   assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");
1296 
1297   ResourceMark rm(current);
1298   int classpath_index = -1;
1299   bool found_invalid = false;

1468       NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1469 
1470       NEWPERFTICKCOUNTER(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic_time");
1471       NEWPERFTICKCOUNTER(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle_time");
1472       NEWPERFTICKCOUNTER(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle_time");
1473       NEWPERFTICKCOUNTER(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType_time");
1474 
1475       NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1476       NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1477       NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1478       NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1479     }
1480   }
1481 
1482   // lookup java library entry points
1483   load_java_library();
1484   // jimage library entry points are loaded below, in lookup_vm_options
1485   setup_bootstrap_search_path(THREAD);
1486 }
1487 











1488 // Lookup VM options embedded in the modules jimage file
1489 char* ClassLoader::lookup_vm_options() {

1490   char modules_path[JVM_MAXPATHLEN];
1491   const char* fileSep = os::file_separator();
1492 
1493   // Initialize jimage library entry points
1494   load_jimage_library();
1495 
1496   jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);
1497   if (jimage_open(modules_path)) {
1498     // Special case where we lookup the options string *before* set_preview_mode() is called.
1499     // Since VM arguments have not been parsed, and the ClassPathImageEntry singleton
1500     // has not been created yet, we access the JImage file directly in non-preview mode.
1501     jlong size;
1502     JImageLocationRef location =
1503             jimage_find_resource(JAVA_BASE_NAME, "jdk/internal/vm/options", /* is_preview */ false, &size);
1504     if (location != 0) {
1505       char *options = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1506       (*JImageGetResource)(jimage_non_null(), location, options, size);
1507       options[size] = '\0';
1508       return options;
1509     }

1510   }
1511   return nullptr;
1512 }
1513 
1514 // Finishes initializing the JImageFile (if present) by setting the access mode.
1515 void ClassLoader::set_preview_mode(bool enable_preview) {
1516   assert(Preview_mode == PREVIEW_MODE_UNINITIALIZED, "set_preview_mode must not be called twice");
1517   Preview_mode = enable_preview ? PREVIEW_MODE_ENABLE_PREVIEW : PREVIEW_MODE_DEFAULT;
1518 }
1519 
1520 bool ClassLoader::is_module_observable(const char* module_name) {
1521   assert(JImageOpen != nullptr, "jimage library should have been opened");
1522   if (!jimage_exists()) {
1523     struct stat st;
1524     const char *path = get_exploded_module_path(module_name, true);
1525     bool res = os::stat(path, &st) == 0;
1526     FREE_C_HEAP_ARRAY(char, path);
1527     return res;
1528   }
1529   // We don't expect preview mode (i.e. --enable-preview) to affect module visibility.
1530   jlong size;
1531   return jimage_find_resource(module_name, "module-info.class", /* is_preview */ false, &size) != 0;

1532 }
1533 
1534 jlong ClassLoader::classloader_time_ms() {
1535   return UsePerfData ?
1536     Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1537 }
1538 
1539 jlong ClassLoader::class_init_count() {
1540   return UsePerfData ? _perf_classes_inited->get_value() : -1;
1541 }
1542 
1543 jlong ClassLoader::class_init_time_ms() {
1544   return UsePerfData ?
1545     Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1546 }
1547 
1548 jlong ClassLoader::class_verify_time_ms() {
1549   return UsePerfData ?
1550     Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1551 }
< prev index next >