< prev index next >

src/hotspot/share/classfile/classLoader.cpp

Print this page

  51 #include "logging/logTag.hpp"
  52 #include "memory/allocation.inline.hpp"
  53 #include "memory/oopFactory.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/instanceKlass.hpp"
  57 #include "oops/instanceRefKlass.hpp"
  58 #include "oops/klass.inline.hpp"
  59 #include "oops/method.inline.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"
  63 #include "prims/jvm_misc.hpp"
  64 #include "runtime/arguments.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/init.hpp"
  67 #include "runtime/interfaceSupport.inline.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/os.hpp"
  71 #include "runtime/perfData.hpp"
  72 #include "runtime/threadCritical.hpp"
  73 #include "runtime/timer.hpp"
  74 #include "runtime/vm_version.hpp"
  75 #include "services/management.hpp"
  76 #include "services/threadService.hpp"
  77 #include "utilities/checkedCast.hpp"
  78 #include "utilities/classpathStream.hpp"
  79 #include "utilities/events.hpp"
  80 #include "utilities/macros.hpp"
  81 #include "utilities/utf8.hpp"
  82 
  83 // Entry point in java.dll for path canonicalization
  84 
  85 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
  86 
  87 static canonicalize_fn_t CanonicalizeEntry  = nullptr;
  88 
  89 // Entry points for jimage.dll for loading jimage file entries
  90 
  91 static JImageOpen_t                    JImageOpen             = nullptr;
  92 static JImageClose_t                   JImageClose            = nullptr;
  93 static JImageFindResource_t            JImageFindResource     = nullptr;
  94 static JImageGetResource_t             JImageGetResource      = nullptr;
  95 
  96 // JimageFile pointer, or null if exploded JDK build.
  97 static JImageFile*                     JImage_file            = nullptr;
  98 
  99 // Globals
 100 
 101 PerfCounter*    ClassLoader::_perf_accumulated_time = nullptr;
 102 PerfCounter*    ClassLoader::_perf_classes_inited = nullptr;
 103 PerfCounter*    ClassLoader::_perf_class_init_time = nullptr;
 104 PerfCounter*    ClassLoader::_perf_class_init_selftime = nullptr;

 105 PerfCounter*    ClassLoader::_perf_classes_verified = nullptr;
 106 PerfCounter*    ClassLoader::_perf_class_verify_time = nullptr;
 107 PerfCounter*    ClassLoader::_perf_class_verify_selftime = nullptr;
 108 PerfCounter*    ClassLoader::_perf_classes_linked = nullptr;
 109 PerfCounter*    ClassLoader::_perf_class_link_time = nullptr;
 110 PerfCounter*    ClassLoader::_perf_class_link_selftime = nullptr;
 111 PerfCounter*    ClassLoader::_perf_shared_classload_time = nullptr;
 112 PerfCounter*    ClassLoader::_perf_sys_classload_time = nullptr;
 113 PerfCounter*    ClassLoader::_perf_app_classload_time = nullptr;
 114 PerfCounter*    ClassLoader::_perf_app_classload_selftime = nullptr;
 115 PerfCounter*    ClassLoader::_perf_app_classload_count = nullptr;
 116 PerfCounter*    ClassLoader::_perf_define_appclasses = nullptr;
 117 PerfCounter*    ClassLoader::_perf_define_appclass_time = nullptr;
 118 PerfCounter*    ClassLoader::_perf_define_appclass_selftime = nullptr;
 119 PerfCounter*    ClassLoader::_perf_app_classfile_bytes_read = nullptr;
 120 PerfCounter*    ClassLoader::_perf_sys_classfile_bytes_read = nullptr;









 121 PerfCounter*    ClassLoader::_unsafe_defineClassCallCounter = nullptr;
 122 










































 123 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
 124 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
 125 ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
 126 
 127 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
 128 ClassPathEntry* volatile ClassLoader::_last_append_entry  = nullptr;
 129 #if INCLUDE_CDS
 130 ClassPathEntry* ClassLoader::_app_classpath_entries = nullptr;
 131 ClassPathEntry* ClassLoader::_last_app_classpath_entry = nullptr;
 132 ClassPathEntry* ClassLoader::_module_path_entries = nullptr;
 133 ClassPathEntry* ClassLoader::_last_module_path_entry = nullptr;
 134 #endif
 135 
 136 // helper routines
 137 #if INCLUDE_CDS
 138 static bool string_starts_with(const char* str, const char* str_to_find) {
 139   size_t str_len = strlen(str);
 140   size_t str_to_find_len = strlen(str_to_find);
 141   if (str_to_find_len > str_len) {
 142     return false;

 254 #ifdef ASSERT
 255         // Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
 256         // debug builds so that we guard against use-after-free bugs.
 257         FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
 258 #endif
 259         // We don't verify the length of the classfile stream fits in an int, but this is the
 260         // bootloader so we have control of this.
 261         // Resource allocated
 262         return new ClassFileStream(buffer,
 263                                    checked_cast<int>(st.st_size),
 264                                    _dir,
 265                                    ClassFileStream::verify);
 266       }
 267     }
 268   }
 269   FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
 270   return nullptr;
 271 }
 272 
 273 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,
 274                                      bool is_boot_append, bool from_class_path_attr) : ClassPathEntry() {
 275   _zip = zip;
 276   _zip_name = copy_path(zip_name);
 277   _from_class_path_attr = from_class_path_attr;
 278 }
 279 
 280 ClassPathZipEntry::~ClassPathZipEntry() {
 281   ZipLibrary::close(_zip);
 282   FREE_C_HEAP_ARRAY(char, _zip_name);
 283 }
 284 














 285 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
 286   // enable call to C land
 287   ThreadToNativeFromVM ttn(current);
 288   // check whether zip archive contains name
 289   jint name_len;
 290   jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len);
 291   if (entry == nullptr) return nullptr;
 292   u1* buffer;
 293   char name_buf[128];
 294   char* filename;
 295   if (name_len < 128) {
 296     filename = name_buf;
 297   } else {
 298     filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
 299   }
 300 
 301   // read contents into resource array
 302   size_t size = (uint32_t)(*filesize);
 303   if (nul_terminate) {
 304     if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
 305       return nullptr; // 32-bit integer overflow will occur.
 306     }
 307     size++;
 308   }


 309   buffer = NEW_RESOURCE_ARRAY(u1, size);
 310   if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) {
 311     return nullptr;
 312   }
 313 
 314   // return result
 315   if (nul_terminate) {
 316     buffer[size - 1] = 0;
 317   }
 318   return buffer;
 319 }
 320 
 321 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
 322   jint filesize;
 323   u1* buffer = open_entry(current, name, &filesize, false);
 324   if (buffer == nullptr) {
 325     return nullptr;
 326   }
 327   if (UsePerfData) {
 328     ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);

 714   HandleMark hm(thread);
 715   return ZipLibrary::open(canonical_path, error_msg);
 716 }
 717 
 718 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
 719                                                      const char *path, const struct stat* st,
 720                                                      bool is_boot_append,
 721                                                      bool from_class_path_attr) {
 722   ClassPathEntry* new_entry = nullptr;
 723   if ((st->st_mode & S_IFMT) == S_IFREG) {
 724     ResourceMark rm(current);
 725     // Regular file, should be a zip file
 726     // Canonicalized filename
 727     const char* canonical_path = get_canonical_path(path, current);
 728     if (canonical_path == nullptr) {
 729       return nullptr;
 730     }
 731     char* error_msg = nullptr;
 732     jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
 733     if (zip != nullptr && error_msg == nullptr) {
 734       new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr);
 735     } else {
 736 #if INCLUDE_CDS
 737       ClassLoaderExt::set_has_non_jar_in_classpath();
 738 #endif
 739       return nullptr;
 740     }
 741     log_info(class, path)("opened: %s", path);
 742     log_info(class, load)("opened: %s", path);
 743   } else {
 744     // Directory
 745     new_entry = new ClassPathDirEntry(path);
 746     log_info(class, load)("path: %s", path);
 747   }
 748   return new_entry;
 749 }
 750 
 751 
 752 // Create a class path zip entry for a given path (return null if not found
 753 // or zip/JAR file cannot be opened)
 754 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {
 755   // check for a regular file
 756   struct stat st;
 757   if (os::stat(path, &st) == 0) {
 758     if ((st.st_mode & S_IFMT) == S_IFREG) {
 759       JavaThread* thread = JavaThread::current();
 760       ResourceMark rm(thread);
 761       const char* canonical_path = get_canonical_path(path, thread);
 762       if (canonical_path != nullptr) {
 763         char* error_msg = nullptr;
 764         jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
 765         if (zip != nullptr && error_msg == nullptr) {
 766           // create using canonical path
 767           return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false);
 768         }
 769       }
 770     }
 771   }
 772   return nullptr;
 773 }
 774 
 775 // The boot append entries are added with a lock, and read lock free.
 776 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
 777   if (new_entry != nullptr) {
 778     MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
 779     if (_last_append_entry == nullptr) {
 780       _last_append_entry = new_entry;
 781       assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
 782       Atomic::release_store(&_first_append_entry_list, new_entry);
 783     } else {
 784       _last_append_entry->set_next(new_entry);
 785       _last_append_entry = new_entry;
 786     }
 787   }

