Path: blob/master/src/hotspot/share/classfile/classLoader.hpp
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#ifndef SHARE_CLASSFILE_CLASSLOADER_HPP25#define SHARE_CLASSFILE_CLASSLOADER_HPP2627#include "jimage.hpp"28#include "runtime/handles.hpp"29#include "runtime/perfDataTypes.hpp"30#include "utilities/exceptions.hpp"31#include "utilities/macros.hpp"3233// The VM class loader.34#include <sys/stat.h>3536// Name of boot "modules" image37#define MODULES_IMAGE_NAME "modules"3839// Class path entry (directory or zip file)4041class JImageFile;42class ClassFileStream;43class PackageEntry;44template <typename T> class GrowableArray;4546class ClassPathEntry : public CHeapObj<mtClass> {47private:48ClassPathEntry* volatile _next;49protected:50const char* copy_path(const char*path);51public:52ClassPathEntry* next() const;53virtual ~ClassPathEntry() {}54void set_next(ClassPathEntry* next);5556virtual bool is_modules_image() const { return false; }57virtual bool is_jar_file() const { return false; }58// Is this entry created from the "Class-path" attribute from a JAR Manifest?59virtual bool from_class_path_attr() const { return false; }60virtual const char* name() const = 0;61virtual JImageFile* jimage() const { return NULL; }62virtual void close_jimage() {}63// Constructor64ClassPathEntry() : _next(NULL) {}65// Attempt to locate file_name through this class path entry.66// Returns a class file parsing stream if successfull.67virtual ClassFileStream* open_stream(JavaThread* current, const char* name) = 0;68// Open the stream for a specific class loader69virtual ClassFileStream* open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data) {70return open_stream(current, name);71}72};7374class ClassPathDirEntry: public ClassPathEntry {75private:76const char* _dir; // Name of directory77public:78const char* name() const { return _dir; }79ClassPathDirEntry(const char* dir) {80_dir = copy_path(dir);81}82virtual ~ClassPathDirEntry() {}83ClassFileStream* open_stream(JavaThread* current, const char* name);84};8586// Type definitions for zip file and zip file entry87typedef void* jzfile;88typedef struct {89char *name; /* entry name */90jlong time; /* modification time */91jlong size; /* size of uncompressed data */92jlong csize; /* size of compressed data (zero if uncompressed) */93jint crc; /* crc of uncompressed data */94char *comment; /* optional zip file comment */95jbyte *extra; /* optional extra data */96jlong pos; /* position of LOC header (if negative) or data */97} jzentry;9899class ClassPathZipEntry: public ClassPathEntry {100private:101jzfile* _zip; // The zip archive102const char* _zip_name; // Name of zip archive103bool _from_class_path_attr; // From the "Class-path" attribute of a jar file104public:105bool is_jar_file() const { return true; }106bool from_class_path_attr() const { return _from_class_path_attr; }107const char* name() const { return _zip_name; }108ClassPathZipEntry(jzfile* zip, const char* zip_name, bool is_boot_append, bool from_class_path_attr);109virtual ~ClassPathZipEntry();110u1* open_entry(JavaThread* current, const char* name, jint* filesize, bool nul_terminate);111ClassFileStream* open_stream(JavaThread* current, const char* name);112void contents_do(void f(const char* name, void* context), void* context);113};114115116// For java image files117class ClassPathImageEntry: public ClassPathEntry {118private:119const char* _name;120DEBUG_ONLY(static ClassPathImageEntry* _singleton;)121public:122bool is_modules_image() const;123const char* name() const { return _name == NULL ? "" : _name; }124JImageFile* jimage() const;125JImageFile* jimage_non_null() const;126void close_jimage();127ClassPathImageEntry(JImageFile* jimage, const char* name);128virtual ~ClassPathImageEntry() { ShouldNotReachHere(); }129ClassFileStream* open_stream(JavaThread* current, const char* name);130ClassFileStream* open_stream_for_loader(JavaThread* current, const char* name, ClassLoaderData* loader_data);131};132133// ModuleClassPathList contains a linked list of ClassPathEntry's134// that have been specified for a specific module. Currently,135// the only way to specify a module/path pair is via the --patch-module136// command line option.137class ModuleClassPathList : public CHeapObj<mtClass> {138private:139Symbol* _module_name;140// First and last entries of class path entries for a specific module141ClassPathEntry* _module_first_entry;142ClassPathEntry* _module_last_entry;143public:144Symbol* module_name() const { return _module_name; }145ClassPathEntry* module_first_entry() const { return _module_first_entry; }146ModuleClassPathList(Symbol* module_name);147~ModuleClassPathList();148void add_to_list(ClassPathEntry* new_entry);149};150151class ClassLoader: AllStatic {152public:153enum ClassLoaderType {154BOOT_LOADER = 1, /* boot loader */155PLATFORM_LOADER = 2, /* PlatformClassLoader */156APP_LOADER = 3 /* AppClassLoader */157};158protected:159160// Performance counters161static PerfCounter* _perf_accumulated_time;162static PerfCounter* _perf_classes_inited;163static PerfCounter* _perf_class_init_time;164static PerfCounter* _perf_class_init_selftime;165static PerfCounter* _perf_classes_verified;166static PerfCounter* _perf_class_verify_time;167static PerfCounter* _perf_class_verify_selftime;168static PerfCounter* _perf_classes_linked;169static PerfCounter* _perf_class_link_time;170static PerfCounter* _perf_class_link_selftime;171static PerfCounter* _perf_sys_class_lookup_time;172static PerfCounter* _perf_shared_classload_time;173static PerfCounter* _perf_sys_classload_time;174static PerfCounter* _perf_app_classload_time;175static PerfCounter* _perf_app_classload_selftime;176static PerfCounter* _perf_app_classload_count;177static PerfCounter* _perf_define_appclasses;178static PerfCounter* _perf_define_appclass_time;179static PerfCounter* _perf_define_appclass_selftime;180static PerfCounter* _perf_app_classfile_bytes_read;181static PerfCounter* _perf_sys_classfile_bytes_read;182183static PerfCounter* _unsafe_defineClassCallCounter;184185// The boot class path consists of 3 ordered pieces:186// 1. the module/path pairs specified to --patch-module187// --patch-module=<module>=<file>(<pathsep><file>)*188// 2. the base piece189// [jimage | build with exploded modules]190// 3. boot loader append path191// [-Xbootclasspath/a]; [jvmti appended entries]192//193// The boot loader must obey this order when attempting194// to load a class.195196// 1. Contains the module/path pairs specified to --patch-module197static GrowableArray<ModuleClassPathList*>* _patch_mod_entries;198199// 2. the base piece200// Contains the ClassPathEntry of the modular java runtime image.201// If no java runtime image is present, this indicates a202// build with exploded modules is being used instead.203static ClassPathEntry* _jrt_entry;204static GrowableArray<ModuleClassPathList*>* _exploded_entries;205enum { EXPLODED_ENTRY_SIZE = 80 }; // Initial number of exploded modules206207// 3. the boot loader's append path208// [-Xbootclasspath/a]; [jvmti appended entries]209// Note: boot loader append path does not support named modules.210static ClassPathEntry* volatile _first_append_entry_list;211static ClassPathEntry* first_append_entry() {212return Atomic::load_acquire(&_first_append_entry_list);213}214215// Last entry in linked list of appended ClassPathEntry instances216static ClassPathEntry* volatile _last_append_entry;217218// Info used by CDS219CDS_ONLY(static ClassPathEntry* _app_classpath_entries;)220CDS_ONLY(static ClassPathEntry* _last_app_classpath_entry;)221CDS_ONLY(static ClassPathEntry* _module_path_entries;)222CDS_ONLY(static ClassPathEntry* _last_module_path_entry;)223CDS_ONLY(static void setup_app_search_path(JavaThread* current, const char* class_path);)224CDS_ONLY(static void setup_module_search_path(JavaThread* current, const char* path);)225static void add_to_app_classpath_entries(JavaThread* current,226const char* path,227ClassPathEntry* entry,228bool check_for_duplicates);229CDS_ONLY(static void add_to_module_path_entries(const char* path,230ClassPathEntry* entry);)231public:232CDS_ONLY(static ClassPathEntry* app_classpath_entries() {return _app_classpath_entries;})233CDS_ONLY(static ClassPathEntry* module_path_entries() {return _module_path_entries;})234235static bool has_bootclasspath_append() { return first_append_entry() != NULL; }236237protected:238// Initialization:239// - setup the boot loader's system class path240// - setup the boot loader's patch mod entries, if present241// - create the ModuleEntry for java.base242static void setup_bootstrap_search_path(JavaThread* current);243static void setup_bootstrap_search_path_impl(JavaThread* current, const char *class_path);244static void setup_patch_mod_entries();245static void create_javabase();246247static void* dll_lookup(void* lib, const char* name, const char* path);248static void load_java_library();249static void load_zip_library();250static void load_jimage_library();251252private:253static int _libzip_loaded; // used to sync loading zip.254static void release_load_zip_library();255static inline void load_zip_library_if_needed();256static jzfile* open_zip_file(const char* canonical_path, char** error_msg, JavaThread* thread);257258public:259static ClassPathEntry* create_class_path_entry(JavaThread* current,260const char *path, const struct stat* st,261bool is_boot_append,262bool from_class_path_attr);263264// Canonicalizes path names, so strcmp will work properly. This is mainly265// to avoid confusing the zip library266static char* get_canonical_path(const char* orig, Thread* thread);267static const char* file_name_for_class_name(const char* class_name,268int class_name_len);269static PackageEntry* get_package_entry(Symbol* pkg_name, ClassLoaderData* loader_data);270static int crc32(int crc, const char* buf, int len);271static bool update_class_path_entry_list(JavaThread* current,272const char *path,273bool check_for_duplicates,274bool is_boot_append,275bool from_class_path_attr);276static void print_bootclasspath();277278// Timing279static PerfCounter* perf_accumulated_time() { return _perf_accumulated_time; }280static PerfCounter* perf_classes_inited() { return _perf_classes_inited; }281static PerfCounter* perf_class_init_time() { return _perf_class_init_time; }282static PerfCounter* perf_class_init_selftime() { return _perf_class_init_selftime; }283static PerfCounter* perf_classes_verified() { return _perf_classes_verified; }284static PerfCounter* perf_class_verify_time() { return _perf_class_verify_time; }285static PerfCounter* perf_class_verify_selftime() { return _perf_class_verify_selftime; }286static PerfCounter* perf_classes_linked() { return _perf_classes_linked; }287static PerfCounter* perf_class_link_time() { return _perf_class_link_time; }288static PerfCounter* perf_class_link_selftime() { return _perf_class_link_selftime; }289static PerfCounter* perf_sys_class_lookup_time() { return _perf_sys_class_lookup_time; }290static PerfCounter* perf_shared_classload_time() { return _perf_shared_classload_time; }291static PerfCounter* perf_sys_classload_time() { return _perf_sys_classload_time; }292static PerfCounter* perf_app_classload_time() { return _perf_app_classload_time; }293static PerfCounter* perf_app_classload_selftime() { return _perf_app_classload_selftime; }294static PerfCounter* perf_app_classload_count() { return _perf_app_classload_count; }295static PerfCounter* perf_define_appclasses() { return _perf_define_appclasses; }296static PerfCounter* perf_define_appclass_time() { return _perf_define_appclass_time; }297static PerfCounter* perf_define_appclass_selftime() { return _perf_define_appclass_selftime; }298static PerfCounter* perf_app_classfile_bytes_read() { return _perf_app_classfile_bytes_read; }299static PerfCounter* perf_sys_classfile_bytes_read() { return _perf_sys_classfile_bytes_read; }300301// Record how many calls to Unsafe_DefineClass302static PerfCounter* unsafe_defineClassCallCounter() {303return _unsafe_defineClassCallCounter;304}305306// Modular java runtime image is present vs. a build with exploded modules307static bool has_jrt_entry() { return (_jrt_entry != NULL); }308static ClassPathEntry* get_jrt_entry() { return _jrt_entry; }309static void close_jrt_image();310311// Add a module's exploded directory to the boot loader's exploded module build list312static void add_to_exploded_build_list(JavaThread* current, Symbol* module_name);313314// Attempt load of individual class from either the patched or exploded modules build lists315static ClassFileStream* search_module_entries(JavaThread* current,316const GrowableArray<ModuleClassPathList*>* const module_list,317const char* const class_name,318const char* const file_name);319320// Load individual .class file321static InstanceKlass* load_class(Symbol* class_name, bool search_append_only, TRAPS);322323// If the specified package has been loaded by the system, then returns324// the name of the directory or ZIP file that the package was loaded from.325// Returns null if the package was not loaded.326// Note: The specified name can either be the name of a class or package.327// If a package name is specified, then it must be "/"-separator and also328// end with a trailing "/".329static oop get_system_package(const char* name, TRAPS);330331// Returns an array of Java strings representing all of the currently332// loaded system packages.333// Note: The package names returned are "/"-separated and end with a334// trailing "/".335static objArrayOop get_system_packages(TRAPS);336337// Initialization338static void initialize(TRAPS);339static void classLoader_init2(JavaThread* current);340CDS_ONLY(static void initialize_shared_path(JavaThread* current);)341CDS_ONLY(static void initialize_module_path(TRAPS);)342343static int compute_Object_vtable();344345static ClassPathEntry* classpath_entry(int n);346347static bool is_in_patch_mod_entries(Symbol* module_name);348349#if INCLUDE_CDS350// Sharing dump and restore351352// Helper function used by CDS code to get the number of boot classpath353// entries during shared classpath setup time.354static int num_boot_classpath_entries();355356static ClassPathEntry* get_next_boot_classpath_entry(ClassPathEntry* e);357358// Helper function used by CDS code to get the number of app classpath359// entries during shared classpath setup time.360static int num_app_classpath_entries();361362// Helper function used by CDS code to get the number of module path363// entries during shared classpath setup time.364static int num_module_path_entries();365static void exit_with_path_failure(const char* error, const char* message);366static char* skip_uri_protocol(char* source);367static void record_result(JavaThread* current, InstanceKlass* ik, const ClassFileStream* stream);368#endif369370static char* lookup_vm_options();371372static JImageLocationRef jimage_find_resource(JImageFile* jf, const char* module_name,373const char* file_name, jlong &size);374375static void trace_class_path(const char* msg, const char* name = NULL);376377// VM monitoring and management support378static jlong classloader_time_ms();379static jlong class_method_total_size();380static jlong class_init_count();381static jlong class_init_time_ms();382static jlong class_verify_time_ms();383static jlong class_link_count();384static jlong class_link_time_ms();385386// adds a class path to the boot append entries387static void add_to_boot_append_entries(ClassPathEntry* new_entry);388389// creates a class path zip entry (returns NULL if JAR file cannot be opened)390static ClassPathZipEntry* create_class_path_zip_entry(const char *apath, bool is_boot_append);391392static bool string_ends_with(const char* str, const char* str_to_find);393394// Extract package name from a fully qualified class name395// *bad_class_name is set to true if there's a problem with parsing class_name, to396// distinguish from a class_name with no package name, as both cases have a NULL return value397static Symbol* package_from_class_name(const Symbol* class_name, bool* bad_class_name = NULL);398399// Debugging400static void verify() PRODUCT_RETURN;401};402403// PerfClassTraceTime is used to measure time for class loading related events.404// This class tracks cumulative time and exclusive time for specific event types.405// During the execution of one event, other event types (e.g. class loading and406// resolution) as well as recursive calls of the same event type could happen.407// Only one elapsed timer (cumulative) and one thread-local self timer (exclusive)408// (i.e. only one event type) are active at a time even multiple PerfClassTraceTime409// instances have been created as multiple events are happening.410class PerfClassTraceTime {411public:412enum {413CLASS_LOAD = 0,414CLASS_LINK = 1,415CLASS_VERIFY = 2,416CLASS_CLINIT = 3,417DEFINE_CLASS = 4,418EVENT_TYPE_COUNT = 5419};420protected:421// _t tracks time from initialization to destruction of this timer instance422// including time for all other event types, and recursive calls of this type.423// When a timer is called recursively, the elapsedTimer _t would not be used.424elapsedTimer _t;425PerfLongCounter* _timep;426PerfLongCounter* _selftimep;427PerfLongCounter* _eventp;428// pointer to thread-local recursion counter and timer array429// The thread_local timers track cumulative time for specific event types430// exclusive of time for other event types, but including recursive calls431// of the same type.432int* _recursion_counters;433elapsedTimer* _timers;434int _event_type;435int _prev_active_event;436437public:438439inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */440PerfLongCounter* selftimep, /* counter incremented with exclusive time */441PerfLongCounter* eventp, /* event counter */442int* recursion_counters, /* thread-local recursion counter array */443elapsedTimer* timers, /* thread-local timer array */444int type /* event type */ ) :445_timep(timep), _selftimep(selftimep), _eventp(eventp), _recursion_counters(recursion_counters), _timers(timers), _event_type(type) {446initialize();447}448449inline PerfClassTraceTime(PerfLongCounter* timep, /* counter incremented with inclusive time */450elapsedTimer* timers, /* thread-local timer array */451int type /* event type */ ) :452_timep(timep), _selftimep(NULL), _eventp(NULL), _recursion_counters(NULL), _timers(timers), _event_type(type) {453initialize();454}455456~PerfClassTraceTime();457void initialize();458};459460#endif // SHARE_CLASSFILE_CLASSLOADER_HPP461462463