< 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(_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) {

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

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

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

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














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

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



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

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




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

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







1427     }
1428     return nullptr;
1429   }


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

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

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

  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(_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) {

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

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

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

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

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











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

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

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

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