1316 #endif // INCLUDE_CDS
1317 
1318 // Initialize the class loader's access to methods in libzip.  Parse and
1319 // process the boot classpath into a list ClassPathEntry objects.  Once
1320 // this list has been created, it must not change order (see class PackageInfo)
1321 // it can be appended to and is by jvmti.
1322 
1323 void ClassLoader::initialize(TRAPS) {
1324   if (UsePerfData) {
1325     // jvmstat performance counters
1326     NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1327     NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1328     NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1329     NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1330     NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1331     NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1332     NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1333     NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1334     NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1335     NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");

1336 
1337     NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1338     NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1339     NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1340     NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1341     NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1342     NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1343     NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1344     NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1345     NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1346     NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1347 
1348     NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");





















1349   }
1350 
1351   // lookup java library entry points
1352   load_java_library();
1353   // jimage library entry points are loaded below, in lookup_vm_options
1354   setup_bootstrap_search_path(THREAD);
1355 }
1356 
1357 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1358   jlong size;
1359   JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1360   if (location == 0)
1361     return nullptr;
1362   char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1363   (*JImageGetResource)(jimage, location, val, size);
1364   val[size] = '\0';
1365   return val;
1366 }
1367 
1368 // Lookup VM options embedded in the modules jimage file

1415 
1416 // Helper function used by CDS code to get the number of module path
1417 // entries during shared classpath setup time.
1418 int ClassLoader::num_module_path_entries() {
1419   assert(CDSConfig::is_dumping_archive(), "sanity");
1420   int num_entries = 0;
1421   ClassPathEntry* e= ClassLoader::_module_path_entries;
1422   while (e != nullptr) {
1423     num_entries ++;
1424     e = e->next();
1425   }
1426   return num_entries;
1427 }
1428 #endif
1429 
1430 jlong ClassLoader::classloader_time_ms() {
1431   return UsePerfData ?
1432     Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1433 }
1434 




