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