1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_CLASSFILE_MODULEENTRY_HPP 26 #define SHARE_VM_CLASSFILE_MODULEENTRY_HPP 27 28 #include "classfile/classLoaderData.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "oops/symbol.hpp" 31 #include "prims/jni.h" 32 #include "runtime/mutexLocker.hpp" 33 #include "trace/traceMacros.hpp" 34 #include "utilities/growableArray.hpp" 35 #include "utilities/hashtable.hpp" 36 #include "utilities/ostream.hpp" 37 38 #define UNNAMED_MODULE "Unnamed Module" 39 #define JAVA_BASE_NAME "java.base" 40 41 class ModuleClosure; 42 43 // A ModuleEntry describes a module that has been defined by a call to JVM_DefineModule. 44 // It contains: 45 // - Symbol* containing the module's name. 46 // - pointer to the java.lang.reflect.Module for this module. 47 // - pointer to the java.security.ProtectionDomain shared by classes defined to this module. 48 // - ClassLoaderData*, class loader of this module. 49 // - a growable array containg other module entries that this module can read. 50 // - a flag indicating if this module can read all unnamed modules. 51 // 52 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either 53 // data structure. 54 class ModuleEntry : public HashtableEntry<Symbol*, mtModule> { 55 private: 56 jobject _module; // java.lang.reflect.Module 57 jobject _pd; // java.security.ProtectionDomain, cached 58 // for shared classes from this module 59 ClassLoaderData* _loader_data; 60 GrowableArray<ModuleEntry*>* _reads; // list of modules that are readable by this module 61 Symbol* _version; // module version number 62 Symbol* _location; // module location 63 bool _can_read_all_unnamed; 64 bool _has_default_read_edges; // JVMTI redefine/retransform support 65 bool _must_walk_reads; // walk module's reads list at GC safepoints to purge out dead modules 66 TRACE_DEFINE_TRACE_ID_FIELD; 67 enum {MODULE_READS_SIZE = 101}; // Initial size of list of modules that the module can read. 68 69 public: 70 void init() { 71 _module = NULL; 72 _loader_data = NULL; 73 _pd = NULL; 74 _reads = NULL; 75 _version = NULL; 76 _location = NULL; 77 _can_read_all_unnamed = false; 78 _has_default_read_edges = false; 79 _must_walk_reads = false; 80 } 81 82 Symbol* name() const { return literal(); } 83 void set_name(Symbol* n) { set_literal(n); } 84 85 jobject module() const { return _module; } 86 void set_module(jobject j) { _module = j; } 87 88 // The shared ProtectionDomain reference is set once the VM loads a shared class 89 // originated from the current Module. The referenced ProtectionDomain object is 90 // created by the ClassLoader when loading a class (shared or non-shared) from the 91 // Module for the first time. This ProtectionDomain object is used for all 92 // classes from the Module loaded by the same ClassLoader. 93 Handle shared_protection_domain(); 94 void set_shared_protection_domain(ClassLoaderData *loader_data, Handle pd); 95 96 ClassLoaderData* loader_data() const { return _loader_data; } 97 void set_loader_data(ClassLoaderData* l) { _loader_data = l; } 98 99 Symbol* version() const { return _version; } 100 void set_version(Symbol* version); 101 102 Symbol* location() const { return _location; } 103 void set_location(Symbol* location); 104 bool is_non_jdk_module(); 105 106 bool can_read(ModuleEntry* m) const; 107 bool has_reads() const; 108 void add_read(ModuleEntry* m); 109 void set_read_walk_required(ClassLoaderData* m_loader_data); 110 111 bool is_named() const { return (name() != NULL); } 112 113 bool can_read_all_unnamed() const { 114 assert(is_named() || _can_read_all_unnamed == true, 115 "unnamed modules can always read all unnamed modules"); 116 return _can_read_all_unnamed; 117 } 118 119 // Modules can only go from strict to loose. 120 void set_can_read_all_unnamed() { _can_read_all_unnamed = true; } 121 122 bool has_default_read_edges() const { 123 return _has_default_read_edges; 124 } 125 126 // Sets true and returns the previous value. 127 bool set_has_default_read_edges() { 128 MutexLocker ml(Module_lock); 129 bool prev = _has_default_read_edges; 130 _has_default_read_edges = true; 131 return prev; 132 } 133 134 ModuleEntry* next() const { 135 return (ModuleEntry*)HashtableEntry<Symbol*, mtModule>::next(); 136 } 137 ModuleEntry** next_addr() { 138 return (ModuleEntry**)HashtableEntry<Symbol*, mtModule>::next_addr(); 139 } 140 141 // iteration support for readability 142 void module_reads_do(ModuleClosure* const f); 143 144 TRACE_DEFINE_TRACE_ID_METHODS; 145 146 // Purge dead weak references out of reads list when any given class loader is unloaded. 147 void purge_reads(); 148 void delete_reads(); 149 150 void print(outputStream* st = tty); 151 void verify(); 152 }; 153 154 // Iterator interface 155 class ModuleClosure: public StackObj { 156 public: 157 virtual void do_module(ModuleEntry* const module) = 0; 158 }; 159 160 161 // The ModuleEntryTable is a Hashtable containing a list of all modules defined 162 // by a particular class loader. Each module is represented as a ModuleEntry node. 163 // 164 // Each ModuleEntryTable contains a _javabase_module field which allows for the 165 // creation of java.base's ModuleEntry very early in bootstrapping before the 166 // corresponding JVM_DefineModule call for java.base occurs during module system 167 // initialization. Setting up java.base's ModuleEntry early enables classes, 168 // loaded prior to the module system being initialized to be created with their 169 // PackageEntry node's correctly pointing at java.base's ModuleEntry. No class 170 // outside of java.base is allowed to be loaded pre-module system initialization. 171 // 172 // The ModuleEntryTable's lookup is lock free. 173 // 174 class ModuleEntryTable : public Hashtable<Symbol*, mtModule> { 175 friend class VMStructs; 176 public: 177 enum Constants { 178 _moduletable_entry_size = 109 // number of entries in module entry table 179 }; 180 181 private: 182 static ModuleEntry* _javabase_module; 183 ModuleEntry* _unnamed_module; 184 185 ModuleEntry* new_entry(unsigned int hash, Handle module_handle, Symbol* name, Symbol* version, 186 Symbol* location, ClassLoaderData* loader_data); 187 void add_entry(int index, ModuleEntry* new_entry); 188 189 int entry_size() const { return BasicHashtable<mtModule>::entry_size(); } 190 191 ModuleEntry** bucket_addr(int i) { 192 return (ModuleEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i); 193 } 194 195 static unsigned int compute_hash(Symbol* name) { return ((name == NULL) ? 0 : (unsigned int)(name->identity_hash())); } 196 int index_for(Symbol* name) const { return hash_to_index(compute_hash(name)); } 197 198 public: 199 ModuleEntryTable(int table_size); 200 ~ModuleEntryTable(); 201 202 ModuleEntry* bucket(int i) { 203 return (ModuleEntry*)Hashtable<Symbol*, mtModule>::bucket(i); 204 } 205 206 // Create module in loader's module entry table, if already exists then 207 // return null. Assume Module_lock has been locked by caller. 208 ModuleEntry* locked_create_entry_or_null(Handle module_handle, 209 Symbol* module_name, 210 Symbol* module_version, 211 Symbol* module_location, 212 ClassLoaderData* loader_data); 213 214 // Only lookup module within loader's module entry table. The table read is lock-free. 215 ModuleEntry* lookup_only(Symbol* name); 216 217 // purge dead weak references out of reads list 218 void purge_all_module_reads(); 219 220 // Special handling for unnamed module, one per class loader's ModuleEntryTable 221 void create_unnamed_module(ClassLoaderData* loader_data); 222 ModuleEntry* unnamed_module() { return _unnamed_module; } 223 224 // Special handling for java.base 225 static ModuleEntry* javabase_moduleEntry() { return _javabase_module; } 226 static void set_javabase_moduleEntry(ModuleEntry* java_base) { _javabase_module = java_base; } 227 static bool javabase_defined() { return ((_javabase_module != NULL) && 228 (_javabase_module->module() != NULL)); } 229 static void finalize_javabase(Handle module_handle, Symbol* version, Symbol* location); 230 static void patch_javabase_entries(Handle module_handle); 231 232 void print(outputStream* st = tty); 233 void verify(); 234 }; 235 236 #endif // SHARE_VM_CLASSFILE_MODULEENTRY_HPP