1435 jlong ClassLoader::class_init_count() {
1436   return UsePerfData ? _perf_classes_inited->get_value() : -1;
1437 }
1438 
1439 jlong ClassLoader::class_init_time_ms() {
1440   return UsePerfData ?
1441     Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1442 }
1443 
1444 jlong ClassLoader::class_verify_time_ms() {
1445   return UsePerfData ?
1446     Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1447 }
1448 
1449 jlong ClassLoader::class_link_count() {
1450   return UsePerfData ? _perf_classes_linked->get_value() : -1;
1451 }
1452 
1453 jlong ClassLoader::class_link_time_ms() {
1454   return UsePerfData ?

  51 #include "logging/logTag.hpp"
  52 #include "memory/allocation.inline.hpp"
  53 #include "memory/oopFactory.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "oops/instanceKlass.hpp"
  57 #include "oops/instanceRefKlass.hpp"
  58 #include "oops/klass.inline.hpp"
  59 #include "oops/method.inline.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"
  63 #include "prims/jvm_misc.hpp"
  64 #include "runtime/arguments.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/init.hpp"
  67 #include "runtime/interfaceSupport.inline.hpp"
  68 #include "runtime/java.hpp"
  69 #include "runtime/javaCalls.hpp"
  70 #include "runtime/os.hpp"
  71 #include "runtime/perfData.inline.hpp"
  72 #include "runtime/threadCritical.hpp"
  73 #include "runtime/timer.hpp"
  74 #include "runtime/vm_version.hpp"
  75 #include "services/management.hpp"
  76 #include "services/threadService.hpp"
  77 #include "utilities/checkedCast.hpp"
  78 #include "utilities/classpathStream.hpp"
  79 #include "utilities/events.hpp"
  80 #include "utilities/macros.hpp"
  81 #include "utilities/utf8.hpp"
  82 
  83 // Entry point in java.dll for path canonicalization
  84 
  85 typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);
  86 
  87 static canonicalize_fn_t CanonicalizeEntry  = nullptr;
  88 
  89 // Entry points for jimage.dll for loading jimage file entries
  90 
  91 static JImageOpen_t                    JImageOpen             = nullptr;
  92 static JImageClose_t                   JImageClose            = nullptr;
  93 static JImageFindResource_t            JImageFindResource     = nullptr;
  94 static JImageGetResource_t             JImageGetResource      = nullptr;
  95 
  96 // JimageFile pointer, or null if exploded JDK build.
  97 static JImageFile*                     JImage_file            = nullptr;
  98 
  99 // Globals
 100 
 101 PerfCounter*    ClassLoader::_perf_accumulated_time = nullptr;
 102 PerfCounter*    ClassLoader::_perf_classes_inited = nullptr;
 103 PerfCounter*    ClassLoader::_perf_class_init_time = nullptr;
 104 PerfCounter*    ClassLoader::_perf_class_init_selftime = nullptr;
 105 PerfCounter*    ClassLoader::_perf_class_init_bytecodes_count = nullptr;
 106 PerfCounter*    ClassLoader::_perf_classes_verified = nullptr;
 107 PerfCounter*    ClassLoader::_perf_class_verify_time = nullptr;
 108 PerfCounter*    ClassLoader::_perf_class_verify_selftime = nullptr;
 109 PerfCounter*    ClassLoader::_perf_classes_linked = nullptr;
 110 PerfCounter*    ClassLoader::_perf_class_link_time = nullptr;
 111 PerfCounter*    ClassLoader::_perf_class_link_selftime = nullptr;
 112 PerfCounter*    ClassLoader::_perf_shared_classload_time = nullptr;
 113 PerfCounter*    ClassLoader::_perf_sys_classload_time = nullptr;
 114 PerfCounter*    ClassLoader::_perf_app_classload_time = nullptr;
 115 PerfCounter*    ClassLoader::_perf_app_classload_selftime = nullptr;
 116 PerfCounter*    ClassLoader::_perf_app_classload_count = nullptr;
 117 PerfCounter*    ClassLoader::_perf_define_appclasses = nullptr;
 118 PerfCounter*    ClassLoader::_perf_define_appclass_time = nullptr;
 119 PerfCounter*    ClassLoader::_perf_define_appclass_selftime = nullptr;
 120 PerfCounter*    ClassLoader::_perf_app_classfile_bytes_read = nullptr;
 121 PerfCounter*    ClassLoader::_perf_sys_classfile_bytes_read = nullptr;
 122 PerfCounter*    ClassLoader::_perf_preload_total_time = nullptr;
 123 PerfCounter*    ClassLoader::_perf_preload_time = nullptr;
 124 PerfCounter*    ClassLoader::_perf_prelink_time = nullptr;
 125 PerfCounter*    ClassLoader::_perf_preinit_time = nullptr;
 126 PerfCounter*    ClassLoader::_perf_preresolve_time = nullptr;
 127 PerfCounter*    ClassLoader::_perf_ik_link_methods_time = nullptr;
 128 PerfCounter*    ClassLoader::_perf_method_adapters_time = nullptr;
 129 PerfCounter*    ClassLoader::_perf_ik_link_methods_count = nullptr;
 130 PerfCounter*    ClassLoader::_perf_method_adapters_count = nullptr;
 131 PerfCounter*    ClassLoader::_unsafe_defineClassCallCounter = nullptr;
 132 
 133 PerfTickCounters*    ClassLoader::_perf_resolve_indy_time = nullptr;
 134 PerfTickCounters*    ClassLoader::_perf_resolve_invokehandle_time = nullptr;
 135 PerfTickCounters*    ClassLoader::_perf_resolve_mh_time = nullptr;
 136 PerfTickCounters*    ClassLoader::_perf_resolve_mt_time = nullptr;
 137 
 138 PerfCounter*    ClassLoader::_perf_resolve_indy_count = nullptr;
 139 PerfCounter*    ClassLoader::_perf_resolve_invokehandle_count = nullptr;
 140 PerfCounter*    ClassLoader::_perf_resolve_mh_count = nullptr;
 141 PerfCounter*    ClassLoader::_perf_resolve_mt_count = nullptr;
 142 
 143 void ClassLoader::print_counters() {
 144   if (UsePerfData) {
 145     LogStreamHandle(Info, init) log;
 146     if (log.is_enabled()) {
 147       log.print_cr("ClassLoader:");
 148       log.print(   "  clinit:               %ldms / %ld events",
 149                    ClassLoader::class_init_time_ms(), ClassLoader::class_init_count());
 150       if (CountBytecodes || CountBytecodesPerThread) {
 151         log.print("; executed %ld bytecodes", ClassLoader::class_init_bytecodes_count());
 152       }
 153       log.cr();
 154       log.print_cr("  resolve...");
 155       log.print_cr("    invokedynamic:   %ldms (elapsed) %ldms (thread) / %ld events",
 156                    _perf_resolve_indy_time->elapsed_counter_value_ms(),
 157                    _perf_resolve_indy_time->thread_counter_value_ms(),
 158                    _perf_resolve_indy_count->get_value());
 159       log.print_cr("    invokehandle:    %ldms (elapsed) %ldms (thread) / %ld events",
 160                    _perf_resolve_invokehandle_time->elapsed_counter_value_ms(),
 161                    _perf_resolve_invokehandle_time->thread_counter_value_ms(),
 162                    _perf_resolve_invokehandle_count->get_value());
 163       log.print_cr("    CP_MethodHandle: %ldms (elapsed) %ldms (thread) / %ld events",
 164                    _perf_resolve_mh_time->elapsed_counter_value_ms(),
 165                    _perf_resolve_mh_time->thread_counter_value_ms(),
 166                    _perf_resolve_mh_count->get_value());
 167       log.print_cr("    CP_MethodType:   %ldms (elapsed) %ldms (thread) / %ld events",
 168                    _perf_resolve_mt_time->elapsed_counter_value_ms(),
 169                    _perf_resolve_mt_time->thread_counter_value_ms(),
 170                    _perf_resolve_mt_count->get_value());
 171     }
 172   }
 173 }
 174 
 175 GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = nullptr;
 176 GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = nullptr;
 177 ClassPathEntry* ClassLoader::_jrt_entry = nullptr;
 178 
 179 ClassPathEntry* volatile ClassLoader::_first_append_entry_list = nullptr;
 180 ClassPathEntry* volatile ClassLoader::_last_append_entry  = nullptr;
 181 #if INCLUDE_CDS
 182 ClassPathEntry* ClassLoader::_app_classpath_entries = nullptr;
 183 ClassPathEntry* ClassLoader::_last_app_classpath_entry = nullptr;
 184 ClassPathEntry* ClassLoader::_module_path_entries = nullptr;
 185 ClassPathEntry* ClassLoader::_last_module_path_entry = nullptr;
 186 #endif
 187 
 188 // helper routines
 189 #if INCLUDE_CDS
 190 static bool string_starts_with(const char* str, const char* str_to_find) {
 191   size_t str_len = strlen(str);
 192   size_t str_to_find_len = strlen(str_to_find);
 193   if (str_to_find_len > str_len) {
 194     return false;

 306 #ifdef ASSERT
 307         // Freeing path is a no-op here as buffer prevents it from being reclaimed. But we keep it for
 308         // debug builds so that we guard against use-after-free bugs.
 309         FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
 310 #endif
 311         // We don't verify the length of the classfile stream fits in an int, but this is the
 312         // bootloader so we have control of this.
 313         // Resource allocated
 314         return new ClassFileStream(buffer,
 315                                    checked_cast<int>(st.st_size),
 316                                    _dir,
 317                                    ClassFileStream::verify);
 318       }
 319     }
 320   }
 321   FREE_RESOURCE_ARRAY_IN_THREAD(current, char, path, path_len);
 322   return nullptr;
 323 }
 324 
 325 ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,
 326                                      bool from_class_path_attr) : ClassPathEntry() {
 327   _zip = zip;
 328   _zip_name = copy_path(zip_name);
 329   _from_class_path_attr = from_class_path_attr;
 330 }
 331 
 332 ClassPathZipEntry::~ClassPathZipEntry() {
 333   ZipLibrary::close(_zip);
 334   FREE_C_HEAP_ARRAY(char, _zip_name);
 335 }
 336 
 337 bool ClassPathZipEntry::has_entry(JavaThread* current, const char* name) {
 338   ThreadToNativeFromVM ttn(current);
 339   // check whether zip archive contains name
 340   jint name_len;
 341   jint filesize;
 342   jzentry* entry = ZipLibrary::find_entry(_zip, name, &filesize, &name_len);
 343   if (entry == nullptr) {
 344     return false;
 345   } else {
 346      ZipLibrary::free_entry(_zip, entry);
 347     return true;
 348   }
 349 }
 350 
 351 u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {
 352   // enable call to C land
 353   ThreadToNativeFromVM ttn(current);
 354   // check whether zip archive contains name
 355   jint name_len;
 356   jzentry* entry = ZipLibrary::find_entry(_zip, name, filesize, &name_len);
 357   if (entry == nullptr) return nullptr;
 358   u1* buffer;
 359   char name_buf[128];
 360   char* filename;
 361   if (name_len < 128) {
 362     filename = name_buf;
 363   } else {
 364     filename = NEW_RESOURCE_ARRAY(char, name_len + 1);
 365   }
 366 
 367   // read contents into resource array
 368   size_t size = (uint32_t)(*filesize);
 369   if (nul_terminate) {
 370     if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {
 371       return nullptr; // 32-bit integer overflow will occur.
 372     }
 373     size++;
 374   }
 375 
 376   // ZIP_ReadEntry also frees zentry
 377   buffer = NEW_RESOURCE_ARRAY(u1, size);
 378   if (!ZipLibrary::read_entry(_zip, entry, buffer, filename)) {
 379     return nullptr;
 380   }
 381 
 382   // return result
 383   if (nul_terminate) {
 384     buffer[size - 1] = 0;
 385   }
 386   return buffer;
 387 }
 388 
 389 ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {
 390   jint filesize;
 391   u1* buffer = open_entry(current, name, &filesize, false);
 392   if (buffer == nullptr) {
 393     return nullptr;
 394   }
 395   if (UsePerfData) {
 396     ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);

 782   HandleMark hm(thread);
 783   return ZipLibrary::open(canonical_path, error_msg);
 784 }
 785 
 786 ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,
 787                                                      const char *path, const struct stat* st,
 788                                                      bool is_boot_append,
 789                                                      bool from_class_path_attr) {
 790   ClassPathEntry* new_entry = nullptr;
 791   if ((st->st_mode & S_IFMT) == S_IFREG) {
 792     ResourceMark rm(current);
 793     // Regular file, should be a zip file
 794     // Canonicalized filename
 795     const char* canonical_path = get_canonical_path(path, current);
 796     if (canonical_path == nullptr) {
 797       return nullptr;
 798     }
 799     char* error_msg = nullptr;
 800     jzfile* zip = open_zip_file(canonical_path, &error_msg, current);
 801     if (zip != nullptr && error_msg == nullptr) {
 802       new_entry = new ClassPathZipEntry(zip, path, from_class_path_attr);
 803     } else {
 804 #if INCLUDE_CDS
 805       ClassLoaderExt::set_has_non_jar_in_classpath();
 806 #endif
 807       return nullptr;
 808     }
 809     log_info(class, path)("opened: %s", path);
 810     log_info(class, load)("opened: %s", path);
 811   } else {
 812     // Directory
 813     new_entry = new ClassPathDirEntry(path);
 814     log_info(class, load)("path: %s", path);
 815   }
 816   return new_entry;
 817 }
 818 
 819 
 820 // Create a class path zip entry for a given path (return null if not found
 821 // or zip/JAR file cannot be opened)
 822 ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path) {
 823   // check for a regular file
 824   struct stat st;
 825   if (os::stat(path, &st) == 0) {
 826     if ((st.st_mode & S_IFMT) == S_IFREG) {
 827       JavaThread* thread = JavaThread::current();
 828       ResourceMark rm(thread);
 829       const char* canonical_path = get_canonical_path(path, thread);
 830       if (canonical_path != nullptr) {
 831         char* error_msg = nullptr;
 832         jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);
 833         if (zip != nullptr && error_msg == nullptr) {
 834           // create using canonical path
 835           return new ClassPathZipEntry(zip, canonical_path, false);
 836         }
 837       }
 838     }
 839   }
 840   return nullptr;
 841 }
 842 
 843 // The boot append entries are added with a lock, and read lock free.
 844 void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {
 845   if (new_entry != nullptr) {
 846     MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);
 847     if (_last_append_entry == nullptr) {
 848       _last_append_entry = new_entry;
 849       assert(first_append_entry() == nullptr, "boot loader's append class path entry list not empty");
 850       Atomic::release_store(&_first_append_entry_list, new_entry);
 851     } else {
 852       _last_append_entry->set_next(new_entry);
 853       _last_append_entry = new_entry;
 854     }
 855   }

