Path: blob/master/src/hotspot/share/classfile/moduleEntry.hpp
40949 views
/*1* Copyright (c) 2016, 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_MODULEENTRY_HPP25#define SHARE_CLASSFILE_MODULEENTRY_HPP2627#include "jni.h"28#include "oops/oopHandle.hpp"29#include "oops/symbol.hpp"30#include "runtime/mutexLocker.hpp"31#include "utilities/growableArray.hpp"32#include "utilities/hashtable.hpp"33#include "utilities/macros.hpp"34#include "utilities/ostream.hpp"35#if INCLUDE_JFR36#include "jfr/support/jfrTraceIdExtension.hpp"37#endif3839#define UNNAMED_MODULE "unnamed module"40#define UNNAMED_MODULE_LEN 1441#define JAVAPKG "java"42#define JAVAPKG_LEN 443#define JAVA_BASE_NAME "java.base"44#define JAVA_BASE_NAME_LEN 94546template <class T> class Array;47class ClassLoaderData;48class MetaspaceClosure;49class ModuleClosure;5051// A ModuleEntry describes a module that has been defined by a call to JVM_DefineModule.52// It contains:53// - Symbol* containing the module's name.54// - pointer to the java.lang.Module for this module.55// - pointer to the java.security.ProtectionDomain shared by classes defined to this module.56// - ClassLoaderData*, class loader of this module.57// - a growable array containg other module entries that this module can read.58// - a flag indicating if this module can read all unnamed modules.59//60// The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either61// data structure.62class ModuleEntry : public HashtableEntry<Symbol*, mtModule> {63private:64OopHandle _module; // java.lang.Module65OopHandle _shared_pd; // java.security.ProtectionDomain, cached66// for shared classes from this module67ClassLoaderData* _loader_data;68GrowableArray<ModuleEntry*>* _reads; // list of modules that are readable by this module69Symbol* _version; // module version number70Symbol* _location; // module location71CDS_ONLY(int _shared_path_index;) // >=0 if classes in this module are in CDS archive72bool _can_read_all_unnamed;73bool _has_default_read_edges; // JVMTI redefine/retransform support74bool _must_walk_reads; // walk module's reads list at GC safepoints to purge out dead modules75bool _is_open; // whether the packages in the module are all unqualifiedly exported76bool _is_patched; // whether the module is patched via --patch-module77CDS_JAVA_HEAP_ONLY(int _archived_module_index;)7879JFR_ONLY(DEFINE_TRACE_ID_FIELD;)80enum {MODULE_READS_SIZE = 101}; // Initial size of list of modules that the module can read.8182public:83void init() {84_module = OopHandle();85_shared_pd = OopHandle();86_loader_data = NULL;87_reads = NULL;88_version = NULL;89_location = NULL;90_can_read_all_unnamed = false;91_has_default_read_edges = false;92_must_walk_reads = false;93_is_patched = false;94_is_open = false;95CDS_ONLY(_shared_path_index = -1);96}9798Symbol* name() const { return literal(); }99void set_name(Symbol* n) { set_literal(n); }100101oop module() const;102OopHandle module_handle() const { return _module; }103void set_module(OopHandle j) { _module = j; }104105// The shared ProtectionDomain reference is set once the VM loads a shared class106// originated from the current Module. The referenced ProtectionDomain object is107// created by the ClassLoader when loading a class (shared or non-shared) from the108// Module for the first time. This ProtectionDomain object is used for all109// classes from the Module loaded by the same ClassLoader.110oop shared_protection_domain();111void set_shared_protection_domain(ClassLoaderData *loader_data, Handle pd);112113ClassLoaderData* loader_data() const { return _loader_data; }114void set_loader_data(ClassLoaderData* cld);115116Symbol* version() const { return _version; }117void set_version(Symbol* version);118119Symbol* location() const { return _location; }120void set_location(Symbol* location);121bool should_show_version();122123bool can_read(ModuleEntry* m) const;124bool has_reads_list() const;125void add_read(ModuleEntry* m);126void set_read_walk_required(ClassLoaderData* m_loader_data);127128bool is_open() const { return _is_open; }129void set_is_open(bool is_open);130131bool is_named() const { return (name() != NULL); }132133bool can_read_all_unnamed() const {134assert(is_named() || _can_read_all_unnamed == true,135"unnamed modules can always read all unnamed modules");136return _can_read_all_unnamed;137}138139// Modules can only go from strict to loose.140void set_can_read_all_unnamed() { _can_read_all_unnamed = true; }141142bool has_default_read_edges() const {143return _has_default_read_edges;144}145146// Sets true and returns the previous value.147bool set_has_default_read_edges() {148MutexLocker ml(Module_lock);149bool prev = _has_default_read_edges;150_has_default_read_edges = true;151return prev;152}153154void set_is_patched() {155_is_patched = true;156CDS_ONLY(_shared_path_index = -1); // Mark all shared classes in this module invisible.157}158bool is_patched() {159return _is_patched;160}161162ModuleEntry* next() const {163return (ModuleEntry*)HashtableEntry<Symbol*, mtModule>::next();164}165ModuleEntry** next_addr() {166return (ModuleEntry**)HashtableEntry<Symbol*, mtModule>::next_addr();167}168169// iteration support for readability170void module_reads_do(ModuleClosure* const f);171172// Purge dead weak references out of reads list when any given class loader is unloaded.173void purge_reads();174void delete_reads();175176// Special handling for unnamed module, one per class loader177static ModuleEntry* create_unnamed_module(ClassLoaderData* cld);178static ModuleEntry* create_boot_unnamed_module(ClassLoaderData* cld);179static ModuleEntry* new_unnamed_module_entry(Handle module_handle, ClassLoaderData* cld);180void delete_unnamed_module();181182void print(outputStream* st = tty);183void verify();184185CDS_ONLY(int shared_path_index() { return _shared_path_index;})186187JFR_ONLY(DEFINE_TRACE_ID_METHODS;)188189#if INCLUDE_CDS_JAVA_HEAP190void iterate_symbols(MetaspaceClosure* closure);191ModuleEntry* allocate_archived_entry() const;192void init_as_archived_entry();193void init_archived_oops();194static ModuleEntry* get_archived_entry(ModuleEntry* orig_entry);195static Array<ModuleEntry*>* write_growable_array(GrowableArray<ModuleEntry*>* array);196static GrowableArray<ModuleEntry*>* restore_growable_array(Array<ModuleEntry*>* archived_array);197void load_from_archive(ClassLoaderData* loader_data);198void restore_archived_oops(ClassLoaderData* loader_data);199void clear_archived_oops();200#endif201};202203// Iterator interface204class ModuleClosure: public StackObj {205public:206virtual void do_module(ModuleEntry* module) = 0;207};208209210// The ModuleEntryTable is a Hashtable containing a list of all modules defined211// by a particular class loader. Each module is represented as a ModuleEntry node.212//213// Each ModuleEntryTable contains a _javabase_module field which allows for the214// creation of java.base's ModuleEntry very early in bootstrapping before the215// corresponding JVM_DefineModule call for java.base occurs during module system216// initialization. Setting up java.base's ModuleEntry early enables classes,217// loaded prior to the module system being initialized to be created with their218// PackageEntry node's correctly pointing at java.base's ModuleEntry. No class219// outside of java.base is allowed to be loaded pre-module system initialization.220//221// The ModuleEntryTable's lookup is lock free.222//223class ModuleEntryTable : public Hashtable<Symbol*, mtModule> {224friend class VMStructs;225public:226enum Constants {227_moduletable_entry_size = 109 // number of entries in module entry table228};229230private:231static ModuleEntry* _javabase_module;232233ModuleEntry* new_entry(unsigned int hash, Handle module_handle, bool is_open,234Symbol* name, Symbol* version, Symbol* location, ClassLoaderData* loader_data);235void add_entry(int index, ModuleEntry* new_entry);236237int entry_size() const { return BasicHashtable<mtModule>::entry_size(); }238239ModuleEntry** bucket_addr(int i) {240return (ModuleEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i);241}242243static unsigned int compute_hash(Symbol* name) { return ((name == NULL) ? 0 : (unsigned int)(name->identity_hash())); }244int index_for(Symbol* name) const { return hash_to_index(compute_hash(name)); }245246public:247ModuleEntryTable(int table_size);248~ModuleEntryTable();249250ModuleEntry* bucket(int i) {251return (ModuleEntry*)Hashtable<Symbol*, mtModule>::bucket(i);252}253254// Create module in loader's module entry table. Assume Module_lock255// has been locked by caller.256ModuleEntry* locked_create_entry(Handle module_handle,257bool is_open,258Symbol* module_name,259Symbol* module_version,260Symbol* module_location,261ClassLoaderData* loader_data);262263// Only lookup module within loader's module entry table. The table read is lock-free.264ModuleEntry* lookup_only(Symbol* name);265266// purge dead weak references out of reads list267void purge_all_module_reads();268269// Special handling for java.base270static ModuleEntry* javabase_moduleEntry() { return _javabase_module; }271static void set_javabase_moduleEntry(ModuleEntry* java_base) {272assert(_javabase_module == NULL, "_javabase_module is already defined");273_javabase_module = java_base;274}275276static bool javabase_defined() { return ((_javabase_module != NULL) &&277(_javabase_module->module() != NULL)); }278static void finalize_javabase(Handle module_handle, Symbol* version, Symbol* location);279static void patch_javabase_entries(Handle module_handle);280281void print(outputStream* st = tty);282void verify();283284#if INCLUDE_CDS_JAVA_HEAP285void iterate_symbols(MetaspaceClosure* closure);286Array<ModuleEntry*>* allocate_archived_entries();287void init_archived_entries(Array<ModuleEntry*>* archived_modules);288void init_archived_oops(Array<ModuleEntry*>* archived_modules);289void load_archived_entries(ClassLoaderData* loader_data,290Array<ModuleEntry*>* archived_modules);291void restore_archived_oops(ClassLoaderData* loader_data,292Array<ModuleEntry*>* archived_modules);293#endif294};295296#endif // SHARE_CLASSFILE_MODULEENTRY_HPP297298299