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