Path: blob/master/src/hotspot/share/classfile/classLoader.cpp
40949 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 array305int size = (*filesize) + ((nul_terminate) ? 1 : 0);306buffer = NEW_RESOURCE_ARRAY(u1, size);307if (!(*ReadEntry)(_zip, entry, buffer, filename)) return NULL;308309// return result310if (nul_terminate) {311buffer[*filesize] = 0;312}313return buffer;314}315316ClassFileStream* ClassPathZipEntry::open_stream(JavaThread* current, const char* name) {317jint filesize;318u1* buffer = open_entry(current, name, &filesize, false);319if (buffer == NULL) {320return NULL;321}322if (UsePerfData) {323ClassLoader::perf_sys_classfile_bytes_read()->inc(filesize);324}325// Resource allocated326return new ClassFileStream(buffer,327filesize,328_zip_name,329ClassFileStream::verify);330}331332// invoke function for each entry in the zip file333void ClassPathZipEntry::contents_do(void f(const char* name, void* context), void* context) {334JavaThread* thread = JavaThread::current();335HandleMark handle_mark(thread);336ThreadToNativeFromVM ttn(thread);337for (int n = 0; ; n++) {338jzentry * ze = ((*GetNextEntry)(_zip, n));339if (ze == NULL) break;340(*f)(ze->name, context);341}342}343344DEBUG_ONLY(ClassPathImageEntry* ClassPathImageEntry::_singleton = NULL;)345346JImageFile* ClassPathImageEntry::jimage() const {347return JImage_file;348}349350JImageFile* ClassPathImageEntry::jimage_non_null() const {351assert(ClassLoader::has_jrt_entry(), "must be");352assert(jimage() != NULL, "should have been opened by ClassLoader::lookup_vm_options "353"and remained throughout normal JVM lifetime");354return jimage();355}356357void ClassPathImageEntry::close_jimage() {358if (jimage() != NULL) {359(*JImageClose)(jimage());360JImage_file = NULL;361}362}363364ClassPathImageEntry::ClassPathImageEntry(JImageFile* jimage, const char* name) :365ClassPathEntry() {366guarantee(jimage != NULL, "jimage file is null");367guarantee(name != NULL, "jimage file name is null");368assert(_singleton == NULL, "VM supports only one jimage");369DEBUG_ONLY(_singleton = this);370size_t len = strlen(name) + 1;371_name = copy_path(name);372}373374ClassFileStream* ClassPathImageEntry::open_stream(JavaThread* current, const char* name) {375return open_stream_for_loader(current, name, ClassLoaderData::the_null_class_loader_data());376}377378// For a class in a named module, look it up in the jimage file using this syntax:379// /<module-name>/<package-name>/<base-class>380//381// Assumptions:382// 1. There are no unnamed modules in the jimage file.383// 2. A package is in at most one module in the jimage file.384//385ClassFileStream* ClassPathImageEntry::open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {386jlong size;387JImageLocationRef location = (*JImageFindResource)(jimage_non_null(), "", get_jimage_version_string(), name, &size);388389if (location == 0) {390TempNewSymbol class_name = SymbolTable::new_symbol(name);391TempNewSymbol pkg_name = ClassLoader::package_from_class_name(class_name);392393if (pkg_name != NULL) {394if (!Universe::is_module_initialized()) {395location = (*JImageFindResource)(jimage_non_null(), JAVA_BASE_NAME, get_jimage_version_string(), name, &size);396} else {397PackageEntry* package_entry = ClassLoader::get_package_entry(pkg_name, loader_data);398if (package_entry != NULL) {399ResourceMark rm(current);400// Get the module name401ModuleEntry* module = package_entry->module();402assert(module != NULL, "Boot classLoader package missing module");403assert(module->is_named(), "Boot classLoader package is in unnamed module");404const char* module_name = module->name()->as_C_string();405if (module_name != NULL) {406location = (*JImageFindResource)(jimage_non_null(), module_name, get_jimage_version_string(), name, &size);407}408}409}410}411}412if (location != 0) {413if (UsePerfData) {414ClassLoader::perf_sys_classfile_bytes_read()->inc(size);415}416char* data = NEW_RESOURCE_ARRAY(char, size);417(*JImageGetResource)(jimage_non_null(), location, data, size);418// Resource allocated419assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be");420return new ClassFileStream((u1*)data,421(int)size,422_name,423ClassFileStream::verify,424true); // from_boot_loader_modules_image425}426427return NULL;428}429430JImageLocationRef ClassLoader::jimage_find_resource(JImageFile* jf,431const char* module_name,432const char* file_name,433jlong &size) {434return ((*JImageFindResource)(jf, module_name, get_jimage_version_string(), file_name, &size));435}436437bool ClassPathImageEntry::is_modules_image() const {438assert(this == _singleton, "VM supports a single jimage");439assert(this == (ClassPathImageEntry*)ClassLoader::get_jrt_entry(), "must be used for jrt entry");440return true;441}442443#if INCLUDE_CDS444void ClassLoader::exit_with_path_failure(const char* error, const char* message) {445Arguments::assert_is_dumping_archive();446tty->print_cr("Hint: enable -Xlog:class+path=info to diagnose the failure");447vm_exit_during_initialization(error, message);448}449#endif450451ModuleClassPathList::ModuleClassPathList(Symbol* module_name) {452_module_name = module_name;453_module_first_entry = NULL;454_module_last_entry = NULL;455}456457ModuleClassPathList::~ModuleClassPathList() {458// Clean out each ClassPathEntry on list459ClassPathEntry* e = _module_first_entry;460while (e != NULL) {461ClassPathEntry* next_entry = e->next();462delete e;463e = next_entry;464}465}466467void ModuleClassPathList::add_to_list(ClassPathEntry* new_entry) {468if (new_entry != NULL) {469if (_module_last_entry == NULL) {470_module_first_entry = _module_last_entry = new_entry;471} else {472_module_last_entry->set_next(new_entry);473_module_last_entry = new_entry;474}475}476}477478void ClassLoader::trace_class_path(const char* msg, const char* name) {479LogTarget(Info, class, path) lt;480if (lt.is_enabled()) {481LogStream ls(lt);482if (msg) {483ls.print("%s", msg);484}485if (name) {486if (strlen(name) < 256) {487ls.print("%s", name);488} else {489// For very long paths, we need to print each character separately,490// as print_cr() has a length limit491while (name[0] != '\0') {492ls.print("%c", name[0]);493name++;494}495}496}497ls.cr();498}499}500501void ClassLoader::setup_bootstrap_search_path(JavaThread* current) {502const char* sys_class_path = Arguments::get_sysclasspath();503assert(sys_class_path != NULL, "System boot class path must not be NULL");504if (PrintSharedArchiveAndExit) {505// Don't print sys_class_path - this is the bootcp of this current VM process, not necessarily506// the same as the bootcp of the shared archive.507} else {508trace_class_path("bootstrap loader class path=", sys_class_path);509}510setup_bootstrap_search_path_impl(current, sys_class_path);511}512513#if INCLUDE_CDS514void ClassLoader::setup_app_search_path(JavaThread* current, const char *class_path) {515Arguments::assert_is_dumping_archive();516517ResourceMark rm(current);518ClasspathStream cp_stream(class_path);519520while (cp_stream.has_next()) {521const char* path = cp_stream.get_next();522update_class_path_entry_list(current, path, false, false, false);523}524}525526void ClassLoader::add_to_module_path_entries(const char* path,527ClassPathEntry* entry) {528assert(entry != NULL, "ClassPathEntry should not be NULL");529Arguments::assert_is_dumping_archive();530531// The entry does not exist, add to the list532if (_module_path_entries == NULL) {533assert(_last_module_path_entry == NULL, "Sanity");534_module_path_entries = _last_module_path_entry = entry;535} else {536_last_module_path_entry->set_next(entry);537_last_module_path_entry = entry;538}539}540541// Add a module path to the _module_path_entries list.542void ClassLoader::setup_module_search_path(JavaThread* current, const char* path) {543Arguments::assert_is_dumping_archive();544struct stat st;545if (os::stat(path, &st) != 0) {546tty->print_cr("os::stat error %d (%s). CDS dump aborted (path was \"%s\").",547errno, os::errno_name(errno), path);548vm_exit_during_initialization();549}550// File or directory found551ClassPathEntry* new_entry = NULL;552new_entry = create_class_path_entry(current, path, &st,553false /*is_boot_append */, false /* from_class_path_attr */);554if (new_entry != NULL) {555add_to_module_path_entries(path, new_entry);556}557}558559#endif // INCLUDE_CDS560561void ClassLoader::close_jrt_image() {562// Not applicable for exploded builds563if (!ClassLoader::has_jrt_entry()) return;564_jrt_entry->close_jimage();565}566567// Construct the array of module/path pairs as specified to --patch-module568// for the boot loader to search ahead of the jimage, if the class being569// loaded is defined to a module that has been specified to --patch-module.570void ClassLoader::setup_patch_mod_entries() {571JavaThread* current = JavaThread::current();572GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();573int num_of_entries = patch_mod_args->length();574575// Set up the boot loader's _patch_mod_entries list576_patch_mod_entries = new (ResourceObj::C_HEAP, mtModule) GrowableArray<ModuleClassPathList*>(num_of_entries, mtModule);577578for (int i = 0; i < num_of_entries; i++) {579const char* module_name = (patch_mod_args->at(i))->module_name();580Symbol* const module_sym = SymbolTable::new_symbol(module_name);581assert(module_sym != NULL, "Failed to obtain Symbol for module name");582ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);583584char* class_path = (patch_mod_args->at(i))->path_string();585ResourceMark rm(current);586ClasspathStream cp_stream(class_path);587588while (cp_stream.has_next()) {589const char* path = cp_stream.get_next();590struct stat st;591if (os::stat(path, &st) == 0) {592// File or directory found593ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);594// If the path specification is valid, enter it into this module's list595if (new_entry != NULL) {596module_cpl->add_to_list(new_entry);597}598}599}600601// Record the module into the list of --patch-module entries only if602// valid ClassPathEntrys have been created603if (module_cpl->module_first_entry() != NULL) {604_patch_mod_entries->push(module_cpl);605}606}607}608609// Determine whether the module has been patched via the command-line610// option --patch-module611bool ClassLoader::is_in_patch_mod_entries(Symbol* module_name) {612if (_patch_mod_entries != NULL && _patch_mod_entries->is_nonempty()) {613int table_len = _patch_mod_entries->length();614for (int i = 0; i < table_len; i++) {615ModuleClassPathList* patch_mod = _patch_mod_entries->at(i);616if (module_name->fast_compare(patch_mod->module_name()) == 0) {617return true;618}619}620}621return false;622}623624// Set up the _jrt_entry if present and boot append path625void ClassLoader::setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path) {626ResourceMark rm(current);627ClasspathStream cp_stream(class_path);628bool set_base_piece = true;629630#if INCLUDE_CDS631if (Arguments::is_dumping_archive()) {632if (!Arguments::has_jimage()) {633vm_exit_during_initialization("CDS is not supported in exploded JDK build", NULL);634}635}636#endif637638while (cp_stream.has_next()) {639const char* path = cp_stream.get_next();640641if (set_base_piece) {642// The first time through the bootstrap_search setup, it must be determined643// what the base or core piece of the boot loader search is. Either a java runtime644// image is present or this is an exploded module build situation.645assert(string_ends_with(path, MODULES_IMAGE_NAME) || string_ends_with(path, JAVA_BASE_NAME),646"Incorrect boot loader search path, no java runtime image or " JAVA_BASE_NAME " exploded build");647struct stat st;648if (os::stat(path, &st) == 0) {649// Directory found650if (JImage_file != NULL) {651assert(Arguments::has_jimage(), "sanity check");652const char* canonical_path = get_canonical_path(path, current);653assert(canonical_path != NULL, "canonical_path issue");654655_jrt_entry = new ClassPathImageEntry(JImage_file, canonical_path);656assert(_jrt_entry != NULL && _jrt_entry->is_modules_image(), "No java runtime image present");657assert(_jrt_entry->jimage() != NULL, "No java runtime image");658} else {659// It's an exploded build.660ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);661}662} else {663// If path does not exist, exit664vm_exit_during_initialization("Unable to establish the boot loader search path", path);665}666set_base_piece = false;667} else {668// Every entry on the system boot class path after the initial base piece,669// which is set by os::set_boot_path(), is considered an appended entry.670update_class_path_entry_list(current, path, false, true, false);671}672}673}674675// During an exploded modules build, each module defined to the boot loader676// will be added to the ClassLoader::_exploded_entries array.677void ClassLoader::add_to_exploded_build_list(JavaThread* current, Symbol* module_sym) {678assert(!ClassLoader::has_jrt_entry(), "Exploded build not applicable");679assert(_exploded_entries != NULL, "_exploded_entries was not initialized");680681// Find the module's symbol682ResourceMark rm(current);683const char *module_name = module_sym->as_C_string();684const char *home = Arguments::get_java_home();685const char file_sep = os::file_separator()[0];686// 10 represents the length of "modules" + 2 file separators + \0687size_t len = strlen(home) + strlen(module_name) + 10;688char *path = NEW_RESOURCE_ARRAY(char, len);689jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name);690691struct stat st;692if (os::stat(path, &st) == 0) {693// Directory found694ClassPathEntry* new_entry = create_class_path_entry(current, path, &st, false, false);695696// If the path specification is valid, enter it into this module's list.697// There is no need to check for duplicate modules in the exploded entry list,698// since no two modules with the same name can be defined to the boot loader.699// This is checked at module definition time in Modules::define_module.700if (new_entry != NULL) {701ModuleClassPathList* module_cpl = new ModuleClassPathList(module_sym);702module_cpl->add_to_list(new_entry);703{704MutexLocker ml(current, Module_lock);705_exploded_entries->push(module_cpl);706}707log_info(class, load)("path: %s", path);708}709}710}711712jzfile* ClassLoader::open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread) {713// enable call to C land714ThreadToNativeFromVM ttn(thread);715HandleMark hm(thread);716load_zip_library_if_needed();717return (*ZipOpen)(canonical_path, error_msg);718}719720ClassPathEntry* ClassLoader::create_class_path_entry(JavaThread* current,721const char *path, const struct stat* st,722bool is_boot_append,723bool from_class_path_attr) {724ClassPathEntry* new_entry = NULL;725if ((st->st_mode & S_IFMT) == S_IFREG) {726ResourceMark rm(current);727// Regular file, should be a zip file728// Canonicalized filename729const char* canonical_path = get_canonical_path(path, current);730if (canonical_path == NULL) {731return NULL;732}733char* error_msg = NULL;734jzfile* zip = open_zip_file(canonical_path, &error_msg, current);735if (zip != NULL && error_msg == NULL) {736new_entry = new ClassPathZipEntry(zip, path, is_boot_append, from_class_path_attr);737} else {738return NULL;739}740log_info(class, path)("opened: %s", path);741log_info(class, load)("opened: %s", path);742} else {743// Directory744new_entry = new ClassPathDirEntry(path);745log_info(class, load)("path: %s", path);746}747return new_entry;748}749750751// Create a class path zip entry for a given path (return NULL if not found752// or zip/JAR file cannot be opened)753ClassPathZipEntry* ClassLoader::create_class_path_zip_entry(const char *path, bool is_boot_append) {754// check for a regular file755struct stat st;756if (os::stat(path, &st) == 0) {757if ((st.st_mode & S_IFMT) == S_IFREG) {758JavaThread* thread = JavaThread::current();759ResourceMark rm(thread);760const char* canonical_path = get_canonical_path(path, thread);761if (canonical_path != NULL) {762char* error_msg = NULL;763jzfile* zip = open_zip_file(canonical_path, &error_msg, thread);764if (zip != NULL && error_msg == NULL) {765// create using canonical path766return new ClassPathZipEntry(zip, canonical_path, is_boot_append, false);767}768}769}770}771return NULL;772}773774// The boot append entries are added with a lock, and read lock free.775void ClassLoader::add_to_boot_append_entries(ClassPathEntry *new_entry) {776if (new_entry != NULL) {777MutexLocker ml(Bootclasspath_lock, Mutex::_no_safepoint_check_flag);778if (_last_append_entry == NULL) {779_last_append_entry = new_entry;780assert(first_append_entry() == NULL, "boot loader's append class path entry list not empty");781Atomic::release_store(&_first_append_entry_list, new_entry);782} else {783_last_append_entry->set_next(new_entry);784_last_append_entry = new_entry;785}786}787}788789// Record the path entries specified in -cp during dump time. The recorded790// information will be used at runtime for loading the archived app classes.791//792// Note that at dump time, ClassLoader::_app_classpath_entries are NOT used for793// loading app classes. Instead, the app class are loaded by the794// jdk/internal/loader/ClassLoaders$AppClassLoader instance.795void ClassLoader::add_to_app_classpath_entries(JavaThread* current,796const char* path,797ClassPathEntry* entry,798bool check_for_duplicates) {799#if INCLUDE_CDS800assert(entry != NULL, "ClassPathEntry should not be NULL");801ClassPathEntry* e = _app_classpath_entries;802if (check_for_duplicates) {803while (e != NULL) {804if (strcmp(e->name(), entry->name()) == 0) {805// entry already exists806return;807}808e = e->next();809}810}811812// The entry does not exist, add to the list813if (_app_classpath_entries == NULL) {814assert(_last_app_classpath_entry == NULL, "Sanity");815_app_classpath_entries = _last_app_classpath_entry = entry;816} else {817_last_app_classpath_entry->set_next(entry);818_last_app_classpath_entry = entry;819}820821if (entry->is_jar_file()) {822ClassLoaderExt::process_jar_manifest(current, entry, check_for_duplicates);823}824#endif825}826827// Returns true IFF the file/dir exists and the entry was successfully created.828bool ClassLoader::update_class_path_entry_list(JavaThread* current,829const char *path,830bool check_for_duplicates,831bool is_boot_append,832bool from_class_path_attr) {833struct stat st;834if (os::stat(path, &st) == 0) {835// File or directory found836ClassPathEntry* new_entry = NULL;837new_entry = create_class_path_entry(current, path, &st, is_boot_append, from_class_path_attr);838if (new_entry == NULL) {839return false;840}841842// Do not reorder the bootclasspath which would break get_system_package().843// Add new entry to linked list844if (is_boot_append) {845add_to_boot_append_entries(new_entry);846} else {847add_to_app_classpath_entries(current, path, new_entry, check_for_duplicates);848}849return true;850} else {851return false;852}853}854855static void print_module_entry_table(const GrowableArray<ModuleClassPathList*>* const module_list) {856ResourceMark rm;857int num_of_entries = module_list->length();858for (int i = 0; i < num_of_entries; i++) {859ClassPathEntry* e;860ModuleClassPathList* mpl = module_list->at(i);861tty->print("%s=", mpl->module_name()->as_C_string());862e = mpl->module_first_entry();863while (e != NULL) {864tty->print("%s", e->name());865e = e->next();866if (e != NULL) {867tty->print("%s", os::path_separator());868}869}870tty->print(" ;");871}872}873874void ClassLoader::print_bootclasspath() {875ClassPathEntry* e;876tty->print("[bootclasspath= ");877878// Print --patch-module module/path specifications first879if (_patch_mod_entries != NULL) {880print_module_entry_table(_patch_mod_entries);881}882883// [jimage | exploded modules build]884if (has_jrt_entry()) {885// Print the location of the java runtime image886tty->print("%s ;", _jrt_entry->name());887} else {888// Print exploded module build path specifications889if (_exploded_entries != NULL) {890print_module_entry_table(_exploded_entries);891}892}893894// appended entries895e = first_append_entry();896while (e != NULL) {897tty->print("%s ;", e->name());898e = e->next();899}900tty->print_cr("]");901}902903void* ClassLoader::dll_lookup(void* lib, const char* name, const char* path) {904void* func = os::dll_lookup(lib, name);905if (func == NULL) {906char msg[256] = "";907jio_snprintf(msg, sizeof(msg), "Could not resolve \"%s\"", name);908vm_exit_during_initialization(msg, path);909}910return func;911}912913void ClassLoader::load_java_library() {914assert(CanonicalizeEntry == NULL, "should not load java library twice");915void *javalib_handle = os::native_java_library();916if (javalib_handle == NULL) {917vm_exit_during_initialization("Unable to load java library", NULL);918}919920CanonicalizeEntry = CAST_TO_FN_PTR(canonicalize_fn_t, dll_lookup(javalib_handle, "JDK_Canonicalize", NULL));921}922923void ClassLoader::release_load_zip_library() {924MutexLocker locker(Zip_lock, Monitor::_no_safepoint_check_flag);925if (_libzip_loaded == 0) {926load_zip_library();927Atomic::release_store(&_libzip_loaded, 1);928}929}930931void ClassLoader::load_zip_library() {932assert(ZipOpen == NULL, "should not load zip library twice");933char path[JVM_MAXPATHLEN];934char ebuf[1024];935void* handle = NULL;936if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "zip")) {937handle = os::dll_load(path, ebuf, sizeof ebuf);938}939if (handle == NULL) {940vm_exit_during_initialization("Unable to load zip library", path);941}942943ZipOpen = CAST_TO_FN_PTR(ZipOpen_t, dll_lookup(handle, "ZIP_Open", path));944ZipClose = CAST_TO_FN_PTR(ZipClose_t, dll_lookup(handle, "ZIP_Close", path));945FindEntry = CAST_TO_FN_PTR(FindEntry_t, dll_lookup(handle, "ZIP_FindEntry", path));946ReadEntry = CAST_TO_FN_PTR(ReadEntry_t, dll_lookup(handle, "ZIP_ReadEntry", path));947GetNextEntry = CAST_TO_FN_PTR(GetNextEntry_t, dll_lookup(handle, "ZIP_GetNextEntry", path));948Crc32 = CAST_TO_FN_PTR(Crc32_t, dll_lookup(handle, "ZIP_CRC32", path));949}950951void ClassLoader::load_jimage_library() {952assert(JImageOpen == NULL, "should not load jimage library twice");953char path[JVM_MAXPATHLEN];954char ebuf[1024];955void* handle = NULL;956if (os::dll_locate_lib(path, sizeof(path), Arguments::get_dll_dir(), "jimage")) {957handle = os::dll_load(path, ebuf, sizeof ebuf);958}959if (handle == NULL) {960vm_exit_during_initialization("Unable to load jimage library", path);961}962963JImageOpen = CAST_TO_FN_PTR(JImageOpen_t, dll_lookup(handle, "JIMAGE_Open", path));964JImageClose = CAST_TO_FN_PTR(JImageClose_t, dll_lookup(handle, "JIMAGE_Close", path));965JImageFindResource = CAST_TO_FN_PTR(JImageFindResource_t, dll_lookup(handle, "JIMAGE_FindResource", path));966JImageGetResource = CAST_TO_FN_PTR(JImageGetResource_t, dll_lookup(handle, "JIMAGE_GetResource", path));967}968969int ClassLoader::crc32(int crc, const char* buf, int len) {970load_zip_library_if_needed();971return (*Crc32)(crc, (const jbyte*)buf, len);972}973974oop ClassLoader::get_system_package(const char* name, TRAPS) {975// Look up the name in the boot loader's package entry table.976if (name != NULL) {977TempNewSymbol package_sym = SymbolTable::new_symbol(name);978// Look for the package entry in the boot loader's package entry table.979PackageEntry* package =980ClassLoaderData::the_null_class_loader_data()->packages()->lookup_only(package_sym);981982// Return NULL if package does not exist or if no classes in that package983// have been loaded.984if (package != NULL && package->has_loaded_class()) {985ModuleEntry* module = package->module();986if (module->location() != NULL) {987ResourceMark rm(THREAD);988Handle ml = java_lang_String::create_from_str(989module->location()->as_C_string(), THREAD);990return ml();991}992// Return entry on boot loader class path.993Handle cph = java_lang_String::create_from_str(994ClassLoader::classpath_entry(package->classpath_index())->name(), THREAD);995return cph();996}997}998return NULL;999}10001001objArrayOop ClassLoader::get_system_packages(TRAPS) {1002ResourceMark rm(THREAD);1003// List of pointers to PackageEntrys that have loaded classes.1004GrowableArray<PackageEntry*>* loaded_class_pkgs = new GrowableArray<PackageEntry*>(50);1005{1006MutexLocker ml(THREAD, Module_lock);10071008PackageEntryTable* pe_table =1009ClassLoaderData::the_null_class_loader_data()->packages();10101011// Collect the packages that have at least one loaded class.1012for (int x = 0; x < pe_table->table_size(); x++) {1013for (PackageEntry* package_entry = pe_table->bucket(x);1014package_entry != NULL;1015package_entry = package_entry->next()) {1016if (package_entry->has_loaded_class()) {1017loaded_class_pkgs->append(package_entry);1018}1019}1020}1021}102210231024// Allocate objArray and fill with java.lang.String1025objArrayOop r = oopFactory::new_objArray(vmClasses::String_klass(),1026loaded_class_pkgs->length(), CHECK_NULL);1027objArrayHandle result(THREAD, r);1028for (int x = 0; x < loaded_class_pkgs->length(); x++) {1029PackageEntry* package_entry = loaded_class_pkgs->at(x);1030Handle str = java_lang_String::create_from_symbol(package_entry->name(), CHECK_NULL);1031result->obj_at_put(x, str());1032}1033return result();1034}10351036// caller needs ResourceMark1037const char* ClassLoader::file_name_for_class_name(const char* class_name,1038int class_name_len) {1039assert(class_name != NULL, "invariant");1040assert((int)strlen(class_name) == class_name_len, "invariant");10411042static const char class_suffix[] = ".class";1043size_t class_suffix_len = sizeof(class_suffix);10441045char* const file_name = NEW_RESOURCE_ARRAY(char,1046class_name_len +1047class_suffix_len); // includes term NULL10481049strncpy(file_name, class_name, class_name_len);1050strncpy(&file_name[class_name_len], class_suffix, class_suffix_len);10511052return file_name;1053}10541055ClassPathEntry* find_first_module_cpe(ModuleEntry* mod_entry,1056const GrowableArray<ModuleClassPathList*>* const module_list) {1057int num_of_entries = module_list->length();1058const Symbol* class_module_name = mod_entry->name();10591060// Loop through all the modules in either the patch-module or exploded entries looking for module1061for (int i = 0; i < num_of_entries; i++) {1062ModuleClassPathList* module_cpl = module_list->at(i);1063Symbol* module_cpl_name = module_cpl->module_name();10641065if (module_cpl_name->fast_compare(class_module_name) == 0) {1066// Class' module has been located.1067return module_cpl->module_first_entry();1068}1069}1070return NULL;1071}107210731074// Search either the patch-module or exploded build entries for class.1075ClassFileStream* ClassLoader::search_module_entries(JavaThread* current,1076const GrowableArray<ModuleClassPathList*>* const module_list,1077const char* const class_name,1078const char* const file_name) {1079ClassFileStream* stream = NULL;10801081// Find the class' defining module in the boot loader's module entry table1082TempNewSymbol class_name_symbol = SymbolTable::new_symbol(class_name);1083TempNewSymbol pkg_name = package_from_class_name(class_name_symbol);1084PackageEntry* pkg_entry = get_package_entry(pkg_name, ClassLoaderData::the_null_class_loader_data());1085ModuleEntry* mod_entry = (pkg_entry != NULL) ? pkg_entry->module() : NULL;10861087// If the module system has not defined java.base yet, then1088// classes loaded are assumed to be defined to java.base.1089// When java.base is eventually defined by the module system,1090// all packages of classes that have been previously loaded1091// are verified in ModuleEntryTable::verify_javabase_packages().1092if (!Universe::is_module_initialized() &&1093!ModuleEntryTable::javabase_defined() &&1094mod_entry == NULL) {1095mod_entry = ModuleEntryTable::javabase_moduleEntry();1096}10971098// The module must be a named module1099ClassPathEntry* e = NULL;1100if (mod_entry != NULL && mod_entry->is_named()) {1101if (module_list == _exploded_entries) {1102// The exploded build entries can be added to at any time so a lock is1103// needed when searching them.1104assert(!ClassLoader::has_jrt_entry(), "Must be exploded build");1105MutexLocker ml(current, Module_lock);1106e = find_first_module_cpe(mod_entry, module_list);1107} else {1108e = find_first_module_cpe(mod_entry, module_list);1109}1110}11111112// Try to load the class from the module's ClassPathEntry list.1113while (e != NULL) {1114stream = e->open_stream(current, file_name);1115// No context.check is required since CDS is not supported1116// for an exploded modules build or if --patch-module is specified.1117if (NULL != stream) {1118return stream;1119}1120e = e->next();1121}1122// If the module was located, break out even if the class was not1123// located successfully from that module's ClassPathEntry list.1124// There will not be another valid entry for that module.1125return NULL;1126}11271128// Called by the boot classloader to load classes1129InstanceKlass* ClassLoader::load_class(Symbol* name, bool search_append_only, TRAPS) {1130assert(name != NULL, "invariant");11311132ResourceMark rm(THREAD);1133HandleMark hm(THREAD);11341135const char* const class_name = name->as_C_string();11361137EventMark m("loading class %s", class_name);11381139const char* const file_name = file_name_for_class_name(class_name,1140name->utf8_length());1141assert(file_name != NULL, "invariant");11421143// Lookup stream for parsing .class file1144ClassFileStream* stream = NULL;1145s2 classpath_index = 0;1146ClassPathEntry* e = NULL;11471148// If search_append_only is true, boot loader visibility boundaries are1149// set to be _first_append_entry to the end. This includes:1150// [-Xbootclasspath/a]; [jvmti appended entries]1151//1152// If search_append_only is false, boot loader visibility boundaries are1153// set to be the --patch-module entries plus the base piece. This includes:1154// [--patch-module=<module>=<file>(<pathsep><file>)*]; [jimage | exploded module build]1155//11561157// Load Attempt #1: --patch-module1158// Determine the class' defining module. If it appears in the _patch_mod_entries,1159// attempt to load the class from those locations specific to the module.1160// Specifications to --patch-module can contain a partial number of classes1161// that are part of the overall module definition. So if a particular class is not1162// found within its module specification, the search should continue to Load Attempt #2.1163// Note: The --patch-module entries are never searched if the boot loader's1164// visibility boundary is limited to only searching the append entries.1165if (_patch_mod_entries != NULL && !search_append_only) {1166// At CDS dump time, the --patch-module entries are ignored. That means a1167// class is still loaded from the runtime image even if it might1168// appear in the _patch_mod_entries. The runtime shared class visibility1169// check will determine if a shared class is visible based on the runtime1170// environemnt, including the runtime --patch-module setting.1171//1172// DynamicDumpSharedSpaces requires UseSharedSpaces to be enabled. Since --patch-module1173// is not supported with UseSharedSpaces, it is not supported with DynamicDumpSharedSpaces.1174assert(!DynamicDumpSharedSpaces, "sanity");1175if (!DumpSharedSpaces) {1176stream = search_module_entries(THREAD, _patch_mod_entries, class_name, file_name);1177}1178}11791180// Load Attempt #2: [jimage | exploded build]1181if (!search_append_only && (NULL == stream)) {1182if (has_jrt_entry()) {1183e = _jrt_entry;1184stream = _jrt_entry->open_stream(THREAD, file_name);1185} else {1186// Exploded build - attempt to locate class in its defining module's location.1187assert(_exploded_entries != NULL, "No exploded build entries present");1188stream = search_module_entries(THREAD, _exploded_entries, class_name, file_name);1189}1190}11911192// Load Attempt #3: [-Xbootclasspath/a]; [jvmti appended entries]1193if (search_append_only && (NULL == stream)) {1194// For the boot loader append path search, the starting classpath_index1195// for the appended piece is always 1 to account for either the1196// _jrt_entry or the _exploded_entries.1197assert(classpath_index == 0, "The classpath_index has been incremented incorrectly");1198classpath_index = 1;11991200e = first_append_entry();1201while (e != NULL) {1202stream = e->open_stream(THREAD, file_name);1203if (NULL != stream) {1204break;1205}1206e = e->next();1207++classpath_index;1208}1209}12101211if (NULL == stream) {1212return NULL;1213}12141215stream->set_verify(ClassLoaderExt::should_verify(classpath_index));12161217ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();1218Handle protection_domain;1219ClassLoadInfo cl_info(protection_domain);12201221InstanceKlass* result = KlassFactory::create_from_stream(stream,1222name,1223loader_data,1224cl_info,1225CHECK_NULL);1226result->set_classpath_index(classpath_index);1227return result;1228}12291230#if INCLUDE_CDS1231char* ClassLoader::skip_uri_protocol(char* source) {1232if (strncmp(source, "file:", 5) == 0) {1233// file: protocol path could start with file:/ or file:///1234// locate the char after all the forward slashes1235int offset = 5;1236while (*(source + offset) == '/') {1237offset++;1238}1239source += offset;1240// for non-windows platforms, move back one char as the path begins with a '/'1241#ifndef _WINDOWS1242source -= 1;1243#endif1244} else if (strncmp(source, "jrt:/", 5) == 0) {1245source += 5;1246}1247return source;1248}12491250// Record the shared classpath index and loader type for classes loaded1251// by the builtin loaders at dump time.1252void ClassLoader::record_result(JavaThread* current, InstanceKlass* ik, const ClassFileStream* stream) {1253Arguments::assert_is_dumping_archive();1254assert(stream != NULL, "sanity");12551256if (ik->is_hidden()) {1257// We do not archive hidden classes.1258return;1259}12601261oop loader = ik->class_loader();1262char* src = (char*)stream->source();1263if (src == NULL) {1264if (loader == NULL) {1265// JFR classes1266ik->set_shared_classpath_index(0);1267ik->set_shared_class_loader_type(ClassLoader::BOOT_LOADER);1268}1269return;1270}12711272assert(has_jrt_entry(), "CDS dumping does not support exploded JDK build");12731274ResourceMark rm(current);1275int classpath_index = -1;1276PackageEntry* pkg_entry = ik->package();12771278if (FileMapInfo::get_number_of_shared_paths() > 0) {1279// Save the path from the file: protocol or the module name from the jrt: protocol1280// if no protocol prefix is found, path is the same as stream->source(). This path1281// must be valid since the class has been successfully parsed.1282char* path = skip_uri_protocol(src);1283assert(path != NULL, "sanity");1284for (int i = 0; i < FileMapInfo::get_number_of_shared_paths(); i++) {1285SharedClassPathEntry* ent = FileMapInfo::shared_path(i);1286// A shared path has been validated during its creation in ClassLoader::create_class_path_entry(),1287// it must be valid here.1288assert(ent->name() != NULL, "sanity");1289// If the path (from the class stream source) is the same as the shared1290// class or module path, then we have a match.1291// src may come from the App/Platform class loaders, which would canonicalize1292// the file name. We cannot use strcmp to check for equality against ent->name().1293// We must use os::same_files (which is faster than canonicalizing ent->name()).1294if (os::same_files(ent->name(), path)) {1295// NULL pkg_entry and pkg_entry in an unnamed module implies the class1296// is from the -cp or boot loader append path which consists of -Xbootclasspath/a1297// and jvmti appended entries.1298if ((pkg_entry == NULL) || (pkg_entry->in_unnamed_module())) {1299// Ensure the index is within the -cp range before assigning1300// to the classpath_index.1301if (SystemDictionary::is_system_class_loader(loader) &&1302(i >= ClassLoaderExt::app_class_paths_start_index()) &&1303(i < ClassLoaderExt::app_module_paths_start_index())) {1304classpath_index = i;1305break;1306} else {1307if ((i >= 1) &&1308(i < ClassLoaderExt::app_class_paths_start_index())) {1309// The class must be from boot loader append path which consists of1310// -Xbootclasspath/a and jvmti appended entries.1311assert(loader == NULL, "sanity");1312classpath_index = i;1313break;1314}1315}1316} else {1317// A class from a named module from the --module-path. Ensure the index is1318// within the --module-path range before assigning to the classpath_index.1319if ((pkg_entry != NULL) && !(pkg_entry->in_unnamed_module()) && (i > 0)) {1320if (i >= ClassLoaderExt::app_module_paths_start_index() &&1321i < FileMapInfo::get_number_of_shared_paths()) {1322classpath_index = i;1323break;1324}1325}1326}1327}1328// for index 0 and the stream->source() is the modules image or has the jrt: protocol.1329// The class must be from the runtime modules image.1330if (i == 0 && (stream->from_boot_loader_modules_image() || string_starts_with(src, "jrt:"))) {1331classpath_index = i;1332break;1333}1334}13351336// No path entry found for this class. Must be a shared class loaded by the1337// user defined classloader.1338if (classpath_index < 0) {1339assert(ik->shared_classpath_index() < 0, "Sanity");1340ik->set_shared_classpath_index(UNREGISTERED_INDEX);1341SystemDictionaryShared::set_shared_class_misc_info(ik, (ClassFileStream*)stream);1342return;1343}1344} else {1345// The shared path table is set up after module system initialization.1346// The path table contains no entry before that. Any classes loaded prior1347// to the setup of the shared path table must be from the modules image.1348assert(stream->from_boot_loader_modules_image(), "stream must be loaded by boot loader from modules image");1349assert(FileMapInfo::get_number_of_shared_paths() == 0, "shared path table must not have been setup");1350classpath_index = 0;1351}13521353const char* const class_name = ik->name()->as_C_string();1354const char* const file_name = file_name_for_class_name(class_name,1355ik->name()->utf8_length());1356assert(file_name != NULL, "invariant");13571358ClassLoaderExt::record_result(classpath_index, ik);1359}1360#endif // INCLUDE_CDS13611362// Initialize the class loader's access to methods in libzip. Parse and1363// process the boot classpath into a list ClassPathEntry objects. Once1364// this list has been created, it must not change order (see class PackageInfo)1365// it can be appended to and is by jvmti.13661367void ClassLoader::initialize(TRAPS) {1368if (UsePerfData) {1369// jvmstat performance counters1370NEWPERFTICKCOUNTER(_perf_accumulated_time, SUN_CLS, "time");1371NEWPERFTICKCOUNTER(_perf_class_init_time, SUN_CLS, "classInitTime");1372NEWPERFTICKCOUNTER(_perf_class_init_selftime, SUN_CLS, "classInitTime.self");1373NEWPERFTICKCOUNTER(_perf_class_verify_time, SUN_CLS, "classVerifyTime");1374NEWPERFTICKCOUNTER(_perf_class_verify_selftime, SUN_CLS, "classVerifyTime.self");1375NEWPERFTICKCOUNTER(_perf_class_link_time, SUN_CLS, "classLinkedTime");1376NEWPERFTICKCOUNTER(_perf_class_link_selftime, SUN_CLS, "classLinkedTime.self");1377NEWPERFEVENTCOUNTER(_perf_classes_inited, SUN_CLS, "initializedClasses");1378NEWPERFEVENTCOUNTER(_perf_classes_linked, SUN_CLS, "linkedClasses");1379NEWPERFEVENTCOUNTER(_perf_classes_verified, SUN_CLS, "verifiedClasses");13801381NEWPERFTICKCOUNTER(_perf_sys_class_lookup_time, SUN_CLS, "lookupSysClassTime");1382NEWPERFTICKCOUNTER(_perf_shared_classload_time, SUN_CLS, "sharedClassLoadTime");1383NEWPERFTICKCOUNTER(_perf_sys_classload_time, SUN_CLS, "sysClassLoadTime");1384NEWPERFTICKCOUNTER(_perf_app_classload_time, SUN_CLS, "appClassLoadTime");1385NEWPERFTICKCOUNTER(_perf_app_classload_selftime, SUN_CLS, "appClassLoadTime.self");1386NEWPERFEVENTCOUNTER(_perf_app_classload_count, SUN_CLS, "appClassLoadCount");1387NEWPERFTICKCOUNTER(_perf_define_appclasses, SUN_CLS, "defineAppClasses");1388NEWPERFTICKCOUNTER(_perf_define_appclass_time, SUN_CLS, "defineAppClassTime");1389NEWPERFTICKCOUNTER(_perf_define_appclass_selftime, SUN_CLS, "defineAppClassTime.self");1390NEWPERFBYTECOUNTER(_perf_app_classfile_bytes_read, SUN_CLS, "appClassBytes");1391NEWPERFBYTECOUNTER(_perf_sys_classfile_bytes_read, SUN_CLS, "sysClassBytes");13921393NEWPERFEVENTCOUNTER(_unsafe_defineClassCallCounter, SUN_CLS, "unsafeDefineClassCalls");1394}13951396// lookup java library entry points1397load_java_library();1398// jimage library entry points are loaded below, in lookup_vm_options1399setup_bootstrap_search_path(THREAD);1400}14011402char* lookup_vm_resource(JImageFile *jimage, const char *jimage_version, const char *path) {1403jlong size;1404JImageLocationRef location = (*JImageFindResource)(jimage, "java.base", jimage_version, path, &size);1405if (location == 0)1406return NULL;1407char *val = NEW_C_HEAP_ARRAY(char, size+1, mtClass);1408(*JImageGetResource)(jimage, location, val, size);1409val[size] = '\0';1410return val;1411}14121413// Lookup VM options embedded in the modules jimage file1414char* ClassLoader::lookup_vm_options() {1415jint error;1416char modules_path[JVM_MAXPATHLEN];1417const char* fileSep = os::file_separator();14181419// Initialize jimage library entry points1420load_jimage_library();14211422jio_snprintf(modules_path, JVM_MAXPATHLEN, "%s%slib%smodules", Arguments::get_java_home(), fileSep, fileSep);1423JImage_file =(*JImageOpen)(modules_path, &error);1424if (JImage_file == NULL) {1425return NULL;1426}14271428const char *jimage_version = get_jimage_version_string();1429char *options = lookup_vm_resource(JImage_file, jimage_version, "jdk/internal/vm/options");1430return options;1431}14321433#if INCLUDE_CDS1434void ClassLoader::initialize_shared_path(JavaThread* current) {1435if (Arguments::is_dumping_archive()) {1436ClassLoaderExt::setup_search_paths(current);1437}1438}14391440void ClassLoader::initialize_module_path(TRAPS) {1441if (Arguments::is_dumping_archive()) {1442ClassLoaderExt::setup_module_paths(THREAD);1443FileMapInfo::allocate_shared_path_table(CHECK);1444}1445}14461447// Helper function used by CDS code to get the number of module path1448// entries during shared classpath setup time.1449int ClassLoader::num_module_path_entries() {1450Arguments::assert_is_dumping_archive();1451int num_entries = 0;1452ClassPathEntry* e= ClassLoader::_module_path_entries;1453while (e != NULL) {1454num_entries ++;1455e = e->next();1456}1457return num_entries;1458}1459#endif14601461jlong ClassLoader::classloader_time_ms() {1462return UsePerfData ?1463Management::ticks_to_ms(_perf_accumulated_time->get_value()) : -1;1464}14651466jlong ClassLoader::class_init_count() {1467return UsePerfData ? _perf_classes_inited->get_value() : -1;1468}14691470jlong ClassLoader::class_init_time_ms() {1471return UsePerfData ?1472Management::ticks_to_ms(_perf_class_init_time->get_value()) : -1;1473}14741475jlong ClassLoader::class_verify_time_ms() {1476return UsePerfData ?1477Management::ticks_to_ms(_perf_class_verify_time->get_value()) : -1;1478}14791480jlong ClassLoader::class_link_count() {1481return UsePerfData ? _perf_classes_linked->get_value() : -1;1482}14831484jlong ClassLoader::class_link_time_ms() {1485return UsePerfData ?1486Management::ticks_to_ms(_perf_class_link_time->get_value()) : -1;1487}14881489int ClassLoader::compute_Object_vtable() {1490// hardwired for JDK1.2 -- would need to duplicate class file parsing1491// code to determine actual value from file1492// Would be value '11' if finals were in vtable1493int JDK_1_2_Object_vtable_size = 5;1494return JDK_1_2_Object_vtable_size * vtableEntry::size();1495}149614971498void classLoader_init1() {1499EXCEPTION_MARK;1500ClassLoader::initialize(THREAD);1501if (HAS_PENDING_EXCEPTION) {1502vm_exit_during_initialization("ClassLoader::initialize() failed unexpectedly");1503}1504}15051506// Complete the ClassPathEntry setup for the boot loader1507void ClassLoader::classLoader_init2(JavaThread* current) {1508// Setup the list of module/path pairs for --patch-module processing1509// This must be done after the SymbolTable is created in order1510// to use fast_compare on module names instead of a string compare.1511if (Arguments::get_patch_mod_prefix() != NULL) {1512setup_patch_mod_entries();1513}15141515// Create the ModuleEntry for java.base (must occur after setup_patch_mod_entries1516// to successfully determine if java.base has been patched)1517create_javabase();15181519// Setup the initial java.base/path pair for the exploded build entries.1520// As more modules are defined during module system initialization, more1521// entries will be added to the exploded build array.1522if (!has_jrt_entry()) {1523assert(!DumpSharedSpaces, "DumpSharedSpaces not supported with exploded module builds");1524assert(!DynamicDumpSharedSpaces, "DynamicDumpSharedSpaces not supported with exploded module builds");1525assert(!UseSharedSpaces, "UsedSharedSpaces not supported with exploded module builds");1526// Set up the boot loader's _exploded_entries list. Note that this gets1527// done before loading any classes, by the same thread that will1528// subsequently do the first class load. So, no lock is needed for this.1529assert(_exploded_entries == NULL, "Should only get initialized once");1530_exploded_entries = new (ResourceObj::C_HEAP, mtModule)1531GrowableArray<ModuleClassPathList*>(EXPLODED_ENTRY_SIZE, mtModule);1532add_to_exploded_build_list(current, vmSymbols::java_base());1533}1534}15351536char* ClassLoader::get_canonical_path(const char* orig, Thread* thread) {1537assert(orig != NULL, "bad arguments");1538// caller needs to allocate ResourceMark for the following output buffer1539char* canonical_path = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, JVM_MAXPATHLEN);1540ResourceMark rm(thread);1541// os::native_path writes into orig_copy1542char* orig_copy = NEW_RESOURCE_ARRAY_IN_THREAD(thread, char, strlen(orig)+1);1543strcpy(orig_copy, orig);1544if ((CanonicalizeEntry)(os::native_path(orig_copy), canonical_path, JVM_MAXPATHLEN) < 0) {1545return NULL;1546}1547return canonical_path;1548}15491550void ClassLoader::create_javabase() {1551JavaThread* current = JavaThread::current();15521553// Create java.base's module entry for the boot1554// class loader prior to loading j.l.Ojbect.1555ClassLoaderData* null_cld = ClassLoaderData::the_null_class_loader_data();15561557// Get module entry table1558ModuleEntryTable* null_cld_modules = null_cld->modules();1559if (null_cld_modules == NULL) {1560vm_exit_during_initialization("No ModuleEntryTable for the boot class loader");1561}15621563{1564MutexLocker ml(current, Module_lock);1565if (ModuleEntryTable::javabase_moduleEntry() == NULL) { // may have been inited by CDS.1566ModuleEntry* jb_module = null_cld_modules->locked_create_entry(Handle(),1567false, vmSymbols::java_base(), NULL, NULL, null_cld);1568if (jb_module == NULL) {1569vm_exit_during_initialization("Unable to create ModuleEntry for " JAVA_BASE_NAME);1570}1571ModuleEntryTable::set_javabase_moduleEntry(jb_module);1572}1573}1574}15751576// Please keep following two functions at end of this file. With them placed at top or in middle of the file,1577// they could get inlined by agressive compiler, an unknown trick, see bug 6966589.1578void PerfClassTraceTime::initialize() {1579if (!UsePerfData) return;15801581if (_eventp != NULL) {1582// increment the event counter1583_eventp->inc();1584}15851586// stop the current active thread-local timer to measure inclusive time1587_prev_active_event = -1;1588for (int i=0; i < EVENT_TYPE_COUNT; i++) {1589if (_timers[i].is_active()) {1590assert(_prev_active_event == -1, "should have only one active timer");1591_prev_active_event = i;1592_timers[i].stop();1593}1594}15951596if (_recursion_counters == NULL || (_recursion_counters[_event_type])++ == 0) {1597// start the inclusive timer if not recursively called1598_t.start();1599}16001601// start thread-local timer of the given event type1602if (!_timers[_event_type].is_active()) {1603_timers[_event_type].start();1604}1605}16061607PerfClassTraceTime::~PerfClassTraceTime() {1608if (!UsePerfData) return;16091610// stop the thread-local timer as the event completes1611// and resume the thread-local timer of the event next on the stack1612_timers[_event_type].stop();1613jlong selftime = _timers[_event_type].ticks();16141615if (_prev_active_event >= 0) {1616_timers[_prev_active_event].start();1617}16181619if (_recursion_counters != NULL && --(_recursion_counters[_event_type]) > 0) return;16201621// increment the counters only on the leaf call1622_t.stop();1623_timep->inc(_t.ticks());1624if (_selftimep != NULL) {1625_selftimep->inc(selftime);1626}1627// add all class loading related event selftime to the accumulated time counter1628ClassLoader::perf_accumulated_time()->inc(selftime);16291630// reset the timer1631_timers[_event_type].reset();1632}163316341635