Path: blob/master/src/hotspot/share/classfile/classLoaderExt.cpp
40949 views
/*1* Copyright (c) 2015, 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 "cds/filemap.hpp"26#include "classfile/classFileParser.hpp"27#include "classfile/classFileStream.hpp"28#include "classfile/classLoader.inline.hpp"29#include "classfile/classLoaderExt.hpp"30#include "classfile/classLoaderData.inline.hpp"31#include "classfile/classLoadInfo.hpp"32#include "classfile/klassFactory.hpp"33#include "classfile/modules.hpp"34#include "classfile/systemDictionary.hpp"35#include "classfile/systemDictionaryShared.hpp"36#include "classfile/vmSymbols.hpp"37#include "gc/shared/collectedHeap.hpp"38#include "logging/log.hpp"39#include "memory/allocation.inline.hpp"40#include "memory/resourceArea.hpp"41#include "oops/instanceKlass.hpp"42#include "oops/klass.inline.hpp"43#include "oops/oop.inline.hpp"44#include "oops/symbol.hpp"45#include "runtime/arguments.hpp"46#include "runtime/handles.inline.hpp"47#include "runtime/java.hpp"48#include "runtime/javaCalls.hpp"49#include "runtime/os.hpp"50#include "services/threadService.hpp"51#include "utilities/stringUtils.hpp"5253jshort ClassLoaderExt::_app_class_paths_start_index = ClassLoaderExt::max_classpath_index;54jshort ClassLoaderExt::_app_module_paths_start_index = ClassLoaderExt::max_classpath_index;55jshort ClassLoaderExt::_max_used_path_index = 0;56bool ClassLoaderExt::_has_app_classes = false;57bool ClassLoaderExt::_has_platform_classes = false;5859void ClassLoaderExt::append_boot_classpath(ClassPathEntry* new_entry) {60if (UseSharedSpaces) {61warning("Sharing is only supported for boot loader classes because bootstrap classpath has been appended");62FileMapInfo::current_info()->set_has_platform_or_app_classes(false);63}64ClassLoader::add_to_boot_append_entries(new_entry);65}6667void ClassLoaderExt::setup_app_search_path(JavaThread* current) {68Arguments::assert_is_dumping_archive();69_app_class_paths_start_index = ClassLoader::num_boot_classpath_entries();70char* app_class_path = os::strdup(Arguments::get_appclasspath());7172if (strcmp(app_class_path, ".") == 0) {73// This doesn't make any sense, even for AppCDS, so let's skip it. We74// don't want to throw an error here because -cp "." is usually assigned75// by the launcher when classpath is not specified.76trace_class_path("app loader class path (skipped)=", app_class_path);77} else {78trace_class_path("app loader class path=", app_class_path);79ClassLoader::setup_app_search_path(current, app_class_path);80}81}8283void ClassLoaderExt::process_module_table(JavaThread* current, ModuleEntryTable* met) {84ResourceMark rm(current);85for (int i = 0; i < met->table_size(); i++) {86for (ModuleEntry* m = met->bucket(i); m != NULL;) {87char* path = m->location()->as_C_string();88if (strncmp(path, "file:", 5) == 0) {89path = ClassLoader::skip_uri_protocol(path);90ClassLoader::setup_module_search_path(current, path);91}92m = m->next();93}94}95}96void ClassLoaderExt::setup_module_paths(JavaThread* current) {97Arguments::assert_is_dumping_archive();98_app_module_paths_start_index = ClassLoader::num_boot_classpath_entries() +99ClassLoader::num_app_classpath_entries();100Handle system_class_loader (current, SystemDictionary::java_system_loader());101ModuleEntryTable* met = Modules::get_module_entry_table(system_class_loader);102process_module_table(current, met);103}104105char* ClassLoaderExt::read_manifest(JavaThread* current, ClassPathEntry* entry,106jint *manifest_size, bool clean_text) {107const char* name = "META-INF/MANIFEST.MF";108char* manifest;109jint size;110111assert(entry->is_jar_file(), "must be");112manifest = (char*) ((ClassPathZipEntry*)entry )->open_entry(current, name, &size, true);113114if (manifest == NULL) { // No Manifest115*manifest_size = 0;116return NULL;117}118119120if (clean_text) {121// See http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#JAR%20Manifest122// (1): replace all CR/LF and CR with LF123StringUtils::replace_no_expand(manifest, "\r\n", "\n");124125// (2) remove all new-line continuation (remove all "\n " substrings)126StringUtils::replace_no_expand(manifest, "\n ", "");127}128129*manifest_size = (jint)strlen(manifest);130return manifest;131}132133char* ClassLoaderExt::get_class_path_attr(const char* jar_path, char* manifest, jint manifest_size) {134const char* tag = "Class-Path: ";135const int tag_len = (int)strlen(tag);136char* found = NULL;137char* line_start = manifest;138char* end = manifest + manifest_size;139140assert(*end == 0, "must be nul-terminated");141142while (line_start < end) {143char* line_end = strchr(line_start, '\n');144if (line_end == NULL) {145// JAR spec require the manifest file to be terminated by a new line.146break;147}148if (strncmp(tag, line_start, tag_len) == 0) {149if (found != NULL) {150// Same behavior as jdk/src/share/classes/java/util/jar/Attributes.java151// If duplicated entries are found, the last one is used.152log_warning(cds)("Warning: Duplicate name in Manifest: %s.\n"153"Ensure that the manifest does not have duplicate entries, and\n"154"that blank lines separate individual sections in both your\n"155"manifest and in the META-INF/MANIFEST.MF entry in the jar file:\n%s\n", tag, jar_path);156}157found = line_start + tag_len;158assert(found <= line_end, "sanity");159*line_end = '\0';160}161line_start = line_end + 1;162}163return found;164}165166void ClassLoaderExt::process_jar_manifest(JavaThread* current, ClassPathEntry* entry,167bool check_for_duplicates) {168ResourceMark rm(current);169jint manifest_size;170char* manifest = read_manifest(current, entry, &manifest_size);171172if (manifest == NULL) {173return;174}175176if (strstr(manifest, "Extension-List:") != NULL) {177vm_exit_during_cds_dumping(err_msg("-Xshare:dump does not support Extension-List in JAR manifest: %s", entry->name()));178}179180char* cp_attr = get_class_path_attr(entry->name(), manifest, manifest_size);181182if (cp_attr != NULL && strlen(cp_attr) > 0) {183trace_class_path("found Class-Path: ", cp_attr);184185char sep = os::file_separator()[0];186const char* dir_name = entry->name();187const char* dir_tail = strrchr(dir_name, sep);188int dir_len;189if (dir_tail == NULL) {190dir_len = 0;191} else {192dir_len = dir_tail - dir_name + 1;193}194195// Split the cp_attr by spaces, and add each file196char* file_start = cp_attr;197char* end = file_start + strlen(file_start);198199while (file_start < end) {200char* file_end = strchr(file_start, ' ');201if (file_end != NULL) {202*file_end = 0;203file_end += 1;204} else {205file_end = end;206}207208size_t name_len = strlen(file_start);209if (name_len > 0) {210ResourceMark rm(current);211size_t libname_len = dir_len + name_len;212char* libname = NEW_RESOURCE_ARRAY(char, libname_len + 1);213int n = os::snprintf(libname, libname_len + 1, "%.*s%s", dir_len, dir_name, file_start);214assert((size_t)n == libname_len, "Unexpected number of characters in string");215if (ClassLoader::update_class_path_entry_list(current, libname, true, false, true /* from_class_path_attr */)) {216trace_class_path("library = ", libname);217} else {218trace_class_path("library (non-existent) = ", libname);219FileMapInfo::record_non_existent_class_path_entry(libname);220}221}222223file_start = file_end;224}225}226}227228void ClassLoaderExt::setup_search_paths(JavaThread* current) {229ClassLoaderExt::setup_app_search_path(current);230}231232void ClassLoaderExt::record_result(const s2 classpath_index, InstanceKlass* result) {233Arguments::assert_is_dumping_archive();234235// We need to remember where the class comes from during dumping.236oop loader = result->class_loader();237s2 classloader_type = ClassLoader::BOOT_LOADER;238if (SystemDictionary::is_system_class_loader(loader)) {239classloader_type = ClassLoader::APP_LOADER;240ClassLoaderExt::set_has_app_classes();241} else if (SystemDictionary::is_platform_class_loader(loader)) {242classloader_type = ClassLoader::PLATFORM_LOADER;243ClassLoaderExt::set_has_platform_classes();244}245if (classpath_index > ClassLoaderExt::max_used_path_index()) {246ClassLoaderExt::set_max_used_path_index(classpath_index);247}248result->set_shared_classpath_index(classpath_index);249result->set_shared_class_loader_type(classloader_type);250}251252// Load the class of the given name from the location given by path. The path is specified by253// the "source:" in the class list file (see classListParser.cpp), and can be a directory or254// a JAR file.255InstanceKlass* ClassLoaderExt::load_class(Symbol* name, const char* path, TRAPS) {256assert(name != NULL, "invariant");257assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");258ResourceMark rm(THREAD);259const char* class_name = name->as_C_string();260const char* file_name = file_name_for_class_name(class_name,261name->utf8_length());262assert(file_name != NULL, "invariant");263264// Lookup stream for parsing .class file265ClassFileStream* stream = NULL;266ClassPathEntry* e = find_classpath_entry_from_cache(THREAD, path);267if (e == NULL) {268THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());269}270271{272PerfClassTraceTime vmtimer(perf_sys_class_lookup_time(),273THREAD->get_thread_stat()->perf_timers_addr(),274PerfClassTraceTime::CLASS_LOAD);275stream = e->open_stream(THREAD, file_name);276}277278if (stream == NULL) {279// open_stream could return NULL even when no exception has be thrown (JDK-8263632).280THROW_NULL(vmSymbols::java_lang_ClassNotFoundException());281return NULL;282}283stream->set_verify(true);284285ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();286Handle protection_domain;287ClassLoadInfo cl_info(protection_domain);288InstanceKlass* k = KlassFactory::create_from_stream(stream,289name,290loader_data,291cl_info,292CHECK_NULL);293return k;294}295296struct CachedClassPathEntry {297const char* _path;298ClassPathEntry* _entry;299};300301static GrowableArray<CachedClassPathEntry>* cached_path_entries = NULL;302303ClassPathEntry* ClassLoaderExt::find_classpath_entry_from_cache(JavaThread* current, const char* path) {304// This is called from dump time so it's single threaded and there's no need for a lock.305assert(DumpSharedSpaces, "this function is only used with -Xshare:dump");306if (cached_path_entries == NULL) {307cached_path_entries = new (ResourceObj::C_HEAP, mtClass) GrowableArray<CachedClassPathEntry>(20, mtClass);308}309CachedClassPathEntry ccpe;310for (int i=0; i<cached_path_entries->length(); i++) {311ccpe = cached_path_entries->at(i);312if (strcmp(ccpe._path, path) == 0) {313if (i != 0) {314// Put recent entries at the beginning to speed up searches.315cached_path_entries->remove_at(i);316cached_path_entries->insert_before(0, ccpe);317}318return ccpe._entry;319}320}321322struct stat st;323if (os::stat(path, &st) != 0) {324// File or directory not found325return NULL;326}327ClassPathEntry* new_entry = NULL;328329new_entry = create_class_path_entry(current, path, &st, false, false);330if (new_entry == NULL) {331return NULL;332}333ccpe._path = strdup(path);334ccpe._entry = new_entry;335cached_path_entries->insert_before(0, ccpe);336return new_entry;337}338339340