1384 #endif // INCLUDE_CDS
1385 
1386 // Initialize the class loader's access to methods in libzip.  Parse and
1387 // process the boot classpath into a list ClassPathEntry objects.  Once
1388 // this list has been created, it must not change order (see class PackageInfo)
1389 // it can be appended to and is by jvmti.
1390 
1391 void ClassLoader::initialize(TRAPS) {
1392   if (UsePerfData) {
1393     // jvmstat performance counters
1394     NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");
1395     NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");
1396     NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");
1397     NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");
1398     NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");
1399     NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");
1400     NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");
1401     NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");
1402     NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");
1403     NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");
1404     NEWPERFEVENTCOUNTER(_perf_class_init_bytecodes_count, SUN_CLS, "clinitBytecodesCount");
1405 
1406     NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");
1407     NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");
1408     NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");
1409     NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");
1410     NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");
1411     NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");
1412     NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");
1413     NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");
1414     NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");
1415     NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");
1416 
1417     NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");
1418 
1419     NEWPERFTICKCOUNTER(_perf_preload_total_time, SUN_CLS, "preloadTotalTime");
1420     NEWPERFTICKCOUNTER(_perf_preload_time, SUN_CLS, "preloadTime");
1421     NEWPERFTICKCOUNTER(_perf_prelink_time, SUN_CLS, "prelinkTime");
1422     NEWPERFTICKCOUNTER(_perf_preinit_time, SUN_CLS, "preinitTime");
1423     NEWPERFTICKCOUNTER(_perf_preresolve_time, SUN_CLS, "preresolveTime");
1424 
1425     NEWPERFTICKCOUNTER(_perf_ik_link_methods_time, SUN_CLS, "linkMethodsTime");
1426     NEWPERFTICKCOUNTER(_perf_method_adapters_time, SUN_CLS, "makeAdaptersTime");
1427     NEWPERFEVENTCOUNTER(_perf_ik_link_methods_count, SUN_CLS, "linkMethodsCount");
1428     NEWPERFEVENTCOUNTER(_perf_method_adapters_count, SUN_CLS, "makeAdaptersCount");
1429 
1430     NEWPERFTICKCOUNTERS(_perf_resolve_indy_time, SUN_CLS, "resolve_invokedynamic");
1431     NEWPERFTICKCOUNTERS(_perf_resolve_invokehandle_time, SUN_CLS, "resolve_invokehandle");
1432     NEWPERFTICKCOUNTERS(_perf_resolve_mh_time, SUN_CLS, "resolve_MethodHandle");
1433     NEWPERFTICKCOUNTERS(_perf_resolve_mt_time, SUN_CLS, "resolve_MethodType");
1434 
1435     NEWPERFEVENTCOUNTER(_perf_resolve_indy_count, SUN_CLS, "resolve_invokedynamic_count");
1436     NEWPERFEVENTCOUNTER(_perf_resolve_invokehandle_count, SUN_CLS, "resolve_invokehandle_count");
1437     NEWPERFEVENTCOUNTER(_perf_resolve_mh_count, SUN_CLS, "resolve_MethodHandle_count");
1438     NEWPERFEVENTCOUNTER(_perf_resolve_mt_count, SUN_CLS, "resolve_MethodType_count");
1439   }
1440 
1441   // lookup java library entry points
1442   load_java_library();
1443   // jimage library entry points are loaded below, in lookup_vm_options
1444   setup_bootstrap_search_path(THREAD);
1445 }
1446 
1447 static char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {
1448   jlong size;
1449   JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);
1450   if (location == 0)
1451     return nullptr;
1452   char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);
1453   (*JImageGetResource)(jimage, location, val, size);
1454   val[size] = '\0';
1455   return val;
1456 }
1457 
1458 // Lookup VM options embedded in the modules jimage file

