Path: blob/master/src/hotspot/share/classfile/classLoader.cpp
64440 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jvm.h"26#include "jimage.hpp"27#include "cds/filemap.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/classLoadInfo.hpp"33#include "classfile/javaClasses.hpp"34#include "classfile/moduleEntry.hpp"35#include "classfile/modules.hpp"36#include "classfile/packageEntry.hpp"37#include "classfile/klassFactory.hpp"38#include "classfile/symbolTable.hpp"39#include "classfile/systemDictionary.hpp"40#include "classfile/systemDictionaryShared.hpp"41#include "classfile/vmClasses.hpp"42#include "classfile/vmSymbols.hpp"43#include "compiler/compileBroker.hpp"44#include "interpreter/bytecodeStream.hpp"45#include "interpreter/oopMapCache.hpp"46#include "logging/log.hpp"47#include "logging/logStream.hpp"48#include "logging/logTag.hpp"49#include "memory/allocation.inline.hpp"50#include "memory/oopFactory.hpp"51#include "memory/resourceArea.hpp"52#include "memory/universe.hpp"53#include "oops/instanceKlass.hpp"54#include "oops/instanceRefKlass.hpp"55#include "oops/klass.inline.hpp"56#include "oops/method.inline.hpp"57#include "oops/objArrayOop.inline.hpp"58#include "oops/oop.inline.hpp"59#include "oops/symbol.hpp"60#include "prims/jvm_misc.hpp"61#include "runtime/arguments.hpp"62#include "runtime/handles.inline.hpp"63#include "runtime/init.hpp"64#include "runtime/interfaceSupport.inline.hpp"65#include "runtime/java.hpp"66#include "runtime/javaCalls.hpp"67#include "runtime/os.hpp"68#include "runtime/perfData.hpp"69#include "runtime/threadCritical.hpp"70#include "runtime/timer.hpp"71#include "runtime/vm_version.hpp"72#include "services/management.hpp"73#include "services/threadService.hpp"74#include "utilities/classpathStream.hpp"75#include "utilities/events.hpp"76#include "utilities/hashtable.inline.hpp"77#include "utilities/macros.hpp"78#include "utilities/utf8.hpp"7980// Entry point in java.dll for path canonicalization8182typedef int (*canonicalize_fn_t)(const char *orig, char *out, int len);8384static canonicalize_fn_t CanonicalizeEntry = NULL;8586// Entry points in zip.dll for loading zip/jar file entries8788typedef void * * (*ZipOpen_t)(const char *name, char **pmsg);89typedef void (*ZipClose_t)(jzfile *zip);90typedef jzentry* (*FindEntry_t)(jzfile *zip, const char *name, jint *sizeP, jint *nameLen);91typedef jboolean (*ReadEntry_t)(jzfile *zip, jzentry *entry, unsigned char *buf, char *namebuf);92typedef jzentry* (*GetNextEntry_t)(jzfile *zip, jint n);93typedef jint (*Crc32_t)(jint crc, const jbyte *buf, jint len);9495static ZipOpen_t ZipOpen = NULL;96static ZipClose_t ZipClose = NULL;97static FindEntry_t FindEntry = NULL;98static ReadEntry_t ReadEntry = NULL;99static GetNextEntry_t GetNextEntry = NULL;100static Crc32_t Crc32 = NULL;101int ClassLoader::_libzip_loaded = 0;102103// Entry points for jimage.dll for loading jimage file entries104105static JImageOpen_t JImageOpen = NULL;106static JImageClose_t JImageClose = NULL;107static JImageFindResource_t JImageFindResource = NULL;108static JImageGetResource_t JImageGetResource = NULL;109110// JimageFile pointer, or null if exploded JDK build.111static JImageFile* JImage_file = NULL;112113// Globals114115PerfCounter* ClassLoader::_perf_accumulated_time = NULL;116PerfCounter* ClassLoader::_perf_classes_inited = NULL;117PerfCounter* ClassLoader::_perf_class_init_time = NULL;118PerfCounter* ClassLoader::_perf_class_init_selftime = NULL;119PerfCounter* ClassLoader::_perf_classes_verified = NULL;120PerfCounter* ClassLoader::_perf_class_verify_time = NULL;121PerfCounter* ClassLoader::_perf_class_verify_selftime = NULL;122PerfCounter* ClassLoader::_perf_classes_linked = NULL;123PerfCounter* ClassLoader::_perf_class_link_time = NULL;124PerfCounter* ClassLoader::_perf_class_link_selftime = NULL;125PerfCounter* ClassLoader::_perf_sys_class_lookup_time = NULL;126PerfCounter* ClassLoader::_perf_shared_classload_time = NULL;127PerfCounter* ClassLoader::_perf_sys_classload_time = NULL;128PerfCounter* ClassLoader::_perf_app_classload_time = NULL;129PerfCounter* ClassLoader::_perf_app_classload_selftime = NULL;130PerfCounter* ClassLoader::_perf_app_classload_count = NULL;131PerfCounter* ClassLoader::_perf_define_appclasses = NULL;132PerfCounter* ClassLoader::_perf_define_appclass_time = NULL;133PerfCounter* ClassLoader::_perf_define_appclass_selftime = NULL;134PerfCounter* ClassLoader::_perf_app_classfile_bytes_read = NULL;135PerfCounter* ClassLoader::_perf_sys_classfile_bytes_read = NULL;136PerfCounter* ClassLoader::_unsafe_defineClassCallCounter = NULL;137138GrowableArray<ModuleClassPathList*>* ClassLoader::_patch_mod_entries = NULL;139GrowableArray<ModuleClassPathList*>* ClassLoader::_exploded_entries = NULL;140ClassPathEntry* ClassLoader::_jrt_entry = NULL;141142ClassPathEntry* volatile ClassLoader::_first_append_entry_list = NULL;143ClassPathEntry* volatile ClassLoader::_last_append_entry = NULL;144#if INCLUDE_CDS145ClassPathEntry* ClassLoader::_app_classpath_entries = NULL;146ClassPathEntry* ClassLoader::_last_app_classpath_entry = NULL;147ClassPathEntry* ClassLoader::_module_path_entries = NULL;148ClassPathEntry* ClassLoader::_last_module_path_entry = NULL;149#endif150151// helper routines152bool string_starts_with(const char* str, const char* str_to_find) {153size_t str_len = strlen(str);154size_t str_to_find_len = strlen(str_to_find);155if (str_to_find_len > str_len) {156return false;157}158return (strncmp(str, str_to_find, str_to_find_len) == 0);159}160161static const char* get_jimage_version_string() {162static char version_string[10] = "";163if (version_string[0] == '\0') {164jio_snprintf(version_string, sizeof(version_string), "%d.%d",165VM_Version::vm_major_version(), VM_Version::vm_minor_version());166}167return (const char*)version_string;168}169170bool ClassLoader::string_ends_with(const char* str, const char* str_to_find) {171size_t str_len = strlen(str);172size_t str_to_find_len = strlen(str_to_find);173if (str_to_find_len > str_len) {174return false;175}176return (strncmp(str + (str_len - str_to_find_len), str_to_find, str_to_find_len) == 0);177}178179// Used to obtain the package name from a fully qualified class name.180Symbol* ClassLoader::package_from_class_name(const Symbol* name, bool* bad_class_name) {181if (name == NULL) {182if (bad_class_name != NULL) {183*bad_class_name = true;184}185return NULL;186}187188int utf_len = name->utf8_length();189const jbyte* base = (const jbyte*)name->base();190const jbyte* start = base;191const jbyte* end = UTF8::strrchr(start, utf_len, JVM_SIGNATURE_SLASH);192if (end == NULL) {193return NULL;194}195// Skip over '['s196if (*start == JVM_SIGNATURE_ARRAY) {197do {198start++;199} while (start < end && *start == JVM_SIGNATURE_ARRAY);200201// Fully qualified class names should not contain a 'L'.202// Set bad_class_name to true to indicate that the package name203// could not be obtained due to an error condition.204// In this situation, is_same_class_package returns false.205if (*start == JVM_SIGNATURE_CLASS) {206if (bad_class_name != NULL) {207*bad_class_name = true;208}209return NULL;210}211}212// A class name could have just the slash character in the name,213// in which case start > end214if (start >= end) {215// No package name216if (bad_class_name != NULL) {217*bad_class_name = true;218}219return NULL;220}221return SymbolTable::new_symbol(name, start - base, end - base);222}223224// Given a fully qualified package name, find its defining package in the class loader's225// package entry table.226PackageEntry* ClassLoader::get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data) {227if (pkg_name == NULL) {228return NULL;229}230PackageEntryTable* pkgEntryTable = loader_data->packages();231return pkgEntryTable->lookup_only(pkg_name);232}233234const char* ClassPathEntry::copy_path(const char* path) {235char* copy = NEW_C_HEAP_ARRAY(char, strlen(path)+1, mtClass);236strcpy(copy, path);237return copy;238}239240ClassFileStream* ClassPathDirEntry::open_stream(JavaThread* current, const char* name) {241// construct full path name242assert((_dir != NULL) && (name != NULL), "sanity");243size_t path_len = strlen(_dir) + strlen(name) + strlen(os::file_separator()) + 1;244char* path = NEW_RESOURCE_ARRAY_IN_THREAD(current, char, path_len);245int len = jio_snprintf(path, path_len, "%s%s%s", _dir, os::file_separator(), name);246assert(len == (int)(path_len - 1), "sanity");247// check if file exists248struct stat st;249if (os::stat(path, &st) == 0) {250// found file, open it251int file_handle = os::open(path, 0, 0);252if (file_handle != -1) {253// read contents into resource array254u1* buffer = NEW_RESOURCE_ARRAY(u1, st.st_size);255size_t num_read = os::read(file_handle, (char*) buffer, st.st_size);256// close file257os::close(file_handle);258// construct ClassFileStream259if (num_read == (size_t)st.st_size) {260if (UsePerfData) {261ClassLoader::perf_sys_classfile_bytes_read()->inc(num_read);262}263FREE_RESOURCE_ARRAY(char, path, path_len);264// Resource allocated265return new ClassFileStream(buffer,266st.st_size,267_dir,268ClassFileStream::verify);269}270}271}272FREE_RESOURCE_ARRAY(char, path, path_len);273return NULL;274}275276ClassPathZipEntry::ClassPathZipEntry(jzfile* zip, const char* zip_name,277bool is_boot_append, bool from_class_path_attr) : ClassPathEntry() {278_zip = zip;279_zip_name = copy_path(zip_name);280_from_class_path_attr = from_class_path_attr;281}282283ClassPathZipEntry::~ClassPathZipEntry() {284(*ZipClose)(_zip);285FREE_C_HEAP_ARRAY(char, _zip_name);286}287288u1* ClassPathZipEntry::open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate) {289// enable call to C land290ThreadToNativeFromVM ttn(current);291// check whether zip archive contains name292jint name_len;293jzentry* entry = (*FindEntry)(_zip, name, filesize, &name_len);294if (entry == NULL) return NULL;295u1* buffer;296char name_buf[128];297char* filename;298if (name_len < 128) {299filename = name_buf;300} else {301filename = NEW_RESOURCE_ARRAY(char, name_len + 1);302}303304// read contents into resource array305size_t size = (uint32_t)(*filesize);306if (nul_terminate) {307if (sizeof(size) == sizeof(uint32_t) && size == UINT_MAX) {308return NULL; // 32-bit integer overflow will occur.309}310size++;311}312buffer = NEW_RESOURCE_ARRAY(u1, size);313if (!(*ReadEntry)(_zip, entry, buffer, filename)) return NULL;314315// return result316if (nul_terminate) {317buffer[size - 1] = 0;318}319return buffer;320}321322ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {323jint filesize;324u1* buffer = open_entry(current, name, &filesize, false);325if (buffer == NULL) {326return NULL;327}328if (UsePerfData) {329ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);330}331// Resource allocated332return new ClassFileStream(buffer,333filesize,334_zip_name,335ClassFileStream::verify);336}337338// invoke function for each entry in the zip file339void ClassPathZipEntry::contents_do(void f(const char* name, void* context), void* context) {340JavaThread* thread = JavaThread::current();341HandleMark handle_mark(thread);342ThreadToNativeFromVM ttn(thread);343for (int n = 0; ; n++) {344jzentry * ze = ((*GetNextEntry)(_zip, n));345if (ze == NULL) break;346(*f)(ze->name, context);347}348}349350DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = NULL;)351352JImageFile* ClassPathImageEntry::jimage() const {353return JImage_file;354}355356JImageFile* ClassPathImageEntry::jimage_non_null() const {357assert(ClassLoader::has_jrt_entry(), "must be");358assert(jimage() != NULL, "should have been opened by ClassLoader::lookup_vm_options "359"and remained throughout normal JVM lifetime");360return jimage();361}362363void ClassPathImageEntry::close_jimage() {364if (jimage() != NULL) {365(*JImageClose)(jimage());366JImage_file = NULL;367}368}369370ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) :371ClassPathEntry() {372guarantee(jimage != NULL, "jimage file is null");373guarantee(name != NULL, "jimage file name is null");374assert(_singleton == NULL, "VM supports only one jimage");375DEBUG_ONLY(_singleton = this);376size_t len = strlen(name) + 1;377_name = copy_path(name);378}379380ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {381return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());382}383384// For a class in a named module, look it up in the jimage file using this syntax:385// /<module-name>/<package-name>/<base-class>386//387// Assumptions:388// 1. There are no unnamed modules in the jimage file.389// 2. A package is in at most one module in the jimage file.390//391ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {392jlong size;393JImageLocationRef location = (*JImageFindResource)(jimage_non_null(), "", get_jimage_version_string(), name, &size);394395if (location == 0) {396TempNewSymbol class_name = SymbolTable::new_symbol(name);397TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);398399if (pkg_name != NULL) {400if (!Universe::is_module_initialized()) {401location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);402} else {403PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);404if (package_entry != NULL) {405ResourceMark rm(current);406// Get the module name407ModuleEntry* module = package_entry->module();408assert(module != NULL, "Boot classLoader package missing module");409assert(module->is_named(), "Boot classLoader package is in unnamed module");410const char* module_name = module->name()->as_C_string();411if (module_name != NULL) {412location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);413}414}415}416}417}418if (location != 0) {419if (UsePerfData) {420ClassLoader::perf_sys_classfile_bytes_read()->inc(size);421}422char* data = NEW_RESOURCE_ARRAY(char, size);423(*JImageGetResource)(jimage_non_null(), location, data, size);424// Resource allocated425assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");426return new ClassFileStream((u1*)data,427(int)size,428_name,429ClassFileStream::verify,430true); // from_boot_loader_modules_image431}432433return NULL;434}435436JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,437const char* module_name,438const char* file_name,439jlong &size) {440return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));441}442443bool ClassPathImageEntry::is_modules_image() const {444assert(this == _singleton, "VM supports a single jimage");445assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");446return true;447}448449#if INCLUDE_CDS450void ClassLoader::exit_with_path_failure(const char* error, const char* message) {451Arguments::assert_is_dumping_archive();452tty->print_cr("Hint: enable -Xlog:class+path=info to diagnose the failure");453vm_exit_during_initialization(error, message);454}455#endif456457ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {458_module_name = module_name;459_module_first_entry = NULL;460_module_last_entry = NULL;461}462463ModuleClassPathList::~ModuleClassPathList() {464// Clean out each ClassPathEntry on list465ClassPathEntry* e = _module_first_entry;466while (e != NULL) {467ClassPathEntry* next_entry = e->next();468delete e;469e = next_entry;470}471}472473void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {474if (new_entry != NULL) {475if (_module_last_entry == NULL) {476_module_first_entry = _module_last_entry = new_entry;477} else {478_module_last_entry->set_next(new_entry);479_module_last_entry = new_entry;480}481}482}483484void ClassLoader::trace_class_path(const char* msg, const char* name) {485LogTarget(Info, class, path) lt;486if (lt.is_enabled()) {487LogStream ls(lt);488if (msg) {489ls.print("%s", msg);490}491if (name) {492if (strlen(name) < 256) {493ls.print("%s", name);494} else {495// For very long paths, we need to print each character separately,496// as print_cr() has a length limit497while (name[0] != '\0') {498ls.print("%c", name[0]);499name++;500}501}502}503ls.cr();504}505}506507void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {508const char* sys_class_path = Arguments::get_sysclasspath();509assert(sys_class_path != NULL, "System boot class path must not be NULL");510if (PrintSharedArchiveAndExit) {511// Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily512// the same as the bootcp of the shared archive.513} else {514trace_class_path("bootstrap loader class path=", sys_class_path);515}516setup_bootstrap_search_path_impl(current, sys_class_path);517}518519#if INCLUDE_CDS520void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {521Arguments::assert_is_dumping_archive();522523ResourceMark rm(current);524ClasspathStream cp_stream(class_path);525526while (cp_stream.has_next()) {527const char* path = cp_stream.get_next();528update_class_path_entry_list(current, path, false, false, false);529}530}531532void ClassLoader::add_to_module_path_entries(const char* path,533ClassPathEntry* entry) {534assert(entry != NULL, "ClassPathEntry should not be NULL");535Arguments::assert_is_dumping_archive();536537// The entry does not exist, add to the list538if (_module_path_entries == NULL) {539assert(_last_module_path_entry == NULL, "Sanity");540_module_path_entries = _last_module_path_entry = entry;541} else {542_last_module_path_entry->set_next(entry);543_last_module_path_entry = entry;544}545}546547// Add a module path to the _module_path_entries list.548void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {549Arguments::assert_is_dumping_archive();550struct stat st;551if (os::stat(path, &st) != 0) {552tty->print_cr("os::stat error %d (%s). CDS dump aborted (path was \"%s\").",553errno, os::errno_name(errno), path);554vm_exit_during_initialization();555}556// File or directory found557ClassPathEntry* new_entry = NULL;558new_entry = create_class_path_entry(current, path, &st,559false /*is_boot_append */, false /* from_class_path_attr */);560if (new_entry != NULL) {561add_to_module_path_entries(path, new_entry);562}563}564565#endif // INCLUDE_CDS566567void ClassLoader::close_jrt_image() {568// Not applicable for exploded builds569if (!ClassLoader::has_jrt_entry()) return;570_jrt_entry->close_jimage();571}572573// Construct the array of module/path pairs as specified to --patch-module574// for the boot loader to search ahead of the jimage, if the class being575// loaded is defined to a module that has been specified to --patch-module.576void ClassLoader::setup_patch_mod_entries() {577JavaThread* current = JavaThread::current();578GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();579int num_of_entries = patch_mod_args->length();580581// Set up the boot loader's _patch_mod_entries list582_patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, mtModule);583584for (int i = 0; i < num_of_entries; i++) {585const char* module_name = (patch_mod_args->at(i))->module_name();586Symbol* const module_sym = SymbolTable::new_symbol(module_name);587assert(module_sym != NULL, "Failed to obtain Symbol for module name");588ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);589590char* class_path = (patch_mod_args->at(i))->path_string();591ResourceMark rm(current);592ClasspathStream cp_stream(class_path);593594while (cp_stream.has_next()) {595const char* path = cp_stream.get_next();596struct stat st;597if (os::stat(path, &st) == 0) {598// File or directory found599ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);600// If the path specification is valid, enter it into this module's list601if (new_entry != NULL) {602module_cpl->add_to_list(new_entry);603}604}605}606607// Record the module into the list of --patch-module entries only if608// valid ClassPathEntrys have been created609if (module_cpl->module_first_entry() != NULL) {610_patch_mod_entries->push(module_cpl);611}612}613}614615// Determine whether the module has been patched via the command-line616// option --patch-module617bool ClassLoader::is_in_patch_mod_entries(Symbol* module_name) {618if (_patch_mod_entries != NULL && _patch_mod_entries->is_nonempty()) {619int table_len = _patch_mod_entries->length();620for (int i = 0; i < table_len; i++) {621ModuleClassPathList* patch_mod = _patch_mod_entries->at(i);622if (module_name->fast_compare(patch_mod->module_name()) == 0) {623return true;624}625}626}627return false;628}629630// Set up the _jrt_entry if present and boot append path631void ClassLoader::setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path) {632ResourceMark rm(current);633ClasspathStream cp_stream(class_path);634bool set_base_piece = true;635636#if INCLUDE_CDS637if (Arguments::is_dumping_archive()) {638if (!Arguments::has_jimage()) {639vm_exit_during_initialization("CDS is not supported in exploded JDK build", NULL);640}641}642#endif643644while (cp_stream.has_next()) {645const char* path = cp_stream.get_next();646647if (set_base_piece) {648// The first time through the bootstrap_search setup, it must be determined649// what the base or core piece of the boot loader search is. Either a java runtime650// image is present or this is an exploded module build situation.651assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),652"Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");653struct stat st;654if (os::stat(path, &st) == 0) {655// Directory found656if (JImage_file != NULL) {657assert(Arguments::has_jimage(), "sanity check");658const char* canonical_path = get_canonical_path(path, current);659assert(canonical_path != NULL, "canonical_path issue");660661_jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);662assert(_jrt_entry != NULL && _jrt_entry->is_modules_image(), "No java runtime image present");663assert(_jrt_entry->jimage() != NULL, "No java runtime image");664} else {665// It's an exploded build.666ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);667}668} else {669// If path does not exist, exit670vm_exit_during_initialization("Unable to establish the boot loader search path", path);671}672set_base_piece = false;673} else {674// Every entry on the system boot class path after the initial base piece,675// which is set by os::set_boot_path(), is considered an appended entry.676update_class_path_entry_list(current, path, false, true, false);677}678}679}680681// During an exploded modules build, each module defined to the boot loader682// will be added to the ClassLoader::_exploded_entries array.683void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {684assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");685assert(_exploded_entries != NULL, "_exploded_entries was not initialized");686687// Find the module's symbol688ResourceMark rm(current);689const char *module_name = module_sym->as_C_string();690const char *home = Arguments::get_java_home();691const char file_sep = os::file_separator()[0];692// 10 represents the length of "modules" + 2 file separators + \0693size_t len = strlen(home) + strlen(module_name) + 10;694char *path = NEW_RESOURCE_ARRAY(char, len);695jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);696697struct stat st;698if (os::stat(path, &st) == 0) {699// Directory found700ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);701702// If the path specification is valid, enter it into this module's list.703// There is no need to check for duplicate modules in the exploded entry list,704// since no two modules with the same name can be defined to the boot loader.705// This is checked at module definition time in Modules::define_module.706if (new_entry != NULL) {707ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);708module_cpl->add_to_list(new_entry);709{710MutexLocker ml(current, Module_lock);711_exploded_entries->push(module_cpl);712}713log_info(class, load)("path: %s", path);714}715}716}717718jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {719// enable call to C land720ThreadToNativeFromVM ttn(thread);721HandleMark hm(thread);722load_zip_library_if_needed();723return (*ZipOpen)(canonical_path, error_msg);724}725726ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,727const char *path, const struct stat* st,728bool is_boot_append,729bool from_class_path_attr) {730ClassPathEntry* new_entry = NULL;731if ((st->st_mode & S_IFMT) == S_IFREG) {732ResourceMark rm(current);733// Regular file, should be a zip file734// Canonicalized filename735const char* canonical_path = get_canonical_path(path, current);736if (canonical_path == NULL) {737return NULL;738}739char* error_msg = NULL;740jzfile* zip = open_zip_file(canonical_path, &error_msg, current);741if (zip != NULL && error_msg == NULL) {742new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr);743} else {744return NULL;745}746log_info(class, path)("opened: %s", path);747log_info(class, load)("opened: %s", path);748} else {749// Directory750new_entry = new ClassPathDirEntry(path);751log_info(class, load)("path: %s", path);752}753return new_entry;754}755756757// Create a class path zip entry for a given path (return NULL if not found758// or zip/JAR file cannot be opened)759ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {760// check for a regular file761struct stat st;762if (os::stat(path, &st) == 0) {763if ((st.st_mode & S_IFMT) == S_IFREG) {764JavaThread* thread = JavaThread::current();765ResourceMark rm(thread);766const char* canonical_path = get_canonical_path(path, thread);767if (canonical_path != NULL) {768char* error_msg = NULL;769jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);770if (zip != NULL && error_msg == NULL) {771// create using canonical path772return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false);773}774}775}776}777return NULL;778}779780// The boot append entries are added with a lock, and read lock free.781void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {782if (new_entry != NULL) {783MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);784if (_last_append_entry == NULL) {785_last_append_entry = new_entry;786assert(first_append_entry() == NULL, "boot loader's append class path entry list not empty");787Atomic::release_store(&_first_append_entry_list, new_entry);788} else {789_last_append_entry->set_next(new_entry);790_last_append_entry = new_entry;791}792}793}794795// Record the path entries specified in -cp during dump time. The recorded796// information will be used at runtime for loading the archived app classes.797//798// Note that at dump time, ClassLoader::_app_classpath_entries are NOT used for799// loading app classes. Instead, the app class are loaded by the800// jdk/internal/loader/ClassLoaders$AppClassLoader instance.801void ClassLoader::add_to_app_classpath_entries(JavaThread* current,802const char* path,803ClassPathEntry* entry,804bool check_for_duplicates) {805#if INCLUDE_CDS806assert(entry != NULL, "ClassPathEntry should not be NULL");807ClassPathEntry* e = _app_classpath_entries;808if (check_for_duplicates) {809while (e != NULL) {810if (strcmp(e->name(), entry->name()) == 0) {811// entry already exists812return;813}814e = e->next();815}816}817818// The entry does not exist, add to the list819if (_app_classpath_entries == NULL) {820assert(_last_app_classpath_entry == NULL, "Sanity");821_app_classpath_entries = _last_app_classpath_entry = entry;822} else {823_last_app_classpath_entry->set_next(entry);824_last_app_classpath_entry = entry;825}826827if (entry->is_jar_file()) {828ClassLoaderExt::process_jar_manifest(current, entry, check_for_duplicates);829}830#endif831}832833// Returns true IFF the file/dir exists and the entry was successfully created.834bool ClassLoader::update_class_path_entry_list(JavaThread* current,835const char *path,836bool check_for_duplicates,837bool is_boot_append,838bool from_class_path_attr) {839struct stat st;840if (os::stat(path, &st) == 0) {841// File or directory found842ClassPathEntry* new_entry = NULL;843new_entry = create_class_path_entry(current, path, &st, is_boot_append, from_class_path_attr);844if (new_entry == NULL) {845return false;846}847848// Do not reorder the bootclasspath which would break get_system_package().849// Add new entry to linked list850if (is_boot_append) {851add_to_boot_append_entries(new_entry);852} else {853add_to_app_classpath_entries(current, path, new_entry, check_for_duplicates);854}855return true;856} else {857return false;858}859}860861static void print_module_entry_table(const GrowableArray<ModuleClassPathList*>* const module_list) {862ResourceMark rm;863int num_of_entries = module_list->length();864for (int i = 0; i < num_of_entries; i++) {865ClassPathEntry* e;866ModuleClassPathList* mpl = module_list->at(i);867tty->print("%s=", mpl->module_name()->as_C_string());868e = mpl->module_first_entry();869while (e != NULL) {870tty->print("%s", e->name());871e = e->next();872if (e != NULL) {873tty->print("%s", os::path_separator());874}875}876tty->print(" ;");877}878}879880void ClassLoader::print_bootclasspath() {881ClassPathEntry* e;882tty->print("[bootclasspath= ");883884// Print --patch-module module/path specifications first885if (_patch_mod_entries != NULL) {886print_module_entry_table(_patch_mod_entries);887}888889// [jimage | exploded modules build]890if (has_jrt_entry()) {891// Print the location of the java runtime image892tty->print("%s ;", _jrt_entry->name());893} else {894// Print exploded module build path specifications895if (_exploded_entries != NULL) {896print_module_entry_table(_exploded_entries);897}898}899900// appended entries901e = first_append_entry();902while (e != NULL) {903tty->print("%s ;", e->name());904e = e->next();905}906tty->print_cr("]");907}908909void* ClassLoader::dll_lookup(void* lib, const char* name, const char* path) {910void* func = os::dll_lookup(lib, name);911if (func == NULL) {912char msg[256] = "";913jio_snprintf(msg, sizeof(msg), "Could not resolve \"%s\"", name);914vm_exit_during_initialization(msg, path);915}916return func;917}918919void ClassLoader::load_java_library() {920assert(CanonicalizeEntry == NULL, "should not load java library twice");921void *javalib_handle = os::native_java_library();922if (javalib_handle == NULL) {923vm_exit_during_initialization("Unable to load java library", NULL);924}925926CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, dll_lookup(javalib_handle, "JDK_Canonicalize", NULL));927}928929void ClassLoader::release_load_zip_library() {930MutexLocker locker(Zip_lock, Monitor::_no_safepoint_check_flag);931if (_libzip_loaded == 0) {932load_zip_library();933Atomic::release_store(&_libzip_loaded, 1);934}935}936937void ClassLoader::load_zip_library() {938assert(ZipOpen == NULL, "should not load zip library twice");939char path[JVM_MAXPATHLEN];940char ebuf[1024];941void* handle = NULL;942if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {943handle = os::dll_load(path, ebuf, sizeof ebuf);944}945if (handle == NULL) {946vm_exit_during_initialization("Unable to load zip library", path);947}948949ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, dll_lookup(handle, "ZIP_Open", path));950ZipClose = CAST_TO_FN_PTR(ZipClose_t, dll_lookup(handle, "ZIP_Close", path));951FindEntry = CAST_TO_FN_PTR(FindEntry_t, dll_lookup(handle, "ZIP_FindEntry", path));952ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, dll_lookup(handle, "ZIP_ReadEntry", path));953GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, dll_lookup(handle, "ZIP_GetNextEntry", path));954Crc32 = CAST_TO_FN_PTR(Crc32_t, dll_lookup(handle, "ZIP_CRC32", path));955}956957void ClassLoader::load_jimage_library() {958assert(JImageOpen == NULL, "should not load jimage library twice");959char path[JVM_MAXPATHLEN];960char ebuf[1024];961void* handle = NULL;962if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) {963handle = os::dll_load(path, ebuf, sizeof ebuf);964}965if (handle == NULL) {966vm_exit_during_initialization("Unable to load jimage library", path);967}968969JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path));970JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path));971JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path));972JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));973}974975int ClassLoader::crc32(int crc, const char* buf, int len) {976load_zip_library_if_needed();977return (*Crc32)(crc, (const jbyte*)buf, len);978}979980oop ClassLoader::get_system_package(const char* name, TRAPS) {981// Look up the name in the boot loader's package entry table.982if (name != NULL) {983TempNewSymbol package_sym = SymbolTable::new_symbol(name);984// Look for the package entry in the boot loader's package entry table.985PackageEntry* package =986ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym);987988// Return NULL if package does not exist or if no classes in that package989// have been loaded.990if (package != NULL && package->has_loaded_class()) {991ModuleEntry* module = package->module();992if (module->location() != NULL) {993ResourceMark rm(THREAD);994Handle ml = java_lang_String::create_from_str(995module->location()->as_C_string(), THREAD);996return ml();997}998// Return entry on boot loader class path.999Handle cph = java_lang_String::create_from_str(1000ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD);1001return cph();1002}1003}1004return NULL;1005}10061007objArrayOop ClassLoader::get_system_packages(TRAPS) {1008ResourceMark rm(THREAD);1009// List of pointers to PackageEntrys that have loaded classes.1010GrowableArray<PackageEntry*>* loaded_class_pkgs = new GrowableArray<PackageEntry*>(50);1011{1012MutexLocker ml(THREAD, Module_lock);10131014PackageEntryTable* pe_table =1015ClassLoaderData::the_null_class_loader_data()->packages();10161017// Collect the packages that have at least one loaded class.1018for (int x = 0; x < pe_table->table_size(); x++) {1019for (PackageEntry* package_entry = pe_table->bucket(x);1020package_entry != NULL;1021package_entry = package_entry->next()) {1022if (package_entry->has_loaded_class()) {1023loaded_class_pkgs->append(package_entry);1024}1025}1026}1027}102810291030// Allocate objArray and fill with java.lang.String1031objArrayOop r = oopFactory::new_objArray(vmClasses::String_klass(),1032loaded_class_pkgs->length(), CHECK_NULL);1033objArrayHandle result(THREAD, r);1034for (int x = 0; x < loaded_class_pkgs->length(); x++) {1035PackageEntry* package_entry = loaded_class_pkgs->at(x);1036Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL);1037result->obj_at_put(x, str());1038}1039return result();1040}10411042// caller needs ResourceMark1043const char* ClassLoader::file_name_for_class_name(const char* class_name,1044int class_name_len) {1045assert(class_name != NULL, "invariant");1046assert((int)strlen(class_name) == class_name_len, "invariant");10471048static const char class_suffix[] = ".class";1049size_t class_suffix_len = sizeof(class_suffix);10501051char* const file_name = NEW_RESOURCE_ARRAY(char,1052class_name_len +1053class_suffix_len); // includes term NULL10541055strncpy(file_name, class_name, class_name_len);1056strncpy(&file_name[class_name_len], class_suffix, class_suffix_len);10571058return file_name;1059}10601061ClassPathEntry* find_first_module_cpe(ModuleEntry* mod_entry,1062const GrowableArray<ModuleClassPathList*>* const module_list) {1063int num_of_entries = module_list->length();1064const Symbol* class_module_name = mod_entry->name();10651066// Loop through all the modules in either the patch-module or exploded entries looking for module1067for (int i = 0; i < num_of_entries; i++) {1068ModuleClassPathList* module_cpl = module_list->at(i);1069Symbol* module_cpl_name = module_cpl->module_name();10701071if (module_cpl_name->fast_compare(class_module_name) == 0) {1072// Class' module has been located.1073return module_cpl->module_first_entry();1074}1075}1076return NULL;1077}107810791080// Search either the patch-module or exploded build entries for class.1081ClassFileStream* ClassLoader::search_module_entries(JavaThread* current,1082const GrowableArray<ModuleClassPathList*>* const module_list,1083const char* const class_name,1084const char* const file_name) {1085ClassFileStream* stream = NULL;10861087// Find the class' defining module in the boot loader's module entry table1088TempNewSymbol class_name_symbol = SymbolTable::new_symbol(class_name);1089TempNewSymbol pkg_name = package_from_class_name(class_name_symbol);1090PackageEntry* pkg_entry = get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data());1091ModuleEntry* mod_entry = (pkg_entry != NULL) ? pkg_entry->module() : NULL;10921093// If the module system has not defined java.base yet, then1094// classes loaded are assumed to be defined to java.base.1095// When java.base is eventually defined by the module system,1096// all packages of classes that have been previously loaded1097// are verified in ModuleEntryTable::verify_javabase_packages().1098if (!Universe::is_module_initialized() &&1099!ModuleEntryTable::javabase_defined() &&1100mod_entry == NULL) {1101mod_entry = ModuleEntryTable::javabase_moduleEntry();1102}11031104// The module must be a named module1105ClassPathEntry* e = NULL;1106if (mod_entry != NULL && mod_entry->is_named()) {1107if (module_list == _exploded_entries) {1108// The exploded build entries can be added to at any time so a lock is1109// needed when searching them.1110assert(!ClassLoader::has_jrt_entry(), "Must be exploded build");1111MutexLocker ml(current, Module_lock);1112e = find_first_module_cpe(mod_entry, module_list);1113} else {1114e = find_first_module_cpe(mod_entry, module_list);1115}1116}11171118// Try to load the class from the module's ClassPathEntry list.1119while (e != NULL) {1120stream = e->open_stream(current, file_name);1121// No context.check is required since CDS is not supported1122// for an exploded modules build or if --patch-module is specified.1123if (NULL != stream) {1124return stream;1125}1126e = e->next();1127}1128// If the module was located, break out even if the class was not1129// located successfully from that module's ClassPathEntry list.1130// There will not be another valid entry for that module.1131return NULL;1132}11331134// Called by the boot classloader to load classes1135InstanceKlass* ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {1136assert(name != NULL, "invariant");11371138ResourceMark rm(THREAD);1139HandleMark hm(THREAD);11401141const char* const class_name = name->as_C_string();11421143EventMark m("loading class %s", class_name);11441145const char* const file_name = file_name_for_class_name(class_name,1146name->utf8_length());1147assert(file_name != NULL, "invariant");11481149// Lookup stream for parsing .class file1150ClassFileStream* stream = NULL;1151s2 classpath_index = 0;1152ClassPathEntry* e = NULL;11531154// If search_append_only is true, boot loader visibility boundaries are1155// set to be _first_append_entry to the end. This includes:1156// [-Xbootclasspath/a]; [jvmti appended entries]1157//1158// If search_append_only is false, boot loader visibility boundaries are1159// set to be the --patch-module entries plus the base piece. This includes:1160// [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]1161//11621163// Load Attempt #1: --patch-module1164// Determine the class' defining module. If it appears in the _patch_mod_entries,1165// attempt to load the class from those locations specific to the module.1166// Specifications to --patch-module can contain a partial number of classes1167// that are part of the overall module definition. So if a particular class is not1168// found within its module specification, the search should continue to Load Attempt #2.1169// Note: The --patch-module entries are never searched if the boot loader's1170// visibility boundary is limited to only searching the append entries.1171if (_patch_mod_entries != NULL && !search_append_only) {1172// At CDS dump time, the --patch-module entries are ignored. That means a1173// class is still loaded from the runtime image even if it might1174// appear in the _patch_mod_entries. The runtime shared class visibility1175// check will determine if a shared class is visible based on the runtime1176// environemnt, including the runtime --patch-module setting.1177//1178// DynamicDumpSharedSpaces requires UseSharedSpaces to be enabled. Since --patch-module1179// is not supported with UseSharedSpaces, it is not supported with DynamicDumpSharedSpaces.1180assert(!DynamicDumpSharedSpaces, "sanity");1181if (!DumpSharedSpaces) {1182stream = search_module_entries(THREAD, _patch_mod_entries, class_name, file_name);1183}1184}11851186// Load Attempt #2: [jimage | exploded build]1187if (!search_append_only && (NULL == stream)) {1188if (has_jrt_entry()) {1189e = _jrt_entry;1190stream = _jrt_entry->open_stream(THREAD, file_name);1191} else {1192// Exploded build - attempt to locate class in its defining module's location.1193assert(_exploded_entries != NULL, "No exploded build entries present");1194stream = search_module_entries(THREAD, _exploded_entries, class_name, file_name);1195}1196}11971198// Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]1199if (search_append_only && (NULL == stream)) {1200// For the boot loader append path search, the starting classpath_index1201// for the appended piece is always 1 to account for either the1202// _jrt_entry or the _exploded_entries.1203assert(classpath_index == 0, "The classpath_index has been incremented incorrectly");1204classpath_index = 1;12051206e = first_append_entry();1207while (e != NULL) {1208stream = e->open_stream(THREAD, file_name);1209if (NULL != stream) {1210break;1211}1212e = e->next();1213++classpath_index;1214}1215}12161217if (NULL == stream) {1218return NULL;1219}12201221stream->set_verify(ClassLoaderExt::should_verify(classpath_index));12221223ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();1224Handle protection_domain;1225ClassLoadInfo cl_info(protection_domain);12261227InstanceKlass* result = KlassFactory::create_from_stream(stream,1228name,1229loader_data,1230cl_info,1231CHECK_NULL);1232result->set_classpath_index(classpath_index);1233return result;1234}12351236#if INCLUDE_CDS1237char* ClassLoader::skip_uri_protocol(char* source) {1238if (strncmp(source, "file:", 5) == 0) {1239// file: protocol path could start with file:/ or file:///1240// locate the char after all the forward slashes1241int offset = 5;1242while (*(source + offset) == '/') {1243offset++;1244}1245source += offset;1246// for non-windows platforms, move back one char as the path begins with a '/'1247#ifndef _WINDOWS1248source -= 1;1249#endif1250} else if (strncmp(source, "jrt:/", 5) == 0) {1251source += 5;1252}1253return source;1254}12551256// Record the shared classpath index and loader type for classes loaded1257// by the builtin loaders at dump time.1258void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik, const ClassFileStream* stream) {1259Arguments::assert_is_dumping_archive();1260assert(stream != NULL, "sanity");12611262if (ik->is_hidden()) {1263// We do not archive hidden classes.1264return;1265}12661267oop loader = ik->class_loader();1268char* src = (char*)stream->source();1269if (src == NULL) {1270if (loader == NULL) {1271// JFR classes1272ik->set_shared_classpath_index(0);1273ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);1274}1275return;1276}12771278assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");12791280ResourceMark rm(current);1281int classpath_index = -1;1282PackageEntry* pkg_entry = ik->package();12831284if (FileMapInfo::get_number_of_shared_paths() > 0) {1285// Save the path from the file: protocol or the module name from the jrt: protocol1286// if no protocol prefix is found, path is the same as stream->source(). This path1287// must be valid since the class has been successfully parsed.1288char* path = skip_uri_protocol(src);1289assert(path != NULL, "sanity");1290for (int i = 0; i < FileMapInfo::get_number_of_shared_paths(); i++) {1291SharedClassPathEntry* ent = FileMapInfo::shared_path(i);1292// A shared path has been validated during its creation in ClassLoader::create_class_path_entry(),1293// it must be valid here.1294assert(ent->name() != NULL, "sanity");1295// If the path (from the class stream source) is the same as the shared1296// class or module path, then we have a match.1297// src may come from the App/Platform class loaders, which would canonicalize1298// the file name. We cannot use strcmp to check for equality against ent->name().1299// We must use os::same_files (which is faster than canonicalizing ent->name()).1300if (os::same_files(ent->name(), path)) {1301// NULL pkg_entry and pkg_entry in an unnamed module implies the class1302// is from the -cp or boot loader append path which consists of -Xbootclasspath/a1303// and jvmti appended entries.1304if ((pkg_entry == NULL) || (pkg_entry->in_unnamed_module())) {1305// Ensure the index is within the -cp range before assigning1306// to the classpath_index.1307if (SystemDictionary::is_system_class_loader(loader) &&1308(i >= ClassLoaderExt::app_class_paths_start_index()) &&1309(i < ClassLoaderExt::app_module_paths_start_index())) {1310classpath_index = i;1311break;1312} else {1313if ((i >= 1) &&1314(i < ClassLoaderExt::app_class_paths_start_index())) {1315// The class must be from boot loader append path which consists of1316// -Xbootclasspath/a and jvmti appended entries.1317assert(loader == NULL, "sanity");1318classpath_index = i;1319break;1320}1321}1322} else {1323// A class from a named module from the --module-path. Ensure the index is1324// within the --module-path range before assigning to the classpath_index.1325if ((pkg_entry != NULL) && !(pkg_entry->in_unnamed_module()) && (i > 0)) {1326if (i >= ClassLoaderExt::app_module_paths_start_index() &&1327i < FileMapInfo::get_number_of_shared_paths()) {1328classpath_index = i;1329break;1330}1331}1332}1333}1334// for index 0 and the stream->source() is the modules image or has the jrt: protocol.1335// The class must be from the runtime modules image.1336if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {1337classpath_index = i;1338break;1339}1340}13411342// No path entry found for this class. Must be a shared class loaded by the1343// user defined classloader.1344if (classpath_index < 0) {1345assert(ik->shared_classpath_index() < 0, "Sanity");1346ik->set_shared_classpath_index(UNREGISTERED_INDEX);1347SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);1348return;1349}1350} else {1351// The shared path table is set up after module system initialization.1352// The path table contains no entry before that. Any classes loaded prior1353// to the setup of the shared path table must be from the modules image.1354assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");1355assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");1356classpath_index = 0;1357}13581359const char* const class_name = ik->name()->as_C_string();1360const char* const file_name = file_name_for_class_name(class_name,1361ik->name()->utf8_length());1362assert(file_name != NULL, "invariant");13631364ClassLoaderExt::record_result(classpath_index, ik);1365}1366#endif // INCLUDE_CDS13671368// Initialize the class loader's access to methods in libzip. Parse and1369// process the boot classpath into a list ClassPathEntry objects. Once1370// this list has been created, it must not change order (see class PackageInfo)1371// it can be appended to and is by jvmti.13721373void ClassLoader::initialize(TRAPS) {1374if (UsePerfData) {1375// jvmstat performance counters1376NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");1377NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");1378NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");1379NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");1380NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");1381NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");1382NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");1383NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");1384NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");1385NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");13861387NEWPERFTICKCOUNTER(_perf_sys_class_lookup_time, SUN_CLS, "lookupSysClassTime");1388NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");1389NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");1390NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");1391NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");1392NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");1393NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");1394NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");1395NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");1396NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");1397NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");13981399NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");1400}14011402// lookup java library entry points1403load_java_library();1404// jimage library entry points are loaded below, in lookup_vm_options1405setup_bootstrap_search_path(THREAD);1406}14071408char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {1409jlong size;1410JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);1411if (location == 0)1412return NULL;1413char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);1414(*JImageGetResource)(jimage, location, val, size);1415val[size] = '\0';1416return val;1417}14181419// Lookup VM options embedded in the modules jimage file1420char* ClassLoader::lookup_vm_options() {1421jint error;1422char modules_path[JVM_MAXPATHLEN];1423const char* fileSep = os::file_separator();14241425// Initialize jimage library entry points1426load_jimage_library();14271428jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);1429JImage_file =(*JImageOpen)(modules_path, &error);1430if (JImage_file == NULL) {1431return NULL;1432}14331434const char *jimage_version = get_jimage_version_string();1435char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options");1436return options;1437}14381439#if INCLUDE_CDS1440void ClassLoader::initialize_shared_path(JavaThread* current) {1441if (Arguments::is_dumping_archive()) {1442ClassLoaderExt::setup_search_paths(current);1443}1444}14451446void ClassLoader::initialize_module_path(TRAPS) {1447if (Arguments::is_dumping_archive()) {1448ClassLoaderExt::setup_module_paths(THREAD);1449FileMapInfo::allocate_shared_path_table(CHECK);1450}1451}14521453// Helper function used by CDS code to get the number of module path1454// entries during shared classpath setup time.1455int ClassLoader::num_module_path_entries() {1456Arguments::assert_is_dumping_archive();1457int num_entries = 0;1458ClassPathEntry* e= ClassLoader::_module_path_entries;1459while (e != NULL) {1460num_entries ++;1461e = e->next();1462}1463return num_entries;1464}1465#endif14661467jlong ClassLoader::classloader_time_ms() {1468return UsePerfData ?1469Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;1470}14711472jlong ClassLoader::class_init_count() {1473return UsePerfData ? _perf_classes_inited->get_value() : -1;1474}14751476jlong ClassLoader::class_init_time_ms() {1477return UsePerfData ?1478Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;1479}14801481jlong ClassLoader::class_verify_time_ms() {1482return UsePerfData ?1483Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;1484}14851486jlong ClassLoader::class_link_count() {1487return UsePerfData ? _perf_classes_linked->get_value() : -1;1488}14891490jlong ClassLoader::class_link_time_ms() {1491return UsePerfData ?1492Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1;1493}14941495int ClassLoader::compute_Object_vtable() {1496// hardwired for JDK1.2 -- would need to duplicate class file parsing1497// code to determine actual value from file1498// Would be value '11' if finals were in vtable1499int JDK_1_2_Object_vtable_size = 5;1500return JDK_1_2_Object_vtable_size * vtableEntry::size();1501}150215031504void classLoader_init1() {1505EXCEPTION_MARK;1506ClassLoader::initialize(THREAD);1507if (HAS_PENDING_EXCEPTION) {1508vm_exit_during_initialization("ClassLoader::initialize() failed unexpectedly");1509}1510}15111512// Complete the ClassPathEntry setup for the boot loader1513void ClassLoader::classLoader_init2(JavaThread* current) {1514// Setup the list of module/path pairs for --patch-module processing1515// This must be done after the SymbolTable is created in order1516// to use fast_compare on module names instead of a string compare.1517if (Arguments::get_patch_mod_prefix() != NULL) {1518setup_patch_mod_entries();1519}15201521// Create the ModuleEntry for java.base (must occur after setup_patch_mod_entries1522// to successfully determine if java.base has been patched)1523create_javabase();15241525// Setup the initial java.base/path pair for the exploded build entries.1526// As more modules are defined during module system initialization, more1527// entries will be added to the exploded build array.1528if (!has_jrt_entry()) {1529assert(!DumpSharedSpaces, "DumpSharedSpaces not supported with exploded module builds");1530assert(!DynamicDumpSharedSpaces, "DynamicDumpSharedSpaces not supported with exploded module builds");1531assert(!UseSharedSpaces, "UsedSharedSpaces not supported with exploded module builds");1532// Set up the boot loader's _exploded_entries list. Note that this gets1533// done before loading any classes, by the same thread that will1534// subsequently do the first class load. So, no lock is needed for this.1535assert(_exploded_entries == NULL, "Should only get initialized once");1536_exploded_entries = new (ResourceObj::C_HEAP, mtModule)1537GrowableArray<ModuleClassPathList*>(EXPLODED_ENTRY_SIZE, mtModule);1538add_to_exploded_build_list(current, vmSymbols::java_base());1539}1540}15411542char* ClassLoader::get_canonical_path(const char* orig, Thread* thread) {1543assert(orig != NULL, "bad arguments");1544// caller needs to allocate ResourceMark for the following output buffer1545char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);1546ResourceMark rm(thread);1547// os::native_path writes into orig_copy1548char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(orig)+1);1549strcpy(orig_copy, orig);1550if ((CanonicalizeEntry)(os::native_path(orig_copy), canonical_path, JVM_MAXPATHLEN) < 0) {1551return NULL;1552}1553return canonical_path;1554}15551556void ClassLoader::create_javabase() {1557JavaThread* current = JavaThread::current();15581559// Create java.base's module entry for the boot1560// class loader prior to loading j.l.Ojbect.1561ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data();15621563// Get module entry table1564ModuleEntryTable* null_cld_modules = null_cld->modules();1565if (null_cld_modules == NULL) {1566vm_exit_during_initialization("No ModuleEntryTable for the boot class loader");1567}15681569{1570MutexLocker ml(current, Module_lock);1571if (ModuleEntryTable::javabase_moduleEntry() == NULL) { // may have been inited by CDS.1572ModuleEntry* jb_module = null_cld_modules->locked_create_entry(Handle(),1573false, vmSymbols::java_base(), NULL, NULL, null_cld);1574if (jb_module == NULL) {1575vm_exit_during_initialization("Unable to create ModuleEntry for " JAVA_BASE_NAME);1576}1577ModuleEntryTable::set_javabase_moduleEntry(jb_module);1578}1579}1580}15811582// Please keep following two functions at end of this file. With them placed at top or in middle of the file,1583// they could get inlined by agressive compiler, an unknown trick, see bug 6966589.1584void PerfClassTraceTime::initialize() {1585if (!UsePerfData) return;15861587if (_eventp != NULL) {1588// increment the event counter1589_eventp->inc();1590}15911592// stop the current active thread-local timer to measure inclusive time1593_prev_active_event = -1;1594for (int i=0; i < EVENT_TYPE_COUNT; i++) {1595if (_timers[i].is_active()) {1596assert(_prev_active_event == -1, "should have only one active timer");1597_prev_active_event = i;1598_timers[i].stop();1599}1600}16011602if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) {1603// start the inclusive timer if not recursively called1604_t.start();1605}16061607// start thread-local timer of the given event type1608if (!_timers[_event_type].is_active()) {1609_timers[_event_type].start();1610}1611}16121613PerfClassTraceTime::~PerfClassTraceTime() {1614if (!UsePerfData) return;16151616// stop the thread-local timer as the event completes1617// and resume the thread-local timer of the event next on the stack1618_timers[_event_type].stop();1619jlong selftime = _timers[_event_type].ticks();16201621if (_prev_active_event >= 0) {1622_timers[_prev_active_event].start();1623}16241625if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return;16261627// increment the counters only on the leaf call1628_t.stop();1629_timep->inc(_t.ticks());1630if (_selftimep != NULL) {1631_selftimep->inc(selftime);1632}1633// add all class loading related event selftime to the accumulated time counter1634ClassLoader::perf_accumulated_time()->inc(selftime);16351636// reset the timer1637_timers[_event_type].reset();1638}163916401641