1505 
1506 // Helper function used by CDS code to get the number of module path
1507 // entries during shared classpath setup time.
1508 int ClassLoader::num_module_path_entries() {
1509   assert(CDSConfig::is_dumping_archive(), "sanity");
1510   int num_entries = 0;
1511   ClassPathEntry* e= ClassLoader::_module_path_entries;
1512   while (e != nullptr) {
1513     num_entries ++;
1514     e = e->next();
1515   }
1516   return num_entries;
1517 }
1518 #endif
1519 
1520 jlong ClassLoader::classloader_time_ms() {
1521   return UsePerfData ?
1522     Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;
1523 }
1524 
1525 jlong ClassLoader::class_init_bytecodes_count() {
1526   return UsePerfData ? _perf_class_init_bytecodes_count->get_value() : -1;
1527 }
1528 
1529 jlong ClassLoader::class_init_count() {
1530   return UsePerfData ? _perf_classes_inited->get_value() : -1;
1531 }
1532 
1533 jlong ClassLoader::class_init_time_ms() {
1534   return UsePerfData ?
1535     Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;
1536 }
1537 
1538 jlong ClassLoader::class_verify_time_ms() {
1539   return UsePerfData ?
1540     Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;
1541 }
1542 
1543 jlong ClassLoader::class_link_count() {
1544   return UsePerfData ? _perf_classes_linked->get_value() : -1;
1545 }
1546 
1547 jlong ClassLoader::class_link_time_ms() {
1548   return UsePerfData ?
< prev index next >